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