2011

Dependencies:   mbed FastPWM

Committer:
Lightvalve
Date:
Mon Sep 09 10:10:31 2019 +0000
Revision:
21:e5f1a43ea6f9
Parent:
20:806196fda269
Child:
23:59218d4a256d
190909_19_10

Who changed what in which revision?

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