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