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