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