rainbow

Dependencies:   mbed FastPWM

Committer:
Lightvalve
Date:
Mon Sep 09 06:29:56 2019 +0000
Revision:
19:23b7c1ad8683
Parent:
18:b8adf1582ea3
Child:
20:806196fda269
190909

Who changed what in which revision?

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