Sungwoo Kim
/
HydraulicControlBoard_Rainbow_v1_2_copy1
2011
main.cpp@199:ee79b4692e3b, 2020-12-20 (annotated)
- Committer:
- Lightvalve
- Date:
- Sun Dec 20 03:25:36 2020 +0000
- Revision:
- 199:ee79b4692e3b
- Parent:
- 198:bc4dfecb8e32
201220_2_delay
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Lightvalve | 199:ee79b4692e3b | 1 | //201220_2_with_delay |
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 | 179:d5377766d7ea | 14 | #include <cmath> |
Lightvalve | 61:bc8c8270f0ab | 15 | |
Lightvalve | 61:bc8c8270f0ab | 16 | using namespace std; |
Lightvalve | 61:bc8c8270f0ab | 17 | Timer t; |
GiJeongKim | 0:51c43836c1d7 | 18 | |
Lightvalve | 31:66738bfecec5 | 19 | ///191008//// |
Lightvalve | 31:66738bfecec5 | 20 | |
jobuuu | 7:e9086c72bb22 | 21 | // dac & check /////////////////////////////////////////// |
GiJeongKim | 0:51c43836c1d7 | 22 | DigitalOut check(PC_2); |
GiJeongKim | 0:51c43836c1d7 | 23 | DigitalOut check_2(PC_3); |
GiJeongKim | 0:51c43836c1d7 | 24 | AnalogOut dac_1(PA_4); |
GiJeongKim | 0:51c43836c1d7 | 25 | AnalogOut dac_2(PA_5); |
Lightvalve | 14:8e7590227d22 | 26 | AnalogIn adc1(PC_4); //pressure_1 |
Lightvalve | 14:8e7590227d22 | 27 | AnalogIn adc2(PB_0); //pressure_2 |
Lightvalve | 14:8e7590227d22 | 28 | AnalogIn adc3(PC_1); //current |
GiJeongKim | 0:51c43836c1d7 | 29 | |
Lightvalve | 24:ef6e1092e9e6 | 30 | |
jobuuu | 7:e9086c72bb22 | 31 | // PWM /////////////////////////////////////////// |
Lightvalve | 30:8d561f16383b | 32 | float dtc_v=0.0f; |
Lightvalve | 30:8d561f16383b | 33 | float dtc_w=0.0f; |
GiJeongKim | 0:51c43836c1d7 | 34 | |
jobuuu | 7:e9086c72bb22 | 35 | // I2C /////////////////////////////////////////// |
GiJeongKim | 0:51c43836c1d7 | 36 | I2C i2c(PC_9,PA_8); // SDA, SCL (for K22F) |
GiJeongKim | 0:51c43836c1d7 | 37 | const int i2c_slave_addr1 = 0x56; |
GiJeongKim | 0:51c43836c1d7 | 38 | unsigned int value; // 10bit output of reading sensor AS5510 |
GiJeongKim | 0:51c43836c1d7 | 39 | |
jobuuu | 7:e9086c72bb22 | 40 | // SPI /////////////////////////////////////////// |
Lightvalve | 170:42c938a40313 | 41 | SPI eeprom(PB_15, PB_14, PB_13); // EEPROM //(SPI_MOSI, SPI_MISO, SPI_SCK); |
Lightvalve | 170:42c938a40313 | 42 | DigitalOut eeprom_cs(PB_12); |
Lightvalve | 16:903b5a4433b4 | 43 | //FlashWriter writer(6);//2부터 7까지 되는듯 아마 sector |
GiJeongKim | 0:51c43836c1d7 | 44 | SPI enc(PC_12,PC_11,PC_10); |
GiJeongKim | 0:51c43836c1d7 | 45 | DigitalOut enc_cs(PD_2); |
Lightvalve | 11:82d8768d7351 | 46 | DigitalOut LED(PA_15); |
GiJeongKim | 0:51c43836c1d7 | 47 | |
jobuuu | 7:e9086c72bb22 | 48 | // UART /////////////////////////////////////////// |
GiJeongKim | 0:51c43836c1d7 | 49 | Serial pc(PA_9,PA_10); // _ UART |
GiJeongKim | 0:51c43836c1d7 | 50 | |
jobuuu | 7:e9086c72bb22 | 51 | // CAN /////////////////////////////////////////// |
GiJeongKim | 0:51c43836c1d7 | 52 | CAN can(PB_8, PB_9, 1000000); |
GiJeongKim | 0:51c43836c1d7 | 53 | CANMessage msg; |
Lightvalve | 11:82d8768d7351 | 54 | void onMsgReceived() |
Lightvalve | 11:82d8768d7351 | 55 | { |
Lightvalve | 11:82d8768d7351 | 56 | CAN_RX_HANDLER(); |
Lightvalve | 11:82d8768d7351 | 57 | } |
jobuuu | 2:a1c0a37df760 | 58 | |
jobuuu | 7:e9086c72bb22 | 59 | // Variables /////////////////////////////////////////// |
jobuuu | 7:e9086c72bb22 | 60 | State pos; |
jobuuu | 7:e9086c72bb22 | 61 | State vel; |
jobuuu | 7:e9086c72bb22 | 62 | State Vout; |
jobuuu | 7:e9086c72bb22 | 63 | State torq; |
jobuuu | 7:e9086c72bb22 | 64 | State pres_A; |
jobuuu | 7:e9086c72bb22 | 65 | State pres_B; |
jobuuu | 7:e9086c72bb22 | 66 | State cur; |
Lightvalve | 14:8e7590227d22 | 67 | State valve_pos; |
Lightvalve | 14:8e7590227d22 | 68 | |
Lightvalve | 14:8e7590227d22 | 69 | State INIT_Vout; |
Lightvalve | 14:8e7590227d22 | 70 | State INIT_Valve_Pos; |
Lightvalve | 14:8e7590227d22 | 71 | State INIT_Pos; |
Lightvalve | 14:8e7590227d22 | 72 | State INIT_torq; |
jobuuu | 5:a4319f79457b | 73 | |
Lightvalve | 19:23b7c1ad8683 | 74 | extern int CID_RX_CMD; |
Lightvalve | 19:23b7c1ad8683 | 75 | extern int CID_RX_REF_POSITION; |
Lightvalve | 49:e7bcfc244d40 | 76 | extern int CID_RX_REF_VALVE_POS; |
Lightvalve | 45:35fa6884d0c6 | 77 | extern int CID_RX_REF_PWM; |
Lightvalve | 19:23b7c1ad8683 | 78 | |
Lightvalve | 19:23b7c1ad8683 | 79 | extern int CID_TX_INFO; |
Lightvalve | 19:23b7c1ad8683 | 80 | extern int CID_TX_POSITION; |
Lightvalve | 19:23b7c1ad8683 | 81 | extern int CID_TX_TORQUE; |
Lightvalve | 19:23b7c1ad8683 | 82 | extern int CID_TX_PRES; |
Lightvalve | 19:23b7c1ad8683 | 83 | extern int CID_TX_VOUT; |
Lightvalve | 19:23b7c1ad8683 | 84 | extern int CID_TX_VALVE_POSITION; |
Lightvalve | 19:23b7c1ad8683 | 85 | |
Lightvalve | 61:bc8c8270f0ab | 86 | |
Lightvalve | 61:bc8c8270f0ab | 87 | |
Lightvalve | 61:bc8c8270f0ab | 88 | |
jobuuu | 7:e9086c72bb22 | 89 | // ============================================================================= |
jobuuu | 7:e9086c72bb22 | 90 | // ============================================================================= |
jobuuu | 7:e9086c72bb22 | 91 | // ============================================================================= |
jobuuu | 7:e9086c72bb22 | 92 | |
Lightvalve | 12:6f2531038ea4 | 93 | /******************************************************************************* |
Lightvalve | 12:6f2531038ea4 | 94 | * REFERENCE MODE |
Lightvalve | 12:6f2531038ea4 | 95 | ******************************************************************************/ |
Lightvalve | 13:747daba9cf59 | 96 | enum _REFERENCE_MODE { |
Lightvalve | 13:747daba9cf59 | 97 | MODE_REF_NO_ACT = 0, //0 |
Lightvalve | 12:6f2531038ea4 | 98 | MODE_REF_DIRECT, //1 |
Lightvalve | 12:6f2531038ea4 | 99 | MODE_REF_COS_INC, //2 |
Lightvalve | 12:6f2531038ea4 | 100 | MODE_REF_LINE_INC, //3 |
Lightvalve | 12:6f2531038ea4 | 101 | MODE_REF_SIN_WAVE, //4 |
Lightvalve | 12:6f2531038ea4 | 102 | MODE_REF_SQUARE_WAVE, //5 |
Lightvalve | 12:6f2531038ea4 | 103 | }; |
Lightvalve | 12:6f2531038ea4 | 104 | |
Lightvalve | 12:6f2531038ea4 | 105 | /******************************************************************************* |
Lightvalve | 12:6f2531038ea4 | 106 | * CONTROL MODE |
Lightvalve | 12:6f2531038ea4 | 107 | ******************************************************************************/ |
Lightvalve | 13:747daba9cf59 | 108 | enum _CONTROL_MODE { |
Lightvalve | 12:6f2531038ea4 | 109 | //control mode |
Lightvalve | 12:6f2531038ea4 | 110 | MODE_NO_ACT = 0, //0 |
Lightvalve | 47:fdcb8bd86fd6 | 111 | MODE_VALVE_POSITION_CONTROL, //1 |
Lightvalve | 47:fdcb8bd86fd6 | 112 | MODE_JOINT_CONTROL, //2 |
Lightvalve | 50:3c630b5eba9f | 113 | |
Lightvalve | 47:fdcb8bd86fd6 | 114 | MODE_VALVE_OPEN_LOOP, //3 |
Lightvalve | 138:a843f32ced33 | 115 | MODE_JOINT_ADAPTIVE_BACKSTEPPING, //4 |
Lightvalve | 171:bfc1fd2629d8 | 116 | MODE_RL, //5 |
Lightvalve | 13:747daba9cf59 | 117 | |
Lightvalve | 12:6f2531038ea4 | 118 | MODE_JOINT_POSITION_PRES_CONTROL_PWM, //6 |
Lightvalve | 12:6f2531038ea4 | 119 | MODE_JOINT_POSITION_PRES_CONTROL_VALVE_POSITION, //7 |
Lightvalve | 12:6f2531038ea4 | 120 | MODE_VALVE_POSITION_PRES_CONTROL_LEARNING, //8 |
Lightvalve | 13:747daba9cf59 | 121 | |
Lightvalve | 12:6f2531038ea4 | 122 | MODE_TEST_CURRENT_CONTROL, //9 |
Lightvalve | 12:6f2531038ea4 | 123 | MODE_TEST_PWM_CONTROL, //10 |
Lightvalve | 21:e5f1a43ea6f9 | 124 | |
Lightvalve | 14:8e7590227d22 | 125 | MODE_CURRENT_CONTROL, //11 |
Lightvalve | 14:8e7590227d22 | 126 | MODE_JOINT_POSITION_TORQUE_CONTROL_CURRENT, //12 |
Lightvalve | 14:8e7590227d22 | 127 | MODE_JOINT_POSITION_PRES_CONTROL_CURRENT, //13 |
Lightvalve | 171:bfc1fd2629d8 | 128 | MODE_VALVE_POSITION_TORQUE_CONTROL_LEARNING, //14 |
Lightvalve | 13:747daba9cf59 | 129 | |
Lightvalve | 12:6f2531038ea4 | 130 | //utility |
Lightvalve | 12:6f2531038ea4 | 131 | MODE_TORQUE_SENSOR_NULLING = 20, //20 |
Lightvalve | 12:6f2531038ea4 | 132 | MODE_VALVE_NULLING_AND_DEADZONE_SETTING, //21 |
Lightvalve | 12:6f2531038ea4 | 133 | MODE_FIND_HOME, //22 |
Lightvalve | 57:f4819de54e7a | 134 | MODE_VALVE_GAIN_SETTING, //23 |
Lightvalve | 12:6f2531038ea4 | 135 | MODE_PRESSURE_SENSOR_NULLING, //24 |
Lightvalve | 12:6f2531038ea4 | 136 | MODE_PRESSURE_SENSOR_CALIB, //25 |
Lightvalve | 12:6f2531038ea4 | 137 | MODE_ROTARY_FRICTION_TUNING, //26 |
Lightvalve | 13:747daba9cf59 | 138 | |
Lightvalve | 12:6f2531038ea4 | 139 | MODE_DDV_POS_VS_PWM_ID = 30, //30 |
Lightvalve | 12:6f2531038ea4 | 140 | MODE_DDV_DEADZONE_AND_CENTER, //31 |
Lightvalve | 12:6f2531038ea4 | 141 | MODE_DDV_POS_VS_FLOWRATE, //32 |
Lightvalve | 57:f4819de54e7a | 142 | MODE_SYSTEM_ID, //33 |
Lightvalve | 169:645207e160ca | 143 | MODE_FREQ_TEST, //34 |
Lightvalve | 169:645207e160ca | 144 | MODE_SEND_BUFFER, //35 |
Lightvalve | 169:645207e160ca | 145 | MODE_SEND_OVER, //36 |
Lightvalve | 169:645207e160ca | 146 | MODE_STEP_TEST, //37 |
Lightvalve | 12:6f2531038ea4 | 147 | }; |
Lightvalve | 12:6f2531038ea4 | 148 | |
Lightvalve | 65:a2d7c63419c2 | 149 | void SystemClock_Config(void) |
Lightvalve | 65:a2d7c63419c2 | 150 | { |
Lightvalve | 169:645207e160ca | 151 | RCC_OscInitTypeDef RCC_OscInitStruct = {0}; |
Lightvalve | 169:645207e160ca | 152 | RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; |
Lightvalve | 65:a2d7c63419c2 | 153 | |
Lightvalve | 169:645207e160ca | 154 | /** Configure the main internal regulator output voltage |
Lightvalve | 169:645207e160ca | 155 | */ |
Lightvalve | 169:645207e160ca | 156 | __HAL_RCC_PWR_CLK_ENABLE(); |
Lightvalve | 169:645207e160ca | 157 | __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); |
Lightvalve | 169:645207e160ca | 158 | /** Initializes the CPU, AHB and APB busses clocks |
Lightvalve | 169:645207e160ca | 159 | */ |
Lightvalve | 169:645207e160ca | 160 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; |
Lightvalve | 169:645207e160ca | 161 | RCC_OscInitStruct.HSIState = RCC_HSI_ON; |
Lightvalve | 169:645207e160ca | 162 | RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; |
Lightvalve | 169:645207e160ca | 163 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
Lightvalve | 169:645207e160ca | 164 | RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; |
Lightvalve | 169:645207e160ca | 165 | RCC_OscInitStruct.PLL.PLLM = 8;//8 |
Lightvalve | 169:645207e160ca | 166 | RCC_OscInitStruct.PLL.PLLN = 180; //180 |
Lightvalve | 169:645207e160ca | 167 | RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; |
Lightvalve | 169:645207e160ca | 168 | RCC_OscInitStruct.PLL.PLLQ = 2; |
Lightvalve | 169:645207e160ca | 169 | RCC_OscInitStruct.PLL.PLLR = 2; |
Lightvalve | 169:645207e160ca | 170 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { |
Lightvalve | 169:645207e160ca | 171 | //Error_Handler(); |
Lightvalve | 169:645207e160ca | 172 | } |
Lightvalve | 169:645207e160ca | 173 | /** Activate the Over-Drive mode |
Lightvalve | 169:645207e160ca | 174 | */ |
Lightvalve | 169:645207e160ca | 175 | if (HAL_PWREx_EnableOverDrive() != HAL_OK) { |
Lightvalve | 169:645207e160ca | 176 | //Error_Handler(); |
Lightvalve | 169:645207e160ca | 177 | } |
Lightvalve | 169:645207e160ca | 178 | /** Initializes the CPU, AHB and APB busses clocks |
Lightvalve | 169:645207e160ca | 179 | */ |
Lightvalve | 169:645207e160ca | 180 | RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |
Lightvalve | 169:645207e160ca | 181 | |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; |
Lightvalve | 169:645207e160ca | 182 | RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; |
Lightvalve | 169:645207e160ca | 183 | RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; |
Lightvalve | 169:645207e160ca | 184 | RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4; |
Lightvalve | 169:645207e160ca | 185 | RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; |
Lightvalve | 65:a2d7c63419c2 | 186 | |
Lightvalve | 169:645207e160ca | 187 | if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) { |
Lightvalve | 169:645207e160ca | 188 | //Error_Handler(); |
Lightvalve | 169:645207e160ca | 189 | } |
Lightvalve | 65:a2d7c63419c2 | 190 | } |
Lightvalve | 65:a2d7c63419c2 | 191 | |
Lightvalve | 99:7bbcb3c0fb06 | 192 | float u_past[num_array_u_past] = {0.0f}; |
Lightvalve | 68:328e1be06f5d | 193 | float x_past[num_array_x_past] = {0.0f}; |
Lightvalve | 85:a3b46118b5cd | 194 | float x_future[num_array_x_future] = {0.0f}; |
Lightvalve | 68:328e1be06f5d | 195 | float f_past[num_array_f_past] = {0.0f}; |
Lightvalve | 68:328e1be06f5d | 196 | float f_future[num_array_f_future] = {0.0f}; |
Lightvalve | 66:a8e6799dbce3 | 197 | |
Lightvalve | 73:f80dc3970c99 | 198 | float input_NN[num_input] = { 0.0f }; |
Lightvalve | 65:a2d7c63419c2 | 199 | |
Lightvalve | 169:645207e160ca | 200 | const float h1[num_input][16] = { |
Lightvalve | 199:ee79b4692e3b | 201 | {-2.1837844848632812f,2.0101635456085205f,0.04588095843791962f,-0.5758384466171265f,-2.3859260082244873f,2.2956244945526123f,1.8129379749298096f,1.5355861186981201f,-0.22093690931797028f,-2.2866008281707764f,0.8032577037811279f,-2.308483362197876f,-2.022364616394043f,-1.44899320602417f,-0.2571594715118408f,2.1746459007263184f}, |
Lightvalve | 199:ee79b4692e3b | 202 | {-1.2958452701568604f,1.0988526344299316f,-0.47577235102653503f,-0.19999195635318756f,-1.253292202949524f,1.2035913467407227f,0.16407740116119385f,-0.06658501923084259f,-0.47375917434692383f,-1.3384493589401245f,-0.3297125995159149f,-1.3080674409866333f,-0.06550247967243195f,-1.1850271224975586f,-0.20967772603034973f,1.4772138595581055f}, |
Lightvalve | 199:ee79b4692e3b | 203 | {-0.9654735326766968f,0.7904215455055237f,0.2810189127922058f,-0.8030968308448792f,-0.6013320088386536f,0.9044144153594971f,0.38279950618743896f,-0.35799551010131836f,-0.4297952651977539f,-0.605270266532898f,-0.19510450959205627f,-0.9962825775146484f,0.22925516963005066f,-0.08333431929349899f,0.05590221285820007f,0.581075131893158f}, |
Lightvalve | 199:ee79b4692e3b | 204 | {-0.38367339968681335f,0.14867372810840607f,0.3145712614059448f,-0.5230110287666321f,-0.14263394474983215f,-0.11347147822380066f,-0.11111853271722794f,0.1234128400683403f,0.2909422218799591f,0.46737194061279297f,0.8569537997245789f,-0.06507605314254761f,0.14980092644691467f,0.3622553050518036f,-0.2961459457874298f,0.11472762376070023f}, |
Lightvalve | 199:ee79b4692e3b | 205 | {1.2189306020736694f,-0.7193593978881836f,-0.08788592368364334f,-0.07936348766088486f,0.9448050260543823f,-1.0137641429901123f,-0.27093255519866943f,-0.020022014155983925f,0.6358230113983154f,1.3716148138046265f,0.8794701099395752f,0.8379188776016235f,0.45183658599853516f,0.9175698161125183f,-0.3865056037902832f,-1.2692279815673828f}, |
Lightvalve | 199:ee79b4692e3b | 206 | {3.590186834335327f,-3.4975996017456055f,0.1350027471780777f,2.3956410884857178f,3.5060923099517822f,-3.3447601795196533f,-1.807590126991272f,-1.1423075199127197f,0.1169700175523758f,2.501425266265869f,-2.12386155128479f,3.2425308227539062f,1.163952112197876f,1.4989044666290283f,-0.4132833778858185f,-2.9116435050964355f}, |
Lightvalve | 199:ee79b4692e3b | 207 | {-0.006196335889399052f,0.03741585835814476f,-0.2773531675338745f,-4.473310947418213f,-0.27634772658348083f,0.12931323051452637f,0.04171861708164215f,-3.9280078411102295f,3.5595314502716064f,-0.8166646361351013f,3.1788687705993652f,-0.24399122595787048f,-0.14087243378162384f,3.2437639236450195f,-0.15618428587913513f,-2.124624490737915f}, |
Lightvalve | 199:ee79b4692e3b | 208 | {-0.3596242070198059f,-0.171567901968956f,-0.35079166293144226f,-1.5157039165496826f,-0.5067394375801086f,-1.009583830833435f,4.817088603973389f,-0.4044037163257599f,-0.5577526688575745f,4.15724515914917f,2.5925800800323486f,-0.024581750854849815f,-2.805974245071411f,0.1435716301202774f,0.04303058981895447f,0.8803895711898804f}, |
Lightvalve | 199:ee79b4692e3b | 209 | {-0.11844680458307266f,0.7361156940460205f,0.021878330036997795f,-0.21332386136054993f,-0.1096244677901268f,-0.5460144877433777f,-1.9238289594650269f,0.47892892360687256f,-0.43380141258239746f,0.149037167429924f,1.3499506711959839f,0.0702681839466095f,2.0669760704040527f,1.0241925716400146f,-0.30555272102355957f,-0.980358898639679f}, |
Lightvalve | 199:ee79b4692e3b | 210 | {-0.02248750813305378f,-0.36836478114128113f,0.10568398237228394f,0.9146596193313599f,-0.09461541473865509f,0.10696340352296829f,-7.119680881500244f,-0.5392789840698242f,1.5476149320602417f,-0.5685486793518066f,-1.2822149991989136f,-0.6936250925064087f,5.557674407958984f,-0.6546996235847473f,0.3484981060028076f,-0.30507388710975647f}, |
Lightvalve | 199:ee79b4692e3b | 211 | {0.20626938343048096f,-0.1237199530005455f,0.2928805947303772f,0.5191757082939148f,0.1295856237411499f,0.30068445205688477f,3.9183223247528076f,-2.5037665367126465f,-2.3492307662963867f,-0.7124243974685669f,-0.8994795680046082f,-0.46030789613723755f,-3.5722081661224365f,1.8683465719223022f,0.26688337326049805f,-0.44005087018013f}, |
Lightvalve | 199:ee79b4692e3b | 212 | {-0.1235814169049263f,-0.31122681498527527f,-0.4815855920314789f,0.7879880666732788f,0.28719767928123474f,-0.08064402639865875f,0.6580206751823425f,-0.9771832823753357f,-0.07167230546474457f,-0.4789198040962219f,-0.7216929197311401f,0.22976653277873993f,-0.4228055477142334f,0.6537925004959106f,0.10493969917297363f,-0.35554176568984985f}, |
Lightvalve | 199:ee79b4692e3b | 213 | {-0.06496161222457886f,-0.01388863380998373f,0.1188524067401886f,0.5036965012550354f,0.1850457340478897f,-0.033558476716279984f,-0.004450216423720121f,-0.19332146644592285f,0.4412379264831543f,-0.12896977365016937f,-0.41583219170570374f,0.10597457736730576f,0.19178208708763123f,0.16101115942001343f,-0.22268100082874298f,-0.02980675920844078f}, |
Lightvalve | 199:ee79b4692e3b | 214 | {-0.03522493317723274f,-0.006083943881094456f,0.3326011002063751f,0.8511806726455688f,-0.12043089419603348f,-0.18758372962474823f,0.0751999244093895f,-0.3965107500553131f,0.9551570415496826f,-0.06834441423416138f,-0.6902320384979248f,0.23172923922538757f,-0.114925317466259f,-0.10242374241352081f,0.0479682981967926f,0.09168383479118347f}, |
Lightvalve | 199:ee79b4692e3b | 215 | {0.022258363664150238f,0.18318961560726166f,0.12307672202587128f,0.48128053545951843f,-0.029876722022891045f,-0.05051718279719353f,-0.03138022497296333f,0.24694964289665222f,0.18856319785118103f,-0.16514801979064941f,-0.39376768469810486f,-0.29822978377342224f,0.463131308555603f,0.18574166297912598f,0.0668090283870697f,-0.29248541593551636f}, |
Lightvalve | 199:ee79b4692e3b | 216 | {0.0049180155619978905f,-0.2213272899389267f,-0.483573853969574f,-0.2487296462059021f,-0.03675578907132149f,0.058383576571941376f,-0.43105602264404297f,-0.06576662510633469f,-0.6182113289833069f,0.0011734002036973834f,0.13351257145404816f,-0.008947508409619331f,-0.4899154603481293f,-0.21355555951595306f,-0.08245879411697388f,0.016924362629652023f}, |
Lightvalve | 199:ee79b4692e3b | 217 | {0.01701810397207737f,-0.03982752561569214f,0.08773337304592133f,0.9987277984619141f,-0.23670294880867004f,-0.003839959856122732f,0.5555870532989502f,-0.08025806397199631f,1.011113166809082f,0.003458167891949415f,-0.6364181637763977f,0.13058964908123016f,0.0927204042673111f,0.2622024714946747f,0.29427415132522583f,-0.36754608154296875f}, |
Lightvalve | 65:a2d7c63419c2 | 218 | }; |
Lightvalve | 65:a2d7c63419c2 | 219 | |
Lightvalve | 169:645207e160ca | 220 | const float h2[16][16] = { |
Lightvalve | 199:ee79b4692e3b | 221 | {-2.674283027648926f,-19.75362205505371f,-0.1445033997297287f,7.779426097869873f,-0.3419828712940216f,0.6111319661140442f,-11.97992992401123f,-0.06811906397342682f,-0.5635062456130981f,-0.14760400354862213f,5.501676082611084f,-3.258754253387451f,4.001481056213379f,-4.758901119232178f,-0.9312655925750732f,0.7366103529930115f}, |
Lightvalve | 199:ee79b4692e3b | 222 | {-0.7434616684913635f,3.092632293701172f,-0.05838218703866005f,-1.307464361190796f,-0.35503754019737244f,-0.2724722623825073f,1.319793462753296f,-0.5438210368156433f,-0.42702803015708923f,0.2895788848400116f,0.7404713034629822f,-0.7428285479545593f,-1.0050708055496216f,0.47870922088623047f,-0.018869943916797638f,-0.18738682568073273f}, |
Lightvalve | 199:ee79b4692e3b | 223 | {-0.22745239734649658f,0.045928705483675f,-0.061119019985198975f,0.3182114362716675f,0.05568113923072815f,-0.0237321425229311f,-0.20225946605205536f,-0.08465918898582458f,0.3489862382411957f,0.0687277615070343f,0.2845090329647064f,0.3004753887653351f,0.31974947452545166f,-0.41946619749069214f,0.10742761939764023f,-0.005538635421544313f}, |
Lightvalve | 199:ee79b4692e3b | 224 | {0.1112687960267067f,0.17640958726406097f,-0.2894435524940491f,-0.029508210718631744f,-0.04560476168990135f,1.5404884815216064f,-0.5512773394584656f,0.08028208464384079f,0.18367992341518402f,0.21083085238933563f,-0.84100741147995f,0.3499418795108795f,-0.8262040019035339f,-0.06061369925737381f,0.7440986037254333f,0.7937288880348206f}, |
Lightvalve | 199:ee79b4692e3b | 225 | {1.2113111019134521f,-1.9501395225524902f,-0.3431500196456909f,1.5768429040908813f,-0.09858585894107819f,0.9739106893539429f,-1.2162216901779175f,-0.4212343394756317f,-0.17827872931957245f,-0.5757948756217957f,1.239785075187683f,-0.7534657716751099f,1.118362545967102f,-2.2174103260040283f,-1.7887440919876099f,1.190489649772644f}, |
Lightvalve | 199:ee79b4692e3b | 226 | {0.1777067929506302f,-1.8983210325241089f,-0.45307424664497375f,-2.1425976753234863f,-0.3015052080154419f,3.18461275100708f,-18.18368911743164f,-0.26703914999961853f,-0.48995697498321533f,0.3579089343547821f,-0.0014333250001072884f,0.4553060531616211f,-0.9638712406158447f,1.2035548686981201f,1.1829919815063477f,-0.458189994096756f}, |
Lightvalve | 199:ee79b4692e3b | 227 | {-0.2140461951494217f,1.3666870594024658f,-0.3260969817638397f,-0.3568636476993561f,-0.38453540205955505f,-0.4754043519496918f,-4.1757025718688965f,-0.0017336906166747212f,-0.15257470309734344f,0.13111665844917297f,0.1802264153957367f,-3.16748309135437f,-0.41486334800720215f,0.4974520206451416f,0.6583006381988525f,-0.8409538269042969f}, |
Lightvalve | 199:ee79b4692e3b | 228 | {-3.7754693031311035f,-0.8927761912345886f,0.18919311463832855f,0.14834003150463104f,-0.27119147777557373f,-0.024219632148742676f,-4.014308452606201f,-0.4794021248817444f,0.03760408237576485f,-0.5122731924057007f,0.20397134125232697f,-14.069512367248535f,-2.336082696914673f,0.6071321964263916f,-0.05089356750249863f,0.2677140235900879f}, |
Lightvalve | 199:ee79b4692e3b | 229 | {-0.498852401971817f,-0.3680291175842285f,-0.24363061785697937f,0.15364482998847961f,-0.08847162872552872f,-0.010616462677717209f,0.8844281435012817f,-0.3258861303329468f,0.06103590130805969f,-0.38046833872795105f,-1.264649510383606f,-1.2231134176254272f,-0.5222742557525635f,0.17115530371665955f,-1.6105502843856812f,-1.0130584239959717f}, |
Lightvalve | 199:ee79b4692e3b | 230 | {-0.10957334190607071f,2.477494955062866f,-0.13502129912376404f,0.39086753129959106f,-0.0030171233229339123f,-0.7244225144386292f,-2.244135618209839f,-0.6697809100151062f,-0.24925290048122406f,-0.0661936029791832f,0.22846399247646332f,1.8263009786605835f,-0.25706198811531067f,0.16737937927246094f,-0.31483668088912964f,0.2415773868560791f}, |
Lightvalve | 199:ee79b4692e3b | 231 | {0.5647047162055969f,-2.1719229221343994f,-0.09408819675445557f,0.08505217730998993f,-0.03321319818496704f,-0.80712890625f,0.4001992642879486f,-0.40854141116142273f,-0.050184011459350586f,0.12480869889259338f,0.6780188679695129f,-0.27626320719718933f,0.48307815194129944f,0.9442746639251709f,-0.8515918850898743f,-1.0515382289886475f}, |
Lightvalve | 199:ee79b4692e3b | 232 | {0.414889395236969f,-1.0162369012832642f,0.2711864113807678f,0.6045194864273071f,-0.12988048791885376f,0.11452009528875351f,-0.13452677428722382f,-0.3723098933696747f,0.11079556494951248f,-0.3499957025051117f,0.5277608633041382f,0.43197011947631836f,-0.054778654128313065f,0.09976109862327576f,0.19868263602256775f,0.9474283456802368f}, |
Lightvalve | 199:ee79b4692e3b | 233 | {-1.251092791557312f,-2.3950695991516113f,-0.15798500180244446f,0.44822025299072266f,-0.5053848028182983f,0.7921834588050842f,2.1565396785736084f,-0.3741874396800995f,-0.4484594464302063f,0.11803500354290009f,-0.009846722707152367f,0.02426297776401043f,0.3284200429916382f,-0.278394490480423f,-0.4773232340812683f,-0.630092442035675f}, |
Lightvalve | 199:ee79b4692e3b | 234 | {0.4361220896244049f,0.039302293211221695f,-0.2018718123435974f,0.09616457670927048f,0.07545611262321472f,-0.4409101605415344f,-0.870441734790802f,-0.319128155708313f,-0.33705347776412964f,0.06882216781377792f,-0.4649412930011749f,1.0272188186645508f,1.4670559167861938f,0.0006485587218776345f,-0.9369511604309082f,-1.3251198530197144f}, |
Lightvalve | 179:d5377766d7ea | 235 | {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 | 199:ee79b4692e3b | 236 | {-2.1636977195739746f,1.3953567743301392f,-0.002055981196463108f,-0.37223538756370544f,0.164200097322464f,0.08797144889831543f,-8.68027400970459f,0.04972444847226143f,-0.5795959830284119f,-0.22661522030830383f,0.4256688058376312f,-5.334555149078369f,-2.306769847869873f,-0.09140407294034958f,1.1666741371154785f,0.2710472345352173f}, |
Lightvalve | 65:a2d7c63419c2 | 237 | }; |
Lightvalve | 65:a2d7c63419c2 | 238 | |
Lightvalve | 169:645207e160ca | 239 | const float h3[16][16] = { |
Lightvalve | 199:ee79b4692e3b | 240 | {-0.36079341173171997f,-3.9992425441741943f,-0.019920704886317253f,-3.7401278018951416f,0.35766279697418213f,0.7107656598091125f,0.15916572511196136f,-0.02236497588455677f,-0.6144473552703857f,-0.04320945963263512f,-0.11870327591896057f,0.0760134756565094f,-1.3407607078552246f,-0.35452646017074585f,0.8123229146003723f,0.6210054755210876f}, |
Lightvalve | 199:ee79b4692e3b | 241 | {0.047732532024383545f,-0.08606647700071335f,0.3044530749320984f,-0.5685889720916748f,-0.5859180688858032f,0.5168874859809875f,-0.3198729455471039f,-0.0616832859814167f,-0.7202454805374146f,-0.24511195719242096f,0.2780170738697052f,-0.3857530355453491f,0.6130155324935913f,-0.3128521144390106f,1.8281869888305664f,-1.2537225484848022f}, |
Lightvalve | 199:ee79b4692e3b | 242 | {0.07903262972831726f,0.2567390203475952f,-0.07798504829406738f,0.04248586297035217f,-0.1963958442211151f,-0.19260792434215546f,-0.4038352966308594f,0.015906542539596558f,0.15353140234947205f,0.030178606510162354f,0.2488909661769867f,0.13805970549583435f,-0.057077180594205856f,-0.235126793384552f,-0.3036302626132965f,0.08288257569074631f}, |
Lightvalve | 199:ee79b4692e3b | 243 | {-0.30922991037368774f,-0.5792930126190186f,-4.273653984069824f,1.1453341245651245f,-4.5254364013671875f,0.282318651676178f,0.059267595410346985f,-0.25690674781799316f,1.9092224836349487f,-0.7065148949623108f,-0.1480179727077484f,-0.08419273048639297f,-1.9301722049713135f,1.1525237560272217f,1.7244079113006592f,0.7795507311820984f}, |
Lightvalve | 199:ee79b4692e3b | 244 | {-0.39607733488082886f,0.04120592027902603f,0.1131146028637886f,0.022423356771469116f,0.09063010662794113f,-0.17578840255737305f,0.16012099385261536f,0.3626593053340912f,-0.17838427424430847f,-0.11053556203842163f,-0.10529157519340515f,-0.31317979097366333f,-0.23336799442768097f,-0.05953550338745117f,0.3211229145526886f,0.19986507296562195f}, |
Lightvalve | 199:ee79b4692e3b | 245 | {-0.37449589371681213f,-0.5102877020835876f,-2.3412106037139893f,-1.9704302549362183f,-0.718756377696991f,0.4018186628818512f,-0.3192301094532013f,-0.37182047963142395f,0.11803273111581802f,-0.37043070793151855f,-0.25327548384666443f,-0.23666924238204956f,-0.43877601623535156f,0.18859024345874786f,-1.599037766456604f,-0.23787960410118103f}, |
Lightvalve | 199:ee79b4692e3b | 246 | {0.4110594093799591f,0.3641268312931061f,-0.24094732105731964f,0.39241111278533936f,1.028276801109314f,0.09239723533391953f,-0.08670487999916077f,-0.25336313247680664f,-0.04216204956173897f,-0.06259563565254211f,-0.1344406008720398f,0.35313835740089417f,-2.1582298278808594f,0.9374165534973145f,0.3446822166442871f,-0.48981598019599915f}, |
Lightvalve | 199:ee79b4692e3b | 247 | {-0.40892091393470764f,-0.05935406684875488f,-0.2623085081577301f,0.24000565707683563f,0.4653308093547821f,-0.13498064875602722f,-0.23312048614025116f,-0.390264093875885f,0.1819913387298584f,-0.1559126079082489f,-0.14134526252746582f,-0.0003446042537689209f,-0.16760802268981934f,-0.46420595049858093f,-0.04577748477458954f,-0.024266792461276054f}, |
Lightvalve | 199:ee79b4692e3b | 248 | {0.10697010159492493f,0.18819847702980042f,-0.37870171666145325f,0.21184906363487244f,-0.37222859263420105f,0.013411798514425755f,-0.1382003128528595f,0.3493293821811676f,-0.360889196395874f,-0.3875247836112976f,0.42142823338508606f,-0.3482915461063385f,-0.3289247751235962f,-0.010594150982797146f,0.09620395302772522f,-0.06898030638694763f}, |
Lightvalve | 199:ee79b4692e3b | 249 | {0.2847062647342682f,0.018552124500274658f,0.11435768008232117f,0.36562982201576233f,-0.047046810388565063f,0.1384449154138565f,0.2430230677127838f,0.2909286320209503f,-0.2802048921585083f,0.18043199181556702f,0.41849127411842346f,-0.287167489528656f,0.24782028794288635f,-0.3141365051269531f,-0.10168051719665527f,0.010465055704116821f}, |
Lightvalve | 199:ee79b4692e3b | 250 | {0.15459725260734558f,0.7376908659934998f,0.28115618228912354f,-0.6975672245025635f,-0.5065810084342957f,-0.3017660677433014f,-0.08742031455039978f,-0.0630878359079361f,-0.9642277359962463f,-0.2220277339220047f,-0.020114075392484665f,-0.4160120189189911f,-0.6593955755233765f,-0.25608909130096436f,0.08111029863357544f,0.36404433846473694f}, |
Lightvalve | 199:ee79b4692e3b | 251 | {-0.408692330121994f,0.11722688376903534f,-2.6202144622802734f,1.1614540815353394f,0.04688110575079918f,0.8050221800804138f,-0.10356326401233673f,0.040458135306835175f,-1.7546080350875854f,-0.3304281532764435f,0.02475929632782936f,-0.40160971879959106f,1.5910406112670898f,-0.8608219027519226f,-0.8164851069450378f,0.849300742149353f}, |
Lightvalve | 199:ee79b4692e3b | 252 | {-0.2991822361946106f,-16.444902420043945f,-2.7731211185455322f,-9.340137481689453f,-0.21075564622879028f,-0.1905771791934967f,-0.29088878631591797f,0.1455707550048828f,0.9463095664978027f,-0.4181814193725586f,-0.044228196144104004f,-0.44137144088745117f,-3.0297417640686035f,0.3898687958717346f,-0.4988105893135071f,-1.0869044065475464f}, |
Lightvalve | 199:ee79b4692e3b | 253 | {-0.2675279378890991f,-0.6821674108505249f,-0.42561009526252747f,0.20795567333698273f,0.15388953685760498f,-0.2611711621284485f,-0.5058139562606812f,-0.7550562024116516f,-0.1124289333820343f,-0.8204157948493958f,-0.48899734020233154f,-0.7760970592498779f,0.9054858088493347f,-0.5096421241760254f,-0.6139748096466064f,0.4697928726673126f}, |
Lightvalve | 199:ee79b4692e3b | 254 | {-0.015470266342163086f,0.3406878411769867f,-0.34045615792274475f,0.7845275402069092f,0.12758617103099823f,-0.1876579076051712f,0.05196094512939453f,0.020785843953490257f,-5.119916915893555f,-0.5137317776679993f,0.1519099771976471f,-0.3556460440158844f,-1.3449710607528687f,-0.5167357921600342f,-8.097357749938965f,1.6800681352615356f}, |
Lightvalve | 199:ee79b4692e3b | 255 | {0.1863725483417511f,0.4552655518054962f,-1.4158952236175537f,-2.1374547481536865f,0.7595136761665344f,-0.04729481041431427f,0.04860696196556091f,0.06409500539302826f,1.3406081199645996f,0.18544192612171173f,-0.5370227098464966f,0.20714588463306427f,1.428971290588379f,-0.014740538783371449f,-0.4222463369369507f,-1.120627760887146f}, |
Lightvalve | 66:a8e6799dbce3 | 256 | }; |
Lightvalve | 65:a2d7c63419c2 | 257 | |
Lightvalve | 199:ee79b4692e3b | 258 | const float hout[16] = { 0.45773375034332275f,0.06918291747570038f,-0.12925687432289124f,-0.15688492357730865f,-0.09581243246793747f,0.12980027496814728f,0.05758979171514511f,-0.07756929844617844f,-0.07437136024236679f,0.088389091193676f,0.1648382991552353f,-0.1090453714132309f,-0.07618726044893265f,0.10542070865631104f,-0.10273952782154083f,-0.09904715418815613f }; |
Lightvalve | 199:ee79b4692e3b | 259 | |
Lightvalve | 199:ee79b4692e3b | 260 | const float b1[16] = { 0.3319792151451111f,0.38004234433174133f,-1.7917866706848145f,0.8671742081642151f,0.7934799790382385f,0.8670303821563721f,-0.651479184627533f,4.418480396270752f,-1.6263774633407593f,-0.5555577278137207f,-0.8048836588859558f,1.0582845211029053f,-0.5338819622993469f,-2.1279237270355225f,-1.087764859199524f,2.0707762241363525f }; |
Lightvalve | 199:ee79b4692e3b | 261 | |
Lightvalve | 199:ee79b4692e3b | 262 | const float b2[16] = { 1.2374228239059448f,1.128933072090149f,-1.5688477754592896f,-0.9324873685836792f,-0.827792227268219f,-0.05313319340348244f,0.6596731543540955f,-0.6443398594856262f,-1.352556824684143f,-2.093569755554199f,-0.12882685661315918f,-0.05392090231180191f,-1.7019206285476685f,1.365263819694519f,1.3740285634994507f,1.7110040187835693f }; |
Lightvalve | 199:ee79b4692e3b | 263 | |
Lightvalve | 199:ee79b4692e3b | 264 | const float b3[16] = { -1.963319182395935f,0.3110598921775818f,1.3809770345687866f,1.4498616456985474f,1.02727472782135f,0.8841015696525574f,-0.6412962675094604f,-1.1947987079620361f,-2.0401856899261475f,0.14284981787204742f,-0.3684021830558777f,-0.4421199858188629f,-1.098542332649231f,2.0177974700927734f,-1.0107595920562744f,-0.3846563398838043f }; |
Lightvalve | 199:ee79b4692e3b | 265 | |
Lightvalve | 199:ee79b4692e3b | 266 | const float bout[1] = { -0.2631767988204956f }; |
Lightvalve | 65:a2d7c63419c2 | 267 | |
Lightvalve | 197:50e95ba0ade2 | 268 | |
Lightvalve | 197:50e95ba0ade2 | 269 | float valve_ref_pos_buffer[10] = {0.0f}; |
Lightvalve | 197:50e95ba0ade2 | 270 | |
Lightvalve | 170:42c938a40313 | 271 | /////////////////////////////////////////////////////////////////////////////////////////////RL |
Lightvalve | 170:42c938a40313 | 272 | float input_RL[num_input_RL] = { 0.0f }; |
Lightvalve | 170:42c938a40313 | 273 | |
Lightvalve | 170:42c938a40313 | 274 | //Critic Networks |
Lightvalve | 173:68c7914679ec | 275 | float hc1[num_input_RL][num_hidden_unit1] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 276 | float bc1[num_hidden_unit1] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 277 | float hc2[num_hidden_unit1][num_hidden_unit2] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 278 | float bc2[num_hidden_unit2] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 279 | float hc3[num_hidden_unit2] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 280 | float bc3 = 0.0f; |
Lightvalve | 170:42c938a40313 | 281 | |
Lightvalve | 170:42c938a40313 | 282 | //Critic Networks Temporary |
Lightvalve | 173:68c7914679ec | 283 | float hc1_temp[num_input_RL][num_hidden_unit1] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 284 | float bc1_temp[num_hidden_unit1] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 285 | float hc2_temp[num_hidden_unit1][num_hidden_unit2] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 286 | float bc2_temp[num_hidden_unit2] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 287 | float hc3_temp[num_hidden_unit2] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 288 | float bc3_temp = 0.0f; |
Lightvalve | 170:42c938a40313 | 289 | |
Lightvalve | 170:42c938a40313 | 290 | //Actor Networks |
Lightvalve | 173:68c7914679ec | 291 | float ha1[num_input_RL][num_hidden_unit1] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 292 | float ba1[num_hidden_unit1] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 293 | float ha2[num_hidden_unit1][num_hidden_unit2] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 294 | float ba2[num_hidden_unit2] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 295 | float ha3[num_hidden_unit2][2] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 296 | float ba3[2] = {0.0f}; |
Lightvalve | 170:42c938a40313 | 297 | |
Lightvalve | 170:42c938a40313 | 298 | //Actor Networks Temporary |
Lightvalve | 173:68c7914679ec | 299 | float ha1_temp[num_input_RL][num_hidden_unit1] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 300 | float ba1_temp[num_hidden_unit1] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 301 | float ha2_temp[num_hidden_unit1][num_hidden_unit2] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 302 | float ba2_temp[num_hidden_unit2] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 303 | float ha3_temp[num_hidden_unit2][2] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 304 | float ba3_temp[2] = {0.0f}; |
Lightvalve | 66:a8e6799dbce3 | 305 | |
Lightvalve | 87:471334725012 | 306 | float VALVE_POS_RAW_NN = 0.0f; |
Lightvalve | 87:471334725012 | 307 | float DDV_JOINT_POS_FF(float REF_JOINT_VEL); |
Lightvalve | 61:bc8c8270f0ab | 308 | |
Lightvalve | 179:d5377766d7ea | 309 | /////////////////////////////////////////////RL tuning |
Lightvalve | 179:d5377766d7ea | 310 | float Gradient_Limit = 0.5f; |
Lightvalve | 179:d5377766d7ea | 311 | float gradient_rate_actor = 0.001f; |
Lightvalve | 179:d5377766d7ea | 312 | float gradient_rate_critic = 0.001f; |
Lightvalve | 179:d5377766d7ea | 313 | ////////////////////////////////////////////////////////////////////////////// |
Lightvalve | 170:42c938a40313 | 314 | |
Lightvalve | 170:42c938a40313 | 315 | float Critic_Network_Temp(float *arr) |
Lightvalve | 170:42c938a40313 | 316 | { |
Lightvalve | 173:68c7914679ec | 317 | float output1[num_hidden_unit1] = { 0.0f }; |
Lightvalve | 173:68c7914679ec | 318 | float output2[num_hidden_unit2] = { 0.0f }; |
Lightvalve | 170:42c938a40313 | 319 | float output = 0.0f; |
Lightvalve | 173:68c7914679ec | 320 | for (int index2 = 0; index2 < num_hidden_unit1; index2++) { |
Lightvalve | 170:42c938a40313 | 321 | for (int index1 = 0; index1 < num_input_RL; index1++) { |
Lightvalve | 170:42c938a40313 | 322 | output1[index2] = output1[index2] + hc1_temp[index1][index2] * arr[index1]; |
Lightvalve | 170:42c938a40313 | 323 | } |
Lightvalve | 173:68c7914679ec | 324 | //ReLU |
Lightvalve | 173:68c7914679ec | 325 | output1[index2] = output1[index2] + bc1_temp[index2]; |
Lightvalve | 173:68c7914679ec | 326 | hx_c_sum[index2] = output1[index2]; |
Lightvalve | 173:68c7914679ec | 327 | if (output1[index2] < 0) { |
Lightvalve | 173:68c7914679ec | 328 | output1[index2] = 0; |
Lightvalve | 173:68c7914679ec | 329 | } |
Lightvalve | 173:68c7914679ec | 330 | //tanh |
Lightvalve | 173:68c7914679ec | 331 | //output1[index2] = tanh(output1[index2] + bc1_temp[index2]); |
Lightvalve | 173:68c7914679ec | 332 | } |
Lightvalve | 173:68c7914679ec | 333 | for (int index2 = 0; index2 < num_hidden_unit2; index2++) { |
Lightvalve | 173:68c7914679ec | 334 | for (int index1 = 0; index1 < num_hidden_unit1; index1++) { |
Lightvalve | 174:c828479f53f9 | 335 | output2[index2] = output2[index2] + hc2_temp[index1][index2] * output1[index1]; |
Lightvalve | 173:68c7914679ec | 336 | } |
Lightvalve | 173:68c7914679ec | 337 | //ReLU |
Lightvalve | 173:68c7914679ec | 338 | output2[index2] = output2[index2] + bc2_temp[index2]; |
Lightvalve | 173:68c7914679ec | 339 | hxh_c_sum[index2] = output2[index2]; |
Lightvalve | 173:68c7914679ec | 340 | if (output2[index2] < 0) { |
Lightvalve | 173:68c7914679ec | 341 | output2[index2] = 0; |
Lightvalve | 173:68c7914679ec | 342 | } |
Lightvalve | 173:68c7914679ec | 343 | //tanh |
Lightvalve | 173:68c7914679ec | 344 | //output2[index2] = tanh(output2[index2] + bc2_temp[index2]); |
Lightvalve | 170:42c938a40313 | 345 | } |
Lightvalve | 170:42c938a40313 | 346 | for (int index2 = 0; index2 < 1; index2++) { |
Lightvalve | 173:68c7914679ec | 347 | for (int index1 = 0; index1 < num_hidden_unit2; index1++) { |
Lightvalve | 173:68c7914679ec | 348 | output = output + hc3_temp[index1] * output2[index1]; |
Lightvalve | 170:42c938a40313 | 349 | } |
Lightvalve | 173:68c7914679ec | 350 | output = output + bc3_temp; |
Lightvalve | 173:68c7914679ec | 351 | hxhh_c_sum = output; |
Lightvalve | 170:42c938a40313 | 352 | } |
Lightvalve | 170:42c938a40313 | 353 | return output; |
Lightvalve | 170:42c938a40313 | 354 | } |
Lightvalve | 170:42c938a40313 | 355 | |
Lightvalve | 170:42c938a40313 | 356 | |
Lightvalve | 170:42c938a40313 | 357 | void Actor_Network(float *arr) |
Lightvalve | 170:42c938a40313 | 358 | { |
Lightvalve | 173:68c7914679ec | 359 | float output1[num_hidden_unit1] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 360 | float output2[num_hidden_unit2] = {0.0f}; |
Lightvalve | 170:42c938a40313 | 361 | float output[2] = {0.0f}; |
Lightvalve | 170:42c938a40313 | 362 | |
Lightvalve | 173:68c7914679ec | 363 | for (int index2 = 0; index2 < num_hidden_unit1; index2++) { |
Lightvalve | 173:68c7914679ec | 364 | for (int index1 = 0; index1 < num_input_RL; index1++) { |
Lightvalve | 178:1074553d2f6f | 365 | output1[index2] = output1[index2] + ha1_temp[index1][index2] * arr[index1]; |
Lightvalve | 173:68c7914679ec | 366 | } |
Lightvalve | 178:1074553d2f6f | 367 | output1[index2] = output1[index2] + ba1_temp[index2]; |
Lightvalve | 173:68c7914679ec | 368 | hx_a_sum[index2] = output1[index2]; |
Lightvalve | 173:68c7914679ec | 369 | if (output1[index2] < 0) { |
Lightvalve | 173:68c7914679ec | 370 | output1[index2] = 0; |
Lightvalve | 173:68c7914679ec | 371 | } |
Lightvalve | 173:68c7914679ec | 372 | } |
Lightvalve | 173:68c7914679ec | 373 | for (int index2 = 0; index2 < num_hidden_unit2; index2++) { |
Lightvalve | 173:68c7914679ec | 374 | for (int index1 = 0; index1 < num_hidden_unit1; index1++) { |
Lightvalve | 178:1074553d2f6f | 375 | output2[index2] = output2[index2] + ha2_temp[index1][index2] * output1[index1]; |
Lightvalve | 173:68c7914679ec | 376 | } |
Lightvalve | 178:1074553d2f6f | 377 | output2[index2] = output2[index2] + ba2_temp[index2]; |
Lightvalve | 173:68c7914679ec | 378 | hxh_a_sum[index2] = output2[index2]; |
Lightvalve | 173:68c7914679ec | 379 | if (output2[index2] < 0) { |
Lightvalve | 173:68c7914679ec | 380 | output2[index2] = 0; |
Lightvalve | 173:68c7914679ec | 381 | } |
Lightvalve | 173:68c7914679ec | 382 | } |
Lightvalve | 173:68c7914679ec | 383 | for (int index2 = 0; index2 < 2; index2++) { |
Lightvalve | 173:68c7914679ec | 384 | for (int index1 = 0; index1 < num_hidden_unit2; index1++) { |
Lightvalve | 178:1074553d2f6f | 385 | output[index2] = output[index2] + ha3_temp[index1][index2] * output2[index1]; |
Lightvalve | 173:68c7914679ec | 386 | } |
Lightvalve | 178:1074553d2f6f | 387 | hxhh_a_sum[index2] = output[index2] + ba3_temp[index2]; |
Lightvalve | 173:68c7914679ec | 388 | } |
Lightvalve | 178:1074553d2f6f | 389 | |
Lightvalve | 178:1074553d2f6f | 390 | mean_before_SP = output[0] + ba3_temp[0]; //SP = softplus |
Lightvalve | 178:1074553d2f6f | 391 | deviation_before_SP = output[1] + ba3_temp[1]; |
Lightvalve | 173:68c7914679ec | 392 | //Softplus |
Lightvalve | 173:68c7914679ec | 393 | mean = log(1.0f+exp(mean_before_SP)); |
Lightvalve | 173:68c7914679ec | 394 | deviation = log(1.0f+exp(deviation_before_SP)); |
Lightvalve | 179:d5377766d7ea | 395 | logging2 = mean; |
Lightvalve | 179:d5377766d7ea | 396 | logging4 = deviation; |
Lightvalve | 173:68c7914679ec | 397 | } |
Lightvalve | 173:68c7914679ec | 398 | |
Lightvalve | 173:68c7914679ec | 399 | |
Lightvalve | 173:68c7914679ec | 400 | void Actor_Network_Old(float *arr) |
Lightvalve | 173:68c7914679ec | 401 | { |
Lightvalve | 173:68c7914679ec | 402 | float output1[num_hidden_unit1] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 403 | float output2[num_hidden_unit2] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 404 | float output[2] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 405 | |
Lightvalve | 173:68c7914679ec | 406 | for (int index2 = 0; index2 < num_hidden_unit1; index2++) { |
Lightvalve | 170:42c938a40313 | 407 | for (int index1 = 0; index1 < num_input_RL; index1++) { |
Lightvalve | 170:42c938a40313 | 408 | output1[index2] = output1[index2] + ha1[index1][index2] * arr[index1]; |
Lightvalve | 170:42c938a40313 | 409 | } |
Lightvalve | 170:42c938a40313 | 410 | output1[index2] = output1[index2] + ba1[index2]; |
Lightvalve | 170:42c938a40313 | 411 | if (output1[index2] < 0) { |
Lightvalve | 170:42c938a40313 | 412 | output1[index2] = 0; |
Lightvalve | 170:42c938a40313 | 413 | } |
Lightvalve | 170:42c938a40313 | 414 | } |
Lightvalve | 173:68c7914679ec | 415 | for (int index2 = 0; index2 < num_hidden_unit2; index2++) { |
Lightvalve | 173:68c7914679ec | 416 | for (int index1 = 0; index1 < num_hidden_unit1; index1++) { |
Lightvalve | 174:c828479f53f9 | 417 | output2[index2] = output2[index2] + ha2[index1][index2] * output1[index1]; |
Lightvalve | 170:42c938a40313 | 418 | } |
Lightvalve | 173:68c7914679ec | 419 | output2[index2] = output2[index2] + ba2[index2]; |
Lightvalve | 173:68c7914679ec | 420 | if (output2[index2] < 0) { |
Lightvalve | 173:68c7914679ec | 421 | output2[index2] = 0; |
Lightvalve | 170:42c938a40313 | 422 | } |
Lightvalve | 170:42c938a40313 | 423 | } |
Lightvalve | 170:42c938a40313 | 424 | for (int index2 = 0; index2 < 2; index2++) { |
Lightvalve | 173:68c7914679ec | 425 | for (int index1 = 0; index1 < num_hidden_unit2; index1++) { |
Lightvalve | 173:68c7914679ec | 426 | output[index2] = output[index2] + ha3[index1][index2] * output2[index1]; |
Lightvalve | 170:42c938a40313 | 427 | } |
Lightvalve | 170:42c938a40313 | 428 | } |
Lightvalve | 173:68c7914679ec | 429 | mean_old = output[0] + ba3[0]; |
Lightvalve | 173:68c7914679ec | 430 | deviation_old = output[1] + ba3[1]; |
Lightvalve | 173:68c7914679ec | 431 | //Softplus |
Lightvalve | 173:68c7914679ec | 432 | mean_old = log(1.0f+exp(mean_old)); |
Lightvalve | 173:68c7914679ec | 433 | deviation_old = log(1.0f+exp(deviation_old)); |
Lightvalve | 170:42c938a40313 | 434 | } |
Lightvalve | 170:42c938a40313 | 435 | |
Lightvalve | 170:42c938a40313 | 436 | float Grad_Normal_Dist_Mean(float mean, float deviation, float action) |
Lightvalve | 170:42c938a40313 | 437 | { |
Lightvalve | 170:42c938a40313 | 438 | float grad_mean = 0.0f; |
Lightvalve | 170:42c938a40313 | 439 | grad_mean = (action-mean)*exp(-(action-mean)*(action-mean)/(2.0f*deviation*deviation))/(sqrt(2.0f*PI)*deviation*deviation*deviation); |
Lightvalve | 170:42c938a40313 | 440 | return grad_mean; |
Lightvalve | 170:42c938a40313 | 441 | } |
Lightvalve | 170:42c938a40313 | 442 | |
Lightvalve | 170:42c938a40313 | 443 | float Grad_Normal_Dist_Deviation(float mean, float deviation, float action) |
Lightvalve | 170:42c938a40313 | 444 | { |
Lightvalve | 170:42c938a40313 | 445 | float grad_dev = 0.0f; |
Lightvalve | 170:42c938a40313 | 446 | 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 | 447 | return grad_dev; |
Lightvalve | 170:42c938a40313 | 448 | } |
Lightvalve | 170:42c938a40313 | 449 | |
Lightvalve | 173:68c7914679ec | 450 | float ReLU(float x) |
Lightvalve | 173:68c7914679ec | 451 | { |
Lightvalve | 173:68c7914679ec | 452 | if (x >= 0) { |
Lightvalve | 173:68c7914679ec | 453 | return x; |
Lightvalve | 173:68c7914679ec | 454 | } else { |
Lightvalve | 173:68c7914679ec | 455 | return 0.0f; |
Lightvalve | 173:68c7914679ec | 456 | } |
Lightvalve | 173:68c7914679ec | 457 | } |
Lightvalve | 173:68c7914679ec | 458 | |
Lightvalve | 170:42c938a40313 | 459 | void update_Critic_Networks(float (*arr)[num_input_RL]) |
Lightvalve | 170:42c938a40313 | 460 | { |
Lightvalve | 173:68c7914679ec | 461 | float G_hc1[num_input_RL][num_hidden_unit1] = {0.0f}; |
Lightvalve | 177:8e9cf31d63f4 | 462 | float G_bc1[num_hidden_unit1] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 463 | for (int index2 = 0; index2 < num_hidden_unit1; index2++) { |
Lightvalve | 170:42c938a40313 | 464 | for (int index1 = 0; index1 < num_input_RL; index1++) { |
Lightvalve | 173:68c7914679ec | 465 | for (int n=0; n<batch_size; n++) { |
Lightvalve | 177:8e9cf31d63f4 | 466 | float d_V_d_hc1 = 0.0f; |
Lightvalve | 173:68c7914679ec | 467 | for(int k=0; k<num_hidden_unit2; k++) { |
Lightvalve | 173:68c7914679ec | 468 | if (hxh_c_sum_array[n][k] >= 0) { |
Lightvalve | 173:68c7914679ec | 469 | if (hx_c_sum_array[n][index2] > 0) { |
Lightvalve | 177:8e9cf31d63f4 | 470 | d_V_d_hc1 = d_V_d_hc1 + arr[n][index1]*hc2_temp[index2][k]*hc3_temp[k]; |
Lightvalve | 173:68c7914679ec | 471 | } |
Lightvalve | 173:68c7914679ec | 472 | } |
Lightvalve | 170:42c938a40313 | 473 | } |
Lightvalve | 177:8e9cf31d63f4 | 474 | G_hc1[index1][index2] = G_hc1[index1][index2] + 2.0f*(return_G[n]-V[n])*(-d_V_d_hc1); |
Lightvalve | 170:42c938a40313 | 475 | } |
Lightvalve | 170:42c938a40313 | 476 | G_hc1[index1][index2] = G_hc1[index1][index2] / batch_size; |
Lightvalve | 179:d5377766d7ea | 477 | if(G_hc1[index1][index2] > Gradient_Limit) G_hc1[index1][index2] = Gradient_Limit; |
Lightvalve | 179:d5377766d7ea | 478 | else if (G_hc1[index1][index2] < -Gradient_Limit) G_hc1[index1][index2] = -Gradient_Limit; |
Lightvalve | 179:d5377766d7ea | 479 | //hc1_temp[index1][index2] = hc1_temp[index1][index2] - gradient_rate_critic * G_hc1[index1][index2]; |
Lightvalve | 173:68c7914679ec | 480 | } |
Lightvalve | 173:68c7914679ec | 481 | for (int n=0; n<batch_size; n++) { |
Lightvalve | 177:8e9cf31d63f4 | 482 | float d_V_d_bc1 = 0.0f; |
Lightvalve | 173:68c7914679ec | 483 | for(int k=0; k<num_hidden_unit2; k++) { |
Lightvalve | 173:68c7914679ec | 484 | if (hxh_c_sum_array[n][k] >= 0) { |
Lightvalve | 173:68c7914679ec | 485 | if (hx_c_sum_array[n][index2] > 0) { |
Lightvalve | 177:8e9cf31d63f4 | 486 | d_V_d_bc1 = d_V_d_bc1 + hc2_temp[index2][k]*hc3_temp[k]; |
Lightvalve | 173:68c7914679ec | 487 | } |
Lightvalve | 173:68c7914679ec | 488 | } |
Lightvalve | 173:68c7914679ec | 489 | } |
Lightvalve | 177:8e9cf31d63f4 | 490 | G_bc1[index2] = G_bc1[index2] + 2.0f*(return_G[n]-V[n])*(-d_V_d_bc1); |
Lightvalve | 173:68c7914679ec | 491 | } |
Lightvalve | 173:68c7914679ec | 492 | G_bc1[index2] = G_bc1[index2] / batch_size; |
Lightvalve | 179:d5377766d7ea | 493 | if(G_bc1[index2] > Gradient_Limit) G_bc1[index2] = Gradient_Limit; |
Lightvalve | 179:d5377766d7ea | 494 | else if (G_bc1[index2] < -Gradient_Limit) G_bc1[index2] = -Gradient_Limit; |
Lightvalve | 179:d5377766d7ea | 495 | //bc1_temp[index2] = bc1_temp[index2] - gradient_rate_critic * G_bc1[index2]; |
Lightvalve | 173:68c7914679ec | 496 | } |
Lightvalve | 173:68c7914679ec | 497 | |
Lightvalve | 175:2f7289dbd488 | 498 | |
Lightvalve | 173:68c7914679ec | 499 | float G_hc2[num_hidden_unit1][num_hidden_unit2] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 500 | float G_bc2[num_hidden_unit2] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 501 | for (int index2 = 0; index2 < num_hidden_unit2; index2++) { |
Lightvalve | 173:68c7914679ec | 502 | for (int index1 = 0; index1 < num_hidden_unit1; index1++) { |
Lightvalve | 173:68c7914679ec | 503 | for (int n=0; n<batch_size; n++) { |
Lightvalve | 177:8e9cf31d63f4 | 504 | float d_V_d_hc2 = 0.0f; |
Lightvalve | 173:68c7914679ec | 505 | if (hxh_c_sum_array[n][index2] >= 0) { |
Lightvalve | 173:68c7914679ec | 506 | if (hx_c_sum_array[n][index1] > 0) { |
Lightvalve | 177:8e9cf31d63f4 | 507 | d_V_d_hc2 = hx_c_sum_array[n][index1]*hc3_temp[index2]; |
Lightvalve | 173:68c7914679ec | 508 | } |
Lightvalve | 173:68c7914679ec | 509 | } |
Lightvalve | 177:8e9cf31d63f4 | 510 | G_hc2[index1][index2] = G_hc2[index1][index2] + 2.0f*(return_G[n]-V[n])*(-d_V_d_hc2); |
Lightvalve | 173:68c7914679ec | 511 | } |
Lightvalve | 173:68c7914679ec | 512 | G_hc2[index1][index2] = G_hc2[index1][index2] / batch_size; |
Lightvalve | 179:d5377766d7ea | 513 | if(G_hc2[index1][index2] > Gradient_Limit) G_hc2[index1][index2] = Gradient_Limit; |
Lightvalve | 179:d5377766d7ea | 514 | else if (G_hc2[index1][index2] < -Gradient_Limit) G_hc2[index1][index2] = -Gradient_Limit; |
Lightvalve | 179:d5377766d7ea | 515 | //hc2_temp[index1][index2] = hc2_temp[index1][index2] - gradient_rate_critic * G_hc2[index1][index2]; |
Lightvalve | 173:68c7914679ec | 516 | } |
Lightvalve | 173:68c7914679ec | 517 | for (int n=0; n<batch_size; n++) { |
Lightvalve | 177:8e9cf31d63f4 | 518 | float d_V_d_bc2 = 0.0f; |
Lightvalve | 173:68c7914679ec | 519 | if (hxh_c_sum_array[n][index2] >= 0) { |
Lightvalve | 177:8e9cf31d63f4 | 520 | d_V_d_bc2 = hc3_temp[index2]; |
Lightvalve | 173:68c7914679ec | 521 | } |
Lightvalve | 177:8e9cf31d63f4 | 522 | G_bc2[index2] = G_bc2[index2] + 2.0f*(return_G[n]-V[n])*(-d_V_d_bc2); |
Lightvalve | 173:68c7914679ec | 523 | } |
Lightvalve | 173:68c7914679ec | 524 | G_bc2[index2] = G_bc2[index2] / batch_size; |
Lightvalve | 179:d5377766d7ea | 525 | if(G_bc2[index2] > Gradient_Limit) G_bc2[index2] = Gradient_Limit; |
Lightvalve | 179:d5377766d7ea | 526 | else if (G_bc2[index2] < -Gradient_Limit) G_bc2[index2] = -Gradient_Limit; |
Lightvalve | 179:d5377766d7ea | 527 | //bc2_temp[index2] = bc2_temp[index2] - gradient_rate_critic * G_bc2[index2]; |
Lightvalve | 173:68c7914679ec | 528 | } |
Lightvalve | 173:68c7914679ec | 529 | |
Lightvalve | 173:68c7914679ec | 530 | float G_hc3[num_hidden_unit2]= {0.0f}; |
Lightvalve | 173:68c7914679ec | 531 | float G_bc3 = 0.0f; |
Lightvalve | 173:68c7914679ec | 532 | for (int index2 = 0; index2 < 1; index2++) { |
Lightvalve | 173:68c7914679ec | 533 | for (int index1 = 0; index1 < num_hidden_unit2; index1++) { |
Lightvalve | 173:68c7914679ec | 534 | for (int n=0; n<batch_size; n++) { |
Lightvalve | 177:8e9cf31d63f4 | 535 | float d_V_d_hc3 = 0.0f; |
Lightvalve | 173:68c7914679ec | 536 | if (hxh_c_sum_array[n][index1] >= 0) { |
Lightvalve | 177:8e9cf31d63f4 | 537 | d_V_d_hc3 = d_V_d_hc3 + hxh_c_sum_array[n][index1]; |
Lightvalve | 173:68c7914679ec | 538 | } |
Lightvalve | 177:8e9cf31d63f4 | 539 | G_hc3[index1] = G_hc3[index1] + 2.0f*(return_G[n]-V[n])*(-d_V_d_hc3); |
Lightvalve | 173:68c7914679ec | 540 | } |
Lightvalve | 173:68c7914679ec | 541 | G_hc3[index1] = G_hc3[index1] / batch_size; |
Lightvalve | 179:d5377766d7ea | 542 | if(G_hc3[index1] > Gradient_Limit) G_hc3[index1] = Gradient_Limit; |
Lightvalve | 179:d5377766d7ea | 543 | else if (G_hc3[index1] < -Gradient_Limit) G_hc3[index1] = -Gradient_Limit; |
Lightvalve | 179:d5377766d7ea | 544 | //hc3_temp[index1] = hc3_temp[index1] - gradient_rate_critic * G_hc3[index1]; |
Lightvalve | 173:68c7914679ec | 545 | } |
Lightvalve | 173:68c7914679ec | 546 | for (int n=0; n<batch_size; n++) { |
Lightvalve | 177:8e9cf31d63f4 | 547 | float d_V_d_bc3 = 0.0f; |
Lightvalve | 177:8e9cf31d63f4 | 548 | d_V_d_bc3 = 1.0f; |
Lightvalve | 177:8e9cf31d63f4 | 549 | G_bc3 = G_bc3 + 2.0f*(return_G[n]-V[n])*(-d_V_d_bc3); |
Lightvalve | 173:68c7914679ec | 550 | } |
Lightvalve | 173:68c7914679ec | 551 | G_bc3 = G_bc3 / batch_size; |
Lightvalve | 179:d5377766d7ea | 552 | if(G_bc3 > Gradient_Limit) G_bc3 = Gradient_Limit; |
Lightvalve | 179:d5377766d7ea | 553 | else if (G_bc3 < -Gradient_Limit) G_bc3 = -Gradient_Limit; |
Lightvalve | 179:d5377766d7ea | 554 | //bc3_temp = bc3_temp - gradient_rate_critic * G_bc3; |
Lightvalve | 173:68c7914679ec | 555 | } |
Lightvalve | 173:68c7914679ec | 556 | |
Lightvalve | 173:68c7914679ec | 557 | // Simultaneous Update |
Lightvalve | 173:68c7914679ec | 558 | for (int index2 = 0; index2 < num_hidden_unit1; index2++) { |
Lightvalve | 173:68c7914679ec | 559 | for (int index1 = 0; index1 < num_input_RL; index1++) { |
Lightvalve | 179:d5377766d7ea | 560 | hc1_temp[index1][index2] = hc1_temp[index1][index2] - gradient_rate_critic * G_hc1[index1][index2]; |
Lightvalve | 170:42c938a40313 | 561 | } |
Lightvalve | 179:d5377766d7ea | 562 | bc1_temp[index2] = bc1_temp[index2] - gradient_rate_critic * G_bc1[index2]; |
Lightvalve | 170:42c938a40313 | 563 | } |
Lightvalve | 173:68c7914679ec | 564 | for (int index2 = 0; index2 < num_hidden_unit2; index2++) { |
Lightvalve | 173:68c7914679ec | 565 | for (int index1 = 0; index1 < num_hidden_unit1; index1++) { |
Lightvalve | 179:d5377766d7ea | 566 | hc2_temp[index1][index2] = hc2_temp[index1][index2] - gradient_rate_critic * G_hc2[index1][index2]; |
Lightvalve | 173:68c7914679ec | 567 | } |
Lightvalve | 179:d5377766d7ea | 568 | bc2_temp[index2] = bc2_temp[index2] - gradient_rate_critic * G_bc2[index2]; |
Lightvalve | 173:68c7914679ec | 569 | } |
Lightvalve | 170:42c938a40313 | 570 | for (int index2 = 0; index2 < 1; index2++) { |
Lightvalve | 173:68c7914679ec | 571 | for (int index1 = 0; index1 < num_hidden_unit2; index1++) { |
Lightvalve | 179:d5377766d7ea | 572 | hc3_temp[index1] = hc3_temp[index1] - gradient_rate_critic * G_hc3[index1]; |
Lightvalve | 170:42c938a40313 | 573 | } |
Lightvalve | 179:d5377766d7ea | 574 | bc3_temp = bc3_temp - gradient_rate_critic * G_bc3; |
Lightvalve | 170:42c938a40313 | 575 | } |
Lightvalve | 170:42c938a40313 | 576 | } |
Lightvalve | 170:42c938a40313 | 577 | |
Lightvalve | 173:68c7914679ec | 578 | ///////////////////////////Softplus////////////////////////////////// |
Lightvalve | 170:42c938a40313 | 579 | void update_Actor_Networks(float (*arr)[num_input_RL]) |
Lightvalve | 170:42c938a40313 | 580 | { |
Lightvalve | 179:d5377766d7ea | 581 | |
Lightvalve | 173:68c7914679ec | 582 | |
Lightvalve | 173:68c7914679ec | 583 | float G_ha1[num_input_RL][num_hidden_unit1] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 584 | float G_ba1[num_hidden_unit1] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 585 | |
Lightvalve | 173:68c7914679ec | 586 | for (int index2 = 0; index2 < num_hidden_unit1; index2++) { |
Lightvalve | 170:42c938a40313 | 587 | for (int index1 = 0; index1 < num_input_RL; index1++) { |
Lightvalve | 173:68c7914679ec | 588 | for (int n=0; n<batch_size; n++) { |
Lightvalve | 177:8e9cf31d63f4 | 589 | float d_x_d_ha1 = 0.0f; |
Lightvalve | 177:8e9cf31d63f4 | 590 | float d_y_d_ha1 = 0.0f; |
Lightvalve | 173:68c7914679ec | 591 | if((advantage[n] >= 0.0f && ratio[n] >= 1.0f + epsilon) || (advantage[n] < 0.0f && ratio[n] < 1.0f - epsilon)) { |
Lightvalve | 170:42c938a40313 | 592 | G_ha1[index1][index2] = G_ha1[index1][index2]; |
Lightvalve | 170:42c938a40313 | 593 | } else { |
Lightvalve | 173:68c7914679ec | 594 | for(int k=0; k<num_hidden_unit2; k++) { |
Lightvalve | 173:68c7914679ec | 595 | if (hxh_a_sum_array[n][k] >= 0) { |
Lightvalve | 173:68c7914679ec | 596 | if (hx_a_sum_array[n][index2] > 0) { |
Lightvalve | 177:8e9cf31d63f4 | 597 | d_x_d_ha1 = d_x_d_ha1 + arr[n][index1]*ha2_temp[index2][k]*ha3_temp[k][0]; |
Lightvalve | 177:8e9cf31d63f4 | 598 | d_y_d_ha1 = d_y_d_ha1 + arr[n][index1]*ha2_temp[index2][k]*ha3_temp[k][1]; |
Lightvalve | 173:68c7914679ec | 599 | } |
Lightvalve | 171:bfc1fd2629d8 | 600 | } |
Lightvalve | 170:42c938a40313 | 601 | } |
Lightvalve | 170:42c938a40313 | 602 | float d_mean_d_ha1 = 0.0f; |
Lightvalve | 170:42c938a40313 | 603 | float d_dev_d_ha1 = 0.0f; |
Lightvalve | 177:8e9cf31d63f4 | 604 | d_mean_d_ha1 = exp(hxhh_a_sum_array[n][0])/(1.0f+exp(hxhh_a_sum_array[n][0]))*d_x_d_ha1; |
Lightvalve | 177:8e9cf31d63f4 | 605 | d_dev_d_ha1 = exp(hxhh_a_sum_array[n][1])/(1.0f+exp(hxhh_a_sum_array[n][1]))*d_y_d_ha1; |
Lightvalve | 173:68c7914679ec | 606 | |
Lightvalve | 173:68c7914679ec | 607 | 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 | 608 | } |
Lightvalve | 170:42c938a40313 | 609 | } |
Lightvalve | 176:589ea3edcf3c | 610 | G_ha1[index1][index2] = -G_ha1[index1][index2] / batch_size; |
Lightvalve | 179:d5377766d7ea | 611 | if(G_ha1[index1][index2] > Gradient_Limit) G_ha1[index1][index2] = Gradient_Limit; |
Lightvalve | 179:d5377766d7ea | 612 | else if (G_ha1[index1][index2] < -Gradient_Limit) G_ha1[index1][index2] = -Gradient_Limit; |
Lightvalve | 179:d5377766d7ea | 613 | //ha1_temp[index1][index2] = ha1_temp[index1][index2] - gradient_rate_actor * G_ha1[index1][index2]; |
Lightvalve | 170:42c938a40313 | 614 | } |
Lightvalve | 173:68c7914679ec | 615 | |
Lightvalve | 173:68c7914679ec | 616 | for (int n=0; n<batch_size; n++) { |
Lightvalve | 177:8e9cf31d63f4 | 617 | float d_x_d_ba1 = 0.0f; |
Lightvalve | 177:8e9cf31d63f4 | 618 | float d_y_d_ba1 = 0.0f; |
Lightvalve | 173:68c7914679ec | 619 | if((advantage[n] >= 0.0f && ratio[n] >= 1.0f + epsilon) || (advantage[n] < 0.0f && ratio[n] < 1.0f - epsilon)) { |
Lightvalve | 170:42c938a40313 | 620 | G_ba1[index2] = G_ba1[index2]; |
Lightvalve | 170:42c938a40313 | 621 | } else { |
Lightvalve | 173:68c7914679ec | 622 | for(int k=0; k<num_hidden_unit2; k++) { |
Lightvalve | 173:68c7914679ec | 623 | if (hxh_a_sum_array[n][k] >= 0) { |
Lightvalve | 173:68c7914679ec | 624 | if (hx_a_sum_array[n][index2] > 0) { |
Lightvalve | 177:8e9cf31d63f4 | 625 | d_x_d_ba1 = d_x_d_ba1 + ha2_temp[index2][k]*ha3_temp[k][0]; |
Lightvalve | 177:8e9cf31d63f4 | 626 | d_y_d_ba1 = d_y_d_ba1 + ha2_temp[index2][k]*ha3_temp[k][1]; |
Lightvalve | 173:68c7914679ec | 627 | } |
Lightvalve | 171:bfc1fd2629d8 | 628 | } |
Lightvalve | 170:42c938a40313 | 629 | } |
Lightvalve | 170:42c938a40313 | 630 | float d_mean_d_ba1 = 0.0f; |
Lightvalve | 170:42c938a40313 | 631 | float d_dev_d_ba1 = 0.0f; |
Lightvalve | 177:8e9cf31d63f4 | 632 | d_mean_d_ba1 = exp(hxhh_a_sum_array[n][0])/(1.0f+exp(hxhh_a_sum_array[n][0]))*d_x_d_ba1; |
Lightvalve | 177:8e9cf31d63f4 | 633 | d_dev_d_ba1 = exp(hxhh_a_sum_array[n][1])/(1.0f+exp(hxhh_a_sum_array[n][1]))*d_y_d_ba1; |
Lightvalve | 173:68c7914679ec | 634 | |
Lightvalve | 173:68c7914679ec | 635 | 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 | 636 | } |
Lightvalve | 170:42c938a40313 | 637 | } |
Lightvalve | 176:589ea3edcf3c | 638 | G_ba1[index2] = -G_ba1[index2] / batch_size; |
Lightvalve | 179:d5377766d7ea | 639 | if(G_ba1[index2] > Gradient_Limit) G_ba1[index2] = Gradient_Limit; |
Lightvalve | 179:d5377766d7ea | 640 | else if (G_ba1[index2] < -Gradient_Limit) G_ba1[index2] = -Gradient_Limit; |
Lightvalve | 179:d5377766d7ea | 641 | //ba1_temp[index2] = ba1_temp[index2] - gradient_rate_actor * G_ba1[index2]; |
Lightvalve | 170:42c938a40313 | 642 | } |
Lightvalve | 170:42c938a40313 | 643 | |
Lightvalve | 173:68c7914679ec | 644 | float G_ha2[num_hidden_unit1][num_hidden_unit2] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 645 | float G_ba2[num_hidden_unit2] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 646 | |
Lightvalve | 173:68c7914679ec | 647 | for (int index2 = 0; index2 < num_hidden_unit2; index2++) { |
Lightvalve | 173:68c7914679ec | 648 | for (int index1 = 0; index1 < num_hidden_unit1; index1++) { |
Lightvalve | 173:68c7914679ec | 649 | for (int n=0; n<batch_size; n++) { |
Lightvalve | 177:8e9cf31d63f4 | 650 | float d_x_d_ha2 = 0.0f; |
Lightvalve | 177:8e9cf31d63f4 | 651 | float d_y_d_ha2 = 0.0f; |
Lightvalve | 173:68c7914679ec | 652 | if((advantage[n] >= 0.0f && ratio[n] >= 1.0f + epsilon) || (advantage[n] < 0.0f && ratio[n] < 1.0f - epsilon)) { |
Lightvalve | 170:42c938a40313 | 653 | G_ha2[index1][index2] = G_ha2[index1][index2]; |
Lightvalve | 170:42c938a40313 | 654 | } else { |
Lightvalve | 173:68c7914679ec | 655 | if (hxh_a_sum_array[n][index2] >= 0) { |
Lightvalve | 173:68c7914679ec | 656 | if (hx_a_sum_array[n][index1] > 0) { |
Lightvalve | 177:8e9cf31d63f4 | 657 | d_x_d_ha2 = hx_a_sum_array[n][index1]*ha3_temp[index2][0]; |
Lightvalve | 177:8e9cf31d63f4 | 658 | d_y_d_ha2 = hx_a_sum_array[n][index1]*ha3_temp[index2][1]; |
Lightvalve | 171:bfc1fd2629d8 | 659 | } |
Lightvalve | 170:42c938a40313 | 660 | } |
Lightvalve | 173:68c7914679ec | 661 | |
Lightvalve | 170:42c938a40313 | 662 | float d_mean_d_ha2 = 0.0f; |
Lightvalve | 170:42c938a40313 | 663 | float d_dev_d_ha2 = 0.0f; |
Lightvalve | 177:8e9cf31d63f4 | 664 | d_mean_d_ha2 = exp(hxhh_a_sum_array[n][0])/(1.0f+exp(hxhh_a_sum_array[n][0]))*d_x_d_ha2; |
Lightvalve | 177:8e9cf31d63f4 | 665 | d_dev_d_ha2 = exp(hxhh_a_sum_array[n][1])/(1.0f+exp(hxhh_a_sum_array[n][1]))*d_y_d_ha2; |
Lightvalve | 173:68c7914679ec | 666 | |
Lightvalve | 173:68c7914679ec | 667 | 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 | 668 | } |
Lightvalve | 170:42c938a40313 | 669 | } |
Lightvalve | 176:589ea3edcf3c | 670 | G_ha2[index1][index2] = -G_ha2[index1][index2] / batch_size; |
Lightvalve | 179:d5377766d7ea | 671 | if(G_ha2[index1][index2] > Gradient_Limit) G_ha2[index1][index2] = Gradient_Limit; |
Lightvalve | 179:d5377766d7ea | 672 | else if (G_ha2[index1][index2] < -Gradient_Limit) G_ha2[index1][index2] = -Gradient_Limit; |
Lightvalve | 179:d5377766d7ea | 673 | //ha2_temp[index1][index2] = ha2_temp[index1][index2] - gradient_rate_actor * G_ha2[index1][index2]; |
Lightvalve | 170:42c938a40313 | 674 | } |
Lightvalve | 173:68c7914679ec | 675 | |
Lightvalve | 173:68c7914679ec | 676 | for (int n=0; n<batch_size; n++) { |
Lightvalve | 177:8e9cf31d63f4 | 677 | float d_x_d_ba2 = 0.0f; |
Lightvalve | 177:8e9cf31d63f4 | 678 | float d_y_d_ba2 = 0.0f; |
Lightvalve | 173:68c7914679ec | 679 | if((advantage[n] >= 0.0f && ratio[n] >= 1.0f + epsilon) || (advantage[n] < 0.0f && ratio[n] < 1.0f - epsilon)) { |
Lightvalve | 170:42c938a40313 | 680 | G_ba2[index2] = G_ba2[index2]; |
Lightvalve | 170:42c938a40313 | 681 | } else { |
Lightvalve | 170:42c938a40313 | 682 | |
Lightvalve | 173:68c7914679ec | 683 | if (hxh_a_sum_array[n][index2] >= 0) { |
Lightvalve | 177:8e9cf31d63f4 | 684 | d_x_d_ba2 = ha3_temp[index2][0]; |
Lightvalve | 177:8e9cf31d63f4 | 685 | d_y_d_ba2 = ha3_temp[index2][1]; |
Lightvalve | 173:68c7914679ec | 686 | } |
Lightvalve | 173:68c7914679ec | 687 | float d_mean_d_ba2= 0.0f; |
Lightvalve | 173:68c7914679ec | 688 | float d_dev_d_ba2= 0.0f; |
Lightvalve | 177:8e9cf31d63f4 | 689 | d_mean_d_ba2 = exp(hxhh_a_sum_array[n][0])/(1.0f+exp(hxhh_a_sum_array[n][0]))*d_x_d_ba2; |
Lightvalve | 177:8e9cf31d63f4 | 690 | d_dev_d_ba2 = exp(hxhh_a_sum_array[n][1])/(1.0f+exp(hxhh_a_sum_array[n][1]))*d_y_d_ba2; |
Lightvalve | 173:68c7914679ec | 691 | |
Lightvalve | 173:68c7914679ec | 692 | 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 | 693 | } |
Lightvalve | 170:42c938a40313 | 694 | } |
Lightvalve | 176:589ea3edcf3c | 695 | G_ba2[index2] = -G_ba2[index2] / batch_size; |
Lightvalve | 179:d5377766d7ea | 696 | if(G_ba2[index2] > Gradient_Limit) G_ba2[index2] = Gradient_Limit; |
Lightvalve | 179:d5377766d7ea | 697 | else if (G_ba2[index2] < -Gradient_Limit) G_ba2[index2] = -Gradient_Limit; |
Lightvalve | 179:d5377766d7ea | 698 | //ba2_temp[index2] = ba2_temp[index2] - gradient_rate_actor * G_ba2[index2]; |
Lightvalve | 170:42c938a40313 | 699 | } |
Lightvalve | 173:68c7914679ec | 700 | |
Lightvalve | 173:68c7914679ec | 701 | float G_ha3[num_hidden_unit2][2] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 702 | float G_ba3[2] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 703 | |
Lightvalve | 173:68c7914679ec | 704 | for (int index2 = 0; index2 < 2; index2++) { |
Lightvalve | 173:68c7914679ec | 705 | for (int index1 = 0; index1 < num_hidden_unit2; index1++) { |
Lightvalve | 173:68c7914679ec | 706 | for (int n=0; n<batch_size; n++) { |
Lightvalve | 177:8e9cf31d63f4 | 707 | float d_x_d_ha3 = 0.0f; |
Lightvalve | 177:8e9cf31d63f4 | 708 | float d_y_d_ha3 = 0.0f; |
Lightvalve | 173:68c7914679ec | 709 | if((advantage[n] >= 0.0f && ratio[n] >= 1.0f + epsilon) || (advantage[n] < 0.0f && ratio[n] < 1.0f - epsilon)) { |
Lightvalve | 173:68c7914679ec | 710 | G_ha3[index1][index2] = G_ha3[index1][index2]; |
Lightvalve | 173:68c7914679ec | 711 | } else { |
Lightvalve | 173:68c7914679ec | 712 | if (hxh_a_sum_array[n][index1] >= 0) { |
Lightvalve | 173:68c7914679ec | 713 | if (hx_a_sum_array[n][index1] > 0) { |
Lightvalve | 177:8e9cf31d63f4 | 714 | d_x_d_ha3 = hxh_a_sum_array[n][index1]; |
Lightvalve | 177:8e9cf31d63f4 | 715 | d_y_d_ha3 = hxh_a_sum_array[n][index1]; |
Lightvalve | 173:68c7914679ec | 716 | } |
Lightvalve | 173:68c7914679ec | 717 | } |
Lightvalve | 173:68c7914679ec | 718 | float d_mean_d_ha3 = 0.0f; |
Lightvalve | 173:68c7914679ec | 719 | float d_dev_d_ha3 = 0.0f; |
Lightvalve | 177:8e9cf31d63f4 | 720 | d_mean_d_ha3 = exp(hxhh_a_sum_array[n][0])/(1.0f+exp(hxhh_a_sum_array[n][0]))*d_x_d_ha3; |
Lightvalve | 177:8e9cf31d63f4 | 721 | d_dev_d_ha3 = exp(hxhh_a_sum_array[n][1])/(1.0f+exp(hxhh_a_sum_array[n][1]))*d_y_d_ha3; |
Lightvalve | 173:68c7914679ec | 722 | |
Lightvalve | 173:68c7914679ec | 723 | 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 | 724 | } |
Lightvalve | 173:68c7914679ec | 725 | } |
Lightvalve | 176:589ea3edcf3c | 726 | G_ha3[index1][index2] = -G_ha3[index1][index2] / batch_size; |
Lightvalve | 179:d5377766d7ea | 727 | if(G_ha3[index1][index2] > Gradient_Limit) G_ha3[index1][index2] = Gradient_Limit; |
Lightvalve | 179:d5377766d7ea | 728 | else if (G_ha3[index1][index2] < -Gradient_Limit) G_ha3[index1][index2] = -Gradient_Limit; |
Lightvalve | 179:d5377766d7ea | 729 | //ha3_temp[index1][index2] = ha3_temp[index1][index2] - gradient_rate_actor * G_ha3[index1][index2]; |
Lightvalve | 173:68c7914679ec | 730 | } |
Lightvalve | 173:68c7914679ec | 731 | |
Lightvalve | 173:68c7914679ec | 732 | for (int n=0; n<batch_size; n++) { |
Lightvalve | 177:8e9cf31d63f4 | 733 | float d_x_d_ba3 = 0.0f; |
Lightvalve | 177:8e9cf31d63f4 | 734 | float d_y_d_ba3 = 0.0f; |
Lightvalve | 173:68c7914679ec | 735 | if((advantage[n] >= 0.0f && ratio[n] >= 1.0f + epsilon) || (advantage[n] < 0.0f && ratio[n] < 1.0f - epsilon)) { |
Lightvalve | 173:68c7914679ec | 736 | G_ba3[index2] = G_ba3[index2]; |
Lightvalve | 173:68c7914679ec | 737 | } else { |
Lightvalve | 173:68c7914679ec | 738 | |
Lightvalve | 177:8e9cf31d63f4 | 739 | d_x_d_ba3 = 1.0f; |
Lightvalve | 177:8e9cf31d63f4 | 740 | d_y_d_ba3 = 1.0f; |
Lightvalve | 173:68c7914679ec | 741 | |
Lightvalve | 173:68c7914679ec | 742 | float d_mean_d_ba3= 0.0f; |
Lightvalve | 173:68c7914679ec | 743 | float d_dev_d_ba3= 0.0f; |
Lightvalve | 177:8e9cf31d63f4 | 744 | d_mean_d_ba3 = exp(hxhh_a_sum_array[n][0])/(1.0f+exp(hxhh_a_sum_array[n][0]))*d_x_d_ba3; |
Lightvalve | 177:8e9cf31d63f4 | 745 | d_dev_d_ba3 = exp(hxhh_a_sum_array[n][1])/(1.0f+exp(hxhh_a_sum_array[n][1]))*d_y_d_ba3; |
Lightvalve | 173:68c7914679ec | 746 | |
Lightvalve | 173:68c7914679ec | 747 | 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 | 748 | } |
Lightvalve | 173:68c7914679ec | 749 | } |
Lightvalve | 176:589ea3edcf3c | 750 | G_ba3[index2] = -G_ba3[index2] / batch_size; |
Lightvalve | 179:d5377766d7ea | 751 | if(G_ba3[index2] > Gradient_Limit) G_ba3[index2] = Gradient_Limit; |
Lightvalve | 179:d5377766d7ea | 752 | else if (G_ba3[index2] < -Gradient_Limit) G_ba3[index2] = -Gradient_Limit; |
Lightvalve | 179:d5377766d7ea | 753 | //ba3_temp[index2] = ba3_temp[index2] - gradient_rate_actor * G_ba3[index2]; |
Lightvalve | 175:2f7289dbd488 | 754 | } |
Lightvalve | 177:8e9cf31d63f4 | 755 | |
Lightvalve | 175:2f7289dbd488 | 756 | // Simultaneous Update |
Lightvalve | 175:2f7289dbd488 | 757 | for (int index2 = 0; index2 < num_hidden_unit1; index2++) { |
Lightvalve | 175:2f7289dbd488 | 758 | for (int index1 = 0; index1 < num_input_RL; index1++) { |
Lightvalve | 179:d5377766d7ea | 759 | ha1_temp[index1][index2] = ha1_temp[index1][index2] - gradient_rate_actor * G_ha1[index1][index2]; |
Lightvalve | 175:2f7289dbd488 | 760 | } |
Lightvalve | 179:d5377766d7ea | 761 | ba1_temp[index2] = ba1_temp[index2] - gradient_rate_actor * G_ba1[index2]; |
Lightvalve | 175:2f7289dbd488 | 762 | } |
Lightvalve | 175:2f7289dbd488 | 763 | for (int index2 = 0; index2 < num_hidden_unit2; index2++) { |
Lightvalve | 175:2f7289dbd488 | 764 | for (int index1 = 0; index1 < num_hidden_unit1; index1++) { |
Lightvalve | 179:d5377766d7ea | 765 | ha2_temp[index1][index2] = ha2_temp[index1][index2] - gradient_rate_actor * G_ha2[index1][index2]; |
Lightvalve | 175:2f7289dbd488 | 766 | } |
Lightvalve | 179:d5377766d7ea | 767 | ba2_temp[index2] = ba2_temp[index2] - gradient_rate_actor * G_ba2[index2]; |
Lightvalve | 175:2f7289dbd488 | 768 | } |
Lightvalve | 175:2f7289dbd488 | 769 | for (int index2 = 0; index2 < 2; index2++) { |
Lightvalve | 175:2f7289dbd488 | 770 | for (int index1 = 0; index1 < num_hidden_unit2; index1++) { |
Lightvalve | 179:d5377766d7ea | 771 | ha3_temp[index1][index2] = ha3_temp[index1][index2] - gradient_rate_actor * G_ha3[index1][index2]; |
Lightvalve | 175:2f7289dbd488 | 772 | } |
Lightvalve | 179:d5377766d7ea | 773 | ba3_temp[index2] = ba3_temp[index2] - gradient_rate_actor * G_ba3[index2]; |
Lightvalve | 173:68c7914679ec | 774 | } |
Lightvalve | 170:42c938a40313 | 775 | } |
Lightvalve | 170:42c938a40313 | 776 | |
Lightvalve | 170:42c938a40313 | 777 | float rand_normal(double mean, double stddev) |
Lightvalve | 170:42c938a40313 | 778 | { |
Lightvalve | 170:42c938a40313 | 779 | //Box muller method |
Lightvalve | 170:42c938a40313 | 780 | static double n2 = 0.0f; |
Lightvalve | 170:42c938a40313 | 781 | static int n2_cached = 0; |
Lightvalve | 170:42c938a40313 | 782 | if (!n2_cached) { |
Lightvalve | 170:42c938a40313 | 783 | double x, y, r; |
Lightvalve | 170:42c938a40313 | 784 | do { |
Lightvalve | 170:42c938a40313 | 785 | x = 2.0f*rand()/RAND_MAX - 1; |
Lightvalve | 170:42c938a40313 | 786 | y = 2.0f*rand()/RAND_MAX - 1; |
Lightvalve | 170:42c938a40313 | 787 | |
Lightvalve | 170:42c938a40313 | 788 | r = x*x + y*y; |
Lightvalve | 170:42c938a40313 | 789 | } while (r == 0.0f || r > 1.0f); |
Lightvalve | 170:42c938a40313 | 790 | { |
Lightvalve | 170:42c938a40313 | 791 | double d = sqrt(-2.0f*log(r)/r); |
Lightvalve | 170:42c938a40313 | 792 | double n1 = x*d; |
Lightvalve | 170:42c938a40313 | 793 | n2 = y*d; |
Lightvalve | 170:42c938a40313 | 794 | double result = n1*stddev + mean; |
Lightvalve | 170:42c938a40313 | 795 | n2_cached = 1; |
Lightvalve | 170:42c938a40313 | 796 | return result; |
Lightvalve | 170:42c938a40313 | 797 | } |
Lightvalve | 170:42c938a40313 | 798 | } else { |
Lightvalve | 170:42c938a40313 | 799 | n2_cached = 0; |
Lightvalve | 170:42c938a40313 | 800 | return n2*stddev + mean; |
Lightvalve | 170:42c938a40313 | 801 | } |
Lightvalve | 170:42c938a40313 | 802 | } |
Lightvalve | 170:42c938a40313 | 803 | |
Lightvalve | 179:d5377766d7ea | 804 | float mean_adv(float x[], int size) |
Lightvalve | 179:d5377766d7ea | 805 | { |
Lightvalve | 179:d5377766d7ea | 806 | float add = 0.0f; |
Lightvalve | 179:d5377766d7ea | 807 | float result; |
Lightvalve | 179:d5377766d7ea | 808 | |
Lightvalve | 179:d5377766d7ea | 809 | for (int i=0; i<size; i++) |
Lightvalve | 179:d5377766d7ea | 810 | { |
Lightvalve | 179:d5377766d7ea | 811 | add += x[i]; |
Lightvalve | 179:d5377766d7ea | 812 | } |
Lightvalve | 179:d5377766d7ea | 813 | result = (float) add/size; |
Lightvalve | 179:d5377766d7ea | 814 | return result; |
Lightvalve | 179:d5377766d7ea | 815 | } |
Lightvalve | 179:d5377766d7ea | 816 | float deviation_adv(float x[], int size) |
Lightvalve | 179:d5377766d7ea | 817 | { |
Lightvalve | 179:d5377766d7ea | 818 | float sigma = 0.0f; |
Lightvalve | 179:d5377766d7ea | 819 | float resultDeb = 0.0f; |
Lightvalve | 179:d5377766d7ea | 820 | |
Lightvalve | 179:d5377766d7ea | 821 | for (int k=0; k<size; k++) |
Lightvalve | 179:d5377766d7ea | 822 | { |
Lightvalve | 179:d5377766d7ea | 823 | sigma = pow((float)x[k]-mean_adv(x,size), (float)2.0f)/(size-1); |
Lightvalve | 179:d5377766d7ea | 824 | resultDeb += sqrt(sigma); |
Lightvalve | 179:d5377766d7ea | 825 | } |
Lightvalve | 179:d5377766d7ea | 826 | return resultDeb; |
Lightvalve | 179:d5377766d7ea | 827 | } |
Lightvalve | 179:d5377766d7ea | 828 | |
Lightvalve | 173:68c7914679ec | 829 | |
Lightvalve | 170:42c938a40313 | 830 | void Overwirte_Critic_Networks() |
Lightvalve | 170:42c938a40313 | 831 | { |
Lightvalve | 173:68c7914679ec | 832 | for (int index2 = 0; index2 < num_hidden_unit1; index2++) { |
Lightvalve | 170:42c938a40313 | 833 | for (int index1 = 0; index1 < num_input_RL; index1++) { |
Lightvalve | 170:42c938a40313 | 834 | hc1[index1][index2] = hc1_temp[index1][index2]; |
Lightvalve | 170:42c938a40313 | 835 | } |
Lightvalve | 170:42c938a40313 | 836 | bc1[index2] = bc1_temp[index2]; |
Lightvalve | 170:42c938a40313 | 837 | } |
Lightvalve | 173:68c7914679ec | 838 | for (int index2 = 0; index2 < num_hidden_unit2; index2++) { |
Lightvalve | 173:68c7914679ec | 839 | for (int index1 = 0; index1 < num_hidden_unit1; index1++) { |
Lightvalve | 173:68c7914679ec | 840 | hc2[index1][index2] = hc2_temp[index1][index2]; |
Lightvalve | 173:68c7914679ec | 841 | } |
Lightvalve | 173:68c7914679ec | 842 | bc2[index2] = bc2_temp[index2]; |
Lightvalve | 173:68c7914679ec | 843 | hc3[index2] = hc3_temp[index2]; |
Lightvalve | 173:68c7914679ec | 844 | } |
Lightvalve | 173:68c7914679ec | 845 | bc3 = bc3_temp; |
Lightvalve | 170:42c938a40313 | 846 | } |
Lightvalve | 170:42c938a40313 | 847 | void Overwirte_Actor_Networks() |
Lightvalve | 170:42c938a40313 | 848 | { |
Lightvalve | 173:68c7914679ec | 849 | for (int index2 = 0; index2 < num_hidden_unit1; index2++) { |
Lightvalve | 170:42c938a40313 | 850 | for (int index1 = 0; index1 < num_input_RL; index1++) { |
Lightvalve | 170:42c938a40313 | 851 | ha1[index1][index2] = ha1_temp[index1][index2]; |
Lightvalve | 170:42c938a40313 | 852 | } |
Lightvalve | 170:42c938a40313 | 853 | ba1[index2] = ba1_temp[index2]; |
Lightvalve | 170:42c938a40313 | 854 | } |
Lightvalve | 173:68c7914679ec | 855 | for (int index2 = 0; index2 < num_hidden_unit2; index2++) { |
Lightvalve | 173:68c7914679ec | 856 | for (int index1 = 0; index1 < num_hidden_unit1; index1++) { |
Lightvalve | 170:42c938a40313 | 857 | ha2[index1][index2] = ha2_temp[index1][index2]; |
Lightvalve | 170:42c938a40313 | 858 | } |
Lightvalve | 170:42c938a40313 | 859 | ba2[index2] = ba2_temp[index2]; |
Lightvalve | 170:42c938a40313 | 860 | } |
Lightvalve | 173:68c7914679ec | 861 | for (int index2 = 0; index2 < 2; index2++) { |
Lightvalve | 173:68c7914679ec | 862 | for (int index1 = 0; index1 < num_hidden_unit2; index1++) { |
Lightvalve | 173:68c7914679ec | 863 | ha3[index1][index2] = ha3_temp[index1][index2]; |
Lightvalve | 173:68c7914679ec | 864 | } |
Lightvalve | 173:68c7914679ec | 865 | ba3[index2] = ba3_temp[index2]; |
Lightvalve | 173:68c7914679ec | 866 | } |
Lightvalve | 170:42c938a40313 | 867 | } |
Lightvalve | 170:42c938a40313 | 868 | |
Lightvalve | 170:42c938a40313 | 869 | |
GiJeongKim | 0:51c43836c1d7 | 870 | int main() |
GiJeongKim | 0:51c43836c1d7 | 871 | { |
Lightvalve | 66:a8e6799dbce3 | 872 | |
Lightvalve | 65:a2d7c63419c2 | 873 | HAL_Init(); |
Lightvalve | 65:a2d7c63419c2 | 874 | SystemClock_Config(); |
Lightvalve | 169:645207e160ca | 875 | |
jobuuu | 6:df07d3491e3a | 876 | /********************************* |
jobuuu | 1:e04e563be5ce | 877 | *** Initialization |
jobuuu | 6:df07d3491e3a | 878 | *********************************/ |
Lightvalve | 69:3995ffeaa786 | 879 | LED = 0; |
Lightvalve | 61:bc8c8270f0ab | 880 | pc.baud(9600); |
Lightvalve | 21:e5f1a43ea6f9 | 881 | |
GiJeongKim | 0:51c43836c1d7 | 882 | // i2c init |
Lightvalve | 8:5d2eebdad025 | 883 | i2c.frequency(400 * 1000); // 0.4 mHz |
Lightvalve | 8:5d2eebdad025 | 884 | wait_ms(2); // Power Up wait |
Lightvalve | 8:5d2eebdad025 | 885 | look_for_hardware_i2c(); // Hardware present |
Lightvalve | 8:5d2eebdad025 | 886 | init_as5510(i2c_slave_addr1); |
Lightvalve | 11:82d8768d7351 | 887 | make_delay(); |
jobuuu | 2:a1c0a37df760 | 888 | |
GiJeongKim | 0:51c43836c1d7 | 889 | // // spi init |
Lightvalve | 170:42c938a40313 | 890 | eeprom.format(8,3); |
Lightvalve | 170:42c938a40313 | 891 | eeprom.frequency(5000000); //5M |
GiJeongKim | 0:51c43836c1d7 | 892 | enc.format(8,0); |
GiJeongKim | 0:51c43836c1d7 | 893 | enc.frequency(5000000); //5M |
Lightvalve | 11:82d8768d7351 | 894 | make_delay(); |
Lightvalve | 21:e5f1a43ea6f9 | 895 | |
Lightvalve | 16:903b5a4433b4 | 896 | //rom |
Lightvalve | 19:23b7c1ad8683 | 897 | ROM_CALL_DATA(); |
Lightvalve | 16:903b5a4433b4 | 898 | make_delay(); |
Lightvalve | 13:747daba9cf59 | 899 | |
GiJeongKim | 0:51c43836c1d7 | 900 | // ADC init |
jobuuu | 5:a4319f79457b | 901 | Init_ADC(); |
Lightvalve | 11:82d8768d7351 | 902 | make_delay(); |
jobuuu | 2:a1c0a37df760 | 903 | |
GiJeongKim | 0:51c43836c1d7 | 904 | // Pwm init |
GiJeongKim | 0:51c43836c1d7 | 905 | Init_PWM(); |
GiJeongKim | 0:51c43836c1d7 | 906 | TIM4->CR1 ^= TIM_CR1_UDIS; |
Lightvalve | 11:82d8768d7351 | 907 | make_delay(); |
Lightvalve | 13:747daba9cf59 | 908 | |
Lightvalve | 11:82d8768d7351 | 909 | // TMR3 init |
Lightvalve | 11:82d8768d7351 | 910 | Init_TMR3(); |
Lightvalve | 11:82d8768d7351 | 911 | TIM3->CR1 ^= TIM_CR1_UDIS; |
Lightvalve | 11:82d8768d7351 | 912 | make_delay(); |
Lightvalve | 21:e5f1a43ea6f9 | 913 | |
Lightvalve | 50:3c630b5eba9f | 914 | // TMR2 init |
Lightvalve | 56:6f50d9d3bfee | 915 | // Init_TMR2(); |
Lightvalve | 56:6f50d9d3bfee | 916 | // TIM2->CR1 ^= TIM_CR1_UDIS; |
Lightvalve | 56:6f50d9d3bfee | 917 | // make_delay(); |
Lightvalve | 21:e5f1a43ea6f9 | 918 | |
GiJeongKim | 0:51c43836c1d7 | 919 | // CAN |
jobuuu | 2:a1c0a37df760 | 920 | can.attach(&CAN_RX_HANDLER); |
Lightvalve | 11:82d8768d7351 | 921 | CAN_ID_INIT(); |
Lightvalve | 11:82d8768d7351 | 922 | make_delay(); |
Lightvalve | 34:bb2ca2fc2a8e | 923 | |
Lightvalve | 23:59218d4a256d | 924 | //Timer priority |
Lightvalve | 23:59218d4a256d | 925 | NVIC_SetPriority(TIM3_IRQn, 2); |
Lightvalve | 57:f4819de54e7a | 926 | //NVIC_SetPriority(TIM2_IRQn, 3); |
Lightvalve | 54:647072f5307a | 927 | NVIC_SetPriority(TIM4_IRQn, 3); |
Lightvalve | 34:bb2ca2fc2a8e | 928 | |
Lightvalve | 23:59218d4a256d | 929 | //can.reset(); |
Lightvalve | 19:23b7c1ad8683 | 930 | can.filter(msg.id, 0xFFFFF000, CANStandard); |
Lightvalve | 34:bb2ca2fc2a8e | 931 | |
GiJeongKim | 0:51c43836c1d7 | 932 | // spi _ enc |
GiJeongKim | 0:51c43836c1d7 | 933 | spi_enc_set_init(); |
Lightvalve | 11:82d8768d7351 | 934 | make_delay(); |
Lightvalve | 13:747daba9cf59 | 935 | |
Lightvalve | 11:82d8768d7351 | 936 | //DAC init |
Lightvalve | 58:2eade98630e2 | 937 | if (SENSING_MODE == 0) { |
Lightvalve | 58:2eade98630e2 | 938 | dac_1 = TORQUE_VREF / 3.3f; |
Lightvalve | 58:2eade98630e2 | 939 | dac_2 = 0.0f; |
Lightvalve | 58:2eade98630e2 | 940 | } else if (SENSING_MODE == 1) { |
Lightvalve | 58:2eade98630e2 | 941 | dac_1 = PRES_A_VREF / 3.3f; |
Lightvalve | 58:2eade98630e2 | 942 | dac_2 = PRES_B_VREF / 3.3f; |
Lightvalve | 58:2eade98630e2 | 943 | } |
Lightvalve | 11:82d8768d7351 | 944 | make_delay(); |
Lightvalve | 13:747daba9cf59 | 945 | |
Lightvalve | 19:23b7c1ad8683 | 946 | for (int i=0; i<50; i++) { |
Lightvalve | 11:82d8768d7351 | 947 | if(i%2==0) |
Lightvalve | 38:118df027d851 | 948 | ID_index_array[i] = - i * 0.5f; |
Lightvalve | 11:82d8768d7351 | 949 | else |
Lightvalve | 38:118df027d851 | 950 | ID_index_array[i] = (i+1) * 0.5f; |
Lightvalve | 11:82d8768d7351 | 951 | } |
Lightvalve | 169:645207e160ca | 952 | |
Lightvalve | 173:68c7914679ec | 953 | for (int index2 = 0; index2 < num_hidden_unit1; index2++) { |
Lightvalve | 170:42c938a40313 | 954 | for (int index1 = 0; index1 < num_input_RL; index1++) { |
Lightvalve | 179:d5377766d7ea | 955 | hc1_temp[index1][index2] = (float) (rand()%100) * 0.007f ; |
Lightvalve | 170:42c938a40313 | 956 | } |
Lightvalve | 179:d5377766d7ea | 957 | bc1_temp[index2] = (float) (rand()%100) * 0.007f; |
Lightvalve | 173:68c7914679ec | 958 | } |
Lightvalve | 173:68c7914679ec | 959 | for (int index2 = 0; index2 < num_hidden_unit2; index2++) { |
Lightvalve | 173:68c7914679ec | 960 | for (int index1 = 0; index1 < num_hidden_unit1; index1++) { |
Lightvalve | 179:d5377766d7ea | 961 | hc2_temp[index1][index2] = (float) (rand()%100) * 0.007f; |
Lightvalve | 173:68c7914679ec | 962 | } |
Lightvalve | 179:d5377766d7ea | 963 | bc2_temp[index2] = (float) (rand()%100) * 0.007f; |
Lightvalve | 179:d5377766d7ea | 964 | hc3_temp[index2] = (float) (rand()%100) * 0.007f; |
Lightvalve | 170:42c938a40313 | 965 | } |
Lightvalve | 179:d5377766d7ea | 966 | bc3_temp = (float) (rand()%100) * 0.007f; |
Lightvalve | 173:68c7914679ec | 967 | |
Lightvalve | 173:68c7914679ec | 968 | for (int index2 = 0; index2 < num_hidden_unit1; index2++) { |
Lightvalve | 170:42c938a40313 | 969 | for (int index1 = 0; index1 < num_input_RL; index1++) { |
Lightvalve | 179:d5377766d7ea | 970 | ha1_temp[index1][index2] = (float) (rand()%100) * 0.007f; |
Lightvalve | 170:42c938a40313 | 971 | } |
Lightvalve | 179:d5377766d7ea | 972 | ba1_temp[index2] = (float) (rand()%100) * 0.007f; |
Lightvalve | 173:68c7914679ec | 973 | } |
Lightvalve | 173:68c7914679ec | 974 | for (int index2 = 0; index2 < num_hidden_unit2; index2++) { |
Lightvalve | 173:68c7914679ec | 975 | for (int index1 = 0; index1 < num_hidden_unit1; index1++) { |
Lightvalve | 179:d5377766d7ea | 976 | ha2_temp[index1][index2] = (float) (rand()%100) * 0.007f; |
Lightvalve | 173:68c7914679ec | 977 | } |
Lightvalve | 179:d5377766d7ea | 978 | ba2_temp[index2] = (float) (rand()%100) * 0.007f; |
Lightvalve | 170:42c938a40313 | 979 | } |
Lightvalve | 170:42c938a40313 | 980 | for (int index2 = 0; index2 < 2; index2++) { |
Lightvalve | 173:68c7914679ec | 981 | for (int index1 = 0; index1 < num_hidden_unit2; index1++) { |
Lightvalve | 179:d5377766d7ea | 982 | ha3_temp[index1][index2] = (float) (rand()%100) * 0.007f; |
Lightvalve | 170:42c938a40313 | 983 | } |
Lightvalve | 179:d5377766d7ea | 984 | ba3_temp[index2] = (float) (rand()%100) * 0.007f; |
Lightvalve | 170:42c938a40313 | 985 | } |
Lightvalve | 171:bfc1fd2629d8 | 986 | |
Lightvalve | 170:42c938a40313 | 987 | Overwirte_Critic_Networks(); |
Lightvalve | 170:42c938a40313 | 988 | Overwirte_Actor_Networks(); |
Lightvalve | 61:bc8c8270f0ab | 989 | |
jobuuu | 6:df07d3491e3a | 990 | /************************************ |
jobuuu | 1:e04e563be5ce | 991 | *** Program is operating! |
jobuuu | 6:df07d3491e3a | 992 | *************************************/ |
GiJeongKim | 0:51c43836c1d7 | 993 | while(1) { |
Lightvalve | 169:645207e160ca | 994 | |
Lightvalve | 171:bfc1fd2629d8 | 995 | // if(timer_while==27491) { |
Lightvalve | 171:bfc1fd2629d8 | 996 | // timer_while = 0; |
Lightvalve | 171:bfc1fd2629d8 | 997 | // 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 | 998 | // //pc.printf("%f\n", virt_pos); |
Lightvalve | 171:bfc1fd2629d8 | 999 | // //pc.printf("%f\n", pos.sen/(float)(ENC_PULSE_PER_POSITION)); |
Lightvalve | 171:bfc1fd2629d8 | 1000 | // //pc.printf("ref : %f virt_pos : %f\n", pos.sen/(float)(ENC_PULSE_PER_POSITION), virt_pos); |
Lightvalve | 171:bfc1fd2629d8 | 1001 | // } |
Lightvalve | 169:645207e160ca | 1002 | |
Lightvalve | 171:bfc1fd2629d8 | 1003 | |
Lightvalve | 171:bfc1fd2629d8 | 1004 | //i2c |
Lightvalve | 180:02be1711ee0b | 1005 | read_field(i2c_slave_addr1); |
Lightvalve | 180:02be1711ee0b | 1006 | if(DIR_VALVE_ENC < 0) value = 1023 - value; |
Lightvalve | 171:bfc1fd2629d8 | 1007 | |
Lightvalve | 177:8e9cf31d63f4 | 1008 | //timer_while ++; |
Lightvalve | 169:645207e160ca | 1009 | |
Lightvalve | 170:42c938a40313 | 1010 | ///////////////////////////////////////////////////////Neural Network |
Lightvalve | 169:645207e160ca | 1011 | |
Lightvalve | 73:f80dc3970c99 | 1012 | if(NN_Control_Flag == 0) { |
Lightvalve | 73:f80dc3970c99 | 1013 | LED = 0; |
Lightvalve | 73:f80dc3970c99 | 1014 | } |
Lightvalve | 169:645207e160ca | 1015 | |
Lightvalve | 73:f80dc3970c99 | 1016 | else if(NN_Control_Flag == 1) { |
Lightvalve | 169:645207e160ca | 1017 | |
Lightvalve | 162:9dd4f35e9de8 | 1018 | int ind = 0; |
Lightvalve | 162:9dd4f35e9de8 | 1019 | for(int i=0; i<numpast_u; i++) { |
Lightvalve | 162:9dd4f35e9de8 | 1020 | input_NN[ind] = u_past[time_interval*i]; |
Lightvalve | 162:9dd4f35e9de8 | 1021 | ind = ind + 1; |
Lightvalve | 162:9dd4f35e9de8 | 1022 | } |
Lightvalve | 162:9dd4f35e9de8 | 1023 | |
Lightvalve | 162:9dd4f35e9de8 | 1024 | for(int i=0; i<numpast_x; i++) { |
Lightvalve | 162:9dd4f35e9de8 | 1025 | input_NN[ind] = x_past[time_interval*i] / 60.0f; |
Lightvalve | 162:9dd4f35e9de8 | 1026 | ind = ind + 1; |
Lightvalve | 162:9dd4f35e9de8 | 1027 | } |
Lightvalve | 162:9dd4f35e9de8 | 1028 | input_NN[ind] = (pos.sen / ENC_PULSE_PER_POSITION) / 60.0f; |
Lightvalve | 162:9dd4f35e9de8 | 1029 | ind = ind + 1; |
Lightvalve | 169:645207e160ca | 1030 | |
Lightvalve | 162:9dd4f35e9de8 | 1031 | // for(int i=0; i<numfuture_x; i++) { |
Lightvalve | 162:9dd4f35e9de8 | 1032 | // input_NN[ind] = x_future[time_interval*i+time_interval] / 60.0f; |
Lightvalve | 122:dcb3ce3056a0 | 1033 | // ind = ind + 1; |
Lightvalve | 122:dcb3ce3056a0 | 1034 | // } |
Lightvalve | 162:9dd4f35e9de8 | 1035 | |
Lightvalve | 162:9dd4f35e9de8 | 1036 | for(int i=0; i<numpast_f; i++) { |
Lightvalve | 193:4b5e617b8481 | 1037 | input_NN[ind] = f_past[time_interval*i] / 10000.0f * 8.0f + 0.5f; |
Lightvalve | 162:9dd4f35e9de8 | 1038 | ind = ind + 1; |
Lightvalve | 162:9dd4f35e9de8 | 1039 | } |
Lightvalve | 193:4b5e617b8481 | 1040 | input_NN[ind] = torq.sen / 10000.0f * 8.0f + 0.5f; |
Lightvalve | 162:9dd4f35e9de8 | 1041 | ind = ind + 1; |
Lightvalve | 162:9dd4f35e9de8 | 1042 | for(int i=0; i<numfuture_f; i++) { |
Lightvalve | 199:ee79b4692e3b | 1043 | input_NN[ind] = (f_future[time_interval*i+time_interval] - torq.sen)/10000.0f * 8.0f + 0.5f; |
Lightvalve | 199:ee79b4692e3b | 1044 | // input_NN[ind] = (f_future[time_interval*i+time_interval])/10000.0f*8.0f+0.5f; |
Lightvalve | 162:9dd4f35e9de8 | 1045 | ind = ind + 1; |
Lightvalve | 162:9dd4f35e9de8 | 1046 | } |
Lightvalve | 169:645207e160ca | 1047 | |
Lightvalve | 112:8dcb1600cb90 | 1048 | float output1[16] = { 0.0f }; |
Lightvalve | 112:8dcb1600cb90 | 1049 | float output2[16] = { 0.0f }; |
Lightvalve | 112:8dcb1600cb90 | 1050 | float output3[16] = { 0.0f }; |
Lightvalve | 66:a8e6799dbce3 | 1051 | float output = 0.0f; |
Lightvalve | 65:a2d7c63419c2 | 1052 | |
Lightvalve | 112:8dcb1600cb90 | 1053 | for (int index2 = 0; index2 < 16; index2++) { |
Lightvalve | 68:328e1be06f5d | 1054 | for (int index1 = 0; index1 < num_input; index1++) { |
Lightvalve | 66:a8e6799dbce3 | 1055 | output1[index2] = output1[index2] |
Lightvalve | 73:f80dc3970c99 | 1056 | + h1[index1][index2] * input_NN[index1]; |
Lightvalve | 66:a8e6799dbce3 | 1057 | } |
Lightvalve | 66:a8e6799dbce3 | 1058 | output1[index2] = output1[index2] + b1[index2]; |
Lightvalve | 66:a8e6799dbce3 | 1059 | if (output1[index2] < 0) { |
Lightvalve | 66:a8e6799dbce3 | 1060 | output1[index2] = 0; |
Lightvalve | 66:a8e6799dbce3 | 1061 | } |
Lightvalve | 66:a8e6799dbce3 | 1062 | } |
Lightvalve | 65:a2d7c63419c2 | 1063 | |
Lightvalve | 112:8dcb1600cb90 | 1064 | for (int index2 = 0; index2 < 16; index2++) { |
Lightvalve | 112:8dcb1600cb90 | 1065 | for (int index1 = 0; index1 < 16; index1++) { |
Lightvalve | 66:a8e6799dbce3 | 1066 | output2[index2] = output2[index2] |
Lightvalve | 66:a8e6799dbce3 | 1067 | + h2[index1][index2] * output1[index1]; |
Lightvalve | 66:a8e6799dbce3 | 1068 | } |
Lightvalve | 66:a8e6799dbce3 | 1069 | output2[index2] = output2[index2] + b2[index2]; |
Lightvalve | 66:a8e6799dbce3 | 1070 | if (output2[index2] < 0) { |
Lightvalve | 66:a8e6799dbce3 | 1071 | output2[index2] = 0; |
Lightvalve | 66:a8e6799dbce3 | 1072 | } |
Lightvalve | 66:a8e6799dbce3 | 1073 | } |
Lightvalve | 65:a2d7c63419c2 | 1074 | |
Lightvalve | 112:8dcb1600cb90 | 1075 | for (int index2 = 0; index2 < 16; index2++) { |
Lightvalve | 112:8dcb1600cb90 | 1076 | for (int index1 = 0; index1 < 16; index1++) { |
Lightvalve | 66:a8e6799dbce3 | 1077 | output3[index2] = output3[index2] |
Lightvalve | 66:a8e6799dbce3 | 1078 | + h3[index1][index2] * output2[index1]; |
Lightvalve | 66:a8e6799dbce3 | 1079 | } |
Lightvalve | 66:a8e6799dbce3 | 1080 | output3[index2] = output3[index2] + b3[index2]; |
Lightvalve | 66:a8e6799dbce3 | 1081 | if (output3[index2] < 0) { |
Lightvalve | 66:a8e6799dbce3 | 1082 | output3[index2] = 0; |
Lightvalve | 66:a8e6799dbce3 | 1083 | } |
Lightvalve | 65:a2d7c63419c2 | 1084 | } |
Lightvalve | 66:a8e6799dbce3 | 1085 | |
Lightvalve | 66:a8e6799dbce3 | 1086 | for (int index2 = 0; index2 < 1; index2++) { |
Lightvalve | 112:8dcb1600cb90 | 1087 | for (int index1 = 0; index1 < 16; index1++) { |
Lightvalve | 73:f80dc3970c99 | 1088 | output = output + hout[index1] * output3[index1]; |
Lightvalve | 66:a8e6799dbce3 | 1089 | } |
Lightvalve | 66:a8e6799dbce3 | 1090 | output = output + bout[index2]; |
Lightvalve | 169:645207e160ca | 1091 | |
Lightvalve | 66:a8e6799dbce3 | 1092 | } |
Lightvalve | 73:f80dc3970c99 | 1093 | output = 1.0f/(1.0f+exp(-output)); |
Lightvalve | 101:50159049a518 | 1094 | output_normalized = output; |
Lightvalve | 68:328e1be06f5d | 1095 | output = output * 20000.0f - 10000.0f; |
Lightvalve | 169:645207e160ca | 1096 | |
Lightvalve | 66:a8e6799dbce3 | 1097 | if(output>=0) { |
Lightvalve | 66:a8e6799dbce3 | 1098 | valve_pos.ref = output*0.0001f*((double)VALVE_MAX_POS - (double) VALVE_CENTER) + (double) VALVE_CENTER; |
Lightvalve | 66:a8e6799dbce3 | 1099 | } else { |
Lightvalve | 66:a8e6799dbce3 | 1100 | valve_pos.ref = -output*0.0001f*((double)VALVE_MIN_POS - (double) VALVE_CENTER) + (double) VALVE_CENTER; |
Lightvalve | 65:a2d7c63419c2 | 1101 | } |
Lightvalve | 87:471334725012 | 1102 | |
Lightvalve | 169:645207e160ca | 1103 | |
Lightvalve | 69:3995ffeaa786 | 1104 | if(LED==1) { |
Lightvalve | 69:3995ffeaa786 | 1105 | LED=0; |
Lightvalve | 69:3995ffeaa786 | 1106 | } else |
Lightvalve | 69:3995ffeaa786 | 1107 | LED = 1; |
Lightvalve | 169:645207e160ca | 1108 | |
Lightvalve | 65:a2d7c63419c2 | 1109 | } |
Lightvalve | 171:bfc1fd2629d8 | 1110 | |
Lightvalve | 171:bfc1fd2629d8 | 1111 | |
Lightvalve | 170:42c938a40313 | 1112 | /////////////////////////////////////////////////////////////////////RL |
Lightvalve | 170:42c938a40313 | 1113 | switch (Update_Case) { |
Lightvalve | 170:42c938a40313 | 1114 | case 0: { |
Lightvalve | 170:42c938a40313 | 1115 | break; |
Lightvalve | 170:42c938a40313 | 1116 | } |
Lightvalve | 170:42c938a40313 | 1117 | case 1: { |
Lightvalve | 170:42c938a40313 | 1118 | //Network Update(just update and hold network) |
Lightvalve | 170:42c938a40313 | 1119 | for (int epoch = 0; epoch < num_epoch; epoch++) { |
Lightvalve | 170:42c938a40313 | 1120 | float loss_sum = 0.0f; |
Lightvalve | 175:2f7289dbd488 | 1121 | for (int n=batch_size-1; n>=0; n--) { |
Lightvalve | 170:42c938a40313 | 1122 | //Calculate Estimated V |
Lightvalve | 175:2f7289dbd488 | 1123 | //float temp_array[3] = {state_array[n][0], state_array[n][1], state_array[n][2]}; |
Lightvalve | 175:2f7289dbd488 | 1124 | float temp_array[2] = {state_array[n][0], state_array[n][1]}; |
Lightvalve | 175:2f7289dbd488 | 1125 | V[n] = Critic_Network_Temp(temp_array); |
Lightvalve | 173:68c7914679ec | 1126 | for (int i=0; i<num_hidden_unit1; i++) { |
Lightvalve | 175:2f7289dbd488 | 1127 | hx_c_sum_array[n][i] = hx_c_sum[i]; |
Lightvalve | 173:68c7914679ec | 1128 | } |
Lightvalve | 173:68c7914679ec | 1129 | for (int i=0; i<num_hidden_unit2; i++) { |
Lightvalve | 175:2f7289dbd488 | 1130 | hxh_c_sum_array[n][i] = hxh_c_sum[i]; |
Lightvalve | 173:68c7914679ec | 1131 | } |
Lightvalve | 175:2f7289dbd488 | 1132 | hxhh_c_sum_array[n] = hxhh_c_sum; |
Lightvalve | 177:8e9cf31d63f4 | 1133 | |
Lightvalve | 175:2f7289dbd488 | 1134 | 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 | 1135 | Actor_Network_Old(temp_array); |
Lightvalve | 175:2f7289dbd488 | 1136 | 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 | 179:d5377766d7ea | 1137 | r[n] = exp(-0.25f * 5.0f * state_array[n][1] * state_array[n][1]); |
Lightvalve | 175:2f7289dbd488 | 1138 | if(n == batch_size-1) return_G[n] = 0.0f; |
Lightvalve | 175:2f7289dbd488 | 1139 | else return_G[n] = gamma * return_G[n+1] + r[n]; |
Lightvalve | 175:2f7289dbd488 | 1140 | if(n == batch_size-1) td_target[n] = r[n]; |
Lightvalve | 175:2f7289dbd488 | 1141 | else td_target[n] = r[n] + gamma * V[n+1]; |
Lightvalve | 175:2f7289dbd488 | 1142 | delta[n] = td_target[n] - V[n]; |
Lightvalve | 175:2f7289dbd488 | 1143 | if(n == batch_size-1) advantage[n] = 0.0f; |
Lightvalve | 175:2f7289dbd488 | 1144 | else advantage[n] = gamma * lmbda * advantage[n+1] + delta[n]; |
Lightvalve | 179:d5377766d7ea | 1145 | // return_G[n] = advantage[n] + V[n]; |
Lightvalve | 175:2f7289dbd488 | 1146 | ratio[n] = pi[n]/pi_old[n]; |
Lightvalve | 179:d5377766d7ea | 1147 | } |
Lightvalve | 179:d5377766d7ea | 1148 | float mean_advantage = 0.0f; |
Lightvalve | 179:d5377766d7ea | 1149 | float dev_advantage = 0.0f; |
Lightvalve | 179:d5377766d7ea | 1150 | mean_advantage = mean_adv(advantage, batch_size); |
Lightvalve | 179:d5377766d7ea | 1151 | dev_advantage = deviation_adv(advantage, batch_size); |
Lightvalve | 179:d5377766d7ea | 1152 | for (int n=batch_size-1; n>=0; n--) { |
Lightvalve | 179:d5377766d7ea | 1153 | //advantage[n] = (advantage[n]-mean_advantage)/dev_advantage; |
Lightvalve | 175:2f7289dbd488 | 1154 | surr1[n] = ratio[n] * advantage[n]; |
Lightvalve | 175:2f7289dbd488 | 1155 | if (ratio[n] > 1.0f + epsilon) { |
Lightvalve | 175:2f7289dbd488 | 1156 | surr2[n] = (1.0f + epsilon)*advantage[n]; |
Lightvalve | 175:2f7289dbd488 | 1157 | } else if( ratio[n] < 1.0f - epsilon) { |
Lightvalve | 175:2f7289dbd488 | 1158 | surr2[n] = (1.0f - epsilon)*advantage[n]; |
Lightvalve | 170:42c938a40313 | 1159 | } else { |
Lightvalve | 175:2f7289dbd488 | 1160 | surr2[n] = ratio[n]*advantage[n]; |
Lightvalve | 170:42c938a40313 | 1161 | } |
Lightvalve | 175:2f7289dbd488 | 1162 | loss[n] = -min(surr1[n], surr2[n]); |
Lightvalve | 175:2f7289dbd488 | 1163 | loss_sum = loss_sum + loss[n]; |
Lightvalve | 170:42c938a40313 | 1164 | } |
Lightvalve | 170:42c938a40313 | 1165 | reward_sum = 0.0f; |
Lightvalve | 170:42c938a40313 | 1166 | for (int i=0; i<batch_size; i++) { |
Lightvalve | 170:42c938a40313 | 1167 | reward_sum = reward_sum + r[i]; |
Lightvalve | 170:42c938a40313 | 1168 | } |
Lightvalve | 170:42c938a40313 | 1169 | logging5 = reward_sum; |
Lightvalve | 171:bfc1fd2629d8 | 1170 | |
Lightvalve | 171:bfc1fd2629d8 | 1171 | |
Lightvalve | 170:42c938a40313 | 1172 | //loss_batch = loss_sum / (float) batch_size; |
Lightvalve | 170:42c938a40313 | 1173 | loss_batch = loss_sum; |
Lightvalve | 170:42c938a40313 | 1174 | //Update Networks |
Lightvalve | 170:42c938a40313 | 1175 | update_Critic_Networks(state_array); |
Lightvalve | 170:42c938a40313 | 1176 | update_Actor_Networks(state_array); |
Lightvalve | 170:42c938a40313 | 1177 | } |
Lightvalve | 170:42c938a40313 | 1178 | Update_Done_Flag = 1; |
Lightvalve | 170:42c938a40313 | 1179 | Update_Case = 0; |
Lightvalve | 170:42c938a40313 | 1180 | //logging1 = V[0]; |
Lightvalve | 171:bfc1fd2629d8 | 1181 | |
Lightvalve | 170:42c938a40313 | 1182 | break; |
Lightvalve | 170:42c938a40313 | 1183 | } |
Lightvalve | 170:42c938a40313 | 1184 | case 2: { |
Lightvalve | 170:42c938a40313 | 1185 | //Network apply to next Network |
Lightvalve | 170:42c938a40313 | 1186 | Overwirte_Critic_Networks(); |
Lightvalve | 170:42c938a40313 | 1187 | Overwirte_Actor_Networks(); |
Lightvalve | 170:42c938a40313 | 1188 | virt_pos = 10.0f; |
Lightvalve | 170:42c938a40313 | 1189 | Update_Done_Flag = 1; |
Lightvalve | 170:42c938a40313 | 1190 | Update_Case = 0; |
Lightvalve | 170:42c938a40313 | 1191 | break; |
Lightvalve | 170:42c938a40313 | 1192 | } |
Lightvalve | 169:645207e160ca | 1193 | |
Lightvalve | 170:42c938a40313 | 1194 | } |
GiJeongKim | 0:51c43836c1d7 | 1195 | } |
jobuuu | 1:e04e563be5ce | 1196 | } |
jobuuu | 1:e04e563be5ce | 1197 | |
Lightvalve | 33:91b17819ec30 | 1198 | float DDV_JOINT_POS_FF(float REF_JOINT_VEL) |
Lightvalve | 14:8e7590227d22 | 1199 | { |
Lightvalve | 14:8e7590227d22 | 1200 | |
Lightvalve | 13:747daba9cf59 | 1201 | int i = 0; |
Lightvalve | 48:889798ff9329 | 1202 | float Ref_Valve_Pos_FF = 0.0f; |
Lightvalve | 14:8e7590227d22 | 1203 | for(i=0; i<VALVE_POS_NUM; i++) { |
Lightvalve | 14:8e7590227d22 | 1204 | 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 | 1205 | if(i==0) { |
Lightvalve | 50:3c630b5eba9f | 1206 | if(JOINT_VEL[i+1] == JOINT_VEL[i]) { |
Lightvalve | 57:f4819de54e7a | 1207 | Ref_Valve_Pos_FF = (float) VALVE_CENTER; |
Lightvalve | 50:3c630b5eba9f | 1208 | } else { |
Lightvalve | 57:f4819de54e7a | 1209 | 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 | 1210 | } |
Lightvalve | 14:8e7590227d22 | 1211 | } else { |
Lightvalve | 50:3c630b5eba9f | 1212 | if(JOINT_VEL[i+1] == JOINT_VEL[i-1]) { |
Lightvalve | 57:f4819de54e7a | 1213 | Ref_Valve_Pos_FF = (float) VALVE_CENTER; |
Lightvalve | 50:3c630b5eba9f | 1214 | } else { |
Lightvalve | 57:f4819de54e7a | 1215 | 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 | 1216 | } |
Lightvalve | 13:747daba9cf59 | 1217 | } |
Lightvalve | 13:747daba9cf59 | 1218 | break; |
Lightvalve | 13:747daba9cf59 | 1219 | } |
Lightvalve | 13:747daba9cf59 | 1220 | } |
Lightvalve | 14:8e7590227d22 | 1221 | if(REF_JOINT_VEL > max(JOINT_VEL[VALVE_POS_NUM-1], JOINT_VEL[VALVE_POS_NUM-2])) { |
Lightvalve | 33:91b17819ec30 | 1222 | Ref_Valve_Pos_FF = (float) VALVE_MAX_POS; |
Lightvalve | 14:8e7590227d22 | 1223 | } else if(REF_JOINT_VEL < min(JOINT_VEL[VALVE_POS_NUM-1], JOINT_VEL[VALVE_POS_NUM-2])) { |
Lightvalve | 33:91b17819ec30 | 1224 | Ref_Valve_Pos_FF = (float) VALVE_MIN_POS; |
Lightvalve | 13:747daba9cf59 | 1225 | } |
Lightvalve | 36:a46e63505ed8 | 1226 | |
Lightvalve | 57:f4819de54e7a | 1227 | Ref_Valve_Pos_FF = (float) VELOCITY_COMP_GAIN * 0.01f * (float) (Ref_Valve_Pos_FF - (float) VALVE_CENTER); |
Lightvalve | 13:747daba9cf59 | 1228 | return Ref_Valve_Pos_FF; |
Lightvalve | 50:3c630b5eba9f | 1229 | |
Lightvalve | 13:747daba9cf59 | 1230 | } |
jobuuu | 6:df07d3491e3a | 1231 | |
jobuuu | 6:df07d3491e3a | 1232 | |
Lightvalve | 30:8d561f16383b | 1233 | void VALVE_POS_CONTROL(float REF_VALVE_POS) |
Lightvalve | 14:8e7590227d22 | 1234 | { |
Lightvalve | 13:747daba9cf59 | 1235 | int i = 0; |
Lightvalve | 13:747daba9cf59 | 1236 | |
Lightvalve | 38:118df027d851 | 1237 | if(REF_VALVE_POS > VALVE_MAX_POS) { |
Lightvalve | 38:118df027d851 | 1238 | REF_VALVE_POS = VALVE_MAX_POS; |
Lightvalve | 38:118df027d851 | 1239 | } else if(REF_VALVE_POS < VALVE_MIN_POS) { |
Lightvalve | 38:118df027d851 | 1240 | REF_VALVE_POS = VALVE_MIN_POS; |
Lightvalve | 38:118df027d851 | 1241 | } |
Lightvalve | 38:118df027d851 | 1242 | |
Lightvalve | 89:a7b45368ea0f | 1243 | valve_pos_err = (float) (REF_VALVE_POS - value); |
Lightvalve | 13:747daba9cf59 | 1244 | valve_pos_err_diff = valve_pos_err - valve_pos_err_old; |
Lightvalve | 13:747daba9cf59 | 1245 | valve_pos_err_old = valve_pos_err; |
Lightvalve | 13:747daba9cf59 | 1246 | valve_pos_err_sum += valve_pos_err; |
Lightvalve | 89:a7b45368ea0f | 1247 | if (valve_pos_err_sum > 1000.0f) valve_pos_err_sum = 1000.0f; |
Lightvalve | 89:a7b45368ea0f | 1248 | if (valve_pos_err_sum<-1000.0f) valve_pos_err_sum = -1000.0f; |
Lightvalve | 13:747daba9cf59 | 1249 | |
Lightvalve | 13:747daba9cf59 | 1250 | 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 | 1251 | |
Lightvalve | 18:b8adf1582ea3 | 1252 | for(i=0; i<24; i++) { |
Lightvalve | 89:a7b45368ea0f | 1253 | 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 | 1254 | if(i==0) { |
Lightvalve | 48:889798ff9329 | 1255 | 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 | 1256 | } else { |
Lightvalve | 48:889798ff9329 | 1257 | 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 | 1258 | } |
Lightvalve | 13:747daba9cf59 | 1259 | break; |
Lightvalve | 13:747daba9cf59 | 1260 | } |
Lightvalve | 13:747daba9cf59 | 1261 | } |
Lightvalve | 59:f308b1656d9c | 1262 | Vout.ref = VALVE_PWM_RAW_FF + VALVE_PWM_RAW_FB; |
Lightvalve | 13:747daba9cf59 | 1263 | } |
Lightvalve | 13:747daba9cf59 | 1264 | |
Lightvalve | 14:8e7590227d22 | 1265 | #define LT_MAX_IDX 57 |
Lightvalve | 30:8d561f16383b | 1266 | 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 | 1267 | -19.0f, -18.0f, -17.0f, -16.0f, -15.0f, -14.0f, -13.0f, -12.0f, -11.0f, -10.0f, |
Lightvalve | 34:bb2ca2fc2a8e | 1268 | -9.0f, -8.0f, -7.0f, -6.0f, -5.0f, -4.0f, -3.0f, -2.0f, -1.0f, 0.0f, |
Lightvalve | 34:bb2ca2fc2a8e | 1269 | 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f, |
Lightvalve | 34:bb2ca2fc2a8e | 1270 | 11.0f, 12.0f, 13.0f, 14.0f, 15.0f, 16.0f, 17.0f, 18.0f, 19.0f, 20.0f, |
Lightvalve | 34:bb2ca2fc2a8e | 1271 | 25.0f, 30.0f, 35.0f, 40.0f, 50.0f, 60.0f, 80.0f, 100.0f |
Lightvalve | 34:bb2ca2fc2a8e | 1272 | }; // duty |
Lightvalve | 67:c2812cf26c38 | 1273 | 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 | 1274 | -150.0f, -145.0f, -145.0f, -145.0f, -135.0f, -135.0f, -135.0f, -127.5f, -127.5f, -115.0f, |
Lightvalve | 67:c2812cf26c38 | 1275 | -115.0f, -115.0F, -100.0f, -100.0f, -100.0f, -60.0f, -60.0f, -10.0f, -5.0f, 0.0f, |
Lightvalve | 67:c2812cf26c38 | 1276 | 7.5f, 14.0f, 14.0f, 14.0f, 42.5f, 42.5f, 42.5f, 80.0f, 80.0f, 105.0f, |
Lightvalve | 67:c2812cf26c38 | 1277 | 105.0f, 105.0f, 120.0f, 120.0f, 120.0f, 131.0f, 131.0f, 140.0f, 140.0f, 140.0f, |
Lightvalve | 67:c2812cf26c38 | 1278 | 155.0f, 160.0f, 170.0f, 174.0f, 182.0f, 191.0f, 212.0f, 230.0f |
Lightvalve | 34:bb2ca2fc2a8e | 1279 | }; // mV |
Lightvalve | 13:747daba9cf59 | 1280 | |
Lightvalve | 30:8d561f16383b | 1281 | float PWM_duty_byLT(float Ref_V) |
Lightvalve | 14:8e7590227d22 | 1282 | { |
Lightvalve | 30:8d561f16383b | 1283 | float PWM_duty = 0.0f; |
Lightvalve | 13:747daba9cf59 | 1284 | if(Ref_V<LT_Voltage_Output[0]) { |
Lightvalve | 30:8d561f16383b | 1285 | PWM_duty = (Ref_V-LT_Voltage_Output[0])/1.5f+LT_PWM_duty[0]; |
Lightvalve | 13:747daba9cf59 | 1286 | } else if (Ref_V>=LT_Voltage_Output[LT_MAX_IDX-1]) { |
Lightvalve | 30:8d561f16383b | 1287 | PWM_duty = (Ref_V-LT_Voltage_Output[LT_MAX_IDX-1])/1.5f+LT_PWM_duty[LT_MAX_IDX-1]; |
Lightvalve | 13:747daba9cf59 | 1288 | } else { |
Lightvalve | 13:747daba9cf59 | 1289 | int idx = 0; |
Lightvalve | 13:747daba9cf59 | 1290 | for(idx=0; idx<LT_MAX_IDX-1; idx++) { |
Lightvalve | 30:8d561f16383b | 1291 | float ini_x = LT_Voltage_Output[idx]; |
Lightvalve | 30:8d561f16383b | 1292 | float fin_x = LT_Voltage_Output[idx+1]; |
Lightvalve | 30:8d561f16383b | 1293 | float ini_y = LT_PWM_duty[idx]; |
Lightvalve | 30:8d561f16383b | 1294 | float fin_y = LT_PWM_duty[idx+1]; |
Lightvalve | 13:747daba9cf59 | 1295 | if(Ref_V>=ini_x && Ref_V<fin_x) { |
Lightvalve | 13:747daba9cf59 | 1296 | PWM_duty = (fin_y-ini_y)/(fin_x-ini_x)*(Ref_V-ini_x) + ini_y; |
Lightvalve | 13:747daba9cf59 | 1297 | break; |
Lightvalve | 13:747daba9cf59 | 1298 | } |
Lightvalve | 13:747daba9cf59 | 1299 | } |
Lightvalve | 13:747daba9cf59 | 1300 | } |
Lightvalve | 14:8e7590227d22 | 1301 | |
Lightvalve | 13:747daba9cf59 | 1302 | return PWM_duty; |
Lightvalve | 13:747daba9cf59 | 1303 | } |
jobuuu | 6:df07d3491e3a | 1304 | |
Lightvalve | 57:f4819de54e7a | 1305 | |
Lightvalve | 57:f4819de54e7a | 1306 | |
Lightvalve | 57:f4819de54e7a | 1307 | |
Lightvalve | 57:f4819de54e7a | 1308 | |
jobuuu | 2:a1c0a37df760 | 1309 | /******************************************************************************* |
jobuuu | 2:a1c0a37df760 | 1310 | TIMER INTERRUPT |
jobuuu | 2:a1c0a37df760 | 1311 | *******************************************************************************/ |
jobuuu | 2:a1c0a37df760 | 1312 | |
Lightvalve | 51:b46bed7fec80 | 1313 | float FREQ_TMR4 = (float)FREQ_20k; |
Lightvalve | 51:b46bed7fec80 | 1314 | float DT_TMR4 = (float)DT_20k; |
Lightvalve | 57:f4819de54e7a | 1315 | long CNT_TMR4 = 0; |
Lightvalve | 57:f4819de54e7a | 1316 | int TMR4_FREQ_10k = (int)FREQ_10k; |
jobuuu | 1:e04e563be5ce | 1317 | extern "C" void TIM4_IRQHandler(void) |
jobuuu | 1:e04e563be5ce | 1318 | { |
Lightvalve | 19:23b7c1ad8683 | 1319 | if (TIM4->SR & TIM_SR_UIF ) { |
Lightvalve | 21:e5f1a43ea6f9 | 1320 | |
Lightvalve | 21:e5f1a43ea6f9 | 1321 | /******************************************************* |
Lightvalve | 21:e5f1a43ea6f9 | 1322 | *** Sensor Read & Data Handling |
Lightvalve | 21:e5f1a43ea6f9 | 1323 | ********************************************************/ |
Lightvalve | 13:747daba9cf59 | 1324 | |
Lightvalve | 57:f4819de54e7a | 1325 | //Encoder |
Lightvalve | 57:f4819de54e7a | 1326 | if (CNT_TMR4 % (int) ((int) FREQ_TMR4/TMR4_FREQ_10k) == 0) { |
Lightvalve | 57:f4819de54e7a | 1327 | ENC_UPDATE(); |
Lightvalve | 57:f4819de54e7a | 1328 | } |
Lightvalve | 61:bc8c8270f0ab | 1329 | |
Lightvalve | 61:bc8c8270f0ab | 1330 | ADC1->CR2 |= 0x40000000; |
Lightvalve | 58:2eade98630e2 | 1331 | if (SENSING_MODE == 0) { |
Lightvalve | 58:2eade98630e2 | 1332 | // Torque Sensing (0~210)bar ============================================= |
Lightvalve | 58:2eade98630e2 | 1333 | float pres_A_new = (((float) ADC1->DR) - 2047.5f); |
Lightvalve | 58:2eade98630e2 | 1334 | double alpha_update_ft = 1.0f / (1.0f + FREQ_TMR4 / (2.0f * 3.14f * 100.0f)); // f_cutoff : 200Hz |
Lightvalve | 58:2eade98630e2 | 1335 | pres_A.sen = (1.0f - alpha_update_ft) * pres_A.sen + alpha_update_ft * pres_A_new; |
Lightvalve | 67:c2812cf26c38 | 1336 | torq.sen = -pres_A.sen / TORQUE_SENSOR_PULSE_PER_TORQUE; |
Lightvalve | 67:c2812cf26c38 | 1337 | |
Lightvalve | 67:c2812cf26c38 | 1338 | |
Lightvalve | 67:c2812cf26c38 | 1339 | // float alpha_update_pres_A = 1.0f/(1.0f + FREQ_TMR4/(2.0f*3.14f*100.0f)); |
Lightvalve | 67:c2812cf26c38 | 1340 | //// float pres_A_new = ((float)ADC1->DR - PRES_A_NULL) / PRES_SENSOR_A_PULSE_PER_BAR; |
Lightvalve | 67:c2812cf26c38 | 1341 | // float pres_A_new = ((float)ADC1->DR); |
Lightvalve | 67:c2812cf26c38 | 1342 | // pres_A.sen = pres_A.sen*(1.0f-alpha_update_pres_A)+pres_A_new*(alpha_update_pres_A); |
Lightvalve | 67:c2812cf26c38 | 1343 | // torq.sen = - (pres_A.sen-2048.0f); //pulse -2047~2047 |
Lightvalve | 67:c2812cf26c38 | 1344 | |
Lightvalve | 17:1865016ca2e7 | 1345 | |
Lightvalve | 58:2eade98630e2 | 1346 | } else if (SENSING_MODE == 1) { |
Lightvalve | 58:2eade98630e2 | 1347 | // Pressure Sensing (0~210)bar ============================================= |
Lightvalve | 58:2eade98630e2 | 1348 | float pres_A_new = (((float)ADC1->DR) - PRES_A_NULL); |
Lightvalve | 58:2eade98630e2 | 1349 | float pres_B_new = (((float)ADC2->DR) - PRES_B_NULL); |
Lightvalve | 58:2eade98630e2 | 1350 | double alpha_update_pres = 1.0f / (1.0f + FREQ_TMR4 / (2.0f * 3.14f * 200.0f)); // f_cutoff : 500Hz |
Lightvalve | 58:2eade98630e2 | 1351 | pres_A.sen = (1.0f - alpha_update_pres) * pres_A.sen + alpha_update_pres * pres_A_new; |
Lightvalve | 58:2eade98630e2 | 1352 | pres_B.sen = (1.0f - alpha_update_pres) * pres_B.sen + alpha_update_pres * pres_B_new; |
Lightvalve | 58:2eade98630e2 | 1353 | CUR_PRES_A_BAR = pres_A.sen / PRES_SENSOR_A_PULSE_PER_BAR; |
Lightvalve | 58:2eade98630e2 | 1354 | CUR_PRES_B_BAR = pres_B.sen / PRES_SENSOR_B_PULSE_PER_BAR; |
Lightvalve | 50:3c630b5eba9f | 1355 | |
Lightvalve | 58:2eade98630e2 | 1356 | if ((OPERATING_MODE & 0x01) == 0) { // Rotary Actuator |
Lightvalve | 58:2eade98630e2 | 1357 | 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 | 1358 | } else if ((OPERATING_MODE & 0x01) == 1) { // Linear Actuator |
Lightvalve | 58:2eade98630e2 | 1359 | 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 | 1360 | } |
Lightvalve | 58:2eade98630e2 | 1361 | } |
Lightvalve | 61:bc8c8270f0ab | 1362 | |
Lightvalve | 58:2eade98630e2 | 1363 | // //Pressure sensor A |
Lightvalve | 58:2eade98630e2 | 1364 | // ADC1->CR2 |= 0x40000000; // adc _ 12bit |
Lightvalve | 58:2eade98630e2 | 1365 | // //while((ADC1->SR & 0b10)); |
Lightvalve | 58:2eade98630e2 | 1366 | // float alpha_update_pres_A = 1.0f/(1.0f + FREQ_TMR4/(2.0f*3.14f*100.0f)); |
Lightvalve | 58:2eade98630e2 | 1367 | // float pres_A_new = ((float)ADC1->DR); |
Lightvalve | 58:2eade98630e2 | 1368 | // pres_A.sen = pres_A.sen*(1.0f-alpha_update_pres_A)+pres_A_new*(alpha_update_pres_A); |
Lightvalve | 58:2eade98630e2 | 1369 | // 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 | 1370 | // |
Lightvalve | 58:2eade98630e2 | 1371 | // |
Lightvalve | 58:2eade98630e2 | 1372 | // //Pressure sensor B |
Lightvalve | 58:2eade98630e2 | 1373 | // float alpha_update_pres_B = 1.0f/(1.0f + FREQ_TMR4/(2.0f*3.14f*100.0f)); |
Lightvalve | 58:2eade98630e2 | 1374 | // float pres_B_new = ((float)ADC2->DR); |
Lightvalve | 58:2eade98630e2 | 1375 | // pres_B.sen = pres_B.sen*(1.0f-alpha_update_pres_B)+pres_B_new*(alpha_update_pres_B); |
Lightvalve | 58:2eade98630e2 | 1376 | // //torq.sen = pres_A.sen * (float) PISTON_AREA_A - pres_B.sen * (float) PISTON_AREA_B; |
Lightvalve | 17:1865016ca2e7 | 1377 | |
Lightvalve | 17:1865016ca2e7 | 1378 | |
Lightvalve | 21:e5f1a43ea6f9 | 1379 | //Current |
Lightvalve | 21:e5f1a43ea6f9 | 1380 | //ADC3->CR2 |= 0x40000000; // adc _ 12bit |
Lightvalve | 25:3e6b574cab5c | 1381 | //int raw_cur = ADC3->DR; |
Lightvalve | 21:e5f1a43ea6f9 | 1382 | //while((ADC3->SR & 0b10)); |
Lightvalve | 51:b46bed7fec80 | 1383 | float alpha_update_cur = 1.0f/(1.0f + FREQ_TMR4/(2.0f*3.14f*500.0f)); // f_cutoff : 500Hz |
Lightvalve | 30:8d561f16383b | 1384 | float cur_new = ((float)ADC3->DR-2048.0f)*20.0f/4096.0f; // unit : mA |
Lightvalve | 30:8d561f16383b | 1385 | cur.sen=cur.sen*(1.0f-alpha_update_cur)+cur_new*(alpha_update_cur); |
Lightvalve | 25:3e6b574cab5c | 1386 | //cur.sen = raw_cur; |
Lightvalve | 58:2eade98630e2 | 1387 | |
Lightvalve | 57:f4819de54e7a | 1388 | CNT_TMR4++; |
Lightvalve | 21:e5f1a43ea6f9 | 1389 | } |
Lightvalve | 11:82d8768d7351 | 1390 | TIM4->SR = 0x0; // reset the status register |
Lightvalve | 11:82d8768d7351 | 1391 | } |
Lightvalve | 19:23b7c1ad8683 | 1392 | |
Lightvalve | 19:23b7c1ad8683 | 1393 | |
Lightvalve | 18:b8adf1582ea3 | 1394 | int j =0; |
Lightvalve | 54:647072f5307a | 1395 | float FREQ_TMR3 = (float)FREQ_5k; |
Lightvalve | 48:889798ff9329 | 1396 | float DT_TMR3 = (float)DT_5k; |
Lightvalve | 45:35fa6884d0c6 | 1397 | int cnt_trans = 0; |
Lightvalve | 48:889798ff9329 | 1398 | double VALVE_POS_RAW_FORCE_FB_LOGGING = 0.0f; |
Lightvalve | 57:f4819de54e7a | 1399 | int can_rest =0; |
Lightvalve | 48:889798ff9329 | 1400 | |
Lightvalve | 11:82d8768d7351 | 1401 | extern "C" void TIM3_IRQHandler(void) |
Lightvalve | 21:e5f1a43ea6f9 | 1402 | { |
Lightvalve | 19:23b7c1ad8683 | 1403 | if (TIM3->SR & TIM_SR_UIF ) { |
Lightvalve | 57:f4819de54e7a | 1404 | |
Lightvalve | 57:f4819de54e7a | 1405 | if (((OPERATING_MODE&0b110)>>1) == 0) { |
Lightvalve | 57:f4819de54e7a | 1406 | K_v = 0.4f; // Moog (LPM >> mA) , 100bar |
Lightvalve | 57:f4819de54e7a | 1407 | mV_PER_mA = 500.0f; // 5000mV/10mA |
Lightvalve | 57:f4819de54e7a | 1408 | mV_PER_pulse = 0.5f; // 5000mV/10000pulse |
Lightvalve | 57:f4819de54e7a | 1409 | mA_PER_pulse = 0.001f; // 10mA/10000pulse |
Lightvalve | 57:f4819de54e7a | 1410 | } else if (((OPERATING_MODE&0b110)>>1) == 1) { |
Lightvalve | 57:f4819de54e7a | 1411 | K_v = 0.5f; // KNR (LPM >> mA) , 100bar |
Lightvalve | 57:f4819de54e7a | 1412 | mV_PER_mA = 166.6666f; // 5000mV/30mA |
Lightvalve | 57:f4819de54e7a | 1413 | mV_PER_pulse = 0.5f; // 5000mV/10000pulse |
Lightvalve | 57:f4819de54e7a | 1414 | mA_PER_pulse = 0.003f; // 30mA/10000pulse |
Lightvalve | 57:f4819de54e7a | 1415 | } |
Lightvalve | 50:3c630b5eba9f | 1416 | |
Lightvalve | 50:3c630b5eba9f | 1417 | if(MODE_POS_FT_TRANS == 1) { |
Lightvalve | 48:889798ff9329 | 1418 | alpha_trans = (float)(1.0f - cos(3.141592f * (float)cnt_trans * DT_TMR3 /3.0f))/2.0f; |
Lightvalve | 45:35fa6884d0c6 | 1419 | cnt_trans++; |
Lightvalve | 46:2694daea349b | 1420 | torq.err_sum = 0; |
Lightvalve | 48:889798ff9329 | 1421 | if((float)cnt_trans * DT_TMR3 > 3.0f) |
Lightvalve | 45:35fa6884d0c6 | 1422 | MODE_POS_FT_TRANS = 2; |
Lightvalve | 50:3c630b5eba9f | 1423 | } else if(MODE_POS_FT_TRANS == 3) { |
Lightvalve | 48:889798ff9329 | 1424 | alpha_trans = (float)(1.0f + cos(3.141592f * (float)cnt_trans * DT_TMR3 /3.0f))/2.0f; |
Lightvalve | 45:35fa6884d0c6 | 1425 | cnt_trans++; |
Lightvalve | 46:2694daea349b | 1426 | torq.err_sum = 0; |
Lightvalve | 48:889798ff9329 | 1427 | if((float) cnt_trans * DT_TMR3 > 3.0f ) |
Lightvalve | 45:35fa6884d0c6 | 1428 | MODE_POS_FT_TRANS = 0; |
Lightvalve | 50:3c630b5eba9f | 1429 | } else if(MODE_POS_FT_TRANS == 2) { |
Lightvalve | 58:2eade98630e2 | 1430 | alpha_trans = 1.0f; |
Lightvalve | 45:35fa6884d0c6 | 1431 | cnt_trans = 0; |
Lightvalve | 50:3c630b5eba9f | 1432 | } else { |
Lightvalve | 58:2eade98630e2 | 1433 | alpha_trans = 0.0f; |
Lightvalve | 45:35fa6884d0c6 | 1434 | cnt_trans = 0; |
Lightvalve | 45:35fa6884d0c6 | 1435 | } |
Lightvalve | 45:35fa6884d0c6 | 1436 | |
Lightvalve | 50:3c630b5eba9f | 1437 | |
Lightvalve | 57:f4819de54e7a | 1438 | int UTILITY_MODE = 0; |
Lightvalve | 57:f4819de54e7a | 1439 | int CONTROL_MODE = 0; |
Lightvalve | 58:2eade98630e2 | 1440 | |
Lightvalve | 57:f4819de54e7a | 1441 | if (CONTROL_UTILITY_MODE >= 20 || CONTROL_UTILITY_MODE == 0) { |
Lightvalve | 57:f4819de54e7a | 1442 | UTILITY_MODE = CONTROL_UTILITY_MODE; |
Lightvalve | 57:f4819de54e7a | 1443 | CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 57:f4819de54e7a | 1444 | } else { |
Lightvalve | 57:f4819de54e7a | 1445 | CONTROL_MODE = CONTROL_UTILITY_MODE; |
Lightvalve | 57:f4819de54e7a | 1446 | UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 57:f4819de54e7a | 1447 | } |
Lightvalve | 56:6f50d9d3bfee | 1448 | |
Lightvalve | 56:6f50d9d3bfee | 1449 | |
Lightvalve | 56:6f50d9d3bfee | 1450 | |
Lightvalve | 57:f4819de54e7a | 1451 | // UTILITY MODE ------------------------------------------------------------ |
Lightvalve | 56:6f50d9d3bfee | 1452 | |
Lightvalve | 57:f4819de54e7a | 1453 | switch (UTILITY_MODE) { |
Lightvalve | 57:f4819de54e7a | 1454 | case MODE_NO_ACT: { |
Lightvalve | 13:747daba9cf59 | 1455 | break; |
Lightvalve | 13:747daba9cf59 | 1456 | } |
Lightvalve | 14:8e7590227d22 | 1457 | |
Lightvalve | 14:8e7590227d22 | 1458 | case MODE_TORQUE_SENSOR_NULLING: { |
Lightvalve | 13:747daba9cf59 | 1459 | // DAC Voltage reference set |
Lightvalve | 14:8e7590227d22 | 1460 | if (TMR3_COUNT_TORQUE_NULL < TMR_FREQ_5k * 2) { |
Lightvalve | 15:bd0d12728506 | 1461 | CUR_TORQUE_sum += torq.sen; |
Lightvalve | 19:23b7c1ad8683 | 1462 | |
Lightvalve | 14:8e7590227d22 | 1463 | if (TMR3_COUNT_TORQUE_NULL % 10 == 0) { |
Lightvalve | 30:8d561f16383b | 1464 | CUR_TORQUE_mean = CUR_TORQUE_sum / 10.0f; |
Lightvalve | 13:747daba9cf59 | 1465 | CUR_TORQUE_sum = 0; |
Lightvalve | 14:8e7590227d22 | 1466 | |
Lightvalve | 84:c355d3e52bf1 | 1467 | TORQUE_VREF += 0.000003f * (0.0f - CUR_TORQUE_mean); |
Lightvalve | 19:23b7c1ad8683 | 1468 | |
Lightvalve | 30:8d561f16383b | 1469 | if (TORQUE_VREF > 3.3f) TORQUE_VREF = 3.3f; |
Lightvalve | 59:f308b1656d9c | 1470 | if (TORQUE_VREF < 0.0f) TORQUE_VREF = 0.0f; |
Lightvalve | 50:3c630b5eba9f | 1471 | |
Lightvalve | 16:903b5a4433b4 | 1472 | //spi_eeprom_write(RID_TORQUE_SENSOR_VREF, (int16_t) (TORQUE_VREF * 1000.0)); |
Lightvalve | 30:8d561f16383b | 1473 | dac_1 = TORQUE_VREF / 3.3f; |
Lightvalve | 13:747daba9cf59 | 1474 | } |
Lightvalve | 13:747daba9cf59 | 1475 | } else { |
Lightvalve | 58:2eade98630e2 | 1476 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 14:8e7590227d22 | 1477 | TMR3_COUNT_TORQUE_NULL = 0; |
Lightvalve | 13:747daba9cf59 | 1478 | CUR_TORQUE_sum = 0; |
Lightvalve | 13:747daba9cf59 | 1479 | CUR_TORQUE_mean = 0; |
Lightvalve | 19:23b7c1ad8683 | 1480 | |
Lightvalve | 170:42c938a40313 | 1481 | // ROM_RESET_DATA(); |
Lightvalve | 170:42c938a40313 | 1482 | spi_eeprom_write(RID_TORQUE_SENSOR_VREF, (int16_t) (TORQUE_VREF * 1000.0f)); |
Lightvalve | 19:23b7c1ad8683 | 1483 | |
Lightvalve | 30:8d561f16383b | 1484 | dac_1 = TORQUE_VREF / 3.3f; |
Lightvalve | 14:8e7590227d22 | 1485 | |
Lightvalve | 13:747daba9cf59 | 1486 | } |
Lightvalve | 14:8e7590227d22 | 1487 | TMR3_COUNT_TORQUE_NULL++; |
Lightvalve | 13:747daba9cf59 | 1488 | break; |
Lightvalve | 19:23b7c1ad8683 | 1489 | } |
Lightvalve | 14:8e7590227d22 | 1490 | |
Lightvalve | 50:3c630b5eba9f | 1491 | // case MODE_VALVE_NULLING_AND_DEADZONE_SETTING: { |
Lightvalve | 50:3c630b5eba9f | 1492 | // if (TMR3_COUNT_DEADZONE == 0) { |
Lightvalve | 50:3c630b5eba9f | 1493 | // if (pos_plus_end == pos_minus_end) need_enc_init = true; |
Lightvalve | 50:3c630b5eba9f | 1494 | // else temp_time = 0; |
Lightvalve | 50:3c630b5eba9f | 1495 | // } |
Lightvalve | 50:3c630b5eba9f | 1496 | // if (need_enc_init) { |
Lightvalve | 50:3c630b5eba9f | 1497 | // if (TMR3_COUNT_DEADZONE < (int) (0.5f * (float) TMR_FREQ_5k)) { |
Lightvalve | 50:3c630b5eba9f | 1498 | // V_out = VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 50:3c630b5eba9f | 1499 | // pos_plus_end = pos.sen; |
Lightvalve | 50:3c630b5eba9f | 1500 | // } else if (TMR3_COUNT_DEADZONE < TMR_FREQ_5k) { |
Lightvalve | 50:3c630b5eba9f | 1501 | // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 50:3c630b5eba9f | 1502 | // pos_minus_end = pos.sen; |
Lightvalve | 50:3c630b5eba9f | 1503 | // } else if (TMR3_COUNT_DEADZONE == TMR_FREQ_5k) need_enc_init = false; |
Lightvalve | 50:3c630b5eba9f | 1504 | // temp_time = TMR_FREQ_5k; |
Lightvalve | 50:3c630b5eba9f | 1505 | // } |
Lightvalve | 50:3c630b5eba9f | 1506 | // |
Lightvalve | 50:3c630b5eba9f | 1507 | // if (temp_time <= TMR3_COUNT_DEADZONE && TMR3_COUNT_DEADZONE < (temp_time + TMR_FREQ_5k)) { |
Lightvalve | 50:3c630b5eba9f | 1508 | // 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 | 1509 | // VALVE_CENTER = VALVE_DEADZONE_PLUS = VALVE_DEADZONE_MINUS = 0; |
Lightvalve | 50:3c630b5eba9f | 1510 | // |
Lightvalve | 50:3c630b5eba9f | 1511 | // } else if (temp_time <= TMR3_COUNT_DEADZONE && TMR3_COUNT_DEADZONE < (temp_time + (int) (1.9f * (float) TMR_FREQ_5k))) { |
Lightvalve | 50:3c630b5eba9f | 1512 | // V_out = 0; |
Lightvalve | 50:3c630b5eba9f | 1513 | // CUR_VELOCITY_sum += CUR_VELOCITY; |
Lightvalve | 50:3c630b5eba9f | 1514 | // } else if (TMR3_COUNT_DEADZONE == (temp_time + 2 * TMR_FREQ_5k)) { |
Lightvalve | 50:3c630b5eba9f | 1515 | // if (CUR_VELOCITY_sum == 0) DZ_dir = 1; |
Lightvalve | 50:3c630b5eba9f | 1516 | // else if (CUR_VELOCITY_sum > 0) DZ_dir = 1; |
Lightvalve | 50:3c630b5eba9f | 1517 | // else if (CUR_VELOCITY_sum < 0) DZ_dir = -1; |
Lightvalve | 50:3c630b5eba9f | 1518 | // else DZ_temp_cnt2 = DZ_end; |
Lightvalve | 50:3c630b5eba9f | 1519 | // CUR_VELOCITY_sum = 0; |
Lightvalve | 50:3c630b5eba9f | 1520 | // } else if (TMR3_COUNT_DEADZONE > (temp_time + 2 * TMR_FREQ_5k)) { |
Lightvalve | 50:3c630b5eba9f | 1521 | // if (TMR3_COUNT_DEADZONE > (temp_time + 10 * TMR_FREQ_5k)) DZ_temp_cnt2 = DZ_end; |
Lightvalve | 50:3c630b5eba9f | 1522 | // |
Lightvalve | 50:3c630b5eba9f | 1523 | // // Position of Dead Zone |
Lightvalve | 50:3c630b5eba9f | 1524 | // // (CUR_VELOCITY < 0) (CUR_VELOCITY == 0) (CUR_VELOCITY > 0) |
Lightvalve | 50:3c630b5eba9f | 1525 | // // | / | / |/ |
Lightvalve | 50:3c630b5eba9f | 1526 | // // | ______/ ___|___/ ______/| |
Lightvalve | 50:3c630b5eba9f | 1527 | // // |/ / | / | |
Lightvalve | 50:3c630b5eba9f | 1528 | // // /| / | / | |
Lightvalve | 50:3c630b5eba9f | 1529 | // // 0V 0V 0V |
Lightvalve | 50:3c630b5eba9f | 1530 | // |
Lightvalve | 50:3c630b5eba9f | 1531 | // if (DZ_temp_cnt2 < DZ_end) { |
Lightvalve | 50:3c630b5eba9f | 1532 | // if (TMR3_COUNT_DEADZONE % 20 != 0) { |
Lightvalve | 50:3c630b5eba9f | 1533 | // CUR_VELOCITY_sum += CUR_VELOCITY; |
Lightvalve | 50:3c630b5eba9f | 1534 | // } else { |
Lightvalve | 50:3c630b5eba9f | 1535 | // V_out -= DZ_dir; |
Lightvalve | 50:3c630b5eba9f | 1536 | // if (CUR_VELOCITY_sum * DZ_dir < 0) DZ_temp_cnt++; |
Lightvalve | 50:3c630b5eba9f | 1537 | // CUR_VELOCITY_sum = 0; |
Lightvalve | 50:3c630b5eba9f | 1538 | // } |
Lightvalve | 50:3c630b5eba9f | 1539 | // if (DZ_temp_cnt == 5) { |
Lightvalve | 50:3c630b5eba9f | 1540 | // if (DZ_dir >= 0) VALVE_DEADZONE_MINUS = (int16_t) V_out; |
Lightvalve | 50:3c630b5eba9f | 1541 | // else VALVE_DEADZONE_PLUS = (int16_t) V_out; |
Lightvalve | 50:3c630b5eba9f | 1542 | // DZ_dir = -DZ_dir; |
Lightvalve | 50:3c630b5eba9f | 1543 | // DZ_temp_cnt = 0; |
Lightvalve | 50:3c630b5eba9f | 1544 | // DZ_temp_cnt2++; |
Lightvalve | 50:3c630b5eba9f | 1545 | // } |
Lightvalve | 50:3c630b5eba9f | 1546 | // } else { |
Lightvalve | 50:3c630b5eba9f | 1547 | // TMR3_COUNT_DEADZONE = -1; |
Lightvalve | 50:3c630b5eba9f | 1548 | // VALVE_CENTER = VALVE_DEADZONE_PLUS / 2 + VALVE_DEADZONE_MINUS / 2; |
Lightvalve | 50:3c630b5eba9f | 1549 | // if (VALVE_DEADZONE_PLUS < VALVE_DEADZONE_MINUS) { |
Lightvalve | 50:3c630b5eba9f | 1550 | // VALVE_DEADZONE_PLUS = VALVE_CENTER; |
Lightvalve | 50:3c630b5eba9f | 1551 | // VALVE_DEADZONE_MINUS = VALVE_CENTER; |
Lightvalve | 50:3c630b5eba9f | 1552 | // } |
Lightvalve | 50:3c630b5eba9f | 1553 | // V_out = 0; |
Lightvalve | 50:3c630b5eba9f | 1554 | // |
Lightvalve | 50:3c630b5eba9f | 1555 | // ROM_RESET_DATA(); |
Lightvalve | 50:3c630b5eba9f | 1556 | // |
Lightvalve | 50:3c630b5eba9f | 1557 | // //spi_eeprom_write(RID_VALVE_DEADZONE_PLUS, VALVE_DEADZONE_PLUS); |
Lightvalve | 50:3c630b5eba9f | 1558 | // //spi_eeprom_write(RID_VALVE_DEADZONE_MINUS, VALVE_DEADZONE_MINUS); |
Lightvalve | 50:3c630b5eba9f | 1559 | // |
Lightvalve | 50:3c630b5eba9f | 1560 | // CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 50:3c630b5eba9f | 1561 | // DZ_temp_cnt2 = 0; |
Lightvalve | 50:3c630b5eba9f | 1562 | // } |
Lightvalve | 50:3c630b5eba9f | 1563 | // } |
Lightvalve | 50:3c630b5eba9f | 1564 | // TMR3_COUNT_DEADZONE++; |
Lightvalve | 50:3c630b5eba9f | 1565 | // break; |
Lightvalve | 50:3c630b5eba9f | 1566 | // } |
Lightvalve | 14:8e7590227d22 | 1567 | |
Lightvalve | 14:8e7590227d22 | 1568 | case MODE_FIND_HOME: { |
Lightvalve | 29:69f3f5445d6d | 1569 | if (FINDHOME_STAGE == FINDHOME_INIT) { |
Lightvalve | 14:8e7590227d22 | 1570 | cnt_findhome = 0; |
Lightvalve | 14:8e7590227d22 | 1571 | cnt_vel_findhome = 0; |
Lightvalve | 14:8e7590227d22 | 1572 | //REFERENCE_MODE = MODE_REF_NO_ACT; // Stop taking reference data from PODO |
Lightvalve | 59:f308b1656d9c | 1573 | pos.ref = pos.sen; |
Lightvalve | 59:f308b1656d9c | 1574 | vel.ref = 0.0f; |
Lightvalve | 29:69f3f5445d6d | 1575 | FINDHOME_STAGE = FINDHOME_GOTOLIMIT; |
Lightvalve | 29:69f3f5445d6d | 1576 | } else if (FINDHOME_STAGE == FINDHOME_GOTOLIMIT) { |
Lightvalve | 67:c2812cf26c38 | 1577 | int cnt_check_enc = (TMR_FREQ_5k/20); |
Lightvalve | 29:69f3f5445d6d | 1578 | if(cnt_findhome%cnt_check_enc == 0) { |
Lightvalve | 29:69f3f5445d6d | 1579 | FINDHOME_POSITION = pos.sen; |
Lightvalve | 29:69f3f5445d6d | 1580 | FINDHOME_VELOCITY = FINDHOME_POSITION - FINDHOME_POSITION_OLD; |
Lightvalve | 29:69f3f5445d6d | 1581 | FINDHOME_POSITION_OLD = FINDHOME_POSITION; |
Lightvalve | 29:69f3f5445d6d | 1582 | } |
Lightvalve | 29:69f3f5445d6d | 1583 | cnt_findhome++; |
Lightvalve | 14:8e7590227d22 | 1584 | |
Lightvalve | 29:69f3f5445d6d | 1585 | if (abs(FINDHOME_VELOCITY) <= 1) { |
Lightvalve | 29:69f3f5445d6d | 1586 | cnt_vel_findhome = cnt_vel_findhome + 1; |
Lightvalve | 29:69f3f5445d6d | 1587 | } else { |
Lightvalve | 29:69f3f5445d6d | 1588 | cnt_vel_findhome = 0; |
Lightvalve | 29:69f3f5445d6d | 1589 | } |
Lightvalve | 19:23b7c1ad8683 | 1590 | |
Lightvalve | 57:f4819de54e7a | 1591 | if ((cnt_vel_findhome < 3*TMR_FREQ_5k) && cnt_findhome < 10*TMR_FREQ_5k) { // wait for 3sec |
Lightvalve | 29:69f3f5445d6d | 1592 | //REFERENCE_MODE = MODE_REF_NO_ACT; |
Lightvalve | 67:c2812cf26c38 | 1593 | if (HOMEPOS_OFFSET > 0) pos.ref = pos.ref + 12.0f; |
Lightvalve | 67:c2812cf26c38 | 1594 | else pos.ref = pos.ref - 12.0f; |
Lightvalve | 61:bc8c8270f0ab | 1595 | |
Lightvalve | 59:f308b1656d9c | 1596 | // pos.err = pos.ref_home_pos - pos.sen; |
Lightvalve | 59:f308b1656d9c | 1597 | // float VALVE_POS_RAW_POS_FB = 0.0f; |
Lightvalve | 59:f308b1656d9c | 1598 | // VALVE_POS_RAW_POS_FB = (float) P_GAIN_JOINT_POSITION * pos.err/(float) ENC_PULSE_PER_POSITION * 0.01f; |
Lightvalve | 59:f308b1656d9c | 1599 | // valve_pos.ref = VALVE_POS_RAW_POS_FB + (float) VALVE_CENTER; |
Lightvalve | 59:f308b1656d9c | 1600 | // VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 61:bc8c8270f0ab | 1601 | |
Lightvalve | 59:f308b1656d9c | 1602 | CONTROL_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 59:f308b1656d9c | 1603 | alpha_trans = 0.0f; |
Lightvalve | 61:bc8c8270f0ab | 1604 | |
Lightvalve | 34:bb2ca2fc2a8e | 1605 | |
Lightvalve | 29:69f3f5445d6d | 1606 | } else { |
Lightvalve | 29:69f3f5445d6d | 1607 | ENC_SET(HOMEPOS_OFFSET); |
Lightvalve | 67:c2812cf26c38 | 1608 | // ENC_SET_ZERO(); |
Lightvalve | 29:69f3f5445d6d | 1609 | INIT_REF_POS = HOMEPOS_OFFSET; |
Lightvalve | 29:69f3f5445d6d | 1610 | REF_POSITION = 0; |
Lightvalve | 29:69f3f5445d6d | 1611 | REF_VELOCITY = 0; |
Lightvalve | 29:69f3f5445d6d | 1612 | FINDHOME_POSITION = 0; |
Lightvalve | 29:69f3f5445d6d | 1613 | FINDHOME_POSITION_OLD = 0; |
Lightvalve | 29:69f3f5445d6d | 1614 | FINDHOME_VELOCITY = 0; |
Lightvalve | 29:69f3f5445d6d | 1615 | cnt_findhome = 0; |
Lightvalve | 29:69f3f5445d6d | 1616 | cnt_vel_findhome = 0; |
Lightvalve | 29:69f3f5445d6d | 1617 | FINDHOME_STAGE = FINDHOME_ZEROPOSE; |
Lightvalve | 67:c2812cf26c38 | 1618 | |
Lightvalve | 67:c2812cf26c38 | 1619 | |
Lightvalve | 67:c2812cf26c38 | 1620 | cnt_findhome = 0; |
Lightvalve | 67:c2812cf26c38 | 1621 | pos.ref = 0.0f; |
Lightvalve | 67:c2812cf26c38 | 1622 | vel.ref = 0.0f; |
Lightvalve | 67:c2812cf26c38 | 1623 | pos.ref_home_pos = 0.0f; |
Lightvalve | 67:c2812cf26c38 | 1624 | vel.ref_home_pos = 0.0f; |
Lightvalve | 67:c2812cf26c38 | 1625 | //FINDHOME_STAGE = FINDHOME_INIT; |
Lightvalve | 67:c2812cf26c38 | 1626 | //CONTROL_UTILITY_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 169:645207e160ca | 1627 | |
Lightvalve | 169:645207e160ca | 1628 | |
Lightvalve | 29:69f3f5445d6d | 1629 | } |
Lightvalve | 29:69f3f5445d6d | 1630 | } else if (FINDHOME_STAGE == FINDHOME_ZEROPOSE) { |
Lightvalve | 29:69f3f5445d6d | 1631 | int T_move = 2*TMR_FREQ_5k; |
Lightvalve | 59:f308b1656d9c | 1632 | 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 | 1633 | //pos.ref = 0.0f; |
Lightvalve | 59:f308b1656d9c | 1634 | vel.ref = 0.0f; |
Lightvalve | 169:645207e160ca | 1635 | |
Lightvalve | 29:69f3f5445d6d | 1636 | // input for position control |
Lightvalve | 169:645207e160ca | 1637 | |
Lightvalve | 67:c2812cf26c38 | 1638 | // CONTROL_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 67:c2812cf26c38 | 1639 | alpha_trans = 0.0f; |
Lightvalve | 61:bc8c8270f0ab | 1640 | |
Lightvalve | 67:c2812cf26c38 | 1641 | double torq_ref = 0.0f; |
Lightvalve | 67:c2812cf26c38 | 1642 | pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm] |
Lightvalve | 67:c2812cf26c38 | 1643 | vel.err = (0.0f - vel.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm/s] |
Lightvalve | 67:c2812cf26c38 | 1644 | pos.err_sum += pos.err/(float) TMR_FREQ_5k; //[mm] |
Lightvalve | 67:c2812cf26c38 | 1645 | |
Lightvalve | 67:c2812cf26c38 | 1646 | if (((OPERATING_MODE&0b110)>>1) == 0 || ((OPERATING_MODE&0b110)>>1) == 1) { |
Lightvalve | 67:c2812cf26c38 | 1647 | |
Lightvalve | 67:c2812cf26c38 | 1648 | double I_REF_POS = 0.0f; |
Lightvalve | 67:c2812cf26c38 | 1649 | double I_REF_FORCE_FB = 0.0f; // I_REF by Force Feedback |
Lightvalve | 67:c2812cf26c38 | 1650 | double I_REF_VC = 0.0f; // I_REF for velocity compensation |
Lightvalve | 67:c2812cf26c38 | 1651 | |
Lightvalve | 67:c2812cf26c38 | 1652 | double temp_vel_pos = 0.0f; |
Lightvalve | 67:c2812cf26c38 | 1653 | double temp_vel_torq = 0.0f; |
Lightvalve | 67:c2812cf26c38 | 1654 | double wn_Pos = 2.0f * PI * 5.0f; // f_cut : 5Hz Position Control |
Lightvalve | 67:c2812cf26c38 | 1655 | |
Lightvalve | 69:3995ffeaa786 | 1656 | if ((OPERATING_MODE & 0x01) == 0) { // Rotary Mode |
Lightvalve | 67:c2812cf26c38 | 1657 | 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 | 1658 | // L when P-gain = 100, f_cut = 10Hz L feedforward velocity |
Lightvalve | 69:3995ffeaa786 | 1659 | } else if ((OPERATING_MODE & 0x01) == 1) { |
Lightvalve | 67:c2812cf26c38 | 1660 | 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 | 1661 | // L when P-gain = 100, f_cut = 10Hz L feedforward velocity |
Lightvalve | 67:c2812cf26c38 | 1662 | } |
Lightvalve | 67:c2812cf26c38 | 1663 | 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 | 1664 | else I_REF_POS = temp_vel_pos * ((double) PISTON_AREA_B * 0.00006f / (K_v * sqrt(2.0f / (alpha3 + 1.0f)))); |
Lightvalve | 67:c2812cf26c38 | 1665 | |
Lightvalve | 67:c2812cf26c38 | 1666 | I_REF = I_REF_POS; |
Lightvalve | 67:c2812cf26c38 | 1667 | |
Lightvalve | 67:c2812cf26c38 | 1668 | |
Lightvalve | 67:c2812cf26c38 | 1669 | |
Lightvalve | 67:c2812cf26c38 | 1670 | } else { |
Lightvalve | 67:c2812cf26c38 | 1671 | float VALVE_POS_RAW_FORCE_FB = 0.0f; |
Lightvalve | 67:c2812cf26c38 | 1672 | 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 | 1673 | |
Lightvalve | 67:c2812cf26c38 | 1674 | if (VALVE_POS_RAW_FORCE_FB >= 0) { |
Lightvalve | 67:c2812cf26c38 | 1675 | valve_pos.ref = VALVE_POS_RAW_FORCE_FB + VALVE_DEADZONE_PLUS; |
Lightvalve | 67:c2812cf26c38 | 1676 | } else { |
Lightvalve | 67:c2812cf26c38 | 1677 | valve_pos.ref = VALVE_POS_RAW_FORCE_FB + VALVE_DEADZONE_MINUS; |
Lightvalve | 67:c2812cf26c38 | 1678 | } |
Lightvalve | 67:c2812cf26c38 | 1679 | |
Lightvalve | 67:c2812cf26c38 | 1680 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 67:c2812cf26c38 | 1681 | |
Lightvalve | 67:c2812cf26c38 | 1682 | V_out = (float) Vout.ref; |
Lightvalve | 67:c2812cf26c38 | 1683 | |
Lightvalve | 67:c2812cf26c38 | 1684 | } |
Lightvalve | 67:c2812cf26c38 | 1685 | |
Lightvalve | 67:c2812cf26c38 | 1686 | |
Lightvalve | 169:645207e160ca | 1687 | |
Lightvalve | 169:645207e160ca | 1688 | |
Lightvalve | 59:f308b1656d9c | 1689 | // pos.err = pos.ref - (float)pos.sen; |
Lightvalve | 59:f308b1656d9c | 1690 | // float VALVE_POS_RAW_POS_FB = 0.0f; |
Lightvalve | 59:f308b1656d9c | 1691 | // VALVE_POS_RAW_POS_FB = (float) P_GAIN_JOINT_POSITION * 0.01f * pos.err/(float) ENC_PULSE_PER_POSITION; |
Lightvalve | 59:f308b1656d9c | 1692 | // valve_pos.ref = VALVE_POS_RAW_POS_FB + (float) VALVE_CENTER; |
Lightvalve | 59:f308b1656d9c | 1693 | // VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 29:69f3f5445d6d | 1694 | |
Lightvalve | 29:69f3f5445d6d | 1695 | cnt_findhome++; |
Lightvalve | 29:69f3f5445d6d | 1696 | if (cnt_findhome >= T_move) { |
Lightvalve | 29:69f3f5445d6d | 1697 | //REFERENCE_MODE = MODE_REF_DIRECT; |
Lightvalve | 29:69f3f5445d6d | 1698 | cnt_findhome = 0; |
Lightvalve | 30:8d561f16383b | 1699 | pos.ref = 0.0f; |
Lightvalve | 30:8d561f16383b | 1700 | vel.ref = 0.0f; |
Lightvalve | 30:8d561f16383b | 1701 | pos.ref_home_pos = 0.0f; |
Lightvalve | 30:8d561f16383b | 1702 | vel.ref_home_pos = 0.0f; |
Lightvalve | 29:69f3f5445d6d | 1703 | FINDHOME_STAGE = FINDHOME_INIT; |
Lightvalve | 57:f4819de54e7a | 1704 | CONTROL_UTILITY_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 29:69f3f5445d6d | 1705 | } |
Lightvalve | 13:747daba9cf59 | 1706 | } |
Lightvalve | 19:23b7c1ad8683 | 1707 | |
Lightvalve | 13:747daba9cf59 | 1708 | break; |
Lightvalve | 13:747daba9cf59 | 1709 | } |
Lightvalve | 14:8e7590227d22 | 1710 | |
Lightvalve | 50:3c630b5eba9f | 1711 | // case MODE_VALVE_GAIN_SETTING: { |
Lightvalve | 50:3c630b5eba9f | 1712 | // if (TMR3_COUNT_FLOWRATE == 0) { |
Lightvalve | 50:3c630b5eba9f | 1713 | // if (pos_plus_end == pos_minus_end) need_enc_init = true; |
Lightvalve | 50:3c630b5eba9f | 1714 | // else { |
Lightvalve | 50:3c630b5eba9f | 1715 | // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 50:3c630b5eba9f | 1716 | // temp_time = (int) (0.5f * (float) TMR_FREQ_5k); |
Lightvalve | 50:3c630b5eba9f | 1717 | // } |
Lightvalve | 50:3c630b5eba9f | 1718 | // } |
Lightvalve | 50:3c630b5eba9f | 1719 | // if (need_enc_init) { |
Lightvalve | 50:3c630b5eba9f | 1720 | // if (TMR3_COUNT_FLOWRATE < (int) (0.5f * (float) TMR_FREQ_5k)) { |
Lightvalve | 50:3c630b5eba9f | 1721 | // V_out = VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 50:3c630b5eba9f | 1722 | // pos_plus_end = pos.sen; |
Lightvalve | 50:3c630b5eba9f | 1723 | // } else if (TMR3_COUNT_FLOWRATE < TMR_FREQ_5k) { |
Lightvalve | 50:3c630b5eba9f | 1724 | // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 50:3c630b5eba9f | 1725 | // pos_minus_end = pos.sen; |
Lightvalve | 50:3c630b5eba9f | 1726 | // } else if (TMR3_COUNT_FLOWRATE == TMR_FREQ_5k) { |
Lightvalve | 50:3c630b5eba9f | 1727 | // need_enc_init = false; |
Lightvalve | 50:3c630b5eba9f | 1728 | // check_vel_pos_init = (int) (0.9f * (float) (pos_plus_end - pos_minus_end)); |
Lightvalve | 50:3c630b5eba9f | 1729 | // check_vel_pos_fin = (int) (0.95f * (float) (pos_plus_end - pos_minus_end)); |
Lightvalve | 50:3c630b5eba9f | 1730 | // check_vel_pos_interv = check_vel_pos_fin - check_vel_pos_init; |
Lightvalve | 50:3c630b5eba9f | 1731 | // } |
Lightvalve | 50:3c630b5eba9f | 1732 | // temp_time = TMR_FREQ_5k; |
Lightvalve | 50:3c630b5eba9f | 1733 | // } |
Lightvalve | 50:3c630b5eba9f | 1734 | // TMR3_COUNT_FLOWRATE++; |
Lightvalve | 50:3c630b5eba9f | 1735 | // if (TMR3_COUNT_FLOWRATE > temp_time) { |
Lightvalve | 50:3c630b5eba9f | 1736 | // if (flag_flowrate % 2 == 0) { // (+) |
Lightvalve | 50:3c630b5eba9f | 1737 | // VALVE_VOLTAGE = 1000.0f * (float) (flag_flowrate / 2 + 1); |
Lightvalve | 50:3c630b5eba9f | 1738 | // V_out = VALVE_VOLTAGE; |
Lightvalve | 50:3c630b5eba9f | 1739 | // if (pos.sen > (pos_minus_end + check_vel_pos_init) && pos.sen < (pos_minus_end + check_vel_pos_fin)) { |
Lightvalve | 50:3c630b5eba9f | 1740 | // fl_temp_cnt++; |
Lightvalve | 50:3c630b5eba9f | 1741 | // } else if (pos.sen >= (pos_minus_end + check_vel_pos_fin) && CUR_VELOCITY == 0) { |
Lightvalve | 50:3c630b5eba9f | 1742 | // 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 | 1743 | // // 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 | 1744 | // fl_temp_cnt2++; |
Lightvalve | 50:3c630b5eba9f | 1745 | // } |
Lightvalve | 50:3c630b5eba9f | 1746 | // } else if (flag_flowrate % 2 == 1) { // (-) |
Lightvalve | 50:3c630b5eba9f | 1747 | // VALVE_VOLTAGE = -1. * (float) (flag_flowrate / 2 + 1); |
Lightvalve | 50:3c630b5eba9f | 1748 | // V_out = VALVE_VOLTAGE; |
Lightvalve | 50:3c630b5eba9f | 1749 | // if (pos.sen < (pos_plus_end - check_vel_pos_init) && pos.sen > (pos_plus_end - check_vel_pos_fin)) { |
Lightvalve | 50:3c630b5eba9f | 1750 | // fl_temp_cnt++; |
Lightvalve | 50:3c630b5eba9f | 1751 | // } else if (pos.sen <= (pos_plus_end - check_vel_pos_fin) && CUR_VELOCITY == 0) { |
Lightvalve | 50:3c630b5eba9f | 1752 | // 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 | 1753 | // // 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 | 1754 | // fl_temp_cnt2++; |
Lightvalve | 50:3c630b5eba9f | 1755 | // } |
Lightvalve | 50:3c630b5eba9f | 1756 | // } |
Lightvalve | 50:3c630b5eba9f | 1757 | // if (fl_temp_cnt2 == 100) { |
Lightvalve | 50:3c630b5eba9f | 1758 | // |
Lightvalve | 50:3c630b5eba9f | 1759 | // ROM_RESET_DATA(); |
Lightvalve | 50:3c630b5eba9f | 1760 | // |
Lightvalve | 50:3c630b5eba9f | 1761 | // //spi_eeprom_write(RID_VALVE_GAIN_PLUS_1 + flag_flowrate, (int16_t) (VALVE_GAIN_LPM_PER_V[flag_flowrate] * 100.0f)); |
Lightvalve | 50:3c630b5eba9f | 1762 | // cur_vel_sum = 0; |
Lightvalve | 50:3c630b5eba9f | 1763 | // fl_temp_cnt = 0; |
Lightvalve | 50:3c630b5eba9f | 1764 | // fl_temp_cnt2 = 0; |
Lightvalve | 50:3c630b5eba9f | 1765 | // flag_flowrate++; |
Lightvalve | 50:3c630b5eba9f | 1766 | // } |
Lightvalve | 50:3c630b5eba9f | 1767 | // if (flag_flowrate == 10) { |
Lightvalve | 50:3c630b5eba9f | 1768 | // V_out = 0; |
Lightvalve | 50:3c630b5eba9f | 1769 | // flag_flowrate = 0; |
Lightvalve | 50:3c630b5eba9f | 1770 | // TMR3_COUNT_FLOWRATE = 0; |
Lightvalve | 50:3c630b5eba9f | 1771 | // valve_gain_repeat_cnt++; |
Lightvalve | 50:3c630b5eba9f | 1772 | // if (valve_gain_repeat_cnt >= 1) { |
Lightvalve | 50:3c630b5eba9f | 1773 | // CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 50:3c630b5eba9f | 1774 | // valve_gain_repeat_cnt = 0; |
Lightvalve | 50:3c630b5eba9f | 1775 | // } |
Lightvalve | 50:3c630b5eba9f | 1776 | // |
Lightvalve | 50:3c630b5eba9f | 1777 | // } |
Lightvalve | 50:3c630b5eba9f | 1778 | // break; |
Lightvalve | 50:3c630b5eba9f | 1779 | // } |
Lightvalve | 50:3c630b5eba9f | 1780 | // |
Lightvalve | 50:3c630b5eba9f | 1781 | // } |
Lightvalve | 14:8e7590227d22 | 1782 | case MODE_PRESSURE_SENSOR_NULLING: { |
Lightvalve | 13:747daba9cf59 | 1783 | // DAC Voltage reference set |
Lightvalve | 14:8e7590227d22 | 1784 | if (TMR3_COUNT_PRES_NULL < TMR_FREQ_5k * 2) { |
Lightvalve | 14:8e7590227d22 | 1785 | CUR_PRES_A_sum += pres_A.sen; |
Lightvalve | 14:8e7590227d22 | 1786 | CUR_PRES_B_sum += pres_B.sen; |
Lightvalve | 14:8e7590227d22 | 1787 | |
Lightvalve | 14:8e7590227d22 | 1788 | if (TMR3_COUNT_PRES_NULL % 10 == 0) { |
Lightvalve | 30:8d561f16383b | 1789 | CUR_PRES_A_mean = CUR_PRES_A_sum / 10.0f; |
Lightvalve | 30:8d561f16383b | 1790 | CUR_PRES_B_mean = CUR_PRES_B_sum / 10.0f; |
Lightvalve | 13:747daba9cf59 | 1791 | CUR_PRES_A_sum = 0; |
Lightvalve | 13:747daba9cf59 | 1792 | CUR_PRES_B_sum = 0; |
Lightvalve | 14:8e7590227d22 | 1793 | |
Lightvalve | 38:118df027d851 | 1794 | float VREF_NullingGain = 0.0003f; |
Lightvalve | 58:2eade98630e2 | 1795 | PRES_A_VREF = PRES_A_VREF + VREF_NullingGain * CUR_PRES_A_mean; |
Lightvalve | 58:2eade98630e2 | 1796 | PRES_B_VREF = PRES_B_VREF + VREF_NullingGain * CUR_PRES_B_mean; |
Lightvalve | 14:8e7590227d22 | 1797 | |
Lightvalve | 30:8d561f16383b | 1798 | if (PRES_A_VREF > 3.3f) PRES_A_VREF = 3.3f; |
Lightvalve | 30:8d561f16383b | 1799 | if (PRES_A_VREF < 0.0f) PRES_A_VREF = 0.0f; |
Lightvalve | 30:8d561f16383b | 1800 | if (PRES_B_VREF > 3.3f) PRES_B_VREF = 3.3f; |
Lightvalve | 30:8d561f16383b | 1801 | if (PRES_B_VREF < 0.0f) PRES_B_VREF = 0.0f; |
Lightvalve | 19:23b7c1ad8683 | 1802 | |
Lightvalve | 30:8d561f16383b | 1803 | dac_1 = PRES_A_VREF / 3.3f; |
Lightvalve | 30:8d561f16383b | 1804 | dac_2 = PRES_B_VREF / 3.3f; |
Lightvalve | 13:747daba9cf59 | 1805 | } |
Lightvalve | 13:747daba9cf59 | 1806 | } else { |
Lightvalve | 57:f4819de54e7a | 1807 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 14:8e7590227d22 | 1808 | TMR3_COUNT_PRES_NULL = 0; |
Lightvalve | 13:747daba9cf59 | 1809 | CUR_PRES_A_sum = 0; |
Lightvalve | 13:747daba9cf59 | 1810 | CUR_PRES_B_sum = 0; |
Lightvalve | 13:747daba9cf59 | 1811 | CUR_PRES_A_mean = 0; |
Lightvalve | 13:747daba9cf59 | 1812 | CUR_PRES_B_mean = 0; |
Lightvalve | 14:8e7590227d22 | 1813 | |
Lightvalve | 170:42c938a40313 | 1814 | // ROM_RESET_DATA(); |
Lightvalve | 170:42c938a40313 | 1815 | spi_eeprom_write(RID_PRES_A_SENSOR_VREF, (int16_t) (PRES_A_VREF * 1000.0f)); |
Lightvalve | 170:42c938a40313 | 1816 | spi_eeprom_write(RID_PRES_B_SENSOR_VREF, (int16_t) (PRES_B_VREF * 1000.0f)); |
Lightvalve | 19:23b7c1ad8683 | 1817 | |
Lightvalve | 30:8d561f16383b | 1818 | dac_1 = PRES_A_VREF / 3.3f; |
Lightvalve | 30:8d561f16383b | 1819 | dac_2 = PRES_B_VREF / 3.3f; |
Lightvalve | 17:1865016ca2e7 | 1820 | //pc.printf("nulling end"); |
Lightvalve | 13:747daba9cf59 | 1821 | } |
Lightvalve | 14:8e7590227d22 | 1822 | TMR3_COUNT_PRES_NULL++; |
Lightvalve | 13:747daba9cf59 | 1823 | break; |
Lightvalve | 13:747daba9cf59 | 1824 | } |
Lightvalve | 14:8e7590227d22 | 1825 | |
Lightvalve | 50:3c630b5eba9f | 1826 | // case MODE_PRESSURE_SENSOR_CALIB: { |
Lightvalve | 50:3c630b5eba9f | 1827 | // if (TMR3_COUNT_PRES_CALIB < 2 * TMR_FREQ_5k) { |
Lightvalve | 50:3c630b5eba9f | 1828 | // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 50:3c630b5eba9f | 1829 | // if (TMR3_COUNT_PRES_CALIB >= TMR_FREQ_5k) { |
Lightvalve | 50:3c630b5eba9f | 1830 | // CUR_PRES_A_sum += CUR_PRES_A; |
Lightvalve | 50:3c630b5eba9f | 1831 | // } |
Lightvalve | 50:3c630b5eba9f | 1832 | // } else if (TMR3_COUNT_PRES_CALIB < 4 * TMR_FREQ_5k) { |
Lightvalve | 50:3c630b5eba9f | 1833 | // V_out = VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 50:3c630b5eba9f | 1834 | // if (TMR3_COUNT_PRES_CALIB >= 3 * TMR_FREQ_5k) { |
Lightvalve | 50:3c630b5eba9f | 1835 | // CUR_PRES_B_sum += CUR_PRES_B; |
Lightvalve | 50:3c630b5eba9f | 1836 | // } |
Lightvalve | 50:3c630b5eba9f | 1837 | // } else { |
Lightvalve | 50:3c630b5eba9f | 1838 | // CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 50:3c630b5eba9f | 1839 | // TMR3_COUNT_PRES_CALIB = 0; |
Lightvalve | 50:3c630b5eba9f | 1840 | // V_out = 0; |
Lightvalve | 50:3c630b5eba9f | 1841 | // PRES_SENSOR_A_PULSE_PER_BAR = CUR_PRES_A_sum / ((float) TMR_FREQ_5k - 1.0f) - PRES_A_NULL; |
Lightvalve | 50:3c630b5eba9f | 1842 | // PRES_SENSOR_A_PULSE_PER_BAR = PRES_SENSOR_A_PULSE_PER_BAR / ((float) PRES_SUPPLY - 1.0f); |
Lightvalve | 50:3c630b5eba9f | 1843 | // PRES_SENSOR_B_PULSE_PER_BAR = CUR_PRES_B_sum / ((float) TMR_FREQ_5k - 1.0f) - PRES_B_NULL; |
Lightvalve | 50:3c630b5eba9f | 1844 | // PRES_SENSOR_B_PULSE_PER_BAR = PRES_SENSOR_B_PULSE_PER_BAR / ((float) PRES_SUPPLY - 1.0f); |
Lightvalve | 50:3c630b5eba9f | 1845 | // CUR_PRES_A_sum = 0; |
Lightvalve | 50:3c630b5eba9f | 1846 | // CUR_PRES_B_sum = 0; |
Lightvalve | 50:3c630b5eba9f | 1847 | // CUR_PRES_A_mean = 0; |
Lightvalve | 50:3c630b5eba9f | 1848 | // CUR_PRES_B_mean = 0; |
Lightvalve | 50:3c630b5eba9f | 1849 | // |
Lightvalve | 50:3c630b5eba9f | 1850 | // ROM_RESET_DATA(); |
Lightvalve | 50:3c630b5eba9f | 1851 | // |
Lightvalve | 50:3c630b5eba9f | 1852 | // //spi_eeprom_write(RID_PRES_SENSOR_A_PULSE_PER_BAR, (int16_t) (PRES_SENSOR_A_PULSE_PER_BAR * 100.0f)); |
Lightvalve | 50:3c630b5eba9f | 1853 | // //spi_eeprom_write(RID_PRES_SENSOR_B_PULSE_PER_BAR, (int16_t) (PRES_SENSOR_B_PULSE_PER_BAR * 100.0f)); |
Lightvalve | 50:3c630b5eba9f | 1854 | // } |
Lightvalve | 50:3c630b5eba9f | 1855 | // TMR3_COUNT_PRES_CALIB++; |
Lightvalve | 50:3c630b5eba9f | 1856 | // break; |
Lightvalve | 50:3c630b5eba9f | 1857 | // } |
Lightvalve | 19:23b7c1ad8683 | 1858 | |
Lightvalve | 50:3c630b5eba9f | 1859 | // case MODE_ROTARY_FRICTION_TUNING: { |
Lightvalve | 50:3c630b5eba9f | 1860 | // 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 | 1861 | // V_out = PWM_out * sin(2 * 3.14159f * freq_fric_tune * TMR3_COUNT_ROTARY_FRIC_TUNE * 0.0001f); |
Lightvalve | 50:3c630b5eba9f | 1862 | // if (V_out > 0) V_out = VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 50:3c630b5eba9f | 1863 | // else V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 50:3c630b5eba9f | 1864 | // TMR3_COUNT_ROTARY_FRIC_TUNE++; |
Lightvalve | 50:3c630b5eba9f | 1865 | // if (TMR3_COUNT_ROTARY_FRIC_TUNE > TUNING_TIME * TMR_FREQ_5k) { |
Lightvalve | 50:3c630b5eba9f | 1866 | // TMR3_COUNT_ROTARY_FRIC_TUNE = 0; |
Lightvalve | 50:3c630b5eba9f | 1867 | // V_out = 0.0f; |
Lightvalve | 50:3c630b5eba9f | 1868 | // CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 50:3c630b5eba9f | 1869 | // } |
Lightvalve | 50:3c630b5eba9f | 1870 | // break; |
Lightvalve | 50:3c630b5eba9f | 1871 | // } |
Lightvalve | 14:8e7590227d22 | 1872 | |
Lightvalve | 14:8e7590227d22 | 1873 | case MODE_DDV_POS_VS_PWM_ID: { |
Lightvalve | 59:f308b1656d9c | 1874 | CONTROL_MODE = MODE_VALVE_OPEN_LOOP; |
Lightvalve | 13:747daba9cf59 | 1875 | VALVE_ID_timer = VALVE_ID_timer + 1; |
Lightvalve | 14:8e7590227d22 | 1876 | |
Lightvalve | 14:8e7590227d22 | 1877 | if(VALVE_ID_timer < TMR_FREQ_5k*1) { |
Lightvalve | 60:64181f1d3e60 | 1878 | Vout.ref = 3000.0f * sin(2.0f*3.14f*VALVE_ID_timer/TMR_FREQ_5k * 100.0f); |
Lightvalve | 14:8e7590227d22 | 1879 | } else if(VALVE_ID_timer < TMR_FREQ_5k*2) { |
Lightvalve | 59:f308b1656d9c | 1880 | Vout.ref = 1000.0f*(ID_index_array[ID_index]); |
Lightvalve | 14:8e7590227d22 | 1881 | } else if(VALVE_ID_timer == TMR_FREQ_5k*2) { |
Lightvalve | 13:747daba9cf59 | 1882 | VALVE_POS_TMP = 0; |
Lightvalve | 13:747daba9cf59 | 1883 | data_num = 0; |
Lightvalve | 14:8e7590227d22 | 1884 | } else if(VALVE_ID_timer < TMR_FREQ_5k*3) { |
Lightvalve | 13:747daba9cf59 | 1885 | data_num = data_num + 1; |
Lightvalve | 13:747daba9cf59 | 1886 | VALVE_POS_TMP = VALVE_POS_TMP + value; |
Lightvalve | 14:8e7590227d22 | 1887 | } else if(VALVE_ID_timer == TMR_FREQ_5k*3) { |
Lightvalve | 59:f308b1656d9c | 1888 | Vout.ref = 0.0f; |
Lightvalve | 59:f308b1656d9c | 1889 | } else { |
Lightvalve | 13:747daba9cf59 | 1890 | VALVE_POS_AVG[ID_index] = VALVE_POS_TMP / data_num; |
Lightvalve | 13:747daba9cf59 | 1891 | VALVE_ID_timer = 0; |
Lightvalve | 13:747daba9cf59 | 1892 | ID_index= ID_index +1; |
Lightvalve | 13:747daba9cf59 | 1893 | } |
Lightvalve | 14:8e7590227d22 | 1894 | |
Lightvalve | 17:1865016ca2e7 | 1895 | if(ID_index>=25) { |
Lightvalve | 13:747daba9cf59 | 1896 | int i; |
Lightvalve | 13:747daba9cf59 | 1897 | VALVE_POS_AVG_OLD = VALVE_POS_AVG[0]; |
Lightvalve | 17:1865016ca2e7 | 1898 | for(i=0; i<25; i++) { |
Lightvalve | 13:747daba9cf59 | 1899 | VALVE_POS_VS_PWM[i] = (int16_t) (VALVE_POS_AVG[i]); |
Lightvalve | 14:8e7590227d22 | 1900 | if(VALVE_POS_AVG[i] > VALVE_POS_AVG_OLD) { |
Lightvalve | 13:747daba9cf59 | 1901 | VALVE_MAX_POS = VALVE_POS_AVG[i]; |
Lightvalve | 13:747daba9cf59 | 1902 | VALVE_POS_AVG_OLD = VALVE_MAX_POS; |
Lightvalve | 14:8e7590227d22 | 1903 | } else if(VALVE_POS_AVG[i] < VALVE_POS_AVG_OLD) { |
Lightvalve | 13:747daba9cf59 | 1904 | VALVE_MIN_POS = VALVE_POS_AVG[i]; |
Lightvalve | 13:747daba9cf59 | 1905 | VALVE_POS_AVG_OLD = VALVE_MIN_POS; |
Lightvalve | 13:747daba9cf59 | 1906 | } |
Lightvalve | 13:747daba9cf59 | 1907 | } |
Lightvalve | 170:42c938a40313 | 1908 | // ROM_RESET_DATA(); |
Lightvalve | 170:42c938a40313 | 1909 | spi_eeprom_write(RID_VALVE_MAX_POS, (int16_t) VALVE_MAX_POS); |
Lightvalve | 170:42c938a40313 | 1910 | spi_eeprom_write(RID_VALVE_MIN_POS, (int16_t) VALVE_MIN_POS); |
Lightvalve | 170:42c938a40313 | 1911 | for(int i=0; i<25; i++) { |
Lightvalve | 170:42c938a40313 | 1912 | spi_eeprom_write(RID_VALVE_POS_VS_PWM_0 + i, (int16_t) VALVE_POS_VS_PWM[i]); |
Lightvalve | 170:42c938a40313 | 1913 | } |
Lightvalve | 13:747daba9cf59 | 1914 | ID_index = 0; |
Lightvalve | 57:f4819de54e7a | 1915 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 13:747daba9cf59 | 1916 | } |
Lightvalve | 14:8e7590227d22 | 1917 | |
Lightvalve | 14:8e7590227d22 | 1918 | |
Lightvalve | 13:747daba9cf59 | 1919 | break; |
Lightvalve | 13:747daba9cf59 | 1920 | } |
Lightvalve | 14:8e7590227d22 | 1921 | |
Lightvalve | 14:8e7590227d22 | 1922 | case MODE_DDV_DEADZONE_AND_CENTER: { |
Lightvalve | 59:f308b1656d9c | 1923 | CONTROL_MODE = MODE_VALVE_OPEN_LOOP; |
Lightvalve | 13:747daba9cf59 | 1924 | VALVE_DZ_timer = VALVE_DZ_timer + 1; |
Lightvalve | 14:8e7590227d22 | 1925 | if(first_check == 0) { |
Lightvalve | 30:8d561f16383b | 1926 | if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 1927 | Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 30:8d561f16383b | 1928 | } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 1929 | Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 14:8e7590227d22 | 1930 | pos_plus_end = pos.sen; |
Lightvalve | 30:8d561f16383b | 1931 | } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 1932 | Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 30:8d561f16383b | 1933 | } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 1934 | Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 14:8e7590227d22 | 1935 | pos_minus_end = pos.sen; |
Lightvalve | 30:8d561f16383b | 1936 | } else if(VALVE_DZ_timer < (int) (3.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 1937 | 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 | 1938 | } else if(VALVE_DZ_timer < (int) (4.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 1939 | 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 | 1940 | data_num = data_num + 1; |
Lightvalve | 13:747daba9cf59 | 1941 | VALVE_POS_TMP = VALVE_POS_TMP + value; |
Lightvalve | 30:8d561f16383b | 1942 | } else if(VALVE_DZ_timer == (int) (4.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 1943 | 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 | 1944 | DDV_POS_AVG = VALVE_POS_TMP / data_num; |
Lightvalve | 14:8e7590227d22 | 1945 | START_POS = pos.sen; |
Lightvalve | 13:747daba9cf59 | 1946 | VALVE_POS_TMP = 0; |
Lightvalve | 13:747daba9cf59 | 1947 | data_num = 0; |
Lightvalve | 14:8e7590227d22 | 1948 | |
Lightvalve | 30:8d561f16383b | 1949 | } else if(VALVE_DZ_timer < (int) (5.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1950 | valve_pos.ref = DDV_POS_AVG; |
Lightvalve | 14:8e7590227d22 | 1951 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 14:8e7590227d22 | 1952 | |
Lightvalve | 30:8d561f16383b | 1953 | } else if(VALVE_DZ_timer < (int) (6.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1954 | valve_pos.ref = DDV_POS_AVG; |
Lightvalve | 14:8e7590227d22 | 1955 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 60:64181f1d3e60 | 1956 | |
Lightvalve | 30:8d561f16383b | 1957 | } else if(VALVE_DZ_timer == (int) (6.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1958 | valve_pos.ref = DDV_POS_AVG; |
Lightvalve | 14:8e7590227d22 | 1959 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 14:8e7590227d22 | 1960 | FINAL_POS = pos.sen; |
Lightvalve | 14:8e7590227d22 | 1961 | |
Lightvalve | 60:64181f1d3e60 | 1962 | if((FINAL_POS - START_POS)>200) { |
Lightvalve | 13:747daba9cf59 | 1963 | DZ_case = 1; |
Lightvalve | 60:64181f1d3e60 | 1964 | } else if((FINAL_POS - START_POS)<-200) { |
Lightvalve | 13:747daba9cf59 | 1965 | DZ_case = -1; |
Lightvalve | 14:8e7590227d22 | 1966 | } else { |
Lightvalve | 13:747daba9cf59 | 1967 | DZ_case = 0; |
Lightvalve | 13:747daba9cf59 | 1968 | } |
Lightvalve | 61:bc8c8270f0ab | 1969 | |
Lightvalve | 60:64181f1d3e60 | 1970 | CAN_TX_PRES((int16_t) (DZ_case), (int16_t) (6)); |
Lightvalve | 50:3c630b5eba9f | 1971 | |
Lightvalve | 13:747daba9cf59 | 1972 | first_check = 1; |
Lightvalve | 13:747daba9cf59 | 1973 | DZ_DIRECTION = 1; |
Lightvalve | 13:747daba9cf59 | 1974 | VALVE_DZ_timer = 0; |
Lightvalve | 13:747daba9cf59 | 1975 | Ref_Valve_Pos_Old = DDV_POS_AVG; |
Lightvalve | 13:747daba9cf59 | 1976 | DZ_NUM = 1; |
Lightvalve | 13:747daba9cf59 | 1977 | DZ_index = 1; |
Lightvalve | 14:8e7590227d22 | 1978 | |
Lightvalve | 13:747daba9cf59 | 1979 | } |
Lightvalve | 19:23b7c1ad8683 | 1980 | } else { |
Lightvalve | 14:8e7590227d22 | 1981 | if((DZ_case == -1 && DZ_NUM == 1) | (DZ_case == 1 && DZ_NUM == 1)) { |
Lightvalve | 30:8d561f16383b | 1982 | if(VALVE_DZ_timer < (int) (1.0 * (float) TMR_FREQ_5k)) { |
Lightvalve | 60:64181f1d3e60 | 1983 | 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 | 1984 | //pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end; |
Lightvalve | 60:64181f1d3e60 | 1985 | //CONTROL_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 30:8d561f16383b | 1986 | } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1987 | START_POS = pos.sen; |
Lightvalve | 30:8d561f16383b | 1988 | } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1989 | valve_pos.ref = Ref_Valve_Pos_Old - DZ_case * DZ_DIRECTION * 64 / DZ_index; |
Lightvalve | 14:8e7590227d22 | 1990 | if(valve_pos.ref <= VALVE_MIN_POS) { |
Lightvalve | 14:8e7590227d22 | 1991 | valve_pos.ref = VALVE_MIN_POS; |
Lightvalve | 14:8e7590227d22 | 1992 | } else if(valve_pos.ref >= VALVE_MAX_POS) { |
Lightvalve | 14:8e7590227d22 | 1993 | valve_pos.ref = VALVE_MAX_POS; |
Lightvalve | 13:747daba9cf59 | 1994 | } |
Lightvalve | 14:8e7590227d22 | 1995 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 60:64181f1d3e60 | 1996 | |
Lightvalve | 30:8d561f16383b | 1997 | } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1998 | Ref_Valve_Pos_Old = valve_pos.ref; |
Lightvalve | 14:8e7590227d22 | 1999 | FINAL_POS = pos.sen; |
Lightvalve | 14:8e7590227d22 | 2000 | |
Lightvalve | 14:8e7590227d22 | 2001 | if((FINAL_POS - START_POS)>100) { |
Lightvalve | 14:8e7590227d22 | 2002 | DZ_DIRECTION = 1 * DZ_case; |
Lightvalve | 14:8e7590227d22 | 2003 | } else if((FINAL_POS - START_POS)<-100) { |
Lightvalve | 14:8e7590227d22 | 2004 | DZ_DIRECTION = -1 * DZ_case; |
Lightvalve | 14:8e7590227d22 | 2005 | } else { |
Lightvalve | 13:747daba9cf59 | 2006 | DZ_DIRECTION = 1 * DZ_case; |
Lightvalve | 13:747daba9cf59 | 2007 | } |
Lightvalve | 14:8e7590227d22 | 2008 | |
Lightvalve | 13:747daba9cf59 | 2009 | VALVE_DZ_timer = 0; |
Lightvalve | 13:747daba9cf59 | 2010 | DZ_index= DZ_index *2; |
Lightvalve | 14:8e7590227d22 | 2011 | if(DZ_index >= 128) { |
Lightvalve | 14:8e7590227d22 | 2012 | FIRST_DZ = valve_pos.ref; |
Lightvalve | 13:747daba9cf59 | 2013 | DZ_NUM = 2; |
Lightvalve | 13:747daba9cf59 | 2014 | Ref_Valve_Pos_Old = FIRST_DZ; |
Lightvalve | 13:747daba9cf59 | 2015 | DZ_index = 1; |
Lightvalve | 13:747daba9cf59 | 2016 | DZ_DIRECTION = 1; |
Lightvalve | 13:747daba9cf59 | 2017 | } |
Lightvalve | 13:747daba9cf59 | 2018 | } |
Lightvalve | 14:8e7590227d22 | 2019 | } else if((DZ_case == -1 && DZ_NUM == 2) | (DZ_case == 1 && DZ_NUM == 2)) { |
Lightvalve | 30:8d561f16383b | 2020 | if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 60:64181f1d3e60 | 2021 | 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 | 2022 | //pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end; |
Lightvalve | 60:64181f1d3e60 | 2023 | //CONTROL_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 30:8d561f16383b | 2024 | } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 2025 | START_POS = pos.sen; |
Lightvalve | 30:8d561f16383b | 2026 | } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 60:64181f1d3e60 | 2027 | valve_pos.ref = Ref_Valve_Pos_Old - DZ_case * DZ_DIRECTION * 64 / DZ_index; |
Lightvalve | 14:8e7590227d22 | 2028 | if(valve_pos.ref <= VALVE_MIN_POS) { |
Lightvalve | 14:8e7590227d22 | 2029 | valve_pos.ref = VALVE_MIN_POS; |
Lightvalve | 14:8e7590227d22 | 2030 | } else if(valve_pos.ref >= VALVE_MAX_POS) { |
Lightvalve | 14:8e7590227d22 | 2031 | valve_pos.ref = VALVE_MAX_POS; |
Lightvalve | 13:747daba9cf59 | 2032 | } |
Lightvalve | 14:8e7590227d22 | 2033 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 61:bc8c8270f0ab | 2034 | |
Lightvalve | 30:8d561f16383b | 2035 | } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 2036 | Vout.ref = 0.0f; |
Lightvalve | 59:f308b1656d9c | 2037 | } else if(VALVE_DZ_timer > (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 2038 | Ref_Valve_Pos_Old = valve_pos.ref; |
Lightvalve | 14:8e7590227d22 | 2039 | FINAL_POS = pos.sen; |
Lightvalve | 14:8e7590227d22 | 2040 | |
Lightvalve | 14:8e7590227d22 | 2041 | if((FINAL_POS - START_POS)>100) { |
Lightvalve | 60:64181f1d3e60 | 2042 | DZ_DIRECTION = 1 * DZ_case; |
Lightvalve | 14:8e7590227d22 | 2043 | } else if((FINAL_POS - START_POS)<-100) { |
Lightvalve | 60:64181f1d3e60 | 2044 | DZ_DIRECTION = -1 * DZ_case; |
Lightvalve | 14:8e7590227d22 | 2045 | } else { |
Lightvalve | 60:64181f1d3e60 | 2046 | DZ_DIRECTION = -1 * DZ_case; |
Lightvalve | 13:747daba9cf59 | 2047 | } |
Lightvalve | 14:8e7590227d22 | 2048 | |
Lightvalve | 13:747daba9cf59 | 2049 | VALVE_DZ_timer = 0; |
Lightvalve | 60:64181f1d3e60 | 2050 | DZ_index= DZ_index * 2; |
Lightvalve | 14:8e7590227d22 | 2051 | if(DZ_index >= 128) { |
Lightvalve | 14:8e7590227d22 | 2052 | SECOND_DZ = valve_pos.ref; |
Lightvalve | 57:f4819de54e7a | 2053 | VALVE_CENTER = (int) (0.5f * (float) (FIRST_DZ) + 0.5f * (float) (SECOND_DZ)); |
Lightvalve | 13:747daba9cf59 | 2054 | first_check = 0; |
Lightvalve | 33:91b17819ec30 | 2055 | VALVE_DEADZONE_MINUS = (float) FIRST_DZ; |
Lightvalve | 33:91b17819ec30 | 2056 | VALVE_DEADZONE_PLUS = (float) SECOND_DZ; |
Lightvalve | 19:23b7c1ad8683 | 2057 | |
Lightvalve | 170:42c938a40313 | 2058 | // ROM_RESET_DATA(); |
Lightvalve | 170:42c938a40313 | 2059 | spi_eeprom_write(RID_VALVE_CNETER, (int16_t) VALVE_CENTER); |
Lightvalve | 170:42c938a40313 | 2060 | spi_eeprom_write(RID_VALVE_MAX_POS, (int16_t) VALVE_MAX_POS); |
Lightvalve | 170:42c938a40313 | 2061 | spi_eeprom_write(RID_VALVE_MIN_POS, (int16_t) VALVE_MIN_POS); |
Lightvalve | 34:bb2ca2fc2a8e | 2062 | |
Lightvalve | 60:64181f1d3e60 | 2063 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 13:747daba9cf59 | 2064 | DZ_index = 1; |
Lightvalve | 13:747daba9cf59 | 2065 | } |
Lightvalve | 13:747daba9cf59 | 2066 | } |
Lightvalve | 14:8e7590227d22 | 2067 | } else if(DZ_case == 0 && DZ_NUM ==1) { |
Lightvalve | 30:8d561f16383b | 2068 | if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 60:64181f1d3e60 | 2069 | 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 | 2070 | //pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end; |
Lightvalve | 60:64181f1d3e60 | 2071 | //CONTROL_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 30:8d561f16383b | 2072 | } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 2073 | START_POS = pos.sen; |
Lightvalve | 30:8d561f16383b | 2074 | } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 2075 | valve_pos.ref = Ref_Valve_Pos_Old - DZ_DIRECTION * 64 / DZ_index; |
Lightvalve | 14:8e7590227d22 | 2076 | if(valve_pos.ref <= VALVE_MIN_POS) { |
Lightvalve | 14:8e7590227d22 | 2077 | valve_pos.ref = VALVE_MIN_POS; |
Lightvalve | 14:8e7590227d22 | 2078 | } else if(valve_pos.ref >= VALVE_MAX_POS) { |
Lightvalve | 14:8e7590227d22 | 2079 | valve_pos.ref = VALVE_MAX_POS; |
Lightvalve | 13:747daba9cf59 | 2080 | } |
Lightvalve | 14:8e7590227d22 | 2081 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 61:bc8c8270f0ab | 2082 | |
Lightvalve | 30:8d561f16383b | 2083 | } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 2084 | Ref_Valve_Pos_Old = valve_pos.ref; |
Lightvalve | 14:8e7590227d22 | 2085 | FINAL_POS = pos.sen; |
Lightvalve | 14:8e7590227d22 | 2086 | |
Lightvalve | 14:8e7590227d22 | 2087 | if((FINAL_POS - START_POS)>100) { |
Lightvalve | 14:8e7590227d22 | 2088 | DZ_DIRECTION = 1; |
Lightvalve | 14:8e7590227d22 | 2089 | } else if((FINAL_POS - START_POS)<-100) { |
Lightvalve | 14:8e7590227d22 | 2090 | DZ_DIRECTION = -1; |
Lightvalve | 14:8e7590227d22 | 2091 | } else { |
Lightvalve | 13:747daba9cf59 | 2092 | DZ_DIRECTION = 1; |
Lightvalve | 13:747daba9cf59 | 2093 | } |
Lightvalve | 13:747daba9cf59 | 2094 | VALVE_DZ_timer = 0; |
Lightvalve | 13:747daba9cf59 | 2095 | DZ_index= DZ_index *2; |
Lightvalve | 14:8e7590227d22 | 2096 | if(DZ_index >= 128) { |
Lightvalve | 14:8e7590227d22 | 2097 | FIRST_DZ = valve_pos.ref; |
Lightvalve | 13:747daba9cf59 | 2098 | DZ_NUM = 2; |
Lightvalve | 13:747daba9cf59 | 2099 | Ref_Valve_Pos_Old = FIRST_DZ; |
Lightvalve | 13:747daba9cf59 | 2100 | DZ_index = 1; |
Lightvalve | 13:747daba9cf59 | 2101 | DZ_DIRECTION = 1; |
Lightvalve | 13:747daba9cf59 | 2102 | } |
Lightvalve | 13:747daba9cf59 | 2103 | } |
Lightvalve | 14:8e7590227d22 | 2104 | } else { |
Lightvalve | 30:8d561f16383b | 2105 | if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 60:64181f1d3e60 | 2106 | 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 | 2107 | //pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end; |
Lightvalve | 60:64181f1d3e60 | 2108 | //CONTROL_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 30:8d561f16383b | 2109 | } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 2110 | START_POS = pos.sen; |
Lightvalve | 30:8d561f16383b | 2111 | } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 2112 | valve_pos.ref = Ref_Valve_Pos_Old + DZ_DIRECTION * 64 / DZ_index; |
Lightvalve | 14:8e7590227d22 | 2113 | if(valve_pos.ref <= VALVE_MIN_POS) { |
Lightvalve | 14:8e7590227d22 | 2114 | valve_pos.ref = VALVE_MIN_POS; |
Lightvalve | 14:8e7590227d22 | 2115 | } else if(valve_pos.ref > VALVE_MAX_POS) { |
Lightvalve | 14:8e7590227d22 | 2116 | valve_pos.ref = VALVE_MAX_POS - 1; |
Lightvalve | 13:747daba9cf59 | 2117 | } |
Lightvalve | 14:8e7590227d22 | 2118 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 60:64181f1d3e60 | 2119 | |
Lightvalve | 30:8d561f16383b | 2120 | } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 2121 | Vout.ref = 0.0f; |
Lightvalve | 59:f308b1656d9c | 2122 | } else if(VALVE_DZ_timer > (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 2123 | Ref_Valve_Pos_Old = valve_pos.ref; |
Lightvalve | 14:8e7590227d22 | 2124 | FINAL_POS = pos.sen; |
Lightvalve | 61:bc8c8270f0ab | 2125 | |
Lightvalve | 60:64181f1d3e60 | 2126 | if((FINAL_POS - START_POS)>100) { |
Lightvalve | 13:747daba9cf59 | 2127 | DZ_DIRECTION = 1; |
Lightvalve | 60:64181f1d3e60 | 2128 | } else if((FINAL_POS - START_POS)<-100) { |
Lightvalve | 60:64181f1d3e60 | 2129 | DZ_DIRECTION = -1; |
Lightvalve | 60:64181f1d3e60 | 2130 | } else { |
Lightvalve | 13:747daba9cf59 | 2131 | DZ_DIRECTION = 1; |
Lightvalve | 60:64181f1d3e60 | 2132 | } |
Lightvalve | 14:8e7590227d22 | 2133 | |
Lightvalve | 13:747daba9cf59 | 2134 | VALVE_DZ_timer = 0; |
Lightvalve | 13:747daba9cf59 | 2135 | DZ_index= DZ_index *2; |
Lightvalve | 14:8e7590227d22 | 2136 | if(DZ_index >= 128) { |
Lightvalve | 14:8e7590227d22 | 2137 | SECOND_DZ = valve_pos.ref; |
Lightvalve | 57:f4819de54e7a | 2138 | VALVE_CENTER = (int) (0.5f * (float) (FIRST_DZ) + 0.5f * (float) (SECOND_DZ)); |
Lightvalve | 13:747daba9cf59 | 2139 | first_check = 0; |
Lightvalve | 33:91b17819ec30 | 2140 | VALVE_DEADZONE_MINUS = (float) FIRST_DZ; |
Lightvalve | 33:91b17819ec30 | 2141 | VALVE_DEADZONE_PLUS = (float) SECOND_DZ; |
Lightvalve | 61:bc8c8270f0ab | 2142 | |
Lightvalve | 170:42c938a40313 | 2143 | // ROM_RESET_DATA(); |
Lightvalve | 170:42c938a40313 | 2144 | spi_eeprom_write(RID_VALVE_CNETER, (int16_t) VALVE_CENTER); |
Lightvalve | 170:42c938a40313 | 2145 | spi_eeprom_write(RID_VALVE_MAX_POS, (int16_t) VALVE_MAX_POS); |
Lightvalve | 170:42c938a40313 | 2146 | spi_eeprom_write(RID_VALVE_MIN_POS, (int16_t) VALVE_MIN_POS); |
Lightvalve | 34:bb2ca2fc2a8e | 2147 | |
Lightvalve | 57:f4819de54e7a | 2148 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 13:747daba9cf59 | 2149 | DZ_index = 1; |
Lightvalve | 13:747daba9cf59 | 2150 | } |
Lightvalve | 13:747daba9cf59 | 2151 | } |
Lightvalve | 13:747daba9cf59 | 2152 | } |
Lightvalve | 14:8e7590227d22 | 2153 | } |
Lightvalve | 13:747daba9cf59 | 2154 | break; |
Lightvalve | 13:747daba9cf59 | 2155 | } |
Lightvalve | 14:8e7590227d22 | 2156 | |
Lightvalve | 14:8e7590227d22 | 2157 | case MODE_DDV_POS_VS_FLOWRATE: { |
Lightvalve | 59:f308b1656d9c | 2158 | CONTROL_MODE = MODE_VALVE_OPEN_LOOP; |
Lightvalve | 13:747daba9cf59 | 2159 | VALVE_FR_timer = VALVE_FR_timer + 1; |
Lightvalve | 14:8e7590227d22 | 2160 | if(first_check == 0) { |
Lightvalve | 30:8d561f16383b | 2161 | if(VALVE_FR_timer < (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 2162 | Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 23:59218d4a256d | 2163 | //CAN_TX_PRES((int16_t) (VALVE_FR_timer), (int16_t) (6)); |
Lightvalve | 30:8d561f16383b | 2164 | } else if(VALVE_FR_timer == (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 2165 | Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 14:8e7590227d22 | 2166 | pos_plus_end = pos.sen; |
Lightvalve | 14:8e7590227d22 | 2167 | // CAN_TX_PRES((int16_t) (V_out), (int16_t) (7)); |
Lightvalve | 30:8d561f16383b | 2168 | } else if(VALVE_FR_timer < (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 2169 | Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 30:8d561f16383b | 2170 | } else if(VALVE_FR_timer == (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 2171 | // CAN_TX_PRES((int16_t) (V_out), (int16_t) (8)); |
Lightvalve | 59:f308b1656d9c | 2172 | Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 14:8e7590227d22 | 2173 | pos_minus_end = pos.sen; |
Lightvalve | 13:747daba9cf59 | 2174 | first_check = 1; |
Lightvalve | 13:747daba9cf59 | 2175 | VALVE_FR_timer = 0; |
Lightvalve | 57:f4819de54e7a | 2176 | valve_pos.ref = (float) VALVE_CENTER; |
Lightvalve | 13:747daba9cf59 | 2177 | ID_index = 0; |
Lightvalve | 13:747daba9cf59 | 2178 | max_check = 0; |
Lightvalve | 13:747daba9cf59 | 2179 | min_check = 0; |
Lightvalve | 13:747daba9cf59 | 2180 | } |
Lightvalve | 14:8e7590227d22 | 2181 | } else { |
Lightvalve | 30:8d561f16383b | 2182 | if(VALVE_FR_timer < (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 2183 | //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 | 2184 | pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end; |
Lightvalve | 59:f308b1656d9c | 2185 | CONTROL_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 30:8d561f16383b | 2186 | } else if(VALVE_FR_timer == (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 13:747daba9cf59 | 2187 | data_num = 0; |
Lightvalve | 57:f4819de54e7a | 2188 | valve_pos.ref = 10.0f*((float) ID_index_array[ID_index]) + (float) VALVE_CENTER; |
Lightvalve | 14:8e7590227d22 | 2189 | |
Lightvalve | 14:8e7590227d22 | 2190 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 14:8e7590227d22 | 2191 | START_POS = pos.sen; |
Lightvalve | 30:8d561f16383b | 2192 | } else if(VALVE_FR_timer < (int) (5.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 57:f4819de54e7a | 2193 | valve_pos.ref = 10.0f*((float) ID_index_array[ID_index]) + (float) VALVE_CENTER; |
Lightvalve | 14:8e7590227d22 | 2194 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 13:747daba9cf59 | 2195 | data_num = data_num + 1; |
Lightvalve | 30:8d561f16383b | 2196 | if(abs(0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen) > 20000.0f) { |
Lightvalve | 14:8e7590227d22 | 2197 | FINAL_POS = pos.sen; |
Lightvalve | 14:8e7590227d22 | 2198 | one_period_end = 1; |
Lightvalve | 13:747daba9cf59 | 2199 | } |
Lightvalve | 30:8d561f16383b | 2200 | } else if(VALVE_FR_timer == (int) (5.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 2201 | FINAL_POS = pos.sen; |
Lightvalve | 13:747daba9cf59 | 2202 | one_period_end = 1; |
Lightvalve | 59:f308b1656d9c | 2203 | V_out = 0.0f; |
Lightvalve | 13:747daba9cf59 | 2204 | } |
Lightvalve | 14:8e7590227d22 | 2205 | |
Lightvalve | 14:8e7590227d22 | 2206 | if(one_period_end == 1) { |
Lightvalve | 14:8e7590227d22 | 2207 | if(valve_pos.ref > VALVE_MAX_POS) { |
Lightvalve | 13:747daba9cf59 | 2208 | max_check = 1; |
Lightvalve | 14:8e7590227d22 | 2209 | } else if(valve_pos.ref < VALVE_MIN_POS) { |
Lightvalve | 13:747daba9cf59 | 2210 | min_check = 1; |
Lightvalve | 13:747daba9cf59 | 2211 | } |
Lightvalve | 13:747daba9cf59 | 2212 | JOINT_VEL[ID_index] = (FINAL_POS - START_POS) / data_num * TMR_FREQ_5k; // pulse/sec |
Lightvalve | 19:23b7c1ad8683 | 2213 | |
Lightvalve | 13:747daba9cf59 | 2214 | VALVE_FR_timer = 0; |
Lightvalve | 13:747daba9cf59 | 2215 | one_period_end = 0; |
Lightvalve | 13:747daba9cf59 | 2216 | ID_index= ID_index +1; |
Lightvalve | 30:8d561f16383b | 2217 | V_out = 0.0f; |
Lightvalve | 13:747daba9cf59 | 2218 | } |
Lightvalve | 14:8e7590227d22 | 2219 | |
Lightvalve | 14:8e7590227d22 | 2220 | if(max_check == 1 && min_check == 1) { |
Lightvalve | 19:23b7c1ad8683 | 2221 | |
Lightvalve | 13:747daba9cf59 | 2222 | VALVE_POS_NUM = ID_index; |
Lightvalve | 170:42c938a40313 | 2223 | // ROM_RESET_DATA(); |
Lightvalve | 170:42c938a40313 | 2224 | for(int i=0; i<100; i++) { |
Lightvalve | 170:42c938a40313 | 2225 | spi_eeprom_write(RID_VALVE_POS_VS_FLOWRATE_0 + i, (int16_t) (JOINT_VEL[i] & 0xFFFF)); |
Lightvalve | 170:42c938a40313 | 2226 | spi_eeprom_write(RID_VALVE_POS_VS_FLOWRATE_0_1 + i, (int16_t) ((JOINT_VEL[i] >> 16) & 0xFFFF)); |
Lightvalve | 170:42c938a40313 | 2227 | } |
Lightvalve | 13:747daba9cf59 | 2228 | ID_index = 0; |
Lightvalve | 13:747daba9cf59 | 2229 | first_check = 0; |
Lightvalve | 13:747daba9cf59 | 2230 | VALVE_FR_timer = 0; |
Lightvalve | 57:f4819de54e7a | 2231 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 49:e7bcfc244d40 | 2232 | // CAN_TX_PRES((int16_t) (VALVE_FR_timer), (int16_t) (6)); |
Lightvalve | 13:747daba9cf59 | 2233 | } |
Lightvalve | 13:747daba9cf59 | 2234 | } |
Lightvalve | 13:747daba9cf59 | 2235 | break; |
Lightvalve | 13:747daba9cf59 | 2236 | } |
Lightvalve | 58:2eade98630e2 | 2237 | |
Lightvalve | 57:f4819de54e7a | 2238 | case MODE_SYSTEM_ID: { |
Lightvalve | 169:645207e160ca | 2239 | freq_sysid_Iref = (double) cnt_sysid * DT_TMR3 * 3.0f; |
Lightvalve | 57:f4819de54e7a | 2240 | valve_pos.ref = 2500.0f * sin(2.0f * 3.14159f * freq_sysid_Iref * (double) cnt_sysid * DT_TMR3); |
Lightvalve | 57:f4819de54e7a | 2241 | CONTROL_MODE = MODE_VALVE_OPEN_LOOP; |
Lightvalve | 57:f4819de54e7a | 2242 | cnt_sysid++; |
Lightvalve | 57:f4819de54e7a | 2243 | if (freq_sysid_Iref >= 300) { |
Lightvalve | 57:f4819de54e7a | 2244 | cnt_sysid = 0; |
Lightvalve | 57:f4819de54e7a | 2245 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 57:f4819de54e7a | 2246 | } |
Lightvalve | 57:f4819de54e7a | 2247 | break; |
Lightvalve | 57:f4819de54e7a | 2248 | } |
Lightvalve | 58:2eade98630e2 | 2249 | |
Lightvalve | 169:645207e160ca | 2250 | case MODE_FREQ_TEST: { |
Lightvalve | 169:645207e160ca | 2251 | float valve_pos_ref = 2500.0f * sin(2.0f * 3.141592f * freq_test_valve_ref * (float) cnt_freq_test * DT_TMR3); |
Lightvalve | 169:645207e160ca | 2252 | if(valve_pos_ref >= 0) { |
Lightvalve | 169:645207e160ca | 2253 | valve_pos.ref = (double)VALVE_CENTER + (double)valve_pos_ref * ((double)VALVE_MAX_POS-(double)VALVE_CENTER)/10000.0f; |
Lightvalve | 169:645207e160ca | 2254 | } else { |
Lightvalve | 169:645207e160ca | 2255 | valve_pos.ref = (double)VALVE_CENTER - (double)valve_pos_ref * ((double)VALVE_MIN_POS-(double)VALVE_CENTER)/10000.0f; |
Lightvalve | 169:645207e160ca | 2256 | } |
Lightvalve | 169:645207e160ca | 2257 | ref_array[cnt_freq_test] = valve_pos_ref; |
Lightvalve | 169:645207e160ca | 2258 | if(value>=(float) VALVE_CENTER) { |
Lightvalve | 169:645207e160ca | 2259 | pos_array[cnt_freq_test] = 10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER); |
Lightvalve | 169:645207e160ca | 2260 | } else { |
Lightvalve | 169:645207e160ca | 2261 | pos_array[cnt_freq_test] = -10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER); |
Lightvalve | 169:645207e160ca | 2262 | } |
Lightvalve | 58:2eade98630e2 | 2263 | |
Lightvalve | 169:645207e160ca | 2264 | CONTROL_MODE = MODE_VALVE_POSITION_CONTROL; |
Lightvalve | 169:645207e160ca | 2265 | cnt_freq_test++; |
Lightvalve | 169:645207e160ca | 2266 | if (freq_test_valve_ref * (float) cnt_freq_test * DT_TMR3 > 2) { |
Lightvalve | 169:645207e160ca | 2267 | buffer_data_size = cnt_freq_test; |
Lightvalve | 169:645207e160ca | 2268 | cnt_freq_test = 0; |
Lightvalve | 169:645207e160ca | 2269 | cnt_send_buffer = 0; |
Lightvalve | 169:645207e160ca | 2270 | freq_test_valve_ref = freq_test_valve_ref * 1.05f; |
Lightvalve | 169:645207e160ca | 2271 | if (freq_test_valve_ref >= 400) { |
Lightvalve | 169:645207e160ca | 2272 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 169:645207e160ca | 2273 | CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 169:645207e160ca | 2274 | CAN_TX_PWM((int16_t) (1)); //1300 |
Lightvalve | 169:645207e160ca | 2275 | } |
Lightvalve | 169:645207e160ca | 2276 | CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 169:645207e160ca | 2277 | CONTROL_UTILITY_MODE = MODE_SEND_OVER; |
Lightvalve | 170:42c938a40313 | 2278 | |
Lightvalve | 169:645207e160ca | 2279 | } |
Lightvalve | 169:645207e160ca | 2280 | break; |
Lightvalve | 169:645207e160ca | 2281 | } |
Lightvalve | 169:645207e160ca | 2282 | case MODE_SEND_BUFFER: { |
Lightvalve | 169:645207e160ca | 2283 | // if (TMR2_COUNT_CAN_TX % (int) ((int) TMR_FREQ_5k/CAN_FREQ) == 0) { |
Lightvalve | 169:645207e160ca | 2284 | // CAN_TX_PRES((int16_t) (pos_array[cnt_send_buffer]), (int16_t) (ref_array[cnt_send_buffer])); // 1400 |
Lightvalve | 169:645207e160ca | 2285 | // if(cnt_send_buffer>=buffer_data_size) { |
Lightvalve | 169:645207e160ca | 2286 | // CONTROL_UTILITY_MODE = MODE_FREQ_TEST; |
Lightvalve | 169:645207e160ca | 2287 | // } |
Lightvalve | 169:645207e160ca | 2288 | // cnt_send_buffer++; |
Lightvalve | 169:645207e160ca | 2289 | // } |
Lightvalve | 169:645207e160ca | 2290 | |
Lightvalve | 169:645207e160ca | 2291 | break; |
Lightvalve | 169:645207e160ca | 2292 | } |
Lightvalve | 169:645207e160ca | 2293 | case MODE_SEND_OVER: { |
Lightvalve | 169:645207e160ca | 2294 | CAN_TX_TORQUE((int16_t) (buffer_data_size)); //1300 |
Lightvalve | 169:645207e160ca | 2295 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 169:645207e160ca | 2296 | CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 169:645207e160ca | 2297 | break; |
Lightvalve | 169:645207e160ca | 2298 | } |
Lightvalve | 170:42c938a40313 | 2299 | |
Lightvalve | 169:645207e160ca | 2300 | case MODE_STEP_TEST: { |
Lightvalve | 169:645207e160ca | 2301 | float valve_pos_ref = 0.0f; |
Lightvalve | 170:42c938a40313 | 2302 | if (cnt_step_test < (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 169:645207e160ca | 2303 | valve_pos_ref = 0.0f; |
Lightvalve | 170:42c938a40313 | 2304 | } else { |
Lightvalve | 169:645207e160ca | 2305 | valve_pos_ref = 10000.0f; |
Lightvalve | 169:645207e160ca | 2306 | } |
Lightvalve | 169:645207e160ca | 2307 | if(valve_pos_ref >= 0) { |
Lightvalve | 169:645207e160ca | 2308 | valve_pos.ref = (double)VALVE_CENTER + (double)valve_pos_ref * ((double)VALVE_MAX_POS-(double)VALVE_CENTER)/10000.0f; |
Lightvalve | 169:645207e160ca | 2309 | } else { |
Lightvalve | 169:645207e160ca | 2310 | valve_pos.ref = (double)VALVE_CENTER - (double)valve_pos_ref * ((double)VALVE_MIN_POS-(double)VALVE_CENTER)/10000.0f; |
Lightvalve | 169:645207e160ca | 2311 | } |
Lightvalve | 169:645207e160ca | 2312 | ref_array[cnt_step_test] = valve_pos_ref; |
Lightvalve | 169:645207e160ca | 2313 | if(value>=(float) VALVE_CENTER) { |
Lightvalve | 169:645207e160ca | 2314 | pos_array[cnt_step_test] = 10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER); |
Lightvalve | 169:645207e160ca | 2315 | } else { |
Lightvalve | 169:645207e160ca | 2316 | pos_array[cnt_step_test] = -10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER); |
Lightvalve | 169:645207e160ca | 2317 | } |
Lightvalve | 169:645207e160ca | 2318 | |
Lightvalve | 169:645207e160ca | 2319 | CONTROL_MODE = MODE_VALVE_POSITION_CONTROL; |
Lightvalve | 169:645207e160ca | 2320 | cnt_step_test++; |
Lightvalve | 170:42c938a40313 | 2321 | if (cnt_step_test > (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 169:645207e160ca | 2322 | buffer_data_size = cnt_step_test; |
Lightvalve | 169:645207e160ca | 2323 | cnt_step_test = 0; |
Lightvalve | 169:645207e160ca | 2324 | cnt_send_buffer = 0; |
Lightvalve | 169:645207e160ca | 2325 | CONTROL_UTILITY_MODE = MODE_SEND_OVER; |
Lightvalve | 169:645207e160ca | 2326 | CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 169:645207e160ca | 2327 | } |
Lightvalve | 169:645207e160ca | 2328 | // if (cnt_step_test > (int) (2.0f * (float) TMR_FREQ_5k)) |
Lightvalve | 169:645207e160ca | 2329 | // { |
Lightvalve | 169:645207e160ca | 2330 | // CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 169:645207e160ca | 2331 | // CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 169:645207e160ca | 2332 | // CAN_TX_PWM((int16_t) (1)); //1300 |
Lightvalve | 169:645207e160ca | 2333 | // } |
Lightvalve | 170:42c938a40313 | 2334 | |
Lightvalve | 169:645207e160ca | 2335 | break; |
Lightvalve | 169:645207e160ca | 2336 | } |
Lightvalve | 57:f4819de54e7a | 2337 | |
Lightvalve | 57:f4819de54e7a | 2338 | default: |
Lightvalve | 57:f4819de54e7a | 2339 | break; |
Lightvalve | 57:f4819de54e7a | 2340 | } |
Lightvalve | 57:f4819de54e7a | 2341 | |
Lightvalve | 57:f4819de54e7a | 2342 | // CONTROL MODE ------------------------------------------------------------ |
Lightvalve | 57:f4819de54e7a | 2343 | |
Lightvalve | 57:f4819de54e7a | 2344 | switch (CONTROL_MODE) { |
Lightvalve | 57:f4819de54e7a | 2345 | case MODE_NO_ACT: { |
Lightvalve | 57:f4819de54e7a | 2346 | V_out = 0.0f; |
Lightvalve | 57:f4819de54e7a | 2347 | break; |
Lightvalve | 57:f4819de54e7a | 2348 | } |
Lightvalve | 57:f4819de54e7a | 2349 | |
Lightvalve | 57:f4819de54e7a | 2350 | case MODE_VALVE_POSITION_CONTROL: { |
Lightvalve | 67:c2812cf26c38 | 2351 | if (OPERATING_MODE == 5) { //SW Valve |
Lightvalve | 199:ee79b4692e3b | 2352 | |
Lightvalve | 199:ee79b4692e3b | 2353 | ///////////For Test LIMC/////////////////////////////////////////////////////////////////////////////////////////////////// |
Lightvalve | 199:ee79b4692e3b | 2354 | for(int i=0; i<9; i++) { |
Lightvalve | 199:ee79b4692e3b | 2355 | valve_ref_pos_buffer[i] = valve_ref_pos_buffer[i+1]; |
Lightvalve | 199:ee79b4692e3b | 2356 | } |
Lightvalve | 199:ee79b4692e3b | 2357 | valve_ref_pos_buffer[9] = valve_pos.ref; |
Lightvalve | 199:ee79b4692e3b | 2358 | VALVE_POS_CONTROL(valve_ref_pos_buffer[0]); |
Lightvalve | 199:ee79b4692e3b | 2359 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
Lightvalve | 199:ee79b4692e3b | 2360 | |
Lightvalve | 199:ee79b4692e3b | 2361 | |
Lightvalve | 199:ee79b4692e3b | 2362 | //VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 59:f308b1656d9c | 2363 | V_out = Vout.ref; |
Lightvalve | 67:c2812cf26c38 | 2364 | } else if (CURRENT_CONTROL_MODE == 0) { //PWM |
Lightvalve | 67:c2812cf26c38 | 2365 | V_out = valve_pos.ref; |
Lightvalve | 169:645207e160ca | 2366 | } else { |
Lightvalve | 67:c2812cf26c38 | 2367 | I_REF = valve_pos.ref * 0.001f; |
Lightvalve | 57:f4819de54e7a | 2368 | } |
Lightvalve | 57:f4819de54e7a | 2369 | break; |
Lightvalve | 57:f4819de54e7a | 2370 | } |
Lightvalve | 57:f4819de54e7a | 2371 | |
Lightvalve | 57:f4819de54e7a | 2372 | case MODE_JOINT_CONTROL: { |
Lightvalve | 169:645207e160ca | 2373 | |
Lightvalve | 57:f4819de54e7a | 2374 | double torq_ref = 0.0f; |
Lightvalve | 57:f4819de54e7a | 2375 | pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm] |
Lightvalve | 67:c2812cf26c38 | 2376 | vel.err = (0.0f - vel.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm/s] |
Lightvalve | 57:f4819de54e7a | 2377 | pos.err_sum += pos.err/(float) TMR_FREQ_5k; //[mm] |
Lightvalve | 169:645207e160ca | 2378 | |
Lightvalve | 67:c2812cf26c38 | 2379 | //K & D Low Pass Filter |
Lightvalve | 67:c2812cf26c38 | 2380 | float alpha_K_D = 1.0f/(1.0f + 5000.0f/(2.0f*3.14f*30.0f)); // f_cutoff : 30Hz |
Lightvalve | 67:c2812cf26c38 | 2381 | K_LPF = K_LPF*(1.0f-alpha_K_D)+K_SPRING*(alpha_K_D); |
Lightvalve | 67:c2812cf26c38 | 2382 | D_LPF = D_LPF*(1.0f-alpha_K_D)+D_DAMPER*(alpha_K_D); |
Lightvalve | 169:645207e160ca | 2383 | |
Lightvalve | 139:15621998925b | 2384 | // torq_ref = torq.ref + K_LPF * pos.err - D_LPF * vel.sen / ENC_PULSE_PER_POSITION; //[N] |
Lightvalve | 139:15621998925b | 2385 | torq_ref = torq.ref; |
Lightvalve | 57:f4819de54e7a | 2386 | |
Lightvalve | 57:f4819de54e7a | 2387 | // torque feedback |
Lightvalve | 67:c2812cf26c38 | 2388 | torq.err = torq_ref - torq.sen; //[N] |
Lightvalve | 57:f4819de54e7a | 2389 | torq.err_sum += torq.err/(float) TMR_FREQ_5k; //[N] |
Lightvalve | 57:f4819de54e7a | 2390 | |
Lightvalve | 57:f4819de54e7a | 2391 | if (((OPERATING_MODE&0b110)>>1) == 0 || ((OPERATING_MODE&0b110)>>1) == 1) { |
Lightvalve | 58:2eade98630e2 | 2392 | |
Lightvalve | 57:f4819de54e7a | 2393 | double I_REF_POS = 0.0f; |
Lightvalve | 57:f4819de54e7a | 2394 | double I_REF_FORCE_FB = 0.0f; // I_REF by Force Feedback |
Lightvalve | 57:f4819de54e7a | 2395 | double I_REF_VC = 0.0f; // I_REF for velocity compensation |
Lightvalve | 57:f4819de54e7a | 2396 | |
Lightvalve | 57:f4819de54e7a | 2397 | double temp_vel_pos = 0.0f; |
Lightvalve | 57:f4819de54e7a | 2398 | double temp_vel_torq = 0.0f; |
Lightvalve | 57:f4819de54e7a | 2399 | double wn_Pos = 2.0f * PI * 5.0f; // f_cut : 5Hz Position Control |
Lightvalve | 58:2eade98630e2 | 2400 | |
Lightvalve | 69:3995ffeaa786 | 2401 | if ((OPERATING_MODE & 0x01) == 0) { // Rotary Mode |
Lightvalve | 57:f4819de54e7a | 2402 | 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 | 2403 | // L when P-gain = 100, f_cut = 10Hz L feedforward velocity |
Lightvalve | 69:3995ffeaa786 | 2404 | } else if ((OPERATING_MODE & 0x01) == 1) { |
Lightvalve | 57:f4819de54e7a | 2405 | 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 | 2406 | // L when P-gain = 100, f_cut = 10Hz L feedforward velocity |
Lightvalve | 57:f4819de54e7a | 2407 | } |
Lightvalve | 57:f4819de54e7a | 2408 | 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 | 2409 | else I_REF_POS = temp_vel_pos * ((double) PISTON_AREA_B * 0.00006f / (K_v * sqrt(2.0f / (alpha3 + 1.0f)))); |
Lightvalve | 57:f4819de54e7a | 2410 | |
Lightvalve | 57:f4819de54e7a | 2411 | // velocity compensation for torque control |
Lightvalve | 69:3995ffeaa786 | 2412 | if ((OPERATING_MODE & 0x01) == 0) { // Rotary Mode |
Lightvalve | 57:f4819de54e7a | 2413 | I_REF_FORCE_FB = 0.001f * ((double) P_GAIN_JOINT_TORQUE * torq.err + (double) I_GAIN_JOINT_TORQUE * torq.err_sum); |
Lightvalve | 57:f4819de54e7a | 2414 | // 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 | 2415 | temp_vel_torq = (0.01f * (double) VELOCITY_COMP_GAIN * vel.ref / (double) ENC_PULSE_PER_POSITION) * PI / 180.0f; // rad/s |
Lightvalve | 57:f4819de54e7a | 2416 | // L feedforward velocity |
Lightvalve | 69:3995ffeaa786 | 2417 | } else if ((OPERATING_MODE & 0x01) == 1) { |
Lightvalve | 57:f4819de54e7a | 2418 | 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 | 2419 | // temp_vel_torq = (0.01 * (double) VELOCITY_COMP_GAIN * (double) CUR_VELOCITY / (double) ENC_PULSE_PER_POSITION); // mm/s |
Lightvalve | 57:f4819de54e7a | 2420 | temp_vel_torq = (0.01f * (double) VELOCITY_COMP_GAIN * vel.ref / (double) ENC_PULSE_PER_POSITION); // mm/s |
Lightvalve | 57:f4819de54e7a | 2421 | // L feedforward velocity |
Lightvalve | 57:f4819de54e7a | 2422 | } |
Lightvalve | 57:f4819de54e7a | 2423 | 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 | 2424 | else I_REF_VC = temp_vel_torq * ((double) PISTON_AREA_B * 0.00006f / (K_v * sqrt(2.0f / (alpha3 + 1.0f)))); |
Lightvalve | 57:f4819de54e7a | 2425 | // L velocity(rad/s or mm/s) >> I_ref(mA) |
Lightvalve | 57:f4819de54e7a | 2426 | // Ref_Joint_FT_dot = (Ref_Joint_FT_Nm - Ref_Joint_FT_Nm_old) / TMR_DT_5k; |
Lightvalve | 57:f4819de54e7a | 2427 | // Ref_Joint_FT_Nm_old = Ref_Joint_FT_Nm; |
Lightvalve | 57:f4819de54e7a | 2428 | |
Lightvalve | 57:f4819de54e7a | 2429 | I_REF = (1.0f - alpha_trans) * I_REF_POS + alpha_trans * (I_REF_VC + I_REF_FORCE_FB); |
Lightvalve | 169:645207e160ca | 2430 | |
Lightvalve | 67:c2812cf26c38 | 2431 | // Anti-windup for FT |
Lightvalve | 67:c2812cf26c38 | 2432 | if (I_GAIN_JOINT_TORQUE != 0) { |
Lightvalve | 67:c2812cf26c38 | 2433 | double I_MAX = 10.0f; // Maximum Current : 10mV |
Lightvalve | 67:c2812cf26c38 | 2434 | double Ka = 2.0f / ((double) I_GAIN_JOINT_TORQUE * 0.001f); |
Lightvalve | 67:c2812cf26c38 | 2435 | if (I_REF > I_MAX) { |
Lightvalve | 67:c2812cf26c38 | 2436 | double I_rem = I_REF - I_MAX; |
Lightvalve | 67:c2812cf26c38 | 2437 | I_rem = Ka*I_rem; |
Lightvalve | 67:c2812cf26c38 | 2438 | I_REF = I_MAX; |
Lightvalve | 67:c2812cf26c38 | 2439 | torq.err_sum = torq.err_sum - I_rem /(float) TMR_FREQ_5k; |
Lightvalve | 67:c2812cf26c38 | 2440 | } else if (I_REF < -I_MAX) { |
Lightvalve | 67:c2812cf26c38 | 2441 | double I_rem = I_REF - (-I_MAX); |
Lightvalve | 67:c2812cf26c38 | 2442 | I_rem = Ka*I_rem; |
Lightvalve | 67:c2812cf26c38 | 2443 | I_REF = -I_MAX; |
Lightvalve | 67:c2812cf26c38 | 2444 | torq.err_sum = torq.err_sum - I_rem /(float) TMR_FREQ_5k; |
Lightvalve | 67:c2812cf26c38 | 2445 | } |
Lightvalve | 67:c2812cf26c38 | 2446 | } |
Lightvalve | 57:f4819de54e7a | 2447 | |
Lightvalve | 57:f4819de54e7a | 2448 | } else { |
Lightvalve | 57:f4819de54e7a | 2449 | float VALVE_POS_RAW_FORCE_FB = 0.0f; |
Lightvalve | 72:3436ce769b1e | 2450 | float VALVE_POS_RAW_FORCE_FF = 0.0f; |
Lightvalve | 72:3436ce769b1e | 2451 | float VALVE_POS_RAW = 0.0f; |
Lightvalve | 169:645207e160ca | 2452 | |
Lightvalve | 57:f4819de54e7a | 2453 | 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 | 2454 | + 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 | 2455 | |
Lightvalve | 72:3436ce769b1e | 2456 | 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 | 2457 | |
Lightvalve | 72:3436ce769b1e | 2458 | VALVE_POS_RAW = VALVE_POS_RAW_FORCE_FB + VALVE_POS_RAW_FORCE_FF; |
Lightvalve | 169:645207e160ca | 2459 | |
Lightvalve | 169:645207e160ca | 2460 | |
Lightvalve | 72:3436ce769b1e | 2461 | if (VALVE_POS_RAW >= 0) { |
Lightvalve | 72:3436ce769b1e | 2462 | valve_pos.ref = VALVE_POS_RAW + VALVE_DEADZONE_PLUS; |
Lightvalve | 57:f4819de54e7a | 2463 | } else { |
Lightvalve | 72:3436ce769b1e | 2464 | valve_pos.ref = VALVE_POS_RAW + VALVE_DEADZONE_MINUS; |
Lightvalve | 57:f4819de54e7a | 2465 | } |
Lightvalve | 57:f4819de54e7a | 2466 | |
Lightvalve | 57:f4819de54e7a | 2467 | if(I_GAIN_JOINT_TORQUE != 0) { |
Lightvalve | 139:15621998925b | 2468 | double Ka = 2.0f / (double) I_GAIN_JOINT_TORQUE * 100.0f; |
Lightvalve | 57:f4819de54e7a | 2469 | if(valve_pos.ref>VALVE_MAX_POS) { |
Lightvalve | 57:f4819de54e7a | 2470 | double valve_pos_rem = valve_pos.ref - VALVE_MAX_POS; |
Lightvalve | 57:f4819de54e7a | 2471 | valve_pos_rem = valve_pos_rem * Ka; |
Lightvalve | 57:f4819de54e7a | 2472 | valve_pos.ref = VALVE_MAX_POS; |
Lightvalve | 57:f4819de54e7a | 2473 | torq.err_sum = torq.err_sum - valve_pos_rem/(float) TMR_FREQ_5k; |
Lightvalve | 57:f4819de54e7a | 2474 | } else if(valve_pos.ref < VALVE_MIN_POS) { |
Lightvalve | 57:f4819de54e7a | 2475 | double valve_pos_rem = valve_pos.ref - VALVE_MIN_POS; |
Lightvalve | 57:f4819de54e7a | 2476 | valve_pos_rem = valve_pos_rem * Ka; |
Lightvalve | 57:f4819de54e7a | 2477 | valve_pos.ref = VALVE_MIN_POS; |
Lightvalve | 57:f4819de54e7a | 2478 | torq.err_sum = torq.err_sum - valve_pos_rem/(float) TMR_FREQ_5k; |
Lightvalve | 57:f4819de54e7a | 2479 | } |
Lightvalve | 57:f4819de54e7a | 2480 | } |
Lightvalve | 197:50e95ba0ade2 | 2481 | |
Lightvalve | 197:50e95ba0ade2 | 2482 | ///////////For Test LIMC/////////////////////////////////////////////////////////////////////////////////////////////////// |
Lightvalve | 197:50e95ba0ade2 | 2483 | for(int i=0; i<9; i++) { |
Lightvalve | 197:50e95ba0ade2 | 2484 | valve_ref_pos_buffer[i] = valve_ref_pos_buffer[i+1]; |
Lightvalve | 197:50e95ba0ade2 | 2485 | } |
Lightvalve | 197:50e95ba0ade2 | 2486 | valve_ref_pos_buffer[9] = valve_pos.ref; |
Lightvalve | 197:50e95ba0ade2 | 2487 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
Lightvalve | 197:50e95ba0ade2 | 2488 | |
Lightvalve | 197:50e95ba0ade2 | 2489 | ///////////For Test LIMC/////////////////////////////////////////////////////////////////////////////////////////////////// |
Lightvalve | 197:50e95ba0ade2 | 2490 | //VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 197:50e95ba0ade2 | 2491 | VALVE_POS_CONTROL(valve_ref_pos_buffer[0]); |
Lightvalve | 197:50e95ba0ade2 | 2492 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
Lightvalve | 61:bc8c8270f0ab | 2493 | |
Lightvalve | 67:c2812cf26c38 | 2494 | // Vout.ref = (float) P_GAIN_JOINT_POSITION * 0.01f * ((float) pos.err); |
Lightvalve | 60:64181f1d3e60 | 2495 | V_out = (float) Vout.ref; |
Lightvalve | 57:f4819de54e7a | 2496 | |
Lightvalve | 57:f4819de54e7a | 2497 | } |
Lightvalve | 169:645207e160ca | 2498 | |
Lightvalve | 72:3436ce769b1e | 2499 | torq_ref_past = torq_ref; |
Lightvalve | 58:2eade98630e2 | 2500 | |
Lightvalve | 133:22ab22818e01 | 2501 | |
Lightvalve | 57:f4819de54e7a | 2502 | break; |
Lightvalve | 57:f4819de54e7a | 2503 | } |
Lightvalve | 58:2eade98630e2 | 2504 | |
Lightvalve | 57:f4819de54e7a | 2505 | case MODE_VALVE_OPEN_LOOP: { |
Lightvalve | 57:f4819de54e7a | 2506 | V_out = (float) Vout.ref; |
Lightvalve | 57:f4819de54e7a | 2507 | break; |
Lightvalve | 57:f4819de54e7a | 2508 | } |
Lightvalve | 169:645207e160ca | 2509 | |
Lightvalve | 138:a843f32ced33 | 2510 | case MODE_JOINT_ADAPTIVE_BACKSTEPPING: { |
Lightvalve | 169:645207e160ca | 2511 | |
Lightvalve | 169:645207e160ca | 2512 | |
Lightvalve | 139:15621998925b | 2513 | 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 | 2514 | 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 | 2515 | |
Lightvalve | 139:15621998925b | 2516 | // float Va = (1256.6f + Amm * 39.5f) * 0.000000001f; // 4mm pipe * 100mm + (25mm Cylinder 18mm Rod) * x, unit : m^3 |
Lightvalve | 139:15621998925b | 2517 | // 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 | 2518 | V_adapt = 1.0f / (1.0f/Va + 1.0f/Vb); //initial 0.0000053f |
Lightvalve | 169:645207e160ca | 2519 | |
Lightvalve | 169:645207e160ca | 2520 | |
Lightvalve | 170:42c938a40313 | 2521 | 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 | 2522 | |
Lightvalve | 138:a843f32ced33 | 2523 | float g3_prime = 0.0f; |
Lightvalve | 169:645207e160ca | 2524 | if (torq.sen > Amm*(Ps-Pt)*0.000001f) { |
Lightvalve | 138:a843f32ced33 | 2525 | g3_prime = 1.0f; |
Lightvalve | 138:a843f32ced33 | 2526 | } else if (torq.sen < -Amm*(Ps-Pt)*0.000001f) { |
Lightvalve | 138:a843f32ced33 | 2527 | g3_prime = -1.0f; |
Lightvalve | 138:a843f32ced33 | 2528 | } else { |
Lightvalve | 138:a843f32ced33 | 2529 | if ((value-VALVE_CENTER) > 0) { |
Lightvalve | 169:645207e160ca | 2530 | g3_prime = sqrt(Ps-Pt-torq.sen/Amm*1000000.0f); |
Lightvalve | 169:645207e160ca | 2531 | // g3_prime = sqrt(Ps-Pt); |
Lightvalve | 138:a843f32ced33 | 2532 | } else { |
Lightvalve | 138:a843f32ced33 | 2533 | g3_prime = sqrt(Ps-Pt+torq.sen/Amm*1000000.0f); |
Lightvalve | 139:15621998925b | 2534 | // g3_prime = sqrt(Ps-Pt); |
Lightvalve | 138:a843f32ced33 | 2535 | } |
Lightvalve | 138:a843f32ced33 | 2536 | } |
Lightvalve | 138:a843f32ced33 | 2537 | float tau = 0.01f; |
Lightvalve | 142:43026242815a | 2538 | float K_valve = 0.0004f; |
Lightvalve | 169:645207e160ca | 2539 | |
Lightvalve | 138:a843f32ced33 | 2540 | float x_v = 0.0f; //x_v : -1~1 |
Lightvalve | 138:a843f32ced33 | 2541 | if(value>=VALVE_CENTER) { |
Lightvalve | 138:a843f32ced33 | 2542 | x_v = 1.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER); |
Lightvalve | 138:a843f32ced33 | 2543 | } else { |
Lightvalve | 138:a843f32ced33 | 2544 | x_v = -1.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER); |
Lightvalve | 138:a843f32ced33 | 2545 | } |
Lightvalve | 138:a843f32ced33 | 2546 | float f4 = -x_v/tau; |
Lightvalve | 138:a843f32ced33 | 2547 | float g4 = K_valve/tau; |
Lightvalve | 169:645207e160ca | 2548 | |
Lightvalve | 139:15621998925b | 2549 | float torq_ref_dot = torq.ref_diff * 500.0f; |
Lightvalve | 169:645207e160ca | 2550 | |
Lightvalve | 138:a843f32ced33 | 2551 | pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm] |
Lightvalve | 138:a843f32ced33 | 2552 | vel.err = (0.0f - vel.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm/s] |
Lightvalve | 138:a843f32ced33 | 2553 | pos.err_sum += pos.err/(float) TMR_FREQ_5k; //[mm] |
Lightvalve | 169:645207e160ca | 2554 | |
Lightvalve | 138:a843f32ced33 | 2555 | torq.err = torq.ref - torq.sen; //[N] |
Lightvalve | 138:a843f32ced33 | 2556 | torq.err_sum += torq.err/(float) TMR_FREQ_5k; //[N] |
Lightvalve | 169:645207e160ca | 2557 | |
Lightvalve | 142:43026242815a | 2558 | float k3 = 20000.0f; //2000 |
Lightvalve | 142:43026242815a | 2559 | float k4 = 10.0f; |
Lightvalve | 142:43026242815a | 2560 | float rho3 = 3.2f; |
Lightvalve | 142:43026242815a | 2561 | float rho4 = 25000000.0f; |
Lightvalve | 139:15621998925b | 2562 | float x_4_des = (-f3 + torq_ref_dot - k3*(-torq.err))/(gamma_hat*g3_prime); |
Lightvalve | 138:a843f32ced33 | 2563 | if (x_4_des > 1) x_4_des = 1; |
Lightvalve | 138:a843f32ced33 | 2564 | else if (x_4_des < -1) x_4_des = -1; |
Lightvalve | 169:645207e160ca | 2565 | |
Lightvalve | 139:15621998925b | 2566 | if (x_4_des > 0) { |
Lightvalve | 139:15621998925b | 2567 | valve_pos.ref = x_4_des * (float)(VALVE_MAX_POS - VALVE_CENTER) + (float) VALVE_CENTER; |
Lightvalve | 139:15621998925b | 2568 | } else { |
Lightvalve | 139:15621998925b | 2569 | valve_pos.ref = x_4_des * (float)(VALVE_CENTER - VALVE_MIN_POS) + (float) VALVE_CENTER; |
Lightvalve | 139:15621998925b | 2570 | } |
Lightvalve | 169:645207e160ca | 2571 | |
Lightvalve | 169:645207e160ca | 2572 | |
Lightvalve | 138:a843f32ced33 | 2573 | float x_4_des_dot = (x_4_des - x_4_des_old)*(float) TMR_FREQ_5k; |
Lightvalve | 138:a843f32ced33 | 2574 | x_4_des_old = x_4_des; |
Lightvalve | 169:645207e160ca | 2575 | |
Lightvalve | 139:15621998925b | 2576 | 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 | 2577 | |
Lightvalve | 142:43026242815a | 2578 | float rho_gamma = 50000.0f;//5000 |
Lightvalve | 139:15621998925b | 2579 | 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 | 2580 | gamma_hat = gamma_hat + gamma_hat_dot / (float) TMR_FREQ_5k; |
Lightvalve | 169:645207e160ca | 2581 | break; |
Lightvalve | 138:a843f32ced33 | 2582 | } |
Lightvalve | 171:bfc1fd2629d8 | 2583 | |
Lightvalve | 170:42c938a40313 | 2584 | case MODE_RL: { |
Lightvalve | 170:42c938a40313 | 2585 | //t.reset(); |
Lightvalve | 170:42c938a40313 | 2586 | //t.start(); |
Lightvalve | 170:42c938a40313 | 2587 | |
Lightvalve | 170:42c938a40313 | 2588 | // if(LED == 0) LED = 1; |
Lightvalve | 170:42c938a40313 | 2589 | // else LED = 0; |
Lightvalve | 170:42c938a40313 | 2590 | |
Lightvalve | 170:42c938a40313 | 2591 | if (Update_Done_Flag == 1) { |
Lightvalve | 170:42c938a40313 | 2592 | //Gather Data on each loop |
Lightvalve | 170:42c938a40313 | 2593 | // pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm] |
Lightvalve | 170:42c938a40313 | 2594 | // train_set_x[RL_timer] = pos.sen/(float)(ENC_PULSE_PER_POSITION)/35.0f - 1.0f; //-1.0~1.0 |
Lightvalve | 170:42c938a40313 | 2595 | // train_set_error[RL_timer] = pos.err/70.0f; //-1.0~1.0 |
Lightvalve | 170:42c938a40313 | 2596 | pos.err = pos.sen/(float)(ENC_PULSE_PER_POSITION) - virt_pos; //[mm] |
Lightvalve | 170:42c938a40313 | 2597 | train_set_x[RL_timer] = virt_pos/70.0f; //-1.0~1.0 |
Lightvalve | 170:42c938a40313 | 2598 | train_set_error[RL_timer] = pos.err/70.0f; //-1.0~1.0 |
Lightvalve | 170:42c938a40313 | 2599 | //train_set_count[RL_timer] = (float) RL_timer / (batch_size *num_batch); //-1.0~1.0 |
Lightvalve | 170:42c938a40313 | 2600 | //float temp_array[3] = {train_set_x[RL_timer], train_set_error[RL_timer], train_set_count[RL_timer]}; |
Lightvalve | 170:42c938a40313 | 2601 | float temp_array[2] = {train_set_x[RL_timer], train_set_error[RL_timer]}; |
Lightvalve | 170:42c938a40313 | 2602 | Actor_Network(temp_array); |
Lightvalve | 173:68c7914679ec | 2603 | for (int i=0; i<num_hidden_unit1; i++) { |
Lightvalve | 173:68c7914679ec | 2604 | hx_a_sum_array[RL_timer][i] = hx_a_sum[i]; |
Lightvalve | 173:68c7914679ec | 2605 | } |
Lightvalve | 173:68c7914679ec | 2606 | for (int i=0; i<num_hidden_unit2; i++) { |
Lightvalve | 173:68c7914679ec | 2607 | hxh_a_sum_array[RL_timer][i] = hxh_a_sum[i]; |
Lightvalve | 173:68c7914679ec | 2608 | } |
Lightvalve | 173:68c7914679ec | 2609 | hxhh_a_sum_array[RL_timer][0] = hxhh_a_sum[0]; |
Lightvalve | 173:68c7914679ec | 2610 | hxhh_a_sum_array[RL_timer][1] = hxhh_a_sum[1]; |
Lightvalve | 170:42c938a40313 | 2611 | mean_array[RL_timer] = mean; |
Lightvalve | 170:42c938a40313 | 2612 | deviation_array[RL_timer] = deviation; |
Lightvalve | 170:42c938a40313 | 2613 | action_array[RL_timer] = rand_normal(mean_array[RL_timer], deviation_array[RL_timer]); |
Lightvalve | 170:42c938a40313 | 2614 | |
Lightvalve | 179:d5377766d7ea | 2615 | virt_pos = virt_pos + (action_array[RL_timer] - 5.0f) * 1000.0f * 0.0002f; |
Lightvalve | 179:d5377766d7ea | 2616 | if (virt_pos > 70 ) { |
Lightvalve | 170:42c938a40313 | 2617 | virt_pos = 70.0f; |
Lightvalve | 179:d5377766d7ea | 2618 | }else if(virt_pos < -70) { |
Lightvalve | 170:42c938a40313 | 2619 | virt_pos = -70.0f; |
Lightvalve | 170:42c938a40313 | 2620 | } |
Lightvalve | 171:bfc1fd2629d8 | 2621 | |
Lightvalve | 170:42c938a40313 | 2622 | RL_timer++; |
Lightvalve | 170:42c938a40313 | 2623 | |
Lightvalve | 170:42c938a40313 | 2624 | |
Lightvalve | 170:42c938a40313 | 2625 | if (RL_timer >= batch_size) { |
Lightvalve | 170:42c938a40313 | 2626 | RL_timer = 0; |
Lightvalve | 170:42c938a40313 | 2627 | batch++; |
Lightvalve | 170:42c938a40313 | 2628 | for(int i=0; i<batch_size; i++) { |
Lightvalve | 170:42c938a40313 | 2629 | state_array[i][0] = train_set_x[i]; |
Lightvalve | 170:42c938a40313 | 2630 | state_array[i][1] = train_set_error[i]; |
Lightvalve | 170:42c938a40313 | 2631 | //state_array[i][2] = train_set_count[i]; |
Lightvalve | 170:42c938a40313 | 2632 | } |
Lightvalve | 170:42c938a40313 | 2633 | Update_Case = 1; |
Lightvalve | 170:42c938a40313 | 2634 | Update_Done_Flag = 0; |
Lightvalve | 170:42c938a40313 | 2635 | logging1 = virt_pos; |
Lightvalve | 179:d5377766d7ea | 2636 | |
Lightvalve | 170:42c938a40313 | 2637 | if(batch >= num_batch) { |
Lightvalve | 170:42c938a40313 | 2638 | batch = 0; |
Lightvalve | 170:42c938a40313 | 2639 | RL_timer = 0; |
Lightvalve | 170:42c938a40313 | 2640 | Update_Case = 2; |
Lightvalve | 170:42c938a40313 | 2641 | Update_Done_Flag = 0; |
Lightvalve | 170:42c938a40313 | 2642 | virt_pos = 10.0f; |
Lightvalve | 170:42c938a40313 | 2643 | } |
Lightvalve | 170:42c938a40313 | 2644 | } |
Lightvalve | 170:42c938a40313 | 2645 | } |
Lightvalve | 170:42c938a40313 | 2646 | |
Lightvalve | 170:42c938a40313 | 2647 | else { |
Lightvalve | 170:42c938a40313 | 2648 | pos.err = pos.sen/(float)(ENC_PULSE_PER_POSITION) - virt_pos; //[mm] |
Lightvalve | 170:42c938a40313 | 2649 | float temp_array[3] = {0.0f}; |
Lightvalve | 170:42c938a40313 | 2650 | temp_array[0] = virt_pos/70.0f; //-1.0~1.0 |
Lightvalve | 170:42c938a40313 | 2651 | temp_array[1] = pos.err/70.0f; //-1.0~1.0 |
Lightvalve | 170:42c938a40313 | 2652 | //temp_array[2] = (float) RL_timer / (batch_size *num_batch); //-1.0~1.0 |
Lightvalve | 170:42c938a40313 | 2653 | Actor_Network(temp_array); |
Lightvalve | 170:42c938a40313 | 2654 | action = rand_normal(mean, deviation); |
Lightvalve | 170:42c938a40313 | 2655 | //logging1 = action; |
Lightvalve | 179:d5377766d7ea | 2656 | //logging2 = mean; |
Lightvalve | 179:d5377766d7ea | 2657 | //logging4 = deviation; |
Lightvalve | 179:d5377766d7ea | 2658 | virt_pos = virt_pos + (action-5.0f) * 1000.0f * 0.0002f; |
Lightvalve | 179:d5377766d7ea | 2659 | if (virt_pos > 70) { |
Lightvalve | 170:42c938a40313 | 2660 | virt_pos = 70.0f; |
Lightvalve | 179:d5377766d7ea | 2661 | }else if(virt_pos < -70) { |
Lightvalve | 170:42c938a40313 | 2662 | virt_pos = -70.0f; |
Lightvalve | 170:42c938a40313 | 2663 | } |
Lightvalve | 170:42c938a40313 | 2664 | |
Lightvalve | 170:42c938a40313 | 2665 | logging3 = virt_pos; |
Lightvalve | 170:42c938a40313 | 2666 | } |
Lightvalve | 170:42c938a40313 | 2667 | |
Lightvalve | 170:42c938a40313 | 2668 | //t.stop(); |
Lightvalve | 170:42c938a40313 | 2669 | //logging1 = t.read()*1000.0f; //msec |
Lightvalve | 170:42c938a40313 | 2670 | |
Lightvalve | 170:42c938a40313 | 2671 | break; |
Lightvalve | 170:42c938a40313 | 2672 | } |
Lightvalve | 14:8e7590227d22 | 2673 | |
Lightvalve | 12:6f2531038ea4 | 2674 | default: |
Lightvalve | 12:6f2531038ea4 | 2675 | break; |
Lightvalve | 12:6f2531038ea4 | 2676 | } |
Lightvalve | 14:8e7590227d22 | 2677 | |
Lightvalve | 57:f4819de54e7a | 2678 | |
Lightvalve | 57:f4819de54e7a | 2679 | if (((OPERATING_MODE&0b110)>>1) == 0 || ((OPERATING_MODE&0b110)>>1) == 1) { //Moog Valve or KNR Valve |
Lightvalve | 58:2eade98630e2 | 2680 | |
Lightvalve | 57:f4819de54e7a | 2681 | //////////////////////////////////////////////////////////////////////////// |
Lightvalve | 57:f4819de54e7a | 2682 | //////////////////////////// CURRENT CONTROL ////////////////////////////// |
Lightvalve | 57:f4819de54e7a | 2683 | //////////////////////////////////////////////////////////////////////////// |
Lightvalve | 57:f4819de54e7a | 2684 | if (CURRENT_CONTROL_MODE) { |
Lightvalve | 67:c2812cf26c38 | 2685 | double alpha_update_Iref = 1.0f / (1.0f + 5000.0f / (2.0f * 3.14f * 300.0f)); // f_cutoff : 500Hz |
Lightvalve | 57:f4819de54e7a | 2686 | I_REF_fil = (1.0f - alpha_update_Iref) * I_REF_fil + alpha_update_Iref*I_REF; |
Lightvalve | 57:f4819de54e7a | 2687 | |
Lightvalve | 57:f4819de54e7a | 2688 | I_ERR = I_REF_fil - cur.sen; |
Lightvalve | 67:c2812cf26c38 | 2689 | I_ERR_INT = I_ERR_INT + (I_ERR) * 0.0002f; |
Lightvalve | 57:f4819de54e7a | 2690 | |
Lightvalve | 57:f4819de54e7a | 2691 | |
Lightvalve | 57:f4819de54e7a | 2692 | // Moog Valve Current Control Gain |
Lightvalve | 67:c2812cf26c38 | 2693 | double R_model = 500.0f; // ohm |
Lightvalve | 57:f4819de54e7a | 2694 | double L_model = 1.2f; |
Lightvalve | 57:f4819de54e7a | 2695 | double w0 = 2.0f * 3.14f * 150.0f; |
Lightvalve | 57:f4819de54e7a | 2696 | double KP_I = 0.1f * L_model*w0; |
Lightvalve | 57:f4819de54e7a | 2697 | double KI_I = 0.1f * R_model*w0; |
Lightvalve | 57:f4819de54e7a | 2698 | |
Lightvalve | 57:f4819de54e7a | 2699 | // KNR Valve Current Control Gain |
Lightvalve | 57:f4819de54e7a | 2700 | if (((OPERATING_MODE & 0b110)>>1) == 1) { // KNR Valve |
Lightvalve | 57:f4819de54e7a | 2701 | R_model = 163.0f; // ohm |
Lightvalve | 57:f4819de54e7a | 2702 | L_model = 1.0f; |
Lightvalve | 57:f4819de54e7a | 2703 | w0 = 2.0f * 3.14f * 80.0f; |
Lightvalve | 57:f4819de54e7a | 2704 | KP_I = 1.0f * L_model*w0; |
Lightvalve | 57:f4819de54e7a | 2705 | KI_I = 0.08f * R_model*w0; |
Lightvalve | 57:f4819de54e7a | 2706 | } |
Lightvalve | 57:f4819de54e7a | 2707 | |
Lightvalve | 57:f4819de54e7a | 2708 | double FF_gain = 1.0f; |
Lightvalve | 57:f4819de54e7a | 2709 | |
Lightvalve | 67:c2812cf26c38 | 2710 | VALVE_PWM_RAW = KP_I * 2.0f * I_ERR + KI_I * 2.0f* I_ERR_INT; |
Lightvalve | 57:f4819de54e7a | 2711 | // VALVE_PWM_RAW = VALVE_PWM_RAW + FF_gain * (R_model*I_REF); // Unit : mV |
Lightvalve | 57:f4819de54e7a | 2712 | I_REF_fil_diff = I_REF_fil - I_REF_fil_old; |
Lightvalve | 57:f4819de54e7a | 2713 | I_REF_fil_old = I_REF_fil; |
Lightvalve | 67:c2812cf26c38 | 2714 | // 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 | 2715 | VALVE_PWM_RAW = VALVE_PWM_RAW + FF_gain * (R_model * I_REF_fil); // Unit : mV |
Lightvalve | 67:c2812cf26c38 | 2716 | double V_MAX = 12000.0f; // Maximum Voltage : 12V = 12000mV |
Lightvalve | 57:f4819de54e7a | 2717 | |
Lightvalve | 57:f4819de54e7a | 2718 | double Ka = 3.0f / KP_I; |
Lightvalve | 57:f4819de54e7a | 2719 | if (VALVE_PWM_RAW > V_MAX) { |
Lightvalve | 57:f4819de54e7a | 2720 | V_rem = VALVE_PWM_RAW - V_MAX; |
Lightvalve | 57:f4819de54e7a | 2721 | V_rem = Ka*V_rem; |
Lightvalve | 57:f4819de54e7a | 2722 | VALVE_PWM_RAW = V_MAX; |
Lightvalve | 67:c2812cf26c38 | 2723 | I_ERR_INT = I_ERR_INT - V_rem * 0.0002f; |
Lightvalve | 57:f4819de54e7a | 2724 | } else if (VALVE_PWM_RAW < -V_MAX) { |
Lightvalve | 57:f4819de54e7a | 2725 | V_rem = VALVE_PWM_RAW - (-V_MAX); |
Lightvalve | 57:f4819de54e7a | 2726 | V_rem = Ka*V_rem; |
Lightvalve | 57:f4819de54e7a | 2727 | VALVE_PWM_RAW = -V_MAX; |
Lightvalve | 67:c2812cf26c38 | 2728 | I_ERR_INT = I_ERR_INT - V_rem * 0.0002f; |
Lightvalve | 57:f4819de54e7a | 2729 | } |
Lightvalve | 57:f4819de54e7a | 2730 | Cur_Valve_Open_pulse = cur.sen / mA_PER_pulse; |
Lightvalve | 57:f4819de54e7a | 2731 | } else { |
Lightvalve | 57:f4819de54e7a | 2732 | VALVE_PWM_RAW = I_REF * mV_PER_mA; |
Lightvalve | 57:f4819de54e7a | 2733 | Cur_Valve_Open_pulse = I_REF / mA_PER_pulse; |
Lightvalve | 57:f4819de54e7a | 2734 | } |
Lightvalve | 57:f4819de54e7a | 2735 | |
Lightvalve | 57:f4819de54e7a | 2736 | //////////////////////////////////////////////////////////////////////////// |
Lightvalve | 57:f4819de54e7a | 2737 | ///////////////// Dead Zone Cancellation & Linearization ////////////////// |
Lightvalve | 57:f4819de54e7a | 2738 | //////////////////////////////////////////////////////////////////////////// |
Lightvalve | 57:f4819de54e7a | 2739 | // Dead Zone Cancellation (Mechanical Valve dead-zone) |
Lightvalve | 57:f4819de54e7a | 2740 | if (FLAG_VALVE_DEADZONE) { |
Lightvalve | 57:f4819de54e7a | 2741 | if (VALVE_PWM_RAW > 0) VALVE_PWM_RAW = VALVE_PWM_RAW + VALVE_DEADZONE_PLUS * mV_PER_pulse; // unit: mV |
Lightvalve | 57:f4819de54e7a | 2742 | else if (VALVE_PWM_RAW < 0) VALVE_PWM_RAW = VALVE_PWM_RAW + VALVE_DEADZONE_MINUS * mV_PER_pulse; // unit: mV |
Lightvalve | 57:f4819de54e7a | 2743 | |
Lightvalve | 57:f4819de54e7a | 2744 | VALVE_PWM_VALVE_DZ = VALVE_PWM_RAW + (double)VALVE_CENTER * mV_PER_pulse; // unit: mV |
Lightvalve | 57:f4819de54e7a | 2745 | |
Lightvalve | 57:f4819de54e7a | 2746 | } else { |
Lightvalve | 57:f4819de54e7a | 2747 | VALVE_PWM_VALVE_DZ = VALVE_PWM_RAW; |
Lightvalve | 57:f4819de54e7a | 2748 | } |
Lightvalve | 57:f4819de54e7a | 2749 | |
Lightvalve | 57:f4819de54e7a | 2750 | // Output Voltage Linearization |
Lightvalve | 57:f4819de54e7a | 2751 | double CUR_PWM_nonlin = VALVE_PWM_VALVE_DZ; // Unit : mV |
Lightvalve | 67:c2812cf26c38 | 2752 | double CUR_PWM_lin = PWM_duty_byLT(CUR_PWM_nonlin); // -8000~8000 |
Lightvalve | 57:f4819de54e7a | 2753 | |
Lightvalve | 57:f4819de54e7a | 2754 | // Dead Zone Cancellation (Electrical dead-zone) |
Lightvalve | 67:c2812cf26c38 | 2755 | if (CUR_PWM_lin > 0) V_out = (float) (CUR_PWM_lin + 169.0f); |
Lightvalve | 67:c2812cf26c38 | 2756 | else if (CUR_PWM_lin < 0) V_out = (float) (CUR_PWM_lin - 174.0f); |
Lightvalve | 67:c2812cf26c38 | 2757 | else V_out = (float) (CUR_PWM_lin); |
Lightvalve | 169:645207e160ca | 2758 | } else { //////////////////////////sw valve |
Lightvalve | 89:a7b45368ea0f | 2759 | // Output Voltage Linearization |
Lightvalve | 135:79885a39c161 | 2760 | // double CUR_PWM_nonlin = V_out; // Unit : mV |
Lightvalve | 135:79885a39c161 | 2761 | // double CUR_PWM_lin = PWM_duty_byLT(CUR_PWM_nonlin); // -8000~8000 |
Lightvalve | 89:a7b45368ea0f | 2762 | |
Lightvalve | 89:a7b45368ea0f | 2763 | // Dead Zone Cancellation (Electrical dead-zone) |
Lightvalve | 135:79885a39c161 | 2764 | // if (CUR_PWM_lin > 0) V_out = (float) (CUR_PWM_lin + 169.0f); |
Lightvalve | 135:79885a39c161 | 2765 | // else if (CUR_PWM_lin < 0) V_out = (float) (CUR_PWM_lin - 174.0f); |
Lightvalve | 135:79885a39c161 | 2766 | // else V_out = (float) (CUR_PWM_lin); |
Lightvalve | 135:79885a39c161 | 2767 | |
Lightvalve | 135:79885a39c161 | 2768 | if (V_out > 0 ) V_out = (V_out + 180.0f)/0.8588f; |
Lightvalve | 135:79885a39c161 | 2769 | else if (V_out < 0) V_out = (V_out - 200.0f)/0.8651f; |
Lightvalve | 135:79885a39c161 | 2770 | else V_out = 0.0f; |
Lightvalve | 89:a7b45368ea0f | 2771 | } |
Lightvalve | 169:645207e160ca | 2772 | |
Lightvalve | 67:c2812cf26c38 | 2773 | // if(V_out > 0.0f) V_out = (float) (V_out + 169.0f); |
Lightvalve | 67:c2812cf26c38 | 2774 | // else if(V_out < 0.0f) V_out = (float) (V_out - 174.0f); |
Lightvalve | 67:c2812cf26c38 | 2775 | // else V_out = V_out; |
Lightvalve | 169:645207e160ca | 2776 | |
jobuuu | 7:e9086c72bb22 | 2777 | /******************************************************* |
jobuuu | 7:e9086c72bb22 | 2778 | *** PWM |
jobuuu | 7:e9086c72bb22 | 2779 | ********************************************************/ |
Lightvalve | 169:645207e160ca | 2780 | if(DIR_VALVE<0) { |
Lightvalve | 67:c2812cf26c38 | 2781 | V_out = -V_out; |
Lightvalve | 67:c2812cf26c38 | 2782 | } |
Lightvalve | 169:645207e160ca | 2783 | |
Lightvalve | 49:e7bcfc244d40 | 2784 | if (V_out >= VALVE_VOLTAGE_LIMIT*1000.0f) { |
Lightvalve | 49:e7bcfc244d40 | 2785 | V_out = VALVE_VOLTAGE_LIMIT*1000.0f; |
Lightvalve | 49:e7bcfc244d40 | 2786 | } else if(V_out<=-VALVE_VOLTAGE_LIMIT*1000.0f) { |
Lightvalve | 49:e7bcfc244d40 | 2787 | V_out = -VALVE_VOLTAGE_LIMIT*1000.0f; |
Lightvalve | 27:a2254a485f23 | 2788 | } |
Lightvalve | 49:e7bcfc244d40 | 2789 | PWM_out= V_out/(SUPPLY_VOLTAGE*1000.0f); // Full duty : 12000.0mV |
Lightvalve | 13:747daba9cf59 | 2790 | |
Lightvalve | 19:23b7c1ad8683 | 2791 | // Saturation of output voltage to 12.0V |
Lightvalve | 30:8d561f16383b | 2792 | if(PWM_out > 1.0f) PWM_out=1.0f; |
Lightvalve | 30:8d561f16383b | 2793 | else if (PWM_out < -1.0f) PWM_out=-1.0f; |
Lightvalve | 13:747daba9cf59 | 2794 | |
Lightvalve | 30:8d561f16383b | 2795 | if (PWM_out>0.0f) { |
Lightvalve | 30:8d561f16383b | 2796 | dtc_v=0.0f; |
jobuuu | 1:e04e563be5ce | 2797 | dtc_w=PWM_out; |
jobuuu | 1:e04e563be5ce | 2798 | } else { |
jobuuu | 2:a1c0a37df760 | 2799 | dtc_v=-PWM_out; |
Lightvalve | 30:8d561f16383b | 2800 | dtc_w=0.0f; |
jobuuu | 1:e04e563be5ce | 2801 | } |
Lightvalve | 13:747daba9cf59 | 2802 | |
jobuuu | 1:e04e563be5ce | 2803 | //pwm |
Lightvalve | 30:8d561f16383b | 2804 | TIM4->CCR2 = (PWM_ARR)*(1.0f-dtc_v); |
Lightvalve | 30:8d561f16383b | 2805 | TIM4->CCR1 = (PWM_ARR)*(1.0f-dtc_w); |
Lightvalve | 67:c2812cf26c38 | 2806 | |
Lightvalve | 61:bc8c8270f0ab | 2807 | |
Lightvalve | 57:f4819de54e7a | 2808 | if (TMR2_COUNT_CAN_TX % (int) ((int) TMR_FREQ_5k/CAN_FREQ) == 0) { |
Lightvalve | 20:806196fda269 | 2809 | |
Lightvalve | 54:647072f5307a | 2810 | // Position, Velocity, and Torque (ID:1200) |
Lightvalve | 56:6f50d9d3bfee | 2811 | if (flag_data_request[0] == HIGH) { |
Lightvalve | 57:f4819de54e7a | 2812 | if ((OPERATING_MODE & 0b01) == 0) { // Rotary Actuator |
Lightvalve | 54:647072f5307a | 2813 | if (SENSING_MODE == 0) { |
Lightvalve | 67:c2812cf26c38 | 2814 | CAN_TX_POSITION_FT((int16_t) (pos.sen), (int16_t) (vel.sen/10.0f), (int16_t) (torq.sen*10.0f)); |
Lightvalve | 54:647072f5307a | 2815 | } else if (SENSING_MODE == 1) { |
Lightvalve | 58:2eade98630e2 | 2816 | 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 | 2817 | } |
Lightvalve | 57:f4819de54e7a | 2818 | } else if ((OPERATING_MODE & 0b01) == 1) { // Linear Actuator |
Lightvalve | 54:647072f5307a | 2819 | if (SENSING_MODE == 0) { |
Lightvalve | 67:c2812cf26c38 | 2820 | 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 | 2821 | } else if (SENSING_MODE == 1) { |
Lightvalve | 67:c2812cf26c38 | 2822 | 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 | 2823 | } |
Lightvalve | 52:8ea76864368a | 2824 | } |
Lightvalve | 52:8ea76864368a | 2825 | } |
Lightvalve | 56:6f50d9d3bfee | 2826 | if (flag_data_request[1] == HIGH) { |
Lightvalve | 179:d5377766d7ea | 2827 | CAN_TX_TORQUE((int16_t) (return_G[0]*100.0f)); //1300 |
Lightvalve | 54:647072f5307a | 2828 | } |
Lightvalve | 58:2eade98630e2 | 2829 | |
Lightvalve | 58:2eade98630e2 | 2830 | |
Lightvalve | 171:bfc1fd2629d8 | 2831 | if (flag_data_request[2] == HIGH) { |
Lightvalve | 169:645207e160ca | 2832 | double t_value = 0.0f; |
Lightvalve | 171:bfc1fd2629d8 | 2833 | if(value>=(float) VALVE_CENTER) { |
Lightvalve | 131:d08121ac87ba | 2834 | t_value = 10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER); |
Lightvalve | 89:a7b45368ea0f | 2835 | } else { |
Lightvalve | 131:d08121ac87ba | 2836 | t_value = -10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER); |
Lightvalve | 89:a7b45368ea0f | 2837 | } |
Lightvalve | 169:645207e160ca | 2838 | double t_value_ref = 0.0f; |
Lightvalve | 169:645207e160ca | 2839 | if(valve_pos.ref>=(float) VALVE_CENTER) { |
Lightvalve | 169:645207e160ca | 2840 | t_value_ref = 10000.0f*((double)valve_pos.ref - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER); |
Lightvalve | 169:645207e160ca | 2841 | } else { |
Lightvalve | 169:645207e160ca | 2842 | t_value_ref = -10000.0f*((double)valve_pos.ref - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER); |
Lightvalve | 169:645207e160ca | 2843 | } |
Lightvalve | 169:645207e160ca | 2844 | CAN_TX_PRES((int16_t) (t_value), (int16_t) (t_value_ref)); // 1400 |
Lightvalve | 55:b25725257569 | 2845 | } |
Lightvalve | 58:2eade98630e2 | 2846 | |
Lightvalve | 57:f4819de54e7a | 2847 | //If it doesn't rest, below can can not work. |
Lightvalve | 57:f4819de54e7a | 2848 | for (can_rest = 0; can_rest < 10000; can_rest++) { |
Lightvalve | 57:f4819de54e7a | 2849 | ; |
Lightvalve | 57:f4819de54e7a | 2850 | } |
Lightvalve | 58:2eade98630e2 | 2851 | |
Lightvalve | 169:645207e160ca | 2852 | if (flag_data_request[3] == HIGH) { |
Lightvalve | 54:647072f5307a | 2853 | //PWM |
Lightvalve | 180:02be1711ee0b | 2854 | //CAN_TX_PWM((int16_t) (V[0]*100.0f)); //1500 |
Lightvalve | 192:637092202815 | 2855 | CAN_TX_PWM((int16_t) (f_future[1])); //1500 |
Lightvalve | 54:647072f5307a | 2856 | } |
Lightvalve | 179:d5377766d7ea | 2857 | |
Lightvalve | 56:6f50d9d3bfee | 2858 | if (flag_data_request[4] == HIGH) { |
Lightvalve | 54:647072f5307a | 2859 | //valve position |
Lightvalve | 179:d5377766d7ea | 2860 | CAN_TX_VALVE_POSITION((int16_t) pos.sen/(float)(ENC_PULSE_PER_POSITION), (int16_t) virt_pos, (int16_t) (logging2*1000.0f), (int16_t) (logging4*1000.0f)); //1600 |
Lightvalve | 179:d5377766d7ea | 2861 | //CAN_TX_VALVE_POSITION((int16_t) action_array[20], (int16_t) virt_pos, (int16_t) Update_Case*1000, (int16_t) (logging4*1000.0f)); //1600 |
Lightvalve | 54:647072f5307a | 2862 | } |
Lightvalve | 20:806196fda269 | 2863 | |
Lightvalve | 54:647072f5307a | 2864 | // Others : Reference position, Reference FT, PWM, Current (ID:1300) |
Lightvalve | 52:8ea76864368a | 2865 | // if (flag_data_request[1] == HIGH) { |
Lightvalve | 52:8ea76864368a | 2866 | // CAN_TX_SOMETHING((int) (FORCE_VREF), (int16_t) (1), (int16_t) (2), (int16_t) (3)); |
Lightvalve | 52:8ea76864368a | 2867 | // } |
Lightvalve | 54:647072f5307a | 2868 | //if (flag_delay_test == 1){ |
Lightvalve | 58:2eade98630e2 | 2869 | //CAN_TX_PRES((int16_t) (0),(int16_t) torq_ref); |
Lightvalve | 54:647072f5307a | 2870 | //} |
Lightvalve | 52:8ea76864368a | 2871 | |
Lightvalve | 54:647072f5307a | 2872 | TMR2_COUNT_CAN_TX = 0; |
Lightvalve | 54:647072f5307a | 2873 | } |
Lightvalve | 54:647072f5307a | 2874 | TMR2_COUNT_CAN_TX++; |
Lightvalve | 52:8ea76864368a | 2875 | |
Lightvalve | 20:806196fda269 | 2876 | } |
Lightvalve | 52:8ea76864368a | 2877 | TIM3->SR = 0x0; // reset the status register |
Lightvalve | 52:8ea76864368a | 2878 | |
Lightvalve | 58:2eade98630e2 | 2879 | } |