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