LIGHT2

Dependencies:   FastPWM mbed

Committer:
Lightvalve
Date:
Tue Nov 24 08:15:29 2020 +0000
Revision:
175:2f7289dbd488
Parent:
174:c828479f53f9
Child:
176:589ea3edcf3c
201124-3

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Lightvalve 175:2f7289dbd488 1 //201124_3
GiJeongKim 0:51c43836c1d7 2 #include "mbed.h"
GiJeongKim 0:51c43836c1d7 3 #include "FastPWM.h"
GiJeongKim 0:51c43836c1d7 4 #include "INIT_HW.h"
jobuuu 2:a1c0a37df760 5 #include "function_CAN.h"
GiJeongKim 0:51c43836c1d7 6 #include "SPI_EEP_ENC.h"
GiJeongKim 0:51c43836c1d7 7 #include "I2C_AS5510.h"
GiJeongKim 0:51c43836c1d7 8 #include "setting.h"
Lightvalve 11:82d8768d7351 9 #include "function_utilities.h"
Lightvalve 16:903b5a4433b4 10 #include "stm32f4xx_flash.h"
Lightvalve 169:645207e160ca 11 #include "FlashWriter.h"
Lightvalve 170:42c938a40313 12 #include <string>
Lightvalve 170:42c938a40313 13 #include <iostream>
Lightvalve 61:bc8c8270f0ab 14
Lightvalve 61:bc8c8270f0ab 15 using namespace std;
Lightvalve 61:bc8c8270f0ab 16 Timer t;
GiJeongKim 0:51c43836c1d7 17
Lightvalve 31:66738bfecec5 18 ///191008////
Lightvalve 31:66738bfecec5 19
jobuuu 7:e9086c72bb22 20 // dac & check ///////////////////////////////////////////
GiJeongKim 0:51c43836c1d7 21 DigitalOut check(PC_2);
GiJeongKim 0:51c43836c1d7 22 DigitalOut check_2(PC_3);
GiJeongKim 0:51c43836c1d7 23 AnalogOut dac_1(PA_4);
GiJeongKim 0:51c43836c1d7 24 AnalogOut dac_2(PA_5);
Lightvalve 14:8e7590227d22 25 AnalogIn adc1(PC_4); //pressure_1
Lightvalve 14:8e7590227d22 26 AnalogIn adc2(PB_0); //pressure_2
Lightvalve 14:8e7590227d22 27 AnalogIn adc3(PC_1); //current
GiJeongKim 0:51c43836c1d7 28
Lightvalve 24:ef6e1092e9e6 29
jobuuu 7:e9086c72bb22 30 // PWM ///////////////////////////////////////////
Lightvalve 30:8d561f16383b 31 float dtc_v=0.0f;
Lightvalve 30:8d561f16383b 32 float dtc_w=0.0f;
GiJeongKim 0:51c43836c1d7 33
jobuuu 7:e9086c72bb22 34 // I2C ///////////////////////////////////////////
GiJeongKim 0:51c43836c1d7 35 I2C i2c(PC_9,PA_8); // SDA, SCL (for K22F)
GiJeongKim 0:51c43836c1d7 36 const int i2c_slave_addr1 = 0x56;
GiJeongKim 0:51c43836c1d7 37 unsigned int value; // 10bit output of reading sensor AS5510
GiJeongKim 0:51c43836c1d7 38
jobuuu 7:e9086c72bb22 39 // SPI ///////////////////////////////////////////
Lightvalve 170:42c938a40313 40 SPI eeprom(PB_15, PB_14, PB_13); // EEPROM //(SPI_MOSI, SPI_MISO, SPI_SCK);
Lightvalve 170:42c938a40313 41 DigitalOut eeprom_cs(PB_12);
Lightvalve 16:903b5a4433b4 42 //FlashWriter writer(6);//2부터 7까지 되는듯 아마 sector
GiJeongKim 0:51c43836c1d7 43 SPI enc(PC_12,PC_11,PC_10);
GiJeongKim 0:51c43836c1d7 44 DigitalOut enc_cs(PD_2);
Lightvalve 11:82d8768d7351 45 DigitalOut LED(PA_15);
GiJeongKim 0:51c43836c1d7 46
jobuuu 7:e9086c72bb22 47 // UART ///////////////////////////////////////////
GiJeongKim 0:51c43836c1d7 48 Serial pc(PA_9,PA_10); // _ UART
GiJeongKim 0:51c43836c1d7 49
jobuuu 7:e9086c72bb22 50 // CAN ///////////////////////////////////////////
GiJeongKim 0:51c43836c1d7 51 CAN can(PB_8, PB_9, 1000000);
GiJeongKim 0:51c43836c1d7 52 CANMessage msg;
Lightvalve 11:82d8768d7351 53 void onMsgReceived()
Lightvalve 11:82d8768d7351 54 {
Lightvalve 11:82d8768d7351 55 CAN_RX_HANDLER();
Lightvalve 11:82d8768d7351 56 }
jobuuu 2:a1c0a37df760 57
jobuuu 7:e9086c72bb22 58 // Variables ///////////////////////////////////////////
jobuuu 7:e9086c72bb22 59 State pos;
jobuuu 7:e9086c72bb22 60 State vel;
jobuuu 7:e9086c72bb22 61 State Vout;
jobuuu 7:e9086c72bb22 62 State torq;
jobuuu 7:e9086c72bb22 63 State pres_A;
jobuuu 7:e9086c72bb22 64 State pres_B;
jobuuu 7:e9086c72bb22 65 State cur;
Lightvalve 14:8e7590227d22 66 State valve_pos;
Lightvalve 14:8e7590227d22 67
Lightvalve 14:8e7590227d22 68 State INIT_Vout;
Lightvalve 14:8e7590227d22 69 State INIT_Valve_Pos;
Lightvalve 14:8e7590227d22 70 State INIT_Pos;
Lightvalve 14:8e7590227d22 71 State INIT_torq;
jobuuu 5:a4319f79457b 72
Lightvalve 19:23b7c1ad8683 73 extern int CID_RX_CMD;
Lightvalve 19:23b7c1ad8683 74 extern int CID_RX_REF_POSITION;
Lightvalve 49:e7bcfc244d40 75 extern int CID_RX_REF_VALVE_POS;
Lightvalve 45:35fa6884d0c6 76 extern int CID_RX_REF_PWM;
Lightvalve 19:23b7c1ad8683 77
Lightvalve 19:23b7c1ad8683 78 extern int CID_TX_INFO;
Lightvalve 19:23b7c1ad8683 79 extern int CID_TX_POSITION;
Lightvalve 19:23b7c1ad8683 80 extern int CID_TX_TORQUE;
Lightvalve 19:23b7c1ad8683 81 extern int CID_TX_PRES;
Lightvalve 19:23b7c1ad8683 82 extern int CID_TX_VOUT;
Lightvalve 19:23b7c1ad8683 83 extern int CID_TX_VALVE_POSITION;
Lightvalve 19:23b7c1ad8683 84
Lightvalve 61:bc8c8270f0ab 85
Lightvalve 61:bc8c8270f0ab 86
Lightvalve 61:bc8c8270f0ab 87
jobuuu 7:e9086c72bb22 88 // =============================================================================
jobuuu 7:e9086c72bb22 89 // =============================================================================
jobuuu 7:e9086c72bb22 90 // =============================================================================
jobuuu 7:e9086c72bb22 91
Lightvalve 12:6f2531038ea4 92 /*******************************************************************************
Lightvalve 12:6f2531038ea4 93 * REFERENCE MODE
Lightvalve 12:6f2531038ea4 94 ******************************************************************************/
Lightvalve 13:747daba9cf59 95 enum _REFERENCE_MODE {
Lightvalve 13:747daba9cf59 96 MODE_REF_NO_ACT = 0, //0
Lightvalve 12:6f2531038ea4 97 MODE_REF_DIRECT, //1
Lightvalve 12:6f2531038ea4 98 MODE_REF_COS_INC, //2
Lightvalve 12:6f2531038ea4 99 MODE_REF_LINE_INC, //3
Lightvalve 12:6f2531038ea4 100 MODE_REF_SIN_WAVE, //4
Lightvalve 12:6f2531038ea4 101 MODE_REF_SQUARE_WAVE, //5
Lightvalve 12:6f2531038ea4 102 };
Lightvalve 12:6f2531038ea4 103
Lightvalve 12:6f2531038ea4 104 /*******************************************************************************
Lightvalve 12:6f2531038ea4 105 * CONTROL MODE
Lightvalve 12:6f2531038ea4 106 ******************************************************************************/
Lightvalve 13:747daba9cf59 107 enum _CONTROL_MODE {
Lightvalve 12:6f2531038ea4 108 //control mode
Lightvalve 12:6f2531038ea4 109 MODE_NO_ACT = 0, //0
Lightvalve 47:fdcb8bd86fd6 110 MODE_VALVE_POSITION_CONTROL, //1
Lightvalve 47:fdcb8bd86fd6 111 MODE_JOINT_CONTROL, //2
Lightvalve 50:3c630b5eba9f 112
Lightvalve 47:fdcb8bd86fd6 113 MODE_VALVE_OPEN_LOOP, //3
Lightvalve 138:a843f32ced33 114 MODE_JOINT_ADAPTIVE_BACKSTEPPING, //4
Lightvalve 171:bfc1fd2629d8 115 MODE_RL, //5
Lightvalve 13:747daba9cf59 116
Lightvalve 12:6f2531038ea4 117 MODE_JOINT_POSITION_PRES_CONTROL_PWM, //6
Lightvalve 12:6f2531038ea4 118 MODE_JOINT_POSITION_PRES_CONTROL_VALVE_POSITION, //7
Lightvalve 12:6f2531038ea4 119 MODE_VALVE_POSITION_PRES_CONTROL_LEARNING, //8
Lightvalve 13:747daba9cf59 120
Lightvalve 12:6f2531038ea4 121 MODE_TEST_CURRENT_CONTROL, //9
Lightvalve 12:6f2531038ea4 122 MODE_TEST_PWM_CONTROL, //10
Lightvalve 21:e5f1a43ea6f9 123
Lightvalve 14:8e7590227d22 124 MODE_CURRENT_CONTROL, //11
Lightvalve 14:8e7590227d22 125 MODE_JOINT_POSITION_TORQUE_CONTROL_CURRENT, //12
Lightvalve 14:8e7590227d22 126 MODE_JOINT_POSITION_PRES_CONTROL_CURRENT, //13
Lightvalve 171:bfc1fd2629d8 127 MODE_VALVE_POSITION_TORQUE_CONTROL_LEARNING, //14
Lightvalve 13:747daba9cf59 128
Lightvalve 12:6f2531038ea4 129 //utility
Lightvalve 12:6f2531038ea4 130 MODE_TORQUE_SENSOR_NULLING = 20, //20
Lightvalve 12:6f2531038ea4 131 MODE_VALVE_NULLING_AND_DEADZONE_SETTING, //21
Lightvalve 12:6f2531038ea4 132 MODE_FIND_HOME, //22
Lightvalve 57:f4819de54e7a 133 MODE_VALVE_GAIN_SETTING, //23
Lightvalve 12:6f2531038ea4 134 MODE_PRESSURE_SENSOR_NULLING, //24
Lightvalve 12:6f2531038ea4 135 MODE_PRESSURE_SENSOR_CALIB, //25
Lightvalve 12:6f2531038ea4 136 MODE_ROTARY_FRICTION_TUNING, //26
Lightvalve 13:747daba9cf59 137
Lightvalve 12:6f2531038ea4 138 MODE_DDV_POS_VS_PWM_ID = 30, //30
Lightvalve 12:6f2531038ea4 139 MODE_DDV_DEADZONE_AND_CENTER, //31
Lightvalve 12:6f2531038ea4 140 MODE_DDV_POS_VS_FLOWRATE, //32
Lightvalve 57:f4819de54e7a 141 MODE_SYSTEM_ID, //33
Lightvalve 169:645207e160ca 142 MODE_FREQ_TEST, //34
Lightvalve 169:645207e160ca 143 MODE_SEND_BUFFER, //35
Lightvalve 169:645207e160ca 144 MODE_SEND_OVER, //36
Lightvalve 169:645207e160ca 145 MODE_STEP_TEST, //37
Lightvalve 12:6f2531038ea4 146 };
Lightvalve 12:6f2531038ea4 147
Lightvalve 65:a2d7c63419c2 148 void SystemClock_Config(void)
Lightvalve 65:a2d7c63419c2 149 {
Lightvalve 169:645207e160ca 150 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
Lightvalve 169:645207e160ca 151 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
Lightvalve 65:a2d7c63419c2 152
Lightvalve 169:645207e160ca 153 /** Configure the main internal regulator output voltage
Lightvalve 169:645207e160ca 154 */
Lightvalve 169:645207e160ca 155 __HAL_RCC_PWR_CLK_ENABLE();
Lightvalve 169:645207e160ca 156 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
Lightvalve 169:645207e160ca 157 /** Initializes the CPU, AHB and APB busses clocks
Lightvalve 169:645207e160ca 158 */
Lightvalve 169:645207e160ca 159 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
Lightvalve 169:645207e160ca 160 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
Lightvalve 169:645207e160ca 161 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
Lightvalve 169:645207e160ca 162 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
Lightvalve 169:645207e160ca 163 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
Lightvalve 169:645207e160ca 164 RCC_OscInitStruct.PLL.PLLM = 8;//8
Lightvalve 169:645207e160ca 165 RCC_OscInitStruct.PLL.PLLN = 180; //180
Lightvalve 169:645207e160ca 166 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
Lightvalve 169:645207e160ca 167 RCC_OscInitStruct.PLL.PLLQ = 2;
Lightvalve 169:645207e160ca 168 RCC_OscInitStruct.PLL.PLLR = 2;
Lightvalve 169:645207e160ca 169 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
Lightvalve 169:645207e160ca 170 //Error_Handler();
Lightvalve 169:645207e160ca 171 }
Lightvalve 169:645207e160ca 172 /** Activate the Over-Drive mode
Lightvalve 169:645207e160ca 173 */
Lightvalve 169:645207e160ca 174 if (HAL_PWREx_EnableOverDrive() != HAL_OK) {
Lightvalve 169:645207e160ca 175 //Error_Handler();
Lightvalve 169:645207e160ca 176 }
Lightvalve 169:645207e160ca 177 /** Initializes the CPU, AHB and APB busses clocks
Lightvalve 169:645207e160ca 178 */
Lightvalve 169:645207e160ca 179 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
Lightvalve 169:645207e160ca 180 |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
Lightvalve 169:645207e160ca 181 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
Lightvalve 169:645207e160ca 182 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
Lightvalve 169:645207e160ca 183 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
Lightvalve 169:645207e160ca 184 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
Lightvalve 65:a2d7c63419c2 185
Lightvalve 169:645207e160ca 186 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) {
Lightvalve 169:645207e160ca 187 //Error_Handler();
Lightvalve 169:645207e160ca 188 }
Lightvalve 65:a2d7c63419c2 189 }
Lightvalve 65:a2d7c63419c2 190
Lightvalve 99:7bbcb3c0fb06 191 float u_past[num_array_u_past] = {0.0f};
Lightvalve 68:328e1be06f5d 192 float x_past[num_array_x_past] = {0.0f};
Lightvalve 85:a3b46118b5cd 193 float x_future[num_array_x_future] = {0.0f};
Lightvalve 68:328e1be06f5d 194 float f_past[num_array_f_past] = {0.0f};
Lightvalve 68:328e1be06f5d 195 float f_future[num_array_f_future] = {0.0f};
Lightvalve 66:a8e6799dbce3 196
Lightvalve 73:f80dc3970c99 197 float input_NN[num_input] = { 0.0f };
Lightvalve 65:a2d7c63419c2 198
Lightvalve 169:645207e160ca 199 const float h1[num_input][16] = {
Lightvalve 169:645207e160ca 200 {-1.3752100467681885f,0.9850414395332336f,0.12903714179992676f,-0.23237967491149902f,-1.5664141178131104f,0.36565715074539185f,-0.17472904920578003f,-0.3780243396759033f,-0.5318458676338196f,-0.6931207776069641f,-0.24376395344734192f,-3.9280478954315186f,-0.259793758392334f,-0.5339483022689819f,-0.2571594715118408f,-0.4960939884185791f},
Lightvalve 169:645207e160ca 201 {-1.184814453125f,0.8704654574394226f,-0.3927857577800751f,-0.40050771832466125f,-1.4921096563339233f,0.5157514810562134f,-0.4054011106491089f,-0.3723553717136383f,-0.264297217130661f,-0.4598728120326996f,-0.2805407643318176f,-2.543855905532837f,0.24001067876815796f,-0.9143947958946228f,-0.20967772603034973f,-0.46271881461143494f},
Lightvalve 169:645207e160ca 202 {-0.9112814664840698f,0.6714562773704529f,0.36391180753707886f,-0.6117674708366394f,-1.1069424152374268f,0.9712548851966858f,0.2872133255004883f,-0.059537652879953384f,-0.28272250294685364f,-0.4464949667453766f,-0.3544096052646637f,-1.0609019994735718f,0.0019771659281104803f,-0.2590618431568146f,0.05590221285820007f,-1.0201798677444458f},
Lightvalve 169:645207e160ca 203 {-0.2895396053791046f,-0.01403216365724802f,0.39742106199264526f,0.20069332420825958f,-0.6715657114982605f,0.5361601710319519f,-0.11584559082984924f,0.3479021489620209f,-0.01779201813042164f,-0.017457854002714157f,0.08943979442119598f,0.845181941986084f,-0.15994226932525635f,-0.14522784948349f,-0.2961459457874298f,-0.7345172166824341f},
Lightvalve 169:645207e160ca 204 {1.1968307495117188f,-0.8280011415481567f,-0.0051451027393341064f,0.39627134799957275f,0.3181920051574707f,-0.05303953215479851f,0.1294034719467163f,0.16528914868831635f,0.07129872590303421f,0.7101694941520691f,0.34140077233314514f,1.623598575592041f,-0.06323867291212082f,0.27155500650405884f,-0.3865056037902832f,-0.30631402134895325f},
Lightvalve 169:645207e160ca 205 {3.009206533432007f,-3.122997522354126f,0.21773302555084229f,0.7692314386367798f,2.249300003051758f,-2.275317907333374f,0.16514194011688232f,0.11058115214109421f,0.17132356762886047f,2.417614459991455f,-0.25413116812705994f,2.2743594646453857f,-0.25959381461143494f,0.8003392219543457f,-0.4132833778858185f,1.4499995708465576f},
Lightvalve 169:645207e160ca 206 {-0.7179104089736938f,0.8526995182037354f,-0.2501986622810364f,-0.875395655632019f,-0.5985659956932068f,2.8744397163391113f,0.06014183163642883f,-0.8439451456069946f,-0.05617062747478485f,-0.640657901763916f,0.14294663071632385f,3.381211519241333f,-0.1638868898153305f,-0.2370987981557846f,-0.15618428587913513f,-0.7710326910018921f},
Lightvalve 169:645207e160ca 207 {-0.5504567623138428f,0.25199615955352783f,-0.26619744300842285f,-0.08971554040908813f,-0.3043712079524994f,0.903326153755188f,-0.17925891280174255f,-0.6943695545196533f,-0.25526830554008484f,0.20558907091617584f,0.09723648428916931f,2.3618743419647217f,0.38060376048088074f,-0.1769549399614334f,0.04303058981895447f,-0.12222103029489517f},
Lightvalve 169:645207e160ca 208 {0.06810358166694641f,0.17962348461151123f,0.10248100757598877f,-0.30372604727745056f,0.4354613721370697f,-0.7276828289031982f,-0.07246989011764526f,-0.6118704676628113f,-0.42743170261383057f,1.453460693359375f,-0.30540645122528076f,1.6834946870803833f,0.23858578503131866f,0.21455851197242737f,-0.30555272102355957f,0.8063939213752747f},
Lightvalve 169:645207e160ca 209 {-0.0713198110461235f,-0.4495846927165985f,0.18462657928466797f,0.05500239133834839f,0.7719969153404236f,-1.2848507165908813f,-0.2990540862083435f,-0.22474081814289093f,-0.1015893891453743f,0.7663454413414001f,-0.016399379819631577f,-0.03539150580763817f,0.38201630115509033f,-0.09081173688173294f,0.3484981060028076f,0.7820040583610535f},
Lightvalve 169:645207e160ca 210 {0.33533185720443726f,0.13371603190898895f,0.37182438373565674f,0.39484986662864685f,0.35771995782852173f,-1.3015228509902954f,-0.20339298248291016f,-0.14145439863204956f,-0.5059540271759033f,0.8015591502189636f,0.34025880694389343f,-0.6485148072242737f,-0.05657690018415451f,0.09265313297510147f,0.26688337326049805f,0.2284013032913208f},
Lightvalve 169:645207e160ca 211 {-0.4380135238170624f,-0.15535405278205872f,-0.40320003032684326f,0.0452105812728405f,0.23505118489265442f,-0.8950125575065613f,0.1533789038658142f,0.1997886300086975f,-0.24218705296516418f,0.45601460337638855f,-0.42232224345207214f,-0.5756524205207825f,0.15488487482070923f,-0.013335001654922962f,0.10493969917297363f,0.23898905515670776f},
Lightvalve 169:645207e160ca 212 {-0.02198643982410431f,0.009633726440370083f,0.19847965240478516f,0.0860355794429779f,0.336037278175354f,-0.277248352766037f,-0.2404318004846573f,-0.07731665670871735f,-0.2765587270259857f,-0.1708865910768509f,-0.11712735891342163f,-0.7335506677627563f,0.10241620987653732f,-0.06552530825138092f,-0.22268100082874298f,-0.13819573819637299f},
Lightvalve 169:645207e160ca 213 {0.020149538293480873f,-0.03840681165456772f,0.4127817749977112f,-0.2523573637008667f,-0.21765978634357452f,-0.4540342092514038f,-0.32001304626464844f,-0.5396589040756226f,-0.21919101476669312f,-0.2550864815711975f,-0.20302052795886993f,-0.2918916642665863f,-0.3021690249443054f,-0.19007131457328796f,0.0479682981967926f,0.28083792328834534f},
Lightvalve 169:645207e160ca 214 {0.19955825805664062f,0.13080951571464539f,0.20282304286956787f,-0.21126452088356018f,0.15339018404483795f,-0.06708966940641403f,0.04202890396118164f,0.06752092391252518f,0.25217023491859436f,-0.20238792896270752f,-0.2873592674732208f,-0.42253321409225464f,0.11435109376907349f,-0.2376958578824997f,0.0668090283870697f,0.04261681064963341f},
Lightvalve 169:645207e160ca 215 {-0.03747318685054779f,-0.29164087772369385f,-0.40439701080322266f,-0.3783135414123535f,-0.40124887228012085f,-0.026558339595794678f,-0.15568238496780396f,-0.08766567707061768f,0.21709603071212769f,-0.35802650451660156f,-0.4817592203617096f,-0.3809756338596344f,-0.2601439654827118f,-0.1273980438709259f,-0.08245879411697388f,0.014104350470006466f},
Lightvalve 169:645207e160ca 216 {0.16147102415561676f,-0.5628581643104553f,0.16706281900405884f,-0.1172998696565628f,-0.03297983109951019f,-0.9104568362236023f,-0.041414469480514526f,0.1465616226196289f,0.04248951002955437f,0.007101915311068296f,-0.3614332377910614f,-0.48312774300575256f,0.12316523492336273f,0.3939219117164612f,0.29427415132522583f,0.3038717210292816f},
Lightvalve 65:a2d7c63419c2 217 };
Lightvalve 65:a2d7c63419c2 218
Lightvalve 169:645207e160ca 219 const float h2[16][16] = {
Lightvalve 168:f05a45d5f318 220 {0.33418041467666626f,2.9079723358154297f,-0.06966331601142883f,-0.009902028366923332f,2.812035322189331f,4.195849418640137f,-0.038234543055295944f,0.0904630720615387f,-0.43008196353912354f,-0.1415480375289917f,0.10707370936870575f,-0.20333009958267212f,-0.031739287078380585f,-3.289416551589966f,-1.172767996788025f,-0.6248859763145447f},
Lightvalve 169:645207e160ca 221 {0.11070519685745239f,-2.3348469734191895f,0.057057321071624756f,-0.10134492069482803f,-5.252432346343994f,-1.3715343475341797f,-0.8426902890205383f,-0.2990124225616455f,-0.33757925033569336f,0.2895788848400116f,-7.361403465270996f,-0.858361005783081f,0.0845860168337822f,2.2169580459594727f,-3.0816471576690674f,-1.0190033912658691f},
Lightvalve 169:645207e160ca 222 {-0.22745239734649658f,0.003037691116333008f,-0.061119019985198975f,0.35696902871131897f,0.05568113923072815f,0.011741191148757935f,-0.20225946605205536f,-0.08465918898582458f,0.3489862382411957f,0.0687277615070343f,0.31964078545570374f,0.3004753887653351f,0.36063823103904724f,-0.42892736196517944f,0.08652284741401672f,0.027493387460708618f},
Lightvalve 169:645207e160ca 223 {-0.1391567885875702f,0.03293241187930107f,-0.2894435524940491f,0.25554025173187256f,0.08946844935417175f,0.1340968757867813f,-0.39087218046188354f,0.4122363030910492f,0.26211628317832947f,0.2953031361103058f,0.7025435566902161f,0.05798906087875366f,-0.5013425946235657f,-0.030901746824383736f,-0.06675297021865845f,-0.0446302704513073f},
Lightvalve 169:645207e160ca 224 {-2.245600938796997f,1.6008015871047974f,-0.25313520431518555f,0.1469619870185852f,1.6072996854782104f,-1.3637473583221436f,-0.5957030653953552f,-0.08570799231529236f,-0.024399548768997192f,-0.39465832710266113f,-6.811410427093506f,-0.7977238893508911f,-0.022602174431085587f,-1.6454986333847046f,0.5441725850105286f,-0.7979243397712708f},
Lightvalve 169:645207e160ca 225 {0.5210384130477905f,-0.11346126347780228f,-0.3519742488861084f,0.29306867718696594f,-0.3572634160518646f,1.75344979763031f,-0.744201123714447f,-0.019414573907852173f,-0.23047015070915222f,0.3579089343547821f,-2.398893117904663f,-0.48980283737182617f,-0.45957082509994507f,0.5487804412841797f,-0.7850930690765381f,-0.7663227319717407f},
Lightvalve 169:645207e160ca 226 {-0.1828227937221527f,-0.02554568648338318f,-0.3260969817638397f,0.08422836661338806f,-0.38453540205955505f,-0.25432005524635315f,0.285016268491745f,0.12387624382972717f,-0.0982072651386261f,0.13111665844917297f,-0.03692615032196045f,-0.32796353101730347f,-0.21546880900859833f,0.049302369356155396f,-0.27088475227355957f,-0.4124959409236908f},
Lightvalve 169:645207e160ca 227 {-0.007747650612145662f,0.3667842745780945f,0.33083590865135193f,-0.38213321566581726f,-0.14358049631118774f,-0.2546055316925049f,-0.09043094515800476f,-0.3003333508968353f,0.2600560486316681f,-0.37898191809654236f,0.49078798294067383f,-0.3058888614177704f,-0.31673234701156616f,0.44664251804351807f,-0.9339061975479126f,-0.1061311885714531f},
Lightvalve 169:645207e160ca 228 {-0.17694538831710815f,0.1767565757036209f,-0.11379697918891907f,-0.07116051763296127f,0.041274964809417725f,-0.517110288143158f,-0.10284432768821716f,0.0019084513187408447f,0.06103590130805969f,-0.38046833872795105f,-0.3456110656261444f,-0.24793067574501038f,-0.4166972041130066f,-0.30618393421173096f,-0.33565962314605713f,-0.48154377937316895f},
Lightvalve 169:645207e160ca 229 {0.5043365359306335f,0.1850307136774063f,-0.13502129912376404f,-0.25706031918525696f,-0.8451530337333679f,-0.9415315389633179f,-0.2777014970779419f,-0.3632148504257202f,-0.13619378209114075f,0.16938945651054382f,1.5622152090072632f,-0.19312366843223572f,0.1250869333744049f,-0.1843482404947281f,1.0270578861236572f,-0.5840676426887512f},
Lightvalve 169:645207e160ca 230 {-0.25693249702453613f,-0.010147050023078918f,0.0457797646522522f,-0.3549601435661316f,-0.03321319818496704f,-0.4391511380672455f,0.17873415350914001f,-0.20421427488327026f,-0.050184011459350586f,0.12480869889259338f,-0.22192376852035522f,0.3545852601528168f,-0.3647043704986572f,0.4194667637348175f,-0.3910166025161743f,-0.2837793529033661f},
Lightvalve 169:645207e160ca 231 {2.3816981315612793f,0.4564768075942993f,0.3954955041408539f,0.11254638433456421f,1.1973353624343872f,1.804193377494812f,-0.8583166599273682f,-0.11795541644096375f,0.3487861454486847f,-0.32520344853401184f,-1.3897980451583862f,-0.5921090841293335f,-0.14213289320468903f,-0.26851770281791687f,1.597784161567688f,-0.5877130031585693f},
Lightvalve 169:645207e160ca 232 {-0.267646849155426f,-0.20429572463035583f,-0.15798500180244446f,0.3999568819999695f,-0.37393757700920105f,0.35770177841186523f,0.11292675137519836f,-0.2947862446308136f,-0.3764709532260895f,0.2424570620059967f,-0.10294663906097412f,-0.2837170362472534f,0.41839322447776794f,-0.02792874164879322f,-0.13706544041633606f,0.11999254673719406f},
Lightvalve 169:645207e160ca 233 {0.2282707840204239f,-0.13254989683628082f,-0.2018718123435974f,-0.07019486278295517f,0.07545611262321472f,-0.33204764127731323f,-0.4035329520702362f,-0.21416273713111877f,-0.24509364366531372f,0.19522181153297424f,0.47974857687950134f,-0.8069085478782654f,-0.40297332406044006f,0.23708327114582062f,0.5259239673614502f,-0.3548051416873932f},
Lightvalve 169:645207e160ca 234 {0.4138670265674591f,0.1604653298854828f,0.056746453046798706f,0.036025404930114746f,0.3228367865085602f,-0.07083973288536072f,0.018455177545547485f,0.0059362053871154785f,0.40515169501304626f,0.014240056276321411f,-0.07738298177719116f,0.1407785713672638f,-0.13024571537971497f,-0.29546058177948f,-0.11976784467697144f,-0.35825538635253906f},
Lightvalve 169:645207e160ca 235 {-0.5818981528282166f,0.014110059477388859f,0.12081471085548401f,-0.2973254919052124f,0.5392167568206787f,0.3773144781589508f,-0.39351940155029297f,0.31089308857917786f,-0.3893685042858124f,-0.02222958207130432f,-6.493835926055908f,-0.12242847681045532f,-0.2689415514469147f,-0.2518419325351715f,-0.1694900244474411f,-0.652117908000946f},
Lightvalve 65:a2d7c63419c2 236 };
Lightvalve 65:a2d7c63419c2 237
Lightvalve 169:645207e160ca 238 const float h3[16][16] = {
Lightvalve 168:f05a45d5f318 239 {-0.36079341173171997f,-1.5671656131744385f,-0.870830237865448f,-0.4786093831062317f,-0.5575059056282043f,-0.773746132850647f,0.29977259039878845f,0.1743643879890442f,-0.5892294645309448f,-0.19605471193790436f,-0.27379146218299866f,0.06893575936555862f,2.2012624740600586f,-2.7650468349456787f,-1.8527313470840454f,1.5724681615829468f},
Lightvalve 169:645207e160ca 240 {0.047732532024383545f,-0.23476845026016235f,-2.8836917877197266f,-0.42602431774139404f,-2.397148370742798f,1.254254937171936f,-0.3198729455471039f,-0.25123724341392517f,0.013695899397134781f,-0.5729482173919678f,0.09775999933481216f,-0.5587971806526184f,-0.9381403923034668f,-1.532671570777893f,-0.6944977045059204f,1.4258947372436523f},
Lightvalve 169:645207e160ca 241 {0.07903262972831726f,0.2790505588054657f,-0.07798504829406738f,0.04248586297035217f,-0.1963958442211151f,-0.19260792434215546f,-0.4038352966308594f,0.015906542539596558f,0.15353140234947205f,0.030178606510162354f,0.2488909661769867f,0.13805970549583435f,-0.0816211998462677f,-0.20733052492141724f,-0.3036302626132965f,0.054825395345687866f},
Lightvalve 169:645207e160ca 242 {-0.30922991037368774f,-0.16969707608222961f,0.3056027889251709f,-0.380797415971756f,-0.17742466926574707f,0.10660405457019806f,0.20021501183509827f,0.07002416253089905f,-0.25412267446517944f,-0.365601509809494f,0.13192829489707947f,0.41021624207496643f,-0.13397035002708435f,0.0815882533788681f,0.15073642134666443f,0.10944337397813797f},
Lightvalve 169:645207e160ca 243 {-0.39607733488082886f,-0.05481579899787903f,0.1976260244846344f,0.022423356771469116f,0.16892847418785095f,-0.6999471187591553f,0.16012099385261536f,0.21129152178764343f,-0.08640444278717041f,-0.11053556203842163f,-0.2634495496749878f,-0.31317979097366333f,-0.1530032455921173f,-0.24679358303546906f,0.22959044575691223f,-3.0342295169830322f},
Lightvalve 169:645207e160ca 244 {-0.37449589371681213f,-0.10077743977308273f,-4.51036262512207f,-0.04963836818933487f,-0.8192825317382812f,-0.6951172947883606f,-0.19780586659908295f,-0.09049295634031296f,-0.9114404916763306f,-0.8623005151748657f,-0.4059421718120575f,-0.16295623779296875f,-5.017462730407715f,-1.1079373359680176f,0.34683844447135925f,0.6427209377288818f},
Lightvalve 169:645207e160ca 245 {0.4110594093799591f,0.2715781033039093f,-0.38537508249282837f,0.37246426939964294f,-0.05490662157535553f,-0.009114405140280724f,-0.08670487999916077f,-0.25336313247680664f,-0.030661463737487793f,-0.06259563565254211f,-0.1344406008720398f,0.35313835740089417f,-0.13377737998962402f,0.25604528188705444f,0.3126353323459625f,-0.1528691202402115f},
Lightvalve 169:645207e160ca 246 {-0.40892091393470764f,0.043769627809524536f,-0.3867315948009491f,0.25968697667121887f,0.3424709737300873f,-0.051169753074645996f,-0.23312048614025116f,-0.390264093875885f,0.28059282898902893f,-0.1559126079082489f,-0.14134526252746582f,-0.0003446042537689209f,-0.2742875814437866f,-0.36560842394828796f,0.07994696497917175f,0.005298197269439697f},
Lightvalve 169:645207e160ca 247 {0.10697010159492493f,-0.12228584289550781f,-0.37870171666145325f,0.21184906363487244f,-0.37222859263420105f,-0.17138728499412537f,-0.1382003128528595f,0.3493293821811676f,-0.360889196395874f,-0.3875247836112976f,0.42142823338508606f,-0.3482915461063385f,-0.3289247751235962f,-0.2186824083328247f,0.09620395302772522f,-0.06898030638694763f},
Lightvalve 169:645207e160ca 248 {0.2847062647342682f,0.018552124500274658f,0.11435768008232117f,0.36562982201576233f,-0.047046810388565063f,0.30447837710380554f,0.2430230677127838f,0.2909286320209503f,-0.2802048921585083f,0.18043199181556702f,0.41849127411842346f,-0.287167489528656f,0.24394884705543518f,-0.14084559679031372f,-0.10168051719665527f,0.010465055704116821f},
Lightvalve 169:645207e160ca 249 {0.15459725260734558f,0.22762465476989746f,1.5232031345367432f,-0.2233445942401886f,1.524779200553894f,2.6997625827789307f,-0.08742031455039978f,0.05785742402076721f,-0.1277361363172531f,-0.37371426820755005f,-0.03133596479892731f,-0.30447322130203247f,-1.9101834297180176f,0.6954238414764404f,0.46117544174194336f,1.1762653589248657f},
Lightvalve 169:645207e160ca 250 {-0.408692330121994f,0.07260357588529587f,-0.02147701010107994f,0.0922636091709137f,-0.1529182642698288f,-0.05657944083213806f,0.03285527229309082f,0.38763079047203064f,-0.20705322921276093f,-0.25883403420448303f,0.12809070944786072f,0.03996849060058594f,-0.6609845757484436f,-0.1087266057729721f,-0.10636871308088303f,-0.0133456289768219f},
Lightvalve 169:645207e160ca 251 {-0.2991822361946106f,0.3794580399990082f,-0.08715943992137909f,-0.05932474136352539f,0.11478022485971451f,0.3007120192050934f,-0.11253207921981812f,0.34576353430747986f,0.04814547300338745f,-0.35770976543426514f,-0.044228196144104004f,-0.36229726672172546f,0.05380958318710327f,-0.13672849535942078f,0.35829514265060425f,-0.10585878044366837f},
Lightvalve 169:645207e160ca 252 {-0.2675279378890991f,-0.1429223269224167f,-0.9267327189445496f,-0.1256239265203476f,0.49980443716049194f,-0.843601405620575f,-0.33532586693763733f,-0.31893211603164673f,-1.478386402130127f,-1.1926257610321045f,-0.5260127782821655f,-0.8384293913841248f,0.9772797226905823f,0.1813446283340454f,0.17612296342849731f,1.590701699256897f},
Lightvalve 169:645207e160ca 253 {-0.015470266342163086f,-0.4976375699043274f,2.497199058532715f,0.14489558339118958f,0.11334121972322464f,0.3805958926677704f,0.02394937537610531f,-0.16103173792362213f,-0.677362859249115f,-0.8498573303222656f,-0.026575788855552673f,-0.427055299282074f,0.17177484929561615f,1.67837655544281f,0.08538639545440674f,-1.3552721738815308f},
Lightvalve 169:645207e160ca 254 {0.1863725483417511f,0.13157431781291962f,-0.047772910445928574f,-0.49708226323127747f,0.7275430560112f,-0.145163431763649f,0.04860696196556091f,0.17769548296928406f,-0.050254471600055695f,0.19876523315906525f,-0.5504136085510254f,0.19592127203941345f,0.23928996920585632f,-0.12190091609954834f,-0.1250786930322647f,0.041962411254644394f},
Lightvalve 66:a8e6799dbce3 255 };
Lightvalve 65:a2d7c63419c2 256
Lightvalve 168:f05a45d5f318 257 const float hout[16] = { 0.45773375034332275f,0.5733839869499207f,-0.5152473449707031f,-0.035915032029151917f,-0.306951105594635f,0.30648893117904663f,-0.1005084440112114f,-0.08898112922906876f,-0.2034129500389099f,-0.05844772607088089f,-0.07081260532140732f,0.08596939593553543f,0.5426119565963745f,0.5678461194038391f,1.0715678930282593f,-0.25178417563438416f };
Lightvalve 66:a8e6799dbce3 258
Lightvalve 168:f05a45d5f318 259 const float b1[16] = { 0.5956194400787354f,1.4903173446655273f,-1.7145336866378784f,0.2382747083902359f,1.812330722808838f,1.1310549974441528f,-0.058932315558195114f,1.0475828647613525f,0.5750831365585327f,-1.1691874265670776f,0.564017653465271f,0.8430382013320923f,-0.3627738058567047f,-0.8757394552230835f,-1.087764859199524f,1.8534502983093262f };
Lightvalve 87:471334725012 260
Lightvalve 168:f05a45d5f318 261 const float b2[16] = { -0.19254711270332336f,-1.2538373470306396f,-1.4564176797866821f,-0.6360846757888794f,-2.2159981727600098f,-0.5284036993980408f,0.07729385793209076f,-0.30969977378845215f,-1.20063054561615f,-1.912178635597229f,0.16195619106292725f,0.17741809785366058f,-0.3301374316215515f,1.8553483486175537f,-0.1654871553182602f,0.5861462950706482f };
Lightvalve 65:a2d7c63419c2 262
Lightvalve 168:f05a45d5f318 263 const float b3[16] = { -1.963319182395935f,-0.5293647646903992f,-0.10495924949645996f,-0.23769626021385193f,1.5550658702850342f,1.2147407531738281f,-0.47398853302001953f,-0.9554895758628845f,-0.067354217171669f,-0.006115084979683161f,-0.45877549052238464f,-0.290895938873291f,-1.0047727823257446f,1.1215981245040894f,-1.407442569732666f,-1.6007274389266968f };
Lightvalve 87:471334725012 264
Lightvalve 168:f05a45d5f318 265 const float bout[1] = { -0.10839174687862396f };
Lightvalve 65:a2d7c63419c2 266
Lightvalve 170:42c938a40313 267 /////////////////////////////////////////////////////////////////////////////////////////////RL
Lightvalve 170:42c938a40313 268 float input_RL[num_input_RL] = { 0.0f };
Lightvalve 170:42c938a40313 269
Lightvalve 170:42c938a40313 270 //Critic Networks
Lightvalve 173:68c7914679ec 271 float hc1[num_input_RL][num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 272 float bc1[num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 273 float hc2[num_hidden_unit1][num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 274 float bc2[num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 275 float hc3[num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 276 float bc3 = 0.0f;
Lightvalve 170:42c938a40313 277
Lightvalve 170:42c938a40313 278 //Critic Networks Temporary
Lightvalve 173:68c7914679ec 279 float hc1_temp[num_input_RL][num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 280 float bc1_temp[num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 281 float hc2_temp[num_hidden_unit1][num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 282 float bc2_temp[num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 283 float hc3_temp[num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 284 float bc3_temp = 0.0f;
Lightvalve 170:42c938a40313 285
Lightvalve 170:42c938a40313 286 //Actor Networks
Lightvalve 173:68c7914679ec 287 float ha1[num_input_RL][num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 288 float ba1[num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 289 float ha2[num_hidden_unit1][num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 290 float ba2[num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 291 float ha3[num_hidden_unit2][2] = {0.0f};
Lightvalve 173:68c7914679ec 292 float ba3[2] = {0.0f};
Lightvalve 170:42c938a40313 293
Lightvalve 170:42c938a40313 294 //Actor Networks Temporary
Lightvalve 173:68c7914679ec 295 float ha1_temp[num_input_RL][num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 296 float ba1_temp[num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 297 float ha2_temp[num_hidden_unit1][num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 298 float ba2_temp[num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 299 float ha3_temp[num_hidden_unit2][2] = {0.0f};
Lightvalve 173:68c7914679ec 300 float ba3_temp[2] = {0.0f};
Lightvalve 66:a8e6799dbce3 301
Lightvalve 87:471334725012 302 float VALVE_POS_RAW_NN = 0.0f;
Lightvalve 87:471334725012 303 float DDV_JOINT_POS_FF(float REF_JOINT_VEL);
Lightvalve 61:bc8c8270f0ab 304
Lightvalve 170:42c938a40313 305
Lightvalve 170:42c938a40313 306 float Critic_Network(float *arr)
Lightvalve 170:42c938a40313 307 {
Lightvalve 173:68c7914679ec 308 float output1[num_hidden_unit1] = { 0.0f };
Lightvalve 173:68c7914679ec 309 float output2[num_hidden_unit2] = { 0.0f };
Lightvalve 170:42c938a40313 310 float output = 0.0f;
Lightvalve 173:68c7914679ec 311 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 170:42c938a40313 312 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 170:42c938a40313 313 output1[index2] = output1[index2] + hc1[index1][index2] * arr[index1];
Lightvalve 170:42c938a40313 314 }
Lightvalve 173:68c7914679ec 315 //ReLU
Lightvalve 173:68c7914679ec 316 output1[index2] = output1[index2] + bc1[index2];
Lightvalve 173:68c7914679ec 317 hx_c_sum[index2] = output1[index2];
Lightvalve 173:68c7914679ec 318 if (output1[index2] < 0) {
Lightvalve 173:68c7914679ec 319 output1[index2] = 0;
Lightvalve 173:68c7914679ec 320 }
Lightvalve 173:68c7914679ec 321 //tanh
Lightvalve 173:68c7914679ec 322 //output1[index2] = tanh(output1[index2] + bc1[index2]);
Lightvalve 173:68c7914679ec 323 }
Lightvalve 173:68c7914679ec 324 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 173:68c7914679ec 325 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 174:c828479f53f9 326 output2[index2] = output2[index2] + hc2[index1][index2] * output1[index1];
Lightvalve 173:68c7914679ec 327 }
Lightvalve 173:68c7914679ec 328 //ReLU
Lightvalve 173:68c7914679ec 329 output2[index2] = output2[index2] + bc2[index2];
Lightvalve 173:68c7914679ec 330 hxh_c_sum[index2] = output2[index2];
Lightvalve 173:68c7914679ec 331 if (output2[index2] < 0) {
Lightvalve 173:68c7914679ec 332 output2[index2] = 0;
Lightvalve 173:68c7914679ec 333 }
Lightvalve 173:68c7914679ec 334 //tanh
Lightvalve 173:68c7914679ec 335 //output2[index2] = tanh(output2[index2] + bc2[index2]);
Lightvalve 170:42c938a40313 336 }
Lightvalve 170:42c938a40313 337 for (int index2 = 0; index2 < 1; index2++) {
Lightvalve 173:68c7914679ec 338 for (int index1 = 0; index1 < num_hidden_unit2; index1++) {
Lightvalve 173:68c7914679ec 339 output = output + hc3[index1] * output2[index1];
Lightvalve 170:42c938a40313 340 }
Lightvalve 173:68c7914679ec 341 output = output + bc3;
Lightvalve 173:68c7914679ec 342 hxhh_c_sum = output;
Lightvalve 170:42c938a40313 343 }
Lightvalve 170:42c938a40313 344 return output;
Lightvalve 170:42c938a40313 345 }
Lightvalve 170:42c938a40313 346
Lightvalve 170:42c938a40313 347 float Critic_Network_Temp(float *arr)
Lightvalve 170:42c938a40313 348 {
Lightvalve 173:68c7914679ec 349 float output1[num_hidden_unit1] = { 0.0f };
Lightvalve 173:68c7914679ec 350 float output2[num_hidden_unit2] = { 0.0f };
Lightvalve 170:42c938a40313 351 float output = 0.0f;
Lightvalve 173:68c7914679ec 352 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 170:42c938a40313 353 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 170:42c938a40313 354 output1[index2] = output1[index2] + hc1_temp[index1][index2] * arr[index1];
Lightvalve 170:42c938a40313 355 }
Lightvalve 173:68c7914679ec 356 //ReLU
Lightvalve 173:68c7914679ec 357 output1[index2] = output1[index2] + bc1_temp[index2];
Lightvalve 173:68c7914679ec 358 hx_c_sum[index2] = output1[index2];
Lightvalve 173:68c7914679ec 359 if (output1[index2] < 0) {
Lightvalve 173:68c7914679ec 360 output1[index2] = 0;
Lightvalve 173:68c7914679ec 361 }
Lightvalve 173:68c7914679ec 362 //tanh
Lightvalve 173:68c7914679ec 363 //output1[index2] = tanh(output1[index2] + bc1_temp[index2]);
Lightvalve 173:68c7914679ec 364 }
Lightvalve 173:68c7914679ec 365 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 173:68c7914679ec 366 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 174:c828479f53f9 367 output2[index2] = output2[index2] + hc2_temp[index1][index2] * output1[index1];
Lightvalve 173:68c7914679ec 368 }
Lightvalve 173:68c7914679ec 369 //ReLU
Lightvalve 173:68c7914679ec 370 output2[index2] = output2[index2] + bc2_temp[index2];
Lightvalve 173:68c7914679ec 371 hxh_c_sum[index2] = output2[index2];
Lightvalve 173:68c7914679ec 372 if (output2[index2] < 0) {
Lightvalve 173:68c7914679ec 373 output2[index2] = 0;
Lightvalve 173:68c7914679ec 374 }
Lightvalve 173:68c7914679ec 375 //tanh
Lightvalve 173:68c7914679ec 376 //output2[index2] = tanh(output2[index2] + bc2_temp[index2]);
Lightvalve 170:42c938a40313 377 }
Lightvalve 170:42c938a40313 378 for (int index2 = 0; index2 < 1; index2++) {
Lightvalve 173:68c7914679ec 379 for (int index1 = 0; index1 < num_hidden_unit2; index1++) {
Lightvalve 173:68c7914679ec 380 output = output + hc3_temp[index1] * output2[index1];
Lightvalve 170:42c938a40313 381 }
Lightvalve 173:68c7914679ec 382 output = output + bc3_temp;
Lightvalve 173:68c7914679ec 383 hxhh_c_sum = output;
Lightvalve 170:42c938a40313 384 }
Lightvalve 170:42c938a40313 385 return output;
Lightvalve 170:42c938a40313 386 }
Lightvalve 170:42c938a40313 387
Lightvalve 170:42c938a40313 388
Lightvalve 170:42c938a40313 389 void Actor_Network(float *arr)
Lightvalve 170:42c938a40313 390 {
Lightvalve 173:68c7914679ec 391 float output1[num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 392 float output2[num_hidden_unit2] = {0.0f};
Lightvalve 170:42c938a40313 393 float output[2] = {0.0f};
Lightvalve 170:42c938a40313 394
Lightvalve 173:68c7914679ec 395 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 173:68c7914679ec 396 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 173:68c7914679ec 397 output1[index2] = output1[index2] + ha1[index1][index2] * arr[index1];
Lightvalve 173:68c7914679ec 398 }
Lightvalve 173:68c7914679ec 399 output1[index2] = output1[index2] + ba1[index2];
Lightvalve 173:68c7914679ec 400 hx_a_sum[index2] = output1[index2];
Lightvalve 173:68c7914679ec 401 if (output1[index2] < 0) {
Lightvalve 173:68c7914679ec 402 output1[index2] = 0;
Lightvalve 173:68c7914679ec 403 }
Lightvalve 173:68c7914679ec 404 }
Lightvalve 173:68c7914679ec 405 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 173:68c7914679ec 406 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 174:c828479f53f9 407 output2[index2] = output2[index2] + ha2[index1][index2] * output1[index1];
Lightvalve 173:68c7914679ec 408 }
Lightvalve 173:68c7914679ec 409 output2[index2] = output2[index2] + ba2[index2];
Lightvalve 173:68c7914679ec 410 hxh_a_sum[index2] = output2[index2];
Lightvalve 173:68c7914679ec 411 if (output2[index2] < 0) {
Lightvalve 173:68c7914679ec 412 output2[index2] = 0;
Lightvalve 173:68c7914679ec 413 }
Lightvalve 173:68c7914679ec 414 }
Lightvalve 173:68c7914679ec 415 for (int index2 = 0; index2 < 2; index2++) {
Lightvalve 173:68c7914679ec 416 for (int index1 = 0; index1 < num_hidden_unit2; index1++) {
Lightvalve 173:68c7914679ec 417 output[index2] = output[index2] + ha3[index1][index2] * output2[index1];
Lightvalve 173:68c7914679ec 418 }
Lightvalve 173:68c7914679ec 419 }
Lightvalve 173:68c7914679ec 420 hxhh_a_sum[0] = output[0] + ba3[0];
Lightvalve 173:68c7914679ec 421 hxhh_a_sum[1] = output[1] + ba3[1];
Lightvalve 173:68c7914679ec 422
Lightvalve 173:68c7914679ec 423 mean_before_SP = output[0] + ba3[0]; //SP = softplus
Lightvalve 173:68c7914679ec 424 deviation_before_SP = output[1] + ba3[1];
Lightvalve 173:68c7914679ec 425 //Softplus
Lightvalve 173:68c7914679ec 426 mean = log(1.0f+exp(mean_before_SP));
Lightvalve 173:68c7914679ec 427 deviation = log(1.0f+exp(deviation_before_SP));
Lightvalve 173:68c7914679ec 428 }
Lightvalve 173:68c7914679ec 429
Lightvalve 173:68c7914679ec 430
Lightvalve 173:68c7914679ec 431 void Actor_Network_Old(float *arr)
Lightvalve 173:68c7914679ec 432 {
Lightvalve 173:68c7914679ec 433 float output1[num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 434 float output2[num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 435 float output[2] = {0.0f};
Lightvalve 173:68c7914679ec 436
Lightvalve 173:68c7914679ec 437 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 170:42c938a40313 438 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 170:42c938a40313 439 output1[index2] = output1[index2] + ha1[index1][index2] * arr[index1];
Lightvalve 170:42c938a40313 440 }
Lightvalve 170:42c938a40313 441 output1[index2] = output1[index2] + ba1[index2];
Lightvalve 170:42c938a40313 442 if (output1[index2] < 0) {
Lightvalve 170:42c938a40313 443 output1[index2] = 0;
Lightvalve 170:42c938a40313 444 }
Lightvalve 170:42c938a40313 445 }
Lightvalve 173:68c7914679ec 446 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 173:68c7914679ec 447 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 174:c828479f53f9 448 output2[index2] = output2[index2] + ha2[index1][index2] * output1[index1];
Lightvalve 170:42c938a40313 449 }
Lightvalve 173:68c7914679ec 450 output2[index2] = output2[index2] + ba2[index2];
Lightvalve 173:68c7914679ec 451 if (output2[index2] < 0) {
Lightvalve 173:68c7914679ec 452 output2[index2] = 0;
Lightvalve 170:42c938a40313 453 }
Lightvalve 170:42c938a40313 454 }
Lightvalve 170:42c938a40313 455 for (int index2 = 0; index2 < 2; index2++) {
Lightvalve 173:68c7914679ec 456 for (int index1 = 0; index1 < num_hidden_unit2; index1++) {
Lightvalve 173:68c7914679ec 457 output[index2] = output[index2] + ha3[index1][index2] * output2[index1];
Lightvalve 170:42c938a40313 458 }
Lightvalve 170:42c938a40313 459 }
Lightvalve 173:68c7914679ec 460 mean_old = output[0] + ba3[0];
Lightvalve 173:68c7914679ec 461 deviation_old = output[1] + ba3[1];
Lightvalve 173:68c7914679ec 462 //Softplus
Lightvalve 173:68c7914679ec 463 mean_old = log(1.0f+exp(mean_old));
Lightvalve 173:68c7914679ec 464 deviation_old = log(1.0f+exp(deviation_old));
Lightvalve 170:42c938a40313 465 }
Lightvalve 170:42c938a40313 466
Lightvalve 170:42c938a40313 467 float Grad_Normal_Dist_Mean(float mean, float deviation, float action)
Lightvalve 170:42c938a40313 468 {
Lightvalve 170:42c938a40313 469 float grad_mean = 0.0f;
Lightvalve 170:42c938a40313 470 grad_mean = (action-mean)*exp(-(action-mean)*(action-mean)/(2.0f*deviation*deviation))/(sqrt(2.0f*PI)*deviation*deviation*deviation);
Lightvalve 170:42c938a40313 471 return grad_mean;
Lightvalve 170:42c938a40313 472 }
Lightvalve 170:42c938a40313 473
Lightvalve 170:42c938a40313 474 float Grad_Normal_Dist_Deviation(float mean, float deviation, float action)
Lightvalve 170:42c938a40313 475 {
Lightvalve 170:42c938a40313 476 float grad_dev = 0.0f;
Lightvalve 170:42c938a40313 477 grad_dev = exp(-(action-mean)*(action-mean)/(2.0f*deviation*deviation))*(-1.0f/(sqrt(2.0f*PI)*deviation*deviation) + (action-mean)*(action-mean)/(sqrt(2.0f*PI)*deviation*deviation*deviation*deviation));
Lightvalve 170:42c938a40313 478 return grad_dev;
Lightvalve 170:42c938a40313 479 }
Lightvalve 170:42c938a40313 480
Lightvalve 173:68c7914679ec 481 float ReLU(float x)
Lightvalve 173:68c7914679ec 482 {
Lightvalve 173:68c7914679ec 483 if (x >= 0) {
Lightvalve 173:68c7914679ec 484 return x;
Lightvalve 173:68c7914679ec 485 } else {
Lightvalve 173:68c7914679ec 486 return 0.0f;
Lightvalve 173:68c7914679ec 487 }
Lightvalve 173:68c7914679ec 488 }
Lightvalve 173:68c7914679ec 489
Lightvalve 170:42c938a40313 490 void update_Critic_Networks(float (*arr)[num_input_RL])
Lightvalve 170:42c938a40313 491 {
Lightvalve 172:63af34265fe9 492 float gradient_rate = 0.001f;
Lightvalve 173:68c7914679ec 493 float G_hc1[num_input_RL][num_hidden_unit1] = {0.0f};
Lightvalve 175:2f7289dbd488 494 float d_V_d_hc1[batch_size][num_input_RL][num_hidden_unit1] = {0.0f}; ////////////////1
Lightvalve 175:2f7289dbd488 495 float G_bc1[num_hidden_unit1] = {0.0f};
Lightvalve 175:2f7289dbd488 496 float d_V_d_bc1[batch_size][num_hidden_unit1] = {0.0f}; ////////////////2
Lightvalve 173:68c7914679ec 497 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 170:42c938a40313 498 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 173:68c7914679ec 499 for (int n=0; n<batch_size; n++) {
Lightvalve 173:68c7914679ec 500 for(int k=0; k<num_hidden_unit2; k++) {
Lightvalve 173:68c7914679ec 501 if (hxh_c_sum_array[n][k] >= 0) {
Lightvalve 173:68c7914679ec 502 if (hx_c_sum_array[n][index2] > 0) {
Lightvalve 175:2f7289dbd488 503 //G_hc1[index1][index2] = G_hc1[index1][index2] + arr[n][index1]*hc2_temp[index2][k]*hc3_temp[k]; //////////////////////3
Lightvalve 175:2f7289dbd488 504 d_V_d_hc1[n][index1][index2] = d_V_d_hc1[n][index1][index2] + arr[n][index1]*hc2_temp[index2][k]*hc3_temp[k]; //////////////////////4
Lightvalve 173:68c7914679ec 505 }
Lightvalve 173:68c7914679ec 506 }
Lightvalve 170:42c938a40313 507 }
Lightvalve 175:2f7289dbd488 508 G_hc1[index1][index2] = G_hc1[index1][index2] + 2.0f*(return_G[n]-V[n])*(-d_V_d_hc1[n][index1][index2]); /////////////////////5
Lightvalve 170:42c938a40313 509 }
Lightvalve 170:42c938a40313 510 G_hc1[index1][index2] = G_hc1[index1][index2] / batch_size;
Lightvalve 173:68c7914679ec 511 //hc1_temp[index1][index2] = hc1_temp[index1][index2] - gradient_rate * G_hc1[index1][index2];
Lightvalve 173:68c7914679ec 512 }
Lightvalve 173:68c7914679ec 513 for (int n=0; n<batch_size; n++) {
Lightvalve 173:68c7914679ec 514 for(int k=0; k<num_hidden_unit2; k++) {
Lightvalve 173:68c7914679ec 515 if (hxh_c_sum_array[n][k] >= 0) {
Lightvalve 173:68c7914679ec 516 if (hx_c_sum_array[n][index2] > 0) {
Lightvalve 175:2f7289dbd488 517 //G_bc1[index2] = G_bc1[index2] + hc2_temp[index2][k]*hc3_temp[k]; //////////////////6
Lightvalve 175:2f7289dbd488 518 d_V_d_bc1[n][index2] = d_V_d_bc1[n][index2] + hc2_temp[index2][k]*hc3_temp[k]; //////////////////7
Lightvalve 173:68c7914679ec 519 }
Lightvalve 173:68c7914679ec 520
Lightvalve 173:68c7914679ec 521 }
Lightvalve 173:68c7914679ec 522 }
Lightvalve 175:2f7289dbd488 523 G_bc1[index2] = G_bc1[index2] + 2.0f*(return_G[n]-V[n])*(-d_V_d_bc1[n][index2]); /////////////////////8
Lightvalve 173:68c7914679ec 524 }
Lightvalve 173:68c7914679ec 525 G_bc1[index2] = G_bc1[index2] / batch_size;
Lightvalve 173:68c7914679ec 526 //bc1_temp[index2] = bc1_temp[index2] - gradient_rate * G_bc1[index2];
Lightvalve 173:68c7914679ec 527 }
Lightvalve 173:68c7914679ec 528
Lightvalve 175:2f7289dbd488 529
Lightvalve 173:68c7914679ec 530 float G_hc2[num_hidden_unit1][num_hidden_unit2] = {0.0f};
Lightvalve 175:2f7289dbd488 531 float d_V_d_hc2[batch_size][num_hidden_unit1][num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 532 float G_bc2[num_hidden_unit2] = {0.0f};
Lightvalve 175:2f7289dbd488 533 float d_V_d_bc2[batch_size][num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 534 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 173:68c7914679ec 535 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 173:68c7914679ec 536 for (int n=0; n<batch_size; n++) {
Lightvalve 173:68c7914679ec 537 if (hxh_c_sum_array[n][index2] >= 0) {
Lightvalve 173:68c7914679ec 538 if (hx_c_sum_array[n][index1] > 0) {
Lightvalve 175:2f7289dbd488 539 //G_hc2[index1][index2] = G_hc2[index1][index2] + hx_c_sum_array[n][index1]*hc3_temp[index2];
Lightvalve 175:2f7289dbd488 540 d_V_d_hc2[n][index1][index2] = hx_c_sum_array[n][index1]*hc3_temp[index2];
Lightvalve 173:68c7914679ec 541 }
Lightvalve 173:68c7914679ec 542 }
Lightvalve 175:2f7289dbd488 543 G_hc2[index1][index2] = G_hc2[index1][index2] + 2.0f*(return_G[n]-V[n])*(-d_V_d_hc2[n][index1][index2]);
Lightvalve 173:68c7914679ec 544 }
Lightvalve 173:68c7914679ec 545 G_hc2[index1][index2] = G_hc2[index1][index2] / batch_size;
Lightvalve 173:68c7914679ec 546 //hc2_temp[index1][index2] = hc2_temp[index1][index2] - gradient_rate * G_hc2[index1][index2];
Lightvalve 173:68c7914679ec 547 }
Lightvalve 173:68c7914679ec 548 for (int n=0; n<batch_size; n++) {
Lightvalve 173:68c7914679ec 549 if (hxh_c_sum_array[n][index2] >= 0) {
Lightvalve 175:2f7289dbd488 550 //G_bc2[index2] = G_bc2[index2] + hc3_temp[index2];
Lightvalve 175:2f7289dbd488 551 d_V_d_bc2[n][index2] = hc3_temp[index2];
Lightvalve 173:68c7914679ec 552 }
Lightvalve 175:2f7289dbd488 553 G_bc2[index2] = G_bc2[index2] + 2.0f*(return_G[n]-V[n])*(-d_V_d_bc2[n][index2]);
Lightvalve 173:68c7914679ec 554 }
Lightvalve 173:68c7914679ec 555 G_bc2[index2] = G_bc2[index2] / batch_size;
Lightvalve 173:68c7914679ec 556 //bc2_temp[index2] = bc2_temp[index2] - gradient_rate * G_bc2[index2];
Lightvalve 173:68c7914679ec 557 }
Lightvalve 173:68c7914679ec 558
Lightvalve 173:68c7914679ec 559 float G_hc3[num_hidden_unit2]= {0.0f};
Lightvalve 175:2f7289dbd488 560 float d_V_d_hc3[batch_size][num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 561 float G_bc3 = 0.0f;
Lightvalve 175:2f7289dbd488 562 float d_V_d_bc3[batch_size] = {0.0f};
Lightvalve 173:68c7914679ec 563 for (int index2 = 0; index2 < 1; index2++) {
Lightvalve 173:68c7914679ec 564 for (int index1 = 0; index1 < num_hidden_unit2; index1++) {
Lightvalve 173:68c7914679ec 565 for (int n=0; n<batch_size; n++) {
Lightvalve 173:68c7914679ec 566 if (hxh_c_sum_array[n][index1] >= 0) {
Lightvalve 175:2f7289dbd488 567 //G_hc3[index1] = G_hc3[index1] + hxh_c_sum_array[n][index1];
Lightvalve 175:2f7289dbd488 568 d_V_d_hc3[n][index1] = d_V_d_hc3[n][index1] + hxh_c_sum_array[n][index1];
Lightvalve 173:68c7914679ec 569 }
Lightvalve 175:2f7289dbd488 570 G_hc3[index1] = G_hc3[index1] + 2.0f*(return_G[n]-V[n])*(-d_V_d_hc3[n][index1]);
Lightvalve 173:68c7914679ec 571 }
Lightvalve 173:68c7914679ec 572 G_hc3[index1] = G_hc3[index1] / batch_size;
Lightvalve 173:68c7914679ec 573 //hc3_temp[index1] = hc3_temp[index1] - gradient_rate * G_hc3[index1];
Lightvalve 173:68c7914679ec 574 }
Lightvalve 173:68c7914679ec 575 for (int n=0; n<batch_size; n++) {
Lightvalve 175:2f7289dbd488 576 //G_bc2[index2] = G_bc2[index2] + 1.0f;
Lightvalve 175:2f7289dbd488 577 d_V_d_bc3[n] = 1.0f;
Lightvalve 175:2f7289dbd488 578 G_bc3 = G_bc3 + 2.0f*(return_G[n]-V[n])*(-d_V_d_bc3[n]);
Lightvalve 173:68c7914679ec 579 }
Lightvalve 173:68c7914679ec 580 G_bc3 = G_bc3 / batch_size;
Lightvalve 173:68c7914679ec 581 //bc3_temp = bc3_temp - gradient_rate * G_bc3;
Lightvalve 173:68c7914679ec 582 }
Lightvalve 173:68c7914679ec 583
Lightvalve 173:68c7914679ec 584 // Simultaneous Update
Lightvalve 173:68c7914679ec 585 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 173:68c7914679ec 586 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 170:42c938a40313 587 hc1_temp[index1][index2] = hc1_temp[index1][index2] - gradient_rate * G_hc1[index1][index2];
Lightvalve 170:42c938a40313 588 }
Lightvalve 170:42c938a40313 589 bc1_temp[index2] = bc1_temp[index2] - gradient_rate * G_bc1[index2];
Lightvalve 170:42c938a40313 590 }
Lightvalve 173:68c7914679ec 591 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 173:68c7914679ec 592 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 173:68c7914679ec 593 hc2_temp[index1][index2] = hc2_temp[index1][index2] - gradient_rate * G_hc2[index1][index2];
Lightvalve 173:68c7914679ec 594 }
Lightvalve 173:68c7914679ec 595 bc2_temp[index2] = bc2_temp[index2] - gradient_rate * G_bc2[index2];
Lightvalve 173:68c7914679ec 596 }
Lightvalve 170:42c938a40313 597 for (int index2 = 0; index2 < 1; index2++) {
Lightvalve 173:68c7914679ec 598 for (int index1 = 0; index1 < num_hidden_unit2; index1++) {
Lightvalve 173:68c7914679ec 599 hc3_temp[index1] = hc3_temp[index1] - gradient_rate * G_hc3[index1];
Lightvalve 170:42c938a40313 600 }
Lightvalve 173:68c7914679ec 601 bc3_temp = bc3_temp - gradient_rate * G_bc3;
Lightvalve 170:42c938a40313 602 }
Lightvalve 173:68c7914679ec 603
Lightvalve 170:42c938a40313 604 }
Lightvalve 170:42c938a40313 605
Lightvalve 173:68c7914679ec 606 ///////////////////////////Softplus//////////////////////////////////
Lightvalve 170:42c938a40313 607 void update_Actor_Networks(float (*arr)[num_input_RL])
Lightvalve 170:42c938a40313 608 {
Lightvalve 173:68c7914679ec 609 float gradient_rate = 0.001f;
Lightvalve 173:68c7914679ec 610
Lightvalve 173:68c7914679ec 611 float G_ha1[num_input_RL][num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 612 float G_ba1[num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 613 float d_x_d_ha1[num_input_RL][num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 614 float d_x_d_ba1[num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 615 float d_y_d_ha1[num_input_RL][num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 616 float d_y_d_ba1[num_hidden_unit1] = {0.0f};
Lightvalve 173:68c7914679ec 617
Lightvalve 173:68c7914679ec 618 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 170:42c938a40313 619 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 173:68c7914679ec 620 for (int n=0; n<batch_size; n++) {
Lightvalve 173:68c7914679ec 621 if((advantage[n] >= 0.0f && ratio[n] >= 1.0f + epsilon) || (advantage[n] < 0.0f && ratio[n] < 1.0f - epsilon)) {
Lightvalve 170:42c938a40313 622 G_ha1[index1][index2] = G_ha1[index1][index2];
Lightvalve 170:42c938a40313 623 } else {
Lightvalve 173:68c7914679ec 624 for(int k=0; k<num_hidden_unit2; k++) {
Lightvalve 173:68c7914679ec 625 if (hxh_a_sum_array[n][k] >= 0) {
Lightvalve 173:68c7914679ec 626 if (hx_a_sum_array[n][index2] > 0) {
Lightvalve 173:68c7914679ec 627 d_x_d_ha1[index1][index2] = d_x_d_ha1[index1][index2] + arr[n][index1]*ha2_temp[index2][k]*ha3_temp[k][0];
Lightvalve 173:68c7914679ec 628 d_y_d_ha1[index1][index2] = d_y_d_ha1[index1][index2] + arr[n][index1]*ha2_temp[index2][k]*ha3_temp[k][1];
Lightvalve 173:68c7914679ec 629 }
Lightvalve 171:bfc1fd2629d8 630 }
Lightvalve 170:42c938a40313 631 }
Lightvalve 170:42c938a40313 632 float d_mean_d_ha1 = 0.0f;
Lightvalve 170:42c938a40313 633 float d_dev_d_ha1 = 0.0f;
Lightvalve 173:68c7914679ec 634 d_mean_d_ha1 = exp(hxhh_a_sum_array[n][0])/(1.0f+exp(hxhh_a_sum_array[n][0]))*d_x_d_ha1[index1][index2];
Lightvalve 173:68c7914679ec 635 d_dev_d_ha1 = exp(hxhh_a_sum_array[n][1])/(1.0f+exp(hxhh_a_sum_array[n][1]))*d_y_d_ha1[index1][index2];
Lightvalve 173:68c7914679ec 636
Lightvalve 173:68c7914679ec 637 G_ha1[index1][index2] = G_ha1[index1][index2] + advantage[n]/pi_old[n]*(d_mean_d_ha1*Grad_Normal_Dist_Mean(mean_array[n],deviation_array[n],action_array[n])+d_dev_d_ha1*Grad_Normal_Dist_Deviation(mean_array[n],deviation_array[n],action_array[n]));
Lightvalve 170:42c938a40313 638 }
Lightvalve 170:42c938a40313 639 }
Lightvalve 170:42c938a40313 640 G_ha1[index1][index2] = G_ha1[index1][index2] / batch_size;
Lightvalve 175:2f7289dbd488 641 //ha1_temp[index1][index2] = ha1_temp[index1][index2] - gradient_rate * G_ha1[index1][index2];
Lightvalve 170:42c938a40313 642 }
Lightvalve 173:68c7914679ec 643
Lightvalve 173:68c7914679ec 644 for (int n=0; n<batch_size; n++) {
Lightvalve 173:68c7914679ec 645 if((advantage[n] >= 0.0f && ratio[n] >= 1.0f + epsilon) || (advantage[n] < 0.0f && ratio[n] < 1.0f - epsilon)) {
Lightvalve 170:42c938a40313 646 G_ba1[index2] = G_ba1[index2];
Lightvalve 170:42c938a40313 647 } else {
Lightvalve 173:68c7914679ec 648 for(int k=0; k<num_hidden_unit2; k++) {
Lightvalve 173:68c7914679ec 649 if (hxh_a_sum_array[n][k] >= 0) {
Lightvalve 173:68c7914679ec 650 if (hx_a_sum_array[n][index2] > 0) {
Lightvalve 173:68c7914679ec 651 d_x_d_ba1[index2] = d_x_d_ba1[index2] + ha2_temp[index2][k]*ha3_temp[k][0];
Lightvalve 173:68c7914679ec 652 d_y_d_ba1[index2] = d_y_d_ba1[index2] + ha2_temp[index2][k]*ha3_temp[k][1];
Lightvalve 173:68c7914679ec 653 }
Lightvalve 171:bfc1fd2629d8 654 }
Lightvalve 170:42c938a40313 655 }
Lightvalve 170:42c938a40313 656 float d_mean_d_ba1 = 0.0f;
Lightvalve 170:42c938a40313 657 float d_dev_d_ba1 = 0.0f;
Lightvalve 173:68c7914679ec 658 d_mean_d_ba1 = exp(hxhh_a_sum_array[n][0])/(1.0f+exp(hxhh_a_sum_array[n][0]))*d_x_d_ba1[index2];
Lightvalve 173:68c7914679ec 659 d_dev_d_ba1 = exp(hxhh_a_sum_array[n][1])/(1.0f+exp(hxhh_a_sum_array[n][1]))*d_y_d_ba1[index2];
Lightvalve 173:68c7914679ec 660
Lightvalve 173:68c7914679ec 661 G_ba1[index2] = G_ba1[index2] + advantage[n]/pi_old[n]*(d_mean_d_ba1*Grad_Normal_Dist_Mean(mean_array[n],deviation_array[n],action_array[n])+d_dev_d_ba1*Grad_Normal_Dist_Deviation(mean_array[n],deviation_array[n],action_array[n]));
Lightvalve 170:42c938a40313 662 }
Lightvalve 170:42c938a40313 663 }
Lightvalve 170:42c938a40313 664 G_ba1[index2] = G_ba1[index2] / batch_size;
Lightvalve 175:2f7289dbd488 665 //ba1_temp[index2] = ba1_temp[index2] - gradient_rate * G_ba1[index2];
Lightvalve 170:42c938a40313 666 }
Lightvalve 170:42c938a40313 667
Lightvalve 173:68c7914679ec 668 float G_ha2[num_hidden_unit1][num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 669 float G_ba2[num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 670 float d_x_d_ha2[num_hidden_unit1][num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 671 float d_x_d_ba2[num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 672 float d_y_d_ha2[num_hidden_unit1][num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 673 float d_y_d_ba2[num_hidden_unit2] = {0.0f};
Lightvalve 173:68c7914679ec 674
Lightvalve 173:68c7914679ec 675 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 173:68c7914679ec 676 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 173:68c7914679ec 677 for (int n=0; n<batch_size; n++) {
Lightvalve 173:68c7914679ec 678 if((advantage[n] >= 0.0f && ratio[n] >= 1.0f + epsilon) || (advantage[n] < 0.0f && ratio[n] < 1.0f - epsilon)) {
Lightvalve 170:42c938a40313 679 G_ha2[index1][index2] = G_ha2[index1][index2];
Lightvalve 170:42c938a40313 680 } else {
Lightvalve 171:bfc1fd2629d8 681
Lightvalve 173:68c7914679ec 682 if (hxh_a_sum_array[n][index2] >= 0) {
Lightvalve 173:68c7914679ec 683 if (hx_a_sum_array[n][index1] > 0) {
Lightvalve 173:68c7914679ec 684 d_x_d_ha2[index1][index2] = d_x_d_ha2[index1][index2] + hx_a_sum_array[n][index1]*ha3_temp[index2][0];
Lightvalve 173:68c7914679ec 685 d_y_d_ha2[index1][index2] = d_y_d_ha2[index1][index2] + hx_a_sum_array[n][index1]*ha3_temp[index2][1];
Lightvalve 171:bfc1fd2629d8 686 }
Lightvalve 170:42c938a40313 687 }
Lightvalve 173:68c7914679ec 688
Lightvalve 170:42c938a40313 689 float d_mean_d_ha2 = 0.0f;
Lightvalve 170:42c938a40313 690 float d_dev_d_ha2 = 0.0f;
Lightvalve 173:68c7914679ec 691 d_mean_d_ha2 = exp(hxhh_a_sum_array[n][0])/(1.0f+exp(hxhh_a_sum_array[n][0]))*d_x_d_ha2[index1][index2];
Lightvalve 173:68c7914679ec 692 d_dev_d_ha2 = exp(hxhh_a_sum_array[n][1])/(1.0f+exp(hxhh_a_sum_array[n][1]))*d_y_d_ha2[index1][index2];
Lightvalve 173:68c7914679ec 693
Lightvalve 173:68c7914679ec 694 G_ha2[index1][index2] = G_ha2[index1][index2] + advantage[n]/pi_old[n]*(d_mean_d_ha2*Grad_Normal_Dist_Mean(mean_array[n],deviation_array[n],action_array[n])+d_dev_d_ha2*Grad_Normal_Dist_Deviation(mean_array[n],deviation_array[n],action_array[n]));
Lightvalve 170:42c938a40313 695 }
Lightvalve 170:42c938a40313 696 }
Lightvalve 170:42c938a40313 697 G_ha2[index1][index2] = G_ha2[index1][index2] / batch_size;
Lightvalve 175:2f7289dbd488 698 //ha2_temp[index1][index2] = ha2_temp[index1][index2] - gradient_rate * G_ha2[index1][index2];
Lightvalve 170:42c938a40313 699 }
Lightvalve 173:68c7914679ec 700
Lightvalve 173:68c7914679ec 701 for (int n=0; n<batch_size; n++) {
Lightvalve 173:68c7914679ec 702 if((advantage[n] >= 0.0f && ratio[n] >= 1.0f + epsilon) || (advantage[n] < 0.0f && ratio[n] < 1.0f - epsilon)) {
Lightvalve 170:42c938a40313 703 G_ba2[index2] = G_ba2[index2];
Lightvalve 170:42c938a40313 704 } else {
Lightvalve 170:42c938a40313 705
Lightvalve 173:68c7914679ec 706 if (hxh_a_sum_array[n][index2] >= 0) {
Lightvalve 173:68c7914679ec 707 d_x_d_ba2[index2] = d_x_d_ba2[index2] + ha3_temp[index2][0];
Lightvalve 173:68c7914679ec 708 d_y_d_ba2[index2] = d_y_d_ba2[index2] + ha3_temp[index2][1];
Lightvalve 173:68c7914679ec 709 }
Lightvalve 173:68c7914679ec 710 float d_mean_d_ba2= 0.0f;
Lightvalve 173:68c7914679ec 711 float d_dev_d_ba2= 0.0f;
Lightvalve 173:68c7914679ec 712 d_mean_d_ba2 = exp(hxhh_a_sum_array[n][0])/(1.0f+exp(hxhh_a_sum_array[n][0]))*d_x_d_ba2[index2];
Lightvalve 173:68c7914679ec 713 d_dev_d_ba2 = exp(hxhh_a_sum_array[n][1])/(1.0f+exp(hxhh_a_sum_array[n][1]))*d_y_d_ba2[index2];
Lightvalve 173:68c7914679ec 714
Lightvalve 173:68c7914679ec 715 G_ba2[index2] = G_ba2[index2] + advantage[n]/pi_old[n]*(d_mean_d_ba2*Grad_Normal_Dist_Mean(mean_array[n],deviation_array[n],action_array[n])+d_dev_d_ba2*Grad_Normal_Dist_Deviation(mean_array[n],deviation_array[n],action_array[n]));
Lightvalve 170:42c938a40313 716 }
Lightvalve 170:42c938a40313 717 }
Lightvalve 170:42c938a40313 718 G_ba2[index2] = G_ba2[index2] / batch_size;
Lightvalve 175:2f7289dbd488 719 //ba2_temp[index2] = ba2_temp[index2] - gradient_rate * G_ba2[index2];
Lightvalve 170:42c938a40313 720 }
Lightvalve 173:68c7914679ec 721
Lightvalve 173:68c7914679ec 722 float G_ha3[num_hidden_unit2][2] = {0.0f};
Lightvalve 173:68c7914679ec 723 float G_ba3[2] = {0.0f};
Lightvalve 173:68c7914679ec 724 float d_x_d_ha3[num_hidden_unit2][2] = {0.0f};
Lightvalve 173:68c7914679ec 725 float d_x_d_ba3[2] = {0.0f};
Lightvalve 173:68c7914679ec 726 float d_y_d_ha3[num_hidden_unit2][2] = {0.0f};
Lightvalve 173:68c7914679ec 727 float d_y_d_ba3[2] = {0.0f};
Lightvalve 173:68c7914679ec 728
Lightvalve 173:68c7914679ec 729 for (int index2 = 0; index2 < 2; index2++) {
Lightvalve 173:68c7914679ec 730 for (int index1 = 0; index1 < num_hidden_unit2; index1++) {
Lightvalve 173:68c7914679ec 731 for (int n=0; n<batch_size; n++) {
Lightvalve 173:68c7914679ec 732 if((advantage[n] >= 0.0f && ratio[n] >= 1.0f + epsilon) || (advantage[n] < 0.0f && ratio[n] < 1.0f - epsilon)) {
Lightvalve 173:68c7914679ec 733 G_ha3[index1][index2] = G_ha3[index1][index2];
Lightvalve 173:68c7914679ec 734 } else {
Lightvalve 173:68c7914679ec 735
Lightvalve 173:68c7914679ec 736 if (hxh_a_sum_array[n][index1] >= 0) {
Lightvalve 173:68c7914679ec 737 if (hx_a_sum_array[n][index1] > 0) {
Lightvalve 173:68c7914679ec 738 d_x_d_ha3[index1][index2] = d_x_d_ha3[index1][index2] + hxh_a_sum_array[n][index1];
Lightvalve 173:68c7914679ec 739 d_y_d_ha3[index1][index2] = d_y_d_ha3[index1][index2] + hxh_a_sum_array[n][index1];
Lightvalve 173:68c7914679ec 740 }
Lightvalve 173:68c7914679ec 741 }
Lightvalve 173:68c7914679ec 742 float d_mean_d_ha3 = 0.0f;
Lightvalve 173:68c7914679ec 743 float d_dev_d_ha3 = 0.0f;
Lightvalve 173:68c7914679ec 744 d_mean_d_ha3 = exp(hxhh_a_sum_array[n][0])/(1.0f+exp(hxhh_a_sum_array[n][0]))*d_x_d_ha3[index1][index2];
Lightvalve 173:68c7914679ec 745 d_dev_d_ha3 = exp(hxhh_a_sum_array[n][1])/(1.0f+exp(hxhh_a_sum_array[n][1]))*d_y_d_ha3[index1][index2];
Lightvalve 173:68c7914679ec 746
Lightvalve 173:68c7914679ec 747 G_ha3[index1][index2] = G_ha3[index1][index2] + advantage[n]/pi_old[n]*(d_mean_d_ha3*Grad_Normal_Dist_Mean(mean_array[n],deviation_array[n],action_array[n])+d_dev_d_ha3*Grad_Normal_Dist_Deviation(mean_array[n],deviation_array[n],action_array[n]));
Lightvalve 173:68c7914679ec 748 }
Lightvalve 173:68c7914679ec 749 }
Lightvalve 173:68c7914679ec 750 G_ha3[index1][index2] = G_ha3[index1][index2] / batch_size;
Lightvalve 175:2f7289dbd488 751 //ha3_temp[index1][index2] = ha3_temp[index1][index2] - gradient_rate * G_ha3[index1][index2];
Lightvalve 173:68c7914679ec 752 }
Lightvalve 173:68c7914679ec 753
Lightvalve 173:68c7914679ec 754 for (int n=0; n<batch_size; n++) {
Lightvalve 173:68c7914679ec 755 if((advantage[n] >= 0.0f && ratio[n] >= 1.0f + epsilon) || (advantage[n] < 0.0f && ratio[n] < 1.0f - epsilon)) {
Lightvalve 173:68c7914679ec 756 G_ba3[index2] = G_ba3[index2];
Lightvalve 173:68c7914679ec 757 } else {
Lightvalve 173:68c7914679ec 758
Lightvalve 173:68c7914679ec 759 d_x_d_ba3[index2] = d_x_d_ba3[index2] + 1.0f;
Lightvalve 173:68c7914679ec 760 d_y_d_ba3[index2] = d_y_d_ba3[index2] + 1.0f;
Lightvalve 173:68c7914679ec 761
Lightvalve 173:68c7914679ec 762 float d_mean_d_ba3= 0.0f;
Lightvalve 173:68c7914679ec 763 float d_dev_d_ba3= 0.0f;
Lightvalve 173:68c7914679ec 764 d_mean_d_ba3 = exp(hxhh_a_sum_array[n][0])/(1.0f+exp(hxhh_a_sum_array[n][0]))*d_x_d_ba3[index2];
Lightvalve 173:68c7914679ec 765 d_dev_d_ba3 = exp(hxhh_a_sum_array[n][1])/(1.0f+exp(hxhh_a_sum_array[n][1]))*d_y_d_ba3[index2];
Lightvalve 173:68c7914679ec 766
Lightvalve 173:68c7914679ec 767 G_ba3[index2] = G_ba3[index2] + advantage[n]/pi_old[n]*(d_mean_d_ba3*Grad_Normal_Dist_Mean(mean_array[n],deviation_array[n],action_array[n])+d_dev_d_ba3*Grad_Normal_Dist_Deviation(mean_array[n],deviation_array[n],action_array[n]));
Lightvalve 173:68c7914679ec 768 }
Lightvalve 173:68c7914679ec 769 }
Lightvalve 173:68c7914679ec 770 G_ba3[index2] = G_ba3[index2] / batch_size;
Lightvalve 175:2f7289dbd488 771 //ba3_temp[index2] = ba3_temp[index2] - gradient_rate * G_ba3[index2];
Lightvalve 175:2f7289dbd488 772 }
Lightvalve 175:2f7289dbd488 773
Lightvalve 175:2f7289dbd488 774 // Simultaneous Update
Lightvalve 175:2f7289dbd488 775 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 175:2f7289dbd488 776 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 175:2f7289dbd488 777 ha1_temp[index1][index2] = ha1_temp[index1][index2] - gradient_rate * G_ha1[index1][index2];
Lightvalve 175:2f7289dbd488 778 }
Lightvalve 175:2f7289dbd488 779 ba1_temp[index2] = ba1_temp[index2] - gradient_rate * G_ba1[index2];
Lightvalve 175:2f7289dbd488 780 }
Lightvalve 175:2f7289dbd488 781 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 175:2f7289dbd488 782 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 175:2f7289dbd488 783 ha2_temp[index1][index2] = ha2_temp[index1][index2] - gradient_rate * G_ha2[index1][index2];
Lightvalve 175:2f7289dbd488 784 }
Lightvalve 175:2f7289dbd488 785 ba2_temp[index2] = ba2_temp[index2] - gradient_rate * G_ba2[index2];
Lightvalve 175:2f7289dbd488 786 }
Lightvalve 175:2f7289dbd488 787 for (int index2 = 0; index2 < 2; index2++) {
Lightvalve 175:2f7289dbd488 788 for (int index1 = 0; index1 < num_hidden_unit2; index1++) {
Lightvalve 175:2f7289dbd488 789 ha3_temp[index1][index2] = ha3_temp[index1][index2] - gradient_rate * G_ha3[index1][index2];
Lightvalve 175:2f7289dbd488 790 }
Lightvalve 173:68c7914679ec 791 ba3_temp[index2] = ba3_temp[index2] - gradient_rate * G_ba3[index2];
Lightvalve 173:68c7914679ec 792 }
Lightvalve 175:2f7289dbd488 793
Lightvalve 170:42c938a40313 794 }
Lightvalve 170:42c938a40313 795
Lightvalve 173:68c7914679ec 796 ///////////////////////////ReLU - Bad performance//////////////////////////////////
Lightvalve 173:68c7914679ec 797 //void update_Actor_Networks(float (*arr)[num_input_RL])
Lightvalve 173:68c7914679ec 798 //{
Lightvalve 173:68c7914679ec 799 // float gradient_rate = 0.001f; //-0.01f
Lightvalve 173:68c7914679ec 800 //
Lightvalve 173:68c7914679ec 801 // float G_ha1[num_input_RL][num_hidden_unit] = {0.0f};
Lightvalve 173:68c7914679ec 802 // float G_ba1[num_hidden_unit] = {0.0f};
Lightvalve 173:68c7914679ec 803 // for (int index2 = 0; index2 < num_hidden_unit; index2++) {
Lightvalve 173:68c7914679ec 804 // for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 173:68c7914679ec 805 // for (int i=0; i<batch_size; i++) {
Lightvalve 173:68c7914679ec 806 // if((advantage[i] >= 0.0f && ratio[i] >= 1.0f + epsilon) || (advantage[i] < 0.0f && ratio[i] < 1.0f - epsilon)) {
Lightvalve 173:68c7914679ec 807 // G_ha1[index1][index2] = G_ha1[index1][index2];
Lightvalve 173:68c7914679ec 808 // } else {
Lightvalve 173:68c7914679ec 809 //
Lightvalve 173:68c7914679ec 810 // float hx_sum_total = 0.0f;
Lightvalve 173:68c7914679ec 811 // for(int m = 0; m < num_hidden_unit; m++) {
Lightvalve 173:68c7914679ec 812 // for(int n = 0; n < num_input_RL; n++) {
Lightvalve 173:68c7914679ec 813 // hx_sum_total = hx_sum_total + ha1_temp[n][m]*arr[i][n];
Lightvalve 173:68c7914679ec 814 // }
Lightvalve 173:68c7914679ec 815 // }
Lightvalve 173:68c7914679ec 816 // hx_sum_total = hx_sum_total + bc1_temp[index2];
Lightvalve 173:68c7914679ec 817 // float d_mean_d_ha1 = 0.0f;
Lightvalve 173:68c7914679ec 818 // float d_dev_d_ha1 = 0.0f;
Lightvalve 173:68c7914679ec 819 // if (hx_sum_total >=0) {
Lightvalve 173:68c7914679ec 820 // float hx_sum = 0.0f;
Lightvalve 173:68c7914679ec 821 // for(int j=0; j<num_input_RL; j++) {
Lightvalve 173:68c7914679ec 822 // hx_sum = hx_sum + ha1_temp[j][index2]*arr[i][j];
Lightvalve 173:68c7914679ec 823 // }
Lightvalve 173:68c7914679ec 824 // hx_sum = hx_sum + bc1_temp[index2];
Lightvalve 173:68c7914679ec 825 // if (hx_sum >= 0) {
Lightvalve 173:68c7914679ec 826 // d_mean_d_ha1 = ha2_temp[index2][0]*arr[i][index1];
Lightvalve 173:68c7914679ec 827 // d_dev_d_ha1 = ha2_temp[index2][1]*arr[i][index1];
Lightvalve 173:68c7914679ec 828 // } else {
Lightvalve 173:68c7914679ec 829 // d_mean_d_ha1 = 0.0f;
Lightvalve 173:68c7914679ec 830 // d_dev_d_ha1 = 0.0f;
Lightvalve 173:68c7914679ec 831 // }
Lightvalve 173:68c7914679ec 832 // } else {
Lightvalve 173:68c7914679ec 833 // d_mean_d_ha1 = 0.0f;
Lightvalve 173:68c7914679ec 834 // d_dev_d_ha1 = 0.0f;
Lightvalve 173:68c7914679ec 835 // }
Lightvalve 173:68c7914679ec 836 // G_ha1[index1][index2] = G_ha1[index1][index2] + advantage[i]/pi_old[i]*(d_mean_d_ha1*Grad_Normal_Dist_Mean(mean_array[i],deviation_array[i],action_array[i])+d_dev_d_ha1*Grad_Normal_Dist_Deviation(mean_array[i],deviation_array[i],action_array[i]));
Lightvalve 173:68c7914679ec 837 // }
Lightvalve 173:68c7914679ec 838 // }
Lightvalve 173:68c7914679ec 839 // G_ha1[index1][index2] = G_ha1[index1][index2] / batch_size;
Lightvalve 173:68c7914679ec 840 // ha1_temp[index1][index2] = ha1_temp[index1][index2] - gradient_rate * G_ha1[index1][index2];
Lightvalve 173:68c7914679ec 841 // }
Lightvalve 173:68c7914679ec 842 // for (int i=0; i<batch_size; i++) {
Lightvalve 173:68c7914679ec 843 // if((advantage[i] >= 0.0f && ratio[i] >= 1.0f + epsilon) || (advantage[i] < 0.0f && ratio[i] < 1.0f - epsilon)) {
Lightvalve 173:68c7914679ec 844 // G_ba1[index2] = G_ba1[index2];
Lightvalve 173:68c7914679ec 845 // } else {
Lightvalve 173:68c7914679ec 846 //
Lightvalve 173:68c7914679ec 847 // float hx_sum_total = 0.0f;
Lightvalve 173:68c7914679ec 848 // for(int m = 0; m < num_hidden_unit; m++) {
Lightvalve 173:68c7914679ec 849 // for(int n = 0; n < num_input_RL; n++) {
Lightvalve 173:68c7914679ec 850 // hx_sum_total = hx_sum_total + ha1_temp[n][m]*arr[i][n];
Lightvalve 173:68c7914679ec 851 // }
Lightvalve 173:68c7914679ec 852 // }
Lightvalve 173:68c7914679ec 853 // hx_sum_total = hx_sum_total + bc1_temp[index2];
Lightvalve 173:68c7914679ec 854 // float d_mean_d_ba1 = 0.0f;
Lightvalve 173:68c7914679ec 855 // float d_dev_d_ba1 = 0.0f;
Lightvalve 173:68c7914679ec 856 // if (hx_sum_total >=0) {
Lightvalve 173:68c7914679ec 857 //
Lightvalve 173:68c7914679ec 858 // float hx_sum = 0.0f;
Lightvalve 173:68c7914679ec 859 // for(int j=0; j<num_input_RL; j++) {
Lightvalve 173:68c7914679ec 860 // hx_sum = hx_sum + ha1_temp[j][index2]*arr[i][j];
Lightvalve 173:68c7914679ec 861 // }
Lightvalve 173:68c7914679ec 862 // hx_sum = hx_sum + bc1_temp[index2];
Lightvalve 173:68c7914679ec 863 //
Lightvalve 173:68c7914679ec 864 // if(hx_sum >=0) {
Lightvalve 173:68c7914679ec 865 // d_mean_d_ba1 = ha2_temp[index2][0];
Lightvalve 173:68c7914679ec 866 // d_dev_d_ba1 = ha2_temp[index2][1];
Lightvalve 173:68c7914679ec 867 // } else {
Lightvalve 173:68c7914679ec 868 // d_mean_d_ba1 = 0.0f;
Lightvalve 173:68c7914679ec 869 // d_dev_d_ba1 = 0.0f;
Lightvalve 173:68c7914679ec 870 // }
Lightvalve 173:68c7914679ec 871 // } else {
Lightvalve 173:68c7914679ec 872 // d_mean_d_ba1 = 0.0f;
Lightvalve 173:68c7914679ec 873 // d_dev_d_ba1 = 0.0f;
Lightvalve 173:68c7914679ec 874 // }
Lightvalve 173:68c7914679ec 875 // G_ba1[index2] = G_ba1[index2] + advantage[i]/pi_old[i]*(d_mean_d_ba1*Grad_Normal_Dist_Mean(mean_array[i],deviation_array[i],action_array[i])+d_dev_d_ba1*Grad_Normal_Dist_Deviation(mean_array[i],deviation_array[i],action_array[i]));
Lightvalve 173:68c7914679ec 876 // }
Lightvalve 173:68c7914679ec 877 // }
Lightvalve 173:68c7914679ec 878 // G_ba1[index2] = G_ba1[index2] / batch_size;
Lightvalve 173:68c7914679ec 879 // ba1_temp[index2] = ba1_temp[index2] - gradient_rate * G_ba1[index2];
Lightvalve 173:68c7914679ec 880 // }
Lightvalve 173:68c7914679ec 881 //
Lightvalve 173:68c7914679ec 882 // float G_ha2[num_hidden_unit][2] = {0.0f};
Lightvalve 173:68c7914679ec 883 // float G_ba2[2] = {0.0f};
Lightvalve 173:68c7914679ec 884 // for (int index2 = 0; index2 < 2; index2++) {
Lightvalve 173:68c7914679ec 885 // for (int index1 = 0; index1 < num_hidden_unit; index1++) {
Lightvalve 173:68c7914679ec 886 // for (int i=0; i<batch_size; i++) {
Lightvalve 173:68c7914679ec 887 // if((advantage[i] >= 0.0f && ratio[i] >= 1.0f + epsilon) || (advantage[i] < 0.0f && ratio[i] < 1.0f - epsilon)) {
Lightvalve 173:68c7914679ec 888 // G_ha2[index1][index2] = G_ha2[index1][index2];
Lightvalve 173:68c7914679ec 889 // } else {
Lightvalve 173:68c7914679ec 890 //
Lightvalve 173:68c7914679ec 891 // float hx_sum_total = 0.0f;
Lightvalve 173:68c7914679ec 892 // for(int m = 0; m < num_hidden_unit; m++) {
Lightvalve 173:68c7914679ec 893 // for(int n = 0; n < num_input_RL; n++) {
Lightvalve 173:68c7914679ec 894 // hx_sum_total = hx_sum_total + ha1_temp[n][m]*arr[i][n];
Lightvalve 173:68c7914679ec 895 // }
Lightvalve 173:68c7914679ec 896 // }
Lightvalve 173:68c7914679ec 897 // hx_sum_total = hx_sum_total + bc1_temp[index2];
Lightvalve 173:68c7914679ec 898 // float d_mean_d_ha2 = 0.0f;
Lightvalve 173:68c7914679ec 899 // float d_dev_d_ha2 = 0.0f;
Lightvalve 173:68c7914679ec 900 // if (hx_sum_total >=0) {
Lightvalve 173:68c7914679ec 901 // float hx_sum = 0.0f;
Lightvalve 173:68c7914679ec 902 // for(int j=0; j<num_input_RL; j++) {
Lightvalve 173:68c7914679ec 903 // hx_sum = hx_sum + ha1_temp[j][index1]*arr[i][j];
Lightvalve 173:68c7914679ec 904 // }
Lightvalve 173:68c7914679ec 905 // hx_sum = hx_sum + bc1_temp[index1];
Lightvalve 173:68c7914679ec 906 // if (hx_sum >= 0) {
Lightvalve 173:68c7914679ec 907 // d_mean_d_ha2 = hx_sum;
Lightvalve 173:68c7914679ec 908 // d_dev_d_ha2 = hx_sum;
Lightvalve 173:68c7914679ec 909 // } else {
Lightvalve 173:68c7914679ec 910 // d_mean_d_ha2 = 0.0f;
Lightvalve 173:68c7914679ec 911 // d_mean_d_ha2 = 0.0f;
Lightvalve 173:68c7914679ec 912 // }
Lightvalve 173:68c7914679ec 913 // } else {
Lightvalve 173:68c7914679ec 914 // d_mean_d_ha2 = 0.0f;
Lightvalve 173:68c7914679ec 915 // d_mean_d_ha2 = 0.0f;
Lightvalve 173:68c7914679ec 916 // }
Lightvalve 173:68c7914679ec 917 // G_ha2[index1][index2] = G_ha2[index1][index2] + advantage[i]/pi_old[i]*(d_mean_d_ha2*Grad_Normal_Dist_Mean(mean_array[i],deviation_array[i],action_array[i])+d_dev_d_ha2*Grad_Normal_Dist_Deviation(mean_array[i],deviation_array[i],action_array[i]));
Lightvalve 173:68c7914679ec 918 // }
Lightvalve 173:68c7914679ec 919 // }
Lightvalve 173:68c7914679ec 920 // G_ha2[index1][index2] = G_ha2[index1][index2] / batch_size;
Lightvalve 173:68c7914679ec 921 // ha2_temp[index1][index2] = ha2_temp[index1][index2] - gradient_rate * G_ha2[index1][index2];
Lightvalve 173:68c7914679ec 922 // }
Lightvalve 173:68c7914679ec 923 // for (int i=0; i<batch_size; i++) {
Lightvalve 173:68c7914679ec 924 // if((advantage[i] >= 0.0f && ratio[i] >= 1.0f + epsilon) || (advantage[i] < 0.0f && ratio[i] < 1.0f - epsilon)) {
Lightvalve 173:68c7914679ec 925 // G_ba2[index2] = G_ba2[index2];
Lightvalve 173:68c7914679ec 926 // } else {
Lightvalve 173:68c7914679ec 927 //
Lightvalve 173:68c7914679ec 928 // float d_mean_d_ba2 = 0.0f;
Lightvalve 173:68c7914679ec 929 // float d_dev_d_ba2 = 0.0f;
Lightvalve 173:68c7914679ec 930 // d_mean_d_ba2 = 1.0f;
Lightvalve 173:68c7914679ec 931 // d_dev_d_ba2 = 1.0f;
Lightvalve 173:68c7914679ec 932 // G_ba1[index2] = G_ba1[index2] + advantage[i]/pi_old[i]*(d_mean_d_ba2*Grad_Normal_Dist_Mean(mean_array[i],deviation_array[i],action_array[i])+d_dev_d_ba2*Grad_Normal_Dist_Deviation(mean_array[i],deviation_array[i],action_array[i]));
Lightvalve 173:68c7914679ec 933 // }
Lightvalve 173:68c7914679ec 934 // }
Lightvalve 173:68c7914679ec 935 // G_ba2[index2] = G_ba2[index2] / batch_size;
Lightvalve 173:68c7914679ec 936 // ba2_temp[index2] = ba2_temp[index2] - gradient_rate * G_ba2[index2];
Lightvalve 173:68c7914679ec 937 // }
Lightvalve 173:68c7914679ec 938 //}
Lightvalve 173:68c7914679ec 939
Lightvalve 170:42c938a40313 940
Lightvalve 170:42c938a40313 941
Lightvalve 170:42c938a40313 942 float rand_normal(double mean, double stddev)
Lightvalve 170:42c938a40313 943 {
Lightvalve 170:42c938a40313 944 //Box muller method
Lightvalve 170:42c938a40313 945 static double n2 = 0.0f;
Lightvalve 170:42c938a40313 946 static int n2_cached = 0;
Lightvalve 170:42c938a40313 947 if (!n2_cached) {
Lightvalve 170:42c938a40313 948 double x, y, r;
Lightvalve 170:42c938a40313 949 do {
Lightvalve 170:42c938a40313 950 x = 2.0f*rand()/RAND_MAX - 1;
Lightvalve 170:42c938a40313 951 y = 2.0f*rand()/RAND_MAX - 1;
Lightvalve 170:42c938a40313 952
Lightvalve 170:42c938a40313 953 r = x*x + y*y;
Lightvalve 170:42c938a40313 954 } while (r == 0.0f || r > 1.0f);
Lightvalve 170:42c938a40313 955 {
Lightvalve 170:42c938a40313 956 double d = sqrt(-2.0f*log(r)/r);
Lightvalve 170:42c938a40313 957 double n1 = x*d;
Lightvalve 170:42c938a40313 958 n2 = y*d;
Lightvalve 170:42c938a40313 959 double result = n1*stddev + mean;
Lightvalve 170:42c938a40313 960 n2_cached = 1;
Lightvalve 170:42c938a40313 961 return result;
Lightvalve 170:42c938a40313 962 }
Lightvalve 170:42c938a40313 963 } else {
Lightvalve 170:42c938a40313 964 n2_cached = 0;
Lightvalve 170:42c938a40313 965 return n2*stddev + mean;
Lightvalve 170:42c938a40313 966 }
Lightvalve 170:42c938a40313 967 }
Lightvalve 170:42c938a40313 968
Lightvalve 173:68c7914679ec 969
Lightvalve 170:42c938a40313 970 void Overwirte_Critic_Networks()
Lightvalve 170:42c938a40313 971 {
Lightvalve 173:68c7914679ec 972 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 170:42c938a40313 973 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 170:42c938a40313 974 hc1[index1][index2] = hc1_temp[index1][index2];
Lightvalve 170:42c938a40313 975 }
Lightvalve 170:42c938a40313 976 bc1[index2] = bc1_temp[index2];
Lightvalve 170:42c938a40313 977 }
Lightvalve 173:68c7914679ec 978 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 173:68c7914679ec 979 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 173:68c7914679ec 980 hc2[index1][index2] = hc2_temp[index1][index2];
Lightvalve 173:68c7914679ec 981 }
Lightvalve 173:68c7914679ec 982 bc2[index2] = bc2_temp[index2];
Lightvalve 173:68c7914679ec 983 hc3[index2] = hc3_temp[index2];
Lightvalve 173:68c7914679ec 984 }
Lightvalve 173:68c7914679ec 985 bc3 = bc3_temp;
Lightvalve 170:42c938a40313 986 }
Lightvalve 170:42c938a40313 987 void Overwirte_Actor_Networks()
Lightvalve 170:42c938a40313 988 {
Lightvalve 173:68c7914679ec 989 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 170:42c938a40313 990 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 170:42c938a40313 991 ha1[index1][index2] = ha1_temp[index1][index2];
Lightvalve 170:42c938a40313 992 }
Lightvalve 170:42c938a40313 993 ba1[index2] = ba1_temp[index2];
Lightvalve 170:42c938a40313 994 }
Lightvalve 173:68c7914679ec 995 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 173:68c7914679ec 996 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 170:42c938a40313 997 ha2[index1][index2] = ha2_temp[index1][index2];
Lightvalve 170:42c938a40313 998 }
Lightvalve 170:42c938a40313 999 ba2[index2] = ba2_temp[index2];
Lightvalve 170:42c938a40313 1000 }
Lightvalve 173:68c7914679ec 1001 for (int index2 = 0; index2 < 2; index2++) {
Lightvalve 173:68c7914679ec 1002 for (int index1 = 0; index1 < num_hidden_unit2; index1++) {
Lightvalve 173:68c7914679ec 1003 ha3[index1][index2] = ha3_temp[index1][index2];
Lightvalve 173:68c7914679ec 1004 }
Lightvalve 173:68c7914679ec 1005 ba3[index2] = ba3_temp[index2];
Lightvalve 173:68c7914679ec 1006 }
Lightvalve 170:42c938a40313 1007 }
Lightvalve 170:42c938a40313 1008
Lightvalve 170:42c938a40313 1009
GiJeongKim 0:51c43836c1d7 1010 int main()
GiJeongKim 0:51c43836c1d7 1011 {
Lightvalve 66:a8e6799dbce3 1012
Lightvalve 65:a2d7c63419c2 1013 HAL_Init();
Lightvalve 65:a2d7c63419c2 1014 SystemClock_Config();
Lightvalve 169:645207e160ca 1015
jobuuu 6:df07d3491e3a 1016 /*********************************
jobuuu 1:e04e563be5ce 1017 *** Initialization
jobuuu 6:df07d3491e3a 1018 *********************************/
Lightvalve 69:3995ffeaa786 1019 LED = 0;
Lightvalve 61:bc8c8270f0ab 1020 pc.baud(9600);
Lightvalve 21:e5f1a43ea6f9 1021
GiJeongKim 0:51c43836c1d7 1022 // i2c init
Lightvalve 8:5d2eebdad025 1023 i2c.frequency(400 * 1000); // 0.4 mHz
Lightvalve 8:5d2eebdad025 1024 wait_ms(2); // Power Up wait
Lightvalve 8:5d2eebdad025 1025 look_for_hardware_i2c(); // Hardware present
Lightvalve 8:5d2eebdad025 1026 init_as5510(i2c_slave_addr1);
Lightvalve 11:82d8768d7351 1027 make_delay();
jobuuu 2:a1c0a37df760 1028
GiJeongKim 0:51c43836c1d7 1029 // // spi init
Lightvalve 170:42c938a40313 1030 eeprom.format(8,3);
Lightvalve 170:42c938a40313 1031 eeprom.frequency(5000000); //5M
GiJeongKim 0:51c43836c1d7 1032 enc.format(8,0);
GiJeongKim 0:51c43836c1d7 1033 enc.frequency(5000000); //5M
Lightvalve 11:82d8768d7351 1034 make_delay();
Lightvalve 21:e5f1a43ea6f9 1035
Lightvalve 16:903b5a4433b4 1036 //rom
Lightvalve 19:23b7c1ad8683 1037 ROM_CALL_DATA();
Lightvalve 16:903b5a4433b4 1038 make_delay();
Lightvalve 13:747daba9cf59 1039
GiJeongKim 0:51c43836c1d7 1040 // ADC init
jobuuu 5:a4319f79457b 1041 Init_ADC();
Lightvalve 11:82d8768d7351 1042 make_delay();
jobuuu 2:a1c0a37df760 1043
GiJeongKim 0:51c43836c1d7 1044 // Pwm init
GiJeongKim 0:51c43836c1d7 1045 Init_PWM();
GiJeongKim 0:51c43836c1d7 1046 TIM4->CR1 ^= TIM_CR1_UDIS;
Lightvalve 11:82d8768d7351 1047 make_delay();
Lightvalve 13:747daba9cf59 1048
Lightvalve 11:82d8768d7351 1049 // TMR3 init
Lightvalve 11:82d8768d7351 1050 Init_TMR3();
Lightvalve 11:82d8768d7351 1051 TIM3->CR1 ^= TIM_CR1_UDIS;
Lightvalve 11:82d8768d7351 1052 make_delay();
Lightvalve 21:e5f1a43ea6f9 1053
Lightvalve 50:3c630b5eba9f 1054 // TMR2 init
Lightvalve 56:6f50d9d3bfee 1055 // Init_TMR2();
Lightvalve 56:6f50d9d3bfee 1056 // TIM2->CR1 ^= TIM_CR1_UDIS;
Lightvalve 56:6f50d9d3bfee 1057 // make_delay();
Lightvalve 21:e5f1a43ea6f9 1058
GiJeongKim 0:51c43836c1d7 1059 // CAN
jobuuu 2:a1c0a37df760 1060 can.attach(&CAN_RX_HANDLER);
Lightvalve 11:82d8768d7351 1061 CAN_ID_INIT();
Lightvalve 11:82d8768d7351 1062 make_delay();
Lightvalve 34:bb2ca2fc2a8e 1063
Lightvalve 23:59218d4a256d 1064 //Timer priority
Lightvalve 23:59218d4a256d 1065 NVIC_SetPriority(TIM3_IRQn, 2);
Lightvalve 57:f4819de54e7a 1066 //NVIC_SetPriority(TIM2_IRQn, 3);
Lightvalve 54:647072f5307a 1067 NVIC_SetPriority(TIM4_IRQn, 3);
Lightvalve 34:bb2ca2fc2a8e 1068
Lightvalve 23:59218d4a256d 1069 //can.reset();
Lightvalve 19:23b7c1ad8683 1070 can.filter(msg.id, 0xFFFFF000, CANStandard);
Lightvalve 34:bb2ca2fc2a8e 1071
GiJeongKim 0:51c43836c1d7 1072 // spi _ enc
GiJeongKim 0:51c43836c1d7 1073 spi_enc_set_init();
Lightvalve 11:82d8768d7351 1074 make_delay();
Lightvalve 13:747daba9cf59 1075
Lightvalve 11:82d8768d7351 1076 //DAC init
Lightvalve 58:2eade98630e2 1077 if (SENSING_MODE == 0) {
Lightvalve 58:2eade98630e2 1078 dac_1 = TORQUE_VREF / 3.3f;
Lightvalve 58:2eade98630e2 1079 dac_2 = 0.0f;
Lightvalve 58:2eade98630e2 1080 } else if (SENSING_MODE == 1) {
Lightvalve 58:2eade98630e2 1081 dac_1 = PRES_A_VREF / 3.3f;
Lightvalve 58:2eade98630e2 1082 dac_2 = PRES_B_VREF / 3.3f;
Lightvalve 58:2eade98630e2 1083 }
Lightvalve 11:82d8768d7351 1084 make_delay();
Lightvalve 13:747daba9cf59 1085
Lightvalve 19:23b7c1ad8683 1086 for (int i=0; i<50; i++) {
Lightvalve 11:82d8768d7351 1087 if(i%2==0)
Lightvalve 38:118df027d851 1088 ID_index_array[i] = - i * 0.5f;
Lightvalve 11:82d8768d7351 1089 else
Lightvalve 38:118df027d851 1090 ID_index_array[i] = (i+1) * 0.5f;
Lightvalve 11:82d8768d7351 1091 }
Lightvalve 169:645207e160ca 1092
Lightvalve 173:68c7914679ec 1093 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 170:42c938a40313 1094 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 170:42c938a40313 1095 hc1_temp[index1][index2] = (float) (rand()%100) * 0.01f ;
Lightvalve 170:42c938a40313 1096 }
Lightvalve 170:42c938a40313 1097 bc1_temp[index2] = (float) (rand()%100) * 0.01f;
Lightvalve 173:68c7914679ec 1098 }
Lightvalve 173:68c7914679ec 1099 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 173:68c7914679ec 1100 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 173:68c7914679ec 1101 hc2_temp[index1][index2] = (float) (rand()%100) * 0.01f;
Lightvalve 173:68c7914679ec 1102 }
Lightvalve 173:68c7914679ec 1103 bc2[index2] = (float) (rand()%100) * 0.01f;
Lightvalve 173:68c7914679ec 1104 hc3[index2] = (float) (rand()%100) * 0.01f;
Lightvalve 170:42c938a40313 1105 }
Lightvalve 173:68c7914679ec 1106 bc3 = (float) (rand()%100) * 0.01f;
Lightvalve 173:68c7914679ec 1107
Lightvalve 173:68c7914679ec 1108 for (int index2 = 0; index2 < num_hidden_unit1; index2++) {
Lightvalve 170:42c938a40313 1109 for (int index1 = 0; index1 < num_input_RL; index1++) {
Lightvalve 173:68c7914679ec 1110 ha1[index1][index2] = (float) (rand()%100) * 0.01f;
Lightvalve 170:42c938a40313 1111 }
Lightvalve 173:68c7914679ec 1112 ba1[index2] = (float) (rand()%100) * 0.01f;
Lightvalve 173:68c7914679ec 1113 }
Lightvalve 173:68c7914679ec 1114 for (int index2 = 0; index2 < num_hidden_unit2; index2++) {
Lightvalve 173:68c7914679ec 1115 for (int index1 = 0; index1 < num_hidden_unit1; index1++) {
Lightvalve 173:68c7914679ec 1116 ha2[index1][index2] = (float) (rand()%100) * 0.01f;
Lightvalve 173:68c7914679ec 1117 }
Lightvalve 173:68c7914679ec 1118 ba2[index2] = (float) (rand()%100) * 0.01f;
Lightvalve 170:42c938a40313 1119 }
Lightvalve 170:42c938a40313 1120 for (int index2 = 0; index2 < 2; index2++) {
Lightvalve 173:68c7914679ec 1121 for (int index1 = 0; index1 < num_hidden_unit2; index1++) {
Lightvalve 173:68c7914679ec 1122 ha3[index1][index2] = (float) (rand()%100) * 0.01f;
Lightvalve 170:42c938a40313 1123 }
Lightvalve 173:68c7914679ec 1124 ba3[index2] = (float) (rand()%100) * 0.01f;
Lightvalve 170:42c938a40313 1125 }
Lightvalve 171:bfc1fd2629d8 1126
Lightvalve 170:42c938a40313 1127 Overwirte_Critic_Networks();
Lightvalve 170:42c938a40313 1128 Overwirte_Actor_Networks();
Lightvalve 61:bc8c8270f0ab 1129
jobuuu 6:df07d3491e3a 1130 /************************************
jobuuu 1:e04e563be5ce 1131 *** Program is operating!
jobuuu 6:df07d3491e3a 1132 *************************************/
GiJeongKim 0:51c43836c1d7 1133 while(1) {
Lightvalve 169:645207e160ca 1134
Lightvalve 171:bfc1fd2629d8 1135 // if(timer_while==27491) {
Lightvalve 171:bfc1fd2629d8 1136 // timer_while = 0;
Lightvalve 171:bfc1fd2629d8 1137 // pc.printf("ref : %f virt_pos : %f mean : %f deviation : %f Last_pos_of_batch : %f reward_sum : %f\n", pos.sen/(float)(ENC_PULSE_PER_POSITION), logging3, logging2, logging4, logging1, logging5);
Lightvalve 171:bfc1fd2629d8 1138 // //pc.printf("%f\n", virt_pos);
Lightvalve 171:bfc1fd2629d8 1139 // //pc.printf("%f\n", pos.sen/(float)(ENC_PULSE_PER_POSITION));
Lightvalve 171:bfc1fd2629d8 1140 // //pc.printf("ref : %f virt_pos : %f\n", pos.sen/(float)(ENC_PULSE_PER_POSITION), virt_pos);
Lightvalve 171:bfc1fd2629d8 1141 // }
Lightvalve 169:645207e160ca 1142
Lightvalve 171:bfc1fd2629d8 1143
Lightvalve 171:bfc1fd2629d8 1144 //i2c
Lightvalve 171:bfc1fd2629d8 1145 //read_field(i2c_slave_addr1);
Lightvalve 171:bfc1fd2629d8 1146 //if(DIR_VALVE_ENC < 0) value = 1023 - value;
Lightvalve 171:bfc1fd2629d8 1147
Lightvalve 171:bfc1fd2629d8 1148 // timer_while ++;
Lightvalve 169:645207e160ca 1149
Lightvalve 170:42c938a40313 1150 ///////////////////////////////////////////////////////Neural Network
Lightvalve 169:645207e160ca 1151
Lightvalve 73:f80dc3970c99 1152 if(NN_Control_Flag == 0) {
Lightvalve 73:f80dc3970c99 1153 LED = 0;
Lightvalve 73:f80dc3970c99 1154 }
Lightvalve 169:645207e160ca 1155
Lightvalve 73:f80dc3970c99 1156 else if(NN_Control_Flag == 1) {
Lightvalve 169:645207e160ca 1157
Lightvalve 162:9dd4f35e9de8 1158 int ind = 0;
Lightvalve 162:9dd4f35e9de8 1159 for(int i=0; i<numpast_u; i++) {
Lightvalve 162:9dd4f35e9de8 1160 input_NN[ind] = u_past[time_interval*i];
Lightvalve 162:9dd4f35e9de8 1161 ind = ind + 1;
Lightvalve 162:9dd4f35e9de8 1162 }
Lightvalve 162:9dd4f35e9de8 1163
Lightvalve 162:9dd4f35e9de8 1164 for(int i=0; i<numpast_x; i++) {
Lightvalve 162:9dd4f35e9de8 1165 input_NN[ind] = x_past[time_interval*i] / 60.0f;
Lightvalve 162:9dd4f35e9de8 1166 ind = ind + 1;
Lightvalve 162:9dd4f35e9de8 1167 }
Lightvalve 162:9dd4f35e9de8 1168 input_NN[ind] = (pos.sen / ENC_PULSE_PER_POSITION) / 60.0f;
Lightvalve 162:9dd4f35e9de8 1169 ind = ind + 1;
Lightvalve 169:645207e160ca 1170
Lightvalve 162:9dd4f35e9de8 1171 // for(int i=0; i<numfuture_x; i++) {
Lightvalve 162:9dd4f35e9de8 1172 // input_NN[ind] = x_future[time_interval*i+time_interval] / 60.0f;
Lightvalve 122:dcb3ce3056a0 1173 // ind = ind + 1;
Lightvalve 122:dcb3ce3056a0 1174 // }
Lightvalve 162:9dd4f35e9de8 1175
Lightvalve 162:9dd4f35e9de8 1176 for(int i=0; i<numpast_f; i++) {
Lightvalve 162:9dd4f35e9de8 1177 input_NN[ind] = f_past[time_interval*i] / 10000.0f + 0.5f;
Lightvalve 162:9dd4f35e9de8 1178 ind = ind + 1;
Lightvalve 162:9dd4f35e9de8 1179 }
Lightvalve 162:9dd4f35e9de8 1180 input_NN[ind] = torq.sen / 10000.0f + 0.5f;
Lightvalve 162:9dd4f35e9de8 1181 ind = ind + 1;
Lightvalve 162:9dd4f35e9de8 1182 for(int i=0; i<numfuture_f; i++) {
Lightvalve 168:f05a45d5f318 1183 // input_NN[ind] = (f_future[time_interval*i+time_interval] - torq.sen)/10000.0f+0.5f;
Lightvalve 168:f05a45d5f318 1184 input_NN[ind] = (f_future[time_interval*i+time_interval])/10000.0f+0.5f;
Lightvalve 162:9dd4f35e9de8 1185 ind = ind + 1;
Lightvalve 162:9dd4f35e9de8 1186 }
Lightvalve 169:645207e160ca 1187
Lightvalve 112:8dcb1600cb90 1188 float output1[16] = { 0.0f };
Lightvalve 112:8dcb1600cb90 1189 float output2[16] = { 0.0f };
Lightvalve 112:8dcb1600cb90 1190 float output3[16] = { 0.0f };
Lightvalve 66:a8e6799dbce3 1191 float output = 0.0f;
Lightvalve 65:a2d7c63419c2 1192
Lightvalve 112:8dcb1600cb90 1193 for (int index2 = 0; index2 < 16; index2++) {
Lightvalve 68:328e1be06f5d 1194 for (int index1 = 0; index1 < num_input; index1++) {
Lightvalve 66:a8e6799dbce3 1195 output1[index2] = output1[index2]
Lightvalve 73:f80dc3970c99 1196 + h1[index1][index2] * input_NN[index1];
Lightvalve 66:a8e6799dbce3 1197 }
Lightvalve 66:a8e6799dbce3 1198 output1[index2] = output1[index2] + b1[index2];
Lightvalve 66:a8e6799dbce3 1199 if (output1[index2] < 0) {
Lightvalve 66:a8e6799dbce3 1200 output1[index2] = 0;
Lightvalve 66:a8e6799dbce3 1201 }
Lightvalve 66:a8e6799dbce3 1202 }
Lightvalve 65:a2d7c63419c2 1203
Lightvalve 112:8dcb1600cb90 1204 for (int index2 = 0; index2 < 16; index2++) {
Lightvalve 112:8dcb1600cb90 1205 for (int index1 = 0; index1 < 16; index1++) {
Lightvalve 66:a8e6799dbce3 1206 output2[index2] = output2[index2]
Lightvalve 66:a8e6799dbce3 1207 + h2[index1][index2] * output1[index1];
Lightvalve 66:a8e6799dbce3 1208 }
Lightvalve 66:a8e6799dbce3 1209 output2[index2] = output2[index2] + b2[index2];
Lightvalve 66:a8e6799dbce3 1210 if (output2[index2] < 0) {
Lightvalve 66:a8e6799dbce3 1211 output2[index2] = 0;
Lightvalve 66:a8e6799dbce3 1212 }
Lightvalve 66:a8e6799dbce3 1213 }
Lightvalve 65:a2d7c63419c2 1214
Lightvalve 112:8dcb1600cb90 1215 for (int index2 = 0; index2 < 16; index2++) {
Lightvalve 112:8dcb1600cb90 1216 for (int index1 = 0; index1 < 16; index1++) {
Lightvalve 66:a8e6799dbce3 1217 output3[index2] = output3[index2]
Lightvalve 66:a8e6799dbce3 1218 + h3[index1][index2] * output2[index1];
Lightvalve 66:a8e6799dbce3 1219 }
Lightvalve 66:a8e6799dbce3 1220 output3[index2] = output3[index2] + b3[index2];
Lightvalve 66:a8e6799dbce3 1221 if (output3[index2] < 0) {
Lightvalve 66:a8e6799dbce3 1222 output3[index2] = 0;
Lightvalve 66:a8e6799dbce3 1223 }
Lightvalve 65:a2d7c63419c2 1224 }
Lightvalve 66:a8e6799dbce3 1225
Lightvalve 66:a8e6799dbce3 1226 for (int index2 = 0; index2 < 1; index2++) {
Lightvalve 112:8dcb1600cb90 1227 for (int index1 = 0; index1 < 16; index1++) {
Lightvalve 73:f80dc3970c99 1228 output = output + hout[index1] * output3[index1];
Lightvalve 66:a8e6799dbce3 1229 }
Lightvalve 66:a8e6799dbce3 1230 output = output + bout[index2];
Lightvalve 169:645207e160ca 1231
Lightvalve 66:a8e6799dbce3 1232 }
Lightvalve 73:f80dc3970c99 1233 output = 1.0f/(1.0f+exp(-output));
Lightvalve 101:50159049a518 1234 output_normalized = output;
Lightvalve 68:328e1be06f5d 1235 output = output * 20000.0f - 10000.0f;
Lightvalve 169:645207e160ca 1236
Lightvalve 66:a8e6799dbce3 1237 if(output>=0) {
Lightvalve 66:a8e6799dbce3 1238 valve_pos.ref = output*0.0001f*((double)VALVE_MAX_POS - (double) VALVE_CENTER) + (double) VALVE_CENTER;
Lightvalve 66:a8e6799dbce3 1239 } else {
Lightvalve 66:a8e6799dbce3 1240 valve_pos.ref = -output*0.0001f*((double)VALVE_MIN_POS - (double) VALVE_CENTER) + (double) VALVE_CENTER;
Lightvalve 65:a2d7c63419c2 1241 }
Lightvalve 87:471334725012 1242
Lightvalve 169:645207e160ca 1243
Lightvalve 69:3995ffeaa786 1244 if(LED==1) {
Lightvalve 69:3995ffeaa786 1245 LED=0;
Lightvalve 69:3995ffeaa786 1246 } else
Lightvalve 69:3995ffeaa786 1247 LED = 1;
Lightvalve 169:645207e160ca 1248
Lightvalve 65:a2d7c63419c2 1249 }
Lightvalve 171:bfc1fd2629d8 1250
Lightvalve 171:bfc1fd2629d8 1251
Lightvalve 170:42c938a40313 1252 /////////////////////////////////////////////////////////////////////RL
Lightvalve 170:42c938a40313 1253 switch (Update_Case) {
Lightvalve 170:42c938a40313 1254 case 0: {
Lightvalve 170:42c938a40313 1255 break;
Lightvalve 170:42c938a40313 1256 }
Lightvalve 170:42c938a40313 1257 case 1: {
Lightvalve 170:42c938a40313 1258 //Network Update(just update and hold network)
Lightvalve 170:42c938a40313 1259 for (int epoch = 0; epoch < num_epoch; epoch++) {
Lightvalve 170:42c938a40313 1260 float loss_sum = 0.0f;
Lightvalve 175:2f7289dbd488 1261 for (int n=batch_size-1; n>=0; n--) {
Lightvalve 170:42c938a40313 1262 //Calculate Estimated V
Lightvalve 175:2f7289dbd488 1263 //float temp_array[3] = {state_array[n][0], state_array[n][1], state_array[n][2]};
Lightvalve 175:2f7289dbd488 1264 float temp_array[2] = {state_array[n][0], state_array[n][1]};
Lightvalve 175:2f7289dbd488 1265 V[n] = Critic_Network_Temp(temp_array);
Lightvalve 173:68c7914679ec 1266 for (int i=0; i<num_hidden_unit1; i++) {
Lightvalve 175:2f7289dbd488 1267 hx_c_sum_array[n][i] = hx_c_sum[i];
Lightvalve 173:68c7914679ec 1268 }
Lightvalve 173:68c7914679ec 1269 for (int i=0; i<num_hidden_unit2; i++) {
Lightvalve 175:2f7289dbd488 1270 hxh_c_sum_array[n][i] = hxh_c_sum[i];
Lightvalve 173:68c7914679ec 1271 }
Lightvalve 175:2f7289dbd488 1272 hxhh_c_sum_array[n] = hxhh_c_sum;
Lightvalve 175:2f7289dbd488 1273
Lightvalve 175:2f7289dbd488 1274 pi[n] = exp(-(action_array[n]-mean_array[n])*(action_array[n]-mean_array[n])/(2.0f*deviation_array[n]*deviation_array[n]))/(sqrt(2.0f*PI)*deviation_array[n]);
Lightvalve 170:42c938a40313 1275 Actor_Network_Old(temp_array);
Lightvalve 175:2f7289dbd488 1276 pi_old[n] = exp(-(action_array[n]-mean_old)*(action_array[n]-mean_old)/(2.0f*deviation_old*deviation_old))/(sqrt(2.0f*PI)*deviation_old);
Lightvalve 175:2f7289dbd488 1277 r[n] = exp(-0.25f * state_array[n][1] * state_array[n][1]);
Lightvalve 175:2f7289dbd488 1278 if(n == batch_size-1) return_G[n] = 0.0f;
Lightvalve 175:2f7289dbd488 1279 else return_G[n] = gamma * return_G[n+1] + r[n];
Lightvalve 175:2f7289dbd488 1280 if(n == batch_size-1) td_target[n] = r[n];
Lightvalve 175:2f7289dbd488 1281 else td_target[n] = r[n] + gamma * V[n+1];
Lightvalve 175:2f7289dbd488 1282 delta[n] = td_target[n] - V[n];
Lightvalve 175:2f7289dbd488 1283 if(n == batch_size-1) advantage[n] = 0.0f;
Lightvalve 175:2f7289dbd488 1284 else advantage[n] = gamma * lmbda * advantage[n+1] + delta[n];
Lightvalve 175:2f7289dbd488 1285 ratio[n] = pi[n]/pi_old[n];
Lightvalve 175:2f7289dbd488 1286 surr1[n] = ratio[n] * advantage[n];
Lightvalve 175:2f7289dbd488 1287 if (ratio[n] > 1.0f + epsilon) {
Lightvalve 175:2f7289dbd488 1288 surr2[n] = (1.0f + epsilon)*advantage[n];
Lightvalve 175:2f7289dbd488 1289 } else if( ratio[n] < 1.0f - epsilon) {
Lightvalve 175:2f7289dbd488 1290 surr2[n] = (1.0f - epsilon)*advantage[n];
Lightvalve 170:42c938a40313 1291 } else {
Lightvalve 175:2f7289dbd488 1292 surr2[n] = ratio[n]*advantage[n];
Lightvalve 170:42c938a40313 1293 }
Lightvalve 175:2f7289dbd488 1294 loss[n] = -min(surr1[n], surr2[n]);
Lightvalve 175:2f7289dbd488 1295 loss_sum = loss_sum + loss[n];
Lightvalve 170:42c938a40313 1296 }
Lightvalve 170:42c938a40313 1297 reward_sum = 0.0f;
Lightvalve 170:42c938a40313 1298 for (int i=0; i<batch_size; i++) {
Lightvalve 170:42c938a40313 1299 reward_sum = reward_sum + r[i];
Lightvalve 170:42c938a40313 1300 }
Lightvalve 170:42c938a40313 1301 logging5 = reward_sum;
Lightvalve 171:bfc1fd2629d8 1302
Lightvalve 171:bfc1fd2629d8 1303
Lightvalve 170:42c938a40313 1304 //loss_batch = loss_sum / (float) batch_size;
Lightvalve 170:42c938a40313 1305 loss_batch = loss_sum;
Lightvalve 170:42c938a40313 1306 //Update Networks
Lightvalve 170:42c938a40313 1307 update_Critic_Networks(state_array);
Lightvalve 170:42c938a40313 1308 update_Actor_Networks(state_array);
Lightvalve 170:42c938a40313 1309 }
Lightvalve 170:42c938a40313 1310 //virt_pos = 10.0f;
Lightvalve 170:42c938a40313 1311 Update_Done_Flag = 1;
Lightvalve 170:42c938a40313 1312 Update_Case = 0;
Lightvalve 170:42c938a40313 1313 //logging1 = V[0];
Lightvalve 171:bfc1fd2629d8 1314
Lightvalve 170:42c938a40313 1315 break;
Lightvalve 170:42c938a40313 1316 }
Lightvalve 170:42c938a40313 1317 case 2: {
Lightvalve 170:42c938a40313 1318 //Network apply to next Network
Lightvalve 170:42c938a40313 1319 Overwirte_Critic_Networks();
Lightvalve 170:42c938a40313 1320 Overwirte_Actor_Networks();
Lightvalve 170:42c938a40313 1321 virt_pos = 10.0f;
Lightvalve 170:42c938a40313 1322 Update_Done_Flag = 1;
Lightvalve 170:42c938a40313 1323 Update_Case = 0;
Lightvalve 170:42c938a40313 1324 break;
Lightvalve 170:42c938a40313 1325 }
Lightvalve 169:645207e160ca 1326
Lightvalve 170:42c938a40313 1327 }
GiJeongKim 0:51c43836c1d7 1328 }
jobuuu 1:e04e563be5ce 1329 }
jobuuu 1:e04e563be5ce 1330
Lightvalve 33:91b17819ec30 1331 float DDV_JOINT_POS_FF(float REF_JOINT_VEL)
Lightvalve 14:8e7590227d22 1332 {
Lightvalve 14:8e7590227d22 1333
Lightvalve 13:747daba9cf59 1334 int i = 0;
Lightvalve 48:889798ff9329 1335 float Ref_Valve_Pos_FF = 0.0f;
Lightvalve 14:8e7590227d22 1336 for(i=0; i<VALVE_POS_NUM; i++) {
Lightvalve 14:8e7590227d22 1337 if(REF_JOINT_VEL >= min(JOINT_VEL[i],JOINT_VEL[i+1]) && REF_JOINT_VEL <= max(JOINT_VEL[i],JOINT_VEL[i+1])) {
Lightvalve 14:8e7590227d22 1338 if(i==0) {
Lightvalve 50:3c630b5eba9f 1339 if(JOINT_VEL[i+1] == JOINT_VEL[i]) {
Lightvalve 57:f4819de54e7a 1340 Ref_Valve_Pos_FF = (float) VALVE_CENTER;
Lightvalve 50:3c630b5eba9f 1341 } else {
Lightvalve 57:f4819de54e7a 1342 Ref_Valve_Pos_FF = ((float) 10/(JOINT_VEL[i+1] - JOINT_VEL[i]) * (REF_JOINT_VEL - JOINT_VEL[i])) + (float) VALVE_CENTER;
Lightvalve 38:118df027d851 1343 }
Lightvalve 14:8e7590227d22 1344 } else {
Lightvalve 50:3c630b5eba9f 1345 if(JOINT_VEL[i+1] == JOINT_VEL[i-1]) {
Lightvalve 57:f4819de54e7a 1346 Ref_Valve_Pos_FF = (float) VALVE_CENTER;
Lightvalve 50:3c630b5eba9f 1347 } else {
Lightvalve 57:f4819de54e7a 1348 Ref_Valve_Pos_FF = ((float) 10*(ID_index_array[i+1] - ID_index_array[i-1])/(JOINT_VEL[i+1] - JOINT_VEL[i-1]) * (REF_JOINT_VEL - JOINT_VEL[i-1])) + (float) VALVE_CENTER + (float) (10*ID_index_array[i-1]);
Lightvalve 38:118df027d851 1349 }
Lightvalve 13:747daba9cf59 1350 }
Lightvalve 13:747daba9cf59 1351 break;
Lightvalve 13:747daba9cf59 1352 }
Lightvalve 13:747daba9cf59 1353 }
Lightvalve 14:8e7590227d22 1354 if(REF_JOINT_VEL > max(JOINT_VEL[VALVE_POS_NUM-1], JOINT_VEL[VALVE_POS_NUM-2])) {
Lightvalve 33:91b17819ec30 1355 Ref_Valve_Pos_FF = (float) VALVE_MAX_POS;
Lightvalve 14:8e7590227d22 1356 } else if(REF_JOINT_VEL < min(JOINT_VEL[VALVE_POS_NUM-1], JOINT_VEL[VALVE_POS_NUM-2])) {
Lightvalve 33:91b17819ec30 1357 Ref_Valve_Pos_FF = (float) VALVE_MIN_POS;
Lightvalve 13:747daba9cf59 1358 }
Lightvalve 36:a46e63505ed8 1359
Lightvalve 57:f4819de54e7a 1360 Ref_Valve_Pos_FF = (float) VELOCITY_COMP_GAIN * 0.01f * (float) (Ref_Valve_Pos_FF - (float) VALVE_CENTER);
Lightvalve 13:747daba9cf59 1361 return Ref_Valve_Pos_FF;
Lightvalve 50:3c630b5eba9f 1362
Lightvalve 13:747daba9cf59 1363 }
jobuuu 6:df07d3491e3a 1364
jobuuu 6:df07d3491e3a 1365
Lightvalve 30:8d561f16383b 1366 void VALVE_POS_CONTROL(float REF_VALVE_POS)
Lightvalve 14:8e7590227d22 1367 {
Lightvalve 13:747daba9cf59 1368 int i = 0;
Lightvalve 13:747daba9cf59 1369
Lightvalve 38:118df027d851 1370 if(REF_VALVE_POS > VALVE_MAX_POS) {
Lightvalve 38:118df027d851 1371 REF_VALVE_POS = VALVE_MAX_POS;
Lightvalve 38:118df027d851 1372 } else if(REF_VALVE_POS < VALVE_MIN_POS) {
Lightvalve 38:118df027d851 1373 REF_VALVE_POS = VALVE_MIN_POS;
Lightvalve 38:118df027d851 1374 }
Lightvalve 38:118df027d851 1375
Lightvalve 89:a7b45368ea0f 1376 valve_pos_err = (float) (REF_VALVE_POS - value);
Lightvalve 13:747daba9cf59 1377 valve_pos_err_diff = valve_pos_err - valve_pos_err_old;
Lightvalve 13:747daba9cf59 1378 valve_pos_err_old = valve_pos_err;
Lightvalve 13:747daba9cf59 1379 valve_pos_err_sum += valve_pos_err;
Lightvalve 89:a7b45368ea0f 1380 if (valve_pos_err_sum > 1000.0f) valve_pos_err_sum = 1000.0f;
Lightvalve 89:a7b45368ea0f 1381 if (valve_pos_err_sum<-1000.0f) valve_pos_err_sum = -1000.0f;
Lightvalve 13:747daba9cf59 1382
Lightvalve 13:747daba9cf59 1383 VALVE_PWM_RAW_FB = P_GAIN_VALVE_POSITION * valve_pos_err + I_GAIN_VALVE_POSITION * valve_pos_err_sum + D_GAIN_VALVE_POSITION * valve_pos_err_diff;
Lightvalve 14:8e7590227d22 1384
Lightvalve 18:b8adf1582ea3 1385 for(i=0; i<24; i++) {
Lightvalve 89:a7b45368ea0f 1386 if(REF_VALVE_POS >= min(VALVE_POS_VS_PWM[i],VALVE_POS_VS_PWM[i+1]) && (float) REF_VALVE_POS <= max(VALVE_POS_VS_PWM[i],VALVE_POS_VS_PWM[i+1])) {
Lightvalve 14:8e7590227d22 1387 if(i==0) {
Lightvalve 48:889798ff9329 1388 VALVE_PWM_RAW_FF = (float) 1000.0f / (float) (VALVE_POS_VS_PWM[i+1] - VALVE_POS_VS_PWM[i]) * ((float) REF_VALVE_POS - VALVE_POS_VS_PWM[i]);
Lightvalve 14:8e7590227d22 1389 } else {
Lightvalve 48:889798ff9329 1390 VALVE_PWM_RAW_FF = (float) 1000.0f* (float) (ID_index_array[i+1] - ID_index_array[i-1])/(VALVE_POS_VS_PWM[i+1] - VALVE_POS_VS_PWM[i-1]) * ((float) REF_VALVE_POS - VALVE_POS_VS_PWM[i-1]) + 1000.0f * (float) ID_index_array[i-1];
Lightvalve 13:747daba9cf59 1391 }
Lightvalve 13:747daba9cf59 1392 break;
Lightvalve 13:747daba9cf59 1393 }
Lightvalve 13:747daba9cf59 1394 }
Lightvalve 59:f308b1656d9c 1395 Vout.ref = VALVE_PWM_RAW_FF + VALVE_PWM_RAW_FB;
Lightvalve 13:747daba9cf59 1396 }
Lightvalve 13:747daba9cf59 1397
Lightvalve 14:8e7590227d22 1398 #define LT_MAX_IDX 57
Lightvalve 30:8d561f16383b 1399 float LT_PWM_duty[LT_MAX_IDX] = {-100.0f, -80.0f, -60.0f, -50.0f, -40.0f, -35.0f, -30.0f, -25.0f, -20.0f,
Lightvalve 34:bb2ca2fc2a8e 1400 -19.0f, -18.0f, -17.0f, -16.0f, -15.0f, -14.0f, -13.0f, -12.0f, -11.0f, -10.0f,
Lightvalve 34:bb2ca2fc2a8e 1401 -9.0f, -8.0f, -7.0f, -6.0f, -5.0f, -4.0f, -3.0f, -2.0f, -1.0f, 0.0f,
Lightvalve 34:bb2ca2fc2a8e 1402 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f,
Lightvalve 34:bb2ca2fc2a8e 1403 11.0f, 12.0f, 13.0f, 14.0f, 15.0f, 16.0f, 17.0f, 18.0f, 19.0f, 20.0f,
Lightvalve 34:bb2ca2fc2a8e 1404 25.0f, 30.0f, 35.0f, 40.0f, 50.0f, 60.0f, 80.0f, 100.0f
Lightvalve 34:bb2ca2fc2a8e 1405 }; // duty
Lightvalve 67:c2812cf26c38 1406 float LT_Voltage_Output[LT_MAX_IDX] = {-230.0f, -215.0f, -192.5f, -185.0f, -177.5f, -170.0f, -164.0f, -160.0f, -150.0f,
Lightvalve 67:c2812cf26c38 1407 -150.0f, -145.0f, -145.0f, -145.0f, -135.0f, -135.0f, -135.0f, -127.5f, -127.5f, -115.0f,
Lightvalve 67:c2812cf26c38 1408 -115.0f, -115.0F, -100.0f, -100.0f, -100.0f, -60.0f, -60.0f, -10.0f, -5.0f, 0.0f,
Lightvalve 67:c2812cf26c38 1409 7.5f, 14.0f, 14.0f, 14.0f, 42.5f, 42.5f, 42.5f, 80.0f, 80.0f, 105.0f,
Lightvalve 67:c2812cf26c38 1410 105.0f, 105.0f, 120.0f, 120.0f, 120.0f, 131.0f, 131.0f, 140.0f, 140.0f, 140.0f,
Lightvalve 67:c2812cf26c38 1411 155.0f, 160.0f, 170.0f, 174.0f, 182.0f, 191.0f, 212.0f, 230.0f
Lightvalve 34:bb2ca2fc2a8e 1412 }; // mV
Lightvalve 13:747daba9cf59 1413
Lightvalve 30:8d561f16383b 1414 float PWM_duty_byLT(float Ref_V)
Lightvalve 14:8e7590227d22 1415 {
Lightvalve 30:8d561f16383b 1416 float PWM_duty = 0.0f;
Lightvalve 13:747daba9cf59 1417 if(Ref_V<LT_Voltage_Output[0]) {
Lightvalve 30:8d561f16383b 1418 PWM_duty = (Ref_V-LT_Voltage_Output[0])/1.5f+LT_PWM_duty[0];
Lightvalve 13:747daba9cf59 1419 } else if (Ref_V>=LT_Voltage_Output[LT_MAX_IDX-1]) {
Lightvalve 30:8d561f16383b 1420 PWM_duty = (Ref_V-LT_Voltage_Output[LT_MAX_IDX-1])/1.5f+LT_PWM_duty[LT_MAX_IDX-1];
Lightvalve 13:747daba9cf59 1421 } else {
Lightvalve 13:747daba9cf59 1422 int idx = 0;
Lightvalve 13:747daba9cf59 1423 for(idx=0; idx<LT_MAX_IDX-1; idx++) {
Lightvalve 30:8d561f16383b 1424 float ini_x = LT_Voltage_Output[idx];
Lightvalve 30:8d561f16383b 1425 float fin_x = LT_Voltage_Output[idx+1];
Lightvalve 30:8d561f16383b 1426 float ini_y = LT_PWM_duty[idx];
Lightvalve 30:8d561f16383b 1427 float fin_y = LT_PWM_duty[idx+1];
Lightvalve 13:747daba9cf59 1428 if(Ref_V>=ini_x && Ref_V<fin_x) {
Lightvalve 13:747daba9cf59 1429 PWM_duty = (fin_y-ini_y)/(fin_x-ini_x)*(Ref_V-ini_x) + ini_y;
Lightvalve 13:747daba9cf59 1430 break;
Lightvalve 13:747daba9cf59 1431 }
Lightvalve 13:747daba9cf59 1432 }
Lightvalve 13:747daba9cf59 1433 }
Lightvalve 14:8e7590227d22 1434
Lightvalve 13:747daba9cf59 1435 return PWM_duty;
Lightvalve 13:747daba9cf59 1436 }
jobuuu 6:df07d3491e3a 1437
Lightvalve 57:f4819de54e7a 1438
Lightvalve 57:f4819de54e7a 1439
Lightvalve 57:f4819de54e7a 1440
Lightvalve 57:f4819de54e7a 1441
jobuuu 2:a1c0a37df760 1442 /*******************************************************************************
jobuuu 2:a1c0a37df760 1443 TIMER INTERRUPT
jobuuu 2:a1c0a37df760 1444 *******************************************************************************/
jobuuu 2:a1c0a37df760 1445
Lightvalve 51:b46bed7fec80 1446 float FREQ_TMR4 = (float)FREQ_20k;
Lightvalve 51:b46bed7fec80 1447 float DT_TMR4 = (float)DT_20k;
Lightvalve 57:f4819de54e7a 1448 long CNT_TMR4 = 0;
Lightvalve 57:f4819de54e7a 1449 int TMR4_FREQ_10k = (int)FREQ_10k;
jobuuu 1:e04e563be5ce 1450 extern "C" void TIM4_IRQHandler(void)
jobuuu 1:e04e563be5ce 1451 {
Lightvalve 19:23b7c1ad8683 1452 if (TIM4->SR & TIM_SR_UIF ) {
Lightvalve 21:e5f1a43ea6f9 1453
Lightvalve 21:e5f1a43ea6f9 1454 /*******************************************************
Lightvalve 21:e5f1a43ea6f9 1455 *** Sensor Read & Data Handling
Lightvalve 21:e5f1a43ea6f9 1456 ********************************************************/
Lightvalve 13:747daba9cf59 1457
Lightvalve 57:f4819de54e7a 1458 //Encoder
Lightvalve 57:f4819de54e7a 1459 if (CNT_TMR4 % (int) ((int) FREQ_TMR4/TMR4_FREQ_10k) == 0) {
Lightvalve 57:f4819de54e7a 1460 ENC_UPDATE();
Lightvalve 57:f4819de54e7a 1461 }
Lightvalve 61:bc8c8270f0ab 1462
Lightvalve 61:bc8c8270f0ab 1463 ADC1->CR2 |= 0x40000000;
Lightvalve 58:2eade98630e2 1464 if (SENSING_MODE == 0) {
Lightvalve 58:2eade98630e2 1465 // Torque Sensing (0~210)bar =============================================
Lightvalve 58:2eade98630e2 1466 float pres_A_new = (((float) ADC1->DR) - 2047.5f);
Lightvalve 58:2eade98630e2 1467 double alpha_update_ft = 1.0f / (1.0f + FREQ_TMR4 / (2.0f * 3.14f * 100.0f)); // f_cutoff : 200Hz
Lightvalve 58:2eade98630e2 1468 pres_A.sen = (1.0f - alpha_update_ft) * pres_A.sen + alpha_update_ft * pres_A_new;
Lightvalve 67:c2812cf26c38 1469 torq.sen = -pres_A.sen / TORQUE_SENSOR_PULSE_PER_TORQUE;
Lightvalve 67:c2812cf26c38 1470
Lightvalve 67:c2812cf26c38 1471
Lightvalve 67:c2812cf26c38 1472 // float alpha_update_pres_A = 1.0f/(1.0f + FREQ_TMR4/(2.0f*3.14f*100.0f));
Lightvalve 67:c2812cf26c38 1473 //// float pres_A_new = ((float)ADC1->DR - PRES_A_NULL) / PRES_SENSOR_A_PULSE_PER_BAR;
Lightvalve 67:c2812cf26c38 1474 // float pres_A_new = ((float)ADC1->DR);
Lightvalve 67:c2812cf26c38 1475 // pres_A.sen = pres_A.sen*(1.0f-alpha_update_pres_A)+pres_A_new*(alpha_update_pres_A);
Lightvalve 67:c2812cf26c38 1476 // torq.sen = - (pres_A.sen-2048.0f); //pulse -2047~2047
Lightvalve 67:c2812cf26c38 1477
Lightvalve 17:1865016ca2e7 1478
Lightvalve 58:2eade98630e2 1479 } else if (SENSING_MODE == 1) {
Lightvalve 58:2eade98630e2 1480 // Pressure Sensing (0~210)bar =============================================
Lightvalve 58:2eade98630e2 1481 float pres_A_new = (((float)ADC1->DR) - PRES_A_NULL);
Lightvalve 58:2eade98630e2 1482 float pres_B_new = (((float)ADC2->DR) - PRES_B_NULL);
Lightvalve 58:2eade98630e2 1483 double alpha_update_pres = 1.0f / (1.0f + FREQ_TMR4 / (2.0f * 3.14f * 200.0f)); // f_cutoff : 500Hz
Lightvalve 58:2eade98630e2 1484 pres_A.sen = (1.0f - alpha_update_pres) * pres_A.sen + alpha_update_pres * pres_A_new;
Lightvalve 58:2eade98630e2 1485 pres_B.sen = (1.0f - alpha_update_pres) * pres_B.sen + alpha_update_pres * pres_B_new;
Lightvalve 58:2eade98630e2 1486 CUR_PRES_A_BAR = pres_A.sen / PRES_SENSOR_A_PULSE_PER_BAR;
Lightvalve 58:2eade98630e2 1487 CUR_PRES_B_BAR = pres_B.sen / PRES_SENSOR_B_PULSE_PER_BAR;
Lightvalve 50:3c630b5eba9f 1488
Lightvalve 58:2eade98630e2 1489 if ((OPERATING_MODE & 0x01) == 0) { // Rotary Actuator
Lightvalve 58:2eade98630e2 1490 torq.sen = (PISTON_AREA_A * CUR_PRES_A_BAR - PISTON_AREA_B * CUR_PRES_B_BAR) * 0.0001f; // mm^3*bar >> Nm
Lightvalve 58:2eade98630e2 1491 } else if ((OPERATING_MODE & 0x01) == 1) { // Linear Actuator
Lightvalve 58:2eade98630e2 1492 torq.sen = (PISTON_AREA_A * CUR_PRES_A_BAR - PISTON_AREA_B * CUR_PRES_B_BAR) * 0.1f; // mm^2*bar >> N
Lightvalve 58:2eade98630e2 1493 }
Lightvalve 58:2eade98630e2 1494 }
Lightvalve 61:bc8c8270f0ab 1495
Lightvalve 58:2eade98630e2 1496 // //Pressure sensor A
Lightvalve 58:2eade98630e2 1497 // ADC1->CR2 |= 0x40000000; // adc _ 12bit
Lightvalve 58:2eade98630e2 1498 // //while((ADC1->SR & 0b10));
Lightvalve 58:2eade98630e2 1499 // float alpha_update_pres_A = 1.0f/(1.0f + FREQ_TMR4/(2.0f*3.14f*100.0f));
Lightvalve 58:2eade98630e2 1500 // float pres_A_new = ((float)ADC1->DR);
Lightvalve 58:2eade98630e2 1501 // pres_A.sen = pres_A.sen*(1.0f-alpha_update_pres_A)+pres_A_new*(alpha_update_pres_A);
Lightvalve 58:2eade98630e2 1502 // torq.sen = - (pres_A.sen-2048.0f); //pulse -2047~2047 //SW just changed the sign to correct the direction of loadcell on LIGHT. Correct later.
Lightvalve 58:2eade98630e2 1503 //
Lightvalve 58:2eade98630e2 1504 //
Lightvalve 58:2eade98630e2 1505 // //Pressure sensor B
Lightvalve 58:2eade98630e2 1506 // float alpha_update_pres_B = 1.0f/(1.0f + FREQ_TMR4/(2.0f*3.14f*100.0f));
Lightvalve 58:2eade98630e2 1507 // float pres_B_new = ((float)ADC2->DR);
Lightvalve 58:2eade98630e2 1508 // pres_B.sen = pres_B.sen*(1.0f-alpha_update_pres_B)+pres_B_new*(alpha_update_pres_B);
Lightvalve 58:2eade98630e2 1509 // //torq.sen = pres_A.sen * (float) PISTON_AREA_A - pres_B.sen * (float) PISTON_AREA_B;
Lightvalve 17:1865016ca2e7 1510
Lightvalve 17:1865016ca2e7 1511
Lightvalve 21:e5f1a43ea6f9 1512 //Current
Lightvalve 21:e5f1a43ea6f9 1513 //ADC3->CR2 |= 0x40000000; // adc _ 12bit
Lightvalve 25:3e6b574cab5c 1514 //int raw_cur = ADC3->DR;
Lightvalve 21:e5f1a43ea6f9 1515 //while((ADC3->SR & 0b10));
Lightvalve 51:b46bed7fec80 1516 float alpha_update_cur = 1.0f/(1.0f + FREQ_TMR4/(2.0f*3.14f*500.0f)); // f_cutoff : 500Hz
Lightvalve 30:8d561f16383b 1517 float cur_new = ((float)ADC3->DR-2048.0f)*20.0f/4096.0f; // unit : mA
Lightvalve 30:8d561f16383b 1518 cur.sen=cur.sen*(1.0f-alpha_update_cur)+cur_new*(alpha_update_cur);
Lightvalve 25:3e6b574cab5c 1519 //cur.sen = raw_cur;
Lightvalve 58:2eade98630e2 1520
Lightvalve 57:f4819de54e7a 1521 CNT_TMR4++;
Lightvalve 21:e5f1a43ea6f9 1522 }
Lightvalve 11:82d8768d7351 1523 TIM4->SR = 0x0; // reset the status register
Lightvalve 11:82d8768d7351 1524 }
Lightvalve 19:23b7c1ad8683 1525
Lightvalve 19:23b7c1ad8683 1526
Lightvalve 18:b8adf1582ea3 1527 int j =0;
Lightvalve 54:647072f5307a 1528 float FREQ_TMR3 = (float)FREQ_5k;
Lightvalve 48:889798ff9329 1529 float DT_TMR3 = (float)DT_5k;
Lightvalve 45:35fa6884d0c6 1530 int cnt_trans = 0;
Lightvalve 48:889798ff9329 1531 double VALVE_POS_RAW_FORCE_FB_LOGGING = 0.0f;
Lightvalve 57:f4819de54e7a 1532 int can_rest =0;
Lightvalve 48:889798ff9329 1533
Lightvalve 11:82d8768d7351 1534 extern "C" void TIM3_IRQHandler(void)
Lightvalve 21:e5f1a43ea6f9 1535 {
Lightvalve 19:23b7c1ad8683 1536 if (TIM3->SR & TIM_SR_UIF ) {
Lightvalve 57:f4819de54e7a 1537
Lightvalve 57:f4819de54e7a 1538 if (((OPERATING_MODE&0b110)>>1) == 0) {
Lightvalve 57:f4819de54e7a 1539 K_v = 0.4f; // Moog (LPM >> mA) , 100bar
Lightvalve 57:f4819de54e7a 1540 mV_PER_mA = 500.0f; // 5000mV/10mA
Lightvalve 57:f4819de54e7a 1541 mV_PER_pulse = 0.5f; // 5000mV/10000pulse
Lightvalve 57:f4819de54e7a 1542 mA_PER_pulse = 0.001f; // 10mA/10000pulse
Lightvalve 57:f4819de54e7a 1543 } else if (((OPERATING_MODE&0b110)>>1) == 1) {
Lightvalve 57:f4819de54e7a 1544 K_v = 0.5f; // KNR (LPM >> mA) , 100bar
Lightvalve 57:f4819de54e7a 1545 mV_PER_mA = 166.6666f; // 5000mV/30mA
Lightvalve 57:f4819de54e7a 1546 mV_PER_pulse = 0.5f; // 5000mV/10000pulse
Lightvalve 57:f4819de54e7a 1547 mA_PER_pulse = 0.003f; // 30mA/10000pulse
Lightvalve 57:f4819de54e7a 1548 }
Lightvalve 50:3c630b5eba9f 1549
Lightvalve 50:3c630b5eba9f 1550 if(MODE_POS_FT_TRANS == 1) {
Lightvalve 48:889798ff9329 1551 alpha_trans = (float)(1.0f - cos(3.141592f * (float)cnt_trans * DT_TMR3 /3.0f))/2.0f;
Lightvalve 45:35fa6884d0c6 1552 cnt_trans++;
Lightvalve 46:2694daea349b 1553 torq.err_sum = 0;
Lightvalve 48:889798ff9329 1554 if((float)cnt_trans * DT_TMR3 > 3.0f)
Lightvalve 45:35fa6884d0c6 1555 MODE_POS_FT_TRANS = 2;
Lightvalve 50:3c630b5eba9f 1556 } else if(MODE_POS_FT_TRANS == 3) {
Lightvalve 48:889798ff9329 1557 alpha_trans = (float)(1.0f + cos(3.141592f * (float)cnt_trans * DT_TMR3 /3.0f))/2.0f;
Lightvalve 45:35fa6884d0c6 1558 cnt_trans++;
Lightvalve 46:2694daea349b 1559 torq.err_sum = 0;
Lightvalve 48:889798ff9329 1560 if((float) cnt_trans * DT_TMR3 > 3.0f )
Lightvalve 45:35fa6884d0c6 1561 MODE_POS_FT_TRANS = 0;
Lightvalve 50:3c630b5eba9f 1562 } else if(MODE_POS_FT_TRANS == 2) {
Lightvalve 58:2eade98630e2 1563 alpha_trans = 1.0f;
Lightvalve 45:35fa6884d0c6 1564 cnt_trans = 0;
Lightvalve 50:3c630b5eba9f 1565 } else {
Lightvalve 58:2eade98630e2 1566 alpha_trans = 0.0f;
Lightvalve 45:35fa6884d0c6 1567 cnt_trans = 0;
Lightvalve 45:35fa6884d0c6 1568 }
Lightvalve 45:35fa6884d0c6 1569
Lightvalve 50:3c630b5eba9f 1570
Lightvalve 57:f4819de54e7a 1571 int UTILITY_MODE = 0;
Lightvalve 57:f4819de54e7a 1572 int CONTROL_MODE = 0;
Lightvalve 58:2eade98630e2 1573
Lightvalve 57:f4819de54e7a 1574 if (CONTROL_UTILITY_MODE >= 20 || CONTROL_UTILITY_MODE == 0) {
Lightvalve 57:f4819de54e7a 1575 UTILITY_MODE = CONTROL_UTILITY_MODE;
Lightvalve 57:f4819de54e7a 1576 CONTROL_MODE = MODE_NO_ACT;
Lightvalve 57:f4819de54e7a 1577 } else {
Lightvalve 57:f4819de54e7a 1578 CONTROL_MODE = CONTROL_UTILITY_MODE;
Lightvalve 57:f4819de54e7a 1579 UTILITY_MODE = MODE_NO_ACT;
Lightvalve 57:f4819de54e7a 1580 }
Lightvalve 56:6f50d9d3bfee 1581
Lightvalve 56:6f50d9d3bfee 1582
Lightvalve 56:6f50d9d3bfee 1583
Lightvalve 57:f4819de54e7a 1584 // UTILITY MODE ------------------------------------------------------------
Lightvalve 56:6f50d9d3bfee 1585
Lightvalve 57:f4819de54e7a 1586 switch (UTILITY_MODE) {
Lightvalve 57:f4819de54e7a 1587 case MODE_NO_ACT: {
Lightvalve 13:747daba9cf59 1588 break;
Lightvalve 13:747daba9cf59 1589 }
Lightvalve 14:8e7590227d22 1590
Lightvalve 14:8e7590227d22 1591 case MODE_TORQUE_SENSOR_NULLING: {
Lightvalve 13:747daba9cf59 1592 // DAC Voltage reference set
Lightvalve 14:8e7590227d22 1593 if (TMR3_COUNT_TORQUE_NULL < TMR_FREQ_5k * 2) {
Lightvalve 15:bd0d12728506 1594 CUR_TORQUE_sum += torq.sen;
Lightvalve 19:23b7c1ad8683 1595
Lightvalve 14:8e7590227d22 1596 if (TMR3_COUNT_TORQUE_NULL % 10 == 0) {
Lightvalve 30:8d561f16383b 1597 CUR_TORQUE_mean = CUR_TORQUE_sum / 10.0f;
Lightvalve 13:747daba9cf59 1598 CUR_TORQUE_sum = 0;
Lightvalve 14:8e7590227d22 1599
Lightvalve 84:c355d3e52bf1 1600 TORQUE_VREF += 0.000003f * (0.0f - CUR_TORQUE_mean);
Lightvalve 19:23b7c1ad8683 1601
Lightvalve 30:8d561f16383b 1602 if (TORQUE_VREF > 3.3f) TORQUE_VREF = 3.3f;
Lightvalve 59:f308b1656d9c 1603 if (TORQUE_VREF < 0.0f) TORQUE_VREF = 0.0f;
Lightvalve 50:3c630b5eba9f 1604
Lightvalve 16:903b5a4433b4 1605 //spi_eeprom_write(RID_TORQUE_SENSOR_VREF, (int16_t) (TORQUE_VREF * 1000.0));
Lightvalve 30:8d561f16383b 1606 dac_1 = TORQUE_VREF / 3.3f;
Lightvalve 13:747daba9cf59 1607 }
Lightvalve 13:747daba9cf59 1608 } else {
Lightvalve 58:2eade98630e2 1609 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 14:8e7590227d22 1610 TMR3_COUNT_TORQUE_NULL = 0;
Lightvalve 13:747daba9cf59 1611 CUR_TORQUE_sum = 0;
Lightvalve 13:747daba9cf59 1612 CUR_TORQUE_mean = 0;
Lightvalve 19:23b7c1ad8683 1613
Lightvalve 170:42c938a40313 1614 // ROM_RESET_DATA();
Lightvalve 170:42c938a40313 1615 spi_eeprom_write(RID_TORQUE_SENSOR_VREF, (int16_t) (TORQUE_VREF * 1000.0f));
Lightvalve 19:23b7c1ad8683 1616
Lightvalve 30:8d561f16383b 1617 dac_1 = TORQUE_VREF / 3.3f;
Lightvalve 14:8e7590227d22 1618
Lightvalve 13:747daba9cf59 1619 }
Lightvalve 14:8e7590227d22 1620 TMR3_COUNT_TORQUE_NULL++;
Lightvalve 13:747daba9cf59 1621 break;
Lightvalve 19:23b7c1ad8683 1622 }
Lightvalve 14:8e7590227d22 1623
Lightvalve 50:3c630b5eba9f 1624 // case MODE_VALVE_NULLING_AND_DEADZONE_SETTING: {
Lightvalve 50:3c630b5eba9f 1625 // if (TMR3_COUNT_DEADZONE == 0) {
Lightvalve 50:3c630b5eba9f 1626 // if (pos_plus_end == pos_minus_end) need_enc_init = true;
Lightvalve 50:3c630b5eba9f 1627 // else temp_time = 0;
Lightvalve 50:3c630b5eba9f 1628 // }
Lightvalve 50:3c630b5eba9f 1629 // if (need_enc_init) {
Lightvalve 50:3c630b5eba9f 1630 // if (TMR3_COUNT_DEADZONE < (int) (0.5f * (float) TMR_FREQ_5k)) {
Lightvalve 50:3c630b5eba9f 1631 // V_out = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1632 // pos_plus_end = pos.sen;
Lightvalve 50:3c630b5eba9f 1633 // } else if (TMR3_COUNT_DEADZONE < TMR_FREQ_5k) {
Lightvalve 50:3c630b5eba9f 1634 // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1635 // pos_minus_end = pos.sen;
Lightvalve 50:3c630b5eba9f 1636 // } else if (TMR3_COUNT_DEADZONE == TMR_FREQ_5k) need_enc_init = false;
Lightvalve 50:3c630b5eba9f 1637 // temp_time = TMR_FREQ_5k;
Lightvalve 50:3c630b5eba9f 1638 // }
Lightvalve 50:3c630b5eba9f 1639 //
Lightvalve 50:3c630b5eba9f 1640 // if (temp_time <= TMR3_COUNT_DEADZONE && TMR3_COUNT_DEADZONE < (temp_time + TMR_FREQ_5k)) {
Lightvalve 50:3c630b5eba9f 1641 // V_out = (float) P_GAIN_JOINT_POSITION * (0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen);
Lightvalve 50:3c630b5eba9f 1642 // VALVE_CENTER = VALVE_DEADZONE_PLUS = VALVE_DEADZONE_MINUS = 0;
Lightvalve 50:3c630b5eba9f 1643 //
Lightvalve 50:3c630b5eba9f 1644 // } else if (temp_time <= TMR3_COUNT_DEADZONE && TMR3_COUNT_DEADZONE < (temp_time + (int) (1.9f * (float) TMR_FREQ_5k))) {
Lightvalve 50:3c630b5eba9f 1645 // V_out = 0;
Lightvalve 50:3c630b5eba9f 1646 // CUR_VELOCITY_sum += CUR_VELOCITY;
Lightvalve 50:3c630b5eba9f 1647 // } else if (TMR3_COUNT_DEADZONE == (temp_time + 2 * TMR_FREQ_5k)) {
Lightvalve 50:3c630b5eba9f 1648 // if (CUR_VELOCITY_sum == 0) DZ_dir = 1;
Lightvalve 50:3c630b5eba9f 1649 // else if (CUR_VELOCITY_sum > 0) DZ_dir = 1;
Lightvalve 50:3c630b5eba9f 1650 // else if (CUR_VELOCITY_sum < 0) DZ_dir = -1;
Lightvalve 50:3c630b5eba9f 1651 // else DZ_temp_cnt2 = DZ_end;
Lightvalve 50:3c630b5eba9f 1652 // CUR_VELOCITY_sum = 0;
Lightvalve 50:3c630b5eba9f 1653 // } else if (TMR3_COUNT_DEADZONE > (temp_time + 2 * TMR_FREQ_5k)) {
Lightvalve 50:3c630b5eba9f 1654 // if (TMR3_COUNT_DEADZONE > (temp_time + 10 * TMR_FREQ_5k)) DZ_temp_cnt2 = DZ_end;
Lightvalve 50:3c630b5eba9f 1655 //
Lightvalve 50:3c630b5eba9f 1656 // // Position of Dead Zone
Lightvalve 50:3c630b5eba9f 1657 // // (CUR_VELOCITY < 0) (CUR_VELOCITY == 0) (CUR_VELOCITY > 0)
Lightvalve 50:3c630b5eba9f 1658 // // | / | / |/
Lightvalve 50:3c630b5eba9f 1659 // // | ______/ ___|___/ ______/|
Lightvalve 50:3c630b5eba9f 1660 // // |/ / | / |
Lightvalve 50:3c630b5eba9f 1661 // // /| / | / |
Lightvalve 50:3c630b5eba9f 1662 // // 0V 0V 0V
Lightvalve 50:3c630b5eba9f 1663 //
Lightvalve 50:3c630b5eba9f 1664 // if (DZ_temp_cnt2 < DZ_end) {
Lightvalve 50:3c630b5eba9f 1665 // if (TMR3_COUNT_DEADZONE % 20 != 0) {
Lightvalve 50:3c630b5eba9f 1666 // CUR_VELOCITY_sum += CUR_VELOCITY;
Lightvalve 50:3c630b5eba9f 1667 // } else {
Lightvalve 50:3c630b5eba9f 1668 // V_out -= DZ_dir;
Lightvalve 50:3c630b5eba9f 1669 // if (CUR_VELOCITY_sum * DZ_dir < 0) DZ_temp_cnt++;
Lightvalve 50:3c630b5eba9f 1670 // CUR_VELOCITY_sum = 0;
Lightvalve 50:3c630b5eba9f 1671 // }
Lightvalve 50:3c630b5eba9f 1672 // if (DZ_temp_cnt == 5) {
Lightvalve 50:3c630b5eba9f 1673 // if (DZ_dir >= 0) VALVE_DEADZONE_MINUS = (int16_t) V_out;
Lightvalve 50:3c630b5eba9f 1674 // else VALVE_DEADZONE_PLUS = (int16_t) V_out;
Lightvalve 50:3c630b5eba9f 1675 // DZ_dir = -DZ_dir;
Lightvalve 50:3c630b5eba9f 1676 // DZ_temp_cnt = 0;
Lightvalve 50:3c630b5eba9f 1677 // DZ_temp_cnt2++;
Lightvalve 50:3c630b5eba9f 1678 // }
Lightvalve 50:3c630b5eba9f 1679 // } else {
Lightvalve 50:3c630b5eba9f 1680 // TMR3_COUNT_DEADZONE = -1;
Lightvalve 50:3c630b5eba9f 1681 // VALVE_CENTER = VALVE_DEADZONE_PLUS / 2 + VALVE_DEADZONE_MINUS / 2;
Lightvalve 50:3c630b5eba9f 1682 // if (VALVE_DEADZONE_PLUS < VALVE_DEADZONE_MINUS) {
Lightvalve 50:3c630b5eba9f 1683 // VALVE_DEADZONE_PLUS = VALVE_CENTER;
Lightvalve 50:3c630b5eba9f 1684 // VALVE_DEADZONE_MINUS = VALVE_CENTER;
Lightvalve 50:3c630b5eba9f 1685 // }
Lightvalve 50:3c630b5eba9f 1686 // V_out = 0;
Lightvalve 50:3c630b5eba9f 1687 //
Lightvalve 50:3c630b5eba9f 1688 // ROM_RESET_DATA();
Lightvalve 50:3c630b5eba9f 1689 //
Lightvalve 50:3c630b5eba9f 1690 // //spi_eeprom_write(RID_VALVE_DEADZONE_PLUS, VALVE_DEADZONE_PLUS);
Lightvalve 50:3c630b5eba9f 1691 // //spi_eeprom_write(RID_VALVE_DEADZONE_MINUS, VALVE_DEADZONE_MINUS);
Lightvalve 50:3c630b5eba9f 1692 //
Lightvalve 50:3c630b5eba9f 1693 // CONTROL_MODE = MODE_NO_ACT;
Lightvalve 50:3c630b5eba9f 1694 // DZ_temp_cnt2 = 0;
Lightvalve 50:3c630b5eba9f 1695 // }
Lightvalve 50:3c630b5eba9f 1696 // }
Lightvalve 50:3c630b5eba9f 1697 // TMR3_COUNT_DEADZONE++;
Lightvalve 50:3c630b5eba9f 1698 // break;
Lightvalve 50:3c630b5eba9f 1699 // }
Lightvalve 14:8e7590227d22 1700
Lightvalve 14:8e7590227d22 1701 case MODE_FIND_HOME: {
Lightvalve 29:69f3f5445d6d 1702 if (FINDHOME_STAGE == FINDHOME_INIT) {
Lightvalve 14:8e7590227d22 1703 cnt_findhome = 0;
Lightvalve 14:8e7590227d22 1704 cnt_vel_findhome = 0;
Lightvalve 14:8e7590227d22 1705 //REFERENCE_MODE = MODE_REF_NO_ACT; // Stop taking reference data from PODO
Lightvalve 59:f308b1656d9c 1706 pos.ref = pos.sen;
Lightvalve 59:f308b1656d9c 1707 vel.ref = 0.0f;
Lightvalve 29:69f3f5445d6d 1708 FINDHOME_STAGE = FINDHOME_GOTOLIMIT;
Lightvalve 29:69f3f5445d6d 1709 } else if (FINDHOME_STAGE == FINDHOME_GOTOLIMIT) {
Lightvalve 67:c2812cf26c38 1710 int cnt_check_enc = (TMR_FREQ_5k/20);
Lightvalve 29:69f3f5445d6d 1711 if(cnt_findhome%cnt_check_enc == 0) {
Lightvalve 29:69f3f5445d6d 1712 FINDHOME_POSITION = pos.sen;
Lightvalve 29:69f3f5445d6d 1713 FINDHOME_VELOCITY = FINDHOME_POSITION - FINDHOME_POSITION_OLD;
Lightvalve 29:69f3f5445d6d 1714 FINDHOME_POSITION_OLD = FINDHOME_POSITION;
Lightvalve 29:69f3f5445d6d 1715 }
Lightvalve 29:69f3f5445d6d 1716 cnt_findhome++;
Lightvalve 14:8e7590227d22 1717
Lightvalve 29:69f3f5445d6d 1718 if (abs(FINDHOME_VELOCITY) <= 1) {
Lightvalve 29:69f3f5445d6d 1719 cnt_vel_findhome = cnt_vel_findhome + 1;
Lightvalve 29:69f3f5445d6d 1720 } else {
Lightvalve 29:69f3f5445d6d 1721 cnt_vel_findhome = 0;
Lightvalve 29:69f3f5445d6d 1722 }
Lightvalve 19:23b7c1ad8683 1723
Lightvalve 57:f4819de54e7a 1724 if ((cnt_vel_findhome < 3*TMR_FREQ_5k) && cnt_findhome < 10*TMR_FREQ_5k) { // wait for 3sec
Lightvalve 29:69f3f5445d6d 1725 //REFERENCE_MODE = MODE_REF_NO_ACT;
Lightvalve 67:c2812cf26c38 1726 if (HOMEPOS_OFFSET > 0) pos.ref = pos.ref + 12.0f;
Lightvalve 67:c2812cf26c38 1727 else pos.ref = pos.ref - 12.0f;
Lightvalve 61:bc8c8270f0ab 1728
Lightvalve 59:f308b1656d9c 1729 // pos.err = pos.ref_home_pos - pos.sen;
Lightvalve 59:f308b1656d9c 1730 // float VALVE_POS_RAW_POS_FB = 0.0f;
Lightvalve 59:f308b1656d9c 1731 // VALVE_POS_RAW_POS_FB = (float) P_GAIN_JOINT_POSITION * pos.err/(float) ENC_PULSE_PER_POSITION * 0.01f;
Lightvalve 59:f308b1656d9c 1732 // valve_pos.ref = VALVE_POS_RAW_POS_FB + (float) VALVE_CENTER;
Lightvalve 59:f308b1656d9c 1733 // VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 61:bc8c8270f0ab 1734
Lightvalve 59:f308b1656d9c 1735 CONTROL_MODE = MODE_JOINT_CONTROL;
Lightvalve 59:f308b1656d9c 1736 alpha_trans = 0.0f;
Lightvalve 61:bc8c8270f0ab 1737
Lightvalve 34:bb2ca2fc2a8e 1738
Lightvalve 29:69f3f5445d6d 1739 } else {
Lightvalve 29:69f3f5445d6d 1740 ENC_SET(HOMEPOS_OFFSET);
Lightvalve 67:c2812cf26c38 1741 // ENC_SET_ZERO();
Lightvalve 29:69f3f5445d6d 1742 INIT_REF_POS = HOMEPOS_OFFSET;
Lightvalve 29:69f3f5445d6d 1743 REF_POSITION = 0;
Lightvalve 29:69f3f5445d6d 1744 REF_VELOCITY = 0;
Lightvalve 29:69f3f5445d6d 1745 FINDHOME_POSITION = 0;
Lightvalve 29:69f3f5445d6d 1746 FINDHOME_POSITION_OLD = 0;
Lightvalve 29:69f3f5445d6d 1747 FINDHOME_VELOCITY = 0;
Lightvalve 29:69f3f5445d6d 1748 cnt_findhome = 0;
Lightvalve 29:69f3f5445d6d 1749 cnt_vel_findhome = 0;
Lightvalve 29:69f3f5445d6d 1750 FINDHOME_STAGE = FINDHOME_ZEROPOSE;
Lightvalve 67:c2812cf26c38 1751
Lightvalve 67:c2812cf26c38 1752
Lightvalve 67:c2812cf26c38 1753 cnt_findhome = 0;
Lightvalve 67:c2812cf26c38 1754 pos.ref = 0.0f;
Lightvalve 67:c2812cf26c38 1755 vel.ref = 0.0f;
Lightvalve 67:c2812cf26c38 1756 pos.ref_home_pos = 0.0f;
Lightvalve 67:c2812cf26c38 1757 vel.ref_home_pos = 0.0f;
Lightvalve 67:c2812cf26c38 1758 //FINDHOME_STAGE = FINDHOME_INIT;
Lightvalve 67:c2812cf26c38 1759 //CONTROL_UTILITY_MODE = MODE_JOINT_CONTROL;
Lightvalve 169:645207e160ca 1760
Lightvalve 169:645207e160ca 1761
Lightvalve 29:69f3f5445d6d 1762 }
Lightvalve 29:69f3f5445d6d 1763 } else if (FINDHOME_STAGE == FINDHOME_ZEROPOSE) {
Lightvalve 29:69f3f5445d6d 1764 int T_move = 2*TMR_FREQ_5k;
Lightvalve 59:f308b1656d9c 1765 pos.ref = (0.0f - (float)INIT_REF_POS)*0.5f*(1.0f - cos(3.14159f * (float)cnt_findhome / (float)T_move)) + (float)INIT_REF_POS;
Lightvalve 67:c2812cf26c38 1766 //pos.ref = 0.0f;
Lightvalve 59:f308b1656d9c 1767 vel.ref = 0.0f;
Lightvalve 169:645207e160ca 1768
Lightvalve 29:69f3f5445d6d 1769 // input for position control
Lightvalve 169:645207e160ca 1770
Lightvalve 67:c2812cf26c38 1771 // CONTROL_MODE = MODE_JOINT_CONTROL;
Lightvalve 67:c2812cf26c38 1772 alpha_trans = 0.0f;
Lightvalve 61:bc8c8270f0ab 1773
Lightvalve 67:c2812cf26c38 1774 double torq_ref = 0.0f;
Lightvalve 67:c2812cf26c38 1775 pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm]
Lightvalve 67:c2812cf26c38 1776 vel.err = (0.0f - vel.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm/s]
Lightvalve 67:c2812cf26c38 1777 pos.err_sum += pos.err/(float) TMR_FREQ_5k; //[mm]
Lightvalve 67:c2812cf26c38 1778
Lightvalve 67:c2812cf26c38 1779 if (((OPERATING_MODE&0b110)>>1) == 0 || ((OPERATING_MODE&0b110)>>1) == 1) {
Lightvalve 67:c2812cf26c38 1780
Lightvalve 67:c2812cf26c38 1781 double I_REF_POS = 0.0f;
Lightvalve 67:c2812cf26c38 1782 double I_REF_FORCE_FB = 0.0f; // I_REF by Force Feedback
Lightvalve 67:c2812cf26c38 1783 double I_REF_VC = 0.0f; // I_REF for velocity compensation
Lightvalve 67:c2812cf26c38 1784
Lightvalve 67:c2812cf26c38 1785 double temp_vel_pos = 0.0f;
Lightvalve 67:c2812cf26c38 1786 double temp_vel_torq = 0.0f;
Lightvalve 67:c2812cf26c38 1787 double wn_Pos = 2.0f * PI * 5.0f; // f_cut : 5Hz Position Control
Lightvalve 67:c2812cf26c38 1788
Lightvalve 69:3995ffeaa786 1789 if ((OPERATING_MODE & 0x01) == 0) { // Rotary Mode
Lightvalve 67:c2812cf26c38 1790 temp_vel_pos = (0.01f * (double) P_GAIN_JOINT_POSITION * wn_Pos * pos.err + 0.01f * (double) I_GAIN_JOINT_POSITION * wn_Pos * pos.err_sum + 0.01f * (double) VELOCITY_COMP_GAIN * vel.ref / ENC_PULSE_PER_POSITION) * 3.14159f / 180.0f; // rad/s
Lightvalve 67:c2812cf26c38 1791 // L when P-gain = 100, f_cut = 10Hz L feedforward velocity
Lightvalve 69:3995ffeaa786 1792 } else if ((OPERATING_MODE & 0x01) == 1) {
Lightvalve 67:c2812cf26c38 1793 temp_vel_pos = (0.01f * (double) P_GAIN_JOINT_POSITION * wn_Pos * pos.err + 0.01f * (double) I_GAIN_JOINT_POSITION * wn_Pos * pos.err_sum + 0.01f * (double) VELOCITY_COMP_GAIN * vel.ref / ENC_PULSE_PER_POSITION); // mm/s
Lightvalve 67:c2812cf26c38 1794 // L when P-gain = 100, f_cut = 10Hz L feedforward velocity
Lightvalve 67:c2812cf26c38 1795 }
Lightvalve 67:c2812cf26c38 1796 if (temp_vel_pos > 0.0f) I_REF_POS = temp_vel_pos * ((double) PISTON_AREA_A * 0.00006f / (K_v * sqrt(2.0f * alpha3 / (alpha3 + 1.0f))));
Lightvalve 67:c2812cf26c38 1797 else I_REF_POS = temp_vel_pos * ((double) PISTON_AREA_B * 0.00006f / (K_v * sqrt(2.0f / (alpha3 + 1.0f))));
Lightvalve 67:c2812cf26c38 1798
Lightvalve 67:c2812cf26c38 1799 I_REF = I_REF_POS;
Lightvalve 67:c2812cf26c38 1800
Lightvalve 67:c2812cf26c38 1801
Lightvalve 67:c2812cf26c38 1802
Lightvalve 67:c2812cf26c38 1803 } else {
Lightvalve 67:c2812cf26c38 1804 float VALVE_POS_RAW_FORCE_FB = 0.0f;
Lightvalve 67:c2812cf26c38 1805 VALVE_POS_RAW_FORCE_FB = DDV_JOINT_POS_FF(vel.sen) + (P_GAIN_JOINT_POSITION * 0.01f * pos.err + DDV_JOINT_POS_FF(vel.ref));
Lightvalve 67:c2812cf26c38 1806
Lightvalve 67:c2812cf26c38 1807 if (VALVE_POS_RAW_FORCE_FB >= 0) {
Lightvalve 67:c2812cf26c38 1808 valve_pos.ref = VALVE_POS_RAW_FORCE_FB + VALVE_DEADZONE_PLUS;
Lightvalve 67:c2812cf26c38 1809 } else {
Lightvalve 67:c2812cf26c38 1810 valve_pos.ref = VALVE_POS_RAW_FORCE_FB + VALVE_DEADZONE_MINUS;
Lightvalve 67:c2812cf26c38 1811 }
Lightvalve 67:c2812cf26c38 1812
Lightvalve 67:c2812cf26c38 1813 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 67:c2812cf26c38 1814
Lightvalve 67:c2812cf26c38 1815 V_out = (float) Vout.ref;
Lightvalve 67:c2812cf26c38 1816
Lightvalve 67:c2812cf26c38 1817 }
Lightvalve 67:c2812cf26c38 1818
Lightvalve 67:c2812cf26c38 1819
Lightvalve 169:645207e160ca 1820
Lightvalve 169:645207e160ca 1821
Lightvalve 59:f308b1656d9c 1822 // pos.err = pos.ref - (float)pos.sen;
Lightvalve 59:f308b1656d9c 1823 // float VALVE_POS_RAW_POS_FB = 0.0f;
Lightvalve 59:f308b1656d9c 1824 // VALVE_POS_RAW_POS_FB = (float) P_GAIN_JOINT_POSITION * 0.01f * pos.err/(float) ENC_PULSE_PER_POSITION;
Lightvalve 59:f308b1656d9c 1825 // valve_pos.ref = VALVE_POS_RAW_POS_FB + (float) VALVE_CENTER;
Lightvalve 59:f308b1656d9c 1826 // VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 29:69f3f5445d6d 1827
Lightvalve 29:69f3f5445d6d 1828 cnt_findhome++;
Lightvalve 29:69f3f5445d6d 1829 if (cnt_findhome >= T_move) {
Lightvalve 29:69f3f5445d6d 1830 //REFERENCE_MODE = MODE_REF_DIRECT;
Lightvalve 29:69f3f5445d6d 1831 cnt_findhome = 0;
Lightvalve 30:8d561f16383b 1832 pos.ref = 0.0f;
Lightvalve 30:8d561f16383b 1833 vel.ref = 0.0f;
Lightvalve 30:8d561f16383b 1834 pos.ref_home_pos = 0.0f;
Lightvalve 30:8d561f16383b 1835 vel.ref_home_pos = 0.0f;
Lightvalve 29:69f3f5445d6d 1836 FINDHOME_STAGE = FINDHOME_INIT;
Lightvalve 57:f4819de54e7a 1837 CONTROL_UTILITY_MODE = MODE_JOINT_CONTROL;
Lightvalve 29:69f3f5445d6d 1838 }
Lightvalve 13:747daba9cf59 1839 }
Lightvalve 19:23b7c1ad8683 1840
Lightvalve 13:747daba9cf59 1841 break;
Lightvalve 13:747daba9cf59 1842 }
Lightvalve 14:8e7590227d22 1843
Lightvalve 50:3c630b5eba9f 1844 // case MODE_VALVE_GAIN_SETTING: {
Lightvalve 50:3c630b5eba9f 1845 // if (TMR3_COUNT_FLOWRATE == 0) {
Lightvalve 50:3c630b5eba9f 1846 // if (pos_plus_end == pos_minus_end) need_enc_init = true;
Lightvalve 50:3c630b5eba9f 1847 // else {
Lightvalve 50:3c630b5eba9f 1848 // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1849 // temp_time = (int) (0.5f * (float) TMR_FREQ_5k);
Lightvalve 50:3c630b5eba9f 1850 // }
Lightvalve 50:3c630b5eba9f 1851 // }
Lightvalve 50:3c630b5eba9f 1852 // if (need_enc_init) {
Lightvalve 50:3c630b5eba9f 1853 // if (TMR3_COUNT_FLOWRATE < (int) (0.5f * (float) TMR_FREQ_5k)) {
Lightvalve 50:3c630b5eba9f 1854 // V_out = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1855 // pos_plus_end = pos.sen;
Lightvalve 50:3c630b5eba9f 1856 // } else if (TMR3_COUNT_FLOWRATE < TMR_FREQ_5k) {
Lightvalve 50:3c630b5eba9f 1857 // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1858 // pos_minus_end = pos.sen;
Lightvalve 50:3c630b5eba9f 1859 // } else if (TMR3_COUNT_FLOWRATE == TMR_FREQ_5k) {
Lightvalve 50:3c630b5eba9f 1860 // need_enc_init = false;
Lightvalve 50:3c630b5eba9f 1861 // check_vel_pos_init = (int) (0.9f * (float) (pos_plus_end - pos_minus_end));
Lightvalve 50:3c630b5eba9f 1862 // check_vel_pos_fin = (int) (0.95f * (float) (pos_plus_end - pos_minus_end));
Lightvalve 50:3c630b5eba9f 1863 // check_vel_pos_interv = check_vel_pos_fin - check_vel_pos_init;
Lightvalve 50:3c630b5eba9f 1864 // }
Lightvalve 50:3c630b5eba9f 1865 // temp_time = TMR_FREQ_5k;
Lightvalve 50:3c630b5eba9f 1866 // }
Lightvalve 50:3c630b5eba9f 1867 // TMR3_COUNT_FLOWRATE++;
Lightvalve 50:3c630b5eba9f 1868 // if (TMR3_COUNT_FLOWRATE > temp_time) {
Lightvalve 50:3c630b5eba9f 1869 // if (flag_flowrate % 2 == 0) { // (+)
Lightvalve 50:3c630b5eba9f 1870 // VALVE_VOLTAGE = 1000.0f * (float) (flag_flowrate / 2 + 1);
Lightvalve 50:3c630b5eba9f 1871 // V_out = VALVE_VOLTAGE;
Lightvalve 50:3c630b5eba9f 1872 // if (pos.sen > (pos_minus_end + check_vel_pos_init) && pos.sen < (pos_minus_end + check_vel_pos_fin)) {
Lightvalve 50:3c630b5eba9f 1873 // fl_temp_cnt++;
Lightvalve 50:3c630b5eba9f 1874 // } else if (pos.sen >= (pos_minus_end + check_vel_pos_fin) && CUR_VELOCITY == 0) {
Lightvalve 50:3c630b5eba9f 1875 // VALVE_GAIN_LPM_PER_V[flag_flowrate] = 0.95873f * 0.5757f * (float) TMR_FREQ_5k / 10000.0 * (float) check_vel_pos_interv / (float) fl_temp_cnt / VALVE_VOLTAGE; // 0.9587=6*pi/65536*10000 0.5757=0.02525*0.02*0.0095*2*60*1000
Lightvalve 50:3c630b5eba9f 1876 // // VALVE_GAIN_LPM_PER_V[flag_flowrate] = (float) TMR_FREQ_10k * (float) check_vel_pos_interv / (float) fl_temp_cnt / VALVE_VOLTAGE; // PULSE/sec
Lightvalve 50:3c630b5eba9f 1877 // fl_temp_cnt2++;
Lightvalve 50:3c630b5eba9f 1878 // }
Lightvalve 50:3c630b5eba9f 1879 // } else if (flag_flowrate % 2 == 1) { // (-)
Lightvalve 50:3c630b5eba9f 1880 // VALVE_VOLTAGE = -1. * (float) (flag_flowrate / 2 + 1);
Lightvalve 50:3c630b5eba9f 1881 // V_out = VALVE_VOLTAGE;
Lightvalve 50:3c630b5eba9f 1882 // if (pos.sen < (pos_plus_end - check_vel_pos_init) && pos.sen > (pos_plus_end - check_vel_pos_fin)) {
Lightvalve 50:3c630b5eba9f 1883 // fl_temp_cnt++;
Lightvalve 50:3c630b5eba9f 1884 // } else if (pos.sen <= (pos_plus_end - check_vel_pos_fin) && CUR_VELOCITY == 0) {
Lightvalve 50:3c630b5eba9f 1885 // VALVE_GAIN_LPM_PER_V[flag_flowrate] = 0.95873f * 0.5757f * (float) TMR_FREQ_5k / 10000.0f * (float) check_vel_pos_interv / (float) fl_temp_cnt / (-VALVE_VOLTAGE);
Lightvalve 50:3c630b5eba9f 1886 // // VALVE_GAIN_LPM_PER_V[flag_flowrate] = (float) TMR_FREQ_10k * (float) check_vel_pos_interv / (float) fl_temp_cnt / (-VALVE_VOLTAGE); // PULSE/sec
Lightvalve 50:3c630b5eba9f 1887 // fl_temp_cnt2++;
Lightvalve 50:3c630b5eba9f 1888 // }
Lightvalve 50:3c630b5eba9f 1889 // }
Lightvalve 50:3c630b5eba9f 1890 // if (fl_temp_cnt2 == 100) {
Lightvalve 50:3c630b5eba9f 1891 //
Lightvalve 50:3c630b5eba9f 1892 // ROM_RESET_DATA();
Lightvalve 50:3c630b5eba9f 1893 //
Lightvalve 50:3c630b5eba9f 1894 // //spi_eeprom_write(RID_VALVE_GAIN_PLUS_1 + flag_flowrate, (int16_t) (VALVE_GAIN_LPM_PER_V[flag_flowrate] * 100.0f));
Lightvalve 50:3c630b5eba9f 1895 // cur_vel_sum = 0;
Lightvalve 50:3c630b5eba9f 1896 // fl_temp_cnt = 0;
Lightvalve 50:3c630b5eba9f 1897 // fl_temp_cnt2 = 0;
Lightvalve 50:3c630b5eba9f 1898 // flag_flowrate++;
Lightvalve 50:3c630b5eba9f 1899 // }
Lightvalve 50:3c630b5eba9f 1900 // if (flag_flowrate == 10) {
Lightvalve 50:3c630b5eba9f 1901 // V_out = 0;
Lightvalve 50:3c630b5eba9f 1902 // flag_flowrate = 0;
Lightvalve 50:3c630b5eba9f 1903 // TMR3_COUNT_FLOWRATE = 0;
Lightvalve 50:3c630b5eba9f 1904 // valve_gain_repeat_cnt++;
Lightvalve 50:3c630b5eba9f 1905 // if (valve_gain_repeat_cnt >= 1) {
Lightvalve 50:3c630b5eba9f 1906 // CONTROL_MODE = MODE_NO_ACT;
Lightvalve 50:3c630b5eba9f 1907 // valve_gain_repeat_cnt = 0;
Lightvalve 50:3c630b5eba9f 1908 // }
Lightvalve 50:3c630b5eba9f 1909 //
Lightvalve 50:3c630b5eba9f 1910 // }
Lightvalve 50:3c630b5eba9f 1911 // break;
Lightvalve 50:3c630b5eba9f 1912 // }
Lightvalve 50:3c630b5eba9f 1913 //
Lightvalve 50:3c630b5eba9f 1914 // }
Lightvalve 14:8e7590227d22 1915 case MODE_PRESSURE_SENSOR_NULLING: {
Lightvalve 13:747daba9cf59 1916 // DAC Voltage reference set
Lightvalve 14:8e7590227d22 1917 if (TMR3_COUNT_PRES_NULL < TMR_FREQ_5k * 2) {
Lightvalve 14:8e7590227d22 1918 CUR_PRES_A_sum += pres_A.sen;
Lightvalve 14:8e7590227d22 1919 CUR_PRES_B_sum += pres_B.sen;
Lightvalve 14:8e7590227d22 1920
Lightvalve 14:8e7590227d22 1921 if (TMR3_COUNT_PRES_NULL % 10 == 0) {
Lightvalve 30:8d561f16383b 1922 CUR_PRES_A_mean = CUR_PRES_A_sum / 10.0f;
Lightvalve 30:8d561f16383b 1923 CUR_PRES_B_mean = CUR_PRES_B_sum / 10.0f;
Lightvalve 13:747daba9cf59 1924 CUR_PRES_A_sum = 0;
Lightvalve 13:747daba9cf59 1925 CUR_PRES_B_sum = 0;
Lightvalve 14:8e7590227d22 1926
Lightvalve 38:118df027d851 1927 float VREF_NullingGain = 0.0003f;
Lightvalve 58:2eade98630e2 1928 PRES_A_VREF = PRES_A_VREF + VREF_NullingGain * CUR_PRES_A_mean;
Lightvalve 58:2eade98630e2 1929 PRES_B_VREF = PRES_B_VREF + VREF_NullingGain * CUR_PRES_B_mean;
Lightvalve 14:8e7590227d22 1930
Lightvalve 30:8d561f16383b 1931 if (PRES_A_VREF > 3.3f) PRES_A_VREF = 3.3f;
Lightvalve 30:8d561f16383b 1932 if (PRES_A_VREF < 0.0f) PRES_A_VREF = 0.0f;
Lightvalve 30:8d561f16383b 1933 if (PRES_B_VREF > 3.3f) PRES_B_VREF = 3.3f;
Lightvalve 30:8d561f16383b 1934 if (PRES_B_VREF < 0.0f) PRES_B_VREF = 0.0f;
Lightvalve 19:23b7c1ad8683 1935
Lightvalve 30:8d561f16383b 1936 dac_1 = PRES_A_VREF / 3.3f;
Lightvalve 30:8d561f16383b 1937 dac_2 = PRES_B_VREF / 3.3f;
Lightvalve 13:747daba9cf59 1938 }
Lightvalve 13:747daba9cf59 1939 } else {
Lightvalve 57:f4819de54e7a 1940 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 14:8e7590227d22 1941 TMR3_COUNT_PRES_NULL = 0;
Lightvalve 13:747daba9cf59 1942 CUR_PRES_A_sum = 0;
Lightvalve 13:747daba9cf59 1943 CUR_PRES_B_sum = 0;
Lightvalve 13:747daba9cf59 1944 CUR_PRES_A_mean = 0;
Lightvalve 13:747daba9cf59 1945 CUR_PRES_B_mean = 0;
Lightvalve 14:8e7590227d22 1946
Lightvalve 170:42c938a40313 1947 // ROM_RESET_DATA();
Lightvalve 170:42c938a40313 1948 spi_eeprom_write(RID_PRES_A_SENSOR_VREF, (int16_t) (PRES_A_VREF * 1000.0f));
Lightvalve 170:42c938a40313 1949 spi_eeprom_write(RID_PRES_B_SENSOR_VREF, (int16_t) (PRES_B_VREF * 1000.0f));
Lightvalve 19:23b7c1ad8683 1950
Lightvalve 30:8d561f16383b 1951 dac_1 = PRES_A_VREF / 3.3f;
Lightvalve 30:8d561f16383b 1952 dac_2 = PRES_B_VREF / 3.3f;
Lightvalve 17:1865016ca2e7 1953 //pc.printf("nulling end");
Lightvalve 13:747daba9cf59 1954 }
Lightvalve 14:8e7590227d22 1955 TMR3_COUNT_PRES_NULL++;
Lightvalve 13:747daba9cf59 1956 break;
Lightvalve 13:747daba9cf59 1957 }
Lightvalve 14:8e7590227d22 1958
Lightvalve 50:3c630b5eba9f 1959 // case MODE_PRESSURE_SENSOR_CALIB: {
Lightvalve 50:3c630b5eba9f 1960 // if (TMR3_COUNT_PRES_CALIB < 2 * TMR_FREQ_5k) {
Lightvalve 50:3c630b5eba9f 1961 // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1962 // if (TMR3_COUNT_PRES_CALIB >= TMR_FREQ_5k) {
Lightvalve 50:3c630b5eba9f 1963 // CUR_PRES_A_sum += CUR_PRES_A;
Lightvalve 50:3c630b5eba9f 1964 // }
Lightvalve 50:3c630b5eba9f 1965 // } else if (TMR3_COUNT_PRES_CALIB < 4 * TMR_FREQ_5k) {
Lightvalve 50:3c630b5eba9f 1966 // V_out = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1967 // if (TMR3_COUNT_PRES_CALIB >= 3 * TMR_FREQ_5k) {
Lightvalve 50:3c630b5eba9f 1968 // CUR_PRES_B_sum += CUR_PRES_B;
Lightvalve 50:3c630b5eba9f 1969 // }
Lightvalve 50:3c630b5eba9f 1970 // } else {
Lightvalve 50:3c630b5eba9f 1971 // CONTROL_MODE = MODE_NO_ACT;
Lightvalve 50:3c630b5eba9f 1972 // TMR3_COUNT_PRES_CALIB = 0;
Lightvalve 50:3c630b5eba9f 1973 // V_out = 0;
Lightvalve 50:3c630b5eba9f 1974 // PRES_SENSOR_A_PULSE_PER_BAR = CUR_PRES_A_sum / ((float) TMR_FREQ_5k - 1.0f) - PRES_A_NULL;
Lightvalve 50:3c630b5eba9f 1975 // PRES_SENSOR_A_PULSE_PER_BAR = PRES_SENSOR_A_PULSE_PER_BAR / ((float) PRES_SUPPLY - 1.0f);
Lightvalve 50:3c630b5eba9f 1976 // PRES_SENSOR_B_PULSE_PER_BAR = CUR_PRES_B_sum / ((float) TMR_FREQ_5k - 1.0f) - PRES_B_NULL;
Lightvalve 50:3c630b5eba9f 1977 // PRES_SENSOR_B_PULSE_PER_BAR = PRES_SENSOR_B_PULSE_PER_BAR / ((float) PRES_SUPPLY - 1.0f);
Lightvalve 50:3c630b5eba9f 1978 // CUR_PRES_A_sum = 0;
Lightvalve 50:3c630b5eba9f 1979 // CUR_PRES_B_sum = 0;
Lightvalve 50:3c630b5eba9f 1980 // CUR_PRES_A_mean = 0;
Lightvalve 50:3c630b5eba9f 1981 // CUR_PRES_B_mean = 0;
Lightvalve 50:3c630b5eba9f 1982 //
Lightvalve 50:3c630b5eba9f 1983 // ROM_RESET_DATA();
Lightvalve 50:3c630b5eba9f 1984 //
Lightvalve 50:3c630b5eba9f 1985 // //spi_eeprom_write(RID_PRES_SENSOR_A_PULSE_PER_BAR, (int16_t) (PRES_SENSOR_A_PULSE_PER_BAR * 100.0f));
Lightvalve 50:3c630b5eba9f 1986 // //spi_eeprom_write(RID_PRES_SENSOR_B_PULSE_PER_BAR, (int16_t) (PRES_SENSOR_B_PULSE_PER_BAR * 100.0f));
Lightvalve 50:3c630b5eba9f 1987 // }
Lightvalve 50:3c630b5eba9f 1988 // TMR3_COUNT_PRES_CALIB++;
Lightvalve 50:3c630b5eba9f 1989 // break;
Lightvalve 50:3c630b5eba9f 1990 // }
Lightvalve 19:23b7c1ad8683 1991
Lightvalve 50:3c630b5eba9f 1992 // case MODE_ROTARY_FRICTION_TUNING: {
Lightvalve 50:3c630b5eba9f 1993 // if (TMR3_COUNT_ROTARY_FRIC_TUNE % (5 * TMR_FREQ_5k) == 0) freq_fric_tune = 4.0f + 3.0f * sin(2 * 3.14159f * 0.5f * TMR3_COUNT_ROTARY_FRIC_TUNE * 0.0001f * 0.05f);
Lightvalve 50:3c630b5eba9f 1994 // V_out = PWM_out * sin(2 * 3.14159f * freq_fric_tune * TMR3_COUNT_ROTARY_FRIC_TUNE * 0.0001f);
Lightvalve 50:3c630b5eba9f 1995 // if (V_out > 0) V_out = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1996 // else V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1997 // TMR3_COUNT_ROTARY_FRIC_TUNE++;
Lightvalve 50:3c630b5eba9f 1998 // if (TMR3_COUNT_ROTARY_FRIC_TUNE > TUNING_TIME * TMR_FREQ_5k) {
Lightvalve 50:3c630b5eba9f 1999 // TMR3_COUNT_ROTARY_FRIC_TUNE = 0;
Lightvalve 50:3c630b5eba9f 2000 // V_out = 0.0f;
Lightvalve 50:3c630b5eba9f 2001 // CONTROL_MODE = MODE_NO_ACT;
Lightvalve 50:3c630b5eba9f 2002 // }
Lightvalve 50:3c630b5eba9f 2003 // break;
Lightvalve 50:3c630b5eba9f 2004 // }
Lightvalve 14:8e7590227d22 2005
Lightvalve 14:8e7590227d22 2006 case MODE_DDV_POS_VS_PWM_ID: {
Lightvalve 59:f308b1656d9c 2007 CONTROL_MODE = MODE_VALVE_OPEN_LOOP;
Lightvalve 13:747daba9cf59 2008 VALVE_ID_timer = VALVE_ID_timer + 1;
Lightvalve 14:8e7590227d22 2009
Lightvalve 14:8e7590227d22 2010 if(VALVE_ID_timer < TMR_FREQ_5k*1) {
Lightvalve 60:64181f1d3e60 2011 Vout.ref = 3000.0f * sin(2.0f*3.14f*VALVE_ID_timer/TMR_FREQ_5k * 100.0f);
Lightvalve 14:8e7590227d22 2012 } else if(VALVE_ID_timer < TMR_FREQ_5k*2) {
Lightvalve 59:f308b1656d9c 2013 Vout.ref = 1000.0f*(ID_index_array[ID_index]);
Lightvalve 14:8e7590227d22 2014 } else if(VALVE_ID_timer == TMR_FREQ_5k*2) {
Lightvalve 13:747daba9cf59 2015 VALVE_POS_TMP = 0;
Lightvalve 13:747daba9cf59 2016 data_num = 0;
Lightvalve 14:8e7590227d22 2017 } else if(VALVE_ID_timer < TMR_FREQ_5k*3) {
Lightvalve 13:747daba9cf59 2018 data_num = data_num + 1;
Lightvalve 13:747daba9cf59 2019 VALVE_POS_TMP = VALVE_POS_TMP + value;
Lightvalve 14:8e7590227d22 2020 } else if(VALVE_ID_timer == TMR_FREQ_5k*3) {
Lightvalve 59:f308b1656d9c 2021 Vout.ref = 0.0f;
Lightvalve 59:f308b1656d9c 2022 } else {
Lightvalve 13:747daba9cf59 2023 VALVE_POS_AVG[ID_index] = VALVE_POS_TMP / data_num;
Lightvalve 13:747daba9cf59 2024 VALVE_ID_timer = 0;
Lightvalve 13:747daba9cf59 2025 ID_index= ID_index +1;
Lightvalve 13:747daba9cf59 2026 }
Lightvalve 14:8e7590227d22 2027
Lightvalve 17:1865016ca2e7 2028 if(ID_index>=25) {
Lightvalve 13:747daba9cf59 2029 int i;
Lightvalve 13:747daba9cf59 2030 VALVE_POS_AVG_OLD = VALVE_POS_AVG[0];
Lightvalve 17:1865016ca2e7 2031 for(i=0; i<25; i++) {
Lightvalve 13:747daba9cf59 2032 VALVE_POS_VS_PWM[i] = (int16_t) (VALVE_POS_AVG[i]);
Lightvalve 14:8e7590227d22 2033 if(VALVE_POS_AVG[i] > VALVE_POS_AVG_OLD) {
Lightvalve 13:747daba9cf59 2034 VALVE_MAX_POS = VALVE_POS_AVG[i];
Lightvalve 13:747daba9cf59 2035 VALVE_POS_AVG_OLD = VALVE_MAX_POS;
Lightvalve 14:8e7590227d22 2036 } else if(VALVE_POS_AVG[i] < VALVE_POS_AVG_OLD) {
Lightvalve 13:747daba9cf59 2037 VALVE_MIN_POS = VALVE_POS_AVG[i];
Lightvalve 13:747daba9cf59 2038 VALVE_POS_AVG_OLD = VALVE_MIN_POS;
Lightvalve 13:747daba9cf59 2039 }
Lightvalve 13:747daba9cf59 2040 }
Lightvalve 170:42c938a40313 2041 // ROM_RESET_DATA();
Lightvalve 170:42c938a40313 2042 spi_eeprom_write(RID_VALVE_MAX_POS, (int16_t) VALVE_MAX_POS);
Lightvalve 170:42c938a40313 2043 spi_eeprom_write(RID_VALVE_MIN_POS, (int16_t) VALVE_MIN_POS);
Lightvalve 170:42c938a40313 2044 for(int i=0; i<25; i++) {
Lightvalve 170:42c938a40313 2045 spi_eeprom_write(RID_VALVE_POS_VS_PWM_0 + i, (int16_t) VALVE_POS_VS_PWM[i]);
Lightvalve 170:42c938a40313 2046 }
Lightvalve 13:747daba9cf59 2047 ID_index = 0;
Lightvalve 57:f4819de54e7a 2048 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 13:747daba9cf59 2049 }
Lightvalve 14:8e7590227d22 2050
Lightvalve 14:8e7590227d22 2051
Lightvalve 13:747daba9cf59 2052 break;
Lightvalve 13:747daba9cf59 2053 }
Lightvalve 14:8e7590227d22 2054
Lightvalve 14:8e7590227d22 2055 case MODE_DDV_DEADZONE_AND_CENTER: {
Lightvalve 59:f308b1656d9c 2056 CONTROL_MODE = MODE_VALVE_OPEN_LOOP;
Lightvalve 13:747daba9cf59 2057 VALVE_DZ_timer = VALVE_DZ_timer + 1;
Lightvalve 14:8e7590227d22 2058 if(first_check == 0) {
Lightvalve 30:8d561f16383b 2059 if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 2060 Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 30:8d561f16383b 2061 } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 2062 Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 14:8e7590227d22 2063 pos_plus_end = pos.sen;
Lightvalve 30:8d561f16383b 2064 } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 2065 Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 30:8d561f16383b 2066 } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 2067 Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 14:8e7590227d22 2068 pos_minus_end = pos.sen;
Lightvalve 30:8d561f16383b 2069 } else if(VALVE_DZ_timer < (int) (3.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 2070 Vout.ref = (float) P_GAIN_JOINT_POSITION * (0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen)/(float) ENC_PULSE_PER_POSITION;
Lightvalve 30:8d561f16383b 2071 } else if(VALVE_DZ_timer < (int) (4.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 2072 Vout.ref = (float) P_GAIN_JOINT_POSITION * (0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen)/(float) ENC_PULSE_PER_POSITION;
Lightvalve 13:747daba9cf59 2073 data_num = data_num + 1;
Lightvalve 13:747daba9cf59 2074 VALVE_POS_TMP = VALVE_POS_TMP + value;
Lightvalve 30:8d561f16383b 2075 } else if(VALVE_DZ_timer == (int) (4.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 2076 Vout.ref = (float) P_GAIN_JOINT_POSITION * (0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen)/(float) ENC_PULSE_PER_POSITION;
Lightvalve 13:747daba9cf59 2077 DDV_POS_AVG = VALVE_POS_TMP / data_num;
Lightvalve 14:8e7590227d22 2078 START_POS = pos.sen;
Lightvalve 13:747daba9cf59 2079 VALVE_POS_TMP = 0;
Lightvalve 13:747daba9cf59 2080 data_num = 0;
Lightvalve 14:8e7590227d22 2081
Lightvalve 30:8d561f16383b 2082 } else if(VALVE_DZ_timer < (int) (5.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2083 valve_pos.ref = DDV_POS_AVG;
Lightvalve 14:8e7590227d22 2084 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 14:8e7590227d22 2085
Lightvalve 30:8d561f16383b 2086 } else if(VALVE_DZ_timer < (int) (6.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2087 valve_pos.ref = DDV_POS_AVG;
Lightvalve 14:8e7590227d22 2088 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 60:64181f1d3e60 2089
Lightvalve 30:8d561f16383b 2090 } else if(VALVE_DZ_timer == (int) (6.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2091 valve_pos.ref = DDV_POS_AVG;
Lightvalve 14:8e7590227d22 2092 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 14:8e7590227d22 2093 FINAL_POS = pos.sen;
Lightvalve 14:8e7590227d22 2094
Lightvalve 60:64181f1d3e60 2095 if((FINAL_POS - START_POS)>200) {
Lightvalve 13:747daba9cf59 2096 DZ_case = 1;
Lightvalve 60:64181f1d3e60 2097 } else if((FINAL_POS - START_POS)<-200) {
Lightvalve 13:747daba9cf59 2098 DZ_case = -1;
Lightvalve 14:8e7590227d22 2099 } else {
Lightvalve 13:747daba9cf59 2100 DZ_case = 0;
Lightvalve 13:747daba9cf59 2101 }
Lightvalve 61:bc8c8270f0ab 2102
Lightvalve 60:64181f1d3e60 2103 CAN_TX_PRES((int16_t) (DZ_case), (int16_t) (6));
Lightvalve 50:3c630b5eba9f 2104
Lightvalve 13:747daba9cf59 2105 first_check = 1;
Lightvalve 13:747daba9cf59 2106 DZ_DIRECTION = 1;
Lightvalve 13:747daba9cf59 2107 VALVE_DZ_timer = 0;
Lightvalve 13:747daba9cf59 2108 Ref_Valve_Pos_Old = DDV_POS_AVG;
Lightvalve 13:747daba9cf59 2109 DZ_NUM = 1;
Lightvalve 13:747daba9cf59 2110 DZ_index = 1;
Lightvalve 14:8e7590227d22 2111
Lightvalve 13:747daba9cf59 2112 }
Lightvalve 19:23b7c1ad8683 2113 } else {
Lightvalve 14:8e7590227d22 2114 if((DZ_case == -1 && DZ_NUM == 1) | (DZ_case == 1 && DZ_NUM == 1)) {
Lightvalve 30:8d561f16383b 2115 if(VALVE_DZ_timer < (int) (1.0 * (float) TMR_FREQ_5k)) {
Lightvalve 60:64181f1d3e60 2116 Vout.ref = (float) P_GAIN_JOINT_POSITION * (0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen)/(float) ENC_PULSE_PER_POSITION;
Lightvalve 60:64181f1d3e60 2117 //pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end;
Lightvalve 60:64181f1d3e60 2118 //CONTROL_MODE = MODE_JOINT_CONTROL;
Lightvalve 30:8d561f16383b 2119 } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2120 START_POS = pos.sen;
Lightvalve 30:8d561f16383b 2121 } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2122 valve_pos.ref = Ref_Valve_Pos_Old - DZ_case * DZ_DIRECTION * 64 / DZ_index;
Lightvalve 14:8e7590227d22 2123 if(valve_pos.ref <= VALVE_MIN_POS) {
Lightvalve 14:8e7590227d22 2124 valve_pos.ref = VALVE_MIN_POS;
Lightvalve 14:8e7590227d22 2125 } else if(valve_pos.ref >= VALVE_MAX_POS) {
Lightvalve 14:8e7590227d22 2126 valve_pos.ref = VALVE_MAX_POS;
Lightvalve 13:747daba9cf59 2127 }
Lightvalve 14:8e7590227d22 2128 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 60:64181f1d3e60 2129
Lightvalve 30:8d561f16383b 2130 } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2131 Ref_Valve_Pos_Old = valve_pos.ref;
Lightvalve 14:8e7590227d22 2132 FINAL_POS = pos.sen;
Lightvalve 14:8e7590227d22 2133
Lightvalve 14:8e7590227d22 2134 if((FINAL_POS - START_POS)>100) {
Lightvalve 14:8e7590227d22 2135 DZ_DIRECTION = 1 * DZ_case;
Lightvalve 14:8e7590227d22 2136 } else if((FINAL_POS - START_POS)<-100) {
Lightvalve 14:8e7590227d22 2137 DZ_DIRECTION = -1 * DZ_case;
Lightvalve 14:8e7590227d22 2138 } else {
Lightvalve 13:747daba9cf59 2139 DZ_DIRECTION = 1 * DZ_case;
Lightvalve 13:747daba9cf59 2140 }
Lightvalve 14:8e7590227d22 2141
Lightvalve 13:747daba9cf59 2142 VALVE_DZ_timer = 0;
Lightvalve 13:747daba9cf59 2143 DZ_index= DZ_index *2;
Lightvalve 14:8e7590227d22 2144 if(DZ_index >= 128) {
Lightvalve 14:8e7590227d22 2145 FIRST_DZ = valve_pos.ref;
Lightvalve 13:747daba9cf59 2146 DZ_NUM = 2;
Lightvalve 13:747daba9cf59 2147 Ref_Valve_Pos_Old = FIRST_DZ;
Lightvalve 13:747daba9cf59 2148 DZ_index = 1;
Lightvalve 13:747daba9cf59 2149 DZ_DIRECTION = 1;
Lightvalve 13:747daba9cf59 2150 }
Lightvalve 13:747daba9cf59 2151 }
Lightvalve 14:8e7590227d22 2152 } else if((DZ_case == -1 && DZ_NUM == 2) | (DZ_case == 1 && DZ_NUM == 2)) {
Lightvalve 30:8d561f16383b 2153 if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 60:64181f1d3e60 2154 Vout.ref = (float) P_GAIN_JOINT_POSITION * (0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen)/(float) ENC_PULSE_PER_POSITION;
Lightvalve 60:64181f1d3e60 2155 //pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end;
Lightvalve 60:64181f1d3e60 2156 //CONTROL_MODE = MODE_JOINT_CONTROL;
Lightvalve 30:8d561f16383b 2157 } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2158 START_POS = pos.sen;
Lightvalve 30:8d561f16383b 2159 } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 60:64181f1d3e60 2160 valve_pos.ref = Ref_Valve_Pos_Old - DZ_case * DZ_DIRECTION * 64 / DZ_index;
Lightvalve 14:8e7590227d22 2161 if(valve_pos.ref <= VALVE_MIN_POS) {
Lightvalve 14:8e7590227d22 2162 valve_pos.ref = VALVE_MIN_POS;
Lightvalve 14:8e7590227d22 2163 } else if(valve_pos.ref >= VALVE_MAX_POS) {
Lightvalve 14:8e7590227d22 2164 valve_pos.ref = VALVE_MAX_POS;
Lightvalve 13:747daba9cf59 2165 }
Lightvalve 14:8e7590227d22 2166 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 61:bc8c8270f0ab 2167
Lightvalve 30:8d561f16383b 2168 } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 2169 Vout.ref = 0.0f;
Lightvalve 59:f308b1656d9c 2170 } else if(VALVE_DZ_timer > (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2171 Ref_Valve_Pos_Old = valve_pos.ref;
Lightvalve 14:8e7590227d22 2172 FINAL_POS = pos.sen;
Lightvalve 14:8e7590227d22 2173
Lightvalve 14:8e7590227d22 2174 if((FINAL_POS - START_POS)>100) {
Lightvalve 60:64181f1d3e60 2175 DZ_DIRECTION = 1 * DZ_case;
Lightvalve 14:8e7590227d22 2176 } else if((FINAL_POS - START_POS)<-100) {
Lightvalve 60:64181f1d3e60 2177 DZ_DIRECTION = -1 * DZ_case;
Lightvalve 14:8e7590227d22 2178 } else {
Lightvalve 60:64181f1d3e60 2179 DZ_DIRECTION = -1 * DZ_case;
Lightvalve 13:747daba9cf59 2180 }
Lightvalve 14:8e7590227d22 2181
Lightvalve 13:747daba9cf59 2182 VALVE_DZ_timer = 0;
Lightvalve 60:64181f1d3e60 2183 DZ_index= DZ_index * 2;
Lightvalve 14:8e7590227d22 2184 if(DZ_index >= 128) {
Lightvalve 14:8e7590227d22 2185 SECOND_DZ = valve_pos.ref;
Lightvalve 57:f4819de54e7a 2186 VALVE_CENTER = (int) (0.5f * (float) (FIRST_DZ) + 0.5f * (float) (SECOND_DZ));
Lightvalve 13:747daba9cf59 2187 first_check = 0;
Lightvalve 33:91b17819ec30 2188 VALVE_DEADZONE_MINUS = (float) FIRST_DZ;
Lightvalve 33:91b17819ec30 2189 VALVE_DEADZONE_PLUS = (float) SECOND_DZ;
Lightvalve 19:23b7c1ad8683 2190
Lightvalve 170:42c938a40313 2191 // ROM_RESET_DATA();
Lightvalve 170:42c938a40313 2192 spi_eeprom_write(RID_VALVE_CNETER, (int16_t) VALVE_CENTER);
Lightvalve 170:42c938a40313 2193 spi_eeprom_write(RID_VALVE_MAX_POS, (int16_t) VALVE_MAX_POS);
Lightvalve 170:42c938a40313 2194 spi_eeprom_write(RID_VALVE_MIN_POS, (int16_t) VALVE_MIN_POS);
Lightvalve 34:bb2ca2fc2a8e 2195
Lightvalve 60:64181f1d3e60 2196 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 13:747daba9cf59 2197 DZ_index = 1;
Lightvalve 13:747daba9cf59 2198 }
Lightvalve 13:747daba9cf59 2199 }
Lightvalve 14:8e7590227d22 2200 } else if(DZ_case == 0 && DZ_NUM ==1) {
Lightvalve 30:8d561f16383b 2201 if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 60:64181f1d3e60 2202 Vout.ref = (float) P_GAIN_JOINT_POSITION * (0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen)/(float) ENC_PULSE_PER_POSITION;
Lightvalve 60:64181f1d3e60 2203 //pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end;
Lightvalve 60:64181f1d3e60 2204 //CONTROL_MODE = MODE_JOINT_CONTROL;
Lightvalve 30:8d561f16383b 2205 } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2206 START_POS = pos.sen;
Lightvalve 30:8d561f16383b 2207 } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2208 valve_pos.ref = Ref_Valve_Pos_Old - DZ_DIRECTION * 64 / DZ_index;
Lightvalve 14:8e7590227d22 2209 if(valve_pos.ref <= VALVE_MIN_POS) {
Lightvalve 14:8e7590227d22 2210 valve_pos.ref = VALVE_MIN_POS;
Lightvalve 14:8e7590227d22 2211 } else if(valve_pos.ref >= VALVE_MAX_POS) {
Lightvalve 14:8e7590227d22 2212 valve_pos.ref = VALVE_MAX_POS;
Lightvalve 13:747daba9cf59 2213 }
Lightvalve 14:8e7590227d22 2214 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 61:bc8c8270f0ab 2215
Lightvalve 30:8d561f16383b 2216 } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2217 Ref_Valve_Pos_Old = valve_pos.ref;
Lightvalve 14:8e7590227d22 2218 FINAL_POS = pos.sen;
Lightvalve 14:8e7590227d22 2219
Lightvalve 14:8e7590227d22 2220 if((FINAL_POS - START_POS)>100) {
Lightvalve 14:8e7590227d22 2221 DZ_DIRECTION = 1;
Lightvalve 14:8e7590227d22 2222 } else if((FINAL_POS - START_POS)<-100) {
Lightvalve 14:8e7590227d22 2223 DZ_DIRECTION = -1;
Lightvalve 14:8e7590227d22 2224 } else {
Lightvalve 13:747daba9cf59 2225 DZ_DIRECTION = 1;
Lightvalve 13:747daba9cf59 2226 }
Lightvalve 13:747daba9cf59 2227 VALVE_DZ_timer = 0;
Lightvalve 13:747daba9cf59 2228 DZ_index= DZ_index *2;
Lightvalve 14:8e7590227d22 2229 if(DZ_index >= 128) {
Lightvalve 14:8e7590227d22 2230 FIRST_DZ = valve_pos.ref;
Lightvalve 13:747daba9cf59 2231 DZ_NUM = 2;
Lightvalve 13:747daba9cf59 2232 Ref_Valve_Pos_Old = FIRST_DZ;
Lightvalve 13:747daba9cf59 2233 DZ_index = 1;
Lightvalve 13:747daba9cf59 2234 DZ_DIRECTION = 1;
Lightvalve 13:747daba9cf59 2235 }
Lightvalve 13:747daba9cf59 2236 }
Lightvalve 14:8e7590227d22 2237 } else {
Lightvalve 30:8d561f16383b 2238 if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 60:64181f1d3e60 2239 Vout.ref = (float) P_GAIN_JOINT_POSITION * (0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen)/(float) ENC_PULSE_PER_POSITION;
Lightvalve 60:64181f1d3e60 2240 //pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end;
Lightvalve 60:64181f1d3e60 2241 //CONTROL_MODE = MODE_JOINT_CONTROL;
Lightvalve 30:8d561f16383b 2242 } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2243 START_POS = pos.sen;
Lightvalve 30:8d561f16383b 2244 } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2245 valve_pos.ref = Ref_Valve_Pos_Old + DZ_DIRECTION * 64 / DZ_index;
Lightvalve 14:8e7590227d22 2246 if(valve_pos.ref <= VALVE_MIN_POS) {
Lightvalve 14:8e7590227d22 2247 valve_pos.ref = VALVE_MIN_POS;
Lightvalve 14:8e7590227d22 2248 } else if(valve_pos.ref > VALVE_MAX_POS) {
Lightvalve 14:8e7590227d22 2249 valve_pos.ref = VALVE_MAX_POS - 1;
Lightvalve 13:747daba9cf59 2250 }
Lightvalve 14:8e7590227d22 2251 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 60:64181f1d3e60 2252
Lightvalve 30:8d561f16383b 2253 } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 2254 Vout.ref = 0.0f;
Lightvalve 59:f308b1656d9c 2255 } else if(VALVE_DZ_timer > (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2256 Ref_Valve_Pos_Old = valve_pos.ref;
Lightvalve 14:8e7590227d22 2257 FINAL_POS = pos.sen;
Lightvalve 61:bc8c8270f0ab 2258
Lightvalve 60:64181f1d3e60 2259 if((FINAL_POS - START_POS)>100) {
Lightvalve 13:747daba9cf59 2260 DZ_DIRECTION = 1;
Lightvalve 60:64181f1d3e60 2261 } else if((FINAL_POS - START_POS)<-100) {
Lightvalve 60:64181f1d3e60 2262 DZ_DIRECTION = -1;
Lightvalve 60:64181f1d3e60 2263 } else {
Lightvalve 13:747daba9cf59 2264 DZ_DIRECTION = 1;
Lightvalve 60:64181f1d3e60 2265 }
Lightvalve 14:8e7590227d22 2266
Lightvalve 13:747daba9cf59 2267 VALVE_DZ_timer = 0;
Lightvalve 13:747daba9cf59 2268 DZ_index= DZ_index *2;
Lightvalve 14:8e7590227d22 2269 if(DZ_index >= 128) {
Lightvalve 14:8e7590227d22 2270 SECOND_DZ = valve_pos.ref;
Lightvalve 57:f4819de54e7a 2271 VALVE_CENTER = (int) (0.5f * (float) (FIRST_DZ) + 0.5f * (float) (SECOND_DZ));
Lightvalve 13:747daba9cf59 2272 first_check = 0;
Lightvalve 33:91b17819ec30 2273 VALVE_DEADZONE_MINUS = (float) FIRST_DZ;
Lightvalve 33:91b17819ec30 2274 VALVE_DEADZONE_PLUS = (float) SECOND_DZ;
Lightvalve 61:bc8c8270f0ab 2275
Lightvalve 170:42c938a40313 2276 // ROM_RESET_DATA();
Lightvalve 170:42c938a40313 2277 spi_eeprom_write(RID_VALVE_CNETER, (int16_t) VALVE_CENTER);
Lightvalve 170:42c938a40313 2278 spi_eeprom_write(RID_VALVE_MAX_POS, (int16_t) VALVE_MAX_POS);
Lightvalve 170:42c938a40313 2279 spi_eeprom_write(RID_VALVE_MIN_POS, (int16_t) VALVE_MIN_POS);
Lightvalve 34:bb2ca2fc2a8e 2280
Lightvalve 57:f4819de54e7a 2281 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 13:747daba9cf59 2282 DZ_index = 1;
Lightvalve 13:747daba9cf59 2283 }
Lightvalve 13:747daba9cf59 2284 }
Lightvalve 13:747daba9cf59 2285 }
Lightvalve 14:8e7590227d22 2286 }
Lightvalve 13:747daba9cf59 2287 break;
Lightvalve 13:747daba9cf59 2288 }
Lightvalve 14:8e7590227d22 2289
Lightvalve 14:8e7590227d22 2290 case MODE_DDV_POS_VS_FLOWRATE: {
Lightvalve 59:f308b1656d9c 2291 CONTROL_MODE = MODE_VALVE_OPEN_LOOP;
Lightvalve 13:747daba9cf59 2292 VALVE_FR_timer = VALVE_FR_timer + 1;
Lightvalve 14:8e7590227d22 2293 if(first_check == 0) {
Lightvalve 30:8d561f16383b 2294 if(VALVE_FR_timer < (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 2295 Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 23:59218d4a256d 2296 //CAN_TX_PRES((int16_t) (VALVE_FR_timer), (int16_t) (6));
Lightvalve 30:8d561f16383b 2297 } else if(VALVE_FR_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 2298 Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 14:8e7590227d22 2299 pos_plus_end = pos.sen;
Lightvalve 14:8e7590227d22 2300 // CAN_TX_PRES((int16_t) (V_out), (int16_t) (7));
Lightvalve 30:8d561f16383b 2301 } else if(VALVE_FR_timer < (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 2302 Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 30:8d561f16383b 2303 } else if(VALVE_FR_timer == (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2304 // CAN_TX_PRES((int16_t) (V_out), (int16_t) (8));
Lightvalve 59:f308b1656d9c 2305 Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 14:8e7590227d22 2306 pos_minus_end = pos.sen;
Lightvalve 13:747daba9cf59 2307 first_check = 1;
Lightvalve 13:747daba9cf59 2308 VALVE_FR_timer = 0;
Lightvalve 57:f4819de54e7a 2309 valve_pos.ref = (float) VALVE_CENTER;
Lightvalve 13:747daba9cf59 2310 ID_index = 0;
Lightvalve 13:747daba9cf59 2311 max_check = 0;
Lightvalve 13:747daba9cf59 2312 min_check = 0;
Lightvalve 13:747daba9cf59 2313 }
Lightvalve 14:8e7590227d22 2314 } else {
Lightvalve 30:8d561f16383b 2315 if(VALVE_FR_timer < (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 59:f308b1656d9c 2316 //V_out = (float) P_GAIN_JOINT_POSITION * (0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen)/(float) ENC_PULSE_PER_POSITION;
Lightvalve 59:f308b1656d9c 2317 pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end;
Lightvalve 59:f308b1656d9c 2318 CONTROL_MODE = MODE_JOINT_CONTROL;
Lightvalve 30:8d561f16383b 2319 } else if(VALVE_FR_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 13:747daba9cf59 2320 data_num = 0;
Lightvalve 57:f4819de54e7a 2321 valve_pos.ref = 10.0f*((float) ID_index_array[ID_index]) + (float) VALVE_CENTER;
Lightvalve 14:8e7590227d22 2322
Lightvalve 14:8e7590227d22 2323 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 14:8e7590227d22 2324 START_POS = pos.sen;
Lightvalve 30:8d561f16383b 2325 } else if(VALVE_FR_timer < (int) (5.0f * (float) TMR_FREQ_5k)) {
Lightvalve 57:f4819de54e7a 2326 valve_pos.ref = 10.0f*((float) ID_index_array[ID_index]) + (float) VALVE_CENTER;
Lightvalve 14:8e7590227d22 2327 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 13:747daba9cf59 2328 data_num = data_num + 1;
Lightvalve 30:8d561f16383b 2329 if(abs(0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen) > 20000.0f) {
Lightvalve 14:8e7590227d22 2330 FINAL_POS = pos.sen;
Lightvalve 14:8e7590227d22 2331 one_period_end = 1;
Lightvalve 13:747daba9cf59 2332 }
Lightvalve 30:8d561f16383b 2333 } else if(VALVE_FR_timer == (int) (5.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 2334 FINAL_POS = pos.sen;
Lightvalve 13:747daba9cf59 2335 one_period_end = 1;
Lightvalve 59:f308b1656d9c 2336 V_out = 0.0f;
Lightvalve 13:747daba9cf59 2337 }
Lightvalve 14:8e7590227d22 2338
Lightvalve 14:8e7590227d22 2339 if(one_period_end == 1) {
Lightvalve 14:8e7590227d22 2340 if(valve_pos.ref > VALVE_MAX_POS) {
Lightvalve 13:747daba9cf59 2341 max_check = 1;
Lightvalve 14:8e7590227d22 2342 } else if(valve_pos.ref < VALVE_MIN_POS) {
Lightvalve 13:747daba9cf59 2343 min_check = 1;
Lightvalve 13:747daba9cf59 2344 }
Lightvalve 13:747daba9cf59 2345 JOINT_VEL[ID_index] = (FINAL_POS - START_POS) / data_num * TMR_FREQ_5k; // pulse/sec
Lightvalve 19:23b7c1ad8683 2346
Lightvalve 13:747daba9cf59 2347 VALVE_FR_timer = 0;
Lightvalve 13:747daba9cf59 2348 one_period_end = 0;
Lightvalve 13:747daba9cf59 2349 ID_index= ID_index +1;
Lightvalve 30:8d561f16383b 2350 V_out = 0.0f;
Lightvalve 13:747daba9cf59 2351 }
Lightvalve 14:8e7590227d22 2352
Lightvalve 14:8e7590227d22 2353 if(max_check == 1 && min_check == 1) {
Lightvalve 19:23b7c1ad8683 2354
Lightvalve 13:747daba9cf59 2355 VALVE_POS_NUM = ID_index;
Lightvalve 170:42c938a40313 2356 // ROM_RESET_DATA();
Lightvalve 170:42c938a40313 2357 for(int i=0; i<100; i++) {
Lightvalve 170:42c938a40313 2358 spi_eeprom_write(RID_VALVE_POS_VS_FLOWRATE_0 + i, (int16_t) (JOINT_VEL[i] & 0xFFFF));
Lightvalve 170:42c938a40313 2359 spi_eeprom_write(RID_VALVE_POS_VS_FLOWRATE_0_1 + i, (int16_t) ((JOINT_VEL[i] >> 16) & 0xFFFF));
Lightvalve 170:42c938a40313 2360 }
Lightvalve 13:747daba9cf59 2361 ID_index = 0;
Lightvalve 13:747daba9cf59 2362 first_check = 0;
Lightvalve 13:747daba9cf59 2363 VALVE_FR_timer = 0;
Lightvalve 57:f4819de54e7a 2364 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 49:e7bcfc244d40 2365 // CAN_TX_PRES((int16_t) (VALVE_FR_timer), (int16_t) (6));
Lightvalve 13:747daba9cf59 2366 }
Lightvalve 13:747daba9cf59 2367 }
Lightvalve 13:747daba9cf59 2368 break;
Lightvalve 13:747daba9cf59 2369 }
Lightvalve 58:2eade98630e2 2370
Lightvalve 57:f4819de54e7a 2371 case MODE_SYSTEM_ID: {
Lightvalve 169:645207e160ca 2372 freq_sysid_Iref = (double) cnt_sysid * DT_TMR3 * 3.0f;
Lightvalve 57:f4819de54e7a 2373 valve_pos.ref = 2500.0f * sin(2.0f * 3.14159f * freq_sysid_Iref * (double) cnt_sysid * DT_TMR3);
Lightvalve 57:f4819de54e7a 2374 CONTROL_MODE = MODE_VALVE_OPEN_LOOP;
Lightvalve 57:f4819de54e7a 2375 cnt_sysid++;
Lightvalve 57:f4819de54e7a 2376 if (freq_sysid_Iref >= 300) {
Lightvalve 57:f4819de54e7a 2377 cnt_sysid = 0;
Lightvalve 57:f4819de54e7a 2378 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 57:f4819de54e7a 2379 }
Lightvalve 57:f4819de54e7a 2380 break;
Lightvalve 57:f4819de54e7a 2381 }
Lightvalve 58:2eade98630e2 2382
Lightvalve 169:645207e160ca 2383 case MODE_FREQ_TEST: {
Lightvalve 169:645207e160ca 2384 float valve_pos_ref = 2500.0f * sin(2.0f * 3.141592f * freq_test_valve_ref * (float) cnt_freq_test * DT_TMR3);
Lightvalve 169:645207e160ca 2385 if(valve_pos_ref >= 0) {
Lightvalve 169:645207e160ca 2386 valve_pos.ref = (double)VALVE_CENTER + (double)valve_pos_ref * ((double)VALVE_MAX_POS-(double)VALVE_CENTER)/10000.0f;
Lightvalve 169:645207e160ca 2387 } else {
Lightvalve 169:645207e160ca 2388 valve_pos.ref = (double)VALVE_CENTER - (double)valve_pos_ref * ((double)VALVE_MIN_POS-(double)VALVE_CENTER)/10000.0f;
Lightvalve 169:645207e160ca 2389 }
Lightvalve 169:645207e160ca 2390 ref_array[cnt_freq_test] = valve_pos_ref;
Lightvalve 169:645207e160ca 2391 if(value>=(float) VALVE_CENTER) {
Lightvalve 169:645207e160ca 2392 pos_array[cnt_freq_test] = 10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER);
Lightvalve 169:645207e160ca 2393 } else {
Lightvalve 169:645207e160ca 2394 pos_array[cnt_freq_test] = -10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER);
Lightvalve 169:645207e160ca 2395 }
Lightvalve 58:2eade98630e2 2396
Lightvalve 169:645207e160ca 2397 CONTROL_MODE = MODE_VALVE_POSITION_CONTROL;
Lightvalve 169:645207e160ca 2398 cnt_freq_test++;
Lightvalve 169:645207e160ca 2399 if (freq_test_valve_ref * (float) cnt_freq_test * DT_TMR3 > 2) {
Lightvalve 169:645207e160ca 2400 buffer_data_size = cnt_freq_test;
Lightvalve 169:645207e160ca 2401 cnt_freq_test = 0;
Lightvalve 169:645207e160ca 2402 cnt_send_buffer = 0;
Lightvalve 169:645207e160ca 2403 freq_test_valve_ref = freq_test_valve_ref * 1.05f;
Lightvalve 169:645207e160ca 2404 if (freq_test_valve_ref >= 400) {
Lightvalve 169:645207e160ca 2405 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 169:645207e160ca 2406 CONTROL_MODE = MODE_NO_ACT;
Lightvalve 169:645207e160ca 2407 CAN_TX_PWM((int16_t) (1)); //1300
Lightvalve 169:645207e160ca 2408 }
Lightvalve 169:645207e160ca 2409 CONTROL_MODE = MODE_NO_ACT;
Lightvalve 169:645207e160ca 2410 CONTROL_UTILITY_MODE = MODE_SEND_OVER;
Lightvalve 170:42c938a40313 2411
Lightvalve 169:645207e160ca 2412 }
Lightvalve 169:645207e160ca 2413 break;
Lightvalve 169:645207e160ca 2414 }
Lightvalve 169:645207e160ca 2415 case MODE_SEND_BUFFER: {
Lightvalve 169:645207e160ca 2416 // if (TMR2_COUNT_CAN_TX % (int) ((int) TMR_FREQ_5k/CAN_FREQ) == 0) {
Lightvalve 169:645207e160ca 2417 // CAN_TX_PRES((int16_t) (pos_array[cnt_send_buffer]), (int16_t) (ref_array[cnt_send_buffer])); // 1400
Lightvalve 169:645207e160ca 2418 // if(cnt_send_buffer>=buffer_data_size) {
Lightvalve 169:645207e160ca 2419 // CONTROL_UTILITY_MODE = MODE_FREQ_TEST;
Lightvalve 169:645207e160ca 2420 // }
Lightvalve 169:645207e160ca 2421 // cnt_send_buffer++;
Lightvalve 169:645207e160ca 2422 // }
Lightvalve 169:645207e160ca 2423
Lightvalve 169:645207e160ca 2424 break;
Lightvalve 169:645207e160ca 2425 }
Lightvalve 169:645207e160ca 2426 case MODE_SEND_OVER: {
Lightvalve 169:645207e160ca 2427 CAN_TX_TORQUE((int16_t) (buffer_data_size)); //1300
Lightvalve 169:645207e160ca 2428 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 169:645207e160ca 2429 CONTROL_MODE = MODE_NO_ACT;
Lightvalve 169:645207e160ca 2430 break;
Lightvalve 169:645207e160ca 2431 }
Lightvalve 170:42c938a40313 2432
Lightvalve 169:645207e160ca 2433 case MODE_STEP_TEST: {
Lightvalve 169:645207e160ca 2434 float valve_pos_ref = 0.0f;
Lightvalve 170:42c938a40313 2435 if (cnt_step_test < (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 169:645207e160ca 2436 valve_pos_ref = 0.0f;
Lightvalve 170:42c938a40313 2437 } else {
Lightvalve 169:645207e160ca 2438 valve_pos_ref = 10000.0f;
Lightvalve 169:645207e160ca 2439 }
Lightvalve 169:645207e160ca 2440 if(valve_pos_ref >= 0) {
Lightvalve 169:645207e160ca 2441 valve_pos.ref = (double)VALVE_CENTER + (double)valve_pos_ref * ((double)VALVE_MAX_POS-(double)VALVE_CENTER)/10000.0f;
Lightvalve 169:645207e160ca 2442 } else {
Lightvalve 169:645207e160ca 2443 valve_pos.ref = (double)VALVE_CENTER - (double)valve_pos_ref * ((double)VALVE_MIN_POS-(double)VALVE_CENTER)/10000.0f;
Lightvalve 169:645207e160ca 2444 }
Lightvalve 169:645207e160ca 2445 ref_array[cnt_step_test] = valve_pos_ref;
Lightvalve 169:645207e160ca 2446 if(value>=(float) VALVE_CENTER) {
Lightvalve 169:645207e160ca 2447 pos_array[cnt_step_test] = 10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER);
Lightvalve 169:645207e160ca 2448 } else {
Lightvalve 169:645207e160ca 2449 pos_array[cnt_step_test] = -10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER);
Lightvalve 169:645207e160ca 2450 }
Lightvalve 169:645207e160ca 2451
Lightvalve 169:645207e160ca 2452 CONTROL_MODE = MODE_VALVE_POSITION_CONTROL;
Lightvalve 169:645207e160ca 2453 cnt_step_test++;
Lightvalve 170:42c938a40313 2454 if (cnt_step_test > (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 169:645207e160ca 2455 buffer_data_size = cnt_step_test;
Lightvalve 169:645207e160ca 2456 cnt_step_test = 0;
Lightvalve 169:645207e160ca 2457 cnt_send_buffer = 0;
Lightvalve 169:645207e160ca 2458 CONTROL_UTILITY_MODE = MODE_SEND_OVER;
Lightvalve 169:645207e160ca 2459 CONTROL_MODE = MODE_NO_ACT;
Lightvalve 169:645207e160ca 2460 }
Lightvalve 169:645207e160ca 2461 // if (cnt_step_test > (int) (2.0f * (float) TMR_FREQ_5k))
Lightvalve 169:645207e160ca 2462 // {
Lightvalve 169:645207e160ca 2463 // CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 169:645207e160ca 2464 // CONTROL_MODE = MODE_NO_ACT;
Lightvalve 169:645207e160ca 2465 // CAN_TX_PWM((int16_t) (1)); //1300
Lightvalve 169:645207e160ca 2466 // }
Lightvalve 170:42c938a40313 2467
Lightvalve 169:645207e160ca 2468 break;
Lightvalve 169:645207e160ca 2469 }
Lightvalve 57:f4819de54e7a 2470
Lightvalve 57:f4819de54e7a 2471 default:
Lightvalve 57:f4819de54e7a 2472 break;
Lightvalve 57:f4819de54e7a 2473 }
Lightvalve 57:f4819de54e7a 2474
Lightvalve 57:f4819de54e7a 2475 // CONTROL MODE ------------------------------------------------------------
Lightvalve 57:f4819de54e7a 2476
Lightvalve 57:f4819de54e7a 2477 switch (CONTROL_MODE) {
Lightvalve 57:f4819de54e7a 2478 case MODE_NO_ACT: {
Lightvalve 57:f4819de54e7a 2479 V_out = 0.0f;
Lightvalve 57:f4819de54e7a 2480 break;
Lightvalve 57:f4819de54e7a 2481 }
Lightvalve 57:f4819de54e7a 2482
Lightvalve 57:f4819de54e7a 2483 case MODE_VALVE_POSITION_CONTROL: {
Lightvalve 67:c2812cf26c38 2484 if (OPERATING_MODE == 5) { //SW Valve
Lightvalve 57:f4819de54e7a 2485 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 59:f308b1656d9c 2486 V_out = Vout.ref;
Lightvalve 67:c2812cf26c38 2487 } else if (CURRENT_CONTROL_MODE == 0) { //PWM
Lightvalve 67:c2812cf26c38 2488 V_out = valve_pos.ref;
Lightvalve 169:645207e160ca 2489 } else {
Lightvalve 67:c2812cf26c38 2490 I_REF = valve_pos.ref * 0.001f;
Lightvalve 57:f4819de54e7a 2491 }
Lightvalve 57:f4819de54e7a 2492 break;
Lightvalve 57:f4819de54e7a 2493 }
Lightvalve 57:f4819de54e7a 2494
Lightvalve 57:f4819de54e7a 2495 case MODE_JOINT_CONTROL: {
Lightvalve 169:645207e160ca 2496
Lightvalve 57:f4819de54e7a 2497 double torq_ref = 0.0f;
Lightvalve 57:f4819de54e7a 2498 pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm]
Lightvalve 67:c2812cf26c38 2499 vel.err = (0.0f - vel.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm/s]
Lightvalve 57:f4819de54e7a 2500 pos.err_sum += pos.err/(float) TMR_FREQ_5k; //[mm]
Lightvalve 169:645207e160ca 2501
Lightvalve 67:c2812cf26c38 2502 //K & D Low Pass Filter
Lightvalve 67:c2812cf26c38 2503 float alpha_K_D = 1.0f/(1.0f + 5000.0f/(2.0f*3.14f*30.0f)); // f_cutoff : 30Hz
Lightvalve 67:c2812cf26c38 2504 K_LPF = K_LPF*(1.0f-alpha_K_D)+K_SPRING*(alpha_K_D);
Lightvalve 67:c2812cf26c38 2505 D_LPF = D_LPF*(1.0f-alpha_K_D)+D_DAMPER*(alpha_K_D);
Lightvalve 169:645207e160ca 2506
Lightvalve 139:15621998925b 2507 // torq_ref = torq.ref + K_LPF * pos.err - D_LPF * vel.sen / ENC_PULSE_PER_POSITION; //[N]
Lightvalve 139:15621998925b 2508 torq_ref = torq.ref;
Lightvalve 57:f4819de54e7a 2509
Lightvalve 57:f4819de54e7a 2510 // torque feedback
Lightvalve 67:c2812cf26c38 2511 torq.err = torq_ref - torq.sen; //[N]
Lightvalve 57:f4819de54e7a 2512 torq.err_sum += torq.err/(float) TMR_FREQ_5k; //[N]
Lightvalve 57:f4819de54e7a 2513
Lightvalve 57:f4819de54e7a 2514 if (((OPERATING_MODE&0b110)>>1) == 0 || ((OPERATING_MODE&0b110)>>1) == 1) {
Lightvalve 58:2eade98630e2 2515
Lightvalve 57:f4819de54e7a 2516 double I_REF_POS = 0.0f;
Lightvalve 57:f4819de54e7a 2517 double I_REF_FORCE_FB = 0.0f; // I_REF by Force Feedback
Lightvalve 57:f4819de54e7a 2518 double I_REF_VC = 0.0f; // I_REF for velocity compensation
Lightvalve 57:f4819de54e7a 2519
Lightvalve 57:f4819de54e7a 2520 double temp_vel_pos = 0.0f;
Lightvalve 57:f4819de54e7a 2521 double temp_vel_torq = 0.0f;
Lightvalve 57:f4819de54e7a 2522 double wn_Pos = 2.0f * PI * 5.0f; // f_cut : 5Hz Position Control
Lightvalve 58:2eade98630e2 2523
Lightvalve 69:3995ffeaa786 2524 if ((OPERATING_MODE & 0x01) == 0) { // Rotary Mode
Lightvalve 57:f4819de54e7a 2525 temp_vel_pos = (0.01f * (double) P_GAIN_JOINT_POSITION * wn_Pos * pos.err + 0.01f * (double) I_GAIN_JOINT_POSITION * wn_Pos * pos.err_sum + 0.01f * (double) VELOCITY_COMP_GAIN * vel.ref / ENC_PULSE_PER_POSITION) * PI / 180.0f; // rad/s
Lightvalve 57:f4819de54e7a 2526 // L when P-gain = 100, f_cut = 10Hz L feedforward velocity
Lightvalve 69:3995ffeaa786 2527 } else if ((OPERATING_MODE & 0x01) == 1) {
Lightvalve 57:f4819de54e7a 2528 temp_vel_pos = (0.01f * (double) P_GAIN_JOINT_POSITION * wn_Pos * pos.err + 0.01f * (double) I_GAIN_JOINT_POSITION * wn_Pos * pos.err_sum + 0.01f * (double) VELOCITY_COMP_GAIN * vel.ref / ENC_PULSE_PER_POSITION); // mm/s
Lightvalve 57:f4819de54e7a 2529 // L when P-gain = 100, f_cut = 10Hz L feedforward velocity
Lightvalve 57:f4819de54e7a 2530 }
Lightvalve 57:f4819de54e7a 2531 if (temp_vel_pos > 0.0f) I_REF_POS = temp_vel_pos * ((double) PISTON_AREA_A * 0.00006f / (K_v * sqrt(2.0f * alpha3 / (alpha3 + 1.0f))));
Lightvalve 57:f4819de54e7a 2532 else I_REF_POS = temp_vel_pos * ((double) PISTON_AREA_B * 0.00006f / (K_v * sqrt(2.0f / (alpha3 + 1.0f))));
Lightvalve 57:f4819de54e7a 2533
Lightvalve 57:f4819de54e7a 2534 // velocity compensation for torque control
Lightvalve 69:3995ffeaa786 2535 if ((OPERATING_MODE & 0x01) == 0) { // Rotary Mode
Lightvalve 57:f4819de54e7a 2536 I_REF_FORCE_FB = 0.001f * ((double) P_GAIN_JOINT_TORQUE * torq.err + (double) I_GAIN_JOINT_TORQUE * torq.err_sum);
Lightvalve 57:f4819de54e7a 2537 // temp_vel_torq = (0.01 * (double) VELOCITY_COMP_GAIN * (double) CUR_VELOCITY / (double) ENC_PULSE_PER_POSITION) * PI / 180.0; // rad/s
Lightvalve 57:f4819de54e7a 2538 temp_vel_torq = (0.01f * (double) VELOCITY_COMP_GAIN * vel.ref / (double) ENC_PULSE_PER_POSITION) * PI / 180.0f; // rad/s
Lightvalve 57:f4819de54e7a 2539 // L feedforward velocity
Lightvalve 69:3995ffeaa786 2540 } else if ((OPERATING_MODE & 0x01) == 1) {
Lightvalve 57:f4819de54e7a 2541 I_REF_FORCE_FB = 0.001f * 0.01f*((double) P_GAIN_JOINT_TORQUE * torq.err + (double) I_GAIN_JOINT_TORQUE * torq.err_sum); // Linear Actuators are more sensitive.
Lightvalve 57:f4819de54e7a 2542 // temp_vel_torq = (0.01 * (double) VELOCITY_COMP_GAIN * (double) CUR_VELOCITY / (double) ENC_PULSE_PER_POSITION); // mm/s
Lightvalve 57:f4819de54e7a 2543 temp_vel_torq = (0.01f * (double) VELOCITY_COMP_GAIN * vel.ref / (double) ENC_PULSE_PER_POSITION); // mm/s
Lightvalve 57:f4819de54e7a 2544 // L feedforward velocity
Lightvalve 57:f4819de54e7a 2545 }
Lightvalve 57:f4819de54e7a 2546 if (temp_vel_torq > 0.0f) I_REF_VC = temp_vel_torq * ((double) PISTON_AREA_A * 0.00006f / (K_v * sqrt(2.0f * alpha3 / (alpha3 + 1.0f))));
Lightvalve 57:f4819de54e7a 2547 else I_REF_VC = temp_vel_torq * ((double) PISTON_AREA_B * 0.00006f / (K_v * sqrt(2.0f / (alpha3 + 1.0f))));
Lightvalve 57:f4819de54e7a 2548 // L velocity(rad/s or mm/s) >> I_ref(mA)
Lightvalve 57:f4819de54e7a 2549 // Ref_Joint_FT_dot = (Ref_Joint_FT_Nm - Ref_Joint_FT_Nm_old) / TMR_DT_5k;
Lightvalve 57:f4819de54e7a 2550 // Ref_Joint_FT_Nm_old = Ref_Joint_FT_Nm;
Lightvalve 57:f4819de54e7a 2551
Lightvalve 57:f4819de54e7a 2552 I_REF = (1.0f - alpha_trans) * I_REF_POS + alpha_trans * (I_REF_VC + I_REF_FORCE_FB);
Lightvalve 169:645207e160ca 2553
Lightvalve 67:c2812cf26c38 2554 // Anti-windup for FT
Lightvalve 67:c2812cf26c38 2555 if (I_GAIN_JOINT_TORQUE != 0) {
Lightvalve 67:c2812cf26c38 2556 double I_MAX = 10.0f; // Maximum Current : 10mV
Lightvalve 67:c2812cf26c38 2557 double Ka = 2.0f / ((double) I_GAIN_JOINT_TORQUE * 0.001f);
Lightvalve 67:c2812cf26c38 2558 if (I_REF > I_MAX) {
Lightvalve 67:c2812cf26c38 2559 double I_rem = I_REF - I_MAX;
Lightvalve 67:c2812cf26c38 2560 I_rem = Ka*I_rem;
Lightvalve 67:c2812cf26c38 2561 I_REF = I_MAX;
Lightvalve 67:c2812cf26c38 2562 torq.err_sum = torq.err_sum - I_rem /(float) TMR_FREQ_5k;
Lightvalve 67:c2812cf26c38 2563 } else if (I_REF < -I_MAX) {
Lightvalve 67:c2812cf26c38 2564 double I_rem = I_REF - (-I_MAX);
Lightvalve 67:c2812cf26c38 2565 I_rem = Ka*I_rem;
Lightvalve 67:c2812cf26c38 2566 I_REF = -I_MAX;
Lightvalve 67:c2812cf26c38 2567 torq.err_sum = torq.err_sum - I_rem /(float) TMR_FREQ_5k;
Lightvalve 67:c2812cf26c38 2568 }
Lightvalve 67:c2812cf26c38 2569 }
Lightvalve 57:f4819de54e7a 2570
Lightvalve 57:f4819de54e7a 2571 } else {
Lightvalve 57:f4819de54e7a 2572 float VALVE_POS_RAW_FORCE_FB = 0.0f;
Lightvalve 72:3436ce769b1e 2573 float VALVE_POS_RAW_FORCE_FF = 0.0f;
Lightvalve 72:3436ce769b1e 2574 float VALVE_POS_RAW = 0.0f;
Lightvalve 169:645207e160ca 2575
Lightvalve 57:f4819de54e7a 2576 VALVE_POS_RAW_FORCE_FB = alpha_trans*(((float) P_GAIN_JOINT_TORQUE * torq.err + (float) I_GAIN_JOINT_TORQUE * torq.err_sum) * 0.01f
Lightvalve 57:f4819de54e7a 2577 + DDV_JOINT_POS_FF(vel.sen))+ (1.0f-alpha_trans) * (P_GAIN_JOINT_POSITION * 0.01f * pos.err + DDV_JOINT_POS_FF(vel.ref));
Lightvalve 57:f4819de54e7a 2578
Lightvalve 72:3436ce769b1e 2579 VALVE_POS_RAW_FORCE_FF = P_GAIN_JOINT_TORQUE_FF * torq_ref * 0.001f + D_GAIN_JOINT_TORQUE_FF * (torq_ref - torq_ref_past) * 0.0001f;
Lightvalve 169:645207e160ca 2580
Lightvalve 72:3436ce769b1e 2581 VALVE_POS_RAW = VALVE_POS_RAW_FORCE_FB + VALVE_POS_RAW_FORCE_FF;
Lightvalve 169:645207e160ca 2582
Lightvalve 169:645207e160ca 2583
Lightvalve 72:3436ce769b1e 2584 if (VALVE_POS_RAW >= 0) {
Lightvalve 72:3436ce769b1e 2585 valve_pos.ref = VALVE_POS_RAW + VALVE_DEADZONE_PLUS;
Lightvalve 57:f4819de54e7a 2586 } else {
Lightvalve 72:3436ce769b1e 2587 valve_pos.ref = VALVE_POS_RAW + VALVE_DEADZONE_MINUS;
Lightvalve 57:f4819de54e7a 2588 }
Lightvalve 57:f4819de54e7a 2589
Lightvalve 57:f4819de54e7a 2590 if(I_GAIN_JOINT_TORQUE != 0) {
Lightvalve 139:15621998925b 2591 double Ka = 2.0f / (double) I_GAIN_JOINT_TORQUE * 100.0f;
Lightvalve 57:f4819de54e7a 2592 if(valve_pos.ref>VALVE_MAX_POS) {
Lightvalve 57:f4819de54e7a 2593 double valve_pos_rem = valve_pos.ref - VALVE_MAX_POS;
Lightvalve 57:f4819de54e7a 2594 valve_pos_rem = valve_pos_rem * Ka;
Lightvalve 57:f4819de54e7a 2595 valve_pos.ref = VALVE_MAX_POS;
Lightvalve 57:f4819de54e7a 2596 torq.err_sum = torq.err_sum - valve_pos_rem/(float) TMR_FREQ_5k;
Lightvalve 57:f4819de54e7a 2597 } else if(valve_pos.ref < VALVE_MIN_POS) {
Lightvalve 57:f4819de54e7a 2598 double valve_pos_rem = valve_pos.ref - VALVE_MIN_POS;
Lightvalve 57:f4819de54e7a 2599 valve_pos_rem = valve_pos_rem * Ka;
Lightvalve 57:f4819de54e7a 2600 valve_pos.ref = VALVE_MIN_POS;
Lightvalve 57:f4819de54e7a 2601 torq.err_sum = torq.err_sum - valve_pos_rem/(float) TMR_FREQ_5k;
Lightvalve 57:f4819de54e7a 2602 }
Lightvalve 57:f4819de54e7a 2603 }
Lightvalve 61:bc8c8270f0ab 2604
Lightvalve 57:f4819de54e7a 2605 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 61:bc8c8270f0ab 2606
Lightvalve 67:c2812cf26c38 2607 // Vout.ref = (float) P_GAIN_JOINT_POSITION * 0.01f * ((float) pos.err);
Lightvalve 60:64181f1d3e60 2608 V_out = (float) Vout.ref;
Lightvalve 57:f4819de54e7a 2609
Lightvalve 57:f4819de54e7a 2610 }
Lightvalve 169:645207e160ca 2611
Lightvalve 72:3436ce769b1e 2612 torq_ref_past = torq_ref;
Lightvalve 58:2eade98630e2 2613
Lightvalve 133:22ab22818e01 2614
Lightvalve 57:f4819de54e7a 2615 break;
Lightvalve 57:f4819de54e7a 2616 }
Lightvalve 58:2eade98630e2 2617
Lightvalve 57:f4819de54e7a 2618 case MODE_VALVE_OPEN_LOOP: {
Lightvalve 57:f4819de54e7a 2619 V_out = (float) Vout.ref;
Lightvalve 57:f4819de54e7a 2620 break;
Lightvalve 57:f4819de54e7a 2621 }
Lightvalve 169:645207e160ca 2622
Lightvalve 138:a843f32ced33 2623 case MODE_JOINT_ADAPTIVE_BACKSTEPPING: {
Lightvalve 169:645207e160ca 2624
Lightvalve 169:645207e160ca 2625
Lightvalve 139:15621998925b 2626 float Va = (1256.6f + Amm * pos.sen/(float)(ENC_PULSE_PER_POSITION)) * 0.000000001f; // 4mm pipe * 100mm + (25mm Cylinder 18mm Rod) * x, unit : m^3
Lightvalve 139:15621998925b 2627 float Vb = (1256.6f + Amm * (79.0f - pos.sen/(float)(ENC_PULSE_PER_POSITION))) * 0.000000001f; // 4mm pipe * 100mm + (25mm Cylinder 18mm Rod) * (79.0mm-x), unit : m^3
Lightvalve 169:645207e160ca 2628
Lightvalve 139:15621998925b 2629 // float Va = (1256.6f + Amm * 39.5f) * 0.000000001f; // 4mm pipe * 100mm + (25mm Cylinder 18mm Rod) * x, unit : m^3
Lightvalve 139:15621998925b 2630 // float Vb = (1256.6f + Amm * 39.5f) * 0.000000001f; // 4mm pipe * 100mm + (25mm Cylinder 18mm Rod) * (79.0mm-x), unit : m^3
Lightvalve 170:42c938a40313 2631 V_adapt = 1.0f / (1.0f/Va + 1.0f/Vb); //initial 0.0000053f
Lightvalve 169:645207e160ca 2632
Lightvalve 169:645207e160ca 2633
Lightvalve 170:42c938a40313 2634 float f3 = -Amm*Amm*beta*0.000001f*0.000001f/V_adapt * vel.sen/(float)(ENC_PULSE_PER_POSITION)*0.001f; // unit : N/s //xdot=10mm/s일때 -137076
Lightvalve 169:645207e160ca 2635
Lightvalve 138:a843f32ced33 2636 float g3_prime = 0.0f;
Lightvalve 169:645207e160ca 2637 if (torq.sen > Amm*(Ps-Pt)*0.000001f) {
Lightvalve 138:a843f32ced33 2638 g3_prime = 1.0f;
Lightvalve 138:a843f32ced33 2639 } else if (torq.sen < -Amm*(Ps-Pt)*0.000001f) {
Lightvalve 138:a843f32ced33 2640 g3_prime = -1.0f;
Lightvalve 138:a843f32ced33 2641 } else {
Lightvalve 138:a843f32ced33 2642 if ((value-VALVE_CENTER) > 0) {
Lightvalve 169:645207e160ca 2643 g3_prime = sqrt(Ps-Pt-torq.sen/Amm*1000000.0f);
Lightvalve 169:645207e160ca 2644 // g3_prime = sqrt(Ps-Pt);
Lightvalve 138:a843f32ced33 2645 } else {
Lightvalve 138:a843f32ced33 2646 g3_prime = sqrt(Ps-Pt+torq.sen/Amm*1000000.0f);
Lightvalve 139:15621998925b 2647 // g3_prime = sqrt(Ps-Pt);
Lightvalve 138:a843f32ced33 2648 }
Lightvalve 138:a843f32ced33 2649 }
Lightvalve 138:a843f32ced33 2650 float tau = 0.01f;
Lightvalve 142:43026242815a 2651 float K_valve = 0.0004f;
Lightvalve 169:645207e160ca 2652
Lightvalve 138:a843f32ced33 2653 float x_v = 0.0f; //x_v : -1~1
Lightvalve 138:a843f32ced33 2654 if(value>=VALVE_CENTER) {
Lightvalve 138:a843f32ced33 2655 x_v = 1.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER);
Lightvalve 138:a843f32ced33 2656 } else {
Lightvalve 138:a843f32ced33 2657 x_v = -1.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER);
Lightvalve 138:a843f32ced33 2658 }
Lightvalve 138:a843f32ced33 2659 float f4 = -x_v/tau;
Lightvalve 138:a843f32ced33 2660 float g4 = K_valve/tau;
Lightvalve 169:645207e160ca 2661
Lightvalve 139:15621998925b 2662 float torq_ref_dot = torq.ref_diff * 500.0f;
Lightvalve 169:645207e160ca 2663
Lightvalve 138:a843f32ced33 2664 pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm]
Lightvalve 138:a843f32ced33 2665 vel.err = (0.0f - vel.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm/s]
Lightvalve 138:a843f32ced33 2666 pos.err_sum += pos.err/(float) TMR_FREQ_5k; //[mm]
Lightvalve 169:645207e160ca 2667
Lightvalve 138:a843f32ced33 2668 torq.err = torq.ref - torq.sen; //[N]
Lightvalve 138:a843f32ced33 2669 torq.err_sum += torq.err/(float) TMR_FREQ_5k; //[N]
Lightvalve 169:645207e160ca 2670
Lightvalve 142:43026242815a 2671 float k3 = 20000.0f; //2000
Lightvalve 142:43026242815a 2672 float k4 = 10.0f;
Lightvalve 142:43026242815a 2673 float rho3 = 3.2f;
Lightvalve 142:43026242815a 2674 float rho4 = 25000000.0f;
Lightvalve 139:15621998925b 2675 float x_4_des = (-f3 + torq_ref_dot - k3*(-torq.err))/(gamma_hat*g3_prime);
Lightvalve 138:a843f32ced33 2676 if (x_4_des > 1) x_4_des = 1;
Lightvalve 138:a843f32ced33 2677 else if (x_4_des < -1) x_4_des = -1;
Lightvalve 169:645207e160ca 2678
Lightvalve 139:15621998925b 2679 if (x_4_des > 0) {
Lightvalve 139:15621998925b 2680 valve_pos.ref = x_4_des * (float)(VALVE_MAX_POS - VALVE_CENTER) + (float) VALVE_CENTER;
Lightvalve 139:15621998925b 2681 } else {
Lightvalve 139:15621998925b 2682 valve_pos.ref = x_4_des * (float)(VALVE_CENTER - VALVE_MIN_POS) + (float) VALVE_CENTER;
Lightvalve 139:15621998925b 2683 }
Lightvalve 169:645207e160ca 2684
Lightvalve 169:645207e160ca 2685
Lightvalve 138:a843f32ced33 2686 float x_4_des_dot = (x_4_des - x_4_des_old)*(float) TMR_FREQ_5k;
Lightvalve 138:a843f32ced33 2687 x_4_des_old = x_4_des;
Lightvalve 169:645207e160ca 2688
Lightvalve 139:15621998925b 2689 V_out = (-f4 + x_4_des_dot - k4*(x_v-x_4_des)- rho3/rho4*gamma_hat*g3_prime*(-torq.err))/g4;
Lightvalve 169:645207e160ca 2690
Lightvalve 142:43026242815a 2691 float rho_gamma = 50000.0f;//5000
Lightvalve 139:15621998925b 2692 float gamma_hat_dot = rho3*(-torq.err)/rho_gamma*((-f3+torq_ref_dot-k3*(-torq.err))/gamma_hat + g3_prime*(x_v-x_4_des));
Lightvalve 138:a843f32ced33 2693 gamma_hat = gamma_hat + gamma_hat_dot / (float) TMR_FREQ_5k;
Lightvalve 169:645207e160ca 2694 break;
Lightvalve 138:a843f32ced33 2695 }
Lightvalve 171:bfc1fd2629d8 2696
Lightvalve 170:42c938a40313 2697 case MODE_RL: {
Lightvalve 170:42c938a40313 2698 //t.reset();
Lightvalve 170:42c938a40313 2699 //t.start();
Lightvalve 170:42c938a40313 2700
Lightvalve 170:42c938a40313 2701 // if(LED == 0) LED = 1;
Lightvalve 170:42c938a40313 2702 // else LED = 0;
Lightvalve 170:42c938a40313 2703
Lightvalve 170:42c938a40313 2704 if (Update_Done_Flag == 1) {
Lightvalve 170:42c938a40313 2705 //Gather Data on each loop
Lightvalve 170:42c938a40313 2706 // pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm]
Lightvalve 170:42c938a40313 2707 // train_set_x[RL_timer] = pos.sen/(float)(ENC_PULSE_PER_POSITION)/35.0f - 1.0f; //-1.0~1.0
Lightvalve 170:42c938a40313 2708 // train_set_error[RL_timer] = pos.err/70.0f; //-1.0~1.0
Lightvalve 170:42c938a40313 2709 pos.err = pos.sen/(float)(ENC_PULSE_PER_POSITION) - virt_pos; //[mm]
Lightvalve 170:42c938a40313 2710 train_set_x[RL_timer] = virt_pos/70.0f; //-1.0~1.0
Lightvalve 170:42c938a40313 2711 train_set_error[RL_timer] = pos.err/70.0f; //-1.0~1.0
Lightvalve 170:42c938a40313 2712 //train_set_count[RL_timer] = (float) RL_timer / (batch_size *num_batch); //-1.0~1.0
Lightvalve 170:42c938a40313 2713 //float temp_array[3] = {train_set_x[RL_timer], train_set_error[RL_timer], train_set_count[RL_timer]};
Lightvalve 170:42c938a40313 2714 float temp_array[2] = {train_set_x[RL_timer], train_set_error[RL_timer]};
Lightvalve 170:42c938a40313 2715 Actor_Network(temp_array);
Lightvalve 173:68c7914679ec 2716 for (int i=0; i<num_hidden_unit1; i++) {
Lightvalve 173:68c7914679ec 2717 hx_a_sum_array[RL_timer][i] = hx_a_sum[i];
Lightvalve 173:68c7914679ec 2718 }
Lightvalve 173:68c7914679ec 2719 for (int i=0; i<num_hidden_unit2; i++) {
Lightvalve 173:68c7914679ec 2720 hxh_a_sum_array[RL_timer][i] = hxh_a_sum[i];
Lightvalve 173:68c7914679ec 2721 }
Lightvalve 173:68c7914679ec 2722 hxhh_a_sum_array[RL_timer][0] = hxhh_a_sum[0];
Lightvalve 173:68c7914679ec 2723 hxhh_a_sum_array[RL_timer][1] = hxhh_a_sum[1];
Lightvalve 170:42c938a40313 2724 mean_array[RL_timer] = mean;
Lightvalve 170:42c938a40313 2725 deviation_array[RL_timer] = deviation;
Lightvalve 170:42c938a40313 2726 action_array[RL_timer] = rand_normal(mean_array[RL_timer], deviation_array[RL_timer]);
Lightvalve 170:42c938a40313 2727
Lightvalve 170:42c938a40313 2728 virt_pos = virt_pos + (action_array[RL_timer] - 3.0f) * 1000.0f * 0.0002f;
Lightvalve 170:42c938a40313 2729 if (virt_pos > 70.0f ) {
Lightvalve 170:42c938a40313 2730 virt_pos = 70.0f;
Lightvalve 170:42c938a40313 2731 } else if(virt_pos < -70.0f) {
Lightvalve 170:42c938a40313 2732 virt_pos = -70.0f;
Lightvalve 170:42c938a40313 2733 }
Lightvalve 171:bfc1fd2629d8 2734
Lightvalve 170:42c938a40313 2735 RL_timer++;
Lightvalve 170:42c938a40313 2736
Lightvalve 170:42c938a40313 2737
Lightvalve 170:42c938a40313 2738 if (RL_timer >= batch_size) {
Lightvalve 170:42c938a40313 2739 RL_timer = 0;
Lightvalve 170:42c938a40313 2740 batch++;
Lightvalve 170:42c938a40313 2741 for(int i=0; i<batch_size; i++) {
Lightvalve 170:42c938a40313 2742 state_array[i][0] = train_set_x[i];
Lightvalve 170:42c938a40313 2743 state_array[i][1] = train_set_error[i];
Lightvalve 170:42c938a40313 2744 //state_array[i][2] = train_set_count[i];
Lightvalve 170:42c938a40313 2745 }
Lightvalve 170:42c938a40313 2746 Update_Case = 1;
Lightvalve 170:42c938a40313 2747 Update_Done_Flag = 0;
Lightvalve 170:42c938a40313 2748 logging1 = virt_pos;
Lightvalve 170:42c938a40313 2749 //virt_pos = 10.0f;
Lightvalve 170:42c938a40313 2750
Lightvalve 170:42c938a40313 2751 if(batch >= num_batch) {
Lightvalve 170:42c938a40313 2752 batch = 0;
Lightvalve 170:42c938a40313 2753 RL_timer = 0;
Lightvalve 170:42c938a40313 2754 Update_Case = 2;
Lightvalve 170:42c938a40313 2755 Update_Done_Flag = 0;
Lightvalve 170:42c938a40313 2756 virt_pos = 10.0f;
Lightvalve 170:42c938a40313 2757 }
Lightvalve 170:42c938a40313 2758 }
Lightvalve 170:42c938a40313 2759 }
Lightvalve 170:42c938a40313 2760
Lightvalve 170:42c938a40313 2761 else {
Lightvalve 170:42c938a40313 2762
Lightvalve 170:42c938a40313 2763 pos.err = pos.sen/(float)(ENC_PULSE_PER_POSITION) - virt_pos; //[mm]
Lightvalve 170:42c938a40313 2764 float temp_array[3] = {0.0f};
Lightvalve 170:42c938a40313 2765 temp_array[0] = virt_pos/70.0f; //-1.0~1.0
Lightvalve 170:42c938a40313 2766 temp_array[1] = pos.err/70.0f; //-1.0~1.0
Lightvalve 170:42c938a40313 2767 //temp_array[2] = (float) RL_timer / (batch_size *num_batch); //-1.0~1.0
Lightvalve 170:42c938a40313 2768 Actor_Network(temp_array);
Lightvalve 170:42c938a40313 2769 action = rand_normal(mean, deviation);
Lightvalve 170:42c938a40313 2770 //logging1 = action;
Lightvalve 170:42c938a40313 2771 logging2 = mean;
Lightvalve 170:42c938a40313 2772 logging4 = deviation;
Lightvalve 170:42c938a40313 2773 virt_pos = virt_pos + (action-3.0f) * 1000.0f * 0.0002f;
Lightvalve 170:42c938a40313 2774 if (virt_pos > 70.0f) {
Lightvalve 170:42c938a40313 2775 virt_pos = 70.0f;
Lightvalve 170:42c938a40313 2776 } else if(virt_pos < -70.0f) {
Lightvalve 170:42c938a40313 2777 virt_pos = -70.0f;
Lightvalve 170:42c938a40313 2778 }
Lightvalve 170:42c938a40313 2779
Lightvalve 170:42c938a40313 2780 logging3 = virt_pos;
Lightvalve 170:42c938a40313 2781 }
Lightvalve 170:42c938a40313 2782
Lightvalve 170:42c938a40313 2783 //t.stop();
Lightvalve 170:42c938a40313 2784 //logging1 = t.read()*1000.0f; //msec
Lightvalve 170:42c938a40313 2785
Lightvalve 170:42c938a40313 2786 break;
Lightvalve 170:42c938a40313 2787 }
Lightvalve 14:8e7590227d22 2788
Lightvalve 12:6f2531038ea4 2789 default:
Lightvalve 12:6f2531038ea4 2790 break;
Lightvalve 12:6f2531038ea4 2791 }
Lightvalve 14:8e7590227d22 2792
Lightvalve 57:f4819de54e7a 2793
Lightvalve 57:f4819de54e7a 2794 if (((OPERATING_MODE&0b110)>>1) == 0 || ((OPERATING_MODE&0b110)>>1) == 1) { //Moog Valve or KNR Valve
Lightvalve 58:2eade98630e2 2795
Lightvalve 57:f4819de54e7a 2796 ////////////////////////////////////////////////////////////////////////////
Lightvalve 57:f4819de54e7a 2797 //////////////////////////// CURRENT CONTROL //////////////////////////////
Lightvalve 57:f4819de54e7a 2798 ////////////////////////////////////////////////////////////////////////////
Lightvalve 57:f4819de54e7a 2799 if (CURRENT_CONTROL_MODE) {
Lightvalve 67:c2812cf26c38 2800 double alpha_update_Iref = 1.0f / (1.0f + 5000.0f / (2.0f * 3.14f * 300.0f)); // f_cutoff : 500Hz
Lightvalve 57:f4819de54e7a 2801 I_REF_fil = (1.0f - alpha_update_Iref) * I_REF_fil + alpha_update_Iref*I_REF;
Lightvalve 57:f4819de54e7a 2802
Lightvalve 57:f4819de54e7a 2803 I_ERR = I_REF_fil - cur.sen;
Lightvalve 67:c2812cf26c38 2804 I_ERR_INT = I_ERR_INT + (I_ERR) * 0.0002f;
Lightvalve 57:f4819de54e7a 2805
Lightvalve 57:f4819de54e7a 2806
Lightvalve 57:f4819de54e7a 2807 // Moog Valve Current Control Gain
Lightvalve 67:c2812cf26c38 2808 double R_model = 500.0f; // ohm
Lightvalve 57:f4819de54e7a 2809 double L_model = 1.2f;
Lightvalve 57:f4819de54e7a 2810 double w0 = 2.0f * 3.14f * 150.0f;
Lightvalve 57:f4819de54e7a 2811 double KP_I = 0.1f * L_model*w0;
Lightvalve 57:f4819de54e7a 2812 double KI_I = 0.1f * R_model*w0;
Lightvalve 57:f4819de54e7a 2813
Lightvalve 57:f4819de54e7a 2814 // KNR Valve Current Control Gain
Lightvalve 57:f4819de54e7a 2815 if (((OPERATING_MODE & 0b110)>>1) == 1) { // KNR Valve
Lightvalve 57:f4819de54e7a 2816 R_model = 163.0f; // ohm
Lightvalve 57:f4819de54e7a 2817 L_model = 1.0f;
Lightvalve 57:f4819de54e7a 2818 w0 = 2.0f * 3.14f * 80.0f;
Lightvalve 57:f4819de54e7a 2819 KP_I = 1.0f * L_model*w0;
Lightvalve 57:f4819de54e7a 2820 KI_I = 0.08f * R_model*w0;
Lightvalve 57:f4819de54e7a 2821 }
Lightvalve 57:f4819de54e7a 2822
Lightvalve 57:f4819de54e7a 2823 double FF_gain = 1.0f;
Lightvalve 57:f4819de54e7a 2824
Lightvalve 67:c2812cf26c38 2825 VALVE_PWM_RAW = KP_I * 2.0f * I_ERR + KI_I * 2.0f* I_ERR_INT;
Lightvalve 57:f4819de54e7a 2826 // VALVE_PWM_RAW = VALVE_PWM_RAW + FF_gain * (R_model*I_REF); // Unit : mV
Lightvalve 57:f4819de54e7a 2827 I_REF_fil_diff = I_REF_fil - I_REF_fil_old;
Lightvalve 57:f4819de54e7a 2828 I_REF_fil_old = I_REF_fil;
Lightvalve 67:c2812cf26c38 2829 // VALVE_PWM_RAW = VALVE_PWM_RAW + FF_gain * (R_model * I_REF_fil + L_model * I_REF_fil_diff * 5000.0f); // Unit : mV
Lightvalve 67:c2812cf26c38 2830 VALVE_PWM_RAW = VALVE_PWM_RAW + FF_gain * (R_model * I_REF_fil); // Unit : mV
Lightvalve 67:c2812cf26c38 2831 double V_MAX = 12000.0f; // Maximum Voltage : 12V = 12000mV
Lightvalve 57:f4819de54e7a 2832
Lightvalve 57:f4819de54e7a 2833 double Ka = 3.0f / KP_I;
Lightvalve 57:f4819de54e7a 2834 if (VALVE_PWM_RAW > V_MAX) {
Lightvalve 57:f4819de54e7a 2835 V_rem = VALVE_PWM_RAW - V_MAX;
Lightvalve 57:f4819de54e7a 2836 V_rem = Ka*V_rem;
Lightvalve 57:f4819de54e7a 2837 VALVE_PWM_RAW = V_MAX;
Lightvalve 67:c2812cf26c38 2838 I_ERR_INT = I_ERR_INT - V_rem * 0.0002f;
Lightvalve 57:f4819de54e7a 2839 } else if (VALVE_PWM_RAW < -V_MAX) {
Lightvalve 57:f4819de54e7a 2840 V_rem = VALVE_PWM_RAW - (-V_MAX);
Lightvalve 57:f4819de54e7a 2841 V_rem = Ka*V_rem;
Lightvalve 57:f4819de54e7a 2842 VALVE_PWM_RAW = -V_MAX;
Lightvalve 67:c2812cf26c38 2843 I_ERR_INT = I_ERR_INT - V_rem * 0.0002f;
Lightvalve 57:f4819de54e7a 2844 }
Lightvalve 57:f4819de54e7a 2845 Cur_Valve_Open_pulse = cur.sen / mA_PER_pulse;
Lightvalve 57:f4819de54e7a 2846 } else {
Lightvalve 57:f4819de54e7a 2847 VALVE_PWM_RAW = I_REF * mV_PER_mA;
Lightvalve 57:f4819de54e7a 2848 Cur_Valve_Open_pulse = I_REF / mA_PER_pulse;
Lightvalve 57:f4819de54e7a 2849 }
Lightvalve 57:f4819de54e7a 2850
Lightvalve 57:f4819de54e7a 2851 ////////////////////////////////////////////////////////////////////////////
Lightvalve 57:f4819de54e7a 2852 ///////////////// Dead Zone Cancellation & Linearization //////////////////
Lightvalve 57:f4819de54e7a 2853 ////////////////////////////////////////////////////////////////////////////
Lightvalve 57:f4819de54e7a 2854 // Dead Zone Cancellation (Mechanical Valve dead-zone)
Lightvalve 57:f4819de54e7a 2855 if (FLAG_VALVE_DEADZONE) {
Lightvalve 57:f4819de54e7a 2856 if (VALVE_PWM_RAW > 0) VALVE_PWM_RAW = VALVE_PWM_RAW + VALVE_DEADZONE_PLUS * mV_PER_pulse; // unit: mV
Lightvalve 57:f4819de54e7a 2857 else if (VALVE_PWM_RAW < 0) VALVE_PWM_RAW = VALVE_PWM_RAW + VALVE_DEADZONE_MINUS * mV_PER_pulse; // unit: mV
Lightvalve 57:f4819de54e7a 2858
Lightvalve 57:f4819de54e7a 2859 VALVE_PWM_VALVE_DZ = VALVE_PWM_RAW + (double)VALVE_CENTER * mV_PER_pulse; // unit: mV
Lightvalve 57:f4819de54e7a 2860
Lightvalve 57:f4819de54e7a 2861 } else {
Lightvalve 57:f4819de54e7a 2862 VALVE_PWM_VALVE_DZ = VALVE_PWM_RAW;
Lightvalve 57:f4819de54e7a 2863 }
Lightvalve 57:f4819de54e7a 2864
Lightvalve 57:f4819de54e7a 2865 // Output Voltage Linearization
Lightvalve 57:f4819de54e7a 2866 double CUR_PWM_nonlin = VALVE_PWM_VALVE_DZ; // Unit : mV
Lightvalve 67:c2812cf26c38 2867 double CUR_PWM_lin = PWM_duty_byLT(CUR_PWM_nonlin); // -8000~8000
Lightvalve 57:f4819de54e7a 2868
Lightvalve 57:f4819de54e7a 2869 // Dead Zone Cancellation (Electrical dead-zone)
Lightvalve 67:c2812cf26c38 2870 if (CUR_PWM_lin > 0) V_out = (float) (CUR_PWM_lin + 169.0f);
Lightvalve 67:c2812cf26c38 2871 else if (CUR_PWM_lin < 0) V_out = (float) (CUR_PWM_lin - 174.0f);
Lightvalve 67:c2812cf26c38 2872 else V_out = (float) (CUR_PWM_lin);
Lightvalve 169:645207e160ca 2873 } else { //////////////////////////sw valve
Lightvalve 89:a7b45368ea0f 2874 // Output Voltage Linearization
Lightvalve 135:79885a39c161 2875 // double CUR_PWM_nonlin = V_out; // Unit : mV
Lightvalve 135:79885a39c161 2876 // double CUR_PWM_lin = PWM_duty_byLT(CUR_PWM_nonlin); // -8000~8000
Lightvalve 89:a7b45368ea0f 2877
Lightvalve 89:a7b45368ea0f 2878 // Dead Zone Cancellation (Electrical dead-zone)
Lightvalve 135:79885a39c161 2879 // if (CUR_PWM_lin > 0) V_out = (float) (CUR_PWM_lin + 169.0f);
Lightvalve 135:79885a39c161 2880 // else if (CUR_PWM_lin < 0) V_out = (float) (CUR_PWM_lin - 174.0f);
Lightvalve 135:79885a39c161 2881 // else V_out = (float) (CUR_PWM_lin);
Lightvalve 135:79885a39c161 2882
Lightvalve 135:79885a39c161 2883 if (V_out > 0 ) V_out = (V_out + 180.0f)/0.8588f;
Lightvalve 135:79885a39c161 2884 else if (V_out < 0) V_out = (V_out - 200.0f)/0.8651f;
Lightvalve 135:79885a39c161 2885 else V_out = 0.0f;
Lightvalve 89:a7b45368ea0f 2886 }
Lightvalve 169:645207e160ca 2887
Lightvalve 67:c2812cf26c38 2888 // if(V_out > 0.0f) V_out = (float) (V_out + 169.0f);
Lightvalve 67:c2812cf26c38 2889 // else if(V_out < 0.0f) V_out = (float) (V_out - 174.0f);
Lightvalve 67:c2812cf26c38 2890 // else V_out = V_out;
Lightvalve 169:645207e160ca 2891
jobuuu 7:e9086c72bb22 2892 /*******************************************************
jobuuu 7:e9086c72bb22 2893 *** PWM
jobuuu 7:e9086c72bb22 2894 ********************************************************/
Lightvalve 169:645207e160ca 2895 if(DIR_VALVE<0) {
Lightvalve 67:c2812cf26c38 2896 V_out = -V_out;
Lightvalve 67:c2812cf26c38 2897 }
Lightvalve 169:645207e160ca 2898
Lightvalve 49:e7bcfc244d40 2899 if (V_out >= VALVE_VOLTAGE_LIMIT*1000.0f) {
Lightvalve 49:e7bcfc244d40 2900 V_out = VALVE_VOLTAGE_LIMIT*1000.0f;
Lightvalve 49:e7bcfc244d40 2901 } else if(V_out<=-VALVE_VOLTAGE_LIMIT*1000.0f) {
Lightvalve 49:e7bcfc244d40 2902 V_out = -VALVE_VOLTAGE_LIMIT*1000.0f;
Lightvalve 27:a2254a485f23 2903 }
Lightvalve 49:e7bcfc244d40 2904 PWM_out= V_out/(SUPPLY_VOLTAGE*1000.0f); // Full duty : 12000.0mV
Lightvalve 13:747daba9cf59 2905
Lightvalve 19:23b7c1ad8683 2906 // Saturation of output voltage to 12.0V
Lightvalve 30:8d561f16383b 2907 if(PWM_out > 1.0f) PWM_out=1.0f;
Lightvalve 30:8d561f16383b 2908 else if (PWM_out < -1.0f) PWM_out=-1.0f;
Lightvalve 13:747daba9cf59 2909
Lightvalve 30:8d561f16383b 2910 if (PWM_out>0.0f) {
Lightvalve 30:8d561f16383b 2911 dtc_v=0.0f;
jobuuu 1:e04e563be5ce 2912 dtc_w=PWM_out;
jobuuu 1:e04e563be5ce 2913 } else {
jobuuu 2:a1c0a37df760 2914 dtc_v=-PWM_out;
Lightvalve 30:8d561f16383b 2915 dtc_w=0.0f;
jobuuu 1:e04e563be5ce 2916 }
Lightvalve 13:747daba9cf59 2917
jobuuu 1:e04e563be5ce 2918 //pwm
Lightvalve 30:8d561f16383b 2919 TIM4->CCR2 = (PWM_ARR)*(1.0f-dtc_v);
Lightvalve 30:8d561f16383b 2920 TIM4->CCR1 = (PWM_ARR)*(1.0f-dtc_w);
Lightvalve 67:c2812cf26c38 2921
Lightvalve 61:bc8c8270f0ab 2922
Lightvalve 57:f4819de54e7a 2923 if (TMR2_COUNT_CAN_TX % (int) ((int) TMR_FREQ_5k/CAN_FREQ) == 0) {
Lightvalve 20:806196fda269 2924
Lightvalve 54:647072f5307a 2925 // Position, Velocity, and Torque (ID:1200)
Lightvalve 56:6f50d9d3bfee 2926 if (flag_data_request[0] == HIGH) {
Lightvalve 57:f4819de54e7a 2927 if ((OPERATING_MODE & 0b01) == 0) { // Rotary Actuator
Lightvalve 54:647072f5307a 2928 if (SENSING_MODE == 0) {
Lightvalve 67:c2812cf26c38 2929 CAN_TX_POSITION_FT((int16_t) (pos.sen), (int16_t) (vel.sen/10.0f), (int16_t) (torq.sen*10.0f));
Lightvalve 54:647072f5307a 2930 } else if (SENSING_MODE == 1) {
Lightvalve 58:2eade98630e2 2931 CAN_TX_POSITION_PRESSURE((int16_t) (pos.sen), (int16_t) (vel.sen/10.0f), (int16_t) ((pres_A.sen)*5.0f), (int16_t) ((pres_B.sen)*5.0f));
Lightvalve 54:647072f5307a 2932 }
Lightvalve 57:f4819de54e7a 2933 } else if ((OPERATING_MODE & 0b01) == 1) { // Linear Actuator
Lightvalve 54:647072f5307a 2934 if (SENSING_MODE == 0) {
Lightvalve 67:c2812cf26c38 2935 CAN_TX_POSITION_FT((int16_t) (pos.sen/10.0f), (int16_t) (vel.sen/256.0f), (int16_t) (torq.sen * 10.0f * (float)(TORQUE_SENSOR_PULSE_PER_TORQUE)));
Lightvalve 54:647072f5307a 2936 } else if (SENSING_MODE == 1) {
Lightvalve 67:c2812cf26c38 2937 CAN_TX_POSITION_PRESSURE((int16_t) (pos.sen/10.0f), (int16_t) (vel.sen/256.0f), (int16_t) ((pres_A.sen)*5.0f), (int16_t) ((pres_B.sen)*5.0f));
Lightvalve 54:647072f5307a 2938 }
Lightvalve 52:8ea76864368a 2939 }
Lightvalve 52:8ea76864368a 2940 }
Lightvalve 56:6f50d9d3bfee 2941 if (flag_data_request[1] == HIGH) {
Lightvalve 54:647072f5307a 2942 //valve position
Lightvalve 169:645207e160ca 2943 double t_value = 0.0f;
Lightvalve 97:d71c57e3515e 2944 if(valve_pos.ref>=(float) VALVE_CENTER) {
Lightvalve 97:d71c57e3515e 2945 t_value = 10000.0f*((double)valve_pos.ref - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER);
Lightvalve 97:d71c57e3515e 2946 } else {
Lightvalve 97:d71c57e3515e 2947 t_value = -10000.0f*((double)valve_pos.ref - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER);
Lightvalve 97:d71c57e3515e 2948 }
Lightvalve 67:c2812cf26c38 2949 CAN_TX_TORQUE((int16_t) (t_value)); //1300
Lightvalve 54:647072f5307a 2950 }
Lightvalve 58:2eade98630e2 2951
Lightvalve 58:2eade98630e2 2952
Lightvalve 171:bfc1fd2629d8 2953 if (flag_data_request[2] == HIGH) {
Lightvalve 169:645207e160ca 2954 double t_value = 0.0f;
Lightvalve 171:bfc1fd2629d8 2955 if(value>=(float) VALVE_CENTER) {
Lightvalve 131:d08121ac87ba 2956 t_value = 10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER);
Lightvalve 89:a7b45368ea0f 2957 } else {
Lightvalve 131:d08121ac87ba 2958 t_value = -10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER);
Lightvalve 89:a7b45368ea0f 2959 }
Lightvalve 169:645207e160ca 2960 double t_value_ref = 0.0f;
Lightvalve 169:645207e160ca 2961 if(valve_pos.ref>=(float) VALVE_CENTER) {
Lightvalve 169:645207e160ca 2962 t_value_ref = 10000.0f*((double)valve_pos.ref - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER);
Lightvalve 169:645207e160ca 2963 } else {
Lightvalve 169:645207e160ca 2964 t_value_ref = -10000.0f*((double)valve_pos.ref - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER);
Lightvalve 169:645207e160ca 2965 }
Lightvalve 169:645207e160ca 2966
Lightvalve 169:645207e160ca 2967
Lightvalve 169:645207e160ca 2968 CAN_TX_PRES((int16_t) (t_value), (int16_t) (t_value_ref)); // 1400
Lightvalve 55:b25725257569 2969 }
Lightvalve 58:2eade98630e2 2970
Lightvalve 57:f4819de54e7a 2971 //If it doesn't rest, below can can not work.
Lightvalve 57:f4819de54e7a 2972 for (can_rest = 0; can_rest < 10000; can_rest++) {
Lightvalve 57:f4819de54e7a 2973 ;
Lightvalve 57:f4819de54e7a 2974 }
Lightvalve 58:2eade98630e2 2975
Lightvalve 169:645207e160ca 2976 if (flag_data_request[3] == HIGH) {
Lightvalve 54:647072f5307a 2977 //PWM
Lightvalve 73:f80dc3970c99 2978 //CAN_TX_PWM((int16_t) value); //1500
Lightvalve 139:15621998925b 2979 CAN_TX_PWM((int16_t) gamma_hat); //1500
Lightvalve 54:647072f5307a 2980 }
Lightvalve 57:f4819de54e7a 2981 //for (i = 0; i < 10000; i++) {
Lightvalve 57:f4819de54e7a 2982 // ;
Lightvalve 57:f4819de54e7a 2983 // }
Lightvalve 56:6f50d9d3bfee 2984 if (flag_data_request[4] == HIGH) {
Lightvalve 54:647072f5307a 2985 //valve position
Lightvalve 171:bfc1fd2629d8 2986 CAN_TX_VALVE_POSITION((int16_t) pos.sen/(float)(ENC_PULSE_PER_POSITION), (int16_t) virt_pos, (int16_t) 0, (int16_t) 0); //1600
Lightvalve 54:647072f5307a 2987 }
Lightvalve 20:806196fda269 2988
Lightvalve 54:647072f5307a 2989 // Others : Reference position, Reference FT, PWM, Current (ID:1300)
Lightvalve 52:8ea76864368a 2990 // if (flag_data_request[1] == HIGH) {
Lightvalve 52:8ea76864368a 2991 // CAN_TX_SOMETHING((int) (FORCE_VREF), (int16_t) (1), (int16_t) (2), (int16_t) (3));
Lightvalve 52:8ea76864368a 2992 // }
Lightvalve 54:647072f5307a 2993 //if (flag_delay_test == 1){
Lightvalve 58:2eade98630e2 2994 //CAN_TX_PRES((int16_t) (0),(int16_t) torq_ref);
Lightvalve 54:647072f5307a 2995 //}
Lightvalve 52:8ea76864368a 2996
Lightvalve 54:647072f5307a 2997 TMR2_COUNT_CAN_TX = 0;
Lightvalve 54:647072f5307a 2998 }
Lightvalve 54:647072f5307a 2999 TMR2_COUNT_CAN_TX++;
Lightvalve 52:8ea76864368a 3000
Lightvalve 20:806196fda269 3001 }
Lightvalve 52:8ea76864368a 3002 TIM3->SR = 0x0; // reset the status register
Lightvalve 52:8ea76864368a 3003
Lightvalve 58:2eade98630e2 3004 }