![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
2011
main.cpp@219:e00e71ca3e80, 2021-04-27 (annotated)
- Committer:
- Lightvalve
- Date:
- Tue Apr 27 11:31:49 2021 +0000
- Revision:
- 219:e00e71ca3e80
- Parent:
- 218:2c3e5ecbe7e1
- Child:
- 220:2976cf1b4252
210427
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Lightvalve | 212:985dba42f261 | 1 | //Hydraulic Control Board |
Lightvalve | 212:985dba42f261 | 2 | //distributed by Sungwoo Kim |
Lightvalve | 212:985dba42f261 | 3 | // 2020/12/28 |
jobuuu | 216:763bbefcb79f | 4 | //revised by Buyoun Cho |
jobuuu | 217:7a1c46b9b471 | 5 | // 2021/04/20 |
Lightvalve | 212:985dba42f261 | 6 | |
Lightvalve | 212:985dba42f261 | 7 | // 유의사항 |
Lightvalve | 212:985dba42f261 | 8 | // 소수 적을때 뒤에 f 꼭 붙이기 |
Lightvalve | 212:985dba42f261 | 9 | // CAN 선은 ground까지 있는 3상 선으로 써야함. |
Lightvalve | 212:985dba42f261 | 10 | // 전원은 12~24V 인가. |
Lightvalve | 212: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 | 164:645207e160ca | 21 | #include "FlashWriter.h" |
Lightvalve | 165:42c938a40313 | 22 | #include <string> |
Lightvalve | 165:42c938a40313 | 23 | #include <iostream> |
Lightvalve | 174:d5377766d7ea | 24 | #include <cmath> |
Lightvalve | 59:bc8c8270f0ab | 25 | |
Lightvalve | 59:bc8c8270f0ab | 26 | using namespace std; |
Lightvalve | 59: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 | 218:2c3e5ecbe7e1 | 32 | AnalogOut dac_1(PA_4); // 0.0f ~ 1.0f |
jobuuu | 218: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 | 212: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 | 165:42c938a40313 | 48 | SPI eeprom(PB_15, PB_14, PB_13); // EEPROM //(SPI_MOSI, SPI_MISO, SPI_SCK); |
Lightvalve | 165: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 | 218:2c3e5ecbe7e1 | 69 | State force; |
jobuuu | 218:2c3e5ecbe7e1 | 70 | State torq; // unit : N |
Lightvalve | 198:c70c5a9f17dd | 71 | State torq_dot; |
jobuuu | 218:2c3e5ecbe7e1 | 72 | State pres_A; // unit : bar |
jobuuu | 7:e9086c72bb22 | 73 | State pres_B; |
jobuuu | 218:2c3e5ecbe7e1 | 74 | State cur; // unit : mA |
Lightvalve | 14:8e7590227d22 | 75 | State valve_pos; |
Lightvalve | 14:8e7590227d22 | 76 | |
Lightvalve | 14:8e7590227d22 | 77 | State INIT_Vout; |
Lightvalve | 14:8e7590227d22 | 78 | State INIT_Valve_Pos; |
Lightvalve | 14:8e7590227d22 | 79 | State INIT_Pos; |
Lightvalve | 14:8e7590227d22 | 80 | State INIT_torq; |
jobuuu | 5:a4319f79457b | 81 | |
Lightvalve | 19:23b7c1ad8683 | 82 | extern int CID_RX_CMD; |
Lightvalve | 19:23b7c1ad8683 | 83 | extern int CID_RX_REF_POSITION; |
hyhoon | 215:699c3e572283 | 84 | extern int CID_RX_REF_OPENLOOP; |
Lightvalve | 44:35fa6884d0c6 | 85 | extern int CID_RX_REF_PWM; |
Lightvalve | 19:23b7c1ad8683 | 86 | |
Lightvalve | 19:23b7c1ad8683 | 87 | extern int CID_TX_INFO; |
hyhoon | 215:699c3e572283 | 88 | extern int CID_TX_POS_VEL_TORQ; |
hyhoon | 215:699c3e572283 | 89 | extern int CID_TX_PWM; |
hyhoon | 215:699c3e572283 | 90 | extern int CID_TX_CURRENT; |
Lightvalve | 19:23b7c1ad8683 | 91 | extern int CID_TX_VOUT; |
Lightvalve | 19:23b7c1ad8683 | 92 | extern int CID_TX_VALVE_POSITION; |
jeongyeseong | 213:9c0becc196ba | 93 | extern int CID_TX_SOMETHING; |
Lightvalve | 19:23b7c1ad8683 | 94 | |
hyhoon | 215:699c3e572283 | 95 | float temp_P_GAIN = 0.0f; |
hyhoon | 215:699c3e572283 | 96 | float temp_I_GAIN = 0.0f; |
hyhoon | 215:699c3e572283 | 97 | int temp_VELOCITY_COMP_GAIN = 0; |
hyhoon | 215:699c3e572283 | 98 | |
jobuuu | 218:2c3e5ecbe7e1 | 99 | inline float tanh_inv(float y) { |
jobuuu | 218:2c3e5ecbe7e1 | 100 | if(y >= 1.0f - 0.000001f) y = 1.0f - 0.000001f; |
jobuuu | 218:2c3e5ecbe7e1 | 101 | if(y <= -1.0f + 0.000001f) y = -1.0f + 0.000001f; |
jobuuu | 218:2c3e5ecbe7e1 | 102 | return log(sqrt((1.0f+y)/(1.0f-y))); |
jobuuu | 218:2c3e5ecbe7e1 | 103 | } |
jobuuu | 218:2c3e5ecbe7e1 | 104 | |
hyhoon | 215:699c3e572283 | 105 | |
Lightvalve | 12:6f2531038ea4 | 106 | /******************************************************************************* |
Lightvalve | 12:6f2531038ea4 | 107 | * REFERENCE MODE |
Lightvalve | 12:6f2531038ea4 | 108 | ******************************************************************************/ |
Lightvalve | 13:747daba9cf59 | 109 | enum _REFERENCE_MODE { |
jobuuu | 218:2c3e5ecbe7e1 | 110 | MODE_REF_NO_ACT = 0, |
jobuuu | 218:2c3e5ecbe7e1 | 111 | MODE_REF_DIRECT, |
jobuuu | 218:2c3e5ecbe7e1 | 112 | MODE_REF_FINDHOME |
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 | 46:fdcb8bd86fd6 | 121 | MODE_VALVE_POSITION_CONTROL, //1 |
Lightvalve | 46:fdcb8bd86fd6 | 122 | MODE_JOINT_CONTROL, //2 |
Lightvalve | 49:3c630b5eba9f | 123 | |
Lightvalve | 46:fdcb8bd86fd6 | 124 | MODE_VALVE_OPEN_LOOP, //3 |
Lightvalve | 136:a843f32ced33 | 125 | MODE_JOINT_ADAPTIVE_BACKSTEPPING, //4 |
Lightvalve | 166: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 | 212: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 | 55: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 | 212: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 | 55:f4819de54e7a | 152 | MODE_SYSTEM_ID, //33 |
Lightvalve | 164:645207e160ca | 153 | MODE_FREQ_TEST, //34 |
Lightvalve | 164:645207e160ca | 154 | MODE_SEND_BUFFER, //35 |
Lightvalve | 164:645207e160ca | 155 | MODE_SEND_OVER, //36 |
Lightvalve | 164:645207e160ca | 156 | MODE_STEP_TEST, //37 |
Lightvalve | 12:6f2531038ea4 | 157 | }; |
Lightvalve | 12:6f2531038ea4 | 158 | |
Lightvalve | 63:a2d7c63419c2 | 159 | void SystemClock_Config(void) |
Lightvalve | 63:a2d7c63419c2 | 160 | { |
Lightvalve | 164:645207e160ca | 161 | RCC_OscInitTypeDef RCC_OscInitStruct = {0}; |
Lightvalve | 164:645207e160ca | 162 | RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; |
Lightvalve | 63:a2d7c63419c2 | 163 | |
Lightvalve | 212:985dba42f261 | 164 | /* Configure the main internal regulator output voltage |
Lightvalve | 164:645207e160ca | 165 | */ |
Lightvalve | 164:645207e160ca | 166 | __HAL_RCC_PWR_CLK_ENABLE(); |
Lightvalve | 164:645207e160ca | 167 | __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); |
Lightvalve | 212:985dba42f261 | 168 | /* Initializes the CPU, AHB and APB busses clocks |
Lightvalve | 164:645207e160ca | 169 | */ |
Lightvalve | 164:645207e160ca | 170 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; |
Lightvalve | 164:645207e160ca | 171 | RCC_OscInitStruct.HSIState = RCC_HSI_ON; |
Lightvalve | 164:645207e160ca | 172 | RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; |
Lightvalve | 164:645207e160ca | 173 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
Lightvalve | 164:645207e160ca | 174 | RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; |
Lightvalve | 164:645207e160ca | 175 | RCC_OscInitStruct.PLL.PLLM = 8;//8 |
Lightvalve | 164:645207e160ca | 176 | RCC_OscInitStruct.PLL.PLLN = 180; //180 |
Lightvalve | 164:645207e160ca | 177 | RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; |
Lightvalve | 164:645207e160ca | 178 | RCC_OscInitStruct.PLL.PLLQ = 2; |
Lightvalve | 164:645207e160ca | 179 | RCC_OscInitStruct.PLL.PLLR = 2; |
Lightvalve | 164:645207e160ca | 180 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { |
Lightvalve | 164:645207e160ca | 181 | //Error_Handler(); |
Lightvalve | 164:645207e160ca | 182 | } |
Lightvalve | 164:645207e160ca | 183 | /** Activate the Over-Drive mode |
Lightvalve | 164:645207e160ca | 184 | */ |
Lightvalve | 164:645207e160ca | 185 | if (HAL_PWREx_EnableOverDrive() != HAL_OK) { |
Lightvalve | 164:645207e160ca | 186 | //Error_Handler(); |
Lightvalve | 164:645207e160ca | 187 | } |
Lightvalve | 164:645207e160ca | 188 | /** Initializes the CPU, AHB and APB busses clocks |
Lightvalve | 164:645207e160ca | 189 | */ |
Lightvalve | 164:645207e160ca | 190 | RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |
Lightvalve | 164:645207e160ca | 191 | |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; |
Lightvalve | 164:645207e160ca | 192 | RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; |
Lightvalve | 164:645207e160ca | 193 | RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; |
Lightvalve | 164:645207e160ca | 194 | RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4; |
Lightvalve | 164:645207e160ca | 195 | RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; |
Lightvalve | 63:a2d7c63419c2 | 196 | |
Lightvalve | 164:645207e160ca | 197 | if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) { |
Lightvalve | 164:645207e160ca | 198 | //Error_Handler(); |
Lightvalve | 164:645207e160ca | 199 | } |
Lightvalve | 63:a2d7c63419c2 | 200 | } |
Lightvalve | 63:a2d7c63419c2 | 201 | |
Lightvalve | 165: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 | 214:82a3ca333004 | 208 | |
Lightvalve | 212:985dba42f261 | 209 | HAL_Init(); |
Lightvalve | 212:985dba42f261 | 210 | SystemClock_Config(); |
hyhoon | 214:82a3ca333004 | 211 | |
Lightvalve | 67:3995ffeaa786 | 212 | LED = 0; |
Lightvalve | 59:bc8c8270f0ab | 213 | pc.baud(9600); |
Lightvalve | 21:e5f1a43ea6f9 | 214 | |
GiJeongKim | 0:51c43836c1d7 | 215 | // i2c init |
hyhoon | 214:82a3ca333004 | 216 | // i2c.frequency(400 * 1000); // 0.4 mHz |
hyhoon | 214:82a3ca333004 | 217 | // wait_ms(2); // Power Up wait |
hyhoon | 214:82a3ca333004 | 218 | // look_for_hardware_i2c(); // Hardware present |
hyhoon | 214:82a3ca333004 | 219 | // init_as5510(i2c_slave_addr1); |
hyhoon | 214:82a3ca333004 | 220 | // make_delay(); |
jobuuu | 2:a1c0a37df760 | 221 | |
Lightvalve | 212:985dba42f261 | 222 | // spi init |
hyhoon | 215:699c3e572283 | 223 | eeprom_cs = 1; |
Lightvalve | 165:42c938a40313 | 224 | eeprom.format(8,3); |
Lightvalve | 165:42c938a40313 | 225 | eeprom.frequency(5000000); //5M |
hyhoon | 215:699c3e572283 | 226 | eeprom_cs = 0; |
hyhoon | 215:699c3e572283 | 227 | make_delay(); |
hyhoon | 215:699c3e572283 | 228 | |
hyhoon | 215:699c3e572283 | 229 | enc_cs = 1; //sw add |
GiJeongKim | 0:51c43836c1d7 | 230 | enc.format(8,0); |
hyhoon | 215:699c3e572283 | 231 | enc.frequency(5000000); //10M |
hyhoon | 215:699c3e572283 | 232 | enc_cs = 0; //sw add |
hyhoon | 215:699c3e572283 | 233 | |
Lightvalve | 11:82d8768d7351 | 234 | make_delay(); |
Lightvalve | 21:e5f1a43ea6f9 | 235 | |
hyhoon | 214:82a3ca333004 | 236 | // spi _ enc |
hyhoon | 214:82a3ca333004 | 237 | spi_enc_set_init(); |
hyhoon | 214:82a3ca333004 | 238 | make_delay(); |
hyhoon | 214:82a3ca333004 | 239 | |
hyhoon | 214:82a3ca333004 | 240 | ////// bno rom |
Lightvalve | 219:e00e71ca3e80 | 241 | // spi_eeprom_write(RID_BNO, (int16_t) 4); |
hyhoon | 214:82a3ca333004 | 242 | // make_delay(); |
hyhoon | 214:82a3ca333004 | 243 | //////// |
hyhoon | 214:82a3ca333004 | 244 | |
Lightvalve | 212:985dba42f261 | 245 | // rom |
Lightvalve | 19:23b7c1ad8683 | 246 | ROM_CALL_DATA(); |
Lightvalve | 16:903b5a4433b4 | 247 | make_delay(); |
Lightvalve | 13:747daba9cf59 | 248 | |
GiJeongKim | 0:51c43836c1d7 | 249 | // ADC init |
jobuuu | 5:a4319f79457b | 250 | Init_ADC(); |
Lightvalve | 11:82d8768d7351 | 251 | make_delay(); |
jobuuu | 2:a1c0a37df760 | 252 | |
GiJeongKim | 0:51c43836c1d7 | 253 | // Pwm init |
GiJeongKim | 0:51c43836c1d7 | 254 | Init_PWM(); |
GiJeongKim | 0:51c43836c1d7 | 255 | TIM4->CR1 ^= TIM_CR1_UDIS; |
Lightvalve | 11:82d8768d7351 | 256 | make_delay(); |
Lightvalve | 13:747daba9cf59 | 257 | |
GiJeongKim | 0:51c43836c1d7 | 258 | // CAN |
jobuuu | 2:a1c0a37df760 | 259 | can.attach(&CAN_RX_HANDLER); |
Lightvalve | 11:82d8768d7351 | 260 | CAN_ID_INIT(); |
Lightvalve | 11:82d8768d7351 | 261 | make_delay(); |
Lightvalve | 34:bb2ca2fc2a8e | 262 | |
hyhoon | 214:82a3ca333004 | 263 | //can.reset(); |
hyhoon | 214:82a3ca333004 | 264 | can.filter(msg.id, 0xFFFFF000, CANStandard); |
hyhoon | 214:82a3ca333004 | 265 | |
hyhoon | 214:82a3ca333004 | 266 | // TMR3 init |
hyhoon | 214:82a3ca333004 | 267 | Init_TMR3(); |
hyhoon | 214:82a3ca333004 | 268 | TIM3->CR1 ^= TIM_CR1_UDIS; |
hyhoon | 214:82a3ca333004 | 269 | make_delay(); |
hyhoon | 214:82a3ca333004 | 270 | |
Lightvalve | 23:59218d4a256d | 271 | //Timer priority |
Lightvalve | 23:59218d4a256d | 272 | NVIC_SetPriority(TIM3_IRQn, 2); |
Lightvalve | 52:647072f5307a | 273 | NVIC_SetPriority(TIM4_IRQn, 3); |
Lightvalve | 34:bb2ca2fc2a8e | 274 | |
hyhoon | 214:82a3ca333004 | 275 | |
Lightvalve | 11:82d8768d7351 | 276 | //DAC init |
Lightvalve | 56:2eade98630e2 | 277 | if (SENSING_MODE == 0) { |
jobuuu | 218:2c3e5ecbe7e1 | 278 | dac_1 = FORCE_VREF / 3.3f; |
Lightvalve | 56:2eade98630e2 | 279 | dac_2 = 0.0f; |
Lightvalve | 56:2eade98630e2 | 280 | } else if (SENSING_MODE == 1) { |
hyhoon | 214:82a3ca333004 | 281 | if (DIR_VALVE_ENC > 0) { |
hyhoon | 214:82a3ca333004 | 282 | dac_1 = PRES_A_VREF / 3.3f; |
hyhoon | 214:82a3ca333004 | 283 | dac_2 = PRES_B_VREF / 3.3f; |
hyhoon | 214:82a3ca333004 | 284 | } else { |
hyhoon | 214:82a3ca333004 | 285 | dac_1 = PRES_B_VREF / 3.3f; |
hyhoon | 214:82a3ca333004 | 286 | dac_2 = PRES_A_VREF / 3.3f; |
hyhoon | 214:82a3ca333004 | 287 | } |
Lightvalve | 56:2eade98630e2 | 288 | } |
Lightvalve | 11:82d8768d7351 | 289 | make_delay(); |
Lightvalve | 13:747daba9cf59 | 290 | |
Lightvalve | 19:23b7c1ad8683 | 291 | for (int i=0; i<50; i++) { |
Lightvalve | 11:82d8768d7351 | 292 | if(i%2==0) |
Lightvalve | 38:118df027d851 | 293 | ID_index_array[i] = - i * 0.5f; |
Lightvalve | 11:82d8768d7351 | 294 | else |
Lightvalve | 38:118df027d851 | 295 | ID_index_array[i] = (i+1) * 0.5f; |
Lightvalve | 11:82d8768d7351 | 296 | } |
Lightvalve | 164:645207e160ca | 297 | |
jobuuu | 6:df07d3491e3a | 298 | /************************************ |
jobuuu | 1:e04e563be5ce | 299 | *** Program is operating! |
jobuuu | 6:df07d3491e3a | 300 | *************************************/ |
GiJeongKim | 0:51c43836c1d7 | 301 | while(1) { |
Lightvalve | 164:645207e160ca | 302 | |
Lightvalve | 212:985dba42f261 | 303 | // UART example |
Lightvalve | 212:985dba42f261 | 304 | // if(timer_while==100000) { |
Lightvalve | 166:bfc1fd2629d8 | 305 | // timer_while = 0; |
Lightvalve | 212:985dba42f261 | 306 | // pc.printf("%f\n", value); |
Lightvalve | 166:bfc1fd2629d8 | 307 | // } |
Lightvalve | 212:985dba42f261 | 308 | // timer_while ++; |
Lightvalve | 164:645207e160ca | 309 | |
Lightvalve | 212:985dba42f261 | 310 | //i2c for SW valve |
hyhoon | 215:699c3e572283 | 311 | //if(OPERATING_MODE == 5) { |
hyhoon | 215:699c3e572283 | 312 | // read_field(i2c_slave_addr1); |
hyhoon | 215:699c3e572283 | 313 | // if(DIR_VALVE_ENC < 0) value = 1023 - value; |
hyhoon | 215:699c3e572283 | 314 | // } |
GiJeongKim | 0:51c43836c1d7 | 315 | } |
jobuuu | 1:e04e563be5ce | 316 | } |
jobuuu | 1:e04e563be5ce | 317 | |
Lightvalve | 212:985dba42f261 | 318 | |
Lightvalve | 212:985dba42f261 | 319 | // Velocity feedforward for SW valve |
Lightvalve | 33:91b17819ec30 | 320 | float DDV_JOINT_POS_FF(float REF_JOINT_VEL) |
Lightvalve | 14:8e7590227d22 | 321 | { |
Lightvalve | 13:747daba9cf59 | 322 | int i = 0; |
Lightvalve | 47:889798ff9329 | 323 | float Ref_Valve_Pos_FF = 0.0f; |
Lightvalve | 14:8e7590227d22 | 324 | for(i=0; i<VALVE_POS_NUM; i++) { |
Lightvalve | 14:8e7590227d22 | 325 | 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 | 326 | if(i==0) { |
Lightvalve | 49:3c630b5eba9f | 327 | if(JOINT_VEL[i+1] == JOINT_VEL[i]) { |
Lightvalve | 55:f4819de54e7a | 328 | Ref_Valve_Pos_FF = (float) VALVE_CENTER; |
Lightvalve | 49:3c630b5eba9f | 329 | } else { |
Lightvalve | 55:f4819de54e7a | 330 | 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 | 331 | } |
Lightvalve | 14:8e7590227d22 | 332 | } else { |
Lightvalve | 49:3c630b5eba9f | 333 | if(JOINT_VEL[i+1] == JOINT_VEL[i-1]) { |
Lightvalve | 55:f4819de54e7a | 334 | Ref_Valve_Pos_FF = (float) VALVE_CENTER; |
Lightvalve | 49:3c630b5eba9f | 335 | } else { |
Lightvalve | 55:f4819de54e7a | 336 | 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 | 337 | } |
Lightvalve | 13:747daba9cf59 | 338 | } |
Lightvalve | 13:747daba9cf59 | 339 | break; |
Lightvalve | 13:747daba9cf59 | 340 | } |
Lightvalve | 13:747daba9cf59 | 341 | } |
Lightvalve | 14:8e7590227d22 | 342 | if(REF_JOINT_VEL > max(JOINT_VEL[VALVE_POS_NUM-1], JOINT_VEL[VALVE_POS_NUM-2])) { |
Lightvalve | 33:91b17819ec30 | 343 | Ref_Valve_Pos_FF = (float) VALVE_MAX_POS; |
Lightvalve | 14:8e7590227d22 | 344 | } else if(REF_JOINT_VEL < min(JOINT_VEL[VALVE_POS_NUM-1], JOINT_VEL[VALVE_POS_NUM-2])) { |
Lightvalve | 33:91b17819ec30 | 345 | Ref_Valve_Pos_FF = (float) VALVE_MIN_POS; |
Lightvalve | 13:747daba9cf59 | 346 | } |
Lightvalve | 36:a46e63505ed8 | 347 | |
Lightvalve | 212:985dba42f261 | 348 | 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 | 349 | return Ref_Valve_Pos_FF; |
Lightvalve | 13:747daba9cf59 | 350 | } |
jobuuu | 6:df07d3491e3a | 351 | |
Lightvalve | 212:985dba42f261 | 352 | // Valve feedforward for SW valve |
Lightvalve | 30:8d561f16383b | 353 | void VALVE_POS_CONTROL(float REF_VALVE_POS) |
Lightvalve | 14:8e7590227d22 | 354 | { |
Lightvalve | 13:747daba9cf59 | 355 | int i = 0; |
Lightvalve | 13:747daba9cf59 | 356 | |
Lightvalve | 38:118df027d851 | 357 | if(REF_VALVE_POS > VALVE_MAX_POS) { |
Lightvalve | 38:118df027d851 | 358 | REF_VALVE_POS = VALVE_MAX_POS; |
Lightvalve | 38:118df027d851 | 359 | } else if(REF_VALVE_POS < VALVE_MIN_POS) { |
Lightvalve | 38:118df027d851 | 360 | REF_VALVE_POS = VALVE_MIN_POS; |
Lightvalve | 38:118df027d851 | 361 | } |
Lightvalve | 87:a7b45368ea0f | 362 | valve_pos_err = (float) (REF_VALVE_POS - value); |
Lightvalve | 13:747daba9cf59 | 363 | valve_pos_err_diff = valve_pos_err - valve_pos_err_old; |
Lightvalve | 13:747daba9cf59 | 364 | valve_pos_err_old = valve_pos_err; |
Lightvalve | 13:747daba9cf59 | 365 | valve_pos_err_sum += valve_pos_err; |
Lightvalve | 87:a7b45368ea0f | 366 | if (valve_pos_err_sum > 1000.0f) valve_pos_err_sum = 1000.0f; |
Lightvalve | 87:a7b45368ea0f | 367 | if (valve_pos_err_sum<-1000.0f) valve_pos_err_sum = -1000.0f; |
Lightvalve | 13:747daba9cf59 | 368 | |
Lightvalve | 13:747daba9cf59 | 369 | 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 | 370 | |
Lightvalve | 18:b8adf1582ea3 | 371 | for(i=0; i<24; i++) { |
Lightvalve | 87:a7b45368ea0f | 372 | 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 | 373 | if(i==0) { |
Lightvalve | 47:889798ff9329 | 374 | 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 | 375 | } else { |
Lightvalve | 47:889798ff9329 | 376 | 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 | 377 | } |
Lightvalve | 13:747daba9cf59 | 378 | break; |
Lightvalve | 13:747daba9cf59 | 379 | } |
Lightvalve | 13:747daba9cf59 | 380 | } |
Lightvalve | 57:f308b1656d9c | 381 | Vout.ref = VALVE_PWM_RAW_FF + VALVE_PWM_RAW_FB; |
Lightvalve | 13:747daba9cf59 | 382 | } |
Lightvalve | 13:747daba9cf59 | 383 | |
Lightvalve | 212:985dba42f261 | 384 | // PWM duty vs. voltage output of L6205 in STM board |
Lightvalve | 14:8e7590227d22 | 385 | #define LT_MAX_IDX 57 |
Lightvalve | 30:8d561f16383b | 386 | 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 | 387 | -19.0f, -18.0f, -17.0f, -16.0f, -15.0f, -14.0f, -13.0f, -12.0f, -11.0f, -10.0f, |
Lightvalve | 34:bb2ca2fc2a8e | 388 | -9.0f, -8.0f, -7.0f, -6.0f, -5.0f, -4.0f, -3.0f, -2.0f, -1.0f, 0.0f, |
Lightvalve | 34:bb2ca2fc2a8e | 389 | 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f, |
Lightvalve | 34:bb2ca2fc2a8e | 390 | 11.0f, 12.0f, 13.0f, 14.0f, 15.0f, 16.0f, 17.0f, 18.0f, 19.0f, 20.0f, |
Lightvalve | 34:bb2ca2fc2a8e | 391 | 25.0f, 30.0f, 35.0f, 40.0f, 50.0f, 60.0f, 80.0f, 100.0f |
Lightvalve | 34:bb2ca2fc2a8e | 392 | }; // duty |
Lightvalve | 65:c2812cf26c38 | 393 | float LT_Voltage_Output[LT_MAX_IDX] = {-230.0f, -215.0f, -192.5f, -185.0f, -177.5f, -170.0f, -164.0f, -160.0f, -150.0f, |
Lightvalve | 65:c2812cf26c38 | 394 | -150.0f, -145.0f, -145.0f, -145.0f, -135.0f, -135.0f, -135.0f, -127.5f, -127.5f, -115.0f, |
Lightvalve | 65:c2812cf26c38 | 395 | -115.0f, -115.0F, -100.0f, -100.0f, -100.0f, -60.0f, -60.0f, -10.0f, -5.0f, 0.0f, |
Lightvalve | 65:c2812cf26c38 | 396 | 7.5f, 14.0f, 14.0f, 14.0f, 42.5f, 42.5f, 42.5f, 80.0f, 80.0f, 105.0f, |
Lightvalve | 65:c2812cf26c38 | 397 | 105.0f, 105.0f, 120.0f, 120.0f, 120.0f, 131.0f, 131.0f, 140.0f, 140.0f, 140.0f, |
Lightvalve | 65:c2812cf26c38 | 398 | 155.0f, 160.0f, 170.0f, 174.0f, 182.0f, 191.0f, 212.0f, 230.0f |
Lightvalve | 34:bb2ca2fc2a8e | 399 | }; // mV |
Lightvalve | 13:747daba9cf59 | 400 | |
Lightvalve | 30:8d561f16383b | 401 | float PWM_duty_byLT(float Ref_V) |
Lightvalve | 14:8e7590227d22 | 402 | { |
Lightvalve | 30:8d561f16383b | 403 | float PWM_duty = 0.0f; |
Lightvalve | 13:747daba9cf59 | 404 | if(Ref_V<LT_Voltage_Output[0]) { |
Lightvalve | 30:8d561f16383b | 405 | PWM_duty = (Ref_V-LT_Voltage_Output[0])/1.5f+LT_PWM_duty[0]; |
Lightvalve | 13:747daba9cf59 | 406 | } else if (Ref_V>=LT_Voltage_Output[LT_MAX_IDX-1]) { |
Lightvalve | 30:8d561f16383b | 407 | PWM_duty = (Ref_V-LT_Voltage_Output[LT_MAX_IDX-1])/1.5f+LT_PWM_duty[LT_MAX_IDX-1]; |
Lightvalve | 13:747daba9cf59 | 408 | } else { |
Lightvalve | 13:747daba9cf59 | 409 | int idx = 0; |
Lightvalve | 13:747daba9cf59 | 410 | for(idx=0; idx<LT_MAX_IDX-1; idx++) { |
Lightvalve | 30:8d561f16383b | 411 | float ini_x = LT_Voltage_Output[idx]; |
Lightvalve | 30:8d561f16383b | 412 | float fin_x = LT_Voltage_Output[idx+1]; |
Lightvalve | 30:8d561f16383b | 413 | float ini_y = LT_PWM_duty[idx]; |
Lightvalve | 30:8d561f16383b | 414 | float fin_y = LT_PWM_duty[idx+1]; |
Lightvalve | 13:747daba9cf59 | 415 | if(Ref_V>=ini_x && Ref_V<fin_x) { |
Lightvalve | 13:747daba9cf59 | 416 | PWM_duty = (fin_y-ini_y)/(fin_x-ini_x)*(Ref_V-ini_x) + ini_y; |
Lightvalve | 13:747daba9cf59 | 417 | break; |
Lightvalve | 13:747daba9cf59 | 418 | } |
Lightvalve | 13:747daba9cf59 | 419 | } |
Lightvalve | 13:747daba9cf59 | 420 | } |
Lightvalve | 14:8e7590227d22 | 421 | |
Lightvalve | 13:747daba9cf59 | 422 | return PWM_duty; |
Lightvalve | 13:747daba9cf59 | 423 | } |
jobuuu | 6:df07d3491e3a | 424 | |
Lightvalve | 55:f4819de54e7a | 425 | |
Lightvalve | 55:f4819de54e7a | 426 | |
jobuuu | 2:a1c0a37df760 | 427 | /******************************************************************************* |
jobuuu | 2:a1c0a37df760 | 428 | TIMER INTERRUPT |
jobuuu | 2:a1c0a37df760 | 429 | *******************************************************************************/ |
jobuuu | 2:a1c0a37df760 | 430 | |
jobuuu | 218:2c3e5ecbe7e1 | 431 | //------------------------------------------------ |
jobuuu | 218:2c3e5ecbe7e1 | 432 | // TMR4 : Sensor Read & Data Handling |
jobuuu | 218:2c3e5ecbe7e1 | 433 | //----------------------------------------------- |
Lightvalve | 50:b46bed7fec80 | 434 | float FREQ_TMR4 = (float)FREQ_20k; |
Lightvalve | 50:b46bed7fec80 | 435 | float DT_TMR4 = (float)DT_20k; |
Lightvalve | 55:f4819de54e7a | 436 | long CNT_TMR4 = 0; |
Lightvalve | 55:f4819de54e7a | 437 | int TMR4_FREQ_10k = (int)FREQ_10k; |
jobuuu | 1:e04e563be5ce | 438 | extern "C" void TIM4_IRQHandler(void) |
jobuuu | 1:e04e563be5ce | 439 | { |
Lightvalve | 19:23b7c1ad8683 | 440 | if (TIM4->SR & TIM_SR_UIF ) { |
Lightvalve | 13:747daba9cf59 | 441 | |
jobuuu | 218:2c3e5ecbe7e1 | 442 | // Current =================================================== |
jobuuu | 218:2c3e5ecbe7e1 | 443 | //ADC3->CR2 |= 0x40000000; // adc _ 12bit |
jobuuu | 218:2c3e5ecbe7e1 | 444 | |
jobuuu | 218:2c3e5ecbe7e1 | 445 | cur.UpdateSen(((float)ADC3->DR-2047.5f)/2047.5f*10.0f, FREQ_TMR4, 500.0f); // unit : mA |
jobuuu | 218:2c3e5ecbe7e1 | 446 | |
jobuuu | 218:2c3e5ecbe7e1 | 447 | // Encoder =================================================== |
Lightvalve | 55:f4819de54e7a | 448 | if (CNT_TMR4 % (int) ((int) FREQ_TMR4/TMR4_FREQ_10k) == 0) { |
Lightvalve | 55:f4819de54e7a | 449 | ENC_UPDATE(); |
Lightvalve | 55:f4819de54e7a | 450 | } |
Lightvalve | 59:bc8c8270f0ab | 451 | |
jobuuu | 218:2c3e5ecbe7e1 | 452 | // Force or Pressure Transducer ============================================= |
Lightvalve | 59:bc8c8270f0ab | 453 | ADC1->CR2 |= 0x40000000; |
jobuuu | 218:2c3e5ecbe7e1 | 454 | if (SENSING_MODE == 0) { // Force sensing |
jobuuu | 218:2c3e5ecbe7e1 | 455 | force.UpdateSen((((float)ADC1->DR) - 2047.5f)/TORQUE_SENSOR_PULSE_PER_TORQUE, FREQ_TMR4, 100.0f); // unit : N |
jobuuu | 218:2c3e5ecbe7e1 | 456 | } else if (SENSING_MODE == 1) { // Pressure sensing |
jobuuu | 218:2c3e5ecbe7e1 | 457 | float pres_A_new, pres_B_new; |
hyhoon | 214:82a3ca333004 | 458 | if (DIR_VALVE_ENC > 0) { |
jobuuu | 218:2c3e5ecbe7e1 | 459 | pres_A_new = (((float)ADC1->DR) - PRES_A_NULL_pulse)/ PRES_SENSOR_A_PULSE_PER_BAR; // unit : bar |
jobuuu | 218:2c3e5ecbe7e1 | 460 | pres_B_new = (((float)ADC2->DR) - PRES_B_NULL_pulse)/ PRES_SENSOR_A_PULSE_PER_BAR; |
hyhoon | 214:82a3ca333004 | 461 | } else { |
jobuuu | 218:2c3e5ecbe7e1 | 462 | pres_A_new = (((float)ADC2->DR) - PRES_A_NULL_pulse)/ PRES_SENSOR_A_PULSE_PER_BAR; // unit : bar |
jobuuu | 218:2c3e5ecbe7e1 | 463 | pres_B_new = (((float)ADC1->DR) - PRES_B_NULL_pulse)/ PRES_SENSOR_A_PULSE_PER_BAR; |
hyhoon | 214:82a3ca333004 | 464 | } |
jobuuu | 218:2c3e5ecbe7e1 | 465 | pres_A.UpdateSen(pres_A_new,FREQ_TMR4,200.0f); |
jobuuu | 218:2c3e5ecbe7e1 | 466 | pres_B.UpdateSen(pres_B_new,FREQ_TMR4,200.0f); |
Lightvalve | 49:3c630b5eba9f | 467 | |
Lightvalve | 56:2eade98630e2 | 468 | if ((OPERATING_MODE & 0x01) == 0) { // Rotary Actuator |
jobuuu | 218:2c3e5ecbe7e1 | 469 | float torq_new = (PISTON_AREA_A * pres_A.sen - PISTON_AREA_B * pres_B.sen) * 0.0001f; // mm^3*bar >> Nm |
jobuuu | 218:2c3e5ecbe7e1 | 470 | torq.UpdateSen(torq_new,FREQ_TMR4,1000.0); // unit : Nm |
Lightvalve | 56:2eade98630e2 | 471 | } else if ((OPERATING_MODE & 0x01) == 1) { // Linear Actuator |
jobuuu | 218:2c3e5ecbe7e1 | 472 | float force_new = (PISTON_AREA_A * pres_A.sen - PISTON_AREA_B * pres_B.sen) * 0.1f; // mm^2*bar >> N |
jobuuu | 218:2c3e5ecbe7e1 | 473 | force.UpdateSen(force_new,FREQ_TMR4,1000.0); // unit : N |
Lightvalve | 56:2eade98630e2 | 474 | } |
Lightvalve | 56:2eade98630e2 | 475 | } |
Lightvalve | 59:bc8c8270f0ab | 476 | |
Lightvalve | 55:f4819de54e7a | 477 | CNT_TMR4++; |
Lightvalve | 21:e5f1a43ea6f9 | 478 | } |
Lightvalve | 11:82d8768d7351 | 479 | TIM4->SR = 0x0; // reset the status register |
Lightvalve | 11:82d8768d7351 | 480 | } |
Lightvalve | 19:23b7c1ad8683 | 481 | |
Lightvalve | 19:23b7c1ad8683 | 482 | |
Lightvalve | 18:b8adf1582ea3 | 483 | int j =0; |
Lightvalve | 52:647072f5307a | 484 | float FREQ_TMR3 = (float)FREQ_5k; |
Lightvalve | 47:889798ff9329 | 485 | float DT_TMR3 = (float)DT_5k; |
Lightvalve | 44:35fa6884d0c6 | 486 | int cnt_trans = 0; |
Lightvalve | 47:889798ff9329 | 487 | double VALVE_POS_RAW_FORCE_FB_LOGGING = 0.0f; |
Lightvalve | 55:f4819de54e7a | 488 | int can_rest =0; |
Lightvalve | 47:889798ff9329 | 489 | |
Lightvalve | 11:82d8768d7351 | 490 | extern "C" void TIM3_IRQHandler(void) |
Lightvalve | 21:e5f1a43ea6f9 | 491 | { |
Lightvalve | 19:23b7c1ad8683 | 492 | if (TIM3->SR & TIM_SR_UIF ) { |
Lightvalve | 55:f4819de54e7a | 493 | |
Lightvalve | 49:3c630b5eba9f | 494 | if(MODE_POS_FT_TRANS == 1) { |
jobuuu | 218:2c3e5ecbe7e1 | 495 | if (alpha_trans == 1.0f) MODE_POS_FT_TRANS = 2; |
Lightvalve | 47:889798ff9329 | 496 | alpha_trans = (float)(1.0f - cos(3.141592f * (float)cnt_trans * DT_TMR3 /3.0f))/2.0f; |
Lightvalve | 44:35fa6884d0c6 | 497 | cnt_trans++; |
Lightvalve | 45:2694daea349b | 498 | torq.err_sum = 0; |
Lightvalve | 47:889798ff9329 | 499 | if((float)cnt_trans * DT_TMR3 > 3.0f) |
Lightvalve | 44:35fa6884d0c6 | 500 | MODE_POS_FT_TRANS = 2; |
Lightvalve | 49:3c630b5eba9f | 501 | } else if(MODE_POS_FT_TRANS == 3) { |
jobuuu | 218:2c3e5ecbe7e1 | 502 | if (alpha_trans == 0.0f) MODE_POS_FT_TRANS = 0; |
Lightvalve | 47:889798ff9329 | 503 | alpha_trans = (float)(1.0f + cos(3.141592f * (float)cnt_trans * DT_TMR3 /3.0f))/2.0f; |
Lightvalve | 44:35fa6884d0c6 | 504 | cnt_trans++; |
Lightvalve | 45:2694daea349b | 505 | torq.err_sum = 0; |
Lightvalve | 47:889798ff9329 | 506 | if((float) cnt_trans * DT_TMR3 > 3.0f ) |
Lightvalve | 44:35fa6884d0c6 | 507 | MODE_POS_FT_TRANS = 0; |
Lightvalve | 49:3c630b5eba9f | 508 | } else if(MODE_POS_FT_TRANS == 2) { |
Lightvalve | 56:2eade98630e2 | 509 | alpha_trans = 1.0f; |
Lightvalve | 44:35fa6884d0c6 | 510 | cnt_trans = 0; |
Lightvalve | 49:3c630b5eba9f | 511 | } else { |
Lightvalve | 56:2eade98630e2 | 512 | alpha_trans = 0.0f; |
Lightvalve | 44:35fa6884d0c6 | 513 | cnt_trans = 0; |
Lightvalve | 44:35fa6884d0c6 | 514 | } |
Lightvalve | 44:35fa6884d0c6 | 515 | |
Lightvalve | 49:3c630b5eba9f | 516 | |
jobuuu | 218:2c3e5ecbe7e1 | 517 | // Reference Update ========================================================== |
hyhoon | 215:699c3e572283 | 518 | switch (REFERENCE_MODE) { |
hyhoon | 215:699c3e572283 | 519 | case MODE_REF_NO_ACT: { |
hyhoon | 215:699c3e572283 | 520 | break; |
hyhoon | 215:699c3e572283 | 521 | } |
hyhoon | 215:699c3e572283 | 522 | case MODE_REF_DIRECT: { |
hyhoon | 215:699c3e572283 | 523 | pos.ref = REF_POSITION; |
hyhoon | 215:699c3e572283 | 524 | vel.ref = REF_VELOCITY; |
hyhoon | 215:699c3e572283 | 525 | torq.ref = REF_TORQUE; |
jobuuu | 218:2c3e5ecbe7e1 | 526 | force.ref = REF_FORCE; |
jobuuu | 218:2c3e5ecbe7e1 | 527 | break; |
jobuuu | 218:2c3e5ecbe7e1 | 528 | } |
jobuuu | 218:2c3e5ecbe7e1 | 529 | case MODE_REF_FINDHOME: { |
jobuuu | 218:2c3e5ecbe7e1 | 530 | pos.ref = REF_POSITION_FINDHOME; |
jobuuu | 218:2c3e5ecbe7e1 | 531 | vel.ref = 0.0f; |
jobuuu | 218:2c3e5ecbe7e1 | 532 | torq.ref = 0.0f; |
jobuuu | 218:2c3e5ecbe7e1 | 533 | force.ref = 0.0f; |
hyhoon | 215:699c3e572283 | 534 | break; |
hyhoon | 215:699c3e572283 | 535 | } |
hyhoon | 215:699c3e572283 | 536 | default: |
hyhoon | 215:699c3e572283 | 537 | break; |
hyhoon | 215:699c3e572283 | 538 | } |
hyhoon | 215:699c3e572283 | 539 | |
jobuuu | 218:2c3e5ecbe7e1 | 540 | if (((OPERATING_MODE&0b010)>>1) == 0) { |
jobuuu | 218:2c3e5ecbe7e1 | 541 | K_v = 1.03f; // Q = K_v*sqrt(deltaP)*tanh(C_d*Xv); |
jobuuu | 218:2c3e5ecbe7e1 | 542 | K_v = 0.16f; |
jobuuu | 218:2c3e5ecbe7e1 | 543 | mV_PER_mA = 500.0f; // 5000mV/10mA |
jobuuu | 218:2c3e5ecbe7e1 | 544 | mV_PER_pulse = 0.5f; // 5000mV/10000pulse |
jobuuu | 218:2c3e5ecbe7e1 | 545 | mA_PER_pulse = 0.001f; // 10mA/10000pulse |
jobuuu | 218:2c3e5ecbe7e1 | 546 | } else if (((OPERATING_MODE&0b010)>>1) == 1) { |
jobuuu | 218:2c3e5ecbe7e1 | 547 | K_v = 0.5f; // KNR (LPM >> mA) , 100bar |
jobuuu | 218:2c3e5ecbe7e1 | 548 | mV_PER_mA = 166.6666f; // 5000mV/30mA |
jobuuu | 218:2c3e5ecbe7e1 | 549 | mV_PER_pulse = 0.5f; // 5000mV/10000pulse |
jobuuu | 218:2c3e5ecbe7e1 | 550 | mA_PER_pulse = 0.003f; // 30mA/10000pulse |
jobuuu | 218:2c3e5ecbe7e1 | 551 | } |
jobuuu | 218:2c3e5ecbe7e1 | 552 | |
jobuuu | 218:2c3e5ecbe7e1 | 553 | // ===================================================================== |
jobuuu | 218:2c3e5ecbe7e1 | 554 | // CONTROL LOOP -------------------------------------------------------- |
jobuuu | 218:2c3e5ecbe7e1 | 555 | // ===================================================================== |
Lightvalve | 55:f4819de54e7a | 556 | int UTILITY_MODE = 0; |
Lightvalve | 55:f4819de54e7a | 557 | int CONTROL_MODE = 0; |
Lightvalve | 56:2eade98630e2 | 558 | |
Lightvalve | 55:f4819de54e7a | 559 | if (CONTROL_UTILITY_MODE >= 20 || CONTROL_UTILITY_MODE == 0) { |
Lightvalve | 55:f4819de54e7a | 560 | UTILITY_MODE = CONTROL_UTILITY_MODE; |
Lightvalve | 55:f4819de54e7a | 561 | CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 55:f4819de54e7a | 562 | } else { |
Lightvalve | 55:f4819de54e7a | 563 | CONTROL_MODE = CONTROL_UTILITY_MODE; |
Lightvalve | 55:f4819de54e7a | 564 | UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 55:f4819de54e7a | 565 | } |
Lightvalve | 55:f4819de54e7a | 566 | // UTILITY MODE ------------------------------------------------------------ |
Lightvalve | 55:f4819de54e7a | 567 | switch (UTILITY_MODE) { |
Lightvalve | 55:f4819de54e7a | 568 | case MODE_NO_ACT: { |
Lightvalve | 13:747daba9cf59 | 569 | break; |
Lightvalve | 13:747daba9cf59 | 570 | } |
Lightvalve | 14:8e7590227d22 | 571 | |
jobuuu | 218:2c3e5ecbe7e1 | 572 | case MODE_TORQUE_SENSOR_NULLING: |
jobuuu | 218:2c3e5ecbe7e1 | 573 | { |
jobuuu | 218:2c3e5ecbe7e1 | 574 | static float FORCE_pulse_sum = 0.0; |
jobuuu | 218:2c3e5ecbe7e1 | 575 | static float PresA_pulse_sum = 0.0; |
jobuuu | 218:2c3e5ecbe7e1 | 576 | static float PresB_pulse_sum = 0.0; |
jobuuu | 218:2c3e5ecbe7e1 | 577 | |
Lightvalve | 13:747daba9cf59 | 578 | // DAC Voltage reference set |
jobuuu | 218:2c3e5ecbe7e1 | 579 | float VREF_TuningGain = 0.000003f; |
jobuuu | 218:2c3e5ecbe7e1 | 580 | if (TMR3_COUNT_TORQUE_NULL < TMR_FREQ_5k * 5) { |
jobuuu | 218:2c3e5ecbe7e1 | 581 | LED = 1; |
jobuuu | 218:2c3e5ecbe7e1 | 582 | if(SENSING_MODE == 0) { // Force Sensor (Loadcell) |
jobuuu | 218:2c3e5ecbe7e1 | 583 | FORCE_pulse_sum = FORCE_pulse_sum + force.sen*TORQUE_SENSOR_PULSE_PER_TORQUE; |
jobuuu | 218:2c3e5ecbe7e1 | 584 | if (TMR3_COUNT_TORQUE_NULL % 10 == 0) { |
jobuuu | 218:2c3e5ecbe7e1 | 585 | float FORCE_pluse_mean = FORCE_pulse_sum / 10.0f; |
jobuuu | 218:2c3e5ecbe7e1 | 586 | FORCE_pulse_sum = 0.0f; |
Lightvalve | 19:23b7c1ad8683 | 587 | |
jobuuu | 218:2c3e5ecbe7e1 | 588 | FORCE_VREF += VREF_TuningGain * (0.0f - FORCE_pluse_mean); |
jobuuu | 218:2c3e5ecbe7e1 | 589 | if (FORCE_VREF > 3.3f) FORCE_VREF = 3.3f; |
jobuuu | 218:2c3e5ecbe7e1 | 590 | if (FORCE_VREF < 0.0f) FORCE_VREF = 0.0f; |
jobuuu | 218:2c3e5ecbe7e1 | 591 | dac_1 = FORCE_VREF / 3.3f; |
jobuuu | 218:2c3e5ecbe7e1 | 592 | } |
jobuuu | 218:2c3e5ecbe7e1 | 593 | } else if (SENSING_MODE == 1) { // Pressure Sensor |
jobuuu | 218:2c3e5ecbe7e1 | 594 | PresA_pulse_sum += pres_A.sen*PRES_SENSOR_A_PULSE_PER_BAR; |
jobuuu | 218:2c3e5ecbe7e1 | 595 | PresB_pulse_sum += pres_B.sen*PRES_SENSOR_B_PULSE_PER_BAR; |
jobuuu | 218:2c3e5ecbe7e1 | 596 | if (TMR3_COUNT_TORQUE_NULL % 10 == 0) { |
jobuuu | 218:2c3e5ecbe7e1 | 597 | float PresA_pluse_mean = PresA_pulse_sum / 10.0f; |
jobuuu | 218:2c3e5ecbe7e1 | 598 | float PresB_pluse_mean = PresB_pulse_sum / 10.0f; |
jobuuu | 218:2c3e5ecbe7e1 | 599 | PresA_pulse_sum = 0.0f; |
jobuuu | 218:2c3e5ecbe7e1 | 600 | PresB_pulse_sum = 0.0f; |
Lightvalve | 19:23b7c1ad8683 | 601 | |
jobuuu | 218:2c3e5ecbe7e1 | 602 | PRES_A_VREF += VREF_TuningGain * (0.0f - PresA_pluse_mean); |
jobuuu | 218:2c3e5ecbe7e1 | 603 | if (PRES_A_VREF > 3.3f) PRES_A_VREF = 3.3f; |
jobuuu | 218:2c3e5ecbe7e1 | 604 | if (PRES_A_VREF < 0.0f) PRES_A_VREF = 0.0f; |
jobuuu | 218:2c3e5ecbe7e1 | 605 | dac_1 = PRES_A_VREF / 3.3f; |
jobuuu | 218:2c3e5ecbe7e1 | 606 | PRES_B_VREF += VREF_TuningGain * (0.0f - PresB_pluse_mean); |
jobuuu | 218:2c3e5ecbe7e1 | 607 | if (PRES_B_VREF > 3.3f) PRES_B_VREF = 3.3f; |
jobuuu | 218:2c3e5ecbe7e1 | 608 | if (PRES_B_VREF < 0.0f) PRES_B_VREF = 0.0f; |
jobuuu | 218:2c3e5ecbe7e1 | 609 | dac_2 = PRES_B_VREF / 3.3f; |
jobuuu | 218:2c3e5ecbe7e1 | 610 | } |
Lightvalve | 13:747daba9cf59 | 611 | } |
jobuuu | 218:2c3e5ecbe7e1 | 612 | TMR3_COUNT_TORQUE_NULL++; |
Lightvalve | 13:747daba9cf59 | 613 | } else { |
jobuuu | 218:2c3e5ecbe7e1 | 614 | if(SENSING_MODE == 0 ) { // Force Sensor (Loadcell) |
jobuuu | 218:2c3e5ecbe7e1 | 615 | FORCE_pulse_sum = 0.0f; |
jobuuu | 218:2c3e5ecbe7e1 | 616 | dac_1 = FORCE_VREF / 3.3f; |
jobuuu | 218:2c3e5ecbe7e1 | 617 | spi_eeprom_write(RID_FORCE_SENSOR_VREF, (int16_t)(FORCE_VREF * 1000.0f)); |
jobuuu | 218:2c3e5ecbe7e1 | 618 | } else if (SENSING_MODE == 1) { |
jobuuu | 218:2c3e5ecbe7e1 | 619 | PresA_pulse_sum = 0.0f; |
jobuuu | 218:2c3e5ecbe7e1 | 620 | PresB_pulse_sum = 0.0f; |
jobuuu | 218:2c3e5ecbe7e1 | 621 | dac_1 = PRES_A_VREF / 3.3f; |
jobuuu | 218:2c3e5ecbe7e1 | 622 | dac_2 = PRES_B_VREF / 3.3f; |
jobuuu | 218:2c3e5ecbe7e1 | 623 | spi_eeprom_write(RID_PRES_A_SENSOR_VREF, (int16_t)(PRES_A_VREF * 1000.0f)); |
jobuuu | 218:2c3e5ecbe7e1 | 624 | spi_eeprom_write(RID_PRES_B_SENSOR_VREF, (int16_t)(PRES_B_VREF * 1000.0f)); |
jobuuu | 218:2c3e5ecbe7e1 | 625 | } |
Lightvalve | 56:2eade98630e2 | 626 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 14:8e7590227d22 | 627 | TMR3_COUNT_TORQUE_NULL = 0; |
Lightvalve | 13:747daba9cf59 | 628 | } |
Lightvalve | 13:747daba9cf59 | 629 | break; |
Lightvalve | 19:23b7c1ad8683 | 630 | } |
Lightvalve | 14:8e7590227d22 | 631 | |
jobuuu | 218:2c3e5ecbe7e1 | 632 | case MODE_FIND_HOME: |
jobuuu | 218:2c3e5ecbe7e1 | 633 | { |
jobuuu | 218:2c3e5ecbe7e1 | 634 | static int cnt_findhome = 0; |
jobuuu | 218:2c3e5ecbe7e1 | 635 | static int cnt_terminate_findhome = 0; |
jobuuu | 218:2c3e5ecbe7e1 | 636 | static float FINDHOME_POSITION_pulse = 0.0f; |
jobuuu | 218:2c3e5ecbe7e1 | 637 | static float FINDHOME_POSITION_pulse_OLD = 0.0f; |
jobuuu | 218:2c3e5ecbe7e1 | 638 | static float FINDHOME_VELOCITY_pulse = 0.0f; |
jobuuu | 218:2c3e5ecbe7e1 | 639 | static float REF_POSITION_FINDHOME_INIT = 0.0f; |
jobuuu | 218:2c3e5ecbe7e1 | 640 | |
Lightvalve | 29:69f3f5445d6d | 641 | if (FINDHOME_STAGE == FINDHOME_INIT) { |
jobuuu | 218:2c3e5ecbe7e1 | 642 | REFERENCE_MODE = MODE_REF_FINDHOME; |
Lightvalve | 14:8e7590227d22 | 643 | cnt_findhome = 0; |
jobuuu | 218:2c3e5ecbe7e1 | 644 | cnt_terminate_findhome = 0; |
Lightvalve | 57:f308b1656d9c | 645 | pos.ref = pos.sen; |
Lightvalve | 57:f308b1656d9c | 646 | vel.ref = 0.0f; |
jobuuu | 218:2c3e5ecbe7e1 | 647 | REF_POSITION_FINDHOME = pos.ref; |
Lightvalve | 29:69f3f5445d6d | 648 | FINDHOME_STAGE = FINDHOME_GOTOLIMIT; |
Lightvalve | 29:69f3f5445d6d | 649 | } else if (FINDHOME_STAGE == FINDHOME_GOTOLIMIT) { |
jobuuu | 218:2c3e5ecbe7e1 | 650 | int cnt_check_enc = (TMR_FREQ_5k/20); // 5000/20 = 250tic = 50msec |
Lightvalve | 29:69f3f5445d6d | 651 | if(cnt_findhome%cnt_check_enc == 0) { |
jobuuu | 218:2c3e5ecbe7e1 | 652 | FINDHOME_POSITION_pulse = pos.sen*ENC_PULSE_PER_POSITION; |
jobuuu | 218:2c3e5ecbe7e1 | 653 | FINDHOME_VELOCITY_pulse = FINDHOME_POSITION_pulse - FINDHOME_POSITION_pulse_OLD; |
jobuuu | 218:2c3e5ecbe7e1 | 654 | FINDHOME_POSITION_pulse_OLD = FINDHOME_POSITION_pulse; |
Lightvalve | 29:69f3f5445d6d | 655 | } |
Lightvalve | 29:69f3f5445d6d | 656 | cnt_findhome++; |
Lightvalve | 14:8e7590227d22 | 657 | |
jobuuu | 218:2c3e5ecbe7e1 | 658 | if (fabs(FINDHOME_VELOCITY_pulse) <= 1) { |
jobuuu | 218:2c3e5ecbe7e1 | 659 | cnt_terminate_findhome = cnt_terminate_findhome + 1; |
Lightvalve | 29:69f3f5445d6d | 660 | } else { |
jobuuu | 218:2c3e5ecbe7e1 | 661 | cnt_terminate_findhome = 0; |
Lightvalve | 29:69f3f5445d6d | 662 | } |
Lightvalve | 19:23b7c1ad8683 | 663 | |
jobuuu | 218:2c3e5ecbe7e1 | 664 | if ((cnt_terminate_findhome < 3*TMR_FREQ_5k) && cnt_findhome < 10*TMR_FREQ_5k) { // wait for 3sec |
jobuuu | 218:2c3e5ecbe7e1 | 665 | double GOTOHOME_SPEED = 10.0f; // 20mm/s or 20deg/s |
hyhoon | 214:82a3ca333004 | 666 | if (HOMEPOS_OFFSET > 0) { |
jobuuu | 218:2c3e5ecbe7e1 | 667 | REF_POSITION_FINDHOME = REF_POSITION_FINDHOME + GOTOHOME_SPEED*DT_5k; |
hyhoon | 214:82a3ca333004 | 668 | } else { |
jobuuu | 218:2c3e5ecbe7e1 | 669 | REF_POSITION_FINDHOME = REF_POSITION_FINDHOME - GOTOHOME_SPEED*DT_5k; |
hyhoon | 214:82a3ca333004 | 670 | } |
Lightvalve | 57:f308b1656d9c | 671 | CONTROL_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 57:f308b1656d9c | 672 | alpha_trans = 0.0f; |
Lightvalve | 29:69f3f5445d6d | 673 | } else { |
jobuuu | 218:2c3e5ecbe7e1 | 674 | ENC_SET((long)((long)HOMEPOS_OFFSET*10)); |
jobuuu | 218:2c3e5ecbe7e1 | 675 | REF_POSITION_FINDHOME_INIT = (float)((long)HOMEPOS_OFFSET*10); |
jobuuu | 218:2c3e5ecbe7e1 | 676 | FINDHOME_POSITION_pulse = 0; |
jobuuu | 218:2c3e5ecbe7e1 | 677 | FINDHOME_POSITION_pulse_OLD = 0; |
jobuuu | 218:2c3e5ecbe7e1 | 678 | FINDHOME_VELOCITY_pulse = 0; |
jobuuu | 218:2c3e5ecbe7e1 | 679 | |
Lightvalve | 29:69f3f5445d6d | 680 | cnt_findhome = 0; |
jobuuu | 218:2c3e5ecbe7e1 | 681 | cnt_terminate_findhome = 0; |
jobuuu | 218:2c3e5ecbe7e1 | 682 | pos.ref = 0.0f; |
Lightvalve | 29:69f3f5445d6d | 683 | FINDHOME_STAGE = FINDHOME_ZEROPOSE; |
Lightvalve | 29:69f3f5445d6d | 684 | } |
Lightvalve | 29:69f3f5445d6d | 685 | } else if (FINDHOME_STAGE == FINDHOME_ZEROPOSE) { |
jobuuu | 218:2c3e5ecbe7e1 | 686 | |
Lightvalve | 29:69f3f5445d6d | 687 | int T_move = 2*TMR_FREQ_5k; |
jobuuu | 218:2c3e5ecbe7e1 | 688 | 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; |
Lightvalve | 65:c2812cf26c38 | 689 | |
Lightvalve | 29:69f3f5445d6d | 690 | cnt_findhome++; |
Lightvalve | 29:69f3f5445d6d | 691 | if (cnt_findhome >= T_move) { |
Lightvalve | 29:69f3f5445d6d | 692 | cnt_findhome = 0; |
Lightvalve | 30:8d561f16383b | 693 | pos.ref = 0.0f; |
Lightvalve | 29:69f3f5445d6d | 694 | FINDHOME_STAGE = FINDHOME_INIT; |
Lightvalve | 55:f4819de54e7a | 695 | CONTROL_UTILITY_MODE = MODE_JOINT_CONTROL; |
jobuuu | 218:2c3e5ecbe7e1 | 696 | REFERENCE_MODE = MODE_REF_DIRECT; |
hyhoon | 214:82a3ca333004 | 697 | } |
Lightvalve | 13:747daba9cf59 | 698 | } |
Lightvalve | 13:747daba9cf59 | 699 | break; |
Lightvalve | 13:747daba9cf59 | 700 | } |
Lightvalve | 55:f4819de54e7a | 701 | default: |
Lightvalve | 55:f4819de54e7a | 702 | break; |
Lightvalve | 55:f4819de54e7a | 703 | } |
Lightvalve | 55:f4819de54e7a | 704 | |
Lightvalve | 55:f4819de54e7a | 705 | // CONTROL MODE ------------------------------------------------------------ |
Lightvalve | 55:f4819de54e7a | 706 | switch (CONTROL_MODE) { |
Lightvalve | 55:f4819de54e7a | 707 | case MODE_NO_ACT: { |
Lightvalve | 55:f4819de54e7a | 708 | V_out = 0.0f; |
Lightvalve | 55:f4819de54e7a | 709 | break; |
Lightvalve | 55:f4819de54e7a | 710 | } |
Lightvalve | 55:f4819de54e7a | 711 | |
Lightvalve | 55:f4819de54e7a | 712 | case MODE_VALVE_POSITION_CONTROL: { |
Lightvalve | 65:c2812cf26c38 | 713 | if (OPERATING_MODE == 5) { //SW Valve |
Lightvalve | 55:f4819de54e7a | 714 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 57:f308b1656d9c | 715 | V_out = Vout.ref; |
Lightvalve | 65:c2812cf26c38 | 716 | } else if (CURRENT_CONTROL_MODE == 0) { //PWM |
jobuuu | 218:2c3e5ecbe7e1 | 717 | V_out = valve_pos.ref; |
Lightvalve | 164:645207e160ca | 718 | } else { |
jobuuu | 218:2c3e5ecbe7e1 | 719 | I_REF = valve_pos.ref * 0.001f; // Unit : pulse >> mA |
jobuuu | 218:2c3e5ecbe7e1 | 720 | float I_MAX = 10.0f; // Max : 10mA |
jobuuu | 218:2c3e5ecbe7e1 | 721 | if (I_REF > I_MAX) { |
jobuuu | 218:2c3e5ecbe7e1 | 722 | I_REF = I_MAX; |
jobuuu | 218:2c3e5ecbe7e1 | 723 | } else if (I_REF < -I_MAX) { |
jobuuu | 218:2c3e5ecbe7e1 | 724 | I_REF = -I_MAX; |
jobuuu | 218:2c3e5ecbe7e1 | 725 | } |
Lightvalve | 55:f4819de54e7a | 726 | } |
Lightvalve | 55:f4819de54e7a | 727 | break; |
Lightvalve | 55:f4819de54e7a | 728 | } |
Lightvalve | 55:f4819de54e7a | 729 | |
jobuuu | 218:2c3e5ecbe7e1 | 730 | case MODE_JOINT_CONTROL: |
jobuuu | 218:2c3e5ecbe7e1 | 731 | { |
Lightvalve | 55:f4819de54e7a | 732 | |
jobuuu | 218:2c3e5ecbe7e1 | 733 | float temp_vel_pos = 0.0f; // desired velocity for position control |
jobuuu | 218:2c3e5ecbe7e1 | 734 | float temp_vel_FT = 0.0f; // desired velocity for force/torque control |
jobuuu | 218:2c3e5ecbe7e1 | 735 | float temp_vel_ff = 0.0f; // desired velocity for feedforward control |
jobuuu | 218:2c3e5ecbe7e1 | 736 | float temp_vel = 0.0f; |
Lightvalve | 56:2eade98630e2 | 737 | |
jobuuu | 218:2c3e5ecbe7e1 | 738 | float wn_Pos = 2.0f * PI * 5.0f; // f_cut : 5Hz Position Control |
jobuuu | 218:2c3e5ecbe7e1 | 739 | |
jobuuu | 218:2c3e5ecbe7e1 | 740 | pos.err = pos.ref - pos.sen; // Unit : mm or deg |
jobuuu | 218:2c3e5ecbe7e1 | 741 | vel.err = vel.ref - vel.sen; // Unit : mm/s or deg/s |
jobuuu | 218:2c3e5ecbe7e1 | 742 | |
jobuuu | 218:2c3e5ecbe7e1 | 743 | // position control command =============================================================================================================================================== |
jobuuu | 218:2c3e5ecbe7e1 | 744 | if ((OPERATING_MODE & 0x01) == 0) { // Rotary Mode |
jobuuu | 218:2c3e5ecbe7e1 | 745 | temp_vel_pos = 0.01f * (P_GAIN_JOINT_POSITION * wn_Pos * pos.err) * PI / 180.0f; // rad/s |
jobuuu | 218:2c3e5ecbe7e1 | 746 | // L when P-gain = 100, f_cut = 10Hz |
jobuuu | 218:2c3e5ecbe7e1 | 747 | } else { |
jobuuu | 218:2c3e5ecbe7e1 | 748 | temp_vel_pos = 0.01f * (P_GAIN_JOINT_POSITION * wn_Pos * pos.err); // mm/s |
jobuuu | 218:2c3e5ecbe7e1 | 749 | // L when P-gain = 100, f_cut = 10Hz |
jobuuu | 218:2c3e5ecbe7e1 | 750 | } |
Lightvalve | 55:f4819de54e7a | 751 | |
jobuuu | 218:2c3e5ecbe7e1 | 752 | // torque control command =============================================================================================================================================== |
jobuuu | 218:2c3e5ecbe7e1 | 753 | float alpha_SpringDamper = 1.0f/(1.0f+TMR_FREQ_5k/(2.0f*PI*30.0f)); |
jobuuu | 218:2c3e5ecbe7e1 | 754 | K_LPF = alpha_SpringDamper * K_LPF + (1.0f-alpha_SpringDamper) * K_SPRING; |
jobuuu | 218:2c3e5ecbe7e1 | 755 | D_LPF = alpha_SpringDamper * D_LPF + (1.0f-alpha_SpringDamper) * D_DAMPER; |
jobuuu | 218:2c3e5ecbe7e1 | 756 | |
jobuuu | 218:2c3e5ecbe7e1 | 757 | if ((OPERATING_MODE & 0x01) == 0) { // Rotary Mode |
jobuuu | 218:2c3e5ecbe7e1 | 758 | float torq_ref_act = torq.ref + K_SPRING * pos.err + D_DAMPER * vel.err; // unit : Nm |
jobuuu | 218:2c3e5ecbe7e1 | 759 | torq.err = torq_ref_act - torq.sen; |
jobuuu | 218:2c3e5ecbe7e1 | 760 | torq.err_int += torq.err/((float)TMR_FREQ_5k); |
jobuuu | 218:2c3e5ecbe7e1 | 761 | temp_vel_FT = 0.0001f * (P_GAIN_JOINT_TORQUE * torq.err + I_GAIN_JOINT_TORQUE * torq.err_int); // Nm >> rad/s |
Lightvalve | 55:f4819de54e7a | 762 | } else { |
jobuuu | 218:2c3e5ecbe7e1 | 763 | float force_ref_act = force.ref + K_SPRING * pos.err + D_DAMPER * vel.err; // unit : N |
jobuuu | 218:2c3e5ecbe7e1 | 764 | force.err = force_ref_act - force.sen; |
jobuuu | 218:2c3e5ecbe7e1 | 765 | force.err_int += force.err/((float)TMR_FREQ_5k); |
jobuuu | 218:2c3e5ecbe7e1 | 766 | temp_vel_FT = 0.0001f * (P_GAIN_JOINT_TORQUE * force.err + I_GAIN_JOINT_TORQUE * force.err_int); // N >> mm/s |
jobuuu | 218:2c3e5ecbe7e1 | 767 | } |
jobuuu | 218:2c3e5ecbe7e1 | 768 | |
jobuuu | 218:2c3e5ecbe7e1 | 769 | // velocity feedforward command ======================================================================================================================================== |
jobuuu | 218:2c3e5ecbe7e1 | 770 | if ((OPERATING_MODE & 0x01) == 0) { // Rotary Mode |
jobuuu | 218:2c3e5ecbe7e1 | 771 | temp_vel_ff = 0.01f * (float)VELOCITY_COMP_GAIN * vel.ref * PI / 180.0f; // rad/s |
jobuuu | 218:2c3e5ecbe7e1 | 772 | } else { |
jobuuu | 218:2c3e5ecbe7e1 | 773 | temp_vel_ff = 0.01f * (float)VELOCITY_COMP_GAIN * vel.ref; // mm/s |
jobuuu | 218:2c3e5ecbe7e1 | 774 | } |
Lightvalve | 164:645207e160ca | 775 | |
jobuuu | 218:2c3e5ecbe7e1 | 776 | // command integration ================================================================================================================================================= |
jobuuu | 218:2c3e5ecbe7e1 | 777 | temp_vel = (1.0f - alpha_trans) * temp_vel_pos + alpha_trans * temp_vel_FT + temp_vel_ff; // Position Control + Torque Control + Velocity Feedforward |
jobuuu | 218:2c3e5ecbe7e1 | 778 | |
jobuuu | 218:2c3e5ecbe7e1 | 779 | float Qact = 0.0f; // required flow rate |
jobuuu | 218:2c3e5ecbe7e1 | 780 | if( temp_vel > 0.0f ) { |
jobuuu | 218:2c3e5ecbe7e1 | 781 | Qact = temp_vel * ((float)PISTON_AREA_A * 0.00006f); // mm^3/sec >> LPM |
jobuuu | 218:2c3e5ecbe7e1 | 782 | I_REF = tanh_inv(Qact/(K_v * sqrt(PRES_SUPPLY * alpha3 / (alpha3 + 1.0f))))/C_d; |
jobuuu | 218:2c3e5ecbe7e1 | 783 | } else { |
jobuuu | 218:2c3e5ecbe7e1 | 784 | Qact = temp_vel * ((float)PISTON_AREA_B * 0.00006f); // mm^3/sec >> LPM |
jobuuu | 218:2c3e5ecbe7e1 | 785 | I_REF = tanh_inv(Qact/(K_v * sqrt(PRES_SUPPLY / (alpha3 + 1.0f))))/C_d; |
jobuuu | 218:2c3e5ecbe7e1 | 786 | } |
jobuuu | 218:2c3e5ecbe7e1 | 787 | |
jobuuu | 218:2c3e5ecbe7e1 | 788 | // Anti-windup for FT |
jobuuu | 218:2c3e5ecbe7e1 | 789 | if (I_GAIN_JOINT_TORQUE != 0.0f) { |
jobuuu | 218:2c3e5ecbe7e1 | 790 | float I_MAX = 10.0f; // Maximum Current : 10mA |
jobuuu | 218:2c3e5ecbe7e1 | 791 | float Ka = 2.0f; |
jobuuu | 218:2c3e5ecbe7e1 | 792 | if (I_REF > I_MAX) { |
jobuuu | 218:2c3e5ecbe7e1 | 793 | float I_rem = I_REF - I_MAX; |
jobuuu | 218:2c3e5ecbe7e1 | 794 | I_REF = I_MAX; |
jobuuu | 218:2c3e5ecbe7e1 | 795 | 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] |
jobuuu | 218:2c3e5ecbe7e1 | 796 | torq.err_int = torq.err_int - Ka * temp_vel_rem * (10000.0f/I_GAIN_JOINT_TORQUE)*(float)TMR_FREQ_5k; // Need to Check! |
jobuuu | 218:2c3e5ecbe7e1 | 797 | } else if (I_REF < -I_MAX) { |
jobuuu | 218:2c3e5ecbe7e1 | 798 | double I_rem = I_REF - (-I_MAX); |
jobuuu | 218:2c3e5ecbe7e1 | 799 | I_REF = -I_MAX; |
jobuuu | 218:2c3e5ecbe7e1 | 800 | 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] |
jobuuu | 218:2c3e5ecbe7e1 | 801 | torq.err_int = torq.err_int - Ka * temp_vel_rem * (10000.0f/I_GAIN_JOINT_TORQUE)*(float)TMR_FREQ_5k; // Need to Check! |
Lightvalve | 55:f4819de54e7a | 802 | } |
Lightvalve | 55:f4819de54e7a | 803 | } |
Lightvalve | 55:f4819de54e7a | 804 | break; |
Lightvalve | 55:f4819de54e7a | 805 | } |
Lightvalve | 56:2eade98630e2 | 806 | |
Lightvalve | 55:f4819de54e7a | 807 | case MODE_VALVE_OPEN_LOOP: { |
Lightvalve | 55:f4819de54e7a | 808 | V_out = (float) Vout.ref; |
Lightvalve | 55:f4819de54e7a | 809 | break; |
Lightvalve | 55:f4819de54e7a | 810 | } |
Lightvalve | 164:645207e160ca | 811 | |
jobuuu | 218:2c3e5ecbe7e1 | 812 | // case MODE_JOINT_ADAPTIVE_BACKSTEPPING: { |
jobuuu | 218:2c3e5ecbe7e1 | 813 | // |
jobuuu | 218:2c3e5ecbe7e1 | 814 | // 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 | 218:2c3e5ecbe7e1 | 815 | // 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 | 218:2c3e5ecbe7e1 | 816 | // |
jobuuu | 218:2c3e5ecbe7e1 | 817 | // V_adapt = 1.0f / (1.0f/Va + 1.0f/Vb); //initial 0.0000053f |
jobuuu | 218:2c3e5ecbe7e1 | 818 | // |
jobuuu | 218:2c3e5ecbe7e1 | 819 | // //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 | 218:2c3e5ecbe7e1 | 820 | // float f3_hat = -a_hat * vel.sen/(float)(ENC_PULSE_PER_POSITION)*0.001f; // unit : N/s //xdot=10mm/s일때 -137076 |
jobuuu | 218:2c3e5ecbe7e1 | 821 | // |
jobuuu | 218:2c3e5ecbe7e1 | 822 | // float g3_prime = 0.0f; |
jobuuu | 218:2c3e5ecbe7e1 | 823 | // if (torq.sen > Amm*(Ps-Pt)*0.000001f) { |
jobuuu | 218:2c3e5ecbe7e1 | 824 | // g3_prime = 1.0f; |
jobuuu | 218:2c3e5ecbe7e1 | 825 | // } else if (torq.sen < -Amm*(Ps-Pt)*0.000001f) { |
jobuuu | 218:2c3e5ecbe7e1 | 826 | // g3_prime = -1.0f; |
jobuuu | 218:2c3e5ecbe7e1 | 827 | // } else { |
jobuuu | 218:2c3e5ecbe7e1 | 828 | // if ((value-VALVE_CENTER) > 0) { |
jobuuu | 218:2c3e5ecbe7e1 | 829 | // g3_prime = sqrt(Ps-Pt-torq.sen/Amm*1000000.0f); |
jobuuu | 218:2c3e5ecbe7e1 | 830 | //// g3_prime = sqrt(Ps-Pt); |
jobuuu | 218:2c3e5ecbe7e1 | 831 | // } else { |
jobuuu | 218:2c3e5ecbe7e1 | 832 | // g3_prime = sqrt(Ps-Pt+torq.sen/Amm*1000000.0f); |
jobuuu | 218:2c3e5ecbe7e1 | 833 | //// g3_prime = sqrt(Ps-Pt); |
jobuuu | 218:2c3e5ecbe7e1 | 834 | // } |
jobuuu | 218:2c3e5ecbe7e1 | 835 | // } |
jobuuu | 218:2c3e5ecbe7e1 | 836 | // float tau = 0.01f; |
jobuuu | 218:2c3e5ecbe7e1 | 837 | // float K_valve = 0.0004f; |
jobuuu | 218:2c3e5ecbe7e1 | 838 | // |
jobuuu | 218:2c3e5ecbe7e1 | 839 | // float x_v = 0.0f; //x_v : -1~1 |
jobuuu | 218:2c3e5ecbe7e1 | 840 | // if(value>=VALVE_CENTER) { |
jobuuu | 218:2c3e5ecbe7e1 | 841 | // x_v = 1.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER); |
jobuuu | 218:2c3e5ecbe7e1 | 842 | // } else { |
jobuuu | 218:2c3e5ecbe7e1 | 843 | // x_v = -1.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER); |
jobuuu | 218:2c3e5ecbe7e1 | 844 | // } |
jobuuu | 218:2c3e5ecbe7e1 | 845 | // float f4 = -x_v/tau; |
jobuuu | 218:2c3e5ecbe7e1 | 846 | // float g4 = K_valve/tau; |
jobuuu | 218:2c3e5ecbe7e1 | 847 | // |
jobuuu | 218:2c3e5ecbe7e1 | 848 | // float torq_ref_dot = torq.ref_diff * 500.0f; |
jobuuu | 218:2c3e5ecbe7e1 | 849 | // |
jobuuu | 218:2c3e5ecbe7e1 | 850 | // pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm] |
jobuuu | 218:2c3e5ecbe7e1 | 851 | // vel.err = (0.0f - vel.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm/s] |
jobuuu | 218:2c3e5ecbe7e1 | 852 | // pos.err_sum += pos.err/(float) TMR_FREQ_5k; //[mm] |
jobuuu | 218:2c3e5ecbe7e1 | 853 | // |
jobuuu | 218:2c3e5ecbe7e1 | 854 | // torq.err = torq.ref - torq.sen; //[N] |
jobuuu | 218:2c3e5ecbe7e1 | 855 | // torq.err_sum += torq.err/(float) TMR_FREQ_5k; //[N] |
jobuuu | 218:2c3e5ecbe7e1 | 856 | // |
jobuuu | 218:2c3e5ecbe7e1 | 857 | // float k3 = 2000.0f; //2000 //20000 |
jobuuu | 218:2c3e5ecbe7e1 | 858 | // float k4 = 10.0f; |
jobuuu | 218:2c3e5ecbe7e1 | 859 | // float rho3 = 3.2f; |
jobuuu | 218:2c3e5ecbe7e1 | 860 | // float rho4 = 10000000.0f; //25000000.0f; |
jobuuu | 218:2c3e5ecbe7e1 | 861 | // float x_4_des = (-f3_hat + torq_ref_dot - k3*(-torq.err))/(gamma_hat*g3_prime); |
jobuuu | 218:2c3e5ecbe7e1 | 862 | // if (x_4_des > 1) x_4_des = 1; |
jobuuu | 218:2c3e5ecbe7e1 | 863 | // else if (x_4_des < -1) x_4_des = -1; |
jobuuu | 218:2c3e5ecbe7e1 | 864 | // |
jobuuu | 218:2c3e5ecbe7e1 | 865 | // if (x_4_des > 0) { |
jobuuu | 218:2c3e5ecbe7e1 | 866 | // valve_pos.ref = x_4_des * (float)(VALVE_MAX_POS - VALVE_CENTER) + (float) VALVE_CENTER; |
jobuuu | 218:2c3e5ecbe7e1 | 867 | // } else { |
jobuuu | 218:2c3e5ecbe7e1 | 868 | // valve_pos.ref = x_4_des * (float)(VALVE_CENTER - VALVE_MIN_POS) + (float) VALVE_CENTER; |
jobuuu | 218:2c3e5ecbe7e1 | 869 | // } |
jobuuu | 218:2c3e5ecbe7e1 | 870 | // |
jobuuu | 218:2c3e5ecbe7e1 | 871 | // float x_4_des_dot = (x_4_des - x_4_des_old)*(float) TMR_FREQ_5k; |
jobuuu | 218:2c3e5ecbe7e1 | 872 | // x_4_des_old = x_4_des; |
jobuuu | 218:2c3e5ecbe7e1 | 873 | // V_out = (-f4 + x_4_des_dot - k4*(x_v-x_4_des)- rho3/rho4*gamma_hat*g3_prime*(-torq.err))/g4; |
jobuuu | 218:2c3e5ecbe7e1 | 874 | // |
jobuuu | 218:2c3e5ecbe7e1 | 875 | // float rho_a = 0.00001f; |
jobuuu | 218:2c3e5ecbe7e1 | 876 | // float a_hat_dot = -rho3/rho_a*vel.sen/(float)(ENC_PULSE_PER_POSITION)*0.001f*(-torq.err); |
jobuuu | 218:2c3e5ecbe7e1 | 877 | // a_hat = a_hat + a_hat_dot / (float) TMR_FREQ_5k; |
jobuuu | 218:2c3e5ecbe7e1 | 878 | // |
jobuuu | 218:2c3e5ecbe7e1 | 879 | // if(a_hat > -3000000.0f) a_hat = -3000000.0f; |
jobuuu | 218:2c3e5ecbe7e1 | 880 | // else if(a_hat < -30000000.0f) a_hat = -30000000.0f; |
jobuuu | 218:2c3e5ecbe7e1 | 881 | // |
jobuuu | 218:2c3e5ecbe7e1 | 882 | // break; |
jobuuu | 218:2c3e5ecbe7e1 | 883 | // } |
Lightvalve | 166:bfc1fd2629d8 | 884 | |
Lightvalve | 12:6f2531038ea4 | 885 | default: |
Lightvalve | 12:6f2531038ea4 | 886 | break; |
Lightvalve | 12:6f2531038ea4 | 887 | } |
Lightvalve | 14:8e7590227d22 | 888 | |
Lightvalve | 55:f4819de54e7a | 889 | |
Lightvalve | 55:f4819de54e7a | 890 | if (((OPERATING_MODE&0b110)>>1) == 0 || ((OPERATING_MODE&0b110)>>1) == 1) { //Moog Valve or KNR Valve |
Lightvalve | 56:2eade98630e2 | 891 | |
Lightvalve | 55:f4819de54e7a | 892 | //////////////////////////////////////////////////////////////////////////// |
Lightvalve | 55:f4819de54e7a | 893 | //////////////////////////// CURRENT CONTROL ////////////////////////////// |
Lightvalve | 55:f4819de54e7a | 894 | //////////////////////////////////////////////////////////////////////////// |
Lightvalve | 55:f4819de54e7a | 895 | if (CURRENT_CONTROL_MODE) { |
Lightvalve | 65:c2812cf26c38 | 896 | double alpha_update_Iref = 1.0f / (1.0f + 5000.0f / (2.0f * 3.14f * 300.0f)); // f_cutoff : 500Hz |
Lightvalve | 55:f4819de54e7a | 897 | I_REF_fil = (1.0f - alpha_update_Iref) * I_REF_fil + alpha_update_Iref*I_REF; |
Lightvalve | 55:f4819de54e7a | 898 | |
jobuuu | 218:2c3e5ecbe7e1 | 899 | if (I_REF_fil > 0.0f) I_REF_fil_DZ = I_REF_fil + (double)VALVE_DEADZONE_PLUS*mA_PER_pulse; // unit: mA |
jobuuu | 218:2c3e5ecbe7e1 | 900 | else if (I_REF_fil < 0.0f) I_REF_fil_DZ = I_REF_fil + (double)VALVE_DEADZONE_MINUS*mA_PER_pulse; // unit: mA |
jobuuu | 218:2c3e5ecbe7e1 | 901 | else I_REF_fil_DZ = I_REF_fil + (double)(VALVE_DEADZONE_PLUS+VALVE_DEADZONE_MINUS)/2.0f*mA_PER_pulse; // unit: mA |
jobuuu | 218:2c3e5ecbe7e1 | 902 | |
hyhoon | 214:82a3ca333004 | 903 | I_ERR = I_REF_fil_DZ - (double)cur.sen; |
Lightvalve | 65:c2812cf26c38 | 904 | I_ERR_INT = I_ERR_INT + (I_ERR) * 0.0002f; |
Lightvalve | 55:f4819de54e7a | 905 | |
Lightvalve | 55:f4819de54e7a | 906 | |
Lightvalve | 55:f4819de54e7a | 907 | // Moog Valve Current Control Gain |
Lightvalve | 65:c2812cf26c38 | 908 | double R_model = 500.0f; // ohm |
Lightvalve | 55:f4819de54e7a | 909 | double L_model = 1.2f; |
Lightvalve | 55:f4819de54e7a | 910 | double w0 = 2.0f * 3.14f * 150.0f; |
Lightvalve | 55:f4819de54e7a | 911 | double KP_I = 0.1f * L_model*w0; |
Lightvalve | 55:f4819de54e7a | 912 | double KI_I = 0.1f * R_model*w0; |
Lightvalve | 55:f4819de54e7a | 913 | |
Lightvalve | 55:f4819de54e7a | 914 | // KNR Valve Current Control Gain |
Lightvalve | 55:f4819de54e7a | 915 | if (((OPERATING_MODE & 0b110)>>1) == 1) { // KNR Valve |
Lightvalve | 55:f4819de54e7a | 916 | R_model = 163.0f; // ohm |
Lightvalve | 55:f4819de54e7a | 917 | L_model = 1.0f; |
Lightvalve | 55:f4819de54e7a | 918 | w0 = 2.0f * 3.14f * 80.0f; |
Lightvalve | 55:f4819de54e7a | 919 | KP_I = 1.0f * L_model*w0; |
Lightvalve | 55:f4819de54e7a | 920 | KI_I = 0.08f * R_model*w0; |
Lightvalve | 55:f4819de54e7a | 921 | } |
Lightvalve | 55:f4819de54e7a | 922 | |
Lightvalve | 55:f4819de54e7a | 923 | double FF_gain = 1.0f; |
Lightvalve | 55:f4819de54e7a | 924 | |
Lightvalve | 65:c2812cf26c38 | 925 | VALVE_PWM_RAW = KP_I * 2.0f * I_ERR + KI_I * 2.0f* I_ERR_INT; |
hyhoon | 214:82a3ca333004 | 926 | I_REF_fil_diff = I_REF_fil_DZ - I_REF_fil_old; |
hyhoon | 214:82a3ca333004 | 927 | I_REF_fil_old = I_REF_fil_DZ; |
Lightvalve | 65:c2812cf26c38 | 928 | // VALVE_PWM_RAW = VALVE_PWM_RAW + FF_gain * (R_model * I_REF_fil + L_model * I_REF_fil_diff * 5000.0f); // Unit : mV |
hyhoon | 214:82a3ca333004 | 929 | VALVE_PWM_RAW = VALVE_PWM_RAW + FF_gain * (R_model * I_REF_fil_DZ); // Unit : mV |
Lightvalve | 65:c2812cf26c38 | 930 | double V_MAX = 12000.0f; // Maximum Voltage : 12V = 12000mV |
Lightvalve | 55:f4819de54e7a | 931 | |
Lightvalve | 55:f4819de54e7a | 932 | double Ka = 3.0f / KP_I; |
Lightvalve | 55:f4819de54e7a | 933 | if (VALVE_PWM_RAW > V_MAX) { |
Lightvalve | 55:f4819de54e7a | 934 | V_rem = VALVE_PWM_RAW - V_MAX; |
Lightvalve | 55:f4819de54e7a | 935 | V_rem = Ka*V_rem; |
Lightvalve | 55:f4819de54e7a | 936 | VALVE_PWM_RAW = V_MAX; |
Lightvalve | 65:c2812cf26c38 | 937 | I_ERR_INT = I_ERR_INT - V_rem * 0.0002f; |
Lightvalve | 55:f4819de54e7a | 938 | } else if (VALVE_PWM_RAW < -V_MAX) { |
Lightvalve | 55:f4819de54e7a | 939 | V_rem = VALVE_PWM_RAW - (-V_MAX); |
Lightvalve | 55:f4819de54e7a | 940 | V_rem = Ka*V_rem; |
Lightvalve | 55:f4819de54e7a | 941 | VALVE_PWM_RAW = -V_MAX; |
Lightvalve | 65:c2812cf26c38 | 942 | I_ERR_INT = I_ERR_INT - V_rem * 0.0002f; |
Lightvalve | 55:f4819de54e7a | 943 | } |
Lightvalve | 55:f4819de54e7a | 944 | } else { |
Lightvalve | 55:f4819de54e7a | 945 | VALVE_PWM_RAW = I_REF * mV_PER_mA; |
Lightvalve | 55:f4819de54e7a | 946 | } |
Lightvalve | 55:f4819de54e7a | 947 | |
Lightvalve | 55:f4819de54e7a | 948 | //////////////////////////////////////////////////////////////////////////// |
Lightvalve | 55:f4819de54e7a | 949 | ///////////////// Dead Zone Cancellation & Linearization ////////////////// |
Lightvalve | 55:f4819de54e7a | 950 | //////////////////////////////////////////////////////////////////////////// |
Lightvalve | 55:f4819de54e7a | 951 | |
Lightvalve | 55:f4819de54e7a | 952 | // Output Voltage Linearization |
hyhoon | 214:82a3ca333004 | 953 | double CUR_PWM_nonlin = (double)VALVE_PWM_RAW; // Unit : mV |
Lightvalve | 65:c2812cf26c38 | 954 | double CUR_PWM_lin = PWM_duty_byLT(CUR_PWM_nonlin); // -8000~8000 |
Lightvalve | 55:f4819de54e7a | 955 | |
Lightvalve | 55:f4819de54e7a | 956 | // Dead Zone Cancellation (Electrical dead-zone) |
Lightvalve | 65:c2812cf26c38 | 957 | if (CUR_PWM_lin > 0) V_out = (float) (CUR_PWM_lin + 169.0f); |
Lightvalve | 65:c2812cf26c38 | 958 | else if (CUR_PWM_lin < 0) V_out = (float) (CUR_PWM_lin - 174.0f); |
Lightvalve | 65:c2812cf26c38 | 959 | else V_out = (float) (CUR_PWM_lin); |
jobuuu | 218:2c3e5ecbe7e1 | 960 | |
Lightvalve | 164:645207e160ca | 961 | } else { //////////////////////////sw valve |
Lightvalve | 212:985dba42f261 | 962 | // Output Voltage Linearization & Dead Zone Cancellation (Electrical dead-zone) by SW |
Lightvalve | 133:79885a39c161 | 963 | if (V_out > 0 ) V_out = (V_out + 180.0f)/0.8588f; |
Lightvalve | 133:79885a39c161 | 964 | else if (V_out < 0) V_out = (V_out - 200.0f)/0.8651f; |
Lightvalve | 133:79885a39c161 | 965 | else V_out = 0.0f; |
Lightvalve | 87:a7b45368ea0f | 966 | } |
Lightvalve | 164:645207e160ca | 967 | |
jobuuu | 218:2c3e5ecbe7e1 | 968 | //////////////////////////////////////////////////////////////////// |
jobuuu | 218:2c3e5ecbe7e1 | 969 | /////////////////// PWM Command /////////////////////////////////// |
jobuuu | 218:2c3e5ecbe7e1 | 970 | //////////////////////////////////////////////////////////////////// |
Lightvalve | 164:645207e160ca | 971 | if(DIR_VALVE<0) { |
Lightvalve | 65:c2812cf26c38 | 972 | V_out = -V_out; |
Lightvalve | 65:c2812cf26c38 | 973 | } |
Lightvalve | 164:645207e160ca | 974 | |
Lightvalve | 48:e7bcfc244d40 | 975 | if (V_out >= VALVE_VOLTAGE_LIMIT*1000.0f) { |
Lightvalve | 48:e7bcfc244d40 | 976 | V_out = VALVE_VOLTAGE_LIMIT*1000.0f; |
Lightvalve | 48:e7bcfc244d40 | 977 | } else if(V_out<=-VALVE_VOLTAGE_LIMIT*1000.0f) { |
Lightvalve | 48:e7bcfc244d40 | 978 | V_out = -VALVE_VOLTAGE_LIMIT*1000.0f; |
Lightvalve | 27:a2254a485f23 | 979 | } |
Lightvalve | 212:985dba42f261 | 980 | PWM_out= V_out/(SUPPLY_VOLTAGE*1000.0f); |
Lightvalve | 13:747daba9cf59 | 981 | |
Lightvalve | 212:985dba42f261 | 982 | // Saturation of output voltage |
Lightvalve | 30:8d561f16383b | 983 | if(PWM_out > 1.0f) PWM_out=1.0f; |
Lightvalve | 30:8d561f16383b | 984 | else if (PWM_out < -1.0f) PWM_out=-1.0f; |
Lightvalve | 13:747daba9cf59 | 985 | |
Lightvalve | 30:8d561f16383b | 986 | if (PWM_out>0.0f) { |
Lightvalve | 30:8d561f16383b | 987 | dtc_v=0.0f; |
jobuuu | 1:e04e563be5ce | 988 | dtc_w=PWM_out; |
jobuuu | 1:e04e563be5ce | 989 | } else { |
jobuuu | 2:a1c0a37df760 | 990 | dtc_v=-PWM_out; |
Lightvalve | 30:8d561f16383b | 991 | dtc_w=0.0f; |
jobuuu | 1:e04e563be5ce | 992 | } |
Lightvalve | 13:747daba9cf59 | 993 | |
jobuuu | 1:e04e563be5ce | 994 | //pwm |
Lightvalve | 30:8d561f16383b | 995 | TIM4->CCR2 = (PWM_ARR)*(1.0f-dtc_v); |
Lightvalve | 30:8d561f16383b | 996 | TIM4->CCR1 = (PWM_ARR)*(1.0f-dtc_w); |
Lightvalve | 65:c2812cf26c38 | 997 | |
jobuuu | 218:2c3e5ecbe7e1 | 998 | //////////////////////////////////////////////////////////////////////////// |
jobuuu | 218:2c3e5ecbe7e1 | 999 | ////////////////////// Data transmission through CAN ////////////////////// |
jobuuu | 218:2c3e5ecbe7e1 | 1000 | //////////////////////////////////////////////////////////////////////////// |
jobuuu | 218:2c3e5ecbe7e1 | 1001 | |
hyhoon | 214:82a3ca333004 | 1002 | if (TMR2_COUNT_CAN_TX % (int) ((int) TMR_FREQ_5k/CAN_FREQ) == 0) { |
jobuuu | 218:2c3e5ecbe7e1 | 1003 | |
hyhoon | 214:82a3ca333004 | 1004 | // Position, Velocity, and Torque (ID:1200) |
hyhoon | 214:82a3ca333004 | 1005 | if (flag_data_request[0] == HIGH) { |
hyhoon | 214:82a3ca333004 | 1006 | if ((OPERATING_MODE & 0b01) == 0) { // Rotary Actuator |
Lightvalve | 219:e00e71ca3e80 | 1007 | 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*8.0f)); |
hyhoon | 214:82a3ca333004 | 1008 | } else if ((OPERATING_MODE & 0b01) == 1) { // Linear Actuator |
Lightvalve | 219:e00e71ca3e80 | 1009 | 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*8.0f)); |
hyhoon | 214:82a3ca333004 | 1010 | } |
hyhoon | 214:82a3ca333004 | 1011 | } |
hyhoon | 214:82a3ca333004 | 1012 | |
jobuuu | 218:2c3e5ecbe7e1 | 1013 | // Valve Position (ID:1300) |
Lightvalve | 54:6f50d9d3bfee | 1014 | if (flag_data_request[1] == HIGH) { |
jobuuu | 218:2c3e5ecbe7e1 | 1015 | CAN_TX_PWM((int16_t)(cur.sen/mA_PER_pulse)); |
Lightvalve | 52:647072f5307a | 1016 | } |
Lightvalve | 56:2eade98630e2 | 1017 | |
jobuuu | 218:2c3e5ecbe7e1 | 1018 | // Others : Pressure A, B, Supply Pressure, etc. (for Debugging) (ID:1400) |
Lightvalve | 166:bfc1fd2629d8 | 1019 | if (flag_data_request[2] == HIGH) { |
jobuuu | 218:2c3e5ecbe7e1 | 1020 | CAN_TX_SOMETHING((int16_t)(pres_A.sen*100.0f), (int16_t)(pres_B.sen*100.0f), (int16_t) (PRES_SUPPLY), (int16_t) (PRES_SUPPLY_NOM)); |
Lightvalve | 52:647072f5307a | 1021 | } |
Lightvalve | 20:806196fda269 | 1022 | |
Lightvalve | 52:647072f5307a | 1023 | TMR2_COUNT_CAN_TX = 0; |
Lightvalve | 52:647072f5307a | 1024 | } |
Lightvalve | 52:647072f5307a | 1025 | TMR2_COUNT_CAN_TX++; |
Lightvalve | 51:8ea76864368a | 1026 | |
Lightvalve | 20:806196fda269 | 1027 | } |
Lightvalve | 51:8ea76864368a | 1028 | TIM3->SR = 0x0; // reset the status register |
Lightvalve | 51:8ea76864368a | 1029 | |
Lightvalve | 56:2eade98630e2 | 1030 | } |