![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
for learning
main.cpp@240:bffaba802829, 2021-01-03 (annotated)
- Committer:
- Lightvalve
- Date:
- Sun Jan 03 06:18:00 2021 +0000
- Revision:
- 240:bffaba802829
- Parent:
- 239:cfce386d54a3
- Child:
- 241:01a01b683a42
210103-2 500Hz num_input 13 210102 data
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Lightvalve | 240:bffaba802829 | 1 | //210103_2 500Hz num_input 13 210102 data |
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; |
Lightvalve | 207:c70c5a9f17dd | 64 | State torq_dot; |
jobuuu | 7:e9086c72bb22 | 65 | State pres_A; |
jobuuu | 7:e9086c72bb22 | 66 | State pres_B; |
jobuuu | 7:e9086c72bb22 | 67 | State cur; |
Lightvalve | 14:8e7590227d22 | 68 | State valve_pos; |
Lightvalve | 14:8e7590227d22 | 69 | |
Lightvalve | 14:8e7590227d22 | 70 | State INIT_Vout; |
Lightvalve | 14:8e7590227d22 | 71 | State INIT_Valve_Pos; |
Lightvalve | 14:8e7590227d22 | 72 | State INIT_Pos; |
Lightvalve | 14:8e7590227d22 | 73 | State INIT_torq; |
jobuuu | 5:a4319f79457b | 74 | |
Lightvalve | 19:23b7c1ad8683 | 75 | extern int CID_RX_CMD; |
Lightvalve | 19:23b7c1ad8683 | 76 | extern int CID_RX_REF_POSITION; |
Lightvalve | 49:e7bcfc244d40 | 77 | extern int CID_RX_REF_VALVE_POS; |
Lightvalve | 45:35fa6884d0c6 | 78 | extern int CID_RX_REF_PWM; |
Lightvalve | 19:23b7c1ad8683 | 79 | |
Lightvalve | 19:23b7c1ad8683 | 80 | extern int CID_TX_INFO; |
Lightvalve | 19:23b7c1ad8683 | 81 | extern int CID_TX_POSITION; |
Lightvalve | 19:23b7c1ad8683 | 82 | extern int CID_TX_TORQUE; |
Lightvalve | 19:23b7c1ad8683 | 83 | extern int CID_TX_PRES; |
Lightvalve | 19:23b7c1ad8683 | 84 | extern int CID_TX_VOUT; |
Lightvalve | 19:23b7c1ad8683 | 85 | extern int CID_TX_VALVE_POSITION; |
Lightvalve | 19:23b7c1ad8683 | 86 | |
Lightvalve | 61:bc8c8270f0ab | 87 | |
Lightvalve | 61:bc8c8270f0ab | 88 | |
Lightvalve | 61:bc8c8270f0ab | 89 | |
jobuuu | 7:e9086c72bb22 | 90 | // ============================================================================= |
jobuuu | 7:e9086c72bb22 | 91 | // ============================================================================= |
jobuuu | 7:e9086c72bb22 | 92 | // ============================================================================= |
jobuuu | 7:e9086c72bb22 | 93 | |
Lightvalve | 12:6f2531038ea4 | 94 | /******************************************************************************* |
Lightvalve | 12:6f2531038ea4 | 95 | * REFERENCE MODE |
Lightvalve | 12:6f2531038ea4 | 96 | ******************************************************************************/ |
Lightvalve | 13:747daba9cf59 | 97 | enum _REFERENCE_MODE { |
Lightvalve | 13:747daba9cf59 | 98 | MODE_REF_NO_ACT = 0, //0 |
Lightvalve | 12:6f2531038ea4 | 99 | MODE_REF_DIRECT, //1 |
Lightvalve | 12:6f2531038ea4 | 100 | MODE_REF_COS_INC, //2 |
Lightvalve | 12:6f2531038ea4 | 101 | MODE_REF_LINE_INC, //3 |
Lightvalve | 12:6f2531038ea4 | 102 | MODE_REF_SIN_WAVE, //4 |
Lightvalve | 12:6f2531038ea4 | 103 | MODE_REF_SQUARE_WAVE, //5 |
Lightvalve | 12:6f2531038ea4 | 104 | }; |
Lightvalve | 12:6f2531038ea4 | 105 | |
Lightvalve | 12:6f2531038ea4 | 106 | /******************************************************************************* |
Lightvalve | 12:6f2531038ea4 | 107 | * CONTROL MODE |
Lightvalve | 12:6f2531038ea4 | 108 | ******************************************************************************/ |
Lightvalve | 13:747daba9cf59 | 109 | enum _CONTROL_MODE { |
Lightvalve | 12:6f2531038ea4 | 110 | //control mode |
Lightvalve | 12:6f2531038ea4 | 111 | MODE_NO_ACT = 0, //0 |
Lightvalve | 47:fdcb8bd86fd6 | 112 | MODE_VALVE_POSITION_CONTROL, //1 |
Lightvalve | 47:fdcb8bd86fd6 | 113 | MODE_JOINT_CONTROL, //2 |
Lightvalve | 50:3c630b5eba9f | 114 | |
Lightvalve | 47:fdcb8bd86fd6 | 115 | MODE_VALVE_OPEN_LOOP, //3 |
Lightvalve | 138:a843f32ced33 | 116 | MODE_JOINT_ADAPTIVE_BACKSTEPPING, //4 |
Lightvalve | 171:bfc1fd2629d8 | 117 | MODE_RL, //5 |
Lightvalve | 13:747daba9cf59 | 118 | |
Lightvalve | 12:6f2531038ea4 | 119 | MODE_JOINT_POSITION_PRES_CONTROL_PWM, //6 |
Lightvalve | 12:6f2531038ea4 | 120 | MODE_JOINT_POSITION_PRES_CONTROL_VALVE_POSITION, //7 |
Lightvalve | 12:6f2531038ea4 | 121 | MODE_VALVE_POSITION_PRES_CONTROL_LEARNING, //8 |
Lightvalve | 13:747daba9cf59 | 122 | |
Lightvalve | 12:6f2531038ea4 | 123 | MODE_TEST_CURRENT_CONTROL, //9 |
Lightvalve | 12:6f2531038ea4 | 124 | MODE_TEST_PWM_CONTROL, //10 |
Lightvalve | 21:e5f1a43ea6f9 | 125 | |
Lightvalve | 14:8e7590227d22 | 126 | MODE_CURRENT_CONTROL, //11 |
Lightvalve | 14:8e7590227d22 | 127 | MODE_JOINT_POSITION_TORQUE_CONTROL_CURRENT, //12 |
Lightvalve | 14:8e7590227d22 | 128 | MODE_JOINT_POSITION_PRES_CONTROL_CURRENT, //13 |
Lightvalve | 171:bfc1fd2629d8 | 129 | MODE_VALVE_POSITION_TORQUE_CONTROL_LEARNING, //14 |
Lightvalve | 13:747daba9cf59 | 130 | |
Lightvalve | 12:6f2531038ea4 | 131 | //utility |
Lightvalve | 12:6f2531038ea4 | 132 | MODE_TORQUE_SENSOR_NULLING = 20, //20 |
Lightvalve | 12:6f2531038ea4 | 133 | MODE_VALVE_NULLING_AND_DEADZONE_SETTING, //21 |
Lightvalve | 12:6f2531038ea4 | 134 | MODE_FIND_HOME, //22 |
Lightvalve | 57:f4819de54e7a | 135 | MODE_VALVE_GAIN_SETTING, //23 |
Lightvalve | 12:6f2531038ea4 | 136 | MODE_PRESSURE_SENSOR_NULLING, //24 |
Lightvalve | 12:6f2531038ea4 | 137 | MODE_PRESSURE_SENSOR_CALIB, //25 |
Lightvalve | 12:6f2531038ea4 | 138 | MODE_ROTARY_FRICTION_TUNING, //26 |
Lightvalve | 13:747daba9cf59 | 139 | |
Lightvalve | 12:6f2531038ea4 | 140 | MODE_DDV_POS_VS_PWM_ID = 30, //30 |
Lightvalve | 12:6f2531038ea4 | 141 | MODE_DDV_DEADZONE_AND_CENTER, //31 |
Lightvalve | 12:6f2531038ea4 | 142 | MODE_DDV_POS_VS_FLOWRATE, //32 |
Lightvalve | 57:f4819de54e7a | 143 | MODE_SYSTEM_ID, //33 |
Lightvalve | 169:645207e160ca | 144 | MODE_FREQ_TEST, //34 |
Lightvalve | 169:645207e160ca | 145 | MODE_SEND_BUFFER, //35 |
Lightvalve | 169:645207e160ca | 146 | MODE_SEND_OVER, //36 |
Lightvalve | 169:645207e160ca | 147 | MODE_STEP_TEST, //37 |
Lightvalve | 12:6f2531038ea4 | 148 | }; |
Lightvalve | 12:6f2531038ea4 | 149 | |
Lightvalve | 65:a2d7c63419c2 | 150 | void SystemClock_Config(void) |
Lightvalve | 65:a2d7c63419c2 | 151 | { |
Lightvalve | 169:645207e160ca | 152 | RCC_OscInitTypeDef RCC_OscInitStruct = {0}; |
Lightvalve | 169:645207e160ca | 153 | RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; |
Lightvalve | 65:a2d7c63419c2 | 154 | |
Lightvalve | 169:645207e160ca | 155 | /** Configure the main internal regulator output voltage |
Lightvalve | 169:645207e160ca | 156 | */ |
Lightvalve | 169:645207e160ca | 157 | __HAL_RCC_PWR_CLK_ENABLE(); |
Lightvalve | 169:645207e160ca | 158 | __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); |
Lightvalve | 169:645207e160ca | 159 | /** Initializes the CPU, AHB and APB busses clocks |
Lightvalve | 169:645207e160ca | 160 | */ |
Lightvalve | 169:645207e160ca | 161 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; |
Lightvalve | 169:645207e160ca | 162 | RCC_OscInitStruct.HSIState = RCC_HSI_ON; |
Lightvalve | 169:645207e160ca | 163 | RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; |
Lightvalve | 169:645207e160ca | 164 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
Lightvalve | 169:645207e160ca | 165 | RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; |
Lightvalve | 169:645207e160ca | 166 | RCC_OscInitStruct.PLL.PLLM = 8;//8 |
Lightvalve | 169:645207e160ca | 167 | RCC_OscInitStruct.PLL.PLLN = 180; //180 |
Lightvalve | 169:645207e160ca | 168 | RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; |
Lightvalve | 169:645207e160ca | 169 | RCC_OscInitStruct.PLL.PLLQ = 2; |
Lightvalve | 169:645207e160ca | 170 | RCC_OscInitStruct.PLL.PLLR = 2; |
Lightvalve | 169:645207e160ca | 171 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { |
Lightvalve | 169:645207e160ca | 172 | //Error_Handler(); |
Lightvalve | 169:645207e160ca | 173 | } |
Lightvalve | 169:645207e160ca | 174 | /** Activate the Over-Drive mode |
Lightvalve | 169:645207e160ca | 175 | */ |
Lightvalve | 169:645207e160ca | 176 | if (HAL_PWREx_EnableOverDrive() != HAL_OK) { |
Lightvalve | 169:645207e160ca | 177 | //Error_Handler(); |
Lightvalve | 169:645207e160ca | 178 | } |
Lightvalve | 169:645207e160ca | 179 | /** Initializes the CPU, AHB and APB busses clocks |
Lightvalve | 169:645207e160ca | 180 | */ |
Lightvalve | 169:645207e160ca | 181 | RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |
Lightvalve | 169:645207e160ca | 182 | |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; |
Lightvalve | 169:645207e160ca | 183 | RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; |
Lightvalve | 169:645207e160ca | 184 | RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; |
Lightvalve | 169:645207e160ca | 185 | RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4; |
Lightvalve | 169:645207e160ca | 186 | RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; |
Lightvalve | 65:a2d7c63419c2 | 187 | |
Lightvalve | 169:645207e160ca | 188 | if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) { |
Lightvalve | 169:645207e160ca | 189 | //Error_Handler(); |
Lightvalve | 169:645207e160ca | 190 | } |
Lightvalve | 65:a2d7c63419c2 | 191 | } |
Lightvalve | 65:a2d7c63419c2 | 192 | |
Lightvalve | 99:7bbcb3c0fb06 | 193 | float u_past[num_array_u_past] = {0.0f}; |
Lightvalve | 68:328e1be06f5d | 194 | float x_past[num_array_x_past] = {0.0f}; |
Lightvalve | 85:a3b46118b5cd | 195 | float x_future[num_array_x_future] = {0.0f}; |
Lightvalve | 68:328e1be06f5d | 196 | float f_past[num_array_f_past] = {0.0f}; |
Lightvalve | 68:328e1be06f5d | 197 | float f_future[num_array_f_future] = {0.0f}; |
Lightvalve | 66:a8e6799dbce3 | 198 | |
Lightvalve | 73:f80dc3970c99 | 199 | float input_NN[num_input] = { 0.0f }; |
Lightvalve | 65:a2d7c63419c2 | 200 | |
Lightvalve | 169:645207e160ca | 201 | const float h1[num_input][16] = { |
Lightvalve | 240:bffaba802829 | 202 | {2.122974395751953f,-0.38776177167892456f,-0.42449960112571716f,2.0468480587005615f,-0.18718937039375305f,0.11834079772233963f,-0.03311857581138611f,-0.054195523262023926f,-1.8910423517227173f,-0.04320235922932625f,-0.1637221872806549f,-0.022264301776885986f,1.3689982891082764f,-1.3352489471435547f,-0.014840304851531982f,-1.893905758857727f}, |
Lightvalve | 240:bffaba802829 | 203 | {1.054883599281311f,-0.38444966077804565f,0.1132459044456482f,1.1167829036712646f,0.1572902798652649f,0.1293565332889557f,0.15329760313034058f,-0.20474094152450562f,-1.1521263122558594f,0.0402907058596611f,0.2308642417192459f,0.027331318706274033f,1.1790597438812256f,-0.25108602643013f,0.11683917045593262f,-1.3148897886276245f}, |
Lightvalve | 240:bffaba802829 | 204 | {0.3260919451713562f,-0.20189961791038513f,-0.20909681916236877f,0.35111910104751587f,-0.20735450088977814f,0.16978304088115692f,-0.21266265213489532f,0.12177395820617676f,-0.4303519129753113f,-0.47478383779525757f,0.25495073199272156f,-0.4649527668952942f,0.9698973298072815f,-0.6320164799690247f,0.21537500619888306f,-0.6965923309326172f}, |
Lightvalve | 240:bffaba802829 | 205 | {-0.2091784030199051f,-0.2055417001247406f,-0.27100086212158203f,0.13696816563606262f,-0.09469953179359436f,-0.2871801257133484f,-0.10579374432563782f,0.09893089532852173f,0.08474475890398026f,-0.09117371588945389f,-0.0946950688958168f,-0.1597663164138794f,0.10443052649497986f,0.40466687083244324f,-0.44673171639442444f,0.4296048581600189f}, |
Lightvalve | 240:bffaba802829 | 206 | {-1.072396993637085f,-0.4420686662197113f,-0.23704257607460022f,-1.532881498336792f,-0.06314542889595032f,-0.190965935587883f,-0.1467491090297699f,-0.32198232412338257f,0.672989547252655f,0.3549209535121918f,0.15744635462760925f,0.08820515871047974f,-1.0165066719055176f,0.3843304216861725f,0.2870906591415405f,0.9832358956336975f}, |
Lightvalve | 240:bffaba802829 | 207 | {-2.5933806896209717f,-0.49318426847457886f,0.24129003286361694f,-2.101083517074585f,0.25041860342025757f,-0.3333922326564789f,0.1312219500541687f,0.4296189546585083f,1.8742114305496216f,-0.0453566238284111f,-0.30207350850105286f,-0.02464285120368004f,-2.6114871501922607f,1.7934314012527466f,0.21352016925811768f,2.4308128356933594f}, |
Lightvalve | 240:bffaba802829 | 208 | {1.4060094356536865f,-0.4593402147293091f,-0.17035451531410217f,-2.0941596031188965f,-0.2231789529323578f,-0.7733786106109619f,-0.30459409952163696f,0.08766061067581177f,-1.5012120008468628f,-0.5738863348960876f,0.28786879777908325f,0.07115523517131805f,1.3073227405548096f,-2.0536022186279297f,-0.13316160440444946f,0.612513542175293f}, |
Lightvalve | 240:bffaba802829 | 209 | {0.4193624258041382f,0.2952363193035126f,0.1189124584197998f,-0.801013708114624f,-0.19704079627990723f,-0.9535843133926392f,-0.3993116319179535f,0.13529682159423828f,-0.09175866842269897f,-0.22769828140735626f,-0.18271225690841675f,-0.2803727984428406f,-0.12039829790592194f,-0.569157600402832f,-0.350609689950943f,-0.19401194155216217f}, |
Lightvalve | 240:bffaba802829 | 210 | {-0.6279920339584351f,-0.3600463271141052f,0.32874053716659546f,0.9218637347221375f,-0.43446803092956543f,-0.6581318378448486f,-0.3732292056083679f,-0.03713726997375488f,1.1847983598709106f,-0.3185393214225769f,-0.014060413464903831f,0.1525433510541916f,-1.4669981002807617f,2.6286604404449463f,0.24592554569244385f,-0.5994797348976135f}, |
Lightvalve | 240:bffaba802829 | 211 | {-0.4040067791938782f,-0.45135271549224854f,-0.43931299448013306f,0.20023031532764435f,-0.40003886818885803f,-0.09406930953264236f,-0.28505805134773254f,-0.025462865829467773f,0.2026372104883194f,0.09695766121149063f,0.012715869583189487f,0.2449764609336853f,-0.8653539419174194f,0.32294973731040955f,0.21423685550689697f,-0.5703613758087158f}, |
Lightvalve | 240:bffaba802829 | 212 | {-0.5376168489456177f,0.2158193290233612f,-0.38009896874427795f,0.14540553092956543f,0.3989499807357788f,0.24190852046012878f,0.444122850894928f,0.2396908402442932f,0.059057943522930145f,0.20812928676605225f,-0.49277493357658386f,0.04622955247759819f,-0.04156821593642235f,-0.7683967351913452f,-0.06324887275695801f,-0.4491087794303894f}, |
Lightvalve | 240:bffaba802829 | 213 | {-0.29452797770500183f,-0.3503324091434479f,0.009307235479354858f,0.20129835605621338f,0.0268385112285614f,0.5140036344528198f,-0.3031163811683655f,0.11987882852554321f,-0.20511949062347412f,-0.31979477405548096f,0.3825034499168396f,-0.2279401421546936f,0.10135215520858765f,-1.0714203119277954f,0.12017554044723511f,-0.07516074925661087f}, |
Lightvalve | 240:bffaba802829 | 214 | {-0.27208060026168823f,0.07206348329782486f,0.10736191272735596f,0.3668510317802429f,-0.3137713074684143f,0.08469216525554657f,0.283999502658844f,-0.3683045208454132f,0.5107637643814087f,0.42085954546928406f,0.06602732837200165f,0.31689608097076416f,-0.6041010618209839f,-0.5318670868873596f,0.3271452784538269f,-0.37038949131965637f}, |
Lightvalve | 65:a2d7c63419c2 | 215 | }; |
Lightvalve | 65:a2d7c63419c2 | 216 | |
Lightvalve | 169:645207e160ca | 217 | const float h2[16][16] = { |
Lightvalve | 240:bffaba802829 | 218 | {-0.20274241268634796f,2.421971082687378f,0.3970688283443451f,0.1834750473499298f,2.4249231815338135f,0.5273239612579346f,-0.4416234493255615f,0.3705202639102936f,0.7134361267089844f,0.2516556680202484f,-1.4060431718826294f,-0.2831149697303772f,-0.05893652141094208f,-0.9120204448699951f,0.36879292130470276f,0.13572558760643005f}, |
Lightvalve | 240:bffaba802829 | 219 | {-0.4029994606971741f,0.38608962297439575f,-0.1258525252342224f,0.30357667803764343f,0.3360573947429657f,0.36685335636138916f,-0.22937726974487305f,-0.002998271957039833f,-0.20960953831672668f,0.0450192466378212f,-0.16880837082862854f,0.17755672335624695f,0.2323802411556244f,0.2596715986728668f,0.3483935296535492f,0.1732930839061737f}, |
Lightvalve | 240:bffaba802829 | 220 | {0.11663869023323059f,0.16539296507835388f,-0.18695728480815887f,-0.2917379140853882f,-0.30005210638046265f,0.26175805926322937f,-0.07184237241744995f,0.07815548777580261f,-0.12396180629730225f,0.1325257122516632f,0.05719554424285889f,0.26685407757759094f,-0.3447650372982025f,0.055430084466934204f,-0.4035770893096924f,-0.05605858564376831f}, |
Lightvalve | 240:bffaba802829 | 221 | {-0.37363696098327637f,0.7793343663215637f,-0.21874599158763885f,0.40131691098213196f,-5.776402473449707f,0.0008264562929980457f,-0.03333192691206932f,-0.19506579637527466f,-1.8619548082351685f,-0.009909694083034992f,-1.4413032531738281f,0.06510794162750244f,-0.3263755142688751f,0.7545951008796692f,-0.212007537484169f,-0.35539939999580383f}, |
Lightvalve | 240:bffaba802829 | 222 | {-0.27256131172180176f,-0.2740507423877716f,-0.4207117557525635f,-0.11853066086769104f,0.11417070031166077f,-0.11616286635398865f,0.4096128046512604f,-0.01269116997718811f,-0.18566495180130005f,0.07329544425010681f,0.357684463262558f,-0.02883175015449524f,-0.00022298097610473633f,0.13074633479118347f,0.15151432156562805f,0.20117709040641785f}, |
Lightvalve | 240:bffaba802829 | 223 | {0.1631280481815338f,-0.3066669702529907f,-0.2956621050834656f,-0.20416049659252167f,-0.7335392236709595f,-0.170254185795784f,-0.18918344378471375f,-0.15483447909355164f,-0.4351620376110077f,0.2498018443584442f,-0.40331128239631653f,-0.35881948471069336f,-0.17363902926445007f,0.33837366104125977f,-0.4114897847175598f,-0.04338723421096802f}, |
Lightvalve | 240:bffaba802829 | 224 | {0.07912376523017883f,0.20786848664283752f,0.35353556275367737f,-0.21646185219287872f,0.10170641541481018f,-0.3878430724143982f,-0.02557438611984253f,0.3767019212245941f,0.16915258765220642f,-0.3738635778427124f,-0.3689180314540863f,-0.059652626514434814f,0.3187622129917145f,-0.4291103780269623f,0.08102068305015564f,-0.3701837360858917f}, |
Lightvalve | 240:bffaba802829 | 225 | {0.1521454155445099f,-0.27620697021484375f,0.16151782870292664f,-0.3318508565425873f,-0.03230056166648865f,0.2152136266231537f,0.04167729616165161f,-0.35635125637054443f,-0.4140225350856781f,-0.4184368848800659f,-0.3026992678642273f,-0.42655059695243835f,-0.30292224884033203f,-0.1371966302394867f,-0.4306396543979645f,-0.2759517729282379f}, |
Lightvalve | 240:bffaba802829 | 226 | {0.18394997715950012f,-1.0212006568908691f,0.24676629900932312f,-0.07940912246704102f,-1.515834093093872f,-0.9244871735572815f,-0.18848899006843567f,0.1286889910697937f,-2.457322597503662f,-0.2793862223625183f,0.363804429769516f,-0.1368420124053955f,0.1348545402288437f,0.6231523752212524f,-0.1576608419418335f,0.13910707831382751f}, |
Lightvalve | 240:bffaba802829 | 227 | {-0.25617533922195435f,-0.3932470977306366f,-0.4131093919277191f,0.2990696132183075f,-0.4584915339946747f,0.14110223948955536f,-0.22487886250019073f,-0.2127760350704193f,-0.6305164694786072f,0.010719792917370796f,-0.2152911275625229f,-0.2806147336959839f,0.3250197768211365f,-0.1093597263097763f,-0.4016580879688263f,0.0012068450450897217f}, |
Lightvalve | 240:bffaba802829 | 228 | {0.17695042490959167f,0.42441698908805847f,-0.14654973149299622f,-0.2512646019458771f,0.28533807396888733f,-0.33422166109085083f,-0.35073933005332947f,-0.171255961060524f,0.292316734790802f,-0.2816634774208069f,0.15038667619228363f,0.32135525345802307f,0.2513468563556671f,0.22251132130622864f,-0.3458877503871918f,0.38228967785835266f}, |
Lightvalve | 240:bffaba802829 | 229 | {-0.2570708394050598f,0.06738440692424774f,-0.2685445249080658f,-0.2199736088514328f,0.11006828397512436f,-0.0013313778908923268f,-0.06503403186798096f,-0.14890238642692566f,0.20612774789333344f,-0.3696085810661316f,0.13957753777503967f,0.006326436996459961f,-0.1564968228340149f,-0.43253979086875916f,0.026546984910964966f,0.21132752299308777f}, |
Lightvalve | 240:bffaba802829 | 230 | {0.4071715176105499f,1.356438398361206f,-0.19858418405056f,-0.07120859622955322f,-1.4493643045425415f,0.41052812337875366f,-0.48465821146965027f,0.015368802472949028f,3.8531370162963867f,0.3758598864078522f,-1.0595192909240723f,-0.0878061056137085f,-0.08074226975440979f,-3.5286545753479004f,-0.3104020655155182f,0.4041280448436737f}, |
Lightvalve | 240:bffaba802829 | 231 | {0.41842469573020935f,-0.2479589879512787f,0.4205797016620636f,-0.29916518926620483f,-0.7210080623626709f,-1.0200499296188354f,0.050872188061475754f,0.02579045481979847f,-4.840049743652344f,-0.7202125191688538f,-0.1097707450389862f,0.17138007283210754f,0.06840566545724869f,0.18428462743759155f,0.2775091230869293f,0.11071965098381042f}, |
Lightvalve | 240:bffaba802829 | 232 | {-0.09231144189834595f,0.208247572183609f,0.2369612753391266f,-0.1378183364868164f,-0.10159772634506226f,-0.09860122203826904f,0.39953842759132385f,-0.3619869351387024f,0.05624133348464966f,0.057282477617263794f,-0.09186843037605286f,-0.3802386224269867f,0.33520951867103577f,-0.3143694996833801f,0.07573774456977844f,0.1171349585056305f}, |
Lightvalve | 240:bffaba802829 | 233 | {-0.22812023758888245f,-2.06713604927063f,-0.39575254917144775f,-0.1444512903690338f,-0.36608341336250305f,-0.526474118232727f,-0.23619110882282257f,-0.08812745660543442f,4.559253692626953f,-0.39894798398017883f,2.622720956802368f,-0.11011743545532227f,0.1662178635597229f,-3.1915431022644043f,0.34983590245246887f,-0.1711532473564148f}, |
Lightvalve | 65:a2d7c63419c2 | 234 | }; |
Lightvalve | 65:a2d7c63419c2 | 235 | |
Lightvalve | 169:645207e160ca | 236 | const float h3[16][16] = { |
Lightvalve | 240:bffaba802829 | 237 | {-0.006790608167648315f,0.21018794178962708f,0.19160166382789612f,-0.09405046701431274f,-0.20767121016979218f,0.41050222516059875f,-0.17231479287147522f,-0.2754950523376465f,0.2988417446613312f,0.22258707880973816f,-0.4052674174308777f,-0.43136799335479736f,0.052298545837402344f,-0.16892734169960022f,0.3712175786495209f,-0.06832495331764221f}, |
Lightvalve | 240:bffaba802829 | 238 | {-0.03865167498588562f,-0.10611102730035782f,-0.33619338274002075f,0.32088032364845276f,0.17324191331863403f,-0.291770339012146f,0.06339046359062195f,-1.2900278568267822f,2.254979133605957f,-3.3876941204071045f,-0.44062796235084534f,-7.36557149887085f,-1.7634016275405884f,0.391641229391098f,-0.10375964641571045f,0.03748287260532379f}, |
Lightvalve | 240:bffaba802829 | 239 | {0.10098925232887268f,0.04721498489379883f,0.256857305765152f,0.3052078187465668f,0.03930395841598511f,0.3237522542476654f,-0.24151644110679626f,-0.3711833953857422f,-0.15678805112838745f,-0.255717396736145f,-0.17049190402030945f,0.3933774530887604f,0.3142060935497284f,0.07015183568000793f,-0.045980989933013916f,0.1601240336894989f}, |
Lightvalve | 240:bffaba802829 | 240 | {0.31332454085350037f,-0.312250554561615f,-0.13433364033699036f,0.32684752345085144f,-0.39805084466934204f,-0.059238433837890625f,0.2678447663784027f,-0.10679224133491516f,-0.30091023445129395f,-0.3048761487007141f,-0.037360578775405884f,0.31614676117897034f,0.023632794618606567f,-0.27491989731788635f,0.29395321011543274f,0.3450176417827606f}, |
Lightvalve | 240:bffaba802829 | 241 | {0.0049107372760772705f,-1.2520864009857178f,0.2015954554080963f,-0.277707040309906f,0.37562888860702515f,-0.39366021752357483f,0.10676625370979309f,-0.8682335019111633f,-0.27148571610450745f,0.4265214800834656f,0.7545055747032166f,-3.308232069015503f,0.8076616525650024f,-1.6762073040008545f,-2.6712536811828613f,-0.4618574380874634f}, |
Lightvalve | 240:bffaba802829 | 242 | {0.21470609307289124f,-1.7353445291519165f,-0.2561623454093933f,-0.30005985498428345f,0.11890976130962372f,0.21759197115898132f,-0.19221603870391846f,0.20264285802841187f,1.0542842149734497f,-1.4650459289550781f,0.5328110456466675f,-0.8438037633895874f,-0.16612306237220764f,-0.2636428475379944f,-0.6987592577934265f,0.014738291501998901f}, |
Lightvalve | 240:bffaba802829 | 243 | {-0.08789396286010742f,0.2853599190711975f,-0.32605651021003723f,-0.3530013859272003f,0.40897294878959656f,-0.43169721961021423f,0.3490438759326935f,-0.24153192341327667f,0.3819259703159332f,0.27927279472351074f,-0.41248786449432373f,0.10553863644599915f,0.04561987146735191f,-0.15471892058849335f,0.3892192542552948f,-0.17004820704460144f}, |
Lightvalve | 240:bffaba802829 | 244 | {-0.31195756793022156f,0.3535780608654022f,0.37471166253089905f,0.2081839144229889f,-0.39857012033462524f,0.1478196680545807f,0.39524081349372864f,-0.3342057168483734f,-0.17245899140834808f,0.3294520378112793f,-0.3792600631713867f,0.09461972117424011f,-0.1773693859577179f,-0.13848626613616943f,0.2583646774291992f,0.4144686162471771f}, |
Lightvalve | 240:bffaba802829 | 245 | {-0.145101398229599f,-3.2133281230926514f,0.08441874384880066f,0.2192457616329193f,-0.356086790561676f,0.09400740265846252f,-0.06423744559288025f,-0.4791902005672455f,-0.8166681528091431f,-0.33449169993400574f,0.8101168274879456f,-0.23406243324279785f,-0.31428906321525574f,-5.128968238830566f,-2.421018362045288f,-0.14175555109977722f}, |
Lightvalve | 240:bffaba802829 | 246 | {0.031499654054641724f,-0.3453395366668701f,-0.3276645541191101f,-0.3151174783706665f,-0.41578027606010437f,0.09287729859352112f,0.06111881136894226f,0.20733843743801117f,0.592309296131134f,-0.4272315502166748f,0.5247583389282227f,-0.21140308678150177f,-0.21721763908863068f,0.014157879166305065f,-0.42281362414360046f,-0.3252115845680237f}, |
Lightvalve | 240:bffaba802829 | 247 | {-0.3886617422103882f,2.009561061859131f,-0.15268433094024658f,-0.22167590260505676f,-0.3943225145339966f,-0.32775455713272095f,0.2432309091091156f,-0.21888288855552673f,-1.7100589275360107f,1.0955884456634521f,1.05848228931427f,0.24929127097129822f,3.7007298469543457f,0.41221246123313904f,-4.2899627685546875f,0.09496057778596878f}, |
Lightvalve | 240:bffaba802829 | 248 | {0.10831388831138611f,-0.42922312021255493f,0.4159962236881256f,-0.4228442311286926f,-0.17440855503082275f,-0.14694729447364807f,-0.08575206995010376f,-0.03497600555419922f,0.12500670552253723f,0.1220981776714325f,0.42109981179237366f,0.2404528558254242f,0.15480300784111023f,-0.046617358922958374f,0.1445848047733307f,0.208059161901474f}, |
Lightvalve | 240:bffaba802829 | 249 | {-0.16982737183570862f,-0.08366802334785461f,0.021271735429763794f,-0.23457923531532288f,0.3594605624675751f,0.12175920605659485f,0.196598082780838f,0.0048421225510537624f,0.455380380153656f,0.2666841447353363f,0.3527265787124634f,-0.36466607451438904f,-0.07807370275259018f,-0.024496499449014664f,0.24257664382457733f,-0.01678675413131714f}, |
Lightvalve | 240:bffaba802829 | 250 | {-0.4233904778957367f,-0.5184310674667358f,0.10355612635612488f,-0.17819368839263916f,0.2248094230890274f,0.25831249356269836f,0.3987707197666168f,-0.20740818977355957f,0.3185001015663147f,-0.39416059851646423f,-0.7869706153869629f,-1.8323378562927246f,-2.679171562194824f,-1.9532601833343506f,0.510806143283844f,0.1838276982307434f}, |
Lightvalve | 240:bffaba802829 | 251 | {0.4077700078487396f,-0.3607521951198578f,0.43178996443748474f,0.4325244724750519f,0.2711908519268036f,0.12385454773902893f,-0.39350762963294983f,0.3581089675426483f,0.4176264703273773f,-0.24286937713623047f,-0.2572516202926636f,0.3357771337032318f,-0.016610532999038696f,-0.244097501039505f,0.20021793246269226f,0.4071304500102997f}, |
Lightvalve | 240:bffaba802829 | 252 | {0.03745567798614502f,0.31836649775505066f,-0.05532115697860718f,0.028223276138305664f,0.117482990026474f,0.3893933594226837f,0.3383820354938507f,-0.0021976232528686523f,-0.23759111762046814f,-0.31924113631248474f,-0.2046361118555069f,-0.3622658848762512f,-0.0513286292552948f,-0.18417160212993622f,-0.3451819121837616f,-0.2967826724052429f}, |
Lightvalve | 66:a8e6799dbce3 | 253 | }; |
Lightvalve | 65:a2d7c63419c2 | 254 | |
Lightvalve | 240:bffaba802829 | 255 | const float hout[16] = { 0.5272732973098755f,0.29130640625953674f,-0.5444002151489258f,-0.3080372214317322f,-0.46838802099227905f,0.22877520322799683f,0.12085825204849243f,0.11469389498233795f,-0.3717977702617645f,0.4081546664237976f,-0.34571924805641174f,2.4782423973083496f,-0.49329257011413574f,-0.18705929815769196f,0.470443993806839f,-0.07612967491149902f }; |
Lightvalve | 240:bffaba802829 | 256 | |
Lightvalve | 240:bffaba802829 | 257 | const float b1[16] = { 0.6079384684562683f,0.7488086819648743f,0.2734024226665497f,0.6207621693611145f,-0.10124898701906204f,0.6065471768379211f,-1.3996264934539795f,-0.8490302562713623f,0.7256325483322144f,0.29943183064460754f,-0.413175493478775f,-0.18533024191856384f,1.0015991926193237f,1.0738469362258911f,-1.4473942518234253f,1.0611149072647095f }; |
Lightvalve | 240:bffaba802829 | 258 | |
Lightvalve | 240:bffaba802829 | 259 | const float b2[16] = { -0.5715829730033875f,0.024753810837864876f,-0.5197042226791382f,-0.9603403210639954f,1.2647051811218262f,-0.4483911395072937f,-0.10769303143024445f,-0.356192022562027f,0.09464974701404572f,-0.2368306964635849f,0.5349050760269165f,-0.33337149024009705f,-0.4760465621948242f,1.2915849685668945f,-0.41767919063568115f,-1.0683845281600952f }; |
Lightvalve | 240:bffaba802829 | 260 | |
Lightvalve | 240:bffaba802829 | 261 | const float b3[16] = { -2.198793888092041f,-0.39774423837661743f,-0.7633510828018188f,-1.8875172138214111f,-0.9303251504898071f,-0.5480363965034485f,-1.3439136743545532f,-0.1289718896150589f,0.781519889831543f,-0.049977123737335205f,0.3549143671989441f,-0.22415409982204437f,-2.2565112113952637f,1.0141292810440063f,1.966643214225769f,-0.3500630259513855f }; |
Lightvalve | 240:bffaba802829 | 262 | |
Lightvalve | 240:bffaba802829 | 263 | const float bout[1] = { 0.2728830575942993f }; |
Lightvalve | 225:278b48b86f27 | 264 | |
Lightvalve | 225:278b48b86f27 | 265 | float valve_ref_pos_buffer[10] = {0.0f}; |
Lightvalve | 65:a2d7c63419c2 | 266 | |
Lightvalve | 170:42c938a40313 | 267 | /////////////////////////////////////////////////////////////////////////////////////////////RL |
Lightvalve | 170:42c938a40313 | 268 | float input_RL[num_input_RL] = { 0.0f }; |
Lightvalve | 170:42c938a40313 | 269 | |
Lightvalve | 170:42c938a40313 | 270 | //Critic Networks |
Lightvalve | 173:68c7914679ec | 271 | float hc1[num_input_RL][num_hidden_unit1] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 272 | float bc1[num_hidden_unit1] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 273 | float hc2[num_hidden_unit1][num_hidden_unit2] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 274 | float bc2[num_hidden_unit2] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 275 | float hc3[num_hidden_unit2] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 276 | float bc3 = 0.0f; |
Lightvalve | 170:42c938a40313 | 277 | |
Lightvalve | 170:42c938a40313 | 278 | //Critic Networks Temporary |
Lightvalve | 173:68c7914679ec | 279 | float hc1_temp[num_input_RL][num_hidden_unit1] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 280 | float bc1_temp[num_hidden_unit1] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 281 | float hc2_temp[num_hidden_unit1][num_hidden_unit2] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 282 | float bc2_temp[num_hidden_unit2] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 283 | float hc3_temp[num_hidden_unit2] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 284 | float bc3_temp = 0.0f; |
Lightvalve | 170:42c938a40313 | 285 | |
Lightvalve | 170:42c938a40313 | 286 | //Actor Networks |
Lightvalve | 173:68c7914679ec | 287 | float ha1[num_input_RL][num_hidden_unit1] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 288 | float ba1[num_hidden_unit1] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 289 | float ha2[num_hidden_unit1][num_hidden_unit2] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 290 | float ba2[num_hidden_unit2] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 291 | float ha3[num_hidden_unit2][2] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 292 | float ba3[2] = {0.0f}; |
Lightvalve | 170:42c938a40313 | 293 | |
Lightvalve | 170:42c938a40313 | 294 | //Actor Networks Temporary |
Lightvalve | 173:68c7914679ec | 295 | float ha1_temp[num_input_RL][num_hidden_unit1] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 296 | float ba1_temp[num_hidden_unit1] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 297 | float ha2_temp[num_hidden_unit1][num_hidden_unit2] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 298 | float ba2_temp[num_hidden_unit2] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 299 | float ha3_temp[num_hidden_unit2][2] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 300 | float ba3_temp[2] = {0.0f}; |
Lightvalve | 66:a8e6799dbce3 | 301 | |
Lightvalve | 87:471334725012 | 302 | float VALVE_POS_RAW_NN = 0.0f; |
Lightvalve | 87:471334725012 | 303 | float DDV_JOINT_POS_FF(float REF_JOINT_VEL); |
Lightvalve | 61:bc8c8270f0ab | 304 | |
Lightvalve | 179:d5377766d7ea | 305 | /////////////////////////////////////////////RL tuning |
Lightvalve | 179:d5377766d7ea | 306 | float Gradient_Limit = 0.5f; |
Lightvalve | 179:d5377766d7ea | 307 | float gradient_rate_actor = 0.001f; |
Lightvalve | 179:d5377766d7ea | 308 | float gradient_rate_critic = 0.001f; |
Lightvalve | 179:d5377766d7ea | 309 | ////////////////////////////////////////////////////////////////////////////// |
Lightvalve | 170:42c938a40313 | 310 | |
Lightvalve | 170:42c938a40313 | 311 | float Critic_Network_Temp(float *arr) |
Lightvalve | 170:42c938a40313 | 312 | { |
Lightvalve | 173:68c7914679ec | 313 | float output1[num_hidden_unit1] = { 0.0f }; |
Lightvalve | 173:68c7914679ec | 314 | float output2[num_hidden_unit2] = { 0.0f }; |
Lightvalve | 170:42c938a40313 | 315 | float output = 0.0f; |
Lightvalve | 173:68c7914679ec | 316 | for (int index2 = 0; index2 < num_hidden_unit1; index2++) { |
Lightvalve | 170:42c938a40313 | 317 | for (int index1 = 0; index1 < num_input_RL; index1++) { |
Lightvalve | 170:42c938a40313 | 318 | output1[index2] = output1[index2] + hc1_temp[index1][index2] * arr[index1]; |
Lightvalve | 170:42c938a40313 | 319 | } |
Lightvalve | 173:68c7914679ec | 320 | //ReLU |
Lightvalve | 173:68c7914679ec | 321 | output1[index2] = output1[index2] + bc1_temp[index2]; |
Lightvalve | 173:68c7914679ec | 322 | hx_c_sum[index2] = output1[index2]; |
Lightvalve | 173:68c7914679ec | 323 | if (output1[index2] < 0) { |
Lightvalve | 173:68c7914679ec | 324 | output1[index2] = 0; |
Lightvalve | 173:68c7914679ec | 325 | } |
Lightvalve | 173:68c7914679ec | 326 | //tanh |
Lightvalve | 173:68c7914679ec | 327 | //output1[index2] = tanh(output1[index2] + bc1_temp[index2]); |
Lightvalve | 173:68c7914679ec | 328 | } |
Lightvalve | 173:68c7914679ec | 329 | for (int index2 = 0; index2 < num_hidden_unit2; index2++) { |
Lightvalve | 173:68c7914679ec | 330 | for (int index1 = 0; index1 < num_hidden_unit1; index1++) { |
Lightvalve | 174:c828479f53f9 | 331 | output2[index2] = output2[index2] + hc2_temp[index1][index2] * output1[index1]; |
Lightvalve | 173:68c7914679ec | 332 | } |
Lightvalve | 173:68c7914679ec | 333 | //ReLU |
Lightvalve | 173:68c7914679ec | 334 | output2[index2] = output2[index2] + bc2_temp[index2]; |
Lightvalve | 173:68c7914679ec | 335 | hxh_c_sum[index2] = output2[index2]; |
Lightvalve | 173:68c7914679ec | 336 | if (output2[index2] < 0) { |
Lightvalve | 173:68c7914679ec | 337 | output2[index2] = 0; |
Lightvalve | 173:68c7914679ec | 338 | } |
Lightvalve | 173:68c7914679ec | 339 | //tanh |
Lightvalve | 173:68c7914679ec | 340 | //output2[index2] = tanh(output2[index2] + bc2_temp[index2]); |
Lightvalve | 170:42c938a40313 | 341 | } |
Lightvalve | 170:42c938a40313 | 342 | for (int index2 = 0; index2 < 1; index2++) { |
Lightvalve | 173:68c7914679ec | 343 | for (int index1 = 0; index1 < num_hidden_unit2; index1++) { |
Lightvalve | 173:68c7914679ec | 344 | output = output + hc3_temp[index1] * output2[index1]; |
Lightvalve | 170:42c938a40313 | 345 | } |
Lightvalve | 173:68c7914679ec | 346 | output = output + bc3_temp; |
Lightvalve | 173:68c7914679ec | 347 | hxhh_c_sum = output; |
Lightvalve | 170:42c938a40313 | 348 | } |
Lightvalve | 170:42c938a40313 | 349 | return output; |
Lightvalve | 170:42c938a40313 | 350 | } |
Lightvalve | 170:42c938a40313 | 351 | |
Lightvalve | 170:42c938a40313 | 352 | |
Lightvalve | 170:42c938a40313 | 353 | void Actor_Network(float *arr) |
Lightvalve | 170:42c938a40313 | 354 | { |
Lightvalve | 173:68c7914679ec | 355 | float output1[num_hidden_unit1] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 356 | float output2[num_hidden_unit2] = {0.0f}; |
Lightvalve | 170:42c938a40313 | 357 | float output[2] = {0.0f}; |
Lightvalve | 170:42c938a40313 | 358 | |
Lightvalve | 173:68c7914679ec | 359 | for (int index2 = 0; index2 < num_hidden_unit1; index2++) { |
Lightvalve | 173:68c7914679ec | 360 | for (int index1 = 0; index1 < num_input_RL; index1++) { |
Lightvalve | 178:1074553d2f6f | 361 | output1[index2] = output1[index2] + ha1_temp[index1][index2] * arr[index1]; |
Lightvalve | 173:68c7914679ec | 362 | } |
Lightvalve | 178:1074553d2f6f | 363 | output1[index2] = output1[index2] + ba1_temp[index2]; |
Lightvalve | 173:68c7914679ec | 364 | hx_a_sum[index2] = output1[index2]; |
Lightvalve | 173:68c7914679ec | 365 | if (output1[index2] < 0) { |
Lightvalve | 173:68c7914679ec | 366 | output1[index2] = 0; |
Lightvalve | 173:68c7914679ec | 367 | } |
Lightvalve | 173:68c7914679ec | 368 | } |
Lightvalve | 173:68c7914679ec | 369 | for (int index2 = 0; index2 < num_hidden_unit2; index2++) { |
Lightvalve | 173:68c7914679ec | 370 | for (int index1 = 0; index1 < num_hidden_unit1; index1++) { |
Lightvalve | 178:1074553d2f6f | 371 | output2[index2] = output2[index2] + ha2_temp[index1][index2] * output1[index1]; |
Lightvalve | 173:68c7914679ec | 372 | } |
Lightvalve | 178:1074553d2f6f | 373 | output2[index2] = output2[index2] + ba2_temp[index2]; |
Lightvalve | 173:68c7914679ec | 374 | hxh_a_sum[index2] = output2[index2]; |
Lightvalve | 173:68c7914679ec | 375 | if (output2[index2] < 0) { |
Lightvalve | 173:68c7914679ec | 376 | output2[index2] = 0; |
Lightvalve | 173:68c7914679ec | 377 | } |
Lightvalve | 173:68c7914679ec | 378 | } |
Lightvalve | 173:68c7914679ec | 379 | for (int index2 = 0; index2 < 2; index2++) { |
Lightvalve | 173:68c7914679ec | 380 | for (int index1 = 0; index1 < num_hidden_unit2; index1++) { |
Lightvalve | 178:1074553d2f6f | 381 | output[index2] = output[index2] + ha3_temp[index1][index2] * output2[index1]; |
Lightvalve | 173:68c7914679ec | 382 | } |
Lightvalve | 178:1074553d2f6f | 383 | hxhh_a_sum[index2] = output[index2] + ba3_temp[index2]; |
Lightvalve | 173:68c7914679ec | 384 | } |
Lightvalve | 178:1074553d2f6f | 385 | |
Lightvalve | 178:1074553d2f6f | 386 | mean_before_SP = output[0] + ba3_temp[0]; //SP = softplus |
Lightvalve | 178:1074553d2f6f | 387 | deviation_before_SP = output[1] + ba3_temp[1]; |
Lightvalve | 173:68c7914679ec | 388 | //Softplus |
Lightvalve | 173:68c7914679ec | 389 | mean = log(1.0f+exp(mean_before_SP)); |
Lightvalve | 173:68c7914679ec | 390 | deviation = log(1.0f+exp(deviation_before_SP)); |
Lightvalve | 179:d5377766d7ea | 391 | logging2 = mean; |
Lightvalve | 179:d5377766d7ea | 392 | logging4 = deviation; |
Lightvalve | 173:68c7914679ec | 393 | } |
Lightvalve | 173:68c7914679ec | 394 | |
Lightvalve | 173:68c7914679ec | 395 | |
Lightvalve | 173:68c7914679ec | 396 | void Actor_Network_Old(float *arr) |
Lightvalve | 173:68c7914679ec | 397 | { |
Lightvalve | 173:68c7914679ec | 398 | float output1[num_hidden_unit1] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 399 | float output2[num_hidden_unit2] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 400 | float output[2] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 401 | |
Lightvalve | 173:68c7914679ec | 402 | for (int index2 = 0; index2 < num_hidden_unit1; index2++) { |
Lightvalve | 170:42c938a40313 | 403 | for (int index1 = 0; index1 < num_input_RL; index1++) { |
Lightvalve | 170:42c938a40313 | 404 | output1[index2] = output1[index2] + ha1[index1][index2] * arr[index1]; |
Lightvalve | 170:42c938a40313 | 405 | } |
Lightvalve | 170:42c938a40313 | 406 | output1[index2] = output1[index2] + ba1[index2]; |
Lightvalve | 170:42c938a40313 | 407 | if (output1[index2] < 0) { |
Lightvalve | 170:42c938a40313 | 408 | output1[index2] = 0; |
Lightvalve | 170:42c938a40313 | 409 | } |
Lightvalve | 170:42c938a40313 | 410 | } |
Lightvalve | 173:68c7914679ec | 411 | for (int index2 = 0; index2 < num_hidden_unit2; index2++) { |
Lightvalve | 173:68c7914679ec | 412 | for (int index1 = 0; index1 < num_hidden_unit1; index1++) { |
Lightvalve | 174:c828479f53f9 | 413 | output2[index2] = output2[index2] + ha2[index1][index2] * output1[index1]; |
Lightvalve | 170:42c938a40313 | 414 | } |
Lightvalve | 173:68c7914679ec | 415 | output2[index2] = output2[index2] + ba2[index2]; |
Lightvalve | 173:68c7914679ec | 416 | if (output2[index2] < 0) { |
Lightvalve | 173:68c7914679ec | 417 | output2[index2] = 0; |
Lightvalve | 170:42c938a40313 | 418 | } |
Lightvalve | 170:42c938a40313 | 419 | } |
Lightvalve | 170:42c938a40313 | 420 | for (int index2 = 0; index2 < 2; index2++) { |
Lightvalve | 173:68c7914679ec | 421 | for (int index1 = 0; index1 < num_hidden_unit2; index1++) { |
Lightvalve | 173:68c7914679ec | 422 | output[index2] = output[index2] + ha3[index1][index2] * output2[index1]; |
Lightvalve | 170:42c938a40313 | 423 | } |
Lightvalve | 170:42c938a40313 | 424 | } |
Lightvalve | 173:68c7914679ec | 425 | mean_old = output[0] + ba3[0]; |
Lightvalve | 173:68c7914679ec | 426 | deviation_old = output[1] + ba3[1]; |
Lightvalve | 173:68c7914679ec | 427 | //Softplus |
Lightvalve | 173:68c7914679ec | 428 | mean_old = log(1.0f+exp(mean_old)); |
Lightvalve | 173:68c7914679ec | 429 | deviation_old = log(1.0f+exp(deviation_old)); |
Lightvalve | 170:42c938a40313 | 430 | } |
Lightvalve | 170:42c938a40313 | 431 | |
Lightvalve | 170:42c938a40313 | 432 | float Grad_Normal_Dist_Mean(float mean, float deviation, float action) |
Lightvalve | 170:42c938a40313 | 433 | { |
Lightvalve | 170:42c938a40313 | 434 | float grad_mean = 0.0f; |
Lightvalve | 170:42c938a40313 | 435 | grad_mean = (action-mean)*exp(-(action-mean)*(action-mean)/(2.0f*deviation*deviation))/(sqrt(2.0f*PI)*deviation*deviation*deviation); |
Lightvalve | 170:42c938a40313 | 436 | return grad_mean; |
Lightvalve | 170:42c938a40313 | 437 | } |
Lightvalve | 170:42c938a40313 | 438 | |
Lightvalve | 170:42c938a40313 | 439 | float Grad_Normal_Dist_Deviation(float mean, float deviation, float action) |
Lightvalve | 170:42c938a40313 | 440 | { |
Lightvalve | 170:42c938a40313 | 441 | float grad_dev = 0.0f; |
Lightvalve | 170:42c938a40313 | 442 | 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 | 443 | return grad_dev; |
Lightvalve | 170:42c938a40313 | 444 | } |
Lightvalve | 170:42c938a40313 | 445 | |
Lightvalve | 173:68c7914679ec | 446 | float ReLU(float x) |
Lightvalve | 173:68c7914679ec | 447 | { |
Lightvalve | 173:68c7914679ec | 448 | if (x >= 0) { |
Lightvalve | 173:68c7914679ec | 449 | return x; |
Lightvalve | 173:68c7914679ec | 450 | } else { |
Lightvalve | 173:68c7914679ec | 451 | return 0.0f; |
Lightvalve | 173:68c7914679ec | 452 | } |
Lightvalve | 173:68c7914679ec | 453 | } |
Lightvalve | 173:68c7914679ec | 454 | |
Lightvalve | 170:42c938a40313 | 455 | void update_Critic_Networks(float (*arr)[num_input_RL]) |
Lightvalve | 170:42c938a40313 | 456 | { |
Lightvalve | 173:68c7914679ec | 457 | float G_hc1[num_input_RL][num_hidden_unit1] = {0.0f}; |
Lightvalve | 177:8e9cf31d63f4 | 458 | float G_bc1[num_hidden_unit1] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 459 | for (int index2 = 0; index2 < num_hidden_unit1; index2++) { |
Lightvalve | 170:42c938a40313 | 460 | for (int index1 = 0; index1 < num_input_RL; index1++) { |
Lightvalve | 173:68c7914679ec | 461 | for (int n=0; n<batch_size; n++) { |
Lightvalve | 177:8e9cf31d63f4 | 462 | float d_V_d_hc1 = 0.0f; |
Lightvalve | 173:68c7914679ec | 463 | for(int k=0; k<num_hidden_unit2; k++) { |
Lightvalve | 173:68c7914679ec | 464 | if (hxh_c_sum_array[n][k] >= 0) { |
Lightvalve | 173:68c7914679ec | 465 | if (hx_c_sum_array[n][index2] > 0) { |
Lightvalve | 177:8e9cf31d63f4 | 466 | d_V_d_hc1 = d_V_d_hc1 + arr[n][index1]*hc2_temp[index2][k]*hc3_temp[k]; |
Lightvalve | 173:68c7914679ec | 467 | } |
Lightvalve | 173:68c7914679ec | 468 | } |
Lightvalve | 170:42c938a40313 | 469 | } |
Lightvalve | 177:8e9cf31d63f4 | 470 | G_hc1[index1][index2] = G_hc1[index1][index2] + 2.0f*(return_G[n]-V[n])*(-d_V_d_hc1); |
Lightvalve | 170:42c938a40313 | 471 | } |
Lightvalve | 170:42c938a40313 | 472 | G_hc1[index1][index2] = G_hc1[index1][index2] / batch_size; |
Lightvalve | 179:d5377766d7ea | 473 | if(G_hc1[index1][index2] > Gradient_Limit) G_hc1[index1][index2] = Gradient_Limit; |
Lightvalve | 179:d5377766d7ea | 474 | else if (G_hc1[index1][index2] < -Gradient_Limit) G_hc1[index1][index2] = -Gradient_Limit; |
Lightvalve | 179:d5377766d7ea | 475 | //hc1_temp[index1][index2] = hc1_temp[index1][index2] - gradient_rate_critic * G_hc1[index1][index2]; |
Lightvalve | 173:68c7914679ec | 476 | } |
Lightvalve | 173:68c7914679ec | 477 | for (int n=0; n<batch_size; n++) { |
Lightvalve | 177:8e9cf31d63f4 | 478 | float d_V_d_bc1 = 0.0f; |
Lightvalve | 173:68c7914679ec | 479 | for(int k=0; k<num_hidden_unit2; k++) { |
Lightvalve | 173:68c7914679ec | 480 | if (hxh_c_sum_array[n][k] >= 0) { |
Lightvalve | 173:68c7914679ec | 481 | if (hx_c_sum_array[n][index2] > 0) { |
Lightvalve | 177:8e9cf31d63f4 | 482 | d_V_d_bc1 = d_V_d_bc1 + hc2_temp[index2][k]*hc3_temp[k]; |
Lightvalve | 173:68c7914679ec | 483 | } |
Lightvalve | 173:68c7914679ec | 484 | } |
Lightvalve | 173:68c7914679ec | 485 | } |
Lightvalve | 177:8e9cf31d63f4 | 486 | G_bc1[index2] = G_bc1[index2] + 2.0f*(return_G[n]-V[n])*(-d_V_d_bc1); |
Lightvalve | 173:68c7914679ec | 487 | } |
Lightvalve | 173:68c7914679ec | 488 | G_bc1[index2] = G_bc1[index2] / batch_size; |
Lightvalve | 179:d5377766d7ea | 489 | if(G_bc1[index2] > Gradient_Limit) G_bc1[index2] = Gradient_Limit; |
Lightvalve | 179:d5377766d7ea | 490 | else if (G_bc1[index2] < -Gradient_Limit) G_bc1[index2] = -Gradient_Limit; |
Lightvalve | 179:d5377766d7ea | 491 | //bc1_temp[index2] = bc1_temp[index2] - gradient_rate_critic * G_bc1[index2]; |
Lightvalve | 173:68c7914679ec | 492 | } |
Lightvalve | 173:68c7914679ec | 493 | |
Lightvalve | 175:2f7289dbd488 | 494 | |
Lightvalve | 173:68c7914679ec | 495 | float G_hc2[num_hidden_unit1][num_hidden_unit2] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 496 | float G_bc2[num_hidden_unit2] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 497 | for (int index2 = 0; index2 < num_hidden_unit2; index2++) { |
Lightvalve | 173:68c7914679ec | 498 | for (int index1 = 0; index1 < num_hidden_unit1; index1++) { |
Lightvalve | 173:68c7914679ec | 499 | for (int n=0; n<batch_size; n++) { |
Lightvalve | 177:8e9cf31d63f4 | 500 | float d_V_d_hc2 = 0.0f; |
Lightvalve | 173:68c7914679ec | 501 | if (hxh_c_sum_array[n][index2] >= 0) { |
Lightvalve | 173:68c7914679ec | 502 | if (hx_c_sum_array[n][index1] > 0) { |
Lightvalve | 177:8e9cf31d63f4 | 503 | d_V_d_hc2 = hx_c_sum_array[n][index1]*hc3_temp[index2]; |
Lightvalve | 173:68c7914679ec | 504 | } |
Lightvalve | 173:68c7914679ec | 505 | } |
Lightvalve | 177:8e9cf31d63f4 | 506 | G_hc2[index1][index2] = G_hc2[index1][index2] + 2.0f*(return_G[n]-V[n])*(-d_V_d_hc2); |
Lightvalve | 173:68c7914679ec | 507 | } |
Lightvalve | 173:68c7914679ec | 508 | G_hc2[index1][index2] = G_hc2[index1][index2] / batch_size; |
Lightvalve | 179:d5377766d7ea | 509 | if(G_hc2[index1][index2] > Gradient_Limit) G_hc2[index1][index2] = Gradient_Limit; |
Lightvalve | 179:d5377766d7ea | 510 | else if (G_hc2[index1][index2] < -Gradient_Limit) G_hc2[index1][index2] = -Gradient_Limit; |
Lightvalve | 179:d5377766d7ea | 511 | //hc2_temp[index1][index2] = hc2_temp[index1][index2] - gradient_rate_critic * G_hc2[index1][index2]; |
Lightvalve | 173:68c7914679ec | 512 | } |
Lightvalve | 173:68c7914679ec | 513 | for (int n=0; n<batch_size; n++) { |
Lightvalve | 177:8e9cf31d63f4 | 514 | float d_V_d_bc2 = 0.0f; |
Lightvalve | 173:68c7914679ec | 515 | if (hxh_c_sum_array[n][index2] >= 0) { |
Lightvalve | 177:8e9cf31d63f4 | 516 | d_V_d_bc2 = hc3_temp[index2]; |
Lightvalve | 173:68c7914679ec | 517 | } |
Lightvalve | 177:8e9cf31d63f4 | 518 | G_bc2[index2] = G_bc2[index2] + 2.0f*(return_G[n]-V[n])*(-d_V_d_bc2); |
Lightvalve | 173:68c7914679ec | 519 | } |
Lightvalve | 173:68c7914679ec | 520 | G_bc2[index2] = G_bc2[index2] / batch_size; |
Lightvalve | 179:d5377766d7ea | 521 | if(G_bc2[index2] > Gradient_Limit) G_bc2[index2] = Gradient_Limit; |
Lightvalve | 179:d5377766d7ea | 522 | else if (G_bc2[index2] < -Gradient_Limit) G_bc2[index2] = -Gradient_Limit; |
Lightvalve | 179:d5377766d7ea | 523 | //bc2_temp[index2] = bc2_temp[index2] - gradient_rate_critic * G_bc2[index2]; |
Lightvalve | 173:68c7914679ec | 524 | } |
Lightvalve | 173:68c7914679ec | 525 | |
Lightvalve | 173:68c7914679ec | 526 | float G_hc3[num_hidden_unit2]= {0.0f}; |
Lightvalve | 173:68c7914679ec | 527 | float G_bc3 = 0.0f; |
Lightvalve | 173:68c7914679ec | 528 | for (int index2 = 0; index2 < 1; index2++) { |
Lightvalve | 173:68c7914679ec | 529 | for (int index1 = 0; index1 < num_hidden_unit2; index1++) { |
Lightvalve | 173:68c7914679ec | 530 | for (int n=0; n<batch_size; n++) { |
Lightvalve | 177:8e9cf31d63f4 | 531 | float d_V_d_hc3 = 0.0f; |
Lightvalve | 173:68c7914679ec | 532 | if (hxh_c_sum_array[n][index1] >= 0) { |
Lightvalve | 177:8e9cf31d63f4 | 533 | d_V_d_hc3 = d_V_d_hc3 + hxh_c_sum_array[n][index1]; |
Lightvalve | 173:68c7914679ec | 534 | } |
Lightvalve | 177:8e9cf31d63f4 | 535 | G_hc3[index1] = G_hc3[index1] + 2.0f*(return_G[n]-V[n])*(-d_V_d_hc3); |
Lightvalve | 173:68c7914679ec | 536 | } |
Lightvalve | 173:68c7914679ec | 537 | G_hc3[index1] = G_hc3[index1] / batch_size; |
Lightvalve | 179:d5377766d7ea | 538 | if(G_hc3[index1] > Gradient_Limit) G_hc3[index1] = Gradient_Limit; |
Lightvalve | 179:d5377766d7ea | 539 | else if (G_hc3[index1] < -Gradient_Limit) G_hc3[index1] = -Gradient_Limit; |
Lightvalve | 179:d5377766d7ea | 540 | //hc3_temp[index1] = hc3_temp[index1] - gradient_rate_critic * G_hc3[index1]; |
Lightvalve | 173:68c7914679ec | 541 | } |
Lightvalve | 173:68c7914679ec | 542 | for (int n=0; n<batch_size; n++) { |
Lightvalve | 177:8e9cf31d63f4 | 543 | float d_V_d_bc3 = 0.0f; |
Lightvalve | 177:8e9cf31d63f4 | 544 | d_V_d_bc3 = 1.0f; |
Lightvalve | 177:8e9cf31d63f4 | 545 | G_bc3 = G_bc3 + 2.0f*(return_G[n]-V[n])*(-d_V_d_bc3); |
Lightvalve | 173:68c7914679ec | 546 | } |
Lightvalve | 173:68c7914679ec | 547 | G_bc3 = G_bc3 / batch_size; |
Lightvalve | 179:d5377766d7ea | 548 | if(G_bc3 > Gradient_Limit) G_bc3 = Gradient_Limit; |
Lightvalve | 179:d5377766d7ea | 549 | else if (G_bc3 < -Gradient_Limit) G_bc3 = -Gradient_Limit; |
Lightvalve | 179:d5377766d7ea | 550 | //bc3_temp = bc3_temp - gradient_rate_critic * G_bc3; |
Lightvalve | 173:68c7914679ec | 551 | } |
Lightvalve | 173:68c7914679ec | 552 | |
Lightvalve | 173:68c7914679ec | 553 | // Simultaneous Update |
Lightvalve | 173:68c7914679ec | 554 | for (int index2 = 0; index2 < num_hidden_unit1; index2++) { |
Lightvalve | 173:68c7914679ec | 555 | for (int index1 = 0; index1 < num_input_RL; index1++) { |
Lightvalve | 179:d5377766d7ea | 556 | hc1_temp[index1][index2] = hc1_temp[index1][index2] - gradient_rate_critic * G_hc1[index1][index2]; |
Lightvalve | 170:42c938a40313 | 557 | } |
Lightvalve | 179:d5377766d7ea | 558 | bc1_temp[index2] = bc1_temp[index2] - gradient_rate_critic * G_bc1[index2]; |
Lightvalve | 170:42c938a40313 | 559 | } |
Lightvalve | 173:68c7914679ec | 560 | for (int index2 = 0; index2 < num_hidden_unit2; index2++) { |
Lightvalve | 173:68c7914679ec | 561 | for (int index1 = 0; index1 < num_hidden_unit1; index1++) { |
Lightvalve | 179:d5377766d7ea | 562 | hc2_temp[index1][index2] = hc2_temp[index1][index2] - gradient_rate_critic * G_hc2[index1][index2]; |
Lightvalve | 173:68c7914679ec | 563 | } |
Lightvalve | 179:d5377766d7ea | 564 | bc2_temp[index2] = bc2_temp[index2] - gradient_rate_critic * G_bc2[index2]; |
Lightvalve | 173:68c7914679ec | 565 | } |
Lightvalve | 170:42c938a40313 | 566 | for (int index2 = 0; index2 < 1; index2++) { |
Lightvalve | 173:68c7914679ec | 567 | for (int index1 = 0; index1 < num_hidden_unit2; index1++) { |
Lightvalve | 179:d5377766d7ea | 568 | hc3_temp[index1] = hc3_temp[index1] - gradient_rate_critic * G_hc3[index1]; |
Lightvalve | 170:42c938a40313 | 569 | } |
Lightvalve | 179:d5377766d7ea | 570 | bc3_temp = bc3_temp - gradient_rate_critic * G_bc3; |
Lightvalve | 170:42c938a40313 | 571 | } |
Lightvalve | 170:42c938a40313 | 572 | } |
Lightvalve | 170:42c938a40313 | 573 | |
Lightvalve | 173:68c7914679ec | 574 | ///////////////////////////Softplus////////////////////////////////// |
Lightvalve | 170:42c938a40313 | 575 | void update_Actor_Networks(float (*arr)[num_input_RL]) |
Lightvalve | 170:42c938a40313 | 576 | { |
Lightvalve | 218:066030f7951f | 577 | |
Lightvalve | 173:68c7914679ec | 578 | |
Lightvalve | 173:68c7914679ec | 579 | float G_ha1[num_input_RL][num_hidden_unit1] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 580 | float G_ba1[num_hidden_unit1] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 581 | |
Lightvalve | 173:68c7914679ec | 582 | for (int index2 = 0; index2 < num_hidden_unit1; index2++) { |
Lightvalve | 170:42c938a40313 | 583 | for (int index1 = 0; index1 < num_input_RL; index1++) { |
Lightvalve | 173:68c7914679ec | 584 | for (int n=0; n<batch_size; n++) { |
Lightvalve | 177:8e9cf31d63f4 | 585 | float d_x_d_ha1 = 0.0f; |
Lightvalve | 177:8e9cf31d63f4 | 586 | float d_y_d_ha1 = 0.0f; |
Lightvalve | 173:68c7914679ec | 587 | if((advantage[n] >= 0.0f && ratio[n] >= 1.0f + epsilon) || (advantage[n] < 0.0f && ratio[n] < 1.0f - epsilon)) { |
Lightvalve | 170:42c938a40313 | 588 | G_ha1[index1][index2] = G_ha1[index1][index2]; |
Lightvalve | 170:42c938a40313 | 589 | } else { |
Lightvalve | 173:68c7914679ec | 590 | for(int k=0; k<num_hidden_unit2; k++) { |
Lightvalve | 173:68c7914679ec | 591 | if (hxh_a_sum_array[n][k] >= 0) { |
Lightvalve | 173:68c7914679ec | 592 | if (hx_a_sum_array[n][index2] > 0) { |
Lightvalve | 177:8e9cf31d63f4 | 593 | d_x_d_ha1 = d_x_d_ha1 + arr[n][index1]*ha2_temp[index2][k]*ha3_temp[k][0]; |
Lightvalve | 177:8e9cf31d63f4 | 594 | d_y_d_ha1 = d_y_d_ha1 + arr[n][index1]*ha2_temp[index2][k]*ha3_temp[k][1]; |
Lightvalve | 173:68c7914679ec | 595 | } |
Lightvalve | 171:bfc1fd2629d8 | 596 | } |
Lightvalve | 170:42c938a40313 | 597 | } |
Lightvalve | 170:42c938a40313 | 598 | float d_mean_d_ha1 = 0.0f; |
Lightvalve | 170:42c938a40313 | 599 | float d_dev_d_ha1 = 0.0f; |
Lightvalve | 177:8e9cf31d63f4 | 600 | 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 | 601 | 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 | 602 | |
Lightvalve | 173:68c7914679ec | 603 | 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 | 604 | } |
Lightvalve | 170:42c938a40313 | 605 | } |
Lightvalve | 176:589ea3edcf3c | 606 | G_ha1[index1][index2] = -G_ha1[index1][index2] / batch_size; |
Lightvalve | 179:d5377766d7ea | 607 | if(G_ha1[index1][index2] > Gradient_Limit) G_ha1[index1][index2] = Gradient_Limit; |
Lightvalve | 179:d5377766d7ea | 608 | else if (G_ha1[index1][index2] < -Gradient_Limit) G_ha1[index1][index2] = -Gradient_Limit; |
Lightvalve | 179:d5377766d7ea | 609 | //ha1_temp[index1][index2] = ha1_temp[index1][index2] - gradient_rate_actor * G_ha1[index1][index2]; |
Lightvalve | 170:42c938a40313 | 610 | } |
Lightvalve | 173:68c7914679ec | 611 | |
Lightvalve | 173:68c7914679ec | 612 | for (int n=0; n<batch_size; n++) { |
Lightvalve | 177:8e9cf31d63f4 | 613 | float d_x_d_ba1 = 0.0f; |
Lightvalve | 177:8e9cf31d63f4 | 614 | float d_y_d_ba1 = 0.0f; |
Lightvalve | 173:68c7914679ec | 615 | if((advantage[n] >= 0.0f && ratio[n] >= 1.0f + epsilon) || (advantage[n] < 0.0f && ratio[n] < 1.0f - epsilon)) { |
Lightvalve | 170:42c938a40313 | 616 | G_ba1[index2] = G_ba1[index2]; |
Lightvalve | 170:42c938a40313 | 617 | } else { |
Lightvalve | 173:68c7914679ec | 618 | for(int k=0; k<num_hidden_unit2; k++) { |
Lightvalve | 173:68c7914679ec | 619 | if (hxh_a_sum_array[n][k] >= 0) { |
Lightvalve | 173:68c7914679ec | 620 | if (hx_a_sum_array[n][index2] > 0) { |
Lightvalve | 177:8e9cf31d63f4 | 621 | d_x_d_ba1 = d_x_d_ba1 + ha2_temp[index2][k]*ha3_temp[k][0]; |
Lightvalve | 177:8e9cf31d63f4 | 622 | d_y_d_ba1 = d_y_d_ba1 + ha2_temp[index2][k]*ha3_temp[k][1]; |
Lightvalve | 173:68c7914679ec | 623 | } |
Lightvalve | 171:bfc1fd2629d8 | 624 | } |
Lightvalve | 170:42c938a40313 | 625 | } |
Lightvalve | 170:42c938a40313 | 626 | float d_mean_d_ba1 = 0.0f; |
Lightvalve | 170:42c938a40313 | 627 | float d_dev_d_ba1 = 0.0f; |
Lightvalve | 177:8e9cf31d63f4 | 628 | 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 | 629 | 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 | 630 | |
Lightvalve | 173:68c7914679ec | 631 | 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 | 632 | } |
Lightvalve | 170:42c938a40313 | 633 | } |
Lightvalve | 176:589ea3edcf3c | 634 | G_ba1[index2] = -G_ba1[index2] / batch_size; |
Lightvalve | 179:d5377766d7ea | 635 | if(G_ba1[index2] > Gradient_Limit) G_ba1[index2] = Gradient_Limit; |
Lightvalve | 179:d5377766d7ea | 636 | else if (G_ba1[index2] < -Gradient_Limit) G_ba1[index2] = -Gradient_Limit; |
Lightvalve | 179:d5377766d7ea | 637 | //ba1_temp[index2] = ba1_temp[index2] - gradient_rate_actor * G_ba1[index2]; |
Lightvalve | 170:42c938a40313 | 638 | } |
Lightvalve | 170:42c938a40313 | 639 | |
Lightvalve | 173:68c7914679ec | 640 | float G_ha2[num_hidden_unit1][num_hidden_unit2] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 641 | float G_ba2[num_hidden_unit2] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 642 | |
Lightvalve | 173:68c7914679ec | 643 | for (int index2 = 0; index2 < num_hidden_unit2; index2++) { |
Lightvalve | 173:68c7914679ec | 644 | for (int index1 = 0; index1 < num_hidden_unit1; index1++) { |
Lightvalve | 173:68c7914679ec | 645 | for (int n=0; n<batch_size; n++) { |
Lightvalve | 177:8e9cf31d63f4 | 646 | float d_x_d_ha2 = 0.0f; |
Lightvalve | 177:8e9cf31d63f4 | 647 | float d_y_d_ha2 = 0.0f; |
Lightvalve | 173:68c7914679ec | 648 | if((advantage[n] >= 0.0f && ratio[n] >= 1.0f + epsilon) || (advantage[n] < 0.0f && ratio[n] < 1.0f - epsilon)) { |
Lightvalve | 170:42c938a40313 | 649 | G_ha2[index1][index2] = G_ha2[index1][index2]; |
Lightvalve | 170:42c938a40313 | 650 | } else { |
Lightvalve | 173:68c7914679ec | 651 | if (hxh_a_sum_array[n][index2] >= 0) { |
Lightvalve | 173:68c7914679ec | 652 | if (hx_a_sum_array[n][index1] > 0) { |
Lightvalve | 177:8e9cf31d63f4 | 653 | d_x_d_ha2 = hx_a_sum_array[n][index1]*ha3_temp[index2][0]; |
Lightvalve | 177:8e9cf31d63f4 | 654 | d_y_d_ha2 = hx_a_sum_array[n][index1]*ha3_temp[index2][1]; |
Lightvalve | 171:bfc1fd2629d8 | 655 | } |
Lightvalve | 170:42c938a40313 | 656 | } |
Lightvalve | 173:68c7914679ec | 657 | |
Lightvalve | 170:42c938a40313 | 658 | float d_mean_d_ha2 = 0.0f; |
Lightvalve | 170:42c938a40313 | 659 | float d_dev_d_ha2 = 0.0f; |
Lightvalve | 177:8e9cf31d63f4 | 660 | 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 | 661 | 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 | 662 | |
Lightvalve | 173:68c7914679ec | 663 | 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 | 664 | } |
Lightvalve | 170:42c938a40313 | 665 | } |
Lightvalve | 176:589ea3edcf3c | 666 | G_ha2[index1][index2] = -G_ha2[index1][index2] / batch_size; |
Lightvalve | 179:d5377766d7ea | 667 | if(G_ha2[index1][index2] > Gradient_Limit) G_ha2[index1][index2] = Gradient_Limit; |
Lightvalve | 179:d5377766d7ea | 668 | else if (G_ha2[index1][index2] < -Gradient_Limit) G_ha2[index1][index2] = -Gradient_Limit; |
Lightvalve | 179:d5377766d7ea | 669 | //ha2_temp[index1][index2] = ha2_temp[index1][index2] - gradient_rate_actor * G_ha2[index1][index2]; |
Lightvalve | 170:42c938a40313 | 670 | } |
Lightvalve | 173:68c7914679ec | 671 | |
Lightvalve | 173:68c7914679ec | 672 | for (int n=0; n<batch_size; n++) { |
Lightvalve | 177:8e9cf31d63f4 | 673 | float d_x_d_ba2 = 0.0f; |
Lightvalve | 177:8e9cf31d63f4 | 674 | float d_y_d_ba2 = 0.0f; |
Lightvalve | 173:68c7914679ec | 675 | if((advantage[n] >= 0.0f && ratio[n] >= 1.0f + epsilon) || (advantage[n] < 0.0f && ratio[n] < 1.0f - epsilon)) { |
Lightvalve | 170:42c938a40313 | 676 | G_ba2[index2] = G_ba2[index2]; |
Lightvalve | 170:42c938a40313 | 677 | } else { |
Lightvalve | 170:42c938a40313 | 678 | |
Lightvalve | 173:68c7914679ec | 679 | if (hxh_a_sum_array[n][index2] >= 0) { |
Lightvalve | 177:8e9cf31d63f4 | 680 | d_x_d_ba2 = ha3_temp[index2][0]; |
Lightvalve | 177:8e9cf31d63f4 | 681 | d_y_d_ba2 = ha3_temp[index2][1]; |
Lightvalve | 173:68c7914679ec | 682 | } |
Lightvalve | 173:68c7914679ec | 683 | float d_mean_d_ba2= 0.0f; |
Lightvalve | 173:68c7914679ec | 684 | float d_dev_d_ba2= 0.0f; |
Lightvalve | 177:8e9cf31d63f4 | 685 | 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 | 686 | 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 | 687 | |
Lightvalve | 173:68c7914679ec | 688 | 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 | 689 | } |
Lightvalve | 170:42c938a40313 | 690 | } |
Lightvalve | 176:589ea3edcf3c | 691 | G_ba2[index2] = -G_ba2[index2] / batch_size; |
Lightvalve | 179:d5377766d7ea | 692 | if(G_ba2[index2] > Gradient_Limit) G_ba2[index2] = Gradient_Limit; |
Lightvalve | 179:d5377766d7ea | 693 | else if (G_ba2[index2] < -Gradient_Limit) G_ba2[index2] = -Gradient_Limit; |
Lightvalve | 179:d5377766d7ea | 694 | //ba2_temp[index2] = ba2_temp[index2] - gradient_rate_actor * G_ba2[index2]; |
Lightvalve | 170:42c938a40313 | 695 | } |
Lightvalve | 173:68c7914679ec | 696 | |
Lightvalve | 173:68c7914679ec | 697 | float G_ha3[num_hidden_unit2][2] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 698 | float G_ba3[2] = {0.0f}; |
Lightvalve | 173:68c7914679ec | 699 | |
Lightvalve | 173:68c7914679ec | 700 | for (int index2 = 0; index2 < 2; index2++) { |
Lightvalve | 173:68c7914679ec | 701 | for (int index1 = 0; index1 < num_hidden_unit2; index1++) { |
Lightvalve | 173:68c7914679ec | 702 | for (int n=0; n<batch_size; n++) { |
Lightvalve | 177:8e9cf31d63f4 | 703 | float d_x_d_ha3 = 0.0f; |
Lightvalve | 177:8e9cf31d63f4 | 704 | float d_y_d_ha3 = 0.0f; |
Lightvalve | 173:68c7914679ec | 705 | if((advantage[n] >= 0.0f && ratio[n] >= 1.0f + epsilon) || (advantage[n] < 0.0f && ratio[n] < 1.0f - epsilon)) { |
Lightvalve | 173:68c7914679ec | 706 | G_ha3[index1][index2] = G_ha3[index1][index2]; |
Lightvalve | 173:68c7914679ec | 707 | } else { |
Lightvalve | 173:68c7914679ec | 708 | if (hxh_a_sum_array[n][index1] >= 0) { |
Lightvalve | 173:68c7914679ec | 709 | if (hx_a_sum_array[n][index1] > 0) { |
Lightvalve | 177:8e9cf31d63f4 | 710 | d_x_d_ha3 = hxh_a_sum_array[n][index1]; |
Lightvalve | 177:8e9cf31d63f4 | 711 | d_y_d_ha3 = hxh_a_sum_array[n][index1]; |
Lightvalve | 173:68c7914679ec | 712 | } |
Lightvalve | 173:68c7914679ec | 713 | } |
Lightvalve | 173:68c7914679ec | 714 | float d_mean_d_ha3 = 0.0f; |
Lightvalve | 173:68c7914679ec | 715 | float d_dev_d_ha3 = 0.0f; |
Lightvalve | 177:8e9cf31d63f4 | 716 | 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 | 717 | 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 | 718 | |
Lightvalve | 173:68c7914679ec | 719 | 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 | 720 | } |
Lightvalve | 173:68c7914679ec | 721 | } |
Lightvalve | 176:589ea3edcf3c | 722 | G_ha3[index1][index2] = -G_ha3[index1][index2] / batch_size; |
Lightvalve | 179:d5377766d7ea | 723 | if(G_ha3[index1][index2] > Gradient_Limit) G_ha3[index1][index2] = Gradient_Limit; |
Lightvalve | 179:d5377766d7ea | 724 | else if (G_ha3[index1][index2] < -Gradient_Limit) G_ha3[index1][index2] = -Gradient_Limit; |
Lightvalve | 179:d5377766d7ea | 725 | //ha3_temp[index1][index2] = ha3_temp[index1][index2] - gradient_rate_actor * G_ha3[index1][index2]; |
Lightvalve | 173:68c7914679ec | 726 | } |
Lightvalve | 173:68c7914679ec | 727 | |
Lightvalve | 173:68c7914679ec | 728 | for (int n=0; n<batch_size; n++) { |
Lightvalve | 177:8e9cf31d63f4 | 729 | float d_x_d_ba3 = 0.0f; |
Lightvalve | 177:8e9cf31d63f4 | 730 | float d_y_d_ba3 = 0.0f; |
Lightvalve | 173:68c7914679ec | 731 | if((advantage[n] >= 0.0f && ratio[n] >= 1.0f + epsilon) || (advantage[n] < 0.0f && ratio[n] < 1.0f - epsilon)) { |
Lightvalve | 173:68c7914679ec | 732 | G_ba3[index2] = G_ba3[index2]; |
Lightvalve | 173:68c7914679ec | 733 | } else { |
Lightvalve | 173:68c7914679ec | 734 | |
Lightvalve | 177:8e9cf31d63f4 | 735 | d_x_d_ba3 = 1.0f; |
Lightvalve | 177:8e9cf31d63f4 | 736 | d_y_d_ba3 = 1.0f; |
Lightvalve | 173:68c7914679ec | 737 | |
Lightvalve | 173:68c7914679ec | 738 | float d_mean_d_ba3= 0.0f; |
Lightvalve | 173:68c7914679ec | 739 | float d_dev_d_ba3= 0.0f; |
Lightvalve | 177:8e9cf31d63f4 | 740 | 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 | 741 | 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 | 742 | |
Lightvalve | 173:68c7914679ec | 743 | 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 | 744 | } |
Lightvalve | 173:68c7914679ec | 745 | } |
Lightvalve | 176:589ea3edcf3c | 746 | G_ba3[index2] = -G_ba3[index2] / batch_size; |
Lightvalve | 179:d5377766d7ea | 747 | if(G_ba3[index2] > Gradient_Limit) G_ba3[index2] = Gradient_Limit; |
Lightvalve | 179:d5377766d7ea | 748 | else if (G_ba3[index2] < -Gradient_Limit) G_ba3[index2] = -Gradient_Limit; |
Lightvalve | 179:d5377766d7ea | 749 | //ba3_temp[index2] = ba3_temp[index2] - gradient_rate_actor * G_ba3[index2]; |
Lightvalve | 175:2f7289dbd488 | 750 | } |
Lightvalve | 177:8e9cf31d63f4 | 751 | |
Lightvalve | 175:2f7289dbd488 | 752 | // Simultaneous Update |
Lightvalve | 175:2f7289dbd488 | 753 | for (int index2 = 0; index2 < num_hidden_unit1; index2++) { |
Lightvalve | 175:2f7289dbd488 | 754 | for (int index1 = 0; index1 < num_input_RL; index1++) { |
Lightvalve | 179:d5377766d7ea | 755 | ha1_temp[index1][index2] = ha1_temp[index1][index2] - gradient_rate_actor * G_ha1[index1][index2]; |
Lightvalve | 175:2f7289dbd488 | 756 | } |
Lightvalve | 179:d5377766d7ea | 757 | ba1_temp[index2] = ba1_temp[index2] - gradient_rate_actor * G_ba1[index2]; |
Lightvalve | 175:2f7289dbd488 | 758 | } |
Lightvalve | 175:2f7289dbd488 | 759 | for (int index2 = 0; index2 < num_hidden_unit2; index2++) { |
Lightvalve | 175:2f7289dbd488 | 760 | for (int index1 = 0; index1 < num_hidden_unit1; index1++) { |
Lightvalve | 179:d5377766d7ea | 761 | ha2_temp[index1][index2] = ha2_temp[index1][index2] - gradient_rate_actor * G_ha2[index1][index2]; |
Lightvalve | 175:2f7289dbd488 | 762 | } |
Lightvalve | 179:d5377766d7ea | 763 | ba2_temp[index2] = ba2_temp[index2] - gradient_rate_actor * G_ba2[index2]; |
Lightvalve | 175:2f7289dbd488 | 764 | } |
Lightvalve | 175:2f7289dbd488 | 765 | for (int index2 = 0; index2 < 2; index2++) { |
Lightvalve | 175:2f7289dbd488 | 766 | for (int index1 = 0; index1 < num_hidden_unit2; index1++) { |
Lightvalve | 179:d5377766d7ea | 767 | ha3_temp[index1][index2] = ha3_temp[index1][index2] - gradient_rate_actor * G_ha3[index1][index2]; |
Lightvalve | 175:2f7289dbd488 | 768 | } |
Lightvalve | 179:d5377766d7ea | 769 | ba3_temp[index2] = ba3_temp[index2] - gradient_rate_actor * G_ba3[index2]; |
Lightvalve | 173:68c7914679ec | 770 | } |
Lightvalve | 170:42c938a40313 | 771 | } |
Lightvalve | 170:42c938a40313 | 772 | |
Lightvalve | 170:42c938a40313 | 773 | float rand_normal(double mean, double stddev) |
Lightvalve | 170:42c938a40313 | 774 | { |
Lightvalve | 170:42c938a40313 | 775 | //Box muller method |
Lightvalve | 170:42c938a40313 | 776 | static double n2 = 0.0f; |
Lightvalve | 170:42c938a40313 | 777 | static int n2_cached = 0; |
Lightvalve | 170:42c938a40313 | 778 | if (!n2_cached) { |
Lightvalve | 170:42c938a40313 | 779 | double x, y, r; |
Lightvalve | 170:42c938a40313 | 780 | do { |
Lightvalve | 170:42c938a40313 | 781 | x = 2.0f*rand()/RAND_MAX - 1; |
Lightvalve | 170:42c938a40313 | 782 | y = 2.0f*rand()/RAND_MAX - 1; |
Lightvalve | 170:42c938a40313 | 783 | |
Lightvalve | 170:42c938a40313 | 784 | r = x*x + y*y; |
Lightvalve | 170:42c938a40313 | 785 | } while (r == 0.0f || r > 1.0f); |
Lightvalve | 170:42c938a40313 | 786 | { |
Lightvalve | 170:42c938a40313 | 787 | double d = sqrt(-2.0f*log(r)/r); |
Lightvalve | 170:42c938a40313 | 788 | double n1 = x*d; |
Lightvalve | 170:42c938a40313 | 789 | n2 = y*d; |
Lightvalve | 170:42c938a40313 | 790 | double result = n1*stddev + mean; |
Lightvalve | 170:42c938a40313 | 791 | n2_cached = 1; |
Lightvalve | 170:42c938a40313 | 792 | return result; |
Lightvalve | 170:42c938a40313 | 793 | } |
Lightvalve | 170:42c938a40313 | 794 | } else { |
Lightvalve | 170:42c938a40313 | 795 | n2_cached = 0; |
Lightvalve | 170:42c938a40313 | 796 | return n2*stddev + mean; |
Lightvalve | 170:42c938a40313 | 797 | } |
Lightvalve | 170:42c938a40313 | 798 | } |
Lightvalve | 170:42c938a40313 | 799 | |
Lightvalve | 179:d5377766d7ea | 800 | float mean_adv(float x[], int size) |
Lightvalve | 179:d5377766d7ea | 801 | { |
Lightvalve | 179:d5377766d7ea | 802 | float add = 0.0f; |
Lightvalve | 179:d5377766d7ea | 803 | float result; |
Lightvalve | 218:066030f7951f | 804 | |
Lightvalve | 218:066030f7951f | 805 | for (int i=0; i<size; i++) { |
Lightvalve | 179:d5377766d7ea | 806 | add += x[i]; |
Lightvalve | 179:d5377766d7ea | 807 | } |
Lightvalve | 179:d5377766d7ea | 808 | result = (float) add/size; |
Lightvalve | 179:d5377766d7ea | 809 | return result; |
Lightvalve | 179:d5377766d7ea | 810 | } |
Lightvalve | 179:d5377766d7ea | 811 | float deviation_adv(float x[], int size) |
Lightvalve | 179:d5377766d7ea | 812 | { |
Lightvalve | 179:d5377766d7ea | 813 | float sigma = 0.0f; |
Lightvalve | 179:d5377766d7ea | 814 | float resultDeb = 0.0f; |
Lightvalve | 218:066030f7951f | 815 | |
Lightvalve | 218:066030f7951f | 816 | for (int k=0; k<size; k++) { |
Lightvalve | 179:d5377766d7ea | 817 | sigma = pow((float)x[k]-mean_adv(x,size), (float)2.0f)/(size-1); |
Lightvalve | 179:d5377766d7ea | 818 | resultDeb += sqrt(sigma); |
Lightvalve | 179:d5377766d7ea | 819 | } |
Lightvalve | 179:d5377766d7ea | 820 | return resultDeb; |
Lightvalve | 218:066030f7951f | 821 | } |
Lightvalve | 218:066030f7951f | 822 | |
Lightvalve | 173:68c7914679ec | 823 | |
Lightvalve | 170:42c938a40313 | 824 | void Overwirte_Critic_Networks() |
Lightvalve | 170:42c938a40313 | 825 | { |
Lightvalve | 173:68c7914679ec | 826 | for (int index2 = 0; index2 < num_hidden_unit1; index2++) { |
Lightvalve | 170:42c938a40313 | 827 | for (int index1 = 0; index1 < num_input_RL; index1++) { |
Lightvalve | 170:42c938a40313 | 828 | hc1[index1][index2] = hc1_temp[index1][index2]; |
Lightvalve | 170:42c938a40313 | 829 | } |
Lightvalve | 170:42c938a40313 | 830 | bc1[index2] = bc1_temp[index2]; |
Lightvalve | 170:42c938a40313 | 831 | } |
Lightvalve | 173:68c7914679ec | 832 | for (int index2 = 0; index2 < num_hidden_unit2; index2++) { |
Lightvalve | 173:68c7914679ec | 833 | for (int index1 = 0; index1 < num_hidden_unit1; index1++) { |
Lightvalve | 173:68c7914679ec | 834 | hc2[index1][index2] = hc2_temp[index1][index2]; |
Lightvalve | 173:68c7914679ec | 835 | } |
Lightvalve | 173:68c7914679ec | 836 | bc2[index2] = bc2_temp[index2]; |
Lightvalve | 173:68c7914679ec | 837 | hc3[index2] = hc3_temp[index2]; |
Lightvalve | 173:68c7914679ec | 838 | } |
Lightvalve | 173:68c7914679ec | 839 | bc3 = bc3_temp; |
Lightvalve | 170:42c938a40313 | 840 | } |
Lightvalve | 170:42c938a40313 | 841 | void Overwirte_Actor_Networks() |
Lightvalve | 170:42c938a40313 | 842 | { |
Lightvalve | 173:68c7914679ec | 843 | for (int index2 = 0; index2 < num_hidden_unit1; index2++) { |
Lightvalve | 170:42c938a40313 | 844 | for (int index1 = 0; index1 < num_input_RL; index1++) { |
Lightvalve | 170:42c938a40313 | 845 | ha1[index1][index2] = ha1_temp[index1][index2]; |
Lightvalve | 170:42c938a40313 | 846 | } |
Lightvalve | 170:42c938a40313 | 847 | ba1[index2] = ba1_temp[index2]; |
Lightvalve | 170:42c938a40313 | 848 | } |
Lightvalve | 173:68c7914679ec | 849 | for (int index2 = 0; index2 < num_hidden_unit2; index2++) { |
Lightvalve | 173:68c7914679ec | 850 | for (int index1 = 0; index1 < num_hidden_unit1; index1++) { |
Lightvalve | 170:42c938a40313 | 851 | ha2[index1][index2] = ha2_temp[index1][index2]; |
Lightvalve | 170:42c938a40313 | 852 | } |
Lightvalve | 170:42c938a40313 | 853 | ba2[index2] = ba2_temp[index2]; |
Lightvalve | 170:42c938a40313 | 854 | } |
Lightvalve | 173:68c7914679ec | 855 | for (int index2 = 0; index2 < 2; index2++) { |
Lightvalve | 173:68c7914679ec | 856 | for (int index1 = 0; index1 < num_hidden_unit2; index1++) { |
Lightvalve | 173:68c7914679ec | 857 | ha3[index1][index2] = ha3_temp[index1][index2]; |
Lightvalve | 173:68c7914679ec | 858 | } |
Lightvalve | 173:68c7914679ec | 859 | ba3[index2] = ba3_temp[index2]; |
Lightvalve | 173:68c7914679ec | 860 | } |
Lightvalve | 170:42c938a40313 | 861 | } |
Lightvalve | 170:42c938a40313 | 862 | |
Lightvalve | 170:42c938a40313 | 863 | |
GiJeongKim | 0:51c43836c1d7 | 864 | int main() |
GiJeongKim | 0:51c43836c1d7 | 865 | { |
Lightvalve | 66:a8e6799dbce3 | 866 | |
Lightvalve | 65:a2d7c63419c2 | 867 | HAL_Init(); |
Lightvalve | 65:a2d7c63419c2 | 868 | SystemClock_Config(); |
Lightvalve | 169:645207e160ca | 869 | |
jobuuu | 6:df07d3491e3a | 870 | /********************************* |
jobuuu | 1:e04e563be5ce | 871 | *** Initialization |
jobuuu | 6:df07d3491e3a | 872 | *********************************/ |
Lightvalve | 69:3995ffeaa786 | 873 | LED = 0; |
Lightvalve | 61:bc8c8270f0ab | 874 | pc.baud(9600); |
Lightvalve | 21:e5f1a43ea6f9 | 875 | |
GiJeongKim | 0:51c43836c1d7 | 876 | // i2c init |
Lightvalve | 8:5d2eebdad025 | 877 | i2c.frequency(400 * 1000); // 0.4 mHz |
Lightvalve | 8:5d2eebdad025 | 878 | wait_ms(2); // Power Up wait |
Lightvalve | 8:5d2eebdad025 | 879 | look_for_hardware_i2c(); // Hardware present |
Lightvalve | 8:5d2eebdad025 | 880 | init_as5510(i2c_slave_addr1); |
Lightvalve | 11:82d8768d7351 | 881 | make_delay(); |
jobuuu | 2:a1c0a37df760 | 882 | |
GiJeongKim | 0:51c43836c1d7 | 883 | // // spi init |
Lightvalve | 170:42c938a40313 | 884 | eeprom.format(8,3); |
Lightvalve | 170:42c938a40313 | 885 | eeprom.frequency(5000000); //5M |
GiJeongKim | 0:51c43836c1d7 | 886 | enc.format(8,0); |
GiJeongKim | 0:51c43836c1d7 | 887 | enc.frequency(5000000); //5M |
Lightvalve | 11:82d8768d7351 | 888 | make_delay(); |
Lightvalve | 21:e5f1a43ea6f9 | 889 | |
Lightvalve | 16:903b5a4433b4 | 890 | //rom |
Lightvalve | 19:23b7c1ad8683 | 891 | ROM_CALL_DATA(); |
Lightvalve | 16:903b5a4433b4 | 892 | make_delay(); |
Lightvalve | 13:747daba9cf59 | 893 | |
GiJeongKim | 0:51c43836c1d7 | 894 | // ADC init |
jobuuu | 5:a4319f79457b | 895 | Init_ADC(); |
Lightvalve | 11:82d8768d7351 | 896 | make_delay(); |
jobuuu | 2:a1c0a37df760 | 897 | |
GiJeongKim | 0:51c43836c1d7 | 898 | // Pwm init |
GiJeongKim | 0:51c43836c1d7 | 899 | Init_PWM(); |
GiJeongKim | 0:51c43836c1d7 | 900 | TIM4->CR1 ^= TIM_CR1_UDIS; |
Lightvalve | 11:82d8768d7351 | 901 | make_delay(); |
Lightvalve | 13:747daba9cf59 | 902 | |
Lightvalve | 11:82d8768d7351 | 903 | // TMR3 init |
Lightvalve | 11:82d8768d7351 | 904 | Init_TMR3(); |
Lightvalve | 11:82d8768d7351 | 905 | TIM3->CR1 ^= TIM_CR1_UDIS; |
Lightvalve | 11:82d8768d7351 | 906 | make_delay(); |
Lightvalve | 21:e5f1a43ea6f9 | 907 | |
Lightvalve | 50:3c630b5eba9f | 908 | // TMR2 init |
Lightvalve | 56:6f50d9d3bfee | 909 | // Init_TMR2(); |
Lightvalve | 56:6f50d9d3bfee | 910 | // TIM2->CR1 ^= TIM_CR1_UDIS; |
Lightvalve | 56:6f50d9d3bfee | 911 | // make_delay(); |
Lightvalve | 21:e5f1a43ea6f9 | 912 | |
GiJeongKim | 0:51c43836c1d7 | 913 | // CAN |
jobuuu | 2:a1c0a37df760 | 914 | can.attach(&CAN_RX_HANDLER); |
Lightvalve | 11:82d8768d7351 | 915 | CAN_ID_INIT(); |
Lightvalve | 11:82d8768d7351 | 916 | make_delay(); |
Lightvalve | 34:bb2ca2fc2a8e | 917 | |
Lightvalve | 23:59218d4a256d | 918 | //Timer priority |
Lightvalve | 23:59218d4a256d | 919 | NVIC_SetPriority(TIM3_IRQn, 2); |
Lightvalve | 57:f4819de54e7a | 920 | //NVIC_SetPriority(TIM2_IRQn, 3); |
Lightvalve | 54:647072f5307a | 921 | NVIC_SetPriority(TIM4_IRQn, 3); |
Lightvalve | 34:bb2ca2fc2a8e | 922 | |
Lightvalve | 23:59218d4a256d | 923 | //can.reset(); |
Lightvalve | 19:23b7c1ad8683 | 924 | can.filter(msg.id, 0xFFFFF000, CANStandard); |
Lightvalve | 34:bb2ca2fc2a8e | 925 | |
GiJeongKim | 0:51c43836c1d7 | 926 | // spi _ enc |
GiJeongKim | 0:51c43836c1d7 | 927 | spi_enc_set_init(); |
Lightvalve | 11:82d8768d7351 | 928 | make_delay(); |
Lightvalve | 13:747daba9cf59 | 929 | |
Lightvalve | 11:82d8768d7351 | 930 | //DAC init |
Lightvalve | 58:2eade98630e2 | 931 | if (SENSING_MODE == 0) { |
Lightvalve | 58:2eade98630e2 | 932 | dac_1 = TORQUE_VREF / 3.3f; |
Lightvalve | 58:2eade98630e2 | 933 | dac_2 = 0.0f; |
Lightvalve | 58:2eade98630e2 | 934 | } else if (SENSING_MODE == 1) { |
Lightvalve | 58:2eade98630e2 | 935 | dac_1 = PRES_A_VREF / 3.3f; |
Lightvalve | 58:2eade98630e2 | 936 | dac_2 = PRES_B_VREF / 3.3f; |
Lightvalve | 58:2eade98630e2 | 937 | } |
Lightvalve | 11:82d8768d7351 | 938 | make_delay(); |
Lightvalve | 13:747daba9cf59 | 939 | |
Lightvalve | 19:23b7c1ad8683 | 940 | for (int i=0; i<50; i++) { |
Lightvalve | 11:82d8768d7351 | 941 | if(i%2==0) |
Lightvalve | 38:118df027d851 | 942 | ID_index_array[i] = - i * 0.5f; |
Lightvalve | 11:82d8768d7351 | 943 | else |
Lightvalve | 38:118df027d851 | 944 | ID_index_array[i] = (i+1) * 0.5f; |
Lightvalve | 11:82d8768d7351 | 945 | } |
Lightvalve | 169:645207e160ca | 946 | |
Lightvalve | 173:68c7914679ec | 947 | for (int index2 = 0; index2 < num_hidden_unit1; index2++) { |
Lightvalve | 170:42c938a40313 | 948 | for (int index1 = 0; index1 < num_input_RL; index1++) { |
Lightvalve | 179:d5377766d7ea | 949 | hc1_temp[index1][index2] = (float) (rand()%100) * 0.007f ; |
Lightvalve | 170:42c938a40313 | 950 | } |
Lightvalve | 179:d5377766d7ea | 951 | bc1_temp[index2] = (float) (rand()%100) * 0.007f; |
Lightvalve | 173:68c7914679ec | 952 | } |
Lightvalve | 173:68c7914679ec | 953 | for (int index2 = 0; index2 < num_hidden_unit2; index2++) { |
Lightvalve | 173:68c7914679ec | 954 | for (int index1 = 0; index1 < num_hidden_unit1; index1++) { |
Lightvalve | 179:d5377766d7ea | 955 | hc2_temp[index1][index2] = (float) (rand()%100) * 0.007f; |
Lightvalve | 173:68c7914679ec | 956 | } |
Lightvalve | 179:d5377766d7ea | 957 | bc2_temp[index2] = (float) (rand()%100) * 0.007f; |
Lightvalve | 179:d5377766d7ea | 958 | hc3_temp[index2] = (float) (rand()%100) * 0.007f; |
Lightvalve | 170:42c938a40313 | 959 | } |
Lightvalve | 179:d5377766d7ea | 960 | bc3_temp = (float) (rand()%100) * 0.007f; |
Lightvalve | 173:68c7914679ec | 961 | |
Lightvalve | 173:68c7914679ec | 962 | for (int index2 = 0; index2 < num_hidden_unit1; index2++) { |
Lightvalve | 170:42c938a40313 | 963 | for (int index1 = 0; index1 < num_input_RL; index1++) { |
Lightvalve | 179:d5377766d7ea | 964 | ha1_temp[index1][index2] = (float) (rand()%100) * 0.007f; |
Lightvalve | 170:42c938a40313 | 965 | } |
Lightvalve | 179:d5377766d7ea | 966 | ba1_temp[index2] = (float) (rand()%100) * 0.007f; |
Lightvalve | 173:68c7914679ec | 967 | } |
Lightvalve | 173:68c7914679ec | 968 | for (int index2 = 0; index2 < num_hidden_unit2; index2++) { |
Lightvalve | 173:68c7914679ec | 969 | for (int index1 = 0; index1 < num_hidden_unit1; index1++) { |
Lightvalve | 179:d5377766d7ea | 970 | ha2_temp[index1][index2] = (float) (rand()%100) * 0.007f; |
Lightvalve | 173:68c7914679ec | 971 | } |
Lightvalve | 179:d5377766d7ea | 972 | ba2_temp[index2] = (float) (rand()%100) * 0.007f; |
Lightvalve | 170:42c938a40313 | 973 | } |
Lightvalve | 170:42c938a40313 | 974 | for (int index2 = 0; index2 < 2; index2++) { |
Lightvalve | 173:68c7914679ec | 975 | for (int index1 = 0; index1 < num_hidden_unit2; index1++) { |
Lightvalve | 179:d5377766d7ea | 976 | ha3_temp[index1][index2] = (float) (rand()%100) * 0.007f; |
Lightvalve | 170:42c938a40313 | 977 | } |
Lightvalve | 179:d5377766d7ea | 978 | ba3_temp[index2] = (float) (rand()%100) * 0.007f; |
Lightvalve | 170:42c938a40313 | 979 | } |
Lightvalve | 171:bfc1fd2629d8 | 980 | |
Lightvalve | 170:42c938a40313 | 981 | Overwirte_Critic_Networks(); |
Lightvalve | 170:42c938a40313 | 982 | Overwirte_Actor_Networks(); |
Lightvalve | 61:bc8c8270f0ab | 983 | |
jobuuu | 6:df07d3491e3a | 984 | /************************************ |
jobuuu | 1:e04e563be5ce | 985 | *** Program is operating! |
jobuuu | 6:df07d3491e3a | 986 | *************************************/ |
GiJeongKim | 0:51c43836c1d7 | 987 | while(1) { |
Lightvalve | 169:645207e160ca | 988 | |
Lightvalve | 171:bfc1fd2629d8 | 989 | // if(timer_while==27491) { |
Lightvalve | 171:bfc1fd2629d8 | 990 | // timer_while = 0; |
Lightvalve | 171:bfc1fd2629d8 | 991 | // 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 | 992 | // //pc.printf("%f\n", virt_pos); |
Lightvalve | 171:bfc1fd2629d8 | 993 | // //pc.printf("%f\n", pos.sen/(float)(ENC_PULSE_PER_POSITION)); |
Lightvalve | 171:bfc1fd2629d8 | 994 | // //pc.printf("ref : %f virt_pos : %f\n", pos.sen/(float)(ENC_PULSE_PER_POSITION), virt_pos); |
Lightvalve | 171:bfc1fd2629d8 | 995 | // } |
Lightvalve | 169:645207e160ca | 996 | |
Lightvalve | 171:bfc1fd2629d8 | 997 | |
Lightvalve | 171:bfc1fd2629d8 | 998 | //i2c |
Lightvalve | 180:02be1711ee0b | 999 | read_field(i2c_slave_addr1); |
Lightvalve | 180:02be1711ee0b | 1000 | if(DIR_VALVE_ENC < 0) value = 1023 - value; |
Lightvalve | 171:bfc1fd2629d8 | 1001 | |
Lightvalve | 177:8e9cf31d63f4 | 1002 | //timer_while ++; |
Lightvalve | 169:645207e160ca | 1003 | |
Lightvalve | 170:42c938a40313 | 1004 | ///////////////////////////////////////////////////////Neural Network |
Lightvalve | 169:645207e160ca | 1005 | |
Lightvalve | 73:f80dc3970c99 | 1006 | if(NN_Control_Flag == 0) { |
Lightvalve | 73:f80dc3970c99 | 1007 | LED = 0; |
Lightvalve | 73:f80dc3970c99 | 1008 | } |
Lightvalve | 169:645207e160ca | 1009 | |
Lightvalve | 73:f80dc3970c99 | 1010 | else if(NN_Control_Flag == 1) { |
Lightvalve | 169:645207e160ca | 1011 | |
Lightvalve | 162:9dd4f35e9de8 | 1012 | int ind = 0; |
Lightvalve | 162:9dd4f35e9de8 | 1013 | for(int i=0; i<numpast_u; i++) { |
Lightvalve | 162:9dd4f35e9de8 | 1014 | input_NN[ind] = u_past[time_interval*i]; |
Lightvalve | 162:9dd4f35e9de8 | 1015 | ind = ind + 1; |
Lightvalve | 162:9dd4f35e9de8 | 1016 | } |
Lightvalve | 162:9dd4f35e9de8 | 1017 | |
Lightvalve | 162:9dd4f35e9de8 | 1018 | for(int i=0; i<numpast_x; i++) { |
Lightvalve | 162:9dd4f35e9de8 | 1019 | input_NN[ind] = x_past[time_interval*i] / 60.0f; |
Lightvalve | 162:9dd4f35e9de8 | 1020 | ind = ind + 1; |
Lightvalve | 162:9dd4f35e9de8 | 1021 | } |
Lightvalve | 162:9dd4f35e9de8 | 1022 | input_NN[ind] = (pos.sen / ENC_PULSE_PER_POSITION) / 60.0f; |
Lightvalve | 162:9dd4f35e9de8 | 1023 | ind = ind + 1; |
Lightvalve | 169:645207e160ca | 1024 | |
Lightvalve | 162:9dd4f35e9de8 | 1025 | // for(int i=0; i<numfuture_x; i++) { |
Lightvalve | 162:9dd4f35e9de8 | 1026 | // input_NN[ind] = x_future[time_interval*i+time_interval] / 60.0f; |
Lightvalve | 122:dcb3ce3056a0 | 1027 | // ind = ind + 1; |
Lightvalve | 122:dcb3ce3056a0 | 1028 | // } |
Lightvalve | 162:9dd4f35e9de8 | 1029 | |
Lightvalve | 162:9dd4f35e9de8 | 1030 | for(int i=0; i<numpast_f; i++) { |
Lightvalve | 208:408f9f15c486 | 1031 | // input_NN[ind] = f_past[time_interval*i] / 10000.0f * 8.0f + 0.5f; |
Lightvalve | 208:408f9f15c486 | 1032 | input_NN[ind] = f_past[time_interval*i] / 10000.0f + 0.5f; |
Lightvalve | 162:9dd4f35e9de8 | 1033 | ind = ind + 1; |
Lightvalve | 162:9dd4f35e9de8 | 1034 | } |
Lightvalve | 208:408f9f15c486 | 1035 | // input_NN[ind] = torq.sen / 10000.0f * 8.0f + 0.5f; |
Lightvalve | 220:153c6f5e1c44 | 1036 | input_NN[ind] = torq.sen / 10000.0f + 0.5f; |
Lightvalve | 220:153c6f5e1c44 | 1037 | ind = ind + 1; |
Lightvalve | 238:e4bda4d06a7d | 1038 | for(int i=0; i<numfuture_f; i++) { |
Lightvalve | 208:408f9f15c486 | 1039 | // input_NN[ind] = (f_future[time_interval*i+time_interval] - torq.sen)/10000.0f * 8.0f + 0.5f; |
Lightvalve | 228:83e3a91aa1c6 | 1040 | // input_NN[ind] = (f_future[time_interval*i+time_interval] - torq.sen)/10000.0f + 0.5f; |
Lightvalve | 206:2e4d0c287578 | 1041 | // input_NN[ind] = (f_future[time_interval*i+time_interval])/10000.0f*8.0f+0.5f; |
Lightvalve | 228:83e3a91aa1c6 | 1042 | input_NN[ind] = (f_future[time_interval*i+time_interval])/10000.0f + 0.5f; |
Lightvalve | 162:9dd4f35e9de8 | 1043 | ind = ind + 1; |
Lightvalve | 162:9dd4f35e9de8 | 1044 | } |
Lightvalve | 169:645207e160ca | 1045 | |
Lightvalve | 112:8dcb1600cb90 | 1046 | float output1[16] = { 0.0f }; |
Lightvalve | 112:8dcb1600cb90 | 1047 | float output2[16] = { 0.0f }; |
Lightvalve | 112:8dcb1600cb90 | 1048 | float output3[16] = { 0.0f }; |
Lightvalve | 66:a8e6799dbce3 | 1049 | float output = 0.0f; |
Lightvalve | 65:a2d7c63419c2 | 1050 | |
Lightvalve | 112:8dcb1600cb90 | 1051 | for (int index2 = 0; index2 < 16; index2++) { |
Lightvalve | 68:328e1be06f5d | 1052 | for (int index1 = 0; index1 < num_input; index1++) { |
Lightvalve | 66:a8e6799dbce3 | 1053 | output1[index2] = output1[index2] |
Lightvalve | 73:f80dc3970c99 | 1054 | + h1[index1][index2] * input_NN[index1]; |
Lightvalve | 66:a8e6799dbce3 | 1055 | } |
Lightvalve | 66:a8e6799dbce3 | 1056 | output1[index2] = output1[index2] + b1[index2]; |
Lightvalve | 66:a8e6799dbce3 | 1057 | if (output1[index2] < 0) { |
Lightvalve | 66:a8e6799dbce3 | 1058 | output1[index2] = 0; |
Lightvalve | 66:a8e6799dbce3 | 1059 | } |
Lightvalve | 66:a8e6799dbce3 | 1060 | } |
Lightvalve | 65:a2d7c63419c2 | 1061 | |
Lightvalve | 112:8dcb1600cb90 | 1062 | for (int index2 = 0; index2 < 16; index2++) { |
Lightvalve | 112:8dcb1600cb90 | 1063 | for (int index1 = 0; index1 < 16; index1++) { |
Lightvalve | 66:a8e6799dbce3 | 1064 | output2[index2] = output2[index2] |
Lightvalve | 66:a8e6799dbce3 | 1065 | + h2[index1][index2] * output1[index1]; |
Lightvalve | 66:a8e6799dbce3 | 1066 | } |
Lightvalve | 66:a8e6799dbce3 | 1067 | output2[index2] = output2[index2] + b2[index2]; |
Lightvalve | 66:a8e6799dbce3 | 1068 | if (output2[index2] < 0) { |
Lightvalve | 66:a8e6799dbce3 | 1069 | output2[index2] = 0; |
Lightvalve | 66:a8e6799dbce3 | 1070 | } |
Lightvalve | 66:a8e6799dbce3 | 1071 | } |
Lightvalve | 65:a2d7c63419c2 | 1072 | |
Lightvalve | 112:8dcb1600cb90 | 1073 | for (int index2 = 0; index2 < 16; index2++) { |
Lightvalve | 112:8dcb1600cb90 | 1074 | for (int index1 = 0; index1 < 16; index1++) { |
Lightvalve | 66:a8e6799dbce3 | 1075 | output3[index2] = output3[index2] |
Lightvalve | 66:a8e6799dbce3 | 1076 | + h3[index1][index2] * output2[index1]; |
Lightvalve | 66:a8e6799dbce3 | 1077 | } |
Lightvalve | 66:a8e6799dbce3 | 1078 | output3[index2] = output3[index2] + b3[index2]; |
Lightvalve | 66:a8e6799dbce3 | 1079 | if (output3[index2] < 0) { |
Lightvalve | 66:a8e6799dbce3 | 1080 | output3[index2] = 0; |
Lightvalve | 66:a8e6799dbce3 | 1081 | } |
Lightvalve | 65:a2d7c63419c2 | 1082 | } |
Lightvalve | 66:a8e6799dbce3 | 1083 | |
Lightvalve | 66:a8e6799dbce3 | 1084 | for (int index2 = 0; index2 < 1; index2++) { |
Lightvalve | 112:8dcb1600cb90 | 1085 | for (int index1 = 0; index1 < 16; index1++) { |
Lightvalve | 73:f80dc3970c99 | 1086 | output = output + hout[index1] * output3[index1]; |
Lightvalve | 66:a8e6799dbce3 | 1087 | } |
Lightvalve | 66:a8e6799dbce3 | 1088 | output = output + bout[index2]; |
Lightvalve | 169:645207e160ca | 1089 | |
Lightvalve | 66:a8e6799dbce3 | 1090 | } |
Lightvalve | 73:f80dc3970c99 | 1091 | output = 1.0f/(1.0f+exp(-output)); |
Lightvalve | 101:50159049a518 | 1092 | output_normalized = output; |
Lightvalve | 68:328e1be06f5d | 1093 | output = output * 20000.0f - 10000.0f; |
Lightvalve | 169:645207e160ca | 1094 | |
Lightvalve | 66:a8e6799dbce3 | 1095 | if(output>=0) { |
Lightvalve | 66:a8e6799dbce3 | 1096 | valve_pos.ref = output*0.0001f*((double)VALVE_MAX_POS - (double) VALVE_CENTER) + (double) VALVE_CENTER; |
Lightvalve | 66:a8e6799dbce3 | 1097 | } else { |
Lightvalve | 66:a8e6799dbce3 | 1098 | valve_pos.ref = -output*0.0001f*((double)VALVE_MIN_POS - (double) VALVE_CENTER) + (double) VALVE_CENTER; |
Lightvalve | 65:a2d7c63419c2 | 1099 | } |
Lightvalve | 87:471334725012 | 1100 | |
Lightvalve | 169:645207e160ca | 1101 | |
Lightvalve | 69:3995ffeaa786 | 1102 | if(LED==1) { |
Lightvalve | 69:3995ffeaa786 | 1103 | LED=0; |
Lightvalve | 69:3995ffeaa786 | 1104 | } else |
Lightvalve | 69:3995ffeaa786 | 1105 | LED = 1; |
Lightvalve | 169:645207e160ca | 1106 | |
Lightvalve | 65:a2d7c63419c2 | 1107 | } |
Lightvalve | 171:bfc1fd2629d8 | 1108 | |
Lightvalve | 171:bfc1fd2629d8 | 1109 | |
Lightvalve | 170:42c938a40313 | 1110 | /////////////////////////////////////////////////////////////////////RL |
Lightvalve | 170:42c938a40313 | 1111 | switch (Update_Case) { |
Lightvalve | 170:42c938a40313 | 1112 | case 0: { |
Lightvalve | 170:42c938a40313 | 1113 | break; |
Lightvalve | 170:42c938a40313 | 1114 | } |
Lightvalve | 170:42c938a40313 | 1115 | case 1: { |
Lightvalve | 170:42c938a40313 | 1116 | //Network Update(just update and hold network) |
Lightvalve | 170:42c938a40313 | 1117 | for (int epoch = 0; epoch < num_epoch; epoch++) { |
Lightvalve | 170:42c938a40313 | 1118 | float loss_sum = 0.0f; |
Lightvalve | 175:2f7289dbd488 | 1119 | for (int n=batch_size-1; n>=0; n--) { |
Lightvalve | 170:42c938a40313 | 1120 | //Calculate Estimated V |
Lightvalve | 175:2f7289dbd488 | 1121 | //float temp_array[3] = {state_array[n][0], state_array[n][1], state_array[n][2]}; |
Lightvalve | 175:2f7289dbd488 | 1122 | float temp_array[2] = {state_array[n][0], state_array[n][1]}; |
Lightvalve | 175:2f7289dbd488 | 1123 | V[n] = Critic_Network_Temp(temp_array); |
Lightvalve | 173:68c7914679ec | 1124 | for (int i=0; i<num_hidden_unit1; i++) { |
Lightvalve | 175:2f7289dbd488 | 1125 | hx_c_sum_array[n][i] = hx_c_sum[i]; |
Lightvalve | 173:68c7914679ec | 1126 | } |
Lightvalve | 173:68c7914679ec | 1127 | for (int i=0; i<num_hidden_unit2; i++) { |
Lightvalve | 175:2f7289dbd488 | 1128 | hxh_c_sum_array[n][i] = hxh_c_sum[i]; |
Lightvalve | 173:68c7914679ec | 1129 | } |
Lightvalve | 175:2f7289dbd488 | 1130 | hxhh_c_sum_array[n] = hxhh_c_sum; |
Lightvalve | 177:8e9cf31d63f4 | 1131 | |
Lightvalve | 175:2f7289dbd488 | 1132 | 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 | 1133 | Actor_Network_Old(temp_array); |
Lightvalve | 175:2f7289dbd488 | 1134 | 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 | 1135 | r[n] = exp(-0.25f * 5.0f * state_array[n][1] * state_array[n][1]); |
Lightvalve | 175:2f7289dbd488 | 1136 | if(n == batch_size-1) return_G[n] = 0.0f; |
Lightvalve | 175:2f7289dbd488 | 1137 | else return_G[n] = gamma * return_G[n+1] + r[n]; |
Lightvalve | 175:2f7289dbd488 | 1138 | if(n == batch_size-1) td_target[n] = r[n]; |
Lightvalve | 175:2f7289dbd488 | 1139 | else td_target[n] = r[n] + gamma * V[n+1]; |
Lightvalve | 175:2f7289dbd488 | 1140 | delta[n] = td_target[n] - V[n]; |
Lightvalve | 175:2f7289dbd488 | 1141 | if(n == batch_size-1) advantage[n] = 0.0f; |
Lightvalve | 175:2f7289dbd488 | 1142 | else advantage[n] = gamma * lmbda * advantage[n+1] + delta[n]; |
Lightvalve | 179:d5377766d7ea | 1143 | // return_G[n] = advantage[n] + V[n]; |
Lightvalve | 175:2f7289dbd488 | 1144 | ratio[n] = pi[n]/pi_old[n]; |
Lightvalve | 179:d5377766d7ea | 1145 | } |
Lightvalve | 179:d5377766d7ea | 1146 | float mean_advantage = 0.0f; |
Lightvalve | 179:d5377766d7ea | 1147 | float dev_advantage = 0.0f; |
Lightvalve | 179:d5377766d7ea | 1148 | mean_advantage = mean_adv(advantage, batch_size); |
Lightvalve | 179:d5377766d7ea | 1149 | dev_advantage = deviation_adv(advantage, batch_size); |
Lightvalve | 179:d5377766d7ea | 1150 | for (int n=batch_size-1; n>=0; n--) { |
Lightvalve | 179:d5377766d7ea | 1151 | //advantage[n] = (advantage[n]-mean_advantage)/dev_advantage; |
Lightvalve | 175:2f7289dbd488 | 1152 | surr1[n] = ratio[n] * advantage[n]; |
Lightvalve | 175:2f7289dbd488 | 1153 | if (ratio[n] > 1.0f + epsilon) { |
Lightvalve | 175:2f7289dbd488 | 1154 | surr2[n] = (1.0f + epsilon)*advantage[n]; |
Lightvalve | 175:2f7289dbd488 | 1155 | } else if( ratio[n] < 1.0f - epsilon) { |
Lightvalve | 175:2f7289dbd488 | 1156 | surr2[n] = (1.0f - epsilon)*advantage[n]; |
Lightvalve | 170:42c938a40313 | 1157 | } else { |
Lightvalve | 175:2f7289dbd488 | 1158 | surr2[n] = ratio[n]*advantage[n]; |
Lightvalve | 170:42c938a40313 | 1159 | } |
Lightvalve | 175:2f7289dbd488 | 1160 | loss[n] = -min(surr1[n], surr2[n]); |
Lightvalve | 175:2f7289dbd488 | 1161 | loss_sum = loss_sum + loss[n]; |
Lightvalve | 170:42c938a40313 | 1162 | } |
Lightvalve | 170:42c938a40313 | 1163 | reward_sum = 0.0f; |
Lightvalve | 170:42c938a40313 | 1164 | for (int i=0; i<batch_size; i++) { |
Lightvalve | 170:42c938a40313 | 1165 | reward_sum = reward_sum + r[i]; |
Lightvalve | 170:42c938a40313 | 1166 | } |
Lightvalve | 170:42c938a40313 | 1167 | logging5 = reward_sum; |
Lightvalve | 171:bfc1fd2629d8 | 1168 | |
Lightvalve | 171:bfc1fd2629d8 | 1169 | |
Lightvalve | 170:42c938a40313 | 1170 | //loss_batch = loss_sum / (float) batch_size; |
Lightvalve | 170:42c938a40313 | 1171 | loss_batch = loss_sum; |
Lightvalve | 170:42c938a40313 | 1172 | //Update Networks |
Lightvalve | 170:42c938a40313 | 1173 | update_Critic_Networks(state_array); |
Lightvalve | 170:42c938a40313 | 1174 | update_Actor_Networks(state_array); |
Lightvalve | 170:42c938a40313 | 1175 | } |
Lightvalve | 170:42c938a40313 | 1176 | Update_Done_Flag = 1; |
Lightvalve | 170:42c938a40313 | 1177 | Update_Case = 0; |
Lightvalve | 170:42c938a40313 | 1178 | //logging1 = V[0]; |
Lightvalve | 171:bfc1fd2629d8 | 1179 | |
Lightvalve | 170:42c938a40313 | 1180 | break; |
Lightvalve | 170:42c938a40313 | 1181 | } |
Lightvalve | 170:42c938a40313 | 1182 | case 2: { |
Lightvalve | 170:42c938a40313 | 1183 | //Network apply to next Network |
Lightvalve | 170:42c938a40313 | 1184 | Overwirte_Critic_Networks(); |
Lightvalve | 170:42c938a40313 | 1185 | Overwirte_Actor_Networks(); |
Lightvalve | 170:42c938a40313 | 1186 | virt_pos = 10.0f; |
Lightvalve | 170:42c938a40313 | 1187 | Update_Done_Flag = 1; |
Lightvalve | 170:42c938a40313 | 1188 | Update_Case = 0; |
Lightvalve | 170:42c938a40313 | 1189 | break; |
Lightvalve | 170:42c938a40313 | 1190 | } |
Lightvalve | 169:645207e160ca | 1191 | |
Lightvalve | 170:42c938a40313 | 1192 | } |
GiJeongKim | 0:51c43836c1d7 | 1193 | } |
jobuuu | 1:e04e563be5ce | 1194 | } |
jobuuu | 1:e04e563be5ce | 1195 | |
Lightvalve | 33:91b17819ec30 | 1196 | float DDV_JOINT_POS_FF(float REF_JOINT_VEL) |
Lightvalve | 14:8e7590227d22 | 1197 | { |
Lightvalve | 14:8e7590227d22 | 1198 | |
Lightvalve | 13:747daba9cf59 | 1199 | int i = 0; |
Lightvalve | 48:889798ff9329 | 1200 | float Ref_Valve_Pos_FF = 0.0f; |
Lightvalve | 14:8e7590227d22 | 1201 | for(i=0; i<VALVE_POS_NUM; i++) { |
Lightvalve | 14:8e7590227d22 | 1202 | 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 | 1203 | if(i==0) { |
Lightvalve | 50:3c630b5eba9f | 1204 | if(JOINT_VEL[i+1] == JOINT_VEL[i]) { |
Lightvalve | 57:f4819de54e7a | 1205 | Ref_Valve_Pos_FF = (float) VALVE_CENTER; |
Lightvalve | 50:3c630b5eba9f | 1206 | } else { |
Lightvalve | 57:f4819de54e7a | 1207 | 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 | 1208 | } |
Lightvalve | 14:8e7590227d22 | 1209 | } else { |
Lightvalve | 50:3c630b5eba9f | 1210 | if(JOINT_VEL[i+1] == JOINT_VEL[i-1]) { |
Lightvalve | 57:f4819de54e7a | 1211 | Ref_Valve_Pos_FF = (float) VALVE_CENTER; |
Lightvalve | 50:3c630b5eba9f | 1212 | } else { |
Lightvalve | 57:f4819de54e7a | 1213 | 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 | 1214 | } |
Lightvalve | 13:747daba9cf59 | 1215 | } |
Lightvalve | 13:747daba9cf59 | 1216 | break; |
Lightvalve | 13:747daba9cf59 | 1217 | } |
Lightvalve | 13:747daba9cf59 | 1218 | } |
Lightvalve | 14:8e7590227d22 | 1219 | if(REF_JOINT_VEL > max(JOINT_VEL[VALVE_POS_NUM-1], JOINT_VEL[VALVE_POS_NUM-2])) { |
Lightvalve | 33:91b17819ec30 | 1220 | Ref_Valve_Pos_FF = (float) VALVE_MAX_POS; |
Lightvalve | 14:8e7590227d22 | 1221 | } else if(REF_JOINT_VEL < min(JOINT_VEL[VALVE_POS_NUM-1], JOINT_VEL[VALVE_POS_NUM-2])) { |
Lightvalve | 33:91b17819ec30 | 1222 | Ref_Valve_Pos_FF = (float) VALVE_MIN_POS; |
Lightvalve | 13:747daba9cf59 | 1223 | } |
Lightvalve | 36:a46e63505ed8 | 1224 | |
Lightvalve | 57:f4819de54e7a | 1225 | Ref_Valve_Pos_FF = (float) VELOCITY_COMP_GAIN * 0.01f * (float) (Ref_Valve_Pos_FF - (float) VALVE_CENTER); |
Lightvalve | 13:747daba9cf59 | 1226 | return Ref_Valve_Pos_FF; |
Lightvalve | 50:3c630b5eba9f | 1227 | |
Lightvalve | 13:747daba9cf59 | 1228 | } |
jobuuu | 6:df07d3491e3a | 1229 | |
jobuuu | 6:df07d3491e3a | 1230 | |
Lightvalve | 30:8d561f16383b | 1231 | void VALVE_POS_CONTROL(float REF_VALVE_POS) |
Lightvalve | 14:8e7590227d22 | 1232 | { |
Lightvalve | 13:747daba9cf59 | 1233 | int i = 0; |
Lightvalve | 13:747daba9cf59 | 1234 | |
Lightvalve | 38:118df027d851 | 1235 | if(REF_VALVE_POS > VALVE_MAX_POS) { |
Lightvalve | 38:118df027d851 | 1236 | REF_VALVE_POS = VALVE_MAX_POS; |
Lightvalve | 38:118df027d851 | 1237 | } else if(REF_VALVE_POS < VALVE_MIN_POS) { |
Lightvalve | 38:118df027d851 | 1238 | REF_VALVE_POS = VALVE_MIN_POS; |
Lightvalve | 38:118df027d851 | 1239 | } |
Lightvalve | 38:118df027d851 | 1240 | |
Lightvalve | 89:a7b45368ea0f | 1241 | valve_pos_err = (float) (REF_VALVE_POS - value); |
Lightvalve | 13:747daba9cf59 | 1242 | valve_pos_err_diff = valve_pos_err - valve_pos_err_old; |
Lightvalve | 13:747daba9cf59 | 1243 | valve_pos_err_old = valve_pos_err; |
Lightvalve | 13:747daba9cf59 | 1244 | valve_pos_err_sum += valve_pos_err; |
Lightvalve | 89:a7b45368ea0f | 1245 | if (valve_pos_err_sum > 1000.0f) valve_pos_err_sum = 1000.0f; |
Lightvalve | 89:a7b45368ea0f | 1246 | if (valve_pos_err_sum<-1000.0f) valve_pos_err_sum = -1000.0f; |
Lightvalve | 13:747daba9cf59 | 1247 | |
Lightvalve | 13:747daba9cf59 | 1248 | 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 | 1249 | |
Lightvalve | 18:b8adf1582ea3 | 1250 | for(i=0; i<24; i++) { |
Lightvalve | 89:a7b45368ea0f | 1251 | 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 | 1252 | if(i==0) { |
Lightvalve | 48:889798ff9329 | 1253 | 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 | 1254 | } else { |
Lightvalve | 48:889798ff9329 | 1255 | 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 | 1256 | } |
Lightvalve | 13:747daba9cf59 | 1257 | break; |
Lightvalve | 13:747daba9cf59 | 1258 | } |
Lightvalve | 13:747daba9cf59 | 1259 | } |
Lightvalve | 59:f308b1656d9c | 1260 | Vout.ref = VALVE_PWM_RAW_FF + VALVE_PWM_RAW_FB; |
Lightvalve | 13:747daba9cf59 | 1261 | } |
Lightvalve | 13:747daba9cf59 | 1262 | |
Lightvalve | 14:8e7590227d22 | 1263 | #define LT_MAX_IDX 57 |
Lightvalve | 30:8d561f16383b | 1264 | 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 | 1265 | -19.0f, -18.0f, -17.0f, -16.0f, -15.0f, -14.0f, -13.0f, -12.0f, -11.0f, -10.0f, |
Lightvalve | 34:bb2ca2fc2a8e | 1266 | -9.0f, -8.0f, -7.0f, -6.0f, -5.0f, -4.0f, -3.0f, -2.0f, -1.0f, 0.0f, |
Lightvalve | 34:bb2ca2fc2a8e | 1267 | 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f, |
Lightvalve | 34:bb2ca2fc2a8e | 1268 | 11.0f, 12.0f, 13.0f, 14.0f, 15.0f, 16.0f, 17.0f, 18.0f, 19.0f, 20.0f, |
Lightvalve | 34:bb2ca2fc2a8e | 1269 | 25.0f, 30.0f, 35.0f, 40.0f, 50.0f, 60.0f, 80.0f, 100.0f |
Lightvalve | 34:bb2ca2fc2a8e | 1270 | }; // duty |
Lightvalve | 67:c2812cf26c38 | 1271 | 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 | 1272 | -150.0f, -145.0f, -145.0f, -145.0f, -135.0f, -135.0f, -135.0f, -127.5f, -127.5f, -115.0f, |
Lightvalve | 67:c2812cf26c38 | 1273 | -115.0f, -115.0F, -100.0f, -100.0f, -100.0f, -60.0f, -60.0f, -10.0f, -5.0f, 0.0f, |
Lightvalve | 67:c2812cf26c38 | 1274 | 7.5f, 14.0f, 14.0f, 14.0f, 42.5f, 42.5f, 42.5f, 80.0f, 80.0f, 105.0f, |
Lightvalve | 67:c2812cf26c38 | 1275 | 105.0f, 105.0f, 120.0f, 120.0f, 120.0f, 131.0f, 131.0f, 140.0f, 140.0f, 140.0f, |
Lightvalve | 67:c2812cf26c38 | 1276 | 155.0f, 160.0f, 170.0f, 174.0f, 182.0f, 191.0f, 212.0f, 230.0f |
Lightvalve | 34:bb2ca2fc2a8e | 1277 | }; // mV |
Lightvalve | 13:747daba9cf59 | 1278 | |
Lightvalve | 30:8d561f16383b | 1279 | float PWM_duty_byLT(float Ref_V) |
Lightvalve | 14:8e7590227d22 | 1280 | { |
Lightvalve | 30:8d561f16383b | 1281 | float PWM_duty = 0.0f; |
Lightvalve | 13:747daba9cf59 | 1282 | if(Ref_V<LT_Voltage_Output[0]) { |
Lightvalve | 30:8d561f16383b | 1283 | PWM_duty = (Ref_V-LT_Voltage_Output[0])/1.5f+LT_PWM_duty[0]; |
Lightvalve | 13:747daba9cf59 | 1284 | } else if (Ref_V>=LT_Voltage_Output[LT_MAX_IDX-1]) { |
Lightvalve | 30:8d561f16383b | 1285 | PWM_duty = (Ref_V-LT_Voltage_Output[LT_MAX_IDX-1])/1.5f+LT_PWM_duty[LT_MAX_IDX-1]; |
Lightvalve | 13:747daba9cf59 | 1286 | } else { |
Lightvalve | 13:747daba9cf59 | 1287 | int idx = 0; |
Lightvalve | 13:747daba9cf59 | 1288 | for(idx=0; idx<LT_MAX_IDX-1; idx++) { |
Lightvalve | 30:8d561f16383b | 1289 | float ini_x = LT_Voltage_Output[idx]; |
Lightvalve | 30:8d561f16383b | 1290 | float fin_x = LT_Voltage_Output[idx+1]; |
Lightvalve | 30:8d561f16383b | 1291 | float ini_y = LT_PWM_duty[idx]; |
Lightvalve | 30:8d561f16383b | 1292 | float fin_y = LT_PWM_duty[idx+1]; |
Lightvalve | 13:747daba9cf59 | 1293 | if(Ref_V>=ini_x && Ref_V<fin_x) { |
Lightvalve | 13:747daba9cf59 | 1294 | PWM_duty = (fin_y-ini_y)/(fin_x-ini_x)*(Ref_V-ini_x) + ini_y; |
Lightvalve | 13:747daba9cf59 | 1295 | break; |
Lightvalve | 13:747daba9cf59 | 1296 | } |
Lightvalve | 13:747daba9cf59 | 1297 | } |
Lightvalve | 13:747daba9cf59 | 1298 | } |
Lightvalve | 14:8e7590227d22 | 1299 | |
Lightvalve | 13:747daba9cf59 | 1300 | return PWM_duty; |
Lightvalve | 13:747daba9cf59 | 1301 | } |
jobuuu | 6:df07d3491e3a | 1302 | |
Lightvalve | 57:f4819de54e7a | 1303 | |
Lightvalve | 57:f4819de54e7a | 1304 | |
Lightvalve | 57:f4819de54e7a | 1305 | |
Lightvalve | 57:f4819de54e7a | 1306 | |
jobuuu | 2:a1c0a37df760 | 1307 | /******************************************************************************* |
jobuuu | 2:a1c0a37df760 | 1308 | TIMER INTERRUPT |
jobuuu | 2:a1c0a37df760 | 1309 | *******************************************************************************/ |
jobuuu | 2:a1c0a37df760 | 1310 | |
Lightvalve | 51:b46bed7fec80 | 1311 | float FREQ_TMR4 = (float)FREQ_20k; |
Lightvalve | 51:b46bed7fec80 | 1312 | float DT_TMR4 = (float)DT_20k; |
Lightvalve | 57:f4819de54e7a | 1313 | long CNT_TMR4 = 0; |
Lightvalve | 57:f4819de54e7a | 1314 | int TMR4_FREQ_10k = (int)FREQ_10k; |
jobuuu | 1:e04e563be5ce | 1315 | extern "C" void TIM4_IRQHandler(void) |
jobuuu | 1:e04e563be5ce | 1316 | { |
Lightvalve | 19:23b7c1ad8683 | 1317 | if (TIM4->SR & TIM_SR_UIF ) { |
Lightvalve | 21:e5f1a43ea6f9 | 1318 | |
Lightvalve | 21:e5f1a43ea6f9 | 1319 | /******************************************************* |
Lightvalve | 21:e5f1a43ea6f9 | 1320 | *** Sensor Read & Data Handling |
Lightvalve | 21:e5f1a43ea6f9 | 1321 | ********************************************************/ |
Lightvalve | 13:747daba9cf59 | 1322 | |
Lightvalve | 57:f4819de54e7a | 1323 | //Encoder |
Lightvalve | 57:f4819de54e7a | 1324 | if (CNT_TMR4 % (int) ((int) FREQ_TMR4/TMR4_FREQ_10k) == 0) { |
Lightvalve | 57:f4819de54e7a | 1325 | ENC_UPDATE(); |
Lightvalve | 57:f4819de54e7a | 1326 | } |
Lightvalve | 61:bc8c8270f0ab | 1327 | |
Lightvalve | 61:bc8c8270f0ab | 1328 | ADC1->CR2 |= 0x40000000; |
Lightvalve | 58:2eade98630e2 | 1329 | if (SENSING_MODE == 0) { |
Lightvalve | 58:2eade98630e2 | 1330 | // Torque Sensing (0~210)bar ============================================= |
Lightvalve | 58:2eade98630e2 | 1331 | float pres_A_new = (((float) ADC1->DR) - 2047.5f); |
Lightvalve | 58:2eade98630e2 | 1332 | double alpha_update_ft = 1.0f / (1.0f + FREQ_TMR4 / (2.0f * 3.14f * 100.0f)); // f_cutoff : 200Hz |
Lightvalve | 58:2eade98630e2 | 1333 | pres_A.sen = (1.0f - alpha_update_ft) * pres_A.sen + alpha_update_ft * pres_A_new; |
Lightvalve | 67:c2812cf26c38 | 1334 | torq.sen = -pres_A.sen / TORQUE_SENSOR_PULSE_PER_TORQUE; |
Lightvalve | 67:c2812cf26c38 | 1335 | |
Lightvalve | 67:c2812cf26c38 | 1336 | |
Lightvalve | 67:c2812cf26c38 | 1337 | // float alpha_update_pres_A = 1.0f/(1.0f + FREQ_TMR4/(2.0f*3.14f*100.0f)); |
Lightvalve | 67:c2812cf26c38 | 1338 | //// float pres_A_new = ((float)ADC1->DR - PRES_A_NULL) / PRES_SENSOR_A_PULSE_PER_BAR; |
Lightvalve | 67:c2812cf26c38 | 1339 | // float pres_A_new = ((float)ADC1->DR); |
Lightvalve | 67:c2812cf26c38 | 1340 | // pres_A.sen = pres_A.sen*(1.0f-alpha_update_pres_A)+pres_A_new*(alpha_update_pres_A); |
Lightvalve | 67:c2812cf26c38 | 1341 | // torq.sen = - (pres_A.sen-2048.0f); //pulse -2047~2047 |
Lightvalve | 67:c2812cf26c38 | 1342 | |
Lightvalve | 17:1865016ca2e7 | 1343 | |
Lightvalve | 58:2eade98630e2 | 1344 | } else if (SENSING_MODE == 1) { |
Lightvalve | 58:2eade98630e2 | 1345 | // Pressure Sensing (0~210)bar ============================================= |
Lightvalve | 58:2eade98630e2 | 1346 | float pres_A_new = (((float)ADC1->DR) - PRES_A_NULL); |
Lightvalve | 58:2eade98630e2 | 1347 | float pres_B_new = (((float)ADC2->DR) - PRES_B_NULL); |
Lightvalve | 58:2eade98630e2 | 1348 | double alpha_update_pres = 1.0f / (1.0f + FREQ_TMR4 / (2.0f * 3.14f * 200.0f)); // f_cutoff : 500Hz |
Lightvalve | 58:2eade98630e2 | 1349 | pres_A.sen = (1.0f - alpha_update_pres) * pres_A.sen + alpha_update_pres * pres_A_new; |
Lightvalve | 58:2eade98630e2 | 1350 | pres_B.sen = (1.0f - alpha_update_pres) * pres_B.sen + alpha_update_pres * pres_B_new; |
Lightvalve | 58:2eade98630e2 | 1351 | CUR_PRES_A_BAR = pres_A.sen / PRES_SENSOR_A_PULSE_PER_BAR; |
Lightvalve | 58:2eade98630e2 | 1352 | CUR_PRES_B_BAR = pres_B.sen / PRES_SENSOR_B_PULSE_PER_BAR; |
Lightvalve | 50:3c630b5eba9f | 1353 | |
Lightvalve | 58:2eade98630e2 | 1354 | if ((OPERATING_MODE & 0x01) == 0) { // Rotary Actuator |
Lightvalve | 58:2eade98630e2 | 1355 | 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 | 1356 | } else if ((OPERATING_MODE & 0x01) == 1) { // Linear Actuator |
Lightvalve | 58:2eade98630e2 | 1357 | 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 | 1358 | } |
Lightvalve | 58:2eade98630e2 | 1359 | } |
Lightvalve | 61:bc8c8270f0ab | 1360 | |
Lightvalve | 58:2eade98630e2 | 1361 | // //Pressure sensor A |
Lightvalve | 58:2eade98630e2 | 1362 | // ADC1->CR2 |= 0x40000000; // adc _ 12bit |
Lightvalve | 58:2eade98630e2 | 1363 | // //while((ADC1->SR & 0b10)); |
Lightvalve | 58:2eade98630e2 | 1364 | // float alpha_update_pres_A = 1.0f/(1.0f + FREQ_TMR4/(2.0f*3.14f*100.0f)); |
Lightvalve | 58:2eade98630e2 | 1365 | // float pres_A_new = ((float)ADC1->DR); |
Lightvalve | 58:2eade98630e2 | 1366 | // pres_A.sen = pres_A.sen*(1.0f-alpha_update_pres_A)+pres_A_new*(alpha_update_pres_A); |
Lightvalve | 58:2eade98630e2 | 1367 | // 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 | 1368 | // |
Lightvalve | 58:2eade98630e2 | 1369 | // |
Lightvalve | 58:2eade98630e2 | 1370 | // //Pressure sensor B |
Lightvalve | 58:2eade98630e2 | 1371 | // float alpha_update_pres_B = 1.0f/(1.0f + FREQ_TMR4/(2.0f*3.14f*100.0f)); |
Lightvalve | 58:2eade98630e2 | 1372 | // float pres_B_new = ((float)ADC2->DR); |
Lightvalve | 58:2eade98630e2 | 1373 | // pres_B.sen = pres_B.sen*(1.0f-alpha_update_pres_B)+pres_B_new*(alpha_update_pres_B); |
Lightvalve | 58:2eade98630e2 | 1374 | // //torq.sen = pres_A.sen * (float) PISTON_AREA_A - pres_B.sen * (float) PISTON_AREA_B; |
Lightvalve | 17:1865016ca2e7 | 1375 | |
Lightvalve | 17:1865016ca2e7 | 1376 | |
Lightvalve | 21:e5f1a43ea6f9 | 1377 | //Current |
Lightvalve | 21:e5f1a43ea6f9 | 1378 | //ADC3->CR2 |= 0x40000000; // adc _ 12bit |
Lightvalve | 25:3e6b574cab5c | 1379 | //int raw_cur = ADC3->DR; |
Lightvalve | 21:e5f1a43ea6f9 | 1380 | //while((ADC3->SR & 0b10)); |
Lightvalve | 51:b46bed7fec80 | 1381 | float alpha_update_cur = 1.0f/(1.0f + FREQ_TMR4/(2.0f*3.14f*500.0f)); // f_cutoff : 500Hz |
Lightvalve | 30:8d561f16383b | 1382 | float cur_new = ((float)ADC3->DR-2048.0f)*20.0f/4096.0f; // unit : mA |
Lightvalve | 30:8d561f16383b | 1383 | cur.sen=cur.sen*(1.0f-alpha_update_cur)+cur_new*(alpha_update_cur); |
Lightvalve | 25:3e6b574cab5c | 1384 | //cur.sen = raw_cur; |
Lightvalve | 58:2eade98630e2 | 1385 | |
Lightvalve | 57:f4819de54e7a | 1386 | CNT_TMR4++; |
Lightvalve | 21:e5f1a43ea6f9 | 1387 | } |
Lightvalve | 11:82d8768d7351 | 1388 | TIM4->SR = 0x0; // reset the status register |
Lightvalve | 11:82d8768d7351 | 1389 | } |
Lightvalve | 19:23b7c1ad8683 | 1390 | |
Lightvalve | 19:23b7c1ad8683 | 1391 | |
Lightvalve | 18:b8adf1582ea3 | 1392 | int j =0; |
Lightvalve | 54:647072f5307a | 1393 | float FREQ_TMR3 = (float)FREQ_5k; |
Lightvalve | 48:889798ff9329 | 1394 | float DT_TMR3 = (float)DT_5k; |
Lightvalve | 45:35fa6884d0c6 | 1395 | int cnt_trans = 0; |
Lightvalve | 48:889798ff9329 | 1396 | double VALVE_POS_RAW_FORCE_FB_LOGGING = 0.0f; |
Lightvalve | 48:889798ff9329 | 1397 | |
Lightvalve | 11:82d8768d7351 | 1398 | extern "C" void TIM3_IRQHandler(void) |
Lightvalve | 21:e5f1a43ea6f9 | 1399 | { |
Lightvalve | 19:23b7c1ad8683 | 1400 | if (TIM3->SR & TIM_SR_UIF ) { |
Lightvalve | 57:f4819de54e7a | 1401 | |
Lightvalve | 57:f4819de54e7a | 1402 | if (((OPERATING_MODE&0b110)>>1) == 0) { |
Lightvalve | 57:f4819de54e7a | 1403 | K_v = 0.4f; // Moog (LPM >> mA) , 100bar |
Lightvalve | 57:f4819de54e7a | 1404 | mV_PER_mA = 500.0f; // 5000mV/10mA |
Lightvalve | 57:f4819de54e7a | 1405 | mV_PER_pulse = 0.5f; // 5000mV/10000pulse |
Lightvalve | 57:f4819de54e7a | 1406 | mA_PER_pulse = 0.001f; // 10mA/10000pulse |
Lightvalve | 57:f4819de54e7a | 1407 | } else if (((OPERATING_MODE&0b110)>>1) == 1) { |
Lightvalve | 57:f4819de54e7a | 1408 | K_v = 0.5f; // KNR (LPM >> mA) , 100bar |
Lightvalve | 57:f4819de54e7a | 1409 | mV_PER_mA = 166.6666f; // 5000mV/30mA |
Lightvalve | 57:f4819de54e7a | 1410 | mV_PER_pulse = 0.5f; // 5000mV/10000pulse |
Lightvalve | 57:f4819de54e7a | 1411 | mA_PER_pulse = 0.003f; // 30mA/10000pulse |
Lightvalve | 57:f4819de54e7a | 1412 | } |
Lightvalve | 50:3c630b5eba9f | 1413 | |
Lightvalve | 50:3c630b5eba9f | 1414 | if(MODE_POS_FT_TRANS == 1) { |
Lightvalve | 48:889798ff9329 | 1415 | alpha_trans = (float)(1.0f - cos(3.141592f * (float)cnt_trans * DT_TMR3 /3.0f))/2.0f; |
Lightvalve | 45:35fa6884d0c6 | 1416 | cnt_trans++; |
Lightvalve | 46:2694daea349b | 1417 | torq.err_sum = 0; |
Lightvalve | 48:889798ff9329 | 1418 | if((float)cnt_trans * DT_TMR3 > 3.0f) |
Lightvalve | 45:35fa6884d0c6 | 1419 | MODE_POS_FT_TRANS = 2; |
Lightvalve | 50:3c630b5eba9f | 1420 | } else if(MODE_POS_FT_TRANS == 3) { |
Lightvalve | 48:889798ff9329 | 1421 | alpha_trans = (float)(1.0f + cos(3.141592f * (float)cnt_trans * DT_TMR3 /3.0f))/2.0f; |
Lightvalve | 45:35fa6884d0c6 | 1422 | cnt_trans++; |
Lightvalve | 46:2694daea349b | 1423 | torq.err_sum = 0; |
Lightvalve | 48:889798ff9329 | 1424 | if((float) cnt_trans * DT_TMR3 > 3.0f ) |
Lightvalve | 45:35fa6884d0c6 | 1425 | MODE_POS_FT_TRANS = 0; |
Lightvalve | 50:3c630b5eba9f | 1426 | } else if(MODE_POS_FT_TRANS == 2) { |
Lightvalve | 58:2eade98630e2 | 1427 | alpha_trans = 1.0f; |
Lightvalve | 45:35fa6884d0c6 | 1428 | cnt_trans = 0; |
Lightvalve | 50:3c630b5eba9f | 1429 | } else { |
Lightvalve | 58:2eade98630e2 | 1430 | alpha_trans = 0.0f; |
Lightvalve | 45:35fa6884d0c6 | 1431 | cnt_trans = 0; |
Lightvalve | 45:35fa6884d0c6 | 1432 | } |
Lightvalve | 45:35fa6884d0c6 | 1433 | |
Lightvalve | 50:3c630b5eba9f | 1434 | |
Lightvalve | 57:f4819de54e7a | 1435 | int UTILITY_MODE = 0; |
Lightvalve | 57:f4819de54e7a | 1436 | int CONTROL_MODE = 0; |
Lightvalve | 58:2eade98630e2 | 1437 | |
Lightvalve | 57:f4819de54e7a | 1438 | if (CONTROL_UTILITY_MODE >= 20 || CONTROL_UTILITY_MODE == 0) { |
Lightvalve | 57:f4819de54e7a | 1439 | UTILITY_MODE = CONTROL_UTILITY_MODE; |
Lightvalve | 57:f4819de54e7a | 1440 | CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 57:f4819de54e7a | 1441 | } else { |
Lightvalve | 57:f4819de54e7a | 1442 | CONTROL_MODE = CONTROL_UTILITY_MODE; |
Lightvalve | 57:f4819de54e7a | 1443 | UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 57:f4819de54e7a | 1444 | } |
Lightvalve | 56:6f50d9d3bfee | 1445 | |
Lightvalve | 56:6f50d9d3bfee | 1446 | |
Lightvalve | 56:6f50d9d3bfee | 1447 | |
Lightvalve | 57:f4819de54e7a | 1448 | // UTILITY MODE ------------------------------------------------------------ |
Lightvalve | 56:6f50d9d3bfee | 1449 | |
Lightvalve | 57:f4819de54e7a | 1450 | switch (UTILITY_MODE) { |
Lightvalve | 57:f4819de54e7a | 1451 | case MODE_NO_ACT: { |
Lightvalve | 13:747daba9cf59 | 1452 | break; |
Lightvalve | 13:747daba9cf59 | 1453 | } |
Lightvalve | 14:8e7590227d22 | 1454 | |
Lightvalve | 14:8e7590227d22 | 1455 | case MODE_TORQUE_SENSOR_NULLING: { |
Lightvalve | 13:747daba9cf59 | 1456 | // DAC Voltage reference set |
Lightvalve | 14:8e7590227d22 | 1457 | if (TMR3_COUNT_TORQUE_NULL < TMR_FREQ_5k * 2) { |
Lightvalve | 15:bd0d12728506 | 1458 | CUR_TORQUE_sum += torq.sen; |
Lightvalve | 19:23b7c1ad8683 | 1459 | |
Lightvalve | 14:8e7590227d22 | 1460 | if (TMR3_COUNT_TORQUE_NULL % 10 == 0) { |
Lightvalve | 30:8d561f16383b | 1461 | CUR_TORQUE_mean = CUR_TORQUE_sum / 10.0f; |
Lightvalve | 13:747daba9cf59 | 1462 | CUR_TORQUE_sum = 0; |
Lightvalve | 14:8e7590227d22 | 1463 | |
Lightvalve | 84:c355d3e52bf1 | 1464 | TORQUE_VREF += 0.000003f * (0.0f - CUR_TORQUE_mean); |
Lightvalve | 19:23b7c1ad8683 | 1465 | |
Lightvalve | 30:8d561f16383b | 1466 | if (TORQUE_VREF > 3.3f) TORQUE_VREF = 3.3f; |
Lightvalve | 59:f308b1656d9c | 1467 | if (TORQUE_VREF < 0.0f) TORQUE_VREF = 0.0f; |
Lightvalve | 50:3c630b5eba9f | 1468 | |
Lightvalve | 16:903b5a4433b4 | 1469 | //spi_eeprom_write(RID_TORQUE_SENSOR_VREF, (int16_t) (TORQUE_VREF * 1000.0)); |
Lightvalve | 30:8d561f16383b | 1470 | dac_1 = TORQUE_VREF / 3.3f; |
Lightvalve | 13:747daba9cf59 | 1471 | } |
Lightvalve | 13:747daba9cf59 | 1472 | } else { |
Lightvalve | 58:2eade98630e2 | 1473 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 14:8e7590227d22 | 1474 | TMR3_COUNT_TORQUE_NULL = 0; |
Lightvalve | 13:747daba9cf59 | 1475 | CUR_TORQUE_sum = 0; |
Lightvalve | 13:747daba9cf59 | 1476 | CUR_TORQUE_mean = 0; |
Lightvalve | 19:23b7c1ad8683 | 1477 | |
Lightvalve | 170:42c938a40313 | 1478 | // ROM_RESET_DATA(); |
Lightvalve | 170:42c938a40313 | 1479 | spi_eeprom_write(RID_TORQUE_SENSOR_VREF, (int16_t) (TORQUE_VREF * 1000.0f)); |
Lightvalve | 19:23b7c1ad8683 | 1480 | |
Lightvalve | 30:8d561f16383b | 1481 | dac_1 = TORQUE_VREF / 3.3f; |
Lightvalve | 14:8e7590227d22 | 1482 | |
Lightvalve | 13:747daba9cf59 | 1483 | } |
Lightvalve | 14:8e7590227d22 | 1484 | TMR3_COUNT_TORQUE_NULL++; |
Lightvalve | 13:747daba9cf59 | 1485 | break; |
Lightvalve | 19:23b7c1ad8683 | 1486 | } |
Lightvalve | 14:8e7590227d22 | 1487 | |
Lightvalve | 50:3c630b5eba9f | 1488 | // case MODE_VALVE_NULLING_AND_DEADZONE_SETTING: { |
Lightvalve | 50:3c630b5eba9f | 1489 | // if (TMR3_COUNT_DEADZONE == 0) { |
Lightvalve | 50:3c630b5eba9f | 1490 | // if (pos_plus_end == pos_minus_end) need_enc_init = true; |
Lightvalve | 50:3c630b5eba9f | 1491 | // else temp_time = 0; |
Lightvalve | 50:3c630b5eba9f | 1492 | // } |
Lightvalve | 50:3c630b5eba9f | 1493 | // if (need_enc_init) { |
Lightvalve | 50:3c630b5eba9f | 1494 | // if (TMR3_COUNT_DEADZONE < (int) (0.5f * (float) TMR_FREQ_5k)) { |
Lightvalve | 50:3c630b5eba9f | 1495 | // V_out = VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 50:3c630b5eba9f | 1496 | // pos_plus_end = pos.sen; |
Lightvalve | 50:3c630b5eba9f | 1497 | // } else if (TMR3_COUNT_DEADZONE < TMR_FREQ_5k) { |
Lightvalve | 50:3c630b5eba9f | 1498 | // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 50:3c630b5eba9f | 1499 | // pos_minus_end = pos.sen; |
Lightvalve | 50:3c630b5eba9f | 1500 | // } else if (TMR3_COUNT_DEADZONE == TMR_FREQ_5k) need_enc_init = false; |
Lightvalve | 50:3c630b5eba9f | 1501 | // temp_time = TMR_FREQ_5k; |
Lightvalve | 50:3c630b5eba9f | 1502 | // } |
Lightvalve | 50:3c630b5eba9f | 1503 | // |
Lightvalve | 50:3c630b5eba9f | 1504 | // if (temp_time <= TMR3_COUNT_DEADZONE && TMR3_COUNT_DEADZONE < (temp_time + TMR_FREQ_5k)) { |
Lightvalve | 50:3c630b5eba9f | 1505 | // 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 | 1506 | // VALVE_CENTER = VALVE_DEADZONE_PLUS = VALVE_DEADZONE_MINUS = 0; |
Lightvalve | 50:3c630b5eba9f | 1507 | // |
Lightvalve | 50:3c630b5eba9f | 1508 | // } else if (temp_time <= TMR3_COUNT_DEADZONE && TMR3_COUNT_DEADZONE < (temp_time + (int) (1.9f * (float) TMR_FREQ_5k))) { |
Lightvalve | 50:3c630b5eba9f | 1509 | // V_out = 0; |
Lightvalve | 50:3c630b5eba9f | 1510 | // CUR_VELOCITY_sum += CUR_VELOCITY; |
Lightvalve | 50:3c630b5eba9f | 1511 | // } else if (TMR3_COUNT_DEADZONE == (temp_time + 2 * TMR_FREQ_5k)) { |
Lightvalve | 50:3c630b5eba9f | 1512 | // if (CUR_VELOCITY_sum == 0) DZ_dir = 1; |
Lightvalve | 50:3c630b5eba9f | 1513 | // else if (CUR_VELOCITY_sum > 0) DZ_dir = 1; |
Lightvalve | 50:3c630b5eba9f | 1514 | // else if (CUR_VELOCITY_sum < 0) DZ_dir = -1; |
Lightvalve | 50:3c630b5eba9f | 1515 | // else DZ_temp_cnt2 = DZ_end; |
Lightvalve | 50:3c630b5eba9f | 1516 | // CUR_VELOCITY_sum = 0; |
Lightvalve | 50:3c630b5eba9f | 1517 | // } else if (TMR3_COUNT_DEADZONE > (temp_time + 2 * TMR_FREQ_5k)) { |
Lightvalve | 50:3c630b5eba9f | 1518 | // if (TMR3_COUNT_DEADZONE > (temp_time + 10 * TMR_FREQ_5k)) DZ_temp_cnt2 = DZ_end; |
Lightvalve | 50:3c630b5eba9f | 1519 | // |
Lightvalve | 50:3c630b5eba9f | 1520 | // // Position of Dead Zone |
Lightvalve | 50:3c630b5eba9f | 1521 | // // (CUR_VELOCITY < 0) (CUR_VELOCITY == 0) (CUR_VELOCITY > 0) |
Lightvalve | 50:3c630b5eba9f | 1522 | // // | / | / |/ |
Lightvalve | 50:3c630b5eba9f | 1523 | // // | ______/ ___|___/ ______/| |
Lightvalve | 50:3c630b5eba9f | 1524 | // // |/ / | / | |
Lightvalve | 50:3c630b5eba9f | 1525 | // // /| / | / | |
Lightvalve | 50:3c630b5eba9f | 1526 | // // 0V 0V 0V |
Lightvalve | 50:3c630b5eba9f | 1527 | // |
Lightvalve | 50:3c630b5eba9f | 1528 | // if (DZ_temp_cnt2 < DZ_end) { |
Lightvalve | 50:3c630b5eba9f | 1529 | // if (TMR3_COUNT_DEADZONE % 20 != 0) { |
Lightvalve | 50:3c630b5eba9f | 1530 | // CUR_VELOCITY_sum += CUR_VELOCITY; |
Lightvalve | 50:3c630b5eba9f | 1531 | // } else { |
Lightvalve | 50:3c630b5eba9f | 1532 | // V_out -= DZ_dir; |
Lightvalve | 50:3c630b5eba9f | 1533 | // if (CUR_VELOCITY_sum * DZ_dir < 0) DZ_temp_cnt++; |
Lightvalve | 50:3c630b5eba9f | 1534 | // CUR_VELOCITY_sum = 0; |
Lightvalve | 50:3c630b5eba9f | 1535 | // } |
Lightvalve | 50:3c630b5eba9f | 1536 | // if (DZ_temp_cnt == 5) { |
Lightvalve | 50:3c630b5eba9f | 1537 | // if (DZ_dir >= 0) VALVE_DEADZONE_MINUS = (int16_t) V_out; |
Lightvalve | 50:3c630b5eba9f | 1538 | // else VALVE_DEADZONE_PLUS = (int16_t) V_out; |
Lightvalve | 50:3c630b5eba9f | 1539 | // DZ_dir = -DZ_dir; |
Lightvalve | 50:3c630b5eba9f | 1540 | // DZ_temp_cnt = 0; |
Lightvalve | 50:3c630b5eba9f | 1541 | // DZ_temp_cnt2++; |
Lightvalve | 50:3c630b5eba9f | 1542 | // } |
Lightvalve | 50:3c630b5eba9f | 1543 | // } else { |
Lightvalve | 50:3c630b5eba9f | 1544 | // TMR3_COUNT_DEADZONE = -1; |
Lightvalve | 50:3c630b5eba9f | 1545 | // VALVE_CENTER = VALVE_DEADZONE_PLUS / 2 + VALVE_DEADZONE_MINUS / 2; |
Lightvalve | 50:3c630b5eba9f | 1546 | // if (VALVE_DEADZONE_PLUS < VALVE_DEADZONE_MINUS) { |
Lightvalve | 50:3c630b5eba9f | 1547 | // VALVE_DEADZONE_PLUS = VALVE_CENTER; |
Lightvalve | 50:3c630b5eba9f | 1548 | // VALVE_DEADZONE_MINUS = VALVE_CENTER; |
Lightvalve | 50:3c630b5eba9f | 1549 | // } |
Lightvalve | 50:3c630b5eba9f | 1550 | // V_out = 0; |
Lightvalve | 50:3c630b5eba9f | 1551 | // |
Lightvalve | 50:3c630b5eba9f | 1552 | // ROM_RESET_DATA(); |
Lightvalve | 50:3c630b5eba9f | 1553 | // |
Lightvalve | 50:3c630b5eba9f | 1554 | // //spi_eeprom_write(RID_VALVE_DEADZONE_PLUS, VALVE_DEADZONE_PLUS); |
Lightvalve | 50:3c630b5eba9f | 1555 | // //spi_eeprom_write(RID_VALVE_DEADZONE_MINUS, VALVE_DEADZONE_MINUS); |
Lightvalve | 50:3c630b5eba9f | 1556 | // |
Lightvalve | 50:3c630b5eba9f | 1557 | // CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 50:3c630b5eba9f | 1558 | // DZ_temp_cnt2 = 0; |
Lightvalve | 50:3c630b5eba9f | 1559 | // } |
Lightvalve | 50:3c630b5eba9f | 1560 | // } |
Lightvalve | 50:3c630b5eba9f | 1561 | // TMR3_COUNT_DEADZONE++; |
Lightvalve | 50:3c630b5eba9f | 1562 | // break; |
Lightvalve | 50:3c630b5eba9f | 1563 | // } |
Lightvalve | 14:8e7590227d22 | 1564 | |
Lightvalve | 14:8e7590227d22 | 1565 | case MODE_FIND_HOME: { |
Lightvalve | 29:69f3f5445d6d | 1566 | if (FINDHOME_STAGE == FINDHOME_INIT) { |
Lightvalve | 14:8e7590227d22 | 1567 | cnt_findhome = 0; |
Lightvalve | 14:8e7590227d22 | 1568 | cnt_vel_findhome = 0; |
Lightvalve | 14:8e7590227d22 | 1569 | //REFERENCE_MODE = MODE_REF_NO_ACT; // Stop taking reference data from PODO |
Lightvalve | 59:f308b1656d9c | 1570 | pos.ref = pos.sen; |
Lightvalve | 59:f308b1656d9c | 1571 | vel.ref = 0.0f; |
Lightvalve | 29:69f3f5445d6d | 1572 | FINDHOME_STAGE = FINDHOME_GOTOLIMIT; |
Lightvalve | 29:69f3f5445d6d | 1573 | } else if (FINDHOME_STAGE == FINDHOME_GOTOLIMIT) { |
Lightvalve | 67:c2812cf26c38 | 1574 | int cnt_check_enc = (TMR_FREQ_5k/20); |
Lightvalve | 29:69f3f5445d6d | 1575 | if(cnt_findhome%cnt_check_enc == 0) { |
Lightvalve | 29:69f3f5445d6d | 1576 | FINDHOME_POSITION = pos.sen; |
Lightvalve | 29:69f3f5445d6d | 1577 | FINDHOME_VELOCITY = FINDHOME_POSITION - FINDHOME_POSITION_OLD; |
Lightvalve | 29:69f3f5445d6d | 1578 | FINDHOME_POSITION_OLD = FINDHOME_POSITION; |
Lightvalve | 29:69f3f5445d6d | 1579 | } |
Lightvalve | 29:69f3f5445d6d | 1580 | cnt_findhome++; |
Lightvalve | 14:8e7590227d22 | 1581 | |
Lightvalve | 29:69f3f5445d6d | 1582 | if (abs(FINDHOME_VELOCITY) <= 1) { |
Lightvalve | 29:69f3f5445d6d | 1583 | cnt_vel_findhome = cnt_vel_findhome + 1; |
Lightvalve | 29:69f3f5445d6d | 1584 | } else { |
Lightvalve | 29:69f3f5445d6d | 1585 | cnt_vel_findhome = 0; |
Lightvalve | 29:69f3f5445d6d | 1586 | } |
Lightvalve | 19:23b7c1ad8683 | 1587 | |
Lightvalve | 57:f4819de54e7a | 1588 | if ((cnt_vel_findhome < 3*TMR_FREQ_5k) && cnt_findhome < 10*TMR_FREQ_5k) { // wait for 3sec |
Lightvalve | 29:69f3f5445d6d | 1589 | //REFERENCE_MODE = MODE_REF_NO_ACT; |
Lightvalve | 67:c2812cf26c38 | 1590 | if (HOMEPOS_OFFSET > 0) pos.ref = pos.ref + 12.0f; |
Lightvalve | 67:c2812cf26c38 | 1591 | else pos.ref = pos.ref - 12.0f; |
Lightvalve | 61:bc8c8270f0ab | 1592 | |
Lightvalve | 59:f308b1656d9c | 1593 | // pos.err = pos.ref_home_pos - pos.sen; |
Lightvalve | 59:f308b1656d9c | 1594 | // float VALVE_POS_RAW_POS_FB = 0.0f; |
Lightvalve | 59:f308b1656d9c | 1595 | // VALVE_POS_RAW_POS_FB = (float) P_GAIN_JOINT_POSITION * pos.err/(float) ENC_PULSE_PER_POSITION * 0.01f; |
Lightvalve | 59:f308b1656d9c | 1596 | // valve_pos.ref = VALVE_POS_RAW_POS_FB + (float) VALVE_CENTER; |
Lightvalve | 59:f308b1656d9c | 1597 | // VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 61:bc8c8270f0ab | 1598 | |
Lightvalve | 59:f308b1656d9c | 1599 | CONTROL_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 59:f308b1656d9c | 1600 | alpha_trans = 0.0f; |
Lightvalve | 61:bc8c8270f0ab | 1601 | |
Lightvalve | 34:bb2ca2fc2a8e | 1602 | |
Lightvalve | 29:69f3f5445d6d | 1603 | } else { |
Lightvalve | 29:69f3f5445d6d | 1604 | ENC_SET(HOMEPOS_OFFSET); |
Lightvalve | 67:c2812cf26c38 | 1605 | // ENC_SET_ZERO(); |
Lightvalve | 29:69f3f5445d6d | 1606 | INIT_REF_POS = HOMEPOS_OFFSET; |
Lightvalve | 29:69f3f5445d6d | 1607 | REF_POSITION = 0; |
Lightvalve | 29:69f3f5445d6d | 1608 | REF_VELOCITY = 0; |
Lightvalve | 29:69f3f5445d6d | 1609 | FINDHOME_POSITION = 0; |
Lightvalve | 29:69f3f5445d6d | 1610 | FINDHOME_POSITION_OLD = 0; |
Lightvalve | 29:69f3f5445d6d | 1611 | FINDHOME_VELOCITY = 0; |
Lightvalve | 29:69f3f5445d6d | 1612 | cnt_findhome = 0; |
Lightvalve | 29:69f3f5445d6d | 1613 | cnt_vel_findhome = 0; |
Lightvalve | 29:69f3f5445d6d | 1614 | FINDHOME_STAGE = FINDHOME_ZEROPOSE; |
Lightvalve | 67:c2812cf26c38 | 1615 | |
Lightvalve | 67:c2812cf26c38 | 1616 | |
Lightvalve | 67:c2812cf26c38 | 1617 | cnt_findhome = 0; |
Lightvalve | 67:c2812cf26c38 | 1618 | pos.ref = 0.0f; |
Lightvalve | 67:c2812cf26c38 | 1619 | vel.ref = 0.0f; |
Lightvalve | 67:c2812cf26c38 | 1620 | pos.ref_home_pos = 0.0f; |
Lightvalve | 67:c2812cf26c38 | 1621 | vel.ref_home_pos = 0.0f; |
Lightvalve | 67:c2812cf26c38 | 1622 | //FINDHOME_STAGE = FINDHOME_INIT; |
Lightvalve | 67:c2812cf26c38 | 1623 | //CONTROL_UTILITY_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 169:645207e160ca | 1624 | |
Lightvalve | 169:645207e160ca | 1625 | |
Lightvalve | 29:69f3f5445d6d | 1626 | } |
Lightvalve | 29:69f3f5445d6d | 1627 | } else if (FINDHOME_STAGE == FINDHOME_ZEROPOSE) { |
Lightvalve | 29:69f3f5445d6d | 1628 | int T_move = 2*TMR_FREQ_5k; |
Lightvalve | 59:f308b1656d9c | 1629 | 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 | 1630 | //pos.ref = 0.0f; |
Lightvalve | 59:f308b1656d9c | 1631 | vel.ref = 0.0f; |
Lightvalve | 169:645207e160ca | 1632 | |
Lightvalve | 29:69f3f5445d6d | 1633 | // input for position control |
Lightvalve | 169:645207e160ca | 1634 | |
Lightvalve | 67:c2812cf26c38 | 1635 | // CONTROL_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 67:c2812cf26c38 | 1636 | alpha_trans = 0.0f; |
Lightvalve | 61:bc8c8270f0ab | 1637 | |
Lightvalve | 67:c2812cf26c38 | 1638 | double torq_ref = 0.0f; |
Lightvalve | 67:c2812cf26c38 | 1639 | pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm] |
Lightvalve | 67:c2812cf26c38 | 1640 | vel.err = (0.0f - vel.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm/s] |
Lightvalve | 67:c2812cf26c38 | 1641 | pos.err_sum += pos.err/(float) TMR_FREQ_5k; //[mm] |
Lightvalve | 67:c2812cf26c38 | 1642 | |
Lightvalve | 67:c2812cf26c38 | 1643 | if (((OPERATING_MODE&0b110)>>1) == 0 || ((OPERATING_MODE&0b110)>>1) == 1) { |
Lightvalve | 67:c2812cf26c38 | 1644 | |
Lightvalve | 67:c2812cf26c38 | 1645 | double I_REF_POS = 0.0f; |
Lightvalve | 67:c2812cf26c38 | 1646 | double I_REF_FORCE_FB = 0.0f; // I_REF by Force Feedback |
Lightvalve | 67:c2812cf26c38 | 1647 | double I_REF_VC = 0.0f; // I_REF for velocity compensation |
Lightvalve | 67:c2812cf26c38 | 1648 | |
Lightvalve | 67:c2812cf26c38 | 1649 | double temp_vel_pos = 0.0f; |
Lightvalve | 67:c2812cf26c38 | 1650 | double temp_vel_torq = 0.0f; |
Lightvalve | 67:c2812cf26c38 | 1651 | double wn_Pos = 2.0f * PI * 5.0f; // f_cut : 5Hz Position Control |
Lightvalve | 67:c2812cf26c38 | 1652 | |
Lightvalve | 69:3995ffeaa786 | 1653 | if ((OPERATING_MODE & 0x01) == 0) { // Rotary Mode |
Lightvalve | 67:c2812cf26c38 | 1654 | 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 | 1655 | // L when P-gain = 100, f_cut = 10Hz L feedforward velocity |
Lightvalve | 69:3995ffeaa786 | 1656 | } else if ((OPERATING_MODE & 0x01) == 1) { |
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); // mm/s |
Lightvalve | 67:c2812cf26c38 | 1658 | // L when P-gain = 100, f_cut = 10Hz L feedforward velocity |
Lightvalve | 67:c2812cf26c38 | 1659 | } |
Lightvalve | 67:c2812cf26c38 | 1660 | 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 | 1661 | else I_REF_POS = temp_vel_pos * ((double) PISTON_AREA_B * 0.00006f / (K_v * sqrt(2.0f / (alpha3 + 1.0f)))); |
Lightvalve | 67:c2812cf26c38 | 1662 | |
Lightvalve | 67:c2812cf26c38 | 1663 | I_REF = I_REF_POS; |
Lightvalve | 67:c2812cf26c38 | 1664 | |
Lightvalve | 67:c2812cf26c38 | 1665 | |
Lightvalve | 67:c2812cf26c38 | 1666 | |
Lightvalve | 67:c2812cf26c38 | 1667 | } else { |
Lightvalve | 67:c2812cf26c38 | 1668 | float VALVE_POS_RAW_FORCE_FB = 0.0f; |
Lightvalve | 67:c2812cf26c38 | 1669 | 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 | 1670 | |
Lightvalve | 67:c2812cf26c38 | 1671 | if (VALVE_POS_RAW_FORCE_FB >= 0) { |
Lightvalve | 67:c2812cf26c38 | 1672 | valve_pos.ref = VALVE_POS_RAW_FORCE_FB + VALVE_DEADZONE_PLUS; |
Lightvalve | 67:c2812cf26c38 | 1673 | } else { |
Lightvalve | 67:c2812cf26c38 | 1674 | valve_pos.ref = VALVE_POS_RAW_FORCE_FB + VALVE_DEADZONE_MINUS; |
Lightvalve | 67:c2812cf26c38 | 1675 | } |
Lightvalve | 67:c2812cf26c38 | 1676 | |
Lightvalve | 67:c2812cf26c38 | 1677 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 67:c2812cf26c38 | 1678 | |
Lightvalve | 67:c2812cf26c38 | 1679 | V_out = (float) Vout.ref; |
Lightvalve | 67:c2812cf26c38 | 1680 | |
Lightvalve | 67:c2812cf26c38 | 1681 | } |
Lightvalve | 67:c2812cf26c38 | 1682 | |
Lightvalve | 67:c2812cf26c38 | 1683 | |
Lightvalve | 169:645207e160ca | 1684 | |
Lightvalve | 169:645207e160ca | 1685 | |
Lightvalve | 59:f308b1656d9c | 1686 | // pos.err = pos.ref - (float)pos.sen; |
Lightvalve | 59:f308b1656d9c | 1687 | // float VALVE_POS_RAW_POS_FB = 0.0f; |
Lightvalve | 59:f308b1656d9c | 1688 | // VALVE_POS_RAW_POS_FB = (float) P_GAIN_JOINT_POSITION * 0.01f * pos.err/(float) ENC_PULSE_PER_POSITION; |
Lightvalve | 59:f308b1656d9c | 1689 | // valve_pos.ref = VALVE_POS_RAW_POS_FB + (float) VALVE_CENTER; |
Lightvalve | 59:f308b1656d9c | 1690 | // VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 29:69f3f5445d6d | 1691 | |
Lightvalve | 29:69f3f5445d6d | 1692 | cnt_findhome++; |
Lightvalve | 29:69f3f5445d6d | 1693 | if (cnt_findhome >= T_move) { |
Lightvalve | 29:69f3f5445d6d | 1694 | //REFERENCE_MODE = MODE_REF_DIRECT; |
Lightvalve | 29:69f3f5445d6d | 1695 | cnt_findhome = 0; |
Lightvalve | 30:8d561f16383b | 1696 | pos.ref = 0.0f; |
Lightvalve | 30:8d561f16383b | 1697 | vel.ref = 0.0f; |
Lightvalve | 30:8d561f16383b | 1698 | pos.ref_home_pos = 0.0f; |
Lightvalve | 30:8d561f16383b | 1699 | vel.ref_home_pos = 0.0f; |
Lightvalve | 29:69f3f5445d6d | 1700 | FINDHOME_STAGE = FINDHOME_INIT; |
Lightvalve | 57:f4819de54e7a | 1701 | CONTROL_UTILITY_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 29:69f3f5445d6d | 1702 | } |
Lightvalve | 13:747daba9cf59 | 1703 | } |
Lightvalve | 19:23b7c1ad8683 | 1704 | |
Lightvalve | 13:747daba9cf59 | 1705 | break; |
Lightvalve | 13:747daba9cf59 | 1706 | } |
Lightvalve | 14:8e7590227d22 | 1707 | |
Lightvalve | 50:3c630b5eba9f | 1708 | // case MODE_VALVE_GAIN_SETTING: { |
Lightvalve | 50:3c630b5eba9f | 1709 | // if (TMR3_COUNT_FLOWRATE == 0) { |
Lightvalve | 50:3c630b5eba9f | 1710 | // if (pos_plus_end == pos_minus_end) need_enc_init = true; |
Lightvalve | 50:3c630b5eba9f | 1711 | // else { |
Lightvalve | 50:3c630b5eba9f | 1712 | // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 50:3c630b5eba9f | 1713 | // temp_time = (int) (0.5f * (float) TMR_FREQ_5k); |
Lightvalve | 50:3c630b5eba9f | 1714 | // } |
Lightvalve | 50:3c630b5eba9f | 1715 | // } |
Lightvalve | 50:3c630b5eba9f | 1716 | // if (need_enc_init) { |
Lightvalve | 50:3c630b5eba9f | 1717 | // if (TMR3_COUNT_FLOWRATE < (int) (0.5f * (float) TMR_FREQ_5k)) { |
Lightvalve | 50:3c630b5eba9f | 1718 | // V_out = VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 50:3c630b5eba9f | 1719 | // pos_plus_end = pos.sen; |
Lightvalve | 50:3c630b5eba9f | 1720 | // } else if (TMR3_COUNT_FLOWRATE < TMR_FREQ_5k) { |
Lightvalve | 50:3c630b5eba9f | 1721 | // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 50:3c630b5eba9f | 1722 | // pos_minus_end = pos.sen; |
Lightvalve | 50:3c630b5eba9f | 1723 | // } else if (TMR3_COUNT_FLOWRATE == TMR_FREQ_5k) { |
Lightvalve | 50:3c630b5eba9f | 1724 | // need_enc_init = false; |
Lightvalve | 50:3c630b5eba9f | 1725 | // check_vel_pos_init = (int) (0.9f * (float) (pos_plus_end - pos_minus_end)); |
Lightvalve | 50:3c630b5eba9f | 1726 | // check_vel_pos_fin = (int) (0.95f * (float) (pos_plus_end - pos_minus_end)); |
Lightvalve | 50:3c630b5eba9f | 1727 | // check_vel_pos_interv = check_vel_pos_fin - check_vel_pos_init; |
Lightvalve | 50:3c630b5eba9f | 1728 | // } |
Lightvalve | 50:3c630b5eba9f | 1729 | // temp_time = TMR_FREQ_5k; |
Lightvalve | 50:3c630b5eba9f | 1730 | // } |
Lightvalve | 50:3c630b5eba9f | 1731 | // TMR3_COUNT_FLOWRATE++; |
Lightvalve | 50:3c630b5eba9f | 1732 | // if (TMR3_COUNT_FLOWRATE > temp_time) { |
Lightvalve | 50:3c630b5eba9f | 1733 | // if (flag_flowrate % 2 == 0) { // (+) |
Lightvalve | 50:3c630b5eba9f | 1734 | // VALVE_VOLTAGE = 1000.0f * (float) (flag_flowrate / 2 + 1); |
Lightvalve | 50:3c630b5eba9f | 1735 | // V_out = VALVE_VOLTAGE; |
Lightvalve | 50:3c630b5eba9f | 1736 | // if (pos.sen > (pos_minus_end + check_vel_pos_init) && pos.sen < (pos_minus_end + check_vel_pos_fin)) { |
Lightvalve | 50:3c630b5eba9f | 1737 | // fl_temp_cnt++; |
Lightvalve | 50:3c630b5eba9f | 1738 | // } else if (pos.sen >= (pos_minus_end + check_vel_pos_fin) && CUR_VELOCITY == 0) { |
Lightvalve | 50:3c630b5eba9f | 1739 | // 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 | 1740 | // // 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 | 1741 | // fl_temp_cnt2++; |
Lightvalve | 50:3c630b5eba9f | 1742 | // } |
Lightvalve | 50:3c630b5eba9f | 1743 | // } else if (flag_flowrate % 2 == 1) { // (-) |
Lightvalve | 50:3c630b5eba9f | 1744 | // VALVE_VOLTAGE = -1. * (float) (flag_flowrate / 2 + 1); |
Lightvalve | 50:3c630b5eba9f | 1745 | // V_out = VALVE_VOLTAGE; |
Lightvalve | 50:3c630b5eba9f | 1746 | // if (pos.sen < (pos_plus_end - check_vel_pos_init) && pos.sen > (pos_plus_end - check_vel_pos_fin)) { |
Lightvalve | 50:3c630b5eba9f | 1747 | // fl_temp_cnt++; |
Lightvalve | 50:3c630b5eba9f | 1748 | // } else if (pos.sen <= (pos_plus_end - check_vel_pos_fin) && CUR_VELOCITY == 0) { |
Lightvalve | 50:3c630b5eba9f | 1749 | // 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 | 1750 | // // 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 | 1751 | // fl_temp_cnt2++; |
Lightvalve | 50:3c630b5eba9f | 1752 | // } |
Lightvalve | 50:3c630b5eba9f | 1753 | // } |
Lightvalve | 50:3c630b5eba9f | 1754 | // if (fl_temp_cnt2 == 100) { |
Lightvalve | 50:3c630b5eba9f | 1755 | // |
Lightvalve | 50:3c630b5eba9f | 1756 | // ROM_RESET_DATA(); |
Lightvalve | 50:3c630b5eba9f | 1757 | // |
Lightvalve | 50:3c630b5eba9f | 1758 | // //spi_eeprom_write(RID_VALVE_GAIN_PLUS_1 + flag_flowrate, (int16_t) (VALVE_GAIN_LPM_PER_V[flag_flowrate] * 100.0f)); |
Lightvalve | 50:3c630b5eba9f | 1759 | // cur_vel_sum = 0; |
Lightvalve | 50:3c630b5eba9f | 1760 | // fl_temp_cnt = 0; |
Lightvalve | 50:3c630b5eba9f | 1761 | // fl_temp_cnt2 = 0; |
Lightvalve | 50:3c630b5eba9f | 1762 | // flag_flowrate++; |
Lightvalve | 50:3c630b5eba9f | 1763 | // } |
Lightvalve | 50:3c630b5eba9f | 1764 | // if (flag_flowrate == 10) { |
Lightvalve | 50:3c630b5eba9f | 1765 | // V_out = 0; |
Lightvalve | 50:3c630b5eba9f | 1766 | // flag_flowrate = 0; |
Lightvalve | 50:3c630b5eba9f | 1767 | // TMR3_COUNT_FLOWRATE = 0; |
Lightvalve | 50:3c630b5eba9f | 1768 | // valve_gain_repeat_cnt++; |
Lightvalve | 50:3c630b5eba9f | 1769 | // if (valve_gain_repeat_cnt >= 1) { |
Lightvalve | 50:3c630b5eba9f | 1770 | // CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 50:3c630b5eba9f | 1771 | // valve_gain_repeat_cnt = 0; |
Lightvalve | 50:3c630b5eba9f | 1772 | // } |
Lightvalve | 50:3c630b5eba9f | 1773 | // |
Lightvalve | 50:3c630b5eba9f | 1774 | // } |
Lightvalve | 50:3c630b5eba9f | 1775 | // break; |
Lightvalve | 50:3c630b5eba9f | 1776 | // } |
Lightvalve | 50:3c630b5eba9f | 1777 | // |
Lightvalve | 50:3c630b5eba9f | 1778 | // } |
Lightvalve | 14:8e7590227d22 | 1779 | case MODE_PRESSURE_SENSOR_NULLING: { |
Lightvalve | 13:747daba9cf59 | 1780 | // DAC Voltage reference set |
Lightvalve | 14:8e7590227d22 | 1781 | if (TMR3_COUNT_PRES_NULL < TMR_FREQ_5k * 2) { |
Lightvalve | 14:8e7590227d22 | 1782 | CUR_PRES_A_sum += pres_A.sen; |
Lightvalve | 14:8e7590227d22 | 1783 | CUR_PRES_B_sum += pres_B.sen; |
Lightvalve | 14:8e7590227d22 | 1784 | |
Lightvalve | 14:8e7590227d22 | 1785 | if (TMR3_COUNT_PRES_NULL % 10 == 0) { |
Lightvalve | 30:8d561f16383b | 1786 | CUR_PRES_A_mean = CUR_PRES_A_sum / 10.0f; |
Lightvalve | 30:8d561f16383b | 1787 | CUR_PRES_B_mean = CUR_PRES_B_sum / 10.0f; |
Lightvalve | 13:747daba9cf59 | 1788 | CUR_PRES_A_sum = 0; |
Lightvalve | 13:747daba9cf59 | 1789 | CUR_PRES_B_sum = 0; |
Lightvalve | 14:8e7590227d22 | 1790 | |
Lightvalve | 38:118df027d851 | 1791 | float VREF_NullingGain = 0.0003f; |
Lightvalve | 58:2eade98630e2 | 1792 | PRES_A_VREF = PRES_A_VREF + VREF_NullingGain * CUR_PRES_A_mean; |
Lightvalve | 58:2eade98630e2 | 1793 | PRES_B_VREF = PRES_B_VREF + VREF_NullingGain * CUR_PRES_B_mean; |
Lightvalve | 14:8e7590227d22 | 1794 | |
Lightvalve | 30:8d561f16383b | 1795 | if (PRES_A_VREF > 3.3f) PRES_A_VREF = 3.3f; |
Lightvalve | 30:8d561f16383b | 1796 | if (PRES_A_VREF < 0.0f) PRES_A_VREF = 0.0f; |
Lightvalve | 30:8d561f16383b | 1797 | if (PRES_B_VREF > 3.3f) PRES_B_VREF = 3.3f; |
Lightvalve | 30:8d561f16383b | 1798 | if (PRES_B_VREF < 0.0f) PRES_B_VREF = 0.0f; |
Lightvalve | 19:23b7c1ad8683 | 1799 | |
Lightvalve | 30:8d561f16383b | 1800 | dac_1 = PRES_A_VREF / 3.3f; |
Lightvalve | 30:8d561f16383b | 1801 | dac_2 = PRES_B_VREF / 3.3f; |
Lightvalve | 13:747daba9cf59 | 1802 | } |
Lightvalve | 13:747daba9cf59 | 1803 | } else { |
Lightvalve | 57:f4819de54e7a | 1804 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 14:8e7590227d22 | 1805 | TMR3_COUNT_PRES_NULL = 0; |
Lightvalve | 13:747daba9cf59 | 1806 | CUR_PRES_A_sum = 0; |
Lightvalve | 13:747daba9cf59 | 1807 | CUR_PRES_B_sum = 0; |
Lightvalve | 13:747daba9cf59 | 1808 | CUR_PRES_A_mean = 0; |
Lightvalve | 13:747daba9cf59 | 1809 | CUR_PRES_B_mean = 0; |
Lightvalve | 14:8e7590227d22 | 1810 | |
Lightvalve | 170:42c938a40313 | 1811 | // ROM_RESET_DATA(); |
Lightvalve | 170:42c938a40313 | 1812 | spi_eeprom_write(RID_PRES_A_SENSOR_VREF, (int16_t) (PRES_A_VREF * 1000.0f)); |
Lightvalve | 170:42c938a40313 | 1813 | spi_eeprom_write(RID_PRES_B_SENSOR_VREF, (int16_t) (PRES_B_VREF * 1000.0f)); |
Lightvalve | 19:23b7c1ad8683 | 1814 | |
Lightvalve | 30:8d561f16383b | 1815 | dac_1 = PRES_A_VREF / 3.3f; |
Lightvalve | 30:8d561f16383b | 1816 | dac_2 = PRES_B_VREF / 3.3f; |
Lightvalve | 17:1865016ca2e7 | 1817 | //pc.printf("nulling end"); |
Lightvalve | 13:747daba9cf59 | 1818 | } |
Lightvalve | 14:8e7590227d22 | 1819 | TMR3_COUNT_PRES_NULL++; |
Lightvalve | 13:747daba9cf59 | 1820 | break; |
Lightvalve | 13:747daba9cf59 | 1821 | } |
Lightvalve | 14:8e7590227d22 | 1822 | |
Lightvalve | 50:3c630b5eba9f | 1823 | // case MODE_PRESSURE_SENSOR_CALIB: { |
Lightvalve | 50:3c630b5eba9f | 1824 | // if (TMR3_COUNT_PRES_CALIB < 2 * TMR_FREQ_5k) { |
Lightvalve | 50:3c630b5eba9f | 1825 | // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 50:3c630b5eba9f | 1826 | // if (TMR3_COUNT_PRES_CALIB >= TMR_FREQ_5k) { |
Lightvalve | 50:3c630b5eba9f | 1827 | // CUR_PRES_A_sum += CUR_PRES_A; |
Lightvalve | 50:3c630b5eba9f | 1828 | // } |
Lightvalve | 50:3c630b5eba9f | 1829 | // } else if (TMR3_COUNT_PRES_CALIB < 4 * TMR_FREQ_5k) { |
Lightvalve | 50:3c630b5eba9f | 1830 | // V_out = VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 50:3c630b5eba9f | 1831 | // if (TMR3_COUNT_PRES_CALIB >= 3 * TMR_FREQ_5k) { |
Lightvalve | 50:3c630b5eba9f | 1832 | // CUR_PRES_B_sum += CUR_PRES_B; |
Lightvalve | 50:3c630b5eba9f | 1833 | // } |
Lightvalve | 50:3c630b5eba9f | 1834 | // } else { |
Lightvalve | 50:3c630b5eba9f | 1835 | // CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 50:3c630b5eba9f | 1836 | // TMR3_COUNT_PRES_CALIB = 0; |
Lightvalve | 50:3c630b5eba9f | 1837 | // V_out = 0; |
Lightvalve | 50:3c630b5eba9f | 1838 | // PRES_SENSOR_A_PULSE_PER_BAR = CUR_PRES_A_sum / ((float) TMR_FREQ_5k - 1.0f) - PRES_A_NULL; |
Lightvalve | 50:3c630b5eba9f | 1839 | // PRES_SENSOR_A_PULSE_PER_BAR = PRES_SENSOR_A_PULSE_PER_BAR / ((float) PRES_SUPPLY - 1.0f); |
Lightvalve | 50:3c630b5eba9f | 1840 | // PRES_SENSOR_B_PULSE_PER_BAR = CUR_PRES_B_sum / ((float) TMR_FREQ_5k - 1.0f) - PRES_B_NULL; |
Lightvalve | 50:3c630b5eba9f | 1841 | // PRES_SENSOR_B_PULSE_PER_BAR = PRES_SENSOR_B_PULSE_PER_BAR / ((float) PRES_SUPPLY - 1.0f); |
Lightvalve | 50:3c630b5eba9f | 1842 | // CUR_PRES_A_sum = 0; |
Lightvalve | 50:3c630b5eba9f | 1843 | // CUR_PRES_B_sum = 0; |
Lightvalve | 50:3c630b5eba9f | 1844 | // CUR_PRES_A_mean = 0; |
Lightvalve | 50:3c630b5eba9f | 1845 | // CUR_PRES_B_mean = 0; |
Lightvalve | 50:3c630b5eba9f | 1846 | // |
Lightvalve | 50:3c630b5eba9f | 1847 | // ROM_RESET_DATA(); |
Lightvalve | 50:3c630b5eba9f | 1848 | // |
Lightvalve | 50:3c630b5eba9f | 1849 | // //spi_eeprom_write(RID_PRES_SENSOR_A_PULSE_PER_BAR, (int16_t) (PRES_SENSOR_A_PULSE_PER_BAR * 100.0f)); |
Lightvalve | 50:3c630b5eba9f | 1850 | // //spi_eeprom_write(RID_PRES_SENSOR_B_PULSE_PER_BAR, (int16_t) (PRES_SENSOR_B_PULSE_PER_BAR * 100.0f)); |
Lightvalve | 50:3c630b5eba9f | 1851 | // } |
Lightvalve | 50:3c630b5eba9f | 1852 | // TMR3_COUNT_PRES_CALIB++; |
Lightvalve | 50:3c630b5eba9f | 1853 | // break; |
Lightvalve | 50:3c630b5eba9f | 1854 | // } |
Lightvalve | 19:23b7c1ad8683 | 1855 | |
Lightvalve | 50:3c630b5eba9f | 1856 | // case MODE_ROTARY_FRICTION_TUNING: { |
Lightvalve | 50:3c630b5eba9f | 1857 | // 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 | 1858 | // V_out = PWM_out * sin(2 * 3.14159f * freq_fric_tune * TMR3_COUNT_ROTARY_FRIC_TUNE * 0.0001f); |
Lightvalve | 50:3c630b5eba9f | 1859 | // if (V_out > 0) V_out = VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 50:3c630b5eba9f | 1860 | // else V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 50:3c630b5eba9f | 1861 | // TMR3_COUNT_ROTARY_FRIC_TUNE++; |
Lightvalve | 50:3c630b5eba9f | 1862 | // if (TMR3_COUNT_ROTARY_FRIC_TUNE > TUNING_TIME * TMR_FREQ_5k) { |
Lightvalve | 50:3c630b5eba9f | 1863 | // TMR3_COUNT_ROTARY_FRIC_TUNE = 0; |
Lightvalve | 50:3c630b5eba9f | 1864 | // V_out = 0.0f; |
Lightvalve | 50:3c630b5eba9f | 1865 | // CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 50:3c630b5eba9f | 1866 | // } |
Lightvalve | 50:3c630b5eba9f | 1867 | // break; |
Lightvalve | 50:3c630b5eba9f | 1868 | // } |
Lightvalve | 14:8e7590227d22 | 1869 | |
Lightvalve | 14:8e7590227d22 | 1870 | case MODE_DDV_POS_VS_PWM_ID: { |
Lightvalve | 59:f308b1656d9c | 1871 | CONTROL_MODE = MODE_VALVE_OPEN_LOOP; |
Lightvalve | 13:747daba9cf59 | 1872 | VALVE_ID_timer = VALVE_ID_timer + 1; |
Lightvalve | 14:8e7590227d22 | 1873 | |
Lightvalve | 14:8e7590227d22 | 1874 | if(VALVE_ID_timer < TMR_FREQ_5k*1) { |
Lightvalve | 60:64181f1d3e60 | 1875 | Vout.ref = 3000.0f * sin(2.0f*3.14f*VALVE_ID_timer/TMR_FREQ_5k * 100.0f); |
Lightvalve | 14:8e7590227d22 | 1876 | } else if(VALVE_ID_timer < TMR_FREQ_5k*2) { |
Lightvalve | 59:f308b1656d9c | 1877 | Vout.ref = 1000.0f*(ID_index_array[ID_index]); |
Lightvalve | 14:8e7590227d22 | 1878 | } else if(VALVE_ID_timer == TMR_FREQ_5k*2) { |
Lightvalve | 13:747daba9cf59 | 1879 | VALVE_POS_TMP = 0; |
Lightvalve | 13:747daba9cf59 | 1880 | data_num = 0; |
Lightvalve | 14:8e7590227d22 | 1881 | } else if(VALVE_ID_timer < TMR_FREQ_5k*3) { |
Lightvalve | 13:747daba9cf59 | 1882 | data_num = data_num + 1; |
Lightvalve | 13:747daba9cf59 | 1883 | VALVE_POS_TMP = VALVE_POS_TMP + value; |
Lightvalve | 14:8e7590227d22 | 1884 | } else if(VALVE_ID_timer == TMR_FREQ_5k*3) { |
Lightvalve | 59:f308b1656d9c | 1885 | Vout.ref = 0.0f; |
Lightvalve | 59:f308b1656d9c | 1886 | } else { |
Lightvalve | 13:747daba9cf59 | 1887 | VALVE_POS_AVG[ID_index] = VALVE_POS_TMP / data_num; |
Lightvalve | 13:747daba9cf59 | 1888 | VALVE_ID_timer = 0; |
Lightvalve | 13:747daba9cf59 | 1889 | ID_index= ID_index +1; |
Lightvalve | 13:747daba9cf59 | 1890 | } |
Lightvalve | 14:8e7590227d22 | 1891 | |
Lightvalve | 17:1865016ca2e7 | 1892 | if(ID_index>=25) { |
Lightvalve | 13:747daba9cf59 | 1893 | int i; |
Lightvalve | 13:747daba9cf59 | 1894 | VALVE_POS_AVG_OLD = VALVE_POS_AVG[0]; |
Lightvalve | 17:1865016ca2e7 | 1895 | for(i=0; i<25; i++) { |
Lightvalve | 13:747daba9cf59 | 1896 | VALVE_POS_VS_PWM[i] = (int16_t) (VALVE_POS_AVG[i]); |
Lightvalve | 14:8e7590227d22 | 1897 | if(VALVE_POS_AVG[i] > VALVE_POS_AVG_OLD) { |
Lightvalve | 13:747daba9cf59 | 1898 | VALVE_MAX_POS = VALVE_POS_AVG[i]; |
Lightvalve | 13:747daba9cf59 | 1899 | VALVE_POS_AVG_OLD = VALVE_MAX_POS; |
Lightvalve | 14:8e7590227d22 | 1900 | } else if(VALVE_POS_AVG[i] < VALVE_POS_AVG_OLD) { |
Lightvalve | 13:747daba9cf59 | 1901 | VALVE_MIN_POS = VALVE_POS_AVG[i]; |
Lightvalve | 13:747daba9cf59 | 1902 | VALVE_POS_AVG_OLD = VALVE_MIN_POS; |
Lightvalve | 13:747daba9cf59 | 1903 | } |
Lightvalve | 13:747daba9cf59 | 1904 | } |
Lightvalve | 170:42c938a40313 | 1905 | // ROM_RESET_DATA(); |
Lightvalve | 170:42c938a40313 | 1906 | spi_eeprom_write(RID_VALVE_MAX_POS, (int16_t) VALVE_MAX_POS); |
Lightvalve | 170:42c938a40313 | 1907 | spi_eeprom_write(RID_VALVE_MIN_POS, (int16_t) VALVE_MIN_POS); |
Lightvalve | 170:42c938a40313 | 1908 | for(int i=0; i<25; i++) { |
Lightvalve | 170:42c938a40313 | 1909 | spi_eeprom_write(RID_VALVE_POS_VS_PWM_0 + i, (int16_t) VALVE_POS_VS_PWM[i]); |
Lightvalve | 170:42c938a40313 | 1910 | } |
Lightvalve | 13:747daba9cf59 | 1911 | ID_index = 0; |
Lightvalve | 57:f4819de54e7a | 1912 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 13:747daba9cf59 | 1913 | } |
Lightvalve | 14:8e7590227d22 | 1914 | |
Lightvalve | 14:8e7590227d22 | 1915 | |
Lightvalve | 13:747daba9cf59 | 1916 | break; |
Lightvalve | 13:747daba9cf59 | 1917 | } |
Lightvalve | 14:8e7590227d22 | 1918 | |
Lightvalve | 14:8e7590227d22 | 1919 | case MODE_DDV_DEADZONE_AND_CENTER: { |
Lightvalve | 59:f308b1656d9c | 1920 | CONTROL_MODE = MODE_VALVE_OPEN_LOOP; |
Lightvalve | 13:747daba9cf59 | 1921 | VALVE_DZ_timer = VALVE_DZ_timer + 1; |
Lightvalve | 14:8e7590227d22 | 1922 | if(first_check == 0) { |
Lightvalve | 30:8d561f16383b | 1923 | if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 1924 | Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 30:8d561f16383b | 1925 | } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 1926 | Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 14:8e7590227d22 | 1927 | pos_plus_end = pos.sen; |
Lightvalve | 30:8d561f16383b | 1928 | } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 1929 | Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 30:8d561f16383b | 1930 | } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 1931 | Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 14:8e7590227d22 | 1932 | pos_minus_end = pos.sen; |
Lightvalve | 30:8d561f16383b | 1933 | } else if(VALVE_DZ_timer < (int) (3.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 1934 | 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 | 1935 | } else if(VALVE_DZ_timer < (int) (4.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 1936 | 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 | 1937 | data_num = data_num + 1; |
Lightvalve | 13:747daba9cf59 | 1938 | VALVE_POS_TMP = VALVE_POS_TMP + value; |
Lightvalve | 30:8d561f16383b | 1939 | } else if(VALVE_DZ_timer == (int) (4.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 1940 | 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 | 1941 | DDV_POS_AVG = VALVE_POS_TMP / data_num; |
Lightvalve | 14:8e7590227d22 | 1942 | START_POS = pos.sen; |
Lightvalve | 13:747daba9cf59 | 1943 | VALVE_POS_TMP = 0; |
Lightvalve | 13:747daba9cf59 | 1944 | data_num = 0; |
Lightvalve | 14:8e7590227d22 | 1945 | |
Lightvalve | 30:8d561f16383b | 1946 | } else if(VALVE_DZ_timer < (int) (5.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1947 | valve_pos.ref = DDV_POS_AVG; |
Lightvalve | 14:8e7590227d22 | 1948 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 14:8e7590227d22 | 1949 | |
Lightvalve | 30:8d561f16383b | 1950 | } else if(VALVE_DZ_timer < (int) (6.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1951 | valve_pos.ref = DDV_POS_AVG; |
Lightvalve | 14:8e7590227d22 | 1952 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 60:64181f1d3e60 | 1953 | |
Lightvalve | 30:8d561f16383b | 1954 | } else if(VALVE_DZ_timer == (int) (6.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1955 | valve_pos.ref = DDV_POS_AVG; |
Lightvalve | 14:8e7590227d22 | 1956 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 14:8e7590227d22 | 1957 | FINAL_POS = pos.sen; |
Lightvalve | 14:8e7590227d22 | 1958 | |
Lightvalve | 60:64181f1d3e60 | 1959 | if((FINAL_POS - START_POS)>200) { |
Lightvalve | 13:747daba9cf59 | 1960 | DZ_case = 1; |
Lightvalve | 60:64181f1d3e60 | 1961 | } else if((FINAL_POS - START_POS)<-200) { |
Lightvalve | 13:747daba9cf59 | 1962 | DZ_case = -1; |
Lightvalve | 14:8e7590227d22 | 1963 | } else { |
Lightvalve | 13:747daba9cf59 | 1964 | DZ_case = 0; |
Lightvalve | 13:747daba9cf59 | 1965 | } |
Lightvalve | 61:bc8c8270f0ab | 1966 | |
Lightvalve | 60:64181f1d3e60 | 1967 | CAN_TX_PRES((int16_t) (DZ_case), (int16_t) (6)); |
Lightvalve | 50:3c630b5eba9f | 1968 | |
Lightvalve | 13:747daba9cf59 | 1969 | first_check = 1; |
Lightvalve | 13:747daba9cf59 | 1970 | DZ_DIRECTION = 1; |
Lightvalve | 13:747daba9cf59 | 1971 | VALVE_DZ_timer = 0; |
Lightvalve | 13:747daba9cf59 | 1972 | Ref_Valve_Pos_Old = DDV_POS_AVG; |
Lightvalve | 13:747daba9cf59 | 1973 | DZ_NUM = 1; |
Lightvalve | 13:747daba9cf59 | 1974 | DZ_index = 1; |
Lightvalve | 14:8e7590227d22 | 1975 | |
Lightvalve | 13:747daba9cf59 | 1976 | } |
Lightvalve | 19:23b7c1ad8683 | 1977 | } else { |
Lightvalve | 14:8e7590227d22 | 1978 | if((DZ_case == -1 && DZ_NUM == 1) | (DZ_case == 1 && DZ_NUM == 1)) { |
Lightvalve | 30:8d561f16383b | 1979 | if(VALVE_DZ_timer < (int) (1.0 * (float) TMR_FREQ_5k)) { |
Lightvalve | 60:64181f1d3e60 | 1980 | 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 | 1981 | //pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end; |
Lightvalve | 60:64181f1d3e60 | 1982 | //CONTROL_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 30:8d561f16383b | 1983 | } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1984 | START_POS = pos.sen; |
Lightvalve | 30:8d561f16383b | 1985 | } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1986 | valve_pos.ref = Ref_Valve_Pos_Old - DZ_case * DZ_DIRECTION * 64 / DZ_index; |
Lightvalve | 14:8e7590227d22 | 1987 | if(valve_pos.ref <= VALVE_MIN_POS) { |
Lightvalve | 14:8e7590227d22 | 1988 | valve_pos.ref = VALVE_MIN_POS; |
Lightvalve | 14:8e7590227d22 | 1989 | } else if(valve_pos.ref >= VALVE_MAX_POS) { |
Lightvalve | 14:8e7590227d22 | 1990 | valve_pos.ref = VALVE_MAX_POS; |
Lightvalve | 13:747daba9cf59 | 1991 | } |
Lightvalve | 14:8e7590227d22 | 1992 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 60:64181f1d3e60 | 1993 | |
Lightvalve | 30:8d561f16383b | 1994 | } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1995 | Ref_Valve_Pos_Old = valve_pos.ref; |
Lightvalve | 14:8e7590227d22 | 1996 | FINAL_POS = pos.sen; |
Lightvalve | 14:8e7590227d22 | 1997 | |
Lightvalve | 14:8e7590227d22 | 1998 | if((FINAL_POS - START_POS)>100) { |
Lightvalve | 14:8e7590227d22 | 1999 | DZ_DIRECTION = 1 * DZ_case; |
Lightvalve | 14:8e7590227d22 | 2000 | } else if((FINAL_POS - START_POS)<-100) { |
Lightvalve | 14:8e7590227d22 | 2001 | DZ_DIRECTION = -1 * DZ_case; |
Lightvalve | 14:8e7590227d22 | 2002 | } else { |
Lightvalve | 13:747daba9cf59 | 2003 | DZ_DIRECTION = 1 * DZ_case; |
Lightvalve | 13:747daba9cf59 | 2004 | } |
Lightvalve | 14:8e7590227d22 | 2005 | |
Lightvalve | 13:747daba9cf59 | 2006 | VALVE_DZ_timer = 0; |
Lightvalve | 13:747daba9cf59 | 2007 | DZ_index= DZ_index *2; |
Lightvalve | 14:8e7590227d22 | 2008 | if(DZ_index >= 128) { |
Lightvalve | 14:8e7590227d22 | 2009 | FIRST_DZ = valve_pos.ref; |
Lightvalve | 13:747daba9cf59 | 2010 | DZ_NUM = 2; |
Lightvalve | 13:747daba9cf59 | 2011 | Ref_Valve_Pos_Old = FIRST_DZ; |
Lightvalve | 13:747daba9cf59 | 2012 | DZ_index = 1; |
Lightvalve | 13:747daba9cf59 | 2013 | DZ_DIRECTION = 1; |
Lightvalve | 13:747daba9cf59 | 2014 | } |
Lightvalve | 13:747daba9cf59 | 2015 | } |
Lightvalve | 14:8e7590227d22 | 2016 | } else if((DZ_case == -1 && DZ_NUM == 2) | (DZ_case == 1 && DZ_NUM == 2)) { |
Lightvalve | 30:8d561f16383b | 2017 | if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 60:64181f1d3e60 | 2018 | 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 | 2019 | //pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end; |
Lightvalve | 60:64181f1d3e60 | 2020 | //CONTROL_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 30:8d561f16383b | 2021 | } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 2022 | START_POS = pos.sen; |
Lightvalve | 30:8d561f16383b | 2023 | } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 60:64181f1d3e60 | 2024 | valve_pos.ref = Ref_Valve_Pos_Old - DZ_case * DZ_DIRECTION * 64 / DZ_index; |
Lightvalve | 14:8e7590227d22 | 2025 | if(valve_pos.ref <= VALVE_MIN_POS) { |
Lightvalve | 14:8e7590227d22 | 2026 | valve_pos.ref = VALVE_MIN_POS; |
Lightvalve | 14:8e7590227d22 | 2027 | } else if(valve_pos.ref >= VALVE_MAX_POS) { |
Lightvalve | 14:8e7590227d22 | 2028 | valve_pos.ref = VALVE_MAX_POS; |
Lightvalve | 13:747daba9cf59 | 2029 | } |
Lightvalve | 14:8e7590227d22 | 2030 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 61:bc8c8270f0ab | 2031 | |
Lightvalve | 30:8d561f16383b | 2032 | } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 2033 | Vout.ref = 0.0f; |
Lightvalve | 59:f308b1656d9c | 2034 | } else if(VALVE_DZ_timer > (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 2035 | Ref_Valve_Pos_Old = valve_pos.ref; |
Lightvalve | 14:8e7590227d22 | 2036 | FINAL_POS = pos.sen; |
Lightvalve | 14:8e7590227d22 | 2037 | |
Lightvalve | 14:8e7590227d22 | 2038 | if((FINAL_POS - START_POS)>100) { |
Lightvalve | 60:64181f1d3e60 | 2039 | DZ_DIRECTION = 1 * DZ_case; |
Lightvalve | 14:8e7590227d22 | 2040 | } else if((FINAL_POS - START_POS)<-100) { |
Lightvalve | 60:64181f1d3e60 | 2041 | DZ_DIRECTION = -1 * DZ_case; |
Lightvalve | 14:8e7590227d22 | 2042 | } else { |
Lightvalve | 60:64181f1d3e60 | 2043 | DZ_DIRECTION = -1 * DZ_case; |
Lightvalve | 13:747daba9cf59 | 2044 | } |
Lightvalve | 14:8e7590227d22 | 2045 | |
Lightvalve | 13:747daba9cf59 | 2046 | VALVE_DZ_timer = 0; |
Lightvalve | 60:64181f1d3e60 | 2047 | DZ_index= DZ_index * 2; |
Lightvalve | 14:8e7590227d22 | 2048 | if(DZ_index >= 128) { |
Lightvalve | 14:8e7590227d22 | 2049 | SECOND_DZ = valve_pos.ref; |
Lightvalve | 57:f4819de54e7a | 2050 | VALVE_CENTER = (int) (0.5f * (float) (FIRST_DZ) + 0.5f * (float) (SECOND_DZ)); |
Lightvalve | 13:747daba9cf59 | 2051 | first_check = 0; |
Lightvalve | 33:91b17819ec30 | 2052 | VALVE_DEADZONE_MINUS = (float) FIRST_DZ; |
Lightvalve | 33:91b17819ec30 | 2053 | VALVE_DEADZONE_PLUS = (float) SECOND_DZ; |
Lightvalve | 19:23b7c1ad8683 | 2054 | |
Lightvalve | 170:42c938a40313 | 2055 | // ROM_RESET_DATA(); |
Lightvalve | 170:42c938a40313 | 2056 | spi_eeprom_write(RID_VALVE_CNETER, (int16_t) VALVE_CENTER); |
Lightvalve | 170:42c938a40313 | 2057 | spi_eeprom_write(RID_VALVE_MAX_POS, (int16_t) VALVE_MAX_POS); |
Lightvalve | 170:42c938a40313 | 2058 | spi_eeprom_write(RID_VALVE_MIN_POS, (int16_t) VALVE_MIN_POS); |
Lightvalve | 34:bb2ca2fc2a8e | 2059 | |
Lightvalve | 60:64181f1d3e60 | 2060 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 13:747daba9cf59 | 2061 | DZ_index = 1; |
Lightvalve | 13:747daba9cf59 | 2062 | } |
Lightvalve | 13:747daba9cf59 | 2063 | } |
Lightvalve | 14:8e7590227d22 | 2064 | } else if(DZ_case == 0 && DZ_NUM ==1) { |
Lightvalve | 30:8d561f16383b | 2065 | if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 60:64181f1d3e60 | 2066 | 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 | 2067 | //pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end; |
Lightvalve | 60:64181f1d3e60 | 2068 | //CONTROL_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 30:8d561f16383b | 2069 | } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 2070 | START_POS = pos.sen; |
Lightvalve | 30:8d561f16383b | 2071 | } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 2072 | valve_pos.ref = Ref_Valve_Pos_Old - DZ_DIRECTION * 64 / DZ_index; |
Lightvalve | 14:8e7590227d22 | 2073 | if(valve_pos.ref <= VALVE_MIN_POS) { |
Lightvalve | 14:8e7590227d22 | 2074 | valve_pos.ref = VALVE_MIN_POS; |
Lightvalve | 14:8e7590227d22 | 2075 | } else if(valve_pos.ref >= VALVE_MAX_POS) { |
Lightvalve | 14:8e7590227d22 | 2076 | valve_pos.ref = VALVE_MAX_POS; |
Lightvalve | 13:747daba9cf59 | 2077 | } |
Lightvalve | 14:8e7590227d22 | 2078 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 61:bc8c8270f0ab | 2079 | |
Lightvalve | 30:8d561f16383b | 2080 | } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 2081 | Ref_Valve_Pos_Old = valve_pos.ref; |
Lightvalve | 14:8e7590227d22 | 2082 | FINAL_POS = pos.sen; |
Lightvalve | 14:8e7590227d22 | 2083 | |
Lightvalve | 14:8e7590227d22 | 2084 | if((FINAL_POS - START_POS)>100) { |
Lightvalve | 14:8e7590227d22 | 2085 | DZ_DIRECTION = 1; |
Lightvalve | 14:8e7590227d22 | 2086 | } else if((FINAL_POS - START_POS)<-100) { |
Lightvalve | 14:8e7590227d22 | 2087 | DZ_DIRECTION = -1; |
Lightvalve | 14:8e7590227d22 | 2088 | } else { |
Lightvalve | 13:747daba9cf59 | 2089 | DZ_DIRECTION = 1; |
Lightvalve | 13:747daba9cf59 | 2090 | } |
Lightvalve | 13:747daba9cf59 | 2091 | VALVE_DZ_timer = 0; |
Lightvalve | 13:747daba9cf59 | 2092 | DZ_index= DZ_index *2; |
Lightvalve | 14:8e7590227d22 | 2093 | if(DZ_index >= 128) { |
Lightvalve | 14:8e7590227d22 | 2094 | FIRST_DZ = valve_pos.ref; |
Lightvalve | 13:747daba9cf59 | 2095 | DZ_NUM = 2; |
Lightvalve | 13:747daba9cf59 | 2096 | Ref_Valve_Pos_Old = FIRST_DZ; |
Lightvalve | 13:747daba9cf59 | 2097 | DZ_index = 1; |
Lightvalve | 13:747daba9cf59 | 2098 | DZ_DIRECTION = 1; |
Lightvalve | 13:747daba9cf59 | 2099 | } |
Lightvalve | 13:747daba9cf59 | 2100 | } |
Lightvalve | 14:8e7590227d22 | 2101 | } else { |
Lightvalve | 30:8d561f16383b | 2102 | if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 60:64181f1d3e60 | 2103 | 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 | 2104 | //pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end; |
Lightvalve | 60:64181f1d3e60 | 2105 | //CONTROL_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 30:8d561f16383b | 2106 | } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 2107 | START_POS = pos.sen; |
Lightvalve | 30:8d561f16383b | 2108 | } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 2109 | valve_pos.ref = Ref_Valve_Pos_Old + DZ_DIRECTION * 64 / DZ_index; |
Lightvalve | 14:8e7590227d22 | 2110 | if(valve_pos.ref <= VALVE_MIN_POS) { |
Lightvalve | 14:8e7590227d22 | 2111 | valve_pos.ref = VALVE_MIN_POS; |
Lightvalve | 14:8e7590227d22 | 2112 | } else if(valve_pos.ref > VALVE_MAX_POS) { |
Lightvalve | 14:8e7590227d22 | 2113 | valve_pos.ref = VALVE_MAX_POS - 1; |
Lightvalve | 13:747daba9cf59 | 2114 | } |
Lightvalve | 14:8e7590227d22 | 2115 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 60:64181f1d3e60 | 2116 | |
Lightvalve | 30:8d561f16383b | 2117 | } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 2118 | Vout.ref = 0.0f; |
Lightvalve | 59:f308b1656d9c | 2119 | } else if(VALVE_DZ_timer > (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 2120 | Ref_Valve_Pos_Old = valve_pos.ref; |
Lightvalve | 14:8e7590227d22 | 2121 | FINAL_POS = pos.sen; |
Lightvalve | 61:bc8c8270f0ab | 2122 | |
Lightvalve | 60:64181f1d3e60 | 2123 | if((FINAL_POS - START_POS)>100) { |
Lightvalve | 13:747daba9cf59 | 2124 | DZ_DIRECTION = 1; |
Lightvalve | 60:64181f1d3e60 | 2125 | } else if((FINAL_POS - START_POS)<-100) { |
Lightvalve | 60:64181f1d3e60 | 2126 | DZ_DIRECTION = -1; |
Lightvalve | 60:64181f1d3e60 | 2127 | } else { |
Lightvalve | 13:747daba9cf59 | 2128 | DZ_DIRECTION = 1; |
Lightvalve | 60:64181f1d3e60 | 2129 | } |
Lightvalve | 14:8e7590227d22 | 2130 | |
Lightvalve | 13:747daba9cf59 | 2131 | VALVE_DZ_timer = 0; |
Lightvalve | 13:747daba9cf59 | 2132 | DZ_index= DZ_index *2; |
Lightvalve | 14:8e7590227d22 | 2133 | if(DZ_index >= 128) { |
Lightvalve | 14:8e7590227d22 | 2134 | SECOND_DZ = valve_pos.ref; |
Lightvalve | 57:f4819de54e7a | 2135 | VALVE_CENTER = (int) (0.5f * (float) (FIRST_DZ) + 0.5f * (float) (SECOND_DZ)); |
Lightvalve | 13:747daba9cf59 | 2136 | first_check = 0; |
Lightvalve | 33:91b17819ec30 | 2137 | VALVE_DEADZONE_MINUS = (float) FIRST_DZ; |
Lightvalve | 33:91b17819ec30 | 2138 | VALVE_DEADZONE_PLUS = (float) SECOND_DZ; |
Lightvalve | 61:bc8c8270f0ab | 2139 | |
Lightvalve | 170:42c938a40313 | 2140 | // ROM_RESET_DATA(); |
Lightvalve | 170:42c938a40313 | 2141 | spi_eeprom_write(RID_VALVE_CNETER, (int16_t) VALVE_CENTER); |
Lightvalve | 170:42c938a40313 | 2142 | spi_eeprom_write(RID_VALVE_MAX_POS, (int16_t) VALVE_MAX_POS); |
Lightvalve | 170:42c938a40313 | 2143 | spi_eeprom_write(RID_VALVE_MIN_POS, (int16_t) VALVE_MIN_POS); |
Lightvalve | 34:bb2ca2fc2a8e | 2144 | |
Lightvalve | 57:f4819de54e7a | 2145 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 13:747daba9cf59 | 2146 | DZ_index = 1; |
Lightvalve | 13:747daba9cf59 | 2147 | } |
Lightvalve | 13:747daba9cf59 | 2148 | } |
Lightvalve | 13:747daba9cf59 | 2149 | } |
Lightvalve | 14:8e7590227d22 | 2150 | } |
Lightvalve | 13:747daba9cf59 | 2151 | break; |
Lightvalve | 13:747daba9cf59 | 2152 | } |
Lightvalve | 14:8e7590227d22 | 2153 | |
Lightvalve | 14:8e7590227d22 | 2154 | case MODE_DDV_POS_VS_FLOWRATE: { |
Lightvalve | 59:f308b1656d9c | 2155 | CONTROL_MODE = MODE_VALVE_OPEN_LOOP; |
Lightvalve | 13:747daba9cf59 | 2156 | VALVE_FR_timer = VALVE_FR_timer + 1; |
Lightvalve | 14:8e7590227d22 | 2157 | if(first_check == 0) { |
Lightvalve | 30:8d561f16383b | 2158 | if(VALVE_FR_timer < (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 2159 | Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 23:59218d4a256d | 2160 | //CAN_TX_PRES((int16_t) (VALVE_FR_timer), (int16_t) (6)); |
Lightvalve | 30:8d561f16383b | 2161 | } else if(VALVE_FR_timer == (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 2162 | Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 14:8e7590227d22 | 2163 | pos_plus_end = pos.sen; |
Lightvalve | 14:8e7590227d22 | 2164 | // CAN_TX_PRES((int16_t) (V_out), (int16_t) (7)); |
Lightvalve | 30:8d561f16383b | 2165 | } else if(VALVE_FR_timer < (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 2166 | Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 30:8d561f16383b | 2167 | } else if(VALVE_FR_timer == (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 2168 | // CAN_TX_PRES((int16_t) (V_out), (int16_t) (8)); |
Lightvalve | 59:f308b1656d9c | 2169 | Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 14:8e7590227d22 | 2170 | pos_minus_end = pos.sen; |
Lightvalve | 13:747daba9cf59 | 2171 | first_check = 1; |
Lightvalve | 13:747daba9cf59 | 2172 | VALVE_FR_timer = 0; |
Lightvalve | 57:f4819de54e7a | 2173 | valve_pos.ref = (float) VALVE_CENTER; |
Lightvalve | 13:747daba9cf59 | 2174 | ID_index = 0; |
Lightvalve | 13:747daba9cf59 | 2175 | max_check = 0; |
Lightvalve | 13:747daba9cf59 | 2176 | min_check = 0; |
Lightvalve | 13:747daba9cf59 | 2177 | } |
Lightvalve | 14:8e7590227d22 | 2178 | } else { |
Lightvalve | 30:8d561f16383b | 2179 | if(VALVE_FR_timer < (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 2180 | //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 | 2181 | pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end; |
Lightvalve | 59:f308b1656d9c | 2182 | CONTROL_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 30:8d561f16383b | 2183 | } else if(VALVE_FR_timer == (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 13:747daba9cf59 | 2184 | data_num = 0; |
Lightvalve | 57:f4819de54e7a | 2185 | valve_pos.ref = 10.0f*((float) ID_index_array[ID_index]) + (float) VALVE_CENTER; |
Lightvalve | 14:8e7590227d22 | 2186 | |
Lightvalve | 14:8e7590227d22 | 2187 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 14:8e7590227d22 | 2188 | START_POS = pos.sen; |
Lightvalve | 30:8d561f16383b | 2189 | } else if(VALVE_FR_timer < (int) (5.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 57:f4819de54e7a | 2190 | valve_pos.ref = 10.0f*((float) ID_index_array[ID_index]) + (float) VALVE_CENTER; |
Lightvalve | 14:8e7590227d22 | 2191 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 13:747daba9cf59 | 2192 | data_num = data_num + 1; |
Lightvalve | 30:8d561f16383b | 2193 | if(abs(0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen) > 20000.0f) { |
Lightvalve | 14:8e7590227d22 | 2194 | FINAL_POS = pos.sen; |
Lightvalve | 14:8e7590227d22 | 2195 | one_period_end = 1; |
Lightvalve | 13:747daba9cf59 | 2196 | } |
Lightvalve | 30:8d561f16383b | 2197 | } else if(VALVE_FR_timer == (int) (5.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 2198 | FINAL_POS = pos.sen; |
Lightvalve | 13:747daba9cf59 | 2199 | one_period_end = 1; |
Lightvalve | 59:f308b1656d9c | 2200 | V_out = 0.0f; |
Lightvalve | 13:747daba9cf59 | 2201 | } |
Lightvalve | 14:8e7590227d22 | 2202 | |
Lightvalve | 14:8e7590227d22 | 2203 | if(one_period_end == 1) { |
Lightvalve | 14:8e7590227d22 | 2204 | if(valve_pos.ref > VALVE_MAX_POS) { |
Lightvalve | 13:747daba9cf59 | 2205 | max_check = 1; |
Lightvalve | 14:8e7590227d22 | 2206 | } else if(valve_pos.ref < VALVE_MIN_POS) { |
Lightvalve | 13:747daba9cf59 | 2207 | min_check = 1; |
Lightvalve | 13:747daba9cf59 | 2208 | } |
Lightvalve | 13:747daba9cf59 | 2209 | JOINT_VEL[ID_index] = (FINAL_POS - START_POS) / data_num * TMR_FREQ_5k; // pulse/sec |
Lightvalve | 19:23b7c1ad8683 | 2210 | |
Lightvalve | 13:747daba9cf59 | 2211 | VALVE_FR_timer = 0; |
Lightvalve | 13:747daba9cf59 | 2212 | one_period_end = 0; |
Lightvalve | 13:747daba9cf59 | 2213 | ID_index= ID_index +1; |
Lightvalve | 30:8d561f16383b | 2214 | V_out = 0.0f; |
Lightvalve | 13:747daba9cf59 | 2215 | } |
Lightvalve | 14:8e7590227d22 | 2216 | |
Lightvalve | 14:8e7590227d22 | 2217 | if(max_check == 1 && min_check == 1) { |
Lightvalve | 19:23b7c1ad8683 | 2218 | |
Lightvalve | 13:747daba9cf59 | 2219 | VALVE_POS_NUM = ID_index; |
Lightvalve | 170:42c938a40313 | 2220 | // ROM_RESET_DATA(); |
Lightvalve | 170:42c938a40313 | 2221 | for(int i=0; i<100; i++) { |
Lightvalve | 170:42c938a40313 | 2222 | spi_eeprom_write(RID_VALVE_POS_VS_FLOWRATE_0 + i, (int16_t) (JOINT_VEL[i] & 0xFFFF)); |
Lightvalve | 170:42c938a40313 | 2223 | spi_eeprom_write(RID_VALVE_POS_VS_FLOWRATE_0_1 + i, (int16_t) ((JOINT_VEL[i] >> 16) & 0xFFFF)); |
Lightvalve | 170:42c938a40313 | 2224 | } |
Lightvalve | 13:747daba9cf59 | 2225 | ID_index = 0; |
Lightvalve | 13:747daba9cf59 | 2226 | first_check = 0; |
Lightvalve | 13:747daba9cf59 | 2227 | VALVE_FR_timer = 0; |
Lightvalve | 57:f4819de54e7a | 2228 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 49:e7bcfc244d40 | 2229 | // CAN_TX_PRES((int16_t) (VALVE_FR_timer), (int16_t) (6)); |
Lightvalve | 13:747daba9cf59 | 2230 | } |
Lightvalve | 13:747daba9cf59 | 2231 | } |
Lightvalve | 13:747daba9cf59 | 2232 | break; |
Lightvalve | 13:747daba9cf59 | 2233 | } |
Lightvalve | 58:2eade98630e2 | 2234 | |
Lightvalve | 57:f4819de54e7a | 2235 | case MODE_SYSTEM_ID: { |
Lightvalve | 169:645207e160ca | 2236 | freq_sysid_Iref = (double) cnt_sysid * DT_TMR3 * 3.0f; |
Lightvalve | 57:f4819de54e7a | 2237 | valve_pos.ref = 2500.0f * sin(2.0f * 3.14159f * freq_sysid_Iref * (double) cnt_sysid * DT_TMR3); |
Lightvalve | 57:f4819de54e7a | 2238 | CONTROL_MODE = MODE_VALVE_OPEN_LOOP; |
Lightvalve | 57:f4819de54e7a | 2239 | cnt_sysid++; |
Lightvalve | 57:f4819de54e7a | 2240 | if (freq_sysid_Iref >= 300) { |
Lightvalve | 57:f4819de54e7a | 2241 | cnt_sysid = 0; |
Lightvalve | 57:f4819de54e7a | 2242 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 57:f4819de54e7a | 2243 | } |
Lightvalve | 57:f4819de54e7a | 2244 | break; |
Lightvalve | 57:f4819de54e7a | 2245 | } |
Lightvalve | 58:2eade98630e2 | 2246 | |
Lightvalve | 169:645207e160ca | 2247 | case MODE_FREQ_TEST: { |
Lightvalve | 169:645207e160ca | 2248 | float valve_pos_ref = 2500.0f * sin(2.0f * 3.141592f * freq_test_valve_ref * (float) cnt_freq_test * DT_TMR3); |
Lightvalve | 169:645207e160ca | 2249 | if(valve_pos_ref >= 0) { |
Lightvalve | 169:645207e160ca | 2250 | valve_pos.ref = (double)VALVE_CENTER + (double)valve_pos_ref * ((double)VALVE_MAX_POS-(double)VALVE_CENTER)/10000.0f; |
Lightvalve | 169:645207e160ca | 2251 | } else { |
Lightvalve | 169:645207e160ca | 2252 | valve_pos.ref = (double)VALVE_CENTER - (double)valve_pos_ref * ((double)VALVE_MIN_POS-(double)VALVE_CENTER)/10000.0f; |
Lightvalve | 169:645207e160ca | 2253 | } |
Lightvalve | 169:645207e160ca | 2254 | ref_array[cnt_freq_test] = valve_pos_ref; |
Lightvalve | 169:645207e160ca | 2255 | if(value>=(float) VALVE_CENTER) { |
Lightvalve | 169:645207e160ca | 2256 | pos_array[cnt_freq_test] = 10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER); |
Lightvalve | 169:645207e160ca | 2257 | } else { |
Lightvalve | 169:645207e160ca | 2258 | pos_array[cnt_freq_test] = -10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER); |
Lightvalve | 169:645207e160ca | 2259 | } |
Lightvalve | 58:2eade98630e2 | 2260 | |
Lightvalve | 169:645207e160ca | 2261 | CONTROL_MODE = MODE_VALVE_POSITION_CONTROL; |
Lightvalve | 169:645207e160ca | 2262 | cnt_freq_test++; |
Lightvalve | 169:645207e160ca | 2263 | if (freq_test_valve_ref * (float) cnt_freq_test * DT_TMR3 > 2) { |
Lightvalve | 169:645207e160ca | 2264 | buffer_data_size = cnt_freq_test; |
Lightvalve | 169:645207e160ca | 2265 | cnt_freq_test = 0; |
Lightvalve | 169:645207e160ca | 2266 | cnt_send_buffer = 0; |
Lightvalve | 169:645207e160ca | 2267 | freq_test_valve_ref = freq_test_valve_ref * 1.05f; |
Lightvalve | 169:645207e160ca | 2268 | if (freq_test_valve_ref >= 400) { |
Lightvalve | 169:645207e160ca | 2269 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 169:645207e160ca | 2270 | CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 169:645207e160ca | 2271 | CAN_TX_PWM((int16_t) (1)); //1300 |
Lightvalve | 169:645207e160ca | 2272 | } |
Lightvalve | 169:645207e160ca | 2273 | CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 169:645207e160ca | 2274 | CONTROL_UTILITY_MODE = MODE_SEND_OVER; |
Lightvalve | 170:42c938a40313 | 2275 | |
Lightvalve | 169:645207e160ca | 2276 | } |
Lightvalve | 169:645207e160ca | 2277 | break; |
Lightvalve | 169:645207e160ca | 2278 | } |
Lightvalve | 169:645207e160ca | 2279 | case MODE_SEND_BUFFER: { |
Lightvalve | 169:645207e160ca | 2280 | // if (TMR2_COUNT_CAN_TX % (int) ((int) TMR_FREQ_5k/CAN_FREQ) == 0) { |
Lightvalve | 169:645207e160ca | 2281 | // CAN_TX_PRES((int16_t) (pos_array[cnt_send_buffer]), (int16_t) (ref_array[cnt_send_buffer])); // 1400 |
Lightvalve | 169:645207e160ca | 2282 | // if(cnt_send_buffer>=buffer_data_size) { |
Lightvalve | 169:645207e160ca | 2283 | // CONTROL_UTILITY_MODE = MODE_FREQ_TEST; |
Lightvalve | 169:645207e160ca | 2284 | // } |
Lightvalve | 169:645207e160ca | 2285 | // cnt_send_buffer++; |
Lightvalve | 169:645207e160ca | 2286 | // } |
Lightvalve | 169:645207e160ca | 2287 | |
Lightvalve | 169:645207e160ca | 2288 | break; |
Lightvalve | 169:645207e160ca | 2289 | } |
Lightvalve | 169:645207e160ca | 2290 | case MODE_SEND_OVER: { |
Lightvalve | 169:645207e160ca | 2291 | CAN_TX_TORQUE((int16_t) (buffer_data_size)); //1300 |
Lightvalve | 169:645207e160ca | 2292 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 169:645207e160ca | 2293 | CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 169:645207e160ca | 2294 | break; |
Lightvalve | 169:645207e160ca | 2295 | } |
Lightvalve | 170:42c938a40313 | 2296 | |
Lightvalve | 169:645207e160ca | 2297 | case MODE_STEP_TEST: { |
Lightvalve | 169:645207e160ca | 2298 | float valve_pos_ref = 0.0f; |
Lightvalve | 170:42c938a40313 | 2299 | if (cnt_step_test < (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 169:645207e160ca | 2300 | valve_pos_ref = 0.0f; |
Lightvalve | 170:42c938a40313 | 2301 | } else { |
Lightvalve | 169:645207e160ca | 2302 | valve_pos_ref = 10000.0f; |
Lightvalve | 169:645207e160ca | 2303 | } |
Lightvalve | 169:645207e160ca | 2304 | if(valve_pos_ref >= 0) { |
Lightvalve | 169:645207e160ca | 2305 | valve_pos.ref = (double)VALVE_CENTER + (double)valve_pos_ref * ((double)VALVE_MAX_POS-(double)VALVE_CENTER)/10000.0f; |
Lightvalve | 169:645207e160ca | 2306 | } else { |
Lightvalve | 169:645207e160ca | 2307 | valve_pos.ref = (double)VALVE_CENTER - (double)valve_pos_ref * ((double)VALVE_MIN_POS-(double)VALVE_CENTER)/10000.0f; |
Lightvalve | 169:645207e160ca | 2308 | } |
Lightvalve | 169:645207e160ca | 2309 | ref_array[cnt_step_test] = valve_pos_ref; |
Lightvalve | 169:645207e160ca | 2310 | if(value>=(float) VALVE_CENTER) { |
Lightvalve | 169:645207e160ca | 2311 | pos_array[cnt_step_test] = 10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER); |
Lightvalve | 169:645207e160ca | 2312 | } else { |
Lightvalve | 169:645207e160ca | 2313 | pos_array[cnt_step_test] = -10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER); |
Lightvalve | 169:645207e160ca | 2314 | } |
Lightvalve | 169:645207e160ca | 2315 | |
Lightvalve | 169:645207e160ca | 2316 | CONTROL_MODE = MODE_VALVE_POSITION_CONTROL; |
Lightvalve | 169:645207e160ca | 2317 | cnt_step_test++; |
Lightvalve | 170:42c938a40313 | 2318 | if (cnt_step_test > (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 169:645207e160ca | 2319 | buffer_data_size = cnt_step_test; |
Lightvalve | 169:645207e160ca | 2320 | cnt_step_test = 0; |
Lightvalve | 169:645207e160ca | 2321 | cnt_send_buffer = 0; |
Lightvalve | 169:645207e160ca | 2322 | CONTROL_UTILITY_MODE = MODE_SEND_OVER; |
Lightvalve | 169:645207e160ca | 2323 | CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 169:645207e160ca | 2324 | } |
Lightvalve | 169:645207e160ca | 2325 | // if (cnt_step_test > (int) (2.0f * (float) TMR_FREQ_5k)) |
Lightvalve | 169:645207e160ca | 2326 | // { |
Lightvalve | 169:645207e160ca | 2327 | // CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 169:645207e160ca | 2328 | // CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 169:645207e160ca | 2329 | // CAN_TX_PWM((int16_t) (1)); //1300 |
Lightvalve | 169:645207e160ca | 2330 | // } |
Lightvalve | 170:42c938a40313 | 2331 | |
Lightvalve | 169:645207e160ca | 2332 | break; |
Lightvalve | 169:645207e160ca | 2333 | } |
Lightvalve | 57:f4819de54e7a | 2334 | |
Lightvalve | 57:f4819de54e7a | 2335 | default: |
Lightvalve | 57:f4819de54e7a | 2336 | break; |
Lightvalve | 57:f4819de54e7a | 2337 | } |
Lightvalve | 57:f4819de54e7a | 2338 | |
Lightvalve | 57:f4819de54e7a | 2339 | // CONTROL MODE ------------------------------------------------------------ |
Lightvalve | 57:f4819de54e7a | 2340 | |
Lightvalve | 57:f4819de54e7a | 2341 | switch (CONTROL_MODE) { |
Lightvalve | 57:f4819de54e7a | 2342 | case MODE_NO_ACT: { |
Lightvalve | 57:f4819de54e7a | 2343 | V_out = 0.0f; |
Lightvalve | 57:f4819de54e7a | 2344 | break; |
Lightvalve | 57:f4819de54e7a | 2345 | } |
Lightvalve | 57:f4819de54e7a | 2346 | |
Lightvalve | 57:f4819de54e7a | 2347 | case MODE_VALVE_POSITION_CONTROL: { |
Lightvalve | 67:c2812cf26c38 | 2348 | if (OPERATING_MODE == 5) { //SW Valve |
Lightvalve | 225:278b48b86f27 | 2349 | ////For Test LIMC////////////////////////////////////////// |
Lightvalve | 233:0a37cdd59651 | 2350 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 233:0a37cdd59651 | 2351 | // for(int i=0; i<9; i++){ |
Lightvalve | 233:0a37cdd59651 | 2352 | // valve_ref_pos_buffer[i] = valve_ref_pos_buffer[i+1]; |
Lightvalve | 233:0a37cdd59651 | 2353 | // } |
Lightvalve | 233:0a37cdd59651 | 2354 | // valve_ref_pos_buffer[9] = valve_pos.ref; |
Lightvalve | 233:0a37cdd59651 | 2355 | // VALVE_POS_CONTROL(valve_ref_pos_buffer[0]); |
Lightvalve | 225:278b48b86f27 | 2356 | //////////////////////////////////////////////////////////// |
Lightvalve | 225:278b48b86f27 | 2357 | |
Lightvalve | 59:f308b1656d9c | 2358 | V_out = Vout.ref; |
Lightvalve | 67:c2812cf26c38 | 2359 | } else if (CURRENT_CONTROL_MODE == 0) { //PWM |
Lightvalve | 67:c2812cf26c38 | 2360 | V_out = valve_pos.ref; |
Lightvalve | 169:645207e160ca | 2361 | } else { |
Lightvalve | 67:c2812cf26c38 | 2362 | I_REF = valve_pos.ref * 0.001f; |
Lightvalve | 57:f4819de54e7a | 2363 | } |
Lightvalve | 57:f4819de54e7a | 2364 | break; |
Lightvalve | 57:f4819de54e7a | 2365 | } |
Lightvalve | 57:f4819de54e7a | 2366 | |
Lightvalve | 57:f4819de54e7a | 2367 | case MODE_JOINT_CONTROL: { |
Lightvalve | 169:645207e160ca | 2368 | |
Lightvalve | 57:f4819de54e7a | 2369 | double torq_ref = 0.0f; |
Lightvalve | 57:f4819de54e7a | 2370 | pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm] |
Lightvalve | 67:c2812cf26c38 | 2371 | vel.err = (0.0f - vel.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm/s] |
Lightvalve | 57:f4819de54e7a | 2372 | pos.err_sum += pos.err/(float) TMR_FREQ_5k; //[mm] |
Lightvalve | 169:645207e160ca | 2373 | |
Lightvalve | 67:c2812cf26c38 | 2374 | //K & D Low Pass Filter |
Lightvalve | 67:c2812cf26c38 | 2375 | float alpha_K_D = 1.0f/(1.0f + 5000.0f/(2.0f*3.14f*30.0f)); // f_cutoff : 30Hz |
Lightvalve | 67:c2812cf26c38 | 2376 | K_LPF = K_LPF*(1.0f-alpha_K_D)+K_SPRING*(alpha_K_D); |
Lightvalve | 67:c2812cf26c38 | 2377 | D_LPF = D_LPF*(1.0f-alpha_K_D)+D_DAMPER*(alpha_K_D); |
Lightvalve | 169:645207e160ca | 2378 | |
Lightvalve | 139:15621998925b | 2379 | // torq_ref = torq.ref + K_LPF * pos.err - D_LPF * vel.sen / ENC_PULSE_PER_POSITION; //[N] |
Lightvalve | 139:15621998925b | 2380 | torq_ref = torq.ref; |
Lightvalve | 57:f4819de54e7a | 2381 | |
Lightvalve | 57:f4819de54e7a | 2382 | // torque feedback |
Lightvalve | 67:c2812cf26c38 | 2383 | torq.err = torq_ref - torq.sen; //[N] |
Lightvalve | 57:f4819de54e7a | 2384 | torq.err_sum += torq.err/(float) TMR_FREQ_5k; //[N] |
Lightvalve | 57:f4819de54e7a | 2385 | |
Lightvalve | 57:f4819de54e7a | 2386 | if (((OPERATING_MODE&0b110)>>1) == 0 || ((OPERATING_MODE&0b110)>>1) == 1) { |
Lightvalve | 58:2eade98630e2 | 2387 | |
Lightvalve | 57:f4819de54e7a | 2388 | double I_REF_POS = 0.0f; |
Lightvalve | 57:f4819de54e7a | 2389 | double I_REF_FORCE_FB = 0.0f; // I_REF by Force Feedback |
Lightvalve | 57:f4819de54e7a | 2390 | double I_REF_VC = 0.0f; // I_REF for velocity compensation |
Lightvalve | 57:f4819de54e7a | 2391 | |
Lightvalve | 57:f4819de54e7a | 2392 | double temp_vel_pos = 0.0f; |
Lightvalve | 57:f4819de54e7a | 2393 | double temp_vel_torq = 0.0f; |
Lightvalve | 57:f4819de54e7a | 2394 | double wn_Pos = 2.0f * PI * 5.0f; // f_cut : 5Hz Position Control |
Lightvalve | 58:2eade98630e2 | 2395 | |
Lightvalve | 69:3995ffeaa786 | 2396 | if ((OPERATING_MODE & 0x01) == 0) { // Rotary Mode |
Lightvalve | 57:f4819de54e7a | 2397 | 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 | 2398 | // L when P-gain = 100, f_cut = 10Hz L feedforward velocity |
Lightvalve | 69:3995ffeaa786 | 2399 | } else if ((OPERATING_MODE & 0x01) == 1) { |
Lightvalve | 57:f4819de54e7a | 2400 | 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 | 2401 | // L when P-gain = 100, f_cut = 10Hz L feedforward velocity |
Lightvalve | 57:f4819de54e7a | 2402 | } |
Lightvalve | 57:f4819de54e7a | 2403 | 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 | 2404 | else I_REF_POS = temp_vel_pos * ((double) PISTON_AREA_B * 0.00006f / (K_v * sqrt(2.0f / (alpha3 + 1.0f)))); |
Lightvalve | 57:f4819de54e7a | 2405 | |
Lightvalve | 57:f4819de54e7a | 2406 | // velocity compensation for torque control |
Lightvalve | 69:3995ffeaa786 | 2407 | if ((OPERATING_MODE & 0x01) == 0) { // Rotary Mode |
Lightvalve | 57:f4819de54e7a | 2408 | I_REF_FORCE_FB = 0.001f * ((double) P_GAIN_JOINT_TORQUE * torq.err + (double) I_GAIN_JOINT_TORQUE * torq.err_sum); |
Lightvalve | 57:f4819de54e7a | 2409 | // 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 | 2410 | temp_vel_torq = (0.01f * (double) VELOCITY_COMP_GAIN * vel.ref / (double) ENC_PULSE_PER_POSITION) * PI / 180.0f; // rad/s |
Lightvalve | 57:f4819de54e7a | 2411 | // L feedforward velocity |
Lightvalve | 69:3995ffeaa786 | 2412 | } else if ((OPERATING_MODE & 0x01) == 1) { |
Lightvalve | 57:f4819de54e7a | 2413 | 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 | 2414 | // temp_vel_torq = (0.01 * (double) VELOCITY_COMP_GAIN * (double) CUR_VELOCITY / (double) ENC_PULSE_PER_POSITION); // mm/s |
Lightvalve | 57:f4819de54e7a | 2415 | temp_vel_torq = (0.01f * (double) VELOCITY_COMP_GAIN * vel.ref / (double) ENC_PULSE_PER_POSITION); // mm/s |
Lightvalve | 57:f4819de54e7a | 2416 | // L feedforward velocity |
Lightvalve | 57:f4819de54e7a | 2417 | } |
Lightvalve | 57:f4819de54e7a | 2418 | 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 | 2419 | else I_REF_VC = temp_vel_torq * ((double) PISTON_AREA_B * 0.00006f / (K_v * sqrt(2.0f / (alpha3 + 1.0f)))); |
Lightvalve | 57:f4819de54e7a | 2420 | // L velocity(rad/s or mm/s) >> I_ref(mA) |
Lightvalve | 57:f4819de54e7a | 2421 | // Ref_Joint_FT_dot = (Ref_Joint_FT_Nm - Ref_Joint_FT_Nm_old) / TMR_DT_5k; |
Lightvalve | 57:f4819de54e7a | 2422 | // Ref_Joint_FT_Nm_old = Ref_Joint_FT_Nm; |
Lightvalve | 57:f4819de54e7a | 2423 | |
Lightvalve | 57:f4819de54e7a | 2424 | I_REF = (1.0f - alpha_trans) * I_REF_POS + alpha_trans * (I_REF_VC + I_REF_FORCE_FB); |
Lightvalve | 169:645207e160ca | 2425 | |
Lightvalve | 67:c2812cf26c38 | 2426 | // Anti-windup for FT |
Lightvalve | 67:c2812cf26c38 | 2427 | if (I_GAIN_JOINT_TORQUE != 0) { |
Lightvalve | 67:c2812cf26c38 | 2428 | double I_MAX = 10.0f; // Maximum Current : 10mV |
Lightvalve | 67:c2812cf26c38 | 2429 | double Ka = 2.0f / ((double) I_GAIN_JOINT_TORQUE * 0.001f); |
Lightvalve | 67:c2812cf26c38 | 2430 | if (I_REF > I_MAX) { |
Lightvalve | 67:c2812cf26c38 | 2431 | double I_rem = I_REF - I_MAX; |
Lightvalve | 67:c2812cf26c38 | 2432 | I_rem = Ka*I_rem; |
Lightvalve | 67:c2812cf26c38 | 2433 | I_REF = I_MAX; |
Lightvalve | 67:c2812cf26c38 | 2434 | torq.err_sum = torq.err_sum - I_rem /(float) TMR_FREQ_5k; |
Lightvalve | 67:c2812cf26c38 | 2435 | } else 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 | } |
Lightvalve | 67:c2812cf26c38 | 2441 | } |
Lightvalve | 57:f4819de54e7a | 2442 | |
Lightvalve | 57:f4819de54e7a | 2443 | } else { |
Lightvalve | 57:f4819de54e7a | 2444 | float VALVE_POS_RAW_FORCE_FB = 0.0f; |
Lightvalve | 72:3436ce769b1e | 2445 | float VALVE_POS_RAW_FORCE_FF = 0.0f; |
Lightvalve | 72:3436ce769b1e | 2446 | float VALVE_POS_RAW = 0.0f; |
Lightvalve | 169:645207e160ca | 2447 | |
Lightvalve | 209:ebc69d6ee6f1 | 2448 | VALVE_POS_RAW_FORCE_FB = alpha_trans*(((float) P_GAIN_JOINT_TORQUE * torq.err + (float) I_GAIN_JOINT_TORQUE * torq.err_sum + (float) D_GAIN_JOINT_TORQUE * (torq.ref_diff - torq_dot.sen)) * 0.01f + 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 | 2449 | |
Lightvalve | 72:3436ce769b1e | 2450 | 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 | 2451 | |
Lightvalve | 72:3436ce769b1e | 2452 | VALVE_POS_RAW = VALVE_POS_RAW_FORCE_FB + VALVE_POS_RAW_FORCE_FF; |
Lightvalve | 169:645207e160ca | 2453 | |
Lightvalve | 169:645207e160ca | 2454 | |
Lightvalve | 72:3436ce769b1e | 2455 | if (VALVE_POS_RAW >= 0) { |
Lightvalve | 72:3436ce769b1e | 2456 | valve_pos.ref = VALVE_POS_RAW + VALVE_DEADZONE_PLUS; |
Lightvalve | 57:f4819de54e7a | 2457 | } else { |
Lightvalve | 72:3436ce769b1e | 2458 | valve_pos.ref = VALVE_POS_RAW + VALVE_DEADZONE_MINUS; |
Lightvalve | 57:f4819de54e7a | 2459 | } |
Lightvalve | 57:f4819de54e7a | 2460 | |
Lightvalve | 57:f4819de54e7a | 2461 | if(I_GAIN_JOINT_TORQUE != 0) { |
Lightvalve | 139:15621998925b | 2462 | double Ka = 2.0f / (double) I_GAIN_JOINT_TORQUE * 100.0f; |
Lightvalve | 57:f4819de54e7a | 2463 | if(valve_pos.ref>VALVE_MAX_POS) { |
Lightvalve | 57:f4819de54e7a | 2464 | double valve_pos_rem = valve_pos.ref - VALVE_MAX_POS; |
Lightvalve | 57:f4819de54e7a | 2465 | valve_pos_rem = valve_pos_rem * Ka; |
Lightvalve | 57:f4819de54e7a | 2466 | valve_pos.ref = VALVE_MAX_POS; |
Lightvalve | 57:f4819de54e7a | 2467 | torq.err_sum = torq.err_sum - valve_pos_rem/(float) TMR_FREQ_5k; |
Lightvalve | 57:f4819de54e7a | 2468 | } else if(valve_pos.ref < VALVE_MIN_POS) { |
Lightvalve | 57:f4819de54e7a | 2469 | double valve_pos_rem = valve_pos.ref - VALVE_MIN_POS; |
Lightvalve | 57:f4819de54e7a | 2470 | valve_pos_rem = valve_pos_rem * Ka; |
Lightvalve | 57:f4819de54e7a | 2471 | valve_pos.ref = VALVE_MIN_POS; |
Lightvalve | 57:f4819de54e7a | 2472 | torq.err_sum = torq.err_sum - valve_pos_rem/(float) TMR_FREQ_5k; |
Lightvalve | 57:f4819de54e7a | 2473 | } |
Lightvalve | 57:f4819de54e7a | 2474 | } |
Lightvalve | 61:bc8c8270f0ab | 2475 | |
Lightvalve | 57:f4819de54e7a | 2476 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 61:bc8c8270f0ab | 2477 | |
Lightvalve | 67:c2812cf26c38 | 2478 | // Vout.ref = (float) P_GAIN_JOINT_POSITION * 0.01f * ((float) pos.err); |
Lightvalve | 60:64181f1d3e60 | 2479 | V_out = (float) Vout.ref; |
Lightvalve | 57:f4819de54e7a | 2480 | |
Lightvalve | 57:f4819de54e7a | 2481 | } |
Lightvalve | 169:645207e160ca | 2482 | |
Lightvalve | 72:3436ce769b1e | 2483 | torq_ref_past = torq_ref; |
Lightvalve | 58:2eade98630e2 | 2484 | |
Lightvalve | 133:22ab22818e01 | 2485 | |
Lightvalve | 57:f4819de54e7a | 2486 | break; |
Lightvalve | 57:f4819de54e7a | 2487 | } |
Lightvalve | 58:2eade98630e2 | 2488 | |
Lightvalve | 57:f4819de54e7a | 2489 | case MODE_VALVE_OPEN_LOOP: { |
Lightvalve | 57:f4819de54e7a | 2490 | V_out = (float) Vout.ref; |
Lightvalve | 57:f4819de54e7a | 2491 | break; |
Lightvalve | 57:f4819de54e7a | 2492 | } |
Lightvalve | 169:645207e160ca | 2493 | |
Lightvalve | 138:a843f32ced33 | 2494 | case MODE_JOINT_ADAPTIVE_BACKSTEPPING: { |
Lightvalve | 169:645207e160ca | 2495 | |
Lightvalve | 169:645207e160ca | 2496 | |
Lightvalve | 139:15621998925b | 2497 | 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 | 2498 | 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 | 218:066030f7951f | 2499 | |
Lightvalve | 170:42c938a40313 | 2500 | V_adapt = 1.0f / (1.0f/Va + 1.0f/Vb); //initial 0.0000053f |
Lightvalve | 169:645207e160ca | 2501 | |
Lightvalve | 218:066030f7951f | 2502 | //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 | 218:066030f7951f | 2503 | float f3_hat = -a_hat * vel.sen/(float)(ENC_PULSE_PER_POSITION)*0.001f; // unit : N/s //xdot=10mm/s일때 -137076 |
Lightvalve | 169:645207e160ca | 2504 | |
Lightvalve | 138:a843f32ced33 | 2505 | float g3_prime = 0.0f; |
Lightvalve | 169:645207e160ca | 2506 | if (torq.sen > Amm*(Ps-Pt)*0.000001f) { |
Lightvalve | 138:a843f32ced33 | 2507 | g3_prime = 1.0f; |
Lightvalve | 138:a843f32ced33 | 2508 | } else if (torq.sen < -Amm*(Ps-Pt)*0.000001f) { |
Lightvalve | 138:a843f32ced33 | 2509 | g3_prime = -1.0f; |
Lightvalve | 138:a843f32ced33 | 2510 | } else { |
Lightvalve | 138:a843f32ced33 | 2511 | if ((value-VALVE_CENTER) > 0) { |
Lightvalve | 169:645207e160ca | 2512 | g3_prime = sqrt(Ps-Pt-torq.sen/Amm*1000000.0f); |
Lightvalve | 169:645207e160ca | 2513 | // g3_prime = sqrt(Ps-Pt); |
Lightvalve | 138:a843f32ced33 | 2514 | } else { |
Lightvalve | 138:a843f32ced33 | 2515 | g3_prime = sqrt(Ps-Pt+torq.sen/Amm*1000000.0f); |
Lightvalve | 139:15621998925b | 2516 | // g3_prime = sqrt(Ps-Pt); |
Lightvalve | 138:a843f32ced33 | 2517 | } |
Lightvalve | 138:a843f32ced33 | 2518 | } |
Lightvalve | 138:a843f32ced33 | 2519 | float tau = 0.01f; |
Lightvalve | 142:43026242815a | 2520 | float K_valve = 0.0004f; |
Lightvalve | 169:645207e160ca | 2521 | |
Lightvalve | 138:a843f32ced33 | 2522 | float x_v = 0.0f; //x_v : -1~1 |
Lightvalve | 138:a843f32ced33 | 2523 | if(value>=VALVE_CENTER) { |
Lightvalve | 138:a843f32ced33 | 2524 | x_v = 1.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER); |
Lightvalve | 138:a843f32ced33 | 2525 | } else { |
Lightvalve | 138:a843f32ced33 | 2526 | x_v = -1.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER); |
Lightvalve | 138:a843f32ced33 | 2527 | } |
Lightvalve | 138:a843f32ced33 | 2528 | float f4 = -x_v/tau; |
Lightvalve | 138:a843f32ced33 | 2529 | float g4 = K_valve/tau; |
Lightvalve | 169:645207e160ca | 2530 | |
Lightvalve | 139:15621998925b | 2531 | float torq_ref_dot = torq.ref_diff * 500.0f; |
Lightvalve | 169:645207e160ca | 2532 | |
Lightvalve | 138:a843f32ced33 | 2533 | pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm] |
Lightvalve | 138:a843f32ced33 | 2534 | vel.err = (0.0f - vel.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm/s] |
Lightvalve | 138:a843f32ced33 | 2535 | pos.err_sum += pos.err/(float) TMR_FREQ_5k; //[mm] |
Lightvalve | 169:645207e160ca | 2536 | |
Lightvalve | 138:a843f32ced33 | 2537 | torq.err = torq.ref - torq.sen; //[N] |
Lightvalve | 138:a843f32ced33 | 2538 | torq.err_sum += torq.err/(float) TMR_FREQ_5k; //[N] |
Lightvalve | 169:645207e160ca | 2539 | |
Lightvalve | 212:ec41f1449ef9 | 2540 | float k3 = 2000.0f; //2000 //20000 |
Lightvalve | 142:43026242815a | 2541 | float k4 = 10.0f; |
Lightvalve | 142:43026242815a | 2542 | float rho3 = 3.2f; |
Lightvalve | 212:ec41f1449ef9 | 2543 | float rho4 = 10000000.0f; //25000000.0f; |
Lightvalve | 218:066030f7951f | 2544 | float x_4_des = (-f3_hat + torq_ref_dot - k3*(-torq.err))/(gamma_hat*g3_prime); |
Lightvalve | 138:a843f32ced33 | 2545 | if (x_4_des > 1) x_4_des = 1; |
Lightvalve | 138:a843f32ced33 | 2546 | else if (x_4_des < -1) x_4_des = -1; |
Lightvalve | 169:645207e160ca | 2547 | |
Lightvalve | 139:15621998925b | 2548 | if (x_4_des > 0) { |
Lightvalve | 139:15621998925b | 2549 | valve_pos.ref = x_4_des * (float)(VALVE_MAX_POS - VALVE_CENTER) + (float) VALVE_CENTER; |
Lightvalve | 139:15621998925b | 2550 | } else { |
Lightvalve | 139:15621998925b | 2551 | valve_pos.ref = x_4_des * (float)(VALVE_CENTER - VALVE_MIN_POS) + (float) VALVE_CENTER; |
Lightvalve | 139:15621998925b | 2552 | } |
Lightvalve | 169:645207e160ca | 2553 | |
Lightvalve | 138:a843f32ced33 | 2554 | float x_4_des_dot = (x_4_des - x_4_des_old)*(float) TMR_FREQ_5k; |
Lightvalve | 138:a843f32ced33 | 2555 | x_4_des_old = x_4_des; |
Lightvalve | 212:ec41f1449ef9 | 2556 | float V_input = 0.0f; |
Lightvalve | 139:15621998925b | 2557 | V_out = (-f4 + x_4_des_dot - k4*(x_v-x_4_des)- rho3/rho4*gamma_hat*g3_prime*(-torq.err))/g4; |
Lightvalve | 212:ec41f1449ef9 | 2558 | // //V_out LPF |
Lightvalve | 212:ec41f1449ef9 | 2559 | // float alpha_V_out = 1.0f/(1.0f + 5000.0f/(2.0f*3.14f*50.0f)); // f_cutoff : 50Hz |
Lightvalve | 212:ec41f1449ef9 | 2560 | // V_out = V_out*(1.0f-alpha_V_out)+V_input*(alpha_V_out); |
Lightvalve | 212:ec41f1449ef9 | 2561 | |
Lightvalve | 218:066030f7951f | 2562 | // float rho_gamma = 5000.0f;//5000 for change //50000 for not change |
Lightvalve | 218:066030f7951f | 2563 | // 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 | 218:066030f7951f | 2564 | // gamma_hat = gamma_hat + gamma_hat_dot / (float) TMR_FREQ_5k; |
Lightvalve | 218:066030f7951f | 2565 | // |
Lightvalve | 218:066030f7951f | 2566 | // if(gamma_hat > 10000.0f) gamma_hat = 10000.0f; |
Lightvalve | 218:066030f7951f | 2567 | // else if(gamma_hat < 100.0f) gamma_hat = 100.0f; |
Lightvalve | 218:066030f7951f | 2568 | |
Lightvalve | 218:066030f7951f | 2569 | float rho_a = 0.00001f; |
Lightvalve | 218:066030f7951f | 2570 | float a_hat_dot = -rho3/rho_a*vel.sen/(float)(ENC_PULSE_PER_POSITION)*0.001f*(-torq.err); |
Lightvalve | 218:066030f7951f | 2571 | a_hat = a_hat + a_hat_dot / (float) TMR_FREQ_5k; |
Lightvalve | 218:066030f7951f | 2572 | |
Lightvalve | 218:066030f7951f | 2573 | if(a_hat > -3000000.0f) a_hat = -3000000.0f; |
Lightvalve | 218:066030f7951f | 2574 | else if(a_hat < -30000000.0f) a_hat = -30000000.0f; |
Lightvalve | 218:066030f7951f | 2575 | |
Lightvalve | 169:645207e160ca | 2576 | break; |
Lightvalve | 138:a843f32ced33 | 2577 | } |
Lightvalve | 171:bfc1fd2629d8 | 2578 | |
Lightvalve | 170:42c938a40313 | 2579 | case MODE_RL: { |
Lightvalve | 170:42c938a40313 | 2580 | //t.reset(); |
Lightvalve | 170:42c938a40313 | 2581 | //t.start(); |
Lightvalve | 170:42c938a40313 | 2582 | |
Lightvalve | 170:42c938a40313 | 2583 | // if(LED == 0) LED = 1; |
Lightvalve | 170:42c938a40313 | 2584 | // else LED = 0; |
Lightvalve | 170:42c938a40313 | 2585 | |
Lightvalve | 170:42c938a40313 | 2586 | if (Update_Done_Flag == 1) { |
Lightvalve | 170:42c938a40313 | 2587 | //Gather Data on each loop |
Lightvalve | 170:42c938a40313 | 2588 | // pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm] |
Lightvalve | 170:42c938a40313 | 2589 | // train_set_x[RL_timer] = pos.sen/(float)(ENC_PULSE_PER_POSITION)/35.0f - 1.0f; //-1.0~1.0 |
Lightvalve | 170:42c938a40313 | 2590 | // train_set_error[RL_timer] = pos.err/70.0f; //-1.0~1.0 |
Lightvalve | 170:42c938a40313 | 2591 | pos.err = pos.sen/(float)(ENC_PULSE_PER_POSITION) - virt_pos; //[mm] |
Lightvalve | 170:42c938a40313 | 2592 | train_set_x[RL_timer] = virt_pos/70.0f; //-1.0~1.0 |
Lightvalve | 170:42c938a40313 | 2593 | train_set_error[RL_timer] = pos.err/70.0f; //-1.0~1.0 |
Lightvalve | 170:42c938a40313 | 2594 | //train_set_count[RL_timer] = (float) RL_timer / (batch_size *num_batch); //-1.0~1.0 |
Lightvalve | 170:42c938a40313 | 2595 | //float temp_array[3] = {train_set_x[RL_timer], train_set_error[RL_timer], train_set_count[RL_timer]}; |
Lightvalve | 170:42c938a40313 | 2596 | float temp_array[2] = {train_set_x[RL_timer], train_set_error[RL_timer]}; |
Lightvalve | 170:42c938a40313 | 2597 | Actor_Network(temp_array); |
Lightvalve | 173:68c7914679ec | 2598 | for (int i=0; i<num_hidden_unit1; i++) { |
Lightvalve | 173:68c7914679ec | 2599 | hx_a_sum_array[RL_timer][i] = hx_a_sum[i]; |
Lightvalve | 173:68c7914679ec | 2600 | } |
Lightvalve | 173:68c7914679ec | 2601 | for (int i=0; i<num_hidden_unit2; i++) { |
Lightvalve | 173:68c7914679ec | 2602 | hxh_a_sum_array[RL_timer][i] = hxh_a_sum[i]; |
Lightvalve | 173:68c7914679ec | 2603 | } |
Lightvalve | 173:68c7914679ec | 2604 | hxhh_a_sum_array[RL_timer][0] = hxhh_a_sum[0]; |
Lightvalve | 173:68c7914679ec | 2605 | hxhh_a_sum_array[RL_timer][1] = hxhh_a_sum[1]; |
Lightvalve | 170:42c938a40313 | 2606 | mean_array[RL_timer] = mean; |
Lightvalve | 170:42c938a40313 | 2607 | deviation_array[RL_timer] = deviation; |
Lightvalve | 170:42c938a40313 | 2608 | action_array[RL_timer] = rand_normal(mean_array[RL_timer], deviation_array[RL_timer]); |
Lightvalve | 170:42c938a40313 | 2609 | |
Lightvalve | 179:d5377766d7ea | 2610 | virt_pos = virt_pos + (action_array[RL_timer] - 5.0f) * 1000.0f * 0.0002f; |
Lightvalve | 179:d5377766d7ea | 2611 | if (virt_pos > 70 ) { |
Lightvalve | 170:42c938a40313 | 2612 | virt_pos = 70.0f; |
Lightvalve | 218:066030f7951f | 2613 | } else if(virt_pos < -70) { |
Lightvalve | 170:42c938a40313 | 2614 | virt_pos = -70.0f; |
Lightvalve | 170:42c938a40313 | 2615 | } |
Lightvalve | 171:bfc1fd2629d8 | 2616 | |
Lightvalve | 170:42c938a40313 | 2617 | RL_timer++; |
Lightvalve | 170:42c938a40313 | 2618 | |
Lightvalve | 170:42c938a40313 | 2619 | |
Lightvalve | 170:42c938a40313 | 2620 | if (RL_timer >= batch_size) { |
Lightvalve | 170:42c938a40313 | 2621 | RL_timer = 0; |
Lightvalve | 170:42c938a40313 | 2622 | batch++; |
Lightvalve | 170:42c938a40313 | 2623 | for(int i=0; i<batch_size; i++) { |
Lightvalve | 170:42c938a40313 | 2624 | state_array[i][0] = train_set_x[i]; |
Lightvalve | 170:42c938a40313 | 2625 | state_array[i][1] = train_set_error[i]; |
Lightvalve | 170:42c938a40313 | 2626 | //state_array[i][2] = train_set_count[i]; |
Lightvalve | 170:42c938a40313 | 2627 | } |
Lightvalve | 170:42c938a40313 | 2628 | Update_Case = 1; |
Lightvalve | 170:42c938a40313 | 2629 | Update_Done_Flag = 0; |
Lightvalve | 170:42c938a40313 | 2630 | logging1 = virt_pos; |
Lightvalve | 179:d5377766d7ea | 2631 | |
Lightvalve | 170:42c938a40313 | 2632 | if(batch >= num_batch) { |
Lightvalve | 170:42c938a40313 | 2633 | batch = 0; |
Lightvalve | 170:42c938a40313 | 2634 | RL_timer = 0; |
Lightvalve | 170:42c938a40313 | 2635 | Update_Case = 2; |
Lightvalve | 170:42c938a40313 | 2636 | Update_Done_Flag = 0; |
Lightvalve | 170:42c938a40313 | 2637 | virt_pos = 10.0f; |
Lightvalve | 170:42c938a40313 | 2638 | } |
Lightvalve | 170:42c938a40313 | 2639 | } |
Lightvalve | 170:42c938a40313 | 2640 | } |
Lightvalve | 170:42c938a40313 | 2641 | |
Lightvalve | 170:42c938a40313 | 2642 | else { |
Lightvalve | 170:42c938a40313 | 2643 | pos.err = pos.sen/(float)(ENC_PULSE_PER_POSITION) - virt_pos; //[mm] |
Lightvalve | 170:42c938a40313 | 2644 | float temp_array[3] = {0.0f}; |
Lightvalve | 170:42c938a40313 | 2645 | temp_array[0] = virt_pos/70.0f; //-1.0~1.0 |
Lightvalve | 170:42c938a40313 | 2646 | temp_array[1] = pos.err/70.0f; //-1.0~1.0 |
Lightvalve | 170:42c938a40313 | 2647 | //temp_array[2] = (float) RL_timer / (batch_size *num_batch); //-1.0~1.0 |
Lightvalve | 170:42c938a40313 | 2648 | Actor_Network(temp_array); |
Lightvalve | 170:42c938a40313 | 2649 | action = rand_normal(mean, deviation); |
Lightvalve | 170:42c938a40313 | 2650 | //logging1 = action; |
Lightvalve | 179:d5377766d7ea | 2651 | //logging2 = mean; |
Lightvalve | 179:d5377766d7ea | 2652 | //logging4 = deviation; |
Lightvalve | 179:d5377766d7ea | 2653 | virt_pos = virt_pos + (action-5.0f) * 1000.0f * 0.0002f; |
Lightvalve | 179:d5377766d7ea | 2654 | if (virt_pos > 70) { |
Lightvalve | 170:42c938a40313 | 2655 | virt_pos = 70.0f; |
Lightvalve | 218:066030f7951f | 2656 | } else if(virt_pos < -70) { |
Lightvalve | 170:42c938a40313 | 2657 | virt_pos = -70.0f; |
Lightvalve | 170:42c938a40313 | 2658 | } |
Lightvalve | 170:42c938a40313 | 2659 | |
Lightvalve | 170:42c938a40313 | 2660 | logging3 = virt_pos; |
Lightvalve | 170:42c938a40313 | 2661 | } |
Lightvalve | 170:42c938a40313 | 2662 | |
Lightvalve | 170:42c938a40313 | 2663 | //t.stop(); |
Lightvalve | 170:42c938a40313 | 2664 | //logging1 = t.read()*1000.0f; //msec |
Lightvalve | 170:42c938a40313 | 2665 | |
Lightvalve | 170:42c938a40313 | 2666 | break; |
Lightvalve | 170:42c938a40313 | 2667 | } |
Lightvalve | 14:8e7590227d22 | 2668 | |
Lightvalve | 12:6f2531038ea4 | 2669 | default: |
Lightvalve | 12:6f2531038ea4 | 2670 | break; |
Lightvalve | 12:6f2531038ea4 | 2671 | } |
Lightvalve | 14:8e7590227d22 | 2672 | |
Lightvalve | 57:f4819de54e7a | 2673 | |
Lightvalve | 57:f4819de54e7a | 2674 | if (((OPERATING_MODE&0b110)>>1) == 0 || ((OPERATING_MODE&0b110)>>1) == 1) { //Moog Valve or KNR Valve |
Lightvalve | 58:2eade98630e2 | 2675 | |
Lightvalve | 57:f4819de54e7a | 2676 | //////////////////////////////////////////////////////////////////////////// |
Lightvalve | 57:f4819de54e7a | 2677 | //////////////////////////// CURRENT CONTROL ////////////////////////////// |
Lightvalve | 57:f4819de54e7a | 2678 | //////////////////////////////////////////////////////////////////////////// |
Lightvalve | 57:f4819de54e7a | 2679 | if (CURRENT_CONTROL_MODE) { |
Lightvalve | 67:c2812cf26c38 | 2680 | double alpha_update_Iref = 1.0f / (1.0f + 5000.0f / (2.0f * 3.14f * 300.0f)); // f_cutoff : 500Hz |
Lightvalve | 57:f4819de54e7a | 2681 | I_REF_fil = (1.0f - alpha_update_Iref) * I_REF_fil + alpha_update_Iref*I_REF; |
Lightvalve | 57:f4819de54e7a | 2682 | |
Lightvalve | 57:f4819de54e7a | 2683 | I_ERR = I_REF_fil - cur.sen; |
Lightvalve | 67:c2812cf26c38 | 2684 | I_ERR_INT = I_ERR_INT + (I_ERR) * 0.0002f; |
Lightvalve | 57:f4819de54e7a | 2685 | |
Lightvalve | 57:f4819de54e7a | 2686 | |
Lightvalve | 57:f4819de54e7a | 2687 | // Moog Valve Current Control Gain |
Lightvalve | 67:c2812cf26c38 | 2688 | double R_model = 500.0f; // ohm |
Lightvalve | 57:f4819de54e7a | 2689 | double L_model = 1.2f; |
Lightvalve | 57:f4819de54e7a | 2690 | double w0 = 2.0f * 3.14f * 150.0f; |
Lightvalve | 57:f4819de54e7a | 2691 | double KP_I = 0.1f * L_model*w0; |
Lightvalve | 57:f4819de54e7a | 2692 | double KI_I = 0.1f * R_model*w0; |
Lightvalve | 57:f4819de54e7a | 2693 | |
Lightvalve | 57:f4819de54e7a | 2694 | // KNR Valve Current Control Gain |
Lightvalve | 57:f4819de54e7a | 2695 | if (((OPERATING_MODE & 0b110)>>1) == 1) { // KNR Valve |
Lightvalve | 57:f4819de54e7a | 2696 | R_model = 163.0f; // ohm |
Lightvalve | 57:f4819de54e7a | 2697 | L_model = 1.0f; |
Lightvalve | 57:f4819de54e7a | 2698 | w0 = 2.0f * 3.14f * 80.0f; |
Lightvalve | 57:f4819de54e7a | 2699 | KP_I = 1.0f * L_model*w0; |
Lightvalve | 57:f4819de54e7a | 2700 | KI_I = 0.08f * R_model*w0; |
Lightvalve | 57:f4819de54e7a | 2701 | } |
Lightvalve | 57:f4819de54e7a | 2702 | |
Lightvalve | 57:f4819de54e7a | 2703 | double FF_gain = 1.0f; |
Lightvalve | 57:f4819de54e7a | 2704 | |
Lightvalve | 67:c2812cf26c38 | 2705 | VALVE_PWM_RAW = KP_I * 2.0f * I_ERR + KI_I * 2.0f* I_ERR_INT; |
Lightvalve | 57:f4819de54e7a | 2706 | // VALVE_PWM_RAW = VALVE_PWM_RAW + FF_gain * (R_model*I_REF); // Unit : mV |
Lightvalve | 57:f4819de54e7a | 2707 | I_REF_fil_diff = I_REF_fil - I_REF_fil_old; |
Lightvalve | 57:f4819de54e7a | 2708 | I_REF_fil_old = I_REF_fil; |
Lightvalve | 67:c2812cf26c38 | 2709 | // 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 | 2710 | VALVE_PWM_RAW = VALVE_PWM_RAW + FF_gain * (R_model * I_REF_fil); // Unit : mV |
Lightvalve | 67:c2812cf26c38 | 2711 | double V_MAX = 12000.0f; // Maximum Voltage : 12V = 12000mV |
Lightvalve | 57:f4819de54e7a | 2712 | |
Lightvalve | 57:f4819de54e7a | 2713 | double Ka = 3.0f / KP_I; |
Lightvalve | 57:f4819de54e7a | 2714 | if (VALVE_PWM_RAW > V_MAX) { |
Lightvalve | 57:f4819de54e7a | 2715 | V_rem = VALVE_PWM_RAW - V_MAX; |
Lightvalve | 57:f4819de54e7a | 2716 | V_rem = Ka*V_rem; |
Lightvalve | 57:f4819de54e7a | 2717 | VALVE_PWM_RAW = V_MAX; |
Lightvalve | 67:c2812cf26c38 | 2718 | I_ERR_INT = I_ERR_INT - V_rem * 0.0002f; |
Lightvalve | 57:f4819de54e7a | 2719 | } else 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 | } |
Lightvalve | 57:f4819de54e7a | 2725 | Cur_Valve_Open_pulse = cur.sen / mA_PER_pulse; |
Lightvalve | 57:f4819de54e7a | 2726 | } else { |
Lightvalve | 57:f4819de54e7a | 2727 | VALVE_PWM_RAW = I_REF * mV_PER_mA; |
Lightvalve | 57:f4819de54e7a | 2728 | Cur_Valve_Open_pulse = I_REF / mA_PER_pulse; |
Lightvalve | 57:f4819de54e7a | 2729 | } |
Lightvalve | 57:f4819de54e7a | 2730 | |
Lightvalve | 57:f4819de54e7a | 2731 | //////////////////////////////////////////////////////////////////////////// |
Lightvalve | 57:f4819de54e7a | 2732 | ///////////////// Dead Zone Cancellation & Linearization ////////////////// |
Lightvalve | 57:f4819de54e7a | 2733 | //////////////////////////////////////////////////////////////////////////// |
Lightvalve | 57:f4819de54e7a | 2734 | // Dead Zone Cancellation (Mechanical Valve dead-zone) |
Lightvalve | 57:f4819de54e7a | 2735 | if (FLAG_VALVE_DEADZONE) { |
Lightvalve | 57:f4819de54e7a | 2736 | if (VALVE_PWM_RAW > 0) VALVE_PWM_RAW = VALVE_PWM_RAW + VALVE_DEADZONE_PLUS * mV_PER_pulse; // unit: mV |
Lightvalve | 57:f4819de54e7a | 2737 | else if (VALVE_PWM_RAW < 0) VALVE_PWM_RAW = VALVE_PWM_RAW + VALVE_DEADZONE_MINUS * mV_PER_pulse; // unit: mV |
Lightvalve | 57:f4819de54e7a | 2738 | |
Lightvalve | 57:f4819de54e7a | 2739 | VALVE_PWM_VALVE_DZ = VALVE_PWM_RAW + (double)VALVE_CENTER * mV_PER_pulse; // unit: mV |
Lightvalve | 57:f4819de54e7a | 2740 | |
Lightvalve | 57:f4819de54e7a | 2741 | } else { |
Lightvalve | 57:f4819de54e7a | 2742 | VALVE_PWM_VALVE_DZ = VALVE_PWM_RAW; |
Lightvalve | 57:f4819de54e7a | 2743 | } |
Lightvalve | 57:f4819de54e7a | 2744 | |
Lightvalve | 57:f4819de54e7a | 2745 | // Output Voltage Linearization |
Lightvalve | 57:f4819de54e7a | 2746 | double CUR_PWM_nonlin = VALVE_PWM_VALVE_DZ; // Unit : mV |
Lightvalve | 67:c2812cf26c38 | 2747 | double CUR_PWM_lin = PWM_duty_byLT(CUR_PWM_nonlin); // -8000~8000 |
Lightvalve | 57:f4819de54e7a | 2748 | |
Lightvalve | 57:f4819de54e7a | 2749 | // Dead Zone Cancellation (Electrical dead-zone) |
Lightvalve | 67:c2812cf26c38 | 2750 | if (CUR_PWM_lin > 0) V_out = (float) (CUR_PWM_lin + 169.0f); |
Lightvalve | 67:c2812cf26c38 | 2751 | else if (CUR_PWM_lin < 0) V_out = (float) (CUR_PWM_lin - 174.0f); |
Lightvalve | 67:c2812cf26c38 | 2752 | else V_out = (float) (CUR_PWM_lin); |
Lightvalve | 169:645207e160ca | 2753 | } else { //////////////////////////sw valve |
Lightvalve | 89:a7b45368ea0f | 2754 | // Output Voltage Linearization |
Lightvalve | 135:79885a39c161 | 2755 | // double CUR_PWM_nonlin = V_out; // Unit : mV |
Lightvalve | 135:79885a39c161 | 2756 | // double CUR_PWM_lin = PWM_duty_byLT(CUR_PWM_nonlin); // -8000~8000 |
Lightvalve | 89:a7b45368ea0f | 2757 | |
Lightvalve | 89:a7b45368ea0f | 2758 | // Dead Zone Cancellation (Electrical dead-zone) |
Lightvalve | 135:79885a39c161 | 2759 | // if (CUR_PWM_lin > 0) V_out = (float) (CUR_PWM_lin + 169.0f); |
Lightvalve | 135:79885a39c161 | 2760 | // else if (CUR_PWM_lin < 0) V_out = (float) (CUR_PWM_lin - 174.0f); |
Lightvalve | 135:79885a39c161 | 2761 | // else V_out = (float) (CUR_PWM_lin); |
Lightvalve | 135:79885a39c161 | 2762 | |
Lightvalve | 135:79885a39c161 | 2763 | if (V_out > 0 ) V_out = (V_out + 180.0f)/0.8588f; |
Lightvalve | 135:79885a39c161 | 2764 | else if (V_out < 0) V_out = (V_out - 200.0f)/0.8651f; |
Lightvalve | 135:79885a39c161 | 2765 | else V_out = 0.0f; |
Lightvalve | 89:a7b45368ea0f | 2766 | } |
Lightvalve | 169:645207e160ca | 2767 | |
Lightvalve | 67:c2812cf26c38 | 2768 | // if(V_out > 0.0f) V_out = (float) (V_out + 169.0f); |
Lightvalve | 67:c2812cf26c38 | 2769 | // else if(V_out < 0.0f) V_out = (float) (V_out - 174.0f); |
Lightvalve | 67:c2812cf26c38 | 2770 | // else V_out = V_out; |
Lightvalve | 169:645207e160ca | 2771 | |
jobuuu | 7:e9086c72bb22 | 2772 | /******************************************************* |
jobuuu | 7:e9086c72bb22 | 2773 | *** PWM |
jobuuu | 7:e9086c72bb22 | 2774 | ********************************************************/ |
Lightvalve | 169:645207e160ca | 2775 | if(DIR_VALVE<0) { |
Lightvalve | 67:c2812cf26c38 | 2776 | V_out = -V_out; |
Lightvalve | 67:c2812cf26c38 | 2777 | } |
Lightvalve | 169:645207e160ca | 2778 | |
Lightvalve | 49:e7bcfc244d40 | 2779 | if (V_out >= VALVE_VOLTAGE_LIMIT*1000.0f) { |
Lightvalve | 49:e7bcfc244d40 | 2780 | V_out = VALVE_VOLTAGE_LIMIT*1000.0f; |
Lightvalve | 49:e7bcfc244d40 | 2781 | } else if(V_out<=-VALVE_VOLTAGE_LIMIT*1000.0f) { |
Lightvalve | 49:e7bcfc244d40 | 2782 | V_out = -VALVE_VOLTAGE_LIMIT*1000.0f; |
Lightvalve | 27:a2254a485f23 | 2783 | } |
Lightvalve | 49:e7bcfc244d40 | 2784 | PWM_out= V_out/(SUPPLY_VOLTAGE*1000.0f); // Full duty : 12000.0mV |
Lightvalve | 13:747daba9cf59 | 2785 | |
Lightvalve | 19:23b7c1ad8683 | 2786 | // Saturation of output voltage to 12.0V |
Lightvalve | 30:8d561f16383b | 2787 | if(PWM_out > 1.0f) PWM_out=1.0f; |
Lightvalve | 30:8d561f16383b | 2788 | else if (PWM_out < -1.0f) PWM_out=-1.0f; |
Lightvalve | 13:747daba9cf59 | 2789 | |
Lightvalve | 30:8d561f16383b | 2790 | if (PWM_out>0.0f) { |
Lightvalve | 30:8d561f16383b | 2791 | dtc_v=0.0f; |
jobuuu | 1:e04e563be5ce | 2792 | dtc_w=PWM_out; |
jobuuu | 1:e04e563be5ce | 2793 | } else { |
jobuuu | 2:a1c0a37df760 | 2794 | dtc_v=-PWM_out; |
Lightvalve | 30:8d561f16383b | 2795 | dtc_w=0.0f; |
jobuuu | 1:e04e563be5ce | 2796 | } |
Lightvalve | 13:747daba9cf59 | 2797 | |
jobuuu | 1:e04e563be5ce | 2798 | //pwm |
Lightvalve | 30:8d561f16383b | 2799 | TIM4->CCR2 = (PWM_ARR)*(1.0f-dtc_v); |
Lightvalve | 30:8d561f16383b | 2800 | TIM4->CCR1 = (PWM_ARR)*(1.0f-dtc_w); |
Lightvalve | 67:c2812cf26c38 | 2801 | |
Lightvalve | 61:bc8c8270f0ab | 2802 | |
Lightvalve | 233:0a37cdd59651 | 2803 | |
Lightvalve | 233:0a37cdd59651 | 2804 | /* |
Lightvalve | 237:aa3863117889 | 2805 | if (TMR2_COUNT_CAN_TX % (int) ((int) TMR_FREQ_5k/CAN_FREQ) == 0) { |
Lightvalve | 233:0a37cdd59651 | 2806 | |
Lightvalve | 54:647072f5307a | 2807 | // Position, Velocity, and Torque (ID:1200) |
Lightvalve | 56:6f50d9d3bfee | 2808 | if (flag_data_request[0] == HIGH) { |
Lightvalve | 57:f4819de54e7a | 2809 | if ((OPERATING_MODE & 0b01) == 0) { // Rotary Actuator |
Lightvalve | 54:647072f5307a | 2810 | if (SENSING_MODE == 0) { |
Lightvalve | 67:c2812cf26c38 | 2811 | CAN_TX_POSITION_FT((int16_t) (pos.sen), (int16_t) (vel.sen/10.0f), (int16_t) (torq.sen*10.0f)); |
Lightvalve | 54:647072f5307a | 2812 | } else if (SENSING_MODE == 1) { |
Lightvalve | 58:2eade98630e2 | 2813 | 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 | 2814 | } |
Lightvalve | 57:f4819de54e7a | 2815 | } else if ((OPERATING_MODE & 0b01) == 1) { // Linear Actuator |
Lightvalve | 54:647072f5307a | 2816 | if (SENSING_MODE == 0) { |
Lightvalve | 67:c2812cf26c38 | 2817 | 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 | 2818 | } else if (SENSING_MODE == 1) { |
Lightvalve | 67:c2812cf26c38 | 2819 | 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 | 2820 | } |
Lightvalve | 52:8ea76864368a | 2821 | } |
Lightvalve | 52:8ea76864368a | 2822 | } |
Lightvalve | 233:0a37cdd59651 | 2823 | |
Lightvalve | 56:6f50d9d3bfee | 2824 | if (flag_data_request[1] == HIGH) { |
Lightvalve | 179:d5377766d7ea | 2825 | CAN_TX_TORQUE((int16_t) (return_G[0]*100.0f)); //1300 |
Lightvalve | 54:647072f5307a | 2826 | } |
Lightvalve | 58:2eade98630e2 | 2827 | |
Lightvalve | 58:2eade98630e2 | 2828 | |
Lightvalve | 171:bfc1fd2629d8 | 2829 | if (flag_data_request[2] == HIGH) { |
Lightvalve | 169:645207e160ca | 2830 | double t_value = 0.0f; |
Lightvalve | 171:bfc1fd2629d8 | 2831 | if(value>=(float) VALVE_CENTER) { |
Lightvalve | 131:d08121ac87ba | 2832 | t_value = 10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER); |
Lightvalve | 89:a7b45368ea0f | 2833 | } else { |
Lightvalve | 131:d08121ac87ba | 2834 | t_value = -10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER); |
Lightvalve | 89:a7b45368ea0f | 2835 | } |
Lightvalve | 169:645207e160ca | 2836 | double t_value_ref = 0.0f; |
Lightvalve | 169:645207e160ca | 2837 | if(valve_pos.ref>=(float) VALVE_CENTER) { |
Lightvalve | 169:645207e160ca | 2838 | t_value_ref = 10000.0f*((double)valve_pos.ref - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER); |
Lightvalve | 169:645207e160ca | 2839 | } else { |
Lightvalve | 169:645207e160ca | 2840 | t_value_ref = -10000.0f*((double)valve_pos.ref - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER); |
Lightvalve | 169:645207e160ca | 2841 | } |
Lightvalve | 169:645207e160ca | 2842 | |
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 | 233:0a37cdd59651 | 2848 | for (int 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 | 209:ebc69d6ee6f1 | 2854 | CAN_TX_PWM((int16_t) (torq.ref)); //1500 |
Lightvalve | 209:ebc69d6ee6f1 | 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 | 212:ec41f1449ef9 | 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 | 218:066030f7951f | 2861 | CAN_TX_VALVE_POSITION((int16_t) (a_hat*0.0001f), (int16_t) 0, (int16_t) 0, (int16_t) 0); //1600 |
Lightvalve | 54:647072f5307a | 2862 | } |
Lightvalve | 237:aa3863117889 | 2863 | |
Lightvalve | 233:0a37cdd59651 | 2864 | |
Lightvalve | 20:806196fda269 | 2865 | |
Lightvalve | 54:647072f5307a | 2866 | // Others : Reference position, Reference FT, PWM, Current (ID:1300) |
Lightvalve | 237:aa3863117889 | 2867 | if (flag_data_request[1] == HIGH) { |
Lightvalve | 237:aa3863117889 | 2868 | CAN_TX_SOMETHING((int) (FORCE_VREF), (int16_t) (1), (int16_t) (2), (int16_t) (3)); |
Lightvalve | 237:aa3863117889 | 2869 | } |
Lightvalve | 237:aa3863117889 | 2870 | if (flag_delay_test == 1){ |
Lightvalve | 237:aa3863117889 | 2871 | CAN_TX_PRES((int16_t) (0),(int16_t) torq_ref); |
Lightvalve | 237:aa3863117889 | 2872 | } |
Lightvalve | 52:8ea76864368a | 2873 | |
Lightvalve | 54:647072f5307a | 2874 | TMR2_COUNT_CAN_TX = 0; |
Lightvalve | 54:647072f5307a | 2875 | } |
Lightvalve | 54:647072f5307a | 2876 | TMR2_COUNT_CAN_TX++; |
Lightvalve | 237:aa3863117889 | 2877 | |
Lightvalve | 237:aa3863117889 | 2878 | */ |
Lightvalve | 237:aa3863117889 | 2879 | |
Lightvalve | 20:806196fda269 | 2880 | } |
Lightvalve | 52:8ea76864368a | 2881 | TIM3->SR = 0x0; // reset the status register |
Lightvalve | 52:8ea76864368a | 2882 | |
Lightvalve | 58:2eade98630e2 | 2883 | } |