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