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