![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
LIGHT2
main.cpp@244:30896263bd8b, 2022-06-13 (annotated)
- Committer:
- Lightvalve
- Date:
- Mon Jun 13 08:48:55 2022 +0000
- Revision:
- 244:30896263bd8b
- Parent:
- 243:b235d67d25ba
- Child:
- 245:e9c5ec04e378
220613
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Lightvalve | 244:30896263bd8b | 1 | //Hydraulic Control Board Rainbow |
Lightvalve | 224:985dba42f261 | 2 | //distributed by Sungwoo Kim |
Lightvalve | 244:30896263bd8b | 3 | // 2022/05/31 |
Lightvalve | 224:985dba42f261 | 4 | |
GiJeongKim | 0:51c43836c1d7 | 5 | #include "mbed.h" |
GiJeongKim | 0:51c43836c1d7 | 6 | #include "FastPWM.h" |
GiJeongKim | 0:51c43836c1d7 | 7 | #include "INIT_HW.h" |
jobuuu | 2:a1c0a37df760 | 8 | #include "function_CAN.h" |
GiJeongKim | 0:51c43836c1d7 | 9 | #include "SPI_EEP_ENC.h" |
GiJeongKim | 0:51c43836c1d7 | 10 | #include "setting.h" |
Lightvalve | 11:82d8768d7351 | 11 | #include "function_utilities.h" |
Lightvalve | 16:903b5a4433b4 | 12 | #include "stm32f4xx_flash.h" |
Lightvalve | 169:645207e160ca | 13 | #include "FlashWriter.h" |
Lightvalve | 170:42c938a40313 | 14 | #include <string> |
Lightvalve | 170:42c938a40313 | 15 | #include <iostream> |
Lightvalve | 179:d5377766d7ea | 16 | #include <cmath> |
Lightvalve | 61:bc8c8270f0ab | 17 | |
GiJeongKim | 0:51c43836c1d7 | 18 | |
Lightvalve | 244:30896263bd8b | 19 | // DAC /////////////////////////////////////////// |
jobuuu | 230:2c3e5ecbe7e1 | 20 | AnalogOut dac_1(PA_4); // 0.0f ~ 1.0f |
jobuuu | 230:2c3e5ecbe7e1 | 21 | AnalogOut dac_2(PA_5); // 0.0f ~ 1.0f |
Lightvalve | 244:30896263bd8b | 22 | |
Lightvalve | 244:30896263bd8b | 23 | // ADC /////////////////////////////////////////// |
Lightvalve | 244:30896263bd8b | 24 | //AnalogIn adc1(PC_4); //pressure_1 |
Lightvalve | 244:30896263bd8b | 25 | //AnalogIn adc2(PC_5); //pressure_2 |
Lightvalve | 244:30896263bd8b | 26 | //AnalogIn adc3(PC_1); //current |
Lightvalve | 244:30896263bd8b | 27 | //AnalogIn adc4(PB_0); //LVDT |
GiJeongKim | 0:51c43836c1d7 | 28 | |
jobuuu | 7:e9086c72bb22 | 29 | // PWM /////////////////////////////////////////// |
Lightvalve | 30:8d561f16383b | 30 | float dtc_v=0.0f; |
Lightvalve | 30:8d561f16383b | 31 | float dtc_w=0.0f; |
GiJeongKim | 0:51c43836c1d7 | 32 | |
Lightvalve | 244:30896263bd8b | 33 | // LVDT /////////////////////////////////////////// |
Lightvalve | 244:30896263bd8b | 34 | //DigitalOut LVDT_H(PB_6); |
Lightvalve | 244:30896263bd8b | 35 | //DigitalOut LVDT_L(PB_7); |
GiJeongKim | 0:51c43836c1d7 | 36 | |
jobuuu | 7:e9086c72bb22 | 37 | // SPI /////////////////////////////////////////// |
Lightvalve | 170:42c938a40313 | 38 | SPI eeprom(PB_15, PB_14, PB_13); // EEPROM //(SPI_MOSI, SPI_MISO, SPI_SCK); |
Lightvalve | 170:42c938a40313 | 39 | DigitalOut eeprom_cs(PB_12); |
GiJeongKim | 0:51c43836c1d7 | 40 | SPI enc(PC_12,PC_11,PC_10); |
GiJeongKim | 0:51c43836c1d7 | 41 | DigitalOut enc_cs(PD_2); |
Lightvalve | 244:30896263bd8b | 42 | |
Lightvalve | 244:30896263bd8b | 43 | // LED /////////////////////////////////////////// |
Lightvalve | 11:82d8768d7351 | 44 | DigitalOut LED(PA_15); |
GiJeongKim | 0:51c43836c1d7 | 45 | |
jobuuu | 7:e9086c72bb22 | 46 | // CAN /////////////////////////////////////////// |
GiJeongKim | 0:51c43836c1d7 | 47 | CAN can(PB_8, PB_9, 1000000); |
GiJeongKim | 0:51c43836c1d7 | 48 | CANMessage msg; |
Lightvalve | 243:b235d67d25ba | 49 | //void onMsgReceived() |
Lightvalve | 243:b235d67d25ba | 50 | //{ |
Lightvalve | 243:b235d67d25ba | 51 | // CAN_RX_HANDLER(); |
Lightvalve | 243:b235d67d25ba | 52 | //} |
jobuuu | 2:a1c0a37df760 | 53 | |
jobuuu | 7:e9086c72bb22 | 54 | // Variables /////////////////////////////////////////// |
jobuuu | 7:e9086c72bb22 | 55 | State pos; |
jobuuu | 7:e9086c72bb22 | 56 | State vel; |
jobuuu | 7:e9086c72bb22 | 57 | State Vout; |
jobuuu | 230:2c3e5ecbe7e1 | 58 | State force; |
jobuuu | 230:2c3e5ecbe7e1 | 59 | State torq; // unit : N |
Lightvalve | 207:c70c5a9f17dd | 60 | State torq_dot; |
jobuuu | 230:2c3e5ecbe7e1 | 61 | State pres_A; // unit : bar |
jobuuu | 7:e9086c72bb22 | 62 | State pres_B; |
jobuuu | 230:2c3e5ecbe7e1 | 63 | State cur; // unit : mA |
Lightvalve | 14:8e7590227d22 | 64 | State valve_pos; |
Lightvalve | 238:4e660aa77eb7 | 65 | State valve_pos_raw; |
Lightvalve | 14:8e7590227d22 | 66 | |
Lightvalve | 19:23b7c1ad8683 | 67 | extern int CID_RX_CMD; |
Lightvalve | 19:23b7c1ad8683 | 68 | extern int CID_RX_REF_POSITION; |
hyhoon | 227:699c3e572283 | 69 | extern int CID_RX_REF_OPENLOOP; |
Lightvalve | 45:35fa6884d0c6 | 70 | extern int CID_RX_REF_PWM; |
Lightvalve | 19:23b7c1ad8683 | 71 | |
Lightvalve | 19:23b7c1ad8683 | 72 | extern int CID_TX_INFO; |
hyhoon | 227:699c3e572283 | 73 | extern int CID_TX_POS_VEL_TORQ; |
hyhoon | 227:699c3e572283 | 74 | extern int CID_TX_PWM; |
hyhoon | 227:699c3e572283 | 75 | extern int CID_TX_CURRENT; |
Lightvalve | 19:23b7c1ad8683 | 76 | extern int CID_TX_VOUT; |
Lightvalve | 19:23b7c1ad8683 | 77 | extern int CID_TX_VALVE_POSITION; |
jeongyeseong | 225:9c0becc196ba | 78 | extern int CID_TX_SOMETHING; |
Lightvalve | 19:23b7c1ad8683 | 79 | |
Lightvalve | 235:c831718303c9 | 80 | inline float tanh_inv(float y) |
Lightvalve | 235:c831718303c9 | 81 | { |
Lightvalve | 235:c831718303c9 | 82 | if(y >= 1.0f - 0.000001f) y = 1.0f - 0.000001f; |
Lightvalve | 235:c831718303c9 | 83 | if(y <= -1.0f + 0.000001f) y = -1.0f + 0.000001f; |
jobuuu | 230:2c3e5ecbe7e1 | 84 | return log(sqrt((1.0f+y)/(1.0f-y))); |
jobuuu | 230:2c3e5ecbe7e1 | 85 | } |
jobuuu | 230:2c3e5ecbe7e1 | 86 | |
hyhoon | 227:699c3e572283 | 87 | |
Lightvalve | 12:6f2531038ea4 | 88 | /******************************************************************************* |
Lightvalve | 12:6f2531038ea4 | 89 | * REFERENCE MODE |
Lightvalve | 12:6f2531038ea4 | 90 | ******************************************************************************/ |
Lightvalve | 13:747daba9cf59 | 91 | enum _REFERENCE_MODE { |
Lightvalve | 235:c831718303c9 | 92 | MODE_REF_NO_ACT = 0, |
Lightvalve | 235:c831718303c9 | 93 | MODE_REF_DIRECT, |
Lightvalve | 235:c831718303c9 | 94 | MODE_REF_FINDHOME |
Lightvalve | 12:6f2531038ea4 | 95 | }; |
Lightvalve | 12:6f2531038ea4 | 96 | |
Lightvalve | 12:6f2531038ea4 | 97 | /******************************************************************************* |
Lightvalve | 12:6f2531038ea4 | 98 | * CONTROL MODE |
Lightvalve | 12:6f2531038ea4 | 99 | ******************************************************************************/ |
Lightvalve | 13:747daba9cf59 | 100 | enum _CONTROL_MODE { |
Lightvalve | 12:6f2531038ea4 | 101 | //control mode |
Lightvalve | 12:6f2531038ea4 | 102 | MODE_NO_ACT = 0, //0 |
Lightvalve | 47:fdcb8bd86fd6 | 103 | MODE_VALVE_POSITION_CONTROL, //1 |
Lightvalve | 47:fdcb8bd86fd6 | 104 | MODE_JOINT_CONTROL, //2 |
Lightvalve | 50:3c630b5eba9f | 105 | |
Lightvalve | 47:fdcb8bd86fd6 | 106 | MODE_VALVE_OPEN_LOOP, //3 |
Lightvalve | 138:a843f32ced33 | 107 | MODE_JOINT_ADAPTIVE_BACKSTEPPING, //4 |
Lightvalve | 171:bfc1fd2629d8 | 108 | MODE_RL, //5 |
Lightvalve | 13:747daba9cf59 | 109 | |
Lightvalve | 12:6f2531038ea4 | 110 | MODE_JOINT_POSITION_PRES_CONTROL_PWM, //6 |
Lightvalve | 12:6f2531038ea4 | 111 | MODE_JOINT_POSITION_PRES_CONTROL_VALVE_POSITION, //7 |
Lightvalve | 12:6f2531038ea4 | 112 | MODE_VALVE_POSITION_PRES_CONTROL_LEARNING, //8 |
Lightvalve | 13:747daba9cf59 | 113 | |
Lightvalve | 12:6f2531038ea4 | 114 | MODE_TEST_CURRENT_CONTROL, //9 |
Lightvalve | 12:6f2531038ea4 | 115 | MODE_TEST_PWM_CONTROL, //10 |
Lightvalve | 21:e5f1a43ea6f9 | 116 | |
Lightvalve | 14:8e7590227d22 | 117 | MODE_CURRENT_CONTROL, //11 |
Lightvalve | 14:8e7590227d22 | 118 | MODE_JOINT_POSITION_TORQUE_CONTROL_CURRENT, //12 |
Lightvalve | 14:8e7590227d22 | 119 | MODE_JOINT_POSITION_PRES_CONTROL_CURRENT, //13 |
Lightvalve | 224:985dba42f261 | 120 | MODE_VALVE_POSITION_TORQUE_CONTROL_LEARNING, //14 |
Lightvalve | 13:747daba9cf59 | 121 | |
Lightvalve | 12:6f2531038ea4 | 122 | //utility |
Lightvalve | 12:6f2531038ea4 | 123 | MODE_TORQUE_SENSOR_NULLING = 20, //20 |
Lightvalve | 12:6f2531038ea4 | 124 | MODE_VALVE_NULLING_AND_DEADZONE_SETTING, //21 |
Lightvalve | 12:6f2531038ea4 | 125 | MODE_FIND_HOME, //22 |
Lightvalve | 57:f4819de54e7a | 126 | MODE_VALVE_GAIN_SETTING, //23 |
Lightvalve | 12:6f2531038ea4 | 127 | MODE_PRESSURE_SENSOR_NULLING, //24 |
Lightvalve | 12:6f2531038ea4 | 128 | MODE_PRESSURE_SENSOR_CALIB, //25 |
Lightvalve | 12:6f2531038ea4 | 129 | MODE_ROTARY_FRICTION_TUNING, //26 |
Lightvalve | 13:747daba9cf59 | 130 | |
Lightvalve | 224:985dba42f261 | 131 | MODE_DDV_POS_VS_PWM_ID = 30, //30 |
Lightvalve | 12:6f2531038ea4 | 132 | MODE_DDV_DEADZONE_AND_CENTER, //31 |
Lightvalve | 12:6f2531038ea4 | 133 | MODE_DDV_POS_VS_FLOWRATE, //32 |
Lightvalve | 57:f4819de54e7a | 134 | MODE_SYSTEM_ID, //33 |
Lightvalve | 169:645207e160ca | 135 | MODE_FREQ_TEST, //34 |
Lightvalve | 169:645207e160ca | 136 | MODE_SEND_BUFFER, //35 |
Lightvalve | 169:645207e160ca | 137 | MODE_SEND_OVER, //36 |
Lightvalve | 169:645207e160ca | 138 | MODE_STEP_TEST, //37 |
Lightvalve | 12:6f2531038ea4 | 139 | }; |
Lightvalve | 12:6f2531038ea4 | 140 | |
Lightvalve | 65:a2d7c63419c2 | 141 | void SystemClock_Config(void) |
Lightvalve | 65:a2d7c63419c2 | 142 | { |
Lightvalve | 169:645207e160ca | 143 | RCC_OscInitTypeDef RCC_OscInitStruct = {0}; |
Lightvalve | 169:645207e160ca | 144 | RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; |
Lightvalve | 65:a2d7c63419c2 | 145 | |
Lightvalve | 169:645207e160ca | 146 | __HAL_RCC_PWR_CLK_ENABLE(); |
Lightvalve | 169:645207e160ca | 147 | __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); |
Lightvalve | 244:30896263bd8b | 148 | |
Lightvalve | 241:fb5b57e3f157 | 149 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; |
Lightvalve | 241:fb5b57e3f157 | 150 | RCC_OscInitStruct.HSEState = RCC_HSE_ON; |
Lightvalve | 241:fb5b57e3f157 | 151 | // RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; |
Lightvalve | 169:645207e160ca | 152 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
Lightvalve | 241:fb5b57e3f157 | 153 | RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; |
Lightvalve | 241:fb5b57e3f157 | 154 | RCC_OscInitStruct.PLL.PLLM = 4;//4 |
Lightvalve | 241:fb5b57e3f157 | 155 | RCC_OscInitStruct.PLL.PLLN = 96; //96 |
Lightvalve | 169:645207e160ca | 156 | RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; |
Lightvalve | 169:645207e160ca | 157 | RCC_OscInitStruct.PLL.PLLQ = 2; |
Lightvalve | 169:645207e160ca | 158 | RCC_OscInitStruct.PLL.PLLR = 2; |
Lightvalve | 169:645207e160ca | 159 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { |
Lightvalve | 169:645207e160ca | 160 | //Error_Handler(); |
Lightvalve | 169:645207e160ca | 161 | } |
Lightvalve | 169:645207e160ca | 162 | /** Activate the Over-Drive mode |
Lightvalve | 169:645207e160ca | 163 | */ |
Lightvalve | 169:645207e160ca | 164 | if (HAL_PWREx_EnableOverDrive() != HAL_OK) { |
Lightvalve | 169:645207e160ca | 165 | //Error_Handler(); |
Lightvalve | 169:645207e160ca | 166 | } |
Lightvalve | 169:645207e160ca | 167 | /** Initializes the CPU, AHB and APB busses clocks |
Lightvalve | 169:645207e160ca | 168 | */ |
Lightvalve | 169:645207e160ca | 169 | RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |
Lightvalve | 169:645207e160ca | 170 | |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; |
Lightvalve | 169:645207e160ca | 171 | RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; |
Lightvalve | 169:645207e160ca | 172 | RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; |
Lightvalve | 169:645207e160ca | 173 | RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4; |
Lightvalve | 169:645207e160ca | 174 | RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; |
Lightvalve | 65:a2d7c63419c2 | 175 | } |
Lightvalve | 65:a2d7c63419c2 | 176 | |
Lightvalve | 170:42c938a40313 | 177 | |
Lightvalve | 244:30896263bd8b | 178 | |
GiJeongKim | 0:51c43836c1d7 | 179 | int main() |
GiJeongKim | 0:51c43836c1d7 | 180 | { |
Lightvalve | 244:30896263bd8b | 181 | |
Lightvalve | 224:985dba42f261 | 182 | HAL_Init(); |
Lightvalve | 224:985dba42f261 | 183 | SystemClock_Config(); |
hyhoon | 226:82a3ca333004 | 184 | |
Lightvalve | 69:3995ffeaa786 | 185 | LED = 0; |
Lightvalve | 21:e5f1a43ea6f9 | 186 | |
Lightvalve | 244:30896263bd8b | 187 | // SPI INIT |
hyhoon | 227:699c3e572283 | 188 | eeprom_cs = 1; |
Lightvalve | 170:42c938a40313 | 189 | eeprom.format(8,3); |
Lightvalve | 170:42c938a40313 | 190 | eeprom.frequency(5000000); //5M |
hyhoon | 227:699c3e572283 | 191 | eeprom_cs = 0; |
hyhoon | 227:699c3e572283 | 192 | make_delay(); |
hyhoon | 227:699c3e572283 | 193 | |
Lightvalve | 244:30896263bd8b | 194 | enc_cs = 1; |
GiJeongKim | 0:51c43836c1d7 | 195 | enc.format(8,0); |
hyhoon | 227:699c3e572283 | 196 | enc.frequency(5000000); //10M |
Lightvalve | 244:30896263bd8b | 197 | enc_cs = 0; |
hyhoon | 227:699c3e572283 | 198 | |
Lightvalve | 11:82d8768d7351 | 199 | make_delay(); |
Lightvalve | 21:e5f1a43ea6f9 | 200 | |
hyhoon | 226:82a3ca333004 | 201 | // spi _ enc |
hyhoon | 226:82a3ca333004 | 202 | spi_enc_set_init(); |
hyhoon | 226:82a3ca333004 | 203 | make_delay(); |
hyhoon | 226:82a3ca333004 | 204 | |
Lightvalve | 244:30896263bd8b | 205 | // bno rom |
Lightvalve | 241:fb5b57e3f157 | 206 | spi_eeprom_write(RID_BNO, (int16_t) 0); |
Lightvalve | 238:4e660aa77eb7 | 207 | make_delay(); |
hyhoon | 226:82a3ca333004 | 208 | //////// |
hyhoon | 226:82a3ca333004 | 209 | |
Lightvalve | 224:985dba42f261 | 210 | // rom |
Lightvalve | 19:23b7c1ad8683 | 211 | ROM_CALL_DATA(); |
Lightvalve | 16:903b5a4433b4 | 212 | make_delay(); |
Lightvalve | 13:747daba9cf59 | 213 | |
GiJeongKim | 0:51c43836c1d7 | 214 | // ADC init |
jobuuu | 5:a4319f79457b | 215 | Init_ADC(); |
Lightvalve | 11:82d8768d7351 | 216 | make_delay(); |
jobuuu | 2:a1c0a37df760 | 217 | |
GiJeongKim | 0:51c43836c1d7 | 218 | // Pwm init |
GiJeongKim | 0:51c43836c1d7 | 219 | Init_PWM(); |
GiJeongKim | 0:51c43836c1d7 | 220 | TIM4->CR1 ^= TIM_CR1_UDIS; |
Lightvalve | 11:82d8768d7351 | 221 | make_delay(); |
Lightvalve | 13:747daba9cf59 | 222 | |
GiJeongKim | 0:51c43836c1d7 | 223 | // CAN |
jobuuu | 2:a1c0a37df760 | 224 | can.attach(&CAN_RX_HANDLER); |
Lightvalve | 11:82d8768d7351 | 225 | CAN_ID_INIT(); |
Lightvalve | 11:82d8768d7351 | 226 | make_delay(); |
Lightvalve | 34:bb2ca2fc2a8e | 227 | |
hyhoon | 226:82a3ca333004 | 228 | //can.reset(); |
Lightvalve | 244:30896263bd8b | 229 | can.filter(msg.id, 0xFFFFF000, CANStandard); |
Lightvalve | 244:30896263bd8b | 230 | // can.filter(0b100000000, 0b100000010, CANStandard); //CAN ID 100~400번대 통과하게 |
hyhoon | 226:82a3ca333004 | 231 | |
hyhoon | 226:82a3ca333004 | 232 | // TMR3 init |
hyhoon | 226:82a3ca333004 | 233 | Init_TMR3(); |
hyhoon | 226:82a3ca333004 | 234 | TIM3->CR1 ^= TIM_CR1_UDIS; |
hyhoon | 226:82a3ca333004 | 235 | make_delay(); |
hyhoon | 226:82a3ca333004 | 236 | |
Lightvalve | 244:30896263bd8b | 237 | // TMR2 init |
Lightvalve | 241:fb5b57e3f157 | 238 | Init_TMR2(); |
Lightvalve | 241:fb5b57e3f157 | 239 | TIM2->CR1 ^= TIM_CR1_UDIS; |
Lightvalve | 241:fb5b57e3f157 | 240 | make_delay(); |
Lightvalve | 241:fb5b57e3f157 | 241 | |
Lightvalve | 244:30896263bd8b | 242 | // TMR1 init |
Lightvalve | 244:30896263bd8b | 243 | Init_TMR1(); |
Lightvalve | 244:30896263bd8b | 244 | TIM1->CR1 ^= TIM_CR1_UDIS; |
Lightvalve | 244:30896263bd8b | 245 | make_delay(); |
Lightvalve | 244:30896263bd8b | 246 | |
Lightvalve | 23:59218d4a256d | 247 | //Timer priority |
Lightvalve | 23:59218d4a256d | 248 | NVIC_SetPriority(TIM3_IRQn, 2); |
Lightvalve | 54:647072f5307a | 249 | NVIC_SetPriority(TIM4_IRQn, 3); |
Lightvalve | 241:fb5b57e3f157 | 250 | NVIC_SetPriority(TIM2_IRQn, 4); |
Lightvalve | 34:bb2ca2fc2a8e | 251 | |
Lightvalve | 244:30896263bd8b | 252 | // HAL_NVIC_SetPriority(TIM2_IRQn, 4, 0); |
Lightvalve | 244:30896263bd8b | 253 | // HAL_NVIC_EnableIRQ(TIM2_IRQn); |
Lightvalve | 244:30896263bd8b | 254 | // /* TIM3_IRQn interrupt configuration */ |
Lightvalve | 244:30896263bd8b | 255 | // HAL_NVIC_SetPriority(TIM3_IRQn, 2, 0); |
Lightvalve | 244:30896263bd8b | 256 | // HAL_NVIC_EnableIRQ(TIM3_IRQn); |
Lightvalve | 244:30896263bd8b | 257 | // /* TIM4_IRQn interrupt configuration */ |
Lightvalve | 244:30896263bd8b | 258 | // HAL_NVIC_SetPriority(TIM4_IRQn, 3, 0); |
Lightvalve | 244:30896263bd8b | 259 | // HAL_NVIC_EnableIRQ(TIM4_IRQn); |
Lightvalve | 244:30896263bd8b | 260 | // /* CAN1_RX0_IRQn interrupt configuration */ |
Lightvalve | 244:30896263bd8b | 261 | // HAL_NVIC_SetPriority(CAN1_RX0_IRQn, 14, 0); |
Lightvalve | 244:30896263bd8b | 262 | // HAL_NVIC_EnableIRQ(CAN1_RX0_IRQn); |
Lightvalve | 244:30896263bd8b | 263 | |
hyhoon | 226:82a3ca333004 | 264 | |
Lightvalve | 11:82d8768d7351 | 265 | //DAC init |
Lightvalve | 58:2eade98630e2 | 266 | if (SENSING_MODE == 0) { |
jobuuu | 230:2c3e5ecbe7e1 | 267 | dac_1 = FORCE_VREF / 3.3f; |
Lightvalve | 58:2eade98630e2 | 268 | dac_2 = 0.0f; |
Lightvalve | 58:2eade98630e2 | 269 | } else if (SENSING_MODE == 1) { |
Lightvalve | 238:4e660aa77eb7 | 270 | // if (DIR_VALVE_ENC > 0) { |
Lightvalve | 241:fb5b57e3f157 | 271 | dac_1 = PRES_A_VREF / 3.3f; |
Lightvalve | 241:fb5b57e3f157 | 272 | dac_2 = PRES_B_VREF / 3.3f; |
Lightvalve | 238:4e660aa77eb7 | 273 | // } else { |
Lightvalve | 238:4e660aa77eb7 | 274 | // dac_1 = PRES_B_VREF / 3.3f; |
Lightvalve | 238:4e660aa77eb7 | 275 | // dac_2 = PRES_A_VREF / 3.3f; |
Lightvalve | 238:4e660aa77eb7 | 276 | // } |
Lightvalve | 58:2eade98630e2 | 277 | } |
Lightvalve | 11:82d8768d7351 | 278 | make_delay(); |
Lightvalve | 13:747daba9cf59 | 279 | |
Lightvalve | 19:23b7c1ad8683 | 280 | for (int i=0; i<50; i++) { |
Lightvalve | 11:82d8768d7351 | 281 | if(i%2==0) |
Lightvalve | 38:118df027d851 | 282 | ID_index_array[i] = - i * 0.5f; |
Lightvalve | 11:82d8768d7351 | 283 | else |
Lightvalve | 38:118df027d851 | 284 | ID_index_array[i] = (i+1) * 0.5f; |
Lightvalve | 11:82d8768d7351 | 285 | } |
Lightvalve | 169:645207e160ca | 286 | |
jobuuu | 6:df07d3491e3a | 287 | /************************************ |
jobuuu | 1:e04e563be5ce | 288 | *** Program is operating! |
jobuuu | 6:df07d3491e3a | 289 | *************************************/ |
GiJeongKim | 0:51c43836c1d7 | 290 | while(1) { |
Lightvalve | 244:30896263bd8b | 291 | |
Lightvalve | 244:30896263bd8b | 292 | // if (LED > 0) LED = 0; |
Lightvalve | 244:30896263bd8b | 293 | // else LED = 1; |
Lightvalve | 244:30896263bd8b | 294 | TIM1->CCR1 = (TMR1_COUNT)*(0.7f); |
Lightvalve | 169:645207e160ca | 295 | |
Lightvalve | 169:645207e160ca | 296 | |
GiJeongKim | 0:51c43836c1d7 | 297 | } |
jobuuu | 1:e04e563be5ce | 298 | } |
jobuuu | 1:e04e563be5ce | 299 | |
Lightvalve | 224:985dba42f261 | 300 | |
Lightvalve | 224:985dba42f261 | 301 | // Velocity feedforward for SW valve |
Lightvalve | 33:91b17819ec30 | 302 | float DDV_JOINT_POS_FF(float REF_JOINT_VEL) |
Lightvalve | 14:8e7590227d22 | 303 | { |
Lightvalve | 13:747daba9cf59 | 304 | int i = 0; |
Lightvalve | 48:889798ff9329 | 305 | float Ref_Valve_Pos_FF = 0.0f; |
Lightvalve | 14:8e7590227d22 | 306 | for(i=0; i<VALVE_POS_NUM; i++) { |
Lightvalve | 14:8e7590227d22 | 307 | 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 | 308 | if(i==0) { |
Lightvalve | 50:3c630b5eba9f | 309 | if(JOINT_VEL[i+1] == JOINT_VEL[i]) { |
Lightvalve | 57:f4819de54e7a | 310 | Ref_Valve_Pos_FF = (float) VALVE_CENTER; |
Lightvalve | 50:3c630b5eba9f | 311 | } else { |
Lightvalve | 57:f4819de54e7a | 312 | 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 | 313 | } |
Lightvalve | 14:8e7590227d22 | 314 | } else { |
Lightvalve | 50:3c630b5eba9f | 315 | if(JOINT_VEL[i+1] == JOINT_VEL[i-1]) { |
Lightvalve | 57:f4819de54e7a | 316 | Ref_Valve_Pos_FF = (float) VALVE_CENTER; |
Lightvalve | 50:3c630b5eba9f | 317 | } else { |
Lightvalve | 57:f4819de54e7a | 318 | 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 | 319 | } |
Lightvalve | 13:747daba9cf59 | 320 | } |
Lightvalve | 13:747daba9cf59 | 321 | break; |
Lightvalve | 13:747daba9cf59 | 322 | } |
Lightvalve | 13:747daba9cf59 | 323 | } |
Lightvalve | 14:8e7590227d22 | 324 | if(REF_JOINT_VEL > max(JOINT_VEL[VALVE_POS_NUM-1], JOINT_VEL[VALVE_POS_NUM-2])) { |
Lightvalve | 33:91b17819ec30 | 325 | Ref_Valve_Pos_FF = (float) VALVE_MAX_POS; |
Lightvalve | 14:8e7590227d22 | 326 | } else if(REF_JOINT_VEL < min(JOINT_VEL[VALVE_POS_NUM-1], JOINT_VEL[VALVE_POS_NUM-2])) { |
Lightvalve | 33:91b17819ec30 | 327 | Ref_Valve_Pos_FF = (float) VALVE_MIN_POS; |
Lightvalve | 13:747daba9cf59 | 328 | } |
Lightvalve | 36:a46e63505ed8 | 329 | |
Lightvalve | 224:985dba42f261 | 330 | Ref_Valve_Pos_FF = (float) VELOCITY_COMP_GAIN * 0.01f * (float) (Ref_Valve_Pos_FF - (float) VALVE_CENTER); //VELOCITY_COMP_GAIN : 0~100 |
Lightvalve | 13:747daba9cf59 | 331 | return Ref_Valve_Pos_FF; |
Lightvalve | 13:747daba9cf59 | 332 | } |
jobuuu | 6:df07d3491e3a | 333 | |
Lightvalve | 224:985dba42f261 | 334 | // Valve feedforward for SW valve |
Lightvalve | 30:8d561f16383b | 335 | void VALVE_POS_CONTROL(float REF_VALVE_POS) |
Lightvalve | 14:8e7590227d22 | 336 | { |
Lightvalve | 13:747daba9cf59 | 337 | int i = 0; |
Lightvalve | 13:747daba9cf59 | 338 | |
Lightvalve | 38:118df027d851 | 339 | if(REF_VALVE_POS > VALVE_MAX_POS) { |
Lightvalve | 38:118df027d851 | 340 | REF_VALVE_POS = VALVE_MAX_POS; |
Lightvalve | 38:118df027d851 | 341 | } else if(REF_VALVE_POS < VALVE_MIN_POS) { |
Lightvalve | 38:118df027d851 | 342 | REF_VALVE_POS = VALVE_MIN_POS; |
Lightvalve | 38:118df027d851 | 343 | } |
Lightvalve | 244:30896263bd8b | 344 | valve_pos_err = (float) (REF_VALVE_POS - valve_pos.sen); |
Lightvalve | 13:747daba9cf59 | 345 | valve_pos_err_diff = valve_pos_err - valve_pos_err_old; |
Lightvalve | 13:747daba9cf59 | 346 | valve_pos_err_old = valve_pos_err; |
Lightvalve | 13:747daba9cf59 | 347 | valve_pos_err_sum += valve_pos_err; |
Lightvalve | 89:a7b45368ea0f | 348 | if (valve_pos_err_sum > 1000.0f) valve_pos_err_sum = 1000.0f; |
Lightvalve | 89:a7b45368ea0f | 349 | if (valve_pos_err_sum<-1000.0f) valve_pos_err_sum = -1000.0f; |
Lightvalve | 13:747daba9cf59 | 350 | |
Lightvalve | 13:747daba9cf59 | 351 | 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 | 352 | |
Lightvalve | 18:b8adf1582ea3 | 353 | for(i=0; i<24; i++) { |
Lightvalve | 89:a7b45368ea0f | 354 | 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 | 355 | if(i==0) { |
Lightvalve | 48:889798ff9329 | 356 | 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 | 357 | } else { |
Lightvalve | 48:889798ff9329 | 358 | 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 | 359 | } |
Lightvalve | 13:747daba9cf59 | 360 | break; |
Lightvalve | 13:747daba9cf59 | 361 | } |
Lightvalve | 13:747daba9cf59 | 362 | } |
Lightvalve | 59:f308b1656d9c | 363 | Vout.ref = VALVE_PWM_RAW_FF + VALVE_PWM_RAW_FB; |
Lightvalve | 13:747daba9cf59 | 364 | } |
Lightvalve | 13:747daba9cf59 | 365 | |
Lightvalve | 238:4e660aa77eb7 | 366 | // Valve feedforward for SW valve |
Lightvalve | 238:4e660aa77eb7 | 367 | void VALVE_POS_CONTROL_DZ(float REF_VALVE_POS_10000) |
Lightvalve | 238:4e660aa77eb7 | 368 | { |
Lightvalve | 238:4e660aa77eb7 | 369 | int i = 0; |
Lightvalve | 238:4e660aa77eb7 | 370 | float valve_pos_ref_dz = 0.0f; |
Lightvalve | 238:4e660aa77eb7 | 371 | if(REF_VALVE_POS_10000 > 10000.0f) { |
Lightvalve | 238:4e660aa77eb7 | 372 | REF_VALVE_POS_10000 = 10000.0f; |
Lightvalve | 238:4e660aa77eb7 | 373 | } else if(REF_VALVE_POS_10000 < -10000.0f) { |
Lightvalve | 238:4e660aa77eb7 | 374 | REF_VALVE_POS_10000 = -10000.0f; |
Lightvalve | 238:4e660aa77eb7 | 375 | } |
Lightvalve | 238:4e660aa77eb7 | 376 | if (REF_VALVE_POS_10000 >= 0) { |
Lightvalve | 243:b235d67d25ba | 377 | // valve_pos_ref_dz = REF_VALVE_POS_10000/10000.0f * (VALVE_MAX_POS-VALVE_DEADZONE_PLUS) + VALVE_DEADZONE_PLUS; |
Lightvalve | 243:b235d67d25ba | 378 | valve_pos_ref_dz = REF_VALVE_POS_10000; |
Lightvalve | 238:4e660aa77eb7 | 379 | } else { |
Lightvalve | 243:b235d67d25ba | 380 | // valve_pos_ref_dz = -REF_VALVE_POS_10000/10000.0f * (VALVE_MIN_POS-VALVE_DEADZONE_MINUS) + VALVE_DEADZONE_MINUS; |
Lightvalve | 243:b235d67d25ba | 381 | valve_pos_ref_dz = REF_VALVE_POS_10000; |
Lightvalve | 238:4e660aa77eb7 | 382 | } |
Lightvalve | 243:b235d67d25ba | 383 | valve_pos_err = (float) (valve_pos_ref_dz - valve_pos.sen); |
Lightvalve | 238:4e660aa77eb7 | 384 | valve_pos_err_diff = valve_pos_err - valve_pos_err_old; |
Lightvalve | 238:4e660aa77eb7 | 385 | valve_pos_err_old = valve_pos_err; |
Lightvalve | 238:4e660aa77eb7 | 386 | valve_pos_err_sum += valve_pos_err; |
Lightvalve | 238:4e660aa77eb7 | 387 | if (valve_pos_err_sum > 1000.0f) valve_pos_err_sum = 1000.0f; |
Lightvalve | 238:4e660aa77eb7 | 388 | if (valve_pos_err_sum<-1000.0f) valve_pos_err_sum = -1000.0f; |
Lightvalve | 238:4e660aa77eb7 | 389 | |
Lightvalve | 238:4e660aa77eb7 | 390 | 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 | 238:4e660aa77eb7 | 391 | |
Lightvalve | 238:4e660aa77eb7 | 392 | for(i=0; i<24; i++) { |
Lightvalve | 238:4e660aa77eb7 | 393 | if(valve_pos_ref_dz >= min(VALVE_POS_VS_PWM[i],VALVE_POS_VS_PWM[i+1]) && (float) valve_pos_ref_dz <= max(VALVE_POS_VS_PWM[i],VALVE_POS_VS_PWM[i+1])) { |
Lightvalve | 238:4e660aa77eb7 | 394 | if(i==0) { |
Lightvalve | 238:4e660aa77eb7 | 395 | VALVE_PWM_RAW_FF = (float) 1000.0f / (float) (VALVE_POS_VS_PWM[i+1] - VALVE_POS_VS_PWM[i]) * ((float) valve_pos_ref_dz - VALVE_POS_VS_PWM[i]); |
Lightvalve | 238:4e660aa77eb7 | 396 | } else { |
Lightvalve | 238:4e660aa77eb7 | 397 | 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) valve_pos_ref_dz - VALVE_POS_VS_PWM[i-1]) + 1000.0f * (float) ID_index_array[i-1]; |
Lightvalve | 238:4e660aa77eb7 | 398 | } |
Lightvalve | 238:4e660aa77eb7 | 399 | break; |
Lightvalve | 238:4e660aa77eb7 | 400 | } |
Lightvalve | 238:4e660aa77eb7 | 401 | } |
Lightvalve | 238:4e660aa77eb7 | 402 | Vout.ref = VALVE_PWM_RAW_FF + VALVE_PWM_RAW_FB; |
Lightvalve | 238:4e660aa77eb7 | 403 | } |
Lightvalve | 238:4e660aa77eb7 | 404 | |
Lightvalve | 224:985dba42f261 | 405 | // PWM duty vs. voltage output of L6205 in STM board |
Lightvalve | 14:8e7590227d22 | 406 | #define LT_MAX_IDX 57 |
Lightvalve | 30:8d561f16383b | 407 | 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 | 408 | -19.0f, -18.0f, -17.0f, -16.0f, -15.0f, -14.0f, -13.0f, -12.0f, -11.0f, -10.0f, |
Lightvalve | 34:bb2ca2fc2a8e | 409 | -9.0f, -8.0f, -7.0f, -6.0f, -5.0f, -4.0f, -3.0f, -2.0f, -1.0f, 0.0f, |
Lightvalve | 34:bb2ca2fc2a8e | 410 | 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f, |
Lightvalve | 34:bb2ca2fc2a8e | 411 | 11.0f, 12.0f, 13.0f, 14.0f, 15.0f, 16.0f, 17.0f, 18.0f, 19.0f, 20.0f, |
Lightvalve | 34:bb2ca2fc2a8e | 412 | 25.0f, 30.0f, 35.0f, 40.0f, 50.0f, 60.0f, 80.0f, 100.0f |
Lightvalve | 34:bb2ca2fc2a8e | 413 | }; // duty |
Lightvalve | 67:c2812cf26c38 | 414 | 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 | 415 | -150.0f, -145.0f, -145.0f, -145.0f, -135.0f, -135.0f, -135.0f, -127.5f, -127.5f, -115.0f, |
Lightvalve | 67:c2812cf26c38 | 416 | -115.0f, -115.0F, -100.0f, -100.0f, -100.0f, -60.0f, -60.0f, -10.0f, -5.0f, 0.0f, |
Lightvalve | 67:c2812cf26c38 | 417 | 7.5f, 14.0f, 14.0f, 14.0f, 42.5f, 42.5f, 42.5f, 80.0f, 80.0f, 105.0f, |
Lightvalve | 67:c2812cf26c38 | 418 | 105.0f, 105.0f, 120.0f, 120.0f, 120.0f, 131.0f, 131.0f, 140.0f, 140.0f, 140.0f, |
Lightvalve | 67:c2812cf26c38 | 419 | 155.0f, 160.0f, 170.0f, 174.0f, 182.0f, 191.0f, 212.0f, 230.0f |
Lightvalve | 34:bb2ca2fc2a8e | 420 | }; // mV |
Lightvalve | 13:747daba9cf59 | 421 | |
Lightvalve | 30:8d561f16383b | 422 | float PWM_duty_byLT(float Ref_V) |
Lightvalve | 14:8e7590227d22 | 423 | { |
Lightvalve | 30:8d561f16383b | 424 | float PWM_duty = 0.0f; |
Lightvalve | 13:747daba9cf59 | 425 | if(Ref_V<LT_Voltage_Output[0]) { |
Lightvalve | 30:8d561f16383b | 426 | PWM_duty = (Ref_V-LT_Voltage_Output[0])/1.5f+LT_PWM_duty[0]; |
Lightvalve | 13:747daba9cf59 | 427 | } else if (Ref_V>=LT_Voltage_Output[LT_MAX_IDX-1]) { |
Lightvalve | 30:8d561f16383b | 428 | PWM_duty = (Ref_V-LT_Voltage_Output[LT_MAX_IDX-1])/1.5f+LT_PWM_duty[LT_MAX_IDX-1]; |
Lightvalve | 13:747daba9cf59 | 429 | } else { |
Lightvalve | 13:747daba9cf59 | 430 | int idx = 0; |
Lightvalve | 13:747daba9cf59 | 431 | for(idx=0; idx<LT_MAX_IDX-1; idx++) { |
Lightvalve | 30:8d561f16383b | 432 | float ini_x = LT_Voltage_Output[idx]; |
Lightvalve | 30:8d561f16383b | 433 | float fin_x = LT_Voltage_Output[idx+1]; |
Lightvalve | 30:8d561f16383b | 434 | float ini_y = LT_PWM_duty[idx]; |
Lightvalve | 30:8d561f16383b | 435 | float fin_y = LT_PWM_duty[idx+1]; |
Lightvalve | 13:747daba9cf59 | 436 | if(Ref_V>=ini_x && Ref_V<fin_x) { |
Lightvalve | 13:747daba9cf59 | 437 | PWM_duty = (fin_y-ini_y)/(fin_x-ini_x)*(Ref_V-ini_x) + ini_y; |
Lightvalve | 13:747daba9cf59 | 438 | break; |
Lightvalve | 13:747daba9cf59 | 439 | } |
Lightvalve | 13:747daba9cf59 | 440 | } |
Lightvalve | 13:747daba9cf59 | 441 | } |
Lightvalve | 13:747daba9cf59 | 442 | return PWM_duty; |
Lightvalve | 13:747daba9cf59 | 443 | } |
jobuuu | 6:df07d3491e3a | 444 | |
Lightvalve | 57:f4819de54e7a | 445 | |
Lightvalve | 57:f4819de54e7a | 446 | |
jobuuu | 2:a1c0a37df760 | 447 | /******************************************************************************* |
jobuuu | 2:a1c0a37df760 | 448 | TIMER INTERRUPT |
jobuuu | 2:a1c0a37df760 | 449 | *******************************************************************************/ |
jobuuu | 2:a1c0a37df760 | 450 | |
Lightvalve | 244:30896263bd8b | 451 | |
Lightvalve | 244:30896263bd8b | 452 | //------------------------------------------------ |
Lightvalve | 244:30896263bd8b | 453 | // TMR3 : LVDT 1kHz |
Lightvalve | 244:30896263bd8b | 454 | //----------------------------------------------- |
Lightvalve | 241:fb5b57e3f157 | 455 | float LVDT_new = 0.0f; |
Lightvalve | 241:fb5b57e3f157 | 456 | float LVDT_old = 0.0f; |
Lightvalve | 241:fb5b57e3f157 | 457 | float LVDT_f_cut = 1000.0f; |
Lightvalve | 241:fb5b57e3f157 | 458 | float LVDT_LPF = 0.0f; |
Lightvalve | 241:fb5b57e3f157 | 459 | float LVDT_sum = 0.0f; |
Lightvalve | 241:fb5b57e3f157 | 460 | |
Lightvalve | 241:fb5b57e3f157 | 461 | extern "C" void TIM3_IRQHandler(void) |
Lightvalve | 241:fb5b57e3f157 | 462 | { |
Lightvalve | 241:fb5b57e3f157 | 463 | if (TIM3->SR & TIM_SR_UIF ) { |
Lightvalve | 241:fb5b57e3f157 | 464 | |
Lightvalve | 244:30896263bd8b | 465 | //// if (LED > 0) LED = 0; |
Lightvalve | 244:30896263bd8b | 466 | //// else LED = 1; |
Lightvalve | 244:30896263bd8b | 467 | // |
Lightvalve | 244:30896263bd8b | 468 | // LVDT_sum = 0.0f; |
Lightvalve | 244:30896263bd8b | 469 | // |
Lightvalve | 244:30896263bd8b | 470 | // |
Lightvalve | 244:30896263bd8b | 471 | // LVDT_L = 0; |
Lightvalve | 244:30896263bd8b | 472 | // LVDT_H = 1; |
Lightvalve | 244:30896263bd8b | 473 | // |
Lightvalve | 244:30896263bd8b | 474 | //// LED = 1; |
Lightvalve | 244:30896263bd8b | 475 | // |
Lightvalve | 244:30896263bd8b | 476 | // for (int ij = 0; ij<150; ij++) { |
Lightvalve | 244:30896263bd8b | 477 | // ADC1->CR2 |= 0x40000000; |
Lightvalve | 244:30896263bd8b | 478 | // LVDT_new = ((float)ADC1->DR) - 2047.5f; |
Lightvalve | 244:30896263bd8b | 479 | // LVDT_sum = LVDT_sum + LVDT_new; |
Lightvalve | 244:30896263bd8b | 480 | // } |
Lightvalve | 244:30896263bd8b | 481 | // |
Lightvalve | 244:30896263bd8b | 482 | //// LED = 0; |
Lightvalve | 244:30896263bd8b | 483 | // |
Lightvalve | 244:30896263bd8b | 484 | // LVDT_H = 0; |
Lightvalve | 244:30896263bd8b | 485 | // LVDT_L = 1; |
Lightvalve | 241:fb5b57e3f157 | 486 | |
Lightvalve | 241:fb5b57e3f157 | 487 | |
Lightvalve | 244:30896263bd8b | 488 | |
Lightvalve | 244:30896263bd8b | 489 | |
Lightvalve | 244:30896263bd8b | 490 | // LVDT_new = LVDT_sum * 0.01f*2.0f; |
Lightvalve | 244:30896263bd8b | 491 | // |
Lightvalve | 244:30896263bd8b | 492 | // float alpha_LVDT = 1.0f/(1.0f+TMR_FREQ_1k/(2.0f*PI*300.0f)); |
Lightvalve | 244:30896263bd8b | 493 | // LVDT_LPF = (1.0f-alpha_LVDT) * LVDT_LPF + alpha_LVDT * LVDT_new; |
Lightvalve | 244:30896263bd8b | 494 | // valve_pos.sen = LVDT_LPF; |
Lightvalve | 244:30896263bd8b | 495 | // if(DIR_VALVE_ENC < 0) valve_pos.sen = 0.0f - valve_pos.sen; |
Lightvalve | 241:fb5b57e3f157 | 496 | |
Lightvalve | 244:30896263bd8b | 497 | // TIM4->CCR2 = (PWM_ARR)*(0.95f); |
Lightvalve | 244:30896263bd8b | 498 | // TIM4->CCR1 = (PWM_ARR)*(0.975f); |
Lightvalve | 244:30896263bd8b | 499 | |
Lightvalve | 244:30896263bd8b | 500 | // TIM8->CCR1 = (TMR8_COUNT)*(0.95f); |
Lightvalve | 244:30896263bd8b | 501 | // TIM8->CCR2 = (TMR8_COUNT)*(0.975f); |
Lightvalve | 244:30896263bd8b | 502 | |
Lightvalve | 241:fb5b57e3f157 | 503 | |
Lightvalve | 244:30896263bd8b | 504 | // PWM_H2 = 0; |
Lightvalve | 244:30896263bd8b | 505 | // PWM_L2 = 1; |
Lightvalve | 241:fb5b57e3f157 | 506 | |
Lightvalve | 244:30896263bd8b | 507 | // TIM3->CCR2 = (TMR3_COUNT)*(0.95f); |
Lightvalve | 244:30896263bd8b | 508 | // TIM3->CCR1 = (TMR3_COUNT)*(0.975f); |
Lightvalve | 244:30896263bd8b | 509 | |
Lightvalve | 241:fb5b57e3f157 | 510 | |
Lightvalve | 241:fb5b57e3f157 | 511 | } |
Lightvalve | 241:fb5b57e3f157 | 512 | TIM3->SR = 0x0; // reset the status register |
Lightvalve | 241:fb5b57e3f157 | 513 | } |
Lightvalve | 241:fb5b57e3f157 | 514 | |
Lightvalve | 241:fb5b57e3f157 | 515 | |
Lightvalve | 241:fb5b57e3f157 | 516 | |
jobuuu | 230:2c3e5ecbe7e1 | 517 | //------------------------------------------------ |
jobuuu | 230:2c3e5ecbe7e1 | 518 | // TMR4 : Sensor Read & Data Handling |
jobuuu | 230:2c3e5ecbe7e1 | 519 | //----------------------------------------------- |
Lightvalve | 51:b46bed7fec80 | 520 | float FREQ_TMR4 = (float)FREQ_20k; |
Lightvalve | 51:b46bed7fec80 | 521 | float DT_TMR4 = (float)DT_20k; |
Lightvalve | 57:f4819de54e7a | 522 | long CNT_TMR4 = 0; |
Lightvalve | 57:f4819de54e7a | 523 | int TMR4_FREQ_10k = (int)FREQ_10k; |
jobuuu | 1:e04e563be5ce | 524 | extern "C" void TIM4_IRQHandler(void) |
jobuuu | 1:e04e563be5ce | 525 | { |
Lightvalve | 19:23b7c1ad8683 | 526 | if (TIM4->SR & TIM_SR_UIF ) { |
Lightvalve | 13:747daba9cf59 | 527 | |
jobuuu | 230:2c3e5ecbe7e1 | 528 | // Current =================================================== |
jobuuu | 230:2c3e5ecbe7e1 | 529 | //ADC3->CR2 |= 0x40000000; // adc _ 12bit |
Lightvalve | 235:c831718303c9 | 530 | |
jobuuu | 230:2c3e5ecbe7e1 | 531 | cur.UpdateSen(((float)ADC3->DR-2047.5f)/2047.5f*10.0f, FREQ_TMR4, 500.0f); // unit : mA |
jobuuu | 230:2c3e5ecbe7e1 | 532 | |
jobuuu | 230:2c3e5ecbe7e1 | 533 | // Encoder =================================================== |
Lightvalve | 57:f4819de54e7a | 534 | if (CNT_TMR4 % (int) ((int) FREQ_TMR4/TMR4_FREQ_10k) == 0) { |
Lightvalve | 57:f4819de54e7a | 535 | ENC_UPDATE(); |
Lightvalve | 57:f4819de54e7a | 536 | } |
Lightvalve | 61:bc8c8270f0ab | 537 | |
Lightvalve | 241:fb5b57e3f157 | 538 | /* |
jobuuu | 230:2c3e5ecbe7e1 | 539 | // Force or Pressure Transducer ============================================= |
Lightvalve | 61:bc8c8270f0ab | 540 | ADC1->CR2 |= 0x40000000; |
jobuuu | 230:2c3e5ecbe7e1 | 541 | if (SENSING_MODE == 0) { // Force sensing |
jobuuu | 230:2c3e5ecbe7e1 | 542 | force.UpdateSen((((float)ADC1->DR) - 2047.5f)/TORQUE_SENSOR_PULSE_PER_TORQUE, FREQ_TMR4, 100.0f); // unit : N |
jobuuu | 230:2c3e5ecbe7e1 | 543 | } else if (SENSING_MODE == 1) { // Pressure sensing |
jobuuu | 230:2c3e5ecbe7e1 | 544 | float pres_A_new, pres_B_new; |
Lightvalve | 241:fb5b57e3f157 | 545 | // if (DIR_VALVE_ENC > 0) { |
Lightvalve | 241:fb5b57e3f157 | 546 | pres_A_new = (((float)ADC1->DR) - PRES_A_NULL_pulse)/ PRES_SENSOR_A_PULSE_PER_BAR; // unit : bar |
Lightvalve | 241:fb5b57e3f157 | 547 | pres_B_new = (((float)ADC2->DR) - PRES_B_NULL_pulse)/ PRES_SENSOR_B_PULSE_PER_BAR; |
Lightvalve | 241:fb5b57e3f157 | 548 | // } else { |
Lightvalve | 241:fb5b57e3f157 | 549 | // pres_A_new = (((float)ADC2->DR) - PRES_A_NULL_pulse)/ PRES_SENSOR_A_PULSE_PER_BAR; // unit : bar |
Lightvalve | 241:fb5b57e3f157 | 550 | // pres_B_new = (((float)ADC1->DR) - PRES_B_NULL_pulse)/ PRES_SENSOR_B_PULSE_PER_BAR; |
Lightvalve | 241:fb5b57e3f157 | 551 | // } |
jobuuu | 230:2c3e5ecbe7e1 | 552 | pres_A.UpdateSen(pres_A_new,FREQ_TMR4,200.0f); |
jobuuu | 230:2c3e5ecbe7e1 | 553 | pres_B.UpdateSen(pres_B_new,FREQ_TMR4,200.0f); |
Lightvalve | 50:3c630b5eba9f | 554 | |
Lightvalve | 238:4e660aa77eb7 | 555 | if ((OPERATING_MODE & 0b01) == 0) { // Rotary Actuator |
Lightvalve | 235:c831718303c9 | 556 | float torq_new = (PISTON_AREA_A * pres_A.sen - PISTON_AREA_B * pres_B.sen) * 0.0001f; // mm^3*bar >> Nm |
Lightvalve | 232:2976cf1b4252 | 557 | torq.UpdateSen(torq_new,FREQ_TMR4,1000.0f); // unit : Nm |
Lightvalve | 238:4e660aa77eb7 | 558 | } else if ((OPERATING_MODE & 0b01) == 1) { // Linear Actuator |
jobuuu | 230:2c3e5ecbe7e1 | 559 | float force_new = (PISTON_AREA_A * pres_A.sen - PISTON_AREA_B * pres_B.sen) * 0.1f; // mm^2*bar >> N |
Lightvalve | 232:2976cf1b4252 | 560 | force.UpdateSen(force_new,FREQ_TMR4,1000.0f); // unit : N |
Lightvalve | 58:2eade98630e2 | 561 | } |
Lightvalve | 58:2eade98630e2 | 562 | } |
Lightvalve | 241:fb5b57e3f157 | 563 | */ |
Lightvalve | 61:bc8c8270f0ab | 564 | |
Lightvalve | 244:30896263bd8b | 565 | // //FORWARD |
Lightvalve | 244:30896263bd8b | 566 | // if(CNT_TMR4 < 2) |
Lightvalve | 244:30896263bd8b | 567 | // { |
Lightvalve | 244:30896263bd8b | 568 | // PWM_L2 = 0; |
Lightvalve | 244:30896263bd8b | 569 | // PWM_H1 = 0; |
Lightvalve | 244:30896263bd8b | 570 | // |
Lightvalve | 244:30896263bd8b | 571 | // PWM_H2 = 1; |
Lightvalve | 244:30896263bd8b | 572 | // PWM_L1 = 1; |
Lightvalve | 244:30896263bd8b | 573 | // } else if(CNT_TMR4 < 50) |
Lightvalve | 244:30896263bd8b | 574 | // { |
Lightvalve | 244:30896263bd8b | 575 | // PWM_H2 = 0; |
Lightvalve | 244:30896263bd8b | 576 | // PWM_H1 = 0; |
Lightvalve | 244:30896263bd8b | 577 | // |
Lightvalve | 244:30896263bd8b | 578 | // PWM_L2 = 1; |
Lightvalve | 244:30896263bd8b | 579 | // PWM_L1 = 1; |
Lightvalve | 244:30896263bd8b | 580 | // } else |
Lightvalve | 244:30896263bd8b | 581 | // { |
Lightvalve | 244:30896263bd8b | 582 | // CNT_TMR4 = 0; |
Lightvalve | 244:30896263bd8b | 583 | // } |
Lightvalve | 244:30896263bd8b | 584 | |
Lightvalve | 244:30896263bd8b | 585 | |
Lightvalve | 244:30896263bd8b | 586 | // //BACKWARD |
Lightvalve | 244:30896263bd8b | 587 | // if(CNT_TMR4 < 2) |
Lightvalve | 244:30896263bd8b | 588 | // { |
Lightvalve | 244:30896263bd8b | 589 | // PWM_H2 = 0; |
Lightvalve | 244:30896263bd8b | 590 | // PWM_L1 = 0; |
Lightvalve | 244:30896263bd8b | 591 | // |
Lightvalve | 244:30896263bd8b | 592 | // PWM_L2 = 1; |
Lightvalve | 244:30896263bd8b | 593 | // PWM_H1 = 1; |
Lightvalve | 244:30896263bd8b | 594 | // } else if(CNT_TMR4 < 50) |
Lightvalve | 244:30896263bd8b | 595 | // { |
Lightvalve | 244:30896263bd8b | 596 | // PWM_H2 = 0; |
Lightvalve | 244:30896263bd8b | 597 | // PWM_H1 = 0; |
Lightvalve | 244:30896263bd8b | 598 | // |
Lightvalve | 244:30896263bd8b | 599 | // PWM_L2 = 1; |
Lightvalve | 244:30896263bd8b | 600 | // PWM_L1 = 1; |
Lightvalve | 244:30896263bd8b | 601 | // } else |
Lightvalve | 244:30896263bd8b | 602 | // { |
Lightvalve | 244:30896263bd8b | 603 | // CNT_TMR4 = 0; |
Lightvalve | 244:30896263bd8b | 604 | // } |
Lightvalve | 244:30896263bd8b | 605 | |
Lightvalve | 244:30896263bd8b | 606 | |
Lightvalve | 244:30896263bd8b | 607 | |
Lightvalve | 244:30896263bd8b | 608 | |
Lightvalve | 244:30896263bd8b | 609 | |
Lightvalve | 57:f4819de54e7a | 610 | CNT_TMR4++; |
Lightvalve | 21:e5f1a43ea6f9 | 611 | } |
Lightvalve | 11:82d8768d7351 | 612 | TIM4->SR = 0x0; // reset the status register |
Lightvalve | 11:82d8768d7351 | 613 | } |
Lightvalve | 19:23b7c1ad8683 | 614 | |
Lightvalve | 19:23b7c1ad8683 | 615 | |
Lightvalve | 18:b8adf1582ea3 | 616 | int j =0; |
Lightvalve | 241:fb5b57e3f157 | 617 | float FREQ_TMR5 = (float)FREQ_5k; |
Lightvalve | 241:fb5b57e3f157 | 618 | float DT_TMR5 = (float)DT_5k; |
Lightvalve | 45:35fa6884d0c6 | 619 | int cnt_trans = 0; |
Lightvalve | 57:f4819de54e7a | 620 | int can_rest =0; |
Lightvalve | 239:8ac5c6162bc1 | 621 | float force_ref_act_can = 0.0f; |
Lightvalve | 48:889798ff9329 | 622 | |
Lightvalve | 241:fb5b57e3f157 | 623 | extern "C" void TIM2_IRQHandler(void) |
Lightvalve | 21:e5f1a43ea6f9 | 624 | { |
Lightvalve | 241:fb5b57e3f157 | 625 | if (TIM2->SR & TIM_SR_UIF ) { |
Lightvalve | 241:fb5b57e3f157 | 626 | |
Lightvalve | 242:27eca07c9591 | 627 | // if (LED > 0) LED = 0; |
Lightvalve | 242:27eca07c9591 | 628 | // else LED = 1; |
Lightvalve | 57:f4819de54e7a | 629 | |
Lightvalve | 50:3c630b5eba9f | 630 | if(MODE_POS_FT_TRANS == 1) { |
jobuuu | 230:2c3e5ecbe7e1 | 631 | if (alpha_trans == 1.0f) MODE_POS_FT_TRANS = 2; |
Lightvalve | 241:fb5b57e3f157 | 632 | alpha_trans = (float)(1.0f - cos(3.141592f * (float)cnt_trans * DT_TMR5 /3.0f))/2.0f; |
Lightvalve | 45:35fa6884d0c6 | 633 | cnt_trans++; |
Lightvalve | 233:2ec0de8590d3 | 634 | torq.err_int = 0.0f; |
Lightvalve | 233:2ec0de8590d3 | 635 | force.err_int = 0.0f; |
Lightvalve | 241:fb5b57e3f157 | 636 | if((float)cnt_trans * DT_TMR5 > 3.0f) |
Lightvalve | 45:35fa6884d0c6 | 637 | MODE_POS_FT_TRANS = 2; |
Lightvalve | 50:3c630b5eba9f | 638 | } else if(MODE_POS_FT_TRANS == 3) { |
jobuuu | 230:2c3e5ecbe7e1 | 639 | if (alpha_trans == 0.0f) MODE_POS_FT_TRANS = 0; |
Lightvalve | 241:fb5b57e3f157 | 640 | alpha_trans = (float)(1.0f + cos(3.141592f * (float)cnt_trans * DT_TMR5 /3.0f))/2.0f; |
Lightvalve | 45:35fa6884d0c6 | 641 | cnt_trans++; |
Lightvalve | 233:2ec0de8590d3 | 642 | torq.err_int = 0.0f; |
Lightvalve | 233:2ec0de8590d3 | 643 | force.err_int = 0.0f; |
Lightvalve | 241:fb5b57e3f157 | 644 | if((float) cnt_trans * DT_TMR5 > 3.0f ) |
Lightvalve | 45:35fa6884d0c6 | 645 | MODE_POS_FT_TRANS = 0; |
Lightvalve | 50:3c630b5eba9f | 646 | } else if(MODE_POS_FT_TRANS == 2) { |
Lightvalve | 58:2eade98630e2 | 647 | alpha_trans = 1.0f; |
Lightvalve | 45:35fa6884d0c6 | 648 | cnt_trans = 0; |
Lightvalve | 50:3c630b5eba9f | 649 | } else { |
Lightvalve | 58:2eade98630e2 | 650 | alpha_trans = 0.0f; |
Lightvalve | 45:35fa6884d0c6 | 651 | cnt_trans = 0; |
Lightvalve | 45:35fa6884d0c6 | 652 | } |
Lightvalve | 45:35fa6884d0c6 | 653 | |
Lightvalve | 50:3c630b5eba9f | 654 | |
jobuuu | 230:2c3e5ecbe7e1 | 655 | // Reference Update ========================================================== |
hyhoon | 227:699c3e572283 | 656 | switch (REFERENCE_MODE) { |
hyhoon | 227:699c3e572283 | 657 | case MODE_REF_NO_ACT: { |
hyhoon | 227:699c3e572283 | 658 | break; |
hyhoon | 227:699c3e572283 | 659 | } |
hyhoon | 227:699c3e572283 | 660 | case MODE_REF_DIRECT: { |
hyhoon | 227:699c3e572283 | 661 | pos.ref = REF_POSITION; |
hyhoon | 227:699c3e572283 | 662 | vel.ref = REF_VELOCITY; |
hyhoon | 227:699c3e572283 | 663 | torq.ref = REF_TORQUE; |
jobuuu | 230:2c3e5ecbe7e1 | 664 | force.ref = REF_FORCE; |
jobuuu | 230:2c3e5ecbe7e1 | 665 | break; |
jobuuu | 230:2c3e5ecbe7e1 | 666 | } |
jobuuu | 230:2c3e5ecbe7e1 | 667 | case MODE_REF_FINDHOME: { |
jobuuu | 230:2c3e5ecbe7e1 | 668 | pos.ref = REF_POSITION_FINDHOME; |
jobuuu | 230:2c3e5ecbe7e1 | 669 | vel.ref = 0.0f; |
jobuuu | 230:2c3e5ecbe7e1 | 670 | torq.ref = 0.0f; |
jobuuu | 230:2c3e5ecbe7e1 | 671 | force.ref = 0.0f; |
hyhoon | 227:699c3e572283 | 672 | break; |
hyhoon | 227:699c3e572283 | 673 | } |
hyhoon | 227:699c3e572283 | 674 | default: |
hyhoon | 227:699c3e572283 | 675 | break; |
hyhoon | 227:699c3e572283 | 676 | } |
hyhoon | 227:699c3e572283 | 677 | |
Lightvalve | 237:c2cfe349f5c1 | 678 | if (((OPERATING_MODE&0b110)>>1) == 0) { |
jobuuu | 230:2c3e5ecbe7e1 | 679 | K_v = 1.03f; // Q = K_v*sqrt(deltaP)*tanh(C_d*Xv); |
Lightvalve | 235:c831718303c9 | 680 | C_d = 0.16f; |
jobuuu | 230:2c3e5ecbe7e1 | 681 | mV_PER_mA = 500.0f; // 5000mV/10mA |
jobuuu | 230:2c3e5ecbe7e1 | 682 | mV_PER_pulse = 0.5f; // 5000mV/10000pulse |
jobuuu | 230:2c3e5ecbe7e1 | 683 | mA_PER_pulse = 0.001f; // 10mA/10000pulse |
Lightvalve | 237:c2cfe349f5c1 | 684 | } else if (((OPERATING_MODE&0b110)>>1) == 1) { |
jobuuu | 230:2c3e5ecbe7e1 | 685 | K_v = 0.5f; // KNR (LPM >> mA) , 100bar |
jobuuu | 230:2c3e5ecbe7e1 | 686 | mV_PER_mA = 166.6666f; // 5000mV/30mA |
jobuuu | 230:2c3e5ecbe7e1 | 687 | mV_PER_pulse = 0.5f; // 5000mV/10000pulse |
jobuuu | 230:2c3e5ecbe7e1 | 688 | mA_PER_pulse = 0.003f; // 30mA/10000pulse |
Lightvalve | 237:c2cfe349f5c1 | 689 | } else if (((OPERATING_MODE&0b110)>>1) == 2) { |
Lightvalve | 237:c2cfe349f5c1 | 690 | C_d = 0.0000845f; // Q = C_d * Valve_pos * sqrt(deltaP*alpha/(1+alpha)) : Valve_pos = 10000, deltaP = 70, alpha = 1 -> Q = 5 |
jobuuu | 230:2c3e5ecbe7e1 | 691 | } |
Lightvalve | 235:c831718303c9 | 692 | |
jobuuu | 230:2c3e5ecbe7e1 | 693 | // ===================================================================== |
jobuuu | 230:2c3e5ecbe7e1 | 694 | // CONTROL LOOP -------------------------------------------------------- |
jobuuu | 230:2c3e5ecbe7e1 | 695 | // ===================================================================== |
Lightvalve | 57:f4819de54e7a | 696 | int UTILITY_MODE = 0; |
Lightvalve | 57:f4819de54e7a | 697 | int CONTROL_MODE = 0; |
Lightvalve | 58:2eade98630e2 | 698 | |
Lightvalve | 57:f4819de54e7a | 699 | if (CONTROL_UTILITY_MODE >= 20 || CONTROL_UTILITY_MODE == 0) { |
Lightvalve | 57:f4819de54e7a | 700 | UTILITY_MODE = CONTROL_UTILITY_MODE; |
Lightvalve | 57:f4819de54e7a | 701 | CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 57:f4819de54e7a | 702 | } else { |
Lightvalve | 57:f4819de54e7a | 703 | CONTROL_MODE = CONTROL_UTILITY_MODE; |
Lightvalve | 57:f4819de54e7a | 704 | UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 57:f4819de54e7a | 705 | } |
Lightvalve | 57:f4819de54e7a | 706 | // UTILITY MODE ------------------------------------------------------------ |
Lightvalve | 57:f4819de54e7a | 707 | switch (UTILITY_MODE) { |
Lightvalve | 57:f4819de54e7a | 708 | case MODE_NO_ACT: { |
Lightvalve | 13:747daba9cf59 | 709 | break; |
Lightvalve | 13:747daba9cf59 | 710 | } |
Lightvalve | 14:8e7590227d22 | 711 | |
Lightvalve | 235:c831718303c9 | 712 | case MODE_TORQUE_SENSOR_NULLING: { |
Lightvalve | 235:c831718303c9 | 713 | static float FORCE_pulse_sum = 0.0; |
Lightvalve | 235:c831718303c9 | 714 | static float PresA_pulse_sum = 0.0; |
Lightvalve | 235:c831718303c9 | 715 | static float PresB_pulse_sum = 0.0; |
Lightvalve | 235:c831718303c9 | 716 | |
Lightvalve | 13:747daba9cf59 | 717 | // DAC Voltage reference set |
Lightvalve | 232:2976cf1b4252 | 718 | float VREF_TuningGain = -0.000003f; |
jobuuu | 230:2c3e5ecbe7e1 | 719 | if (TMR3_COUNT_TORQUE_NULL < TMR_FREQ_5k * 5) { |
jobuuu | 230:2c3e5ecbe7e1 | 720 | LED = 1; |
jobuuu | 230:2c3e5ecbe7e1 | 721 | if(SENSING_MODE == 0) { // Force Sensor (Loadcell) |
jobuuu | 230:2c3e5ecbe7e1 | 722 | FORCE_pulse_sum = FORCE_pulse_sum + force.sen*TORQUE_SENSOR_PULSE_PER_TORQUE; |
jobuuu | 230:2c3e5ecbe7e1 | 723 | if (TMR3_COUNT_TORQUE_NULL % 10 == 0) { |
jobuuu | 230:2c3e5ecbe7e1 | 724 | float FORCE_pluse_mean = FORCE_pulse_sum / 10.0f; |
jobuuu | 230:2c3e5ecbe7e1 | 725 | FORCE_pulse_sum = 0.0f; |
Lightvalve | 19:23b7c1ad8683 | 726 | |
jobuuu | 230:2c3e5ecbe7e1 | 727 | FORCE_VREF += VREF_TuningGain * (0.0f - FORCE_pluse_mean); |
jobuuu | 230:2c3e5ecbe7e1 | 728 | if (FORCE_VREF > 3.3f) FORCE_VREF = 3.3f; |
jobuuu | 230:2c3e5ecbe7e1 | 729 | if (FORCE_VREF < 0.0f) FORCE_VREF = 0.0f; |
jobuuu | 230:2c3e5ecbe7e1 | 730 | dac_1 = FORCE_VREF / 3.3f; |
jobuuu | 230:2c3e5ecbe7e1 | 731 | } |
Lightvalve | 235:c831718303c9 | 732 | } else if (SENSING_MODE == 1) { // Pressure Sensor |
jobuuu | 230:2c3e5ecbe7e1 | 733 | PresA_pulse_sum += pres_A.sen*PRES_SENSOR_A_PULSE_PER_BAR; |
jobuuu | 230:2c3e5ecbe7e1 | 734 | PresB_pulse_sum += pres_B.sen*PRES_SENSOR_B_PULSE_PER_BAR; |
jobuuu | 230:2c3e5ecbe7e1 | 735 | if (TMR3_COUNT_TORQUE_NULL % 10 == 0) { |
jobuuu | 230:2c3e5ecbe7e1 | 736 | float PresA_pluse_mean = PresA_pulse_sum / 10.0f; |
jobuuu | 230:2c3e5ecbe7e1 | 737 | float PresB_pluse_mean = PresB_pulse_sum / 10.0f; |
jobuuu | 230:2c3e5ecbe7e1 | 738 | PresA_pulse_sum = 0.0f; |
jobuuu | 230:2c3e5ecbe7e1 | 739 | PresB_pulse_sum = 0.0f; |
Lightvalve | 19:23b7c1ad8683 | 740 | |
jobuuu | 230:2c3e5ecbe7e1 | 741 | PRES_A_VREF += VREF_TuningGain * (0.0f - PresA_pluse_mean); |
jobuuu | 230:2c3e5ecbe7e1 | 742 | if (PRES_A_VREF > 3.3f) PRES_A_VREF = 3.3f; |
jobuuu | 230:2c3e5ecbe7e1 | 743 | if (PRES_A_VREF < 0.0f) PRES_A_VREF = 0.0f; |
jobuuu | 230:2c3e5ecbe7e1 | 744 | dac_1 = PRES_A_VREF / 3.3f; |
jobuuu | 230:2c3e5ecbe7e1 | 745 | PRES_B_VREF += VREF_TuningGain * (0.0f - PresB_pluse_mean); |
jobuuu | 230:2c3e5ecbe7e1 | 746 | if (PRES_B_VREF > 3.3f) PRES_B_VREF = 3.3f; |
Lightvalve | 235:c831718303c9 | 747 | if (PRES_B_VREF < 0.0f) PRES_B_VREF = 0.0f; |
jobuuu | 230:2c3e5ecbe7e1 | 748 | dac_2 = PRES_B_VREF / 3.3f; |
jobuuu | 230:2c3e5ecbe7e1 | 749 | } |
Lightvalve | 13:747daba9cf59 | 750 | } |
jobuuu | 230:2c3e5ecbe7e1 | 751 | TMR3_COUNT_TORQUE_NULL++; |
Lightvalve | 13:747daba9cf59 | 752 | } else { |
jobuuu | 230:2c3e5ecbe7e1 | 753 | if(SENSING_MODE == 0 ) { // Force Sensor (Loadcell) |
jobuuu | 230:2c3e5ecbe7e1 | 754 | FORCE_pulse_sum = 0.0f; |
jobuuu | 230:2c3e5ecbe7e1 | 755 | dac_1 = FORCE_VREF / 3.3f; |
jobuuu | 230:2c3e5ecbe7e1 | 756 | spi_eeprom_write(RID_FORCE_SENSOR_VREF, (int16_t)(FORCE_VREF * 1000.0f)); |
jobuuu | 230:2c3e5ecbe7e1 | 757 | } else if (SENSING_MODE == 1) { |
jobuuu | 230:2c3e5ecbe7e1 | 758 | PresA_pulse_sum = 0.0f; |
jobuuu | 230:2c3e5ecbe7e1 | 759 | PresB_pulse_sum = 0.0f; |
jobuuu | 230:2c3e5ecbe7e1 | 760 | dac_1 = PRES_A_VREF / 3.3f; |
jobuuu | 230:2c3e5ecbe7e1 | 761 | dac_2 = PRES_B_VREF / 3.3f; |
jobuuu | 230:2c3e5ecbe7e1 | 762 | spi_eeprom_write(RID_PRES_A_SENSOR_VREF, (int16_t)(PRES_A_VREF * 1000.0f)); |
jobuuu | 230:2c3e5ecbe7e1 | 763 | spi_eeprom_write(RID_PRES_B_SENSOR_VREF, (int16_t)(PRES_B_VREF * 1000.0f)); |
jobuuu | 230:2c3e5ecbe7e1 | 764 | } |
Lightvalve | 58:2eade98630e2 | 765 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 14:8e7590227d22 | 766 | TMR3_COUNT_TORQUE_NULL = 0; |
Lightvalve | 13:747daba9cf59 | 767 | } |
Lightvalve | 13:747daba9cf59 | 768 | break; |
Lightvalve | 19:23b7c1ad8683 | 769 | } |
Lightvalve | 14:8e7590227d22 | 770 | |
Lightvalve | 235:c831718303c9 | 771 | case MODE_FIND_HOME: { |
jobuuu | 230:2c3e5ecbe7e1 | 772 | static int cnt_findhome = 0; |
jobuuu | 230:2c3e5ecbe7e1 | 773 | static int cnt_terminate_findhome = 0; |
jobuuu | 230:2c3e5ecbe7e1 | 774 | static float FINDHOME_POSITION_pulse = 0.0f; |
jobuuu | 230:2c3e5ecbe7e1 | 775 | static float FINDHOME_POSITION_pulse_OLD = 0.0f; |
jobuuu | 230:2c3e5ecbe7e1 | 776 | static float FINDHOME_VELOCITY_pulse = 0.0f; |
Lightvalve | 235:c831718303c9 | 777 | static float REF_POSITION_FINDHOME_INIT = 0.0f; |
jobuuu | 230:2c3e5ecbe7e1 | 778 | |
Lightvalve | 29:69f3f5445d6d | 779 | if (FINDHOME_STAGE == FINDHOME_INIT) { |
jobuuu | 230:2c3e5ecbe7e1 | 780 | REFERENCE_MODE = MODE_REF_FINDHOME; |
Lightvalve | 14:8e7590227d22 | 781 | cnt_findhome = 0; |
jobuuu | 230:2c3e5ecbe7e1 | 782 | cnt_terminate_findhome = 0; |
Lightvalve | 59:f308b1656d9c | 783 | pos.ref = pos.sen; |
Lightvalve | 59:f308b1656d9c | 784 | vel.ref = 0.0f; |
jobuuu | 230:2c3e5ecbe7e1 | 785 | REF_POSITION_FINDHOME = pos.ref; |
Lightvalve | 29:69f3f5445d6d | 786 | FINDHOME_STAGE = FINDHOME_GOTOLIMIT; |
Lightvalve | 29:69f3f5445d6d | 787 | } else if (FINDHOME_STAGE == FINDHOME_GOTOLIMIT) { |
jobuuu | 230:2c3e5ecbe7e1 | 788 | int cnt_check_enc = (TMR_FREQ_5k/20); // 5000/20 = 250tic = 50msec |
Lightvalve | 29:69f3f5445d6d | 789 | if(cnt_findhome%cnt_check_enc == 0) { |
jobuuu | 230:2c3e5ecbe7e1 | 790 | FINDHOME_POSITION_pulse = pos.sen*ENC_PULSE_PER_POSITION; |
jobuuu | 230:2c3e5ecbe7e1 | 791 | FINDHOME_VELOCITY_pulse = FINDHOME_POSITION_pulse - FINDHOME_POSITION_pulse_OLD; |
jobuuu | 230:2c3e5ecbe7e1 | 792 | FINDHOME_POSITION_pulse_OLD = FINDHOME_POSITION_pulse; |
Lightvalve | 29:69f3f5445d6d | 793 | } |
Lightvalve | 29:69f3f5445d6d | 794 | cnt_findhome++; |
Lightvalve | 14:8e7590227d22 | 795 | |
jobuuu | 230:2c3e5ecbe7e1 | 796 | if (fabs(FINDHOME_VELOCITY_pulse) <= 1) { |
jobuuu | 230:2c3e5ecbe7e1 | 797 | cnt_terminate_findhome = cnt_terminate_findhome + 1; |
Lightvalve | 29:69f3f5445d6d | 798 | } else { |
jobuuu | 230:2c3e5ecbe7e1 | 799 | cnt_terminate_findhome = 0; |
Lightvalve | 29:69f3f5445d6d | 800 | } |
Lightvalve | 19:23b7c1ad8683 | 801 | |
jobuuu | 230:2c3e5ecbe7e1 | 802 | if ((cnt_terminate_findhome < 3*TMR_FREQ_5k) && cnt_findhome < 10*TMR_FREQ_5k) { // wait for 3sec |
jobuuu | 230:2c3e5ecbe7e1 | 803 | double GOTOHOME_SPEED = 10.0f; // 20mm/s or 20deg/s |
hyhoon | 226:82a3ca333004 | 804 | if (HOMEPOS_OFFSET > 0) { |
Lightvalve | 235:c831718303c9 | 805 | REF_POSITION_FINDHOME = REF_POSITION_FINDHOME + GOTOHOME_SPEED*DT_5k; |
hyhoon | 226:82a3ca333004 | 806 | } else { |
jobuuu | 230:2c3e5ecbe7e1 | 807 | REF_POSITION_FINDHOME = REF_POSITION_FINDHOME - GOTOHOME_SPEED*DT_5k; |
hyhoon | 226:82a3ca333004 | 808 | } |
Lightvalve | 59:f308b1656d9c | 809 | CONTROL_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 59:f308b1656d9c | 810 | alpha_trans = 0.0f; |
Lightvalve | 29:69f3f5445d6d | 811 | } else { |
jobuuu | 230:2c3e5ecbe7e1 | 812 | ENC_SET((long)((long)HOMEPOS_OFFSET*10)); |
jobuuu | 230:2c3e5ecbe7e1 | 813 | REF_POSITION_FINDHOME_INIT = (float)((long)HOMEPOS_OFFSET*10); |
jobuuu | 230:2c3e5ecbe7e1 | 814 | FINDHOME_POSITION_pulse = 0; |
jobuuu | 230:2c3e5ecbe7e1 | 815 | FINDHOME_POSITION_pulse_OLD = 0; |
jobuuu | 230:2c3e5ecbe7e1 | 816 | FINDHOME_VELOCITY_pulse = 0; |
Lightvalve | 235:c831718303c9 | 817 | |
Lightvalve | 29:69f3f5445d6d | 818 | cnt_findhome = 0; |
jobuuu | 230:2c3e5ecbe7e1 | 819 | cnt_terminate_findhome = 0; |
jobuuu | 230:2c3e5ecbe7e1 | 820 | pos.ref = 0.0f; |
Lightvalve | 29:69f3f5445d6d | 821 | FINDHOME_STAGE = FINDHOME_ZEROPOSE; |
Lightvalve | 29:69f3f5445d6d | 822 | } |
Lightvalve | 29:69f3f5445d6d | 823 | } else if (FINDHOME_STAGE == FINDHOME_ZEROPOSE) { |
Lightvalve | 235:c831718303c9 | 824 | |
Lightvalve | 232:2976cf1b4252 | 825 | // int T_move = 2*TMR_FREQ_5k; |
Lightvalve | 232:2976cf1b4252 | 826 | int T_move = 10000; |
Lightvalve | 232:2976cf1b4252 | 827 | REF_POSITION_FINDHOME = ((0.0f - REF_POSITION_FINDHOME_INIT)*0.5f*(1.0f - cos(3.14159f * (float)cnt_findhome / (float)T_move)) + (float)REF_POSITION_FINDHOME_INIT)/ENC_PULSE_PER_POSITION; |
Lightvalve | 235:c831718303c9 | 828 | |
Lightvalve | 29:69f3f5445d6d | 829 | cnt_findhome++; |
Lightvalve | 235:c831718303c9 | 830 | |
Lightvalve | 232:2976cf1b4252 | 831 | REFERENCE_MODE = MODE_REF_FINDHOME; |
Lightvalve | 232:2976cf1b4252 | 832 | CONTROL_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 232:2976cf1b4252 | 833 | alpha_trans = 0.0f; |
Lightvalve | 235:c831718303c9 | 834 | |
Lightvalve | 29:69f3f5445d6d | 835 | if (cnt_findhome >= T_move) { |
Lightvalve | 29:69f3f5445d6d | 836 | cnt_findhome = 0; |
Lightvalve | 30:8d561f16383b | 837 | pos.ref = 0.0f; |
Lightvalve | 29:69f3f5445d6d | 838 | FINDHOME_STAGE = FINDHOME_INIT; |
Lightvalve | 57:f4819de54e7a | 839 | CONTROL_UTILITY_MODE = MODE_JOINT_CONTROL; |
jobuuu | 230:2c3e5ecbe7e1 | 840 | REFERENCE_MODE = MODE_REF_DIRECT; |
hyhoon | 226:82a3ca333004 | 841 | } |
Lightvalve | 13:747daba9cf59 | 842 | } |
Lightvalve | 13:747daba9cf59 | 843 | break; |
Lightvalve | 13:747daba9cf59 | 844 | } |
Lightvalve | 237:c2cfe349f5c1 | 845 | |
Lightvalve | 237:c2cfe349f5c1 | 846 | case MODE_DDV_POS_VS_PWM_ID: { |
Lightvalve | 237:c2cfe349f5c1 | 847 | CONTROL_MODE = MODE_VALVE_OPEN_LOOP; |
Lightvalve | 237:c2cfe349f5c1 | 848 | VALVE_ID_timer = VALVE_ID_timer + 1; |
Lightvalve | 237:c2cfe349f5c1 | 849 | |
Lightvalve | 237:c2cfe349f5c1 | 850 | if(VALVE_ID_timer < TMR_FREQ_5k*1) { |
Lightvalve | 237:c2cfe349f5c1 | 851 | Vout.ref = 3000.0f * sin(2.0f*3.14159f*VALVE_ID_timer/TMR_FREQ_5k * 100.0f); |
Lightvalve | 237:c2cfe349f5c1 | 852 | } else if(VALVE_ID_timer < TMR_FREQ_5k*2) { |
Lightvalve | 237:c2cfe349f5c1 | 853 | Vout.ref = 1000.0f*(ID_index_array[ID_index]); |
Lightvalve | 237:c2cfe349f5c1 | 854 | } else if(VALVE_ID_timer == TMR_FREQ_5k*2) { |
Lightvalve | 237:c2cfe349f5c1 | 855 | VALVE_POS_TMP = 0; |
Lightvalve | 237:c2cfe349f5c1 | 856 | data_num = 0; |
Lightvalve | 237:c2cfe349f5c1 | 857 | } else if(VALVE_ID_timer < TMR_FREQ_5k*3) { |
Lightvalve | 237:c2cfe349f5c1 | 858 | data_num = data_num + 1; |
Lightvalve | 243:b235d67d25ba | 859 | VALVE_POS_TMP = VALVE_POS_TMP + valve_pos.sen; |
Lightvalve | 237:c2cfe349f5c1 | 860 | } else if(VALVE_ID_timer == TMR_FREQ_5k*3) { |
Lightvalve | 237:c2cfe349f5c1 | 861 | Vout.ref = 0.0f; |
Lightvalve | 237:c2cfe349f5c1 | 862 | } else { |
Lightvalve | 237:c2cfe349f5c1 | 863 | VALVE_POS_AVG[ID_index] = VALVE_POS_TMP / data_num; |
Lightvalve | 237:c2cfe349f5c1 | 864 | VALVE_ID_timer = 0; |
Lightvalve | 237:c2cfe349f5c1 | 865 | ID_index= ID_index +1; |
Lightvalve | 237:c2cfe349f5c1 | 866 | } |
Lightvalve | 237:c2cfe349f5c1 | 867 | |
Lightvalve | 237:c2cfe349f5c1 | 868 | if(ID_index>=25) { |
Lightvalve | 237:c2cfe349f5c1 | 869 | int i; |
Lightvalve | 237:c2cfe349f5c1 | 870 | VALVE_POS_AVG_OLD = VALVE_POS_AVG[0]; |
Lightvalve | 237:c2cfe349f5c1 | 871 | for(i=0; i<25; i++) { |
Lightvalve | 237:c2cfe349f5c1 | 872 | VALVE_POS_VS_PWM[i] = (int16_t) (VALVE_POS_AVG[i]); |
Lightvalve | 237:c2cfe349f5c1 | 873 | if(VALVE_POS_AVG[i] > VALVE_POS_AVG_OLD) { |
Lightvalve | 237:c2cfe349f5c1 | 874 | VALVE_MAX_POS = VALVE_POS_AVG[i]; |
Lightvalve | 237:c2cfe349f5c1 | 875 | VALVE_POS_AVG_OLD = VALVE_MAX_POS; |
Lightvalve | 237:c2cfe349f5c1 | 876 | } else if(VALVE_POS_AVG[i] < VALVE_POS_AVG_OLD) { |
Lightvalve | 237:c2cfe349f5c1 | 877 | VALVE_MIN_POS = VALVE_POS_AVG[i]; |
Lightvalve | 237:c2cfe349f5c1 | 878 | VALVE_POS_AVG_OLD = VALVE_MIN_POS; |
Lightvalve | 237:c2cfe349f5c1 | 879 | } |
Lightvalve | 237:c2cfe349f5c1 | 880 | } |
Lightvalve | 237:c2cfe349f5c1 | 881 | VALVE_ELECTRIC_CENTER = VALVE_POS_VS_PWM[0]; |
Lightvalve | 237:c2cfe349f5c1 | 882 | spi_eeprom_write(RID_VALVE_ELECTRIC_CENTER, (int16_t) VALVE_ELECTRIC_CENTER); |
Lightvalve | 237:c2cfe349f5c1 | 883 | spi_eeprom_write(RID_VALVE_MAX_POS, (int16_t) VALVE_MAX_POS); |
Lightvalve | 237:c2cfe349f5c1 | 884 | spi_eeprom_write(RID_VALVE_MIN_POS, (int16_t) VALVE_MIN_POS); |
Lightvalve | 237:c2cfe349f5c1 | 885 | for(int i=0; i<25; i++) { |
Lightvalve | 237:c2cfe349f5c1 | 886 | spi_eeprom_write(RID_VALVE_POS_VS_PWM_0 + i, (int16_t) VALVE_POS_VS_PWM[i]); |
Lightvalve | 237:c2cfe349f5c1 | 887 | } |
Lightvalve | 237:c2cfe349f5c1 | 888 | ID_index = 0; |
Lightvalve | 237:c2cfe349f5c1 | 889 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 237:c2cfe349f5c1 | 890 | } |
Lightvalve | 237:c2cfe349f5c1 | 891 | |
Lightvalve | 237:c2cfe349f5c1 | 892 | |
Lightvalve | 237:c2cfe349f5c1 | 893 | break; |
Lightvalve | 237:c2cfe349f5c1 | 894 | } |
Lightvalve | 237:c2cfe349f5c1 | 895 | case MODE_DDV_DEADZONE_AND_CENTER: { |
Lightvalve | 237:c2cfe349f5c1 | 896 | CONTROL_MODE = MODE_VALVE_OPEN_LOOP; |
Lightvalve | 237:c2cfe349f5c1 | 897 | VALVE_DZ_timer = VALVE_DZ_timer + 1; |
Lightvalve | 237:c2cfe349f5c1 | 898 | if(first_check == 0) { |
Lightvalve | 237:c2cfe349f5c1 | 899 | if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 237:c2cfe349f5c1 | 900 | Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 237:c2cfe349f5c1 | 901 | } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 237:c2cfe349f5c1 | 902 | Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 237:c2cfe349f5c1 | 903 | pos_plus_end = pos.sen; |
Lightvalve | 237:c2cfe349f5c1 | 904 | } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 237:c2cfe349f5c1 | 905 | Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 237:c2cfe349f5c1 | 906 | } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 237:c2cfe349f5c1 | 907 | Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 237:c2cfe349f5c1 | 908 | pos_minus_end = pos.sen; |
Lightvalve | 237:c2cfe349f5c1 | 909 | } else if(VALVE_DZ_timer < (int) (3.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 237:c2cfe349f5c1 | 910 | Vout.ref = (float) P_GAIN_JOINT_POSITION * (0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen)*(float) 50.0f; |
Lightvalve | 237:c2cfe349f5c1 | 911 | } else if(VALVE_DZ_timer < (int) (4.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 237:c2cfe349f5c1 | 912 | Vout.ref = (float) P_GAIN_JOINT_POSITION * (0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen)*(float) 50.0f; |
Lightvalve | 237:c2cfe349f5c1 | 913 | data_num = data_num + 1; |
Lightvalve | 244:30896263bd8b | 914 | VALVE_POS_TMP = VALVE_POS_TMP + valve_pos.sen; |
Lightvalve | 237:c2cfe349f5c1 | 915 | } else if(VALVE_DZ_timer == (int) (4.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 237:c2cfe349f5c1 | 916 | Vout.ref = (float) P_GAIN_JOINT_POSITION * (0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen)*(float) 50.0f; |
Lightvalve | 237:c2cfe349f5c1 | 917 | DDV_POS_AVG = VALVE_POS_TMP / data_num; |
Lightvalve | 237:c2cfe349f5c1 | 918 | START_POS = pos.sen; |
Lightvalve | 237:c2cfe349f5c1 | 919 | VALVE_POS_TMP = 0; |
Lightvalve | 237:c2cfe349f5c1 | 920 | data_num = 0; |
Lightvalve | 237:c2cfe349f5c1 | 921 | |
Lightvalve | 237:c2cfe349f5c1 | 922 | } else if(VALVE_DZ_timer < (int) (5.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 238:4e660aa77eb7 | 923 | valve_pos_raw.ref = DDV_POS_AVG; |
Lightvalve | 238:4e660aa77eb7 | 924 | VALVE_POS_CONTROL(valve_pos_raw.ref); |
Lightvalve | 237:c2cfe349f5c1 | 925 | |
Lightvalve | 237:c2cfe349f5c1 | 926 | } else if(VALVE_DZ_timer < (int) (6.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 238:4e660aa77eb7 | 927 | valve_pos_raw.ref = DDV_POS_AVG; |
Lightvalve | 238:4e660aa77eb7 | 928 | VALVE_POS_CONTROL(valve_pos_raw.ref); |
Lightvalve | 237:c2cfe349f5c1 | 929 | |
Lightvalve | 237:c2cfe349f5c1 | 930 | } else if(VALVE_DZ_timer == (int) (6.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 238:4e660aa77eb7 | 931 | valve_pos_raw.ref = DDV_POS_AVG; |
Lightvalve | 238:4e660aa77eb7 | 932 | VALVE_POS_CONTROL(valve_pos_raw.ref); |
Lightvalve | 237:c2cfe349f5c1 | 933 | FINAL_POS = pos.sen; |
Lightvalve | 237:c2cfe349f5c1 | 934 | |
Lightvalve | 238:4e660aa77eb7 | 935 | if((FINAL_POS - START_POS)>1) { |
Lightvalve | 237:c2cfe349f5c1 | 936 | DZ_case = 1; |
Lightvalve | 238:4e660aa77eb7 | 937 | } else if((FINAL_POS - START_POS)<-1) { |
Lightvalve | 237:c2cfe349f5c1 | 938 | DZ_case = -1; |
Lightvalve | 237:c2cfe349f5c1 | 939 | } else { |
Lightvalve | 237:c2cfe349f5c1 | 940 | DZ_case = 0; |
Lightvalve | 237:c2cfe349f5c1 | 941 | } |
Lightvalve | 237:c2cfe349f5c1 | 942 | |
Lightvalve | 237:c2cfe349f5c1 | 943 | first_check = 1; |
Lightvalve | 237:c2cfe349f5c1 | 944 | DZ_DIRECTION = 1; |
Lightvalve | 237:c2cfe349f5c1 | 945 | VALVE_DZ_timer = 0; |
Lightvalve | 237:c2cfe349f5c1 | 946 | Ref_Valve_Pos_Old = DDV_POS_AVG; |
Lightvalve | 237:c2cfe349f5c1 | 947 | DZ_NUM = 1; |
Lightvalve | 237:c2cfe349f5c1 | 948 | DZ_index = 1; |
Lightvalve | 237:c2cfe349f5c1 | 949 | |
Lightvalve | 237:c2cfe349f5c1 | 950 | } |
Lightvalve | 237:c2cfe349f5c1 | 951 | } else { |
Lightvalve | 237:c2cfe349f5c1 | 952 | if((DZ_case == -1 && DZ_NUM == 1) | (DZ_case == 1 && DZ_NUM == 1)) { |
Lightvalve | 244:30896263bd8b | 953 | if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 237:c2cfe349f5c1 | 954 | Vout.ref = (float) P_GAIN_JOINT_POSITION * (0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen)*(float) 50.0f; |
Lightvalve | 237:c2cfe349f5c1 | 955 | } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 237:c2cfe349f5c1 | 956 | START_POS = pos.sen; |
Lightvalve | 237:c2cfe349f5c1 | 957 | } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 238:4e660aa77eb7 | 958 | valve_pos_raw.ref = Ref_Valve_Pos_Old - DZ_case * DZ_DIRECTION * 64 / DZ_index; |
Lightvalve | 238:4e660aa77eb7 | 959 | if(valve_pos_raw.ref <= VALVE_MIN_POS) { |
Lightvalve | 238:4e660aa77eb7 | 960 | valve_pos_raw.ref = VALVE_MIN_POS; |
Lightvalve | 238:4e660aa77eb7 | 961 | } else if(valve_pos_raw.ref >= VALVE_MAX_POS) { |
Lightvalve | 238:4e660aa77eb7 | 962 | valve_pos_raw.ref = VALVE_MAX_POS; |
Lightvalve | 237:c2cfe349f5c1 | 963 | } |
Lightvalve | 238:4e660aa77eb7 | 964 | VALVE_POS_CONTROL(valve_pos_raw.ref); |
Lightvalve | 237:c2cfe349f5c1 | 965 | |
Lightvalve | 237:c2cfe349f5c1 | 966 | } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 238:4e660aa77eb7 | 967 | Ref_Valve_Pos_Old = valve_pos_raw.ref; |
Lightvalve | 237:c2cfe349f5c1 | 968 | FINAL_POS = pos.sen; |
Lightvalve | 237:c2cfe349f5c1 | 969 | |
Lightvalve | 238:4e660aa77eb7 | 970 | if((FINAL_POS - START_POS)>1) { |
Lightvalve | 237:c2cfe349f5c1 | 971 | DZ_DIRECTION = 1 * DZ_case; |
Lightvalve | 238:4e660aa77eb7 | 972 | } else if((FINAL_POS - START_POS)<-1) { |
Lightvalve | 237:c2cfe349f5c1 | 973 | DZ_DIRECTION = -1 * DZ_case; |
Lightvalve | 237:c2cfe349f5c1 | 974 | } else { |
Lightvalve | 237:c2cfe349f5c1 | 975 | DZ_DIRECTION = 1 * DZ_case; |
Lightvalve | 237:c2cfe349f5c1 | 976 | } |
Lightvalve | 237:c2cfe349f5c1 | 977 | |
Lightvalve | 237:c2cfe349f5c1 | 978 | VALVE_DZ_timer = 0; |
Lightvalve | 237:c2cfe349f5c1 | 979 | DZ_index= DZ_index *2; |
Lightvalve | 237:c2cfe349f5c1 | 980 | if(DZ_index >= 128) { |
Lightvalve | 238:4e660aa77eb7 | 981 | FIRST_DZ = valve_pos_raw.ref; |
Lightvalve | 237:c2cfe349f5c1 | 982 | DZ_NUM = 2; |
Lightvalve | 237:c2cfe349f5c1 | 983 | Ref_Valve_Pos_Old = FIRST_DZ; |
Lightvalve | 237:c2cfe349f5c1 | 984 | DZ_index = 1; |
Lightvalve | 237:c2cfe349f5c1 | 985 | DZ_DIRECTION = 1; |
Lightvalve | 237:c2cfe349f5c1 | 986 | } |
Lightvalve | 237:c2cfe349f5c1 | 987 | } |
Lightvalve | 237:c2cfe349f5c1 | 988 | } else if((DZ_case == -1 && DZ_NUM == 2) | (DZ_case == 1 && DZ_NUM == 2)) { |
Lightvalve | 237:c2cfe349f5c1 | 989 | if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 237:c2cfe349f5c1 | 990 | Vout.ref = (float) P_GAIN_JOINT_POSITION * (0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen)*(float) 50.0f; |
Lightvalve | 237:c2cfe349f5c1 | 991 | } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 237:c2cfe349f5c1 | 992 | START_POS = pos.sen; |
Lightvalve | 237:c2cfe349f5c1 | 993 | } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 238:4e660aa77eb7 | 994 | valve_pos_raw.ref = Ref_Valve_Pos_Old - DZ_case * DZ_DIRECTION * 64 / DZ_index; |
Lightvalve | 238:4e660aa77eb7 | 995 | if(valve_pos_raw.ref <= VALVE_MIN_POS) { |
Lightvalve | 238:4e660aa77eb7 | 996 | valve_pos_raw.ref = VALVE_MIN_POS; |
Lightvalve | 238:4e660aa77eb7 | 997 | } else if(valve_pos_raw.ref >= VALVE_MAX_POS) { |
Lightvalve | 238:4e660aa77eb7 | 998 | valve_pos_raw.ref = VALVE_MAX_POS; |
Lightvalve | 237:c2cfe349f5c1 | 999 | } |
Lightvalve | 238:4e660aa77eb7 | 1000 | VALVE_POS_CONTROL(valve_pos_raw.ref); |
Lightvalve | 237:c2cfe349f5c1 | 1001 | |
Lightvalve | 237:c2cfe349f5c1 | 1002 | } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 237:c2cfe349f5c1 | 1003 | Vout.ref = 0.0f; |
Lightvalve | 237:c2cfe349f5c1 | 1004 | } else if(VALVE_DZ_timer > (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 238:4e660aa77eb7 | 1005 | Ref_Valve_Pos_Old = valve_pos_raw.ref; |
Lightvalve | 237:c2cfe349f5c1 | 1006 | FINAL_POS = pos.sen; |
Lightvalve | 237:c2cfe349f5c1 | 1007 | |
Lightvalve | 238:4e660aa77eb7 | 1008 | if((FINAL_POS - START_POS)>1) { |
Lightvalve | 237:c2cfe349f5c1 | 1009 | DZ_DIRECTION = 1 * DZ_case; |
Lightvalve | 238:4e660aa77eb7 | 1010 | } else if((FINAL_POS - START_POS)<-1) { |
Lightvalve | 237:c2cfe349f5c1 | 1011 | DZ_DIRECTION = -1 * DZ_case; |
Lightvalve | 237:c2cfe349f5c1 | 1012 | } else { |
Lightvalve | 237:c2cfe349f5c1 | 1013 | DZ_DIRECTION = -1 * DZ_case; |
Lightvalve | 237:c2cfe349f5c1 | 1014 | } |
Lightvalve | 237:c2cfe349f5c1 | 1015 | |
Lightvalve | 237:c2cfe349f5c1 | 1016 | VALVE_DZ_timer = 0; |
Lightvalve | 237:c2cfe349f5c1 | 1017 | DZ_index= DZ_index * 2; |
Lightvalve | 237:c2cfe349f5c1 | 1018 | if(DZ_index >= 128) { |
Lightvalve | 238:4e660aa77eb7 | 1019 | SECOND_DZ = valve_pos_raw.ref; |
Lightvalve | 237:c2cfe349f5c1 | 1020 | VALVE_CENTER = (int) (0.5f * (float) (FIRST_DZ) + 0.5f * (float) (SECOND_DZ)); |
Lightvalve | 237:c2cfe349f5c1 | 1021 | first_check = 0; |
Lightvalve | 237:c2cfe349f5c1 | 1022 | VALVE_DEADZONE_MINUS = (float) FIRST_DZ; |
Lightvalve | 237:c2cfe349f5c1 | 1023 | VALVE_DEADZONE_PLUS = (float) SECOND_DZ; |
Lightvalve | 237:c2cfe349f5c1 | 1024 | |
Lightvalve | 237:c2cfe349f5c1 | 1025 | spi_eeprom_write(RID_VALVE_CNETER, (int16_t) VALVE_CENTER); |
Lightvalve | 237:c2cfe349f5c1 | 1026 | spi_eeprom_write(RID_VALVE_DEADZONE_PLUS, (int16_t) VALVE_DEADZONE_PLUS); |
Lightvalve | 237:c2cfe349f5c1 | 1027 | spi_eeprom_write(RID_VALVE_DEADZONE_MINUS, (int16_t) VALVE_DEADZONE_MINUS); |
Lightvalve | 237:c2cfe349f5c1 | 1028 | |
Lightvalve | 237:c2cfe349f5c1 | 1029 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 237:c2cfe349f5c1 | 1030 | DZ_index = 1; |
Lightvalve | 237:c2cfe349f5c1 | 1031 | } |
Lightvalve | 237:c2cfe349f5c1 | 1032 | } |
Lightvalve | 237:c2cfe349f5c1 | 1033 | } else if(DZ_case == 0 && DZ_NUM ==1) { |
Lightvalve | 237:c2cfe349f5c1 | 1034 | if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 237:c2cfe349f5c1 | 1035 | Vout.ref = (float) P_GAIN_JOINT_POSITION * (0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen)*(float) 50.0f; |
Lightvalve | 237:c2cfe349f5c1 | 1036 | } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 237:c2cfe349f5c1 | 1037 | START_POS = pos.sen; |
Lightvalve | 237:c2cfe349f5c1 | 1038 | } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 238:4e660aa77eb7 | 1039 | valve_pos_raw.ref = Ref_Valve_Pos_Old - DZ_DIRECTION * 64 / DZ_index; |
Lightvalve | 238:4e660aa77eb7 | 1040 | if(valve_pos_raw.ref <= VALVE_MIN_POS) { |
Lightvalve | 238:4e660aa77eb7 | 1041 | valve_pos_raw.ref = VALVE_MIN_POS; |
Lightvalve | 238:4e660aa77eb7 | 1042 | } else if(valve_pos_raw.ref >= VALVE_MAX_POS) { |
Lightvalve | 238:4e660aa77eb7 | 1043 | valve_pos_raw.ref = VALVE_MAX_POS; |
Lightvalve | 237:c2cfe349f5c1 | 1044 | } |
Lightvalve | 238:4e660aa77eb7 | 1045 | VALVE_POS_CONTROL(valve_pos_raw.ref); |
Lightvalve | 237:c2cfe349f5c1 | 1046 | |
Lightvalve | 237:c2cfe349f5c1 | 1047 | } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 238:4e660aa77eb7 | 1048 | Ref_Valve_Pos_Old = valve_pos_raw.ref; |
Lightvalve | 237:c2cfe349f5c1 | 1049 | FINAL_POS = pos.sen; |
Lightvalve | 237:c2cfe349f5c1 | 1050 | |
Lightvalve | 238:4e660aa77eb7 | 1051 | if((FINAL_POS - START_POS)>1) { |
Lightvalve | 237:c2cfe349f5c1 | 1052 | DZ_DIRECTION = 1; |
Lightvalve | 238:4e660aa77eb7 | 1053 | } else if((FINAL_POS - START_POS)<-1) { |
Lightvalve | 237:c2cfe349f5c1 | 1054 | DZ_DIRECTION = -1; |
Lightvalve | 237:c2cfe349f5c1 | 1055 | } else { |
Lightvalve | 237:c2cfe349f5c1 | 1056 | DZ_DIRECTION = 1; |
Lightvalve | 237:c2cfe349f5c1 | 1057 | } |
Lightvalve | 237:c2cfe349f5c1 | 1058 | VALVE_DZ_timer = 0; |
Lightvalve | 237:c2cfe349f5c1 | 1059 | DZ_index= DZ_index *2; |
Lightvalve | 237:c2cfe349f5c1 | 1060 | if(DZ_index >= 128) { |
Lightvalve | 238:4e660aa77eb7 | 1061 | FIRST_DZ = valve_pos_raw.ref; |
Lightvalve | 237:c2cfe349f5c1 | 1062 | DZ_NUM = 2; |
Lightvalve | 237:c2cfe349f5c1 | 1063 | Ref_Valve_Pos_Old = FIRST_DZ; |
Lightvalve | 237:c2cfe349f5c1 | 1064 | DZ_index = 1; |
Lightvalve | 237:c2cfe349f5c1 | 1065 | DZ_DIRECTION = 1; |
Lightvalve | 237:c2cfe349f5c1 | 1066 | } |
Lightvalve | 237:c2cfe349f5c1 | 1067 | } |
Lightvalve | 237:c2cfe349f5c1 | 1068 | } else { |
Lightvalve | 237:c2cfe349f5c1 | 1069 | if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 237:c2cfe349f5c1 | 1070 | Vout.ref = (float) P_GAIN_JOINT_POSITION * (0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen)*(float) 50.0f; |
Lightvalve | 237:c2cfe349f5c1 | 1071 | } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 237:c2cfe349f5c1 | 1072 | START_POS = pos.sen; |
Lightvalve | 237:c2cfe349f5c1 | 1073 | } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 238:4e660aa77eb7 | 1074 | valve_pos_raw.ref = Ref_Valve_Pos_Old + DZ_DIRECTION * 64 / DZ_index; |
Lightvalve | 238:4e660aa77eb7 | 1075 | if(valve_pos_raw.ref <= VALVE_MIN_POS) { |
Lightvalve | 238:4e660aa77eb7 | 1076 | valve_pos_raw.ref = VALVE_MIN_POS; |
Lightvalve | 238:4e660aa77eb7 | 1077 | } else if(valve_pos_raw.ref > VALVE_MAX_POS) { |
Lightvalve | 238:4e660aa77eb7 | 1078 | valve_pos_raw.ref = VALVE_MAX_POS; |
Lightvalve | 237:c2cfe349f5c1 | 1079 | } |
Lightvalve | 238:4e660aa77eb7 | 1080 | VALVE_POS_CONTROL(valve_pos_raw.ref); |
Lightvalve | 237:c2cfe349f5c1 | 1081 | |
Lightvalve | 237:c2cfe349f5c1 | 1082 | } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 237:c2cfe349f5c1 | 1083 | Vout.ref = 0.0f; |
Lightvalve | 237:c2cfe349f5c1 | 1084 | } else if(VALVE_DZ_timer > (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 238:4e660aa77eb7 | 1085 | Ref_Valve_Pos_Old = valve_pos_raw.ref; |
Lightvalve | 237:c2cfe349f5c1 | 1086 | FINAL_POS = pos.sen; |
Lightvalve | 237:c2cfe349f5c1 | 1087 | |
Lightvalve | 238:4e660aa77eb7 | 1088 | if((FINAL_POS - START_POS)>1) { |
Lightvalve | 237:c2cfe349f5c1 | 1089 | DZ_DIRECTION = -1; |
Lightvalve | 238:4e660aa77eb7 | 1090 | } else if((FINAL_POS - START_POS)<-1) { |
Lightvalve | 237:c2cfe349f5c1 | 1091 | DZ_DIRECTION = 1; |
Lightvalve | 237:c2cfe349f5c1 | 1092 | } else { |
Lightvalve | 237:c2cfe349f5c1 | 1093 | DZ_DIRECTION = 1; |
Lightvalve | 237:c2cfe349f5c1 | 1094 | } |
Lightvalve | 237:c2cfe349f5c1 | 1095 | |
Lightvalve | 237:c2cfe349f5c1 | 1096 | VALVE_DZ_timer = 0; |
Lightvalve | 237:c2cfe349f5c1 | 1097 | DZ_index= DZ_index *2; |
Lightvalve | 237:c2cfe349f5c1 | 1098 | if(DZ_index >= 128) { |
Lightvalve | 238:4e660aa77eb7 | 1099 | SECOND_DZ = valve_pos_raw.ref; |
Lightvalve | 237:c2cfe349f5c1 | 1100 | VALVE_CENTER = (int) (0.5f * (float) (FIRST_DZ) + 0.5f * (float) (SECOND_DZ)); |
Lightvalve | 237:c2cfe349f5c1 | 1101 | first_check = 0; |
Lightvalve | 237:c2cfe349f5c1 | 1102 | VALVE_DEADZONE_MINUS = (float) FIRST_DZ; |
Lightvalve | 237:c2cfe349f5c1 | 1103 | VALVE_DEADZONE_PLUS = (float) SECOND_DZ; |
Lightvalve | 237:c2cfe349f5c1 | 1104 | |
Lightvalve | 237:c2cfe349f5c1 | 1105 | spi_eeprom_write(RID_VALVE_CNETER, (int16_t) VALVE_CENTER); |
Lightvalve | 237:c2cfe349f5c1 | 1106 | spi_eeprom_write(RID_VALVE_DEADZONE_PLUS, (int16_t) VALVE_DEADZONE_PLUS); |
Lightvalve | 237:c2cfe349f5c1 | 1107 | spi_eeprom_write(RID_VALVE_DEADZONE_MINUS, (int16_t) VALVE_DEADZONE_MINUS); |
Lightvalve | 237:c2cfe349f5c1 | 1108 | |
Lightvalve | 237:c2cfe349f5c1 | 1109 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 237:c2cfe349f5c1 | 1110 | DZ_index = 1; |
Lightvalve | 237:c2cfe349f5c1 | 1111 | } |
Lightvalve | 237:c2cfe349f5c1 | 1112 | } |
Lightvalve | 237:c2cfe349f5c1 | 1113 | } |
Lightvalve | 237:c2cfe349f5c1 | 1114 | } |
Lightvalve | 237:c2cfe349f5c1 | 1115 | break; |
Lightvalve | 237:c2cfe349f5c1 | 1116 | } |
Lightvalve | 238:4e660aa77eb7 | 1117 | |
Lightvalve | 237:c2cfe349f5c1 | 1118 | case MODE_STEP_TEST: { |
Lightvalve | 237:c2cfe349f5c1 | 1119 | float valve_pos_ref = 0.0f; |
Lightvalve | 237:c2cfe349f5c1 | 1120 | if (cnt_step_test < (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 237:c2cfe349f5c1 | 1121 | valve_pos_ref = 0.0f; |
Lightvalve | 237:c2cfe349f5c1 | 1122 | } else { |
Lightvalve | 237:c2cfe349f5c1 | 1123 | valve_pos_ref = 10000.0f; |
Lightvalve | 237:c2cfe349f5c1 | 1124 | } |
Lightvalve | 237:c2cfe349f5c1 | 1125 | if(valve_pos_ref >= 0) { |
Lightvalve | 244:30896263bd8b | 1126 | valve_pos_raw.ref = (float)VALVE_ELECTRIC_CENTER + (float)valve_pos_ref * ((float)VALVE_MAX_POS-(float)VALVE_ELECTRIC_CENTER)/10000.0f; |
Lightvalve | 237:c2cfe349f5c1 | 1127 | } else { |
Lightvalve | 244:30896263bd8b | 1128 | valve_pos_raw.ref = (float)VALVE_ELECTRIC_CENTER - (float)valve_pos_ref * ((float)VALVE_MIN_POS-(float)VALVE_ELECTRIC_CENTER)/10000.0f; |
Lightvalve | 237:c2cfe349f5c1 | 1129 | } |
Lightvalve | 241:fb5b57e3f157 | 1130 | |
Lightvalve | 238:4e660aa77eb7 | 1131 | VALVE_POS_CONTROL(valve_pos_raw.ref); |
Lightvalve | 241:fb5b57e3f157 | 1132 | |
Lightvalve | 237:c2cfe349f5c1 | 1133 | ref_array[cnt_step_test] = valve_pos_ref; |
Lightvalve | 244:30896263bd8b | 1134 | if(valve_pos.sen >= (float) VALVE_ELECTRIC_CENTER) { |
Lightvalve | 244:30896263bd8b | 1135 | pos_array[cnt_step_test] = 10000.0f*((float)valve_pos.sen - (float)VALVE_ELECTRIC_CENTER)/((float)VALVE_MAX_POS - (float)VALVE_ELECTRIC_CENTER); |
Lightvalve | 237:c2cfe349f5c1 | 1136 | } else { |
Lightvalve | 244:30896263bd8b | 1137 | pos_array[cnt_step_test] = -10000.0f*((float)valve_pos.sen - (float)VALVE_ELECTRIC_CENTER)/((float)VALVE_MIN_POS - (float)VALVE_ELECTRIC_CENTER); |
Lightvalve | 237:c2cfe349f5c1 | 1138 | } |
Lightvalve | 237:c2cfe349f5c1 | 1139 | |
Lightvalve | 238:4e660aa77eb7 | 1140 | CONTROL_MODE = MODE_VALVE_OPEN_LOOP; |
Lightvalve | 237:c2cfe349f5c1 | 1141 | cnt_step_test++; |
Lightvalve | 237:c2cfe349f5c1 | 1142 | if (cnt_step_test > (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 237:c2cfe349f5c1 | 1143 | buffer_data_size = cnt_step_test; |
Lightvalve | 237:c2cfe349f5c1 | 1144 | cnt_step_test = 0; |
Lightvalve | 237:c2cfe349f5c1 | 1145 | cnt_send_buffer = 0; |
Lightvalve | 237:c2cfe349f5c1 | 1146 | CONTROL_UTILITY_MODE = MODE_SEND_OVER; |
Lightvalve | 237:c2cfe349f5c1 | 1147 | CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 237:c2cfe349f5c1 | 1148 | } |
Lightvalve | 237:c2cfe349f5c1 | 1149 | |
Lightvalve | 237:c2cfe349f5c1 | 1150 | break; |
Lightvalve | 237:c2cfe349f5c1 | 1151 | } |
Lightvalve | 237:c2cfe349f5c1 | 1152 | case MODE_SEND_OVER: { |
Lightvalve | 237:c2cfe349f5c1 | 1153 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 237:c2cfe349f5c1 | 1154 | CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 237:c2cfe349f5c1 | 1155 | break; |
Lightvalve | 237:c2cfe349f5c1 | 1156 | } |
Lightvalve | 238:4e660aa77eb7 | 1157 | |
Lightvalve | 237:c2cfe349f5c1 | 1158 | case MODE_FREQ_TEST: { |
Lightvalve | 241:fb5b57e3f157 | 1159 | float valve_pos_ref = 2500.0f * sin(2.0f * 3.141592f * freq_test_valve_ref * (float) cnt_freq_test * DT_TMR5); |
Lightvalve | 237:c2cfe349f5c1 | 1160 | if(valve_pos_ref >= 0) { |
Lightvalve | 244:30896263bd8b | 1161 | valve_pos_raw.ref = (float)VALVE_ELECTRIC_CENTER + (float)valve_pos_ref * ((float)VALVE_MAX_POS-(float)VALVE_ELECTRIC_CENTER)/10000.0f; |
Lightvalve | 237:c2cfe349f5c1 | 1162 | } else { |
Lightvalve | 244:30896263bd8b | 1163 | valve_pos_raw.ref = (double)VALVE_ELECTRIC_CENTER - (float)valve_pos_ref * ((float)VALVE_MIN_POS-(float)VALVE_ELECTRIC_CENTER)/10000.0f; |
Lightvalve | 237:c2cfe349f5c1 | 1164 | } |
Lightvalve | 241:fb5b57e3f157 | 1165 | |
Lightvalve | 238:4e660aa77eb7 | 1166 | VALVE_POS_CONTROL(valve_pos_raw.ref); |
Lightvalve | 241:fb5b57e3f157 | 1167 | |
Lightvalve | 237:c2cfe349f5c1 | 1168 | ref_array[cnt_freq_test] = valve_pos_ref; |
Lightvalve | 244:30896263bd8b | 1169 | // if(value>=(float) VALVE_ELECTRIC_CENTER) { |
Lightvalve | 244:30896263bd8b | 1170 | if(valve_pos.sen>=(float) VALVE_ELECTRIC_CENTER) { |
Lightvalve | 244:30896263bd8b | 1171 | // pos_array[cnt_freq_test] = 10000.0f*((float)value - (float)VALVE_ELECTRIC_CENTER)/((float)VALVE_MAX_POS - (float)VALVE_ELECTRIC_CENTER); |
Lightvalve | 244:30896263bd8b | 1172 | pos_array[cnt_freq_test] = 10000.0f*((float)valve_pos.sen - (float)VALVE_ELECTRIC_CENTER)/((float)VALVE_MAX_POS - (float)VALVE_ELECTRIC_CENTER); |
Lightvalve | 237:c2cfe349f5c1 | 1173 | } else { |
Lightvalve | 244:30896263bd8b | 1174 | // pos_array[cnt_freq_test] = -10000.0f*((float)value - (float)VALVE_ELECTRIC_CENTER)/((float)VALVE_MIN_POS - (float)VALVE_ELECTRIC_CENTER); |
Lightvalve | 244:30896263bd8b | 1175 | pos_array[cnt_freq_test] = -10000.0f*((float)valve_pos.sen - (float)VALVE_ELECTRIC_CENTER)/((float)VALVE_MIN_POS - (float)VALVE_ELECTRIC_CENTER); |
Lightvalve | 237:c2cfe349f5c1 | 1176 | } |
Lightvalve | 237:c2cfe349f5c1 | 1177 | |
Lightvalve | 238:4e660aa77eb7 | 1178 | CONTROL_MODE = MODE_VALVE_OPEN_LOOP; |
Lightvalve | 237:c2cfe349f5c1 | 1179 | cnt_freq_test++; |
Lightvalve | 241:fb5b57e3f157 | 1180 | if (freq_test_valve_ref * (float) cnt_freq_test * DT_TMR5 > 2) { |
Lightvalve | 237:c2cfe349f5c1 | 1181 | buffer_data_size = cnt_freq_test; |
Lightvalve | 237:c2cfe349f5c1 | 1182 | cnt_freq_test = 0; |
Lightvalve | 237:c2cfe349f5c1 | 1183 | cnt_send_buffer = 0; |
Lightvalve | 237:c2cfe349f5c1 | 1184 | freq_test_valve_ref = freq_test_valve_ref * 1.05f; |
Lightvalve | 237:c2cfe349f5c1 | 1185 | if (freq_test_valve_ref >= 400) { |
Lightvalve | 237:c2cfe349f5c1 | 1186 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 237:c2cfe349f5c1 | 1187 | CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 237:c2cfe349f5c1 | 1188 | } |
Lightvalve | 237:c2cfe349f5c1 | 1189 | CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 237:c2cfe349f5c1 | 1190 | CONTROL_UTILITY_MODE = MODE_SEND_OVER; |
Lightvalve | 237:c2cfe349f5c1 | 1191 | |
Lightvalve | 237:c2cfe349f5c1 | 1192 | } |
Lightvalve | 237:c2cfe349f5c1 | 1193 | break; |
Lightvalve | 237:c2cfe349f5c1 | 1194 | } |
Lightvalve | 237:c2cfe349f5c1 | 1195 | |
Lightvalve | 57:f4819de54e7a | 1196 | default: |
Lightvalve | 57:f4819de54e7a | 1197 | break; |
Lightvalve | 57:f4819de54e7a | 1198 | } |
Lightvalve | 57:f4819de54e7a | 1199 | |
Lightvalve | 57:f4819de54e7a | 1200 | // CONTROL MODE ------------------------------------------------------------ |
Lightvalve | 57:f4819de54e7a | 1201 | switch (CONTROL_MODE) { |
Lightvalve | 57:f4819de54e7a | 1202 | case MODE_NO_ACT: { |
Lightvalve | 57:f4819de54e7a | 1203 | V_out = 0.0f; |
Lightvalve | 57:f4819de54e7a | 1204 | break; |
Lightvalve | 57:f4819de54e7a | 1205 | } |
Lightvalve | 57:f4819de54e7a | 1206 | |
Lightvalve | 57:f4819de54e7a | 1207 | case MODE_VALVE_POSITION_CONTROL: { |
Lightvalve | 67:c2812cf26c38 | 1208 | if (OPERATING_MODE == 5) { //SW Valve |
Lightvalve | 238:4e660aa77eb7 | 1209 | VALVE_POS_CONTROL_DZ(valve_pos.ref); |
Lightvalve | 59:f308b1656d9c | 1210 | V_out = Vout.ref; |
Lightvalve | 67:c2812cf26c38 | 1211 | } else if (CURRENT_CONTROL_MODE == 0) { //PWM |
Lightvalve | 239:8ac5c6162bc1 | 1212 | I_REF = valve_pos.ref; |
Lightvalve | 169:645207e160ca | 1213 | } else { |
jobuuu | 230:2c3e5ecbe7e1 | 1214 | I_REF = valve_pos.ref * 0.001f; // Unit : pulse >> mA |
jobuuu | 230:2c3e5ecbe7e1 | 1215 | float I_MAX = 10.0f; // Max : 10mA |
jobuuu | 230:2c3e5ecbe7e1 | 1216 | if (I_REF > I_MAX) { |
jobuuu | 230:2c3e5ecbe7e1 | 1217 | I_REF = I_MAX; |
jobuuu | 230:2c3e5ecbe7e1 | 1218 | } else if (I_REF < -I_MAX) { |
jobuuu | 230:2c3e5ecbe7e1 | 1219 | I_REF = -I_MAX; |
jobuuu | 230:2c3e5ecbe7e1 | 1220 | } |
Lightvalve | 57:f4819de54e7a | 1221 | } |
Lightvalve | 57:f4819de54e7a | 1222 | break; |
Lightvalve | 57:f4819de54e7a | 1223 | } |
Lightvalve | 57:f4819de54e7a | 1224 | |
Lightvalve | 235:c831718303c9 | 1225 | case MODE_JOINT_CONTROL: { |
Lightvalve | 57:f4819de54e7a | 1226 | |
jobuuu | 230:2c3e5ecbe7e1 | 1227 | float temp_vel_pos = 0.0f; // desired velocity for position control |
Lightvalve | 235:c831718303c9 | 1228 | float temp_vel_FT = 0.0f; // desired velocity for force/torque control |
Lightvalve | 235:c831718303c9 | 1229 | float temp_vel_ff = 0.0f; // desired velocity for feedforward control |
jobuuu | 230:2c3e5ecbe7e1 | 1230 | float temp_vel = 0.0f; |
Lightvalve | 58:2eade98630e2 | 1231 | |
jobuuu | 230:2c3e5ecbe7e1 | 1232 | float wn_Pos = 2.0f * PI * 5.0f; // f_cut : 5Hz Position Control |
Lightvalve | 235:c831718303c9 | 1233 | |
jobuuu | 230:2c3e5ecbe7e1 | 1234 | pos.err = pos.ref - pos.sen; // Unit : mm or deg |
jobuuu | 230:2c3e5ecbe7e1 | 1235 | vel.err = vel.ref - vel.sen; // Unit : mm/s or deg/s |
Lightvalve | 235:c831718303c9 | 1236 | |
jobuuu | 230:2c3e5ecbe7e1 | 1237 | // position control command =============================================================================================================================================== |
Lightvalve | 238:4e660aa77eb7 | 1238 | if ((OPERATING_MODE & 0b01) == 0) { // Rotary Mode |
Lightvalve | 235:c831718303c9 | 1239 | temp_vel_pos = 0.1f * (P_GAIN_JOINT_POSITION * wn_Pos * pos.err) * PI / 180.0f; // rad/s |
Lightvalve | 235:c831718303c9 | 1240 | // L when P-gain = 100, f_cut = 10Hz |
jobuuu | 230:2c3e5ecbe7e1 | 1241 | } else { |
Lightvalve | 235:c831718303c9 | 1242 | temp_vel_pos = 0.1f * (P_GAIN_JOINT_POSITION * wn_Pos * pos.err); // mm/s |
Lightvalve | 235:c831718303c9 | 1243 | // L when P-gain = 100, f_cut = 10Hz |
jobuuu | 230:2c3e5ecbe7e1 | 1244 | } |
Lightvalve | 57:f4819de54e7a | 1245 | |
jobuuu | 230:2c3e5ecbe7e1 | 1246 | // torque control command =============================================================================================================================================== |
jobuuu | 230:2c3e5ecbe7e1 | 1247 | float alpha_SpringDamper = 1.0f/(1.0f+TMR_FREQ_5k/(2.0f*PI*30.0f)); |
Lightvalve | 238:4e660aa77eb7 | 1248 | K_LPF = (1.0f-alpha_SpringDamper) * K_LPF + alpha_SpringDamper * K_SPRING; |
Lightvalve | 238:4e660aa77eb7 | 1249 | D_LPF = (1.0f-alpha_SpringDamper) * D_LPF + alpha_SpringDamper * D_DAMPER; |
Lightvalve | 235:c831718303c9 | 1250 | |
Lightvalve | 238:4e660aa77eb7 | 1251 | if ((OPERATING_MODE & 0b01) == 0) { // Rotary Mode |
Lightvalve | 238:4e660aa77eb7 | 1252 | float torq_ref_act = torq.ref + K_LPF * pos.err + D_LPF * vel.err; // unit : Nm |
jobuuu | 230:2c3e5ecbe7e1 | 1253 | torq.err = torq_ref_act - torq.sen; |
jobuuu | 230:2c3e5ecbe7e1 | 1254 | torq.err_int += torq.err/((float)TMR_FREQ_5k); |
Lightvalve | 235:c831718303c9 | 1255 | temp_vel_FT = 0.001f * (P_GAIN_JOINT_TORQUE * torq.err + I_GAIN_JOINT_TORQUE * torq.err_int); // Nm >> rad/s |
Lightvalve | 57:f4819de54e7a | 1256 | } else { |
Lightvalve | 238:4e660aa77eb7 | 1257 | float force_ref_act = force.ref + K_LPF * pos.err + D_LPF * vel.err; // unit : N |
Lightvalve | 239:8ac5c6162bc1 | 1258 | //////////////////////////////////////////////////force_reference_filter//////////////////////////////////////////////////////////////////// |
Lightvalve | 241:fb5b57e3f157 | 1259 | // float alpha_torque_ref = 1.0f/(1.0f+TMR_FREQ_5k/(2.0f*PI*1.0f)); |
Lightvalve | 241:fb5b57e3f157 | 1260 | // force_ref_filter = (1.0f-alpha_torque_ref) * force_ref_filter + alpha_torque_ref * force_ref_act; |
Lightvalve | 239:8ac5c6162bc1 | 1261 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
Lightvalve | 241:fb5b57e3f157 | 1262 | force_ref_act_can = force_ref_act; |
Lightvalve | 241:fb5b57e3f157 | 1263 | force.err = force_ref_act - force.sen; |
jobuuu | 230:2c3e5ecbe7e1 | 1264 | force.err_int += force.err/((float)TMR_FREQ_5k); |
Lightvalve | 235:c831718303c9 | 1265 | temp_vel_FT = 0.001f * (P_GAIN_JOINT_TORQUE * force.err + I_GAIN_JOINT_TORQUE * force.err_int); // N >> mm/s |
jobuuu | 230:2c3e5ecbe7e1 | 1266 | } |
Lightvalve | 235:c831718303c9 | 1267 | |
Lightvalve | 235:c831718303c9 | 1268 | |
Lightvalve | 235:c831718303c9 | 1269 | // velocity feedforward command ======================================================================================================================================== |
Lightvalve | 238:4e660aa77eb7 | 1270 | if ((OPERATING_MODE & 0b01) == 0) { // Rotary Mode |
jobuuu | 230:2c3e5ecbe7e1 | 1271 | temp_vel_ff = 0.01f * (float)VELOCITY_COMP_GAIN * vel.ref * PI / 180.0f; // rad/s |
jobuuu | 230:2c3e5ecbe7e1 | 1272 | } else { |
jobuuu | 230:2c3e5ecbe7e1 | 1273 | temp_vel_ff = 0.01f * (float)VELOCITY_COMP_GAIN * vel.ref; // mm/s |
jobuuu | 230:2c3e5ecbe7e1 | 1274 | } |
Lightvalve | 169:645207e160ca | 1275 | |
Lightvalve | 235:c831718303c9 | 1276 | // command integration ================================================================================================================================================= |
jobuuu | 230:2c3e5ecbe7e1 | 1277 | temp_vel = (1.0f - alpha_trans) * temp_vel_pos + alpha_trans * temp_vel_FT + temp_vel_ff; // Position Control + Torque Control + Velocity Feedforward |
Lightvalve | 237:c2cfe349f5c1 | 1278 | |
Lightvalve | 235:c831718303c9 | 1279 | float Qact = 0.0f; // required flow rate |
Lightvalve | 237:c2cfe349f5c1 | 1280 | float valve_pos_pulse = 0.0f; |
jobuuu | 230:2c3e5ecbe7e1 | 1281 | if( temp_vel > 0.0f ) { |
jobuuu | 230:2c3e5ecbe7e1 | 1282 | Qact = temp_vel * ((float)PISTON_AREA_A * 0.00006f); // mm^3/sec >> LPM |
Lightvalve | 237:c2cfe349f5c1 | 1283 | if (((OPERATING_MODE&0b110)>>1) == 0 || ((OPERATING_MODE&0b110)>>1) == 1) { //Moog Valve or KNR Valve |
Lightvalve | 237:c2cfe349f5c1 | 1284 | I_REF = tanh_inv(Qact/(K_v * sqrt(PRES_SUPPLY * alpha3 / (alpha3 + 1.0f))))/C_d; |
Lightvalve | 238:4e660aa77eb7 | 1285 | // I_REF = Qact*2.0f; |
Lightvalve | 237:c2cfe349f5c1 | 1286 | } else { // SW valve |
Lightvalve | 237:c2cfe349f5c1 | 1287 | valve_pos_pulse = Qact / (C_d * sqrt(PRES_SUPPLY * alpha3 / (alpha3 + 1.0f))); |
Lightvalve | 237:c2cfe349f5c1 | 1288 | } |
jobuuu | 230:2c3e5ecbe7e1 | 1289 | } else { |
jobuuu | 230:2c3e5ecbe7e1 | 1290 | Qact = temp_vel * ((float)PISTON_AREA_B * 0.00006f); // mm^3/sec >> LPM |
Lightvalve | 237:c2cfe349f5c1 | 1291 | if (((OPERATING_MODE&0b110)>>1) == 0 || ((OPERATING_MODE&0b110)>>1) == 1) { //Moog Valve or KNR Valve |
Lightvalve | 237:c2cfe349f5c1 | 1292 | I_REF = tanh_inv(Qact/(K_v * sqrt(PRES_SUPPLY / (alpha3 + 1.0f))))/C_d; |
Lightvalve | 238:4e660aa77eb7 | 1293 | // I_REF = Qact*2.0f; |
Lightvalve | 237:c2cfe349f5c1 | 1294 | } else { // SW valve |
Lightvalve | 237:c2cfe349f5c1 | 1295 | valve_pos_pulse = Qact / (C_d * sqrt(PRES_SUPPLY / (alpha3 + 1.0f))); |
Lightvalve | 237:c2cfe349f5c1 | 1296 | } |
jobuuu | 230:2c3e5ecbe7e1 | 1297 | } |
Lightvalve | 237:c2cfe349f5c1 | 1298 | |
Lightvalve | 237:c2cfe349f5c1 | 1299 | |
Lightvalve | 237:c2cfe349f5c1 | 1300 | |
Lightvalve | 237:c2cfe349f5c1 | 1301 | |
Lightvalve | 237:c2cfe349f5c1 | 1302 | if (((OPERATING_MODE&0b110)>>1) == 0 || ((OPERATING_MODE&0b110)>>1) == 1) { //Moog Valve or KNR Valve |
Lightvalve | 237:c2cfe349f5c1 | 1303 | |
Lightvalve | 237:c2cfe349f5c1 | 1304 | float I_MAX = 10.0f; // Maximum Current : 10mA |
Lightvalve | 237:c2cfe349f5c1 | 1305 | // Anti-windup for FT |
Lightvalve | 237:c2cfe349f5c1 | 1306 | if (I_GAIN_JOINT_TORQUE > 0.001f) { |
Lightvalve | 237:c2cfe349f5c1 | 1307 | float Ka = 2.0f; |
Lightvalve | 237:c2cfe349f5c1 | 1308 | if (I_REF > I_MAX) { |
Lightvalve | 237:c2cfe349f5c1 | 1309 | float I_rem = I_REF - I_MAX; |
Lightvalve | 237:c2cfe349f5c1 | 1310 | I_REF = I_MAX; |
Lightvalve | 237:c2cfe349f5c1 | 1311 | float temp_vel_rem = K_v * sqrt(PRES_SUPPLY * alpha3 / (alpha3 + 1.0f)) * tanh(C_d*I_rem) / ((double) PISTON_AREA_A * 0.00006f); // Unit : mm/s [linear] / rad/s [rotary] |
Lightvalve | 237:c2cfe349f5c1 | 1312 | torq.err_int = torq.err_int - Ka * temp_vel_rem * (10000.0f/I_GAIN_JOINT_TORQUE); |
Lightvalve | 237:c2cfe349f5c1 | 1313 | } else if (I_REF < -I_MAX) { |
Lightvalve | 237:c2cfe349f5c1 | 1314 | double I_rem = I_REF - (-I_MAX); |
Lightvalve | 237:c2cfe349f5c1 | 1315 | I_REF = -I_MAX; |
Lightvalve | 237:c2cfe349f5c1 | 1316 | float temp_vel_rem = K_v * sqrt(PRES_SUPPLY / (alpha3 + 1.0f)) * tanh(C_d*I_rem) / ((double) PISTON_AREA_B * 0.00006f); // Unit : mm/s [linear] / rad/s [rotary] |
Lightvalve | 237:c2cfe349f5c1 | 1317 | torq.err_int = torq.err_int - Ka * temp_vel_rem * (10000.0f/I_GAIN_JOINT_TORQUE); |
Lightvalve | 237:c2cfe349f5c1 | 1318 | } |
Lightvalve | 237:c2cfe349f5c1 | 1319 | } else { |
Lightvalve | 237:c2cfe349f5c1 | 1320 | if(I_REF > I_MAX) { |
Lightvalve | 237:c2cfe349f5c1 | 1321 | I_REF = I_MAX; |
Lightvalve | 237:c2cfe349f5c1 | 1322 | } else if (I_REF < -I_MAX) { |
Lightvalve | 237:c2cfe349f5c1 | 1323 | I_REF = -I_MAX; |
Lightvalve | 237:c2cfe349f5c1 | 1324 | } |
Lightvalve | 57:f4819de54e7a | 1325 | } |
Lightvalve | 237:c2cfe349f5c1 | 1326 | } else { //SW valve |
Lightvalve | 237:c2cfe349f5c1 | 1327 | float Valve_pos_MAX = 10000.0f; // Maximum Valve Pos : 10000 |
Lightvalve | 237:c2cfe349f5c1 | 1328 | // Anti-windup for FT |
Lightvalve | 237:c2cfe349f5c1 | 1329 | if (I_GAIN_JOINT_TORQUE > 0.001f) { |
Lightvalve | 237:c2cfe349f5c1 | 1330 | float Ka = 2.0f; |
Lightvalve | 237:c2cfe349f5c1 | 1331 | if (valve_pos_pulse > Valve_pos_MAX) { |
Lightvalve | 237:c2cfe349f5c1 | 1332 | float valve_pos_rem = valve_pos_pulse - Valve_pos_MAX; |
Lightvalve | 237:c2cfe349f5c1 | 1333 | valve_pos_pulse = Valve_pos_MAX; |
Lightvalve | 237:c2cfe349f5c1 | 1334 | float temp_vel_rem = C_d * valve_pos_rem * sqrt(PRES_SUPPLY * alpha3 / (alpha3 + 1.0f)) / ((double) PISTON_AREA_A * 0.00006f); // Unit : mm/s [linear] / rad/s [rotary] |
Lightvalve | 237:c2cfe349f5c1 | 1335 | torq.err_int = torq.err_int - Ka * temp_vel_rem * (10000.0f/I_GAIN_JOINT_TORQUE); |
Lightvalve | 237:c2cfe349f5c1 | 1336 | } else if (valve_pos_pulse < -Valve_pos_MAX) { |
Lightvalve | 237:c2cfe349f5c1 | 1337 | double valve_pos_rem = valve_pos_pulse - (-Valve_pos_MAX); |
Lightvalve | 237:c2cfe349f5c1 | 1338 | valve_pos_pulse = -Valve_pos_MAX; |
Lightvalve | 237:c2cfe349f5c1 | 1339 | float temp_vel_rem = C_d * valve_pos_rem * sqrt(PRES_SUPPLY / (alpha3 + 1.0f)) / ((double) PISTON_AREA_B * 0.00006f); // Unit : mm/s [linear] / rad/s [rotary] |
Lightvalve | 237:c2cfe349f5c1 | 1340 | torq.err_int = torq.err_int - Ka * temp_vel_rem * (10000.0f/I_GAIN_JOINT_TORQUE); |
Lightvalve | 237:c2cfe349f5c1 | 1341 | } |
Lightvalve | 237:c2cfe349f5c1 | 1342 | } else { |
Lightvalve | 237:c2cfe349f5c1 | 1343 | if(valve_pos_pulse > 10000.0f) { |
Lightvalve | 237:c2cfe349f5c1 | 1344 | valve_pos_pulse = 10000.0f; |
Lightvalve | 237:c2cfe349f5c1 | 1345 | } else if(valve_pos_pulse < -10000.0f) { |
Lightvalve | 237:c2cfe349f5c1 | 1346 | valve_pos_pulse = -10000.0f; |
Lightvalve | 237:c2cfe349f5c1 | 1347 | } |
Lightvalve | 235:c831718303c9 | 1348 | } |
Lightvalve | 238:4e660aa77eb7 | 1349 | // if (valve_pos_pulse >= 0) { |
Lightvalve | 238:4e660aa77eb7 | 1350 | // valve_pos.ref = valve_pos_pulse/10000.0f * (VALVE_MAX_POS-VALVE_DEADZONE_PLUS) + VALVE_DEADZONE_PLUS; |
Lightvalve | 238:4e660aa77eb7 | 1351 | // } else { |
Lightvalve | 238:4e660aa77eb7 | 1352 | // valve_pos.ref = -valve_pos_pulse/10000.0f * (VALVE_MIN_POS-VALVE_DEADZONE_MINUS) + VALVE_DEADZONE_MINUS; |
Lightvalve | 238:4e660aa77eb7 | 1353 | // } |
Lightvalve | 238:4e660aa77eb7 | 1354 | VALVE_POS_CONTROL_DZ(valve_pos_pulse); |
Lightvalve | 237:c2cfe349f5c1 | 1355 | V_out = Vout.ref; |
Lightvalve | 57:f4819de54e7a | 1356 | } |
Lightvalve | 57:f4819de54e7a | 1357 | break; |
Lightvalve | 57:f4819de54e7a | 1358 | } |
Lightvalve | 58:2eade98630e2 | 1359 | |
Lightvalve | 57:f4819de54e7a | 1360 | case MODE_VALVE_OPEN_LOOP: { |
Lightvalve | 57:f4819de54e7a | 1361 | V_out = (float) Vout.ref; |
Lightvalve | 57:f4819de54e7a | 1362 | break; |
Lightvalve | 57:f4819de54e7a | 1363 | } |
Lightvalve | 169:645207e160ca | 1364 | |
Lightvalve | 12:6f2531038ea4 | 1365 | default: |
Lightvalve | 12:6f2531038ea4 | 1366 | break; |
Lightvalve | 12:6f2531038ea4 | 1367 | } |
Lightvalve | 14:8e7590227d22 | 1368 | |
Lightvalve | 57:f4819de54e7a | 1369 | |
Lightvalve | 57:f4819de54e7a | 1370 | if (((OPERATING_MODE&0b110)>>1) == 0 || ((OPERATING_MODE&0b110)>>1) == 1) { //Moog Valve or KNR Valve |
Lightvalve | 58:2eade98630e2 | 1371 | |
Lightvalve | 57:f4819de54e7a | 1372 | //////////////////////////////////////////////////////////////////////////// |
Lightvalve | 57:f4819de54e7a | 1373 | //////////////////////////// CURRENT CONTROL ////////////////////////////// |
Lightvalve | 57:f4819de54e7a | 1374 | //////////////////////////////////////////////////////////////////////////// |
Lightvalve | 57:f4819de54e7a | 1375 | if (CURRENT_CONTROL_MODE) { |
Lightvalve | 67:c2812cf26c38 | 1376 | double alpha_update_Iref = 1.0f / (1.0f + 5000.0f / (2.0f * 3.14f * 300.0f)); // f_cutoff : 500Hz |
Lightvalve | 57:f4819de54e7a | 1377 | I_REF_fil = (1.0f - alpha_update_Iref) * I_REF_fil + alpha_update_Iref*I_REF; |
Lightvalve | 57:f4819de54e7a | 1378 | |
jobuuu | 230:2c3e5ecbe7e1 | 1379 | if (I_REF_fil > 0.0f) I_REF_fil_DZ = I_REF_fil + (double)VALVE_DEADZONE_PLUS*mA_PER_pulse; // unit: mA |
jobuuu | 230:2c3e5ecbe7e1 | 1380 | else if (I_REF_fil < 0.0f) I_REF_fil_DZ = I_REF_fil + (double)VALVE_DEADZONE_MINUS*mA_PER_pulse; // unit: mA |
jobuuu | 230:2c3e5ecbe7e1 | 1381 | else I_REF_fil_DZ = I_REF_fil + (double)(VALVE_DEADZONE_PLUS+VALVE_DEADZONE_MINUS)/2.0f*mA_PER_pulse; // unit: mA |
Lightvalve | 235:c831718303c9 | 1382 | |
hyhoon | 226:82a3ca333004 | 1383 | I_ERR = I_REF_fil_DZ - (double)cur.sen; |
Lightvalve | 67:c2812cf26c38 | 1384 | I_ERR_INT = I_ERR_INT + (I_ERR) * 0.0002f; |
Lightvalve | 57:f4819de54e7a | 1385 | |
Lightvalve | 57:f4819de54e7a | 1386 | // Moog Valve Current Control Gain |
Lightvalve | 67:c2812cf26c38 | 1387 | double R_model = 500.0f; // ohm |
Lightvalve | 57:f4819de54e7a | 1388 | double L_model = 1.2f; |
Lightvalve | 239:8ac5c6162bc1 | 1389 | double w0 = 2.0f * 3.14f * 100.0f; |
Lightvalve | 57:f4819de54e7a | 1390 | double KP_I = 0.1f * L_model*w0; |
Lightvalve | 57:f4819de54e7a | 1391 | double KI_I = 0.1f * R_model*w0; |
Lightvalve | 57:f4819de54e7a | 1392 | |
Lightvalve | 57:f4819de54e7a | 1393 | // KNR Valve Current Control Gain |
Lightvalve | 57:f4819de54e7a | 1394 | if (((OPERATING_MODE & 0b110)>>1) == 1) { // KNR Valve |
Lightvalve | 57:f4819de54e7a | 1395 | R_model = 163.0f; // ohm |
Lightvalve | 57:f4819de54e7a | 1396 | L_model = 1.0f; |
Lightvalve | 57:f4819de54e7a | 1397 | w0 = 2.0f * 3.14f * 80.0f; |
Lightvalve | 57:f4819de54e7a | 1398 | KP_I = 1.0f * L_model*w0; |
Lightvalve | 57:f4819de54e7a | 1399 | KI_I = 0.08f * R_model*w0; |
Lightvalve | 57:f4819de54e7a | 1400 | } |
Lightvalve | 57:f4819de54e7a | 1401 | |
Lightvalve | 57:f4819de54e7a | 1402 | double FF_gain = 1.0f; |
Lightvalve | 67:c2812cf26c38 | 1403 | VALVE_PWM_RAW = KP_I * 2.0f * I_ERR + KI_I * 2.0f* I_ERR_INT; |
hyhoon | 226:82a3ca333004 | 1404 | I_REF_fil_diff = I_REF_fil_DZ - I_REF_fil_old; |
hyhoon | 226:82a3ca333004 | 1405 | I_REF_fil_old = I_REF_fil_DZ; |
Lightvalve | 67:c2812cf26c38 | 1406 | // VALVE_PWM_RAW = VALVE_PWM_RAW + FF_gain * (R_model * I_REF_fil + L_model * I_REF_fil_diff * 5000.0f); // Unit : mV |
hyhoon | 226:82a3ca333004 | 1407 | VALVE_PWM_RAW = VALVE_PWM_RAW + FF_gain * (R_model * I_REF_fil_DZ); // Unit : mV |
Lightvalve | 238:4e660aa77eb7 | 1408 | double V_MAX = VALVE_VOLTAGE_LIMIT*1000.0f; // Maximum Voltage : 12V = 12000mV |
Lightvalve | 57:f4819de54e7a | 1409 | |
Lightvalve | 57:f4819de54e7a | 1410 | double Ka = 3.0f / KP_I; |
Lightvalve | 57:f4819de54e7a | 1411 | if (VALVE_PWM_RAW > V_MAX) { |
Lightvalve | 57:f4819de54e7a | 1412 | V_rem = VALVE_PWM_RAW - V_MAX; |
Lightvalve | 57:f4819de54e7a | 1413 | V_rem = Ka*V_rem; |
Lightvalve | 57:f4819de54e7a | 1414 | VALVE_PWM_RAW = V_MAX; |
Lightvalve | 234:bd00f763acb1 | 1415 | // I_ERR_INT = I_ERR_INT - V_rem * 0.0002f; |
Lightvalve | 234:bd00f763acb1 | 1416 | I_ERR_INT = I_ERR_INT - V_rem; |
Lightvalve | 57:f4819de54e7a | 1417 | } else if (VALVE_PWM_RAW < -V_MAX) { |
Lightvalve | 57:f4819de54e7a | 1418 | V_rem = VALVE_PWM_RAW - (-V_MAX); |
Lightvalve | 57:f4819de54e7a | 1419 | V_rem = Ka*V_rem; |
Lightvalve | 57:f4819de54e7a | 1420 | VALVE_PWM_RAW = -V_MAX; |
Lightvalve | 234:bd00f763acb1 | 1421 | // I_ERR_INT = I_ERR_INT - V_rem * 0.0002f; |
Lightvalve | 234:bd00f763acb1 | 1422 | I_ERR_INT = I_ERR_INT - V_rem; |
Lightvalve | 57:f4819de54e7a | 1423 | } |
Lightvalve | 57:f4819de54e7a | 1424 | } else { |
Lightvalve | 57:f4819de54e7a | 1425 | VALVE_PWM_RAW = I_REF * mV_PER_mA; |
Lightvalve | 57:f4819de54e7a | 1426 | } |
Lightvalve | 57:f4819de54e7a | 1427 | |
Lightvalve | 57:f4819de54e7a | 1428 | //////////////////////////////////////////////////////////////////////////// |
Lightvalve | 57:f4819de54e7a | 1429 | ///////////////// Dead Zone Cancellation & Linearization ////////////////// |
Lightvalve | 57:f4819de54e7a | 1430 | //////////////////////////////////////////////////////////////////////////// |
Lightvalve | 57:f4819de54e7a | 1431 | |
Lightvalve | 57:f4819de54e7a | 1432 | // Output Voltage Linearization |
hyhoon | 226:82a3ca333004 | 1433 | double CUR_PWM_nonlin = (double)VALVE_PWM_RAW; // Unit : mV |
Lightvalve | 67:c2812cf26c38 | 1434 | double CUR_PWM_lin = PWM_duty_byLT(CUR_PWM_nonlin); // -8000~8000 |
Lightvalve | 57:f4819de54e7a | 1435 | |
Lightvalve | 57:f4819de54e7a | 1436 | // Dead Zone Cancellation (Electrical dead-zone) |
Lightvalve | 67:c2812cf26c38 | 1437 | if (CUR_PWM_lin > 0) V_out = (float) (CUR_PWM_lin + 169.0f); |
Lightvalve | 67:c2812cf26c38 | 1438 | else if (CUR_PWM_lin < 0) V_out = (float) (CUR_PWM_lin - 174.0f); |
Lightvalve | 67:c2812cf26c38 | 1439 | else V_out = (float) (CUR_PWM_lin); |
Lightvalve | 235:c831718303c9 | 1440 | |
Lightvalve | 169:645207e160ca | 1441 | } else { //////////////////////////sw valve |
Lightvalve | 224:985dba42f261 | 1442 | // Output Voltage Linearization & Dead Zone Cancellation (Electrical dead-zone) by SW |
Lightvalve | 135:79885a39c161 | 1443 | if (V_out > 0 ) V_out = (V_out + 180.0f)/0.8588f; |
Lightvalve | 135:79885a39c161 | 1444 | else if (V_out < 0) V_out = (V_out - 200.0f)/0.8651f; |
Lightvalve | 135:79885a39c161 | 1445 | else V_out = 0.0f; |
Lightvalve | 89:a7b45368ea0f | 1446 | } |
Lightvalve | 169:645207e160ca | 1447 | |
jobuuu | 230:2c3e5ecbe7e1 | 1448 | //////////////////////////////////////////////////////////////////// |
jobuuu | 230:2c3e5ecbe7e1 | 1449 | /////////////////// PWM Command /////////////////////////////////// |
jobuuu | 230:2c3e5ecbe7e1 | 1450 | //////////////////////////////////////////////////////////////////// |
Lightvalve | 169:645207e160ca | 1451 | if(DIR_VALVE<0) { |
Lightvalve | 67:c2812cf26c38 | 1452 | V_out = -V_out; |
Lightvalve | 67:c2812cf26c38 | 1453 | } |
Lightvalve | 169:645207e160ca | 1454 | |
Lightvalve | 49:e7bcfc244d40 | 1455 | if (V_out >= VALVE_VOLTAGE_LIMIT*1000.0f) { |
Lightvalve | 49:e7bcfc244d40 | 1456 | V_out = VALVE_VOLTAGE_LIMIT*1000.0f; |
Lightvalve | 49:e7bcfc244d40 | 1457 | } else if(V_out<=-VALVE_VOLTAGE_LIMIT*1000.0f) { |
Lightvalve | 49:e7bcfc244d40 | 1458 | V_out = -VALVE_VOLTAGE_LIMIT*1000.0f; |
Lightvalve | 27:a2254a485f23 | 1459 | } |
Lightvalve | 224:985dba42f261 | 1460 | PWM_out= V_out/(SUPPLY_VOLTAGE*1000.0f); |
Lightvalve | 13:747daba9cf59 | 1461 | |
Lightvalve | 224:985dba42f261 | 1462 | // Saturation of output voltage |
Lightvalve | 30:8d561f16383b | 1463 | if(PWM_out > 1.0f) PWM_out=1.0f; |
Lightvalve | 30:8d561f16383b | 1464 | else if (PWM_out < -1.0f) PWM_out=-1.0f; |
Lightvalve | 13:747daba9cf59 | 1465 | |
Lightvalve | 30:8d561f16383b | 1466 | if (PWM_out>0.0f) { |
Lightvalve | 30:8d561f16383b | 1467 | dtc_v=0.0f; |
jobuuu | 1:e04e563be5ce | 1468 | dtc_w=PWM_out; |
jobuuu | 1:e04e563be5ce | 1469 | } else { |
jobuuu | 2:a1c0a37df760 | 1470 | dtc_v=-PWM_out; |
Lightvalve | 30:8d561f16383b | 1471 | dtc_w=0.0f; |
jobuuu | 1:e04e563be5ce | 1472 | } |
Lightvalve | 13:747daba9cf59 | 1473 | |
Lightvalve | 67:c2812cf26c38 | 1474 | |
jobuuu | 230:2c3e5ecbe7e1 | 1475 | //////////////////////////////////////////////////////////////////////////// |
jobuuu | 230:2c3e5ecbe7e1 | 1476 | ////////////////////// Data transmission through CAN ////////////////////// |
Lightvalve | 235:c831718303c9 | 1477 | //////////////////////////////////////////////////////////////////////////// |
Lightvalve | 235:c831718303c9 | 1478 | |
Lightvalve | 243:b235d67d25ba | 1479 | |
Lightvalve | 244:30896263bd8b | 1480 | // if (TMR2_COUNT_CAN_TX % (int) ((int) TMR_FREQ_5k/CAN_FREQ) == 0) { |
Lightvalve | 244:30896263bd8b | 1481 | if (TMR2_COUNT_CAN_TX % (int) ((int) TMR_FREQ_5k/500) == 0) { |
Lightvalve | 235:c831718303c9 | 1482 | |
hyhoon | 226:82a3ca333004 | 1483 | // Position, Velocity, and Torque (ID:1200) |
Lightvalve | 244:30896263bd8b | 1484 | if (flag_data_request[0] == HIGH) { |
Lightvalve | 237:c2cfe349f5c1 | 1485 | |
Lightvalve | 244:30896263bd8b | 1486 | if ((OPERATING_MODE & 0b01) == 0) { // Rotary Actuator |
Lightvalve | 244:30896263bd8b | 1487 | CAN_TX_POSITION_FT((int16_t) (pos.sen*200.0f), (int16_t) (vel.sen*20.0f), (int16_t) (torq.sen*TORQUE_SENSOR_PULSE_PER_TORQUE*10.0f)); |
Lightvalve | 235:c831718303c9 | 1488 | |
Lightvalve | 244:30896263bd8b | 1489 | } else if ((OPERATING_MODE & 0b01) == 1) { // Linear Actuator |
Lightvalve | 244:30896263bd8b | 1490 | CAN_TX_POSITION_FT((int16_t) (pos.sen*200.0f), (int16_t) (vel.sen*20.0f), (int16_t) (force.sen*TORQUE_SENSOR_PULSE_PER_TORQUE*10.0f)); |
Lightvalve | 244:30896263bd8b | 1491 | } |
Lightvalve | 244:30896263bd8b | 1492 | } |
hyhoon | 226:82a3ca333004 | 1493 | |
jobuuu | 230:2c3e5ecbe7e1 | 1494 | // Valve Position (ID:1300) |
Lightvalve | 56:6f50d9d3bfee | 1495 | if (flag_data_request[1] == HIGH) { |
Lightvalve | 244:30896263bd8b | 1496 | if (((OPERATING_MODE&0b110)>>1) == 0 || ((OPERATING_MODE&0b110)>>1) == 1) { //Moog Valve or KNR Valve |
Lightvalve | 244:30896263bd8b | 1497 | CAN_TX_PWM((int16_t)(cur.sen/mA_PER_pulse)); |
Lightvalve | 244:30896263bd8b | 1498 | } else { |
Lightvalve | 244:30896263bd8b | 1499 | CAN_TX_PWM((int16_t)(valve_pos.sen)); |
Lightvalve | 244:30896263bd8b | 1500 | } |
Lightvalve | 54:647072f5307a | 1501 | } |
Lightvalve | 58:2eade98630e2 | 1502 | |
Lightvalve | 244:30896263bd8b | 1503 | // Others : SW (ID:1400) |
Lightvalve | 171:bfc1fd2629d8 | 1504 | if (flag_data_request[2] == HIGH) { |
Lightvalve | 244:30896263bd8b | 1505 | CAN_TX_CURRENT((int16_t) valve_pos.sen, (int16_t) valve_pos.ref); |
Lightvalve | 54:647072f5307a | 1506 | } |
Lightvalve | 20:806196fda269 | 1507 | |
Lightvalve | 54:647072f5307a | 1508 | TMR2_COUNT_CAN_TX = 0; |
Lightvalve | 54:647072f5307a | 1509 | } |
Lightvalve | 54:647072f5307a | 1510 | TMR2_COUNT_CAN_TX++; |
Lightvalve | 52:8ea76864368a | 1511 | |
Lightvalve | 20:806196fda269 | 1512 | } |
Lightvalve | 241:fb5b57e3f157 | 1513 | TIM2->SR = 0x0; // reset the status register |
Lightvalve | 244:30896263bd8b | 1514 | } |