20210203

Dependencies:   mbed FastPWM

Committer:
Lightvalve
Date:
Thu Aug 29 07:38:00 2019 +0000
Revision:
14:8e7590227d22
Parent:
13:747daba9cf59
Child:
15:bd0d12728506
190829

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