rainbow

Dependencies:   mbed FastPWM

Committer:
Lightvalve
Date:
Tue Mar 03 12:27:53 2020 +0000
Revision:
56:6f50d9d3bfee
Parent:
55:b25725257569
Child:
57:f4819de54e7a
200303

Who changed what in which revision?

UserRevisionLine numberNew contents of line
GiJeongKim 0:51c43836c1d7 1 #include "mbed.h"
GiJeongKim 0:51c43836c1d7 2 #include "FastPWM.h"
GiJeongKim 0:51c43836c1d7 3 #include "INIT_HW.h"
jobuuu 2:a1c0a37df760 4 #include "function_CAN.h"
GiJeongKim 0:51c43836c1d7 5 #include "SPI_EEP_ENC.h"
GiJeongKim 0:51c43836c1d7 6 #include "I2C_AS5510.h"
GiJeongKim 0:51c43836c1d7 7 #include "setting.h"
Lightvalve 11:82d8768d7351 8 #include "function_utilities.h"
Lightvalve 16:903b5a4433b4 9 #include "stm32f4xx_flash.h"
Lightvalve 16:903b5a4433b4 10 #include "FlashWriter.h"
GiJeongKim 0:51c43836c1d7 11
Lightvalve 31:66738bfecec5 12 ///191008////
Lightvalve 31:66738bfecec5 13
jobuuu 7:e9086c72bb22 14 // dac & check ///////////////////////////////////////////
GiJeongKim 0:51c43836c1d7 15 DigitalOut check(PC_2);
GiJeongKim 0:51c43836c1d7 16 DigitalOut check_2(PC_3);
GiJeongKim 0:51c43836c1d7 17 AnalogOut dac_1(PA_4);
GiJeongKim 0:51c43836c1d7 18 AnalogOut dac_2(PA_5);
Lightvalve 14:8e7590227d22 19 AnalogIn adc1(PC_4); //pressure_1
Lightvalve 14:8e7590227d22 20 AnalogIn adc2(PB_0); //pressure_2
Lightvalve 14:8e7590227d22 21 AnalogIn adc3(PC_1); //current
GiJeongKim 0:51c43836c1d7 22
Lightvalve 24:ef6e1092e9e6 23
jobuuu 7:e9086c72bb22 24 // PWM ///////////////////////////////////////////
Lightvalve 30:8d561f16383b 25 float dtc_v=0.0f;
Lightvalve 30:8d561f16383b 26 float dtc_w=0.0f;
GiJeongKim 0:51c43836c1d7 27
jobuuu 7:e9086c72bb22 28 // I2C ///////////////////////////////////////////
GiJeongKim 0:51c43836c1d7 29 I2C i2c(PC_9,PA_8); // SDA, SCL (for K22F)
GiJeongKim 0:51c43836c1d7 30 const int i2c_slave_addr1 = 0x56;
GiJeongKim 0:51c43836c1d7 31 unsigned int value; // 10bit output of reading sensor AS5510
GiJeongKim 0:51c43836c1d7 32
jobuuu 7:e9086c72bb22 33 // SPI ///////////////////////////////////////////
Lightvalve 16:903b5a4433b4 34 //SPI eeprom(PB_15, PB_14, PB_13); // EEPROM //(SPI_MOSI, SPI_MISO, SPI_SCK);
Lightvalve 16:903b5a4433b4 35 //DigitalOut eeprom_cs(PB_12);
Lightvalve 16:903b5a4433b4 36 //FlashWriter writer(6);//2부터 7까지 되는듯 아마 sector
GiJeongKim 0:51c43836c1d7 37 SPI enc(PC_12,PC_11,PC_10);
GiJeongKim 0:51c43836c1d7 38 DigitalOut enc_cs(PD_2);
Lightvalve 11:82d8768d7351 39 DigitalOut LED(PA_15);
GiJeongKim 0:51c43836c1d7 40
jobuuu 7:e9086c72bb22 41 // UART ///////////////////////////////////////////
GiJeongKim 0:51c43836c1d7 42 Serial pc(PA_9,PA_10); // _ UART
GiJeongKim 0:51c43836c1d7 43
jobuuu 7:e9086c72bb22 44 // CAN ///////////////////////////////////////////
GiJeongKim 0:51c43836c1d7 45 CAN can(PB_8, PB_9, 1000000);
GiJeongKim 0:51c43836c1d7 46 CANMessage msg;
Lightvalve 11:82d8768d7351 47 void onMsgReceived()
Lightvalve 11:82d8768d7351 48 {
Lightvalve 11:82d8768d7351 49 CAN_RX_HANDLER();
Lightvalve 11:82d8768d7351 50 }
jobuuu 2:a1c0a37df760 51
jobuuu 7:e9086c72bb22 52 // Variables ///////////////////////////////////////////
jobuuu 7:e9086c72bb22 53 State pos;
jobuuu 7:e9086c72bb22 54 State vel;
jobuuu 7:e9086c72bb22 55 State Vout;
jobuuu 7:e9086c72bb22 56 State torq;
jobuuu 7:e9086c72bb22 57 State pres_A;
jobuuu 7:e9086c72bb22 58 State pres_B;
jobuuu 7:e9086c72bb22 59 State cur;
Lightvalve 14:8e7590227d22 60 State valve_pos;
Lightvalve 14:8e7590227d22 61
Lightvalve 14:8e7590227d22 62 State INIT_Vout;
Lightvalve 14:8e7590227d22 63 State INIT_Valve_Pos;
Lightvalve 14:8e7590227d22 64 State INIT_Pos;
Lightvalve 14:8e7590227d22 65 State INIT_torq;
jobuuu 5:a4319f79457b 66
Lightvalve 56:6f50d9d3bfee 67 float V_out=0.0f;
Lightvalve 56:6f50d9d3bfee 68 float V_rem=0.0f; // for anti-windup
Lightvalve 56:6f50d9d3bfee 69 float V_MAX = 12000.0f; // Maximum Voltage : 12V = 12000mV
Lightvalve 56:6f50d9d3bfee 70
Lightvalve 56:6f50d9d3bfee 71 float PWM_out=0.0f;
Lightvalve 56:6f50d9d3bfee 72
Lightvalve 56:6f50d9d3bfee 73 int timer_while = 0;
Lightvalve 56:6f50d9d3bfee 74 int while_index = 0;
Lightvalve 56:6f50d9d3bfee 75
Lightvalve 19:23b7c1ad8683 76 extern int CID_RX_CMD;
Lightvalve 19:23b7c1ad8683 77 extern int CID_RX_REF_POSITION;
Lightvalve 49:e7bcfc244d40 78 extern int CID_RX_REF_VALVE_POS;
Lightvalve 45:35fa6884d0c6 79 extern int CID_RX_REF_PWM;
Lightvalve 19:23b7c1ad8683 80
Lightvalve 19:23b7c1ad8683 81 extern int CID_TX_INFO;
Lightvalve 19:23b7c1ad8683 82 extern int CID_TX_POSITION;
Lightvalve 19:23b7c1ad8683 83 extern int CID_TX_TORQUE;
Lightvalve 19:23b7c1ad8683 84 extern int CID_TX_PRES;
Lightvalve 19:23b7c1ad8683 85 extern int CID_TX_VOUT;
Lightvalve 19:23b7c1ad8683 86 extern int CID_TX_VALVE_POSITION;
Lightvalve 19:23b7c1ad8683 87
jobuuu 7:e9086c72bb22 88 // =============================================================================
jobuuu 7:e9086c72bb22 89 // =============================================================================
jobuuu 7:e9086c72bb22 90 // =============================================================================
jobuuu 7:e9086c72bb22 91
Lightvalve 12:6f2531038ea4 92 /*******************************************************************************
Lightvalve 12:6f2531038ea4 93 * REFERENCE MODE
Lightvalve 12:6f2531038ea4 94 ******************************************************************************/
Lightvalve 13:747daba9cf59 95 enum _REFERENCE_MODE {
Lightvalve 13:747daba9cf59 96 MODE_REF_NO_ACT = 0, //0
Lightvalve 12:6f2531038ea4 97 MODE_REF_DIRECT, //1
Lightvalve 12:6f2531038ea4 98 MODE_REF_COS_INC, //2
Lightvalve 12:6f2531038ea4 99 MODE_REF_LINE_INC, //3
Lightvalve 12:6f2531038ea4 100 MODE_REF_SIN_WAVE, //4
Lightvalve 12:6f2531038ea4 101 MODE_REF_SQUARE_WAVE, //5
Lightvalve 12:6f2531038ea4 102 };
Lightvalve 12:6f2531038ea4 103
Lightvalve 12:6f2531038ea4 104 /*******************************************************************************
Lightvalve 12:6f2531038ea4 105 * CONTROL MODE
Lightvalve 12:6f2531038ea4 106 ******************************************************************************/
Lightvalve 13:747daba9cf59 107 enum _CONTROL_MODE {
Lightvalve 12:6f2531038ea4 108 //control mode
Lightvalve 12:6f2531038ea4 109 MODE_NO_ACT = 0, //0
Lightvalve 47:fdcb8bd86fd6 110 MODE_VALVE_POSITION_CONTROL, //1
Lightvalve 47:fdcb8bd86fd6 111 MODE_JOINT_CONTROL, //2
Lightvalve 50:3c630b5eba9f 112
Lightvalve 47:fdcb8bd86fd6 113 MODE_VALVE_OPEN_LOOP, //3
Lightvalve 12:6f2531038ea4 114 MODE_JOINT_POSITION_TORQUE_CONTROL_VALVE_POSITION, //4
Lightvalve 12:6f2531038ea4 115 MODE_VALVE_POSITION_TORQUE_CONTROL_LEARNING, //5
Lightvalve 13:747daba9cf59 116
Lightvalve 12:6f2531038ea4 117 MODE_JOINT_POSITION_PRES_CONTROL_PWM, //6
Lightvalve 12:6f2531038ea4 118 MODE_JOINT_POSITION_PRES_CONTROL_VALVE_POSITION, //7
Lightvalve 12:6f2531038ea4 119 MODE_VALVE_POSITION_PRES_CONTROL_LEARNING, //8
Lightvalve 13:747daba9cf59 120
Lightvalve 12:6f2531038ea4 121 MODE_TEST_CURRENT_CONTROL, //9
Lightvalve 12:6f2531038ea4 122 MODE_TEST_PWM_CONTROL, //10
Lightvalve 21:e5f1a43ea6f9 123
Lightvalve 14:8e7590227d22 124 MODE_CURRENT_CONTROL, //11
Lightvalve 14:8e7590227d22 125 MODE_JOINT_POSITION_TORQUE_CONTROL_CURRENT, //12
Lightvalve 14:8e7590227d22 126 MODE_JOINT_POSITION_PRES_CONTROL_CURRENT, //13
Lightvalve 13:747daba9cf59 127
Lightvalve 12:6f2531038ea4 128 //utility
Lightvalve 12:6f2531038ea4 129 MODE_TORQUE_SENSOR_NULLING = 20, //20
Lightvalve 12:6f2531038ea4 130 MODE_VALVE_NULLING_AND_DEADZONE_SETTING, //21
Lightvalve 12:6f2531038ea4 131 MODE_FIND_HOME, //22
Lightvalve 56:6f50d9d3bfee 132 MODE_VALVE_GAIN_SETTING, //23
Lightvalve 12:6f2531038ea4 133 MODE_PRESSURE_SENSOR_NULLING, //24
Lightvalve 12:6f2531038ea4 134 MODE_PRESSURE_SENSOR_CALIB, //25
Lightvalve 12:6f2531038ea4 135 MODE_ROTARY_FRICTION_TUNING, //26
Lightvalve 13:747daba9cf59 136
Lightvalve 12:6f2531038ea4 137 MODE_DDV_POS_VS_PWM_ID = 30, //30
Lightvalve 12:6f2531038ea4 138 MODE_DDV_DEADZONE_AND_CENTER, //31
Lightvalve 12:6f2531038ea4 139 MODE_DDV_POS_VS_FLOWRATE, //32
Lightvalve 12:6f2531038ea4 140 };
Lightvalve 12:6f2531038ea4 141
GiJeongKim 0:51c43836c1d7 142 int main()
GiJeongKim 0:51c43836c1d7 143 {
jobuuu 6:df07d3491e3a 144 /*********************************
jobuuu 1:e04e563be5ce 145 *** Initialization
jobuuu 6:df07d3491e3a 146 *********************************/
Lightvalve 56:6f50d9d3bfee 147 //LED = 1;
Lightvalve 19:23b7c1ad8683 148 //pc.baud(9600);
Lightvalve 21:e5f1a43ea6f9 149
GiJeongKim 0:51c43836c1d7 150 // i2c init
Lightvalve 8:5d2eebdad025 151 i2c.frequency(400 * 1000); // 0.4 mHz
Lightvalve 8:5d2eebdad025 152 wait_ms(2); // Power Up wait
Lightvalve 8:5d2eebdad025 153 look_for_hardware_i2c(); // Hardware present
Lightvalve 8:5d2eebdad025 154 init_as5510(i2c_slave_addr1);
Lightvalve 11:82d8768d7351 155 make_delay();
jobuuu 2:a1c0a37df760 156
GiJeongKim 0:51c43836c1d7 157 // // spi init
Lightvalve 16:903b5a4433b4 158 //eeprom.format(8,3);
Lightvalve 16:903b5a4433b4 159 //eeprom.frequency(5000000); //5M
GiJeongKim 0:51c43836c1d7 160 enc.format(8,0);
GiJeongKim 0:51c43836c1d7 161 enc.frequency(5000000); //5M
Lightvalve 11:82d8768d7351 162 make_delay();
Lightvalve 21:e5f1a43ea6f9 163
Lightvalve 16:903b5a4433b4 164 //rom
Lightvalve 19:23b7c1ad8683 165 ROM_CALL_DATA();
Lightvalve 16:903b5a4433b4 166 make_delay();
Lightvalve 13:747daba9cf59 167
GiJeongKim 0:51c43836c1d7 168 // ADC init
jobuuu 5:a4319f79457b 169 Init_ADC();
Lightvalve 11:82d8768d7351 170 make_delay();
jobuuu 2:a1c0a37df760 171
GiJeongKim 0:51c43836c1d7 172 // Pwm init
GiJeongKim 0:51c43836c1d7 173 Init_PWM();
GiJeongKim 0:51c43836c1d7 174 TIM4->CR1 ^= TIM_CR1_UDIS;
Lightvalve 11:82d8768d7351 175 make_delay();
Lightvalve 13:747daba9cf59 176
Lightvalve 11:82d8768d7351 177 // TMR3 init
Lightvalve 11:82d8768d7351 178 Init_TMR3();
Lightvalve 11:82d8768d7351 179 TIM3->CR1 ^= TIM_CR1_UDIS;
Lightvalve 11:82d8768d7351 180 make_delay();
Lightvalve 21:e5f1a43ea6f9 181
Lightvalve 50:3c630b5eba9f 182 // TMR2 init
Lightvalve 56:6f50d9d3bfee 183 // Init_TMR2();
Lightvalve 56:6f50d9d3bfee 184 // TIM2->CR1 ^= TIM_CR1_UDIS;
Lightvalve 56:6f50d9d3bfee 185 // make_delay();
Lightvalve 21:e5f1a43ea6f9 186
GiJeongKim 0:51c43836c1d7 187 // CAN
jobuuu 2:a1c0a37df760 188 can.attach(&CAN_RX_HANDLER);
Lightvalve 11:82d8768d7351 189 CAN_ID_INIT();
Lightvalve 11:82d8768d7351 190 make_delay();
Lightvalve 34:bb2ca2fc2a8e 191
Lightvalve 23:59218d4a256d 192 //Timer priority
Lightvalve 23:59218d4a256d 193 NVIC_SetPriority(TIM3_IRQn, 2);
Lightvalve 56:6f50d9d3bfee 194 // NVIC_SetPriority(TIM2_IRQn, 3);
Lightvalve 54:647072f5307a 195 NVIC_SetPriority(TIM4_IRQn, 3);
Lightvalve 34:bb2ca2fc2a8e 196
Lightvalve 23:59218d4a256d 197 //can.reset();
Lightvalve 19:23b7c1ad8683 198 can.filter(msg.id, 0xFFFFF000, CANStandard);
Lightvalve 34:bb2ca2fc2a8e 199
GiJeongKim 0:51c43836c1d7 200 // spi _ enc
GiJeongKim 0:51c43836c1d7 201 spi_enc_set_init();
Lightvalve 11:82d8768d7351 202 make_delay();
Lightvalve 13:747daba9cf59 203
Lightvalve 11:82d8768d7351 204 //DAC init
Lightvalve 43:b084e5f5d0d5 205 // dac_1 = PRES_A_VREF / 3.3f;
Lightvalve 43:b084e5f5d0d5 206 // dac_2 = PRES_B_VREF / 3.3f;
Lightvalve 43:b084e5f5d0d5 207 dac_1 = TORQUE_VREF / 3.3f;
Lightvalve 43:b084e5f5d0d5 208 dac_2 = 0.0f;
Lightvalve 11:82d8768d7351 209 make_delay();
Lightvalve 13:747daba9cf59 210
Lightvalve 19:23b7c1ad8683 211 for (int i=0; i<50; i++) {
Lightvalve 11:82d8768d7351 212 if(i%2==0)
Lightvalve 38:118df027d851 213 ID_index_array[i] = - i * 0.5f;
Lightvalve 11:82d8768d7351 214 else
Lightvalve 38:118df027d851 215 ID_index_array[i] = (i+1) * 0.5f;
Lightvalve 11:82d8768d7351 216 }
Lightvalve 56:6f50d9d3bfee 217
jobuuu 6:df07d3491e3a 218 /************************************
jobuuu 1:e04e563be5ce 219 *** Program is operating!
jobuuu 6:df07d3491e3a 220 *************************************/
GiJeongKim 0:51c43836c1d7 221 while(1) {
Lightvalve 21:e5f1a43ea6f9 222 if(timer_while==1000) {
Lightvalve 17:1865016ca2e7 223 //i2c
Lightvalve 17:1865016ca2e7 224 read_field(i2c_slave_addr1);
Lightvalve 17:1865016ca2e7 225 if(DIR_VALVE_ENC < 0) value = 1023 - value;
Lightvalve 19:23b7c1ad8683 226 // if(LED==1) {
Lightvalve 19:23b7c1ad8683 227 // LED=0;
Lightvalve 19:23b7c1ad8683 228 // } else
Lightvalve 19:23b7c1ad8683 229 // LED = 1;
Lightvalve 17:1865016ca2e7 230 timer_while = 0;
Lightvalve 17:1865016ca2e7 231 }
Lightvalve 17:1865016ca2e7 232 timer_while ++;
GiJeongKim 0:51c43836c1d7 233 }
jobuuu 1:e04e563be5ce 234 }
jobuuu 1:e04e563be5ce 235
Lightvalve 33:91b17819ec30 236 float DDV_JOINT_POS_FF(float REF_JOINT_VEL)
Lightvalve 14:8e7590227d22 237 {
Lightvalve 14:8e7590227d22 238
Lightvalve 13:747daba9cf59 239 int i = 0;
Lightvalve 48:889798ff9329 240 float Ref_Valve_Pos_FF = 0.0f;
Lightvalve 14:8e7590227d22 241 for(i=0; i<VALVE_POS_NUM; i++) {
Lightvalve 14:8e7590227d22 242 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 243 if(i==0) {
Lightvalve 50:3c630b5eba9f 244 if(JOINT_VEL[i+1] == JOINT_VEL[i]) {
Lightvalve 56:6f50d9d3bfee 245 Ref_Valve_Pos_FF = DDV_CENTER;
Lightvalve 50:3c630b5eba9f 246 } else {
Lightvalve 56:6f50d9d3bfee 247 Ref_Valve_Pos_FF = ((float) 10/(JOINT_VEL[i+1] - JOINT_VEL[i]) * (REF_JOINT_VEL - JOINT_VEL[i])) + DDV_CENTER;
Lightvalve 38:118df027d851 248 }
Lightvalve 14:8e7590227d22 249 } else {
Lightvalve 50:3c630b5eba9f 250 if(JOINT_VEL[i+1] == JOINT_VEL[i-1]) {
Lightvalve 56:6f50d9d3bfee 251 Ref_Valve_Pos_FF = DDV_CENTER;
Lightvalve 50:3c630b5eba9f 252 } else {
Lightvalve 56:6f50d9d3bfee 253 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])) + DDV_CENTER + (float) (10*ID_index_array[i-1]);
Lightvalve 38:118df027d851 254 }
Lightvalve 13:747daba9cf59 255 }
Lightvalve 13:747daba9cf59 256 break;
Lightvalve 13:747daba9cf59 257 }
Lightvalve 13:747daba9cf59 258 }
Lightvalve 14:8e7590227d22 259 if(REF_JOINT_VEL > max(JOINT_VEL[VALVE_POS_NUM-1], JOINT_VEL[VALVE_POS_NUM-2])) {
Lightvalve 33:91b17819ec30 260 Ref_Valve_Pos_FF = (float) VALVE_MAX_POS;
Lightvalve 14:8e7590227d22 261 } else if(REF_JOINT_VEL < min(JOINT_VEL[VALVE_POS_NUM-1], JOINT_VEL[VALVE_POS_NUM-2])) {
Lightvalve 33:91b17819ec30 262 Ref_Valve_Pos_FF = (float) VALVE_MIN_POS;
Lightvalve 13:747daba9cf59 263 }
Lightvalve 36:a46e63505ed8 264
Lightvalve 56:6f50d9d3bfee 265 Ref_Valve_Pos_FF = (float) VELOCITY_COMP_GAIN * 0.01f * (float) (Ref_Valve_Pos_FF - DDV_CENTER);
Lightvalve 13:747daba9cf59 266 return Ref_Valve_Pos_FF;
Lightvalve 50:3c630b5eba9f 267
Lightvalve 13:747daba9cf59 268 }
jobuuu 6:df07d3491e3a 269
jobuuu 6:df07d3491e3a 270
Lightvalve 30:8d561f16383b 271 void VALVE_POS_CONTROL(float REF_VALVE_POS)
Lightvalve 14:8e7590227d22 272 {
Lightvalve 13:747daba9cf59 273 int i = 0;
Lightvalve 13:747daba9cf59 274
Lightvalve 38:118df027d851 275 if(REF_VALVE_POS > VALVE_MAX_POS) {
Lightvalve 38:118df027d851 276 REF_VALVE_POS = VALVE_MAX_POS;
Lightvalve 38:118df027d851 277 } else if(REF_VALVE_POS < VALVE_MIN_POS) {
Lightvalve 38:118df027d851 278 REF_VALVE_POS = VALVE_MIN_POS;
Lightvalve 38:118df027d851 279 }
Lightvalve 38:118df027d851 280
Lightvalve 13:747daba9cf59 281 valve_pos_err = REF_VALVE_POS - value;
Lightvalve 13:747daba9cf59 282 valve_pos_err_diff = valve_pos_err - valve_pos_err_old;
Lightvalve 13:747daba9cf59 283 valve_pos_err_old = valve_pos_err;
Lightvalve 13:747daba9cf59 284 valve_pos_err_sum += valve_pos_err;
Lightvalve 13:747daba9cf59 285 if (valve_pos_err_sum > 1000) valve_pos_err_sum = 1000;
Lightvalve 13:747daba9cf59 286 if (valve_pos_err_sum<-1000) valve_pos_err_sum = -1000;
Lightvalve 13:747daba9cf59 287
Lightvalve 13:747daba9cf59 288 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 289
Lightvalve 18:b8adf1582ea3 290 for(i=0; i<24; i++) {
Lightvalve 14:8e7590227d22 291 if(REF_VALVE_POS >= min(VALVE_POS_VS_PWM[i],VALVE_POS_VS_PWM[i+1]) && REF_VALVE_POS <= max(VALVE_POS_VS_PWM[i],VALVE_POS_VS_PWM[i+1])) {
Lightvalve 14:8e7590227d22 292 if(i==0) {
Lightvalve 48:889798ff9329 293 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 294 } else {
Lightvalve 48:889798ff9329 295 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 296 }
Lightvalve 13:747daba9cf59 297 break;
Lightvalve 13:747daba9cf59 298 }
Lightvalve 13:747daba9cf59 299 }
Lightvalve 50:3c630b5eba9f 300 V_out = VALVE_PWM_RAW_FF + VALVE_PWM_RAW_FB;
Lightvalve 13:747daba9cf59 301 }
Lightvalve 13:747daba9cf59 302
Lightvalve 14:8e7590227d22 303 #define LT_MAX_IDX 57
Lightvalve 30:8d561f16383b 304 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 305 -19.0f, -18.0f, -17.0f, -16.0f, -15.0f, -14.0f, -13.0f, -12.0f, -11.0f, -10.0f,
Lightvalve 34:bb2ca2fc2a8e 306 -9.0f, -8.0f, -7.0f, -6.0f, -5.0f, -4.0f, -3.0f, -2.0f, -1.0f, 0.0f,
Lightvalve 34:bb2ca2fc2a8e 307 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f,
Lightvalve 34:bb2ca2fc2a8e 308 11.0f, 12.0f, 13.0f, 14.0f, 15.0f, 16.0f, 17.0f, 18.0f, 19.0f, 20.0f,
Lightvalve 34:bb2ca2fc2a8e 309 25.0f, 30.0f, 35.0f, 40.0f, 50.0f, 60.0f, 80.0f, 100.0f
Lightvalve 34:bb2ca2fc2a8e 310 }; // duty
Lightvalve 30:8d561f16383b 311 float LT_Voltage_Output[LT_MAX_IDX] = {-321.4f, -291.3f, -261.5f, -246.8f, -231.7f, -223.9f, -216.1f, -207.9f, -198.8f,
Lightvalve 34:bb2ca2fc2a8e 312 -196.9f, -195.0f, -192.5f, -188.8f, -184.5f, -180.2f, -175.9f, -171.5f, -166.3f, -161.0f,
Lightvalve 34:bb2ca2fc2a8e 313 -156.0f, -149.5f, -139.0f, -126.0f, -107.0f, -87.5f, -64.0f, -38.5f, -9.4f, 0.0f,
Lightvalve 34:bb2ca2fc2a8e 314 12.0f, 43.5f, 69.0f, 94.0f, 114.0f, 132.0f, 146.0f, 155.5f, 162.3f, 168.2f,
Lightvalve 34:bb2ca2fc2a8e 315 173.1f, 178.2f, 182.8f, 187.4f, 191.8f, 196.0f, 199.7f, 201.9f, 203.8f, 205.6f,
Lightvalve 34:bb2ca2fc2a8e 316 214.6f, 222.5f, 230.4f, 238.2f, 253.3f, 268.0f, 297.6f, 327.7f
Lightvalve 34:bb2ca2fc2a8e 317 }; // mV
Lightvalve 13:747daba9cf59 318
Lightvalve 30:8d561f16383b 319 float PWM_duty_byLT(float Ref_V)
Lightvalve 14:8e7590227d22 320 {
Lightvalve 30:8d561f16383b 321 float PWM_duty = 0.0f;
Lightvalve 13:747daba9cf59 322 if(Ref_V<LT_Voltage_Output[0]) {
Lightvalve 30:8d561f16383b 323 PWM_duty = (Ref_V-LT_Voltage_Output[0])/1.5f+LT_PWM_duty[0];
Lightvalve 13:747daba9cf59 324 } else if (Ref_V>=LT_Voltage_Output[LT_MAX_IDX-1]) {
Lightvalve 30:8d561f16383b 325 PWM_duty = (Ref_V-LT_Voltage_Output[LT_MAX_IDX-1])/1.5f+LT_PWM_duty[LT_MAX_IDX-1];
Lightvalve 13:747daba9cf59 326 } else {
Lightvalve 13:747daba9cf59 327 int idx = 0;
Lightvalve 13:747daba9cf59 328 for(idx=0; idx<LT_MAX_IDX-1; idx++) {
Lightvalve 30:8d561f16383b 329 float ini_x = LT_Voltage_Output[idx];
Lightvalve 30:8d561f16383b 330 float fin_x = LT_Voltage_Output[idx+1];
Lightvalve 30:8d561f16383b 331 float ini_y = LT_PWM_duty[idx];
Lightvalve 30:8d561f16383b 332 float fin_y = LT_PWM_duty[idx+1];
Lightvalve 13:747daba9cf59 333 if(Ref_V>=ini_x && Ref_V<fin_x) {
Lightvalve 13:747daba9cf59 334 PWM_duty = (fin_y-ini_y)/(fin_x-ini_x)*(Ref_V-ini_x) + ini_y;
Lightvalve 13:747daba9cf59 335 break;
Lightvalve 13:747daba9cf59 336 }
Lightvalve 13:747daba9cf59 337 }
Lightvalve 13:747daba9cf59 338 }
Lightvalve 14:8e7590227d22 339
Lightvalve 13:747daba9cf59 340 return PWM_duty;
Lightvalve 13:747daba9cf59 341 }
jobuuu 6:df07d3491e3a 342
jobuuu 2:a1c0a37df760 343 /*******************************************************************************
jobuuu 2:a1c0a37df760 344 TIMER INTERRUPT
jobuuu 2:a1c0a37df760 345 *******************************************************************************/
jobuuu 2:a1c0a37df760 346
Lightvalve 51:b46bed7fec80 347 float FREQ_TMR4 = (float)FREQ_20k;
Lightvalve 51:b46bed7fec80 348 float DT_TMR4 = (float)DT_20k;
jobuuu 1:e04e563be5ce 349 extern "C" void TIM4_IRQHandler(void)
jobuuu 1:e04e563be5ce 350 {
Lightvalve 56:6f50d9d3bfee 351
Lightvalve 19:23b7c1ad8683 352 if (TIM4->SR & TIM_SR_UIF ) {
Lightvalve 21:e5f1a43ea6f9 353
Lightvalve 21:e5f1a43ea6f9 354 /*******************************************************
Lightvalve 21:e5f1a43ea6f9 355 *** Sensor Read & Data Handling
Lightvalve 21:e5f1a43ea6f9 356 ********************************************************/
Lightvalve 13:747daba9cf59 357
Lightvalve 56:6f50d9d3bfee 358
Lightvalve 56:6f50d9d3bfee 359 //Using LoadCell
Lightvalve 56:6f50d9d3bfee 360 // ADC1->CR2 |= 0x40000000; // adc _ 12bit
Lightvalve 56:6f50d9d3bfee 361 // //while((ADC1->SR & 0b10));
Lightvalve 56:6f50d9d3bfee 362 // float alpha_update_torque = 1.0f/(1.0f+(FREQ_TMR4/2.0f)/(2.0f*3.14f*1000.0f));
Lightvalve 56:6f50d9d3bfee 363 // float torque_new = ((float)ADC1->DR - PRES_A_NULL) / TORQUE_SENSOR_PULSE_PER_TORQUE + 1.0f;
Lightvalve 56:6f50d9d3bfee 364 // torq.sen = torq.sen*(1.0f-alpha_update_torque)+torque_new*(alpha_update_torque);
Lightvalve 56:6f50d9d3bfee 365
Lightvalve 56:6f50d9d3bfee 366
Lightvalve 17:1865016ca2e7 367
Lightvalve 21:e5f1a43ea6f9 368 //Pressure sensor A
Lightvalve 21:e5f1a43ea6f9 369 ADC1->CR2 |= 0x40000000; // adc _ 12bit
Lightvalve 21:e5f1a43ea6f9 370 //while((ADC1->SR & 0b10));
Lightvalve 51:b46bed7fec80 371 float alpha_update_pres_A = 1.0f/(1.0f + FREQ_TMR4/(2.0f*3.14f*100.0f));
Lightvalve 56:6f50d9d3bfee 372 // float pres_A_new = ((float)ADC1->DR - PRES_A_NULL) / PRES_SENSOR_A_PULSE_PER_BAR;
Lightvalve 38:118df027d851 373 float pres_A_new = ((float)ADC1->DR);
Lightvalve 30:8d561f16383b 374 pres_A.sen = pres_A.sen*(1.0f-alpha_update_pres_A)+pres_A_new*(alpha_update_pres_A);
Lightvalve 56:6f50d9d3bfee 375 torq.sen = - (pres_A.sen-2048.0f); //pulse -2047~2047
Lightvalve 50:3c630b5eba9f 376
Lightvalve 19:23b7c1ad8683 377
Lightvalve 56:6f50d9d3bfee 378 //Pressure sensor 1B
Lightvalve 56:6f50d9d3bfee 379 //float alpha_update_pres_B = 1.0f/(1.0f+(FREQ_TMR4/2.0f)/(2.0f*3.14f*1000.0f));
Lightvalve 56:6f50d9d3bfee 380 //float pres_B_new = ((float)ADC2->DR);
Lightvalve 56:6f50d9d3bfee 381 //pres_B.sen = pres_B.sen*(1.0f-alpha_update_pres_B)+pres_B_new*(alpha_update_pres_B);
Lightvalve 55:b25725257569 382 //torq.sen = pres_A.sen * (float) PISTON_AREA_A - pres_B.sen * (float) PISTON_AREA_B;
Lightvalve 17:1865016ca2e7 383
Lightvalve 17:1865016ca2e7 384
Lightvalve 56:6f50d9d3bfee 385
Lightvalve 21:e5f1a43ea6f9 386 //Current
Lightvalve 21:e5f1a43ea6f9 387 //ADC3->CR2 |= 0x40000000; // adc _ 12bit
Lightvalve 56:6f50d9d3bfee 388 // a1=ADC2->DR;
Lightvalve 25:3e6b574cab5c 389 //int raw_cur = ADC3->DR;
Lightvalve 21:e5f1a43ea6f9 390 //while((ADC3->SR & 0b10));
Lightvalve 51:b46bed7fec80 391 float alpha_update_cur = 1.0f/(1.0f + FREQ_TMR4/(2.0f*3.14f*500.0f)); // f_cutoff : 500Hz
Lightvalve 30:8d561f16383b 392 float cur_new = ((float)ADC3->DR-2048.0f)*20.0f/4096.0f; // unit : mA
Lightvalve 30:8d561f16383b 393 cur.sen=cur.sen*(1.0f-alpha_update_cur)+cur_new*(alpha_update_cur);
Lightvalve 25:3e6b574cab5c 394 //cur.sen = raw_cur;
Lightvalve 56:6f50d9d3bfee 395
Lightvalve 56:6f50d9d3bfee 396 /*******************************************************
Lightvalve 56:6f50d9d3bfee 397 *** Timer Counting & etc.
Lightvalve 56:6f50d9d3bfee 398 ********************************************************/
Lightvalve 56:6f50d9d3bfee 399 //CNT_TMR4++;
Lightvalve 21:e5f1a43ea6f9 400 }
Lightvalve 11:82d8768d7351 401 TIM4->SR = 0x0; // reset the status register
Lightvalve 11:82d8768d7351 402 }
Lightvalve 19:23b7c1ad8683 403
Lightvalve 19:23b7c1ad8683 404
Lightvalve 18:b8adf1582ea3 405 int j =0;
Lightvalve 56:6f50d9d3bfee 406 //unsigned long CNT_TMR3 = 0;
Lightvalve 56:6f50d9d3bfee 407 //float FREQ_TMR3 = (float)FREQ_5k;
Lightvalve 54:647072f5307a 408 float FREQ_TMR3 = (float)FREQ_5k;
Lightvalve 48:889798ff9329 409 float DT_TMR3 = (float)DT_5k;
Lightvalve 56:6f50d9d3bfee 410 //float DT_TMR3 = (float)DT_1k;
Lightvalve 45:35fa6884d0c6 411 int cnt_trans = 0;
Lightvalve 48:889798ff9329 412 double VALVE_POS_RAW_FORCE_FB_LOGGING = 0.0f;
Lightvalve 56:6f50d9d3bfee 413 int canfreq = CAN_FREQUENCY;
Lightvalve 48:889798ff9329 414
Lightvalve 11:82d8768d7351 415 extern "C" void TIM3_IRQHandler(void)
Lightvalve 21:e5f1a43ea6f9 416 {
Lightvalve 19:23b7c1ad8683 417 if (TIM3->SR & TIM_SR_UIF ) {
Lightvalve 56:6f50d9d3bfee 418 ENC_UPDATE();
Lightvalve 50:3c630b5eba9f 419
Lightvalve 50:3c630b5eba9f 420 if(MODE_POS_FT_TRANS == 1) {
Lightvalve 48:889798ff9329 421 alpha_trans = (float)(1.0f - cos(3.141592f * (float)cnt_trans * DT_TMR3 /3.0f))/2.0f;
Lightvalve 45:35fa6884d0c6 422 cnt_trans++;
Lightvalve 46:2694daea349b 423 torq.err_sum = 0;
Lightvalve 48:889798ff9329 424 if((float)cnt_trans * DT_TMR3 > 3.0f)
Lightvalve 45:35fa6884d0c6 425 MODE_POS_FT_TRANS = 2;
Lightvalve 50:3c630b5eba9f 426 } else if(MODE_POS_FT_TRANS == 3) {
Lightvalve 48:889798ff9329 427 alpha_trans = (float)(1.0f + cos(3.141592f * (float)cnt_trans * DT_TMR3 /3.0f))/2.0f;
Lightvalve 45:35fa6884d0c6 428 cnt_trans++;
Lightvalve 46:2694daea349b 429 torq.err_sum = 0;
Lightvalve 48:889798ff9329 430 if((float) cnt_trans * DT_TMR3 > 3.0f )
Lightvalve 45:35fa6884d0c6 431 MODE_POS_FT_TRANS = 0;
Lightvalve 50:3c630b5eba9f 432 } else if(MODE_POS_FT_TRANS == 2) {
Lightvalve 45:35fa6884d0c6 433 alpha_trans = 1.0;
Lightvalve 45:35fa6884d0c6 434 cnt_trans = 0;
Lightvalve 50:3c630b5eba9f 435 } else {
Lightvalve 45:35fa6884d0c6 436 alpha_trans = 0.0;
Lightvalve 45:35fa6884d0c6 437 cnt_trans = 0;
Lightvalve 45:35fa6884d0c6 438 }
Lightvalve 45:35fa6884d0c6 439
Lightvalve 50:3c630b5eba9f 440
Lightvalve 56:6f50d9d3bfee 441
Lightvalve 56:6f50d9d3bfee 442 // CONTROL LOOP ------------------------------------------------------------
Lightvalve 56:6f50d9d3bfee 443
Lightvalve 56:6f50d9d3bfee 444 switch (CONTROL_MODE) {
Lightvalve 56:6f50d9d3bfee 445 case MODE_NO_ACT: {
Lightvalve 56:6f50d9d3bfee 446 V_out = 0.0f;
Lightvalve 56:6f50d9d3bfee 447 break;
Lightvalve 56:6f50d9d3bfee 448 }
Lightvalve 56:6f50d9d3bfee 449
Lightvalve 56:6f50d9d3bfee 450 case MODE_VALVE_POSITION_CONTROL: {
Lightvalve 56:6f50d9d3bfee 451 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 56:6f50d9d3bfee 452 break;
Lightvalve 56:6f50d9d3bfee 453 }
Lightvalve 56:6f50d9d3bfee 454
Lightvalve 56:6f50d9d3bfee 455 case MODE_JOINT_CONTROL: {
Lightvalve 56:6f50d9d3bfee 456
Lightvalve 56:6f50d9d3bfee 457 float VALVE_POS_RAW_FORCE_FB = 0.0f;
Lightvalve 56:6f50d9d3bfee 458
Lightvalve 56:6f50d9d3bfee 459
Lightvalve 56:6f50d9d3bfee 460 double torq_ref = 0.0f;
Lightvalve 56:6f50d9d3bfee 461 //if(TMR3_COUNT_TEST % (int) (50) == 0){
Lightvalve 56:6f50d9d3bfee 462 pos.err = pos.ref - pos.sen; //[pulse]
Lightvalve 56:6f50d9d3bfee 463 vel.err = vel.ref - vel.sen; //[pulse/s]
Lightvalve 56:6f50d9d3bfee 464 torq_ref = torq.ref + (K_SPRING * pos.err * 0.01f + D_DAMPER * vel.err * 0.0001f) / ENC_PULSE_PER_POSITION; //[N]
Lightvalve 56:6f50d9d3bfee 465 //torq_ref_logging = torq_ref
Lightvalve 56:6f50d9d3bfee 466 //}
Lightvalve 56:6f50d9d3bfee 467
Lightvalve 56:6f50d9d3bfee 468 // torque feedback
Lightvalve 56:6f50d9d3bfee 469 torq.err = torq_ref - torq.sen; //[pulse]
Lightvalve 56:6f50d9d3bfee 470 torq.err_sum += torq.err/(float) TMR_FREQ_5k; //[pulse]
Lightvalve 56:6f50d9d3bfee 471 // if (torq.err_sum > 1000) torq.err_sum = 1000;
Lightvalve 56:6f50d9d3bfee 472 // if (torq.err_sum<-1000) torq.err_sum = -1000;
Lightvalve 56:6f50d9d3bfee 473
Lightvalve 56:6f50d9d3bfee 474
Lightvalve 56:6f50d9d3bfee 475 VALVE_POS_RAW_FORCE_FB = alpha_trans*(((float) P_GAIN_JOINT_TORQUE * torq.err + (float) I_GAIN_JOINT_TORQUE * torq.err_sum) /(float) TORQUE_SENSOR_PULSE_PER_TORQUE * 0.01f
Lightvalve 56:6f50d9d3bfee 476 + DDV_JOINT_POS_FF(vel.sen))+ (1.0f-alpha_trans) * (P_GAIN_JOINT_POSITION * 0.01f * pos.err /(float) ENC_PULSE_PER_POSITION + DDV_JOINT_POS_FF(vel.ref));
Lightvalve 56:6f50d9d3bfee 477
Lightvalve 56:6f50d9d3bfee 478 if (VALVE_POS_RAW_FORCE_FB >= 0) {
Lightvalve 56:6f50d9d3bfee 479 valve_pos.ref = VALVE_POS_RAW_FORCE_FB + VALVE_DEADZONE_PLUS;
Lightvalve 56:6f50d9d3bfee 480 } else {
Lightvalve 56:6f50d9d3bfee 481 valve_pos.ref = VALVE_POS_RAW_FORCE_FB + VALVE_DEADZONE_MINUS;
Lightvalve 56:6f50d9d3bfee 482 }
Lightvalve 56:6f50d9d3bfee 483
Lightvalve 56:6f50d9d3bfee 484 if(I_GAIN_JOINT_TORQUE != 0){
Lightvalve 56:6f50d9d3bfee 485 double Ka = 1.0f / (double) I_GAIN_JOINT_TORQUE * (float) TORQUE_SENSOR_PULSE_PER_TORQUE * 100.0f;
Lightvalve 56:6f50d9d3bfee 486 if(valve_pos.ref>VALVE_MAX_POS){
Lightvalve 56:6f50d9d3bfee 487 double valve_pos_rem = valve_pos.ref - VALVE_MAX_POS;
Lightvalve 56:6f50d9d3bfee 488 valve_pos_rem = valve_pos_rem * Ka;
Lightvalve 56:6f50d9d3bfee 489 valve_pos.ref = VALVE_MAX_POS;
Lightvalve 56:6f50d9d3bfee 490 torq.err_sum = torq.err_sum - valve_pos_rem/(float) TMR_FREQ_5k;
Lightvalve 56:6f50d9d3bfee 491 }
Lightvalve 56:6f50d9d3bfee 492 else if(valve_pos.ref < VALVE_MIN_POS){
Lightvalve 56:6f50d9d3bfee 493 double valve_pos_rem = valve_pos.ref - VALVE_MIN_POS;
Lightvalve 56:6f50d9d3bfee 494 valve_pos_rem = valve_pos_rem * Ka;
Lightvalve 56:6f50d9d3bfee 495 valve_pos.ref = VALVE_MIN_POS;
Lightvalve 56:6f50d9d3bfee 496 torq.err_sum = torq.err_sum - valve_pos_rem/(float) TMR_FREQ_5k;
Lightvalve 56:6f50d9d3bfee 497 }
Lightvalve 56:6f50d9d3bfee 498 }
Lightvalve 56:6f50d9d3bfee 499
Lightvalve 56:6f50d9d3bfee 500 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 56:6f50d9d3bfee 501
Lightvalve 56:6f50d9d3bfee 502 //TMR3_COUNT_TEST++;
Lightvalve 56:6f50d9d3bfee 503
Lightvalve 56:6f50d9d3bfee 504 break;
Lightvalve 56:6f50d9d3bfee 505 }
Lightvalve 56:6f50d9d3bfee 506
Lightvalve 56:6f50d9d3bfee 507
Lightvalve 56:6f50d9d3bfee 508 case MODE_VALVE_OPEN_LOOP: {
Lightvalve 56:6f50d9d3bfee 509 V_out = (float) Vout.ref;
Lightvalve 56:6f50d9d3bfee 510 break;
Lightvalve 56:6f50d9d3bfee 511 }
Lightvalve 56:6f50d9d3bfee 512
Lightvalve 56:6f50d9d3bfee 513 case MODE_JOINT_POSITION_TORQUE_CONTROL_VALVE_POSITION: {
Lightvalve 56:6f50d9d3bfee 514 float VALVE_POS_RAW_POS_FB = 0.0f; // Valve Position by Position Feedback
Lightvalve 56:6f50d9d3bfee 515 //float VALVE_POS_RAW_POS_FF = 0.0f; // Valve Position by Position Feedforward
Lightvalve 56:6f50d9d3bfee 516 float VALVE_POS_RAW_FORCE_FB = 0.0f; // Valve Position by Force Feedback
Lightvalve 56:6f50d9d3bfee 517 //int DDV_JOINT_CAN = 0;
Lightvalve 56:6f50d9d3bfee 518 // feedback input for position control
Lightvalve 56:6f50d9d3bfee 519 pos.err = pos.ref - (float) pos.sen;
Lightvalve 56:6f50d9d3bfee 520 pos.err_diff = pos.err - pos.err_old;
Lightvalve 56:6f50d9d3bfee 521 pos.err_old = pos.err;
Lightvalve 56:6f50d9d3bfee 522 pos.err_sum += pos.err;
Lightvalve 56:6f50d9d3bfee 523 if (pos.err_sum > 1000) pos.err_sum = 1000;
Lightvalve 56:6f50d9d3bfee 524 if (pos.err_sum<-1000) pos.err_sum = -1000;
Lightvalve 56:6f50d9d3bfee 525 VALVE_POS_RAW_POS_FB = (float) P_GAIN_JOINT_POSITION * 0.01f * pos.err/(float) ENC_PULSE_PER_POSITION + (float) I_GAIN_JOINT_POSITION * 0.01f * pos.err_sum/(float) ENC_PULSE_PER_POSITION + (float) D_GAIN_JOINT_POSITION * pos.err_diff/(float) ENC_PULSE_PER_POSITION;
Lightvalve 56:6f50d9d3bfee 526
Lightvalve 56:6f50d9d3bfee 527
Lightvalve 56:6f50d9d3bfee 528 //Ref_Joint_Vel = Ref_Vel_Test;
Lightvalve 56:6f50d9d3bfee 529 // feedforward input for position control
Lightvalve 56:6f50d9d3bfee 530 // float Ref_Joint_Vel_Act = Ref_Joint_Vel/(float)ENC_PULSE_PER_POSITION; // [pulse/s] >> [deg/s]
Lightvalve 56:6f50d9d3bfee 531 // float K_ff = 0.9f;
Lightvalve 56:6f50d9d3bfee 532 // if(Ref_Joint_Vel_Act > 0) K_ff = 0.90f; // open
Lightvalve 56:6f50d9d3bfee 533 // if(Ref_Joint_Vel_Act > 0) K_ff = 0.75f; // close
Lightvalve 56:6f50d9d3bfee 534 // VALVE_POS_RAW_POS_FF = K_ff*Ref_Joint_Vel_Act/0.50f;
Lightvalve 56:6f50d9d3bfee 535
Lightvalve 56:6f50d9d3bfee 536 //torque feedback
Lightvalve 56:6f50d9d3bfee 537 torq.err = - torq.ref + torq.sen;
Lightvalve 56:6f50d9d3bfee 538 torq.err_diff = torq.err - torq.err_old;
Lightvalve 56:6f50d9d3bfee 539 torq.err_old = torq.err;
Lightvalve 56:6f50d9d3bfee 540 torq.err_sum += torq.err;
Lightvalve 56:6f50d9d3bfee 541 if (torq.err_sum > 1000) torq.err_sum = 1000;
Lightvalve 56:6f50d9d3bfee 542 if (torq.err_sum<-1000) torq.err_sum = -1000;
Lightvalve 56:6f50d9d3bfee 543 VALVE_POS_RAW_FORCE_FB = (float) P_GAIN_JOINT_TORQUE * torq.err + (float) I_GAIN_JOINT_TORQUE * torq.err_sum + (float) D_GAIN_JOINT_TORQUE * torq.err_diff;
Lightvalve 56:6f50d9d3bfee 544 VALVE_POS_RAW_FORCE_FB = VALVE_POS_RAW_FORCE_FB * 0.01f;
Lightvalve 56:6f50d9d3bfee 545
Lightvalve 56:6f50d9d3bfee 546 // valve_pos.ref = VALVE_POS_RAW_POS_FB + DDV_JOINT_POS_FF(vel.ref) + VALVE_POS_RAW_FORCE_FB;
Lightvalve 56:6f50d9d3bfee 547 valve_pos.ref = VALVE_POS_RAW_POS_FB + DDV_JOINT_POS_FF(vel.ref);
Lightvalve 56:6f50d9d3bfee 548
Lightvalve 56:6f50d9d3bfee 549 if (valve_pos.ref >= 0) {
Lightvalve 56:6f50d9d3bfee 550 valve_pos.ref = valve_pos.ref + VALVE_DEADZONE_PLUS;
Lightvalve 56:6f50d9d3bfee 551 } else if(valve_pos.ref < 0) {
Lightvalve 56:6f50d9d3bfee 552 valve_pos.ref = valve_pos.ref + VALVE_DEADZONE_MINUS;
Lightvalve 56:6f50d9d3bfee 553 }
Lightvalve 56:6f50d9d3bfee 554 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 50:3c630b5eba9f 555
Lightvalve 14:8e7590227d22 556
Lightvalve 13:747daba9cf59 557 break;
Lightvalve 13:747daba9cf59 558 }
Lightvalve 14:8e7590227d22 559
Lightvalve 56:6f50d9d3bfee 560 // case MODE_VALVE_POSITION_TORQUE_CONTROL_LEARNING: {
Lightvalve 56:6f50d9d3bfee 561 //
Lightvalve 56:6f50d9d3bfee 562 // break;
Lightvalve 56:6f50d9d3bfee 563 // }
Lightvalve 56:6f50d9d3bfee 564
Lightvalve 56:6f50d9d3bfee 565 // case MODE_JOINT_POSITION_PRES_CONTROL_PWM: {
Lightvalve 56:6f50d9d3bfee 566 // pos.err = pos.ref - (float) pos.sen;
Lightvalve 56:6f50d9d3bfee 567 // pos.err_diff = pos.err - pos.err_old;
Lightvalve 56:6f50d9d3bfee 568 // pos.err_old = pos.err;
Lightvalve 56:6f50d9d3bfee 569 // pos.err_sum += pos.err;
Lightvalve 56:6f50d9d3bfee 570 // if (pos.err_sum > 1000) pos.err_sum = 1000;
Lightvalve 56:6f50d9d3bfee 571 // if (pos.err_sum<-1000) pos.err_sum = -1000;
Lightvalve 56:6f50d9d3bfee 572 // VALVE_PWM_RAW_POS = ((float) P_GAIN_JOINT_POSITION * pos.err + (float) I_GAIN_JOINT_POSITION * pos.err_sum + (float) D_GAIN_JOINT_POSITION * pos.err_diff)/(float) ENC_PULSE_PER_POSITION;
Lightvalve 56:6f50d9d3bfee 573 //
Lightvalve 56:6f50d9d3bfee 574 // torq.err = torq.ref - torq.sen;
Lightvalve 56:6f50d9d3bfee 575 // torq.err_diff = torq.err - torq.err_old;
Lightvalve 56:6f50d9d3bfee 576 // torq.err_old = torq.err;
Lightvalve 56:6f50d9d3bfee 577 // torq.err_sum += torq.err;
Lightvalve 56:6f50d9d3bfee 578 // if (torq.err_sum > 1000) torq.err_sum = 1000;
Lightvalve 56:6f50d9d3bfee 579 // if (torq.err_sum<-1000) torq.err_sum = -1000;
Lightvalve 56:6f50d9d3bfee 580 // VALVE_PWM_RAW_TORQ = (float) P_GAIN_JOINT_TORQUE * torq.err + (float) I_GAIN_JOINT_TORQUE * torq.err_sum + (float) D_GAIN_JOINT_TORQUE * torq.err_diff;
Lightvalve 56:6f50d9d3bfee 581 //
Lightvalve 56:6f50d9d3bfee 582 // VALVE_PWM_RAW_TORQ = VALVE_PWM_RAW_TORQ * 0.01f;
Lightvalve 56:6f50d9d3bfee 583 //
Lightvalve 56:6f50d9d3bfee 584 // V_out = VALVE_PWM_RAW_POS + (float) COMPLIANCE_GAIN * 0.01f * VALVE_PWM_RAW_TORQ;
Lightvalve 56:6f50d9d3bfee 585 //
Lightvalve 56:6f50d9d3bfee 586 // CUR_FLOWRATE = (float) CUR_VELOCITY * 0.00009587f;
Lightvalve 56:6f50d9d3bfee 587 // CUR_FLOWRATE = CUR_FLOWRATE * 0.5757f; // 0.4791=2*pi/65536*5000(pulse/tic to rad/s) 0.5757=0.02525*0.02*0.0095*2*60*1000 (radius * area * 2 * 60(sec --> min) * 1000(m^3 --> L))
Lightvalve 56:6f50d9d3bfee 588 // if (DIR_VALVE > 0) {
Lightvalve 56:6f50d9d3bfee 589 // if (CUR_FLOWRATE >= 0 && CUR_FLOWRATE < (VALVE_GAIN_LPM_PER_V[0]*1.0f)) VALVE_FF_VOLTAGE = (CUR_FLOWRATE - 0.0f) / (VALVE_GAIN_LPM_PER_V[0]*1.0f - 0.0f) + 0.0f;
Lightvalve 56:6f50d9d3bfee 590 // else if (CUR_FLOWRATE >= (VALVE_GAIN_LPM_PER_V[0]*1.0f) && CUR_FLOWRATE < (VALVE_GAIN_LPM_PER_V[2]*2.0f)) VALVE_FF_VOLTAGE = (CUR_FLOWRATE - VALVE_GAIN_LPM_PER_V[0]*1.0f) / (VALVE_GAIN_LPM_PER_V[2]*2.0f - VALVE_GAIN_LPM_PER_V[0]*1.) + 1.0f;
Lightvalve 56:6f50d9d3bfee 591 // else if (CUR_FLOWRATE >= (VALVE_GAIN_LPM_PER_V[2]*2.0f) && CUR_FLOWRATE < (VALVE_GAIN_LPM_PER_V[4]*3.0f)) VALVE_FF_VOLTAGE = (CUR_FLOWRATE - VALVE_GAIN_LPM_PER_V[2]*2.0f) / (VALVE_GAIN_LPM_PER_V[4]*3.0f - VALVE_GAIN_LPM_PER_V[2]*2.) + 2.0f;
Lightvalve 56:6f50d9d3bfee 592 // else if (CUR_FLOWRATE >= (VALVE_GAIN_LPM_PER_V[4]*3.0f) && CUR_FLOWRATE < (VALVE_GAIN_LPM_PER_V[6]*4.0f)) VALVE_FF_VOLTAGE = (CUR_FLOWRATE - VALVE_GAIN_LPM_PER_V[4]*3.0f) / (VALVE_GAIN_LPM_PER_V[6]*4.0f - VALVE_GAIN_LPM_PER_V[4]*3.) + 3.0f;
Lightvalve 56:6f50d9d3bfee 593 // else if (CUR_FLOWRATE >= (VALVE_GAIN_LPM_PER_V[6]*4.0f) && CUR_FLOWRATE < (VALVE_GAIN_LPM_PER_V[8]*5.0f)) VALVE_FF_VOLTAGE = (CUR_FLOWRATE - VALVE_GAIN_LPM_PER_V[6]*4.0f) / (VALVE_GAIN_LPM_PER_V[8]*5.0f - VALVE_GAIN_LPM_PER_V[6]*4.) + 4.0f;
Lightvalve 56:6f50d9d3bfee 594 // else if (CUR_FLOWRATE >= (VALVE_GAIN_LPM_PER_V[8]*5.0f)) VALVE_FF_VOLTAGE = 5.0f;
Lightvalve 56:6f50d9d3bfee 595 // else if (CUR_FLOWRATE >= (VALVE_GAIN_LPM_PER_V[1]*(-1.0f)) && CUR_FLOWRATE < 0.0f) VALVE_FF_VOLTAGE = (CUR_FLOWRATE - VALVE_GAIN_LPM_PER_V[1]*(-1.0f)) / (0.0f - VALVE_GAIN_LPM_PER_V[1]*(-1.)) - 1.0f;
Lightvalve 56:6f50d9d3bfee 596 // else if (CUR_FLOWRATE >= (VALVE_GAIN_LPM_PER_V[3]*(-2.0f)) && CUR_FLOWRATE < (VALVE_GAIN_LPM_PER_V[1]*(-1.0f))) VALVE_FF_VOLTAGE = (CUR_FLOWRATE - VALVE_GAIN_LPM_PER_V[3]*(-2.0f)) / ((VALVE_GAIN_LPM_PER_V[1]*(-1.0f)) - VALVE_GAIN_LPM_PER_V[3]*(-2.)) - 2.0f;
Lightvalve 56:6f50d9d3bfee 597 // else if (CUR_FLOWRATE >= (VALVE_GAIN_LPM_PER_V[5]*(-3.0f)) && CUR_FLOWRATE < (VALVE_GAIN_LPM_PER_V[3]*(-2.0f))) VALVE_FF_VOLTAGE = (CUR_FLOWRATE - VALVE_GAIN_LPM_PER_V[5]*(-3.0f)) / ((VALVE_GAIN_LPM_PER_V[3]*(-2.0f)) - VALVE_GAIN_LPM_PER_V[5]*(-3.)) - 3.0f;
Lightvalve 56:6f50d9d3bfee 598 // else if (CUR_FLOWRATE >= (VALVE_GAIN_LPM_PER_V[7]*(-4.0f)) && CUR_FLOWRATE < (VALVE_GAIN_LPM_PER_V[5]*(-3.0f))) VALVE_FF_VOLTAGE = (CUR_FLOWRATE - VALVE_GAIN_LPM_PER_V[7]*(-4.0f)) / ((VALVE_GAIN_LPM_PER_V[5]*(-3.0f)) - VALVE_GAIN_LPM_PER_V[7]*(-4.)) - 4.0f;
Lightvalve 56:6f50d9d3bfee 599 // else if (CUR_FLOWRATE >= (VALVE_GAIN_LPM_PER_V[9]*(-5.0f)) && CUR_FLOWRATE < (VALVE_GAIN_LPM_PER_V[7]*(-4.0f))) VALVE_FF_VOLTAGE = (CUR_FLOWRATE - VALVE_GAIN_LPM_PER_V[9]*(-5.0f)) / ((VALVE_GAIN_LPM_PER_V[7]*(-4.0f)) - VALVE_GAIN_LPM_PER_V[9]*(-5.)) - 5.0f;
Lightvalve 56:6f50d9d3bfee 600 // else if (CUR_FLOWRATE < (VALVE_GAIN_LPM_PER_V[9]*(-5.0f))) VALVE_FF_VOLTAGE = -5;
Lightvalve 56:6f50d9d3bfee 601 // else VALVE_FF_VOLTAGE = 0;
Lightvalve 56:6f50d9d3bfee 602 // } else {
Lightvalve 56:6f50d9d3bfee 603 // if (CUR_FLOWRATE >= 0 && CUR_FLOWRATE < (VALVE_GAIN_LPM_PER_V[0]*1.0f)) VALVE_FF_VOLTAGE = (CUR_FLOWRATE - 0.0f) / (VALVE_GAIN_LPM_PER_V[0]*1.0f - 0.0f) + 0.0f;
Lightvalve 56:6f50d9d3bfee 604 // else if (CUR_FLOWRATE >= (VALVE_GAIN_LPM_PER_V[1]*1.0f) && CUR_FLOWRATE < (VALVE_GAIN_LPM_PER_V[2]*2.0f)) VALVE_FF_VOLTAGE = (CUR_FLOWRATE - VALVE_GAIN_LPM_PER_V[0]*1.0f) / (VALVE_GAIN_LPM_PER_V[2]*2.0f - VALVE_GAIN_LPM_PER_V[0]*1.0f) + 1.0f;
Lightvalve 56:6f50d9d3bfee 605 // else if (CUR_FLOWRATE >= (VALVE_GAIN_LPM_PER_V[3]*2.0f) && CUR_FLOWRATE < (VALVE_GAIN_LPM_PER_V[4]*3.0f)) VALVE_FF_VOLTAGE = (CUR_FLOWRATE - VALVE_GAIN_LPM_PER_V[2]*2.0f) / (VALVE_GAIN_LPM_PER_V[4]*3.0f - VALVE_GAIN_LPM_PER_V[2]*2.0f) + 2.0f;
Lightvalve 56:6f50d9d3bfee 606 // else if (CUR_FLOWRATE >= (VALVE_GAIN_LPM_PER_V[5]*3.0f) && CUR_FLOWRATE < (VALVE_GAIN_LPM_PER_V[6]*4.0f)) VALVE_FF_VOLTAGE = (CUR_FLOWRATE - VALVE_GAIN_LPM_PER_V[4]*3.0f) / (VALVE_GAIN_LPM_PER_V[6]*4.0f - VALVE_GAIN_LPM_PER_V[4]*3.0f) + 3.0f;
Lightvalve 56:6f50d9d3bfee 607 // else if (CUR_FLOWRATE >= (VALVE_GAIN_LPM_PER_V[7]*4.0f) && CUR_FLOWRATE < (VALVE_GAIN_LPM_PER_V[8]*5.0f)) VALVE_FF_VOLTAGE = (CUR_FLOWRATE - VALVE_GAIN_LPM_PER_V[6]*4.0f) / (VALVE_GAIN_LPM_PER_V[8]*5.0f - VALVE_GAIN_LPM_PER_V[6]*4.0f) + 4.0f;
Lightvalve 56:6f50d9d3bfee 608 // else if (CUR_FLOWRATE >= (VALVE_GAIN_LPM_PER_V[9]*5.0f)) VALVE_FF_VOLTAGE = 5.0f;
Lightvalve 56:6f50d9d3bfee 609 // else if (CUR_FLOWRATE >= (VALVE_GAIN_LPM_PER_V[0]*(-1.0f)) && CUR_FLOWRATE < 0.0f) VALVE_FF_VOLTAGE = (CUR_FLOWRATE - VALVE_GAIN_LPM_PER_V[1]*(-1.0f)) / (0.0f - VALVE_GAIN_LPM_PER_V[1]*(-1.0f)) - 1.0f;
Lightvalve 56:6f50d9d3bfee 610 // else if (CUR_FLOWRATE >= (VALVE_GAIN_LPM_PER_V[2]*(-2.0f)) && CUR_FLOWRATE < (VALVE_GAIN_LPM_PER_V[1]*(-1.0f))) VALVE_FF_VOLTAGE = (CUR_FLOWRATE - VALVE_GAIN_LPM_PER_V[3]*(-2.0f)) / ((VALVE_GAIN_LPM_PER_V[1]*(-1.0f)) - VALVE_GAIN_LPM_PER_V[3]*(-2.0f)) - 2.0f;
Lightvalve 56:6f50d9d3bfee 611 // else if (CUR_FLOWRATE >= (VALVE_GAIN_LPM_PER_V[4]*(-3.0f)) && CUR_FLOWRATE < (VALVE_GAIN_LPM_PER_V[3]*(-2.0f))) VALVE_FF_VOLTAGE = (CUR_FLOWRATE - VALVE_GAIN_LPM_PER_V[5]*(-3.0f)) / ((VALVE_GAIN_LPM_PER_V[3]*(-2.0f)) - VALVE_GAIN_LPM_PER_V[5]*(-3.0f)) - 3.0f;
Lightvalve 56:6f50d9d3bfee 612 // else if (CUR_FLOWRATE >= (VALVE_GAIN_LPM_PER_V[6]*(-4.0f)) && CUR_FLOWRATE < (VALVE_GAIN_LPM_PER_V[5]*(-3.0f))) VALVE_FF_VOLTAGE = (CUR_FLOWRATE - VALVE_GAIN_LPM_PER_V[7]*(-4.0f)) / ((VALVE_GAIN_LPM_PER_V[5]*(-3.0f)) - VALVE_GAIN_LPM_PER_V[7]*(-4.0f)) - 4.0f;
Lightvalve 56:6f50d9d3bfee 613 // else if (CUR_FLOWRATE >= (VALVE_GAIN_LPM_PER_V[8]*(-5.0f)) && CUR_FLOWRATE < (VALVE_GAIN_LPM_PER_V[7]*(-4.0f))) VALVE_FF_VOLTAGE = (CUR_FLOWRATE - VALVE_GAIN_LPM_PER_V[9]*(-5.0f)) / ((VALVE_GAIN_LPM_PER_V[7]*(-4.0f)) - VALVE_GAIN_LPM_PER_V[9]*(-5.0f)) - 5.0f;
Lightvalve 56:6f50d9d3bfee 614 // else if (CUR_FLOWRATE < (VALVE_GAIN_LPM_PER_V[8]*(-5.0f))) VALVE_FF_VOLTAGE = -5;
Lightvalve 56:6f50d9d3bfee 615 // else VALVE_FF_VOLTAGE = 0;
Lightvalve 56:6f50d9d3bfee 616 // }
Lightvalve 56:6f50d9d3bfee 617 // // VALVE_FF_VOLTAGE = CUR_FLOWRATE * 0.5f;
Lightvalve 56:6f50d9d3bfee 618 //
Lightvalve 56:6f50d9d3bfee 619 // if (CUR_FLOWRATE >= 0) VALVE_FF_VOLTAGE = (float) VELOCITY_COMP_GAIN * 0.001f * VALVE_FF_VOLTAGE * sqrt((float) PRES_SUPPLY - CUR_PRES_A_BAR) * 0.0707f; // 0.0707 = 1/sqrt(200.))
Lightvalve 56:6f50d9d3bfee 620 // else if (CUR_FLOWRATE < 0) VALVE_FF_VOLTAGE = (float) VELOCITY_COMP_GAIN * 0.001f * VALVE_FF_VOLTAGE * sqrt((float) PRES_SUPPLY - CUR_PRES_B_BAR) * 0.0707f;
Lightvalve 56:6f50d9d3bfee 621 //
Lightvalve 56:6f50d9d3bfee 622 // V_out = V_out + VALVE_FF_VOLTAGE;
Lightvalve 56:6f50d9d3bfee 623 // break;
Lightvalve 56:6f50d9d3bfee 624 // }
Lightvalve 56:6f50d9d3bfee 625
Lightvalve 56:6f50d9d3bfee 626 // case MODE_JOINT_POSITION_PRES_CONTROL_VALVE_POSITION: {
Lightvalve 56:6f50d9d3bfee 627 //
Lightvalve 56:6f50d9d3bfee 628 // pos.err = pos.ref - (float) pos.sen;
Lightvalve 56:6f50d9d3bfee 629 // pos.err_diff = pos.err - pos.err_old;
Lightvalve 56:6f50d9d3bfee 630 // pos.err_old = pos.err;
Lightvalve 56:6f50d9d3bfee 631 // pos.err_sum += pos.err;
Lightvalve 56:6f50d9d3bfee 632 // if (pos.err_sum > 1000) pos.err_sum = 1000;
Lightvalve 56:6f50d9d3bfee 633 // if (pos.err_sum<-1000) pos.err_sum = -1000;
Lightvalve 56:6f50d9d3bfee 634 // VALVE_PWM_RAW_POS = ((float) P_GAIN_JOINT_POSITION * pos.err + (float) I_GAIN_JOINT_POSITION * pos.err_sum + (float) D_GAIN_JOINT_POSITION * pos.err_diff) * 0.01f;
Lightvalve 56:6f50d9d3bfee 635 //
Lightvalve 56:6f50d9d3bfee 636 // torq.err = torq.ref - torq.sen;
Lightvalve 56:6f50d9d3bfee 637 // torq.err_diff = torq.err - torq.err_old;
Lightvalve 56:6f50d9d3bfee 638 // torq.err_old = torq.err;
Lightvalve 56:6f50d9d3bfee 639 // torq.err_sum += torq.err;
Lightvalve 56:6f50d9d3bfee 640 // if (torq.err_sum > 1000) torq.err_sum = 1000;
Lightvalve 56:6f50d9d3bfee 641 // if (torq.err_sum<-1000) torq.err_sum = -1000;
Lightvalve 56:6f50d9d3bfee 642 // VALVE_PWM_RAW_TORQ = (float) P_GAIN_JOINT_TORQUE * torq.err + (float) I_GAIN_JOINT_TORQUE * torq.err_sum + (float) D_GAIN_JOINT_TORQUE * torq.err_diff;
Lightvalve 56:6f50d9d3bfee 643 //
Lightvalve 56:6f50d9d3bfee 644 // valve_pos.ref = VALVE_PWM_RAW_POS + VALVE_PWM_RAW_TORQ;
Lightvalve 56:6f50d9d3bfee 645 // VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 56:6f50d9d3bfee 646 //
Lightvalve 56:6f50d9d3bfee 647 // break;
Lightvalve 56:6f50d9d3bfee 648 // }
Lightvalve 56:6f50d9d3bfee 649
Lightvalve 56:6f50d9d3bfee 650 // case MODE_VALVE_POSITION_PRES_CONTROL_LEARNING: {
Lightvalve 56:6f50d9d3bfee 651 //
Lightvalve 56:6f50d9d3bfee 652 // break;
Lightvalve 56:6f50d9d3bfee 653 // }
Lightvalve 56:6f50d9d3bfee 654
Lightvalve 56:6f50d9d3bfee 655
Lightvalve 56:6f50d9d3bfee 656 // case MODE_TEST_CURRENT_CONTROL: {
Lightvalve 56:6f50d9d3bfee 657 // if (TMR3_COUNT_IREF == TMR_FREQ_5k) {
Lightvalve 56:6f50d9d3bfee 658 // TMR3_COUNT_IREF = 0;
Lightvalve 56:6f50d9d3bfee 659 // }
Lightvalve 56:6f50d9d3bfee 660 // TMR3_COUNT_IREF++;
Lightvalve 56:6f50d9d3bfee 661 //
Lightvalve 56:6f50d9d3bfee 662 // // Set Current Reference
Lightvalve 56:6f50d9d3bfee 663 // float TMR3_CNT_MAX = (float)TMR_FREQ_5k/2.0f;
Lightvalve 56:6f50d9d3bfee 664 // float I_REF_MID = 0.0f;
Lightvalve 56:6f50d9d3bfee 665 // if (TMR3_COUNT_IREF < TMR3_CNT_MAX) {
Lightvalve 56:6f50d9d3bfee 666 // I_REF = I_REF_MID + 1.0f;
Lightvalve 56:6f50d9d3bfee 667 // } else {
Lightvalve 56:6f50d9d3bfee 668 // I_REF = I_REF_MID - 1.0f;
Lightvalve 56:6f50d9d3bfee 669 // }
Lightvalve 56:6f50d9d3bfee 670 //// float T = 1.0; // wave period
Lightvalve 56:6f50d9d3bfee 671 //// I_REF = (5. * sin(2. * 3.1415 * (float) TMR3_COUNT_IREF / (float)TMR_FREQ_5k/ T));
Lightvalve 56:6f50d9d3bfee 672 //// I_REF = (2.0 * sin(2. * 2. * 3.14 * (float) TMR3_COUNT_IREF / 5000.)+(2.0 * sin(2. * 1. * 3.14 * (float)TMR3_COUNT_IREF/ 5000.))+(2.0 * sin(2. * 5. * 3.14 * (float)TMR3_COUNT_IREF/ 5000.))+(2.0 * sin(2. * 10. * 3.14 * (float)TMR3_COUNT_IREF/ 5000.)));
Lightvalve 56:6f50d9d3bfee 673 //
Lightvalve 56:6f50d9d3bfee 674 // if (TMR3_COUNT_IREF % (int) (TMR_FREQ_5k / CAN_FREQ) == 0) {
Lightvalve 56:6f50d9d3bfee 675 // //CAN_TX_PRES((int16_t)(I_REF*1000.0), (int16_t) (CUR_CURRENT*1000.0)); // to check the datas
Lightvalve 56:6f50d9d3bfee 676 // }
Lightvalve 56:6f50d9d3bfee 677 // break;
Lightvalve 56:6f50d9d3bfee 678 // }
Lightvalve 56:6f50d9d3bfee 679
Lightvalve 56:6f50d9d3bfee 680 // case MODE_TEST_PWM_CONTROL: {
Lightvalve 56:6f50d9d3bfee 681 // if (TMR3_COUNT_IREF == TMR_FREQ_5k) {
Lightvalve 56:6f50d9d3bfee 682 // TMR3_COUNT_IREF = 0;
Lightvalve 56:6f50d9d3bfee 683 // }
Lightvalve 56:6f50d9d3bfee 684 // TMR3_COUNT_IREF++;
Lightvalve 56:6f50d9d3bfee 685 //
Lightvalve 56:6f50d9d3bfee 686 // // Set PWM reference
Lightvalve 56:6f50d9d3bfee 687 // float TMR3_CNT_MAX = (float)TMR_FREQ_5k/2.0f;
Lightvalve 56:6f50d9d3bfee 688 // //float I_REF_MID = 0.0f;
Lightvalve 56:6f50d9d3bfee 689 // if (TMR3_COUNT_IREF < TMR3_CNT_MAX) {
Lightvalve 56:6f50d9d3bfee 690 // CUR_PWM = 1000;
Lightvalve 56:6f50d9d3bfee 691 // } else {
Lightvalve 56:6f50d9d3bfee 692 // CUR_PWM = -1000;
Lightvalve 56:6f50d9d3bfee 693 // }
Lightvalve 56:6f50d9d3bfee 694 //
Lightvalve 56:6f50d9d3bfee 695 // if (TMR3_COUNT_IREF % (int) (TMR_FREQ_5k / CAN_FREQ) == 0) {
Lightvalve 56:6f50d9d3bfee 696 // //CAN_TX_PRES((int16_t)(u_CUR[0]*1000.0f), (int16_t) (CUR_CURRENT*1000.0f)); // to check the datas
Lightvalve 56:6f50d9d3bfee 697 // }
Lightvalve 56:6f50d9d3bfee 698 // break;
Lightvalve 56:6f50d9d3bfee 699 // }
Lightvalve 56:6f50d9d3bfee 700
Lightvalve 56:6f50d9d3bfee 701
Lightvalve 56:6f50d9d3bfee 702 // case MODE_CURRENT_CONTROL: {
Lightvalve 56:6f50d9d3bfee 703 //
Lightvalve 56:6f50d9d3bfee 704 // cur.ref = cur.ref; // Unit : mA
Lightvalve 56:6f50d9d3bfee 705 // CurrentControl();
Lightvalve 56:6f50d9d3bfee 706 // break;
Lightvalve 56:6f50d9d3bfee 707 // }
Lightvalve 56:6f50d9d3bfee 708
Lightvalve 56:6f50d9d3bfee 709 // case MODE_JOINT_POSITION_TORQUE_CONTROL_CURRENT: {
Lightvalve 56:6f50d9d3bfee 710 // float I_REF_POS_FB = 0.0f; // I_REF by Position Feedback
Lightvalve 56:6f50d9d3bfee 711 // float I_REF_POS_FF = 0.0f; // I_REF by Position Feedforward
Lightvalve 56:6f50d9d3bfee 712 // float I_REF_FORCE_FB = 0.0f; // I_REF by Force Feedback
Lightvalve 56:6f50d9d3bfee 713 // float I_REF_FORCE_FF = 0.0f; // I_REF by Force Feedforward
Lightvalve 56:6f50d9d3bfee 714 //
Lightvalve 56:6f50d9d3bfee 715 // // feedback input for position control
Lightvalve 56:6f50d9d3bfee 716 // pos.err = pos.ref - pos.sen;
Lightvalve 56:6f50d9d3bfee 717 // float alpha_update_vel = 1.0f/(1.0f+(float)FREQ_TMR4/(2.0f*3.1415f*50.0f)); // f_cutoff : 50Hz
Lightvalve 56:6f50d9d3bfee 718 // float err_diff = (pos.err - pos.err_old)*(float)FREQ_5k;
Lightvalve 56:6f50d9d3bfee 719 // pos.err_diff = (1.0f-alpha_update_vel)*pos.err_diff + alpha_update_vel*err_diff;
Lightvalve 56:6f50d9d3bfee 720 // pos.err_old = pos.err;
Lightvalve 56:6f50d9d3bfee 721 // I_REF_POS_FB = 0.001f*((float)P_GAIN_JOINT_POSITION * pos.err + (float)D_GAIN_JOINT_POSITION * pos.err_diff * 0.1f);
Lightvalve 56:6f50d9d3bfee 722 //
Lightvalve 56:6f50d9d3bfee 723 // // feedforward input for position control
Lightvalve 56:6f50d9d3bfee 724 // float Vel_Act_Ref = vel.ref; // [pulse/s] >> [deg/s]
Lightvalve 56:6f50d9d3bfee 725 // float K_ff = 1.3f;
Lightvalve 56:6f50d9d3bfee 726 // float K_v = 0.0f;
Lightvalve 56:6f50d9d3bfee 727 // if(Vel_Act_Ref > 0) K_v = 1.0f/100.0f; // open, tuning. (deg/s >> mA)
Lightvalve 56:6f50d9d3bfee 728 // if(Vel_Act_Ref < 0) K_v = 1.0f/100.0f; // close, tuning. (deg/s >> mA)
Lightvalve 56:6f50d9d3bfee 729 // I_REF_POS_FF = K_ff*K_v*Vel_Act_Ref;
Lightvalve 56:6f50d9d3bfee 730 //
Lightvalve 56:6f50d9d3bfee 731 // // feedback input for position control
Lightvalve 56:6f50d9d3bfee 732 // I_REF_FORCE_FB = 0.0f;
Lightvalve 56:6f50d9d3bfee 733 //
Lightvalve 56:6f50d9d3bfee 734 // // feedforward input for position control
Lightvalve 56:6f50d9d3bfee 735 // I_REF_FORCE_FF = 0.0f;
Lightvalve 56:6f50d9d3bfee 736 //
Lightvalve 56:6f50d9d3bfee 737 // cur.ref = I_REF_POS_FF + I_REF_POS_FB + I_REF_FORCE_FB + I_REF_FORCE_FF;
Lightvalve 56:6f50d9d3bfee 738 //
Lightvalve 56:6f50d9d3bfee 739 // CurrentControl();
Lightvalve 56:6f50d9d3bfee 740 //
Lightvalve 56:6f50d9d3bfee 741 // break;
Lightvalve 56:6f50d9d3bfee 742 // }
Lightvalve 56:6f50d9d3bfee 743
Lightvalve 56:6f50d9d3bfee 744 // case MODE_JOINT_POSITION_PRES_CONTROL_CURRENT: {
Lightvalve 56:6f50d9d3bfee 745 // //float T_REF = 0.0; // Torque Reference
Lightvalve 56:6f50d9d3bfee 746 // float I_REF_FORCE_FB = 0.; // I_REF by Force Feedback
Lightvalve 56:6f50d9d3bfee 747 // float I_REF_VC = 0.; // I_REF for velocity compensation
Lightvalve 56:6f50d9d3bfee 748 //
Lightvalve 56:6f50d9d3bfee 749 // // feedback input for position control
Lightvalve 56:6f50d9d3bfee 750 // //float Joint_Pos_Err = 34.0f-(float) pos.sen/(float)ENC_PULSE_PER_POSITION; // [pulse/s] >> [deg/s]
Lightvalve 56:6f50d9d3bfee 751 // //float Joint_Vel_Err = 0.0f-(float) vel.sen/(float)ENC_PULSE_PER_POSITION; // [pulse/s] >> [deg/s]
Lightvalve 56:6f50d9d3bfee 752 // //float K_spring = 0.7f;
Lightvalve 56:6f50d9d3bfee 753 // //float D_damper = 0.02f;
Lightvalve 56:6f50d9d3bfee 754 //// T_REF = K_spring * pos.err + D_damper * Joint_Vel_Err; // unit : Nm
Lightvalve 56:6f50d9d3bfee 755 //
Lightvalve 56:6f50d9d3bfee 756 // // torque feedback
Lightvalve 56:6f50d9d3bfee 757 // torq.err = torq.ref - torq.sen;
Lightvalve 56:6f50d9d3bfee 758 // // torq.err_diff = torq.err - torq.err_old;
Lightvalve 56:6f50d9d3bfee 759 // // torq.err_old = torq.err;
Lightvalve 56:6f50d9d3bfee 760 // torq.err_sum = torq.err_sum + torq.err/(float)TMR_FREQ_5k;
Lightvalve 56:6f50d9d3bfee 761 // I_REF_FORCE_FB = 0.001f*((float)P_GAIN_JOINT_TORQUE * torq.err + (float)I_GAIN_JOINT_TORQUE * torq.err_sum);
Lightvalve 56:6f50d9d3bfee 762 //
Lightvalve 56:6f50d9d3bfee 763 // // velocity compensation for torque control
Lightvalve 56:6f50d9d3bfee 764 // float Joint_Vel_Act = vel.sen/(float)ENC_PULSE_PER_POSITION; // [pulse/s] >> [deg/s]
Lightvalve 56:6f50d9d3bfee 765 // float K_vc = 1.5f; // Velocity comp. gain
Lightvalve 56:6f50d9d3bfee 766 // float K_v = 0.0f; // Valve gain
Lightvalve 56:6f50d9d3bfee 767 // if(Joint_Vel_Act > 0) K_v = 1.0f/100.0f; // open, tuning
Lightvalve 56:6f50d9d3bfee 768 // if(Joint_Vel_Act < 0) K_v = 1.0f/100.0f; // close, tuning
Lightvalve 56:6f50d9d3bfee 769 // I_REF_VC = K_vc*K_v*Joint_Vel_Act;
Lightvalve 56:6f50d9d3bfee 770 //
Lightvalve 56:6f50d9d3bfee 771 // cur.ref = I_REF_VC + I_REF_FORCE_FB;
Lightvalve 56:6f50d9d3bfee 772 // // cur.ref = I_REF_FORCE_FB;
Lightvalve 56:6f50d9d3bfee 773 //
Lightvalve 56:6f50d9d3bfee 774 // float I_MAX = 10.0f; // Maximum Current : 10mV
Lightvalve 56:6f50d9d3bfee 775 // float Ka = 1.0f/I_GAIN_JOINT_TORQUE;
Lightvalve 56:6f50d9d3bfee 776 // if(cur.ref > I_MAX) {
Lightvalve 56:6f50d9d3bfee 777 // float I_rem = cur.ref-I_MAX;
Lightvalve 56:6f50d9d3bfee 778 // I_rem = Ka*I_rem;
Lightvalve 56:6f50d9d3bfee 779 // cur.ref = I_MAX;
Lightvalve 56:6f50d9d3bfee 780 // torq.err_sum = torq.err_sum - I_rem/(float)TMR_FREQ_5k;
Lightvalve 56:6f50d9d3bfee 781 // } else if(cur.ref < -I_MAX) {
Lightvalve 56:6f50d9d3bfee 782 // float I_rem = cur.ref-(-I_MAX);
Lightvalve 56:6f50d9d3bfee 783 // I_rem = Ka*I_rem;
Lightvalve 56:6f50d9d3bfee 784 // cur.ref = -I_MAX;
Lightvalve 56:6f50d9d3bfee 785 // torq.err_sum = torq.err_sum - I_rem/(float)TMR_FREQ_5k;
Lightvalve 56:6f50d9d3bfee 786 // }
Lightvalve 56:6f50d9d3bfee 787 //
Lightvalve 56:6f50d9d3bfee 788 // CurrentControl();
Lightvalve 56:6f50d9d3bfee 789 //
Lightvalve 56:6f50d9d3bfee 790 //
Lightvalve 56:6f50d9d3bfee 791 // /*
Lightvalve 56:6f50d9d3bfee 792 // float I_REF_POS_FB = 0.0f; // I_REF by Position Feedback
Lightvalve 56:6f50d9d3bfee 793 // float I_REF_POS_FF = 0.0f; // I_REF by Position Feedforward
Lightvalve 56:6f50d9d3bfee 794 // float I_REF_FORCE_FB = 0.0f; // I_REF by Force Feedback
Lightvalve 56:6f50d9d3bfee 795 // float I_REF_FORCE_FF = 0.0f; // I_REF by Force Feedforward
Lightvalve 56:6f50d9d3bfee 796 //
Lightvalve 56:6f50d9d3bfee 797 // // feedback input for position control
Lightvalve 56:6f50d9d3bfee 798 // pos.err = pos.ref - pos.sen;
Lightvalve 56:6f50d9d3bfee 799 // float alpha_update_vel = 1.0f/(1.0f+(float)FREQ_TMR4/(2.0f*3.1415f*50.0f)); // f_cutoff : 50Hz
Lightvalve 56:6f50d9d3bfee 800 // float err_diff = (pos.err - pos.err_old)*(float)FREQ_5k;
Lightvalve 56:6f50d9d3bfee 801 // pos.err_diff = (1.0f-alpha_update_vel)*pos.err_diff + alpha_update_vel*err_diff;
Lightvalve 56:6f50d9d3bfee 802 // pos.err_old = pos.err;
Lightvalve 56:6f50d9d3bfee 803 // I_REF_POS_FB = 0.001f*((float)P_GAIN_JOINT_POSITION * pos.err + (float)D_GAIN_JOINT_POSITION * pos.err_diff * 0.1f);
Lightvalve 56:6f50d9d3bfee 804 //
Lightvalve 56:6f50d9d3bfee 805 // // feedforward input for position control
Lightvalve 56:6f50d9d3bfee 806 // float Vel_Act_Ref = vel.ref; // [pulse/s] >> [deg/s]
Lightvalve 56:6f50d9d3bfee 807 // float K_ff = 1.3f;
Lightvalve 56:6f50d9d3bfee 808 // float K_v = 0.0f;
Lightvalve 56:6f50d9d3bfee 809 // if(Vel_Act_Ref > 0) K_v = 1.0f/100.0f; // open, tuning. (deg/s >> mA)
Lightvalve 56:6f50d9d3bfee 810 // if(Vel_Act_Ref < 0) K_v = 1.0f/100.0f; // close, tuning. (deg/s >> mA)
Lightvalve 56:6f50d9d3bfee 811 // I_REF_POS_FF = K_ff*K_v*Vel_Act_Ref;
Lightvalve 56:6f50d9d3bfee 812 //
Lightvalve 56:6f50d9d3bfee 813 // // feedback input for position control
Lightvalve 56:6f50d9d3bfee 814 // I_REF_FORCE_FB = 0.0f;
Lightvalve 56:6f50d9d3bfee 815 //
Lightvalve 56:6f50d9d3bfee 816 // // feedforward input for position control
Lightvalve 56:6f50d9d3bfee 817 // I_REF_FORCE_FF = 0.0f;
Lightvalve 56:6f50d9d3bfee 818 //
Lightvalve 56:6f50d9d3bfee 819 // cur.ref = I_REF_POS_FF + I_REF_POS_FB + I_REF_FORCE_FB + I_REF_FORCE_FF;
Lightvalve 56:6f50d9d3bfee 820 //
Lightvalve 56:6f50d9d3bfee 821 // CurrentControl();
Lightvalve 56:6f50d9d3bfee 822 // */
Lightvalve 56:6f50d9d3bfee 823 //
Lightvalve 56:6f50d9d3bfee 824 // break;
Lightvalve 56:6f50d9d3bfee 825 // }
Lightvalve 56:6f50d9d3bfee 826
Lightvalve 14:8e7590227d22 827 case MODE_TORQUE_SENSOR_NULLING: {
Lightvalve 13:747daba9cf59 828 // DAC Voltage reference set
Lightvalve 14:8e7590227d22 829 if (TMR3_COUNT_TORQUE_NULL < TMR_FREQ_5k * 2) {
Lightvalve 15:bd0d12728506 830 CUR_TORQUE_sum += torq.sen;
Lightvalve 19:23b7c1ad8683 831
Lightvalve 14:8e7590227d22 832 if (TMR3_COUNT_TORQUE_NULL % 10 == 0) {
Lightvalve 30:8d561f16383b 833 CUR_TORQUE_mean = CUR_TORQUE_sum / 10.0f;
Lightvalve 13:747daba9cf59 834 CUR_TORQUE_sum = 0;
Lightvalve 14:8e7590227d22 835
Lightvalve 43:b084e5f5d0d5 836 TORQUE_VREF += 0.0003 * (0.0f - CUR_TORQUE_mean);
Lightvalve 19:23b7c1ad8683 837
Lightvalve 30:8d561f16383b 838 if (TORQUE_VREF > 3.3f) TORQUE_VREF = 3.3f;
Lightvalve 13:747daba9cf59 839 if (TORQUE_VREF < 0) TORQUE_VREF = 0;
Lightvalve 50:3c630b5eba9f 840
Lightvalve 16:903b5a4433b4 841 //spi_eeprom_write(RID_TORQUE_SENSOR_VREF, (int16_t) (TORQUE_VREF * 1000.0));
Lightvalve 30:8d561f16383b 842 dac_1 = TORQUE_VREF / 3.3f;
Lightvalve 13:747daba9cf59 843 }
Lightvalve 13:747daba9cf59 844 } else {
Lightvalve 13:747daba9cf59 845 CONTROL_MODE = MODE_NO_ACT;
Lightvalve 14:8e7590227d22 846 TMR3_COUNT_TORQUE_NULL = 0;
Lightvalve 13:747daba9cf59 847 CUR_TORQUE_sum = 0;
Lightvalve 13:747daba9cf59 848 CUR_TORQUE_mean = 0;
Lightvalve 19:23b7c1ad8683 849
Lightvalve 16:903b5a4433b4 850 ROM_RESET_DATA();
Lightvalve 19:23b7c1ad8683 851
Lightvalve 56:6f50d9d3bfee 852 //spi_eeprom_write(RID_TORQUE_SENSOR_VREF, (int16_t) (TORQUE_VREF * 1000.0));
Lightvalve 56:6f50d9d3bfee 853
Lightvalve 56:6f50d9d3bfee 854 //pc.printf("%f\n", TORQUE_VREF);
Lightvalve 30:8d561f16383b 855 dac_1 = TORQUE_VREF / 3.3f;
Lightvalve 14:8e7590227d22 856
Lightvalve 13:747daba9cf59 857 }
Lightvalve 14:8e7590227d22 858 TMR3_COUNT_TORQUE_NULL++;
Lightvalve 13:747daba9cf59 859 break;
Lightvalve 56:6f50d9d3bfee 860
Lightvalve 56:6f50d9d3bfee 861
Lightvalve 56:6f50d9d3bfee 862
Lightvalve 56:6f50d9d3bfee 863 // // DAC Voltage reference set
Lightvalve 56:6f50d9d3bfee 864 // if (TMR3_COUNT_PRES_NULL < TMR_FREQ_5k * 2) {
Lightvalve 56:6f50d9d3bfee 865 // CUR_PRES_A_sum += pres_A.sen;
Lightvalve 56:6f50d9d3bfee 866 // CUR_PRES_B_sum += pres_B.sen;
Lightvalve 56:6f50d9d3bfee 867 //
Lightvalve 56:6f50d9d3bfee 868 // if (TMR3_COUNT_PRES_NULL % 10 == 0) {
Lightvalve 56:6f50d9d3bfee 869 // CUR_PRES_A_mean = CUR_PRES_A_sum / 10.0f;
Lightvalve 56:6f50d9d3bfee 870 // CUR_PRES_B_mean = CUR_PRES_B_sum / 10.0f;
Lightvalve 56:6f50d9d3bfee 871 // CUR_PRES_A_sum = 0;
Lightvalve 56:6f50d9d3bfee 872 // CUR_PRES_B_sum = 0;
Lightvalve 56:6f50d9d3bfee 873 //
Lightvalve 56:6f50d9d3bfee 874 // float VREF_NullingGain = 0.0003f;
Lightvalve 56:6f50d9d3bfee 875 // PRES_A_VREF = PRES_A_VREF - VREF_NullingGain * (PRES_A_NULL - CUR_PRES_A_mean);
Lightvalve 56:6f50d9d3bfee 876 // PRES_B_VREF = PRES_B_VREF - VREF_NullingGain * (PRES_B_NULL - CUR_PRES_B_mean);
Lightvalve 56:6f50d9d3bfee 877 //
Lightvalve 56:6f50d9d3bfee 878 // if (PRES_A_VREF > 3.3f) PRES_A_VREF = 3.3f;
Lightvalve 56:6f50d9d3bfee 879 // if (PRES_A_VREF < 0.0f) PRES_A_VREF = 0.0f;
Lightvalve 56:6f50d9d3bfee 880 // if (PRES_B_VREF > 3.3f) PRES_B_VREF = 3.3f;
Lightvalve 56:6f50d9d3bfee 881 // if (PRES_B_VREF < 0.0f) PRES_B_VREF = 0.0f;
Lightvalve 56:6f50d9d3bfee 882 //
Lightvalve 56:6f50d9d3bfee 883 // dac_1 = PRES_A_VREF / 3.3f;
Lightvalve 56:6f50d9d3bfee 884 // dac_2 = PRES_B_VREF / 3.3f;
Lightvalve 56:6f50d9d3bfee 885 // }
Lightvalve 56:6f50d9d3bfee 886 // } else {
Lightvalve 56:6f50d9d3bfee 887 // CONTROL_MODE = MODE_NO_ACT;
Lightvalve 56:6f50d9d3bfee 888 // TMR3_COUNT_PRES_NULL = 0;
Lightvalve 56:6f50d9d3bfee 889 // CUR_PRES_A_sum = 0;
Lightvalve 56:6f50d9d3bfee 890 // CUR_PRES_B_sum = 0;
Lightvalve 56:6f50d9d3bfee 891 // CUR_PRES_A_mean = 0;
Lightvalve 56:6f50d9d3bfee 892 // CUR_PRES_B_mean = 0;
Lightvalve 56:6f50d9d3bfee 893 //
Lightvalve 56:6f50d9d3bfee 894 // ROM_RESET_DATA();
Lightvalve 56:6f50d9d3bfee 895 //
Lightvalve 56:6f50d9d3bfee 896 // dac_1 = PRES_A_VREF / 3.3f;
Lightvalve 56:6f50d9d3bfee 897 // dac_2 = PRES_B_VREF / 3.3f;
Lightvalve 56:6f50d9d3bfee 898 // //pc.printf("nulling end");
Lightvalve 56:6f50d9d3bfee 899 // }
Lightvalve 56:6f50d9d3bfee 900 // TMR3_COUNT_PRES_NULL++;
Lightvalve 56:6f50d9d3bfee 901 // break;
Lightvalve 56:6f50d9d3bfee 902
Lightvalve 56:6f50d9d3bfee 903
Lightvalve 56:6f50d9d3bfee 904
Lightvalve 56:6f50d9d3bfee 905
Lightvalve 56:6f50d9d3bfee 906
Lightvalve 19:23b7c1ad8683 907 }
Lightvalve 14:8e7590227d22 908
Lightvalve 50:3c630b5eba9f 909 // case MODE_VALVE_NULLING_AND_DEADZONE_SETTING: {
Lightvalve 50:3c630b5eba9f 910 // if (TMR3_COUNT_DEADZONE == 0) {
Lightvalve 50:3c630b5eba9f 911 // if (pos_plus_end == pos_minus_end) need_enc_init = true;
Lightvalve 50:3c630b5eba9f 912 // else temp_time = 0;
Lightvalve 50:3c630b5eba9f 913 // }
Lightvalve 50:3c630b5eba9f 914 // if (need_enc_init) {
Lightvalve 50:3c630b5eba9f 915 // if (TMR3_COUNT_DEADZONE < (int) (0.5f * (float) TMR_FREQ_5k)) {
Lightvalve 50:3c630b5eba9f 916 // V_out = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 917 // pos_plus_end = pos.sen;
Lightvalve 50:3c630b5eba9f 918 // } else if (TMR3_COUNT_DEADZONE < TMR_FREQ_5k) {
Lightvalve 50:3c630b5eba9f 919 // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 920 // pos_minus_end = pos.sen;
Lightvalve 50:3c630b5eba9f 921 // } else if (TMR3_COUNT_DEADZONE == TMR_FREQ_5k) need_enc_init = false;
Lightvalve 50:3c630b5eba9f 922 // temp_time = TMR_FREQ_5k;
Lightvalve 50:3c630b5eba9f 923 // }
Lightvalve 50:3c630b5eba9f 924 //
Lightvalve 50:3c630b5eba9f 925 // if (temp_time <= TMR3_COUNT_DEADZONE && TMR3_COUNT_DEADZONE < (temp_time + TMR_FREQ_5k)) {
Lightvalve 50:3c630b5eba9f 926 // 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 927 // VALVE_CENTER = VALVE_DEADZONE_PLUS = VALVE_DEADZONE_MINUS = 0;
Lightvalve 50:3c630b5eba9f 928 //
Lightvalve 50:3c630b5eba9f 929 // } else if (temp_time <= TMR3_COUNT_DEADZONE && TMR3_COUNT_DEADZONE < (temp_time + (int) (1.9f * (float) TMR_FREQ_5k))) {
Lightvalve 50:3c630b5eba9f 930 // V_out = 0;
Lightvalve 50:3c630b5eba9f 931 // CUR_VELOCITY_sum += CUR_VELOCITY;
Lightvalve 50:3c630b5eba9f 932 // } else if (TMR3_COUNT_DEADZONE == (temp_time + 2 * TMR_FREQ_5k)) {
Lightvalve 50:3c630b5eba9f 933 // if (CUR_VELOCITY_sum == 0) DZ_dir = 1;
Lightvalve 50:3c630b5eba9f 934 // else if (CUR_VELOCITY_sum > 0) DZ_dir = 1;
Lightvalve 50:3c630b5eba9f 935 // else if (CUR_VELOCITY_sum < 0) DZ_dir = -1;
Lightvalve 50:3c630b5eba9f 936 // else DZ_temp_cnt2 = DZ_end;
Lightvalve 50:3c630b5eba9f 937 // CUR_VELOCITY_sum = 0;
Lightvalve 50:3c630b5eba9f 938 // } else if (TMR3_COUNT_DEADZONE > (temp_time + 2 * TMR_FREQ_5k)) {
Lightvalve 50:3c630b5eba9f 939 // if (TMR3_COUNT_DEADZONE > (temp_time + 10 * TMR_FREQ_5k)) DZ_temp_cnt2 = DZ_end;
Lightvalve 50:3c630b5eba9f 940 //
Lightvalve 50:3c630b5eba9f 941 // // Position of Dead Zone
Lightvalve 50:3c630b5eba9f 942 // // (CUR_VELOCITY < 0) (CUR_VELOCITY == 0) (CUR_VELOCITY > 0)
Lightvalve 50:3c630b5eba9f 943 // // | / | / |/
Lightvalve 50:3c630b5eba9f 944 // // | ______/ ___|___/ ______/|
Lightvalve 50:3c630b5eba9f 945 // // |/ / | / |
Lightvalve 50:3c630b5eba9f 946 // // /| / | / |
Lightvalve 50:3c630b5eba9f 947 // // 0V 0V 0V
Lightvalve 50:3c630b5eba9f 948 //
Lightvalve 50:3c630b5eba9f 949 // if (DZ_temp_cnt2 < DZ_end) {
Lightvalve 50:3c630b5eba9f 950 // if (TMR3_COUNT_DEADZONE % 20 != 0) {
Lightvalve 50:3c630b5eba9f 951 // CUR_VELOCITY_sum += CUR_VELOCITY;
Lightvalve 50:3c630b5eba9f 952 // } else {
Lightvalve 50:3c630b5eba9f 953 // V_out -= DZ_dir;
Lightvalve 50:3c630b5eba9f 954 // if (CUR_VELOCITY_sum * DZ_dir < 0) DZ_temp_cnt++;
Lightvalve 50:3c630b5eba9f 955 // CUR_VELOCITY_sum = 0;
Lightvalve 50:3c630b5eba9f 956 // }
Lightvalve 50:3c630b5eba9f 957 // if (DZ_temp_cnt == 5) {
Lightvalve 50:3c630b5eba9f 958 // if (DZ_dir >= 0) VALVE_DEADZONE_MINUS = (int16_t) V_out;
Lightvalve 50:3c630b5eba9f 959 // else VALVE_DEADZONE_PLUS = (int16_t) V_out;
Lightvalve 50:3c630b5eba9f 960 // DZ_dir = -DZ_dir;
Lightvalve 50:3c630b5eba9f 961 // DZ_temp_cnt = 0;
Lightvalve 50:3c630b5eba9f 962 // DZ_temp_cnt2++;
Lightvalve 50:3c630b5eba9f 963 // }
Lightvalve 50:3c630b5eba9f 964 // } else {
Lightvalve 50:3c630b5eba9f 965 // TMR3_COUNT_DEADZONE = -1;
Lightvalve 50:3c630b5eba9f 966 // VALVE_CENTER = VALVE_DEADZONE_PLUS / 2 + VALVE_DEADZONE_MINUS / 2;
Lightvalve 50:3c630b5eba9f 967 // if (VALVE_DEADZONE_PLUS < VALVE_DEADZONE_MINUS) {
Lightvalve 50:3c630b5eba9f 968 // VALVE_DEADZONE_PLUS = VALVE_CENTER;
Lightvalve 50:3c630b5eba9f 969 // VALVE_DEADZONE_MINUS = VALVE_CENTER;
Lightvalve 50:3c630b5eba9f 970 // }
Lightvalve 50:3c630b5eba9f 971 // V_out = 0;
Lightvalve 50:3c630b5eba9f 972 //
Lightvalve 50:3c630b5eba9f 973 // ROM_RESET_DATA();
Lightvalve 50:3c630b5eba9f 974 //
Lightvalve 50:3c630b5eba9f 975 // //spi_eeprom_write(RID_VALVE_DEADZONE_PLUS, VALVE_DEADZONE_PLUS);
Lightvalve 50:3c630b5eba9f 976 // //spi_eeprom_write(RID_VALVE_DEADZONE_MINUS, VALVE_DEADZONE_MINUS);
Lightvalve 50:3c630b5eba9f 977 //
Lightvalve 50:3c630b5eba9f 978 // CONTROL_MODE = MODE_NO_ACT;
Lightvalve 50:3c630b5eba9f 979 // DZ_temp_cnt2 = 0;
Lightvalve 50:3c630b5eba9f 980 // }
Lightvalve 50:3c630b5eba9f 981 // }
Lightvalve 50:3c630b5eba9f 982 // TMR3_COUNT_DEADZONE++;
Lightvalve 50:3c630b5eba9f 983 // break;
Lightvalve 50:3c630b5eba9f 984 // }
Lightvalve 14:8e7590227d22 985
Lightvalve 14:8e7590227d22 986 case MODE_FIND_HOME: {
Lightvalve 29:69f3f5445d6d 987 if (FINDHOME_STAGE == FINDHOME_INIT) {
Lightvalve 14:8e7590227d22 988 cnt_findhome = 0;
Lightvalve 14:8e7590227d22 989 cnt_vel_findhome = 0;
Lightvalve 14:8e7590227d22 990 //REFERENCE_MODE = MODE_REF_NO_ACT; // Stop taking reference data from PODO
Lightvalve 29:69f3f5445d6d 991 pos.ref_home_pos = pos.sen;
Lightvalve 30:8d561f16383b 992 vel.ref_home_pos = 0.0f;
Lightvalve 29:69f3f5445d6d 993 FINDHOME_STAGE = FINDHOME_GOTOLIMIT;
Lightvalve 56:6f50d9d3bfee 994 CAN_TX_PRES((int16_t)(CONTROL_MODE), (int16_t) (3));
Lightvalve 29:69f3f5445d6d 995 } else if (FINDHOME_STAGE == FINDHOME_GOTOLIMIT) {
Lightvalve 29:69f3f5445d6d 996 int cnt_check_enc = (TMR_FREQ_5k/500);
Lightvalve 29:69f3f5445d6d 997 if(cnt_findhome%cnt_check_enc == 0) {
Lightvalve 29:69f3f5445d6d 998 FINDHOME_POSITION = pos.sen;
Lightvalve 29:69f3f5445d6d 999 FINDHOME_VELOCITY = FINDHOME_POSITION - FINDHOME_POSITION_OLD;
Lightvalve 29:69f3f5445d6d 1000 FINDHOME_POSITION_OLD = FINDHOME_POSITION;
Lightvalve 29:69f3f5445d6d 1001 }
Lightvalve 29:69f3f5445d6d 1002 cnt_findhome++;
Lightvalve 56:6f50d9d3bfee 1003 //if(cnt_findhome == TMR_FREQ_5k) cnt_findhome = 0;
Lightvalve 14:8e7590227d22 1004
Lightvalve 29:69f3f5445d6d 1005 if (abs(FINDHOME_VELOCITY) <= 1) {
Lightvalve 29:69f3f5445d6d 1006 cnt_vel_findhome = cnt_vel_findhome + 1;
Lightvalve 29:69f3f5445d6d 1007 } else {
Lightvalve 29:69f3f5445d6d 1008 cnt_vel_findhome = 0;
Lightvalve 29:69f3f5445d6d 1009 }
Lightvalve 19:23b7c1ad8683 1010
Lightvalve 56:6f50d9d3bfee 1011 if ((cnt_vel_findhome < 3*TMR_FREQ_5k) && cnt_findhome <= 10*TMR_FREQ_5k) { // wait for 3sec
Lightvalve 29:69f3f5445d6d 1012 //REFERENCE_MODE = MODE_REF_NO_ACT;
Lightvalve 30:8d561f16383b 1013 if (HOMEPOS_OFFSET > 0) pos.ref_home_pos = pos.ref_home_pos + 2.0f;
Lightvalve 30:8d561f16383b 1014 else pos.ref_home_pos = pos.ref_home_pos - 2.0f;
Lightvalve 29:69f3f5445d6d 1015 pos.err = pos.ref_home_pos - pos.sen;
Lightvalve 30:8d561f16383b 1016 float VALVE_POS_RAW_POS_FB = 0.0f;
Lightvalve 50:3c630b5eba9f 1017 VALVE_POS_RAW_POS_FB = (float) P_GAIN_JOINT_POSITION * pos.err/(float) ENC_PULSE_PER_POSITION * 0.01f;
Lightvalve 56:6f50d9d3bfee 1018 valve_pos.ref = VALVE_POS_RAW_POS_FB + DDV_CENTER;
Lightvalve 29:69f3f5445d6d 1019 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 34:bb2ca2fc2a8e 1020
Lightvalve 30:8d561f16383b 1021 //float wn_Pos = 2.0f*PI*5.0f; // f_cut : 10Hz Position Control
Lightvalve 30:8d561f16383b 1022 //I_REF = 0.04f*wn_Pos*((float)joint_pos_err/ENC_PULSE_PER_POSITION);
Lightvalve 29:69f3f5445d6d 1023 //// L velocity >> mA convert
Lightvalve 30:8d561f16383b 1024 //if(I_REF>5.0f) I_REF = 5.0f;
Lightvalve 30:8d561f16383b 1025 //if(I_REF<-5.0f) I_REF = -5.0f;
Lightvalve 29:69f3f5445d6d 1026 //FLAG_CURRNET_CONTROL = true;
Lightvalve 29:69f3f5445d6d 1027 } else {
Lightvalve 29:69f3f5445d6d 1028 ENC_SET(HOMEPOS_OFFSET);
Lightvalve 29:69f3f5445d6d 1029 INIT_REF_POS = HOMEPOS_OFFSET;
Lightvalve 29:69f3f5445d6d 1030 REF_POSITION = 0;
Lightvalve 29:69f3f5445d6d 1031 REF_VELOCITY = 0;
Lightvalve 29:69f3f5445d6d 1032 FINDHOME_POSITION = 0;
Lightvalve 29:69f3f5445d6d 1033 FINDHOME_POSITION_OLD = 0;
Lightvalve 29:69f3f5445d6d 1034 FINDHOME_VELOCITY = 0;
Lightvalve 29:69f3f5445d6d 1035 cnt_findhome = 0;
Lightvalve 29:69f3f5445d6d 1036 cnt_vel_findhome = 0;
Lightvalve 29:69f3f5445d6d 1037 FINDHOME_STAGE = FINDHOME_ZEROPOSE;
Lightvalve 29:69f3f5445d6d 1038 }
Lightvalve 29:69f3f5445d6d 1039 } else if (FINDHOME_STAGE == FINDHOME_ZEROPOSE) {
Lightvalve 29:69f3f5445d6d 1040 int T_move = 2*TMR_FREQ_5k;
Lightvalve 30:8d561f16383b 1041 pos.ref_home_pos = (0.0f - (float)INIT_REF_POS)*0.5f*(1.0f - cos(3.14159f * (float)cnt_findhome / (float)T_move)) + (float)INIT_REF_POS;
Lightvalve 30:8d561f16383b 1042 vel.ref_home_pos = 0.0f;
Lightvalve 19:23b7c1ad8683 1043
Lightvalve 29:69f3f5445d6d 1044 // input for position control
Lightvalve 40:3f2c0619c8c4 1045 pos.err = pos.ref_home_pos - (float)pos.sen;
Lightvalve 30:8d561f16383b 1046 float VALVE_POS_RAW_POS_FB = 0.0f;
Lightvalve 50:3c630b5eba9f 1047 VALVE_POS_RAW_POS_FB = (float) P_GAIN_JOINT_POSITION * 0.01f * pos.err/(float) ENC_PULSE_PER_POSITION;
Lightvalve 56:6f50d9d3bfee 1048 valve_pos.ref = VALVE_POS_RAW_POS_FB + DDV_CENTER;
Lightvalve 29:69f3f5445d6d 1049 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 29:69f3f5445d6d 1050
Lightvalve 29:69f3f5445d6d 1051 cnt_findhome++;
Lightvalve 29:69f3f5445d6d 1052 if (cnt_findhome >= T_move) {
Lightvalve 29:69f3f5445d6d 1053 //REFERENCE_MODE = MODE_REF_DIRECT;
Lightvalve 29:69f3f5445d6d 1054 cnt_findhome = 0;
Lightvalve 30:8d561f16383b 1055 pos.ref = 0.0f;
Lightvalve 30:8d561f16383b 1056 vel.ref = 0.0f;
Lightvalve 30:8d561f16383b 1057 pos.ref_home_pos = 0.0f;
Lightvalve 30:8d561f16383b 1058 vel.ref_home_pos = 0.0f;
Lightvalve 29:69f3f5445d6d 1059 FINDHOME_STAGE = FINDHOME_INIT;
Lightvalve 56:6f50d9d3bfee 1060 CONTROL_MODE = MODE_JOINT_CONTROL;
Lightvalve 29:69f3f5445d6d 1061 }
Lightvalve 13:747daba9cf59 1062 }
Lightvalve 19:23b7c1ad8683 1063
Lightvalve 13:747daba9cf59 1064 break;
Lightvalve 13:747daba9cf59 1065 }
Lightvalve 14:8e7590227d22 1066
Lightvalve 50:3c630b5eba9f 1067 // case MODE_VALVE_GAIN_SETTING: {
Lightvalve 50:3c630b5eba9f 1068 // if (TMR3_COUNT_FLOWRATE == 0) {
Lightvalve 50:3c630b5eba9f 1069 // if (pos_plus_end == pos_minus_end) need_enc_init = true;
Lightvalve 50:3c630b5eba9f 1070 // else {
Lightvalve 50:3c630b5eba9f 1071 // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1072 // temp_time = (int) (0.5f * (float) TMR_FREQ_5k);
Lightvalve 50:3c630b5eba9f 1073 // }
Lightvalve 50:3c630b5eba9f 1074 // }
Lightvalve 50:3c630b5eba9f 1075 // if (need_enc_init) {
Lightvalve 50:3c630b5eba9f 1076 // if (TMR3_COUNT_FLOWRATE < (int) (0.5f * (float) TMR_FREQ_5k)) {
Lightvalve 50:3c630b5eba9f 1077 // V_out = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1078 // pos_plus_end = pos.sen;
Lightvalve 50:3c630b5eba9f 1079 // } else if (TMR3_COUNT_FLOWRATE < TMR_FREQ_5k) {
Lightvalve 50:3c630b5eba9f 1080 // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1081 // pos_minus_end = pos.sen;
Lightvalve 50:3c630b5eba9f 1082 // } else if (TMR3_COUNT_FLOWRATE == TMR_FREQ_5k) {
Lightvalve 50:3c630b5eba9f 1083 // need_enc_init = false;
Lightvalve 50:3c630b5eba9f 1084 // check_vel_pos_init = (int) (0.9f * (float) (pos_plus_end - pos_minus_end));
Lightvalve 50:3c630b5eba9f 1085 // check_vel_pos_fin = (int) (0.95f * (float) (pos_plus_end - pos_minus_end));
Lightvalve 50:3c630b5eba9f 1086 // check_vel_pos_interv = check_vel_pos_fin - check_vel_pos_init;
Lightvalve 50:3c630b5eba9f 1087 // }
Lightvalve 50:3c630b5eba9f 1088 // temp_time = TMR_FREQ_5k;
Lightvalve 50:3c630b5eba9f 1089 // }
Lightvalve 50:3c630b5eba9f 1090 // TMR3_COUNT_FLOWRATE++;
Lightvalve 50:3c630b5eba9f 1091 // if (TMR3_COUNT_FLOWRATE > temp_time) {
Lightvalve 50:3c630b5eba9f 1092 // if (flag_flowrate % 2 == 0) { // (+)
Lightvalve 50:3c630b5eba9f 1093 // VALVE_VOLTAGE = 1000.0f * (float) (flag_flowrate / 2 + 1);
Lightvalve 50:3c630b5eba9f 1094 // V_out = VALVE_VOLTAGE;
Lightvalve 50:3c630b5eba9f 1095 // if (pos.sen > (pos_minus_end + check_vel_pos_init) && pos.sen < (pos_minus_end + check_vel_pos_fin)) {
Lightvalve 50:3c630b5eba9f 1096 // fl_temp_cnt++;
Lightvalve 50:3c630b5eba9f 1097 // } else if (pos.sen >= (pos_minus_end + check_vel_pos_fin) && CUR_VELOCITY == 0) {
Lightvalve 50:3c630b5eba9f 1098 // 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 1099 // // 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 1100 // fl_temp_cnt2++;
Lightvalve 50:3c630b5eba9f 1101 // }
Lightvalve 50:3c630b5eba9f 1102 // } else if (flag_flowrate % 2 == 1) { // (-)
Lightvalve 50:3c630b5eba9f 1103 // VALVE_VOLTAGE = -1. * (float) (flag_flowrate / 2 + 1);
Lightvalve 50:3c630b5eba9f 1104 // V_out = VALVE_VOLTAGE;
Lightvalve 50:3c630b5eba9f 1105 // if (pos.sen < (pos_plus_end - check_vel_pos_init) && pos.sen > (pos_plus_end - check_vel_pos_fin)) {
Lightvalve 50:3c630b5eba9f 1106 // fl_temp_cnt++;
Lightvalve 50:3c630b5eba9f 1107 // } else if (pos.sen <= (pos_plus_end - check_vel_pos_fin) && CUR_VELOCITY == 0) {
Lightvalve 50:3c630b5eba9f 1108 // 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 1109 // // 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 1110 // fl_temp_cnt2++;
Lightvalve 50:3c630b5eba9f 1111 // }
Lightvalve 50:3c630b5eba9f 1112 // }
Lightvalve 50:3c630b5eba9f 1113 // if (fl_temp_cnt2 == 100) {
Lightvalve 50:3c630b5eba9f 1114 //
Lightvalve 50:3c630b5eba9f 1115 // ROM_RESET_DATA();
Lightvalve 50:3c630b5eba9f 1116 //
Lightvalve 50:3c630b5eba9f 1117 // //spi_eeprom_write(RID_VALVE_GAIN_PLUS_1 + flag_flowrate, (int16_t) (VALVE_GAIN_LPM_PER_V[flag_flowrate] * 100.0f));
Lightvalve 50:3c630b5eba9f 1118 // cur_vel_sum = 0;
Lightvalve 50:3c630b5eba9f 1119 // fl_temp_cnt = 0;
Lightvalve 50:3c630b5eba9f 1120 // fl_temp_cnt2 = 0;
Lightvalve 50:3c630b5eba9f 1121 // flag_flowrate++;
Lightvalve 50:3c630b5eba9f 1122 // }
Lightvalve 50:3c630b5eba9f 1123 // if (flag_flowrate == 10) {
Lightvalve 50:3c630b5eba9f 1124 // V_out = 0;
Lightvalve 50:3c630b5eba9f 1125 // flag_flowrate = 0;
Lightvalve 50:3c630b5eba9f 1126 // TMR3_COUNT_FLOWRATE = 0;
Lightvalve 50:3c630b5eba9f 1127 // valve_gain_repeat_cnt++;
Lightvalve 50:3c630b5eba9f 1128 // if (valve_gain_repeat_cnt >= 1) {
Lightvalve 50:3c630b5eba9f 1129 // CONTROL_MODE = MODE_NO_ACT;
Lightvalve 50:3c630b5eba9f 1130 // valve_gain_repeat_cnt = 0;
Lightvalve 50:3c630b5eba9f 1131 // }
Lightvalve 50:3c630b5eba9f 1132 //
Lightvalve 50:3c630b5eba9f 1133 // }
Lightvalve 50:3c630b5eba9f 1134 // break;
Lightvalve 50:3c630b5eba9f 1135 // }
Lightvalve 50:3c630b5eba9f 1136 //
Lightvalve 50:3c630b5eba9f 1137 // }
Lightvalve 14:8e7590227d22 1138
Lightvalve 14:8e7590227d22 1139 case MODE_PRESSURE_SENSOR_NULLING: {
Lightvalve 13:747daba9cf59 1140 // DAC Voltage reference set
Lightvalve 14:8e7590227d22 1141 if (TMR3_COUNT_PRES_NULL < TMR_FREQ_5k * 2) {
Lightvalve 14:8e7590227d22 1142 CUR_PRES_A_sum += pres_A.sen;
Lightvalve 14:8e7590227d22 1143 CUR_PRES_B_sum += pres_B.sen;
Lightvalve 14:8e7590227d22 1144
Lightvalve 14:8e7590227d22 1145 if (TMR3_COUNT_PRES_NULL % 10 == 0) {
Lightvalve 30:8d561f16383b 1146 CUR_PRES_A_mean = CUR_PRES_A_sum / 10.0f;
Lightvalve 30:8d561f16383b 1147 CUR_PRES_B_mean = CUR_PRES_B_sum / 10.0f;
Lightvalve 13:747daba9cf59 1148 CUR_PRES_A_sum = 0;
Lightvalve 13:747daba9cf59 1149 CUR_PRES_B_sum = 0;
Lightvalve 14:8e7590227d22 1150
Lightvalve 38:118df027d851 1151 float VREF_NullingGain = 0.0003f;
Lightvalve 38:118df027d851 1152 PRES_A_VREF = PRES_A_VREF - VREF_NullingGain * (PRES_A_NULL - CUR_PRES_A_mean);
Lightvalve 38:118df027d851 1153 PRES_B_VREF = PRES_B_VREF - VREF_NullingGain * (PRES_B_NULL - CUR_PRES_B_mean);
Lightvalve 14:8e7590227d22 1154
Lightvalve 30:8d561f16383b 1155 if (PRES_A_VREF > 3.3f) PRES_A_VREF = 3.3f;
Lightvalve 30:8d561f16383b 1156 if (PRES_A_VREF < 0.0f) PRES_A_VREF = 0.0f;
Lightvalve 30:8d561f16383b 1157 if (PRES_B_VREF > 3.3f) PRES_B_VREF = 3.3f;
Lightvalve 30:8d561f16383b 1158 if (PRES_B_VREF < 0.0f) PRES_B_VREF = 0.0f;
Lightvalve 19:23b7c1ad8683 1159
Lightvalve 30:8d561f16383b 1160 dac_1 = PRES_A_VREF / 3.3f;
Lightvalve 30:8d561f16383b 1161 dac_2 = PRES_B_VREF / 3.3f;
Lightvalve 13:747daba9cf59 1162 }
Lightvalve 13:747daba9cf59 1163 } else {
Lightvalve 56:6f50d9d3bfee 1164 CONTROL_MODE = MODE_NO_ACT;
Lightvalve 14:8e7590227d22 1165 TMR3_COUNT_PRES_NULL = 0;
Lightvalve 13:747daba9cf59 1166 CUR_PRES_A_sum = 0;
Lightvalve 13:747daba9cf59 1167 CUR_PRES_B_sum = 0;
Lightvalve 13:747daba9cf59 1168 CUR_PRES_A_mean = 0;
Lightvalve 13:747daba9cf59 1169 CUR_PRES_B_mean = 0;
Lightvalve 14:8e7590227d22 1170
Lightvalve 16:903b5a4433b4 1171 ROM_RESET_DATA();
Lightvalve 19:23b7c1ad8683 1172
Lightvalve 30:8d561f16383b 1173 dac_1 = PRES_A_VREF / 3.3f;
Lightvalve 30:8d561f16383b 1174 dac_2 = PRES_B_VREF / 3.3f;
Lightvalve 17:1865016ca2e7 1175 //pc.printf("nulling end");
Lightvalve 13:747daba9cf59 1176 }
Lightvalve 14:8e7590227d22 1177 TMR3_COUNT_PRES_NULL++;
Lightvalve 13:747daba9cf59 1178 break;
Lightvalve 13:747daba9cf59 1179 }
Lightvalve 14:8e7590227d22 1180
Lightvalve 50:3c630b5eba9f 1181 // case MODE_PRESSURE_SENSOR_CALIB: {
Lightvalve 50:3c630b5eba9f 1182 // if (TMR3_COUNT_PRES_CALIB < 2 * TMR_FREQ_5k) {
Lightvalve 50:3c630b5eba9f 1183 // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1184 // if (TMR3_COUNT_PRES_CALIB >= TMR_FREQ_5k) {
Lightvalve 50:3c630b5eba9f 1185 // CUR_PRES_A_sum += CUR_PRES_A;
Lightvalve 50:3c630b5eba9f 1186 // }
Lightvalve 50:3c630b5eba9f 1187 // } else if (TMR3_COUNT_PRES_CALIB < 4 * TMR_FREQ_5k) {
Lightvalve 50:3c630b5eba9f 1188 // V_out = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1189 // if (TMR3_COUNT_PRES_CALIB >= 3 * TMR_FREQ_5k) {
Lightvalve 50:3c630b5eba9f 1190 // CUR_PRES_B_sum += CUR_PRES_B;
Lightvalve 50:3c630b5eba9f 1191 // }
Lightvalve 50:3c630b5eba9f 1192 // } else {
Lightvalve 50:3c630b5eba9f 1193 // CONTROL_MODE = MODE_NO_ACT;
Lightvalve 50:3c630b5eba9f 1194 // TMR3_COUNT_PRES_CALIB = 0;
Lightvalve 50:3c630b5eba9f 1195 // V_out = 0;
Lightvalve 50:3c630b5eba9f 1196 // PRES_SENSOR_A_PULSE_PER_BAR = CUR_PRES_A_sum / ((float) TMR_FREQ_5k - 1.0f) - PRES_A_NULL;
Lightvalve 50:3c630b5eba9f 1197 // PRES_SENSOR_A_PULSE_PER_BAR = PRES_SENSOR_A_PULSE_PER_BAR / ((float) PRES_SUPPLY - 1.0f);
Lightvalve 50:3c630b5eba9f 1198 // PRES_SENSOR_B_PULSE_PER_BAR = CUR_PRES_B_sum / ((float) TMR_FREQ_5k - 1.0f) - PRES_B_NULL;
Lightvalve 50:3c630b5eba9f 1199 // PRES_SENSOR_B_PULSE_PER_BAR = PRES_SENSOR_B_PULSE_PER_BAR / ((float) PRES_SUPPLY - 1.0f);
Lightvalve 50:3c630b5eba9f 1200 // CUR_PRES_A_sum = 0;
Lightvalve 50:3c630b5eba9f 1201 // CUR_PRES_B_sum = 0;
Lightvalve 50:3c630b5eba9f 1202 // CUR_PRES_A_mean = 0;
Lightvalve 50:3c630b5eba9f 1203 // CUR_PRES_B_mean = 0;
Lightvalve 50:3c630b5eba9f 1204 //
Lightvalve 50:3c630b5eba9f 1205 // ROM_RESET_DATA();
Lightvalve 50:3c630b5eba9f 1206 //
Lightvalve 50:3c630b5eba9f 1207 // //spi_eeprom_write(RID_PRES_SENSOR_A_PULSE_PER_BAR, (int16_t) (PRES_SENSOR_A_PULSE_PER_BAR * 100.0f));
Lightvalve 50:3c630b5eba9f 1208 // //spi_eeprom_write(RID_PRES_SENSOR_B_PULSE_PER_BAR, (int16_t) (PRES_SENSOR_B_PULSE_PER_BAR * 100.0f));
Lightvalve 50:3c630b5eba9f 1209 // }
Lightvalve 50:3c630b5eba9f 1210 // TMR3_COUNT_PRES_CALIB++;
Lightvalve 50:3c630b5eba9f 1211 // break;
Lightvalve 50:3c630b5eba9f 1212 // }
Lightvalve 19:23b7c1ad8683 1213
Lightvalve 50:3c630b5eba9f 1214 // case MODE_ROTARY_FRICTION_TUNING: {
Lightvalve 50:3c630b5eba9f 1215 // 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 1216 // V_out = PWM_out * sin(2 * 3.14159f * freq_fric_tune * TMR3_COUNT_ROTARY_FRIC_TUNE * 0.0001f);
Lightvalve 50:3c630b5eba9f 1217 // if (V_out > 0) V_out = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1218 // else V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 50:3c630b5eba9f 1219 // TMR3_COUNT_ROTARY_FRIC_TUNE++;
Lightvalve 50:3c630b5eba9f 1220 // if (TMR3_COUNT_ROTARY_FRIC_TUNE > TUNING_TIME * TMR_FREQ_5k) {
Lightvalve 50:3c630b5eba9f 1221 // TMR3_COUNT_ROTARY_FRIC_TUNE = 0;
Lightvalve 50:3c630b5eba9f 1222 // V_out = 0.0f;
Lightvalve 50:3c630b5eba9f 1223 // CONTROL_MODE = MODE_NO_ACT;
Lightvalve 50:3c630b5eba9f 1224 // }
Lightvalve 50:3c630b5eba9f 1225 // break;
Lightvalve 50:3c630b5eba9f 1226 // }
Lightvalve 14:8e7590227d22 1227
Lightvalve 14:8e7590227d22 1228 case MODE_DDV_POS_VS_PWM_ID: {
Lightvalve 13:747daba9cf59 1229 VALVE_ID_timer = VALVE_ID_timer + 1;
Lightvalve 14:8e7590227d22 1230
Lightvalve 14:8e7590227d22 1231 if(VALVE_ID_timer < TMR_FREQ_5k*1) {
Lightvalve 30:8d561f16383b 1232 V_out = 3000.0f * sin(2*3.14f*VALVE_ID_timer/TMR_FREQ_5k * 100.0f);
Lightvalve 14:8e7590227d22 1233 } else if(VALVE_ID_timer < TMR_FREQ_5k*2) {
Lightvalve 30:8d561f16383b 1234 V_out = 1000.0f*(ID_index_array[ID_index]);
Lightvalve 14:8e7590227d22 1235 } else if(VALVE_ID_timer == TMR_FREQ_5k*2) {
Lightvalve 13:747daba9cf59 1236 VALVE_POS_TMP = 0;
Lightvalve 13:747daba9cf59 1237 data_num = 0;
Lightvalve 14:8e7590227d22 1238 } else if(VALVE_ID_timer < TMR_FREQ_5k*3) {
Lightvalve 13:747daba9cf59 1239 data_num = data_num + 1;
Lightvalve 13:747daba9cf59 1240 VALVE_POS_TMP = VALVE_POS_TMP + value;
Lightvalve 14:8e7590227d22 1241 } else if(VALVE_ID_timer == TMR_FREQ_5k*3) {
Lightvalve 13:747daba9cf59 1242 VALVE_POS_AVG[ID_index] = VALVE_POS_TMP / data_num;
Lightvalve 13:747daba9cf59 1243 VALVE_ID_timer = 0;
Lightvalve 13:747daba9cf59 1244 ID_index= ID_index +1;
Lightvalve 13:747daba9cf59 1245 }
Lightvalve 14:8e7590227d22 1246
Lightvalve 17:1865016ca2e7 1247 if(ID_index>=25) {
Lightvalve 13:747daba9cf59 1248 int i;
Lightvalve 13:747daba9cf59 1249 VALVE_POS_AVG_OLD = VALVE_POS_AVG[0];
Lightvalve 17:1865016ca2e7 1250 for(i=0; i<25; i++) {
Lightvalve 13:747daba9cf59 1251 VALVE_POS_VS_PWM[i] = (int16_t) (VALVE_POS_AVG[i]);
Lightvalve 14:8e7590227d22 1252 if(VALVE_POS_AVG[i] > VALVE_POS_AVG_OLD) {
Lightvalve 13:747daba9cf59 1253 VALVE_MAX_POS = VALVE_POS_AVG[i];
Lightvalve 13:747daba9cf59 1254 VALVE_POS_AVG_OLD = VALVE_MAX_POS;
Lightvalve 14:8e7590227d22 1255 } else if(VALVE_POS_AVG[i] < VALVE_POS_AVG_OLD) {
Lightvalve 13:747daba9cf59 1256 VALVE_MIN_POS = VALVE_POS_AVG[i];
Lightvalve 13:747daba9cf59 1257 VALVE_POS_AVG_OLD = VALVE_MIN_POS;
Lightvalve 13:747daba9cf59 1258 }
Lightvalve 13:747daba9cf59 1259 }
Lightvalve 27:a2254a485f23 1260 //ROM_RESET_DATA();
Lightvalve 13:747daba9cf59 1261 ID_index = 0;
Lightvalve 56:6f50d9d3bfee 1262 CONTROL_MODE = MODE_NO_ACT;
Lightvalve 13:747daba9cf59 1263 }
Lightvalve 14:8e7590227d22 1264
Lightvalve 14:8e7590227d22 1265
Lightvalve 13:747daba9cf59 1266 break;
Lightvalve 13:747daba9cf59 1267 }
Lightvalve 14:8e7590227d22 1268
Lightvalve 14:8e7590227d22 1269 case MODE_DDV_DEADZONE_AND_CENTER: {
Lightvalve 14:8e7590227d22 1270
Lightvalve 13:747daba9cf59 1271 VALVE_DZ_timer = VALVE_DZ_timer + 1;
Lightvalve 14:8e7590227d22 1272 if(first_check == 0) {
Lightvalve 30:8d561f16383b 1273 if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 49:e7bcfc244d40 1274 V_out = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 30:8d561f16383b 1275 } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 49:e7bcfc244d40 1276 V_out = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 14:8e7590227d22 1277 pos_plus_end = pos.sen;
Lightvalve 30:8d561f16383b 1278 } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 49:e7bcfc244d40 1279 V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 30:8d561f16383b 1280 } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 49:e7bcfc244d40 1281 V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 14:8e7590227d22 1282 pos_minus_end = pos.sen;
Lightvalve 30:8d561f16383b 1283 } else if(VALVE_DZ_timer < (int) (3.0f * (float) TMR_FREQ_5k)) {
Lightvalve 49:e7bcfc244d40 1284 V_out = (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 1285 } else if(VALVE_DZ_timer < (int) (4.0f * (float) TMR_FREQ_5k)) {
Lightvalve 49:e7bcfc244d40 1286 V_out = (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 1287 data_num = data_num + 1;
Lightvalve 13:747daba9cf59 1288 VALVE_POS_TMP = VALVE_POS_TMP + value;
Lightvalve 30:8d561f16383b 1289 } else if(VALVE_DZ_timer == (int) (4.0f * (float) TMR_FREQ_5k)) {
Lightvalve 49:e7bcfc244d40 1290 V_out = (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 1291 DDV_POS_AVG = VALVE_POS_TMP / data_num;
Lightvalve 14:8e7590227d22 1292 START_POS = pos.sen;
Lightvalve 13:747daba9cf59 1293 VALVE_POS_TMP = 0;
Lightvalve 13:747daba9cf59 1294 data_num = 0;
Lightvalve 14:8e7590227d22 1295
Lightvalve 30:8d561f16383b 1296 } else if(VALVE_DZ_timer < (int) (5.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 1297 valve_pos.ref = DDV_POS_AVG;
Lightvalve 14:8e7590227d22 1298 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 14:8e7590227d22 1299
Lightvalve 30:8d561f16383b 1300 } else if(VALVE_DZ_timer < (int) (6.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 1301 valve_pos.ref = DDV_POS_AVG;
Lightvalve 14:8e7590227d22 1302 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 13:747daba9cf59 1303 if(CUR_VELOCITY >= 0)
Lightvalve 13:747daba9cf59 1304 VEL_POINT = VEL_POINT + 1;
Lightvalve 13:747daba9cf59 1305 else
Lightvalve 13:747daba9cf59 1306 VEL_POINT = VEL_POINT - 1;
Lightvalve 30:8d561f16383b 1307 } else if(VALVE_DZ_timer == (int) (6.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 1308 valve_pos.ref = DDV_POS_AVG;
Lightvalve 14:8e7590227d22 1309 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 14:8e7590227d22 1310 FINAL_POS = pos.sen;
Lightvalve 14:8e7590227d22 1311
Lightvalve 14:8e7590227d22 1312 if((FINAL_POS - START_POS)>100) {
Lightvalve 13:747daba9cf59 1313 DZ_case = 1;
Lightvalve 14:8e7590227d22 1314 } else if((FINAL_POS - START_POS)<-100) {
Lightvalve 13:747daba9cf59 1315 DZ_case = -1;
Lightvalve 14:8e7590227d22 1316 } else {
Lightvalve 13:747daba9cf59 1317 DZ_case = 0;
Lightvalve 13:747daba9cf59 1318 }
Lightvalve 14:8e7590227d22 1319
Lightvalve 49:e7bcfc244d40 1320 CAN_TX_PRES((int16_t) (DZ_case), (int16_t) (6));
Lightvalve 50:3c630b5eba9f 1321
Lightvalve 14:8e7590227d22 1322 VEL_POINT = 0;
Lightvalve 13:747daba9cf59 1323 first_check = 1;
Lightvalve 13:747daba9cf59 1324 DZ_DIRECTION = 1;
Lightvalve 13:747daba9cf59 1325 VALVE_DZ_timer = 0;
Lightvalve 13:747daba9cf59 1326 Ref_Valve_Pos_Old = DDV_POS_AVG;
Lightvalve 13:747daba9cf59 1327 DZ_NUM = 1;
Lightvalve 13:747daba9cf59 1328 DZ_index = 1;
Lightvalve 14:8e7590227d22 1329
Lightvalve 13:747daba9cf59 1330 }
Lightvalve 19:23b7c1ad8683 1331 } else {
Lightvalve 14:8e7590227d22 1332 if((DZ_case == -1 && DZ_NUM == 1) | (DZ_case == 1 && DZ_NUM == 1)) {
Lightvalve 30:8d561f16383b 1333 if(VALVE_DZ_timer < (int) (1.0 * (float) TMR_FREQ_5k)) {
Lightvalve 49:e7bcfc244d40 1334 V_out = (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 1335 } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 1336 START_POS = pos.sen;
Lightvalve 30:8d561f16383b 1337 } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 1338 valve_pos.ref = Ref_Valve_Pos_Old - DZ_case * DZ_DIRECTION * 64 / DZ_index;
Lightvalve 14:8e7590227d22 1339 if(valve_pos.ref <= VALVE_MIN_POS) {
Lightvalve 14:8e7590227d22 1340 valve_pos.ref = VALVE_MIN_POS;
Lightvalve 14:8e7590227d22 1341 } else if(valve_pos.ref >= VALVE_MAX_POS) {
Lightvalve 14:8e7590227d22 1342 valve_pos.ref = VALVE_MAX_POS;
Lightvalve 13:747daba9cf59 1343 }
Lightvalve 14:8e7590227d22 1344 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 13:747daba9cf59 1345 if(CUR_VELOCITY >= 0)
Lightvalve 13:747daba9cf59 1346 VEL_POINT = VEL_POINT + 1;
Lightvalve 13:747daba9cf59 1347 else
Lightvalve 13:747daba9cf59 1348 VEL_POINT = VEL_POINT - 1;
Lightvalve 30:8d561f16383b 1349 } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 1350 Ref_Valve_Pos_Old = valve_pos.ref;
Lightvalve 14:8e7590227d22 1351 FINAL_POS = pos.sen;
Lightvalve 14:8e7590227d22 1352
Lightvalve 14:8e7590227d22 1353 if((FINAL_POS - START_POS)>100) {
Lightvalve 14:8e7590227d22 1354 DZ_DIRECTION = 1 * DZ_case;
Lightvalve 14:8e7590227d22 1355 } else if((FINAL_POS - START_POS)<-100) {
Lightvalve 14:8e7590227d22 1356 DZ_DIRECTION = -1 * DZ_case;
Lightvalve 14:8e7590227d22 1357 } else {
Lightvalve 13:747daba9cf59 1358 DZ_DIRECTION = 1 * DZ_case;
Lightvalve 13:747daba9cf59 1359 }
Lightvalve 14:8e7590227d22 1360
Lightvalve 14:8e7590227d22 1361 VEL_POINT = 0;
Lightvalve 14:8e7590227d22 1362
Lightvalve 13:747daba9cf59 1363 VALVE_DZ_timer = 0;
Lightvalve 13:747daba9cf59 1364 DZ_index= DZ_index *2;
Lightvalve 14:8e7590227d22 1365 if(DZ_index >= 128) {
Lightvalve 14:8e7590227d22 1366 FIRST_DZ = valve_pos.ref;
Lightvalve 13:747daba9cf59 1367 DZ_NUM = 2;
Lightvalve 13:747daba9cf59 1368 Ref_Valve_Pos_Old = FIRST_DZ;
Lightvalve 13:747daba9cf59 1369 DZ_index = 1;
Lightvalve 13:747daba9cf59 1370 DZ_DIRECTION = 1;
Lightvalve 13:747daba9cf59 1371 }
Lightvalve 13:747daba9cf59 1372 }
Lightvalve 14:8e7590227d22 1373 } else if((DZ_case == -1 && DZ_NUM == 2) | (DZ_case == 1 && DZ_NUM == 2)) {
Lightvalve 30:8d561f16383b 1374 if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 49:e7bcfc244d40 1375 V_out = (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 1376 } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 1377 START_POS = pos.sen;
Lightvalve 30:8d561f16383b 1378 } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 18:b8adf1582ea3 1379 valve_pos.ref = Ref_Valve_Pos_Old + DZ_DIRECTION * 64 / DZ_index;
Lightvalve 14:8e7590227d22 1380 if(valve_pos.ref <= VALVE_MIN_POS) {
Lightvalve 14:8e7590227d22 1381 valve_pos.ref = VALVE_MIN_POS;
Lightvalve 14:8e7590227d22 1382 } else if(valve_pos.ref >= VALVE_MAX_POS) {
Lightvalve 14:8e7590227d22 1383 valve_pos.ref = VALVE_MAX_POS;
Lightvalve 13:747daba9cf59 1384 }
Lightvalve 14:8e7590227d22 1385 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 13:747daba9cf59 1386 if(CUR_VELOCITY >= 0)
Lightvalve 13:747daba9cf59 1387 VEL_POINT = VEL_POINT + 1;
Lightvalve 13:747daba9cf59 1388 else
Lightvalve 13:747daba9cf59 1389 VEL_POINT = VEL_POINT - 1;
Lightvalve 30:8d561f16383b 1390 } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 1391 Ref_Valve_Pos_Old = valve_pos.ref;
Lightvalve 14:8e7590227d22 1392 FINAL_POS = pos.sen;
Lightvalve 14:8e7590227d22 1393
Lightvalve 14:8e7590227d22 1394 if((FINAL_POS - START_POS)>100) {
Lightvalve 13:747daba9cf59 1395 DZ_DIRECTION = -1;
Lightvalve 14:8e7590227d22 1396 } else if((FINAL_POS - START_POS)<-100) {
Lightvalve 14:8e7590227d22 1397 DZ_DIRECTION = 1;
Lightvalve 14:8e7590227d22 1398 } else {
Lightvalve 13:747daba9cf59 1399 DZ_DIRECTION = 1;
Lightvalve 13:747daba9cf59 1400 }
Lightvalve 14:8e7590227d22 1401
Lightvalve 14:8e7590227d22 1402 VEL_POINT = 0;
Lightvalve 14:8e7590227d22 1403
Lightvalve 14:8e7590227d22 1404
Lightvalve 13:747daba9cf59 1405 VALVE_DZ_timer = 0;
Lightvalve 13:747daba9cf59 1406 DZ_index= DZ_index *2;
Lightvalve 14:8e7590227d22 1407 if(DZ_index >= 128) {
Lightvalve 14:8e7590227d22 1408 SECOND_DZ = valve_pos.ref;
Lightvalve 56:6f50d9d3bfee 1409 DDV_CENTER = 0.5f * (float) (FIRST_DZ) + 0.5f * (float) (SECOND_DZ);
Lightvalve 13:747daba9cf59 1410 first_check = 0;
Lightvalve 33:91b17819ec30 1411 VALVE_DEADZONE_MINUS = (float) FIRST_DZ;
Lightvalve 33:91b17819ec30 1412 VALVE_DEADZONE_PLUS = (float) SECOND_DZ;
Lightvalve 19:23b7c1ad8683 1413
Lightvalve 16:903b5a4433b4 1414 ROM_RESET_DATA();
Lightvalve 34:bb2ca2fc2a8e 1415
Lightvalve 13:747daba9cf59 1416 CONTROL_MODE = MODE_NO_ACT;
Lightvalve 13:747daba9cf59 1417 DZ_index = 1;
Lightvalve 13:747daba9cf59 1418 }
Lightvalve 13:747daba9cf59 1419 }
Lightvalve 14:8e7590227d22 1420 } else if(DZ_case == 0 && DZ_NUM ==1) {
Lightvalve 30:8d561f16383b 1421 if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 49:e7bcfc244d40 1422 V_out = (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 1423 } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 1424 START_POS = pos.sen;
Lightvalve 30:8d561f16383b 1425 } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 1426 valve_pos.ref = Ref_Valve_Pos_Old - DZ_DIRECTION * 64 / DZ_index;
Lightvalve 14:8e7590227d22 1427 if(valve_pos.ref <= VALVE_MIN_POS) {
Lightvalve 14:8e7590227d22 1428 valve_pos.ref = VALVE_MIN_POS;
Lightvalve 14:8e7590227d22 1429 } else if(valve_pos.ref >= VALVE_MAX_POS) {
Lightvalve 14:8e7590227d22 1430 valve_pos.ref = VALVE_MAX_POS;
Lightvalve 13:747daba9cf59 1431 }
Lightvalve 14:8e7590227d22 1432 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 13:747daba9cf59 1433 if(CUR_VELOCITY >= 0)
Lightvalve 13:747daba9cf59 1434 VEL_POINT = VEL_POINT + 1;
Lightvalve 13:747daba9cf59 1435 else
Lightvalve 13:747daba9cf59 1436 VEL_POINT = VEL_POINT - 1;
Lightvalve 30:8d561f16383b 1437 } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 1438 Ref_Valve_Pos_Old = valve_pos.ref;
Lightvalve 14:8e7590227d22 1439 FINAL_POS = pos.sen;
Lightvalve 14:8e7590227d22 1440
Lightvalve 14:8e7590227d22 1441 if((FINAL_POS - START_POS)>100) {
Lightvalve 14:8e7590227d22 1442 DZ_DIRECTION = 1;
Lightvalve 14:8e7590227d22 1443 } else if((FINAL_POS - START_POS)<-100) {
Lightvalve 14:8e7590227d22 1444 DZ_DIRECTION = -1;
Lightvalve 14:8e7590227d22 1445 } else {
Lightvalve 13:747daba9cf59 1446 DZ_DIRECTION = 1;
Lightvalve 13:747daba9cf59 1447 }
Lightvalve 14:8e7590227d22 1448
Lightvalve 14:8e7590227d22 1449 VEL_POINT = 0;
Lightvalve 13:747daba9cf59 1450 VALVE_DZ_timer = 0;
Lightvalve 13:747daba9cf59 1451 DZ_index= DZ_index *2;
Lightvalve 14:8e7590227d22 1452 if(DZ_index >= 128) {
Lightvalve 14:8e7590227d22 1453 FIRST_DZ = valve_pos.ref;
Lightvalve 13:747daba9cf59 1454 DZ_NUM = 2;
Lightvalve 13:747daba9cf59 1455 Ref_Valve_Pos_Old = FIRST_DZ;
Lightvalve 13:747daba9cf59 1456 DZ_index = 1;
Lightvalve 13:747daba9cf59 1457 DZ_DIRECTION = 1;
Lightvalve 13:747daba9cf59 1458 }
Lightvalve 13:747daba9cf59 1459 }
Lightvalve 14:8e7590227d22 1460 } else {
Lightvalve 30:8d561f16383b 1461 if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 49:e7bcfc244d40 1462 V_out = (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 1463 } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 1464 START_POS = pos.sen;
Lightvalve 30:8d561f16383b 1465 } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 1466 valve_pos.ref = Ref_Valve_Pos_Old + DZ_DIRECTION * 64 / DZ_index;
Lightvalve 14:8e7590227d22 1467 if(valve_pos.ref <= VALVE_MIN_POS) {
Lightvalve 14:8e7590227d22 1468 valve_pos.ref = VALVE_MIN_POS;
Lightvalve 14:8e7590227d22 1469 } else if(valve_pos.ref > VALVE_MAX_POS) {
Lightvalve 14:8e7590227d22 1470 valve_pos.ref = VALVE_MAX_POS - 1;
Lightvalve 13:747daba9cf59 1471 }
Lightvalve 14:8e7590227d22 1472 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 13:747daba9cf59 1473 if(CUR_VELOCITY >= 0)
Lightvalve 13:747daba9cf59 1474 VEL_POINT = VEL_POINT + 1;
Lightvalve 13:747daba9cf59 1475 else
Lightvalve 13:747daba9cf59 1476 VEL_POINT = VEL_POINT - 1;
Lightvalve 30:8d561f16383b 1477 } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 1478 Ref_Valve_Pos_Old = valve_pos.ref;
Lightvalve 14:8e7590227d22 1479 FINAL_POS = pos.sen;
Lightvalve 13:747daba9cf59 1480 if(VEL_POINT >= 0)
Lightvalve 13:747daba9cf59 1481 DZ_DIRECTION = -1;
Lightvalve 13:747daba9cf59 1482 else
Lightvalve 13:747daba9cf59 1483 DZ_DIRECTION = 1;
Lightvalve 13:747daba9cf59 1484 if(abs(FINAL_POS - START_POS) < 100)
Lightvalve 13:747daba9cf59 1485 DZ_DIRECTION = 1;
Lightvalve 14:8e7590227d22 1486
Lightvalve 14:8e7590227d22 1487 VEL_POINT = 0;
Lightvalve 14:8e7590227d22 1488
Lightvalve 14:8e7590227d22 1489
Lightvalve 13:747daba9cf59 1490 VALVE_DZ_timer = 0;
Lightvalve 13:747daba9cf59 1491 DZ_index= DZ_index *2;
Lightvalve 14:8e7590227d22 1492 if(DZ_index >= 128) {
Lightvalve 14:8e7590227d22 1493 SECOND_DZ = valve_pos.ref;
Lightvalve 56:6f50d9d3bfee 1494 DDV_CENTER = 0.5f * (float) (FIRST_DZ) + 0.5f * (float) (SECOND_DZ);
Lightvalve 13:747daba9cf59 1495 first_check = 0;
Lightvalve 33:91b17819ec30 1496 VALVE_DEADZONE_MINUS = (float) FIRST_DZ;
Lightvalve 33:91b17819ec30 1497 VALVE_DEADZONE_PLUS = (float) SECOND_DZ;
Lightvalve 34:bb2ca2fc2a8e 1498
Lightvalve 56:6f50d9d3bfee 1499 CONTROL_MODE = MODE_NO_ACT;
Lightvalve 13:747daba9cf59 1500 DZ_index = 1;
Lightvalve 13:747daba9cf59 1501 }
Lightvalve 13:747daba9cf59 1502 }
Lightvalve 13:747daba9cf59 1503 }
Lightvalve 14:8e7590227d22 1504 }
Lightvalve 13:747daba9cf59 1505 break;
Lightvalve 13:747daba9cf59 1506 }
Lightvalve 14:8e7590227d22 1507
Lightvalve 14:8e7590227d22 1508 case MODE_DDV_POS_VS_FLOWRATE: {
Lightvalve 13:747daba9cf59 1509 VALVE_FR_timer = VALVE_FR_timer + 1;
Lightvalve 14:8e7590227d22 1510 if(first_check == 0) {
Lightvalve 30:8d561f16383b 1511 if(VALVE_FR_timer < (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 49:e7bcfc244d40 1512 V_out = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 23:59218d4a256d 1513 //CAN_TX_PRES((int16_t) (VALVE_FR_timer), (int16_t) (6));
Lightvalve 30:8d561f16383b 1514 } else if(VALVE_FR_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 49:e7bcfc244d40 1515 V_out = VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 14:8e7590227d22 1516 pos_plus_end = pos.sen;
Lightvalve 14:8e7590227d22 1517 // CAN_TX_PRES((int16_t) (V_out), (int16_t) (7));
Lightvalve 30:8d561f16383b 1518 } else if(VALVE_FR_timer < (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 49:e7bcfc244d40 1519 V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 30:8d561f16383b 1520 } else if(VALVE_FR_timer == (int) (2.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 1521 // CAN_TX_PRES((int16_t) (V_out), (int16_t) (8));
Lightvalve 49:e7bcfc244d40 1522 V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f;
Lightvalve 14:8e7590227d22 1523 pos_minus_end = pos.sen;
Lightvalve 13:747daba9cf59 1524 first_check = 1;
Lightvalve 13:747daba9cf59 1525 VALVE_FR_timer = 0;
Lightvalve 56:6f50d9d3bfee 1526 valve_pos.ref = DDV_CENTER;
Lightvalve 13:747daba9cf59 1527 ID_index = 0;
Lightvalve 13:747daba9cf59 1528 max_check = 0;
Lightvalve 13:747daba9cf59 1529 min_check = 0;
Lightvalve 13:747daba9cf59 1530 }
Lightvalve 14:8e7590227d22 1531 } else {
Lightvalve 30:8d561f16383b 1532 if(VALVE_FR_timer < (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 49:e7bcfc244d40 1533 V_out = (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 1534 } else if(VALVE_FR_timer == (int) (1.0f * (float) TMR_FREQ_5k)) {
Lightvalve 13:747daba9cf59 1535 data_num = 0;
Lightvalve 56:6f50d9d3bfee 1536 valve_pos.ref = 10.0f*((float) ID_index_array[ID_index]) + DDV_CENTER;
Lightvalve 14:8e7590227d22 1537
Lightvalve 14:8e7590227d22 1538 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 14:8e7590227d22 1539 START_POS = pos.sen;
Lightvalve 30:8d561f16383b 1540 } else if(VALVE_FR_timer < (int) (5.0f * (float) TMR_FREQ_5k)) {
Lightvalve 56:6f50d9d3bfee 1541 valve_pos.ref = 10.0f*((float) ID_index_array[ID_index]) + DDV_CENTER;
Lightvalve 14:8e7590227d22 1542 VALVE_POS_CONTROL(valve_pos.ref);
Lightvalve 13:747daba9cf59 1543 data_num = data_num + 1;
Lightvalve 30:8d561f16383b 1544 if(abs(0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen) > 20000.0f) {
Lightvalve 14:8e7590227d22 1545 FINAL_POS = pos.sen;
Lightvalve 14:8e7590227d22 1546 one_period_end = 1;
Lightvalve 13:747daba9cf59 1547 }
Lightvalve 30:8d561f16383b 1548 } else if(VALVE_FR_timer == (int) (5.0f * (float) TMR_FREQ_5k)) {
Lightvalve 14:8e7590227d22 1549 FINAL_POS = pos.sen;
Lightvalve 13:747daba9cf59 1550 one_period_end = 1;
Lightvalve 13:747daba9cf59 1551 }
Lightvalve 14:8e7590227d22 1552
Lightvalve 14:8e7590227d22 1553 if(one_period_end == 1) {
Lightvalve 14:8e7590227d22 1554 if(valve_pos.ref > VALVE_MAX_POS) {
Lightvalve 13:747daba9cf59 1555 max_check = 1;
Lightvalve 14:8e7590227d22 1556 } else if(valve_pos.ref < VALVE_MIN_POS) {
Lightvalve 13:747daba9cf59 1557 min_check = 1;
Lightvalve 13:747daba9cf59 1558 }
Lightvalve 13:747daba9cf59 1559 JOINT_VEL[ID_index] = (FINAL_POS - START_POS) / data_num * TMR_FREQ_5k; // pulse/sec
Lightvalve 19:23b7c1ad8683 1560
Lightvalve 19:23b7c1ad8683 1561 //ROM_RESET_DATA();
Lightvalve 13:747daba9cf59 1562 VALVE_FR_timer = 0;
Lightvalve 13:747daba9cf59 1563 one_period_end = 0;
Lightvalve 13:747daba9cf59 1564 ID_index= ID_index +1;
Lightvalve 30:8d561f16383b 1565 V_out = 0.0f;
Lightvalve 13:747daba9cf59 1566 }
Lightvalve 14:8e7590227d22 1567
Lightvalve 14:8e7590227d22 1568 if(max_check == 1 && min_check == 1) {
Lightvalve 19:23b7c1ad8683 1569
Lightvalve 13:747daba9cf59 1570 VALVE_POS_NUM = ID_index;
Lightvalve 27:a2254a485f23 1571 //ROM_RESET_DATA();
Lightvalve 13:747daba9cf59 1572 ID_index = 0;
Lightvalve 13:747daba9cf59 1573 first_check = 0;
Lightvalve 13:747daba9cf59 1574 VALVE_FR_timer = 0;
Lightvalve 56:6f50d9d3bfee 1575 CONTROL_MODE = MODE_NO_ACT;
Lightvalve 49:e7bcfc244d40 1576 // CAN_TX_PRES((int16_t) (VALVE_FR_timer), (int16_t) (6));
Lightvalve 13:747daba9cf59 1577 }
Lightvalve 13:747daba9cf59 1578 }
Lightvalve 13:747daba9cf59 1579 break;
Lightvalve 13:747daba9cf59 1580 }
Lightvalve 14:8e7590227d22 1581
Lightvalve 12:6f2531038ea4 1582 default:
Lightvalve 12:6f2531038ea4 1583 break;
Lightvalve 12:6f2531038ea4 1584 }
Lightvalve 14:8e7590227d22 1585
Lightvalve 56:6f50d9d3bfee 1586 // if (FLAG_VALVE_OUTPUT_CALIB) {
Lightvalve 56:6f50d9d3bfee 1587 // // Valve Dead Zone (Mechanical dead-zone canceling)
Lightvalve 56:6f50d9d3bfee 1588 // // if (CONTROL_MODE != MODE_VALVE_NULLING_AND_DEADZONE_SETTING) {
Lightvalve 56:6f50d9d3bfee 1589 // // if (V_out > 0.) {
Lightvalve 56:6f50d9d3bfee 1590 // // VALVE_PWM_VALVE_DZ = (int) V_out + VALVE_DEADZONE_PLUS;
Lightvalve 56:6f50d9d3bfee 1591 // // } else if (V_out < 0.) {
Lightvalve 56:6f50d9d3bfee 1592 // // VALVE_PWM_VALVE_DZ = (int) V_out + VALVE_DEADZONE_MINUS;
Lightvalve 56:6f50d9d3bfee 1593 // // } else VALVE_PWM_VALVE_DZ = (int) V_out + VALVE_CENTER;
Lightvalve 56:6f50d9d3bfee 1594 // // } else VALVE_PWM_VALVE_DZ = (int) V_out;
Lightvalve 56:6f50d9d3bfee 1595 //
Lightvalve 56:6f50d9d3bfee 1596 // if(V_out>0) V_out = V_out + VALVE_DEADZONE_PLUS;
Lightvalve 56:6f50d9d3bfee 1597 // else if(V_out<0) V_out = V_out + VALVE_DEADZONE_MINUS;
Lightvalve 56:6f50d9d3bfee 1598 //
Lightvalve 56:6f50d9d3bfee 1599 // VALVE_PWM_VALVE_DZ = V_out + VALVE_CENTER;
Lightvalve 56:6f50d9d3bfee 1600 //
Lightvalve 56:6f50d9d3bfee 1601 // // Output Voltage Linearization and Dead Zone Cancellation (Electrical dead-zone)
Lightvalve 56:6f50d9d3bfee 1602 // float CUR_PWM_nonlin = VALVE_PWM_VALVE_DZ/5.0f*1000.0f; // convert PWM duty to mV
Lightvalve 56:6f50d9d3bfee 1603 // float CUR_PWM_DZ = PWM_duty_byLT(CUR_PWM_nonlin);
Lightvalve 56:6f50d9d3bfee 1604 //
Lightvalve 56:6f50d9d3bfee 1605 // if (CUR_PWM_DZ > 0) V_out = (int)CUR_PWM_DZ + 143;
Lightvalve 56:6f50d9d3bfee 1606 // else if (CUR_PWM_DZ < 0) V_out = (int)CUR_PWM_DZ - 138;
Lightvalve 56:6f50d9d3bfee 1607 // else V_out = CUR_PWM_DZ;
Lightvalve 56:6f50d9d3bfee 1608 // } else {
Lightvalve 56:6f50d9d3bfee 1609 // V_out = V_out;
Lightvalve 56:6f50d9d3bfee 1610 // }
Lightvalve 14:8e7590227d22 1611
jobuuu 7:e9086c72bb22 1612 /*******************************************************
jobuuu 7:e9086c72bb22 1613 *** PWM
jobuuu 7:e9086c72bb22 1614 ********************************************************/
Lightvalve 49:e7bcfc244d40 1615 if (V_out >= VALVE_VOLTAGE_LIMIT*1000.0f) {
Lightvalve 49:e7bcfc244d40 1616 V_out = VALVE_VOLTAGE_LIMIT*1000.0f;
Lightvalve 49:e7bcfc244d40 1617 } else if(V_out<=-VALVE_VOLTAGE_LIMIT*1000.0f) {
Lightvalve 49:e7bcfc244d40 1618 V_out = -VALVE_VOLTAGE_LIMIT*1000.0f;
Lightvalve 27:a2254a485f23 1619 }
Lightvalve 49:e7bcfc244d40 1620 PWM_out= V_out/(SUPPLY_VOLTAGE*1000.0f); // Full duty : 12000.0mV
Lightvalve 13:747daba9cf59 1621
Lightvalve 56:6f50d9d3bfee 1622 // Saturation of output voltage to 5.0V
Lightvalve 56:6f50d9d3bfee 1623 // if(PWM_out > 0.41667) PWM_out=0.41667; //5.0/12.0 = 0.41667
Lightvalve 56:6f50d9d3bfee 1624 // else if (PWM_out < -0.41667) PWM_out=-0.41667;
Lightvalve 56:6f50d9d3bfee 1625
Lightvalve 19:23b7c1ad8683 1626 // Saturation of output voltage to 12.0V
Lightvalve 30:8d561f16383b 1627 if(PWM_out > 1.0f) PWM_out=1.0f;
Lightvalve 30:8d561f16383b 1628 else if (PWM_out < -1.0f) PWM_out=-1.0f;
Lightvalve 13:747daba9cf59 1629
Lightvalve 30:8d561f16383b 1630 if (PWM_out>0.0f) {
Lightvalve 30:8d561f16383b 1631 dtc_v=0.0f;
jobuuu 1:e04e563be5ce 1632 dtc_w=PWM_out;
jobuuu 1:e04e563be5ce 1633 } else {
jobuuu 2:a1c0a37df760 1634 dtc_v=-PWM_out;
Lightvalve 30:8d561f16383b 1635 dtc_w=0.0f;
jobuuu 1:e04e563be5ce 1636 }
Lightvalve 13:747daba9cf59 1637
jobuuu 1:e04e563be5ce 1638 //pwm
Lightvalve 30:8d561f16383b 1639 TIM4->CCR2 = (PWM_ARR)*(1.0f-dtc_v);
Lightvalve 30:8d561f16383b 1640 TIM4->CCR1 = (PWM_ARR)*(1.0f-dtc_w);
Lightvalve 13:747daba9cf59 1641
Lightvalve 20:806196fda269 1642
Lightvalve 20:806196fda269 1643
Lightvalve 56:6f50d9d3bfee 1644
Lightvalve 56:6f50d9d3bfee 1645 if (TMR2_COUNT_CAN_TX % (int) ((int) TMR_FREQ_5k/canfreq) == 0) {
Lightvalve 56:6f50d9d3bfee 1646 // if (TMR2_COUNT_CAN_TX % 10 == 0) {
Lightvalve 54:647072f5307a 1647 // Position, Velocity, and Torque (ID:1200)
Lightvalve 56:6f50d9d3bfee 1648 if (flag_data_request[0] == HIGH) {
Lightvalve 56:6f50d9d3bfee 1649 if ((OPERATING_MODE & 0x01) == 0) { // Rotary Actuator
Lightvalve 54:647072f5307a 1650 if (SENSING_MODE == 0) {
Lightvalve 54:647072f5307a 1651 CAN_TX_POSITION_FT((int16_t) (pos.sen), (int16_t) (vel.sen/10.0f), (int16_t) (torq.sen*10.0f));
Lightvalve 54:647072f5307a 1652 } else if (SENSING_MODE == 1) {
Lightvalve 54:647072f5307a 1653 CAN_TX_POSITION_PRESSURE((int16_t) (pos.sen), (int16_t) (vel.sen/10.0f), (int16_t) (pres_A.sen*10.0f), (int16_t) (pres_B.sen*10.0f));
Lightvalve 54:647072f5307a 1654 }
Lightvalve 56:6f50d9d3bfee 1655 } else if ((OPERATING_MODE & 0x01) == 1) { // Linear Actuator
Lightvalve 54:647072f5307a 1656 if (SENSING_MODE == 0) {
Lightvalve 54:647072f5307a 1657 CAN_TX_POSITION_FT((int16_t) (pos.sen/4.0f), (int16_t) (vel.sen/100.0f), (int16_t) (torq.sen*10.0f));
Lightvalve 54:647072f5307a 1658 } else if (SENSING_MODE == 1) {
Lightvalve 54:647072f5307a 1659 CAN_TX_POSITION_PRESSURE((int16_t) (pos.sen/4.0f), (int16_t) (vel.sen/100.0f), (int16_t) (pres_A.sen*10.0f), (int16_t) (pres_B.sen*10.0f));
Lightvalve 54:647072f5307a 1660 }
Lightvalve 52:8ea76864368a 1661 }
Lightvalve 52:8ea76864368a 1662 }
Lightvalve 56:6f50d9d3bfee 1663 if (flag_data_request[1] == HIGH) {
Lightvalve 54:647072f5307a 1664 //valve position
Lightvalve 54:647072f5307a 1665 double t_value = 0;
Lightvalve 56:6f50d9d3bfee 1666 if(value>=DDV_CENTER) {
Lightvalve 56:6f50d9d3bfee 1667 t_value = 10000.0f*((double)value-(double)DDV_CENTER)/((double)VALVE_MAX_POS-(double)DDV_CENTER);
Lightvalve 54:647072f5307a 1668 } else {
Lightvalve 56:6f50d9d3bfee 1669 t_value = -10000.0f*((double)value-(double)DDV_CENTER)/((double)VALVE_MIN_POS-(double)DDV_CENTER);
Lightvalve 54:647072f5307a 1670 }
Lightvalve 56:6f50d9d3bfee 1671 CAN_TX_TORQUE((int16_t) (t_value));
Lightvalve 54:647072f5307a 1672 }
Lightvalve 56:6f50d9d3bfee 1673
Lightvalve 56:6f50d9d3bfee 1674 if (flag_data_request[2] == HIGH) {
Lightvalve 56:6f50d9d3bfee 1675 //pressure A and B
Lightvalve 56:6f50d9d3bfee 1676 CAN_TX_PRES((int16_t) (valve_pos.ref), (int16_t) (MODE_POS_FT_TRANS * 100.0f)); // CUR_PRES_X : 0(0bar)~4096(210bar)
Lightvalve 55:b25725257569 1677 }
Lightvalve 56:6f50d9d3bfee 1678
Lightvalve 56:6f50d9d3bfee 1679 if (flag_data_request[3] == HIGH) {
Lightvalve 54:647072f5307a 1680 //PWM
Lightvalve 56:6f50d9d3bfee 1681 CAN_TX_PWM((int16_t) VALVE_DEADZONE_PLUS);
Lightvalve 54:647072f5307a 1682 }
Lightvalve 56:6f50d9d3bfee 1683
Lightvalve 56:6f50d9d3bfee 1684 if (flag_data_request[4] == HIGH) {
Lightvalve 54:647072f5307a 1685 //valve position
Lightvalve 56:6f50d9d3bfee 1686 CAN_TX_VALVE_POSITION((int16_t) (K_SPRING), (int16_t) (D_DAMPER), (int16_t) VALVE_POS_RAW_FORCE_FB_LOGGING);
Lightvalve 54:647072f5307a 1687 }
Lightvalve 20:806196fda269 1688
Lightvalve 54:647072f5307a 1689 // Others : Reference position, Reference FT, PWM, Current (ID:1300)
Lightvalve 52:8ea76864368a 1690 // if (flag_data_request[1] == HIGH) {
Lightvalve 52:8ea76864368a 1691 // CAN_TX_SOMETHING((int) (FORCE_VREF), (int16_t) (1), (int16_t) (2), (int16_t) (3));
Lightvalve 52:8ea76864368a 1692 // }
Lightvalve 54:647072f5307a 1693 //if (flag_delay_test == 1){
Lightvalve 54:647072f5307a 1694 //CAN_TX_PRES((int16_t) (0),(int16_t) torq_ref);
Lightvalve 54:647072f5307a 1695 //}
Lightvalve 52:8ea76864368a 1696
Lightvalve 54:647072f5307a 1697 TMR2_COUNT_CAN_TX = 0;
Lightvalve 54:647072f5307a 1698 }
Lightvalve 54:647072f5307a 1699 TMR2_COUNT_CAN_TX++;
Lightvalve 52:8ea76864368a 1700
Lightvalve 56:6f50d9d3bfee 1701
Lightvalve 56:6f50d9d3bfee 1702
Lightvalve 20:806196fda269 1703 }
Lightvalve 52:8ea76864368a 1704 TIM3->SR = 0x0; // reset the status register
Lightvalve 52:8ea76864368a 1705
Lightvalve 20:806196fda269 1706 }
Lightvalve 20:806196fda269 1707
Lightvalve 20:806196fda269 1708
Lightvalve 20:806196fda269 1709
Lightvalve 52:8ea76864368a 1710 //unsigned long CNT_TMR5 = 0;
Lightvalve 52:8ea76864368a 1711 //float FREQ_TMR5 = (float)FREQ_500;
Lightvalve 52:8ea76864368a 1712 //float DT_TMR5 = (float)DT_500;
Lightvalve 52:8ea76864368a 1713 //extern "C" void TIM2_IRQHandler(void)
Lightvalve 52:8ea76864368a 1714 //{
Lightvalve 52:8ea76864368a 1715 // LED = 1;
Lightvalve 52:8ea76864368a 1716 // if (TIM2->SR & TIM_SR_UIF ) {
Lightvalve 52:8ea76864368a 1717 //
Lightvalve 52:8ea76864368a 1718 // //CAN ----------------------------------------------------------------------
Lightvalve 56:6f50d9d3bfee 1719 // if (flag_data_request[0] == HIGH) {
Lightvalve 52:8ea76864368a 1720 // //position+velocity
Lightvalve 52:8ea76864368a 1721 // CAN_TX_POSITION_FT((int16_t) (pos.sen/4.0f), (int16_t) (vel.sen/100.0f), (int16_t) (torq.sen*10.0f));
Lightvalve 56:6f50d9d3bfee 1722 // }
Lightvalve 52:8ea76864368a 1723 //
Lightvalve 56:6f50d9d3bfee 1724 // if (flag_data_request[1] == HIGH) {
Lightvalve 52:8ea76864368a 1725 // //valve position
Lightvalve 52:8ea76864368a 1726 // double t_value = 0;
Lightvalve 56:6f50d9d3bfee 1727 // if(value>=DDV_CENTER) {
Lightvalve 56:6f50d9d3bfee 1728 // t_value = 10000.0f*((double)value-(double)DDV_CENTER)/((double)VALVE_MAX_POS-(double)DDV_CENTER);
Lightvalve 52:8ea76864368a 1729 // } else {
Lightvalve 56:6f50d9d3bfee 1730 // t_value = -10000.0f*((double)value-(double)DDV_CENTER)/((double)VALVE_MIN_POS-(double)DDV_CENTER);
Lightvalve 52:8ea76864368a 1731 // }
Lightvalve 52:8ea76864368a 1732 // CAN_TX_TORQUE((int16_t) (t_value));
Lightvalve 56:6f50d9d3bfee 1733 // }
Lightvalve 52:8ea76864368a 1734 //
Lightvalve 56:6f50d9d3bfee 1735 // if (flag_data_request[2] == HIGH) {
Lightvalve 52:8ea76864368a 1736 // //pressure A and B
Lightvalve 52:8ea76864368a 1737 // CAN_TX_PRES((int16_t) (valve_pos.ref), (int16_t) (MODE_POS_FT_TRANS * 100.0f)); // CUR_PRES_X : 0(0bar)~4096(210bar)
Lightvalve 56:6f50d9d3bfee 1738 // //CAN_TX_PRES((int16_t) (pres_A.sen), (int16_t) (pres_B.sen)); // CUR_PRES_X : 0(0bar)~4096(210bar)
Lightvalve 56:6f50d9d3bfee 1739 // // CAN_TX_PRES((int16_t) (CUR_PRES_A_BAR * 100.), (int16_t) (CUR_PRES_B_BAR * 100.));
Lightvalve 56:6f50d9d3bfee 1740 // // CAN_TX_PRES((int16_t) ((DEADZONE_MINUS + 1.)*1000.), (int16_t) ((DEADZONE_PLUS + 1.))*1000.);
Lightvalve 56:6f50d9d3bfee 1741 // // CAN_TX_PRES((int16_t) DZ_dir, (int16_t) ((VALVE_DEADZONE_PLUS + 1.))*1000.);
Lightvalve 55:b25725257569 1742 //
Lightvalve 56:6f50d9d3bfee 1743 // }
Lightvalve 52:8ea76864368a 1744 //
Lightvalve 56:6f50d9d3bfee 1745 // if (flag_data_request[3] == HIGH) {
Lightvalve 55:b25725257569 1746 // //PWM
Lightvalve 52:8ea76864368a 1747 // CAN_TX_PWM((int16_t) VALVE_DEADZONE_PLUS);
Lightvalve 52:8ea76864368a 1748 // // CAN_TX_PWM((int16_t) cnt_vel_findhome);
Lightvalve 52:8ea76864368a 1749 // // CAN_TX_PWM((int16_t) (VALVE_VOLTAGE * 1000.));
Lightvalve 52:8ea76864368a 1750 // // CAN_TX_PWM((int16_t) (VALVE_VOLTAGE_VALVE_DZ * 1000.));
Lightvalve 52:8ea76864368a 1751 //
Lightvalve 56:6f50d9d3bfee 1752 // }
Lightvalve 52:8ea76864368a 1753 //
Lightvalve 56:6f50d9d3bfee 1754 // if (flag_data_request[4] == HIGH) {
Lightvalve 56:6f50d9d3bfee 1755 // //valve position
Lightvalve 52:8ea76864368a 1756 // CAN_TX_VALVE_POSITION((int16_t) (K_SPRING), (int16_t) (D_DAMPER), (int16_t) VALVE_POS_RAW_FORCE_FB_LOGGING);
Lightvalve 56:6f50d9d3bfee 1757 // //CAN_TX_VALVE_POSITION((int16_t) (DDV_CENTER * 10.0f), (int16_t) valve_pos.ref, (int16_t) V_out);
Lightvalve 52:8ea76864368a 1758 //
Lightvalve 52:8ea76864368a 1759 // //CAN_TX_VALVE_POSITION((int16_t) (VALVE_POS_NUM));
Lightvalve 52:8ea76864368a 1760 // // CAN_TX_VALVE_POSITIOfxN((int16_t) (VALVE_FF_VOLTAGE / SUPPLY_VOLTAGE));
Lightvalve 52:8ea76864368a 1761 // // CAN_TX_VALVE_POSITION((int16_t) P_GAIN_JOINT_POSITION);
Lightvalve 52:8ea76864368a 1762 // // CAN_TX_VALVE_POSITION((int16_t) Ref_Joint_Pos);
Lightvalve 52:8ea76864368a 1763 // // CAN_TX_VALVE_POSITION((int16_t) flag_flowrate);
Lightvalve 56:6f50d9d3bfee 1764 // }
Lightvalve 52:8ea76864368a 1765 //
Lightvalve 52:8ea76864368a 1766 //
Lightvalve 52:8ea76864368a 1767 // }
Lightvalve 52:8ea76864368a 1768 // TIM2->SR = 0x0; // reset the status register
Lightvalve 52:8ea76864368a 1769 //}
Lightvalve 52:8ea76864368a 1770
Lightvalve 52:8ea76864368a 1771
Lightvalve 56:6f50d9d3bfee 1772
Lightvalve 56:6f50d9d3bfee 1773 void CurrentControl()
Lightvalve 56:6f50d9d3bfee 1774 {
Lightvalve 56:6f50d9d3bfee 1775 cur.err = cur.ref - cur.sen;
Lightvalve 56:6f50d9d3bfee 1776 cur.err_int = cur.err_int + cur.err*DT_TMR4;
Lightvalve 56:6f50d9d3bfee 1777 cur.err_diff = (cur.err - cur.err_old)*FREQ_TMR4;
Lightvalve 56:6f50d9d3bfee 1778 cur.err_old = cur.err;
Lightvalve 56:6f50d9d3bfee 1779
Lightvalve 56:6f50d9d3bfee 1780 float R_model = 150.0f; // ohm
Lightvalve 56:6f50d9d3bfee 1781 float L_model = 0.3f;
Lightvalve 56:6f50d9d3bfee 1782 float w0 = 2.0f*3.14f*90.0f;
Lightvalve 56:6f50d9d3bfee 1783 float KP_I = L_model*w0;
Lightvalve 56:6f50d9d3bfee 1784 float KI_I = R_model*w0;
Lightvalve 56:6f50d9d3bfee 1785 float KD_I = 0.0f;
Lightvalve 56:6f50d9d3bfee 1786
Lightvalve 56:6f50d9d3bfee 1787 float FF_gain = 0.0f;
Lightvalve 56:6f50d9d3bfee 1788 V_out = (int) (KP_I * cur.err + KI_I * cur.err_int + KD_I * cur.err_diff);
Lightvalve 56:6f50d9d3bfee 1789 // V_out = V_out + FF_gain * (R_model*I_REF); // Unit : mV
Lightvalve 56:6f50d9d3bfee 1790 V_out = V_out + FF_gain * (R_model*cur.ref + L_model*cur.ref_diff); // Unit : mV
Lightvalve 56:6f50d9d3bfee 1791
Lightvalve 56:6f50d9d3bfee 1792 float Ka = 5.0f/KP_I;
Lightvalve 56:6f50d9d3bfee 1793 if(V_out > V_MAX) {
Lightvalve 56:6f50d9d3bfee 1794 V_rem = V_out-V_MAX;
Lightvalve 56:6f50d9d3bfee 1795 V_rem = Ka*V_rem;
Lightvalve 56:6f50d9d3bfee 1796 V_out = V_MAX;
Lightvalve 56:6f50d9d3bfee 1797 cur.err_int = cur.err_int - V_rem*DT_5k;
Lightvalve 56:6f50d9d3bfee 1798 } else if(V_out < -V_MAX) {
Lightvalve 56:6f50d9d3bfee 1799 V_rem = V_out-(-V_MAX);
Lightvalve 56:6f50d9d3bfee 1800 V_rem = Ka*V_rem;
Lightvalve 56:6f50d9d3bfee 1801 V_out = -V_MAX;
Lightvalve 56:6f50d9d3bfee 1802 cur.err_int = cur.err_int - V_rem*DT_5k;
Lightvalve 56:6f50d9d3bfee 1803 }
Lightvalve 56:6f50d9d3bfee 1804 }
Lightvalve 23:59218d4a256d 1805
Lightvalve 28:2a62d73e3dd0 1806
Lightvalve 30:8d561f16383b 1807
Lightvalve 32:4b8c0fedaf2c 1808
Lightvalve 36:a46e63505ed8 1809
Lightvalve 42:1cf66990ccab 1810
Lightvalve 44:fe7d5cfd2eea 1811
Lightvalve 50:3c630b5eba9f 1812
Lightvalve 56:6f50d9d3bfee 1813