![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
asdgas
Dependencies: mbed Eigen FastPWM
main.cpp@59:f308b1656d9c, 2020-03-16 (annotated)
- Committer:
- Lightvalve
- Date:
- Mon Mar 16 04:50:54 2020 +0000
- Revision:
- 59:f308b1656d9c
- Parent:
- 58:2eade98630e2
- Child:
- 60:64181f1d3e60
200316
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 | |
Lightvalve | 31:66738bfecec5 | 12 | ///191008//// |
Lightvalve | 31:66738bfecec5 | 13 | |
jobuuu | 7:e9086c72bb22 | 14 | // dac & check /////////////////////////////////////////// |
GiJeongKim | 0:51c43836c1d7 | 15 | DigitalOut check(PC_2); |
GiJeongKim | 0:51c43836c1d7 | 16 | DigitalOut check_2(PC_3); |
GiJeongKim | 0:51c43836c1d7 | 17 | AnalogOut dac_1(PA_4); |
GiJeongKim | 0:51c43836c1d7 | 18 | AnalogOut dac_2(PA_5); |
Lightvalve | 14:8e7590227d22 | 19 | AnalogIn adc1(PC_4); //pressure_1 |
Lightvalve | 14:8e7590227d22 | 20 | AnalogIn adc2(PB_0); //pressure_2 |
Lightvalve | 14:8e7590227d22 | 21 | AnalogIn adc3(PC_1); //current |
GiJeongKim | 0:51c43836c1d7 | 22 | |
Lightvalve | 24:ef6e1092e9e6 | 23 | |
jobuuu | 7:e9086c72bb22 | 24 | // PWM /////////////////////////////////////////// |
Lightvalve | 30:8d561f16383b | 25 | float dtc_v=0.0f; |
Lightvalve | 30:8d561f16383b | 26 | float dtc_w=0.0f; |
GiJeongKim | 0:51c43836c1d7 | 27 | |
jobuuu | 7:e9086c72bb22 | 28 | // I2C /////////////////////////////////////////// |
GiJeongKim | 0:51c43836c1d7 | 29 | I2C i2c(PC_9,PA_8); // SDA, SCL (for K22F) |
GiJeongKim | 0:51c43836c1d7 | 30 | const int i2c_slave_addr1 = 0x56; |
GiJeongKim | 0:51c43836c1d7 | 31 | unsigned int value; // 10bit output of reading sensor AS5510 |
GiJeongKim | 0:51c43836c1d7 | 32 | |
jobuuu | 7:e9086c72bb22 | 33 | // SPI /////////////////////////////////////////// |
Lightvalve | 16:903b5a4433b4 | 34 | //SPI eeprom(PB_15, PB_14, PB_13); // EEPROM //(SPI_MOSI, SPI_MISO, SPI_SCK); |
Lightvalve | 16:903b5a4433b4 | 35 | //DigitalOut eeprom_cs(PB_12); |
Lightvalve | 16:903b5a4433b4 | 36 | //FlashWriter writer(6);//2부터 7까지 되는듯 아마 sector |
GiJeongKim | 0:51c43836c1d7 | 37 | SPI enc(PC_12,PC_11,PC_10); |
GiJeongKim | 0:51c43836c1d7 | 38 | DigitalOut enc_cs(PD_2); |
Lightvalve | 11:82d8768d7351 | 39 | DigitalOut LED(PA_15); |
GiJeongKim | 0:51c43836c1d7 | 40 | |
jobuuu | 7:e9086c72bb22 | 41 | // UART /////////////////////////////////////////// |
GiJeongKim | 0:51c43836c1d7 | 42 | Serial pc(PA_9,PA_10); // _ UART |
GiJeongKim | 0:51c43836c1d7 | 43 | |
jobuuu | 7:e9086c72bb22 | 44 | // CAN /////////////////////////////////////////// |
GiJeongKim | 0:51c43836c1d7 | 45 | CAN can(PB_8, PB_9, 1000000); |
GiJeongKim | 0:51c43836c1d7 | 46 | CANMessage msg; |
Lightvalve | 11:82d8768d7351 | 47 | void onMsgReceived() |
Lightvalve | 11:82d8768d7351 | 48 | { |
Lightvalve | 11:82d8768d7351 | 49 | CAN_RX_HANDLER(); |
Lightvalve | 11:82d8768d7351 | 50 | } |
jobuuu | 2:a1c0a37df760 | 51 | |
jobuuu | 7:e9086c72bb22 | 52 | // Variables /////////////////////////////////////////// |
jobuuu | 7:e9086c72bb22 | 53 | State pos; |
jobuuu | 7:e9086c72bb22 | 54 | State vel; |
jobuuu | 7:e9086c72bb22 | 55 | State Vout; |
jobuuu | 7:e9086c72bb22 | 56 | State torq; |
jobuuu | 7:e9086c72bb22 | 57 | State pres_A; |
jobuuu | 7:e9086c72bb22 | 58 | State pres_B; |
jobuuu | 7:e9086c72bb22 | 59 | State cur; |
Lightvalve | 14:8e7590227d22 | 60 | State valve_pos; |
Lightvalve | 14:8e7590227d22 | 61 | |
Lightvalve | 14:8e7590227d22 | 62 | State INIT_Vout; |
Lightvalve | 14:8e7590227d22 | 63 | State INIT_Valve_Pos; |
Lightvalve | 14:8e7590227d22 | 64 | State INIT_Pos; |
Lightvalve | 14:8e7590227d22 | 65 | State INIT_torq; |
jobuuu | 5:a4319f79457b | 66 | |
Lightvalve | 19:23b7c1ad8683 | 67 | extern int CID_RX_CMD; |
Lightvalve | 19:23b7c1ad8683 | 68 | extern int CID_RX_REF_POSITION; |
Lightvalve | 49:e7bcfc244d40 | 69 | extern int CID_RX_REF_VALVE_POS; |
Lightvalve | 45:35fa6884d0c6 | 70 | extern int CID_RX_REF_PWM; |
Lightvalve | 19:23b7c1ad8683 | 71 | |
Lightvalve | 19:23b7c1ad8683 | 72 | extern int CID_TX_INFO; |
Lightvalve | 19:23b7c1ad8683 | 73 | extern int CID_TX_POSITION; |
Lightvalve | 19:23b7c1ad8683 | 74 | extern int CID_TX_TORQUE; |
Lightvalve | 19:23b7c1ad8683 | 75 | extern int CID_TX_PRES; |
Lightvalve | 19:23b7c1ad8683 | 76 | extern int CID_TX_VOUT; |
Lightvalve | 19:23b7c1ad8683 | 77 | extern int CID_TX_VALVE_POSITION; |
Lightvalve | 19:23b7c1ad8683 | 78 | |
jobuuu | 7:e9086c72bb22 | 79 | // ============================================================================= |
jobuuu | 7:e9086c72bb22 | 80 | // ============================================================================= |
jobuuu | 7:e9086c72bb22 | 81 | // ============================================================================= |
jobuuu | 7:e9086c72bb22 | 82 | |
Lightvalve | 12:6f2531038ea4 | 83 | /******************************************************************************* |
Lightvalve | 12:6f2531038ea4 | 84 | * REFERENCE MODE |
Lightvalve | 12:6f2531038ea4 | 85 | ******************************************************************************/ |
Lightvalve | 13:747daba9cf59 | 86 | enum _REFERENCE_MODE { |
Lightvalve | 13:747daba9cf59 | 87 | MODE_REF_NO_ACT = 0, //0 |
Lightvalve | 12:6f2531038ea4 | 88 | MODE_REF_DIRECT, //1 |
Lightvalve | 12:6f2531038ea4 | 89 | MODE_REF_COS_INC, //2 |
Lightvalve | 12:6f2531038ea4 | 90 | MODE_REF_LINE_INC, //3 |
Lightvalve | 12:6f2531038ea4 | 91 | MODE_REF_SIN_WAVE, //4 |
Lightvalve | 12:6f2531038ea4 | 92 | MODE_REF_SQUARE_WAVE, //5 |
Lightvalve | 12:6f2531038ea4 | 93 | }; |
Lightvalve | 12:6f2531038ea4 | 94 | |
Lightvalve | 12:6f2531038ea4 | 95 | /******************************************************************************* |
Lightvalve | 12:6f2531038ea4 | 96 | * CONTROL MODE |
Lightvalve | 12:6f2531038ea4 | 97 | ******************************************************************************/ |
Lightvalve | 13:747daba9cf59 | 98 | enum _CONTROL_MODE { |
Lightvalve | 12:6f2531038ea4 | 99 | //control mode |
Lightvalve | 12:6f2531038ea4 | 100 | MODE_NO_ACT = 0, //0 |
Lightvalve | 47:fdcb8bd86fd6 | 101 | MODE_VALVE_POSITION_CONTROL, //1 |
Lightvalve | 47:fdcb8bd86fd6 | 102 | MODE_JOINT_CONTROL, //2 |
Lightvalve | 50:3c630b5eba9f | 103 | |
Lightvalve | 47:fdcb8bd86fd6 | 104 | MODE_VALVE_OPEN_LOOP, //3 |
Lightvalve | 12:6f2531038ea4 | 105 | MODE_JOINT_POSITION_TORQUE_CONTROL_VALVE_POSITION, //4 |
Lightvalve | 12:6f2531038ea4 | 106 | MODE_VALVE_POSITION_TORQUE_CONTROL_LEARNING, //5 |
Lightvalve | 13:747daba9cf59 | 107 | |
Lightvalve | 12:6f2531038ea4 | 108 | MODE_JOINT_POSITION_PRES_CONTROL_PWM, //6 |
Lightvalve | 12:6f2531038ea4 | 109 | MODE_JOINT_POSITION_PRES_CONTROL_VALVE_POSITION, //7 |
Lightvalve | 12:6f2531038ea4 | 110 | MODE_VALVE_POSITION_PRES_CONTROL_LEARNING, //8 |
Lightvalve | 13:747daba9cf59 | 111 | |
Lightvalve | 12:6f2531038ea4 | 112 | MODE_TEST_CURRENT_CONTROL, //9 |
Lightvalve | 12:6f2531038ea4 | 113 | MODE_TEST_PWM_CONTROL, //10 |
Lightvalve | 21:e5f1a43ea6f9 | 114 | |
Lightvalve | 14:8e7590227d22 | 115 | MODE_CURRENT_CONTROL, //11 |
Lightvalve | 14:8e7590227d22 | 116 | MODE_JOINT_POSITION_TORQUE_CONTROL_CURRENT, //12 |
Lightvalve | 14:8e7590227d22 | 117 | MODE_JOINT_POSITION_PRES_CONTROL_CURRENT, //13 |
Lightvalve | 13:747daba9cf59 | 118 | |
Lightvalve | 12:6f2531038ea4 | 119 | //utility |
Lightvalve | 12:6f2531038ea4 | 120 | MODE_TORQUE_SENSOR_NULLING = 20, //20 |
Lightvalve | 12:6f2531038ea4 | 121 | MODE_VALVE_NULLING_AND_DEADZONE_SETTING, //21 |
Lightvalve | 12:6f2531038ea4 | 122 | MODE_FIND_HOME, //22 |
Lightvalve | 57:f4819de54e7a | 123 | MODE_VALVE_GAIN_SETTING, //23 |
Lightvalve | 12:6f2531038ea4 | 124 | MODE_PRESSURE_SENSOR_NULLING, //24 |
Lightvalve | 12:6f2531038ea4 | 125 | MODE_PRESSURE_SENSOR_CALIB, //25 |
Lightvalve | 12:6f2531038ea4 | 126 | MODE_ROTARY_FRICTION_TUNING, //26 |
Lightvalve | 13:747daba9cf59 | 127 | |
Lightvalve | 12:6f2531038ea4 | 128 | MODE_DDV_POS_VS_PWM_ID = 30, //30 |
Lightvalve | 12:6f2531038ea4 | 129 | MODE_DDV_DEADZONE_AND_CENTER, //31 |
Lightvalve | 12:6f2531038ea4 | 130 | MODE_DDV_POS_VS_FLOWRATE, //32 |
Lightvalve | 57:f4819de54e7a | 131 | MODE_SYSTEM_ID, //33 |
Lightvalve | 12:6f2531038ea4 | 132 | }; |
Lightvalve | 12:6f2531038ea4 | 133 | |
GiJeongKim | 0:51c43836c1d7 | 134 | int main() |
GiJeongKim | 0:51c43836c1d7 | 135 | { |
jobuuu | 6:df07d3491e3a | 136 | /********************************* |
jobuuu | 1:e04e563be5ce | 137 | *** Initialization |
jobuuu | 6:df07d3491e3a | 138 | *********************************/ |
Lightvalve | 57:f4819de54e7a | 139 | LED = 1; |
Lightvalve | 19:23b7c1ad8683 | 140 | //pc.baud(9600); |
Lightvalve | 21:e5f1a43ea6f9 | 141 | |
GiJeongKim | 0:51c43836c1d7 | 142 | // i2c init |
Lightvalve | 8:5d2eebdad025 | 143 | i2c.frequency(400 * 1000); // 0.4 mHz |
Lightvalve | 8:5d2eebdad025 | 144 | wait_ms(2); // Power Up wait |
Lightvalve | 8:5d2eebdad025 | 145 | look_for_hardware_i2c(); // Hardware present |
Lightvalve | 8:5d2eebdad025 | 146 | init_as5510(i2c_slave_addr1); |
Lightvalve | 11:82d8768d7351 | 147 | make_delay(); |
jobuuu | 2:a1c0a37df760 | 148 | |
GiJeongKim | 0:51c43836c1d7 | 149 | // // spi init |
Lightvalve | 16:903b5a4433b4 | 150 | //eeprom.format(8,3); |
Lightvalve | 16:903b5a4433b4 | 151 | //eeprom.frequency(5000000); //5M |
GiJeongKim | 0:51c43836c1d7 | 152 | enc.format(8,0); |
GiJeongKim | 0:51c43836c1d7 | 153 | enc.frequency(5000000); //5M |
Lightvalve | 11:82d8768d7351 | 154 | make_delay(); |
Lightvalve | 21:e5f1a43ea6f9 | 155 | |
Lightvalve | 16:903b5a4433b4 | 156 | //rom |
Lightvalve | 19:23b7c1ad8683 | 157 | ROM_CALL_DATA(); |
Lightvalve | 16:903b5a4433b4 | 158 | make_delay(); |
Lightvalve | 13:747daba9cf59 | 159 | |
GiJeongKim | 0:51c43836c1d7 | 160 | // ADC init |
jobuuu | 5:a4319f79457b | 161 | Init_ADC(); |
Lightvalve | 11:82d8768d7351 | 162 | make_delay(); |
jobuuu | 2:a1c0a37df760 | 163 | |
GiJeongKim | 0:51c43836c1d7 | 164 | // Pwm init |
GiJeongKim | 0:51c43836c1d7 | 165 | Init_PWM(); |
GiJeongKim | 0:51c43836c1d7 | 166 | TIM4->CR1 ^= TIM_CR1_UDIS; |
Lightvalve | 11:82d8768d7351 | 167 | make_delay(); |
Lightvalve | 13:747daba9cf59 | 168 | |
Lightvalve | 11:82d8768d7351 | 169 | // TMR3 init |
Lightvalve | 11:82d8768d7351 | 170 | Init_TMR3(); |
Lightvalve | 11:82d8768d7351 | 171 | TIM3->CR1 ^= TIM_CR1_UDIS; |
Lightvalve | 11:82d8768d7351 | 172 | make_delay(); |
Lightvalve | 21:e5f1a43ea6f9 | 173 | |
Lightvalve | 50:3c630b5eba9f | 174 | // TMR2 init |
Lightvalve | 56:6f50d9d3bfee | 175 | // Init_TMR2(); |
Lightvalve | 56:6f50d9d3bfee | 176 | // TIM2->CR1 ^= TIM_CR1_UDIS; |
Lightvalve | 56:6f50d9d3bfee | 177 | // make_delay(); |
Lightvalve | 21:e5f1a43ea6f9 | 178 | |
GiJeongKim | 0:51c43836c1d7 | 179 | // CAN |
jobuuu | 2:a1c0a37df760 | 180 | can.attach(&CAN_RX_HANDLER); |
Lightvalve | 11:82d8768d7351 | 181 | CAN_ID_INIT(); |
Lightvalve | 11:82d8768d7351 | 182 | make_delay(); |
Lightvalve | 34:bb2ca2fc2a8e | 183 | |
Lightvalve | 23:59218d4a256d | 184 | //Timer priority |
Lightvalve | 23:59218d4a256d | 185 | NVIC_SetPriority(TIM3_IRQn, 2); |
Lightvalve | 57:f4819de54e7a | 186 | //NVIC_SetPriority(TIM2_IRQn, 3); |
Lightvalve | 54:647072f5307a | 187 | NVIC_SetPriority(TIM4_IRQn, 3); |
Lightvalve | 34:bb2ca2fc2a8e | 188 | |
Lightvalve | 23:59218d4a256d | 189 | //can.reset(); |
Lightvalve | 19:23b7c1ad8683 | 190 | can.filter(msg.id, 0xFFFFF000, CANStandard); |
Lightvalve | 34:bb2ca2fc2a8e | 191 | |
GiJeongKim | 0:51c43836c1d7 | 192 | // spi _ enc |
GiJeongKim | 0:51c43836c1d7 | 193 | spi_enc_set_init(); |
Lightvalve | 11:82d8768d7351 | 194 | make_delay(); |
Lightvalve | 13:747daba9cf59 | 195 | |
Lightvalve | 11:82d8768d7351 | 196 | //DAC init |
Lightvalve | 58:2eade98630e2 | 197 | if (SENSING_MODE == 0) { |
Lightvalve | 58:2eade98630e2 | 198 | dac_1 = TORQUE_VREF / 3.3f; |
Lightvalve | 58:2eade98630e2 | 199 | dac_2 = 0.0f; |
Lightvalve | 58:2eade98630e2 | 200 | } else if (SENSING_MODE == 1) { |
Lightvalve | 58:2eade98630e2 | 201 | dac_1 = PRES_A_VREF / 3.3f; |
Lightvalve | 58:2eade98630e2 | 202 | dac_2 = PRES_B_VREF / 3.3f; |
Lightvalve | 58:2eade98630e2 | 203 | } |
Lightvalve | 11:82d8768d7351 | 204 | make_delay(); |
Lightvalve | 13:747daba9cf59 | 205 | |
Lightvalve | 19:23b7c1ad8683 | 206 | for (int i=0; i<50; i++) { |
Lightvalve | 11:82d8768d7351 | 207 | if(i%2==0) |
Lightvalve | 38:118df027d851 | 208 | ID_index_array[i] = - i * 0.5f; |
Lightvalve | 11:82d8768d7351 | 209 | else |
Lightvalve | 38:118df027d851 | 210 | ID_index_array[i] = (i+1) * 0.5f; |
Lightvalve | 11:82d8768d7351 | 211 | } |
Lightvalve | 58:2eade98630e2 | 212 | |
jobuuu | 6:df07d3491e3a | 213 | /************************************ |
jobuuu | 1:e04e563be5ce | 214 | *** Program is operating! |
jobuuu | 6:df07d3491e3a | 215 | *************************************/ |
GiJeongKim | 0:51c43836c1d7 | 216 | while(1) { |
Lightvalve | 57:f4819de54e7a | 217 | if(timer_while==1000 && OPERATING_MODE==5) { |
Lightvalve | 17:1865016ca2e7 | 218 | //i2c |
Lightvalve | 17:1865016ca2e7 | 219 | read_field(i2c_slave_addr1); |
Lightvalve | 17:1865016ca2e7 | 220 | if(DIR_VALVE_ENC < 0) value = 1023 - value; |
Lightvalve | 57:f4819de54e7a | 221 | // if(LED==1) { |
Lightvalve | 57:f4819de54e7a | 222 | // LED=0; |
Lightvalve | 57:f4819de54e7a | 223 | // } else |
Lightvalve | 57:f4819de54e7a | 224 | // LED = 1; |
Lightvalve | 17:1865016ca2e7 | 225 | timer_while = 0; |
Lightvalve | 17:1865016ca2e7 | 226 | } |
Lightvalve | 17:1865016ca2e7 | 227 | timer_while ++; |
GiJeongKim | 0:51c43836c1d7 | 228 | } |
jobuuu | 1:e04e563be5ce | 229 | } |
jobuuu | 1:e04e563be5ce | 230 | |
Lightvalve | 33:91b17819ec30 | 231 | float DDV_JOINT_POS_FF(float REF_JOINT_VEL) |
Lightvalve | 14:8e7590227d22 | 232 | { |
Lightvalve | 14:8e7590227d22 | 233 | |
Lightvalve | 13:747daba9cf59 | 234 | int i = 0; |
Lightvalve | 48:889798ff9329 | 235 | float Ref_Valve_Pos_FF = 0.0f; |
Lightvalve | 14:8e7590227d22 | 236 | for(i=0; i<VALVE_POS_NUM; i++) { |
Lightvalve | 14:8e7590227d22 | 237 | 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 | 238 | if(i==0) { |
Lightvalve | 50:3c630b5eba9f | 239 | if(JOINT_VEL[i+1] == JOINT_VEL[i]) { |
Lightvalve | 57:f4819de54e7a | 240 | Ref_Valve_Pos_FF = (float) VALVE_CENTER; |
Lightvalve | 50:3c630b5eba9f | 241 | } else { |
Lightvalve | 57:f4819de54e7a | 242 | Ref_Valve_Pos_FF = ((float) 10/(JOINT_VEL[i+1] - JOINT_VEL[i]) * (REF_JOINT_VEL - JOINT_VEL[i])) + (float) VALVE_CENTER; |
Lightvalve | 38:118df027d851 | 243 | } |
Lightvalve | 14:8e7590227d22 | 244 | } else { |
Lightvalve | 50:3c630b5eba9f | 245 | if(JOINT_VEL[i+1] == JOINT_VEL[i-1]) { |
Lightvalve | 57:f4819de54e7a | 246 | Ref_Valve_Pos_FF = (float) VALVE_CENTER; |
Lightvalve | 50:3c630b5eba9f | 247 | } else { |
Lightvalve | 57:f4819de54e7a | 248 | Ref_Valve_Pos_FF = ((float) 10*(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])) + (float) VALVE_CENTER + (float) (10*ID_index_array[i-1]); |
Lightvalve | 38:118df027d851 | 249 | } |
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 | 33:91b17819ec30 | 255 | Ref_Valve_Pos_FF = (float) 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 | 33:91b17819ec30 | 257 | Ref_Valve_Pos_FF = (float) VALVE_MIN_POS; |
Lightvalve | 13:747daba9cf59 | 258 | } |
Lightvalve | 36:a46e63505ed8 | 259 | |
Lightvalve | 57:f4819de54e7a | 260 | Ref_Valve_Pos_FF = (float) VELOCITY_COMP_GAIN * 0.01f * (float) (Ref_Valve_Pos_FF - (float) VALVE_CENTER); |
Lightvalve | 13:747daba9cf59 | 261 | return Ref_Valve_Pos_FF; |
Lightvalve | 50:3c630b5eba9f | 262 | |
Lightvalve | 13:747daba9cf59 | 263 | } |
jobuuu | 6:df07d3491e3a | 264 | |
jobuuu | 6:df07d3491e3a | 265 | |
Lightvalve | 30:8d561f16383b | 266 | void VALVE_POS_CONTROL(float REF_VALVE_POS) |
Lightvalve | 14:8e7590227d22 | 267 | { |
Lightvalve | 13:747daba9cf59 | 268 | int i = 0; |
Lightvalve | 13:747daba9cf59 | 269 | |
Lightvalve | 38:118df027d851 | 270 | if(REF_VALVE_POS > VALVE_MAX_POS) { |
Lightvalve | 38:118df027d851 | 271 | REF_VALVE_POS = VALVE_MAX_POS; |
Lightvalve | 38:118df027d851 | 272 | } else if(REF_VALVE_POS < VALVE_MIN_POS) { |
Lightvalve | 38:118df027d851 | 273 | REF_VALVE_POS = VALVE_MIN_POS; |
Lightvalve | 38:118df027d851 | 274 | } |
Lightvalve | 38:118df027d851 | 275 | |
Lightvalve | 13:747daba9cf59 | 276 | valve_pos_err = REF_VALVE_POS - value; |
Lightvalve | 13:747daba9cf59 | 277 | valve_pos_err_diff = valve_pos_err - valve_pos_err_old; |
Lightvalve | 13:747daba9cf59 | 278 | valve_pos_err_old = valve_pos_err; |
Lightvalve | 13:747daba9cf59 | 279 | valve_pos_err_sum += valve_pos_err; |
Lightvalve | 13:747daba9cf59 | 280 | if (valve_pos_err_sum > 1000) valve_pos_err_sum = 1000; |
Lightvalve | 13:747daba9cf59 | 281 | if (valve_pos_err_sum<-1000) valve_pos_err_sum = -1000; |
Lightvalve | 13:747daba9cf59 | 282 | |
Lightvalve | 13:747daba9cf59 | 283 | 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 | 284 | |
Lightvalve | 18:b8adf1582ea3 | 285 | for(i=0; i<24; i++) { |
Lightvalve | 14:8e7590227d22 | 286 | 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 | 287 | if(i==0) { |
Lightvalve | 48:889798ff9329 | 288 | VALVE_PWM_RAW_FF = (float) 1000.0f / (float) (VALVE_POS_VS_PWM[i+1] - VALVE_POS_VS_PWM[i]) * ((float) REF_VALVE_POS - VALVE_POS_VS_PWM[i]); |
Lightvalve | 14:8e7590227d22 | 289 | } else { |
Lightvalve | 48:889798ff9329 | 290 | VALVE_PWM_RAW_FF = (float) 1000.0f* (float) (ID_index_array[i+1] - ID_index_array[i-1])/(VALVE_POS_VS_PWM[i+1] - VALVE_POS_VS_PWM[i-1]) * ((float) REF_VALVE_POS - VALVE_POS_VS_PWM[i-1]) + 1000.0f * (float) ID_index_array[i-1]; |
Lightvalve | 13:747daba9cf59 | 291 | } |
Lightvalve | 13:747daba9cf59 | 292 | break; |
Lightvalve | 13:747daba9cf59 | 293 | } |
Lightvalve | 13:747daba9cf59 | 294 | } |
Lightvalve | 59:f308b1656d9c | 295 | Vout.ref = VALVE_PWM_RAW_FF + VALVE_PWM_RAW_FB; |
Lightvalve | 13:747daba9cf59 | 296 | } |
Lightvalve | 13:747daba9cf59 | 297 | |
Lightvalve | 14:8e7590227d22 | 298 | #define LT_MAX_IDX 57 |
Lightvalve | 30:8d561f16383b | 299 | float LT_PWM_duty[LT_MAX_IDX] = {-100.0f, -80.0f, -60.0f, -50.0f, -40.0f, -35.0f, -30.0f, -25.0f, -20.0f, |
Lightvalve | 34:bb2ca2fc2a8e | 300 | -19.0f, -18.0f, -17.0f, -16.0f, -15.0f, -14.0f, -13.0f, -12.0f, -11.0f, -10.0f, |
Lightvalve | 34:bb2ca2fc2a8e | 301 | -9.0f, -8.0f, -7.0f, -6.0f, -5.0f, -4.0f, -3.0f, -2.0f, -1.0f, 0.0f, |
Lightvalve | 34:bb2ca2fc2a8e | 302 | 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f, |
Lightvalve | 34:bb2ca2fc2a8e | 303 | 11.0f, 12.0f, 13.0f, 14.0f, 15.0f, 16.0f, 17.0f, 18.0f, 19.0f, 20.0f, |
Lightvalve | 34:bb2ca2fc2a8e | 304 | 25.0f, 30.0f, 35.0f, 40.0f, 50.0f, 60.0f, 80.0f, 100.0f |
Lightvalve | 34:bb2ca2fc2a8e | 305 | }; // duty |
Lightvalve | 30:8d561f16383b | 306 | float LT_Voltage_Output[LT_MAX_IDX] = {-321.4f, -291.3f, -261.5f, -246.8f, -231.7f, -223.9f, -216.1f, -207.9f, -198.8f, |
Lightvalve | 34:bb2ca2fc2a8e | 307 | -196.9f, -195.0f, -192.5f, -188.8f, -184.5f, -180.2f, -175.9f, -171.5f, -166.3f, -161.0f, |
Lightvalve | 34:bb2ca2fc2a8e | 308 | -156.0f, -149.5f, -139.0f, -126.0f, -107.0f, -87.5f, -64.0f, -38.5f, -9.4f, 0.0f, |
Lightvalve | 34:bb2ca2fc2a8e | 309 | 12.0f, 43.5f, 69.0f, 94.0f, 114.0f, 132.0f, 146.0f, 155.5f, 162.3f, 168.2f, |
Lightvalve | 34:bb2ca2fc2a8e | 310 | 173.1f, 178.2f, 182.8f, 187.4f, 191.8f, 196.0f, 199.7f, 201.9f, 203.8f, 205.6f, |
Lightvalve | 34:bb2ca2fc2a8e | 311 | 214.6f, 222.5f, 230.4f, 238.2f, 253.3f, 268.0f, 297.6f, 327.7f |
Lightvalve | 34:bb2ca2fc2a8e | 312 | }; // mV |
Lightvalve | 13:747daba9cf59 | 313 | |
Lightvalve | 30:8d561f16383b | 314 | float PWM_duty_byLT(float Ref_V) |
Lightvalve | 14:8e7590227d22 | 315 | { |
Lightvalve | 30:8d561f16383b | 316 | float PWM_duty = 0.0f; |
Lightvalve | 13:747daba9cf59 | 317 | if(Ref_V<LT_Voltage_Output[0]) { |
Lightvalve | 30:8d561f16383b | 318 | PWM_duty = (Ref_V-LT_Voltage_Output[0])/1.5f+LT_PWM_duty[0]; |
Lightvalve | 13:747daba9cf59 | 319 | } else if (Ref_V>=LT_Voltage_Output[LT_MAX_IDX-1]) { |
Lightvalve | 30:8d561f16383b | 320 | PWM_duty = (Ref_V-LT_Voltage_Output[LT_MAX_IDX-1])/1.5f+LT_PWM_duty[LT_MAX_IDX-1]; |
Lightvalve | 13:747daba9cf59 | 321 | } else { |
Lightvalve | 13:747daba9cf59 | 322 | int idx = 0; |
Lightvalve | 13:747daba9cf59 | 323 | for(idx=0; idx<LT_MAX_IDX-1; idx++) { |
Lightvalve | 30:8d561f16383b | 324 | float ini_x = LT_Voltage_Output[idx]; |
Lightvalve | 30:8d561f16383b | 325 | float fin_x = LT_Voltage_Output[idx+1]; |
Lightvalve | 30:8d561f16383b | 326 | float ini_y = LT_PWM_duty[idx]; |
Lightvalve | 30:8d561f16383b | 327 | float fin_y = LT_PWM_duty[idx+1]; |
Lightvalve | 13:747daba9cf59 | 328 | if(Ref_V>=ini_x && Ref_V<fin_x) { |
Lightvalve | 13:747daba9cf59 | 329 | PWM_duty = (fin_y-ini_y)/(fin_x-ini_x)*(Ref_V-ini_x) + ini_y; |
Lightvalve | 13:747daba9cf59 | 330 | break; |
Lightvalve | 13:747daba9cf59 | 331 | } |
Lightvalve | 13:747daba9cf59 | 332 | } |
Lightvalve | 13:747daba9cf59 | 333 | } |
Lightvalve | 14:8e7590227d22 | 334 | |
Lightvalve | 13:747daba9cf59 | 335 | return PWM_duty; |
Lightvalve | 13:747daba9cf59 | 336 | } |
jobuuu | 6:df07d3491e3a | 337 | |
Lightvalve | 57:f4819de54e7a | 338 | |
Lightvalve | 57:f4819de54e7a | 339 | |
Lightvalve | 57:f4819de54e7a | 340 | |
Lightvalve | 57:f4819de54e7a | 341 | |
jobuuu | 2:a1c0a37df760 | 342 | /******************************************************************************* |
jobuuu | 2:a1c0a37df760 | 343 | TIMER INTERRUPT |
jobuuu | 2:a1c0a37df760 | 344 | *******************************************************************************/ |
jobuuu | 2:a1c0a37df760 | 345 | |
Lightvalve | 51:b46bed7fec80 | 346 | float FREQ_TMR4 = (float)FREQ_20k; |
Lightvalve | 51:b46bed7fec80 | 347 | float DT_TMR4 = (float)DT_20k; |
Lightvalve | 57:f4819de54e7a | 348 | long CNT_TMR4 = 0; |
Lightvalve | 57:f4819de54e7a | 349 | int TMR4_FREQ_10k = (int)FREQ_10k; |
jobuuu | 1:e04e563be5ce | 350 | extern "C" void TIM4_IRQHandler(void) |
jobuuu | 1:e04e563be5ce | 351 | { |
Lightvalve | 19:23b7c1ad8683 | 352 | if (TIM4->SR & TIM_SR_UIF ) { |
Lightvalve | 21:e5f1a43ea6f9 | 353 | |
Lightvalve | 21:e5f1a43ea6f9 | 354 | /******************************************************* |
Lightvalve | 21:e5f1a43ea6f9 | 355 | *** Sensor Read & Data Handling |
Lightvalve | 21:e5f1a43ea6f9 | 356 | ********************************************************/ |
Lightvalve | 13:747daba9cf59 | 357 | |
Lightvalve | 57:f4819de54e7a | 358 | //Encoder |
Lightvalve | 57:f4819de54e7a | 359 | if (CNT_TMR4 % (int) ((int) FREQ_TMR4/TMR4_FREQ_10k) == 0) { |
Lightvalve | 57:f4819de54e7a | 360 | ENC_UPDATE(); |
Lightvalve | 57:f4819de54e7a | 361 | } |
Lightvalve | 58:2eade98630e2 | 362 | |
Lightvalve | 58:2eade98630e2 | 363 | ADC1->CR2 |= 0x40000000; |
Lightvalve | 58:2eade98630e2 | 364 | if (SENSING_MODE == 0) { |
Lightvalve | 58:2eade98630e2 | 365 | // Torque Sensing (0~210)bar ============================================= |
Lightvalve | 58:2eade98630e2 | 366 | float pres_A_new = (((float) ADC1->DR) - 2047.5f); |
Lightvalve | 58:2eade98630e2 | 367 | double alpha_update_ft = 1.0f / (1.0f + FREQ_TMR4 / (2.0f * 3.14f * 100.0f)); // f_cutoff : 200Hz |
Lightvalve | 58:2eade98630e2 | 368 | pres_A.sen = (1.0f - alpha_update_ft) * pres_A.sen + alpha_update_ft * pres_A_new; |
Lightvalve | 58:2eade98630e2 | 369 | torq.sen = pres_A.sen / TORQUE_SENSOR_PULSE_PER_TORQUE; |
Lightvalve | 17:1865016ca2e7 | 370 | |
Lightvalve | 58:2eade98630e2 | 371 | } else if (SENSING_MODE == 1) { |
Lightvalve | 58:2eade98630e2 | 372 | // Pressure Sensing (0~210)bar ============================================= |
Lightvalve | 58:2eade98630e2 | 373 | float pres_A_new = (((float)ADC1->DR) - PRES_A_NULL); |
Lightvalve | 58:2eade98630e2 | 374 | float pres_B_new = (((float)ADC2->DR) - PRES_B_NULL); |
Lightvalve | 58:2eade98630e2 | 375 | double alpha_update_pres = 1.0f / (1.0f + FREQ_TMR4 / (2.0f * 3.14f * 200.0f)); // f_cutoff : 500Hz |
Lightvalve | 58:2eade98630e2 | 376 | pres_A.sen = (1.0f - alpha_update_pres) * pres_A.sen + alpha_update_pres * pres_A_new; |
Lightvalve | 58:2eade98630e2 | 377 | pres_B.sen = (1.0f - alpha_update_pres) * pres_B.sen + alpha_update_pres * pres_B_new; |
Lightvalve | 58:2eade98630e2 | 378 | CUR_PRES_A_BAR = pres_A.sen / PRES_SENSOR_A_PULSE_PER_BAR; |
Lightvalve | 58:2eade98630e2 | 379 | CUR_PRES_B_BAR = pres_B.sen / PRES_SENSOR_B_PULSE_PER_BAR; |
Lightvalve | 50:3c630b5eba9f | 380 | |
Lightvalve | 58:2eade98630e2 | 381 | if ((OPERATING_MODE & 0x01) == 0) { // Rotary Actuator |
Lightvalve | 58:2eade98630e2 | 382 | torq.sen = (PISTON_AREA_A * CUR_PRES_A_BAR - PISTON_AREA_B * CUR_PRES_B_BAR) * 0.0001f; // mm^3*bar >> Nm |
Lightvalve | 58:2eade98630e2 | 383 | } else if ((OPERATING_MODE & 0x01) == 1) { // Linear Actuator |
Lightvalve | 58:2eade98630e2 | 384 | torq.sen = (PISTON_AREA_A * CUR_PRES_A_BAR - PISTON_AREA_B * CUR_PRES_B_BAR) * 0.1f; // mm^2*bar >> N |
Lightvalve | 58:2eade98630e2 | 385 | } |
Lightvalve | 58:2eade98630e2 | 386 | } |
Lightvalve | 58:2eade98630e2 | 387 | |
Lightvalve | 58:2eade98630e2 | 388 | // //Pressure sensor A |
Lightvalve | 58:2eade98630e2 | 389 | // ADC1->CR2 |= 0x40000000; // adc _ 12bit |
Lightvalve | 58:2eade98630e2 | 390 | // //while((ADC1->SR & 0b10)); |
Lightvalve | 58:2eade98630e2 | 391 | // float alpha_update_pres_A = 1.0f/(1.0f + FREQ_TMR4/(2.0f*3.14f*100.0f)); |
Lightvalve | 58:2eade98630e2 | 392 | // float pres_A_new = ((float)ADC1->DR); |
Lightvalve | 58:2eade98630e2 | 393 | // pres_A.sen = pres_A.sen*(1.0f-alpha_update_pres_A)+pres_A_new*(alpha_update_pres_A); |
Lightvalve | 58:2eade98630e2 | 394 | // torq.sen = - (pres_A.sen-2048.0f); //pulse -2047~2047 //SW just changed the sign to correct the direction of loadcell on LIGHT. Correct later. |
Lightvalve | 58:2eade98630e2 | 395 | // |
Lightvalve | 58:2eade98630e2 | 396 | // |
Lightvalve | 58:2eade98630e2 | 397 | // //Pressure sensor B |
Lightvalve | 58:2eade98630e2 | 398 | // float alpha_update_pres_B = 1.0f/(1.0f + FREQ_TMR4/(2.0f*3.14f*100.0f)); |
Lightvalve | 58:2eade98630e2 | 399 | // float pres_B_new = ((float)ADC2->DR); |
Lightvalve | 58:2eade98630e2 | 400 | // pres_B.sen = pres_B.sen*(1.0f-alpha_update_pres_B)+pres_B_new*(alpha_update_pres_B); |
Lightvalve | 58:2eade98630e2 | 401 | // //torq.sen = pres_A.sen * (float) PISTON_AREA_A - pres_B.sen * (float) PISTON_AREA_B; |
Lightvalve | 17:1865016ca2e7 | 402 | |
Lightvalve | 17:1865016ca2e7 | 403 | |
Lightvalve | 21:e5f1a43ea6f9 | 404 | //Current |
Lightvalve | 21:e5f1a43ea6f9 | 405 | //ADC3->CR2 |= 0x40000000; // adc _ 12bit |
Lightvalve | 25:3e6b574cab5c | 406 | //int raw_cur = ADC3->DR; |
Lightvalve | 21:e5f1a43ea6f9 | 407 | //while((ADC3->SR & 0b10)); |
Lightvalve | 51:b46bed7fec80 | 408 | float alpha_update_cur = 1.0f/(1.0f + FREQ_TMR4/(2.0f*3.14f*500.0f)); // f_cutoff : 500Hz |
Lightvalve | 30:8d561f16383b | 409 | float cur_new = ((float)ADC3->DR-2048.0f)*20.0f/4096.0f; // unit : mA |
Lightvalve | 30:8d561f16383b | 410 | cur.sen=cur.sen*(1.0f-alpha_update_cur)+cur_new*(alpha_update_cur); |
Lightvalve | 25:3e6b574cab5c | 411 | //cur.sen = raw_cur; |
Lightvalve | 58:2eade98630e2 | 412 | |
Lightvalve | 57:f4819de54e7a | 413 | CNT_TMR4++; |
Lightvalve | 21:e5f1a43ea6f9 | 414 | } |
Lightvalve | 11:82d8768d7351 | 415 | TIM4->SR = 0x0; // reset the status register |
Lightvalve | 11:82d8768d7351 | 416 | } |
Lightvalve | 19:23b7c1ad8683 | 417 | |
Lightvalve | 19:23b7c1ad8683 | 418 | |
Lightvalve | 18:b8adf1582ea3 | 419 | int j =0; |
Lightvalve | 54:647072f5307a | 420 | float FREQ_TMR3 = (float)FREQ_5k; |
Lightvalve | 48:889798ff9329 | 421 | float DT_TMR3 = (float)DT_5k; |
Lightvalve | 45:35fa6884d0c6 | 422 | int cnt_trans = 0; |
Lightvalve | 48:889798ff9329 | 423 | double VALVE_POS_RAW_FORCE_FB_LOGGING = 0.0f; |
Lightvalve | 57:f4819de54e7a | 424 | int can_rest =0; |
Lightvalve | 48:889798ff9329 | 425 | |
Lightvalve | 11:82d8768d7351 | 426 | extern "C" void TIM3_IRQHandler(void) |
Lightvalve | 21:e5f1a43ea6f9 | 427 | { |
Lightvalve | 19:23b7c1ad8683 | 428 | if (TIM3->SR & TIM_SR_UIF ) { |
Lightvalve | 57:f4819de54e7a | 429 | |
Lightvalve | 57:f4819de54e7a | 430 | if (((OPERATING_MODE&0b110)>>1) == 0) { |
Lightvalve | 57:f4819de54e7a | 431 | K_v = 0.4f; // Moog (LPM >> mA) , 100bar |
Lightvalve | 57:f4819de54e7a | 432 | mV_PER_mA = 500.0f; // 5000mV/10mA |
Lightvalve | 57:f4819de54e7a | 433 | mV_PER_pulse = 0.5f; // 5000mV/10000pulse |
Lightvalve | 57:f4819de54e7a | 434 | mA_PER_pulse = 0.001f; // 10mA/10000pulse |
Lightvalve | 57:f4819de54e7a | 435 | } else if (((OPERATING_MODE&0b110)>>1) == 1) { |
Lightvalve | 57:f4819de54e7a | 436 | K_v = 0.5f; // KNR (LPM >> mA) , 100bar |
Lightvalve | 57:f4819de54e7a | 437 | mV_PER_mA = 166.6666f; // 5000mV/30mA |
Lightvalve | 57:f4819de54e7a | 438 | mV_PER_pulse = 0.5f; // 5000mV/10000pulse |
Lightvalve | 57:f4819de54e7a | 439 | mA_PER_pulse = 0.003f; // 30mA/10000pulse |
Lightvalve | 57:f4819de54e7a | 440 | } |
Lightvalve | 50:3c630b5eba9f | 441 | |
Lightvalve | 50:3c630b5eba9f | 442 | if(MODE_POS_FT_TRANS == 1) { |
Lightvalve | 48:889798ff9329 | 443 | alpha_trans = (float)(1.0f - cos(3.141592f * (float)cnt_trans * DT_TMR3 /3.0f))/2.0f; |
Lightvalve | 45:35fa6884d0c6 | 444 | cnt_trans++; |
Lightvalve | 46:2694daea349b | 445 | torq.err_sum = 0; |
Lightvalve | 48:889798ff9329 | 446 | if((float)cnt_trans * DT_TMR3 > 3.0f) |
Lightvalve | 45:35fa6884d0c6 | 447 | MODE_POS_FT_TRANS = 2; |
Lightvalve | 50:3c630b5eba9f | 448 | } else if(MODE_POS_FT_TRANS == 3) { |
Lightvalve | 48:889798ff9329 | 449 | alpha_trans = (float)(1.0f + cos(3.141592f * (float)cnt_trans * DT_TMR3 /3.0f))/2.0f; |
Lightvalve | 45:35fa6884d0c6 | 450 | cnt_trans++; |
Lightvalve | 46:2694daea349b | 451 | torq.err_sum = 0; |
Lightvalve | 48:889798ff9329 | 452 | if((float) cnt_trans * DT_TMR3 > 3.0f ) |
Lightvalve | 45:35fa6884d0c6 | 453 | MODE_POS_FT_TRANS = 0; |
Lightvalve | 50:3c630b5eba9f | 454 | } else if(MODE_POS_FT_TRANS == 2) { |
Lightvalve | 58:2eade98630e2 | 455 | alpha_trans = 1.0f; |
Lightvalve | 45:35fa6884d0c6 | 456 | cnt_trans = 0; |
Lightvalve | 50:3c630b5eba9f | 457 | } else { |
Lightvalve | 58:2eade98630e2 | 458 | alpha_trans = 0.0f; |
Lightvalve | 45:35fa6884d0c6 | 459 | cnt_trans = 0; |
Lightvalve | 45:35fa6884d0c6 | 460 | } |
Lightvalve | 45:35fa6884d0c6 | 461 | |
Lightvalve | 50:3c630b5eba9f | 462 | |
Lightvalve | 57:f4819de54e7a | 463 | int UTILITY_MODE = 0; |
Lightvalve | 57:f4819de54e7a | 464 | int CONTROL_MODE = 0; |
Lightvalve | 58:2eade98630e2 | 465 | |
Lightvalve | 57:f4819de54e7a | 466 | if (CONTROL_UTILITY_MODE >= 20 || CONTROL_UTILITY_MODE == 0) { |
Lightvalve | 57:f4819de54e7a | 467 | UTILITY_MODE = CONTROL_UTILITY_MODE; |
Lightvalve | 57:f4819de54e7a | 468 | CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 57:f4819de54e7a | 469 | } else { |
Lightvalve | 57:f4819de54e7a | 470 | CONTROL_MODE = CONTROL_UTILITY_MODE; |
Lightvalve | 57:f4819de54e7a | 471 | UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 57:f4819de54e7a | 472 | } |
Lightvalve | 56:6f50d9d3bfee | 473 | |
Lightvalve | 56:6f50d9d3bfee | 474 | |
Lightvalve | 56:6f50d9d3bfee | 475 | |
Lightvalve | 57:f4819de54e7a | 476 | // UTILITY MODE ------------------------------------------------------------ |
Lightvalve | 56:6f50d9d3bfee | 477 | |
Lightvalve | 57:f4819de54e7a | 478 | switch (UTILITY_MODE) { |
Lightvalve | 57:f4819de54e7a | 479 | case MODE_NO_ACT: { |
Lightvalve | 13:747daba9cf59 | 480 | break; |
Lightvalve | 13:747daba9cf59 | 481 | } |
Lightvalve | 14:8e7590227d22 | 482 | |
Lightvalve | 14:8e7590227d22 | 483 | case MODE_TORQUE_SENSOR_NULLING: { |
Lightvalve | 13:747daba9cf59 | 484 | // DAC Voltage reference set |
Lightvalve | 14:8e7590227d22 | 485 | if (TMR3_COUNT_TORQUE_NULL < TMR_FREQ_5k * 2) { |
Lightvalve | 15:bd0d12728506 | 486 | CUR_TORQUE_sum += torq.sen; |
Lightvalve | 19:23b7c1ad8683 | 487 | |
Lightvalve | 14:8e7590227d22 | 488 | if (TMR3_COUNT_TORQUE_NULL % 10 == 0) { |
Lightvalve | 30:8d561f16383b | 489 | CUR_TORQUE_mean = CUR_TORQUE_sum / 10.0f; |
Lightvalve | 13:747daba9cf59 | 490 | CUR_TORQUE_sum = 0; |
Lightvalve | 14:8e7590227d22 | 491 | |
Lightvalve | 59:f308b1656d9c | 492 | TORQUE_VREF += 0.0003f * (0.0f - CUR_TORQUE_mean); |
Lightvalve | 19:23b7c1ad8683 | 493 | |
Lightvalve | 30:8d561f16383b | 494 | if (TORQUE_VREF > 3.3f) TORQUE_VREF = 3.3f; |
Lightvalve | 59:f308b1656d9c | 495 | if (TORQUE_VREF < 0.0f) TORQUE_VREF = 0.0f; |
Lightvalve | 50:3c630b5eba9f | 496 | |
Lightvalve | 16:903b5a4433b4 | 497 | //spi_eeprom_write(RID_TORQUE_SENSOR_VREF, (int16_t) (TORQUE_VREF * 1000.0)); |
Lightvalve | 30:8d561f16383b | 498 | dac_1 = TORQUE_VREF / 3.3f; |
Lightvalve | 13:747daba9cf59 | 499 | } |
Lightvalve | 13:747daba9cf59 | 500 | } else { |
Lightvalve | 58:2eade98630e2 | 501 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 14:8e7590227d22 | 502 | TMR3_COUNT_TORQUE_NULL = 0; |
Lightvalve | 13:747daba9cf59 | 503 | CUR_TORQUE_sum = 0; |
Lightvalve | 13:747daba9cf59 | 504 | CUR_TORQUE_mean = 0; |
Lightvalve | 19:23b7c1ad8683 | 505 | |
Lightvalve | 16:903b5a4433b4 | 506 | ROM_RESET_DATA(); |
Lightvalve | 19:23b7c1ad8683 | 507 | |
Lightvalve | 30:8d561f16383b | 508 | dac_1 = TORQUE_VREF / 3.3f; |
Lightvalve | 14:8e7590227d22 | 509 | |
Lightvalve | 13:747daba9cf59 | 510 | } |
Lightvalve | 14:8e7590227d22 | 511 | TMR3_COUNT_TORQUE_NULL++; |
Lightvalve | 13:747daba9cf59 | 512 | break; |
Lightvalve | 19:23b7c1ad8683 | 513 | } |
Lightvalve | 14:8e7590227d22 | 514 | |
Lightvalve | 50:3c630b5eba9f | 515 | // case MODE_VALVE_NULLING_AND_DEADZONE_SETTING: { |
Lightvalve | 50:3c630b5eba9f | 516 | // if (TMR3_COUNT_DEADZONE == 0) { |
Lightvalve | 50:3c630b5eba9f | 517 | // if (pos_plus_end == pos_minus_end) need_enc_init = true; |
Lightvalve | 50:3c630b5eba9f | 518 | // else temp_time = 0; |
Lightvalve | 50:3c630b5eba9f | 519 | // } |
Lightvalve | 50:3c630b5eba9f | 520 | // if (need_enc_init) { |
Lightvalve | 50:3c630b5eba9f | 521 | // if (TMR3_COUNT_DEADZONE < (int) (0.5f * (float) TMR_FREQ_5k)) { |
Lightvalve | 50:3c630b5eba9f | 522 | // V_out = VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 50:3c630b5eba9f | 523 | // pos_plus_end = pos.sen; |
Lightvalve | 50:3c630b5eba9f | 524 | // } else if (TMR3_COUNT_DEADZONE < TMR_FREQ_5k) { |
Lightvalve | 50:3c630b5eba9f | 525 | // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 50:3c630b5eba9f | 526 | // pos_minus_end = pos.sen; |
Lightvalve | 50:3c630b5eba9f | 527 | // } else if (TMR3_COUNT_DEADZONE == TMR_FREQ_5k) need_enc_init = false; |
Lightvalve | 50:3c630b5eba9f | 528 | // temp_time = TMR_FREQ_5k; |
Lightvalve | 50:3c630b5eba9f | 529 | // } |
Lightvalve | 50:3c630b5eba9f | 530 | // |
Lightvalve | 50:3c630b5eba9f | 531 | // if (temp_time <= TMR3_COUNT_DEADZONE && TMR3_COUNT_DEADZONE < (temp_time + TMR_FREQ_5k)) { |
Lightvalve | 50:3c630b5eba9f | 532 | // V_out = (float) P_GAIN_JOINT_POSITION * (0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen); |
Lightvalve | 50:3c630b5eba9f | 533 | // VALVE_CENTER = VALVE_DEADZONE_PLUS = VALVE_DEADZONE_MINUS = 0; |
Lightvalve | 50:3c630b5eba9f | 534 | // |
Lightvalve | 50:3c630b5eba9f | 535 | // } else if (temp_time <= TMR3_COUNT_DEADZONE && TMR3_COUNT_DEADZONE < (temp_time + (int) (1.9f * (float) TMR_FREQ_5k))) { |
Lightvalve | 50:3c630b5eba9f | 536 | // V_out = 0; |
Lightvalve | 50:3c630b5eba9f | 537 | // CUR_VELOCITY_sum += CUR_VELOCITY; |
Lightvalve | 50:3c630b5eba9f | 538 | // } else if (TMR3_COUNT_DEADZONE == (temp_time + 2 * TMR_FREQ_5k)) { |
Lightvalve | 50:3c630b5eba9f | 539 | // if (CUR_VELOCITY_sum == 0) DZ_dir = 1; |
Lightvalve | 50:3c630b5eba9f | 540 | // else if (CUR_VELOCITY_sum > 0) DZ_dir = 1; |
Lightvalve | 50:3c630b5eba9f | 541 | // else if (CUR_VELOCITY_sum < 0) DZ_dir = -1; |
Lightvalve | 50:3c630b5eba9f | 542 | // else DZ_temp_cnt2 = DZ_end; |
Lightvalve | 50:3c630b5eba9f | 543 | // CUR_VELOCITY_sum = 0; |
Lightvalve | 50:3c630b5eba9f | 544 | // } else if (TMR3_COUNT_DEADZONE > (temp_time + 2 * TMR_FREQ_5k)) { |
Lightvalve | 50:3c630b5eba9f | 545 | // if (TMR3_COUNT_DEADZONE > (temp_time + 10 * TMR_FREQ_5k)) DZ_temp_cnt2 = DZ_end; |
Lightvalve | 50:3c630b5eba9f | 546 | // |
Lightvalve | 50:3c630b5eba9f | 547 | // // Position of Dead Zone |
Lightvalve | 50:3c630b5eba9f | 548 | // // (CUR_VELOCITY < 0) (CUR_VELOCITY == 0) (CUR_VELOCITY > 0) |
Lightvalve | 50:3c630b5eba9f | 549 | // // | / | / |/ |
Lightvalve | 50:3c630b5eba9f | 550 | // // | ______/ ___|___/ ______/| |
Lightvalve | 50:3c630b5eba9f | 551 | // // |/ / | / | |
Lightvalve | 50:3c630b5eba9f | 552 | // // /| / | / | |
Lightvalve | 50:3c630b5eba9f | 553 | // // 0V 0V 0V |
Lightvalve | 50:3c630b5eba9f | 554 | // |
Lightvalve | 50:3c630b5eba9f | 555 | // if (DZ_temp_cnt2 < DZ_end) { |
Lightvalve | 50:3c630b5eba9f | 556 | // if (TMR3_COUNT_DEADZONE % 20 != 0) { |
Lightvalve | 50:3c630b5eba9f | 557 | // CUR_VELOCITY_sum += CUR_VELOCITY; |
Lightvalve | 50:3c630b5eba9f | 558 | // } else { |
Lightvalve | 50:3c630b5eba9f | 559 | // V_out -= DZ_dir; |
Lightvalve | 50:3c630b5eba9f | 560 | // if (CUR_VELOCITY_sum * DZ_dir < 0) DZ_temp_cnt++; |
Lightvalve | 50:3c630b5eba9f | 561 | // CUR_VELOCITY_sum = 0; |
Lightvalve | 50:3c630b5eba9f | 562 | // } |
Lightvalve | 50:3c630b5eba9f | 563 | // if (DZ_temp_cnt == 5) { |
Lightvalve | 50:3c630b5eba9f | 564 | // if (DZ_dir >= 0) VALVE_DEADZONE_MINUS = (int16_t) V_out; |
Lightvalve | 50:3c630b5eba9f | 565 | // else VALVE_DEADZONE_PLUS = (int16_t) V_out; |
Lightvalve | 50:3c630b5eba9f | 566 | // DZ_dir = -DZ_dir; |
Lightvalve | 50:3c630b5eba9f | 567 | // DZ_temp_cnt = 0; |
Lightvalve | 50:3c630b5eba9f | 568 | // DZ_temp_cnt2++; |
Lightvalve | 50:3c630b5eba9f | 569 | // } |
Lightvalve | 50:3c630b5eba9f | 570 | // } else { |
Lightvalve | 50:3c630b5eba9f | 571 | // TMR3_COUNT_DEADZONE = -1; |
Lightvalve | 50:3c630b5eba9f | 572 | // VALVE_CENTER = VALVE_DEADZONE_PLUS / 2 + VALVE_DEADZONE_MINUS / 2; |
Lightvalve | 50:3c630b5eba9f | 573 | // if (VALVE_DEADZONE_PLUS < VALVE_DEADZONE_MINUS) { |
Lightvalve | 50:3c630b5eba9f | 574 | // VALVE_DEADZONE_PLUS = VALVE_CENTER; |
Lightvalve | 50:3c630b5eba9f | 575 | // VALVE_DEADZONE_MINUS = VALVE_CENTER; |
Lightvalve | 50:3c630b5eba9f | 576 | // } |
Lightvalve | 50:3c630b5eba9f | 577 | // V_out = 0; |
Lightvalve | 50:3c630b5eba9f | 578 | // |
Lightvalve | 50:3c630b5eba9f | 579 | // ROM_RESET_DATA(); |
Lightvalve | 50:3c630b5eba9f | 580 | // |
Lightvalve | 50:3c630b5eba9f | 581 | // //spi_eeprom_write(RID_VALVE_DEADZONE_PLUS, VALVE_DEADZONE_PLUS); |
Lightvalve | 50:3c630b5eba9f | 582 | // //spi_eeprom_write(RID_VALVE_DEADZONE_MINUS, VALVE_DEADZONE_MINUS); |
Lightvalve | 50:3c630b5eba9f | 583 | // |
Lightvalve | 50:3c630b5eba9f | 584 | // CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 50:3c630b5eba9f | 585 | // DZ_temp_cnt2 = 0; |
Lightvalve | 50:3c630b5eba9f | 586 | // } |
Lightvalve | 50:3c630b5eba9f | 587 | // } |
Lightvalve | 50:3c630b5eba9f | 588 | // TMR3_COUNT_DEADZONE++; |
Lightvalve | 50:3c630b5eba9f | 589 | // break; |
Lightvalve | 50:3c630b5eba9f | 590 | // } |
Lightvalve | 14:8e7590227d22 | 591 | |
Lightvalve | 14:8e7590227d22 | 592 | case MODE_FIND_HOME: { |
Lightvalve | 29:69f3f5445d6d | 593 | if (FINDHOME_STAGE == FINDHOME_INIT) { |
Lightvalve | 14:8e7590227d22 | 594 | cnt_findhome = 0; |
Lightvalve | 14:8e7590227d22 | 595 | cnt_vel_findhome = 0; |
Lightvalve | 14:8e7590227d22 | 596 | //REFERENCE_MODE = MODE_REF_NO_ACT; // Stop taking reference data from PODO |
Lightvalve | 59:f308b1656d9c | 597 | pos.ref = pos.sen; |
Lightvalve | 59:f308b1656d9c | 598 | vel.ref = 0.0f; |
Lightvalve | 29:69f3f5445d6d | 599 | FINDHOME_STAGE = FINDHOME_GOTOLIMIT; |
Lightvalve | 29:69f3f5445d6d | 600 | } else if (FINDHOME_STAGE == FINDHOME_GOTOLIMIT) { |
Lightvalve | 29:69f3f5445d6d | 601 | int cnt_check_enc = (TMR_FREQ_5k/500); |
Lightvalve | 29:69f3f5445d6d | 602 | if(cnt_findhome%cnt_check_enc == 0) { |
Lightvalve | 29:69f3f5445d6d | 603 | FINDHOME_POSITION = pos.sen; |
Lightvalve | 29:69f3f5445d6d | 604 | FINDHOME_VELOCITY = FINDHOME_POSITION - FINDHOME_POSITION_OLD; |
Lightvalve | 29:69f3f5445d6d | 605 | FINDHOME_POSITION_OLD = FINDHOME_POSITION; |
Lightvalve | 29:69f3f5445d6d | 606 | } |
Lightvalve | 29:69f3f5445d6d | 607 | cnt_findhome++; |
Lightvalve | 14:8e7590227d22 | 608 | |
Lightvalve | 29:69f3f5445d6d | 609 | if (abs(FINDHOME_VELOCITY) <= 1) { |
Lightvalve | 29:69f3f5445d6d | 610 | cnt_vel_findhome = cnt_vel_findhome + 1; |
Lightvalve | 29:69f3f5445d6d | 611 | } else { |
Lightvalve | 29:69f3f5445d6d | 612 | cnt_vel_findhome = 0; |
Lightvalve | 29:69f3f5445d6d | 613 | } |
Lightvalve | 19:23b7c1ad8683 | 614 | |
Lightvalve | 57:f4819de54e7a | 615 | if ((cnt_vel_findhome < 3*TMR_FREQ_5k) && cnt_findhome < 10*TMR_FREQ_5k) { // wait for 3sec |
Lightvalve | 29:69f3f5445d6d | 616 | //REFERENCE_MODE = MODE_REF_NO_ACT; |
Lightvalve | 59:f308b1656d9c | 617 | if (HOMEPOS_OFFSET > 0) pos.ref = pos.ref + 2.0f; |
Lightvalve | 59:f308b1656d9c | 618 | else pos.ref = pos.ref - 2.0f; |
Lightvalve | 59:f308b1656d9c | 619 | |
Lightvalve | 59:f308b1656d9c | 620 | // pos.err = pos.ref_home_pos - pos.sen; |
Lightvalve | 59:f308b1656d9c | 621 | // float VALVE_POS_RAW_POS_FB = 0.0f; |
Lightvalve | 59:f308b1656d9c | 622 | // VALVE_POS_RAW_POS_FB = (float) P_GAIN_JOINT_POSITION * pos.err/(float) ENC_PULSE_PER_POSITION * 0.01f; |
Lightvalve | 59:f308b1656d9c | 623 | // valve_pos.ref = VALVE_POS_RAW_POS_FB + (float) VALVE_CENTER; |
Lightvalve | 59:f308b1656d9c | 624 | // VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 59:f308b1656d9c | 625 | |
Lightvalve | 59:f308b1656d9c | 626 | CONTROL_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 59:f308b1656d9c | 627 | alpha_trans = 0.0f; |
Lightvalve | 59:f308b1656d9c | 628 | |
Lightvalve | 34:bb2ca2fc2a8e | 629 | |
Lightvalve | 29:69f3f5445d6d | 630 | } else { |
Lightvalve | 29:69f3f5445d6d | 631 | ENC_SET(HOMEPOS_OFFSET); |
Lightvalve | 29:69f3f5445d6d | 632 | INIT_REF_POS = HOMEPOS_OFFSET; |
Lightvalve | 29:69f3f5445d6d | 633 | REF_POSITION = 0; |
Lightvalve | 29:69f3f5445d6d | 634 | REF_VELOCITY = 0; |
Lightvalve | 29:69f3f5445d6d | 635 | FINDHOME_POSITION = 0; |
Lightvalve | 29:69f3f5445d6d | 636 | FINDHOME_POSITION_OLD = 0; |
Lightvalve | 29:69f3f5445d6d | 637 | FINDHOME_VELOCITY = 0; |
Lightvalve | 29:69f3f5445d6d | 638 | cnt_findhome = 0; |
Lightvalve | 29:69f3f5445d6d | 639 | cnt_vel_findhome = 0; |
Lightvalve | 29:69f3f5445d6d | 640 | FINDHOME_STAGE = FINDHOME_ZEROPOSE; |
Lightvalve | 29:69f3f5445d6d | 641 | } |
Lightvalve | 29:69f3f5445d6d | 642 | } else if (FINDHOME_STAGE == FINDHOME_ZEROPOSE) { |
Lightvalve | 29:69f3f5445d6d | 643 | int T_move = 2*TMR_FREQ_5k; |
Lightvalve | 59:f308b1656d9c | 644 | pos.ref = (0.0f - (float)INIT_REF_POS)*0.5f*(1.0f - cos(3.14159f * (float)cnt_findhome / (float)T_move)) + (float)INIT_REF_POS; |
Lightvalve | 59:f308b1656d9c | 645 | vel.ref = 0.0f; |
Lightvalve | 19:23b7c1ad8683 | 646 | |
Lightvalve | 29:69f3f5445d6d | 647 | // input for position control |
Lightvalve | 59:f308b1656d9c | 648 | |
Lightvalve | 59:f308b1656d9c | 649 | CONTROL_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 59:f308b1656d9c | 650 | alpha_trans = 0.0f; |
Lightvalve | 59:f308b1656d9c | 651 | // pos.err = pos.ref - (float)pos.sen; |
Lightvalve | 59:f308b1656d9c | 652 | // float VALVE_POS_RAW_POS_FB = 0.0f; |
Lightvalve | 59:f308b1656d9c | 653 | // VALVE_POS_RAW_POS_FB = (float) P_GAIN_JOINT_POSITION * 0.01f * pos.err/(float) ENC_PULSE_PER_POSITION; |
Lightvalve | 59:f308b1656d9c | 654 | // valve_pos.ref = VALVE_POS_RAW_POS_FB + (float) VALVE_CENTER; |
Lightvalve | 59:f308b1656d9c | 655 | // VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 29:69f3f5445d6d | 656 | |
Lightvalve | 29:69f3f5445d6d | 657 | cnt_findhome++; |
Lightvalve | 29:69f3f5445d6d | 658 | if (cnt_findhome >= T_move) { |
Lightvalve | 29:69f3f5445d6d | 659 | //REFERENCE_MODE = MODE_REF_DIRECT; |
Lightvalve | 29:69f3f5445d6d | 660 | cnt_findhome = 0; |
Lightvalve | 30:8d561f16383b | 661 | pos.ref = 0.0f; |
Lightvalve | 30:8d561f16383b | 662 | vel.ref = 0.0f; |
Lightvalve | 30:8d561f16383b | 663 | pos.ref_home_pos = 0.0f; |
Lightvalve | 30:8d561f16383b | 664 | vel.ref_home_pos = 0.0f; |
Lightvalve | 29:69f3f5445d6d | 665 | FINDHOME_STAGE = FINDHOME_INIT; |
Lightvalve | 57:f4819de54e7a | 666 | CONTROL_UTILITY_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 29:69f3f5445d6d | 667 | } |
Lightvalve | 13:747daba9cf59 | 668 | } |
Lightvalve | 19:23b7c1ad8683 | 669 | |
Lightvalve | 13:747daba9cf59 | 670 | break; |
Lightvalve | 13:747daba9cf59 | 671 | } |
Lightvalve | 14:8e7590227d22 | 672 | |
Lightvalve | 50:3c630b5eba9f | 673 | // case MODE_VALVE_GAIN_SETTING: { |
Lightvalve | 50:3c630b5eba9f | 674 | // if (TMR3_COUNT_FLOWRATE == 0) { |
Lightvalve | 50:3c630b5eba9f | 675 | // if (pos_plus_end == pos_minus_end) need_enc_init = true; |
Lightvalve | 50:3c630b5eba9f | 676 | // else { |
Lightvalve | 50:3c630b5eba9f | 677 | // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 50:3c630b5eba9f | 678 | // temp_time = (int) (0.5f * (float) TMR_FREQ_5k); |
Lightvalve | 50:3c630b5eba9f | 679 | // } |
Lightvalve | 50:3c630b5eba9f | 680 | // } |
Lightvalve | 50:3c630b5eba9f | 681 | // if (need_enc_init) { |
Lightvalve | 50:3c630b5eba9f | 682 | // if (TMR3_COUNT_FLOWRATE < (int) (0.5f * (float) TMR_FREQ_5k)) { |
Lightvalve | 50:3c630b5eba9f | 683 | // V_out = VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 50:3c630b5eba9f | 684 | // pos_plus_end = pos.sen; |
Lightvalve | 50:3c630b5eba9f | 685 | // } else if (TMR3_COUNT_FLOWRATE < TMR_FREQ_5k) { |
Lightvalve | 50:3c630b5eba9f | 686 | // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 50:3c630b5eba9f | 687 | // pos_minus_end = pos.sen; |
Lightvalve | 50:3c630b5eba9f | 688 | // } else if (TMR3_COUNT_FLOWRATE == TMR_FREQ_5k) { |
Lightvalve | 50:3c630b5eba9f | 689 | // need_enc_init = false; |
Lightvalve | 50:3c630b5eba9f | 690 | // check_vel_pos_init = (int) (0.9f * (float) (pos_plus_end - pos_minus_end)); |
Lightvalve | 50:3c630b5eba9f | 691 | // check_vel_pos_fin = (int) (0.95f * (float) (pos_plus_end - pos_minus_end)); |
Lightvalve | 50:3c630b5eba9f | 692 | // check_vel_pos_interv = check_vel_pos_fin - check_vel_pos_init; |
Lightvalve | 50:3c630b5eba9f | 693 | // } |
Lightvalve | 50:3c630b5eba9f | 694 | // temp_time = TMR_FREQ_5k; |
Lightvalve | 50:3c630b5eba9f | 695 | // } |
Lightvalve | 50:3c630b5eba9f | 696 | // TMR3_COUNT_FLOWRATE++; |
Lightvalve | 50:3c630b5eba9f | 697 | // if (TMR3_COUNT_FLOWRATE > temp_time) { |
Lightvalve | 50:3c630b5eba9f | 698 | // if (flag_flowrate % 2 == 0) { // (+) |
Lightvalve | 50:3c630b5eba9f | 699 | // VALVE_VOLTAGE = 1000.0f * (float) (flag_flowrate / 2 + 1); |
Lightvalve | 50:3c630b5eba9f | 700 | // V_out = VALVE_VOLTAGE; |
Lightvalve | 50:3c630b5eba9f | 701 | // if (pos.sen > (pos_minus_end + check_vel_pos_init) && pos.sen < (pos_minus_end + check_vel_pos_fin)) { |
Lightvalve | 50:3c630b5eba9f | 702 | // fl_temp_cnt++; |
Lightvalve | 50:3c630b5eba9f | 703 | // } else if (pos.sen >= (pos_minus_end + check_vel_pos_fin) && CUR_VELOCITY == 0) { |
Lightvalve | 50:3c630b5eba9f | 704 | // VALVE_GAIN_LPM_PER_V[flag_flowrate] = 0.95873f * 0.5757f * (float) TMR_FREQ_5k / 10000.0 * (float) check_vel_pos_interv / (float) fl_temp_cnt / VALVE_VOLTAGE; // 0.9587=6*pi/65536*10000 0.5757=0.02525*0.02*0.0095*2*60*1000 |
Lightvalve | 50:3c630b5eba9f | 705 | // // VALVE_GAIN_LPM_PER_V[flag_flowrate] = (float) TMR_FREQ_10k * (float) check_vel_pos_interv / (float) fl_temp_cnt / VALVE_VOLTAGE; // PULSE/sec |
Lightvalve | 50:3c630b5eba9f | 706 | // fl_temp_cnt2++; |
Lightvalve | 50:3c630b5eba9f | 707 | // } |
Lightvalve | 50:3c630b5eba9f | 708 | // } else if (flag_flowrate % 2 == 1) { // (-) |
Lightvalve | 50:3c630b5eba9f | 709 | // VALVE_VOLTAGE = -1. * (float) (flag_flowrate / 2 + 1); |
Lightvalve | 50:3c630b5eba9f | 710 | // V_out = VALVE_VOLTAGE; |
Lightvalve | 50:3c630b5eba9f | 711 | // if (pos.sen < (pos_plus_end - check_vel_pos_init) && pos.sen > (pos_plus_end - check_vel_pos_fin)) { |
Lightvalve | 50:3c630b5eba9f | 712 | // fl_temp_cnt++; |
Lightvalve | 50:3c630b5eba9f | 713 | // } else if (pos.sen <= (pos_plus_end - check_vel_pos_fin) && CUR_VELOCITY == 0) { |
Lightvalve | 50:3c630b5eba9f | 714 | // VALVE_GAIN_LPM_PER_V[flag_flowrate] = 0.95873f * 0.5757f * (float) TMR_FREQ_5k / 10000.0f * (float) check_vel_pos_interv / (float) fl_temp_cnt / (-VALVE_VOLTAGE); |
Lightvalve | 50:3c630b5eba9f | 715 | // // VALVE_GAIN_LPM_PER_V[flag_flowrate] = (float) TMR_FREQ_10k * (float) check_vel_pos_interv / (float) fl_temp_cnt / (-VALVE_VOLTAGE); // PULSE/sec |
Lightvalve | 50:3c630b5eba9f | 716 | // fl_temp_cnt2++; |
Lightvalve | 50:3c630b5eba9f | 717 | // } |
Lightvalve | 50:3c630b5eba9f | 718 | // } |
Lightvalve | 50:3c630b5eba9f | 719 | // if (fl_temp_cnt2 == 100) { |
Lightvalve | 50:3c630b5eba9f | 720 | // |
Lightvalve | 50:3c630b5eba9f | 721 | // ROM_RESET_DATA(); |
Lightvalve | 50:3c630b5eba9f | 722 | // |
Lightvalve | 50:3c630b5eba9f | 723 | // //spi_eeprom_write(RID_VALVE_GAIN_PLUS_1 + flag_flowrate, (int16_t) (VALVE_GAIN_LPM_PER_V[flag_flowrate] * 100.0f)); |
Lightvalve | 50:3c630b5eba9f | 724 | // cur_vel_sum = 0; |
Lightvalve | 50:3c630b5eba9f | 725 | // fl_temp_cnt = 0; |
Lightvalve | 50:3c630b5eba9f | 726 | // fl_temp_cnt2 = 0; |
Lightvalve | 50:3c630b5eba9f | 727 | // flag_flowrate++; |
Lightvalve | 50:3c630b5eba9f | 728 | // } |
Lightvalve | 50:3c630b5eba9f | 729 | // if (flag_flowrate == 10) { |
Lightvalve | 50:3c630b5eba9f | 730 | // V_out = 0; |
Lightvalve | 50:3c630b5eba9f | 731 | // flag_flowrate = 0; |
Lightvalve | 50:3c630b5eba9f | 732 | // TMR3_COUNT_FLOWRATE = 0; |
Lightvalve | 50:3c630b5eba9f | 733 | // valve_gain_repeat_cnt++; |
Lightvalve | 50:3c630b5eba9f | 734 | // if (valve_gain_repeat_cnt >= 1) { |
Lightvalve | 50:3c630b5eba9f | 735 | // CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 50:3c630b5eba9f | 736 | // valve_gain_repeat_cnt = 0; |
Lightvalve | 50:3c630b5eba9f | 737 | // } |
Lightvalve | 50:3c630b5eba9f | 738 | // |
Lightvalve | 50:3c630b5eba9f | 739 | // } |
Lightvalve | 50:3c630b5eba9f | 740 | // break; |
Lightvalve | 50:3c630b5eba9f | 741 | // } |
Lightvalve | 50:3c630b5eba9f | 742 | // |
Lightvalve | 50:3c630b5eba9f | 743 | // } |
Lightvalve | 14:8e7590227d22 | 744 | case MODE_PRESSURE_SENSOR_NULLING: { |
Lightvalve | 13:747daba9cf59 | 745 | // DAC Voltage reference set |
Lightvalve | 14:8e7590227d22 | 746 | if (TMR3_COUNT_PRES_NULL < TMR_FREQ_5k * 2) { |
Lightvalve | 14:8e7590227d22 | 747 | CUR_PRES_A_sum += pres_A.sen; |
Lightvalve | 14:8e7590227d22 | 748 | CUR_PRES_B_sum += pres_B.sen; |
Lightvalve | 14:8e7590227d22 | 749 | |
Lightvalve | 14:8e7590227d22 | 750 | if (TMR3_COUNT_PRES_NULL % 10 == 0) { |
Lightvalve | 30:8d561f16383b | 751 | CUR_PRES_A_mean = CUR_PRES_A_sum / 10.0f; |
Lightvalve | 30:8d561f16383b | 752 | CUR_PRES_B_mean = CUR_PRES_B_sum / 10.0f; |
Lightvalve | 13:747daba9cf59 | 753 | CUR_PRES_A_sum = 0; |
Lightvalve | 13:747daba9cf59 | 754 | CUR_PRES_B_sum = 0; |
Lightvalve | 14:8e7590227d22 | 755 | |
Lightvalve | 38:118df027d851 | 756 | float VREF_NullingGain = 0.0003f; |
Lightvalve | 58:2eade98630e2 | 757 | PRES_A_VREF = PRES_A_VREF + VREF_NullingGain * CUR_PRES_A_mean; |
Lightvalve | 58:2eade98630e2 | 758 | PRES_B_VREF = PRES_B_VREF + VREF_NullingGain * CUR_PRES_B_mean; |
Lightvalve | 14:8e7590227d22 | 759 | |
Lightvalve | 30:8d561f16383b | 760 | if (PRES_A_VREF > 3.3f) PRES_A_VREF = 3.3f; |
Lightvalve | 30:8d561f16383b | 761 | if (PRES_A_VREF < 0.0f) PRES_A_VREF = 0.0f; |
Lightvalve | 30:8d561f16383b | 762 | if (PRES_B_VREF > 3.3f) PRES_B_VREF = 3.3f; |
Lightvalve | 30:8d561f16383b | 763 | if (PRES_B_VREF < 0.0f) PRES_B_VREF = 0.0f; |
Lightvalve | 19:23b7c1ad8683 | 764 | |
Lightvalve | 30:8d561f16383b | 765 | dac_1 = PRES_A_VREF / 3.3f; |
Lightvalve | 30:8d561f16383b | 766 | dac_2 = PRES_B_VREF / 3.3f; |
Lightvalve | 13:747daba9cf59 | 767 | } |
Lightvalve | 13:747daba9cf59 | 768 | } else { |
Lightvalve | 57:f4819de54e7a | 769 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 14:8e7590227d22 | 770 | TMR3_COUNT_PRES_NULL = 0; |
Lightvalve | 13:747daba9cf59 | 771 | CUR_PRES_A_sum = 0; |
Lightvalve | 13:747daba9cf59 | 772 | CUR_PRES_B_sum = 0; |
Lightvalve | 13:747daba9cf59 | 773 | CUR_PRES_A_mean = 0; |
Lightvalve | 13:747daba9cf59 | 774 | CUR_PRES_B_mean = 0; |
Lightvalve | 14:8e7590227d22 | 775 | |
Lightvalve | 16:903b5a4433b4 | 776 | ROM_RESET_DATA(); |
Lightvalve | 19:23b7c1ad8683 | 777 | |
Lightvalve | 30:8d561f16383b | 778 | dac_1 = PRES_A_VREF / 3.3f; |
Lightvalve | 30:8d561f16383b | 779 | dac_2 = PRES_B_VREF / 3.3f; |
Lightvalve | 17:1865016ca2e7 | 780 | //pc.printf("nulling end"); |
Lightvalve | 13:747daba9cf59 | 781 | } |
Lightvalve | 14:8e7590227d22 | 782 | TMR3_COUNT_PRES_NULL++; |
Lightvalve | 13:747daba9cf59 | 783 | break; |
Lightvalve | 13:747daba9cf59 | 784 | } |
Lightvalve | 14:8e7590227d22 | 785 | |
Lightvalve | 50:3c630b5eba9f | 786 | // case MODE_PRESSURE_SENSOR_CALIB: { |
Lightvalve | 50:3c630b5eba9f | 787 | // if (TMR3_COUNT_PRES_CALIB < 2 * TMR_FREQ_5k) { |
Lightvalve | 50:3c630b5eba9f | 788 | // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 50:3c630b5eba9f | 789 | // if (TMR3_COUNT_PRES_CALIB >= TMR_FREQ_5k) { |
Lightvalve | 50:3c630b5eba9f | 790 | // CUR_PRES_A_sum += CUR_PRES_A; |
Lightvalve | 50:3c630b5eba9f | 791 | // } |
Lightvalve | 50:3c630b5eba9f | 792 | // } else if (TMR3_COUNT_PRES_CALIB < 4 * TMR_FREQ_5k) { |
Lightvalve | 50:3c630b5eba9f | 793 | // V_out = VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 50:3c630b5eba9f | 794 | // if (TMR3_COUNT_PRES_CALIB >= 3 * TMR_FREQ_5k) { |
Lightvalve | 50:3c630b5eba9f | 795 | // CUR_PRES_B_sum += CUR_PRES_B; |
Lightvalve | 50:3c630b5eba9f | 796 | // } |
Lightvalve | 50:3c630b5eba9f | 797 | // } else { |
Lightvalve | 50:3c630b5eba9f | 798 | // CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 50:3c630b5eba9f | 799 | // TMR3_COUNT_PRES_CALIB = 0; |
Lightvalve | 50:3c630b5eba9f | 800 | // V_out = 0; |
Lightvalve | 50:3c630b5eba9f | 801 | // PRES_SENSOR_A_PULSE_PER_BAR = CUR_PRES_A_sum / ((float) TMR_FREQ_5k - 1.0f) - PRES_A_NULL; |
Lightvalve | 50:3c630b5eba9f | 802 | // PRES_SENSOR_A_PULSE_PER_BAR = PRES_SENSOR_A_PULSE_PER_BAR / ((float) PRES_SUPPLY - 1.0f); |
Lightvalve | 50:3c630b5eba9f | 803 | // PRES_SENSOR_B_PULSE_PER_BAR = CUR_PRES_B_sum / ((float) TMR_FREQ_5k - 1.0f) - PRES_B_NULL; |
Lightvalve | 50:3c630b5eba9f | 804 | // PRES_SENSOR_B_PULSE_PER_BAR = PRES_SENSOR_B_PULSE_PER_BAR / ((float) PRES_SUPPLY - 1.0f); |
Lightvalve | 50:3c630b5eba9f | 805 | // CUR_PRES_A_sum = 0; |
Lightvalve | 50:3c630b5eba9f | 806 | // CUR_PRES_B_sum = 0; |
Lightvalve | 50:3c630b5eba9f | 807 | // CUR_PRES_A_mean = 0; |
Lightvalve | 50:3c630b5eba9f | 808 | // CUR_PRES_B_mean = 0; |
Lightvalve | 50:3c630b5eba9f | 809 | // |
Lightvalve | 50:3c630b5eba9f | 810 | // ROM_RESET_DATA(); |
Lightvalve | 50:3c630b5eba9f | 811 | // |
Lightvalve | 50:3c630b5eba9f | 812 | // //spi_eeprom_write(RID_PRES_SENSOR_A_PULSE_PER_BAR, (int16_t) (PRES_SENSOR_A_PULSE_PER_BAR * 100.0f)); |
Lightvalve | 50:3c630b5eba9f | 813 | // //spi_eeprom_write(RID_PRES_SENSOR_B_PULSE_PER_BAR, (int16_t) (PRES_SENSOR_B_PULSE_PER_BAR * 100.0f)); |
Lightvalve | 50:3c630b5eba9f | 814 | // } |
Lightvalve | 50:3c630b5eba9f | 815 | // TMR3_COUNT_PRES_CALIB++; |
Lightvalve | 50:3c630b5eba9f | 816 | // break; |
Lightvalve | 50:3c630b5eba9f | 817 | // } |
Lightvalve | 19:23b7c1ad8683 | 818 | |
Lightvalve | 50:3c630b5eba9f | 819 | // case MODE_ROTARY_FRICTION_TUNING: { |
Lightvalve | 50:3c630b5eba9f | 820 | // if (TMR3_COUNT_ROTARY_FRIC_TUNE % (5 * TMR_FREQ_5k) == 0) freq_fric_tune = 4.0f + 3.0f * sin(2 * 3.14159f * 0.5f * TMR3_COUNT_ROTARY_FRIC_TUNE * 0.0001f * 0.05f); |
Lightvalve | 50:3c630b5eba9f | 821 | // V_out = PWM_out * sin(2 * 3.14159f * freq_fric_tune * TMR3_COUNT_ROTARY_FRIC_TUNE * 0.0001f); |
Lightvalve | 50:3c630b5eba9f | 822 | // if (V_out > 0) V_out = VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 50:3c630b5eba9f | 823 | // else V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 50:3c630b5eba9f | 824 | // TMR3_COUNT_ROTARY_FRIC_TUNE++; |
Lightvalve | 50:3c630b5eba9f | 825 | // if (TMR3_COUNT_ROTARY_FRIC_TUNE > TUNING_TIME * TMR_FREQ_5k) { |
Lightvalve | 50:3c630b5eba9f | 826 | // TMR3_COUNT_ROTARY_FRIC_TUNE = 0; |
Lightvalve | 50:3c630b5eba9f | 827 | // V_out = 0.0f; |
Lightvalve | 50:3c630b5eba9f | 828 | // CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 50:3c630b5eba9f | 829 | // } |
Lightvalve | 50:3c630b5eba9f | 830 | // break; |
Lightvalve | 50:3c630b5eba9f | 831 | // } |
Lightvalve | 14:8e7590227d22 | 832 | |
Lightvalve | 14:8e7590227d22 | 833 | case MODE_DDV_POS_VS_PWM_ID: { |
Lightvalve | 59:f308b1656d9c | 834 | CONTROL_MODE = MODE_VALVE_OPEN_LOOP; |
Lightvalve | 13:747daba9cf59 | 835 | VALVE_ID_timer = VALVE_ID_timer + 1; |
Lightvalve | 14:8e7590227d22 | 836 | |
Lightvalve | 14:8e7590227d22 | 837 | if(VALVE_ID_timer < TMR_FREQ_5k*1) { |
Lightvalve | 59:f308b1656d9c | 838 | Vout.ref = 3000.0f * sin(2*3.14f*VALVE_ID_timer/TMR_FREQ_5k * 100.0f); |
Lightvalve | 14:8e7590227d22 | 839 | } else if(VALVE_ID_timer < TMR_FREQ_5k*2) { |
Lightvalve | 59:f308b1656d9c | 840 | Vout.ref = 1000.0f*(ID_index_array[ID_index]); |
Lightvalve | 14:8e7590227d22 | 841 | } else if(VALVE_ID_timer == TMR_FREQ_5k*2) { |
Lightvalve | 13:747daba9cf59 | 842 | VALVE_POS_TMP = 0; |
Lightvalve | 13:747daba9cf59 | 843 | data_num = 0; |
Lightvalve | 14:8e7590227d22 | 844 | } else if(VALVE_ID_timer < TMR_FREQ_5k*3) { |
Lightvalve | 13:747daba9cf59 | 845 | data_num = data_num + 1; |
Lightvalve | 13:747daba9cf59 | 846 | VALVE_POS_TMP = VALVE_POS_TMP + value; |
Lightvalve | 14:8e7590227d22 | 847 | } else if(VALVE_ID_timer == TMR_FREQ_5k*3) { |
Lightvalve | 59:f308b1656d9c | 848 | Vout.ref = 0.0f; |
Lightvalve | 59:f308b1656d9c | 849 | } else { |
Lightvalve | 13:747daba9cf59 | 850 | VALVE_POS_AVG[ID_index] = VALVE_POS_TMP / data_num; |
Lightvalve | 13:747daba9cf59 | 851 | VALVE_ID_timer = 0; |
Lightvalve | 13:747daba9cf59 | 852 | ID_index= ID_index +1; |
Lightvalve | 13:747daba9cf59 | 853 | } |
Lightvalve | 14:8e7590227d22 | 854 | |
Lightvalve | 17:1865016ca2e7 | 855 | if(ID_index>=25) { |
Lightvalve | 13:747daba9cf59 | 856 | int i; |
Lightvalve | 13:747daba9cf59 | 857 | VALVE_POS_AVG_OLD = VALVE_POS_AVG[0]; |
Lightvalve | 17:1865016ca2e7 | 858 | for(i=0; i<25; i++) { |
Lightvalve | 13:747daba9cf59 | 859 | VALVE_POS_VS_PWM[i] = (int16_t) (VALVE_POS_AVG[i]); |
Lightvalve | 14:8e7590227d22 | 860 | if(VALVE_POS_AVG[i] > VALVE_POS_AVG_OLD) { |
Lightvalve | 13:747daba9cf59 | 861 | VALVE_MAX_POS = VALVE_POS_AVG[i]; |
Lightvalve | 13:747daba9cf59 | 862 | VALVE_POS_AVG_OLD = VALVE_MAX_POS; |
Lightvalve | 14:8e7590227d22 | 863 | } else if(VALVE_POS_AVG[i] < VALVE_POS_AVG_OLD) { |
Lightvalve | 13:747daba9cf59 | 864 | VALVE_MIN_POS = VALVE_POS_AVG[i]; |
Lightvalve | 13:747daba9cf59 | 865 | VALVE_POS_AVG_OLD = VALVE_MIN_POS; |
Lightvalve | 13:747daba9cf59 | 866 | } |
Lightvalve | 13:747daba9cf59 | 867 | } |
Lightvalve | 59:f308b1656d9c | 868 | ROM_RESET_DATA(); |
Lightvalve | 13:747daba9cf59 | 869 | ID_index = 0; |
Lightvalve | 57:f4819de54e7a | 870 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 13:747daba9cf59 | 871 | } |
Lightvalve | 14:8e7590227d22 | 872 | |
Lightvalve | 14:8e7590227d22 | 873 | |
Lightvalve | 13:747daba9cf59 | 874 | break; |
Lightvalve | 13:747daba9cf59 | 875 | } |
Lightvalve | 14:8e7590227d22 | 876 | |
Lightvalve | 14:8e7590227d22 | 877 | case MODE_DDV_DEADZONE_AND_CENTER: { |
Lightvalve | 59:f308b1656d9c | 878 | CONTROL_MODE = MODE_VALVE_OPEN_LOOP; |
Lightvalve | 13:747daba9cf59 | 879 | VALVE_DZ_timer = VALVE_DZ_timer + 1; |
Lightvalve | 14:8e7590227d22 | 880 | if(first_check == 0) { |
Lightvalve | 30:8d561f16383b | 881 | if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 882 | Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 30:8d561f16383b | 883 | } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 884 | Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 14:8e7590227d22 | 885 | pos_plus_end = pos.sen; |
Lightvalve | 30:8d561f16383b | 886 | } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 887 | Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 30:8d561f16383b | 888 | } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 889 | Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 14:8e7590227d22 | 890 | pos_minus_end = pos.sen; |
Lightvalve | 30:8d561f16383b | 891 | } else if(VALVE_DZ_timer < (int) (3.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 892 | Vout.ref = (float) P_GAIN_JOINT_POSITION * (0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen)/(float) ENC_PULSE_PER_POSITION; |
Lightvalve | 30:8d561f16383b | 893 | } else if(VALVE_DZ_timer < (int) (4.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 894 | Vout.ref = (float) P_GAIN_JOINT_POSITION * (0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen)/(float) ENC_PULSE_PER_POSITION; |
Lightvalve | 13:747daba9cf59 | 895 | data_num = data_num + 1; |
Lightvalve | 13:747daba9cf59 | 896 | VALVE_POS_TMP = VALVE_POS_TMP + value; |
Lightvalve | 30:8d561f16383b | 897 | } else if(VALVE_DZ_timer == (int) (4.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 898 | Vout.ref = (float) P_GAIN_JOINT_POSITION * (0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen)/(float) ENC_PULSE_PER_POSITION; |
Lightvalve | 13:747daba9cf59 | 899 | DDV_POS_AVG = VALVE_POS_TMP / data_num; |
Lightvalve | 14:8e7590227d22 | 900 | START_POS = pos.sen; |
Lightvalve | 13:747daba9cf59 | 901 | VALVE_POS_TMP = 0; |
Lightvalve | 13:747daba9cf59 | 902 | data_num = 0; |
Lightvalve | 14:8e7590227d22 | 903 | |
Lightvalve | 30:8d561f16383b | 904 | } else if(VALVE_DZ_timer < (int) (5.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 905 | valve_pos.ref = DDV_POS_AVG; |
Lightvalve | 14:8e7590227d22 | 906 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 14:8e7590227d22 | 907 | |
Lightvalve | 30:8d561f16383b | 908 | } else if(VALVE_DZ_timer < (int) (6.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 909 | valve_pos.ref = DDV_POS_AVG; |
Lightvalve | 14:8e7590227d22 | 910 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 13:747daba9cf59 | 911 | if(CUR_VELOCITY >= 0) |
Lightvalve | 13:747daba9cf59 | 912 | VEL_POINT = VEL_POINT + 1; |
Lightvalve | 13:747daba9cf59 | 913 | else |
Lightvalve | 13:747daba9cf59 | 914 | VEL_POINT = VEL_POINT - 1; |
Lightvalve | 30:8d561f16383b | 915 | } else if(VALVE_DZ_timer == (int) (6.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 916 | valve_pos.ref = DDV_POS_AVG; |
Lightvalve | 14:8e7590227d22 | 917 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 14:8e7590227d22 | 918 | FINAL_POS = pos.sen; |
Lightvalve | 14:8e7590227d22 | 919 | |
Lightvalve | 14:8e7590227d22 | 920 | if((FINAL_POS - START_POS)>100) { |
Lightvalve | 13:747daba9cf59 | 921 | DZ_case = 1; |
Lightvalve | 14:8e7590227d22 | 922 | } else if((FINAL_POS - START_POS)<-100) { |
Lightvalve | 13:747daba9cf59 | 923 | DZ_case = -1; |
Lightvalve | 14:8e7590227d22 | 924 | } else { |
Lightvalve | 13:747daba9cf59 | 925 | DZ_case = 0; |
Lightvalve | 13:747daba9cf59 | 926 | } |
Lightvalve | 59:f308b1656d9c | 927 | |
Lightvalve | 59:f308b1656d9c | 928 | //CAN_TX_PRES((int16_t) (DZ_case), (int16_t) (6)); |
Lightvalve | 50:3c630b5eba9f | 929 | |
Lightvalve | 14:8e7590227d22 | 930 | VEL_POINT = 0; |
Lightvalve | 13:747daba9cf59 | 931 | first_check = 1; |
Lightvalve | 13:747daba9cf59 | 932 | DZ_DIRECTION = 1; |
Lightvalve | 13:747daba9cf59 | 933 | VALVE_DZ_timer = 0; |
Lightvalve | 13:747daba9cf59 | 934 | Ref_Valve_Pos_Old = DDV_POS_AVG; |
Lightvalve | 13:747daba9cf59 | 935 | DZ_NUM = 1; |
Lightvalve | 13:747daba9cf59 | 936 | DZ_index = 1; |
Lightvalve | 14:8e7590227d22 | 937 | |
Lightvalve | 13:747daba9cf59 | 938 | } |
Lightvalve | 19:23b7c1ad8683 | 939 | } else { |
Lightvalve | 14:8e7590227d22 | 940 | if((DZ_case == -1 && DZ_NUM == 1) | (DZ_case == 1 && DZ_NUM == 1)) { |
Lightvalve | 30:8d561f16383b | 941 | if(VALVE_DZ_timer < (int) (1.0 * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 942 | //Vout.ref = (float) P_GAIN_JOINT_POSITION * (0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen)/(float) ENC_PULSE_PER_POSITION; |
Lightvalve | 59:f308b1656d9c | 943 | pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end; |
Lightvalve | 59:f308b1656d9c | 944 | CONTROL_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 30:8d561f16383b | 945 | } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 946 | START_POS = pos.sen; |
Lightvalve | 30:8d561f16383b | 947 | } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 948 | valve_pos.ref = Ref_Valve_Pos_Old - DZ_case * DZ_DIRECTION * 64 / DZ_index; |
Lightvalve | 14:8e7590227d22 | 949 | if(valve_pos.ref <= VALVE_MIN_POS) { |
Lightvalve | 14:8e7590227d22 | 950 | valve_pos.ref = VALVE_MIN_POS; |
Lightvalve | 14:8e7590227d22 | 951 | } else if(valve_pos.ref >= VALVE_MAX_POS) { |
Lightvalve | 14:8e7590227d22 | 952 | valve_pos.ref = VALVE_MAX_POS; |
Lightvalve | 13:747daba9cf59 | 953 | } |
Lightvalve | 14:8e7590227d22 | 954 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 13:747daba9cf59 | 955 | if(CUR_VELOCITY >= 0) |
Lightvalve | 13:747daba9cf59 | 956 | VEL_POINT = VEL_POINT + 1; |
Lightvalve | 13:747daba9cf59 | 957 | else |
Lightvalve | 13:747daba9cf59 | 958 | VEL_POINT = VEL_POINT - 1; |
Lightvalve | 30:8d561f16383b | 959 | } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 960 | Ref_Valve_Pos_Old = valve_pos.ref; |
Lightvalve | 14:8e7590227d22 | 961 | FINAL_POS = pos.sen; |
Lightvalve | 14:8e7590227d22 | 962 | |
Lightvalve | 14:8e7590227d22 | 963 | if((FINAL_POS - START_POS)>100) { |
Lightvalve | 14:8e7590227d22 | 964 | DZ_DIRECTION = 1 * DZ_case; |
Lightvalve | 14:8e7590227d22 | 965 | } else if((FINAL_POS - START_POS)<-100) { |
Lightvalve | 14:8e7590227d22 | 966 | DZ_DIRECTION = -1 * DZ_case; |
Lightvalve | 14:8e7590227d22 | 967 | } else { |
Lightvalve | 13:747daba9cf59 | 968 | DZ_DIRECTION = 1 * DZ_case; |
Lightvalve | 13:747daba9cf59 | 969 | } |
Lightvalve | 14:8e7590227d22 | 970 | |
Lightvalve | 14:8e7590227d22 | 971 | VEL_POINT = 0; |
Lightvalve | 14:8e7590227d22 | 972 | |
Lightvalve | 13:747daba9cf59 | 973 | VALVE_DZ_timer = 0; |
Lightvalve | 13:747daba9cf59 | 974 | DZ_index= DZ_index *2; |
Lightvalve | 14:8e7590227d22 | 975 | if(DZ_index >= 128) { |
Lightvalve | 14:8e7590227d22 | 976 | FIRST_DZ = valve_pos.ref; |
Lightvalve | 13:747daba9cf59 | 977 | DZ_NUM = 2; |
Lightvalve | 13:747daba9cf59 | 978 | Ref_Valve_Pos_Old = FIRST_DZ; |
Lightvalve | 13:747daba9cf59 | 979 | DZ_index = 1; |
Lightvalve | 13:747daba9cf59 | 980 | DZ_DIRECTION = 1; |
Lightvalve | 13:747daba9cf59 | 981 | } |
Lightvalve | 13:747daba9cf59 | 982 | } |
Lightvalve | 14:8e7590227d22 | 983 | } else if((DZ_case == -1 && DZ_NUM == 2) | (DZ_case == 1 && DZ_NUM == 2)) { |
Lightvalve | 30:8d561f16383b | 984 | if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 985 | //V_out = (float) P_GAIN_JOINT_POSITION * (0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen)/(float) ENC_PULSE_PER_POSITION; |
Lightvalve | 59:f308b1656d9c | 986 | pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end; |
Lightvalve | 59:f308b1656d9c | 987 | CONTROL_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 30:8d561f16383b | 988 | } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 989 | START_POS = pos.sen; |
Lightvalve | 30:8d561f16383b | 990 | } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 18:b8adf1582ea3 | 991 | valve_pos.ref = Ref_Valve_Pos_Old + DZ_DIRECTION * 64 / DZ_index; |
Lightvalve | 14:8e7590227d22 | 992 | if(valve_pos.ref <= VALVE_MIN_POS) { |
Lightvalve | 14:8e7590227d22 | 993 | valve_pos.ref = VALVE_MIN_POS; |
Lightvalve | 14:8e7590227d22 | 994 | } else if(valve_pos.ref >= VALVE_MAX_POS) { |
Lightvalve | 14:8e7590227d22 | 995 | valve_pos.ref = VALVE_MAX_POS; |
Lightvalve | 13:747daba9cf59 | 996 | } |
Lightvalve | 14:8e7590227d22 | 997 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 13:747daba9cf59 | 998 | if(CUR_VELOCITY >= 0) |
Lightvalve | 13:747daba9cf59 | 999 | VEL_POINT = VEL_POINT + 1; |
Lightvalve | 13:747daba9cf59 | 1000 | else |
Lightvalve | 13:747daba9cf59 | 1001 | VEL_POINT = VEL_POINT - 1; |
Lightvalve | 30:8d561f16383b | 1002 | } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 1003 | Vout.ref = 0.0f; |
Lightvalve | 59:f308b1656d9c | 1004 | } else if(VALVE_DZ_timer > (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1005 | Ref_Valve_Pos_Old = valve_pos.ref; |
Lightvalve | 14:8e7590227d22 | 1006 | FINAL_POS = pos.sen; |
Lightvalve | 14:8e7590227d22 | 1007 | |
Lightvalve | 14:8e7590227d22 | 1008 | if((FINAL_POS - START_POS)>100) { |
Lightvalve | 13:747daba9cf59 | 1009 | DZ_DIRECTION = -1; |
Lightvalve | 14:8e7590227d22 | 1010 | } else if((FINAL_POS - START_POS)<-100) { |
Lightvalve | 14:8e7590227d22 | 1011 | DZ_DIRECTION = 1; |
Lightvalve | 14:8e7590227d22 | 1012 | } else { |
Lightvalve | 13:747daba9cf59 | 1013 | DZ_DIRECTION = 1; |
Lightvalve | 13:747daba9cf59 | 1014 | } |
Lightvalve | 14:8e7590227d22 | 1015 | |
Lightvalve | 14:8e7590227d22 | 1016 | VEL_POINT = 0; |
Lightvalve | 14:8e7590227d22 | 1017 | |
Lightvalve | 14:8e7590227d22 | 1018 | |
Lightvalve | 13:747daba9cf59 | 1019 | VALVE_DZ_timer = 0; |
Lightvalve | 13:747daba9cf59 | 1020 | DZ_index= DZ_index *2; |
Lightvalve | 14:8e7590227d22 | 1021 | if(DZ_index >= 128) { |
Lightvalve | 14:8e7590227d22 | 1022 | SECOND_DZ = valve_pos.ref; |
Lightvalve | 57:f4819de54e7a | 1023 | VALVE_CENTER = (int) (0.5f * (float) (FIRST_DZ) + 0.5f * (float) (SECOND_DZ)); |
Lightvalve | 13:747daba9cf59 | 1024 | first_check = 0; |
Lightvalve | 33:91b17819ec30 | 1025 | VALVE_DEADZONE_MINUS = (float) FIRST_DZ; |
Lightvalve | 33:91b17819ec30 | 1026 | VALVE_DEADZONE_PLUS = (float) SECOND_DZ; |
Lightvalve | 19:23b7c1ad8683 | 1027 | |
Lightvalve | 16:903b5a4433b4 | 1028 | ROM_RESET_DATA(); |
Lightvalve | 34:bb2ca2fc2a8e | 1029 | |
Lightvalve | 13:747daba9cf59 | 1030 | CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 13:747daba9cf59 | 1031 | DZ_index = 1; |
Lightvalve | 13:747daba9cf59 | 1032 | } |
Lightvalve | 13:747daba9cf59 | 1033 | } |
Lightvalve | 14:8e7590227d22 | 1034 | } else if(DZ_case == 0 && DZ_NUM ==1) { |
Lightvalve | 30:8d561f16383b | 1035 | if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 1036 | //V_out = (float) P_GAIN_JOINT_POSITION * (0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen)/(float) ENC_PULSE_PER_POSITION; |
Lightvalve | 59:f308b1656d9c | 1037 | pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end; |
Lightvalve | 59:f308b1656d9c | 1038 | CONTROL_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 30:8d561f16383b | 1039 | } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1040 | START_POS = pos.sen; |
Lightvalve | 30:8d561f16383b | 1041 | } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1042 | valve_pos.ref = Ref_Valve_Pos_Old - DZ_DIRECTION * 64 / DZ_index; |
Lightvalve | 14:8e7590227d22 | 1043 | if(valve_pos.ref <= VALVE_MIN_POS) { |
Lightvalve | 14:8e7590227d22 | 1044 | valve_pos.ref = VALVE_MIN_POS; |
Lightvalve | 14:8e7590227d22 | 1045 | } else if(valve_pos.ref >= VALVE_MAX_POS) { |
Lightvalve | 14:8e7590227d22 | 1046 | valve_pos.ref = VALVE_MAX_POS; |
Lightvalve | 13:747daba9cf59 | 1047 | } |
Lightvalve | 14:8e7590227d22 | 1048 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 13:747daba9cf59 | 1049 | if(CUR_VELOCITY >= 0) |
Lightvalve | 13:747daba9cf59 | 1050 | VEL_POINT = VEL_POINT + 1; |
Lightvalve | 13:747daba9cf59 | 1051 | else |
Lightvalve | 13:747daba9cf59 | 1052 | VEL_POINT = VEL_POINT - 1; |
Lightvalve | 30:8d561f16383b | 1053 | } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1054 | Ref_Valve_Pos_Old = valve_pos.ref; |
Lightvalve | 14:8e7590227d22 | 1055 | FINAL_POS = pos.sen; |
Lightvalve | 14:8e7590227d22 | 1056 | |
Lightvalve | 14:8e7590227d22 | 1057 | if((FINAL_POS - START_POS)>100) { |
Lightvalve | 14:8e7590227d22 | 1058 | DZ_DIRECTION = 1; |
Lightvalve | 14:8e7590227d22 | 1059 | } else if((FINAL_POS - START_POS)<-100) { |
Lightvalve | 14:8e7590227d22 | 1060 | DZ_DIRECTION = -1; |
Lightvalve | 14:8e7590227d22 | 1061 | } else { |
Lightvalve | 13:747daba9cf59 | 1062 | DZ_DIRECTION = 1; |
Lightvalve | 13:747daba9cf59 | 1063 | } |
Lightvalve | 14:8e7590227d22 | 1064 | |
Lightvalve | 14:8e7590227d22 | 1065 | VEL_POINT = 0; |
Lightvalve | 13:747daba9cf59 | 1066 | VALVE_DZ_timer = 0; |
Lightvalve | 13:747daba9cf59 | 1067 | DZ_index= DZ_index *2; |
Lightvalve | 14:8e7590227d22 | 1068 | if(DZ_index >= 128) { |
Lightvalve | 14:8e7590227d22 | 1069 | FIRST_DZ = valve_pos.ref; |
Lightvalve | 13:747daba9cf59 | 1070 | DZ_NUM = 2; |
Lightvalve | 13:747daba9cf59 | 1071 | Ref_Valve_Pos_Old = FIRST_DZ; |
Lightvalve | 13:747daba9cf59 | 1072 | DZ_index = 1; |
Lightvalve | 13:747daba9cf59 | 1073 | DZ_DIRECTION = 1; |
Lightvalve | 13:747daba9cf59 | 1074 | } |
Lightvalve | 13:747daba9cf59 | 1075 | } |
Lightvalve | 14:8e7590227d22 | 1076 | } else { |
Lightvalve | 30:8d561f16383b | 1077 | if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 1078 | //V_out = (float) P_GAIN_JOINT_POSITION * (0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen)/(float) ENC_PULSE_PER_POSITION; |
Lightvalve | 59:f308b1656d9c | 1079 | pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end; |
Lightvalve | 59:f308b1656d9c | 1080 | CONTROL_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 30:8d561f16383b | 1081 | } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1082 | START_POS = pos.sen; |
Lightvalve | 30:8d561f16383b | 1083 | } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1084 | valve_pos.ref = Ref_Valve_Pos_Old + DZ_DIRECTION * 64 / DZ_index; |
Lightvalve | 14:8e7590227d22 | 1085 | if(valve_pos.ref <= VALVE_MIN_POS) { |
Lightvalve | 14:8e7590227d22 | 1086 | valve_pos.ref = VALVE_MIN_POS; |
Lightvalve | 14:8e7590227d22 | 1087 | } else if(valve_pos.ref > VALVE_MAX_POS) { |
Lightvalve | 14:8e7590227d22 | 1088 | valve_pos.ref = VALVE_MAX_POS - 1; |
Lightvalve | 13:747daba9cf59 | 1089 | } |
Lightvalve | 14:8e7590227d22 | 1090 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 13:747daba9cf59 | 1091 | if(CUR_VELOCITY >= 0) |
Lightvalve | 13:747daba9cf59 | 1092 | VEL_POINT = VEL_POINT + 1; |
Lightvalve | 13:747daba9cf59 | 1093 | else |
Lightvalve | 13:747daba9cf59 | 1094 | VEL_POINT = VEL_POINT - 1; |
Lightvalve | 30:8d561f16383b | 1095 | } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 1096 | Vout.ref = 0.0f; |
Lightvalve | 59:f308b1656d9c | 1097 | } else if(VALVE_DZ_timer > (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1098 | Ref_Valve_Pos_Old = valve_pos.ref; |
Lightvalve | 14:8e7590227d22 | 1099 | FINAL_POS = pos.sen; |
Lightvalve | 13:747daba9cf59 | 1100 | if(VEL_POINT >= 0) |
Lightvalve | 13:747daba9cf59 | 1101 | DZ_DIRECTION = -1; |
Lightvalve | 13:747daba9cf59 | 1102 | else |
Lightvalve | 13:747daba9cf59 | 1103 | DZ_DIRECTION = 1; |
Lightvalve | 13:747daba9cf59 | 1104 | if(abs(FINAL_POS - START_POS) < 100) |
Lightvalve | 13:747daba9cf59 | 1105 | DZ_DIRECTION = 1; |
Lightvalve | 14:8e7590227d22 | 1106 | |
Lightvalve | 14:8e7590227d22 | 1107 | VEL_POINT = 0; |
Lightvalve | 14:8e7590227d22 | 1108 | |
Lightvalve | 14:8e7590227d22 | 1109 | |
Lightvalve | 13:747daba9cf59 | 1110 | VALVE_DZ_timer = 0; |
Lightvalve | 13:747daba9cf59 | 1111 | DZ_index= DZ_index *2; |
Lightvalve | 14:8e7590227d22 | 1112 | if(DZ_index >= 128) { |
Lightvalve | 14:8e7590227d22 | 1113 | SECOND_DZ = valve_pos.ref; |
Lightvalve | 57:f4819de54e7a | 1114 | VALVE_CENTER = (int) (0.5f * (float) (FIRST_DZ) + 0.5f * (float) (SECOND_DZ)); |
Lightvalve | 13:747daba9cf59 | 1115 | first_check = 0; |
Lightvalve | 33:91b17819ec30 | 1116 | VALVE_DEADZONE_MINUS = (float) FIRST_DZ; |
Lightvalve | 33:91b17819ec30 | 1117 | VALVE_DEADZONE_PLUS = (float) SECOND_DZ; |
Lightvalve | 59:f308b1656d9c | 1118 | |
Lightvalve | 59:f308b1656d9c | 1119 | ROM_RESET_DATA(); |
Lightvalve | 34:bb2ca2fc2a8e | 1120 | |
Lightvalve | 57:f4819de54e7a | 1121 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 13:747daba9cf59 | 1122 | DZ_index = 1; |
Lightvalve | 13:747daba9cf59 | 1123 | } |
Lightvalve | 13:747daba9cf59 | 1124 | } |
Lightvalve | 13:747daba9cf59 | 1125 | } |
Lightvalve | 14:8e7590227d22 | 1126 | } |
Lightvalve | 13:747daba9cf59 | 1127 | break; |
Lightvalve | 13:747daba9cf59 | 1128 | } |
Lightvalve | 14:8e7590227d22 | 1129 | |
Lightvalve | 14:8e7590227d22 | 1130 | case MODE_DDV_POS_VS_FLOWRATE: { |
Lightvalve | 59:f308b1656d9c | 1131 | CONTROL_MODE = MODE_VALVE_OPEN_LOOP; |
Lightvalve | 13:747daba9cf59 | 1132 | VALVE_FR_timer = VALVE_FR_timer + 1; |
Lightvalve | 14:8e7590227d22 | 1133 | if(first_check == 0) { |
Lightvalve | 30:8d561f16383b | 1134 | if(VALVE_FR_timer < (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 1135 | Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 23:59218d4a256d | 1136 | //CAN_TX_PRES((int16_t) (VALVE_FR_timer), (int16_t) (6)); |
Lightvalve | 30:8d561f16383b | 1137 | } else if(VALVE_FR_timer == (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 1138 | Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 14:8e7590227d22 | 1139 | pos_plus_end = pos.sen; |
Lightvalve | 14:8e7590227d22 | 1140 | // CAN_TX_PRES((int16_t) (V_out), (int16_t) (7)); |
Lightvalve | 30:8d561f16383b | 1141 | } else if(VALVE_FR_timer < (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 1142 | Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 30:8d561f16383b | 1143 | } else if(VALVE_FR_timer == (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1144 | // CAN_TX_PRES((int16_t) (V_out), (int16_t) (8)); |
Lightvalve | 59:f308b1656d9c | 1145 | Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 14:8e7590227d22 | 1146 | pos_minus_end = pos.sen; |
Lightvalve | 13:747daba9cf59 | 1147 | first_check = 1; |
Lightvalve | 13:747daba9cf59 | 1148 | VALVE_FR_timer = 0; |
Lightvalve | 57:f4819de54e7a | 1149 | valve_pos.ref = (float) VALVE_CENTER; |
Lightvalve | 13:747daba9cf59 | 1150 | ID_index = 0; |
Lightvalve | 13:747daba9cf59 | 1151 | max_check = 0; |
Lightvalve | 13:747daba9cf59 | 1152 | min_check = 0; |
Lightvalve | 13:747daba9cf59 | 1153 | } |
Lightvalve | 14:8e7590227d22 | 1154 | } else { |
Lightvalve | 30:8d561f16383b | 1155 | if(VALVE_FR_timer < (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 59:f308b1656d9c | 1156 | //V_out = (float) P_GAIN_JOINT_POSITION * (0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen)/(float) ENC_PULSE_PER_POSITION; |
Lightvalve | 59:f308b1656d9c | 1157 | pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end; |
Lightvalve | 59:f308b1656d9c | 1158 | CONTROL_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 30:8d561f16383b | 1159 | } else if(VALVE_FR_timer == (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 13:747daba9cf59 | 1160 | data_num = 0; |
Lightvalve | 57:f4819de54e7a | 1161 | valve_pos.ref = 10.0f*((float) ID_index_array[ID_index]) + (float) VALVE_CENTER; |
Lightvalve | 14:8e7590227d22 | 1162 | |
Lightvalve | 14:8e7590227d22 | 1163 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 14:8e7590227d22 | 1164 | START_POS = pos.sen; |
Lightvalve | 30:8d561f16383b | 1165 | } else if(VALVE_FR_timer < (int) (5.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 57:f4819de54e7a | 1166 | valve_pos.ref = 10.0f*((float) ID_index_array[ID_index]) + (float) VALVE_CENTER; |
Lightvalve | 14:8e7590227d22 | 1167 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 13:747daba9cf59 | 1168 | data_num = data_num + 1; |
Lightvalve | 30:8d561f16383b | 1169 | if(abs(0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen) > 20000.0f) { |
Lightvalve | 14:8e7590227d22 | 1170 | FINAL_POS = pos.sen; |
Lightvalve | 14:8e7590227d22 | 1171 | one_period_end = 1; |
Lightvalve | 13:747daba9cf59 | 1172 | } |
Lightvalve | 30:8d561f16383b | 1173 | } else if(VALVE_FR_timer == (int) (5.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1174 | FINAL_POS = pos.sen; |
Lightvalve | 13:747daba9cf59 | 1175 | one_period_end = 1; |
Lightvalve | 59:f308b1656d9c | 1176 | V_out = 0.0f; |
Lightvalve | 13:747daba9cf59 | 1177 | } |
Lightvalve | 14:8e7590227d22 | 1178 | |
Lightvalve | 14:8e7590227d22 | 1179 | if(one_period_end == 1) { |
Lightvalve | 14:8e7590227d22 | 1180 | if(valve_pos.ref > VALVE_MAX_POS) { |
Lightvalve | 13:747daba9cf59 | 1181 | max_check = 1; |
Lightvalve | 14:8e7590227d22 | 1182 | } else if(valve_pos.ref < VALVE_MIN_POS) { |
Lightvalve | 13:747daba9cf59 | 1183 | min_check = 1; |
Lightvalve | 13:747daba9cf59 | 1184 | } |
Lightvalve | 13:747daba9cf59 | 1185 | JOINT_VEL[ID_index] = (FINAL_POS - START_POS) / data_num * TMR_FREQ_5k; // pulse/sec |
Lightvalve | 19:23b7c1ad8683 | 1186 | |
Lightvalve | 13:747daba9cf59 | 1187 | VALVE_FR_timer = 0; |
Lightvalve | 13:747daba9cf59 | 1188 | one_period_end = 0; |
Lightvalve | 13:747daba9cf59 | 1189 | ID_index= ID_index +1; |
Lightvalve | 30:8d561f16383b | 1190 | V_out = 0.0f; |
Lightvalve | 13:747daba9cf59 | 1191 | } |
Lightvalve | 14:8e7590227d22 | 1192 | |
Lightvalve | 14:8e7590227d22 | 1193 | if(max_check == 1 && min_check == 1) { |
Lightvalve | 19:23b7c1ad8683 | 1194 | |
Lightvalve | 13:747daba9cf59 | 1195 | VALVE_POS_NUM = ID_index; |
Lightvalve | 59:f308b1656d9c | 1196 | ROM_RESET_DATA(); |
Lightvalve | 13:747daba9cf59 | 1197 | ID_index = 0; |
Lightvalve | 13:747daba9cf59 | 1198 | first_check = 0; |
Lightvalve | 13:747daba9cf59 | 1199 | VALVE_FR_timer = 0; |
Lightvalve | 57:f4819de54e7a | 1200 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 49:e7bcfc244d40 | 1201 | // CAN_TX_PRES((int16_t) (VALVE_FR_timer), (int16_t) (6)); |
Lightvalve | 13:747daba9cf59 | 1202 | } |
Lightvalve | 13:747daba9cf59 | 1203 | } |
Lightvalve | 13:747daba9cf59 | 1204 | break; |
Lightvalve | 13:747daba9cf59 | 1205 | } |
Lightvalve | 58:2eade98630e2 | 1206 | |
Lightvalve | 57:f4819de54e7a | 1207 | case MODE_SYSTEM_ID: { |
Lightvalve | 57:f4819de54e7a | 1208 | freq_sysid_Iref = (double) cnt_sysid * DT_TMR3 * 3.; |
Lightvalve | 57:f4819de54e7a | 1209 | valve_pos.ref = 2500.0f * sin(2.0f * 3.14159f * freq_sysid_Iref * (double) cnt_sysid * DT_TMR3); |
Lightvalve | 57:f4819de54e7a | 1210 | CONTROL_MODE = MODE_VALVE_OPEN_LOOP; |
Lightvalve | 57:f4819de54e7a | 1211 | cnt_sysid++; |
Lightvalve | 57:f4819de54e7a | 1212 | if (freq_sysid_Iref >= 300) { |
Lightvalve | 57:f4819de54e7a | 1213 | cnt_sysid = 0; |
Lightvalve | 57:f4819de54e7a | 1214 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 57:f4819de54e7a | 1215 | } |
Lightvalve | 57:f4819de54e7a | 1216 | break; |
Lightvalve | 57:f4819de54e7a | 1217 | } |
Lightvalve | 58:2eade98630e2 | 1218 | |
Lightvalve | 58:2eade98630e2 | 1219 | |
Lightvalve | 57:f4819de54e7a | 1220 | |
Lightvalve | 57:f4819de54e7a | 1221 | default: |
Lightvalve | 57:f4819de54e7a | 1222 | break; |
Lightvalve | 57:f4819de54e7a | 1223 | } |
Lightvalve | 57:f4819de54e7a | 1224 | |
Lightvalve | 57:f4819de54e7a | 1225 | // CONTROL MODE ------------------------------------------------------------ |
Lightvalve | 57:f4819de54e7a | 1226 | |
Lightvalve | 57:f4819de54e7a | 1227 | switch (CONTROL_MODE) { |
Lightvalve | 57:f4819de54e7a | 1228 | case MODE_NO_ACT: { |
Lightvalve | 57:f4819de54e7a | 1229 | V_out = 0.0f; |
Lightvalve | 57:f4819de54e7a | 1230 | break; |
Lightvalve | 57:f4819de54e7a | 1231 | } |
Lightvalve | 57:f4819de54e7a | 1232 | |
Lightvalve | 57:f4819de54e7a | 1233 | case MODE_VALVE_POSITION_CONTROL: { |
Lightvalve | 57:f4819de54e7a | 1234 | if (((OPERATING_MODE&0b110)>>1) == 0) { //Moog Valve |
Lightvalve | 57:f4819de54e7a | 1235 | I_REF = valve_pos.ref; |
Lightvalve | 57:f4819de54e7a | 1236 | } else if (((OPERATING_MODE&0b110)>>1) == 1) { //KNR Valve |
Lightvalve | 57:f4819de54e7a | 1237 | V_out = valve_pos.ref; |
Lightvalve | 57:f4819de54e7a | 1238 | } else { //SW Valve |
Lightvalve | 57:f4819de54e7a | 1239 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 59:f308b1656d9c | 1240 | V_out = Vout.ref; |
Lightvalve | 57:f4819de54e7a | 1241 | } |
Lightvalve | 58:2eade98630e2 | 1242 | |
Lightvalve | 57:f4819de54e7a | 1243 | break; |
Lightvalve | 57:f4819de54e7a | 1244 | } |
Lightvalve | 57:f4819de54e7a | 1245 | |
Lightvalve | 57:f4819de54e7a | 1246 | case MODE_JOINT_CONTROL: { |
Lightvalve | 57:f4819de54e7a | 1247 | double torq_ref = 0.0f; |
Lightvalve | 57:f4819de54e7a | 1248 | pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm] |
Lightvalve | 57:f4819de54e7a | 1249 | vel.err = (vel.ref - vel.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm/s] |
Lightvalve | 57:f4819de54e7a | 1250 | pos.err_sum += pos.err/(float) TMR_FREQ_5k; //[mm] |
Lightvalve | 57:f4819de54e7a | 1251 | torq_ref = torq.ref + (K_SPRING * pos.err * 0.01f + D_DAMPER * vel.err * 0.0001f) / ENC_PULSE_PER_POSITION; //[N] |
Lightvalve | 57:f4819de54e7a | 1252 | |
Lightvalve | 57:f4819de54e7a | 1253 | // torque feedback |
Lightvalve | 58:2eade98630e2 | 1254 | torq.err = (torq_ref)/(float)(TORQUE_SENSOR_PULSE_PER_TORQUE) - torq.sen; //[N] |
Lightvalve | 57:f4819de54e7a | 1255 | torq.err_sum += torq.err/(float) TMR_FREQ_5k; //[N] |
Lightvalve | 57:f4819de54e7a | 1256 | |
Lightvalve | 57:f4819de54e7a | 1257 | if (((OPERATING_MODE&0b110)>>1) == 0 || ((OPERATING_MODE&0b110)>>1) == 1) { |
Lightvalve | 58:2eade98630e2 | 1258 | |
Lightvalve | 57:f4819de54e7a | 1259 | double I_REF_POS = 0.0f; |
Lightvalve | 57:f4819de54e7a | 1260 | double I_REF_FORCE_FB = 0.0f; // I_REF by Force Feedback |
Lightvalve | 57:f4819de54e7a | 1261 | double I_REF_VC = 0.0f; // I_REF for velocity compensation |
Lightvalve | 57:f4819de54e7a | 1262 | |
Lightvalve | 57:f4819de54e7a | 1263 | double temp_vel_pos = 0.0f; |
Lightvalve | 57:f4819de54e7a | 1264 | double temp_vel_torq = 0.0f; |
Lightvalve | 57:f4819de54e7a | 1265 | double wn_Pos = 2.0f * PI * 5.0f; // f_cut : 5Hz Position Control |
Lightvalve | 58:2eade98630e2 | 1266 | |
Lightvalve | 57:f4819de54e7a | 1267 | if ((OPERATING_MODE && 0x01) == 0) { // Rotary Mode |
Lightvalve | 57:f4819de54e7a | 1268 | temp_vel_pos = (0.01f * (double) P_GAIN_JOINT_POSITION * wn_Pos * pos.err + 0.01f * (double) I_GAIN_JOINT_POSITION * wn_Pos * pos.err_sum + 0.01f * (double) VELOCITY_COMP_GAIN * vel.ref / ENC_PULSE_PER_POSITION) * PI / 180.0f; // rad/s |
Lightvalve | 57:f4819de54e7a | 1269 | // L when P-gain = 100, f_cut = 10Hz L feedforward velocity |
Lightvalve | 57:f4819de54e7a | 1270 | } else if ((OPERATING_MODE && 0x01) == 1) { |
Lightvalve | 57:f4819de54e7a | 1271 | temp_vel_pos = (0.01f * (double) P_GAIN_JOINT_POSITION * wn_Pos * pos.err + 0.01f * (double) I_GAIN_JOINT_POSITION * wn_Pos * pos.err_sum + 0.01f * (double) VELOCITY_COMP_GAIN * vel.ref / ENC_PULSE_PER_POSITION); // mm/s |
Lightvalve | 57:f4819de54e7a | 1272 | // L when P-gain = 100, f_cut = 10Hz L feedforward velocity |
Lightvalve | 57:f4819de54e7a | 1273 | } |
Lightvalve | 57:f4819de54e7a | 1274 | if (temp_vel_pos > 0.0f) I_REF_POS = temp_vel_pos * ((double) PISTON_AREA_A * 0.00006f / (K_v * sqrt(2.0f * alpha3 / (alpha3 + 1.0f)))); |
Lightvalve | 57:f4819de54e7a | 1275 | else I_REF_POS = temp_vel_pos * ((double) PISTON_AREA_B * 0.00006f / (K_v * sqrt(2.0f / (alpha3 + 1.0f)))); |
Lightvalve | 57:f4819de54e7a | 1276 | |
Lightvalve | 57:f4819de54e7a | 1277 | // velocity compensation for torque control |
Lightvalve | 57:f4819de54e7a | 1278 | if ((OPERATING_MODE && 0x01) == 0) { // Rotary Mode |
Lightvalve | 57:f4819de54e7a | 1279 | I_REF_FORCE_FB = 0.001f * ((double) P_GAIN_JOINT_TORQUE * torq.err + (double) I_GAIN_JOINT_TORQUE * torq.err_sum); |
Lightvalve | 57:f4819de54e7a | 1280 | // temp_vel_torq = (0.01 * (double) VELOCITY_COMP_GAIN * (double) CUR_VELOCITY / (double) ENC_PULSE_PER_POSITION) * PI / 180.0; // rad/s |
Lightvalve | 57:f4819de54e7a | 1281 | temp_vel_torq = (0.01f * (double) VELOCITY_COMP_GAIN * vel.ref / (double) ENC_PULSE_PER_POSITION) * PI / 180.0f; // rad/s |
Lightvalve | 57:f4819de54e7a | 1282 | // L feedforward velocity |
Lightvalve | 57:f4819de54e7a | 1283 | } else if ((OPERATING_MODE && 0x01) == 1) { |
Lightvalve | 57:f4819de54e7a | 1284 | I_REF_FORCE_FB = 0.001f * 0.01f*((double) P_GAIN_JOINT_TORQUE * torq.err + (double) I_GAIN_JOINT_TORQUE * torq.err_sum); // Linear Actuators are more sensitive. |
Lightvalve | 57:f4819de54e7a | 1285 | // temp_vel_torq = (0.01 * (double) VELOCITY_COMP_GAIN * (double) CUR_VELOCITY / (double) ENC_PULSE_PER_POSITION); // mm/s |
Lightvalve | 57:f4819de54e7a | 1286 | temp_vel_torq = (0.01f * (double) VELOCITY_COMP_GAIN * vel.ref / (double) ENC_PULSE_PER_POSITION); // mm/s |
Lightvalve | 57:f4819de54e7a | 1287 | // L feedforward velocity |
Lightvalve | 57:f4819de54e7a | 1288 | } |
Lightvalve | 57:f4819de54e7a | 1289 | if (temp_vel_torq > 0.0f) I_REF_VC = temp_vel_torq * ((double) PISTON_AREA_A * 0.00006f / (K_v * sqrt(2.0f * alpha3 / (alpha3 + 1.0f)))); |
Lightvalve | 57:f4819de54e7a | 1290 | else I_REF_VC = temp_vel_torq * ((double) PISTON_AREA_B * 0.00006f / (K_v * sqrt(2.0f / (alpha3 + 1.0f)))); |
Lightvalve | 57:f4819de54e7a | 1291 | // L velocity(rad/s or mm/s) >> I_ref(mA) |
Lightvalve | 57:f4819de54e7a | 1292 | // Ref_Joint_FT_dot = (Ref_Joint_FT_Nm - Ref_Joint_FT_Nm_old) / TMR_DT_5k; |
Lightvalve | 57:f4819de54e7a | 1293 | // Ref_Joint_FT_Nm_old = Ref_Joint_FT_Nm; |
Lightvalve | 57:f4819de54e7a | 1294 | |
Lightvalve | 57:f4819de54e7a | 1295 | I_REF = (1.0f - alpha_trans) * I_REF_POS + alpha_trans * (I_REF_VC + I_REF_FORCE_FB); |
Lightvalve | 57:f4819de54e7a | 1296 | |
Lightvalve | 57:f4819de54e7a | 1297 | } else { |
Lightvalve | 57:f4819de54e7a | 1298 | float VALVE_POS_RAW_FORCE_FB = 0.0f; |
Lightvalve | 57:f4819de54e7a | 1299 | |
Lightvalve | 57:f4819de54e7a | 1300 | VALVE_POS_RAW_FORCE_FB = alpha_trans*(((float) P_GAIN_JOINT_TORQUE * torq.err + (float) I_GAIN_JOINT_TORQUE * torq.err_sum) * 0.01f |
Lightvalve | 57:f4819de54e7a | 1301 | + DDV_JOINT_POS_FF(vel.sen))+ (1.0f-alpha_trans) * (P_GAIN_JOINT_POSITION * 0.01f * pos.err + DDV_JOINT_POS_FF(vel.ref)); |
Lightvalve | 57:f4819de54e7a | 1302 | |
Lightvalve | 57:f4819de54e7a | 1303 | if (VALVE_POS_RAW_FORCE_FB >= 0) { |
Lightvalve | 57:f4819de54e7a | 1304 | valve_pos.ref = VALVE_POS_RAW_FORCE_FB + VALVE_DEADZONE_PLUS; |
Lightvalve | 57:f4819de54e7a | 1305 | } else { |
Lightvalve | 57:f4819de54e7a | 1306 | valve_pos.ref = VALVE_POS_RAW_FORCE_FB + VALVE_DEADZONE_MINUS; |
Lightvalve | 57:f4819de54e7a | 1307 | } |
Lightvalve | 57:f4819de54e7a | 1308 | |
Lightvalve | 57:f4819de54e7a | 1309 | if(I_GAIN_JOINT_TORQUE != 0) { |
Lightvalve | 57:f4819de54e7a | 1310 | double Ka = 1.0f / (double) I_GAIN_JOINT_TORQUE * 100.0f; |
Lightvalve | 57:f4819de54e7a | 1311 | if(valve_pos.ref>VALVE_MAX_POS) { |
Lightvalve | 57:f4819de54e7a | 1312 | double valve_pos_rem = valve_pos.ref - VALVE_MAX_POS; |
Lightvalve | 57:f4819de54e7a | 1313 | valve_pos_rem = valve_pos_rem * Ka; |
Lightvalve | 57:f4819de54e7a | 1314 | valve_pos.ref = VALVE_MAX_POS; |
Lightvalve | 57:f4819de54e7a | 1315 | torq.err_sum = torq.err_sum - valve_pos_rem/(float) TMR_FREQ_5k; |
Lightvalve | 57:f4819de54e7a | 1316 | } else if(valve_pos.ref < VALVE_MIN_POS) { |
Lightvalve | 57:f4819de54e7a | 1317 | double valve_pos_rem = valve_pos.ref - VALVE_MIN_POS; |
Lightvalve | 57:f4819de54e7a | 1318 | valve_pos_rem = valve_pos_rem * Ka; |
Lightvalve | 57:f4819de54e7a | 1319 | valve_pos.ref = VALVE_MIN_POS; |
Lightvalve | 57:f4819de54e7a | 1320 | torq.err_sum = torq.err_sum - valve_pos_rem/(float) TMR_FREQ_5k; |
Lightvalve | 57:f4819de54e7a | 1321 | } |
Lightvalve | 57:f4819de54e7a | 1322 | } |
Lightvalve | 57:f4819de54e7a | 1323 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 57:f4819de54e7a | 1324 | |
Lightvalve | 57:f4819de54e7a | 1325 | } |
Lightvalve | 58:2eade98630e2 | 1326 | |
Lightvalve | 57:f4819de54e7a | 1327 | break; |
Lightvalve | 57:f4819de54e7a | 1328 | } |
Lightvalve | 58:2eade98630e2 | 1329 | |
Lightvalve | 57:f4819de54e7a | 1330 | case MODE_VALVE_OPEN_LOOP: { |
Lightvalve | 57:f4819de54e7a | 1331 | V_out = (float) Vout.ref; |
Lightvalve | 57:f4819de54e7a | 1332 | break; |
Lightvalve | 57:f4819de54e7a | 1333 | } |
Lightvalve | 14:8e7590227d22 | 1334 | |
Lightvalve | 12:6f2531038ea4 | 1335 | default: |
Lightvalve | 12:6f2531038ea4 | 1336 | break; |
Lightvalve | 12:6f2531038ea4 | 1337 | } |
Lightvalve | 14:8e7590227d22 | 1338 | |
Lightvalve | 57:f4819de54e7a | 1339 | |
Lightvalve | 57:f4819de54e7a | 1340 | if (((OPERATING_MODE&0b110)>>1) == 0 || ((OPERATING_MODE&0b110)>>1) == 1) { //Moog Valve or KNR Valve |
Lightvalve | 58:2eade98630e2 | 1341 | |
Lightvalve | 57:f4819de54e7a | 1342 | //////////////////////////////////////////////////////////////////////////// |
Lightvalve | 57:f4819de54e7a | 1343 | //////////////////////////// CURRENT CONTROL ////////////////////////////// |
Lightvalve | 57:f4819de54e7a | 1344 | //////////////////////////////////////////////////////////////////////////// |
Lightvalve | 57:f4819de54e7a | 1345 | if (CURRENT_CONTROL_MODE) { |
Lightvalve | 57:f4819de54e7a | 1346 | double alpha_update_Iref = 1.0f / (1.0f + TMR_FREQ_5k / (2.0f * 3.14f * 300.0f)); // f_cutoff : 500Hz |
Lightvalve | 57:f4819de54e7a | 1347 | I_REF_fil = (1.0f - alpha_update_Iref) * I_REF_fil + alpha_update_Iref*I_REF; |
Lightvalve | 57:f4819de54e7a | 1348 | |
Lightvalve | 57:f4819de54e7a | 1349 | I_ERR = I_REF_fil - cur.sen; |
Lightvalve | 57:f4819de54e7a | 1350 | I_ERR_INT = I_ERR_INT + (I_ERR) * DT_TMR3; |
Lightvalve | 57:f4819de54e7a | 1351 | |
Lightvalve | 57:f4819de54e7a | 1352 | |
Lightvalve | 57:f4819de54e7a | 1353 | // Moog Valve Current Control Gain |
Lightvalve | 57:f4819de54e7a | 1354 | double R_model = 539.0f; // ohm |
Lightvalve | 57:f4819de54e7a | 1355 | double L_model = 1.2f; |
Lightvalve | 57:f4819de54e7a | 1356 | double w0 = 2.0f * 3.14f * 150.0f; |
Lightvalve | 57:f4819de54e7a | 1357 | double KP_I = 0.1f * L_model*w0; |
Lightvalve | 57:f4819de54e7a | 1358 | double KI_I = 0.1f * R_model*w0; |
Lightvalve | 57:f4819de54e7a | 1359 | |
Lightvalve | 57:f4819de54e7a | 1360 | // KNR Valve Current Control Gain |
Lightvalve | 57:f4819de54e7a | 1361 | if (((OPERATING_MODE & 0b110)>>1) == 1) { // KNR Valve |
Lightvalve | 57:f4819de54e7a | 1362 | R_model = 163.0f; // ohm |
Lightvalve | 57:f4819de54e7a | 1363 | L_model = 1.0f; |
Lightvalve | 57:f4819de54e7a | 1364 | w0 = 2.0f * 3.14f * 80.0f; |
Lightvalve | 57:f4819de54e7a | 1365 | KP_I = 1.0f * L_model*w0; |
Lightvalve | 57:f4819de54e7a | 1366 | KI_I = 0.08f * R_model*w0; |
Lightvalve | 57:f4819de54e7a | 1367 | } |
Lightvalve | 57:f4819de54e7a | 1368 | |
Lightvalve | 57:f4819de54e7a | 1369 | double FF_gain = 1.0f; |
Lightvalve | 57:f4819de54e7a | 1370 | |
Lightvalve | 57:f4819de54e7a | 1371 | VALVE_PWM_RAW = KP_I * I_ERR + KI_I * I_ERR_INT; |
Lightvalve | 57:f4819de54e7a | 1372 | // VALVE_PWM_RAW = VALVE_PWM_RAW + FF_gain * (R_model*I_REF); // Unit : mV |
Lightvalve | 57:f4819de54e7a | 1373 | I_REF_fil_diff = I_REF_fil - I_REF_fil_old; |
Lightvalve | 57:f4819de54e7a | 1374 | I_REF_fil_old = I_REF_fil; |
Lightvalve | 57:f4819de54e7a | 1375 | VALVE_PWM_RAW = VALVE_PWM_RAW + FF_gain * (R_model * I_REF_fil + L_model * I_REF_fil_diff * TMR_FREQ_5k); // Unit : mV |
Lightvalve | 57:f4819de54e7a | 1376 | double V_MAX = 12000.; // Maximum Voltage : 12V = 12000mV |
Lightvalve | 57:f4819de54e7a | 1377 | |
Lightvalve | 57:f4819de54e7a | 1378 | double Ka = 3.0f / KP_I; |
Lightvalve | 57:f4819de54e7a | 1379 | if (VALVE_PWM_RAW > V_MAX) { |
Lightvalve | 57:f4819de54e7a | 1380 | V_rem = VALVE_PWM_RAW - V_MAX; |
Lightvalve | 57:f4819de54e7a | 1381 | V_rem = Ka*V_rem; |
Lightvalve | 57:f4819de54e7a | 1382 | VALVE_PWM_RAW = V_MAX; |
Lightvalve | 57:f4819de54e7a | 1383 | I_ERR_INT = I_ERR_INT - V_rem * DT_TMR3; |
Lightvalve | 57:f4819de54e7a | 1384 | } else if (VALVE_PWM_RAW < -V_MAX) { |
Lightvalve | 57:f4819de54e7a | 1385 | V_rem = VALVE_PWM_RAW - (-V_MAX); |
Lightvalve | 57:f4819de54e7a | 1386 | V_rem = Ka*V_rem; |
Lightvalve | 57:f4819de54e7a | 1387 | VALVE_PWM_RAW = -V_MAX; |
Lightvalve | 57:f4819de54e7a | 1388 | I_ERR_INT = I_ERR_INT - V_rem * DT_TMR3; |
Lightvalve | 57:f4819de54e7a | 1389 | } |
Lightvalve | 57:f4819de54e7a | 1390 | Cur_Valve_Open_pulse = cur.sen / mA_PER_pulse; |
Lightvalve | 57:f4819de54e7a | 1391 | } else { |
Lightvalve | 57:f4819de54e7a | 1392 | VALVE_PWM_RAW = I_REF * mV_PER_mA; |
Lightvalve | 57:f4819de54e7a | 1393 | Cur_Valve_Open_pulse = I_REF / mA_PER_pulse; |
Lightvalve | 57:f4819de54e7a | 1394 | } |
Lightvalve | 57:f4819de54e7a | 1395 | |
Lightvalve | 57:f4819de54e7a | 1396 | //////////////////////////////////////////////////////////////////////////// |
Lightvalve | 57:f4819de54e7a | 1397 | ///////////////// Dead Zone Cancellation & Linearization ////////////////// |
Lightvalve | 57:f4819de54e7a | 1398 | //////////////////////////////////////////////////////////////////////////// |
Lightvalve | 57:f4819de54e7a | 1399 | // Dead Zone Cancellation (Mechanical Valve dead-zone) |
Lightvalve | 57:f4819de54e7a | 1400 | if (FLAG_VALVE_DEADZONE) { |
Lightvalve | 57:f4819de54e7a | 1401 | if (VALVE_PWM_RAW > 0) VALVE_PWM_RAW = VALVE_PWM_RAW + VALVE_DEADZONE_PLUS * mV_PER_pulse; // unit: mV |
Lightvalve | 57:f4819de54e7a | 1402 | else if (VALVE_PWM_RAW < 0) VALVE_PWM_RAW = VALVE_PWM_RAW + VALVE_DEADZONE_MINUS * mV_PER_pulse; // unit: mV |
Lightvalve | 57:f4819de54e7a | 1403 | |
Lightvalve | 57:f4819de54e7a | 1404 | VALVE_PWM_VALVE_DZ = VALVE_PWM_RAW + (double)VALVE_CENTER * mV_PER_pulse; // unit: mV |
Lightvalve | 57:f4819de54e7a | 1405 | |
Lightvalve | 57:f4819de54e7a | 1406 | } else { |
Lightvalve | 57:f4819de54e7a | 1407 | VALVE_PWM_VALVE_DZ = VALVE_PWM_RAW; |
Lightvalve | 57:f4819de54e7a | 1408 | } |
Lightvalve | 57:f4819de54e7a | 1409 | |
Lightvalve | 57:f4819de54e7a | 1410 | // Output Voltage Linearization |
Lightvalve | 57:f4819de54e7a | 1411 | double CUR_PWM_nonlin = VALVE_PWM_VALVE_DZ; // Unit : mV |
Lightvalve | 57:f4819de54e7a | 1412 | double CUR_PWM_lin = PWM_duty_byLT(CUR_PWM_nonlin); |
Lightvalve | 57:f4819de54e7a | 1413 | |
Lightvalve | 57:f4819de54e7a | 1414 | // Dead Zone Cancellation (Electrical dead-zone) |
Lightvalve | 57:f4819de54e7a | 1415 | if (CUR_PWM_lin > 0) V_out = (int) CUR_PWM_lin + 140; |
Lightvalve | 57:f4819de54e7a | 1416 | else if (CUR_PWM_lin < 0) V_out = (int) CUR_PWM_lin - 140; |
Lightvalve | 57:f4819de54e7a | 1417 | else V_out = CUR_PWM_lin; |
Lightvalve | 57:f4819de54e7a | 1418 | } |
Lightvalve | 58:2eade98630e2 | 1419 | |
Lightvalve | 58:2eade98630e2 | 1420 | |
Lightvalve | 14:8e7590227d22 | 1421 | |
jobuuu | 7:e9086c72bb22 | 1422 | /******************************************************* |
jobuuu | 7:e9086c72bb22 | 1423 | *** PWM |
jobuuu | 7:e9086c72bb22 | 1424 | ********************************************************/ |
Lightvalve | 49:e7bcfc244d40 | 1425 | if (V_out >= VALVE_VOLTAGE_LIMIT*1000.0f) { |
Lightvalve | 49:e7bcfc244d40 | 1426 | V_out = VALVE_VOLTAGE_LIMIT*1000.0f; |
Lightvalve | 49:e7bcfc244d40 | 1427 | } else if(V_out<=-VALVE_VOLTAGE_LIMIT*1000.0f) { |
Lightvalve | 49:e7bcfc244d40 | 1428 | V_out = -VALVE_VOLTAGE_LIMIT*1000.0f; |
Lightvalve | 27:a2254a485f23 | 1429 | } |
Lightvalve | 49:e7bcfc244d40 | 1430 | PWM_out= V_out/(SUPPLY_VOLTAGE*1000.0f); // Full duty : 12000.0mV |
Lightvalve | 13:747daba9cf59 | 1431 | |
Lightvalve | 19:23b7c1ad8683 | 1432 | // Saturation of output voltage to 12.0V |
Lightvalve | 30:8d561f16383b | 1433 | if(PWM_out > 1.0f) PWM_out=1.0f; |
Lightvalve | 30:8d561f16383b | 1434 | else if (PWM_out < -1.0f) PWM_out=-1.0f; |
Lightvalve | 13:747daba9cf59 | 1435 | |
Lightvalve | 30:8d561f16383b | 1436 | if (PWM_out>0.0f) { |
Lightvalve | 30:8d561f16383b | 1437 | dtc_v=0.0f; |
jobuuu | 1:e04e563be5ce | 1438 | dtc_w=PWM_out; |
jobuuu | 1:e04e563be5ce | 1439 | } else { |
jobuuu | 2:a1c0a37df760 | 1440 | dtc_v=-PWM_out; |
Lightvalve | 30:8d561f16383b | 1441 | dtc_w=0.0f; |
jobuuu | 1:e04e563be5ce | 1442 | } |
Lightvalve | 13:747daba9cf59 | 1443 | |
jobuuu | 1:e04e563be5ce | 1444 | //pwm |
Lightvalve | 30:8d561f16383b | 1445 | TIM4->CCR2 = (PWM_ARR)*(1.0f-dtc_v); |
Lightvalve | 30:8d561f16383b | 1446 | TIM4->CCR1 = (PWM_ARR)*(1.0f-dtc_w); |
Lightvalve | 13:747daba9cf59 | 1447 | |
Lightvalve | 20:806196fda269 | 1448 | |
Lightvalve | 57:f4819de54e7a | 1449 | if (TMR2_COUNT_CAN_TX % (int) ((int) TMR_FREQ_5k/CAN_FREQ) == 0) { |
Lightvalve | 20:806196fda269 | 1450 | |
Lightvalve | 54:647072f5307a | 1451 | // Position, Velocity, and Torque (ID:1200) |
Lightvalve | 56:6f50d9d3bfee | 1452 | if (flag_data_request[0] == HIGH) { |
Lightvalve | 57:f4819de54e7a | 1453 | if ((OPERATING_MODE & 0b01) == 0) { // Rotary Actuator |
Lightvalve | 54:647072f5307a | 1454 | if (SENSING_MODE == 0) { |
Lightvalve | 58:2eade98630e2 | 1455 | CAN_TX_POSITION_FT((int16_t) (pos.sen), (int16_t) (vel.sen/10.0f), (int16_t) (pres_A.sen*10.0f)); |
Lightvalve | 54:647072f5307a | 1456 | } else if (SENSING_MODE == 1) { |
Lightvalve | 58:2eade98630e2 | 1457 | CAN_TX_POSITION_PRESSURE((int16_t) (pos.sen), (int16_t) (vel.sen/10.0f), (int16_t) ((pres_A.sen)*5.0f), (int16_t) ((pres_B.sen)*5.0f)); |
Lightvalve | 54:647072f5307a | 1458 | } |
Lightvalve | 57:f4819de54e7a | 1459 | } else if ((OPERATING_MODE & 0b01) == 1) { // Linear Actuator |
Lightvalve | 54:647072f5307a | 1460 | if (SENSING_MODE == 0) { |
Lightvalve | 59:f308b1656d9c | 1461 | CAN_TX_POSITION_FT((int16_t) (pos.sen/4.0f), (int16_t) (vel.sen/100.0f), (int16_t) (V_out)); |
Lightvalve | 54:647072f5307a | 1462 | } else if (SENSING_MODE == 1) { |
Lightvalve | 58:2eade98630e2 | 1463 | CAN_TX_POSITION_PRESSURE((int16_t) (pos.sen/4.0f), (int16_t) (vel.sen/100.0f), (int16_t) ((pres_A.sen)*5.0f), (int16_t) ((pres_B.sen)*5.0f)); |
Lightvalve | 54:647072f5307a | 1464 | } |
Lightvalve | 52:8ea76864368a | 1465 | } |
Lightvalve | 52:8ea76864368a | 1466 | } |
Lightvalve | 56:6f50d9d3bfee | 1467 | if (flag_data_request[1] == HIGH) { |
Lightvalve | 54:647072f5307a | 1468 | //valve position |
Lightvalve | 54:647072f5307a | 1469 | double t_value = 0; |
Lightvalve | 57:f4819de54e7a | 1470 | // if(value>=(float) VALVE_CENTER) { |
Lightvalve | 57:f4819de54e7a | 1471 | // t_value = 10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER); |
Lightvalve | 57:f4819de54e7a | 1472 | // } else { |
Lightvalve | 57:f4819de54e7a | 1473 | // t_value = -10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER); |
Lightvalve | 57:f4819de54e7a | 1474 | // } |
Lightvalve | 58:2eade98630e2 | 1475 | if(OPERATING_MODE==5) { |
Lightvalve | 57:f4819de54e7a | 1476 | t_value = (double) value; |
Lightvalve | 58:2eade98630e2 | 1477 | } else if(CURRENT_CONTROL_MODE==1) { |
Lightvalve | 57:f4819de54e7a | 1478 | t_value = cur.sen; |
Lightvalve | 58:2eade98630e2 | 1479 | } else { |
Lightvalve | 57:f4819de54e7a | 1480 | t_value = V_out; |
Lightvalve | 54:647072f5307a | 1481 | } |
Lightvalve | 57:f4819de54e7a | 1482 | CAN_TX_TORQUE((int16_t) (t_value)); //1300 |
Lightvalve | 54:647072f5307a | 1483 | } |
Lightvalve | 58:2eade98630e2 | 1484 | |
Lightvalve | 58:2eade98630e2 | 1485 | |
Lightvalve | 56:6f50d9d3bfee | 1486 | if (flag_data_request[2] == HIGH) { |
Lightvalve | 56:6f50d9d3bfee | 1487 | //pressure A and B |
Lightvalve | 57:f4819de54e7a | 1488 | CAN_TX_PRES((int16_t) (pres_A.sen), (int16_t) (pres_B.sen)); // CUR_PRES_X : 0(0bar)~4096(210bar) //1400 |
Lightvalve | 55:b25725257569 | 1489 | } |
Lightvalve | 58:2eade98630e2 | 1490 | |
Lightvalve | 57:f4819de54e7a | 1491 | //If it doesn't rest, below can can not work. |
Lightvalve | 57:f4819de54e7a | 1492 | for (can_rest = 0; can_rest < 10000; can_rest++) { |
Lightvalve | 57:f4819de54e7a | 1493 | ; |
Lightvalve | 57:f4819de54e7a | 1494 | } |
Lightvalve | 58:2eade98630e2 | 1495 | |
Lightvalve | 56:6f50d9d3bfee | 1496 | if (flag_data_request[3] == HIGH) { |
Lightvalve | 54:647072f5307a | 1497 | //PWM |
Lightvalve | 57:f4819de54e7a | 1498 | CAN_TX_PWM((int16_t) cur.sen); //1500 |
Lightvalve | 54:647072f5307a | 1499 | } |
Lightvalve | 57:f4819de54e7a | 1500 | //for (i = 0; i < 10000; i++) { |
Lightvalve | 57:f4819de54e7a | 1501 | // ; |
Lightvalve | 57:f4819de54e7a | 1502 | // } |
Lightvalve | 56:6f50d9d3bfee | 1503 | if (flag_data_request[4] == HIGH) { |
Lightvalve | 54:647072f5307a | 1504 | //valve position |
Lightvalve | 57:f4819de54e7a | 1505 | CAN_TX_VALVE_POSITION((int16_t) (CAN_FREQ), (int16_t) (D_DAMPER), (int16_t) OPERATING_MODE); //1600 |
Lightvalve | 54:647072f5307a | 1506 | } |
Lightvalve | 20:806196fda269 | 1507 | |
Lightvalve | 54:647072f5307a | 1508 | // Others : Reference position, Reference FT, PWM, Current (ID:1300) |
Lightvalve | 52:8ea76864368a | 1509 | // if (flag_data_request[1] == HIGH) { |
Lightvalve | 52:8ea76864368a | 1510 | // CAN_TX_SOMETHING((int) (FORCE_VREF), (int16_t) (1), (int16_t) (2), (int16_t) (3)); |
Lightvalve | 52:8ea76864368a | 1511 | // } |
Lightvalve | 54:647072f5307a | 1512 | //if (flag_delay_test == 1){ |
Lightvalve | 58:2eade98630e2 | 1513 | //CAN_TX_PRES((int16_t) (0),(int16_t) torq_ref); |
Lightvalve | 54:647072f5307a | 1514 | //} |
Lightvalve | 52:8ea76864368a | 1515 | |
Lightvalve | 54:647072f5307a | 1516 | TMR2_COUNT_CAN_TX = 0; |
Lightvalve | 54:647072f5307a | 1517 | } |
Lightvalve | 54:647072f5307a | 1518 | TMR2_COUNT_CAN_TX++; |
Lightvalve | 52:8ea76864368a | 1519 | |
Lightvalve | 20:806196fda269 | 1520 | } |
Lightvalve | 52:8ea76864368a | 1521 | TIM3->SR = 0x0; // reset the status register |
Lightvalve | 52:8ea76864368a | 1522 | |
Lightvalve | 58:2eade98630e2 | 1523 | } |