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