![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Original Version of STM Board
main.cpp@227:699c3e572283, 2021-03-11 (annotated)
- Committer:
- hyhoon
- Date:
- Thu Mar 11 07:27:23 2021 +0000
- Revision:
- 227:699c3e572283
- Parent:
- 226:82a3ca333004
- Child:
- 228:763bbefcb79f
210311
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 |
Lightvalve | 224:985dba42f261 | 4 | |
Lightvalve | 224:985dba42f261 | 5 | |
Lightvalve | 224:985dba42f261 | 6 | // 유의사항 |
Lightvalve | 224:985dba42f261 | 7 | // 소수 적을때 뒤에 f 꼭 붙이기 |
Lightvalve | 224:985dba42f261 | 8 | // CAN 선은 ground까지 있는 3상 선으로 써야함. |
Lightvalve | 224:985dba42f261 | 9 | // 전원은 12~24V 인가. |
Lightvalve | 224:985dba42f261 | 10 | |
GiJeongKim | 0:51c43836c1d7 | 11 | #include "mbed.h" |
GiJeongKim | 0:51c43836c1d7 | 12 | #include "FastPWM.h" |
GiJeongKim | 0:51c43836c1d7 | 13 | #include "INIT_HW.h" |
jobuuu | 2:a1c0a37df760 | 14 | #include "function_CAN.h" |
GiJeongKim | 0:51c43836c1d7 | 15 | #include "SPI_EEP_ENC.h" |
GiJeongKim | 0:51c43836c1d7 | 16 | #include "I2C_AS5510.h" |
GiJeongKim | 0:51c43836c1d7 | 17 | #include "setting.h" |
Lightvalve | 11:82d8768d7351 | 18 | #include "function_utilities.h" |
Lightvalve | 16:903b5a4433b4 | 19 | #include "stm32f4xx_flash.h" |
Lightvalve | 169:645207e160ca | 20 | #include "FlashWriter.h" |
Lightvalve | 170:42c938a40313 | 21 | #include <string> |
Lightvalve | 170:42c938a40313 | 22 | #include <iostream> |
Lightvalve | 179:d5377766d7ea | 23 | #include <cmath> |
Lightvalve | 61:bc8c8270f0ab | 24 | |
Lightvalve | 61:bc8c8270f0ab | 25 | using namespace std; |
Lightvalve | 61:bc8c8270f0ab | 26 | Timer t; |
GiJeongKim | 0:51c43836c1d7 | 27 | |
jobuuu | 7:e9086c72bb22 | 28 | // dac & check /////////////////////////////////////////// |
GiJeongKim | 0:51c43836c1d7 | 29 | DigitalOut check(PC_2); |
GiJeongKim | 0:51c43836c1d7 | 30 | DigitalOut check_2(PC_3); |
GiJeongKim | 0:51c43836c1d7 | 31 | AnalogOut dac_1(PA_4); |
GiJeongKim | 0:51c43836c1d7 | 32 | AnalogOut dac_2(PA_5); |
Lightvalve | 14:8e7590227d22 | 33 | AnalogIn adc1(PC_4); //pressure_1 |
Lightvalve | 14:8e7590227d22 | 34 | AnalogIn adc2(PB_0); //pressure_2 |
Lightvalve | 14:8e7590227d22 | 35 | AnalogIn adc3(PC_1); //current |
GiJeongKim | 0:51c43836c1d7 | 36 | |
jobuuu | 7:e9086c72bb22 | 37 | // PWM /////////////////////////////////////////// |
Lightvalve | 30:8d561f16383b | 38 | float dtc_v=0.0f; |
Lightvalve | 30:8d561f16383b | 39 | float dtc_w=0.0f; |
GiJeongKim | 0:51c43836c1d7 | 40 | |
jobuuu | 7:e9086c72bb22 | 41 | // I2C /////////////////////////////////////////// |
GiJeongKim | 0:51c43836c1d7 | 42 | I2C i2c(PC_9,PA_8); // SDA, SCL (for K22F) |
Lightvalve | 224:985dba42f261 | 43 | const int i2c_slave_addr1 = 0x56; // AS5510 address |
GiJeongKim | 0:51c43836c1d7 | 44 | unsigned int value; // 10bit output of reading sensor AS5510 |
GiJeongKim | 0:51c43836c1d7 | 45 | |
jobuuu | 7:e9086c72bb22 | 46 | // SPI /////////////////////////////////////////// |
Lightvalve | 170:42c938a40313 | 47 | SPI eeprom(PB_15, PB_14, PB_13); // EEPROM //(SPI_MOSI, SPI_MISO, SPI_SCK); |
Lightvalve | 170:42c938a40313 | 48 | DigitalOut eeprom_cs(PB_12); |
GiJeongKim | 0:51c43836c1d7 | 49 | SPI enc(PC_12,PC_11,PC_10); |
GiJeongKim | 0:51c43836c1d7 | 50 | DigitalOut enc_cs(PD_2); |
Lightvalve | 11:82d8768d7351 | 51 | DigitalOut LED(PA_15); |
GiJeongKim | 0:51c43836c1d7 | 52 | |
jobuuu | 7:e9086c72bb22 | 53 | // UART /////////////////////////////////////////// |
GiJeongKim | 0:51c43836c1d7 | 54 | Serial pc(PA_9,PA_10); // _ UART |
GiJeongKim | 0:51c43836c1d7 | 55 | |
jobuuu | 7:e9086c72bb22 | 56 | // CAN /////////////////////////////////////////// |
GiJeongKim | 0:51c43836c1d7 | 57 | CAN can(PB_8, PB_9, 1000000); |
GiJeongKim | 0:51c43836c1d7 | 58 | CANMessage msg; |
Lightvalve | 11:82d8768d7351 | 59 | void onMsgReceived() |
Lightvalve | 11:82d8768d7351 | 60 | { |
Lightvalve | 11:82d8768d7351 | 61 | CAN_RX_HANDLER(); |
Lightvalve | 11:82d8768d7351 | 62 | } |
jobuuu | 2:a1c0a37df760 | 63 | |
jobuuu | 7:e9086c72bb22 | 64 | // Variables /////////////////////////////////////////// |
jobuuu | 7:e9086c72bb22 | 65 | State pos; |
jobuuu | 7:e9086c72bb22 | 66 | State vel; |
jobuuu | 7:e9086c72bb22 | 67 | State Vout; |
jobuuu | 7:e9086c72bb22 | 68 | State torq; |
Lightvalve | 207:c70c5a9f17dd | 69 | State torq_dot; |
jobuuu | 7:e9086c72bb22 | 70 | State pres_A; |
jobuuu | 7:e9086c72bb22 | 71 | State pres_B; |
jobuuu | 7:e9086c72bb22 | 72 | State cur; |
Lightvalve | 14:8e7590227d22 | 73 | State valve_pos; |
Lightvalve | 14:8e7590227d22 | 74 | |
Lightvalve | 14:8e7590227d22 | 75 | State INIT_Vout; |
Lightvalve | 14:8e7590227d22 | 76 | State INIT_Valve_Pos; |
Lightvalve | 14:8e7590227d22 | 77 | State INIT_Pos; |
Lightvalve | 14:8e7590227d22 | 78 | State INIT_torq; |
jobuuu | 5:a4319f79457b | 79 | |
Lightvalve | 19:23b7c1ad8683 | 80 | extern int CID_RX_CMD; |
Lightvalve | 19:23b7c1ad8683 | 81 | extern int CID_RX_REF_POSITION; |
hyhoon | 227:699c3e572283 | 82 | extern int CID_RX_REF_OPENLOOP; |
Lightvalve | 45:35fa6884d0c6 | 83 | extern int CID_RX_REF_PWM; |
Lightvalve | 19:23b7c1ad8683 | 84 | |
Lightvalve | 19:23b7c1ad8683 | 85 | extern int CID_TX_INFO; |
hyhoon | 227:699c3e572283 | 86 | extern int CID_TX_POS_VEL_TORQ; |
hyhoon | 227:699c3e572283 | 87 | extern int CID_TX_PWM; |
hyhoon | 227:699c3e572283 | 88 | extern int CID_TX_CURRENT; |
Lightvalve | 19:23b7c1ad8683 | 89 | extern int CID_TX_VOUT; |
Lightvalve | 19:23b7c1ad8683 | 90 | extern int CID_TX_VALVE_POSITION; |
jeongyeseong | 225:9c0becc196ba | 91 | extern int CID_TX_SOMETHING; |
Lightvalve | 19:23b7c1ad8683 | 92 | |
hyhoon | 226:82a3ca333004 | 93 | float pres_A_new = 0.0f; |
hyhoon | 226:82a3ca333004 | 94 | float pres_B_new = 0.0f; |
Lightvalve | 61:bc8c8270f0ab | 95 | |
hyhoon | 227:699c3e572283 | 96 | |
hyhoon | 227:699c3e572283 | 97 | |
hyhoon | 227:699c3e572283 | 98 | float temp_P_GAIN = 0.0f; |
hyhoon | 227:699c3e572283 | 99 | float temp_I_GAIN = 0.0f; |
hyhoon | 227:699c3e572283 | 100 | int temp_VELOCITY_COMP_GAIN = 0; |
hyhoon | 227:699c3e572283 | 101 | |
hyhoon | 227:699c3e572283 | 102 | |
Lightvalve | 12:6f2531038ea4 | 103 | /******************************************************************************* |
Lightvalve | 12:6f2531038ea4 | 104 | * REFERENCE MODE |
Lightvalve | 12:6f2531038ea4 | 105 | ******************************************************************************/ |
Lightvalve | 13:747daba9cf59 | 106 | enum _REFERENCE_MODE { |
Lightvalve | 13:747daba9cf59 | 107 | MODE_REF_NO_ACT = 0, //0 |
Lightvalve | 224:985dba42f261 | 108 | MODE_REF_DIRECT, //1 |
Lightvalve | 224:985dba42f261 | 109 | MODE_REF_COS_INC, //2 |
Lightvalve | 224:985dba42f261 | 110 | MODE_REF_LINE_INC, //3 |
Lightvalve | 12:6f2531038ea4 | 111 | MODE_REF_SIN_WAVE, //4 |
Lightvalve | 224:985dba42f261 | 112 | MODE_REF_SQUARE_WAVE, //5 |
Lightvalve | 12:6f2531038ea4 | 113 | }; |
Lightvalve | 12:6f2531038ea4 | 114 | |
Lightvalve | 12:6f2531038ea4 | 115 | /******************************************************************************* |
Lightvalve | 12:6f2531038ea4 | 116 | * CONTROL MODE |
Lightvalve | 12:6f2531038ea4 | 117 | ******************************************************************************/ |
Lightvalve | 13:747daba9cf59 | 118 | enum _CONTROL_MODE { |
Lightvalve | 12:6f2531038ea4 | 119 | //control mode |
Lightvalve | 12:6f2531038ea4 | 120 | MODE_NO_ACT = 0, //0 |
Lightvalve | 47:fdcb8bd86fd6 | 121 | MODE_VALVE_POSITION_CONTROL, //1 |
Lightvalve | 47:fdcb8bd86fd6 | 122 | MODE_JOINT_CONTROL, //2 |
Lightvalve | 50:3c630b5eba9f | 123 | |
Lightvalve | 47:fdcb8bd86fd6 | 124 | MODE_VALVE_OPEN_LOOP, //3 |
Lightvalve | 138:a843f32ced33 | 125 | MODE_JOINT_ADAPTIVE_BACKSTEPPING, //4 |
Lightvalve | 171:bfc1fd2629d8 | 126 | MODE_RL, //5 |
Lightvalve | 13:747daba9cf59 | 127 | |
Lightvalve | 12:6f2531038ea4 | 128 | MODE_JOINT_POSITION_PRES_CONTROL_PWM, //6 |
Lightvalve | 12:6f2531038ea4 | 129 | MODE_JOINT_POSITION_PRES_CONTROL_VALVE_POSITION, //7 |
Lightvalve | 12:6f2531038ea4 | 130 | MODE_VALVE_POSITION_PRES_CONTROL_LEARNING, //8 |
Lightvalve | 13:747daba9cf59 | 131 | |
Lightvalve | 12:6f2531038ea4 | 132 | MODE_TEST_CURRENT_CONTROL, //9 |
Lightvalve | 12:6f2531038ea4 | 133 | MODE_TEST_PWM_CONTROL, //10 |
Lightvalve | 21:e5f1a43ea6f9 | 134 | |
Lightvalve | 14:8e7590227d22 | 135 | MODE_CURRENT_CONTROL, //11 |
Lightvalve | 14:8e7590227d22 | 136 | MODE_JOINT_POSITION_TORQUE_CONTROL_CURRENT, //12 |
Lightvalve | 14:8e7590227d22 | 137 | MODE_JOINT_POSITION_PRES_CONTROL_CURRENT, //13 |
Lightvalve | 224:985dba42f261 | 138 | MODE_VALVE_POSITION_TORQUE_CONTROL_LEARNING, //14 |
Lightvalve | 13:747daba9cf59 | 139 | |
Lightvalve | 12:6f2531038ea4 | 140 | //utility |
Lightvalve | 12:6f2531038ea4 | 141 | MODE_TORQUE_SENSOR_NULLING = 20, //20 |
Lightvalve | 12:6f2531038ea4 | 142 | MODE_VALVE_NULLING_AND_DEADZONE_SETTING, //21 |
Lightvalve | 12:6f2531038ea4 | 143 | MODE_FIND_HOME, //22 |
Lightvalve | 57:f4819de54e7a | 144 | MODE_VALVE_GAIN_SETTING, //23 |
Lightvalve | 12:6f2531038ea4 | 145 | MODE_PRESSURE_SENSOR_NULLING, //24 |
Lightvalve | 12:6f2531038ea4 | 146 | MODE_PRESSURE_SENSOR_CALIB, //25 |
Lightvalve | 12:6f2531038ea4 | 147 | MODE_ROTARY_FRICTION_TUNING, //26 |
Lightvalve | 13:747daba9cf59 | 148 | |
Lightvalve | 224:985dba42f261 | 149 | MODE_DDV_POS_VS_PWM_ID = 30, //30 |
Lightvalve | 12:6f2531038ea4 | 150 | MODE_DDV_DEADZONE_AND_CENTER, //31 |
Lightvalve | 12:6f2531038ea4 | 151 | MODE_DDV_POS_VS_FLOWRATE, //32 |
Lightvalve | 57:f4819de54e7a | 152 | MODE_SYSTEM_ID, //33 |
Lightvalve | 169:645207e160ca | 153 | MODE_FREQ_TEST, //34 |
Lightvalve | 169:645207e160ca | 154 | MODE_SEND_BUFFER, //35 |
Lightvalve | 169:645207e160ca | 155 | MODE_SEND_OVER, //36 |
Lightvalve | 169:645207e160ca | 156 | MODE_STEP_TEST, //37 |
Lightvalve | 12:6f2531038ea4 | 157 | }; |
Lightvalve | 12:6f2531038ea4 | 158 | |
Lightvalve | 65:a2d7c63419c2 | 159 | void SystemClock_Config(void) |
Lightvalve | 65:a2d7c63419c2 | 160 | { |
Lightvalve | 169:645207e160ca | 161 | RCC_OscInitTypeDef RCC_OscInitStruct = {0}; |
Lightvalve | 169:645207e160ca | 162 | RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; |
Lightvalve | 65:a2d7c63419c2 | 163 | |
Lightvalve | 224:985dba42f261 | 164 | /* Configure the main internal regulator output voltage |
Lightvalve | 169:645207e160ca | 165 | */ |
Lightvalve | 169:645207e160ca | 166 | __HAL_RCC_PWR_CLK_ENABLE(); |
Lightvalve | 169:645207e160ca | 167 | __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); |
Lightvalve | 224:985dba42f261 | 168 | /* Initializes the CPU, AHB and APB busses clocks |
Lightvalve | 169:645207e160ca | 169 | */ |
Lightvalve | 169:645207e160ca | 170 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; |
Lightvalve | 169:645207e160ca | 171 | RCC_OscInitStruct.HSIState = RCC_HSI_ON; |
Lightvalve | 169:645207e160ca | 172 | RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; |
Lightvalve | 169:645207e160ca | 173 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
Lightvalve | 169:645207e160ca | 174 | RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; |
Lightvalve | 169:645207e160ca | 175 | RCC_OscInitStruct.PLL.PLLM = 8;//8 |
Lightvalve | 169:645207e160ca | 176 | RCC_OscInitStruct.PLL.PLLN = 180; //180 |
Lightvalve | 169:645207e160ca | 177 | RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; |
Lightvalve | 169:645207e160ca | 178 | RCC_OscInitStruct.PLL.PLLQ = 2; |
Lightvalve | 169:645207e160ca | 179 | RCC_OscInitStruct.PLL.PLLR = 2; |
Lightvalve | 169:645207e160ca | 180 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { |
Lightvalve | 169:645207e160ca | 181 | //Error_Handler(); |
Lightvalve | 169:645207e160ca | 182 | } |
Lightvalve | 169:645207e160ca | 183 | /** Activate the Over-Drive mode |
Lightvalve | 169:645207e160ca | 184 | */ |
Lightvalve | 169:645207e160ca | 185 | if (HAL_PWREx_EnableOverDrive() != HAL_OK) { |
Lightvalve | 169:645207e160ca | 186 | //Error_Handler(); |
Lightvalve | 169:645207e160ca | 187 | } |
Lightvalve | 169:645207e160ca | 188 | /** Initializes the CPU, AHB and APB busses clocks |
Lightvalve | 169:645207e160ca | 189 | */ |
Lightvalve | 169:645207e160ca | 190 | RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |
Lightvalve | 169:645207e160ca | 191 | |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; |
Lightvalve | 169:645207e160ca | 192 | RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; |
Lightvalve | 169:645207e160ca | 193 | RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; |
Lightvalve | 169:645207e160ca | 194 | RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4; |
Lightvalve | 169:645207e160ca | 195 | RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; |
Lightvalve | 65:a2d7c63419c2 | 196 | |
Lightvalve | 169:645207e160ca | 197 | if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) { |
Lightvalve | 169:645207e160ca | 198 | //Error_Handler(); |
Lightvalve | 169:645207e160ca | 199 | } |
Lightvalve | 65:a2d7c63419c2 | 200 | } |
Lightvalve | 65:a2d7c63419c2 | 201 | |
Lightvalve | 170:42c938a40313 | 202 | |
GiJeongKim | 0:51c43836c1d7 | 203 | int main() |
GiJeongKim | 0:51c43836c1d7 | 204 | { |
jobuuu | 6:df07d3491e3a | 205 | /********************************* |
jobuuu | 1:e04e563be5ce | 206 | *** Initialization |
jobuuu | 6:df07d3491e3a | 207 | *********************************/ |
hyhoon | 226:82a3ca333004 | 208 | |
Lightvalve | 224:985dba42f261 | 209 | HAL_Init(); |
Lightvalve | 224:985dba42f261 | 210 | SystemClock_Config(); |
hyhoon | 226:82a3ca333004 | 211 | |
Lightvalve | 69:3995ffeaa786 | 212 | LED = 0; |
Lightvalve | 61:bc8c8270f0ab | 213 | pc.baud(9600); |
Lightvalve | 21:e5f1a43ea6f9 | 214 | |
GiJeongKim | 0:51c43836c1d7 | 215 | // i2c init |
hyhoon | 226:82a3ca333004 | 216 | // i2c.frequency(400 * 1000); // 0.4 mHz |
hyhoon | 226:82a3ca333004 | 217 | // wait_ms(2); // Power Up wait |
hyhoon | 226:82a3ca333004 | 218 | // look_for_hardware_i2c(); // Hardware present |
hyhoon | 226:82a3ca333004 | 219 | // init_as5510(i2c_slave_addr1); |
hyhoon | 226:82a3ca333004 | 220 | // make_delay(); |
jobuuu | 2:a1c0a37df760 | 221 | |
Lightvalve | 224:985dba42f261 | 222 | // spi init |
hyhoon | 227:699c3e572283 | 223 | eeprom_cs = 1; |
Lightvalve | 170:42c938a40313 | 224 | eeprom.format(8,3); |
Lightvalve | 170:42c938a40313 | 225 | eeprom.frequency(5000000); //5M |
hyhoon | 227:699c3e572283 | 226 | eeprom_cs = 0; |
hyhoon | 227:699c3e572283 | 227 | make_delay(); |
hyhoon | 227:699c3e572283 | 228 | |
hyhoon | 227:699c3e572283 | 229 | enc_cs = 1; //sw add |
GiJeongKim | 0:51c43836c1d7 | 230 | enc.format(8,0); |
hyhoon | 227:699c3e572283 | 231 | enc.frequency(5000000); //10M |
hyhoon | 227:699c3e572283 | 232 | enc_cs = 0; //sw add |
hyhoon | 227:699c3e572283 | 233 | |
Lightvalve | 11:82d8768d7351 | 234 | make_delay(); |
Lightvalve | 21:e5f1a43ea6f9 | 235 | |
hyhoon | 226:82a3ca333004 | 236 | |
hyhoon | 226:82a3ca333004 | 237 | // spi _ enc |
hyhoon | 226:82a3ca333004 | 238 | spi_enc_set_init(); |
hyhoon | 226:82a3ca333004 | 239 | make_delay(); |
hyhoon | 226:82a3ca333004 | 240 | |
hyhoon | 226:82a3ca333004 | 241 | ////// bno rom |
hyhoon | 226:82a3ca333004 | 242 | // spi_eeprom_write(RID_BNO, (int16_t)1); |
hyhoon | 226:82a3ca333004 | 243 | // make_delay(); |
hyhoon | 226:82a3ca333004 | 244 | //////// |
hyhoon | 226:82a3ca333004 | 245 | |
Lightvalve | 224:985dba42f261 | 246 | // rom |
Lightvalve | 19:23b7c1ad8683 | 247 | ROM_CALL_DATA(); |
Lightvalve | 16:903b5a4433b4 | 248 | make_delay(); |
Lightvalve | 13:747daba9cf59 | 249 | |
GiJeongKim | 0:51c43836c1d7 | 250 | // ADC init |
jobuuu | 5:a4319f79457b | 251 | Init_ADC(); |
Lightvalve | 11:82d8768d7351 | 252 | make_delay(); |
jobuuu | 2:a1c0a37df760 | 253 | |
GiJeongKim | 0:51c43836c1d7 | 254 | // Pwm init |
GiJeongKim | 0:51c43836c1d7 | 255 | Init_PWM(); |
GiJeongKim | 0:51c43836c1d7 | 256 | TIM4->CR1 ^= TIM_CR1_UDIS; |
Lightvalve | 11:82d8768d7351 | 257 | make_delay(); |
Lightvalve | 13:747daba9cf59 | 258 | |
GiJeongKim | 0:51c43836c1d7 | 259 | // CAN |
jobuuu | 2:a1c0a37df760 | 260 | can.attach(&CAN_RX_HANDLER); |
Lightvalve | 11:82d8768d7351 | 261 | CAN_ID_INIT(); |
Lightvalve | 11:82d8768d7351 | 262 | make_delay(); |
Lightvalve | 34:bb2ca2fc2a8e | 263 | |
hyhoon | 226:82a3ca333004 | 264 | //can.reset(); |
hyhoon | 226:82a3ca333004 | 265 | can.filter(msg.id, 0xFFFFF000, CANStandard); |
hyhoon | 226:82a3ca333004 | 266 | |
hyhoon | 226:82a3ca333004 | 267 | // TMR3 init |
hyhoon | 226:82a3ca333004 | 268 | Init_TMR3(); |
hyhoon | 226:82a3ca333004 | 269 | TIM3->CR1 ^= TIM_CR1_UDIS; |
hyhoon | 226:82a3ca333004 | 270 | make_delay(); |
hyhoon | 226:82a3ca333004 | 271 | |
hyhoon | 226:82a3ca333004 | 272 | |
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 | 34:bb2ca2fc2a8e | 279 | |
hyhoon | 226:82a3ca333004 | 280 | |
Lightvalve | 13:747daba9cf59 | 281 | |
Lightvalve | 11:82d8768d7351 | 282 | //DAC init |
Lightvalve | 58:2eade98630e2 | 283 | if (SENSING_MODE == 0) { |
Lightvalve | 58:2eade98630e2 | 284 | dac_1 = TORQUE_VREF / 3.3f; |
Lightvalve | 58:2eade98630e2 | 285 | dac_2 = 0.0f; |
Lightvalve | 58:2eade98630e2 | 286 | } else if (SENSING_MODE == 1) { |
hyhoon | 226:82a3ca333004 | 287 | if (DIR_VALVE_ENC > 0) { |
hyhoon | 226:82a3ca333004 | 288 | dac_1 = PRES_A_VREF / 3.3f; |
hyhoon | 226:82a3ca333004 | 289 | dac_2 = PRES_B_VREF / 3.3f; |
hyhoon | 226:82a3ca333004 | 290 | } else { |
hyhoon | 226:82a3ca333004 | 291 | dac_1 = PRES_B_VREF / 3.3f; |
hyhoon | 226:82a3ca333004 | 292 | dac_2 = PRES_A_VREF / 3.3f; |
hyhoon | 226:82a3ca333004 | 293 | } |
Lightvalve | 58:2eade98630e2 | 294 | } |
Lightvalve | 11:82d8768d7351 | 295 | make_delay(); |
Lightvalve | 13:747daba9cf59 | 296 | |
Lightvalve | 19:23b7c1ad8683 | 297 | for (int i=0; i<50; i++) { |
Lightvalve | 11:82d8768d7351 | 298 | if(i%2==0) |
Lightvalve | 38:118df027d851 | 299 | ID_index_array[i] = - i * 0.5f; |
Lightvalve | 11:82d8768d7351 | 300 | else |
Lightvalve | 38:118df027d851 | 301 | ID_index_array[i] = (i+1) * 0.5f; |
Lightvalve | 11:82d8768d7351 | 302 | } |
Lightvalve | 169:645207e160ca | 303 | |
jobuuu | 6:df07d3491e3a | 304 | /************************************ |
jobuuu | 1:e04e563be5ce | 305 | *** Program is operating! |
jobuuu | 6:df07d3491e3a | 306 | *************************************/ |
GiJeongKim | 0:51c43836c1d7 | 307 | while(1) { |
Lightvalve | 169:645207e160ca | 308 | |
Lightvalve | 224:985dba42f261 | 309 | // UART example |
Lightvalve | 224:985dba42f261 | 310 | // if(timer_while==100000) { |
Lightvalve | 171:bfc1fd2629d8 | 311 | // timer_while = 0; |
Lightvalve | 224:985dba42f261 | 312 | // pc.printf("%f\n", value); |
Lightvalve | 171:bfc1fd2629d8 | 313 | // } |
Lightvalve | 224:985dba42f261 | 314 | // timer_while ++; |
Lightvalve | 169:645207e160ca | 315 | |
Lightvalve | 224:985dba42f261 | 316 | //i2c for SW valve |
hyhoon | 227:699c3e572283 | 317 | //if(OPERATING_MODE == 5) { |
hyhoon | 227:699c3e572283 | 318 | // read_field(i2c_slave_addr1); |
hyhoon | 227:699c3e572283 | 319 | // if(DIR_VALVE_ENC < 0) value = 1023 - value; |
hyhoon | 227:699c3e572283 | 320 | // } |
GiJeongKim | 0:51c43836c1d7 | 321 | } |
jobuuu | 1:e04e563be5ce | 322 | } |
jobuuu | 1:e04e563be5ce | 323 | |
Lightvalve | 224:985dba42f261 | 324 | |
Lightvalve | 224:985dba42f261 | 325 | // Velocity feedforward for SW valve |
Lightvalve | 33:91b17819ec30 | 326 | float DDV_JOINT_POS_FF(float REF_JOINT_VEL) |
Lightvalve | 14:8e7590227d22 | 327 | { |
Lightvalve | 13:747daba9cf59 | 328 | int i = 0; |
Lightvalve | 48:889798ff9329 | 329 | float Ref_Valve_Pos_FF = 0.0f; |
Lightvalve | 14:8e7590227d22 | 330 | for(i=0; i<VALVE_POS_NUM; i++) { |
Lightvalve | 14:8e7590227d22 | 331 | 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 | 332 | if(i==0) { |
Lightvalve | 50:3c630b5eba9f | 333 | if(JOINT_VEL[i+1] == JOINT_VEL[i]) { |
Lightvalve | 57:f4819de54e7a | 334 | Ref_Valve_Pos_FF = (float) VALVE_CENTER; |
Lightvalve | 50:3c630b5eba9f | 335 | } else { |
Lightvalve | 57:f4819de54e7a | 336 | 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 | 337 | } |
Lightvalve | 14:8e7590227d22 | 338 | } else { |
Lightvalve | 50:3c630b5eba9f | 339 | if(JOINT_VEL[i+1] == JOINT_VEL[i-1]) { |
Lightvalve | 57:f4819de54e7a | 340 | Ref_Valve_Pos_FF = (float) VALVE_CENTER; |
Lightvalve | 50:3c630b5eba9f | 341 | } else { |
Lightvalve | 57:f4819de54e7a | 342 | 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 | 343 | } |
Lightvalve | 13:747daba9cf59 | 344 | } |
Lightvalve | 13:747daba9cf59 | 345 | break; |
Lightvalve | 13:747daba9cf59 | 346 | } |
Lightvalve | 13:747daba9cf59 | 347 | } |
Lightvalve | 14:8e7590227d22 | 348 | if(REF_JOINT_VEL > max(JOINT_VEL[VALVE_POS_NUM-1], JOINT_VEL[VALVE_POS_NUM-2])) { |
Lightvalve | 33:91b17819ec30 | 349 | Ref_Valve_Pos_FF = (float) VALVE_MAX_POS; |
Lightvalve | 14:8e7590227d22 | 350 | } else if(REF_JOINT_VEL < min(JOINT_VEL[VALVE_POS_NUM-1], JOINT_VEL[VALVE_POS_NUM-2])) { |
Lightvalve | 33:91b17819ec30 | 351 | Ref_Valve_Pos_FF = (float) VALVE_MIN_POS; |
Lightvalve | 13:747daba9cf59 | 352 | } |
Lightvalve | 36:a46e63505ed8 | 353 | |
Lightvalve | 224:985dba42f261 | 354 | 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 | 355 | return Ref_Valve_Pos_FF; |
Lightvalve | 13:747daba9cf59 | 356 | } |
jobuuu | 6:df07d3491e3a | 357 | |
Lightvalve | 224:985dba42f261 | 358 | // Valve feedforward for SW valve |
Lightvalve | 30:8d561f16383b | 359 | void VALVE_POS_CONTROL(float REF_VALVE_POS) |
Lightvalve | 14:8e7590227d22 | 360 | { |
Lightvalve | 13:747daba9cf59 | 361 | int i = 0; |
Lightvalve | 13:747daba9cf59 | 362 | |
Lightvalve | 38:118df027d851 | 363 | if(REF_VALVE_POS > VALVE_MAX_POS) { |
Lightvalve | 38:118df027d851 | 364 | REF_VALVE_POS = VALVE_MAX_POS; |
Lightvalve | 38:118df027d851 | 365 | } else if(REF_VALVE_POS < VALVE_MIN_POS) { |
Lightvalve | 38:118df027d851 | 366 | REF_VALVE_POS = VALVE_MIN_POS; |
Lightvalve | 38:118df027d851 | 367 | } |
Lightvalve | 89:a7b45368ea0f | 368 | valve_pos_err = (float) (REF_VALVE_POS - value); |
Lightvalve | 13:747daba9cf59 | 369 | valve_pos_err_diff = valve_pos_err - valve_pos_err_old; |
Lightvalve | 13:747daba9cf59 | 370 | valve_pos_err_old = valve_pos_err; |
Lightvalve | 13:747daba9cf59 | 371 | valve_pos_err_sum += valve_pos_err; |
Lightvalve | 89:a7b45368ea0f | 372 | if (valve_pos_err_sum > 1000.0f) valve_pos_err_sum = 1000.0f; |
Lightvalve | 89:a7b45368ea0f | 373 | if (valve_pos_err_sum<-1000.0f) valve_pos_err_sum = -1000.0f; |
Lightvalve | 13:747daba9cf59 | 374 | |
Lightvalve | 13:747daba9cf59 | 375 | 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 | 376 | |
Lightvalve | 18:b8adf1582ea3 | 377 | for(i=0; i<24; i++) { |
Lightvalve | 89:a7b45368ea0f | 378 | 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 | 379 | if(i==0) { |
Lightvalve | 48:889798ff9329 | 380 | 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 | 381 | } else { |
Lightvalve | 48:889798ff9329 | 382 | 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 | 383 | } |
Lightvalve | 13:747daba9cf59 | 384 | break; |
Lightvalve | 13:747daba9cf59 | 385 | } |
Lightvalve | 13:747daba9cf59 | 386 | } |
Lightvalve | 59:f308b1656d9c | 387 | Vout.ref = VALVE_PWM_RAW_FF + VALVE_PWM_RAW_FB; |
Lightvalve | 13:747daba9cf59 | 388 | } |
Lightvalve | 13:747daba9cf59 | 389 | |
Lightvalve | 224:985dba42f261 | 390 | // PWM duty vs. voltage output of L6205 in STM board |
Lightvalve | 14:8e7590227d22 | 391 | #define LT_MAX_IDX 57 |
Lightvalve | 30:8d561f16383b | 392 | 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 | 393 | -19.0f, -18.0f, -17.0f, -16.0f, -15.0f, -14.0f, -13.0f, -12.0f, -11.0f, -10.0f, |
Lightvalve | 34:bb2ca2fc2a8e | 394 | -9.0f, -8.0f, -7.0f, -6.0f, -5.0f, -4.0f, -3.0f, -2.0f, -1.0f, 0.0f, |
Lightvalve | 34:bb2ca2fc2a8e | 395 | 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f, |
Lightvalve | 34:bb2ca2fc2a8e | 396 | 11.0f, 12.0f, 13.0f, 14.0f, 15.0f, 16.0f, 17.0f, 18.0f, 19.0f, 20.0f, |
Lightvalve | 34:bb2ca2fc2a8e | 397 | 25.0f, 30.0f, 35.0f, 40.0f, 50.0f, 60.0f, 80.0f, 100.0f |
Lightvalve | 34:bb2ca2fc2a8e | 398 | }; // duty |
Lightvalve | 67:c2812cf26c38 | 399 | 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 | 400 | -150.0f, -145.0f, -145.0f, -145.0f, -135.0f, -135.0f, -135.0f, -127.5f, -127.5f, -115.0f, |
Lightvalve | 67:c2812cf26c38 | 401 | -115.0f, -115.0F, -100.0f, -100.0f, -100.0f, -60.0f, -60.0f, -10.0f, -5.0f, 0.0f, |
Lightvalve | 67:c2812cf26c38 | 402 | 7.5f, 14.0f, 14.0f, 14.0f, 42.5f, 42.5f, 42.5f, 80.0f, 80.0f, 105.0f, |
Lightvalve | 67:c2812cf26c38 | 403 | 105.0f, 105.0f, 120.0f, 120.0f, 120.0f, 131.0f, 131.0f, 140.0f, 140.0f, 140.0f, |
Lightvalve | 67:c2812cf26c38 | 404 | 155.0f, 160.0f, 170.0f, 174.0f, 182.0f, 191.0f, 212.0f, 230.0f |
Lightvalve | 34:bb2ca2fc2a8e | 405 | }; // mV |
Lightvalve | 13:747daba9cf59 | 406 | |
Lightvalve | 30:8d561f16383b | 407 | float PWM_duty_byLT(float Ref_V) |
Lightvalve | 14:8e7590227d22 | 408 | { |
Lightvalve | 30:8d561f16383b | 409 | float PWM_duty = 0.0f; |
Lightvalve | 13:747daba9cf59 | 410 | if(Ref_V<LT_Voltage_Output[0]) { |
Lightvalve | 30:8d561f16383b | 411 | PWM_duty = (Ref_V-LT_Voltage_Output[0])/1.5f+LT_PWM_duty[0]; |
Lightvalve | 13:747daba9cf59 | 412 | } else if (Ref_V>=LT_Voltage_Output[LT_MAX_IDX-1]) { |
Lightvalve | 30:8d561f16383b | 413 | PWM_duty = (Ref_V-LT_Voltage_Output[LT_MAX_IDX-1])/1.5f+LT_PWM_duty[LT_MAX_IDX-1]; |
Lightvalve | 13:747daba9cf59 | 414 | } else { |
Lightvalve | 13:747daba9cf59 | 415 | int idx = 0; |
Lightvalve | 13:747daba9cf59 | 416 | for(idx=0; idx<LT_MAX_IDX-1; idx++) { |
Lightvalve | 30:8d561f16383b | 417 | float ini_x = LT_Voltage_Output[idx]; |
Lightvalve | 30:8d561f16383b | 418 | float fin_x = LT_Voltage_Output[idx+1]; |
Lightvalve | 30:8d561f16383b | 419 | float ini_y = LT_PWM_duty[idx]; |
Lightvalve | 30:8d561f16383b | 420 | float fin_y = LT_PWM_duty[idx+1]; |
Lightvalve | 13:747daba9cf59 | 421 | if(Ref_V>=ini_x && Ref_V<fin_x) { |
Lightvalve | 13:747daba9cf59 | 422 | PWM_duty = (fin_y-ini_y)/(fin_x-ini_x)*(Ref_V-ini_x) + ini_y; |
Lightvalve | 13:747daba9cf59 | 423 | break; |
Lightvalve | 13:747daba9cf59 | 424 | } |
Lightvalve | 13:747daba9cf59 | 425 | } |
Lightvalve | 13:747daba9cf59 | 426 | } |
Lightvalve | 14:8e7590227d22 | 427 | |
Lightvalve | 13:747daba9cf59 | 428 | return PWM_duty; |
Lightvalve | 13:747daba9cf59 | 429 | } |
jobuuu | 6:df07d3491e3a | 430 | |
Lightvalve | 57:f4819de54e7a | 431 | |
Lightvalve | 57:f4819de54e7a | 432 | |
jobuuu | 2:a1c0a37df760 | 433 | /******************************************************************************* |
jobuuu | 2:a1c0a37df760 | 434 | TIMER INTERRUPT |
jobuuu | 2:a1c0a37df760 | 435 | *******************************************************************************/ |
jobuuu | 2:a1c0a37df760 | 436 | |
Lightvalve | 51:b46bed7fec80 | 437 | float FREQ_TMR4 = (float)FREQ_20k; |
Lightvalve | 51:b46bed7fec80 | 438 | float DT_TMR4 = (float)DT_20k; |
Lightvalve | 57:f4819de54e7a | 439 | long CNT_TMR4 = 0; |
Lightvalve | 57:f4819de54e7a | 440 | int TMR4_FREQ_10k = (int)FREQ_10k; |
jobuuu | 1:e04e563be5ce | 441 | extern "C" void TIM4_IRQHandler(void) |
jobuuu | 1:e04e563be5ce | 442 | { |
Lightvalve | 19:23b7c1ad8683 | 443 | if (TIM4->SR & TIM_SR_UIF ) { |
Lightvalve | 21:e5f1a43ea6f9 | 444 | /******************************************************* |
Lightvalve | 21:e5f1a43ea6f9 | 445 | *** Sensor Read & Data Handling |
Lightvalve | 21:e5f1a43ea6f9 | 446 | ********************************************************/ |
Lightvalve | 13:747daba9cf59 | 447 | |
Lightvalve | 57:f4819de54e7a | 448 | //Encoder |
Lightvalve | 57:f4819de54e7a | 449 | if (CNT_TMR4 % (int) ((int) FREQ_TMR4/TMR4_FREQ_10k) == 0) { |
Lightvalve | 57:f4819de54e7a | 450 | ENC_UPDATE(); |
Lightvalve | 57:f4819de54e7a | 451 | } |
Lightvalve | 61:bc8c8270f0ab | 452 | |
Lightvalve | 61:bc8c8270f0ab | 453 | ADC1->CR2 |= 0x40000000; |
Lightvalve | 224:985dba42f261 | 454 | // Torque Sensing ============================================= |
Lightvalve | 58:2eade98630e2 | 455 | if (SENSING_MODE == 0) { |
Lightvalve | 58:2eade98630e2 | 456 | float pres_A_new = (((float) ADC1->DR) - 2047.5f); |
Lightvalve | 224:985dba42f261 | 457 | double alpha_update_ft = 1.0f / (1.0f + FREQ_TMR4 / (2.0f * 3.14f * 100.0f)); // f_cutoff : 100Hz |
Lightvalve | 58:2eade98630e2 | 458 | pres_A.sen = (1.0f - alpha_update_ft) * pres_A.sen + alpha_update_ft * pres_A_new; |
Lightvalve | 67:c2812cf26c38 | 459 | torq.sen = -pres_A.sen / TORQUE_SENSOR_PULSE_PER_TORQUE; |
Lightvalve | 67:c2812cf26c38 | 460 | |
hyhoon | 226:82a3ca333004 | 461 | // Pressure Sensing (0~210)bar ============================================= |
Lightvalve | 58:2eade98630e2 | 462 | } else if (SENSING_MODE == 1) { |
hyhoon | 226:82a3ca333004 | 463 | |
hyhoon | 226:82a3ca333004 | 464 | if (DIR_VALVE_ENC > 0) { |
hyhoon | 226:82a3ca333004 | 465 | pres_A_new = (((float)ADC1->DR) - PRES_A_NULL); |
hyhoon | 226:82a3ca333004 | 466 | pres_B_new = (((float)ADC2->DR) - PRES_B_NULL); |
hyhoon | 226:82a3ca333004 | 467 | } else { |
hyhoon | 226:82a3ca333004 | 468 | pres_A_new = (((float)ADC2->DR) - PRES_A_NULL); |
hyhoon | 226:82a3ca333004 | 469 | pres_B_new = (((float)ADC1->DR) - PRES_B_NULL); |
hyhoon | 226:82a3ca333004 | 470 | } |
Lightvalve | 224:985dba42f261 | 471 | double alpha_update_pres = 1.0f / (1.0f + FREQ_TMR4 / (2.0f * 3.14f * 200.0f)); // f_cutoff : 200Hz |
Lightvalve | 58:2eade98630e2 | 472 | pres_A.sen = (1.0f - alpha_update_pres) * pres_A.sen + alpha_update_pres * pres_A_new; |
Lightvalve | 58:2eade98630e2 | 473 | pres_B.sen = (1.0f - alpha_update_pres) * pres_B.sen + alpha_update_pres * pres_B_new; |
Lightvalve | 58:2eade98630e2 | 474 | CUR_PRES_A_BAR = pres_A.sen / PRES_SENSOR_A_PULSE_PER_BAR; |
Lightvalve | 58:2eade98630e2 | 475 | CUR_PRES_B_BAR = pres_B.sen / PRES_SENSOR_B_PULSE_PER_BAR; |
Lightvalve | 50:3c630b5eba9f | 476 | |
Lightvalve | 58:2eade98630e2 | 477 | if ((OPERATING_MODE & 0x01) == 0) { // Rotary Actuator |
Lightvalve | 58:2eade98630e2 | 478 | torq.sen = (PISTON_AREA_A * CUR_PRES_A_BAR - PISTON_AREA_B * CUR_PRES_B_BAR) * 0.0001f; // mm^3*bar >> Nm |
Lightvalve | 58:2eade98630e2 | 479 | } else if ((OPERATING_MODE & 0x01) == 1) { // Linear Actuator |
Lightvalve | 58:2eade98630e2 | 480 | torq.sen = (PISTON_AREA_A * CUR_PRES_A_BAR - PISTON_AREA_B * CUR_PRES_B_BAR) * 0.1f; // mm^2*bar >> N |
Lightvalve | 58:2eade98630e2 | 481 | } |
Lightvalve | 58:2eade98630e2 | 482 | } |
Lightvalve | 61:bc8c8270f0ab | 483 | |
Lightvalve | 21:e5f1a43ea6f9 | 484 | //Current |
Lightvalve | 21:e5f1a43ea6f9 | 485 | //ADC3->CR2 |= 0x40000000; // adc _ 12bit |
Lightvalve | 51:b46bed7fec80 | 486 | float alpha_update_cur = 1.0f/(1.0f + FREQ_TMR4/(2.0f*3.14f*500.0f)); // f_cutoff : 500Hz |
Lightvalve | 30:8d561f16383b | 487 | float cur_new = ((float)ADC3->DR-2048.0f)*20.0f/4096.0f; // unit : mA |
Lightvalve | 30:8d561f16383b | 488 | cur.sen=cur.sen*(1.0f-alpha_update_cur)+cur_new*(alpha_update_cur); |
Lightvalve | 58:2eade98630e2 | 489 | |
Lightvalve | 57:f4819de54e7a | 490 | CNT_TMR4++; |
Lightvalve | 21:e5f1a43ea6f9 | 491 | } |
Lightvalve | 11:82d8768d7351 | 492 | TIM4->SR = 0x0; // reset the status register |
Lightvalve | 11:82d8768d7351 | 493 | } |
Lightvalve | 19:23b7c1ad8683 | 494 | |
Lightvalve | 19:23b7c1ad8683 | 495 | |
Lightvalve | 18:b8adf1582ea3 | 496 | int j =0; |
Lightvalve | 54:647072f5307a | 497 | float FREQ_TMR3 = (float)FREQ_5k; |
Lightvalve | 48:889798ff9329 | 498 | float DT_TMR3 = (float)DT_5k; |
Lightvalve | 45:35fa6884d0c6 | 499 | int cnt_trans = 0; |
Lightvalve | 48:889798ff9329 | 500 | double VALVE_POS_RAW_FORCE_FB_LOGGING = 0.0f; |
Lightvalve | 57:f4819de54e7a | 501 | int can_rest =0; |
Lightvalve | 48:889798ff9329 | 502 | |
Lightvalve | 11:82d8768d7351 | 503 | extern "C" void TIM3_IRQHandler(void) |
Lightvalve | 21:e5f1a43ea6f9 | 504 | { |
Lightvalve | 19:23b7c1ad8683 | 505 | if (TIM3->SR & TIM_SR_UIF ) { |
Lightvalve | 57:f4819de54e7a | 506 | |
hyhoon | 226:82a3ca333004 | 507 | |
Lightvalve | 57:f4819de54e7a | 508 | if (((OPERATING_MODE&0b110)>>1) == 0) { |
Lightvalve | 57:f4819de54e7a | 509 | K_v = 0.4f; // Moog (LPM >> mA) , 100bar |
Lightvalve | 57:f4819de54e7a | 510 | mV_PER_mA = 500.0f; // 5000mV/10mA |
Lightvalve | 57:f4819de54e7a | 511 | mV_PER_pulse = 0.5f; // 5000mV/10000pulse |
Lightvalve | 57:f4819de54e7a | 512 | mA_PER_pulse = 0.001f; // 10mA/10000pulse |
Lightvalve | 57:f4819de54e7a | 513 | } else if (((OPERATING_MODE&0b110)>>1) == 1) { |
Lightvalve | 57:f4819de54e7a | 514 | K_v = 0.5f; // KNR (LPM >> mA) , 100bar |
Lightvalve | 57:f4819de54e7a | 515 | mV_PER_mA = 166.6666f; // 5000mV/30mA |
Lightvalve | 57:f4819de54e7a | 516 | mV_PER_pulse = 0.5f; // 5000mV/10000pulse |
Lightvalve | 57:f4819de54e7a | 517 | mA_PER_pulse = 0.003f; // 30mA/10000pulse |
Lightvalve | 57:f4819de54e7a | 518 | } |
Lightvalve | 50:3c630b5eba9f | 519 | |
Lightvalve | 50:3c630b5eba9f | 520 | if(MODE_POS_FT_TRANS == 1) { |
Lightvalve | 48:889798ff9329 | 521 | alpha_trans = (float)(1.0f - cos(3.141592f * (float)cnt_trans * DT_TMR3 /3.0f))/2.0f; |
Lightvalve | 45:35fa6884d0c6 | 522 | cnt_trans++; |
Lightvalve | 46:2694daea349b | 523 | torq.err_sum = 0; |
Lightvalve | 48:889798ff9329 | 524 | if((float)cnt_trans * DT_TMR3 > 3.0f) |
Lightvalve | 45:35fa6884d0c6 | 525 | MODE_POS_FT_TRANS = 2; |
Lightvalve | 50:3c630b5eba9f | 526 | } else if(MODE_POS_FT_TRANS == 3) { |
Lightvalve | 48:889798ff9329 | 527 | alpha_trans = (float)(1.0f + cos(3.141592f * (float)cnt_trans * DT_TMR3 /3.0f))/2.0f; |
Lightvalve | 45:35fa6884d0c6 | 528 | cnt_trans++; |
Lightvalve | 46:2694daea349b | 529 | torq.err_sum = 0; |
Lightvalve | 48:889798ff9329 | 530 | if((float) cnt_trans * DT_TMR3 > 3.0f ) |
Lightvalve | 45:35fa6884d0c6 | 531 | MODE_POS_FT_TRANS = 0; |
Lightvalve | 50:3c630b5eba9f | 532 | } else if(MODE_POS_FT_TRANS == 2) { |
Lightvalve | 58:2eade98630e2 | 533 | alpha_trans = 1.0f; |
Lightvalve | 45:35fa6884d0c6 | 534 | cnt_trans = 0; |
Lightvalve | 50:3c630b5eba9f | 535 | } else { |
Lightvalve | 58:2eade98630e2 | 536 | alpha_trans = 0.0f; |
Lightvalve | 45:35fa6884d0c6 | 537 | cnt_trans = 0; |
Lightvalve | 45:35fa6884d0c6 | 538 | } |
Lightvalve | 45:35fa6884d0c6 | 539 | |
Lightvalve | 50:3c630b5eba9f | 540 | |
hyhoon | 227:699c3e572283 | 541 | // Reference Loop ========================================================== |
hyhoon | 227:699c3e572283 | 542 | switch (REFERENCE_MODE) { |
hyhoon | 227:699c3e572283 | 543 | case MODE_REF_NO_ACT: { |
hyhoon | 227:699c3e572283 | 544 | break; |
hyhoon | 227:699c3e572283 | 545 | } |
hyhoon | 227:699c3e572283 | 546 | |
hyhoon | 227:699c3e572283 | 547 | case MODE_REF_DIRECT: { |
hyhoon | 227:699c3e572283 | 548 | pos.ref = REF_POSITION; |
hyhoon | 227:699c3e572283 | 549 | vel.ref = REF_VELOCITY; |
hyhoon | 227:699c3e572283 | 550 | torq.ref = REF_TORQUE; |
hyhoon | 227:699c3e572283 | 551 | break; |
hyhoon | 227:699c3e572283 | 552 | } |
hyhoon | 227:699c3e572283 | 553 | default: |
hyhoon | 227:699c3e572283 | 554 | break; |
hyhoon | 227:699c3e572283 | 555 | } |
hyhoon | 227:699c3e572283 | 556 | |
Lightvalve | 57:f4819de54e7a | 557 | int UTILITY_MODE = 0; |
Lightvalve | 57:f4819de54e7a | 558 | int CONTROL_MODE = 0; |
Lightvalve | 58:2eade98630e2 | 559 | |
Lightvalve | 57:f4819de54e7a | 560 | if (CONTROL_UTILITY_MODE >= 20 || CONTROL_UTILITY_MODE == 0) { |
Lightvalve | 57:f4819de54e7a | 561 | UTILITY_MODE = CONTROL_UTILITY_MODE; |
Lightvalve | 57:f4819de54e7a | 562 | CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 57:f4819de54e7a | 563 | } else { |
Lightvalve | 57:f4819de54e7a | 564 | CONTROL_MODE = CONTROL_UTILITY_MODE; |
Lightvalve | 57:f4819de54e7a | 565 | UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 57:f4819de54e7a | 566 | } |
Lightvalve | 56:6f50d9d3bfee | 567 | |
Lightvalve | 56:6f50d9d3bfee | 568 | |
Lightvalve | 56:6f50d9d3bfee | 569 | |
Lightvalve | 57:f4819de54e7a | 570 | // UTILITY MODE ------------------------------------------------------------ |
Lightvalve | 57:f4819de54e7a | 571 | switch (UTILITY_MODE) { |
Lightvalve | 57:f4819de54e7a | 572 | case MODE_NO_ACT: { |
Lightvalve | 13:747daba9cf59 | 573 | break; |
Lightvalve | 13:747daba9cf59 | 574 | } |
Lightvalve | 14:8e7590227d22 | 575 | |
Lightvalve | 14:8e7590227d22 | 576 | case MODE_TORQUE_SENSOR_NULLING: { |
Lightvalve | 13:747daba9cf59 | 577 | // DAC Voltage reference set |
Lightvalve | 14:8e7590227d22 | 578 | if (TMR3_COUNT_TORQUE_NULL < TMR_FREQ_5k * 2) { |
Lightvalve | 15:bd0d12728506 | 579 | CUR_TORQUE_sum += torq.sen; |
Lightvalve | 19:23b7c1ad8683 | 580 | |
Lightvalve | 14:8e7590227d22 | 581 | if (TMR3_COUNT_TORQUE_NULL % 10 == 0) { |
Lightvalve | 30:8d561f16383b | 582 | CUR_TORQUE_mean = CUR_TORQUE_sum / 10.0f; |
Lightvalve | 13:747daba9cf59 | 583 | CUR_TORQUE_sum = 0; |
Lightvalve | 14:8e7590227d22 | 584 | |
Lightvalve | 84:c355d3e52bf1 | 585 | TORQUE_VREF += 0.000003f * (0.0f - CUR_TORQUE_mean); |
Lightvalve | 19:23b7c1ad8683 | 586 | |
Lightvalve | 30:8d561f16383b | 587 | if (TORQUE_VREF > 3.3f) TORQUE_VREF = 3.3f; |
Lightvalve | 59:f308b1656d9c | 588 | if (TORQUE_VREF < 0.0f) TORQUE_VREF = 0.0f; |
Lightvalve | 30:8d561f16383b | 589 | dac_1 = TORQUE_VREF / 3.3f; |
Lightvalve | 13:747daba9cf59 | 590 | } |
Lightvalve | 13:747daba9cf59 | 591 | } else { |
Lightvalve | 58:2eade98630e2 | 592 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 14:8e7590227d22 | 593 | TMR3_COUNT_TORQUE_NULL = 0; |
Lightvalve | 13:747daba9cf59 | 594 | CUR_TORQUE_sum = 0; |
Lightvalve | 13:747daba9cf59 | 595 | CUR_TORQUE_mean = 0; |
Lightvalve | 19:23b7c1ad8683 | 596 | |
Lightvalve | 170:42c938a40313 | 597 | spi_eeprom_write(RID_TORQUE_SENSOR_VREF, (int16_t) (TORQUE_VREF * 1000.0f)); |
Lightvalve | 19:23b7c1ad8683 | 598 | |
Lightvalve | 30:8d561f16383b | 599 | dac_1 = TORQUE_VREF / 3.3f; |
Lightvalve | 14:8e7590227d22 | 600 | |
Lightvalve | 13:747daba9cf59 | 601 | } |
Lightvalve | 14:8e7590227d22 | 602 | TMR3_COUNT_TORQUE_NULL++; |
Lightvalve | 13:747daba9cf59 | 603 | break; |
Lightvalve | 19:23b7c1ad8683 | 604 | } |
Lightvalve | 14:8e7590227d22 | 605 | |
Lightvalve | 14:8e7590227d22 | 606 | case MODE_FIND_HOME: { |
Lightvalve | 29:69f3f5445d6d | 607 | if (FINDHOME_STAGE == FINDHOME_INIT) { |
hyhoon | 227:699c3e572283 | 608 | REFERENCE_MODE=MODE_REF_NO_ACT; |
Lightvalve | 14:8e7590227d22 | 609 | cnt_findhome = 0; |
Lightvalve | 14:8e7590227d22 | 610 | cnt_vel_findhome = 0; |
Lightvalve | 59:f308b1656d9c | 611 | pos.ref = pos.sen; |
Lightvalve | 59:f308b1656d9c | 612 | vel.ref = 0.0f; |
Lightvalve | 29:69f3f5445d6d | 613 | FINDHOME_STAGE = FINDHOME_GOTOLIMIT; |
Lightvalve | 29:69f3f5445d6d | 614 | } else if (FINDHOME_STAGE == FINDHOME_GOTOLIMIT) { |
Lightvalve | 67:c2812cf26c38 | 615 | int cnt_check_enc = (TMR_FREQ_5k/20); |
Lightvalve | 29:69f3f5445d6d | 616 | if(cnt_findhome%cnt_check_enc == 0) { |
Lightvalve | 29:69f3f5445d6d | 617 | FINDHOME_POSITION = pos.sen; |
Lightvalve | 29:69f3f5445d6d | 618 | FINDHOME_VELOCITY = FINDHOME_POSITION - FINDHOME_POSITION_OLD; |
Lightvalve | 29:69f3f5445d6d | 619 | FINDHOME_POSITION_OLD = FINDHOME_POSITION; |
Lightvalve | 29:69f3f5445d6d | 620 | } |
Lightvalve | 29:69f3f5445d6d | 621 | cnt_findhome++; |
Lightvalve | 14:8e7590227d22 | 622 | |
Lightvalve | 29:69f3f5445d6d | 623 | if (abs(FINDHOME_VELOCITY) <= 1) { |
Lightvalve | 29:69f3f5445d6d | 624 | cnt_vel_findhome = cnt_vel_findhome + 1; |
Lightvalve | 29:69f3f5445d6d | 625 | } else { |
Lightvalve | 29:69f3f5445d6d | 626 | cnt_vel_findhome = 0; |
Lightvalve | 29:69f3f5445d6d | 627 | } |
Lightvalve | 19:23b7c1ad8683 | 628 | |
Lightvalve | 57:f4819de54e7a | 629 | if ((cnt_vel_findhome < 3*TMR_FREQ_5k) && cnt_findhome < 10*TMR_FREQ_5k) { // wait for 3sec |
hyhoon | 226:82a3ca333004 | 630 | if (HOMEPOS_OFFSET > 0) { |
hyhoon | 226:82a3ca333004 | 631 | pos.ref = pos.ref + 12.0f; |
hyhoon | 226:82a3ca333004 | 632 | } else { |
hyhoon | 226:82a3ca333004 | 633 | pos.ref = pos.ref - 12.0f; |
hyhoon | 226:82a3ca333004 | 634 | } |
hyhoon | 226:82a3ca333004 | 635 | |
Lightvalve | 61:bc8c8270f0ab | 636 | |
Lightvalve | 59:f308b1656d9c | 637 | CONTROL_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 59:f308b1656d9c | 638 | alpha_trans = 0.0f; |
Lightvalve | 61:bc8c8270f0ab | 639 | |
Lightvalve | 29:69f3f5445d6d | 640 | } else { |
hyhoon | 226:82a3ca333004 | 641 | ENC_SET((int32_t)((int32_t)HOMEPOS_OFFSET*10)); |
hyhoon | 226:82a3ca333004 | 642 | INIT_REF_POS = (int32_t)((int32_t)HOMEPOS_OFFSET*10); |
Lightvalve | 29:69f3f5445d6d | 643 | REF_POSITION = 0; |
Lightvalve | 29:69f3f5445d6d | 644 | REF_VELOCITY = 0; |
Lightvalve | 29:69f3f5445d6d | 645 | FINDHOME_POSITION = 0; |
Lightvalve | 29:69f3f5445d6d | 646 | FINDHOME_POSITION_OLD = 0; |
Lightvalve | 29:69f3f5445d6d | 647 | FINDHOME_VELOCITY = 0; |
Lightvalve | 29:69f3f5445d6d | 648 | cnt_findhome = 0; |
Lightvalve | 29:69f3f5445d6d | 649 | cnt_vel_findhome = 0; |
Lightvalve | 29:69f3f5445d6d | 650 | FINDHOME_STAGE = FINDHOME_ZEROPOSE; |
Lightvalve | 67:c2812cf26c38 | 651 | |
Lightvalve | 67:c2812cf26c38 | 652 | |
Lightvalve | 67:c2812cf26c38 | 653 | cnt_findhome = 0; |
Lightvalve | 67:c2812cf26c38 | 654 | pos.ref = 0.0f; |
Lightvalve | 67:c2812cf26c38 | 655 | vel.ref = 0.0f; |
Lightvalve | 67:c2812cf26c38 | 656 | pos.ref_home_pos = 0.0f; |
Lightvalve | 67:c2812cf26c38 | 657 | vel.ref_home_pos = 0.0f; |
Lightvalve | 29:69f3f5445d6d | 658 | } |
Lightvalve | 29:69f3f5445d6d | 659 | } else if (FINDHOME_STAGE == FINDHOME_ZEROPOSE) { |
Lightvalve | 29:69f3f5445d6d | 660 | int T_move = 2*TMR_FREQ_5k; |
Lightvalve | 59:f308b1656d9c | 661 | pos.ref = (0.0f - (float)INIT_REF_POS)*0.5f*(1.0f - cos(3.14159f * (float)cnt_findhome / (float)T_move)) + (float)INIT_REF_POS; |
Lightvalve | 59:f308b1656d9c | 662 | vel.ref = 0.0f; |
Lightvalve | 67:c2812cf26c38 | 663 | alpha_trans = 0.0f; |
Lightvalve | 61:bc8c8270f0ab | 664 | |
Lightvalve | 67:c2812cf26c38 | 665 | pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm] |
Lightvalve | 67:c2812cf26c38 | 666 | vel.err = (0.0f - vel.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm/s] |
Lightvalve | 67:c2812cf26c38 | 667 | pos.err_sum += pos.err/(float) TMR_FREQ_5k; //[mm] |
hyhoon | 226:82a3ca333004 | 668 | CONTROL_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 67:c2812cf26c38 | 669 | |
Lightvalve | 29:69f3f5445d6d | 670 | cnt_findhome++; |
Lightvalve | 29:69f3f5445d6d | 671 | if (cnt_findhome >= T_move) { |
Lightvalve | 29:69f3f5445d6d | 672 | cnt_findhome = 0; |
Lightvalve | 30:8d561f16383b | 673 | pos.ref = 0.0f; |
Lightvalve | 30:8d561f16383b | 674 | vel.ref = 0.0f; |
Lightvalve | 30:8d561f16383b | 675 | pos.ref_home_pos = 0.0f; |
Lightvalve | 30:8d561f16383b | 676 | vel.ref_home_pos = 0.0f; |
Lightvalve | 29:69f3f5445d6d | 677 | FINDHOME_STAGE = FINDHOME_INIT; |
Lightvalve | 57:f4819de54e7a | 678 | CONTROL_UTILITY_MODE = MODE_JOINT_CONTROL; |
hyhoon | 227:699c3e572283 | 679 | REFERENCE_MODE=MODE_REF_DIRECT; |
Lightvalve | 29:69f3f5445d6d | 680 | } |
Lightvalve | 13:747daba9cf59 | 681 | } |
Lightvalve | 19:23b7c1ad8683 | 682 | |
Lightvalve | 13:747daba9cf59 | 683 | break; |
Lightvalve | 13:747daba9cf59 | 684 | } |
Lightvalve | 14:8e7590227d22 | 685 | |
Lightvalve | 14:8e7590227d22 | 686 | case MODE_PRESSURE_SENSOR_NULLING: { |
Lightvalve | 13:747daba9cf59 | 687 | // DAC Voltage reference set |
Lightvalve | 14:8e7590227d22 | 688 | if (TMR3_COUNT_PRES_NULL < TMR_FREQ_5k * 2) { |
Lightvalve | 14:8e7590227d22 | 689 | CUR_PRES_A_sum += pres_A.sen; |
Lightvalve | 14:8e7590227d22 | 690 | CUR_PRES_B_sum += pres_B.sen; |
Lightvalve | 14:8e7590227d22 | 691 | |
Lightvalve | 14:8e7590227d22 | 692 | if (TMR3_COUNT_PRES_NULL % 10 == 0) { |
Lightvalve | 30:8d561f16383b | 693 | CUR_PRES_A_mean = CUR_PRES_A_sum / 10.0f; |
Lightvalve | 30:8d561f16383b | 694 | CUR_PRES_B_mean = CUR_PRES_B_sum / 10.0f; |
Lightvalve | 13:747daba9cf59 | 695 | CUR_PRES_A_sum = 0; |
Lightvalve | 13:747daba9cf59 | 696 | CUR_PRES_B_sum = 0; |
Lightvalve | 14:8e7590227d22 | 697 | |
Lightvalve | 38:118df027d851 | 698 | float VREF_NullingGain = 0.0003f; |
Lightvalve | 58:2eade98630e2 | 699 | PRES_A_VREF = PRES_A_VREF + VREF_NullingGain * CUR_PRES_A_mean; |
Lightvalve | 58:2eade98630e2 | 700 | PRES_B_VREF = PRES_B_VREF + VREF_NullingGain * CUR_PRES_B_mean; |
Lightvalve | 14:8e7590227d22 | 701 | |
Lightvalve | 30:8d561f16383b | 702 | if (PRES_A_VREF > 3.3f) PRES_A_VREF = 3.3f; |
Lightvalve | 30:8d561f16383b | 703 | if (PRES_A_VREF < 0.0f) PRES_A_VREF = 0.0f; |
Lightvalve | 30:8d561f16383b | 704 | if (PRES_B_VREF > 3.3f) PRES_B_VREF = 3.3f; |
Lightvalve | 30:8d561f16383b | 705 | if (PRES_B_VREF < 0.0f) PRES_B_VREF = 0.0f; |
Lightvalve | 19:23b7c1ad8683 | 706 | |
hyhoon | 226:82a3ca333004 | 707 | if (DIR_VALVE_ENC > 0) { |
hyhoon | 226:82a3ca333004 | 708 | dac_1 = PRES_A_VREF / 3.3f; |
hyhoon | 226:82a3ca333004 | 709 | dac_2 = PRES_B_VREF / 3.3f; |
hyhoon | 226:82a3ca333004 | 710 | } else { |
hyhoon | 226:82a3ca333004 | 711 | dac_1 = PRES_B_VREF / 3.3f; |
hyhoon | 226:82a3ca333004 | 712 | dac_2 = PRES_A_VREF / 3.3f; |
hyhoon | 226:82a3ca333004 | 713 | } |
Lightvalve | 13:747daba9cf59 | 714 | } |
Lightvalve | 13:747daba9cf59 | 715 | } else { |
Lightvalve | 57:f4819de54e7a | 716 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 14:8e7590227d22 | 717 | TMR3_COUNT_PRES_NULL = 0; |
Lightvalve | 13:747daba9cf59 | 718 | CUR_PRES_A_sum = 0; |
Lightvalve | 13:747daba9cf59 | 719 | CUR_PRES_B_sum = 0; |
Lightvalve | 13:747daba9cf59 | 720 | CUR_PRES_A_mean = 0; |
Lightvalve | 13:747daba9cf59 | 721 | CUR_PRES_B_mean = 0; |
Lightvalve | 14:8e7590227d22 | 722 | |
Lightvalve | 170:42c938a40313 | 723 | spi_eeprom_write(RID_PRES_A_SENSOR_VREF, (int16_t) (PRES_A_VREF * 1000.0f)); |
Lightvalve | 170:42c938a40313 | 724 | spi_eeprom_write(RID_PRES_B_SENSOR_VREF, (int16_t) (PRES_B_VREF * 1000.0f)); |
Lightvalve | 19:23b7c1ad8683 | 725 | |
hyhoon | 226:82a3ca333004 | 726 | if (DIR_VALVE_ENC > 0) { |
hyhoon | 226:82a3ca333004 | 727 | dac_1 = PRES_A_VREF / 3.3f; |
hyhoon | 226:82a3ca333004 | 728 | dac_2 = PRES_B_VREF / 3.3f; |
hyhoon | 226:82a3ca333004 | 729 | } else { |
hyhoon | 226:82a3ca333004 | 730 | dac_1 = PRES_B_VREF / 3.3f; |
hyhoon | 226:82a3ca333004 | 731 | dac_2 = PRES_A_VREF / 3.3f; |
hyhoon | 226:82a3ca333004 | 732 | } |
Lightvalve | 13:747daba9cf59 | 733 | } |
Lightvalve | 14:8e7590227d22 | 734 | TMR3_COUNT_PRES_NULL++; |
Lightvalve | 13:747daba9cf59 | 735 | break; |
Lightvalve | 13:747daba9cf59 | 736 | } |
Lightvalve | 14:8e7590227d22 | 737 | |
hyhoon | 227:699c3e572283 | 738 | case MODE_DDV_DEADZONE_AND_CENTER: { |
hyhoon | 227:699c3e572283 | 739 | if (FINDDZ_STAGE == FINDDZ_INIT) { |
hyhoon | 227:699c3e572283 | 740 | cnt_finddz = 0; |
hyhoon | 227:699c3e572283 | 741 | cnt_vel_finddz = 0; |
hyhoon | 227:699c3e572283 | 742 | temp_pos_ref = pos.sen; |
hyhoon | 227:699c3e572283 | 743 | temp_pos_ref_offset = 0.0f; |
hyhoon | 227:699c3e572283 | 744 | vel.ref = 0.0f; |
hyhoon | 227:699c3e572283 | 745 | FINDDZ_STAGE = FINDDZ_START1; |
hyhoon | 227:699c3e572283 | 746 | flag_finddz = 1; |
hyhoon | 227:699c3e572283 | 747 | FINDDZ_POSITION = pos.sen; |
hyhoon | 227:699c3e572283 | 748 | FINDDZ_POSITION_OLD = FINDDZ_POSITION; |
hyhoon | 227:699c3e572283 | 749 | |
hyhoon | 227:699c3e572283 | 750 | temp_P_GAIN = P_GAIN_JOINT_POSITION; |
hyhoon | 227:699c3e572283 | 751 | temp_I_GAIN = I_GAIN_JOINT_POSITION; |
hyhoon | 227:699c3e572283 | 752 | temp_VELOCITY_COMP_GAIN = VELOCITY_COMP_GAIN; |
Lightvalve | 19:23b7c1ad8683 | 753 | |
hyhoon | 227:699c3e572283 | 754 | P_GAIN_JOINT_POSITION = 1.0f; |
hyhoon | 227:699c3e572283 | 755 | I_GAIN_JOINT_POSITION = 0.0f; |
hyhoon | 227:699c3e572283 | 756 | VELOCITY_COMP_GAIN = 0; |
hyhoon | 227:699c3e572283 | 757 | FLAG_VALVE_DEADZONE = 0; |
hyhoon | 227:699c3e572283 | 758 | alpha_trans = 0.0f; |
hyhoon | 227:699c3e572283 | 759 | REFERENCE_MODE=MODE_REF_NO_ACT; |
Lightvalve | 14:8e7590227d22 | 760 | |
hyhoon | 227:699c3e572283 | 761 | } else if (FINDDZ_STAGE == FINDDZ_START1) { |
hyhoon | 227:699c3e572283 | 762 | cnt_finddz++; |
hyhoon | 227:699c3e572283 | 763 | CONTROL_MODE = MODE_JOINT_CONTROL; |
hyhoon | 227:699c3e572283 | 764 | FINDDZ_POSITION = pos.sen; |
hyhoon | 227:699c3e572283 | 765 | FINDDZ_VELOCITY = FINDDZ_POSITION - FINDDZ_POSITION_OLD; |
hyhoon | 227:699c3e572283 | 766 | FINDDZ_POSITION_OLD = FINDDZ_POSITION; |
Lightvalve | 14:8e7590227d22 | 767 | |
hyhoon | 227:699c3e572283 | 768 | if (temp_P_GAIN > 0.0f) temp_pos_ref_offset = temp_pos_ref_offset - (float)FINDDZ_VELOCITY*temp_P_GAIN; |
hyhoon | 227:699c3e572283 | 769 | else temp_pos_ref_offset = temp_pos_ref_offset - (float)FINDDZ_VELOCITY*20.0f; |
hyhoon | 227:699c3e572283 | 770 | |
hyhoon | 227:699c3e572283 | 771 | pos.ref = temp_pos_ref + temp_pos_ref_offset; |
hyhoon | 227:699c3e572283 | 772 | |
hyhoon | 227:699c3e572283 | 773 | if (cnt_finddz > 10000) FINDDZ_STAGE = FINDDZ_START2; |
Lightvalve | 14:8e7590227d22 | 774 | |
Lightvalve | 14:8e7590227d22 | 775 | |
hyhoon | 227:699c3e572283 | 776 | } else if (FINDDZ_STAGE == FINDDZ_START2) { |
hyhoon | 227:699c3e572283 | 777 | CONTROL_MODE = MODE_JOINT_CONTROL; |
hyhoon | 227:699c3e572283 | 778 | FINDDZ_POSITION = pos.sen; |
hyhoon | 227:699c3e572283 | 779 | FINDDZ_VELOCITY = FINDDZ_POSITION - FINDDZ_POSITION_OLD; |
hyhoon | 227:699c3e572283 | 780 | FINDDZ_POSITION_OLD = FINDDZ_POSITION; |
Lightvalve | 14:8e7590227d22 | 781 | |
hyhoon | 227:699c3e572283 | 782 | if (flag_finddz > 0) { |
hyhoon | 227:699c3e572283 | 783 | temp_pos_ref = temp_pos_ref + 1.0f; |
hyhoon | 227:699c3e572283 | 784 | if (FINDDZ_VELOCITY > 0) { |
hyhoon | 227:699c3e572283 | 785 | cnt_vel_finddz = cnt_vel_finddz + 1; |
hyhoon | 227:699c3e572283 | 786 | } |
hyhoon | 227:699c3e572283 | 787 | if (cnt_vel_finddz > 5) { |
hyhoon | 227:699c3e572283 | 788 | VALVE_DEADZONE_PLUS = (int)(I_REF_fil/mA_PER_pulse); |
hyhoon | 227:699c3e572283 | 789 | // temp_VALVE_DEADZONE_PLUS = temp_VALVE_DEADZONE_PLUS + I_REF_fil/mA_PER_pulse; |
hyhoon | 227:699c3e572283 | 790 | cnt_vel_finddz = 0; |
hyhoon | 227:699c3e572283 | 791 | flag_finddz = - flag_finddz - 1; |
Lightvalve | 13:747daba9cf59 | 792 | } |
Lightvalve | 61:bc8c8270f0ab | 793 | |
hyhoon | 227:699c3e572283 | 794 | } else if (flag_finddz < 0) { |
hyhoon | 227:699c3e572283 | 795 | temp_pos_ref = temp_pos_ref - 1.0f; |
hyhoon | 227:699c3e572283 | 796 | if (FINDDZ_VELOCITY < 0) { |
hyhoon | 227:699c3e572283 | 797 | cnt_vel_finddz = cnt_vel_finddz + 1; |
Lightvalve | 13:747daba9cf59 | 798 | } |
hyhoon | 227:699c3e572283 | 799 | if (cnt_vel_finddz > 5) { |
hyhoon | 227:699c3e572283 | 800 | VALVE_DEADZONE_MINUS = (int)(I_REF_fil/mA_PER_pulse); |
hyhoon | 227:699c3e572283 | 801 | // temp_VALVE_DEADZONE_MINUS = temp_VALVE_DEADZONE_MINUS + I_REF_fil/mA_PER_pulse; |
hyhoon | 227:699c3e572283 | 802 | cnt_vel_finddz = 0; |
hyhoon | 227:699c3e572283 | 803 | flag_finddz = - flag_finddz + 1; |
Lightvalve | 13:747daba9cf59 | 804 | } |
Lightvalve | 13:747daba9cf59 | 805 | } |
hyhoon | 227:699c3e572283 | 806 | if (abs(flag_finddz) >= 6) { |
hyhoon | 227:699c3e572283 | 807 | FINDDZ_STAGE = FINDDZ_STOP; |
hyhoon | 227:699c3e572283 | 808 | // VALVE_DEADZONE_PLUS = (int)(temp_VALVE_DEADZONE_PLUS / (double)flag_finddz * 2.0f); |
hyhoon | 227:699c3e572283 | 809 | // VALVE_DEADZONE_MINUS = (int)(temp_VALVE_DEADZONE_MINUS / (double)flag_finddz * 2.0f); |
hyhoon | 227:699c3e572283 | 810 | spi_eeprom_write(RID_VALVE_DEADZONE_PLUS, VALVE_DEADZONE_PLUS); |
hyhoon | 227:699c3e572283 | 811 | spi_eeprom_write(RID_VALVE_DEADZONE_MINUS, VALVE_DEADZONE_MINUS); |
Lightvalve | 13:747daba9cf59 | 812 | } |
hyhoon | 227:699c3e572283 | 813 | pos.ref = temp_pos_ref + temp_pos_ref_offset; |
Lightvalve | 14:8e7590227d22 | 814 | |
hyhoon | 227:699c3e572283 | 815 | } else if (FINDDZ_STAGE == FINDDZ_STOP) { |
hyhoon | 227:699c3e572283 | 816 | FINDDZ_STAGE = FINDDZ_INIT; |
hyhoon | 227:699c3e572283 | 817 | CONTROL_UTILITY_MODE = MODE_JOINT_CONTROL; |
hyhoon | 227:699c3e572283 | 818 | P_GAIN_JOINT_POSITION = temp_P_GAIN; |
hyhoon | 227:699c3e572283 | 819 | I_GAIN_JOINT_POSITION = temp_I_GAIN; |
hyhoon | 227:699c3e572283 | 820 | VELOCITY_COMP_GAIN = temp_VELOCITY_COMP_GAIN; |
hyhoon | 227:699c3e572283 | 821 | flag_finddz = 0; |
hyhoon | 227:699c3e572283 | 822 | FLAG_VALVE_DEADZONE = 1; |
hyhoon | 227:699c3e572283 | 823 | REFERENCE_MODE=MODE_REF_DIRECT; |
Lightvalve | 13:747daba9cf59 | 824 | } |
Lightvalve | 13:747daba9cf59 | 825 | break; |
Lightvalve | 13:747daba9cf59 | 826 | } |
Lightvalve | 58:2eade98630e2 | 827 | |
Lightvalve | 57:f4819de54e7a | 828 | case MODE_SYSTEM_ID: { |
Lightvalve | 169:645207e160ca | 829 | freq_sysid_Iref = (double) cnt_sysid * DT_TMR3 * 3.0f; |
Lightvalve | 57:f4819de54e7a | 830 | valve_pos.ref = 2500.0f * sin(2.0f * 3.14159f * freq_sysid_Iref * (double) cnt_sysid * DT_TMR3); |
Lightvalve | 57:f4819de54e7a | 831 | CONTROL_MODE = MODE_VALVE_OPEN_LOOP; |
Lightvalve | 57:f4819de54e7a | 832 | cnt_sysid++; |
Lightvalve | 57:f4819de54e7a | 833 | if (freq_sysid_Iref >= 300) { |
Lightvalve | 57:f4819de54e7a | 834 | cnt_sysid = 0; |
Lightvalve | 57:f4819de54e7a | 835 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 57:f4819de54e7a | 836 | } |
Lightvalve | 57:f4819de54e7a | 837 | break; |
Lightvalve | 57:f4819de54e7a | 838 | } |
Lightvalve | 58:2eade98630e2 | 839 | |
Lightvalve | 169:645207e160ca | 840 | case MODE_FREQ_TEST: { |
Lightvalve | 169:645207e160ca | 841 | float valve_pos_ref = 2500.0f * sin(2.0f * 3.141592f * freq_test_valve_ref * (float) cnt_freq_test * DT_TMR3); |
Lightvalve | 169:645207e160ca | 842 | if(valve_pos_ref >= 0) { |
Lightvalve | 169:645207e160ca | 843 | valve_pos.ref = (double)VALVE_CENTER + (double)valve_pos_ref * ((double)VALVE_MAX_POS-(double)VALVE_CENTER)/10000.0f; |
Lightvalve | 169:645207e160ca | 844 | } else { |
Lightvalve | 169:645207e160ca | 845 | valve_pos.ref = (double)VALVE_CENTER - (double)valve_pos_ref * ((double)VALVE_MIN_POS-(double)VALVE_CENTER)/10000.0f; |
Lightvalve | 169:645207e160ca | 846 | } |
Lightvalve | 169:645207e160ca | 847 | ref_array[cnt_freq_test] = valve_pos_ref; |
Lightvalve | 169:645207e160ca | 848 | if(value>=(float) VALVE_CENTER) { |
Lightvalve | 169:645207e160ca | 849 | pos_array[cnt_freq_test] = 10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER); |
Lightvalve | 169:645207e160ca | 850 | } else { |
Lightvalve | 169:645207e160ca | 851 | pos_array[cnt_freq_test] = -10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER); |
Lightvalve | 169:645207e160ca | 852 | } |
Lightvalve | 58:2eade98630e2 | 853 | |
Lightvalve | 169:645207e160ca | 854 | CONTROL_MODE = MODE_VALVE_POSITION_CONTROL; |
Lightvalve | 169:645207e160ca | 855 | cnt_freq_test++; |
Lightvalve | 169:645207e160ca | 856 | if (freq_test_valve_ref * (float) cnt_freq_test * DT_TMR3 > 2) { |
Lightvalve | 169:645207e160ca | 857 | buffer_data_size = cnt_freq_test; |
Lightvalve | 169:645207e160ca | 858 | cnt_freq_test = 0; |
Lightvalve | 169:645207e160ca | 859 | cnt_send_buffer = 0; |
Lightvalve | 169:645207e160ca | 860 | freq_test_valve_ref = freq_test_valve_ref * 1.05f; |
Lightvalve | 169:645207e160ca | 861 | if (freq_test_valve_ref >= 400) { |
Lightvalve | 169:645207e160ca | 862 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 169:645207e160ca | 863 | CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 169:645207e160ca | 864 | CAN_TX_PWM((int16_t) (1)); //1300 |
Lightvalve | 169:645207e160ca | 865 | } |
Lightvalve | 169:645207e160ca | 866 | CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 169:645207e160ca | 867 | CONTROL_UTILITY_MODE = MODE_SEND_OVER; |
Lightvalve | 170:42c938a40313 | 868 | |
Lightvalve | 169:645207e160ca | 869 | } |
Lightvalve | 169:645207e160ca | 870 | break; |
Lightvalve | 169:645207e160ca | 871 | } |
Lightvalve | 170:42c938a40313 | 872 | |
Lightvalve | 169:645207e160ca | 873 | case MODE_STEP_TEST: { |
Lightvalve | 169:645207e160ca | 874 | float valve_pos_ref = 0.0f; |
Lightvalve | 170:42c938a40313 | 875 | if (cnt_step_test < (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 169:645207e160ca | 876 | valve_pos_ref = 0.0f; |
Lightvalve | 170:42c938a40313 | 877 | } else { |
Lightvalve | 169:645207e160ca | 878 | valve_pos_ref = 10000.0f; |
Lightvalve | 169:645207e160ca | 879 | } |
Lightvalve | 169:645207e160ca | 880 | if(valve_pos_ref >= 0) { |
Lightvalve | 169:645207e160ca | 881 | valve_pos.ref = (double)VALVE_CENTER + (double)valve_pos_ref * ((double)VALVE_MAX_POS-(double)VALVE_CENTER)/10000.0f; |
Lightvalve | 169:645207e160ca | 882 | } else { |
Lightvalve | 169:645207e160ca | 883 | valve_pos.ref = (double)VALVE_CENTER - (double)valve_pos_ref * ((double)VALVE_MIN_POS-(double)VALVE_CENTER)/10000.0f; |
Lightvalve | 169:645207e160ca | 884 | } |
Lightvalve | 169:645207e160ca | 885 | ref_array[cnt_step_test] = valve_pos_ref; |
Lightvalve | 169:645207e160ca | 886 | if(value>=(float) VALVE_CENTER) { |
Lightvalve | 169:645207e160ca | 887 | pos_array[cnt_step_test] = 10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER); |
Lightvalve | 169:645207e160ca | 888 | } else { |
Lightvalve | 169:645207e160ca | 889 | pos_array[cnt_step_test] = -10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER); |
Lightvalve | 169:645207e160ca | 890 | } |
Lightvalve | 169:645207e160ca | 891 | |
Lightvalve | 169:645207e160ca | 892 | CONTROL_MODE = MODE_VALVE_POSITION_CONTROL; |
Lightvalve | 169:645207e160ca | 893 | cnt_step_test++; |
Lightvalve | 170:42c938a40313 | 894 | if (cnt_step_test > (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 169:645207e160ca | 895 | buffer_data_size = cnt_step_test; |
Lightvalve | 169:645207e160ca | 896 | cnt_step_test = 0; |
Lightvalve | 169:645207e160ca | 897 | cnt_send_buffer = 0; |
Lightvalve | 169:645207e160ca | 898 | CONTROL_UTILITY_MODE = MODE_SEND_OVER; |
Lightvalve | 169:645207e160ca | 899 | CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 169:645207e160ca | 900 | } |
Lightvalve | 170:42c938a40313 | 901 | |
Lightvalve | 169:645207e160ca | 902 | break; |
Lightvalve | 169:645207e160ca | 903 | } |
Lightvalve | 57:f4819de54e7a | 904 | |
Lightvalve | 57:f4819de54e7a | 905 | default: |
Lightvalve | 57:f4819de54e7a | 906 | break; |
Lightvalve | 57:f4819de54e7a | 907 | } |
Lightvalve | 57:f4819de54e7a | 908 | |
Lightvalve | 224:985dba42f261 | 909 | |
Lightvalve | 224:985dba42f261 | 910 | |
Lightvalve | 57:f4819de54e7a | 911 | // CONTROL MODE ------------------------------------------------------------ |
Lightvalve | 57:f4819de54e7a | 912 | switch (CONTROL_MODE) { |
Lightvalve | 57:f4819de54e7a | 913 | case MODE_NO_ACT: { |
Lightvalve | 57:f4819de54e7a | 914 | V_out = 0.0f; |
Lightvalve | 57:f4819de54e7a | 915 | break; |
Lightvalve | 57:f4819de54e7a | 916 | } |
Lightvalve | 57:f4819de54e7a | 917 | |
Lightvalve | 57:f4819de54e7a | 918 | case MODE_VALVE_POSITION_CONTROL: { |
Lightvalve | 67:c2812cf26c38 | 919 | if (OPERATING_MODE == 5) { //SW Valve |
Lightvalve | 57:f4819de54e7a | 920 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 59:f308b1656d9c | 921 | V_out = Vout.ref; |
Lightvalve | 67:c2812cf26c38 | 922 | } else if (CURRENT_CONTROL_MODE == 0) { //PWM |
Lightvalve | 67:c2812cf26c38 | 923 | V_out = valve_pos.ref; |
Lightvalve | 169:645207e160ca | 924 | } else { |
hyhoon | 227:699c3e572283 | 925 | I_REF = valve_pos.ref * 0.001f; |
Lightvalve | 57:f4819de54e7a | 926 | } |
Lightvalve | 57:f4819de54e7a | 927 | break; |
Lightvalve | 57:f4819de54e7a | 928 | } |
Lightvalve | 57:f4819de54e7a | 929 | |
Lightvalve | 57:f4819de54e7a | 930 | case MODE_JOINT_CONTROL: { |
Lightvalve | 169:645207e160ca | 931 | |
Lightvalve | 57:f4819de54e7a | 932 | double torq_ref = 0.0f; |
Lightvalve | 57:f4819de54e7a | 933 | pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm] |
Lightvalve | 67:c2812cf26c38 | 934 | vel.err = (0.0f - vel.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm/s] |
Lightvalve | 57:f4819de54e7a | 935 | pos.err_sum += pos.err/(float) TMR_FREQ_5k; //[mm] |
Lightvalve | 169:645207e160ca | 936 | |
Lightvalve | 67:c2812cf26c38 | 937 | //K & D Low Pass Filter |
Lightvalve | 67:c2812cf26c38 | 938 | float alpha_K_D = 1.0f/(1.0f + 5000.0f/(2.0f*3.14f*30.0f)); // f_cutoff : 30Hz |
Lightvalve | 67:c2812cf26c38 | 939 | K_LPF = K_LPF*(1.0f-alpha_K_D)+K_SPRING*(alpha_K_D); |
Lightvalve | 67:c2812cf26c38 | 940 | D_LPF = D_LPF*(1.0f-alpha_K_D)+D_DAMPER*(alpha_K_D); |
Lightvalve | 169:645207e160ca | 941 | |
Lightvalve | 224:985dba42f261 | 942 | torq_ref = torq.ref + K_LPF * pos.err - D_LPF * vel.sen / ENC_PULSE_PER_POSITION; //[N] |
Lightvalve | 57:f4819de54e7a | 943 | |
Lightvalve | 57:f4819de54e7a | 944 | // torque feedback |
Lightvalve | 67:c2812cf26c38 | 945 | torq.err = torq_ref - torq.sen; //[N] |
Lightvalve | 57:f4819de54e7a | 946 | torq.err_sum += torq.err/(float) TMR_FREQ_5k; //[N] |
Lightvalve | 57:f4819de54e7a | 947 | |
Lightvalve | 57:f4819de54e7a | 948 | if (((OPERATING_MODE&0b110)>>1) == 0 || ((OPERATING_MODE&0b110)>>1) == 1) { |
Lightvalve | 58:2eade98630e2 | 949 | |
Lightvalve | 57:f4819de54e7a | 950 | double I_REF_POS = 0.0f; |
Lightvalve | 57:f4819de54e7a | 951 | double I_REF_FORCE_FB = 0.0f; // I_REF by Force Feedback |
Lightvalve | 57:f4819de54e7a | 952 | double I_REF_VC = 0.0f; // I_REF for velocity compensation |
Lightvalve | 57:f4819de54e7a | 953 | |
Lightvalve | 57:f4819de54e7a | 954 | double temp_vel_pos = 0.0f; |
Lightvalve | 57:f4819de54e7a | 955 | double temp_vel_torq = 0.0f; |
Lightvalve | 57:f4819de54e7a | 956 | double wn_Pos = 2.0f * PI * 5.0f; // f_cut : 5Hz Position Control |
Lightvalve | 58:2eade98630e2 | 957 | |
Lightvalve | 69:3995ffeaa786 | 958 | if ((OPERATING_MODE & 0x01) == 0) { // Rotary Mode |
Lightvalve | 57:f4819de54e7a | 959 | temp_vel_pos = (0.01f * (double) P_GAIN_JOINT_POSITION * wn_Pos * pos.err + 0.01f * (double) I_GAIN_JOINT_POSITION * wn_Pos * pos.err_sum + 0.01f * (double) VELOCITY_COMP_GAIN * vel.ref / ENC_PULSE_PER_POSITION) * PI / 180.0f; // rad/s |
Lightvalve | 57:f4819de54e7a | 960 | // L when P-gain = 100, f_cut = 10Hz L feedforward velocity |
Lightvalve | 69:3995ffeaa786 | 961 | } else if ((OPERATING_MODE & 0x01) == 1) { |
Lightvalve | 57:f4819de54e7a | 962 | temp_vel_pos = (0.01f * (double) P_GAIN_JOINT_POSITION * wn_Pos * pos.err + 0.01f * (double) I_GAIN_JOINT_POSITION * wn_Pos * pos.err_sum + 0.01f * (double) VELOCITY_COMP_GAIN * vel.ref / ENC_PULSE_PER_POSITION); // mm/s |
Lightvalve | 57:f4819de54e7a | 963 | // L when P-gain = 100, f_cut = 10Hz L feedforward velocity |
Lightvalve | 57:f4819de54e7a | 964 | } |
Lightvalve | 57:f4819de54e7a | 965 | if (temp_vel_pos > 0.0f) I_REF_POS = temp_vel_pos * ((double) PISTON_AREA_A * 0.00006f / (K_v * sqrt(2.0f * alpha3 / (alpha3 + 1.0f)))); |
Lightvalve | 57:f4819de54e7a | 966 | else I_REF_POS = temp_vel_pos * ((double) PISTON_AREA_B * 0.00006f / (K_v * sqrt(2.0f / (alpha3 + 1.0f)))); |
Lightvalve | 57:f4819de54e7a | 967 | |
Lightvalve | 57:f4819de54e7a | 968 | // velocity compensation for torque control |
Lightvalve | 69:3995ffeaa786 | 969 | if ((OPERATING_MODE & 0x01) == 0) { // Rotary Mode |
Lightvalve | 57:f4819de54e7a | 970 | I_REF_FORCE_FB = 0.001f * ((double) P_GAIN_JOINT_TORQUE * torq.err + (double) I_GAIN_JOINT_TORQUE * torq.err_sum); |
Lightvalve | 57:f4819de54e7a | 971 | // temp_vel_torq = (0.01 * (double) VELOCITY_COMP_GAIN * (double) CUR_VELOCITY / (double) ENC_PULSE_PER_POSITION) * PI / 180.0; // rad/s |
Lightvalve | 57:f4819de54e7a | 972 | temp_vel_torq = (0.01f * (double) VELOCITY_COMP_GAIN * vel.ref / (double) ENC_PULSE_PER_POSITION) * PI / 180.0f; // rad/s |
Lightvalve | 57:f4819de54e7a | 973 | // L feedforward velocity |
Lightvalve | 69:3995ffeaa786 | 974 | } else if ((OPERATING_MODE & 0x01) == 1) { |
Lightvalve | 57:f4819de54e7a | 975 | I_REF_FORCE_FB = 0.001f * 0.01f*((double) P_GAIN_JOINT_TORQUE * torq.err + (double) I_GAIN_JOINT_TORQUE * torq.err_sum); // Linear Actuators are more sensitive. |
Lightvalve | 57:f4819de54e7a | 976 | // temp_vel_torq = (0.01 * (double) VELOCITY_COMP_GAIN * (double) CUR_VELOCITY / (double) ENC_PULSE_PER_POSITION); // mm/s |
Lightvalve | 57:f4819de54e7a | 977 | temp_vel_torq = (0.01f * (double) VELOCITY_COMP_GAIN * vel.ref / (double) ENC_PULSE_PER_POSITION); // mm/s |
Lightvalve | 57:f4819de54e7a | 978 | // L feedforward velocity |
Lightvalve | 57:f4819de54e7a | 979 | } |
Lightvalve | 57:f4819de54e7a | 980 | if (temp_vel_torq > 0.0f) I_REF_VC = temp_vel_torq * ((double) PISTON_AREA_A * 0.00006f / (K_v * sqrt(2.0f * alpha3 / (alpha3 + 1.0f)))); |
Lightvalve | 57:f4819de54e7a | 981 | else I_REF_VC = temp_vel_torq * ((double) PISTON_AREA_B * 0.00006f / (K_v * sqrt(2.0f / (alpha3 + 1.0f)))); |
Lightvalve | 57:f4819de54e7a | 982 | // L velocity(rad/s or mm/s) >> I_ref(mA) |
Lightvalve | 57:f4819de54e7a | 983 | // Ref_Joint_FT_dot = (Ref_Joint_FT_Nm - Ref_Joint_FT_Nm_old) / TMR_DT_5k; |
Lightvalve | 57:f4819de54e7a | 984 | // Ref_Joint_FT_Nm_old = Ref_Joint_FT_Nm; |
Lightvalve | 57:f4819de54e7a | 985 | |
Lightvalve | 57:f4819de54e7a | 986 | I_REF = (1.0f - alpha_trans) * I_REF_POS + alpha_trans * (I_REF_VC + I_REF_FORCE_FB); |
Lightvalve | 169:645207e160ca | 987 | |
Lightvalve | 67:c2812cf26c38 | 988 | // Anti-windup for FT |
Lightvalve | 67:c2812cf26c38 | 989 | if (I_GAIN_JOINT_TORQUE != 0) { |
Lightvalve | 67:c2812cf26c38 | 990 | double I_MAX = 10.0f; // Maximum Current : 10mV |
Lightvalve | 67:c2812cf26c38 | 991 | double Ka = 2.0f / ((double) I_GAIN_JOINT_TORQUE * 0.001f); |
Lightvalve | 67:c2812cf26c38 | 992 | if (I_REF > I_MAX) { |
Lightvalve | 67:c2812cf26c38 | 993 | double I_rem = I_REF - I_MAX; |
Lightvalve | 67:c2812cf26c38 | 994 | I_rem = Ka*I_rem; |
Lightvalve | 67:c2812cf26c38 | 995 | I_REF = I_MAX; |
Lightvalve | 67:c2812cf26c38 | 996 | torq.err_sum = torq.err_sum - I_rem /(float) TMR_FREQ_5k; |
Lightvalve | 67:c2812cf26c38 | 997 | } else if (I_REF < -I_MAX) { |
Lightvalve | 67:c2812cf26c38 | 998 | double I_rem = I_REF - (-I_MAX); |
Lightvalve | 67:c2812cf26c38 | 999 | I_rem = Ka*I_rem; |
Lightvalve | 67:c2812cf26c38 | 1000 | I_REF = -I_MAX; |
Lightvalve | 67:c2812cf26c38 | 1001 | torq.err_sum = torq.err_sum - I_rem /(float) TMR_FREQ_5k; |
Lightvalve | 67:c2812cf26c38 | 1002 | } |
Lightvalve | 67:c2812cf26c38 | 1003 | } |
Lightvalve | 57:f4819de54e7a | 1004 | |
Lightvalve | 57:f4819de54e7a | 1005 | } else { |
Lightvalve | 57:f4819de54e7a | 1006 | float VALVE_POS_RAW_FORCE_FB = 0.0f; |
Lightvalve | 72:3436ce769b1e | 1007 | float VALVE_POS_RAW_FORCE_FF = 0.0f; |
Lightvalve | 72:3436ce769b1e | 1008 | float VALVE_POS_RAW = 0.0f; |
Lightvalve | 169:645207e160ca | 1009 | |
Lightvalve | 209:ebc69d6ee6f1 | 1010 | VALVE_POS_RAW_FORCE_FB = alpha_trans*(((float) P_GAIN_JOINT_TORQUE * torq.err + (float) I_GAIN_JOINT_TORQUE * torq.err_sum + (float) D_GAIN_JOINT_TORQUE * (torq.ref_diff - torq_dot.sen)) * 0.01f + DDV_JOINT_POS_FF(vel.sen))+ (1.0f-alpha_trans) * (P_GAIN_JOINT_POSITION * 0.01f * pos.err + DDV_JOINT_POS_FF(vel.ref)); |
Lightvalve | 57:f4819de54e7a | 1011 | |
Lightvalve | 72:3436ce769b1e | 1012 | VALVE_POS_RAW_FORCE_FF = P_GAIN_JOINT_TORQUE_FF * torq_ref * 0.001f + D_GAIN_JOINT_TORQUE_FF * (torq_ref - torq_ref_past) * 0.0001f; |
Lightvalve | 169:645207e160ca | 1013 | |
Lightvalve | 72:3436ce769b1e | 1014 | VALVE_POS_RAW = VALVE_POS_RAW_FORCE_FB + VALVE_POS_RAW_FORCE_FF; |
Lightvalve | 169:645207e160ca | 1015 | |
Lightvalve | 169:645207e160ca | 1016 | |
Lightvalve | 72:3436ce769b1e | 1017 | if (VALVE_POS_RAW >= 0) { |
Lightvalve | 72:3436ce769b1e | 1018 | valve_pos.ref = VALVE_POS_RAW + VALVE_DEADZONE_PLUS; |
Lightvalve | 57:f4819de54e7a | 1019 | } else { |
Lightvalve | 72:3436ce769b1e | 1020 | valve_pos.ref = VALVE_POS_RAW + VALVE_DEADZONE_MINUS; |
Lightvalve | 57:f4819de54e7a | 1021 | } |
Lightvalve | 57:f4819de54e7a | 1022 | |
Lightvalve | 57:f4819de54e7a | 1023 | if(I_GAIN_JOINT_TORQUE != 0) { |
Lightvalve | 139:15621998925b | 1024 | double Ka = 2.0f / (double) I_GAIN_JOINT_TORQUE * 100.0f; |
Lightvalve | 57:f4819de54e7a | 1025 | if(valve_pos.ref>VALVE_MAX_POS) { |
Lightvalve | 57:f4819de54e7a | 1026 | double valve_pos_rem = valve_pos.ref - VALVE_MAX_POS; |
Lightvalve | 57:f4819de54e7a | 1027 | valve_pos_rem = valve_pos_rem * Ka; |
Lightvalve | 57:f4819de54e7a | 1028 | valve_pos.ref = VALVE_MAX_POS; |
Lightvalve | 57:f4819de54e7a | 1029 | torq.err_sum = torq.err_sum - valve_pos_rem/(float) TMR_FREQ_5k; |
Lightvalve | 57:f4819de54e7a | 1030 | } else if(valve_pos.ref < VALVE_MIN_POS) { |
Lightvalve | 57:f4819de54e7a | 1031 | double valve_pos_rem = valve_pos.ref - VALVE_MIN_POS; |
Lightvalve | 57:f4819de54e7a | 1032 | valve_pos_rem = valve_pos_rem * Ka; |
Lightvalve | 57:f4819de54e7a | 1033 | valve_pos.ref = VALVE_MIN_POS; |
Lightvalve | 57:f4819de54e7a | 1034 | torq.err_sum = torq.err_sum - valve_pos_rem/(float) TMR_FREQ_5k; |
Lightvalve | 57:f4819de54e7a | 1035 | } |
Lightvalve | 57:f4819de54e7a | 1036 | } |
Lightvalve | 61:bc8c8270f0ab | 1037 | |
Lightvalve | 57:f4819de54e7a | 1038 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 60:64181f1d3e60 | 1039 | V_out = (float) Vout.ref; |
Lightvalve | 57:f4819de54e7a | 1040 | } |
Lightvalve | 72:3436ce769b1e | 1041 | torq_ref_past = torq_ref; |
Lightvalve | 57:f4819de54e7a | 1042 | break; |
Lightvalve | 57:f4819de54e7a | 1043 | } |
Lightvalve | 58:2eade98630e2 | 1044 | |
Lightvalve | 57:f4819de54e7a | 1045 | case MODE_VALVE_OPEN_LOOP: { |
Lightvalve | 57:f4819de54e7a | 1046 | V_out = (float) Vout.ref; |
Lightvalve | 57:f4819de54e7a | 1047 | break; |
Lightvalve | 57:f4819de54e7a | 1048 | } |
Lightvalve | 169:645207e160ca | 1049 | |
Lightvalve | 138:a843f32ced33 | 1050 | case MODE_JOINT_ADAPTIVE_BACKSTEPPING: { |
Lightvalve | 169:645207e160ca | 1051 | |
Lightvalve | 169:645207e160ca | 1052 | |
Lightvalve | 139:15621998925b | 1053 | 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 |
Lightvalve | 139:15621998925b | 1054 | 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 |
Lightvalve | 218:066030f7951f | 1055 | |
Lightvalve | 170:42c938a40313 | 1056 | V_adapt = 1.0f / (1.0f/Va + 1.0f/Vb); //initial 0.0000053f |
Lightvalve | 169:645207e160ca | 1057 | |
Lightvalve | 218:066030f7951f | 1058 | //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 |
Lightvalve | 218:066030f7951f | 1059 | float f3_hat = -a_hat * vel.sen/(float)(ENC_PULSE_PER_POSITION)*0.001f; // unit : N/s //xdot=10mm/s일때 -137076 |
Lightvalve | 169:645207e160ca | 1060 | |
Lightvalve | 138:a843f32ced33 | 1061 | float g3_prime = 0.0f; |
Lightvalve | 169:645207e160ca | 1062 | if (torq.sen > Amm*(Ps-Pt)*0.000001f) { |
Lightvalve | 138:a843f32ced33 | 1063 | g3_prime = 1.0f; |
Lightvalve | 138:a843f32ced33 | 1064 | } else if (torq.sen < -Amm*(Ps-Pt)*0.000001f) { |
Lightvalve | 138:a843f32ced33 | 1065 | g3_prime = -1.0f; |
Lightvalve | 138:a843f32ced33 | 1066 | } else { |
Lightvalve | 138:a843f32ced33 | 1067 | if ((value-VALVE_CENTER) > 0) { |
Lightvalve | 169:645207e160ca | 1068 | g3_prime = sqrt(Ps-Pt-torq.sen/Amm*1000000.0f); |
Lightvalve | 169:645207e160ca | 1069 | // g3_prime = sqrt(Ps-Pt); |
Lightvalve | 138:a843f32ced33 | 1070 | } else { |
Lightvalve | 138:a843f32ced33 | 1071 | g3_prime = sqrt(Ps-Pt+torq.sen/Amm*1000000.0f); |
Lightvalve | 139:15621998925b | 1072 | // g3_prime = sqrt(Ps-Pt); |
Lightvalve | 138:a843f32ced33 | 1073 | } |
Lightvalve | 138:a843f32ced33 | 1074 | } |
Lightvalve | 138:a843f32ced33 | 1075 | float tau = 0.01f; |
Lightvalve | 142:43026242815a | 1076 | float K_valve = 0.0004f; |
Lightvalve | 169:645207e160ca | 1077 | |
Lightvalve | 138:a843f32ced33 | 1078 | float x_v = 0.0f; //x_v : -1~1 |
Lightvalve | 138:a843f32ced33 | 1079 | if(value>=VALVE_CENTER) { |
Lightvalve | 138:a843f32ced33 | 1080 | x_v = 1.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER); |
Lightvalve | 138:a843f32ced33 | 1081 | } else { |
Lightvalve | 138:a843f32ced33 | 1082 | x_v = -1.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER); |
Lightvalve | 138:a843f32ced33 | 1083 | } |
Lightvalve | 138:a843f32ced33 | 1084 | float f4 = -x_v/tau; |
Lightvalve | 138:a843f32ced33 | 1085 | float g4 = K_valve/tau; |
Lightvalve | 169:645207e160ca | 1086 | |
Lightvalve | 139:15621998925b | 1087 | float torq_ref_dot = torq.ref_diff * 500.0f; |
Lightvalve | 169:645207e160ca | 1088 | |
Lightvalve | 138:a843f32ced33 | 1089 | pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm] |
Lightvalve | 138:a843f32ced33 | 1090 | vel.err = (0.0f - vel.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm/s] |
Lightvalve | 138:a843f32ced33 | 1091 | pos.err_sum += pos.err/(float) TMR_FREQ_5k; //[mm] |
Lightvalve | 169:645207e160ca | 1092 | |
Lightvalve | 138:a843f32ced33 | 1093 | torq.err = torq.ref - torq.sen; //[N] |
Lightvalve | 138:a843f32ced33 | 1094 | torq.err_sum += torq.err/(float) TMR_FREQ_5k; //[N] |
Lightvalve | 169:645207e160ca | 1095 | |
Lightvalve | 212:ec41f1449ef9 | 1096 | float k3 = 2000.0f; //2000 //20000 |
Lightvalve | 142:43026242815a | 1097 | float k4 = 10.0f; |
Lightvalve | 142:43026242815a | 1098 | float rho3 = 3.2f; |
Lightvalve | 212:ec41f1449ef9 | 1099 | float rho4 = 10000000.0f; //25000000.0f; |
Lightvalve | 218:066030f7951f | 1100 | float x_4_des = (-f3_hat + torq_ref_dot - k3*(-torq.err))/(gamma_hat*g3_prime); |
Lightvalve | 138:a843f32ced33 | 1101 | if (x_4_des > 1) x_4_des = 1; |
Lightvalve | 138:a843f32ced33 | 1102 | else if (x_4_des < -1) x_4_des = -1; |
Lightvalve | 169:645207e160ca | 1103 | |
Lightvalve | 139:15621998925b | 1104 | if (x_4_des > 0) { |
Lightvalve | 139:15621998925b | 1105 | valve_pos.ref = x_4_des * (float)(VALVE_MAX_POS - VALVE_CENTER) + (float) VALVE_CENTER; |
Lightvalve | 139:15621998925b | 1106 | } else { |
Lightvalve | 139:15621998925b | 1107 | valve_pos.ref = x_4_des * (float)(VALVE_CENTER - VALVE_MIN_POS) + (float) VALVE_CENTER; |
Lightvalve | 139:15621998925b | 1108 | } |
Lightvalve | 169:645207e160ca | 1109 | |
Lightvalve | 138:a843f32ced33 | 1110 | float x_4_des_dot = (x_4_des - x_4_des_old)*(float) TMR_FREQ_5k; |
Lightvalve | 138:a843f32ced33 | 1111 | x_4_des_old = x_4_des; |
Lightvalve | 139:15621998925b | 1112 | V_out = (-f4 + x_4_des_dot - k4*(x_v-x_4_des)- rho3/rho4*gamma_hat*g3_prime*(-torq.err))/g4; |
Lightvalve | 218:066030f7951f | 1113 | |
Lightvalve | 218:066030f7951f | 1114 | float rho_a = 0.00001f; |
Lightvalve | 218:066030f7951f | 1115 | float a_hat_dot = -rho3/rho_a*vel.sen/(float)(ENC_PULSE_PER_POSITION)*0.001f*(-torq.err); |
Lightvalve | 218:066030f7951f | 1116 | a_hat = a_hat + a_hat_dot / (float) TMR_FREQ_5k; |
Lightvalve | 218:066030f7951f | 1117 | |
Lightvalve | 218:066030f7951f | 1118 | if(a_hat > -3000000.0f) a_hat = -3000000.0f; |
Lightvalve | 218:066030f7951f | 1119 | else if(a_hat < -30000000.0f) a_hat = -30000000.0f; |
Lightvalve | 218:066030f7951f | 1120 | |
Lightvalve | 169:645207e160ca | 1121 | break; |
Lightvalve | 138:a843f32ced33 | 1122 | } |
Lightvalve | 171:bfc1fd2629d8 | 1123 | |
Lightvalve | 12:6f2531038ea4 | 1124 | default: |
Lightvalve | 12:6f2531038ea4 | 1125 | break; |
Lightvalve | 12:6f2531038ea4 | 1126 | } |
Lightvalve | 14:8e7590227d22 | 1127 | |
Lightvalve | 57:f4819de54e7a | 1128 | |
Lightvalve | 57:f4819de54e7a | 1129 | if (((OPERATING_MODE&0b110)>>1) == 0 || ((OPERATING_MODE&0b110)>>1) == 1) { //Moog Valve or KNR Valve |
Lightvalve | 58:2eade98630e2 | 1130 | |
Lightvalve | 57:f4819de54e7a | 1131 | //////////////////////////////////////////////////////////////////////////// |
Lightvalve | 57:f4819de54e7a | 1132 | //////////////////////////// CURRENT CONTROL ////////////////////////////// |
Lightvalve | 57:f4819de54e7a | 1133 | //////////////////////////////////////////////////////////////////////////// |
Lightvalve | 57:f4819de54e7a | 1134 | if (CURRENT_CONTROL_MODE) { |
Lightvalve | 67:c2812cf26c38 | 1135 | double alpha_update_Iref = 1.0f / (1.0f + 5000.0f / (2.0f * 3.14f * 300.0f)); // f_cutoff : 500Hz |
Lightvalve | 57:f4819de54e7a | 1136 | I_REF_fil = (1.0f - alpha_update_Iref) * I_REF_fil + alpha_update_Iref*I_REF; |
Lightvalve | 57:f4819de54e7a | 1137 | |
hyhoon | 226:82a3ca333004 | 1138 | if (FLAG_VALVE_DEADZONE) { |
hyhoon | 226:82a3ca333004 | 1139 | if (I_REF_fil > 0.0f) I_REF_fil_DZ = I_REF_fil + (double)VALVE_DEADZONE_PLUS*mA_PER_pulse; // unit: mA |
hyhoon | 226:82a3ca333004 | 1140 | else if (I_REF_fil < 0.0f) I_REF_fil_DZ = I_REF_fil + (double)VALVE_DEADZONE_MINUS*mA_PER_pulse; // unit: mA |
hyhoon | 226:82a3ca333004 | 1141 | else I_REF_fil_DZ = I_REF_fil + (double)(VALVE_DEADZONE_PLUS+VALVE_DEADZONE_MINUS)/2.0f*mA_PER_pulse; // unit: mA |
hyhoon | 226:82a3ca333004 | 1142 | } else { |
hyhoon | 226:82a3ca333004 | 1143 | I_REF_fil_DZ = I_REF_fil; |
hyhoon | 226:82a3ca333004 | 1144 | } |
hyhoon | 226:82a3ca333004 | 1145 | I_ERR = I_REF_fil_DZ - (double)cur.sen; |
Lightvalve | 67:c2812cf26c38 | 1146 | I_ERR_INT = I_ERR_INT + (I_ERR) * 0.0002f; |
Lightvalve | 57:f4819de54e7a | 1147 | |
Lightvalve | 57:f4819de54e7a | 1148 | |
Lightvalve | 57:f4819de54e7a | 1149 | // Moog Valve Current Control Gain |
Lightvalve | 67:c2812cf26c38 | 1150 | double R_model = 500.0f; // ohm |
Lightvalve | 57:f4819de54e7a | 1151 | double L_model = 1.2f; |
Lightvalve | 57:f4819de54e7a | 1152 | double w0 = 2.0f * 3.14f * 150.0f; |
Lightvalve | 57:f4819de54e7a | 1153 | double KP_I = 0.1f * L_model*w0; |
Lightvalve | 57:f4819de54e7a | 1154 | double KI_I = 0.1f * R_model*w0; |
Lightvalve | 57:f4819de54e7a | 1155 | |
Lightvalve | 57:f4819de54e7a | 1156 | // KNR Valve Current Control Gain |
Lightvalve | 57:f4819de54e7a | 1157 | if (((OPERATING_MODE & 0b110)>>1) == 1) { // KNR Valve |
Lightvalve | 57:f4819de54e7a | 1158 | R_model = 163.0f; // ohm |
Lightvalve | 57:f4819de54e7a | 1159 | L_model = 1.0f; |
Lightvalve | 57:f4819de54e7a | 1160 | w0 = 2.0f * 3.14f * 80.0f; |
Lightvalve | 57:f4819de54e7a | 1161 | KP_I = 1.0f * L_model*w0; |
Lightvalve | 57:f4819de54e7a | 1162 | KI_I = 0.08f * R_model*w0; |
Lightvalve | 57:f4819de54e7a | 1163 | } |
Lightvalve | 57:f4819de54e7a | 1164 | |
Lightvalve | 57:f4819de54e7a | 1165 | double FF_gain = 1.0f; |
Lightvalve | 57:f4819de54e7a | 1166 | |
Lightvalve | 67:c2812cf26c38 | 1167 | VALVE_PWM_RAW = KP_I * 2.0f * I_ERR + KI_I * 2.0f* I_ERR_INT; |
hyhoon | 226:82a3ca333004 | 1168 | I_REF_fil_diff = I_REF_fil_DZ - I_REF_fil_old; |
hyhoon | 226:82a3ca333004 | 1169 | I_REF_fil_old = I_REF_fil_DZ; |
Lightvalve | 67:c2812cf26c38 | 1170 | // 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 | 1171 | VALVE_PWM_RAW = VALVE_PWM_RAW + FF_gain * (R_model * I_REF_fil_DZ); // Unit : mV |
Lightvalve | 67:c2812cf26c38 | 1172 | double V_MAX = 12000.0f; // Maximum Voltage : 12V = 12000mV |
Lightvalve | 57:f4819de54e7a | 1173 | |
Lightvalve | 57:f4819de54e7a | 1174 | double Ka = 3.0f / KP_I; |
Lightvalve | 57:f4819de54e7a | 1175 | if (VALVE_PWM_RAW > V_MAX) { |
Lightvalve | 57:f4819de54e7a | 1176 | V_rem = VALVE_PWM_RAW - V_MAX; |
Lightvalve | 57:f4819de54e7a | 1177 | V_rem = Ka*V_rem; |
Lightvalve | 57:f4819de54e7a | 1178 | VALVE_PWM_RAW = V_MAX; |
Lightvalve | 67:c2812cf26c38 | 1179 | I_ERR_INT = I_ERR_INT - V_rem * 0.0002f; |
Lightvalve | 57:f4819de54e7a | 1180 | } else if (VALVE_PWM_RAW < -V_MAX) { |
Lightvalve | 57:f4819de54e7a | 1181 | V_rem = VALVE_PWM_RAW - (-V_MAX); |
Lightvalve | 57:f4819de54e7a | 1182 | V_rem = Ka*V_rem; |
Lightvalve | 57:f4819de54e7a | 1183 | VALVE_PWM_RAW = -V_MAX; |
Lightvalve | 67:c2812cf26c38 | 1184 | I_ERR_INT = I_ERR_INT - V_rem * 0.0002f; |
Lightvalve | 57:f4819de54e7a | 1185 | } |
Lightvalve | 57:f4819de54e7a | 1186 | Cur_Valve_Open_pulse = cur.sen / mA_PER_pulse; |
Lightvalve | 57:f4819de54e7a | 1187 | } else { |
Lightvalve | 57:f4819de54e7a | 1188 | VALVE_PWM_RAW = I_REF * mV_PER_mA; |
Lightvalve | 57:f4819de54e7a | 1189 | Cur_Valve_Open_pulse = I_REF / mA_PER_pulse; |
Lightvalve | 57:f4819de54e7a | 1190 | } |
Lightvalve | 57:f4819de54e7a | 1191 | |
Lightvalve | 57:f4819de54e7a | 1192 | //////////////////////////////////////////////////////////////////////////// |
Lightvalve | 57:f4819de54e7a | 1193 | ///////////////// Dead Zone Cancellation & Linearization ////////////////// |
Lightvalve | 57:f4819de54e7a | 1194 | //////////////////////////////////////////////////////////////////////////// |
Lightvalve | 57:f4819de54e7a | 1195 | |
Lightvalve | 57:f4819de54e7a | 1196 | // Output Voltage Linearization |
hyhoon | 226:82a3ca333004 | 1197 | double CUR_PWM_nonlin = (double)VALVE_PWM_RAW; // Unit : mV |
Lightvalve | 67:c2812cf26c38 | 1198 | double CUR_PWM_lin = PWM_duty_byLT(CUR_PWM_nonlin); // -8000~8000 |
Lightvalve | 57:f4819de54e7a | 1199 | |
Lightvalve | 57:f4819de54e7a | 1200 | // Dead Zone Cancellation (Electrical dead-zone) |
Lightvalve | 67:c2812cf26c38 | 1201 | if (CUR_PWM_lin > 0) V_out = (float) (CUR_PWM_lin + 169.0f); |
Lightvalve | 67:c2812cf26c38 | 1202 | else if (CUR_PWM_lin < 0) V_out = (float) (CUR_PWM_lin - 174.0f); |
Lightvalve | 67:c2812cf26c38 | 1203 | else V_out = (float) (CUR_PWM_lin); |
Lightvalve | 169:645207e160ca | 1204 | } else { //////////////////////////sw valve |
Lightvalve | 224:985dba42f261 | 1205 | // Output Voltage Linearization & Dead Zone Cancellation (Electrical dead-zone) by SW |
Lightvalve | 135:79885a39c161 | 1206 | if (V_out > 0 ) V_out = (V_out + 180.0f)/0.8588f; |
Lightvalve | 135:79885a39c161 | 1207 | else if (V_out < 0) V_out = (V_out - 200.0f)/0.8651f; |
Lightvalve | 135:79885a39c161 | 1208 | else V_out = 0.0f; |
Lightvalve | 89:a7b45368ea0f | 1209 | } |
Lightvalve | 169:645207e160ca | 1210 | |
jobuuu | 7:e9086c72bb22 | 1211 | /******************************************************* |
jobuuu | 7:e9086c72bb22 | 1212 | *** PWM |
jobuuu | 7:e9086c72bb22 | 1213 | ********************************************************/ |
Lightvalve | 169:645207e160ca | 1214 | if(DIR_VALVE<0) { |
Lightvalve | 67:c2812cf26c38 | 1215 | V_out = -V_out; |
Lightvalve | 67:c2812cf26c38 | 1216 | } |
Lightvalve | 169:645207e160ca | 1217 | |
Lightvalve | 49:e7bcfc244d40 | 1218 | if (V_out >= VALVE_VOLTAGE_LIMIT*1000.0f) { |
Lightvalve | 49:e7bcfc244d40 | 1219 | V_out = VALVE_VOLTAGE_LIMIT*1000.0f; |
Lightvalve | 49:e7bcfc244d40 | 1220 | } else if(V_out<=-VALVE_VOLTAGE_LIMIT*1000.0f) { |
Lightvalve | 49:e7bcfc244d40 | 1221 | V_out = -VALVE_VOLTAGE_LIMIT*1000.0f; |
Lightvalve | 27:a2254a485f23 | 1222 | } |
Lightvalve | 224:985dba42f261 | 1223 | PWM_out= V_out/(SUPPLY_VOLTAGE*1000.0f); |
Lightvalve | 13:747daba9cf59 | 1224 | |
Lightvalve | 224:985dba42f261 | 1225 | // Saturation of output voltage |
Lightvalve | 30:8d561f16383b | 1226 | if(PWM_out > 1.0f) PWM_out=1.0f; |
Lightvalve | 30:8d561f16383b | 1227 | else if (PWM_out < -1.0f) PWM_out=-1.0f; |
Lightvalve | 13:747daba9cf59 | 1228 | |
Lightvalve | 30:8d561f16383b | 1229 | if (PWM_out>0.0f) { |
Lightvalve | 30:8d561f16383b | 1230 | dtc_v=0.0f; |
jobuuu | 1:e04e563be5ce | 1231 | dtc_w=PWM_out; |
jobuuu | 1:e04e563be5ce | 1232 | } else { |
jobuuu | 2:a1c0a37df760 | 1233 | dtc_v=-PWM_out; |
Lightvalve | 30:8d561f16383b | 1234 | dtc_w=0.0f; |
jobuuu | 1:e04e563be5ce | 1235 | } |
Lightvalve | 13:747daba9cf59 | 1236 | |
jobuuu | 1:e04e563be5ce | 1237 | //pwm |
Lightvalve | 30:8d561f16383b | 1238 | TIM4->CCR2 = (PWM_ARR)*(1.0f-dtc_v); |
Lightvalve | 30:8d561f16383b | 1239 | TIM4->CCR1 = (PWM_ARR)*(1.0f-dtc_w); |
Lightvalve | 67:c2812cf26c38 | 1240 | |
Lightvalve | 20:806196fda269 | 1241 | |
hyhoon | 226:82a3ca333004 | 1242 | if (TMR2_COUNT_CAN_TX % (int) ((int) TMR_FREQ_5k/CAN_FREQ) == 0) { |
hyhoon | 226:82a3ca333004 | 1243 | // Position, Velocity, and Torque (ID:1200) |
hyhoon | 226:82a3ca333004 | 1244 | if (flag_data_request[0] == HIGH) { |
hyhoon | 226:82a3ca333004 | 1245 | if ((OPERATING_MODE & 0b01) == 0) { // Rotary Actuator |
hyhoon | 226:82a3ca333004 | 1246 | if (SENSING_MODE == 0) { |
hyhoon | 226:82a3ca333004 | 1247 | CAN_TX_POSITION_FT((int16_t) (pos.sen), (int16_t) (vel.sen/10.0f), (int16_t) (torq.sen*10.0f)); |
hyhoon | 226:82a3ca333004 | 1248 | } else if (SENSING_MODE == 1) { |
hyhoon | 226:82a3ca333004 | 1249 | CAN_TX_POSITION_PRESSURE((int16_t) (pos.sen), (int16_t) (vel.sen/10.0f), (int16_t) ((pres_A.sen)*5.0f), (int16_t) ((pres_B.sen)*5.0f)); |
hyhoon | 226:82a3ca333004 | 1250 | } |
hyhoon | 226:82a3ca333004 | 1251 | } else if ((OPERATING_MODE & 0b01) == 1) { // Linear Actuator |
hyhoon | 226:82a3ca333004 | 1252 | if (SENSING_MODE == 0) { |
hyhoon | 226:82a3ca333004 | 1253 | CAN_TX_POSITION_FT((int16_t) (pos.sen/10.0f), (int16_t) (vel.sen/256.0f), (int16_t) (torq.sen * 10.0f * (float)(TORQUE_SENSOR_PULSE_PER_TORQUE))); |
hyhoon | 226:82a3ca333004 | 1254 | } else if (SENSING_MODE == 1) { |
hyhoon | 226:82a3ca333004 | 1255 | CAN_TX_POSITION_PRESSURE((int16_t) (pos.sen/10.0f), (int16_t) (vel.sen/256.0f), (int16_t) ((pres_A.sen)*5.0f), (int16_t) ((pres_B.sen)*5.0f)); |
hyhoon | 226:82a3ca333004 | 1256 | } |
hyhoon | 226:82a3ca333004 | 1257 | } |
hyhoon | 226:82a3ca333004 | 1258 | } |
hyhoon | 226:82a3ca333004 | 1259 | |
hyhoon | 227:699c3e572283 | 1260 | // PWM, ID:1300 |
Lightvalve | 56:6f50d9d3bfee | 1261 | if (flag_data_request[1] == HIGH) { |
hyhoon | 227:699c3e572283 | 1262 | CAN_TX_PWM((int16_t) (V_out)); //1300 |
Lightvalve | 54:647072f5307a | 1263 | } |
Lightvalve | 58:2eade98630e2 | 1264 | |
hyhoon | 227:699c3e572283 | 1265 | // Reference Current, Current Current (ID:1400) |
Lightvalve | 171:bfc1fd2629d8 | 1266 | if (flag_data_request[2] == HIGH) { |
hyhoon | 227:699c3e572283 | 1267 | CAN_TX_CURRENT((int16_t) (I_REF_fil_DZ / mA_PER_pulse), (int16_t) (cur.sen / mA_PER_pulse)); // 1400 |
Lightvalve | 55:b25725257569 | 1268 | } |
hyhoon | 227:699c3e572283 | 1269 | |
hyhoon | 227:699c3e572283 | 1270 | // Reference Current, Current Current (ID:1700) |
Lightvalve | 169:645207e160ca | 1271 | if (flag_data_request[3] == HIGH) { |
hyhoon | 227:699c3e572283 | 1272 | CAN_TX_SOMETHING((int16_t) (0), (int16_t) (0), (int16_t) (0), (int16_t) (0)); // 1700 |
Lightvalve | 54:647072f5307a | 1273 | } |
Lightvalve | 20:806196fda269 | 1274 | |
Lightvalve | 54:647072f5307a | 1275 | TMR2_COUNT_CAN_TX = 0; |
Lightvalve | 54:647072f5307a | 1276 | } |
Lightvalve | 54:647072f5307a | 1277 | TMR2_COUNT_CAN_TX++; |
Lightvalve | 52:8ea76864368a | 1278 | |
Lightvalve | 20:806196fda269 | 1279 | } |
Lightvalve | 52:8ea76864368a | 1280 | TIM3->SR = 0x0; // reset the status register |
Lightvalve | 52:8ea76864368a | 1281 | |
Lightvalve | 58:2eade98630e2 | 1282 | } |