2011

Dependencies:   mbed FastPWM

Committer:
jobuuu
Date:
Tue Apr 20 00:42:45 2021 +0000
Revision:
229:7a1c46b9b471
Parent:
228:763bbefcb79f
Child:
230:2c3e5ecbe7e1
[Ver 1.0] The code was given by Seunghoon shin, used for hydraulic quadrupedal robot. Buyoun Cho will revise the code for Post-LIGHT (the robot name is not determined yet).

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Lightvalve 224:985dba42f261 1 //Hydraulic Control Board
Lightvalve 224:985dba42f261 2 //distributed by Sungwoo Kim
Lightvalve 224:985dba42f261 3 // 2020/12/28
jobuuu 228:763bbefcb79f 4 //revised by Buyoun Cho
jobuuu 229:7a1c46b9b471 5 // 2021/04/20
Lightvalve 224:985dba42f261 6
Lightvalve 224:985dba42f261 7 // 유의사항
Lightvalve 224:985dba42f261 8 // 소수 적을때 뒤에 f 꼭 붙이기
Lightvalve 224:985dba42f261 9 // CAN 선은 ground까지 있는 3상 선으로 써야함.
Lightvalve 224:985dba42f261 10 // 전원은 12~24V 인가.
Lightvalve 224:985dba42f261 11
GiJeongKim 0:51c43836c1d7 12 #include "mbed.h"
GiJeongKim 0:51c43836c1d7 13 #include "FastPWM.h"
GiJeongKim 0:51c43836c1d7 14 #include "INIT_HW.h"
jobuuu 2:a1c0a37df760 15 #include "function_CAN.h"
GiJeongKim 0:51c43836c1d7 16 #include "SPI_EEP_ENC.h"
GiJeongKim 0:51c43836c1d7 17 #include "I2C_AS5510.h"
GiJeongKim 0:51c43836c1d7 18 #include "setting.h"
Lightvalve 11:82d8768d7351 19 #include "function_utilities.h"
Lightvalve 16:903b5a4433b4 20 #include "stm32f4xx_flash.h"
Lightvalve 169:645207e160ca 21 #include "FlashWriter.h"
Lightvalve 170:42c938a40313 22 #include <string>
Lightvalve 170:42c938a40313 23 #include <iostream>
Lightvalve 179:d5377766d7ea 24 #include <cmath>
Lightvalve 61:bc8c8270f0ab 25
Lightvalve 61:bc8c8270f0ab 26 using namespace std;
Lightvalve 61:bc8c8270f0ab 27 Timer t;
GiJeongKim 0:51c43836c1d7 28
jobuuu 7:e9086c72bb22 29 // dac & check ///////////////////////////////////////////
GiJeongKim 0:51c43836c1d7 30 DigitalOut check(PC_2);
GiJeongKim 0:51c43836c1d7 31 DigitalOut check_2(PC_3);
GiJeongKim 0:51c43836c1d7 32 AnalogOut dac_1(PA_4);
GiJeongKim 0:51c43836c1d7 33 AnalogOut dac_2(PA_5);
Lightvalve 14:8e7590227d22 34 AnalogIn adc1(PC_4); //pressure_1
Lightvalve 14:8e7590227d22 35 AnalogIn adc2(PB_0); //pressure_2
Lightvalve 14:8e7590227d22 36 AnalogIn adc3(PC_1); //current
GiJeongKim 0:51c43836c1d7 37
jobuuu 7:e9086c72bb22 38 // PWM ///////////////////////////////////////////
Lightvalve 30:8d561f16383b 39 float dtc_v=0.0f;
Lightvalve 30:8d561f16383b 40 float dtc_w=0.0f;
GiJeongKim 0:51c43836c1d7 41
jobuuu 7:e9086c72bb22 42 // I2C ///////////////////////////////////////////
GiJeongKim 0:51c43836c1d7 43 I2C i2c(PC_9,PA_8); // SDA, SCL (for K22F)
Lightvalve 224:985dba42f261 44 const int i2c_slave_addr1 = 0x56; // AS5510 address
GiJeongKim 0:51c43836c1d7 45 unsigned int value; // 10bit output of reading sensor AS5510
GiJeongKim 0:51c43836c1d7 46
jobuuu 7:e9086c72bb22 47 // SPI ///////////////////////////////////////////
Lightvalve 170:42c938a40313 48 SPI eeprom(PB_15, PB_14, PB_13); // EEPROM //(SPI_MOSI, SPI_MISO, SPI_SCK);
Lightvalve 170:42c938a40313 49 DigitalOut eeprom_cs(PB_12);
GiJeongKim 0:51c43836c1d7 50 SPI enc(PC_12,PC_11,PC_10);
GiJeongKim 0:51c43836c1d7 51 DigitalOut enc_cs(PD_2);
Lightvalve 11:82d8768d7351 52 DigitalOut LED(PA_15);
GiJeongKim 0:51c43836c1d7 53
jobuuu 7:e9086c72bb22 54 // UART ///////////////////////////////////////////
GiJeongKim 0:51c43836c1d7 55 Serial pc(PA_9,PA_10); // _ UART
GiJeongKim 0:51c43836c1d7 56
jobuuu 7:e9086c72bb22 57 // CAN ///////////////////////////////////////////
GiJeongKim 0:51c43836c1d7 58 CAN can(PB_8, PB_9, 1000000);
GiJeongKim 0:51c43836c1d7 59 CANMessage msg;
Lightvalve 11:82d8768d7351 60 void onMsgReceived()
Lightvalve 11:82d8768d7351 61 {
Lightvalve 11:82d8768d7351 62 CAN_RX_HANDLER();
Lightvalve 11:82d8768d7351 63 }
jobuuu 2:a1c0a37df760 64
jobuuu 7:e9086c72bb22 65 // Variables ///////////////////////////////////////////
jobuuu 7:e9086c72bb22 66 State pos;
jobuuu 7:e9086c72bb22 67 State vel;
jobuuu 7:e9086c72bb22 68 State Vout;
jobuuu 7:e9086c72bb22 69 State torq;
Lightvalve 207:c70c5a9f17dd 70 State torq_dot;
jobuuu 7:e9086c72bb22 71 State pres_A;
jobuuu 7:e9086c72bb22 72 State pres_B;
jobuuu 7:e9086c72bb22 73 State cur;
Lightvalve 14:8e7590227d22 74 State valve_pos;
Lightvalve 14:8e7590227d22 75
Lightvalve 14:8e7590227d22 76 State INIT_Vout;
Lightvalve 14:8e7590227d22 77 State INIT_Valve_Pos;
Lightvalve 14:8e7590227d22 78 State INIT_Pos;
Lightvalve 14:8e7590227d22 79 State INIT_torq;
jobuuu 5:a4319f79457b 80
Lightvalve 19:23b7c1ad8683 81 extern int CID_RX_CMD;
Lightvalve 19:23b7c1ad8683 82 extern int CID_RX_REF_POSITION;
hyhoon 227:699c3e572283 83 extern int CID_RX_REF_OPENLOOP;
Lightvalve 45:35fa6884d0c6 84 extern int CID_RX_REF_PWM;
Lightvalve 19:23b7c1ad8683 85
Lightvalve 19:23b7c1ad8683 86 extern int CID_TX_INFO;
hyhoon 227:699c3e572283 87 extern int CID_TX_POS_VEL_TORQ;
hyhoon 227:699c3e572283 88 extern int CID_TX_PWM;
hyhoon 227:699c3e572283 89 extern int CID_TX_CURRENT;
Lightvalve 19:23b7c1ad8683 90 extern int CID_TX_VOUT;
Lightvalve 19:23b7c1ad8683 91 extern int CID_TX_VALVE_POSITION;
jeongyeseong 225:9c0becc196ba 92 extern int CID_TX_SOMETHING;
Lightvalve 19:23b7c1ad8683 93
hyhoon 226:82a3ca333004 94 float pres_A_new = 0.0f;
hyhoon 226:82a3ca333004 95 float pres_B_new = 0.0f;
Lightvalve 61:bc8c8270f0ab 96
hyhoon 227:699c3e572283 97
hyhoon 227:699c3e572283 98
hyhoon 227:699c3e572283 99 float temp_P_GAIN = 0.0f;
hyhoon 227:699c3e572283 100 float temp_I_GAIN = 0.0f;
hyhoon 227:699c3e572283 101 int temp_VELOCITY_COMP_GAIN = 0;
hyhoon 227:699c3e572283 102
hyhoon 227:699c3e572283 103
Lightvalve 12:6f2531038ea4 104 /*******************************************************************************
Lightvalve 12:6f2531038ea4 105 * REFERENCE MODE
Lightvalve 12:6f2531038ea4 106 ******************************************************************************/
Lightvalve 13:747daba9cf59 107 enum _REFERENCE_MODE {
Lightvalve 13:747daba9cf59 108 MODE_REF_NO_ACT = 0, //0
Lightvalve 224:985dba42f261 109 MODE_REF_DIRECT, //1
Lightvalve 224:985dba42f261 110 MODE_REF_COS_INC, //2
Lightvalve 224:985dba42f261 111 MODE_REF_LINE_INC, //3
Lightvalve 12:6f2531038ea4 112 MODE_REF_SIN_WAVE, //4
Lightvalve 224:985dba42f261 113 MODE_REF_SQUARE_WAVE, //5
Lightvalve 12:6f2531038ea4 114 };
Lightvalve 12:6f2531038ea4 115
Lightvalve 12:6f2531038ea4 116 /*******************************************************************************
Lightvalve 12:6f2531038ea4 117 * CONTROL MODE
Lightvalve 12:6f2531038ea4 118 ******************************************************************************/
Lightvalve 13:747daba9cf59 119 enum _CONTROL_MODE {
Lightvalve 12:6f2531038ea4 120 //control mode
Lightvalve 12:6f2531038ea4 121 MODE_NO_ACT = 0, //0
Lightvalve 47:fdcb8bd86fd6 122 MODE_VALVE_POSITION_CONTROL, //1
Lightvalve 47:fdcb8bd86fd6 123 MODE_JOINT_CONTROL, //2
Lightvalve 50:3c630b5eba9f 124
Lightvalve 47:fdcb8bd86fd6 125 MODE_VALVE_OPEN_LOOP, //3
Lightvalve 138:a843f32ced33 126 MODE_JOINT_ADAPTIVE_BACKSTEPPING, //4
Lightvalve 171:bfc1fd2629d8 127 MODE_RL, //5
Lightvalve 13:747daba9cf59 128
Lightvalve 12:6f2531038ea4 129 MODE_JOINT_POSITION_PRES_CONTROL_PWM, //6
Lightvalve 12:6f2531038ea4 130 MODE_JOINT_POSITION_PRES_CONTROL_VALVE_POSITION, //7
Lightvalve 12:6f2531038ea4 131 MODE_VALVE_POSITION_PRES_CONTROL_LEARNING, //8
Lightvalve 13:747daba9cf59 132
Lightvalve 12:6f2531038ea4 133 MODE_TEST_CURRENT_CONTROL, //9
Lightvalve 12:6f2531038ea4 134 MODE_TEST_PWM_CONTROL, //10
Lightvalve 21:e5f1a43ea6f9 135
Lightvalve 14:8e7590227d22 136 MODE_CURRENT_CONTROL, //11
Lightvalve 14:8e7590227d22 137 MODE_JOINT_POSITION_TORQUE_CONTROL_CURRENT, //12
Lightvalve 14:8e7590227d22 138 MODE_JOINT_POSITION_PRES_CONTROL_CURRENT, //13
Lightvalve 224:985dba42f261 139 MODE_VALVE_POSITION_TORQUE_CONTROL_LEARNING, //14
Lightvalve 13:747daba9cf59 140
Lightvalve 12:6f2531038ea4 141 //utility
Lightvalve 12:6f2531038ea4 142 MODE_TORQUE_SENSOR_NULLING = 20, //20
Lightvalve 12:6f2531038ea4 143 MODE_VALVE_NULLING_AND_DEADZONE_SETTING, //21
Lightvalve 12:6f2531038ea4 144 MODE_FIND_HOME, //22
Lightvalve 57:f4819de54e7a 145 MODE_VALVE_GAIN_SETTING, //23
Lightvalve 12:6f2531038ea4 146 MODE_PRESSURE_SENSOR_NULLING, //24
Lightvalve 12:6f2531038ea4 147 MODE_PRESSURE_SENSOR_CALIB, //25
Lightvalve 12:6f2531038ea4 148 MODE_ROTARY_FRICTION_TUNING, //26
Lightvalve 13:747daba9cf59 149
Lightvalve 224:985dba42f261 150 MODE_DDV_POS_VS_PWM_ID = 30, //30
Lightvalve 12:6f2531038ea4 151 MODE_DDV_DEADZONE_AND_CENTER, //31
Lightvalve 12:6f2531038ea4 152 MODE_DDV_POS_VS_FLOWRATE, //32
Lightvalve 57:f4819de54e7a 153 MODE_SYSTEM_ID, //33
Lightvalve 169:645207e160ca 154 MODE_FREQ_TEST, //34
Lightvalve 169:645207e160ca 155 MODE_SEND_BUFFER, //35
Lightvalve 169:645207e160ca 156 MODE_SEND_OVER, //36
Lightvalve 169:645207e160ca 157 MODE_STEP_TEST, //37
Lightvalve 12:6f2531038ea4 158 };
Lightvalve 12:6f2531038ea4 159
Lightvalve 65:a2d7c63419c2 160 void SystemClock_Config(void)
Lightvalve 65:a2d7c63419c2 161 {
Lightvalve 169:645207e160ca 162 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
Lightvalve 169:645207e160ca 163 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
Lightvalve 65:a2d7c63419c2 164
Lightvalve 224:985dba42f261 165 /* Configure the main internal regulator output voltage
Lightvalve 169:645207e160ca 166 */
Lightvalve 169:645207e160ca 167 __HAL_RCC_PWR_CLK_ENABLE();
Lightvalve 169:645207e160ca 168 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
Lightvalve 224:985dba42f261 169 /* Initializes the CPU, AHB and APB busses clocks
Lightvalve 169:645207e160ca 170 */
Lightvalve 169:645207e160ca 171 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
Lightvalve 169:645207e160ca 172 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
Lightvalve 169:645207e160ca 173 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
Lightvalve 169:645207e160ca 174 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
Lightvalve 169:645207e160ca 175 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
Lightvalve 169:645207e160ca 176 RCC_OscInitStruct.PLL.PLLM = 8;//8
Lightvalve 169:645207e160ca 177 RCC_OscInitStruct.PLL.PLLN = 180; //180
Lightvalve 169:645207e160ca 178 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
Lightvalve 169:645207e160ca 179 RCC_OscInitStruct.PLL.PLLQ = 2;
Lightvalve 169:645207e160ca 180 RCC_OscInitStruct.PLL.PLLR = 2;
Lightvalve 169:645207e160ca 181 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
Lightvalve 169:645207e160ca 182 //Error_Handler();
Lightvalve 169:645207e160ca 183 }
Lightvalve 169:645207e160ca 184 /** Activate the Over-Drive mode
Lightvalve 169:645207e160ca 185 */
Lightvalve 169:645207e160ca 186 if (HAL_PWREx_EnableOverDrive() != HAL_OK) {
Lightvalve 169:645207e160ca 187 //Error_Handler();
Lightvalve 169:645207e160ca 188 }
Lightvalve 169:645207e160ca 189 /** Initializes the CPU, AHB and APB busses clocks
Lightvalve 169:645207e160ca 190 */
Lightvalve 169:645207e160ca 191 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
Lightvalve 169:645207e160ca 192 |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
Lightvalve 169:645207e160ca 193 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
Lightvalve 169:645207e160ca 194 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
Lightvalve 169:645207e160ca 195 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
Lightvalve 169:645207e160ca 196 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
Lightvalve 65:a2d7c63419c2 197
Lightvalve 169:645207e160ca 198 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) {
Lightvalve 169:645207e160ca 199 //Error_Handler();
Lightvalve 169:645207e160ca 200 }
Lightvalve 65:a2d7c63419c2 201 }
Lightvalve 65:a2d7c63419c2 202
Lightvalve 170:42c938a40313 203
GiJeongKim 0:51c43836c1d7 204 int main()
GiJeongKim 0:51c43836c1d7 205 {
jobuuu 6:df07d3491e3a 206 /*********************************
jobuuu 1:e04e563be5ce 207 *** Initialization
jobuuu 6:df07d3491e3a 208 *********************************/
hyhoon 226:82a3ca333004 209
Lightvalve 224:985dba42f261 210 HAL_Init();
Lightvalve 224:985dba42f261 211 SystemClock_Config();
hyhoon 226:82a3ca333004 212
Lightvalve 69:3995ffeaa786 213 LED = 0;
Lightvalve 61:bc8c8270f0ab 214 pc.baud(9600);
Lightvalve 21:e5f1a43ea6f9 215
GiJeongKim 0:51c43836c1d7 216 // i2c init
hyhoon 226:82a3ca333004 217 // i2c.frequency(400 * 1000); // 0.4 mHz
hyhoon 226:82a3ca333004 218 // wait_ms(2); // Power Up wait
hyhoon 226:82a3ca333004 219 // look_for_hardware_i2c(); // Hardware present
hyhoon 226:82a3ca333004 220 // init_as5510(i2c_slave_addr1);
hyhoon 226:82a3ca333004 221 // make_delay();
jobuuu 2:a1c0a37df760 222
Lightvalve 224:985dba42f261 223 // spi init
hyhoon 227:699c3e572283 224 eeprom_cs = 1;
Lightvalve 170:42c938a40313 225 eeprom.format(8,3);
Lightvalve 170:42c938a40313 226 eeprom.frequency(5000000); //5M
hyhoon 227:699c3e572283 227 eeprom_cs = 0;
hyhoon 227:699c3e572283 228 make_delay();
hyhoon 227:699c3e572283 229
hyhoon 227:699c3e572283 230 enc_cs = 1; //sw add
GiJeongKim 0:51c43836c1d7 231 enc.format(8,0);
hyhoon 227:699c3e572283 232 enc.frequency(5000000); //10M
hyhoon 227:699c3e572283 233 enc_cs = 0; //sw add
hyhoon 227:699c3e572283 234
Lightvalve 11:82d8768d7351 235 make_delay();
Lightvalve 21:e5f1a43ea6f9 236
hyhoon 226:82a3ca333004 237
hyhoon 226:82a3ca333004 238 // spi _ enc
hyhoon 226:82a3ca333004 239 spi_enc_set_init();
hyhoon 226:82a3ca333004 240 make_delay();
hyhoon 226:82a3ca333004 241
hyhoon 226:82a3ca333004 242 ////// bno rom
hyhoon 226:82a3ca333004 243 // spi_eeprom_write(RID_BNO, (int16_t)1);
hyhoon 226:82a3ca333004 244 // make_delay();
hyhoon 226:82a3ca333004 245 ////////
hyhoon 226:82a3ca333004 246
Lightvalve 224:985dba42f261 247 // rom
Lightvalve 19:23b7c1ad8683 248 ROM_CALL_DATA();
Lightvalve 16:903b5a4433b4 249 make_delay();
Lightvalve 13:747daba9cf59 250
GiJeongKim 0:51c43836c1d7 251 // ADC init
jobuuu 5:a4319f79457b 252 Init_ADC();
Lightvalve 11:82d8768d7351 253 make_delay();
jobuuu 2:a1c0a37df760 254
GiJeongKim 0:51c43836c1d7 255 // Pwm init
GiJeongKim 0:51c43836c1d7 256 Init_PWM();
GiJeongKim 0:51c43836c1d7 257 TIM4->CR1 ^= TIM_CR1_UDIS;
Lightvalve 11:82d8768d7351 258 make_delay();
Lightvalve 13:747daba9cf59 259
GiJeongKim 0:51c43836c1d7 260 // CAN
jobuuu 2:a1c0a37df760 261 can.attach(&CAN_RX_HANDLER);
Lightvalve 11:82d8768d7351 262 CAN_ID_INIT();
Lightvalve 11:82d8768d7351 263 make_delay();
Lightvalve 34:bb2ca2fc2a8e 264
hyhoon 226:82a3ca333004 265 //can.reset();
hyhoon 226:82a3ca333004 266 can.filter(msg.id, 0xFFFFF000, CANStandard);
hyhoon 226:82a3ca333004 267
hyhoon 226:82a3ca333004 268 // TMR3 init
hyhoon 226:82a3ca333004 269 Init_TMR3();
hyhoon 226:82a3ca333004 270 TIM3->CR1 ^= TIM_CR1_UDIS;
hyhoon 226:82a3ca333004 271 make_delay();
hyhoon 226:82a3ca333004 272
hyhoon 226:82a3ca333004 273
hyhoon 226:82a3ca333004 274
Lightvalve 23:59218d4a256d 275 //Timer priority
Lightvalve 23:59218d4a256d 276 NVIC_SetPriority(TIM3_IRQn, 2);
Lightvalve 54:647072f5307a 277 NVIC_SetPriority(TIM4_IRQn, 3);
Lightvalve 34:bb2ca2fc2a8e 278
hyhoon 226:82a3ca333004 279
Lightvalve 34:bb2ca2fc2a8e 280
hyhoon 226:82a3ca333004 281
Lightvalve 13:747daba9cf59 282
Lightvalve 11:82d8768d7351 283 //DAC init
Lightvalve 58:2eade98630e2 284 if (SENSING_MODE == 0) {
Lightvalve 58:2eade98630e2 285 dac_1 = TORQUE_VREF / 3.3f;
Lightvalve 58:2eade98630e2 286 dac_2 = 0.0f;
Lightvalve 58:2eade98630e2 287 } else if (SENSING_MODE == 1) {
hyhoon 226:82a3ca333004 288 if (DIR_VALVE_ENC > 0) {
hyhoon 226:82a3ca333004 289 dac_1 = PRES_A_VREF / 3.3f;
hyhoon 226:82a3ca333004 290 dac_2 = PRES_B_VREF / 3.3f;
hyhoon 226:82a3ca333004 291 } else {
hyhoon 226:82a3ca333004 292 dac_1 = PRES_B_VREF / 3.3f;
hyhoon 226:82a3ca333004 293 dac_2 = PRES_A_VREF / 3.3f;
hyhoon 226:82a3ca333004 294 }
Lightvalve 58:2eade98630e2 295 }
Lightvalve 11:82d8768d7351 296 make_delay();
Lightvalve 13:747daba9cf59 297
Lightvalve 19:23b7c1ad8683 298 for (int i=0; i<50; i++) {
Lightvalve 11:82d8768d7351 299 if(i%2==0)
Lightvalve 38:118df027d851 300 ID_index_array[i] = - i * 0.5f;
Lightvalve 11:82d8768d7351 301 else
Lightvalve 38:118df027d851 302 ID_index_array[i] = (i+1) * 0.5f;
Lightvalve 11:82d8768d7351 303 }
Lightvalve 169:645207e160ca 304
jobuuu 6:df07d3491e3a 305 /************************************
jobuuu 1:e04e563be5ce 306 *** Program is operating!
jobuuu 6:df07d3491e3a 307 *************************************/
GiJeongKim 0:51c43836c1d7 308 while(1) {
Lightvalve 169:645207e160ca 309
Lightvalve 224:985dba42f261 310 // UART example
Lightvalve 224:985dba42f261 311 // if(timer_while==100000) {
Lightvalve 171:bfc1fd2629d8 312 // timer_while = 0;
Lightvalve 224:985dba42f261 313 // pc.printf("%f\n", value);
Lightvalve 171:bfc1fd2629d8 314 // }
Lightvalve 224:985dba42f261 315 // timer_while ++;
Lightvalve 169:645207e160ca 316
Lightvalve 224:985dba42f261 317 //i2c for SW valve
hyhoon 227:699c3e572283 318 //if(OPERATING_MODE == 5) {
hyhoon 227:699c3e572283 319 // read_field(i2c_slave_addr1);
hyhoon 227:699c3e572283 320 // if(DIR_VALVE_ENC < 0) value = 1023 - value;
hyhoon 227:699c3e572283 321 // }
GiJeongKim 0:51c43836c1d7 322 }
jobuuu 1:e04e563be5ce 323 }
jobuuu 1:e04e563be5ce 324
Lightvalve 224:985dba42f261 325
Lightvalve 224:985dba42f261 326 // Velocity feedforward for SW valve
Lightvalve 33:91b17819ec30 327 float DDV_JOINT_POS_FF(float REF_JOINT_VEL)
Lightvalve 14:8e7590227d22 328 {
Lightvalve 13:747daba9cf59 329 int i = 0;
Lightvalve 48:889798ff9329 330 float Ref_Valve_Pos_FF = 0.0f;
Lightvalve 14:8e7590227d22 331 for(i=0; i<VALVE_POS_NUM; i++) {
Lightvalve 14:8e7590227d22 332 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 333 if(i==0) {
Lightvalve 50:3c630b5eba9f 334 if(JOINT_VEL[i+1] == JOINT_VEL[i]) {
Lightvalve 57:f4819de54e7a 335 Ref_Valve_Pos_FF = (float) VALVE_CENTER;
Lightvalve 50:3c630b5eba9f 336 } else {
Lightvalve 57:f4819de54e7a 337 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 338 }
Lightvalve 14:8e7590227d22 339 } else {
Lightvalve 50:3c630b5eba9f 340 if(JOINT_VEL[i+1] == JOINT_VEL[i-1]) {
Lightvalve 57:f4819de54e7a 341 Ref_Valve_Pos_FF = (float) VALVE_CENTER;
Lightvalve 50:3c630b5eba9f 342 } else {
Lightvalve 57:f4819de54e7a 343 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 344 }
Lightvalve 13:747daba9cf59 345 }
Lightvalve 13:747daba9cf59 346 break;
Lightvalve 13:747daba9cf59 347 }
Lightvalve 13:747daba9cf59 348 }
Lightvalve 14:8e7590227d22 349 if(REF_JOINT_VEL > max(JOINT_VEL[VALVE_POS_NUM-1], JOINT_VEL[VALVE_POS_NUM-2])) {
Lightvalve 33:91b17819ec30 350 Ref_Valve_Pos_FF = (float) VALVE_MAX_POS;
Lightvalve 14:8e7590227d22 351 } else if(REF_JOINT_VEL < min(JOINT_VEL[VALVE_POS_NUM-1], JOINT_VEL[VALVE_POS_NUM-2])) {
Lightvalve 33:91b17819ec30 352 Ref_Valve_Pos_FF = (float) VALVE_MIN_POS;
Lightvalve 13:747daba9cf59 353 }
Lightvalve 36:a46e63505ed8 354
Lightvalve 224:985dba42f261 355 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 356 return Ref_Valve_Pos_FF;
Lightvalve 13:747daba9cf59 357 }
jobuuu 6:df07d3491e3a 358
Lightvalve 224:985dba42f261 359 // Valve feedforward for SW valve
Lightvalve 30:8d561f16383b 360 void VALVE_POS_CONTROL(float REF_VALVE_POS)
Lightvalve 14:8e7590227d22 361 {
Lightvalve 13:747daba9cf59 362 int i = 0;
Lightvalve 13:747daba9cf59 363
Lightvalve 38:118df027d851 364 if(REF_VALVE_POS > VALVE_MAX_POS) {
Lightvalve 38:118df027d851 365 REF_VALVE_POS = VALVE_MAX_POS;
Lightvalve 38:118df027d851 366 } else if(REF_VALVE_POS < VALVE_MIN_POS) {
Lightvalve 38:118df027d851 367 REF_VALVE_POS = VALVE_MIN_POS;
Lightvalve 38:118df027d851 368 }
Lightvalve 89:a7b45368ea0f 369 valve_pos_err = (float) (REF_VALVE_POS - value);
Lightvalve 13:747daba9cf59 370 valve_pos_err_diff = valve_pos_err - valve_pos_err_old;
Lightvalve 13:747daba9cf59 371 valve_pos_err_old = valve_pos_err;
Lightvalve 13:747daba9cf59 372 valve_pos_err_sum += valve_pos_err;
Lightvalve 89:a7b45368ea0f 373 if (valve_pos_err_sum > 1000.0f) valve_pos_err_sum = 1000.0f;
Lightvalve 89:a7b45368ea0f 374 if (valve_pos_err_sum<-1000.0f) valve_pos_err_sum = -1000.0f;
Lightvalve 13:747daba9cf59 375
Lightvalve 13:747daba9cf59 376 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 377
Lightvalve 18:b8adf1582ea3 378 for(i=0; i<24; i++) {
Lightvalve 89:a7b45368ea0f 379 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 380 if(i==0) {
Lightvalve 48:889798ff9329 381 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 382 } else {
Lightvalve 48:889798ff9329 383 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 384 }
Lightvalve 13:747daba9cf59 385 break;
Lightvalve 13:747daba9cf59 386 }
Lightvalve 13:747daba9cf59 387 }
Lightvalve 59:f308b1656d9c 388 Vout.ref = VALVE_PWM_RAW_FF + VALVE_PWM_RAW_FB;
Lightvalve 13:747daba9cf59 389 }
Lightvalve 13:747daba9cf59 390
Lightvalve 224:985dba42f261 391 // PWM duty vs. voltage output of L6205 in STM board
Lightvalve 14:8e7590227d22 392 #define LT_MAX_IDX 57
Lightvalve 30:8d561f16383b 393 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 394 -19.0f, -18.0f, -17.0f, -16.0f, -15.0f, -14.0f, -13.0f, -12.0f, -11.0f, -10.0f,
Lightvalve 34:bb2ca2fc2a8e 395 -9.0f, -8.0f, -7.0f, -6.0f, -5.0f, -4.0f, -3.0f, -2.0f, -1.0f, 0.0f,
Lightvalve 34:bb2ca2fc2a8e 396 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f,
Lightvalve 34:bb2ca2fc2a8e 397 11.0f, 12.0f, 13.0f, 14.0f, 15.0f, 16.0f, 17.0f, 18.0f, 19.0f, 20.0f,
Lightvalve 34:bb2ca2fc2a8e 398 25.0f, 30.0f, 35.0f, 40.0f, 50.0f, 60.0f, 80.0f, 100.0f
Lightvalve 34:bb2ca2fc2a8e 399 }; // duty
Lightvalve 67:c2812cf26c38 400 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 401 -150.0f, -145.0f, -145.0f, -145.0f, -135.0f, -135.0f, -135.0f, -127.5f, -127.5f, -115.0f,
Lightvalve 67:c2812cf26c38 402 -115.0f, -115.0F, -100.0f, -100.0f, -100.0f, -60.0f, -60.0f, -10.0f, -5.0f, 0.0f,
Lightvalve 67:c2812cf26c38 403 7.5f, 14.0f, 14.0f, 14.0f, 42.5f, 42.5f, 42.5f, 80.0f, 80.0f, 105.0f,
Lightvalve 67:c2812cf26c38 404 105.0f, 105.0f, 120.0f, 120.0f, 120.0f, 131.0f, 131.0f, 140.0f, 140.0f, 140.0f,
Lightvalve 67:c2812cf26c38 405 155.0f, 160.0f, 170.0f, 174.0f, 182.0f, 191.0f, 212.0f, 230.0f
Lightvalve 34:bb2ca2fc2a8e 406 }; // mV
Lightvalve 13:747daba9cf59 407
Lightvalve 30:8d561f16383b 408 float PWM_duty_byLT(float Ref_V)
Lightvalve 14:8e7590227d22 409 {
Lightvalve 30:8d561f16383b 410 float PWM_duty = 0.0f;
Lightvalve 13:747daba9cf59 411 if(Ref_V<LT_Voltage_Output[0]) {
Lightvalve 30:8d561f16383b 412 PWM_duty = (Ref_V-LT_Voltage_Output[0])/1.5f+LT_PWM_duty[0];
Lightvalve 13:747daba9cf59 413 } else if (Ref_V>=LT_Voltage_Output[LT_MAX_IDX-1]) {
Lightvalve 30:8d561f16383b 414 PWM_duty = (Ref_V-LT_Voltage_Output[LT_MAX_IDX-1])/1.5f+LT_PWM_duty[LT_MAX_IDX-1];
Lightvalve 13:747daba9cf59 415 } else {
Lightvalve 13:747daba9cf59 416 int idx = 0;
Lightvalve 13:747daba9cf59 417 for(idx=0; idx<LT_MAX_IDX-1; idx++) {
Lightvalve 30:8d561f16383b 418 float ini_x = LT_Voltage_Output[idx];
Lightvalve 30:8d561f16383b 419 float fin_x = LT_Voltage_Output[idx+1];
Lightvalve 30:8d561f16383b 420 float ini_y = LT_PWM_duty[idx];
Lightvalve 30:8d561f16383b 421 float fin_y = LT_PWM_duty[idx+1];
Lightvalve 13:747daba9cf59 422 if(Ref_V>=ini_x && Ref_V<fin_x) {
Lightvalve 13:747daba9cf59 423 PWM_duty = (fin_y-ini_y)/(fin_x-ini_x)*(Ref_V-ini_x) + ini_y;
Lightvalve 13:747daba9cf59 424 break;
Lightvalve 13:747daba9cf59 425 }
Lightvalve 13:747daba9cf59 426 }
Lightvalve 13:747daba9cf59 427 }
Lightvalve 14:8e7590227d22 428
Lightvalve 13:747daba9cf59 429 return PWM_duty;
Lightvalve 13:747daba9cf59 430 }
jobuuu 6:df07d3491e3a 431
Lightvalve 57:f4819de54e7a 432
Lightvalve 57:f4819de54e7a 433
jobuuu 2:a1c0a37df760 434 /*******************************************************************************
jobuuu 2:a1c0a37df760 435 TIMER INTERRUPT
jobuuu 2:a1c0a37df760 436 *******************************************************************************/
jobuuu 2:a1c0a37df760 437
Lightvalve 51:b46bed7fec80 438 float FREQ_TMR4 = (float)FREQ_20k;
Lightvalve 51:b46bed7fec80 439 float DT_TMR4 = (float)DT_20k;
Lightvalve 57:f4819de54e7a 440 long CNT_TMR4 = 0;
Lightvalve 57:f4819de54e7a 441 int TMR4_FREQ_10k = (int)FREQ_10k;
jobuuu 1:e04e563be5ce 442 extern "C" void TIM4_IRQHandler(void)
jobuuu 1:e04e563be5ce 443 {
Lightvalve 19:23b7c1ad8683 444 if (TIM4->SR & TIM_SR_UIF ) {
Lightvalve 21:e5f1a43ea6f9 445 /*******************************************************
Lightvalve 21:e5f1a43ea6f9 446 *** Sensor Read & Data Handling
Lightvalve 21:e5f1a43ea6f9 447 ********************************************************/
Lightvalve 13:747daba9cf59 448
Lightvalve 57:f4819de54e7a 449 //Encoder
Lightvalve 57:f4819de54e7a 450 if (CNT_TMR4 % (int) ((int) FREQ_TMR4/TMR4_FREQ_10k) == 0) {
Lightvalve 57:f4819de54e7a 451 ENC_UPDATE();
Lightvalve 57:f4819de54e7a 452 }
Lightvalve 61:bc8c8270f0ab 453
Lightvalve 61:bc8c8270f0ab 454 ADC1->CR2 |= 0x40000000;
Lightvalve 224:985dba42f261 455 // Torque Sensing =============================================
Lightvalve 58:2eade98630e2 456 if (SENSING_MODE == 0) {
Lightvalve 58:2eade98630e2 457 float pres_A_new = (((float) ADC1->DR) - 2047.5f);
Lightvalve 224:985dba42f261 458 double alpha_update_ft = 1.0f / (1.0f + FREQ_TMR4 / (2.0f * 3.14f * 100.0f)); // f_cutoff : 100Hz
Lightvalve 58:2eade98630e2 459 pres_A.sen = (1.0f - alpha_update_ft) * pres_A.sen + alpha_update_ft * pres_A_new;
Lightvalve 67:c2812cf26c38 460 torq.sen = -pres_A.sen / TORQUE_SENSOR_PULSE_PER_TORQUE;
Lightvalve 67:c2812cf26c38 461
hyhoon 226:82a3ca333004 462 // Pressure Sensing (0~210)bar =============================================
Lightvalve 58:2eade98630e2 463 } else if (SENSING_MODE == 1) {
hyhoon 226:82a3ca333004 464
hyhoon 226:82a3ca333004 465 if (DIR_VALVE_ENC > 0) {
hyhoon 226:82a3ca333004 466 pres_A_new = (((float)ADC1->DR) - PRES_A_NULL);
hyhoon 226:82a3ca333004 467 pres_B_new = (((float)ADC2->DR) - PRES_B_NULL);
hyhoon 226:82a3ca333004 468 } else {
hyhoon 226:82a3ca333004 469 pres_A_new = (((float)ADC2->DR) - PRES_A_NULL);
hyhoon 226:82a3ca333004 470 pres_B_new = (((float)ADC1->DR) - PRES_B_NULL);
hyhoon 226:82a3ca333004 471 }
Lightvalve 224:985dba42f261 472 double alpha_update_pres = 1.0f / (1.0f + FREQ_TMR4 / (2.0f * 3.14f * 200.0f)); // f_cutoff : 200Hz
Lightvalve 58:2eade98630e2 473 pres_A.sen = (1.0f - alpha_update_pres) * pres_A.sen + alpha_update_pres * pres_A_new;
Lightvalve 58:2eade98630e2 474 pres_B.sen = (1.0f - alpha_update_pres) * pres_B.sen + alpha_update_pres * pres_B_new;
Lightvalve 58:2eade98630e2 475 CUR_PRES_A_BAR = pres_A.sen / PRES_SENSOR_A_PULSE_PER_BAR;
Lightvalve 58:2eade98630e2 476 CUR_PRES_B_BAR = pres_B.sen / PRES_SENSOR_B_PULSE_PER_BAR;
Lightvalve 50:3c630b5eba9f 477
Lightvalve 58:2eade98630e2 478 if ((OPERATING_MODE & 0x01) == 0) { // Rotary Actuator
Lightvalve 58:2eade98630e2 479 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 480 } else if ((OPERATING_MODE & 0x01) == 1) { // Linear Actuator
Lightvalve 58:2eade98630e2 481 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 482 }
Lightvalve 58:2eade98630e2 483 }
Lightvalve 61:bc8c8270f0ab 484
Lightvalve 21:e5f1a43ea6f9 485 //Current
Lightvalve 21:e5f1a43ea6f9 486 //ADC3->CR2 |= 0x40000000; // adc _ 12bit
Lightvalve 51:b46bed7fec80 487 float alpha_update_cur = 1.0f/(1.0f + FREQ_TMR4/(2.0f*3.14f*500.0f)); // f_cutoff : 500Hz
Lightvalve 30:8d561f16383b 488 float cur_new = ((float)ADC3->DR-2048.0f)*20.0f/4096.0f; // unit : mA
Lightvalve 30:8d561f16383b 489 cur.sen=cur.sen*(1.0f-alpha_update_cur)+cur_new*(alpha_update_cur);
Lightvalve 58:2eade98630e2 490
Lightvalve 57:f4819de54e7a 491 CNT_TMR4++;
Lightvalve 21:e5f1a43ea6f9 492 }
Lightvalve 11:82d8768d7351 493 TIM4->SR = 0x0; // reset the status register
Lightvalve 11:82d8768d7351 494 }
Lightvalve 19:23b7c1ad8683 495
Lightvalve 19:23b7c1ad8683 496
Lightvalve 18:b8adf1582ea3 497 int j =0;
Lightvalve 54:647072f5307a 498 float FREQ_TMR3 = (float)FREQ_5k;
Lightvalve 48:889798ff9329 499 float DT_TMR3 = (float)DT_5k;
Lightvalve 45:35fa6884d0c6 500 int cnt_trans = 0;
Lightvalve 48:889798ff9329 501 double VALVE_POS_RAW_FORCE_FB_LOGGING = 0.0f;
Lightvalve 57:f4819de54e7a 502 int can_rest =0;
Lightvalve 48:889798ff9329 503
Lightvalve 11:82d8768d7351 504 extern "C" void TIM3_IRQHandler(void)
Lightvalve 21:e5f1a43ea6f9 505 {
Lightvalve 19:23b7c1ad8683 506 if (TIM3->SR & TIM_SR_UIF ) {
Lightvalve 57:f4819de54e7a 507
hyhoon 226:82a3ca333004 508
Lightvalve 57:f4819de54e7a 509 if (((OPERATING_MODE&0b110)>>1) == 0) {
Lightvalve 57:f4819de54e7a 510 K_v = 0.4f; // Moog (LPM >> mA) , 100bar
Lightvalve 57:f4819de54e7a 511 mV_PER_mA = 500.0f; // 5000mV/10mA
Lightvalve 57:f4819de54e7a 512 mV_PER_pulse = 0.5f; // 5000mV/10000pulse
Lightvalve 57:f4819de54e7a 513 mA_PER_pulse = 0.001f; // 10mA/10000pulse
Lightvalve 57:f4819de54e7a 514 } else if (((OPERATING_MODE&0b110)>>1) == 1) {
Lightvalve 57:f4819de54e7a 515 K_v = 0.5f; // KNR (LPM >> mA) , 100bar
Lightvalve 57:f4819de54e7a 516 mV_PER_mA = 166.6666f; // 5000mV/30mA
Lightvalve 57:f4819de54e7a 517 mV_PER_pulse = 0.5f; // 5000mV/10000pulse
Lightvalve 57:f4819de54e7a 518 mA_PER_pulse = 0.003f; // 30mA/10000pulse
Lightvalve 57:f4819de54e7a 519 }
Lightvalve 50:3c630b5eba9f 520
Lightvalve 50:3c630b5eba9f 521 if(MODE_POS_FT_TRANS == 1) {
Lightvalve 48:889798ff9329 522 alpha_trans = (float)(1.0f - cos(3.141592f * (float)cnt_trans * DT_TMR3 /3.0f))/2.0f;
Lightvalve 45:35fa6884d0c6 523 cnt_trans++;
Lightvalve 46:2694daea349b 524 torq.err_sum = 0;
Lightvalve 48:889798ff9329 525 if((float)cnt_trans * DT_TMR3 > 3.0f)
Lightvalve 45:35fa6884d0c6 526 MODE_POS_FT_TRANS = 2;
Lightvalve 50:3c630b5eba9f 527 } else if(MODE_POS_FT_TRANS == 3) {
Lightvalve 48:889798ff9329 528 alpha_trans = (float)(1.0f + cos(3.141592f * (float)cnt_trans * DT_TMR3 /3.0f))/2.0f;
Lightvalve 45:35fa6884d0c6 529 cnt_trans++;
Lightvalve 46:2694daea349b 530 torq.err_sum = 0;
Lightvalve 48:889798ff9329 531 if((float) cnt_trans * DT_TMR3 > 3.0f )
Lightvalve 45:35fa6884d0c6 532 MODE_POS_FT_TRANS = 0;
Lightvalve 50:3c630b5eba9f 533 } else if(MODE_POS_FT_TRANS == 2) {
Lightvalve 58:2eade98630e2 534 alpha_trans = 1.0f;
Lightvalve 45:35fa6884d0c6 535 cnt_trans = 0;
Lightvalve 50:3c630b5eba9f 536 } else {
Lightvalve 58:2eade98630e2 537 alpha_trans = 0.0f;
Lightvalve 45:35fa6884d0c6 538 cnt_trans = 0;
Lightvalve 45:35fa6884d0c6 539 }
Lightvalve 45:35fa6884d0c6 540
Lightvalve 50:3c630b5eba9f 541
hyhoon 227:699c3e572283 542 // Reference Loop ==========================================================
hyhoon 227:699c3e572283 543 switch (REFERENCE_MODE) {
hyhoon 227:699c3e572283 544 case MODE_REF_NO_ACT: {
hyhoon 227:699c3e572283 545 break;
hyhoon 227:699c3e572283 546 }
hyhoon 227:699c3e572283 547
hyhoon 227:699c3e572283 548 case MODE_REF_DIRECT: {
hyhoon 227:699c3e572283 549 pos.ref = REF_POSITION;
hyhoon 227:699c3e572283 550 vel.ref = REF_VELOCITY;
hyhoon 227:699c3e572283 551 torq.ref = REF_TORQUE;
hyhoon 227:699c3e572283 552 break;
hyhoon 227:699c3e572283 553 }
hyhoon 227:699c3e572283 554 default:
hyhoon 227:699c3e572283 555 break;
hyhoon 227:699c3e572283 556 }
hyhoon 227:699c3e572283 557
Lightvalve 57:f4819de54e7a 558 int UTILITY_MODE = 0;
Lightvalve 57:f4819de54e7a 559 int CONTROL_MODE = 0;
Lightvalve 58:2eade98630e2 560
Lightvalve 57:f4819de54e7a 561 if (CONTROL_UTILITY_MODE >= 20 || CONTROL_UTILITY_MODE == 0) {
Lightvalve 57:f4819de54e7a 562 UTILITY_MODE = CONTROL_UTILITY_MODE;
Lightvalve 57:f4819de54e7a 563 CONTROL_MODE = MODE_NO_ACT;
Lightvalve 57:f4819de54e7a 564 } else {
Lightvalve 57:f4819de54e7a 565 CONTROL_MODE = CONTROL_UTILITY_MODE;
Lightvalve 57:f4819de54e7a 566 UTILITY_MODE = MODE_NO_ACT;
Lightvalve 57:f4819de54e7a 567 }
Lightvalve 56:6f50d9d3bfee 568
Lightvalve 56:6f50d9d3bfee 569
Lightvalve 56:6f50d9d3bfee 570
Lightvalve 57:f4819de54e7a 571 // UTILITY MODE ------------------------------------------------------------
Lightvalve 57:f4819de54e7a 572 switch (UTILITY_MODE) {
Lightvalve 57:f4819de54e7a 573 case MODE_NO_ACT: {
Lightvalve 13:747daba9cf59 574 break;
Lightvalve 13:747daba9cf59 575 }
Lightvalve 14:8e7590227d22 576
Lightvalve 14:8e7590227d22 577 case MODE_TORQUE_SENSOR_NULLING: {
Lightvalve 13:747daba9cf59 578 // DAC Voltage reference set
Lightvalve 14:8e7590227d22 579 if (TMR3_COUNT_TORQUE_NULL < TMR_FREQ_5k * 2) {
Lightvalve 15:bd0d12728506 580 CUR_TORQUE_sum += torq.sen;
Lightvalve 19:23b7c1ad8683 581
Lightvalve 14:8e7590227d22 582 if (TMR3_COUNT_TORQUE_NULL % 10 == 0) {
Lightvalve 30:8d561f16383b 583 CUR_TORQUE_mean = CUR_TORQUE_sum / 10.0f;
Lightvalve 13:747daba9cf59 584 CUR_TORQUE_sum = 0;
Lightvalve 14:8e7590227d22 585
Lightvalve 84:c355d3e52bf1 586 TORQUE_VREF += 0.000003f * (0.0f - CUR_TORQUE_mean);
Lightvalve 19:23b7c1ad8683 587
Lightvalve 30:8d561f16383b 588 if (TORQUE_VREF > 3.3f) TORQUE_VREF = 3.3f;
Lightvalve 59:f308b1656d9c 589 if (TORQUE_VREF < 0.0f) TORQUE_VREF = 0.0f;
Lightvalve 30:8d561f16383b 590 dac_1 = TORQUE_VREF / 3.3f;
Lightvalve 13:747daba9cf59 591 }
Lightvalve 13:747daba9cf59 592 } else {
Lightvalve 58:2eade98630e2 593 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 14:8e7590227d22 594 TMR3_COUNT_TORQUE_NULL = 0;
Lightvalve 13:747daba9cf59 595 CUR_TORQUE_sum = 0;
Lightvalve 13:747daba9cf59 596 CUR_TORQUE_mean = 0;
Lightvalve 19:23b7c1ad8683 597
Lightvalve 170:42c938a40313 598 spi_eeprom_write(RID_TORQUE_SENSOR_VREF, (int16_t) (TORQUE_VREF * 1000.0f));
Lightvalve 19:23b7c1ad8683 599
Lightvalve 30:8d561f16383b 600 dac_1 = TORQUE_VREF / 3.3f;
Lightvalve 14:8e7590227d22 601
Lightvalve 13:747daba9cf59 602 }
Lightvalve 14:8e7590227d22 603 TMR3_COUNT_TORQUE_NULL++;
Lightvalve 13:747daba9cf59 604 break;
Lightvalve 19:23b7c1ad8683 605 }
Lightvalve 14:8e7590227d22 606
Lightvalve 14:8e7590227d22 607 case MODE_FIND_HOME: {
Lightvalve 29:69f3f5445d6d 608 if (FINDHOME_STAGE == FINDHOME_INIT) {
hyhoon 227:699c3e572283 609 REFERENCE_MODE=MODE_REF_NO_ACT;
Lightvalve 14:8e7590227d22 610 cnt_findhome = 0;
Lightvalve 14:8e7590227d22 611 cnt_vel_findhome = 0;
Lightvalve 59:f308b1656d9c 612 pos.ref = pos.sen;
Lightvalve 59:f308b1656d9c 613 vel.ref = 0.0f;
Lightvalve 29:69f3f5445d6d 614 FINDHOME_STAGE = FINDHOME_GOTOLIMIT;
Lightvalve 29:69f3f5445d6d 615 } else if (FINDHOME_STAGE == FINDHOME_GOTOLIMIT) {
Lightvalve 67:c2812cf26c38 616 int cnt_check_enc = (TMR_FREQ_5k/20);
Lightvalve 29:69f3f5445d6d 617 if(cnt_findhome%cnt_check_enc == 0) {
Lightvalve 29:69f3f5445d6d 618 FINDHOME_POSITION = pos.sen;
Lightvalve 29:69f3f5445d6d 619 FINDHOME_VELOCITY = FINDHOME_POSITION - FINDHOME_POSITION_OLD;
Lightvalve 29:69f3f5445d6d 620 FINDHOME_POSITION_OLD = FINDHOME_POSITION;
Lightvalve 29:69f3f5445d6d 621 }
Lightvalve 29:69f3f5445d6d 622 cnt_findhome++;
Lightvalve 14:8e7590227d22 623
Lightvalve 29:69f3f5445d6d 624 if (abs(FINDHOME_VELOCITY) <= 1) {
Lightvalve 29:69f3f5445d6d 625 cnt_vel_findhome = cnt_vel_findhome + 1;
Lightvalve 29:69f3f5445d6d 626 } else {
Lightvalve 29:69f3f5445d6d 627 cnt_vel_findhome = 0;
Lightvalve 29:69f3f5445d6d 628 }
Lightvalve 19:23b7c1ad8683 629
Lightvalve 57:f4819de54e7a 630 if ((cnt_vel_findhome < 3*TMR_FREQ_5k) && cnt_findhome < 10*TMR_FREQ_5k) { // wait for 3sec
hyhoon 226:82a3ca333004 631 if (HOMEPOS_OFFSET > 0) {
hyhoon 226:82a3ca333004 632 pos.ref = pos.ref + 12.0f;
hyhoon 226:82a3ca333004 633 } else {
hyhoon 226:82a3ca333004 634 pos.ref = pos.ref - 12.0f;
hyhoon 226:82a3ca333004 635 }
hyhoon 226:82a3ca333004 636
Lightvalve 61:bc8c8270f0ab 637
Lightvalve 59:f308b1656d9c 638 CONTROL_MODE = MODE_JOINT_CONTROL;
Lightvalve 59:f308b1656d9c 639 alpha_trans = 0.0f;
Lightvalve 61:bc8c8270f0ab 640
Lightvalve 29:69f3f5445d6d 641 } else {
hyhoon 226:82a3ca333004 642 ENC_SET((int32_t)((int32_t)HOMEPOS_OFFSET*10));
hyhoon 226:82a3ca333004 643 INIT_REF_POS = (int32_t)((int32_t)HOMEPOS_OFFSET*10);
Lightvalve 29:69f3f5445d6d 644 REF_POSITION = 0;
Lightvalve 29:69f3f5445d6d 645 REF_VELOCITY = 0;
Lightvalve 29:69f3f5445d6d 646 FINDHOME_POSITION = 0;
Lightvalve 29:69f3f5445d6d 647 FINDHOME_POSITION_OLD = 0;
Lightvalve 29:69f3f5445d6d 648 FINDHOME_VELOCITY = 0;
Lightvalve 29:69f3f5445d6d 649 cnt_findhome = 0;
Lightvalve 29:69f3f5445d6d 650 cnt_vel_findhome = 0;
Lightvalve 29:69f3f5445d6d 651 FINDHOME_STAGE = FINDHOME_ZEROPOSE;
Lightvalve 67:c2812cf26c38 652
Lightvalve 67:c2812cf26c38 653
Lightvalve 67:c2812cf26c38 654 cnt_findhome = 0;
Lightvalve 67:c2812cf26c38 655 pos.ref = 0.0f;
Lightvalve 67:c2812cf26c38 656 vel.ref = 0.0f;
Lightvalve 67:c2812cf26c38 657 pos.ref_home_pos = 0.0f;
Lightvalve 67:c2812cf26c38 658 vel.ref_home_pos = 0.0f;
Lightvalve 29:69f3f5445d6d 659 }
Lightvalve 29:69f3f5445d6d 660 } else if (FINDHOME_STAGE == FINDHOME_ZEROPOSE) {
Lightvalve 29:69f3f5445d6d 661 int T_move = 2*TMR_FREQ_5k;
Lightvalve 59:f308b1656d9c 662 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 663 vel.ref = 0.0f;
Lightvalve 67:c2812cf26c38 664 alpha_trans = 0.0f;
Lightvalve 61:bc8c8270f0ab 665
Lightvalve 67:c2812cf26c38 666 pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm]
Lightvalve 67:c2812cf26c38 667 vel.err = (0.0f - vel.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm/s]
Lightvalve 67:c2812cf26c38 668 pos.err_sum += pos.err/(float) TMR_FREQ_5k; //[mm]
hyhoon 226:82a3ca333004 669 CONTROL_MODE = MODE_JOINT_CONTROL;
Lightvalve 67:c2812cf26c38 670
Lightvalve 29:69f3f5445d6d 671 cnt_findhome++;
Lightvalve 29:69f3f5445d6d 672 if (cnt_findhome >= T_move) {
Lightvalve 29:69f3f5445d6d 673 cnt_findhome = 0;
Lightvalve 30:8d561f16383b 674 pos.ref = 0.0f;
Lightvalve 30:8d561f16383b 675 vel.ref = 0.0f;
Lightvalve 30:8d561f16383b 676 pos.ref_home_pos = 0.0f;
Lightvalve 30:8d561f16383b 677 vel.ref_home_pos = 0.0f;
Lightvalve 29:69f3f5445d6d 678 FINDHOME_STAGE = FINDHOME_INIT;
Lightvalve 57:f4819de54e7a 679 CONTROL_UTILITY_MODE = MODE_JOINT_CONTROL;
hyhoon 227:699c3e572283 680 REFERENCE_MODE=MODE_REF_DIRECT;
Lightvalve 29:69f3f5445d6d 681 }
Lightvalve 13:747daba9cf59 682 }
Lightvalve 19:23b7c1ad8683 683
Lightvalve 13:747daba9cf59 684 break;
Lightvalve 13:747daba9cf59 685 }
Lightvalve 14:8e7590227d22 686
Lightvalve 14:8e7590227d22 687 case MODE_PRESSURE_SENSOR_NULLING: {
Lightvalve 13:747daba9cf59 688 // DAC Voltage reference set
Lightvalve 14:8e7590227d22 689 if (TMR3_COUNT_PRES_NULL < TMR_FREQ_5k * 2) {
Lightvalve 14:8e7590227d22 690 CUR_PRES_A_sum += pres_A.sen;
Lightvalve 14:8e7590227d22 691 CUR_PRES_B_sum += pres_B.sen;
Lightvalve 14:8e7590227d22 692
Lightvalve 14:8e7590227d22 693 if (TMR3_COUNT_PRES_NULL % 10 == 0) {
Lightvalve 30:8d561f16383b 694 CUR_PRES_A_mean = CUR_PRES_A_sum / 10.0f;
Lightvalve 30:8d561f16383b 695 CUR_PRES_B_mean = CUR_PRES_B_sum / 10.0f;
Lightvalve 13:747daba9cf59 696 CUR_PRES_A_sum = 0;
Lightvalve 13:747daba9cf59 697 CUR_PRES_B_sum = 0;
Lightvalve 14:8e7590227d22 698
Lightvalve 38:118df027d851 699 float VREF_NullingGain = 0.0003f;
Lightvalve 58:2eade98630e2 700 PRES_A_VREF = PRES_A_VREF + VREF_NullingGain * CUR_PRES_A_mean;
Lightvalve 58:2eade98630e2 701 PRES_B_VREF = PRES_B_VREF + VREF_NullingGain * CUR_PRES_B_mean;
Lightvalve 14:8e7590227d22 702
Lightvalve 30:8d561f16383b 703 if (PRES_A_VREF > 3.3f) PRES_A_VREF = 3.3f;
Lightvalve 30:8d561f16383b 704 if (PRES_A_VREF < 0.0f) PRES_A_VREF = 0.0f;
Lightvalve 30:8d561f16383b 705 if (PRES_B_VREF > 3.3f) PRES_B_VREF = 3.3f;
Lightvalve 30:8d561f16383b 706 if (PRES_B_VREF < 0.0f) PRES_B_VREF = 0.0f;
Lightvalve 19:23b7c1ad8683 707
hyhoon 226:82a3ca333004 708 if (DIR_VALVE_ENC > 0) {
hyhoon 226:82a3ca333004 709 dac_1 = PRES_A_VREF / 3.3f;
hyhoon 226:82a3ca333004 710 dac_2 = PRES_B_VREF / 3.3f;
hyhoon 226:82a3ca333004 711 } else {
hyhoon 226:82a3ca333004 712 dac_1 = PRES_B_VREF / 3.3f;
hyhoon 226:82a3ca333004 713 dac_2 = PRES_A_VREF / 3.3f;
hyhoon 226:82a3ca333004 714 }
Lightvalve 13:747daba9cf59 715 }
Lightvalve 13:747daba9cf59 716 } else {
Lightvalve 57:f4819de54e7a 717 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 14:8e7590227d22 718 TMR3_COUNT_PRES_NULL = 0;
Lightvalve 13:747daba9cf59 719 CUR_PRES_A_sum = 0;
Lightvalve 13:747daba9cf59 720 CUR_PRES_B_sum = 0;
Lightvalve 13:747daba9cf59 721 CUR_PRES_A_mean = 0;
Lightvalve 13:747daba9cf59 722 CUR_PRES_B_mean = 0;
Lightvalve 14:8e7590227d22 723
Lightvalve 170:42c938a40313 724 spi_eeprom_write(RID_PRES_A_SENSOR_VREF, (int16_t) (PRES_A_VREF * 1000.0f));
Lightvalve 170:42c938a40313 725 spi_eeprom_write(RID_PRES_B_SENSOR_VREF, (int16_t) (PRES_B_VREF * 1000.0f));
Lightvalve 19:23b7c1ad8683 726
hyhoon 226:82a3ca333004 727 if (DIR_VALVE_ENC > 0) {
hyhoon 226:82a3ca333004 728 dac_1 = PRES_A_VREF / 3.3f;
hyhoon 226:82a3ca333004 729 dac_2 = PRES_B_VREF / 3.3f;
hyhoon 226:82a3ca333004 730 } else {
hyhoon 226:82a3ca333004 731 dac_1 = PRES_B_VREF / 3.3f;
hyhoon 226:82a3ca333004 732 dac_2 = PRES_A_VREF / 3.3f;
hyhoon 226:82a3ca333004 733 }
Lightvalve 13:747daba9cf59 734 }
Lightvalve 14:8e7590227d22 735 TMR3_COUNT_PRES_NULL++;
Lightvalve 13:747daba9cf59 736 break;
Lightvalve 13:747daba9cf59 737 }
Lightvalve 14:8e7590227d22 738
hyhoon 227:699c3e572283 739 case MODE_DDV_DEADZONE_AND_CENTER: {
hyhoon 227:699c3e572283 740 if (FINDDZ_STAGE == FINDDZ_INIT) {
hyhoon 227:699c3e572283 741 cnt_finddz = 0;
hyhoon 227:699c3e572283 742 cnt_vel_finddz = 0;
hyhoon 227:699c3e572283 743 temp_pos_ref = pos.sen;
hyhoon 227:699c3e572283 744 temp_pos_ref_offset = 0.0f;
hyhoon 227:699c3e572283 745 vel.ref = 0.0f;
hyhoon 227:699c3e572283 746 FINDDZ_STAGE = FINDDZ_START1;
hyhoon 227:699c3e572283 747 flag_finddz = 1;
hyhoon 227:699c3e572283 748 FINDDZ_POSITION = pos.sen;
hyhoon 227:699c3e572283 749 FINDDZ_POSITION_OLD = FINDDZ_POSITION;
hyhoon 227:699c3e572283 750
hyhoon 227:699c3e572283 751 temp_P_GAIN = P_GAIN_JOINT_POSITION;
hyhoon 227:699c3e572283 752 temp_I_GAIN = I_GAIN_JOINT_POSITION;
hyhoon 227:699c3e572283 753 temp_VELOCITY_COMP_GAIN = VELOCITY_COMP_GAIN;
Lightvalve 19:23b7c1ad8683 754
hyhoon 227:699c3e572283 755 P_GAIN_JOINT_POSITION = 1.0f;
hyhoon 227:699c3e572283 756 I_GAIN_JOINT_POSITION = 0.0f;
hyhoon 227:699c3e572283 757 VELOCITY_COMP_GAIN = 0;
hyhoon 227:699c3e572283 758 FLAG_VALVE_DEADZONE = 0;
hyhoon 227:699c3e572283 759 alpha_trans = 0.0f;
hyhoon 227:699c3e572283 760 REFERENCE_MODE=MODE_REF_NO_ACT;
Lightvalve 14:8e7590227d22 761
hyhoon 227:699c3e572283 762 } else if (FINDDZ_STAGE == FINDDZ_START1) {
hyhoon 227:699c3e572283 763 cnt_finddz++;
hyhoon 227:699c3e572283 764 CONTROL_MODE = MODE_JOINT_CONTROL;
hyhoon 227:699c3e572283 765 FINDDZ_POSITION = pos.sen;
hyhoon 227:699c3e572283 766 FINDDZ_VELOCITY = FINDDZ_POSITION - FINDDZ_POSITION_OLD;
hyhoon 227:699c3e572283 767 FINDDZ_POSITION_OLD = FINDDZ_POSITION;
Lightvalve 14:8e7590227d22 768
hyhoon 227:699c3e572283 769 if (temp_P_GAIN > 0.0f) temp_pos_ref_offset = temp_pos_ref_offset - (float)FINDDZ_VELOCITY*temp_P_GAIN;
hyhoon 227:699c3e572283 770 else temp_pos_ref_offset = temp_pos_ref_offset - (float)FINDDZ_VELOCITY*20.0f;
hyhoon 227:699c3e572283 771
hyhoon 227:699c3e572283 772 pos.ref = temp_pos_ref + temp_pos_ref_offset;
hyhoon 227:699c3e572283 773
hyhoon 227:699c3e572283 774 if (cnt_finddz > 10000) FINDDZ_STAGE = FINDDZ_START2;
Lightvalve 14:8e7590227d22 775
Lightvalve 14:8e7590227d22 776
hyhoon 227:699c3e572283 777 } else if (FINDDZ_STAGE == FINDDZ_START2) {
hyhoon 227:699c3e572283 778 CONTROL_MODE = MODE_JOINT_CONTROL;
hyhoon 227:699c3e572283 779 FINDDZ_POSITION = pos.sen;
hyhoon 227:699c3e572283 780 FINDDZ_VELOCITY = FINDDZ_POSITION - FINDDZ_POSITION_OLD;
hyhoon 227:699c3e572283 781 FINDDZ_POSITION_OLD = FINDDZ_POSITION;
Lightvalve 14:8e7590227d22 782
hyhoon 227:699c3e572283 783 if (flag_finddz > 0) {
hyhoon 227:699c3e572283 784 temp_pos_ref = temp_pos_ref + 1.0f;
hyhoon 227:699c3e572283 785 if (FINDDZ_VELOCITY > 0) {
hyhoon 227:699c3e572283 786 cnt_vel_finddz = cnt_vel_finddz + 1;
hyhoon 227:699c3e572283 787 }
hyhoon 227:699c3e572283 788 if (cnt_vel_finddz > 5) {
hyhoon 227:699c3e572283 789 VALVE_DEADZONE_PLUS = (int)(I_REF_fil/mA_PER_pulse);
hyhoon 227:699c3e572283 790 // temp_VALVE_DEADZONE_PLUS = temp_VALVE_DEADZONE_PLUS + I_REF_fil/mA_PER_pulse;
hyhoon 227:699c3e572283 791 cnt_vel_finddz = 0;
hyhoon 227:699c3e572283 792 flag_finddz = - flag_finddz - 1;
Lightvalve 13:747daba9cf59 793 }
Lightvalve 61:bc8c8270f0ab 794
hyhoon 227:699c3e572283 795 } else if (flag_finddz < 0) {
hyhoon 227:699c3e572283 796 temp_pos_ref = temp_pos_ref - 1.0f;
hyhoon 227:699c3e572283 797 if (FINDDZ_VELOCITY < 0) {
hyhoon 227:699c3e572283 798 cnt_vel_finddz = cnt_vel_finddz + 1;
Lightvalve 13:747daba9cf59 799 }
hyhoon 227:699c3e572283 800 if (cnt_vel_finddz > 5) {
hyhoon 227:699c3e572283 801 VALVE_DEADZONE_MINUS = (int)(I_REF_fil/mA_PER_pulse);
hyhoon 227:699c3e572283 802 // temp_VALVE_DEADZONE_MINUS = temp_VALVE_DEADZONE_MINUS + I_REF_fil/mA_PER_pulse;
hyhoon 227:699c3e572283 803 cnt_vel_finddz = 0;
hyhoon 227:699c3e572283 804 flag_finddz = - flag_finddz + 1;
Lightvalve 13:747daba9cf59 805 }
Lightvalve 13:747daba9cf59 806 }
hyhoon 227:699c3e572283 807 if (abs(flag_finddz) >= 6) {
hyhoon 227:699c3e572283 808 FINDDZ_STAGE = FINDDZ_STOP;
hyhoon 227:699c3e572283 809 // VALVE_DEADZONE_PLUS = (int)(temp_VALVE_DEADZONE_PLUS / (double)flag_finddz * 2.0f);
hyhoon 227:699c3e572283 810 // VALVE_DEADZONE_MINUS = (int)(temp_VALVE_DEADZONE_MINUS / (double)flag_finddz * 2.0f);
hyhoon 227:699c3e572283 811 spi_eeprom_write(RID_VALVE_DEADZONE_PLUS, VALVE_DEADZONE_PLUS);
hyhoon 227:699c3e572283 812 spi_eeprom_write(RID_VALVE_DEADZONE_MINUS, VALVE_DEADZONE_MINUS);
Lightvalve 13:747daba9cf59 813 }
hyhoon 227:699c3e572283 814 pos.ref = temp_pos_ref + temp_pos_ref_offset;
Lightvalve 14:8e7590227d22 815
hyhoon 227:699c3e572283 816 } else if (FINDDZ_STAGE == FINDDZ_STOP) {
hyhoon 227:699c3e572283 817 FINDDZ_STAGE = FINDDZ_INIT;
hyhoon 227:699c3e572283 818 CONTROL_UTILITY_MODE = MODE_JOINT_CONTROL;
hyhoon 227:699c3e572283 819 P_GAIN_JOINT_POSITION = temp_P_GAIN;
hyhoon 227:699c3e572283 820 I_GAIN_JOINT_POSITION = temp_I_GAIN;
hyhoon 227:699c3e572283 821 VELOCITY_COMP_GAIN = temp_VELOCITY_COMP_GAIN;
hyhoon 227:699c3e572283 822 flag_finddz = 0;
hyhoon 227:699c3e572283 823 FLAG_VALVE_DEADZONE = 1;
hyhoon 227:699c3e572283 824 REFERENCE_MODE=MODE_REF_DIRECT;
Lightvalve 13:747daba9cf59 825 }
Lightvalve 13:747daba9cf59 826 break;
Lightvalve 13:747daba9cf59 827 }
Lightvalve 58:2eade98630e2 828
Lightvalve 57:f4819de54e7a 829 case MODE_SYSTEM_ID: {
Lightvalve 169:645207e160ca 830 freq_sysid_Iref = (double) cnt_sysid * DT_TMR3 * 3.0f;
Lightvalve 57:f4819de54e7a 831 valve_pos.ref = 2500.0f * sin(2.0f * 3.14159f * freq_sysid_Iref * (double) cnt_sysid * DT_TMR3);
Lightvalve 57:f4819de54e7a 832 CONTROL_MODE = MODE_VALVE_OPEN_LOOP;
Lightvalve 57:f4819de54e7a 833 cnt_sysid++;
Lightvalve 57:f4819de54e7a 834 if (freq_sysid_Iref >= 300) {
Lightvalve 57:f4819de54e7a 835 cnt_sysid = 0;
Lightvalve 57:f4819de54e7a 836 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 57:f4819de54e7a 837 }
Lightvalve 57:f4819de54e7a 838 break;
Lightvalve 57:f4819de54e7a 839 }
Lightvalve 58:2eade98630e2 840
Lightvalve 169:645207e160ca 841 case MODE_FREQ_TEST: {
Lightvalve 169:645207e160ca 842 float valve_pos_ref = 2500.0f * sin(2.0f * 3.141592f * freq_test_valve_ref * (float) cnt_freq_test * DT_TMR3);
Lightvalve 169:645207e160ca 843 if(valve_pos_ref >= 0) {
Lightvalve 169:645207e160ca 844 valve_pos.ref = (double)VALVE_CENTER + (double)valve_pos_ref * ((double)VALVE_MAX_POS-(double)VALVE_CENTER)/10000.0f;
Lightvalve 169:645207e160ca 845 } else {
Lightvalve 169:645207e160ca 846 valve_pos.ref = (double)VALVE_CENTER - (double)valve_pos_ref * ((double)VALVE_MIN_POS-(double)VALVE_CENTER)/10000.0f;
Lightvalve 169:645207e160ca 847 }
Lightvalve 169:645207e160ca 848 ref_array[cnt_freq_test] = valve_pos_ref;
Lightvalve 169:645207e160ca 849 if(value>=(float) VALVE_CENTER) {
Lightvalve 169:645207e160ca 850 pos_array[cnt_freq_test] = 10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER);
Lightvalve 169:645207e160ca 851 } else {
Lightvalve 169:645207e160ca 852 pos_array[cnt_freq_test] = -10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER);
Lightvalve 169:645207e160ca 853 }
Lightvalve 58:2eade98630e2 854
Lightvalve 169:645207e160ca 855 CONTROL_MODE = MODE_VALVE_POSITION_CONTROL;
Lightvalve 169:645207e160ca 856 cnt_freq_test++;
Lightvalve 169:645207e160ca 857 if (freq_test_valve_ref * (float) cnt_freq_test * DT_TMR3 > 2) {
Lightvalve 169:645207e160ca 858 buffer_data_size = cnt_freq_test;
Lightvalve 169:645207e160ca 859 cnt_freq_test = 0;
Lightvalve 169:645207e160ca 860 cnt_send_buffer = 0;
Lightvalve 169:645207e160ca 861 freq_test_valve_ref = freq_test_valve_ref * 1.05f;
Lightvalve 169:645207e160ca 862 if (freq_test_valve_ref >= 400) {
Lightvalve 169:645207e160ca 863 CONTROL_UTILITY_MODE = MODE_NO_ACT;
Lightvalve 169:645207e160ca 864 CONTROL_MODE = MODE_NO_ACT;
Lightvalve 169:645207e160ca 865 CAN_TX_PWM((int16_t) (1)); //1300
Lightvalve 169:645207e160ca 866 }
Lightvalve 169:645207e160ca 867 CONTROL_MODE = MODE_NO_ACT;
Lightvalve 169:645207e160ca 868 CONTROL_UTILITY_MODE = MODE_SEND_OVER;
Lightvalve 170:42c938a40313 869
Lightvalve 169:645207e160ca 870 }
Lightvalve 169:645207e160ca 871 break;
Lightvalve 169:645207e160ca 872 }
Lightvalve 170:42c938a40313 873
Lightvalve 169:645207e160ca 874 case MODE_STEP_TEST: {
Lightvalve 169:645207e160ca 875 float valve_pos_ref = 0.0f;
Lightvalve 170:42c938a40313 876 if (cnt_step_test < (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 169:645207e160ca 877 valve_pos_ref = 0.0f;
Lightvalve 170:42c938a40313 878 } else {
Lightvalve 169:645207e160ca 879 valve_pos_ref = 10000.0f;
Lightvalve 169:645207e160ca 880 }
Lightvalve 169:645207e160ca 881 if(valve_pos_ref >= 0) {
Lightvalve 169:645207e160ca 882 valve_pos.ref = (double)VALVE_CENTER + (double)valve_pos_ref * ((double)VALVE_MAX_POS-(double)VALVE_CENTER)/10000.0f;
Lightvalve 169:645207e160ca 883 } else {
Lightvalve 169:645207e160ca 884 valve_pos.ref = (double)VALVE_CENTER - (double)valve_pos_ref * ((double)VALVE_MIN_POS-(double)VALVE_CENTER)/10000.0f;
Lightvalve 169:645207e160ca 885 }
Lightvalve 169:645207e160ca 886 ref_array[cnt_step_test] = valve_pos_ref;
Lightvalve 169:645207e160ca 887 if(value>=(float) VALVE_CENTER) {
Lightvalve 169:645207e160ca 888 pos_array[cnt_step_test] = 10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER);
Lightvalve 169:645207e160ca 889 } else {
Lightvalve 169:645207e160ca 890 pos_array[cnt_step_test] = -10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER);
Lightvalve 169:645207e160ca 891 }
Lightvalve 169:645207e160ca 892
Lightvalve 169:645207e160ca 893 CONTROL_MODE = MODE_VALVE_POSITION_CONTROL;
Lightvalve 169:645207e160ca 894 cnt_step_test++;
Lightvalve 170:42c938a40313 895 if (cnt_step_test > (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 169:645207e160ca 896 buffer_data_size = cnt_step_test;
Lightvalve 169:645207e160ca 897 cnt_step_test = 0;
Lightvalve 169:645207e160ca 898 cnt_send_buffer = 0;
Lightvalve 169:645207e160ca 899 CONTROL_UTILITY_MODE = MODE_SEND_OVER;
Lightvalve 169:645207e160ca 900 CONTROL_MODE = MODE_NO_ACT;
Lightvalve 169:645207e160ca 901 }
Lightvalve 170:42c938a40313 902
Lightvalve 169:645207e160ca 903 break;
Lightvalve 169:645207e160ca 904 }
Lightvalve 57:f4819de54e7a 905
Lightvalve 57:f4819de54e7a 906 default:
Lightvalve 57:f4819de54e7a 907 break;
Lightvalve 57:f4819de54e7a 908 }
Lightvalve 57:f4819de54e7a 909
Lightvalve 224:985dba42f261 910
Lightvalve 224:985dba42f261 911
Lightvalve 57:f4819de54e7a 912 // CONTROL MODE ------------------------------------------------------------
Lightvalve 57:f4819de54e7a 913 switch (CONTROL_MODE) {
Lightvalve 57:f4819de54e7a 914 case MODE_NO_ACT: {
Lightvalve 57:f4819de54e7a 915 V_out = 0.0f;
Lightvalve 57:f4819de54e7a 916 break;
Lightvalve 57:f4819de54e7a 917 }
Lightvalve 57:f4819de54e7a 918
Lightvalve 57:f4819de54e7a 919 case MODE_VALVE_POSITION_CONTROL: {
Lightvalve 67:c2812cf26c38 920 if (OPERATING_MODE == 5) { //SW Valve
Lightvalve 57:f4819de54e7a 921 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 59:f308b1656d9c 922 V_out = Vout.ref;
Lightvalve 67:c2812cf26c38 923 } else if (CURRENT_CONTROL_MODE == 0) { //PWM
Lightvalve 67:c2812cf26c38 924 V_out = valve_pos.ref;
Lightvalve 169:645207e160ca 925 } else {
hyhoon 227:699c3e572283 926 I_REF = valve_pos.ref * 0.001f;
Lightvalve 57:f4819de54e7a 927 }
Lightvalve 57:f4819de54e7a 928 break;
Lightvalve 57:f4819de54e7a 929 }
Lightvalve 57:f4819de54e7a 930
Lightvalve 57:f4819de54e7a 931 case MODE_JOINT_CONTROL: {
Lightvalve 169:645207e160ca 932
Lightvalve 57:f4819de54e7a 933 double torq_ref = 0.0f;
Lightvalve 57:f4819de54e7a 934 pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm]
Lightvalve 67:c2812cf26c38 935 vel.err = (0.0f - vel.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm/s]
Lightvalve 57:f4819de54e7a 936 pos.err_sum += pos.err/(float) TMR_FREQ_5k; //[mm]
Lightvalve 169:645207e160ca 937
Lightvalve 67:c2812cf26c38 938 //K & D Low Pass Filter
Lightvalve 67:c2812cf26c38 939 float alpha_K_D = 1.0f/(1.0f + 5000.0f/(2.0f*3.14f*30.0f)); // f_cutoff : 30Hz
Lightvalve 67:c2812cf26c38 940 K_LPF = K_LPF*(1.0f-alpha_K_D)+K_SPRING*(alpha_K_D);
Lightvalve 67:c2812cf26c38 941 D_LPF = D_LPF*(1.0f-alpha_K_D)+D_DAMPER*(alpha_K_D);
Lightvalve 169:645207e160ca 942
Lightvalve 224:985dba42f261 943 torq_ref = torq.ref + K_LPF * pos.err - D_LPF * vel.sen / ENC_PULSE_PER_POSITION; //[N]
Lightvalve 57:f4819de54e7a 944
Lightvalve 57:f4819de54e7a 945 // torque feedback
Lightvalve 67:c2812cf26c38 946 torq.err = torq_ref - torq.sen; //[N]
Lightvalve 57:f4819de54e7a 947 torq.err_sum += torq.err/(float) TMR_FREQ_5k; //[N]
Lightvalve 57:f4819de54e7a 948
Lightvalve 57:f4819de54e7a 949 if (((OPERATING_MODE&0b110)>>1) == 0 || ((OPERATING_MODE&0b110)>>1) == 1) {
Lightvalve 58:2eade98630e2 950
Lightvalve 57:f4819de54e7a 951 double I_REF_POS = 0.0f;
Lightvalve 57:f4819de54e7a 952 double I_REF_FORCE_FB = 0.0f; // I_REF by Force Feedback
Lightvalve 57:f4819de54e7a 953 double I_REF_VC = 0.0f; // I_REF for velocity compensation
Lightvalve 57:f4819de54e7a 954
Lightvalve 57:f4819de54e7a 955 double temp_vel_pos = 0.0f;
Lightvalve 57:f4819de54e7a 956 double temp_vel_torq = 0.0f;
Lightvalve 57:f4819de54e7a 957 double wn_Pos = 2.0f * PI * 5.0f; // f_cut : 5Hz Position Control
Lightvalve 58:2eade98630e2 958
Lightvalve 69:3995ffeaa786 959 if ((OPERATING_MODE & 0x01) == 0) { // Rotary Mode
Lightvalve 57:f4819de54e7a 960 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 961 // L when P-gain = 100, f_cut = 10Hz L feedforward velocity
Lightvalve 69:3995ffeaa786 962 } else if ((OPERATING_MODE & 0x01) == 1) {
Lightvalve 57:f4819de54e7a 963 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 964 // L when P-gain = 100, f_cut = 10Hz L feedforward velocity
Lightvalve 57:f4819de54e7a 965 }
Lightvalve 57:f4819de54e7a 966 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 967 else I_REF_POS = temp_vel_pos * ((double) PISTON_AREA_B * 0.00006f / (K_v * sqrt(2.0f / (alpha3 + 1.0f))));
Lightvalve 57:f4819de54e7a 968
Lightvalve 57:f4819de54e7a 969 // velocity compensation for torque control
Lightvalve 69:3995ffeaa786 970 if ((OPERATING_MODE & 0x01) == 0) { // Rotary Mode
Lightvalve 57:f4819de54e7a 971 I_REF_FORCE_FB = 0.001f * ((double) P_GAIN_JOINT_TORQUE * torq.err + (double) I_GAIN_JOINT_TORQUE * torq.err_sum);
Lightvalve 57:f4819de54e7a 972 // 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 973 temp_vel_torq = (0.01f * (double) VELOCITY_COMP_GAIN * vel.ref / (double) ENC_PULSE_PER_POSITION) * PI / 180.0f; // rad/s
Lightvalve 57:f4819de54e7a 974 // L feedforward velocity
Lightvalve 69:3995ffeaa786 975 } else if ((OPERATING_MODE & 0x01) == 1) {
Lightvalve 57:f4819de54e7a 976 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 977 // temp_vel_torq = (0.01 * (double) VELOCITY_COMP_GAIN * (double) CUR_VELOCITY / (double) ENC_PULSE_PER_POSITION); // mm/s
Lightvalve 57:f4819de54e7a 978 temp_vel_torq = (0.01f * (double) VELOCITY_COMP_GAIN * vel.ref / (double) ENC_PULSE_PER_POSITION); // mm/s
Lightvalve 57:f4819de54e7a 979 // L feedforward velocity
Lightvalve 57:f4819de54e7a 980 }
Lightvalve 57:f4819de54e7a 981 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 982 else I_REF_VC = temp_vel_torq * ((double) PISTON_AREA_B * 0.00006f / (K_v * sqrt(2.0f / (alpha3 + 1.0f))));
Lightvalve 57:f4819de54e7a 983 // L velocity(rad/s or mm/s) >> I_ref(mA)
Lightvalve 57:f4819de54e7a 984 // Ref_Joint_FT_dot = (Ref_Joint_FT_Nm - Ref_Joint_FT_Nm_old) / TMR_DT_5k;
Lightvalve 57:f4819de54e7a 985 // Ref_Joint_FT_Nm_old = Ref_Joint_FT_Nm;
Lightvalve 57:f4819de54e7a 986
Lightvalve 57:f4819de54e7a 987 I_REF = (1.0f - alpha_trans) * I_REF_POS + alpha_trans * (I_REF_VC + I_REF_FORCE_FB);
Lightvalve 169:645207e160ca 988
Lightvalve 67:c2812cf26c38 989 // Anti-windup for FT
Lightvalve 67:c2812cf26c38 990 if (I_GAIN_JOINT_TORQUE != 0) {
Lightvalve 67:c2812cf26c38 991 double I_MAX = 10.0f; // Maximum Current : 10mV
Lightvalve 67:c2812cf26c38 992 double Ka = 2.0f / ((double) I_GAIN_JOINT_TORQUE * 0.001f);
Lightvalve 67:c2812cf26c38 993 if (I_REF > I_MAX) {
Lightvalve 67:c2812cf26c38 994 double I_rem = I_REF - I_MAX;
Lightvalve 67:c2812cf26c38 995 I_rem = Ka*I_rem;
Lightvalve 67:c2812cf26c38 996 I_REF = I_MAX;
Lightvalve 67:c2812cf26c38 997 torq.err_sum = torq.err_sum - I_rem /(float) TMR_FREQ_5k;
Lightvalve 67:c2812cf26c38 998 } else if (I_REF < -I_MAX) {
Lightvalve 67:c2812cf26c38 999 double I_rem = I_REF - (-I_MAX);
Lightvalve 67:c2812cf26c38 1000 I_rem = Ka*I_rem;
Lightvalve 67:c2812cf26c38 1001 I_REF = -I_MAX;
Lightvalve 67:c2812cf26c38 1002 torq.err_sum = torq.err_sum - I_rem /(float) TMR_FREQ_5k;
Lightvalve 67:c2812cf26c38 1003 }
Lightvalve 67:c2812cf26c38 1004 }
Lightvalve 57:f4819de54e7a 1005
Lightvalve 57:f4819de54e7a 1006 } else {
Lightvalve 57:f4819de54e7a 1007 float VALVE_POS_RAW_FORCE_FB = 0.0f;
Lightvalve 72:3436ce769b1e 1008 float VALVE_POS_RAW_FORCE_FF = 0.0f;
Lightvalve 72:3436ce769b1e 1009 float VALVE_POS_RAW = 0.0f;
Lightvalve 169:645207e160ca 1010
Lightvalve 209:ebc69d6ee6f1 1011 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 1012
Lightvalve 72:3436ce769b1e 1013 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 1014
Lightvalve 72:3436ce769b1e 1015 VALVE_POS_RAW = VALVE_POS_RAW_FORCE_FB + VALVE_POS_RAW_FORCE_FF;
Lightvalve 169:645207e160ca 1016
Lightvalve 169:645207e160ca 1017
Lightvalve 72:3436ce769b1e 1018 if (VALVE_POS_RAW >= 0) {
Lightvalve 72:3436ce769b1e 1019 valve_pos.ref = VALVE_POS_RAW + VALVE_DEADZONE_PLUS;
Lightvalve 57:f4819de54e7a 1020 } else {
Lightvalve 72:3436ce769b1e 1021 valve_pos.ref = VALVE_POS_RAW + VALVE_DEADZONE_MINUS;
Lightvalve 57:f4819de54e7a 1022 }
Lightvalve 57:f4819de54e7a 1023
Lightvalve 57:f4819de54e7a 1024 if(I_GAIN_JOINT_TORQUE != 0) {
Lightvalve 139:15621998925b 1025 double Ka = 2.0f / (double) I_GAIN_JOINT_TORQUE * 100.0f;
Lightvalve 57:f4819de54e7a 1026 if(valve_pos.ref>VALVE_MAX_POS) {
Lightvalve 57:f4819de54e7a 1027 double valve_pos_rem = valve_pos.ref - VALVE_MAX_POS;
Lightvalve 57:f4819de54e7a 1028 valve_pos_rem = valve_pos_rem * Ka;
Lightvalve 57:f4819de54e7a 1029 valve_pos.ref = VALVE_MAX_POS;
Lightvalve 57:f4819de54e7a 1030 torq.err_sum = torq.err_sum - valve_pos_rem/(float) TMR_FREQ_5k;
Lightvalve 57:f4819de54e7a 1031 } else if(valve_pos.ref < VALVE_MIN_POS) {
Lightvalve 57:f4819de54e7a 1032 double valve_pos_rem = valve_pos.ref - VALVE_MIN_POS;
Lightvalve 57:f4819de54e7a 1033 valve_pos_rem = valve_pos_rem * Ka;
Lightvalve 57:f4819de54e7a 1034 valve_pos.ref = VALVE_MIN_POS;
Lightvalve 57:f4819de54e7a 1035 torq.err_sum = torq.err_sum - valve_pos_rem/(float) TMR_FREQ_5k;
Lightvalve 57:f4819de54e7a 1036 }
Lightvalve 57:f4819de54e7a 1037 }
Lightvalve 61:bc8c8270f0ab 1038
Lightvalve 57:f4819de54e7a 1039 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 60:64181f1d3e60 1040 V_out = (float) Vout.ref;
Lightvalve 57:f4819de54e7a 1041 }
Lightvalve 72:3436ce769b1e 1042 torq_ref_past = torq_ref;
Lightvalve 57:f4819de54e7a 1043 break;
Lightvalve 57:f4819de54e7a 1044 }
Lightvalve 58:2eade98630e2 1045
Lightvalve 57:f4819de54e7a 1046 case MODE_VALVE_OPEN_LOOP: {
Lightvalve 57:f4819de54e7a 1047 V_out = (float) Vout.ref;
Lightvalve 57:f4819de54e7a 1048 break;
Lightvalve 57:f4819de54e7a 1049 }
Lightvalve 169:645207e160ca 1050
Lightvalve 138:a843f32ced33 1051 case MODE_JOINT_ADAPTIVE_BACKSTEPPING: {
Lightvalve 169:645207e160ca 1052
Lightvalve 169:645207e160ca 1053
Lightvalve 139:15621998925b 1054 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 1055 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 1056
Lightvalve 170:42c938a40313 1057 V_adapt = 1.0f / (1.0f/Va + 1.0f/Vb); //initial 0.0000053f
Lightvalve 169:645207e160ca 1058
Lightvalve 218:066030f7951f 1059 //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 1060 float f3_hat = -a_hat * vel.sen/(float)(ENC_PULSE_PER_POSITION)*0.001f; // unit : N/s //xdot=10mm/s일때 -137076
Lightvalve 169:645207e160ca 1061
Lightvalve 138:a843f32ced33 1062 float g3_prime = 0.0f;
Lightvalve 169:645207e160ca 1063 if (torq.sen > Amm*(Ps-Pt)*0.000001f) {
Lightvalve 138:a843f32ced33 1064 g3_prime = 1.0f;
Lightvalve 138:a843f32ced33 1065 } else if (torq.sen < -Amm*(Ps-Pt)*0.000001f) {
Lightvalve 138:a843f32ced33 1066 g3_prime = -1.0f;
Lightvalve 138:a843f32ced33 1067 } else {
Lightvalve 138:a843f32ced33 1068 if ((value-VALVE_CENTER) > 0) {
Lightvalve 169:645207e160ca 1069 g3_prime = sqrt(Ps-Pt-torq.sen/Amm*1000000.0f);
Lightvalve 169:645207e160ca 1070 // g3_prime = sqrt(Ps-Pt);
Lightvalve 138:a843f32ced33 1071 } else {
Lightvalve 138:a843f32ced33 1072 g3_prime = sqrt(Ps-Pt+torq.sen/Amm*1000000.0f);
Lightvalve 139:15621998925b 1073 // g3_prime = sqrt(Ps-Pt);
Lightvalve 138:a843f32ced33 1074 }
Lightvalve 138:a843f32ced33 1075 }
Lightvalve 138:a843f32ced33 1076 float tau = 0.01f;
Lightvalve 142:43026242815a 1077 float K_valve = 0.0004f;
Lightvalve 169:645207e160ca 1078
Lightvalve 138:a843f32ced33 1079 float x_v = 0.0f; //x_v : -1~1
Lightvalve 138:a843f32ced33 1080 if(value>=VALVE_CENTER) {
Lightvalve 138:a843f32ced33 1081 x_v = 1.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER);
Lightvalve 138:a843f32ced33 1082 } else {
Lightvalve 138:a843f32ced33 1083 x_v = -1.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER);
Lightvalve 138:a843f32ced33 1084 }
Lightvalve 138:a843f32ced33 1085 float f4 = -x_v/tau;
Lightvalve 138:a843f32ced33 1086 float g4 = K_valve/tau;
Lightvalve 169:645207e160ca 1087
Lightvalve 139:15621998925b 1088 float torq_ref_dot = torq.ref_diff * 500.0f;
Lightvalve 169:645207e160ca 1089
Lightvalve 138:a843f32ced33 1090 pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm]
Lightvalve 138:a843f32ced33 1091 vel.err = (0.0f - vel.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm/s]
Lightvalve 138:a843f32ced33 1092 pos.err_sum += pos.err/(float) TMR_FREQ_5k; //[mm]
Lightvalve 169:645207e160ca 1093
Lightvalve 138:a843f32ced33 1094 torq.err = torq.ref - torq.sen; //[N]
Lightvalve 138:a843f32ced33 1095 torq.err_sum += torq.err/(float) TMR_FREQ_5k; //[N]
Lightvalve 169:645207e160ca 1096
Lightvalve 212:ec41f1449ef9 1097 float k3 = 2000.0f; //2000 //20000
Lightvalve 142:43026242815a 1098 float k4 = 10.0f;
Lightvalve 142:43026242815a 1099 float rho3 = 3.2f;
Lightvalve 212:ec41f1449ef9 1100 float rho4 = 10000000.0f; //25000000.0f;
Lightvalve 218:066030f7951f 1101 float x_4_des = (-f3_hat + torq_ref_dot - k3*(-torq.err))/(gamma_hat*g3_prime);
Lightvalve 138:a843f32ced33 1102 if (x_4_des > 1) x_4_des = 1;
Lightvalve 138:a843f32ced33 1103 else if (x_4_des < -1) x_4_des = -1;
Lightvalve 169:645207e160ca 1104
Lightvalve 139:15621998925b 1105 if (x_4_des > 0) {
Lightvalve 139:15621998925b 1106 valve_pos.ref = x_4_des * (float)(VALVE_MAX_POS - VALVE_CENTER) + (float) VALVE_CENTER;
Lightvalve 139:15621998925b 1107 } else {
Lightvalve 139:15621998925b 1108 valve_pos.ref = x_4_des * (float)(VALVE_CENTER - VALVE_MIN_POS) + (float) VALVE_CENTER;
Lightvalve 139:15621998925b 1109 }
Lightvalve 169:645207e160ca 1110
Lightvalve 138:a843f32ced33 1111 float x_4_des_dot = (x_4_des - x_4_des_old)*(float) TMR_FREQ_5k;
Lightvalve 138:a843f32ced33 1112 x_4_des_old = x_4_des;
Lightvalve 139:15621998925b 1113 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 1114
Lightvalve 218:066030f7951f 1115 float rho_a = 0.00001f;
Lightvalve 218:066030f7951f 1116 float a_hat_dot = -rho3/rho_a*vel.sen/(float)(ENC_PULSE_PER_POSITION)*0.001f*(-torq.err);
Lightvalve 218:066030f7951f 1117 a_hat = a_hat + a_hat_dot / (float) TMR_FREQ_5k;
Lightvalve 218:066030f7951f 1118
Lightvalve 218:066030f7951f 1119 if(a_hat > -3000000.0f) a_hat = -3000000.0f;
Lightvalve 218:066030f7951f 1120 else if(a_hat < -30000000.0f) a_hat = -30000000.0f;
Lightvalve 218:066030f7951f 1121
Lightvalve 169:645207e160ca 1122 break;
Lightvalve 138:a843f32ced33 1123 }
Lightvalve 171:bfc1fd2629d8 1124
Lightvalve 12:6f2531038ea4 1125 default:
Lightvalve 12:6f2531038ea4 1126 break;
Lightvalve 12:6f2531038ea4 1127 }
Lightvalve 14:8e7590227d22 1128
Lightvalve 57:f4819de54e7a 1129
Lightvalve 57:f4819de54e7a 1130 if (((OPERATING_MODE&0b110)>>1) == 0 || ((OPERATING_MODE&0b110)>>1) == 1) { //Moog Valve or KNR Valve
Lightvalve 58:2eade98630e2 1131
Lightvalve 57:f4819de54e7a 1132 ////////////////////////////////////////////////////////////////////////////
Lightvalve 57:f4819de54e7a 1133 //////////////////////////// CURRENT CONTROL //////////////////////////////
Lightvalve 57:f4819de54e7a 1134 ////////////////////////////////////////////////////////////////////////////
Lightvalve 57:f4819de54e7a 1135 if (CURRENT_CONTROL_MODE) {
Lightvalve 67:c2812cf26c38 1136 double alpha_update_Iref = 1.0f / (1.0f + 5000.0f / (2.0f * 3.14f * 300.0f)); // f_cutoff : 500Hz
Lightvalve 57:f4819de54e7a 1137 I_REF_fil = (1.0f - alpha_update_Iref) * I_REF_fil + alpha_update_Iref*I_REF;
Lightvalve 57:f4819de54e7a 1138
hyhoon 226:82a3ca333004 1139 if (FLAG_VALVE_DEADZONE) {
hyhoon 226:82a3ca333004 1140 if (I_REF_fil > 0.0f) I_REF_fil_DZ = I_REF_fil + (double)VALVE_DEADZONE_PLUS*mA_PER_pulse; // unit: mA
hyhoon 226:82a3ca333004 1141 else if (I_REF_fil < 0.0f) I_REF_fil_DZ = I_REF_fil + (double)VALVE_DEADZONE_MINUS*mA_PER_pulse; // unit: mA
hyhoon 226:82a3ca333004 1142 else I_REF_fil_DZ = I_REF_fil + (double)(VALVE_DEADZONE_PLUS+VALVE_DEADZONE_MINUS)/2.0f*mA_PER_pulse; // unit: mA
hyhoon 226:82a3ca333004 1143 } else {
hyhoon 226:82a3ca333004 1144 I_REF_fil_DZ = I_REF_fil;
hyhoon 226:82a3ca333004 1145 }
hyhoon 226:82a3ca333004 1146 I_ERR = I_REF_fil_DZ - (double)cur.sen;
Lightvalve 67:c2812cf26c38 1147 I_ERR_INT = I_ERR_INT + (I_ERR) * 0.0002f;
Lightvalve 57:f4819de54e7a 1148
Lightvalve 57:f4819de54e7a 1149
Lightvalve 57:f4819de54e7a 1150 // Moog Valve Current Control Gain
Lightvalve 67:c2812cf26c38 1151 double R_model = 500.0f; // ohm
Lightvalve 57:f4819de54e7a 1152 double L_model = 1.2f;
Lightvalve 57:f4819de54e7a 1153 double w0 = 2.0f * 3.14f * 150.0f;
Lightvalve 57:f4819de54e7a 1154 double KP_I = 0.1f * L_model*w0;
Lightvalve 57:f4819de54e7a 1155 double KI_I = 0.1f * R_model*w0;
Lightvalve 57:f4819de54e7a 1156
Lightvalve 57:f4819de54e7a 1157 // KNR Valve Current Control Gain
Lightvalve 57:f4819de54e7a 1158 if (((OPERATING_MODE & 0b110)>>1) == 1) { // KNR Valve
Lightvalve 57:f4819de54e7a 1159 R_model = 163.0f; // ohm
Lightvalve 57:f4819de54e7a 1160 L_model = 1.0f;
Lightvalve 57:f4819de54e7a 1161 w0 = 2.0f * 3.14f * 80.0f;
Lightvalve 57:f4819de54e7a 1162 KP_I = 1.0f * L_model*w0;
Lightvalve 57:f4819de54e7a 1163 KI_I = 0.08f * R_model*w0;
Lightvalve 57:f4819de54e7a 1164 }
Lightvalve 57:f4819de54e7a 1165
Lightvalve 57:f4819de54e7a 1166 double FF_gain = 1.0f;
Lightvalve 57:f4819de54e7a 1167
Lightvalve 67:c2812cf26c38 1168 VALVE_PWM_RAW = KP_I * 2.0f * I_ERR + KI_I * 2.0f* I_ERR_INT;
hyhoon 226:82a3ca333004 1169 I_REF_fil_diff = I_REF_fil_DZ - I_REF_fil_old;
hyhoon 226:82a3ca333004 1170 I_REF_fil_old = I_REF_fil_DZ;
Lightvalve 67:c2812cf26c38 1171 // VALVE_PWM_RAW = VALVE_PWM_RAW + FF_gain * (R_model * I_REF_fil + L_model * I_REF_fil_diff * 5000.0f); // Unit : mV
hyhoon 226:82a3ca333004 1172 VALVE_PWM_RAW = VALVE_PWM_RAW + FF_gain * (R_model * I_REF_fil_DZ); // Unit : mV
Lightvalve 67:c2812cf26c38 1173 double V_MAX = 12000.0f; // Maximum Voltage : 12V = 12000mV
Lightvalve 57:f4819de54e7a 1174
Lightvalve 57:f4819de54e7a 1175 double Ka = 3.0f / KP_I;
Lightvalve 57:f4819de54e7a 1176 if (VALVE_PWM_RAW > V_MAX) {
Lightvalve 57:f4819de54e7a 1177 V_rem = VALVE_PWM_RAW - V_MAX;
Lightvalve 57:f4819de54e7a 1178 V_rem = Ka*V_rem;
Lightvalve 57:f4819de54e7a 1179 VALVE_PWM_RAW = V_MAX;
Lightvalve 67:c2812cf26c38 1180 I_ERR_INT = I_ERR_INT - V_rem * 0.0002f;
Lightvalve 57:f4819de54e7a 1181 } else if (VALVE_PWM_RAW < -V_MAX) {
Lightvalve 57:f4819de54e7a 1182 V_rem = VALVE_PWM_RAW - (-V_MAX);
Lightvalve 57:f4819de54e7a 1183 V_rem = Ka*V_rem;
Lightvalve 57:f4819de54e7a 1184 VALVE_PWM_RAW = -V_MAX;
Lightvalve 67:c2812cf26c38 1185 I_ERR_INT = I_ERR_INT - V_rem * 0.0002f;
Lightvalve 57:f4819de54e7a 1186 }
Lightvalve 57:f4819de54e7a 1187 Cur_Valve_Open_pulse = cur.sen / mA_PER_pulse;
Lightvalve 57:f4819de54e7a 1188 } else {
Lightvalve 57:f4819de54e7a 1189 VALVE_PWM_RAW = I_REF * mV_PER_mA;
Lightvalve 57:f4819de54e7a 1190 Cur_Valve_Open_pulse = I_REF / mA_PER_pulse;
Lightvalve 57:f4819de54e7a 1191 }
Lightvalve 57:f4819de54e7a 1192
Lightvalve 57:f4819de54e7a 1193 ////////////////////////////////////////////////////////////////////////////
Lightvalve 57:f4819de54e7a 1194 ///////////////// Dead Zone Cancellation & Linearization //////////////////
Lightvalve 57:f4819de54e7a 1195 ////////////////////////////////////////////////////////////////////////////
Lightvalve 57:f4819de54e7a 1196
Lightvalve 57:f4819de54e7a 1197 // Output Voltage Linearization
hyhoon 226:82a3ca333004 1198 double CUR_PWM_nonlin = (double)VALVE_PWM_RAW; // Unit : mV
Lightvalve 67:c2812cf26c38 1199 double CUR_PWM_lin = PWM_duty_byLT(CUR_PWM_nonlin); // -8000~8000
Lightvalve 57:f4819de54e7a 1200
Lightvalve 57:f4819de54e7a 1201 // Dead Zone Cancellation (Electrical dead-zone)
Lightvalve 67:c2812cf26c38 1202 if (CUR_PWM_lin > 0) V_out = (float) (CUR_PWM_lin + 169.0f);
Lightvalve 67:c2812cf26c38 1203 else if (CUR_PWM_lin < 0) V_out = (float) (CUR_PWM_lin - 174.0f);
Lightvalve 67:c2812cf26c38 1204 else V_out = (float) (CUR_PWM_lin);
Lightvalve 169:645207e160ca 1205 } else { //////////////////////////sw valve
Lightvalve 224:985dba42f261 1206 // Output Voltage Linearization & Dead Zone Cancellation (Electrical dead-zone) by SW
Lightvalve 135:79885a39c161 1207 if (V_out > 0 ) V_out = (V_out + 180.0f)/0.8588f;
Lightvalve 135:79885a39c161 1208 else if (V_out < 0) V_out = (V_out - 200.0f)/0.8651f;
Lightvalve 135:79885a39c161 1209 else V_out = 0.0f;
Lightvalve 89:a7b45368ea0f 1210 }
Lightvalve 169:645207e160ca 1211
jobuuu 7:e9086c72bb22 1212 /*******************************************************
jobuuu 7:e9086c72bb22 1213 *** PWM
jobuuu 7:e9086c72bb22 1214 ********************************************************/
Lightvalve 169:645207e160ca 1215 if(DIR_VALVE<0) {
Lightvalve 67:c2812cf26c38 1216 V_out = -V_out;
Lightvalve 67:c2812cf26c38 1217 }
Lightvalve 169:645207e160ca 1218
Lightvalve 49:e7bcfc244d40 1219 if (V_out >= VALVE_VOLTAGE_LIMIT*1000.0f) {
Lightvalve 49:e7bcfc244d40 1220 V_out = VALVE_VOLTAGE_LIMIT*1000.0f;
Lightvalve 49:e7bcfc244d40 1221 } else if(V_out<=-VALVE_VOLTAGE_LIMIT*1000.0f) {
Lightvalve 49:e7bcfc244d40 1222 V_out = -VALVE_VOLTAGE_LIMIT*1000.0f;
Lightvalve 27:a2254a485f23 1223 }
Lightvalve 224:985dba42f261 1224 PWM_out= V_out/(SUPPLY_VOLTAGE*1000.0f);
Lightvalve 13:747daba9cf59 1225
Lightvalve 224:985dba42f261 1226 // Saturation of output voltage
Lightvalve 30:8d561f16383b 1227 if(PWM_out > 1.0f) PWM_out=1.0f;
Lightvalve 30:8d561f16383b 1228 else if (PWM_out < -1.0f) PWM_out=-1.0f;
Lightvalve 13:747daba9cf59 1229
Lightvalve 30:8d561f16383b 1230 if (PWM_out>0.0f) {
Lightvalve 30:8d561f16383b 1231 dtc_v=0.0f;
jobuuu 1:e04e563be5ce 1232 dtc_w=PWM_out;
jobuuu 1:e04e563be5ce 1233 } else {
jobuuu 2:a1c0a37df760 1234 dtc_v=-PWM_out;
Lightvalve 30:8d561f16383b 1235 dtc_w=0.0f;
jobuuu 1:e04e563be5ce 1236 }
Lightvalve 13:747daba9cf59 1237
jobuuu 1:e04e563be5ce 1238 //pwm
Lightvalve 30:8d561f16383b 1239 TIM4->CCR2 = (PWM_ARR)*(1.0f-dtc_v);
Lightvalve 30:8d561f16383b 1240 TIM4->CCR1 = (PWM_ARR)*(1.0f-dtc_w);
Lightvalve 67:c2812cf26c38 1241
Lightvalve 20:806196fda269 1242
hyhoon 226:82a3ca333004 1243 if (TMR2_COUNT_CAN_TX % (int) ((int) TMR_FREQ_5k/CAN_FREQ) == 0) {
hyhoon 226:82a3ca333004 1244 // Position, Velocity, and Torque (ID:1200)
hyhoon 226:82a3ca333004 1245 if (flag_data_request[0] == HIGH) {
hyhoon 226:82a3ca333004 1246 if ((OPERATING_MODE & 0b01) == 0) { // Rotary Actuator
hyhoon 226:82a3ca333004 1247 if (SENSING_MODE == 0) {
hyhoon 226:82a3ca333004 1248 CAN_TX_POSITION_FT((int16_t) (pos.sen), (int16_t) (vel.sen/10.0f), (int16_t) (torq.sen*10.0f));
hyhoon 226:82a3ca333004 1249 } else if (SENSING_MODE == 1) {
hyhoon 226:82a3ca333004 1250 CAN_TX_POSITION_PRESSURE((int16_t) (pos.sen), (int16_t) (vel.sen/10.0f), (int16_t) ((pres_A.sen)*5.0f), (int16_t) ((pres_B.sen)*5.0f));
hyhoon 226:82a3ca333004 1251 }
hyhoon 226:82a3ca333004 1252 } else if ((OPERATING_MODE & 0b01) == 1) { // Linear Actuator
hyhoon 226:82a3ca333004 1253 if (SENSING_MODE == 0) {
hyhoon 226:82a3ca333004 1254 CAN_TX_POSITION_FT((int16_t) (pos.sen/10.0f), (int16_t) (vel.sen/256.0f), (int16_t) (torq.sen * 10.0f * (float)(TORQUE_SENSOR_PULSE_PER_TORQUE)));
hyhoon 226:82a3ca333004 1255 } else if (SENSING_MODE == 1) {
hyhoon 226:82a3ca333004 1256 CAN_TX_POSITION_PRESSURE((int16_t) (pos.sen/10.0f), (int16_t) (vel.sen/256.0f), (int16_t) ((pres_A.sen)*5.0f), (int16_t) ((pres_B.sen)*5.0f));
hyhoon 226:82a3ca333004 1257 }
hyhoon 226:82a3ca333004 1258 }
hyhoon 226:82a3ca333004 1259 }
hyhoon 226:82a3ca333004 1260
hyhoon 227:699c3e572283 1261 // PWM, ID:1300
Lightvalve 56:6f50d9d3bfee 1262 if (flag_data_request[1] == HIGH) {
hyhoon 227:699c3e572283 1263 CAN_TX_PWM((int16_t) (V_out)); //1300
Lightvalve 54:647072f5307a 1264 }
Lightvalve 58:2eade98630e2 1265
hyhoon 227:699c3e572283 1266 // Reference Current, Current Current (ID:1400)
Lightvalve 171:bfc1fd2629d8 1267 if (flag_data_request[2] == HIGH) {
hyhoon 227:699c3e572283 1268 CAN_TX_CURRENT((int16_t) (I_REF_fil_DZ / mA_PER_pulse), (int16_t) (cur.sen / mA_PER_pulse)); // 1400
Lightvalve 55:b25725257569 1269 }
hyhoon 227:699c3e572283 1270
hyhoon 227:699c3e572283 1271 // Reference Current, Current Current (ID:1700)
Lightvalve 169:645207e160ca 1272 if (flag_data_request[3] == HIGH) {
hyhoon 227:699c3e572283 1273 CAN_TX_SOMETHING((int16_t) (0), (int16_t) (0), (int16_t) (0), (int16_t) (0)); // 1700
Lightvalve 54:647072f5307a 1274 }
Lightvalve 20:806196fda269 1275
Lightvalve 54:647072f5307a 1276 TMR2_COUNT_CAN_TX = 0;
Lightvalve 54:647072f5307a 1277 }
Lightvalve 54:647072f5307a 1278 TMR2_COUNT_CAN_TX++;
Lightvalve 52:8ea76864368a 1279
Lightvalve 20:806196fda269 1280 }
Lightvalve 52:8ea76864368a 1281 TIM3->SR = 0x0; // reset the status register
Lightvalve 52:8ea76864368a 1282
Lightvalve 58:2eade98630e2 1283 }