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