rainbow

Dependencies:   mbed FastPWM

Committer:
Lightvalve
Date:
Tue Sep 10 09:38:26 2019 +0000
Revision:
23:59218d4a256d
Parent:
21:e5f1a43ea6f9
Child:
24:ef6e1092e9e6
190910

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