2011

Dependencies:   mbed FastPWM

Committer:
Lightvalve
Date:
Tue Jan 14 09:11:20 2020 +0000
Revision:
43:fe7d5cfd2eea
Parent:
42:b084e5f5d0d5
Child:
44:35fa6884d0c6
20200114

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