![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Original Version of STM Board
main.cpp@226:82a3ca333004, 2021-03-03 (annotated)
- Committer:
- hyhoon
- Date:
- Wed Mar 03 10:56:25 2021 +0000
- Revision:
- 226:82a3ca333004
- Parent:
- 225:9c0becc196ba
- Child:
- 227:699c3e572283
20210305;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Lightvalve | 224:985dba42f261 | 1 | //Hydraulic Control Board |
Lightvalve | 224:985dba42f261 | 2 | //distributed by Sungwoo Kim |
Lightvalve | 224:985dba42f261 | 3 | // 2020/12/28 |
Lightvalve | 224:985dba42f261 | 4 | |
Lightvalve | 224:985dba42f261 | 5 | |
Lightvalve | 224:985dba42f261 | 6 | // 유의사항 |
Lightvalve | 224:985dba42f261 | 7 | // 소수 적을때 뒤에 f 꼭 붙이기 |
Lightvalve | 224:985dba42f261 | 8 | // CAN 선은 ground까지 있는 3상 선으로 써야함. |
Lightvalve | 224:985dba42f261 | 9 | // 전원은 12~24V 인가. |
Lightvalve | 224:985dba42f261 | 10 | |
GiJeongKim | 0:51c43836c1d7 | 11 | #include "mbed.h" |
GiJeongKim | 0:51c43836c1d7 | 12 | #include "FastPWM.h" |
GiJeongKim | 0:51c43836c1d7 | 13 | #include "INIT_HW.h" |
jobuuu | 2:a1c0a37df760 | 14 | #include "function_CAN.h" |
GiJeongKim | 0:51c43836c1d7 | 15 | #include "SPI_EEP_ENC.h" |
GiJeongKim | 0:51c43836c1d7 | 16 | #include "I2C_AS5510.h" |
GiJeongKim | 0:51c43836c1d7 | 17 | #include "setting.h" |
Lightvalve | 11:82d8768d7351 | 18 | #include "function_utilities.h" |
Lightvalve | 16:903b5a4433b4 | 19 | #include "stm32f4xx_flash.h" |
Lightvalve | 169:645207e160ca | 20 | #include "FlashWriter.h" |
Lightvalve | 170:42c938a40313 | 21 | #include <string> |
Lightvalve | 170:42c938a40313 | 22 | #include <iostream> |
Lightvalve | 179:d5377766d7ea | 23 | #include <cmath> |
Lightvalve | 61:bc8c8270f0ab | 24 | |
Lightvalve | 61:bc8c8270f0ab | 25 | using namespace std; |
Lightvalve | 61:bc8c8270f0ab | 26 | Timer t; |
GiJeongKim | 0:51c43836c1d7 | 27 | |
jobuuu | 7:e9086c72bb22 | 28 | // dac & check /////////////////////////////////////////// |
GiJeongKim | 0:51c43836c1d7 | 29 | DigitalOut check(PC_2); |
GiJeongKim | 0:51c43836c1d7 | 30 | DigitalOut check_2(PC_3); |
GiJeongKim | 0:51c43836c1d7 | 31 | AnalogOut dac_1(PA_4); |
GiJeongKim | 0:51c43836c1d7 | 32 | AnalogOut dac_2(PA_5); |
Lightvalve | 14:8e7590227d22 | 33 | AnalogIn adc1(PC_4); //pressure_1 |
Lightvalve | 14:8e7590227d22 | 34 | AnalogIn adc2(PB_0); //pressure_2 |
Lightvalve | 14:8e7590227d22 | 35 | AnalogIn adc3(PC_1); //current |
GiJeongKim | 0:51c43836c1d7 | 36 | |
jobuuu | 7:e9086c72bb22 | 37 | // PWM /////////////////////////////////////////// |
Lightvalve | 30:8d561f16383b | 38 | float dtc_v=0.0f; |
Lightvalve | 30:8d561f16383b | 39 | float dtc_w=0.0f; |
GiJeongKim | 0:51c43836c1d7 | 40 | |
jobuuu | 7:e9086c72bb22 | 41 | // I2C /////////////////////////////////////////// |
GiJeongKim | 0:51c43836c1d7 | 42 | I2C i2c(PC_9,PA_8); // SDA, SCL (for K22F) |
Lightvalve | 224:985dba42f261 | 43 | const int i2c_slave_addr1 = 0x56; // AS5510 address |
GiJeongKim | 0:51c43836c1d7 | 44 | unsigned int value; // 10bit output of reading sensor AS5510 |
GiJeongKim | 0:51c43836c1d7 | 45 | |
jobuuu | 7:e9086c72bb22 | 46 | // SPI /////////////////////////////////////////// |
Lightvalve | 170:42c938a40313 | 47 | SPI eeprom(PB_15, PB_14, PB_13); // EEPROM //(SPI_MOSI, SPI_MISO, SPI_SCK); |
Lightvalve | 170:42c938a40313 | 48 | DigitalOut eeprom_cs(PB_12); |
GiJeongKim | 0:51c43836c1d7 | 49 | SPI enc(PC_12,PC_11,PC_10); |
GiJeongKim | 0:51c43836c1d7 | 50 | DigitalOut enc_cs(PD_2); |
Lightvalve | 11:82d8768d7351 | 51 | DigitalOut LED(PA_15); |
GiJeongKim | 0:51c43836c1d7 | 52 | |
jobuuu | 7:e9086c72bb22 | 53 | // UART /////////////////////////////////////////// |
GiJeongKim | 0:51c43836c1d7 | 54 | Serial pc(PA_9,PA_10); // _ UART |
GiJeongKim | 0:51c43836c1d7 | 55 | |
jobuuu | 7:e9086c72bb22 | 56 | // CAN /////////////////////////////////////////// |
GiJeongKim | 0:51c43836c1d7 | 57 | CAN can(PB_8, PB_9, 1000000); |
GiJeongKim | 0:51c43836c1d7 | 58 | CANMessage msg; |
Lightvalve | 11:82d8768d7351 | 59 | void onMsgReceived() |
Lightvalve | 11:82d8768d7351 | 60 | { |
Lightvalve | 11:82d8768d7351 | 61 | CAN_RX_HANDLER(); |
Lightvalve | 11:82d8768d7351 | 62 | } |
jobuuu | 2:a1c0a37df760 | 63 | |
jobuuu | 7:e9086c72bb22 | 64 | // Variables /////////////////////////////////////////// |
jobuuu | 7:e9086c72bb22 | 65 | State pos; |
jobuuu | 7:e9086c72bb22 | 66 | State vel; |
jobuuu | 7:e9086c72bb22 | 67 | State Vout; |
jobuuu | 7:e9086c72bb22 | 68 | State torq; |
Lightvalve | 207:c70c5a9f17dd | 69 | State torq_dot; |
jobuuu | 7:e9086c72bb22 | 70 | State pres_A; |
jobuuu | 7:e9086c72bb22 | 71 | State pres_B; |
jobuuu | 7:e9086c72bb22 | 72 | State cur; |
Lightvalve | 14:8e7590227d22 | 73 | State valve_pos; |
Lightvalve | 14:8e7590227d22 | 74 | |
Lightvalve | 14:8e7590227d22 | 75 | State INIT_Vout; |
Lightvalve | 14:8e7590227d22 | 76 | State INIT_Valve_Pos; |
Lightvalve | 14:8e7590227d22 | 77 | State INIT_Pos; |
Lightvalve | 14:8e7590227d22 | 78 | State INIT_torq; |
jobuuu | 5:a4319f79457b | 79 | |
Lightvalve | 19:23b7c1ad8683 | 80 | extern int CID_RX_CMD; |
Lightvalve | 19:23b7c1ad8683 | 81 | extern int CID_RX_REF_POSITION; |
Lightvalve | 49:e7bcfc244d40 | 82 | extern int CID_RX_REF_VALVE_POS; |
Lightvalve | 45:35fa6884d0c6 | 83 | extern int CID_RX_REF_PWM; |
Lightvalve | 19:23b7c1ad8683 | 84 | |
Lightvalve | 19:23b7c1ad8683 | 85 | extern int CID_TX_INFO; |
Lightvalve | 19:23b7c1ad8683 | 86 | extern int CID_TX_POSITION; |
Lightvalve | 19:23b7c1ad8683 | 87 | extern int CID_TX_TORQUE; |
Lightvalve | 19:23b7c1ad8683 | 88 | extern int CID_TX_PRES; |
Lightvalve | 19:23b7c1ad8683 | 89 | extern int CID_TX_VOUT; |
Lightvalve | 19:23b7c1ad8683 | 90 | extern int CID_TX_VALVE_POSITION; |
jeongyeseong | 225:9c0becc196ba | 91 | extern int CID_TX_SOMETHING; |
Lightvalve | 19:23b7c1ad8683 | 92 | |
hyhoon | 226:82a3ca333004 | 93 | float pres_A_new = 0.0f; |
hyhoon | 226:82a3ca333004 | 94 | float pres_B_new = 0.0f; |
Lightvalve | 61:bc8c8270f0ab | 95 | |
Lightvalve | 12:6f2531038ea4 | 96 | /******************************************************************************* |
Lightvalve | 12:6f2531038ea4 | 97 | * REFERENCE MODE |
Lightvalve | 12:6f2531038ea4 | 98 | ******************************************************************************/ |
Lightvalve | 13:747daba9cf59 | 99 | enum _REFERENCE_MODE { |
Lightvalve | 13:747daba9cf59 | 100 | MODE_REF_NO_ACT = 0, //0 |
Lightvalve | 224:985dba42f261 | 101 | MODE_REF_DIRECT, //1 |
Lightvalve | 224:985dba42f261 | 102 | MODE_REF_COS_INC, //2 |
Lightvalve | 224:985dba42f261 | 103 | MODE_REF_LINE_INC, //3 |
Lightvalve | 12:6f2531038ea4 | 104 | MODE_REF_SIN_WAVE, //4 |
Lightvalve | 224:985dba42f261 | 105 | MODE_REF_SQUARE_WAVE, //5 |
Lightvalve | 12:6f2531038ea4 | 106 | }; |
Lightvalve | 12:6f2531038ea4 | 107 | |
Lightvalve | 12:6f2531038ea4 | 108 | /******************************************************************************* |
Lightvalve | 12:6f2531038ea4 | 109 | * CONTROL MODE |
Lightvalve | 12:6f2531038ea4 | 110 | ******************************************************************************/ |
Lightvalve | 13:747daba9cf59 | 111 | enum _CONTROL_MODE { |
Lightvalve | 12:6f2531038ea4 | 112 | //control mode |
Lightvalve | 12:6f2531038ea4 | 113 | MODE_NO_ACT = 0, //0 |
Lightvalve | 47:fdcb8bd86fd6 | 114 | MODE_VALVE_POSITION_CONTROL, //1 |
Lightvalve | 47:fdcb8bd86fd6 | 115 | MODE_JOINT_CONTROL, //2 |
Lightvalve | 50:3c630b5eba9f | 116 | |
Lightvalve | 47:fdcb8bd86fd6 | 117 | MODE_VALVE_OPEN_LOOP, //3 |
Lightvalve | 138:a843f32ced33 | 118 | MODE_JOINT_ADAPTIVE_BACKSTEPPING, //4 |
Lightvalve | 171:bfc1fd2629d8 | 119 | MODE_RL, //5 |
Lightvalve | 13:747daba9cf59 | 120 | |
Lightvalve | 12:6f2531038ea4 | 121 | MODE_JOINT_POSITION_PRES_CONTROL_PWM, //6 |
Lightvalve | 12:6f2531038ea4 | 122 | MODE_JOINT_POSITION_PRES_CONTROL_VALVE_POSITION, //7 |
Lightvalve | 12:6f2531038ea4 | 123 | MODE_VALVE_POSITION_PRES_CONTROL_LEARNING, //8 |
Lightvalve | 13:747daba9cf59 | 124 | |
Lightvalve | 12:6f2531038ea4 | 125 | MODE_TEST_CURRENT_CONTROL, //9 |
Lightvalve | 12:6f2531038ea4 | 126 | MODE_TEST_PWM_CONTROL, //10 |
Lightvalve | 21:e5f1a43ea6f9 | 127 | |
Lightvalve | 14:8e7590227d22 | 128 | MODE_CURRENT_CONTROL, //11 |
Lightvalve | 14:8e7590227d22 | 129 | MODE_JOINT_POSITION_TORQUE_CONTROL_CURRENT, //12 |
Lightvalve | 14:8e7590227d22 | 130 | MODE_JOINT_POSITION_PRES_CONTROL_CURRENT, //13 |
Lightvalve | 224:985dba42f261 | 131 | MODE_VALVE_POSITION_TORQUE_CONTROL_LEARNING, //14 |
Lightvalve | 13:747daba9cf59 | 132 | |
Lightvalve | 12:6f2531038ea4 | 133 | //utility |
Lightvalve | 12:6f2531038ea4 | 134 | MODE_TORQUE_SENSOR_NULLING = 20, //20 |
Lightvalve | 12:6f2531038ea4 | 135 | MODE_VALVE_NULLING_AND_DEADZONE_SETTING, //21 |
Lightvalve | 12:6f2531038ea4 | 136 | MODE_FIND_HOME, //22 |
Lightvalve | 57:f4819de54e7a | 137 | MODE_VALVE_GAIN_SETTING, //23 |
Lightvalve | 12:6f2531038ea4 | 138 | MODE_PRESSURE_SENSOR_NULLING, //24 |
Lightvalve | 12:6f2531038ea4 | 139 | MODE_PRESSURE_SENSOR_CALIB, //25 |
Lightvalve | 12:6f2531038ea4 | 140 | MODE_ROTARY_FRICTION_TUNING, //26 |
Lightvalve | 13:747daba9cf59 | 141 | |
Lightvalve | 224:985dba42f261 | 142 | MODE_DDV_POS_VS_PWM_ID = 30, //30 |
Lightvalve | 12:6f2531038ea4 | 143 | MODE_DDV_DEADZONE_AND_CENTER, //31 |
Lightvalve | 12:6f2531038ea4 | 144 | MODE_DDV_POS_VS_FLOWRATE, //32 |
Lightvalve | 57:f4819de54e7a | 145 | MODE_SYSTEM_ID, //33 |
Lightvalve | 169:645207e160ca | 146 | MODE_FREQ_TEST, //34 |
Lightvalve | 169:645207e160ca | 147 | MODE_SEND_BUFFER, //35 |
Lightvalve | 169:645207e160ca | 148 | MODE_SEND_OVER, //36 |
Lightvalve | 169:645207e160ca | 149 | MODE_STEP_TEST, //37 |
Lightvalve | 12:6f2531038ea4 | 150 | }; |
Lightvalve | 12:6f2531038ea4 | 151 | |
Lightvalve | 65:a2d7c63419c2 | 152 | void SystemClock_Config(void) |
Lightvalve | 65:a2d7c63419c2 | 153 | { |
Lightvalve | 169:645207e160ca | 154 | RCC_OscInitTypeDef RCC_OscInitStruct = {0}; |
Lightvalve | 169:645207e160ca | 155 | RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; |
Lightvalve | 65:a2d7c63419c2 | 156 | |
Lightvalve | 224:985dba42f261 | 157 | /* Configure the main internal regulator output voltage |
Lightvalve | 169:645207e160ca | 158 | */ |
Lightvalve | 169:645207e160ca | 159 | __HAL_RCC_PWR_CLK_ENABLE(); |
Lightvalve | 169:645207e160ca | 160 | __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); |
Lightvalve | 224:985dba42f261 | 161 | /* Initializes the CPU, AHB and APB busses clocks |
Lightvalve | 169:645207e160ca | 162 | */ |
Lightvalve | 169:645207e160ca | 163 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; |
Lightvalve | 169:645207e160ca | 164 | RCC_OscInitStruct.HSIState = RCC_HSI_ON; |
Lightvalve | 169:645207e160ca | 165 | RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; |
Lightvalve | 169:645207e160ca | 166 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
Lightvalve | 169:645207e160ca | 167 | RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; |
Lightvalve | 169:645207e160ca | 168 | RCC_OscInitStruct.PLL.PLLM = 8;//8 |
Lightvalve | 169:645207e160ca | 169 | RCC_OscInitStruct.PLL.PLLN = 180; //180 |
Lightvalve | 169:645207e160ca | 170 | RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; |
Lightvalve | 169:645207e160ca | 171 | RCC_OscInitStruct.PLL.PLLQ = 2; |
Lightvalve | 169:645207e160ca | 172 | RCC_OscInitStruct.PLL.PLLR = 2; |
Lightvalve | 169:645207e160ca | 173 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { |
Lightvalve | 169:645207e160ca | 174 | //Error_Handler(); |
Lightvalve | 169:645207e160ca | 175 | } |
Lightvalve | 169:645207e160ca | 176 | /** Activate the Over-Drive mode |
Lightvalve | 169:645207e160ca | 177 | */ |
Lightvalve | 169:645207e160ca | 178 | if (HAL_PWREx_EnableOverDrive() != HAL_OK) { |
Lightvalve | 169:645207e160ca | 179 | //Error_Handler(); |
Lightvalve | 169:645207e160ca | 180 | } |
Lightvalve | 169:645207e160ca | 181 | /** Initializes the CPU, AHB and APB busses clocks |
Lightvalve | 169:645207e160ca | 182 | */ |
Lightvalve | 169:645207e160ca | 183 | RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |
Lightvalve | 169:645207e160ca | 184 | |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; |
Lightvalve | 169:645207e160ca | 185 | RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; |
Lightvalve | 169:645207e160ca | 186 | RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; |
Lightvalve | 169:645207e160ca | 187 | RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4; |
Lightvalve | 169:645207e160ca | 188 | RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; |
Lightvalve | 65:a2d7c63419c2 | 189 | |
Lightvalve | 169:645207e160ca | 190 | if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) { |
Lightvalve | 169:645207e160ca | 191 | //Error_Handler(); |
Lightvalve | 169:645207e160ca | 192 | } |
Lightvalve | 65:a2d7c63419c2 | 193 | } |
Lightvalve | 65:a2d7c63419c2 | 194 | |
Lightvalve | 170:42c938a40313 | 195 | |
GiJeongKim | 0:51c43836c1d7 | 196 | int main() |
GiJeongKim | 0:51c43836c1d7 | 197 | { |
jobuuu | 6:df07d3491e3a | 198 | /********************************* |
jobuuu | 1:e04e563be5ce | 199 | *** Initialization |
jobuuu | 6:df07d3491e3a | 200 | *********************************/ |
hyhoon | 226:82a3ca333004 | 201 | |
Lightvalve | 224:985dba42f261 | 202 | HAL_Init(); |
Lightvalve | 224:985dba42f261 | 203 | SystemClock_Config(); |
hyhoon | 226:82a3ca333004 | 204 | |
Lightvalve | 69:3995ffeaa786 | 205 | LED = 0; |
Lightvalve | 61:bc8c8270f0ab | 206 | pc.baud(9600); |
Lightvalve | 21:e5f1a43ea6f9 | 207 | |
GiJeongKim | 0:51c43836c1d7 | 208 | // i2c init |
hyhoon | 226:82a3ca333004 | 209 | // i2c.frequency(400 * 1000); // 0.4 mHz |
hyhoon | 226:82a3ca333004 | 210 | // wait_ms(2); // Power Up wait |
hyhoon | 226:82a3ca333004 | 211 | // look_for_hardware_i2c(); // Hardware present |
hyhoon | 226:82a3ca333004 | 212 | // init_as5510(i2c_slave_addr1); |
hyhoon | 226:82a3ca333004 | 213 | // make_delay(); |
jobuuu | 2:a1c0a37df760 | 214 | |
Lightvalve | 224:985dba42f261 | 215 | // spi init |
hyhoon | 226:82a3ca333004 | 216 | enc_cs = 1; |
Lightvalve | 170:42c938a40313 | 217 | eeprom.format(8,3); |
Lightvalve | 170:42c938a40313 | 218 | eeprom.frequency(5000000); //5M |
GiJeongKim | 0:51c43836c1d7 | 219 | enc.format(8,0); |
GiJeongKim | 0:51c43836c1d7 | 220 | enc.frequency(5000000); //5M |
hyhoon | 226:82a3ca333004 | 221 | enc_cs = 0; |
Lightvalve | 11:82d8768d7351 | 222 | make_delay(); |
Lightvalve | 21:e5f1a43ea6f9 | 223 | |
hyhoon | 226:82a3ca333004 | 224 | |
hyhoon | 226:82a3ca333004 | 225 | // spi _ enc |
hyhoon | 226:82a3ca333004 | 226 | spi_enc_set_init(); |
hyhoon | 226:82a3ca333004 | 227 | make_delay(); |
hyhoon | 226:82a3ca333004 | 228 | |
hyhoon | 226:82a3ca333004 | 229 | ////// bno rom |
hyhoon | 226:82a3ca333004 | 230 | // spi_eeprom_write(RID_BNO, (int16_t)1); |
hyhoon | 226:82a3ca333004 | 231 | // make_delay(); |
hyhoon | 226:82a3ca333004 | 232 | //////// |
hyhoon | 226:82a3ca333004 | 233 | |
Lightvalve | 224:985dba42f261 | 234 | // rom |
Lightvalve | 19:23b7c1ad8683 | 235 | ROM_CALL_DATA(); |
Lightvalve | 16:903b5a4433b4 | 236 | make_delay(); |
Lightvalve | 13:747daba9cf59 | 237 | |
GiJeongKim | 0:51c43836c1d7 | 238 | // ADC init |
jobuuu | 5:a4319f79457b | 239 | Init_ADC(); |
Lightvalve | 11:82d8768d7351 | 240 | make_delay(); |
jobuuu | 2:a1c0a37df760 | 241 | |
GiJeongKim | 0:51c43836c1d7 | 242 | // Pwm init |
GiJeongKim | 0:51c43836c1d7 | 243 | Init_PWM(); |
GiJeongKim | 0:51c43836c1d7 | 244 | TIM4->CR1 ^= TIM_CR1_UDIS; |
Lightvalve | 11:82d8768d7351 | 245 | make_delay(); |
Lightvalve | 13:747daba9cf59 | 246 | |
GiJeongKim | 0:51c43836c1d7 | 247 | // CAN |
jobuuu | 2:a1c0a37df760 | 248 | can.attach(&CAN_RX_HANDLER); |
Lightvalve | 11:82d8768d7351 | 249 | CAN_ID_INIT(); |
Lightvalve | 11:82d8768d7351 | 250 | make_delay(); |
Lightvalve | 34:bb2ca2fc2a8e | 251 | |
hyhoon | 226:82a3ca333004 | 252 | //can.reset(); |
hyhoon | 226:82a3ca333004 | 253 | can.filter(msg.id, 0xFFFFF000, CANStandard); |
hyhoon | 226:82a3ca333004 | 254 | |
hyhoon | 226:82a3ca333004 | 255 | // TMR3 init |
hyhoon | 226:82a3ca333004 | 256 | Init_TMR3(); |
hyhoon | 226:82a3ca333004 | 257 | TIM3->CR1 ^= TIM_CR1_UDIS; |
hyhoon | 226:82a3ca333004 | 258 | make_delay(); |
hyhoon | 226:82a3ca333004 | 259 | |
hyhoon | 226:82a3ca333004 | 260 | |
hyhoon | 226:82a3ca333004 | 261 | |
Lightvalve | 23:59218d4a256d | 262 | //Timer priority |
Lightvalve | 23:59218d4a256d | 263 | NVIC_SetPriority(TIM3_IRQn, 2); |
Lightvalve | 54:647072f5307a | 264 | NVIC_SetPriority(TIM4_IRQn, 3); |
Lightvalve | 34:bb2ca2fc2a8e | 265 | |
hyhoon | 226:82a3ca333004 | 266 | |
Lightvalve | 34:bb2ca2fc2a8e | 267 | |
hyhoon | 226:82a3ca333004 | 268 | |
Lightvalve | 13:747daba9cf59 | 269 | |
Lightvalve | 11:82d8768d7351 | 270 | //DAC init |
Lightvalve | 58:2eade98630e2 | 271 | if (SENSING_MODE == 0) { |
Lightvalve | 58:2eade98630e2 | 272 | dac_1 = TORQUE_VREF / 3.3f; |
Lightvalve | 58:2eade98630e2 | 273 | dac_2 = 0.0f; |
Lightvalve | 58:2eade98630e2 | 274 | } else if (SENSING_MODE == 1) { |
hyhoon | 226:82a3ca333004 | 275 | if (DIR_VALVE_ENC > 0) { |
hyhoon | 226:82a3ca333004 | 276 | dac_1 = PRES_A_VREF / 3.3f; |
hyhoon | 226:82a3ca333004 | 277 | dac_2 = PRES_B_VREF / 3.3f; |
hyhoon | 226:82a3ca333004 | 278 | } else { |
hyhoon | 226:82a3ca333004 | 279 | dac_1 = PRES_B_VREF / 3.3f; |
hyhoon | 226:82a3ca333004 | 280 | dac_2 = PRES_A_VREF / 3.3f; |
hyhoon | 226:82a3ca333004 | 281 | } |
Lightvalve | 58:2eade98630e2 | 282 | } |
Lightvalve | 11:82d8768d7351 | 283 | make_delay(); |
Lightvalve | 13:747daba9cf59 | 284 | |
Lightvalve | 19:23b7c1ad8683 | 285 | for (int i=0; i<50; i++) { |
Lightvalve | 11:82d8768d7351 | 286 | if(i%2==0) |
Lightvalve | 38:118df027d851 | 287 | ID_index_array[i] = - i * 0.5f; |
Lightvalve | 11:82d8768d7351 | 288 | else |
Lightvalve | 38:118df027d851 | 289 | ID_index_array[i] = (i+1) * 0.5f; |
Lightvalve | 11:82d8768d7351 | 290 | } |
Lightvalve | 169:645207e160ca | 291 | |
jobuuu | 6:df07d3491e3a | 292 | /************************************ |
jobuuu | 1:e04e563be5ce | 293 | *** Program is operating! |
jobuuu | 6:df07d3491e3a | 294 | *************************************/ |
GiJeongKim | 0:51c43836c1d7 | 295 | while(1) { |
Lightvalve | 169:645207e160ca | 296 | |
Lightvalve | 224:985dba42f261 | 297 | // UART example |
Lightvalve | 224:985dba42f261 | 298 | // if(timer_while==100000) { |
Lightvalve | 171:bfc1fd2629d8 | 299 | // timer_while = 0; |
Lightvalve | 224:985dba42f261 | 300 | // pc.printf("%f\n", value); |
Lightvalve | 171:bfc1fd2629d8 | 301 | // } |
Lightvalve | 224:985dba42f261 | 302 | // timer_while ++; |
Lightvalve | 169:645207e160ca | 303 | |
Lightvalve | 224:985dba42f261 | 304 | //i2c for SW valve |
hyhoon | 226:82a3ca333004 | 305 | if(OPERATING_MODE == 5) { |
Lightvalve | 224:985dba42f261 | 306 | read_field(i2c_slave_addr1); |
Lightvalve | 224:985dba42f261 | 307 | if(DIR_VALVE_ENC < 0) value = 1023 - value; |
Lightvalve | 170:42c938a40313 | 308 | } |
GiJeongKim | 0:51c43836c1d7 | 309 | } |
jobuuu | 1:e04e563be5ce | 310 | } |
jobuuu | 1:e04e563be5ce | 311 | |
Lightvalve | 224:985dba42f261 | 312 | |
Lightvalve | 224:985dba42f261 | 313 | // Velocity feedforward for SW valve |
Lightvalve | 33:91b17819ec30 | 314 | float DDV_JOINT_POS_FF(float REF_JOINT_VEL) |
Lightvalve | 14:8e7590227d22 | 315 | { |
Lightvalve | 13:747daba9cf59 | 316 | int i = 0; |
Lightvalve | 48:889798ff9329 | 317 | float Ref_Valve_Pos_FF = 0.0f; |
Lightvalve | 14:8e7590227d22 | 318 | for(i=0; i<VALVE_POS_NUM; i++) { |
Lightvalve | 14:8e7590227d22 | 319 | 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 | 320 | if(i==0) { |
Lightvalve | 50:3c630b5eba9f | 321 | if(JOINT_VEL[i+1] == JOINT_VEL[i]) { |
Lightvalve | 57:f4819de54e7a | 322 | Ref_Valve_Pos_FF = (float) VALVE_CENTER; |
Lightvalve | 50:3c630b5eba9f | 323 | } else { |
Lightvalve | 57:f4819de54e7a | 324 | 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 | 325 | } |
Lightvalve | 14:8e7590227d22 | 326 | } else { |
Lightvalve | 50:3c630b5eba9f | 327 | if(JOINT_VEL[i+1] == JOINT_VEL[i-1]) { |
Lightvalve | 57:f4819de54e7a | 328 | Ref_Valve_Pos_FF = (float) VALVE_CENTER; |
Lightvalve | 50:3c630b5eba9f | 329 | } else { |
Lightvalve | 57:f4819de54e7a | 330 | 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 | 331 | } |
Lightvalve | 13:747daba9cf59 | 332 | } |
Lightvalve | 13:747daba9cf59 | 333 | break; |
Lightvalve | 13:747daba9cf59 | 334 | } |
Lightvalve | 13:747daba9cf59 | 335 | } |
Lightvalve | 14:8e7590227d22 | 336 | if(REF_JOINT_VEL > max(JOINT_VEL[VALVE_POS_NUM-1], JOINT_VEL[VALVE_POS_NUM-2])) { |
Lightvalve | 33:91b17819ec30 | 337 | Ref_Valve_Pos_FF = (float) VALVE_MAX_POS; |
Lightvalve | 14:8e7590227d22 | 338 | } else if(REF_JOINT_VEL < min(JOINT_VEL[VALVE_POS_NUM-1], JOINT_VEL[VALVE_POS_NUM-2])) { |
Lightvalve | 33:91b17819ec30 | 339 | Ref_Valve_Pos_FF = (float) VALVE_MIN_POS; |
Lightvalve | 13:747daba9cf59 | 340 | } |
Lightvalve | 36:a46e63505ed8 | 341 | |
Lightvalve | 224:985dba42f261 | 342 | 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 | 343 | return Ref_Valve_Pos_FF; |
Lightvalve | 13:747daba9cf59 | 344 | } |
jobuuu | 6:df07d3491e3a | 345 | |
Lightvalve | 224:985dba42f261 | 346 | // Valve feedforward for SW valve |
Lightvalve | 30:8d561f16383b | 347 | void VALVE_POS_CONTROL(float REF_VALVE_POS) |
Lightvalve | 14:8e7590227d22 | 348 | { |
Lightvalve | 13:747daba9cf59 | 349 | int i = 0; |
Lightvalve | 13:747daba9cf59 | 350 | |
Lightvalve | 38:118df027d851 | 351 | if(REF_VALVE_POS > VALVE_MAX_POS) { |
Lightvalve | 38:118df027d851 | 352 | REF_VALVE_POS = VALVE_MAX_POS; |
Lightvalve | 38:118df027d851 | 353 | } else if(REF_VALVE_POS < VALVE_MIN_POS) { |
Lightvalve | 38:118df027d851 | 354 | REF_VALVE_POS = VALVE_MIN_POS; |
Lightvalve | 38:118df027d851 | 355 | } |
Lightvalve | 89:a7b45368ea0f | 356 | valve_pos_err = (float) (REF_VALVE_POS - value); |
Lightvalve | 13:747daba9cf59 | 357 | valve_pos_err_diff = valve_pos_err - valve_pos_err_old; |
Lightvalve | 13:747daba9cf59 | 358 | valve_pos_err_old = valve_pos_err; |
Lightvalve | 13:747daba9cf59 | 359 | valve_pos_err_sum += valve_pos_err; |
Lightvalve | 89:a7b45368ea0f | 360 | if (valve_pos_err_sum > 1000.0f) valve_pos_err_sum = 1000.0f; |
Lightvalve | 89:a7b45368ea0f | 361 | if (valve_pos_err_sum<-1000.0f) valve_pos_err_sum = -1000.0f; |
Lightvalve | 13:747daba9cf59 | 362 | |
Lightvalve | 13:747daba9cf59 | 363 | 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 | 364 | |
Lightvalve | 18:b8adf1582ea3 | 365 | for(i=0; i<24; i++) { |
Lightvalve | 89:a7b45368ea0f | 366 | 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 | 367 | if(i==0) { |
Lightvalve | 48:889798ff9329 | 368 | 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 | 369 | } else { |
Lightvalve | 48:889798ff9329 | 370 | 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 | 371 | } |
Lightvalve | 13:747daba9cf59 | 372 | break; |
Lightvalve | 13:747daba9cf59 | 373 | } |
Lightvalve | 13:747daba9cf59 | 374 | } |
Lightvalve | 59:f308b1656d9c | 375 | Vout.ref = VALVE_PWM_RAW_FF + VALVE_PWM_RAW_FB; |
Lightvalve | 13:747daba9cf59 | 376 | } |
Lightvalve | 13:747daba9cf59 | 377 | |
Lightvalve | 224:985dba42f261 | 378 | // PWM duty vs. voltage output of L6205 in STM board |
Lightvalve | 14:8e7590227d22 | 379 | #define LT_MAX_IDX 57 |
Lightvalve | 30:8d561f16383b | 380 | 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 | 381 | -19.0f, -18.0f, -17.0f, -16.0f, -15.0f, -14.0f, -13.0f, -12.0f, -11.0f, -10.0f, |
Lightvalve | 34:bb2ca2fc2a8e | 382 | -9.0f, -8.0f, -7.0f, -6.0f, -5.0f, -4.0f, -3.0f, -2.0f, -1.0f, 0.0f, |
Lightvalve | 34:bb2ca2fc2a8e | 383 | 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f, |
Lightvalve | 34:bb2ca2fc2a8e | 384 | 11.0f, 12.0f, 13.0f, 14.0f, 15.0f, 16.0f, 17.0f, 18.0f, 19.0f, 20.0f, |
Lightvalve | 34:bb2ca2fc2a8e | 385 | 25.0f, 30.0f, 35.0f, 40.0f, 50.0f, 60.0f, 80.0f, 100.0f |
Lightvalve | 34:bb2ca2fc2a8e | 386 | }; // duty |
Lightvalve | 67:c2812cf26c38 | 387 | float LT_Voltage_Output[LT_MAX_IDX] = {-230.0f, -215.0f, -192.5f, -185.0f, -177.5f, -170.0f, -164.0f, -160.0f, -150.0f, |
Lightvalve | 67:c2812cf26c38 | 388 | -150.0f, -145.0f, -145.0f, -145.0f, -135.0f, -135.0f, -135.0f, -127.5f, -127.5f, -115.0f, |
Lightvalve | 67:c2812cf26c38 | 389 | -115.0f, -115.0F, -100.0f, -100.0f, -100.0f, -60.0f, -60.0f, -10.0f, -5.0f, 0.0f, |
Lightvalve | 67:c2812cf26c38 | 390 | 7.5f, 14.0f, 14.0f, 14.0f, 42.5f, 42.5f, 42.5f, 80.0f, 80.0f, 105.0f, |
Lightvalve | 67:c2812cf26c38 | 391 | 105.0f, 105.0f, 120.0f, 120.0f, 120.0f, 131.0f, 131.0f, 140.0f, 140.0f, 140.0f, |
Lightvalve | 67:c2812cf26c38 | 392 | 155.0f, 160.0f, 170.0f, 174.0f, 182.0f, 191.0f, 212.0f, 230.0f |
Lightvalve | 34:bb2ca2fc2a8e | 393 | }; // mV |
Lightvalve | 13:747daba9cf59 | 394 | |
Lightvalve | 30:8d561f16383b | 395 | float PWM_duty_byLT(float Ref_V) |
Lightvalve | 14:8e7590227d22 | 396 | { |
Lightvalve | 30:8d561f16383b | 397 | float PWM_duty = 0.0f; |
Lightvalve | 13:747daba9cf59 | 398 | if(Ref_V<LT_Voltage_Output[0]) { |
Lightvalve | 30:8d561f16383b | 399 | PWM_duty = (Ref_V-LT_Voltage_Output[0])/1.5f+LT_PWM_duty[0]; |
Lightvalve | 13:747daba9cf59 | 400 | } else if (Ref_V>=LT_Voltage_Output[LT_MAX_IDX-1]) { |
Lightvalve | 30:8d561f16383b | 401 | PWM_duty = (Ref_V-LT_Voltage_Output[LT_MAX_IDX-1])/1.5f+LT_PWM_duty[LT_MAX_IDX-1]; |
Lightvalve | 13:747daba9cf59 | 402 | } else { |
Lightvalve | 13:747daba9cf59 | 403 | int idx = 0; |
Lightvalve | 13:747daba9cf59 | 404 | for(idx=0; idx<LT_MAX_IDX-1; idx++) { |
Lightvalve | 30:8d561f16383b | 405 | float ini_x = LT_Voltage_Output[idx]; |
Lightvalve | 30:8d561f16383b | 406 | float fin_x = LT_Voltage_Output[idx+1]; |
Lightvalve | 30:8d561f16383b | 407 | float ini_y = LT_PWM_duty[idx]; |
Lightvalve | 30:8d561f16383b | 408 | float fin_y = LT_PWM_duty[idx+1]; |
Lightvalve | 13:747daba9cf59 | 409 | if(Ref_V>=ini_x && Ref_V<fin_x) { |
Lightvalve | 13:747daba9cf59 | 410 | PWM_duty = (fin_y-ini_y)/(fin_x-ini_x)*(Ref_V-ini_x) + ini_y; |
Lightvalve | 13:747daba9cf59 | 411 | break; |
Lightvalve | 13:747daba9cf59 | 412 | } |
Lightvalve | 13:747daba9cf59 | 413 | } |
Lightvalve | 13:747daba9cf59 | 414 | } |
Lightvalve | 14:8e7590227d22 | 415 | |
Lightvalve | 13:747daba9cf59 | 416 | return PWM_duty; |
Lightvalve | 13:747daba9cf59 | 417 | } |
jobuuu | 6:df07d3491e3a | 418 | |
Lightvalve | 57:f4819de54e7a | 419 | |
Lightvalve | 57:f4819de54e7a | 420 | |
jobuuu | 2:a1c0a37df760 | 421 | /******************************************************************************* |
jobuuu | 2:a1c0a37df760 | 422 | TIMER INTERRUPT |
jobuuu | 2:a1c0a37df760 | 423 | *******************************************************************************/ |
jobuuu | 2:a1c0a37df760 | 424 | |
Lightvalve | 51:b46bed7fec80 | 425 | float FREQ_TMR4 = (float)FREQ_20k; |
Lightvalve | 51:b46bed7fec80 | 426 | float DT_TMR4 = (float)DT_20k; |
Lightvalve | 57:f4819de54e7a | 427 | long CNT_TMR4 = 0; |
Lightvalve | 57:f4819de54e7a | 428 | int TMR4_FREQ_10k = (int)FREQ_10k; |
jobuuu | 1:e04e563be5ce | 429 | extern "C" void TIM4_IRQHandler(void) |
jobuuu | 1:e04e563be5ce | 430 | { |
Lightvalve | 19:23b7c1ad8683 | 431 | if (TIM4->SR & TIM_SR_UIF ) { |
Lightvalve | 21:e5f1a43ea6f9 | 432 | /******************************************************* |
Lightvalve | 21:e5f1a43ea6f9 | 433 | *** Sensor Read & Data Handling |
Lightvalve | 21:e5f1a43ea6f9 | 434 | ********************************************************/ |
Lightvalve | 13:747daba9cf59 | 435 | |
Lightvalve | 57:f4819de54e7a | 436 | //Encoder |
Lightvalve | 57:f4819de54e7a | 437 | if (CNT_TMR4 % (int) ((int) FREQ_TMR4/TMR4_FREQ_10k) == 0) { |
Lightvalve | 57:f4819de54e7a | 438 | ENC_UPDATE(); |
Lightvalve | 57:f4819de54e7a | 439 | } |
Lightvalve | 61:bc8c8270f0ab | 440 | |
Lightvalve | 61:bc8c8270f0ab | 441 | ADC1->CR2 |= 0x40000000; |
Lightvalve | 224:985dba42f261 | 442 | // Torque Sensing ============================================= |
Lightvalve | 58:2eade98630e2 | 443 | if (SENSING_MODE == 0) { |
Lightvalve | 58:2eade98630e2 | 444 | float pres_A_new = (((float) ADC1->DR) - 2047.5f); |
Lightvalve | 224:985dba42f261 | 445 | double alpha_update_ft = 1.0f / (1.0f + FREQ_TMR4 / (2.0f * 3.14f * 100.0f)); // f_cutoff : 100Hz |
Lightvalve | 58:2eade98630e2 | 446 | pres_A.sen = (1.0f - alpha_update_ft) * pres_A.sen + alpha_update_ft * pres_A_new; |
Lightvalve | 67:c2812cf26c38 | 447 | torq.sen = -pres_A.sen / TORQUE_SENSOR_PULSE_PER_TORQUE; |
Lightvalve | 67:c2812cf26c38 | 448 | |
hyhoon | 226:82a3ca333004 | 449 | // Pressure Sensing (0~210)bar ============================================= |
Lightvalve | 58:2eade98630e2 | 450 | } else if (SENSING_MODE == 1) { |
hyhoon | 226:82a3ca333004 | 451 | |
hyhoon | 226:82a3ca333004 | 452 | if (DIR_VALVE_ENC > 0) { |
hyhoon | 226:82a3ca333004 | 453 | pres_A_new = (((float)ADC1->DR) - PRES_A_NULL); |
hyhoon | 226:82a3ca333004 | 454 | pres_B_new = (((float)ADC2->DR) - PRES_B_NULL); |
hyhoon | 226:82a3ca333004 | 455 | } else { |
hyhoon | 226:82a3ca333004 | 456 | pres_A_new = (((float)ADC2->DR) - PRES_A_NULL); |
hyhoon | 226:82a3ca333004 | 457 | pres_B_new = (((float)ADC1->DR) - PRES_B_NULL); |
hyhoon | 226:82a3ca333004 | 458 | } |
Lightvalve | 224:985dba42f261 | 459 | double alpha_update_pres = 1.0f / (1.0f + FREQ_TMR4 / (2.0f * 3.14f * 200.0f)); // f_cutoff : 200Hz |
Lightvalve | 58:2eade98630e2 | 460 | pres_A.sen = (1.0f - alpha_update_pres) * pres_A.sen + alpha_update_pres * pres_A_new; |
Lightvalve | 58:2eade98630e2 | 461 | pres_B.sen = (1.0f - alpha_update_pres) * pres_B.sen + alpha_update_pres * pres_B_new; |
Lightvalve | 58:2eade98630e2 | 462 | CUR_PRES_A_BAR = pres_A.sen / PRES_SENSOR_A_PULSE_PER_BAR; |
Lightvalve | 58:2eade98630e2 | 463 | CUR_PRES_B_BAR = pres_B.sen / PRES_SENSOR_B_PULSE_PER_BAR; |
Lightvalve | 50:3c630b5eba9f | 464 | |
Lightvalve | 58:2eade98630e2 | 465 | if ((OPERATING_MODE & 0x01) == 0) { // Rotary Actuator |
Lightvalve | 58:2eade98630e2 | 466 | torq.sen = (PISTON_AREA_A * CUR_PRES_A_BAR - PISTON_AREA_B * CUR_PRES_B_BAR) * 0.0001f; // mm^3*bar >> Nm |
Lightvalve | 58:2eade98630e2 | 467 | } else if ((OPERATING_MODE & 0x01) == 1) { // Linear Actuator |
Lightvalve | 58:2eade98630e2 | 468 | torq.sen = (PISTON_AREA_A * CUR_PRES_A_BAR - PISTON_AREA_B * CUR_PRES_B_BAR) * 0.1f; // mm^2*bar >> N |
Lightvalve | 58:2eade98630e2 | 469 | } |
Lightvalve | 58:2eade98630e2 | 470 | } |
Lightvalve | 61:bc8c8270f0ab | 471 | |
Lightvalve | 21:e5f1a43ea6f9 | 472 | //Current |
Lightvalve | 21:e5f1a43ea6f9 | 473 | //ADC3->CR2 |= 0x40000000; // adc _ 12bit |
Lightvalve | 51:b46bed7fec80 | 474 | float alpha_update_cur = 1.0f/(1.0f + FREQ_TMR4/(2.0f*3.14f*500.0f)); // f_cutoff : 500Hz |
Lightvalve | 30:8d561f16383b | 475 | float cur_new = ((float)ADC3->DR-2048.0f)*20.0f/4096.0f; // unit : mA |
Lightvalve | 30:8d561f16383b | 476 | cur.sen=cur.sen*(1.0f-alpha_update_cur)+cur_new*(alpha_update_cur); |
Lightvalve | 58:2eade98630e2 | 477 | |
Lightvalve | 57:f4819de54e7a | 478 | CNT_TMR4++; |
Lightvalve | 21:e5f1a43ea6f9 | 479 | } |
Lightvalve | 11:82d8768d7351 | 480 | TIM4->SR = 0x0; // reset the status register |
Lightvalve | 11:82d8768d7351 | 481 | } |
Lightvalve | 19:23b7c1ad8683 | 482 | |
Lightvalve | 19:23b7c1ad8683 | 483 | |
Lightvalve | 18:b8adf1582ea3 | 484 | int j =0; |
Lightvalve | 54:647072f5307a | 485 | float FREQ_TMR3 = (float)FREQ_5k; |
Lightvalve | 48:889798ff9329 | 486 | float DT_TMR3 = (float)DT_5k; |
Lightvalve | 45:35fa6884d0c6 | 487 | int cnt_trans = 0; |
Lightvalve | 48:889798ff9329 | 488 | double VALVE_POS_RAW_FORCE_FB_LOGGING = 0.0f; |
Lightvalve | 57:f4819de54e7a | 489 | int can_rest =0; |
Lightvalve | 48:889798ff9329 | 490 | |
Lightvalve | 11:82d8768d7351 | 491 | extern "C" void TIM3_IRQHandler(void) |
Lightvalve | 21:e5f1a43ea6f9 | 492 | { |
Lightvalve | 19:23b7c1ad8683 | 493 | if (TIM3->SR & TIM_SR_UIF ) { |
Lightvalve | 57:f4819de54e7a | 494 | |
hyhoon | 226:82a3ca333004 | 495 | |
Lightvalve | 57:f4819de54e7a | 496 | if (((OPERATING_MODE&0b110)>>1) == 0) { |
Lightvalve | 57:f4819de54e7a | 497 | K_v = 0.4f; // Moog (LPM >> mA) , 100bar |
Lightvalve | 57:f4819de54e7a | 498 | mV_PER_mA = 500.0f; // 5000mV/10mA |
Lightvalve | 57:f4819de54e7a | 499 | mV_PER_pulse = 0.5f; // 5000mV/10000pulse |
Lightvalve | 57:f4819de54e7a | 500 | mA_PER_pulse = 0.001f; // 10mA/10000pulse |
Lightvalve | 57:f4819de54e7a | 501 | } else if (((OPERATING_MODE&0b110)>>1) == 1) { |
Lightvalve | 57:f4819de54e7a | 502 | K_v = 0.5f; // KNR (LPM >> mA) , 100bar |
Lightvalve | 57:f4819de54e7a | 503 | mV_PER_mA = 166.6666f; // 5000mV/30mA |
Lightvalve | 57:f4819de54e7a | 504 | mV_PER_pulse = 0.5f; // 5000mV/10000pulse |
Lightvalve | 57:f4819de54e7a | 505 | mA_PER_pulse = 0.003f; // 30mA/10000pulse |
Lightvalve | 57:f4819de54e7a | 506 | } |
Lightvalve | 50:3c630b5eba9f | 507 | |
Lightvalve | 50:3c630b5eba9f | 508 | if(MODE_POS_FT_TRANS == 1) { |
Lightvalve | 48:889798ff9329 | 509 | alpha_trans = (float)(1.0f - cos(3.141592f * (float)cnt_trans * DT_TMR3 /3.0f))/2.0f; |
Lightvalve | 45:35fa6884d0c6 | 510 | cnt_trans++; |
Lightvalve | 46:2694daea349b | 511 | torq.err_sum = 0; |
Lightvalve | 48:889798ff9329 | 512 | if((float)cnt_trans * DT_TMR3 > 3.0f) |
Lightvalve | 45:35fa6884d0c6 | 513 | MODE_POS_FT_TRANS = 2; |
Lightvalve | 50:3c630b5eba9f | 514 | } else if(MODE_POS_FT_TRANS == 3) { |
Lightvalve | 48:889798ff9329 | 515 | alpha_trans = (float)(1.0f + cos(3.141592f * (float)cnt_trans * DT_TMR3 /3.0f))/2.0f; |
Lightvalve | 45:35fa6884d0c6 | 516 | cnt_trans++; |
Lightvalve | 46:2694daea349b | 517 | torq.err_sum = 0; |
Lightvalve | 48:889798ff9329 | 518 | if((float) cnt_trans * DT_TMR3 > 3.0f ) |
Lightvalve | 45:35fa6884d0c6 | 519 | MODE_POS_FT_TRANS = 0; |
Lightvalve | 50:3c630b5eba9f | 520 | } else if(MODE_POS_FT_TRANS == 2) { |
Lightvalve | 58:2eade98630e2 | 521 | alpha_trans = 1.0f; |
Lightvalve | 45:35fa6884d0c6 | 522 | cnt_trans = 0; |
Lightvalve | 50:3c630b5eba9f | 523 | } else { |
Lightvalve | 58:2eade98630e2 | 524 | alpha_trans = 0.0f; |
Lightvalve | 45:35fa6884d0c6 | 525 | cnt_trans = 0; |
Lightvalve | 45:35fa6884d0c6 | 526 | } |
Lightvalve | 45:35fa6884d0c6 | 527 | |
Lightvalve | 50:3c630b5eba9f | 528 | |
Lightvalve | 57:f4819de54e7a | 529 | int UTILITY_MODE = 0; |
Lightvalve | 57:f4819de54e7a | 530 | int CONTROL_MODE = 0; |
Lightvalve | 58:2eade98630e2 | 531 | |
Lightvalve | 57:f4819de54e7a | 532 | if (CONTROL_UTILITY_MODE >= 20 || CONTROL_UTILITY_MODE == 0) { |
Lightvalve | 57:f4819de54e7a | 533 | UTILITY_MODE = CONTROL_UTILITY_MODE; |
Lightvalve | 57:f4819de54e7a | 534 | CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 57:f4819de54e7a | 535 | } else { |
Lightvalve | 57:f4819de54e7a | 536 | CONTROL_MODE = CONTROL_UTILITY_MODE; |
Lightvalve | 57:f4819de54e7a | 537 | UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 57:f4819de54e7a | 538 | } |
Lightvalve | 56:6f50d9d3bfee | 539 | |
Lightvalve | 56:6f50d9d3bfee | 540 | |
Lightvalve | 56:6f50d9d3bfee | 541 | |
Lightvalve | 57:f4819de54e7a | 542 | // UTILITY MODE ------------------------------------------------------------ |
Lightvalve | 57:f4819de54e7a | 543 | switch (UTILITY_MODE) { |
Lightvalve | 57:f4819de54e7a | 544 | case MODE_NO_ACT: { |
Lightvalve | 13:747daba9cf59 | 545 | break; |
Lightvalve | 13:747daba9cf59 | 546 | } |
Lightvalve | 14:8e7590227d22 | 547 | |
Lightvalve | 14:8e7590227d22 | 548 | case MODE_TORQUE_SENSOR_NULLING: { |
Lightvalve | 13:747daba9cf59 | 549 | // DAC Voltage reference set |
Lightvalve | 14:8e7590227d22 | 550 | if (TMR3_COUNT_TORQUE_NULL < TMR_FREQ_5k * 2) { |
Lightvalve | 15:bd0d12728506 | 551 | CUR_TORQUE_sum += torq.sen; |
Lightvalve | 19:23b7c1ad8683 | 552 | |
Lightvalve | 14:8e7590227d22 | 553 | if (TMR3_COUNT_TORQUE_NULL % 10 == 0) { |
Lightvalve | 30:8d561f16383b | 554 | CUR_TORQUE_mean = CUR_TORQUE_sum / 10.0f; |
Lightvalve | 13:747daba9cf59 | 555 | CUR_TORQUE_sum = 0; |
Lightvalve | 14:8e7590227d22 | 556 | |
Lightvalve | 84:c355d3e52bf1 | 557 | TORQUE_VREF += 0.000003f * (0.0f - CUR_TORQUE_mean); |
Lightvalve | 19:23b7c1ad8683 | 558 | |
Lightvalve | 30:8d561f16383b | 559 | if (TORQUE_VREF > 3.3f) TORQUE_VREF = 3.3f; |
Lightvalve | 59:f308b1656d9c | 560 | if (TORQUE_VREF < 0.0f) TORQUE_VREF = 0.0f; |
Lightvalve | 30:8d561f16383b | 561 | dac_1 = TORQUE_VREF / 3.3f; |
Lightvalve | 13:747daba9cf59 | 562 | } |
Lightvalve | 13:747daba9cf59 | 563 | } else { |
Lightvalve | 58:2eade98630e2 | 564 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 14:8e7590227d22 | 565 | TMR3_COUNT_TORQUE_NULL = 0; |
Lightvalve | 13:747daba9cf59 | 566 | CUR_TORQUE_sum = 0; |
Lightvalve | 13:747daba9cf59 | 567 | CUR_TORQUE_mean = 0; |
Lightvalve | 19:23b7c1ad8683 | 568 | |
Lightvalve | 170:42c938a40313 | 569 | spi_eeprom_write(RID_TORQUE_SENSOR_VREF, (int16_t) (TORQUE_VREF * 1000.0f)); |
Lightvalve | 19:23b7c1ad8683 | 570 | |
Lightvalve | 30:8d561f16383b | 571 | dac_1 = TORQUE_VREF / 3.3f; |
Lightvalve | 14:8e7590227d22 | 572 | |
Lightvalve | 13:747daba9cf59 | 573 | } |
Lightvalve | 14:8e7590227d22 | 574 | TMR3_COUNT_TORQUE_NULL++; |
Lightvalve | 13:747daba9cf59 | 575 | break; |
Lightvalve | 19:23b7c1ad8683 | 576 | } |
Lightvalve | 14:8e7590227d22 | 577 | |
Lightvalve | 14:8e7590227d22 | 578 | case MODE_FIND_HOME: { |
Lightvalve | 29:69f3f5445d6d | 579 | if (FINDHOME_STAGE == FINDHOME_INIT) { |
Lightvalve | 14:8e7590227d22 | 580 | cnt_findhome = 0; |
Lightvalve | 14:8e7590227d22 | 581 | cnt_vel_findhome = 0; |
Lightvalve | 59:f308b1656d9c | 582 | pos.ref = pos.sen; |
Lightvalve | 59:f308b1656d9c | 583 | vel.ref = 0.0f; |
Lightvalve | 29:69f3f5445d6d | 584 | FINDHOME_STAGE = FINDHOME_GOTOLIMIT; |
Lightvalve | 29:69f3f5445d6d | 585 | } else if (FINDHOME_STAGE == FINDHOME_GOTOLIMIT) { |
Lightvalve | 67:c2812cf26c38 | 586 | int cnt_check_enc = (TMR_FREQ_5k/20); |
Lightvalve | 29:69f3f5445d6d | 587 | if(cnt_findhome%cnt_check_enc == 0) { |
Lightvalve | 29:69f3f5445d6d | 588 | FINDHOME_POSITION = pos.sen; |
Lightvalve | 29:69f3f5445d6d | 589 | FINDHOME_VELOCITY = FINDHOME_POSITION - FINDHOME_POSITION_OLD; |
Lightvalve | 29:69f3f5445d6d | 590 | FINDHOME_POSITION_OLD = FINDHOME_POSITION; |
Lightvalve | 29:69f3f5445d6d | 591 | } |
Lightvalve | 29:69f3f5445d6d | 592 | cnt_findhome++; |
Lightvalve | 14:8e7590227d22 | 593 | |
Lightvalve | 29:69f3f5445d6d | 594 | if (abs(FINDHOME_VELOCITY) <= 1) { |
Lightvalve | 29:69f3f5445d6d | 595 | cnt_vel_findhome = cnt_vel_findhome + 1; |
Lightvalve | 29:69f3f5445d6d | 596 | } else { |
Lightvalve | 29:69f3f5445d6d | 597 | cnt_vel_findhome = 0; |
Lightvalve | 29:69f3f5445d6d | 598 | } |
Lightvalve | 19:23b7c1ad8683 | 599 | |
Lightvalve | 57:f4819de54e7a | 600 | if ((cnt_vel_findhome < 3*TMR_FREQ_5k) && cnt_findhome < 10*TMR_FREQ_5k) { // wait for 3sec |
hyhoon | 226:82a3ca333004 | 601 | if (HOMEPOS_OFFSET > 0) { |
hyhoon | 226:82a3ca333004 | 602 | pos.ref = pos.ref + 12.0f; |
hyhoon | 226:82a3ca333004 | 603 | } else { |
hyhoon | 226:82a3ca333004 | 604 | pos.ref = pos.ref - 12.0f; |
hyhoon | 226:82a3ca333004 | 605 | } |
hyhoon | 226:82a3ca333004 | 606 | |
Lightvalve | 61:bc8c8270f0ab | 607 | |
Lightvalve | 59:f308b1656d9c | 608 | CONTROL_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 59:f308b1656d9c | 609 | alpha_trans = 0.0f; |
Lightvalve | 61:bc8c8270f0ab | 610 | |
Lightvalve | 29:69f3f5445d6d | 611 | } else { |
hyhoon | 226:82a3ca333004 | 612 | ENC_SET((int32_t)((int32_t)HOMEPOS_OFFSET*10)); |
hyhoon | 226:82a3ca333004 | 613 | INIT_REF_POS = (int32_t)((int32_t)HOMEPOS_OFFSET*10); |
Lightvalve | 29:69f3f5445d6d | 614 | REF_POSITION = 0; |
Lightvalve | 29:69f3f5445d6d | 615 | REF_VELOCITY = 0; |
Lightvalve | 29:69f3f5445d6d | 616 | FINDHOME_POSITION = 0; |
Lightvalve | 29:69f3f5445d6d | 617 | FINDHOME_POSITION_OLD = 0; |
Lightvalve | 29:69f3f5445d6d | 618 | FINDHOME_VELOCITY = 0; |
Lightvalve | 29:69f3f5445d6d | 619 | cnt_findhome = 0; |
Lightvalve | 29:69f3f5445d6d | 620 | cnt_vel_findhome = 0; |
Lightvalve | 29:69f3f5445d6d | 621 | FINDHOME_STAGE = FINDHOME_ZEROPOSE; |
Lightvalve | 67:c2812cf26c38 | 622 | |
Lightvalve | 67:c2812cf26c38 | 623 | |
Lightvalve | 67:c2812cf26c38 | 624 | cnt_findhome = 0; |
Lightvalve | 67:c2812cf26c38 | 625 | pos.ref = 0.0f; |
Lightvalve | 67:c2812cf26c38 | 626 | vel.ref = 0.0f; |
Lightvalve | 67:c2812cf26c38 | 627 | pos.ref_home_pos = 0.0f; |
Lightvalve | 67:c2812cf26c38 | 628 | vel.ref_home_pos = 0.0f; |
Lightvalve | 29:69f3f5445d6d | 629 | } |
Lightvalve | 29:69f3f5445d6d | 630 | } else if (FINDHOME_STAGE == FINDHOME_ZEROPOSE) { |
Lightvalve | 29:69f3f5445d6d | 631 | int T_move = 2*TMR_FREQ_5k; |
Lightvalve | 59:f308b1656d9c | 632 | pos.ref = (0.0f - (float)INIT_REF_POS)*0.5f*(1.0f - cos(3.14159f * (float)cnt_findhome / (float)T_move)) + (float)INIT_REF_POS; |
Lightvalve | 59:f308b1656d9c | 633 | vel.ref = 0.0f; |
Lightvalve | 67:c2812cf26c38 | 634 | alpha_trans = 0.0f; |
Lightvalve | 61:bc8c8270f0ab | 635 | |
Lightvalve | 67:c2812cf26c38 | 636 | double torq_ref = 0.0f; |
Lightvalve | 67:c2812cf26c38 | 637 | pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm] |
Lightvalve | 67:c2812cf26c38 | 638 | vel.err = (0.0f - vel.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm/s] |
Lightvalve | 67:c2812cf26c38 | 639 | pos.err_sum += pos.err/(float) TMR_FREQ_5k; //[mm] |
hyhoon | 226:82a3ca333004 | 640 | CONTROL_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 67:c2812cf26c38 | 641 | |
Lightvalve | 67:c2812cf26c38 | 642 | if (((OPERATING_MODE&0b110)>>1) == 0 || ((OPERATING_MODE&0b110)>>1) == 1) { |
Lightvalve | 67:c2812cf26c38 | 643 | |
Lightvalve | 67:c2812cf26c38 | 644 | double I_REF_POS = 0.0f; |
Lightvalve | 67:c2812cf26c38 | 645 | double I_REF_FORCE_FB = 0.0f; // I_REF by Force Feedback |
Lightvalve | 67:c2812cf26c38 | 646 | double I_REF_VC = 0.0f; // I_REF for velocity compensation |
Lightvalve | 67:c2812cf26c38 | 647 | |
Lightvalve | 67:c2812cf26c38 | 648 | double temp_vel_pos = 0.0f; |
Lightvalve | 67:c2812cf26c38 | 649 | double temp_vel_torq = 0.0f; |
Lightvalve | 67:c2812cf26c38 | 650 | double wn_Pos = 2.0f * PI * 5.0f; // f_cut : 5Hz Position Control |
Lightvalve | 67:c2812cf26c38 | 651 | |
Lightvalve | 69:3995ffeaa786 | 652 | if ((OPERATING_MODE & 0x01) == 0) { // Rotary Mode |
Lightvalve | 67:c2812cf26c38 | 653 | temp_vel_pos = (0.01f * (double) P_GAIN_JOINT_POSITION * wn_Pos * pos.err + 0.01f * (double) I_GAIN_JOINT_POSITION * wn_Pos * pos.err_sum + 0.01f * (double) VELOCITY_COMP_GAIN * vel.ref / ENC_PULSE_PER_POSITION) * 3.14159f / 180.0f; // rad/s |
Lightvalve | 67:c2812cf26c38 | 654 | // L when P-gain = 100, f_cut = 10Hz L feedforward velocity |
Lightvalve | 69:3995ffeaa786 | 655 | } else if ((OPERATING_MODE & 0x01) == 1) { |
Lightvalve | 67:c2812cf26c38 | 656 | temp_vel_pos = (0.01f * (double) P_GAIN_JOINT_POSITION * wn_Pos * pos.err + 0.01f * (double) I_GAIN_JOINT_POSITION * wn_Pos * pos.err_sum + 0.01f * (double) VELOCITY_COMP_GAIN * vel.ref / ENC_PULSE_PER_POSITION); // mm/s |
Lightvalve | 67:c2812cf26c38 | 657 | // L when P-gain = 100, f_cut = 10Hz L feedforward velocity |
Lightvalve | 67:c2812cf26c38 | 658 | } |
Lightvalve | 67:c2812cf26c38 | 659 | if (temp_vel_pos > 0.0f) I_REF_POS = temp_vel_pos * ((double) PISTON_AREA_A * 0.00006f / (K_v * sqrt(2.0f * alpha3 / (alpha3 + 1.0f)))); |
Lightvalve | 67:c2812cf26c38 | 660 | else I_REF_POS = temp_vel_pos * ((double) PISTON_AREA_B * 0.00006f / (K_v * sqrt(2.0f / (alpha3 + 1.0f)))); |
Lightvalve | 67:c2812cf26c38 | 661 | |
Lightvalve | 67:c2812cf26c38 | 662 | I_REF = I_REF_POS; |
Lightvalve | 67:c2812cf26c38 | 663 | |
Lightvalve | 67:c2812cf26c38 | 664 | } else { |
Lightvalve | 67:c2812cf26c38 | 665 | float VALVE_POS_RAW_FORCE_FB = 0.0f; |
Lightvalve | 67:c2812cf26c38 | 666 | VALVE_POS_RAW_FORCE_FB = DDV_JOINT_POS_FF(vel.sen) + (P_GAIN_JOINT_POSITION * 0.01f * pos.err + DDV_JOINT_POS_FF(vel.ref)); |
Lightvalve | 67:c2812cf26c38 | 667 | |
Lightvalve | 67:c2812cf26c38 | 668 | if (VALVE_POS_RAW_FORCE_FB >= 0) { |
Lightvalve | 67:c2812cf26c38 | 669 | valve_pos.ref = VALVE_POS_RAW_FORCE_FB + VALVE_DEADZONE_PLUS; |
Lightvalve | 67:c2812cf26c38 | 670 | } else { |
Lightvalve | 67:c2812cf26c38 | 671 | valve_pos.ref = VALVE_POS_RAW_FORCE_FB + VALVE_DEADZONE_MINUS; |
Lightvalve | 67:c2812cf26c38 | 672 | } |
Lightvalve | 67:c2812cf26c38 | 673 | |
Lightvalve | 67:c2812cf26c38 | 674 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 67:c2812cf26c38 | 675 | |
Lightvalve | 67:c2812cf26c38 | 676 | V_out = (float) Vout.ref; |
Lightvalve | 67:c2812cf26c38 | 677 | |
Lightvalve | 67:c2812cf26c38 | 678 | } |
Lightvalve | 67:c2812cf26c38 | 679 | |
Lightvalve | 29:69f3f5445d6d | 680 | cnt_findhome++; |
Lightvalve | 29:69f3f5445d6d | 681 | if (cnt_findhome >= T_move) { |
Lightvalve | 29:69f3f5445d6d | 682 | cnt_findhome = 0; |
Lightvalve | 30:8d561f16383b | 683 | pos.ref = 0.0f; |
Lightvalve | 30:8d561f16383b | 684 | vel.ref = 0.0f; |
Lightvalve | 30:8d561f16383b | 685 | pos.ref_home_pos = 0.0f; |
Lightvalve | 30:8d561f16383b | 686 | vel.ref_home_pos = 0.0f; |
Lightvalve | 29:69f3f5445d6d | 687 | FINDHOME_STAGE = FINDHOME_INIT; |
Lightvalve | 57:f4819de54e7a | 688 | CONTROL_UTILITY_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 29:69f3f5445d6d | 689 | } |
Lightvalve | 13:747daba9cf59 | 690 | } |
Lightvalve | 19:23b7c1ad8683 | 691 | |
Lightvalve | 13:747daba9cf59 | 692 | break; |
Lightvalve | 13:747daba9cf59 | 693 | } |
Lightvalve | 14:8e7590227d22 | 694 | |
Lightvalve | 50:3c630b5eba9f | 695 | // case MODE_VALVE_GAIN_SETTING: { |
Lightvalve | 50:3c630b5eba9f | 696 | // if (TMR3_COUNT_FLOWRATE == 0) { |
Lightvalve | 50:3c630b5eba9f | 697 | // if (pos_plus_end == pos_minus_end) need_enc_init = true; |
Lightvalve | 50:3c630b5eba9f | 698 | // else { |
Lightvalve | 50:3c630b5eba9f | 699 | // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 50:3c630b5eba9f | 700 | // temp_time = (int) (0.5f * (float) TMR_FREQ_5k); |
Lightvalve | 50:3c630b5eba9f | 701 | // } |
Lightvalve | 50:3c630b5eba9f | 702 | // } |
Lightvalve | 50:3c630b5eba9f | 703 | // if (need_enc_init) { |
Lightvalve | 50:3c630b5eba9f | 704 | // if (TMR3_COUNT_FLOWRATE < (int) (0.5f * (float) TMR_FREQ_5k)) { |
Lightvalve | 50:3c630b5eba9f | 705 | // V_out = VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 50:3c630b5eba9f | 706 | // pos_plus_end = pos.sen; |
Lightvalve | 50:3c630b5eba9f | 707 | // } else if (TMR3_COUNT_FLOWRATE < TMR_FREQ_5k) { |
Lightvalve | 50:3c630b5eba9f | 708 | // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 50:3c630b5eba9f | 709 | // pos_minus_end = pos.sen; |
Lightvalve | 50:3c630b5eba9f | 710 | // } else if (TMR3_COUNT_FLOWRATE == TMR_FREQ_5k) { |
Lightvalve | 50:3c630b5eba9f | 711 | // need_enc_init = false; |
Lightvalve | 50:3c630b5eba9f | 712 | // check_vel_pos_init = (int) (0.9f * (float) (pos_plus_end - pos_minus_end)); |
Lightvalve | 50:3c630b5eba9f | 713 | // check_vel_pos_fin = (int) (0.95f * (float) (pos_plus_end - pos_minus_end)); |
Lightvalve | 50:3c630b5eba9f | 714 | // check_vel_pos_interv = check_vel_pos_fin - check_vel_pos_init; |
Lightvalve | 50:3c630b5eba9f | 715 | // } |
Lightvalve | 50:3c630b5eba9f | 716 | // temp_time = TMR_FREQ_5k; |
Lightvalve | 50:3c630b5eba9f | 717 | // } |
Lightvalve | 50:3c630b5eba9f | 718 | // TMR3_COUNT_FLOWRATE++; |
Lightvalve | 50:3c630b5eba9f | 719 | // if (TMR3_COUNT_FLOWRATE > temp_time) { |
Lightvalve | 50:3c630b5eba9f | 720 | // if (flag_flowrate % 2 == 0) { // (+) |
Lightvalve | 50:3c630b5eba9f | 721 | // VALVE_VOLTAGE = 1000.0f * (float) (flag_flowrate / 2 + 1); |
Lightvalve | 50:3c630b5eba9f | 722 | // V_out = VALVE_VOLTAGE; |
Lightvalve | 50:3c630b5eba9f | 723 | // if (pos.sen > (pos_minus_end + check_vel_pos_init) && pos.sen < (pos_minus_end + check_vel_pos_fin)) { |
Lightvalve | 50:3c630b5eba9f | 724 | // fl_temp_cnt++; |
Lightvalve | 50:3c630b5eba9f | 725 | // } else if (pos.sen >= (pos_minus_end + check_vel_pos_fin) && CUR_VELOCITY == 0) { |
Lightvalve | 50:3c630b5eba9f | 726 | // VALVE_GAIN_LPM_PER_V[flag_flowrate] = 0.95873f * 0.5757f * (float) TMR_FREQ_5k / 10000.0 * (float) check_vel_pos_interv / (float) fl_temp_cnt / VALVE_VOLTAGE; // 0.9587=6*pi/65536*10000 0.5757=0.02525*0.02*0.0095*2*60*1000 |
Lightvalve | 50:3c630b5eba9f | 727 | // // VALVE_GAIN_LPM_PER_V[flag_flowrate] = (float) TMR_FREQ_10k * (float) check_vel_pos_interv / (float) fl_temp_cnt / VALVE_VOLTAGE; // PULSE/sec |
Lightvalve | 50:3c630b5eba9f | 728 | // fl_temp_cnt2++; |
Lightvalve | 50:3c630b5eba9f | 729 | // } |
Lightvalve | 50:3c630b5eba9f | 730 | // } else if (flag_flowrate % 2 == 1) { // (-) |
Lightvalve | 50:3c630b5eba9f | 731 | // VALVE_VOLTAGE = -1. * (float) (flag_flowrate / 2 + 1); |
Lightvalve | 50:3c630b5eba9f | 732 | // V_out = VALVE_VOLTAGE; |
Lightvalve | 50:3c630b5eba9f | 733 | // if (pos.sen < (pos_plus_end - check_vel_pos_init) && pos.sen > (pos_plus_end - check_vel_pos_fin)) { |
Lightvalve | 50:3c630b5eba9f | 734 | // fl_temp_cnt++; |
Lightvalve | 50:3c630b5eba9f | 735 | // } else if (pos.sen <= (pos_plus_end - check_vel_pos_fin) && CUR_VELOCITY == 0) { |
Lightvalve | 50:3c630b5eba9f | 736 | // VALVE_GAIN_LPM_PER_V[flag_flowrate] = 0.95873f * 0.5757f * (float) TMR_FREQ_5k / 10000.0f * (float) check_vel_pos_interv / (float) fl_temp_cnt / (-VALVE_VOLTAGE); |
Lightvalve | 50:3c630b5eba9f | 737 | // // VALVE_GAIN_LPM_PER_V[flag_flowrate] = (float) TMR_FREQ_10k * (float) check_vel_pos_interv / (float) fl_temp_cnt / (-VALVE_VOLTAGE); // PULSE/sec |
Lightvalve | 50:3c630b5eba9f | 738 | // fl_temp_cnt2++; |
Lightvalve | 50:3c630b5eba9f | 739 | // } |
Lightvalve | 50:3c630b5eba9f | 740 | // } |
Lightvalve | 50:3c630b5eba9f | 741 | // if (fl_temp_cnt2 == 100) { |
Lightvalve | 50:3c630b5eba9f | 742 | // |
hyhoon | 226:82a3ca333004 | 743 | // |
Lightvalve | 50:3c630b5eba9f | 744 | // |
Lightvalve | 50:3c630b5eba9f | 745 | // //spi_eeprom_write(RID_VALVE_GAIN_PLUS_1 + flag_flowrate, (int16_t) (VALVE_GAIN_LPM_PER_V[flag_flowrate] * 100.0f)); |
Lightvalve | 50:3c630b5eba9f | 746 | // cur_vel_sum = 0; |
Lightvalve | 50:3c630b5eba9f | 747 | // fl_temp_cnt = 0; |
Lightvalve | 50:3c630b5eba9f | 748 | // fl_temp_cnt2 = 0; |
Lightvalve | 50:3c630b5eba9f | 749 | // flag_flowrate++; |
Lightvalve | 50:3c630b5eba9f | 750 | // } |
Lightvalve | 50:3c630b5eba9f | 751 | // if (flag_flowrate == 10) { |
Lightvalve | 50:3c630b5eba9f | 752 | // V_out = 0; |
Lightvalve | 50:3c630b5eba9f | 753 | // flag_flowrate = 0; |
Lightvalve | 50:3c630b5eba9f | 754 | // TMR3_COUNT_FLOWRATE = 0; |
Lightvalve | 50:3c630b5eba9f | 755 | // valve_gain_repeat_cnt++; |
Lightvalve | 50:3c630b5eba9f | 756 | // if (valve_gain_repeat_cnt >= 1) { |
Lightvalve | 50:3c630b5eba9f | 757 | // CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 50:3c630b5eba9f | 758 | // valve_gain_repeat_cnt = 0; |
Lightvalve | 50:3c630b5eba9f | 759 | // } |
Lightvalve | 50:3c630b5eba9f | 760 | // |
Lightvalve | 50:3c630b5eba9f | 761 | // } |
Lightvalve | 50:3c630b5eba9f | 762 | // break; |
Lightvalve | 50:3c630b5eba9f | 763 | // } |
Lightvalve | 50:3c630b5eba9f | 764 | // |
Lightvalve | 50:3c630b5eba9f | 765 | // } |
Lightvalve | 14:8e7590227d22 | 766 | case MODE_PRESSURE_SENSOR_NULLING: { |
Lightvalve | 13:747daba9cf59 | 767 | // DAC Voltage reference set |
Lightvalve | 14:8e7590227d22 | 768 | if (TMR3_COUNT_PRES_NULL < TMR_FREQ_5k * 2) { |
Lightvalve | 14:8e7590227d22 | 769 | CUR_PRES_A_sum += pres_A.sen; |
Lightvalve | 14:8e7590227d22 | 770 | CUR_PRES_B_sum += pres_B.sen; |
Lightvalve | 14:8e7590227d22 | 771 | |
Lightvalve | 14:8e7590227d22 | 772 | if (TMR3_COUNT_PRES_NULL % 10 == 0) { |
Lightvalve | 30:8d561f16383b | 773 | CUR_PRES_A_mean = CUR_PRES_A_sum / 10.0f; |
Lightvalve | 30:8d561f16383b | 774 | CUR_PRES_B_mean = CUR_PRES_B_sum / 10.0f; |
Lightvalve | 13:747daba9cf59 | 775 | CUR_PRES_A_sum = 0; |
Lightvalve | 13:747daba9cf59 | 776 | CUR_PRES_B_sum = 0; |
Lightvalve | 14:8e7590227d22 | 777 | |
Lightvalve | 38:118df027d851 | 778 | float VREF_NullingGain = 0.0003f; |
Lightvalve | 58:2eade98630e2 | 779 | PRES_A_VREF = PRES_A_VREF + VREF_NullingGain * CUR_PRES_A_mean; |
Lightvalve | 58:2eade98630e2 | 780 | PRES_B_VREF = PRES_B_VREF + VREF_NullingGain * CUR_PRES_B_mean; |
Lightvalve | 14:8e7590227d22 | 781 | |
Lightvalve | 30:8d561f16383b | 782 | if (PRES_A_VREF > 3.3f) PRES_A_VREF = 3.3f; |
Lightvalve | 30:8d561f16383b | 783 | if (PRES_A_VREF < 0.0f) PRES_A_VREF = 0.0f; |
Lightvalve | 30:8d561f16383b | 784 | if (PRES_B_VREF > 3.3f) PRES_B_VREF = 3.3f; |
Lightvalve | 30:8d561f16383b | 785 | if (PRES_B_VREF < 0.0f) PRES_B_VREF = 0.0f; |
Lightvalve | 19:23b7c1ad8683 | 786 | |
hyhoon | 226:82a3ca333004 | 787 | if (DIR_VALVE_ENC > 0) { |
hyhoon | 226:82a3ca333004 | 788 | dac_1 = PRES_A_VREF / 3.3f; |
hyhoon | 226:82a3ca333004 | 789 | dac_2 = PRES_B_VREF / 3.3f; |
hyhoon | 226:82a3ca333004 | 790 | } else { |
hyhoon | 226:82a3ca333004 | 791 | dac_1 = PRES_B_VREF / 3.3f; |
hyhoon | 226:82a3ca333004 | 792 | dac_2 = PRES_A_VREF / 3.3f; |
hyhoon | 226:82a3ca333004 | 793 | } |
Lightvalve | 13:747daba9cf59 | 794 | } |
Lightvalve | 13:747daba9cf59 | 795 | } else { |
Lightvalve | 57:f4819de54e7a | 796 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 14:8e7590227d22 | 797 | TMR3_COUNT_PRES_NULL = 0; |
Lightvalve | 13:747daba9cf59 | 798 | CUR_PRES_A_sum = 0; |
Lightvalve | 13:747daba9cf59 | 799 | CUR_PRES_B_sum = 0; |
Lightvalve | 13:747daba9cf59 | 800 | CUR_PRES_A_mean = 0; |
Lightvalve | 13:747daba9cf59 | 801 | CUR_PRES_B_mean = 0; |
Lightvalve | 14:8e7590227d22 | 802 | |
Lightvalve | 170:42c938a40313 | 803 | spi_eeprom_write(RID_PRES_A_SENSOR_VREF, (int16_t) (PRES_A_VREF * 1000.0f)); |
Lightvalve | 170:42c938a40313 | 804 | spi_eeprom_write(RID_PRES_B_SENSOR_VREF, (int16_t) (PRES_B_VREF * 1000.0f)); |
Lightvalve | 19:23b7c1ad8683 | 805 | |
hyhoon | 226:82a3ca333004 | 806 | if (DIR_VALVE_ENC > 0) { |
hyhoon | 226:82a3ca333004 | 807 | dac_1 = PRES_A_VREF / 3.3f; |
hyhoon | 226:82a3ca333004 | 808 | dac_2 = PRES_B_VREF / 3.3f; |
hyhoon | 226:82a3ca333004 | 809 | } else { |
hyhoon | 226:82a3ca333004 | 810 | dac_1 = PRES_B_VREF / 3.3f; |
hyhoon | 226:82a3ca333004 | 811 | dac_2 = PRES_A_VREF / 3.3f; |
hyhoon | 226:82a3ca333004 | 812 | } |
Lightvalve | 13:747daba9cf59 | 813 | } |
Lightvalve | 14:8e7590227d22 | 814 | TMR3_COUNT_PRES_NULL++; |
Lightvalve | 13:747daba9cf59 | 815 | break; |
Lightvalve | 13:747daba9cf59 | 816 | } |
Lightvalve | 14:8e7590227d22 | 817 | |
Lightvalve | 50:3c630b5eba9f | 818 | // case MODE_PRESSURE_SENSOR_CALIB: { |
Lightvalve | 50:3c630b5eba9f | 819 | // if (TMR3_COUNT_PRES_CALIB < 2 * TMR_FREQ_5k) { |
Lightvalve | 50:3c630b5eba9f | 820 | // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 50:3c630b5eba9f | 821 | // if (TMR3_COUNT_PRES_CALIB >= TMR_FREQ_5k) { |
Lightvalve | 50:3c630b5eba9f | 822 | // CUR_PRES_A_sum += CUR_PRES_A; |
Lightvalve | 50:3c630b5eba9f | 823 | // } |
Lightvalve | 50:3c630b5eba9f | 824 | // } else if (TMR3_COUNT_PRES_CALIB < 4 * TMR_FREQ_5k) { |
Lightvalve | 50:3c630b5eba9f | 825 | // V_out = VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 50:3c630b5eba9f | 826 | // if (TMR3_COUNT_PRES_CALIB >= 3 * TMR_FREQ_5k) { |
Lightvalve | 50:3c630b5eba9f | 827 | // CUR_PRES_B_sum += CUR_PRES_B; |
Lightvalve | 50:3c630b5eba9f | 828 | // } |
Lightvalve | 50:3c630b5eba9f | 829 | // } else { |
Lightvalve | 50:3c630b5eba9f | 830 | // CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 50:3c630b5eba9f | 831 | // TMR3_COUNT_PRES_CALIB = 0; |
Lightvalve | 50:3c630b5eba9f | 832 | // V_out = 0; |
Lightvalve | 50:3c630b5eba9f | 833 | // PRES_SENSOR_A_PULSE_PER_BAR = CUR_PRES_A_sum / ((float) TMR_FREQ_5k - 1.0f) - PRES_A_NULL; |
Lightvalve | 50:3c630b5eba9f | 834 | // PRES_SENSOR_A_PULSE_PER_BAR = PRES_SENSOR_A_PULSE_PER_BAR / ((float) PRES_SUPPLY - 1.0f); |
Lightvalve | 50:3c630b5eba9f | 835 | // PRES_SENSOR_B_PULSE_PER_BAR = CUR_PRES_B_sum / ((float) TMR_FREQ_5k - 1.0f) - PRES_B_NULL; |
Lightvalve | 50:3c630b5eba9f | 836 | // PRES_SENSOR_B_PULSE_PER_BAR = PRES_SENSOR_B_PULSE_PER_BAR / ((float) PRES_SUPPLY - 1.0f); |
Lightvalve | 50:3c630b5eba9f | 837 | // CUR_PRES_A_sum = 0; |
Lightvalve | 50:3c630b5eba9f | 838 | // CUR_PRES_B_sum = 0; |
Lightvalve | 50:3c630b5eba9f | 839 | // CUR_PRES_A_mean = 0; |
Lightvalve | 50:3c630b5eba9f | 840 | // CUR_PRES_B_mean = 0; |
Lightvalve | 50:3c630b5eba9f | 841 | // |
hyhoon | 226:82a3ca333004 | 842 | // |
Lightvalve | 50:3c630b5eba9f | 843 | // |
Lightvalve | 50:3c630b5eba9f | 844 | // //spi_eeprom_write(RID_PRES_SENSOR_A_PULSE_PER_BAR, (int16_t) (PRES_SENSOR_A_PULSE_PER_BAR * 100.0f)); |
Lightvalve | 50:3c630b5eba9f | 845 | // //spi_eeprom_write(RID_PRES_SENSOR_B_PULSE_PER_BAR, (int16_t) (PRES_SENSOR_B_PULSE_PER_BAR * 100.0f)); |
Lightvalve | 50:3c630b5eba9f | 846 | // } |
Lightvalve | 50:3c630b5eba9f | 847 | // TMR3_COUNT_PRES_CALIB++; |
Lightvalve | 50:3c630b5eba9f | 848 | // break; |
Lightvalve | 50:3c630b5eba9f | 849 | // } |
Lightvalve | 19:23b7c1ad8683 | 850 | |
Lightvalve | 50:3c630b5eba9f | 851 | // case MODE_ROTARY_FRICTION_TUNING: { |
Lightvalve | 50:3c630b5eba9f | 852 | // if (TMR3_COUNT_ROTARY_FRIC_TUNE % (5 * TMR_FREQ_5k) == 0) freq_fric_tune = 4.0f + 3.0f * sin(2 * 3.14159f * 0.5f * TMR3_COUNT_ROTARY_FRIC_TUNE * 0.0001f * 0.05f); |
Lightvalve | 50:3c630b5eba9f | 853 | // V_out = PWM_out * sin(2 * 3.14159f * freq_fric_tune * TMR3_COUNT_ROTARY_FRIC_TUNE * 0.0001f); |
Lightvalve | 50:3c630b5eba9f | 854 | // if (V_out > 0) V_out = VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 50:3c630b5eba9f | 855 | // else V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 50:3c630b5eba9f | 856 | // TMR3_COUNT_ROTARY_FRIC_TUNE++; |
Lightvalve | 50:3c630b5eba9f | 857 | // if (TMR3_COUNT_ROTARY_FRIC_TUNE > TUNING_TIME * TMR_FREQ_5k) { |
Lightvalve | 50:3c630b5eba9f | 858 | // TMR3_COUNT_ROTARY_FRIC_TUNE = 0; |
Lightvalve | 50:3c630b5eba9f | 859 | // V_out = 0.0f; |
Lightvalve | 50:3c630b5eba9f | 860 | // CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 50:3c630b5eba9f | 861 | // } |
Lightvalve | 50:3c630b5eba9f | 862 | // break; |
Lightvalve | 50:3c630b5eba9f | 863 | // } |
Lightvalve | 14:8e7590227d22 | 864 | |
Lightvalve | 14:8e7590227d22 | 865 | case MODE_DDV_POS_VS_PWM_ID: { |
Lightvalve | 59:f308b1656d9c | 866 | CONTROL_MODE = MODE_VALVE_OPEN_LOOP; |
Lightvalve | 13:747daba9cf59 | 867 | VALVE_ID_timer = VALVE_ID_timer + 1; |
Lightvalve | 14:8e7590227d22 | 868 | |
Lightvalve | 14:8e7590227d22 | 869 | if(VALVE_ID_timer < TMR_FREQ_5k*1) { |
Lightvalve | 60:64181f1d3e60 | 870 | Vout.ref = 3000.0f * sin(2.0f*3.14f*VALVE_ID_timer/TMR_FREQ_5k * 100.0f); |
Lightvalve | 14:8e7590227d22 | 871 | } else if(VALVE_ID_timer < TMR_FREQ_5k*2) { |
Lightvalve | 59:f308b1656d9c | 872 | Vout.ref = 1000.0f*(ID_index_array[ID_index]); |
Lightvalve | 14:8e7590227d22 | 873 | } else if(VALVE_ID_timer == TMR_FREQ_5k*2) { |
Lightvalve | 13:747daba9cf59 | 874 | VALVE_POS_TMP = 0; |
Lightvalve | 13:747daba9cf59 | 875 | data_num = 0; |
Lightvalve | 14:8e7590227d22 | 876 | } else if(VALVE_ID_timer < TMR_FREQ_5k*3) { |
Lightvalve | 13:747daba9cf59 | 877 | data_num = data_num + 1; |
Lightvalve | 13:747daba9cf59 | 878 | VALVE_POS_TMP = VALVE_POS_TMP + value; |
Lightvalve | 14:8e7590227d22 | 879 | } else if(VALVE_ID_timer == TMR_FREQ_5k*3) { |
Lightvalve | 59:f308b1656d9c | 880 | Vout.ref = 0.0f; |
Lightvalve | 59:f308b1656d9c | 881 | } else { |
Lightvalve | 13:747daba9cf59 | 882 | VALVE_POS_AVG[ID_index] = VALVE_POS_TMP / data_num; |
Lightvalve | 13:747daba9cf59 | 883 | VALVE_ID_timer = 0; |
Lightvalve | 13:747daba9cf59 | 884 | ID_index= ID_index +1; |
Lightvalve | 13:747daba9cf59 | 885 | } |
Lightvalve | 14:8e7590227d22 | 886 | |
Lightvalve | 17:1865016ca2e7 | 887 | if(ID_index>=25) { |
Lightvalve | 13:747daba9cf59 | 888 | int i; |
Lightvalve | 13:747daba9cf59 | 889 | VALVE_POS_AVG_OLD = VALVE_POS_AVG[0]; |
Lightvalve | 17:1865016ca2e7 | 890 | for(i=0; i<25; i++) { |
Lightvalve | 13:747daba9cf59 | 891 | VALVE_POS_VS_PWM[i] = (int16_t) (VALVE_POS_AVG[i]); |
Lightvalve | 14:8e7590227d22 | 892 | if(VALVE_POS_AVG[i] > VALVE_POS_AVG_OLD) { |
Lightvalve | 13:747daba9cf59 | 893 | VALVE_MAX_POS = VALVE_POS_AVG[i]; |
Lightvalve | 13:747daba9cf59 | 894 | VALVE_POS_AVG_OLD = VALVE_MAX_POS; |
Lightvalve | 14:8e7590227d22 | 895 | } else if(VALVE_POS_AVG[i] < VALVE_POS_AVG_OLD) { |
Lightvalve | 13:747daba9cf59 | 896 | VALVE_MIN_POS = VALVE_POS_AVG[i]; |
Lightvalve | 13:747daba9cf59 | 897 | VALVE_POS_AVG_OLD = VALVE_MIN_POS; |
Lightvalve | 13:747daba9cf59 | 898 | } |
Lightvalve | 13:747daba9cf59 | 899 | } |
Lightvalve | 170:42c938a40313 | 900 | spi_eeprom_write(RID_VALVE_MAX_POS, (int16_t) VALVE_MAX_POS); |
Lightvalve | 170:42c938a40313 | 901 | spi_eeprom_write(RID_VALVE_MIN_POS, (int16_t) VALVE_MIN_POS); |
Lightvalve | 170:42c938a40313 | 902 | for(int i=0; i<25; i++) { |
Lightvalve | 170:42c938a40313 | 903 | spi_eeprom_write(RID_VALVE_POS_VS_PWM_0 + i, (int16_t) VALVE_POS_VS_PWM[i]); |
Lightvalve | 170:42c938a40313 | 904 | } |
Lightvalve | 13:747daba9cf59 | 905 | ID_index = 0; |
Lightvalve | 57:f4819de54e7a | 906 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 13:747daba9cf59 | 907 | } |
Lightvalve | 14:8e7590227d22 | 908 | |
Lightvalve | 14:8e7590227d22 | 909 | |
Lightvalve | 13:747daba9cf59 | 910 | break; |
Lightvalve | 13:747daba9cf59 | 911 | } |
Lightvalve | 14:8e7590227d22 | 912 | |
Lightvalve | 14:8e7590227d22 | 913 | case MODE_DDV_DEADZONE_AND_CENTER: { |
Lightvalve | 59:f308b1656d9c | 914 | CONTROL_MODE = MODE_VALVE_OPEN_LOOP; |
Lightvalve | 13:747daba9cf59 | 915 | VALVE_DZ_timer = VALVE_DZ_timer + 1; |
Lightvalve | 14:8e7590227d22 | 916 | if(first_check == 0) { |
Lightvalve | 30:8d561f16383b | 917 | if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 918 | Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 30:8d561f16383b | 919 | } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 920 | Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 14:8e7590227d22 | 921 | pos_plus_end = pos.sen; |
Lightvalve | 30:8d561f16383b | 922 | } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 923 | Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 30:8d561f16383b | 924 | } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 925 | Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 14:8e7590227d22 | 926 | pos_minus_end = pos.sen; |
Lightvalve | 30:8d561f16383b | 927 | } else if(VALVE_DZ_timer < (int) (3.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 928 | Vout.ref = (float) P_GAIN_JOINT_POSITION * (0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen)/(float) ENC_PULSE_PER_POSITION; |
Lightvalve | 30:8d561f16383b | 929 | } else if(VALVE_DZ_timer < (int) (4.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 930 | Vout.ref = (float) P_GAIN_JOINT_POSITION * (0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen)/(float) ENC_PULSE_PER_POSITION; |
Lightvalve | 13:747daba9cf59 | 931 | data_num = data_num + 1; |
Lightvalve | 13:747daba9cf59 | 932 | VALVE_POS_TMP = VALVE_POS_TMP + value; |
Lightvalve | 30:8d561f16383b | 933 | } else if(VALVE_DZ_timer == (int) (4.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 934 | Vout.ref = (float) P_GAIN_JOINT_POSITION * (0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen)/(float) ENC_PULSE_PER_POSITION; |
Lightvalve | 13:747daba9cf59 | 935 | DDV_POS_AVG = VALVE_POS_TMP / data_num; |
Lightvalve | 14:8e7590227d22 | 936 | START_POS = pos.sen; |
Lightvalve | 13:747daba9cf59 | 937 | VALVE_POS_TMP = 0; |
Lightvalve | 13:747daba9cf59 | 938 | data_num = 0; |
Lightvalve | 14:8e7590227d22 | 939 | |
Lightvalve | 30:8d561f16383b | 940 | } else if(VALVE_DZ_timer < (int) (5.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 941 | valve_pos.ref = DDV_POS_AVG; |
Lightvalve | 14:8e7590227d22 | 942 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 14:8e7590227d22 | 943 | |
Lightvalve | 30:8d561f16383b | 944 | } else if(VALVE_DZ_timer < (int) (6.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 945 | valve_pos.ref = DDV_POS_AVG; |
Lightvalve | 14:8e7590227d22 | 946 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 60:64181f1d3e60 | 947 | |
Lightvalve | 30:8d561f16383b | 948 | } else if(VALVE_DZ_timer == (int) (6.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 949 | valve_pos.ref = DDV_POS_AVG; |
Lightvalve | 14:8e7590227d22 | 950 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 14:8e7590227d22 | 951 | FINAL_POS = pos.sen; |
Lightvalve | 14:8e7590227d22 | 952 | |
Lightvalve | 60:64181f1d3e60 | 953 | if((FINAL_POS - START_POS)>200) { |
Lightvalve | 13:747daba9cf59 | 954 | DZ_case = 1; |
Lightvalve | 60:64181f1d3e60 | 955 | } else if((FINAL_POS - START_POS)<-200) { |
Lightvalve | 13:747daba9cf59 | 956 | DZ_case = -1; |
Lightvalve | 14:8e7590227d22 | 957 | } else { |
Lightvalve | 13:747daba9cf59 | 958 | DZ_case = 0; |
Lightvalve | 13:747daba9cf59 | 959 | } |
Lightvalve | 61:bc8c8270f0ab | 960 | |
Lightvalve | 60:64181f1d3e60 | 961 | CAN_TX_PRES((int16_t) (DZ_case), (int16_t) (6)); |
Lightvalve | 50:3c630b5eba9f | 962 | |
Lightvalve | 13:747daba9cf59 | 963 | first_check = 1; |
Lightvalve | 13:747daba9cf59 | 964 | DZ_DIRECTION = 1; |
Lightvalve | 13:747daba9cf59 | 965 | VALVE_DZ_timer = 0; |
Lightvalve | 13:747daba9cf59 | 966 | Ref_Valve_Pos_Old = DDV_POS_AVG; |
Lightvalve | 13:747daba9cf59 | 967 | DZ_NUM = 1; |
Lightvalve | 13:747daba9cf59 | 968 | DZ_index = 1; |
Lightvalve | 14:8e7590227d22 | 969 | |
Lightvalve | 13:747daba9cf59 | 970 | } |
Lightvalve | 19:23b7c1ad8683 | 971 | } else { |
Lightvalve | 14:8e7590227d22 | 972 | if((DZ_case == -1 && DZ_NUM == 1) | (DZ_case == 1 && DZ_NUM == 1)) { |
Lightvalve | 30:8d561f16383b | 973 | if(VALVE_DZ_timer < (int) (1.0 * (float) TMR_FREQ_5k)) { |
Lightvalve | 60:64181f1d3e60 | 974 | Vout.ref = (float) P_GAIN_JOINT_POSITION * (0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen)/(float) ENC_PULSE_PER_POSITION; |
Lightvalve | 30:8d561f16383b | 975 | } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 976 | START_POS = pos.sen; |
Lightvalve | 30:8d561f16383b | 977 | } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 978 | valve_pos.ref = Ref_Valve_Pos_Old - DZ_case * DZ_DIRECTION * 64 / DZ_index; |
Lightvalve | 14:8e7590227d22 | 979 | if(valve_pos.ref <= VALVE_MIN_POS) { |
Lightvalve | 14:8e7590227d22 | 980 | valve_pos.ref = VALVE_MIN_POS; |
Lightvalve | 14:8e7590227d22 | 981 | } else if(valve_pos.ref >= VALVE_MAX_POS) { |
Lightvalve | 14:8e7590227d22 | 982 | valve_pos.ref = VALVE_MAX_POS; |
Lightvalve | 13:747daba9cf59 | 983 | } |
Lightvalve | 14:8e7590227d22 | 984 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 60:64181f1d3e60 | 985 | |
Lightvalve | 30:8d561f16383b | 986 | } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 987 | Ref_Valve_Pos_Old = valve_pos.ref; |
Lightvalve | 14:8e7590227d22 | 988 | FINAL_POS = pos.sen; |
Lightvalve | 14:8e7590227d22 | 989 | |
Lightvalve | 14:8e7590227d22 | 990 | if((FINAL_POS - START_POS)>100) { |
Lightvalve | 14:8e7590227d22 | 991 | DZ_DIRECTION = 1 * DZ_case; |
Lightvalve | 14:8e7590227d22 | 992 | } else if((FINAL_POS - START_POS)<-100) { |
Lightvalve | 14:8e7590227d22 | 993 | DZ_DIRECTION = -1 * DZ_case; |
Lightvalve | 14:8e7590227d22 | 994 | } else { |
Lightvalve | 13:747daba9cf59 | 995 | DZ_DIRECTION = 1 * DZ_case; |
Lightvalve | 13:747daba9cf59 | 996 | } |
Lightvalve | 14:8e7590227d22 | 997 | |
Lightvalve | 13:747daba9cf59 | 998 | VALVE_DZ_timer = 0; |
Lightvalve | 13:747daba9cf59 | 999 | DZ_index= DZ_index *2; |
Lightvalve | 14:8e7590227d22 | 1000 | if(DZ_index >= 128) { |
Lightvalve | 14:8e7590227d22 | 1001 | FIRST_DZ = valve_pos.ref; |
Lightvalve | 13:747daba9cf59 | 1002 | DZ_NUM = 2; |
Lightvalve | 13:747daba9cf59 | 1003 | Ref_Valve_Pos_Old = FIRST_DZ; |
Lightvalve | 13:747daba9cf59 | 1004 | DZ_index = 1; |
Lightvalve | 13:747daba9cf59 | 1005 | DZ_DIRECTION = 1; |
Lightvalve | 13:747daba9cf59 | 1006 | } |
Lightvalve | 13:747daba9cf59 | 1007 | } |
Lightvalve | 14:8e7590227d22 | 1008 | } else if((DZ_case == -1 && DZ_NUM == 2) | (DZ_case == 1 && DZ_NUM == 2)) { |
Lightvalve | 30:8d561f16383b | 1009 | if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 60:64181f1d3e60 | 1010 | Vout.ref = (float) P_GAIN_JOINT_POSITION * (0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen)/(float) ENC_PULSE_PER_POSITION; |
Lightvalve | 30:8d561f16383b | 1011 | } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1012 | START_POS = pos.sen; |
Lightvalve | 30:8d561f16383b | 1013 | } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 60:64181f1d3e60 | 1014 | valve_pos.ref = Ref_Valve_Pos_Old - DZ_case * DZ_DIRECTION * 64 / DZ_index; |
Lightvalve | 14:8e7590227d22 | 1015 | if(valve_pos.ref <= VALVE_MIN_POS) { |
Lightvalve | 14:8e7590227d22 | 1016 | valve_pos.ref = VALVE_MIN_POS; |
Lightvalve | 14:8e7590227d22 | 1017 | } else if(valve_pos.ref >= VALVE_MAX_POS) { |
Lightvalve | 14:8e7590227d22 | 1018 | valve_pos.ref = VALVE_MAX_POS; |
Lightvalve | 13:747daba9cf59 | 1019 | } |
Lightvalve | 14:8e7590227d22 | 1020 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 61:bc8c8270f0ab | 1021 | |
Lightvalve | 30:8d561f16383b | 1022 | } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 1023 | Vout.ref = 0.0f; |
Lightvalve | 59:f308b1656d9c | 1024 | } else if(VALVE_DZ_timer > (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1025 | Ref_Valve_Pos_Old = valve_pos.ref; |
Lightvalve | 14:8e7590227d22 | 1026 | FINAL_POS = pos.sen; |
Lightvalve | 14:8e7590227d22 | 1027 | |
Lightvalve | 14:8e7590227d22 | 1028 | if((FINAL_POS - START_POS)>100) { |
Lightvalve | 60:64181f1d3e60 | 1029 | DZ_DIRECTION = 1 * DZ_case; |
Lightvalve | 14:8e7590227d22 | 1030 | } else if((FINAL_POS - START_POS)<-100) { |
Lightvalve | 60:64181f1d3e60 | 1031 | DZ_DIRECTION = -1 * DZ_case; |
Lightvalve | 14:8e7590227d22 | 1032 | } else { |
Lightvalve | 60:64181f1d3e60 | 1033 | DZ_DIRECTION = -1 * DZ_case; |
Lightvalve | 13:747daba9cf59 | 1034 | } |
Lightvalve | 14:8e7590227d22 | 1035 | |
Lightvalve | 13:747daba9cf59 | 1036 | VALVE_DZ_timer = 0; |
Lightvalve | 60:64181f1d3e60 | 1037 | DZ_index= DZ_index * 2; |
Lightvalve | 14:8e7590227d22 | 1038 | if(DZ_index >= 128) { |
Lightvalve | 14:8e7590227d22 | 1039 | SECOND_DZ = valve_pos.ref; |
Lightvalve | 57:f4819de54e7a | 1040 | VALVE_CENTER = (int) (0.5f * (float) (FIRST_DZ) + 0.5f * (float) (SECOND_DZ)); |
Lightvalve | 13:747daba9cf59 | 1041 | first_check = 0; |
Lightvalve | 33:91b17819ec30 | 1042 | VALVE_DEADZONE_MINUS = (float) FIRST_DZ; |
Lightvalve | 33:91b17819ec30 | 1043 | VALVE_DEADZONE_PLUS = (float) SECOND_DZ; |
Lightvalve | 19:23b7c1ad8683 | 1044 | |
Lightvalve | 170:42c938a40313 | 1045 | spi_eeprom_write(RID_VALVE_CNETER, (int16_t) VALVE_CENTER); |
Lightvalve | 170:42c938a40313 | 1046 | spi_eeprom_write(RID_VALVE_MAX_POS, (int16_t) VALVE_MAX_POS); |
Lightvalve | 170:42c938a40313 | 1047 | spi_eeprom_write(RID_VALVE_MIN_POS, (int16_t) VALVE_MIN_POS); |
Lightvalve | 34:bb2ca2fc2a8e | 1048 | |
Lightvalve | 60:64181f1d3e60 | 1049 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 13:747daba9cf59 | 1050 | DZ_index = 1; |
Lightvalve | 13:747daba9cf59 | 1051 | } |
Lightvalve | 13:747daba9cf59 | 1052 | } |
Lightvalve | 14:8e7590227d22 | 1053 | } else if(DZ_case == 0 && DZ_NUM ==1) { |
Lightvalve | 30:8d561f16383b | 1054 | if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 60:64181f1d3e60 | 1055 | Vout.ref = (float) P_GAIN_JOINT_POSITION * (0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen)/(float) ENC_PULSE_PER_POSITION; |
Lightvalve | 30:8d561f16383b | 1056 | } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1057 | START_POS = pos.sen; |
Lightvalve | 30:8d561f16383b | 1058 | } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1059 | valve_pos.ref = Ref_Valve_Pos_Old - DZ_DIRECTION * 64 / DZ_index; |
Lightvalve | 14:8e7590227d22 | 1060 | if(valve_pos.ref <= VALVE_MIN_POS) { |
Lightvalve | 14:8e7590227d22 | 1061 | valve_pos.ref = VALVE_MIN_POS; |
Lightvalve | 14:8e7590227d22 | 1062 | } else if(valve_pos.ref >= VALVE_MAX_POS) { |
Lightvalve | 14:8e7590227d22 | 1063 | valve_pos.ref = VALVE_MAX_POS; |
Lightvalve | 13:747daba9cf59 | 1064 | } |
Lightvalve | 14:8e7590227d22 | 1065 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 61:bc8c8270f0ab | 1066 | |
Lightvalve | 30:8d561f16383b | 1067 | } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1068 | Ref_Valve_Pos_Old = valve_pos.ref; |
Lightvalve | 14:8e7590227d22 | 1069 | FINAL_POS = pos.sen; |
Lightvalve | 14:8e7590227d22 | 1070 | |
Lightvalve | 14:8e7590227d22 | 1071 | if((FINAL_POS - START_POS)>100) { |
Lightvalve | 14:8e7590227d22 | 1072 | DZ_DIRECTION = 1; |
Lightvalve | 14:8e7590227d22 | 1073 | } else if((FINAL_POS - START_POS)<-100) { |
Lightvalve | 14:8e7590227d22 | 1074 | DZ_DIRECTION = -1; |
Lightvalve | 14:8e7590227d22 | 1075 | } else { |
Lightvalve | 13:747daba9cf59 | 1076 | DZ_DIRECTION = 1; |
Lightvalve | 13:747daba9cf59 | 1077 | } |
Lightvalve | 13:747daba9cf59 | 1078 | VALVE_DZ_timer = 0; |
Lightvalve | 13:747daba9cf59 | 1079 | DZ_index= DZ_index *2; |
Lightvalve | 14:8e7590227d22 | 1080 | if(DZ_index >= 128) { |
Lightvalve | 14:8e7590227d22 | 1081 | FIRST_DZ = valve_pos.ref; |
Lightvalve | 13:747daba9cf59 | 1082 | DZ_NUM = 2; |
Lightvalve | 13:747daba9cf59 | 1083 | Ref_Valve_Pos_Old = FIRST_DZ; |
Lightvalve | 13:747daba9cf59 | 1084 | DZ_index = 1; |
Lightvalve | 13:747daba9cf59 | 1085 | DZ_DIRECTION = 1; |
Lightvalve | 13:747daba9cf59 | 1086 | } |
Lightvalve | 13:747daba9cf59 | 1087 | } |
Lightvalve | 14:8e7590227d22 | 1088 | } else { |
Lightvalve | 30:8d561f16383b | 1089 | if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 60:64181f1d3e60 | 1090 | Vout.ref = (float) P_GAIN_JOINT_POSITION * (0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen)/(float) ENC_PULSE_PER_POSITION; |
Lightvalve | 30:8d561f16383b | 1091 | } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1092 | START_POS = pos.sen; |
Lightvalve | 30:8d561f16383b | 1093 | } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1094 | valve_pos.ref = Ref_Valve_Pos_Old + DZ_DIRECTION * 64 / DZ_index; |
Lightvalve | 14:8e7590227d22 | 1095 | if(valve_pos.ref <= VALVE_MIN_POS) { |
Lightvalve | 14:8e7590227d22 | 1096 | valve_pos.ref = VALVE_MIN_POS; |
Lightvalve | 14:8e7590227d22 | 1097 | } else if(valve_pos.ref > VALVE_MAX_POS) { |
Lightvalve | 14:8e7590227d22 | 1098 | valve_pos.ref = VALVE_MAX_POS - 1; |
Lightvalve | 13:747daba9cf59 | 1099 | } |
Lightvalve | 14:8e7590227d22 | 1100 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 60:64181f1d3e60 | 1101 | |
Lightvalve | 30:8d561f16383b | 1102 | } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 1103 | Vout.ref = 0.0f; |
Lightvalve | 59:f308b1656d9c | 1104 | } else if(VALVE_DZ_timer > (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1105 | Ref_Valve_Pos_Old = valve_pos.ref; |
Lightvalve | 14:8e7590227d22 | 1106 | FINAL_POS = pos.sen; |
Lightvalve | 61:bc8c8270f0ab | 1107 | |
Lightvalve | 60:64181f1d3e60 | 1108 | if((FINAL_POS - START_POS)>100) { |
Lightvalve | 13:747daba9cf59 | 1109 | DZ_DIRECTION = 1; |
Lightvalve | 60:64181f1d3e60 | 1110 | } else if((FINAL_POS - START_POS)<-100) { |
Lightvalve | 60:64181f1d3e60 | 1111 | DZ_DIRECTION = -1; |
Lightvalve | 60:64181f1d3e60 | 1112 | } else { |
Lightvalve | 13:747daba9cf59 | 1113 | DZ_DIRECTION = 1; |
Lightvalve | 60:64181f1d3e60 | 1114 | } |
Lightvalve | 14:8e7590227d22 | 1115 | |
Lightvalve | 13:747daba9cf59 | 1116 | VALVE_DZ_timer = 0; |
Lightvalve | 13:747daba9cf59 | 1117 | DZ_index= DZ_index *2; |
Lightvalve | 14:8e7590227d22 | 1118 | if(DZ_index >= 128) { |
Lightvalve | 14:8e7590227d22 | 1119 | SECOND_DZ = valve_pos.ref; |
Lightvalve | 57:f4819de54e7a | 1120 | VALVE_CENTER = (int) (0.5f * (float) (FIRST_DZ) + 0.5f * (float) (SECOND_DZ)); |
Lightvalve | 13:747daba9cf59 | 1121 | first_check = 0; |
Lightvalve | 33:91b17819ec30 | 1122 | VALVE_DEADZONE_MINUS = (float) FIRST_DZ; |
Lightvalve | 33:91b17819ec30 | 1123 | VALVE_DEADZONE_PLUS = (float) SECOND_DZ; |
hyhoon | 226:82a3ca333004 | 1124 | |
Lightvalve | 170:42c938a40313 | 1125 | spi_eeprom_write(RID_VALVE_CNETER, (int16_t) VALVE_CENTER); |
Lightvalve | 170:42c938a40313 | 1126 | spi_eeprom_write(RID_VALVE_MAX_POS, (int16_t) VALVE_MAX_POS); |
Lightvalve | 170:42c938a40313 | 1127 | spi_eeprom_write(RID_VALVE_MIN_POS, (int16_t) VALVE_MIN_POS); |
Lightvalve | 34:bb2ca2fc2a8e | 1128 | |
Lightvalve | 57:f4819de54e7a | 1129 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 13:747daba9cf59 | 1130 | DZ_index = 1; |
Lightvalve | 13:747daba9cf59 | 1131 | } |
Lightvalve | 13:747daba9cf59 | 1132 | } |
Lightvalve | 13:747daba9cf59 | 1133 | } |
Lightvalve | 14:8e7590227d22 | 1134 | } |
Lightvalve | 13:747daba9cf59 | 1135 | break; |
Lightvalve | 13:747daba9cf59 | 1136 | } |
Lightvalve | 14:8e7590227d22 | 1137 | |
Lightvalve | 14:8e7590227d22 | 1138 | case MODE_DDV_POS_VS_FLOWRATE: { |
Lightvalve | 59:f308b1656d9c | 1139 | CONTROL_MODE = MODE_VALVE_OPEN_LOOP; |
Lightvalve | 13:747daba9cf59 | 1140 | VALVE_FR_timer = VALVE_FR_timer + 1; |
Lightvalve | 14:8e7590227d22 | 1141 | if(first_check == 0) { |
Lightvalve | 30:8d561f16383b | 1142 | if(VALVE_FR_timer < (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 1143 | Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 30:8d561f16383b | 1144 | } else if(VALVE_FR_timer == (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 1145 | Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 14:8e7590227d22 | 1146 | pos_plus_end = pos.sen; |
Lightvalve | 30:8d561f16383b | 1147 | } else if(VALVE_FR_timer < (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 1148 | Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 30:8d561f16383b | 1149 | } else if(VALVE_FR_timer == (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 1150 | Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 14:8e7590227d22 | 1151 | pos_minus_end = pos.sen; |
Lightvalve | 13:747daba9cf59 | 1152 | first_check = 1; |
Lightvalve | 13:747daba9cf59 | 1153 | VALVE_FR_timer = 0; |
Lightvalve | 57:f4819de54e7a | 1154 | valve_pos.ref = (float) VALVE_CENTER; |
Lightvalve | 13:747daba9cf59 | 1155 | ID_index = 0; |
Lightvalve | 13:747daba9cf59 | 1156 | max_check = 0; |
Lightvalve | 13:747daba9cf59 | 1157 | min_check = 0; |
Lightvalve | 13:747daba9cf59 | 1158 | } |
Lightvalve | 14:8e7590227d22 | 1159 | } else { |
Lightvalve | 30:8d561f16383b | 1160 | if(VALVE_FR_timer < (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 1161 | pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end; |
Lightvalve | 59:f308b1656d9c | 1162 | CONTROL_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 30:8d561f16383b | 1163 | } else if(VALVE_FR_timer == (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 13:747daba9cf59 | 1164 | data_num = 0; |
Lightvalve | 57:f4819de54e7a | 1165 | valve_pos.ref = 10.0f*((float) ID_index_array[ID_index]) + (float) VALVE_CENTER; |
Lightvalve | 14:8e7590227d22 | 1166 | |
Lightvalve | 14:8e7590227d22 | 1167 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 14:8e7590227d22 | 1168 | START_POS = pos.sen; |
Lightvalve | 30:8d561f16383b | 1169 | } else if(VALVE_FR_timer < (int) (5.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 57:f4819de54e7a | 1170 | valve_pos.ref = 10.0f*((float) ID_index_array[ID_index]) + (float) VALVE_CENTER; |
Lightvalve | 14:8e7590227d22 | 1171 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 13:747daba9cf59 | 1172 | data_num = data_num + 1; |
Lightvalve | 30:8d561f16383b | 1173 | if(abs(0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen) > 20000.0f) { |
Lightvalve | 14:8e7590227d22 | 1174 | FINAL_POS = pos.sen; |
Lightvalve | 14:8e7590227d22 | 1175 | one_period_end = 1; |
Lightvalve | 13:747daba9cf59 | 1176 | } |
Lightvalve | 30:8d561f16383b | 1177 | } else if(VALVE_FR_timer == (int) (5.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1178 | FINAL_POS = pos.sen; |
Lightvalve | 13:747daba9cf59 | 1179 | one_period_end = 1; |
Lightvalve | 59:f308b1656d9c | 1180 | V_out = 0.0f; |
Lightvalve | 13:747daba9cf59 | 1181 | } |
Lightvalve | 14:8e7590227d22 | 1182 | |
Lightvalve | 14:8e7590227d22 | 1183 | if(one_period_end == 1) { |
Lightvalve | 14:8e7590227d22 | 1184 | if(valve_pos.ref > VALVE_MAX_POS) { |
Lightvalve | 13:747daba9cf59 | 1185 | max_check = 1; |
Lightvalve | 14:8e7590227d22 | 1186 | } else if(valve_pos.ref < VALVE_MIN_POS) { |
Lightvalve | 13:747daba9cf59 | 1187 | min_check = 1; |
Lightvalve | 13:747daba9cf59 | 1188 | } |
Lightvalve | 13:747daba9cf59 | 1189 | JOINT_VEL[ID_index] = (FINAL_POS - START_POS) / data_num * TMR_FREQ_5k; // pulse/sec |
Lightvalve | 19:23b7c1ad8683 | 1190 | |
Lightvalve | 13:747daba9cf59 | 1191 | VALVE_FR_timer = 0; |
Lightvalve | 13:747daba9cf59 | 1192 | one_period_end = 0; |
Lightvalve | 13:747daba9cf59 | 1193 | ID_index= ID_index +1; |
Lightvalve | 30:8d561f16383b | 1194 | V_out = 0.0f; |
Lightvalve | 13:747daba9cf59 | 1195 | } |
Lightvalve | 14:8e7590227d22 | 1196 | |
Lightvalve | 14:8e7590227d22 | 1197 | if(max_check == 1 && min_check == 1) { |
Lightvalve | 19:23b7c1ad8683 | 1198 | |
Lightvalve | 13:747daba9cf59 | 1199 | VALVE_POS_NUM = ID_index; |
Lightvalve | 170:42c938a40313 | 1200 | for(int i=0; i<100; i++) { |
Lightvalve | 170:42c938a40313 | 1201 | spi_eeprom_write(RID_VALVE_POS_VS_FLOWRATE_0 + i, (int16_t) (JOINT_VEL[i] & 0xFFFF)); |
Lightvalve | 170:42c938a40313 | 1202 | spi_eeprom_write(RID_VALVE_POS_VS_FLOWRATE_0_1 + i, (int16_t) ((JOINT_VEL[i] >> 16) & 0xFFFF)); |
Lightvalve | 170:42c938a40313 | 1203 | } |
Lightvalve | 13:747daba9cf59 | 1204 | ID_index = 0; |
Lightvalve | 13:747daba9cf59 | 1205 | first_check = 0; |
Lightvalve | 13:747daba9cf59 | 1206 | VALVE_FR_timer = 0; |
Lightvalve | 57:f4819de54e7a | 1207 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 13:747daba9cf59 | 1208 | } |
Lightvalve | 13:747daba9cf59 | 1209 | } |
Lightvalve | 13:747daba9cf59 | 1210 | break; |
Lightvalve | 13:747daba9cf59 | 1211 | } |
Lightvalve | 58:2eade98630e2 | 1212 | |
Lightvalve | 57:f4819de54e7a | 1213 | case MODE_SYSTEM_ID: { |
Lightvalve | 169:645207e160ca | 1214 | freq_sysid_Iref = (double) cnt_sysid * DT_TMR3 * 3.0f; |
Lightvalve | 57:f4819de54e7a | 1215 | valve_pos.ref = 2500.0f * sin(2.0f * 3.14159f * freq_sysid_Iref * (double) cnt_sysid * DT_TMR3); |
Lightvalve | 57:f4819de54e7a | 1216 | CONTROL_MODE = MODE_VALVE_OPEN_LOOP; |
Lightvalve | 57:f4819de54e7a | 1217 | cnt_sysid++; |
Lightvalve | 57:f4819de54e7a | 1218 | if (freq_sysid_Iref >= 300) { |
Lightvalve | 57:f4819de54e7a | 1219 | cnt_sysid = 0; |
Lightvalve | 57:f4819de54e7a | 1220 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 57:f4819de54e7a | 1221 | } |
Lightvalve | 57:f4819de54e7a | 1222 | break; |
Lightvalve | 57:f4819de54e7a | 1223 | } |
Lightvalve | 58:2eade98630e2 | 1224 | |
Lightvalve | 169:645207e160ca | 1225 | case MODE_FREQ_TEST: { |
Lightvalve | 169:645207e160ca | 1226 | float valve_pos_ref = 2500.0f * sin(2.0f * 3.141592f * freq_test_valve_ref * (float) cnt_freq_test * DT_TMR3); |
Lightvalve | 169:645207e160ca | 1227 | if(valve_pos_ref >= 0) { |
Lightvalve | 169:645207e160ca | 1228 | valve_pos.ref = (double)VALVE_CENTER + (double)valve_pos_ref * ((double)VALVE_MAX_POS-(double)VALVE_CENTER)/10000.0f; |
Lightvalve | 169:645207e160ca | 1229 | } else { |
Lightvalve | 169:645207e160ca | 1230 | valve_pos.ref = (double)VALVE_CENTER - (double)valve_pos_ref * ((double)VALVE_MIN_POS-(double)VALVE_CENTER)/10000.0f; |
Lightvalve | 169:645207e160ca | 1231 | } |
Lightvalve | 169:645207e160ca | 1232 | ref_array[cnt_freq_test] = valve_pos_ref; |
Lightvalve | 169:645207e160ca | 1233 | if(value>=(float) VALVE_CENTER) { |
Lightvalve | 169:645207e160ca | 1234 | pos_array[cnt_freq_test] = 10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER); |
Lightvalve | 169:645207e160ca | 1235 | } else { |
Lightvalve | 169:645207e160ca | 1236 | pos_array[cnt_freq_test] = -10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER); |
Lightvalve | 169:645207e160ca | 1237 | } |
Lightvalve | 58:2eade98630e2 | 1238 | |
Lightvalve | 169:645207e160ca | 1239 | CONTROL_MODE = MODE_VALVE_POSITION_CONTROL; |
Lightvalve | 169:645207e160ca | 1240 | cnt_freq_test++; |
Lightvalve | 169:645207e160ca | 1241 | if (freq_test_valve_ref * (float) cnt_freq_test * DT_TMR3 > 2) { |
Lightvalve | 169:645207e160ca | 1242 | buffer_data_size = cnt_freq_test; |
Lightvalve | 169:645207e160ca | 1243 | cnt_freq_test = 0; |
Lightvalve | 169:645207e160ca | 1244 | cnt_send_buffer = 0; |
Lightvalve | 169:645207e160ca | 1245 | freq_test_valve_ref = freq_test_valve_ref * 1.05f; |
Lightvalve | 169:645207e160ca | 1246 | if (freq_test_valve_ref >= 400) { |
Lightvalve | 169:645207e160ca | 1247 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 169:645207e160ca | 1248 | CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 169:645207e160ca | 1249 | CAN_TX_PWM((int16_t) (1)); //1300 |
Lightvalve | 169:645207e160ca | 1250 | } |
Lightvalve | 169:645207e160ca | 1251 | CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 169:645207e160ca | 1252 | CONTROL_UTILITY_MODE = MODE_SEND_OVER; |
Lightvalve | 170:42c938a40313 | 1253 | |
Lightvalve | 169:645207e160ca | 1254 | } |
Lightvalve | 169:645207e160ca | 1255 | break; |
Lightvalve | 169:645207e160ca | 1256 | } |
Lightvalve | 169:645207e160ca | 1257 | case MODE_SEND_BUFFER: { |
Lightvalve | 169:645207e160ca | 1258 | // if (TMR2_COUNT_CAN_TX % (int) ((int) TMR_FREQ_5k/CAN_FREQ) == 0) { |
Lightvalve | 169:645207e160ca | 1259 | // CAN_TX_PRES((int16_t) (pos_array[cnt_send_buffer]), (int16_t) (ref_array[cnt_send_buffer])); // 1400 |
Lightvalve | 169:645207e160ca | 1260 | // if(cnt_send_buffer>=buffer_data_size) { |
Lightvalve | 169:645207e160ca | 1261 | // CONTROL_UTILITY_MODE = MODE_FREQ_TEST; |
Lightvalve | 169:645207e160ca | 1262 | // } |
Lightvalve | 169:645207e160ca | 1263 | // cnt_send_buffer++; |
Lightvalve | 169:645207e160ca | 1264 | // } |
Lightvalve | 169:645207e160ca | 1265 | |
Lightvalve | 169:645207e160ca | 1266 | break; |
Lightvalve | 169:645207e160ca | 1267 | } |
Lightvalve | 169:645207e160ca | 1268 | case MODE_SEND_OVER: { |
Lightvalve | 169:645207e160ca | 1269 | CAN_TX_TORQUE((int16_t) (buffer_data_size)); //1300 |
Lightvalve | 169:645207e160ca | 1270 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 169:645207e160ca | 1271 | CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 169:645207e160ca | 1272 | break; |
Lightvalve | 169:645207e160ca | 1273 | } |
Lightvalve | 170:42c938a40313 | 1274 | |
Lightvalve | 169:645207e160ca | 1275 | case MODE_STEP_TEST: { |
Lightvalve | 169:645207e160ca | 1276 | float valve_pos_ref = 0.0f; |
Lightvalve | 170:42c938a40313 | 1277 | if (cnt_step_test < (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 169:645207e160ca | 1278 | valve_pos_ref = 0.0f; |
Lightvalve | 170:42c938a40313 | 1279 | } else { |
Lightvalve | 169:645207e160ca | 1280 | valve_pos_ref = 10000.0f; |
Lightvalve | 169:645207e160ca | 1281 | } |
Lightvalve | 169:645207e160ca | 1282 | if(valve_pos_ref >= 0) { |
Lightvalve | 169:645207e160ca | 1283 | valve_pos.ref = (double)VALVE_CENTER + (double)valve_pos_ref * ((double)VALVE_MAX_POS-(double)VALVE_CENTER)/10000.0f; |
Lightvalve | 169:645207e160ca | 1284 | } else { |
Lightvalve | 169:645207e160ca | 1285 | valve_pos.ref = (double)VALVE_CENTER - (double)valve_pos_ref * ((double)VALVE_MIN_POS-(double)VALVE_CENTER)/10000.0f; |
Lightvalve | 169:645207e160ca | 1286 | } |
Lightvalve | 169:645207e160ca | 1287 | ref_array[cnt_step_test] = valve_pos_ref; |
Lightvalve | 169:645207e160ca | 1288 | if(value>=(float) VALVE_CENTER) { |
Lightvalve | 169:645207e160ca | 1289 | pos_array[cnt_step_test] = 10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER); |
Lightvalve | 169:645207e160ca | 1290 | } else { |
Lightvalve | 169:645207e160ca | 1291 | pos_array[cnt_step_test] = -10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER); |
Lightvalve | 169:645207e160ca | 1292 | } |
Lightvalve | 169:645207e160ca | 1293 | |
Lightvalve | 169:645207e160ca | 1294 | CONTROL_MODE = MODE_VALVE_POSITION_CONTROL; |
Lightvalve | 169:645207e160ca | 1295 | cnt_step_test++; |
Lightvalve | 170:42c938a40313 | 1296 | if (cnt_step_test > (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 169:645207e160ca | 1297 | buffer_data_size = cnt_step_test; |
Lightvalve | 169:645207e160ca | 1298 | cnt_step_test = 0; |
Lightvalve | 169:645207e160ca | 1299 | cnt_send_buffer = 0; |
Lightvalve | 169:645207e160ca | 1300 | CONTROL_UTILITY_MODE = MODE_SEND_OVER; |
Lightvalve | 169:645207e160ca | 1301 | CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 169:645207e160ca | 1302 | } |
Lightvalve | 170:42c938a40313 | 1303 | |
Lightvalve | 169:645207e160ca | 1304 | break; |
Lightvalve | 169:645207e160ca | 1305 | } |
Lightvalve | 57:f4819de54e7a | 1306 | |
Lightvalve | 57:f4819de54e7a | 1307 | default: |
Lightvalve | 57:f4819de54e7a | 1308 | break; |
Lightvalve | 57:f4819de54e7a | 1309 | } |
Lightvalve | 57:f4819de54e7a | 1310 | |
Lightvalve | 224:985dba42f261 | 1311 | |
Lightvalve | 224:985dba42f261 | 1312 | |
Lightvalve | 57:f4819de54e7a | 1313 | // CONTROL MODE ------------------------------------------------------------ |
Lightvalve | 57:f4819de54e7a | 1314 | switch (CONTROL_MODE) { |
Lightvalve | 57:f4819de54e7a | 1315 | case MODE_NO_ACT: { |
Lightvalve | 57:f4819de54e7a | 1316 | V_out = 0.0f; |
Lightvalve | 57:f4819de54e7a | 1317 | break; |
Lightvalve | 57:f4819de54e7a | 1318 | } |
Lightvalve | 57:f4819de54e7a | 1319 | |
Lightvalve | 57:f4819de54e7a | 1320 | case MODE_VALVE_POSITION_CONTROL: { |
Lightvalve | 67:c2812cf26c38 | 1321 | if (OPERATING_MODE == 5) { //SW Valve |
Lightvalve | 57:f4819de54e7a | 1322 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 59:f308b1656d9c | 1323 | V_out = Vout.ref; |
Lightvalve | 67:c2812cf26c38 | 1324 | } else if (CURRENT_CONTROL_MODE == 0) { //PWM |
Lightvalve | 67:c2812cf26c38 | 1325 | V_out = valve_pos.ref; |
Lightvalve | 169:645207e160ca | 1326 | } else { |
hyhoon | 226:82a3ca333004 | 1327 | I_REF = (double)(valve_pos.ref * (float)mA_PER_pulse); |
Lightvalve | 57:f4819de54e7a | 1328 | } |
Lightvalve | 57:f4819de54e7a | 1329 | break; |
Lightvalve | 57:f4819de54e7a | 1330 | } |
Lightvalve | 57:f4819de54e7a | 1331 | |
Lightvalve | 57:f4819de54e7a | 1332 | case MODE_JOINT_CONTROL: { |
Lightvalve | 169:645207e160ca | 1333 | |
Lightvalve | 57:f4819de54e7a | 1334 | double torq_ref = 0.0f; |
Lightvalve | 57:f4819de54e7a | 1335 | pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm] |
Lightvalve | 67:c2812cf26c38 | 1336 | vel.err = (0.0f - vel.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm/s] |
Lightvalve | 57:f4819de54e7a | 1337 | pos.err_sum += pos.err/(float) TMR_FREQ_5k; //[mm] |
Lightvalve | 169:645207e160ca | 1338 | |
Lightvalve | 67:c2812cf26c38 | 1339 | //K & D Low Pass Filter |
Lightvalve | 67:c2812cf26c38 | 1340 | float alpha_K_D = 1.0f/(1.0f + 5000.0f/(2.0f*3.14f*30.0f)); // f_cutoff : 30Hz |
Lightvalve | 67:c2812cf26c38 | 1341 | K_LPF = K_LPF*(1.0f-alpha_K_D)+K_SPRING*(alpha_K_D); |
Lightvalve | 67:c2812cf26c38 | 1342 | D_LPF = D_LPF*(1.0f-alpha_K_D)+D_DAMPER*(alpha_K_D); |
Lightvalve | 169:645207e160ca | 1343 | |
Lightvalve | 224:985dba42f261 | 1344 | torq_ref = torq.ref + K_LPF * pos.err - D_LPF * vel.sen / ENC_PULSE_PER_POSITION; //[N] |
Lightvalve | 57:f4819de54e7a | 1345 | |
Lightvalve | 57:f4819de54e7a | 1346 | // torque feedback |
Lightvalve | 67:c2812cf26c38 | 1347 | torq.err = torq_ref - torq.sen; //[N] |
Lightvalve | 57:f4819de54e7a | 1348 | torq.err_sum += torq.err/(float) TMR_FREQ_5k; //[N] |
Lightvalve | 57:f4819de54e7a | 1349 | |
Lightvalve | 57:f4819de54e7a | 1350 | if (((OPERATING_MODE&0b110)>>1) == 0 || ((OPERATING_MODE&0b110)>>1) == 1) { |
Lightvalve | 58:2eade98630e2 | 1351 | |
Lightvalve | 57:f4819de54e7a | 1352 | double I_REF_POS = 0.0f; |
Lightvalve | 57:f4819de54e7a | 1353 | double I_REF_FORCE_FB = 0.0f; // I_REF by Force Feedback |
Lightvalve | 57:f4819de54e7a | 1354 | double I_REF_VC = 0.0f; // I_REF for velocity compensation |
Lightvalve | 57:f4819de54e7a | 1355 | |
Lightvalve | 57:f4819de54e7a | 1356 | double temp_vel_pos = 0.0f; |
Lightvalve | 57:f4819de54e7a | 1357 | double temp_vel_torq = 0.0f; |
Lightvalve | 57:f4819de54e7a | 1358 | double wn_Pos = 2.0f * PI * 5.0f; // f_cut : 5Hz Position Control |
Lightvalve | 58:2eade98630e2 | 1359 | |
Lightvalve | 69:3995ffeaa786 | 1360 | if ((OPERATING_MODE & 0x01) == 0) { // Rotary Mode |
Lightvalve | 57:f4819de54e7a | 1361 | temp_vel_pos = (0.01f * (double) P_GAIN_JOINT_POSITION * wn_Pos * pos.err + 0.01f * (double) I_GAIN_JOINT_POSITION * wn_Pos * pos.err_sum + 0.01f * (double) VELOCITY_COMP_GAIN * vel.ref / ENC_PULSE_PER_POSITION) * PI / 180.0f; // rad/s |
Lightvalve | 57:f4819de54e7a | 1362 | // L when P-gain = 100, f_cut = 10Hz L feedforward velocity |
Lightvalve | 69:3995ffeaa786 | 1363 | } else if ((OPERATING_MODE & 0x01) == 1) { |
Lightvalve | 57:f4819de54e7a | 1364 | temp_vel_pos = (0.01f * (double) P_GAIN_JOINT_POSITION * wn_Pos * pos.err + 0.01f * (double) I_GAIN_JOINT_POSITION * wn_Pos * pos.err_sum + 0.01f * (double) VELOCITY_COMP_GAIN * vel.ref / ENC_PULSE_PER_POSITION); // mm/s |
Lightvalve | 57:f4819de54e7a | 1365 | // L when P-gain = 100, f_cut = 10Hz L feedforward velocity |
Lightvalve | 57:f4819de54e7a | 1366 | } |
Lightvalve | 57:f4819de54e7a | 1367 | if (temp_vel_pos > 0.0f) I_REF_POS = temp_vel_pos * ((double) PISTON_AREA_A * 0.00006f / (K_v * sqrt(2.0f * alpha3 / (alpha3 + 1.0f)))); |
Lightvalve | 57:f4819de54e7a | 1368 | else I_REF_POS = temp_vel_pos * ((double) PISTON_AREA_B * 0.00006f / (K_v * sqrt(2.0f / (alpha3 + 1.0f)))); |
Lightvalve | 57:f4819de54e7a | 1369 | |
Lightvalve | 57:f4819de54e7a | 1370 | // velocity compensation for torque control |
Lightvalve | 69:3995ffeaa786 | 1371 | if ((OPERATING_MODE & 0x01) == 0) { // Rotary Mode |
Lightvalve | 57:f4819de54e7a | 1372 | I_REF_FORCE_FB = 0.001f * ((double) P_GAIN_JOINT_TORQUE * torq.err + (double) I_GAIN_JOINT_TORQUE * torq.err_sum); |
Lightvalve | 57:f4819de54e7a | 1373 | // temp_vel_torq = (0.01 * (double) VELOCITY_COMP_GAIN * (double) CUR_VELOCITY / (double) ENC_PULSE_PER_POSITION) * PI / 180.0; // rad/s |
Lightvalve | 57:f4819de54e7a | 1374 | temp_vel_torq = (0.01f * (double) VELOCITY_COMP_GAIN * vel.ref / (double) ENC_PULSE_PER_POSITION) * PI / 180.0f; // rad/s |
Lightvalve | 57:f4819de54e7a | 1375 | // L feedforward velocity |
Lightvalve | 69:3995ffeaa786 | 1376 | } else if ((OPERATING_MODE & 0x01) == 1) { |
Lightvalve | 57:f4819de54e7a | 1377 | I_REF_FORCE_FB = 0.001f * 0.01f*((double) P_GAIN_JOINT_TORQUE * torq.err + (double) I_GAIN_JOINT_TORQUE * torq.err_sum); // Linear Actuators are more sensitive. |
Lightvalve | 57:f4819de54e7a | 1378 | // temp_vel_torq = (0.01 * (double) VELOCITY_COMP_GAIN * (double) CUR_VELOCITY / (double) ENC_PULSE_PER_POSITION); // mm/s |
Lightvalve | 57:f4819de54e7a | 1379 | temp_vel_torq = (0.01f * (double) VELOCITY_COMP_GAIN * vel.ref / (double) ENC_PULSE_PER_POSITION); // mm/s |
Lightvalve | 57:f4819de54e7a | 1380 | // L feedforward velocity |
Lightvalve | 57:f4819de54e7a | 1381 | } |
Lightvalve | 57:f4819de54e7a | 1382 | if (temp_vel_torq > 0.0f) I_REF_VC = temp_vel_torq * ((double) PISTON_AREA_A * 0.00006f / (K_v * sqrt(2.0f * alpha3 / (alpha3 + 1.0f)))); |
Lightvalve | 57:f4819de54e7a | 1383 | else I_REF_VC = temp_vel_torq * ((double) PISTON_AREA_B * 0.00006f / (K_v * sqrt(2.0f / (alpha3 + 1.0f)))); |
Lightvalve | 57:f4819de54e7a | 1384 | // L velocity(rad/s or mm/s) >> I_ref(mA) |
Lightvalve | 57:f4819de54e7a | 1385 | // Ref_Joint_FT_dot = (Ref_Joint_FT_Nm - Ref_Joint_FT_Nm_old) / TMR_DT_5k; |
Lightvalve | 57:f4819de54e7a | 1386 | // Ref_Joint_FT_Nm_old = Ref_Joint_FT_Nm; |
Lightvalve | 57:f4819de54e7a | 1387 | |
Lightvalve | 57:f4819de54e7a | 1388 | I_REF = (1.0f - alpha_trans) * I_REF_POS + alpha_trans * (I_REF_VC + I_REF_FORCE_FB); |
Lightvalve | 169:645207e160ca | 1389 | |
Lightvalve | 67:c2812cf26c38 | 1390 | // Anti-windup for FT |
Lightvalve | 67:c2812cf26c38 | 1391 | if (I_GAIN_JOINT_TORQUE != 0) { |
Lightvalve | 67:c2812cf26c38 | 1392 | double I_MAX = 10.0f; // Maximum Current : 10mV |
Lightvalve | 67:c2812cf26c38 | 1393 | double Ka = 2.0f / ((double) I_GAIN_JOINT_TORQUE * 0.001f); |
Lightvalve | 67:c2812cf26c38 | 1394 | if (I_REF > I_MAX) { |
Lightvalve | 67:c2812cf26c38 | 1395 | double I_rem = I_REF - I_MAX; |
Lightvalve | 67:c2812cf26c38 | 1396 | I_rem = Ka*I_rem; |
Lightvalve | 67:c2812cf26c38 | 1397 | I_REF = I_MAX; |
Lightvalve | 67:c2812cf26c38 | 1398 | torq.err_sum = torq.err_sum - I_rem /(float) TMR_FREQ_5k; |
Lightvalve | 67:c2812cf26c38 | 1399 | } else if (I_REF < -I_MAX) { |
Lightvalve | 67:c2812cf26c38 | 1400 | double I_rem = I_REF - (-I_MAX); |
Lightvalve | 67:c2812cf26c38 | 1401 | I_rem = Ka*I_rem; |
Lightvalve | 67:c2812cf26c38 | 1402 | I_REF = -I_MAX; |
Lightvalve | 67:c2812cf26c38 | 1403 | torq.err_sum = torq.err_sum - I_rem /(float) TMR_FREQ_5k; |
Lightvalve | 67:c2812cf26c38 | 1404 | } |
Lightvalve | 67:c2812cf26c38 | 1405 | } |
Lightvalve | 57:f4819de54e7a | 1406 | |
Lightvalve | 57:f4819de54e7a | 1407 | } else { |
Lightvalve | 57:f4819de54e7a | 1408 | float VALVE_POS_RAW_FORCE_FB = 0.0f; |
Lightvalve | 72:3436ce769b1e | 1409 | float VALVE_POS_RAW_FORCE_FF = 0.0f; |
Lightvalve | 72:3436ce769b1e | 1410 | float VALVE_POS_RAW = 0.0f; |
Lightvalve | 169:645207e160ca | 1411 | |
Lightvalve | 209:ebc69d6ee6f1 | 1412 | VALVE_POS_RAW_FORCE_FB = alpha_trans*(((float) P_GAIN_JOINT_TORQUE * torq.err + (float) I_GAIN_JOINT_TORQUE * torq.err_sum + (float) D_GAIN_JOINT_TORQUE * (torq.ref_diff - torq_dot.sen)) * 0.01f + DDV_JOINT_POS_FF(vel.sen))+ (1.0f-alpha_trans) * (P_GAIN_JOINT_POSITION * 0.01f * pos.err + DDV_JOINT_POS_FF(vel.ref)); |
Lightvalve | 57:f4819de54e7a | 1413 | |
Lightvalve | 72:3436ce769b1e | 1414 | VALVE_POS_RAW_FORCE_FF = P_GAIN_JOINT_TORQUE_FF * torq_ref * 0.001f + D_GAIN_JOINT_TORQUE_FF * (torq_ref - torq_ref_past) * 0.0001f; |
Lightvalve | 169:645207e160ca | 1415 | |
Lightvalve | 72:3436ce769b1e | 1416 | VALVE_POS_RAW = VALVE_POS_RAW_FORCE_FB + VALVE_POS_RAW_FORCE_FF; |
Lightvalve | 169:645207e160ca | 1417 | |
Lightvalve | 169:645207e160ca | 1418 | |
Lightvalve | 72:3436ce769b1e | 1419 | if (VALVE_POS_RAW >= 0) { |
Lightvalve | 72:3436ce769b1e | 1420 | valve_pos.ref = VALVE_POS_RAW + VALVE_DEADZONE_PLUS; |
Lightvalve | 57:f4819de54e7a | 1421 | } else { |
Lightvalve | 72:3436ce769b1e | 1422 | valve_pos.ref = VALVE_POS_RAW + VALVE_DEADZONE_MINUS; |
Lightvalve | 57:f4819de54e7a | 1423 | } |
Lightvalve | 57:f4819de54e7a | 1424 | |
Lightvalve | 57:f4819de54e7a | 1425 | if(I_GAIN_JOINT_TORQUE != 0) { |
Lightvalve | 139:15621998925b | 1426 | double Ka = 2.0f / (double) I_GAIN_JOINT_TORQUE * 100.0f; |
Lightvalve | 57:f4819de54e7a | 1427 | if(valve_pos.ref>VALVE_MAX_POS) { |
Lightvalve | 57:f4819de54e7a | 1428 | double valve_pos_rem = valve_pos.ref - VALVE_MAX_POS; |
Lightvalve | 57:f4819de54e7a | 1429 | valve_pos_rem = valve_pos_rem * Ka; |
Lightvalve | 57:f4819de54e7a | 1430 | valve_pos.ref = VALVE_MAX_POS; |
Lightvalve | 57:f4819de54e7a | 1431 | torq.err_sum = torq.err_sum - valve_pos_rem/(float) TMR_FREQ_5k; |
Lightvalve | 57:f4819de54e7a | 1432 | } else if(valve_pos.ref < VALVE_MIN_POS) { |
Lightvalve | 57:f4819de54e7a | 1433 | double valve_pos_rem = valve_pos.ref - VALVE_MIN_POS; |
Lightvalve | 57:f4819de54e7a | 1434 | valve_pos_rem = valve_pos_rem * Ka; |
Lightvalve | 57:f4819de54e7a | 1435 | valve_pos.ref = VALVE_MIN_POS; |
Lightvalve | 57:f4819de54e7a | 1436 | torq.err_sum = torq.err_sum - valve_pos_rem/(float) TMR_FREQ_5k; |
Lightvalve | 57:f4819de54e7a | 1437 | } |
Lightvalve | 57:f4819de54e7a | 1438 | } |
Lightvalve | 61:bc8c8270f0ab | 1439 | |
Lightvalve | 57:f4819de54e7a | 1440 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 60:64181f1d3e60 | 1441 | V_out = (float) Vout.ref; |
Lightvalve | 57:f4819de54e7a | 1442 | } |
Lightvalve | 72:3436ce769b1e | 1443 | torq_ref_past = torq_ref; |
Lightvalve | 57:f4819de54e7a | 1444 | break; |
Lightvalve | 57:f4819de54e7a | 1445 | } |
Lightvalve | 58:2eade98630e2 | 1446 | |
Lightvalve | 57:f4819de54e7a | 1447 | case MODE_VALVE_OPEN_LOOP: { |
Lightvalve | 57:f4819de54e7a | 1448 | V_out = (float) Vout.ref; |
Lightvalve | 57:f4819de54e7a | 1449 | break; |
Lightvalve | 57:f4819de54e7a | 1450 | } |
Lightvalve | 169:645207e160ca | 1451 | |
Lightvalve | 138:a843f32ced33 | 1452 | case MODE_JOINT_ADAPTIVE_BACKSTEPPING: { |
Lightvalve | 169:645207e160ca | 1453 | |
Lightvalve | 169:645207e160ca | 1454 | |
Lightvalve | 139:15621998925b | 1455 | float Va = (1256.6f + Amm * pos.sen/(float)(ENC_PULSE_PER_POSITION)) * 0.000000001f; // 4mm pipe * 100mm + (25mm Cylinder 18mm Rod) * x, unit : m^3 |
Lightvalve | 139:15621998925b | 1456 | float Vb = (1256.6f + Amm * (79.0f - pos.sen/(float)(ENC_PULSE_PER_POSITION))) * 0.000000001f; // 4mm pipe * 100mm + (25mm Cylinder 18mm Rod) * (79.0mm-x), unit : m^3 |
Lightvalve | 218:066030f7951f | 1457 | |
Lightvalve | 170:42c938a40313 | 1458 | V_adapt = 1.0f / (1.0f/Va + 1.0f/Vb); //initial 0.0000053f |
Lightvalve | 169:645207e160ca | 1459 | |
Lightvalve | 218:066030f7951f | 1460 | //float f3 = -Amm*Amm*beta*0.000001f*0.000001f/V_adapt * vel.sen/(float)(ENC_PULSE_PER_POSITION)*0.001f; // unit : N/s //xdot=10mm/s일때 -137076 |
Lightvalve | 218:066030f7951f | 1461 | float f3_hat = -a_hat * vel.sen/(float)(ENC_PULSE_PER_POSITION)*0.001f; // unit : N/s //xdot=10mm/s일때 -137076 |
Lightvalve | 169:645207e160ca | 1462 | |
Lightvalve | 138:a843f32ced33 | 1463 | float g3_prime = 0.0f; |
Lightvalve | 169:645207e160ca | 1464 | if (torq.sen > Amm*(Ps-Pt)*0.000001f) { |
Lightvalve | 138:a843f32ced33 | 1465 | g3_prime = 1.0f; |
Lightvalve | 138:a843f32ced33 | 1466 | } else if (torq.sen < -Amm*(Ps-Pt)*0.000001f) { |
Lightvalve | 138:a843f32ced33 | 1467 | g3_prime = -1.0f; |
Lightvalve | 138:a843f32ced33 | 1468 | } else { |
Lightvalve | 138:a843f32ced33 | 1469 | if ((value-VALVE_CENTER) > 0) { |
Lightvalve | 169:645207e160ca | 1470 | g3_prime = sqrt(Ps-Pt-torq.sen/Amm*1000000.0f); |
Lightvalve | 169:645207e160ca | 1471 | // g3_prime = sqrt(Ps-Pt); |
Lightvalve | 138:a843f32ced33 | 1472 | } else { |
Lightvalve | 138:a843f32ced33 | 1473 | g3_prime = sqrt(Ps-Pt+torq.sen/Amm*1000000.0f); |
Lightvalve | 139:15621998925b | 1474 | // g3_prime = sqrt(Ps-Pt); |
Lightvalve | 138:a843f32ced33 | 1475 | } |
Lightvalve | 138:a843f32ced33 | 1476 | } |
Lightvalve | 138:a843f32ced33 | 1477 | float tau = 0.01f; |
Lightvalve | 142:43026242815a | 1478 | float K_valve = 0.0004f; |
Lightvalve | 169:645207e160ca | 1479 | |
Lightvalve | 138:a843f32ced33 | 1480 | float x_v = 0.0f; //x_v : -1~1 |
Lightvalve | 138:a843f32ced33 | 1481 | if(value>=VALVE_CENTER) { |
Lightvalve | 138:a843f32ced33 | 1482 | x_v = 1.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER); |
Lightvalve | 138:a843f32ced33 | 1483 | } else { |
Lightvalve | 138:a843f32ced33 | 1484 | x_v = -1.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER); |
Lightvalve | 138:a843f32ced33 | 1485 | } |
Lightvalve | 138:a843f32ced33 | 1486 | float f4 = -x_v/tau; |
Lightvalve | 138:a843f32ced33 | 1487 | float g4 = K_valve/tau; |
Lightvalve | 169:645207e160ca | 1488 | |
Lightvalve | 139:15621998925b | 1489 | float torq_ref_dot = torq.ref_diff * 500.0f; |
Lightvalve | 169:645207e160ca | 1490 | |
Lightvalve | 138:a843f32ced33 | 1491 | pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm] |
Lightvalve | 138:a843f32ced33 | 1492 | vel.err = (0.0f - vel.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm/s] |
Lightvalve | 138:a843f32ced33 | 1493 | pos.err_sum += pos.err/(float) TMR_FREQ_5k; //[mm] |
Lightvalve | 169:645207e160ca | 1494 | |
Lightvalve | 138:a843f32ced33 | 1495 | torq.err = torq.ref - torq.sen; //[N] |
Lightvalve | 138:a843f32ced33 | 1496 | torq.err_sum += torq.err/(float) TMR_FREQ_5k; //[N] |
Lightvalve | 169:645207e160ca | 1497 | |
Lightvalve | 212:ec41f1449ef9 | 1498 | float k3 = 2000.0f; //2000 //20000 |
Lightvalve | 142:43026242815a | 1499 | float k4 = 10.0f; |
Lightvalve | 142:43026242815a | 1500 | float rho3 = 3.2f; |
Lightvalve | 212:ec41f1449ef9 | 1501 | float rho4 = 10000000.0f; //25000000.0f; |
Lightvalve | 218:066030f7951f | 1502 | float x_4_des = (-f3_hat + torq_ref_dot - k3*(-torq.err))/(gamma_hat*g3_prime); |
Lightvalve | 138:a843f32ced33 | 1503 | if (x_4_des > 1) x_4_des = 1; |
Lightvalve | 138:a843f32ced33 | 1504 | else if (x_4_des < -1) x_4_des = -1; |
Lightvalve | 169:645207e160ca | 1505 | |
Lightvalve | 139:15621998925b | 1506 | if (x_4_des > 0) { |
Lightvalve | 139:15621998925b | 1507 | valve_pos.ref = x_4_des * (float)(VALVE_MAX_POS - VALVE_CENTER) + (float) VALVE_CENTER; |
Lightvalve | 139:15621998925b | 1508 | } else { |
Lightvalve | 139:15621998925b | 1509 | valve_pos.ref = x_4_des * (float)(VALVE_CENTER - VALVE_MIN_POS) + (float) VALVE_CENTER; |
Lightvalve | 139:15621998925b | 1510 | } |
Lightvalve | 169:645207e160ca | 1511 | |
Lightvalve | 138:a843f32ced33 | 1512 | float x_4_des_dot = (x_4_des - x_4_des_old)*(float) TMR_FREQ_5k; |
Lightvalve | 138:a843f32ced33 | 1513 | x_4_des_old = x_4_des; |
Lightvalve | 212:ec41f1449ef9 | 1514 | float V_input = 0.0f; |
Lightvalve | 139:15621998925b | 1515 | V_out = (-f4 + x_4_des_dot - k4*(x_v-x_4_des)- rho3/rho4*gamma_hat*g3_prime*(-torq.err))/g4; |
Lightvalve | 218:066030f7951f | 1516 | |
Lightvalve | 218:066030f7951f | 1517 | float rho_a = 0.00001f; |
Lightvalve | 218:066030f7951f | 1518 | float a_hat_dot = -rho3/rho_a*vel.sen/(float)(ENC_PULSE_PER_POSITION)*0.001f*(-torq.err); |
Lightvalve | 218:066030f7951f | 1519 | a_hat = a_hat + a_hat_dot / (float) TMR_FREQ_5k; |
Lightvalve | 218:066030f7951f | 1520 | |
Lightvalve | 218:066030f7951f | 1521 | if(a_hat > -3000000.0f) a_hat = -3000000.0f; |
Lightvalve | 218:066030f7951f | 1522 | else if(a_hat < -30000000.0f) a_hat = -30000000.0f; |
Lightvalve | 218:066030f7951f | 1523 | |
Lightvalve | 169:645207e160ca | 1524 | break; |
Lightvalve | 138:a843f32ced33 | 1525 | } |
Lightvalve | 171:bfc1fd2629d8 | 1526 | |
Lightvalve | 12:6f2531038ea4 | 1527 | default: |
Lightvalve | 12:6f2531038ea4 | 1528 | break; |
Lightvalve | 12:6f2531038ea4 | 1529 | } |
Lightvalve | 14:8e7590227d22 | 1530 | |
Lightvalve | 57:f4819de54e7a | 1531 | |
Lightvalve | 57:f4819de54e7a | 1532 | if (((OPERATING_MODE&0b110)>>1) == 0 || ((OPERATING_MODE&0b110)>>1) == 1) { //Moog Valve or KNR Valve |
Lightvalve | 58:2eade98630e2 | 1533 | |
Lightvalve | 57:f4819de54e7a | 1534 | //////////////////////////////////////////////////////////////////////////// |
Lightvalve | 57:f4819de54e7a | 1535 | //////////////////////////// CURRENT CONTROL ////////////////////////////// |
Lightvalve | 57:f4819de54e7a | 1536 | //////////////////////////////////////////////////////////////////////////// |
Lightvalve | 57:f4819de54e7a | 1537 | if (CURRENT_CONTROL_MODE) { |
Lightvalve | 67:c2812cf26c38 | 1538 | double alpha_update_Iref = 1.0f / (1.0f + 5000.0f / (2.0f * 3.14f * 300.0f)); // f_cutoff : 500Hz |
Lightvalve | 57:f4819de54e7a | 1539 | I_REF_fil = (1.0f - alpha_update_Iref) * I_REF_fil + alpha_update_Iref*I_REF; |
Lightvalve | 57:f4819de54e7a | 1540 | |
hyhoon | 226:82a3ca333004 | 1541 | if (FLAG_VALVE_DEADZONE) { |
hyhoon | 226:82a3ca333004 | 1542 | if (I_REF_fil > 0.0f) I_REF_fil_DZ = I_REF_fil + (double)VALVE_DEADZONE_PLUS*mA_PER_pulse; // unit: mA |
hyhoon | 226:82a3ca333004 | 1543 | else if (I_REF_fil < 0.0f) I_REF_fil_DZ = I_REF_fil + (double)VALVE_DEADZONE_MINUS*mA_PER_pulse; // unit: mA |
hyhoon | 226:82a3ca333004 | 1544 | else I_REF_fil_DZ = I_REF_fil + (double)(VALVE_DEADZONE_PLUS+VALVE_DEADZONE_MINUS)/2.0f*mA_PER_pulse; // unit: mA |
hyhoon | 226:82a3ca333004 | 1545 | } else { |
hyhoon | 226:82a3ca333004 | 1546 | I_REF_fil_DZ = I_REF_fil; |
hyhoon | 226:82a3ca333004 | 1547 | } |
hyhoon | 226:82a3ca333004 | 1548 | I_ERR = I_REF_fil_DZ - (double)cur.sen; |
Lightvalve | 67:c2812cf26c38 | 1549 | I_ERR_INT = I_ERR_INT + (I_ERR) * 0.0002f; |
Lightvalve | 57:f4819de54e7a | 1550 | |
Lightvalve | 57:f4819de54e7a | 1551 | |
Lightvalve | 57:f4819de54e7a | 1552 | // Moog Valve Current Control Gain |
Lightvalve | 67:c2812cf26c38 | 1553 | double R_model = 500.0f; // ohm |
Lightvalve | 57:f4819de54e7a | 1554 | double L_model = 1.2f; |
Lightvalve | 57:f4819de54e7a | 1555 | double w0 = 2.0f * 3.14f * 150.0f; |
Lightvalve | 57:f4819de54e7a | 1556 | double KP_I = 0.1f * L_model*w0; |
Lightvalve | 57:f4819de54e7a | 1557 | double KI_I = 0.1f * R_model*w0; |
Lightvalve | 57:f4819de54e7a | 1558 | |
Lightvalve | 57:f4819de54e7a | 1559 | // KNR Valve Current Control Gain |
Lightvalve | 57:f4819de54e7a | 1560 | if (((OPERATING_MODE & 0b110)>>1) == 1) { // KNR Valve |
Lightvalve | 57:f4819de54e7a | 1561 | R_model = 163.0f; // ohm |
Lightvalve | 57:f4819de54e7a | 1562 | L_model = 1.0f; |
Lightvalve | 57:f4819de54e7a | 1563 | w0 = 2.0f * 3.14f * 80.0f; |
Lightvalve | 57:f4819de54e7a | 1564 | KP_I = 1.0f * L_model*w0; |
Lightvalve | 57:f4819de54e7a | 1565 | KI_I = 0.08f * R_model*w0; |
Lightvalve | 57:f4819de54e7a | 1566 | } |
Lightvalve | 57:f4819de54e7a | 1567 | |
Lightvalve | 57:f4819de54e7a | 1568 | double FF_gain = 1.0f; |
Lightvalve | 57:f4819de54e7a | 1569 | |
Lightvalve | 67:c2812cf26c38 | 1570 | VALVE_PWM_RAW = KP_I * 2.0f * I_ERR + KI_I * 2.0f* I_ERR_INT; |
hyhoon | 226:82a3ca333004 | 1571 | I_REF_fil_diff = I_REF_fil_DZ - I_REF_fil_old; |
hyhoon | 226:82a3ca333004 | 1572 | I_REF_fil_old = I_REF_fil_DZ; |
Lightvalve | 67:c2812cf26c38 | 1573 | // VALVE_PWM_RAW = VALVE_PWM_RAW + FF_gain * (R_model * I_REF_fil + L_model * I_REF_fil_diff * 5000.0f); // Unit : mV |
hyhoon | 226:82a3ca333004 | 1574 | VALVE_PWM_RAW = VALVE_PWM_RAW + FF_gain * (R_model * I_REF_fil_DZ); // Unit : mV |
Lightvalve | 67:c2812cf26c38 | 1575 | double V_MAX = 12000.0f; // Maximum Voltage : 12V = 12000mV |
Lightvalve | 57:f4819de54e7a | 1576 | |
Lightvalve | 57:f4819de54e7a | 1577 | double Ka = 3.0f / KP_I; |
Lightvalve | 57:f4819de54e7a | 1578 | if (VALVE_PWM_RAW > V_MAX) { |
Lightvalve | 57:f4819de54e7a | 1579 | V_rem = VALVE_PWM_RAW - V_MAX; |
Lightvalve | 57:f4819de54e7a | 1580 | V_rem = Ka*V_rem; |
Lightvalve | 57:f4819de54e7a | 1581 | VALVE_PWM_RAW = V_MAX; |
Lightvalve | 67:c2812cf26c38 | 1582 | I_ERR_INT = I_ERR_INT - V_rem * 0.0002f; |
Lightvalve | 57:f4819de54e7a | 1583 | } else if (VALVE_PWM_RAW < -V_MAX) { |
Lightvalve | 57:f4819de54e7a | 1584 | V_rem = VALVE_PWM_RAW - (-V_MAX); |
Lightvalve | 57:f4819de54e7a | 1585 | V_rem = Ka*V_rem; |
Lightvalve | 57:f4819de54e7a | 1586 | VALVE_PWM_RAW = -V_MAX; |
Lightvalve | 67:c2812cf26c38 | 1587 | I_ERR_INT = I_ERR_INT - V_rem * 0.0002f; |
Lightvalve | 57:f4819de54e7a | 1588 | } |
Lightvalve | 57:f4819de54e7a | 1589 | Cur_Valve_Open_pulse = cur.sen / mA_PER_pulse; |
Lightvalve | 57:f4819de54e7a | 1590 | } else { |
Lightvalve | 57:f4819de54e7a | 1591 | VALVE_PWM_RAW = I_REF * mV_PER_mA; |
Lightvalve | 57:f4819de54e7a | 1592 | Cur_Valve_Open_pulse = I_REF / mA_PER_pulse; |
Lightvalve | 57:f4819de54e7a | 1593 | } |
Lightvalve | 57:f4819de54e7a | 1594 | |
Lightvalve | 57:f4819de54e7a | 1595 | //////////////////////////////////////////////////////////////////////////// |
Lightvalve | 57:f4819de54e7a | 1596 | ///////////////// Dead Zone Cancellation & Linearization ////////////////// |
Lightvalve | 57:f4819de54e7a | 1597 | //////////////////////////////////////////////////////////////////////////// |
Lightvalve | 57:f4819de54e7a | 1598 | |
Lightvalve | 57:f4819de54e7a | 1599 | // Output Voltage Linearization |
hyhoon | 226:82a3ca333004 | 1600 | double CUR_PWM_nonlin = (double)VALVE_PWM_RAW; // Unit : mV |
Lightvalve | 67:c2812cf26c38 | 1601 | double CUR_PWM_lin = PWM_duty_byLT(CUR_PWM_nonlin); // -8000~8000 |
Lightvalve | 57:f4819de54e7a | 1602 | |
Lightvalve | 57:f4819de54e7a | 1603 | // Dead Zone Cancellation (Electrical dead-zone) |
Lightvalve | 67:c2812cf26c38 | 1604 | if (CUR_PWM_lin > 0) V_out = (float) (CUR_PWM_lin + 169.0f); |
Lightvalve | 67:c2812cf26c38 | 1605 | else if (CUR_PWM_lin < 0) V_out = (float) (CUR_PWM_lin - 174.0f); |
Lightvalve | 67:c2812cf26c38 | 1606 | else V_out = (float) (CUR_PWM_lin); |
Lightvalve | 169:645207e160ca | 1607 | } else { //////////////////////////sw valve |
Lightvalve | 89:a7b45368ea0f | 1608 | // Output Voltage Linearization |
Lightvalve | 135:79885a39c161 | 1609 | // double CUR_PWM_nonlin = V_out; // Unit : mV |
Lightvalve | 135:79885a39c161 | 1610 | // double CUR_PWM_lin = PWM_duty_byLT(CUR_PWM_nonlin); // -8000~8000 |
Lightvalve | 89:a7b45368ea0f | 1611 | |
Lightvalve | 89:a7b45368ea0f | 1612 | // Dead Zone Cancellation (Electrical dead-zone) |
Lightvalve | 135:79885a39c161 | 1613 | // if (CUR_PWM_lin > 0) V_out = (float) (CUR_PWM_lin + 169.0f); |
Lightvalve | 135:79885a39c161 | 1614 | // else if (CUR_PWM_lin < 0) V_out = (float) (CUR_PWM_lin - 174.0f); |
Lightvalve | 135:79885a39c161 | 1615 | // else V_out = (float) (CUR_PWM_lin); |
Lightvalve | 135:79885a39c161 | 1616 | |
Lightvalve | 224:985dba42f261 | 1617 | // Output Voltage Linearization & Dead Zone Cancellation (Electrical dead-zone) by SW |
Lightvalve | 135:79885a39c161 | 1618 | if (V_out > 0 ) V_out = (V_out + 180.0f)/0.8588f; |
Lightvalve | 135:79885a39c161 | 1619 | else if (V_out < 0) V_out = (V_out - 200.0f)/0.8651f; |
Lightvalve | 135:79885a39c161 | 1620 | else V_out = 0.0f; |
Lightvalve | 89:a7b45368ea0f | 1621 | } |
Lightvalve | 169:645207e160ca | 1622 | |
jobuuu | 7:e9086c72bb22 | 1623 | /******************************************************* |
jobuuu | 7:e9086c72bb22 | 1624 | *** PWM |
jobuuu | 7:e9086c72bb22 | 1625 | ********************************************************/ |
Lightvalve | 169:645207e160ca | 1626 | if(DIR_VALVE<0) { |
Lightvalve | 67:c2812cf26c38 | 1627 | V_out = -V_out; |
Lightvalve | 67:c2812cf26c38 | 1628 | } |
Lightvalve | 169:645207e160ca | 1629 | |
Lightvalve | 49:e7bcfc244d40 | 1630 | if (V_out >= VALVE_VOLTAGE_LIMIT*1000.0f) { |
Lightvalve | 49:e7bcfc244d40 | 1631 | V_out = VALVE_VOLTAGE_LIMIT*1000.0f; |
Lightvalve | 49:e7bcfc244d40 | 1632 | } else if(V_out<=-VALVE_VOLTAGE_LIMIT*1000.0f) { |
Lightvalve | 49:e7bcfc244d40 | 1633 | V_out = -VALVE_VOLTAGE_LIMIT*1000.0f; |
Lightvalve | 27:a2254a485f23 | 1634 | } |
Lightvalve | 224:985dba42f261 | 1635 | PWM_out= V_out/(SUPPLY_VOLTAGE*1000.0f); |
Lightvalve | 13:747daba9cf59 | 1636 | |
Lightvalve | 224:985dba42f261 | 1637 | // Saturation of output voltage |
Lightvalve | 30:8d561f16383b | 1638 | if(PWM_out > 1.0f) PWM_out=1.0f; |
Lightvalve | 30:8d561f16383b | 1639 | else if (PWM_out < -1.0f) PWM_out=-1.0f; |
Lightvalve | 13:747daba9cf59 | 1640 | |
Lightvalve | 30:8d561f16383b | 1641 | if (PWM_out>0.0f) { |
Lightvalve | 30:8d561f16383b | 1642 | dtc_v=0.0f; |
jobuuu | 1:e04e563be5ce | 1643 | dtc_w=PWM_out; |
jobuuu | 1:e04e563be5ce | 1644 | } else { |
jobuuu | 2:a1c0a37df760 | 1645 | dtc_v=-PWM_out; |
Lightvalve | 30:8d561f16383b | 1646 | dtc_w=0.0f; |
jobuuu | 1:e04e563be5ce | 1647 | } |
Lightvalve | 13:747daba9cf59 | 1648 | |
jobuuu | 1:e04e563be5ce | 1649 | //pwm |
Lightvalve | 30:8d561f16383b | 1650 | TIM4->CCR2 = (PWM_ARR)*(1.0f-dtc_v); |
Lightvalve | 30:8d561f16383b | 1651 | TIM4->CCR1 = (PWM_ARR)*(1.0f-dtc_w); |
Lightvalve | 67:c2812cf26c38 | 1652 | |
Lightvalve | 20:806196fda269 | 1653 | |
hyhoon | 226:82a3ca333004 | 1654 | if (TMR2_COUNT_CAN_TX % (int) ((int) TMR_FREQ_5k/CAN_FREQ) == 0) { |
hyhoon | 226:82a3ca333004 | 1655 | // Position, Velocity, and Torque (ID:1200) |
hyhoon | 226:82a3ca333004 | 1656 | if (flag_data_request[0] == HIGH) { |
hyhoon | 226:82a3ca333004 | 1657 | if ((OPERATING_MODE & 0b01) == 0) { // Rotary Actuator |
hyhoon | 226:82a3ca333004 | 1658 | if (SENSING_MODE == 0) { |
hyhoon | 226:82a3ca333004 | 1659 | CAN_TX_POSITION_FT((int16_t) (pos.sen), (int16_t) (vel.sen/10.0f), (int16_t) (torq.sen*10.0f)); |
hyhoon | 226:82a3ca333004 | 1660 | } else if (SENSING_MODE == 1) { |
hyhoon | 226:82a3ca333004 | 1661 | CAN_TX_POSITION_PRESSURE((int16_t) (pos.sen), (int16_t) (vel.sen/10.0f), (int16_t) ((pres_A.sen)*5.0f), (int16_t) ((pres_B.sen)*5.0f)); |
hyhoon | 226:82a3ca333004 | 1662 | } |
hyhoon | 226:82a3ca333004 | 1663 | } else if ((OPERATING_MODE & 0b01) == 1) { // Linear Actuator |
hyhoon | 226:82a3ca333004 | 1664 | if (SENSING_MODE == 0) { |
hyhoon | 226:82a3ca333004 | 1665 | CAN_TX_POSITION_FT((int16_t) (pos.sen/10.0f), (int16_t) (vel.sen/256.0f), (int16_t) (torq.sen * 10.0f * (float)(TORQUE_SENSOR_PULSE_PER_TORQUE))); |
hyhoon | 226:82a3ca333004 | 1666 | } else if (SENSING_MODE == 1) { |
hyhoon | 226:82a3ca333004 | 1667 | CAN_TX_POSITION_PRESSURE((int16_t) (pos.sen/10.0f), (int16_t) (vel.sen/256.0f), (int16_t) ((pres_A.sen)*5.0f), (int16_t) ((pres_B.sen)*5.0f)); |
hyhoon | 226:82a3ca333004 | 1668 | } |
hyhoon | 226:82a3ca333004 | 1669 | } |
jeongyeseong | 225:9c0becc196ba | 1670 | |
hyhoon | 226:82a3ca333004 | 1671 | } |
hyhoon | 226:82a3ca333004 | 1672 | |
hyhoon | 226:82a3ca333004 | 1673 | |
Lightvalve | 224:985dba42f261 | 1674 | // ID:1300 |
Lightvalve | 56:6f50d9d3bfee | 1675 | if (flag_data_request[1] == HIGH) { |
Lightvalve | 224:985dba42f261 | 1676 | CAN_TX_TORQUE((int16_t) 7); //1300 |
Lightvalve | 54:647072f5307a | 1677 | } |
Lightvalve | 58:2eade98630e2 | 1678 | |
hyhoon | 226:82a3ca333004 | 1679 | // Reference Current, Current Current (ID:1700) |
Lightvalve | 171:bfc1fd2629d8 | 1680 | if (flag_data_request[2] == HIGH) { |
hyhoon | 226:82a3ca333004 | 1681 | |
hyhoon | 226:82a3ca333004 | 1682 | CAN_TX_SOMETHING((int16_t) (I_REF_fil_DZ / mA_PER_pulse), (int16_t) (cur.sen / mA_PER_pulse), (int16_t) (HOMEPOS_OFFSET), (int16_t) (0)); |
jeongyeseong | 225:9c0becc196ba | 1683 | //double t_value = 0.0f; |
hyhoon | 226:82a3ca333004 | 1684 | // if(value>=(float) VALVE_CENTER) { |
hyhoon | 226:82a3ca333004 | 1685 | // t_value = 10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER); |
hyhoon | 226:82a3ca333004 | 1686 | // } else { |
hyhoon | 226:82a3ca333004 | 1687 | // t_value = -10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER); |
hyhoon | 226:82a3ca333004 | 1688 | // } |
hyhoon | 226:82a3ca333004 | 1689 | // double t_value_ref = 0.0f; |
hyhoon | 226:82a3ca333004 | 1690 | // if(valve_pos.ref>=(float) VALVE_CENTER) { |
hyhoon | 226:82a3ca333004 | 1691 | // t_value_ref = 10000.0f*((double)valve_pos.ref - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER); |
hyhoon | 226:82a3ca333004 | 1692 | // } else { |
hyhoon | 226:82a3ca333004 | 1693 | // t_value_ref = -10000.0f*((double)valve_pos.ref - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER); |
hyhoon | 226:82a3ca333004 | 1694 | // } |
hyhoon | 226:82a3ca333004 | 1695 | // |
hyhoon | 226:82a3ca333004 | 1696 | // |
hyhoon | 226:82a3ca333004 | 1697 | // CAN_TX_PRES((int16_t) (t_value), (int16_t) (t_value_ref)); // 1400 |
Lightvalve | 55:b25725257569 | 1698 | } |
Lightvalve | 58:2eade98630e2 | 1699 | |
Lightvalve | 57:f4819de54e7a | 1700 | //If it doesn't rest, below can can not work. |
Lightvalve | 57:f4819de54e7a | 1701 | for (can_rest = 0; can_rest < 10000; can_rest++) { |
Lightvalve | 57:f4819de54e7a | 1702 | ; |
Lightvalve | 57:f4819de54e7a | 1703 | } |
hyhoon | 226:82a3ca333004 | 1704 | |
Lightvalve | 224:985dba42f261 | 1705 | // ID:1500 |
Lightvalve | 169:645207e160ca | 1706 | if (flag_data_request[3] == HIGH) { |
Lightvalve | 54:647072f5307a | 1707 | //PWM |
Lightvalve | 209:ebc69d6ee6f1 | 1708 | CAN_TX_PWM((int16_t) (torq.ref)); //1500 |
Lightvalve | 54:647072f5307a | 1709 | } |
hyhoon | 226:82a3ca333004 | 1710 | |
Lightvalve | 224:985dba42f261 | 1711 | // ID:1600 |
Lightvalve | 56:6f50d9d3bfee | 1712 | if (flag_data_request[4] == HIGH) { |
Lightvalve | 54:647072f5307a | 1713 | //valve position |
Lightvalve | 218:066030f7951f | 1714 | CAN_TX_VALVE_POSITION((int16_t) (a_hat*0.0001f), (int16_t) 0, (int16_t) 0, (int16_t) 0); //1600 |
Lightvalve | 54:647072f5307a | 1715 | } |
Lightvalve | 20:806196fda269 | 1716 | |
Lightvalve | 54:647072f5307a | 1717 | TMR2_COUNT_CAN_TX = 0; |
Lightvalve | 54:647072f5307a | 1718 | } |
Lightvalve | 54:647072f5307a | 1719 | TMR2_COUNT_CAN_TX++; |
Lightvalve | 52:8ea76864368a | 1720 | |
Lightvalve | 20:806196fda269 | 1721 | } |
Lightvalve | 52:8ea76864368a | 1722 | TIM3->SR = 0x0; // reset the status register |
Lightvalve | 52:8ea76864368a | 1723 | |
Lightvalve | 58:2eade98630e2 | 1724 | } |