Sungwoo Kim
/
HydraulicControlBoard_Rainbow_v1_2_copy
2011
main.cpp@58:64181f1d3e60, 2020-03-31 (annotated)
- Committer:
- Lightvalve
- Date:
- Tue Mar 31 05:12:37 2020 +0000
- Revision:
- 58:64181f1d3e60
- Parent:
- 57:f308b1656d9c
- Child:
- 59:bc8c8270f0ab
200331
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 | 48:e7bcfc244d40 | 69 | extern int CID_RX_REF_VALVE_POS; |
Lightvalve | 44: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 | 46:fdcb8bd86fd6 | 101 | MODE_VALVE_POSITION_CONTROL, //1 |
Lightvalve | 46:fdcb8bd86fd6 | 102 | MODE_JOINT_CONTROL, //2 |
Lightvalve | 49:3c630b5eba9f | 103 | |
Lightvalve | 46: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 | 55: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 | 55: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 | 55: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 | 49:3c630b5eba9f | 174 | // TMR2 init |
Lightvalve | 54:6f50d9d3bfee | 175 | // Init_TMR2(); |
Lightvalve | 54:6f50d9d3bfee | 176 | // TIM2->CR1 ^= TIM_CR1_UDIS; |
Lightvalve | 54: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 | 55:f4819de54e7a | 186 | //NVIC_SetPriority(TIM2_IRQn, 3); |
Lightvalve | 52: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 | 56:2eade98630e2 | 197 | if (SENSING_MODE == 0) { |
Lightvalve | 56:2eade98630e2 | 198 | dac_1 = TORQUE_VREF / 3.3f; |
Lightvalve | 56:2eade98630e2 | 199 | dac_2 = 0.0f; |
Lightvalve | 56:2eade98630e2 | 200 | } else if (SENSING_MODE == 1) { |
Lightvalve | 56:2eade98630e2 | 201 | dac_1 = PRES_A_VREF / 3.3f; |
Lightvalve | 56:2eade98630e2 | 202 | dac_2 = PRES_B_VREF / 3.3f; |
Lightvalve | 56: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 | 56:2eade98630e2 | 212 | |
jobuuu | 6:df07d3491e3a | 213 | /************************************ |
jobuuu | 1:e04e563be5ce | 214 | *** Program is operating! |
jobuuu | 6:df07d3491e3a | 215 | *************************************/ |
GiJeongKim | 0:51c43836c1d7 | 216 | while(1) { |
Lightvalve | 55: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 | 55:f4819de54e7a | 221 | // if(LED==1) { |
Lightvalve | 55:f4819de54e7a | 222 | // LED=0; |
Lightvalve | 55:f4819de54e7a | 223 | // } else |
Lightvalve | 55: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 | 47: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 | 49:3c630b5eba9f | 239 | if(JOINT_VEL[i+1] == JOINT_VEL[i]) { |
Lightvalve | 55:f4819de54e7a | 240 | Ref_Valve_Pos_FF = (float) VALVE_CENTER; |
Lightvalve | 49:3c630b5eba9f | 241 | } else { |
Lightvalve | 55: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 | 49:3c630b5eba9f | 245 | if(JOINT_VEL[i+1] == JOINT_VEL[i-1]) { |
Lightvalve | 55:f4819de54e7a | 246 | Ref_Valve_Pos_FF = (float) VALVE_CENTER; |
Lightvalve | 49:3c630b5eba9f | 247 | } else { |
Lightvalve | 55: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 | 55: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 | 49: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 | 47: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 | 47: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 | 57: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 | 55:f4819de54e7a | 338 | |
Lightvalve | 55:f4819de54e7a | 339 | |
Lightvalve | 55:f4819de54e7a | 340 | |
Lightvalve | 55:f4819de54e7a | 341 | |
jobuuu | 2:a1c0a37df760 | 342 | /******************************************************************************* |
jobuuu | 2:a1c0a37df760 | 343 | TIMER INTERRUPT |
jobuuu | 2:a1c0a37df760 | 344 | *******************************************************************************/ |
jobuuu | 2:a1c0a37df760 | 345 | |
Lightvalve | 50:b46bed7fec80 | 346 | float FREQ_TMR4 = (float)FREQ_20k; |
Lightvalve | 50:b46bed7fec80 | 347 | float DT_TMR4 = (float)DT_20k; |
Lightvalve | 55:f4819de54e7a | 348 | long CNT_TMR4 = 0; |
Lightvalve | 55: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 | 55:f4819de54e7a | 358 | //Encoder |
Lightvalve | 55:f4819de54e7a | 359 | if (CNT_TMR4 % (int) ((int) FREQ_TMR4/TMR4_FREQ_10k) == 0) { |
Lightvalve | 55:f4819de54e7a | 360 | ENC_UPDATE(); |
Lightvalve | 55:f4819de54e7a | 361 | } |
Lightvalve | 56:2eade98630e2 | 362 | |
Lightvalve | 56:2eade98630e2 | 363 | ADC1->CR2 |= 0x40000000; |
Lightvalve | 56:2eade98630e2 | 364 | if (SENSING_MODE == 0) { |
Lightvalve | 56:2eade98630e2 | 365 | // Torque Sensing (0~210)bar ============================================= |
Lightvalve | 56:2eade98630e2 | 366 | float pres_A_new = (((float) ADC1->DR) - 2047.5f); |
Lightvalve | 56:2eade98630e2 | 367 | double alpha_update_ft = 1.0f / (1.0f + FREQ_TMR4 / (2.0f * 3.14f * 100.0f)); // f_cutoff : 200Hz |
Lightvalve | 56:2eade98630e2 | 368 | pres_A.sen = (1.0f - alpha_update_ft) * pres_A.sen + alpha_update_ft * pres_A_new; |
Lightvalve | 56:2eade98630e2 | 369 | torq.sen = pres_A.sen / TORQUE_SENSOR_PULSE_PER_TORQUE; |
Lightvalve | 17:1865016ca2e7 | 370 | |
Lightvalve | 56:2eade98630e2 | 371 | } else if (SENSING_MODE == 1) { |
Lightvalve | 56:2eade98630e2 | 372 | // Pressure Sensing (0~210)bar ============================================= |
Lightvalve | 56:2eade98630e2 | 373 | float pres_A_new = (((float)ADC1->DR) - PRES_A_NULL); |
Lightvalve | 56:2eade98630e2 | 374 | float pres_B_new = (((float)ADC2->DR) - PRES_B_NULL); |
Lightvalve | 56:2eade98630e2 | 375 | double alpha_update_pres = 1.0f / (1.0f + FREQ_TMR4 / (2.0f * 3.14f * 200.0f)); // f_cutoff : 500Hz |
Lightvalve | 56:2eade98630e2 | 376 | pres_A.sen = (1.0f - alpha_update_pres) * pres_A.sen + alpha_update_pres * pres_A_new; |
Lightvalve | 56:2eade98630e2 | 377 | pres_B.sen = (1.0f - alpha_update_pres) * pres_B.sen + alpha_update_pres * pres_B_new; |
Lightvalve | 56:2eade98630e2 | 378 | CUR_PRES_A_BAR = pres_A.sen / PRES_SENSOR_A_PULSE_PER_BAR; |
Lightvalve | 56:2eade98630e2 | 379 | CUR_PRES_B_BAR = pres_B.sen / PRES_SENSOR_B_PULSE_PER_BAR; |
Lightvalve | 49:3c630b5eba9f | 380 | |
Lightvalve | 56:2eade98630e2 | 381 | if ((OPERATING_MODE & 0x01) == 0) { // Rotary Actuator |
Lightvalve | 56: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 | 56:2eade98630e2 | 383 | } else if ((OPERATING_MODE & 0x01) == 1) { // Linear Actuator |
Lightvalve | 56: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 | 56:2eade98630e2 | 385 | } |
Lightvalve | 56:2eade98630e2 | 386 | } |
Lightvalve | 56:2eade98630e2 | 387 | |
Lightvalve | 56:2eade98630e2 | 388 | // //Pressure sensor A |
Lightvalve | 56:2eade98630e2 | 389 | // ADC1->CR2 |= 0x40000000; // adc _ 12bit |
Lightvalve | 56:2eade98630e2 | 390 | // //while((ADC1->SR & 0b10)); |
Lightvalve | 56:2eade98630e2 | 391 | // float alpha_update_pres_A = 1.0f/(1.0f + FREQ_TMR4/(2.0f*3.14f*100.0f)); |
Lightvalve | 56:2eade98630e2 | 392 | // float pres_A_new = ((float)ADC1->DR); |
Lightvalve | 56:2eade98630e2 | 393 | // pres_A.sen = pres_A.sen*(1.0f-alpha_update_pres_A)+pres_A_new*(alpha_update_pres_A); |
Lightvalve | 56: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 | 56:2eade98630e2 | 395 | // |
Lightvalve | 56:2eade98630e2 | 396 | // |
Lightvalve | 56:2eade98630e2 | 397 | // //Pressure sensor B |
Lightvalve | 56:2eade98630e2 | 398 | // float alpha_update_pres_B = 1.0f/(1.0f + FREQ_TMR4/(2.0f*3.14f*100.0f)); |
Lightvalve | 56:2eade98630e2 | 399 | // float pres_B_new = ((float)ADC2->DR); |
Lightvalve | 56:2eade98630e2 | 400 | // pres_B.sen = pres_B.sen*(1.0f-alpha_update_pres_B)+pres_B_new*(alpha_update_pres_B); |
Lightvalve | 56: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 | 50: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 | 56:2eade98630e2 | 412 | |
Lightvalve | 55: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 | 52:647072f5307a | 420 | float FREQ_TMR3 = (float)FREQ_5k; |
Lightvalve | 47:889798ff9329 | 421 | float DT_TMR3 = (float)DT_5k; |
Lightvalve | 44:35fa6884d0c6 | 422 | int cnt_trans = 0; |
Lightvalve | 47:889798ff9329 | 423 | double VALVE_POS_RAW_FORCE_FB_LOGGING = 0.0f; |
Lightvalve | 55:f4819de54e7a | 424 | int can_rest =0; |
Lightvalve | 47: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 | 55:f4819de54e7a | 429 | |
Lightvalve | 55:f4819de54e7a | 430 | if (((OPERATING_MODE&0b110)>>1) == 0) { |
Lightvalve | 55:f4819de54e7a | 431 | K_v = 0.4f; // Moog (LPM >> mA) , 100bar |
Lightvalve | 55:f4819de54e7a | 432 | mV_PER_mA = 500.0f; // 5000mV/10mA |
Lightvalve | 55:f4819de54e7a | 433 | mV_PER_pulse = 0.5f; // 5000mV/10000pulse |
Lightvalve | 55:f4819de54e7a | 434 | mA_PER_pulse = 0.001f; // 10mA/10000pulse |
Lightvalve | 55:f4819de54e7a | 435 | } else if (((OPERATING_MODE&0b110)>>1) == 1) { |
Lightvalve | 55:f4819de54e7a | 436 | K_v = 0.5f; // KNR (LPM >> mA) , 100bar |
Lightvalve | 55:f4819de54e7a | 437 | mV_PER_mA = 166.6666f; // 5000mV/30mA |
Lightvalve | 55:f4819de54e7a | 438 | mV_PER_pulse = 0.5f; // 5000mV/10000pulse |
Lightvalve | 55:f4819de54e7a | 439 | mA_PER_pulse = 0.003f; // 30mA/10000pulse |
Lightvalve | 55:f4819de54e7a | 440 | } |
Lightvalve | 49:3c630b5eba9f | 441 | |
Lightvalve | 49:3c630b5eba9f | 442 | if(MODE_POS_FT_TRANS == 1) { |
Lightvalve | 47:889798ff9329 | 443 | alpha_trans = (float)(1.0f - cos(3.141592f * (float)cnt_trans * DT_TMR3 /3.0f))/2.0f; |
Lightvalve | 44:35fa6884d0c6 | 444 | cnt_trans++; |
Lightvalve | 45:2694daea349b | 445 | torq.err_sum = 0; |
Lightvalve | 47:889798ff9329 | 446 | if((float)cnt_trans * DT_TMR3 > 3.0f) |
Lightvalve | 44:35fa6884d0c6 | 447 | MODE_POS_FT_TRANS = 2; |
Lightvalve | 49:3c630b5eba9f | 448 | } else if(MODE_POS_FT_TRANS == 3) { |
Lightvalve | 47:889798ff9329 | 449 | alpha_trans = (float)(1.0f + cos(3.141592f * (float)cnt_trans * DT_TMR3 /3.0f))/2.0f; |
Lightvalve | 44:35fa6884d0c6 | 450 | cnt_trans++; |
Lightvalve | 45:2694daea349b | 451 | torq.err_sum = 0; |
Lightvalve | 47:889798ff9329 | 452 | if((float) cnt_trans * DT_TMR3 > 3.0f ) |
Lightvalve | 44:35fa6884d0c6 | 453 | MODE_POS_FT_TRANS = 0; |
Lightvalve | 49:3c630b5eba9f | 454 | } else if(MODE_POS_FT_TRANS == 2) { |
Lightvalve | 56:2eade98630e2 | 455 | alpha_trans = 1.0f; |
Lightvalve | 44:35fa6884d0c6 | 456 | cnt_trans = 0; |
Lightvalve | 49:3c630b5eba9f | 457 | } else { |
Lightvalve | 56:2eade98630e2 | 458 | alpha_trans = 0.0f; |
Lightvalve | 44:35fa6884d0c6 | 459 | cnt_trans = 0; |
Lightvalve | 44:35fa6884d0c6 | 460 | } |
Lightvalve | 44:35fa6884d0c6 | 461 | |
Lightvalve | 49:3c630b5eba9f | 462 | |
Lightvalve | 55:f4819de54e7a | 463 | int UTILITY_MODE = 0; |
Lightvalve | 55:f4819de54e7a | 464 | int CONTROL_MODE = 0; |
Lightvalve | 56:2eade98630e2 | 465 | |
Lightvalve | 55:f4819de54e7a | 466 | if (CONTROL_UTILITY_MODE >= 20 || CONTROL_UTILITY_MODE == 0) { |
Lightvalve | 55:f4819de54e7a | 467 | UTILITY_MODE = CONTROL_UTILITY_MODE; |
Lightvalve | 55:f4819de54e7a | 468 | CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 55:f4819de54e7a | 469 | } else { |
Lightvalve | 55:f4819de54e7a | 470 | CONTROL_MODE = CONTROL_UTILITY_MODE; |
Lightvalve | 55:f4819de54e7a | 471 | UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 55:f4819de54e7a | 472 | } |
Lightvalve | 54:6f50d9d3bfee | 473 | |
Lightvalve | 54:6f50d9d3bfee | 474 | |
Lightvalve | 54:6f50d9d3bfee | 475 | |
Lightvalve | 55:f4819de54e7a | 476 | // UTILITY MODE ------------------------------------------------------------ |
Lightvalve | 54:6f50d9d3bfee | 477 | |
Lightvalve | 55:f4819de54e7a | 478 | switch (UTILITY_MODE) { |
Lightvalve | 55: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 | 57: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 | 57:f308b1656d9c | 495 | if (TORQUE_VREF < 0.0f) TORQUE_VREF = 0.0f; |
Lightvalve | 49: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 | 56: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 | 49:3c630b5eba9f | 515 | // case MODE_VALVE_NULLING_AND_DEADZONE_SETTING: { |
Lightvalve | 49:3c630b5eba9f | 516 | // if (TMR3_COUNT_DEADZONE == 0) { |
Lightvalve | 49:3c630b5eba9f | 517 | // if (pos_plus_end == pos_minus_end) need_enc_init = true; |
Lightvalve | 49:3c630b5eba9f | 518 | // else temp_time = 0; |
Lightvalve | 49:3c630b5eba9f | 519 | // } |
Lightvalve | 49:3c630b5eba9f | 520 | // if (need_enc_init) { |
Lightvalve | 49:3c630b5eba9f | 521 | // if (TMR3_COUNT_DEADZONE < (int) (0.5f * (float) TMR_FREQ_5k)) { |
Lightvalve | 49:3c630b5eba9f | 522 | // V_out = VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 49:3c630b5eba9f | 523 | // pos_plus_end = pos.sen; |
Lightvalve | 49:3c630b5eba9f | 524 | // } else if (TMR3_COUNT_DEADZONE < TMR_FREQ_5k) { |
Lightvalve | 49:3c630b5eba9f | 525 | // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 49:3c630b5eba9f | 526 | // pos_minus_end = pos.sen; |
Lightvalve | 49:3c630b5eba9f | 527 | // } else if (TMR3_COUNT_DEADZONE == TMR_FREQ_5k) need_enc_init = false; |
Lightvalve | 49:3c630b5eba9f | 528 | // temp_time = TMR_FREQ_5k; |
Lightvalve | 49:3c630b5eba9f | 529 | // } |
Lightvalve | 49:3c630b5eba9f | 530 | // |
Lightvalve | 49:3c630b5eba9f | 531 | // if (temp_time <= TMR3_COUNT_DEADZONE && TMR3_COUNT_DEADZONE < (temp_time + TMR_FREQ_5k)) { |
Lightvalve | 49: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 | 49:3c630b5eba9f | 533 | // VALVE_CENTER = VALVE_DEADZONE_PLUS = VALVE_DEADZONE_MINUS = 0; |
Lightvalve | 49:3c630b5eba9f | 534 | // |
Lightvalve | 49:3c630b5eba9f | 535 | // } else if (temp_time <= TMR3_COUNT_DEADZONE && TMR3_COUNT_DEADZONE < (temp_time + (int) (1.9f * (float) TMR_FREQ_5k))) { |
Lightvalve | 49:3c630b5eba9f | 536 | // V_out = 0; |
Lightvalve | 49:3c630b5eba9f | 537 | // CUR_VELOCITY_sum += CUR_VELOCITY; |
Lightvalve | 49:3c630b5eba9f | 538 | // } else if (TMR3_COUNT_DEADZONE == (temp_time + 2 * TMR_FREQ_5k)) { |
Lightvalve | 49:3c630b5eba9f | 539 | // if (CUR_VELOCITY_sum == 0) DZ_dir = 1; |
Lightvalve | 49:3c630b5eba9f | 540 | // else if (CUR_VELOCITY_sum > 0) DZ_dir = 1; |
Lightvalve | 49:3c630b5eba9f | 541 | // else if (CUR_VELOCITY_sum < 0) DZ_dir = -1; |
Lightvalve | 49:3c630b5eba9f | 542 | // else DZ_temp_cnt2 = DZ_end; |
Lightvalve | 49:3c630b5eba9f | 543 | // CUR_VELOCITY_sum = 0; |
Lightvalve | 49:3c630b5eba9f | 544 | // } else if (TMR3_COUNT_DEADZONE > (temp_time + 2 * TMR_FREQ_5k)) { |
Lightvalve | 49:3c630b5eba9f | 545 | // if (TMR3_COUNT_DEADZONE > (temp_time + 10 * TMR_FREQ_5k)) DZ_temp_cnt2 = DZ_end; |
Lightvalve | 49:3c630b5eba9f | 546 | // |
Lightvalve | 49:3c630b5eba9f | 547 | // // Position of Dead Zone |
Lightvalve | 49:3c630b5eba9f | 548 | // // (CUR_VELOCITY < 0) (CUR_VELOCITY == 0) (CUR_VELOCITY > 0) |
Lightvalve | 49:3c630b5eba9f | 549 | // // | / | / |/ |
Lightvalve | 49:3c630b5eba9f | 550 | // // | ______/ ___|___/ ______/| |
Lightvalve | 49:3c630b5eba9f | 551 | // // |/ / | / | |
Lightvalve | 49:3c630b5eba9f | 552 | // // /| / | / | |
Lightvalve | 49:3c630b5eba9f | 553 | // // 0V 0V 0V |
Lightvalve | 49:3c630b5eba9f | 554 | // |
Lightvalve | 49:3c630b5eba9f | 555 | // if (DZ_temp_cnt2 < DZ_end) { |
Lightvalve | 49:3c630b5eba9f | 556 | // if (TMR3_COUNT_DEADZONE % 20 != 0) { |
Lightvalve | 49:3c630b5eba9f | 557 | // CUR_VELOCITY_sum += CUR_VELOCITY; |
Lightvalve | 49:3c630b5eba9f | 558 | // } else { |
Lightvalve | 49:3c630b5eba9f | 559 | // V_out -= DZ_dir; |
Lightvalve | 49:3c630b5eba9f | 560 | // if (CUR_VELOCITY_sum * DZ_dir < 0) DZ_temp_cnt++; |
Lightvalve | 49:3c630b5eba9f | 561 | // CUR_VELOCITY_sum = 0; |
Lightvalve | 49:3c630b5eba9f | 562 | // } |
Lightvalve | 49:3c630b5eba9f | 563 | // if (DZ_temp_cnt == 5) { |
Lightvalve | 49:3c630b5eba9f | 564 | // if (DZ_dir >= 0) VALVE_DEADZONE_MINUS = (int16_t) V_out; |
Lightvalve | 49:3c630b5eba9f | 565 | // else VALVE_DEADZONE_PLUS = (int16_t) V_out; |
Lightvalve | 49:3c630b5eba9f | 566 | // DZ_dir = -DZ_dir; |
Lightvalve | 49:3c630b5eba9f | 567 | // DZ_temp_cnt = 0; |
Lightvalve | 49:3c630b5eba9f | 568 | // DZ_temp_cnt2++; |
Lightvalve | 49:3c630b5eba9f | 569 | // } |
Lightvalve | 49:3c630b5eba9f | 570 | // } else { |
Lightvalve | 49:3c630b5eba9f | 571 | // TMR3_COUNT_DEADZONE = -1; |
Lightvalve | 49:3c630b5eba9f | 572 | // VALVE_CENTER = VALVE_DEADZONE_PLUS / 2 + VALVE_DEADZONE_MINUS / 2; |
Lightvalve | 49:3c630b5eba9f | 573 | // if (VALVE_DEADZONE_PLUS < VALVE_DEADZONE_MINUS) { |
Lightvalve | 49:3c630b5eba9f | 574 | // VALVE_DEADZONE_PLUS = VALVE_CENTER; |
Lightvalve | 49:3c630b5eba9f | 575 | // VALVE_DEADZONE_MINUS = VALVE_CENTER; |
Lightvalve | 49:3c630b5eba9f | 576 | // } |
Lightvalve | 49:3c630b5eba9f | 577 | // V_out = 0; |
Lightvalve | 49:3c630b5eba9f | 578 | // |
Lightvalve | 49:3c630b5eba9f | 579 | // ROM_RESET_DATA(); |
Lightvalve | 49:3c630b5eba9f | 580 | // |
Lightvalve | 49:3c630b5eba9f | 581 | // //spi_eeprom_write(RID_VALVE_DEADZONE_PLUS, VALVE_DEADZONE_PLUS); |
Lightvalve | 49:3c630b5eba9f | 582 | // //spi_eeprom_write(RID_VALVE_DEADZONE_MINUS, VALVE_DEADZONE_MINUS); |
Lightvalve | 49:3c630b5eba9f | 583 | // |
Lightvalve | 49:3c630b5eba9f | 584 | // CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 49:3c630b5eba9f | 585 | // DZ_temp_cnt2 = 0; |
Lightvalve | 49:3c630b5eba9f | 586 | // } |
Lightvalve | 49:3c630b5eba9f | 587 | // } |
Lightvalve | 49:3c630b5eba9f | 588 | // TMR3_COUNT_DEADZONE++; |
Lightvalve | 49:3c630b5eba9f | 589 | // break; |
Lightvalve | 49: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 | 57:f308b1656d9c | 597 | pos.ref = pos.sen; |
Lightvalve | 57: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 | 55: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 | 57:f308b1656d9c | 617 | if (HOMEPOS_OFFSET > 0) pos.ref = pos.ref + 2.0f; |
Lightvalve | 57:f308b1656d9c | 618 | else pos.ref = pos.ref - 2.0f; |
Lightvalve | 57:f308b1656d9c | 619 | |
Lightvalve | 57:f308b1656d9c | 620 | // pos.err = pos.ref_home_pos - pos.sen; |
Lightvalve | 57:f308b1656d9c | 621 | // float VALVE_POS_RAW_POS_FB = 0.0f; |
Lightvalve | 57:f308b1656d9c | 622 | // VALVE_POS_RAW_POS_FB = (float) P_GAIN_JOINT_POSITION * pos.err/(float) ENC_PULSE_PER_POSITION * 0.01f; |
Lightvalve | 57:f308b1656d9c | 623 | // valve_pos.ref = VALVE_POS_RAW_POS_FB + (float) VALVE_CENTER; |
Lightvalve | 57:f308b1656d9c | 624 | // VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 57:f308b1656d9c | 625 | |
Lightvalve | 57:f308b1656d9c | 626 | CONTROL_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 57:f308b1656d9c | 627 | alpha_trans = 0.0f; |
Lightvalve | 57: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 | 57: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 | 57:f308b1656d9c | 645 | vel.ref = 0.0f; |
Lightvalve | 19:23b7c1ad8683 | 646 | |
Lightvalve | 29:69f3f5445d6d | 647 | // input for position control |
Lightvalve | 57:f308b1656d9c | 648 | |
Lightvalve | 57:f308b1656d9c | 649 | CONTROL_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 57:f308b1656d9c | 650 | alpha_trans = 0.0f; |
Lightvalve | 57:f308b1656d9c | 651 | // pos.err = pos.ref - (float)pos.sen; |
Lightvalve | 57:f308b1656d9c | 652 | // float VALVE_POS_RAW_POS_FB = 0.0f; |
Lightvalve | 57:f308b1656d9c | 653 | // VALVE_POS_RAW_POS_FB = (float) P_GAIN_JOINT_POSITION * 0.01f * pos.err/(float) ENC_PULSE_PER_POSITION; |
Lightvalve | 57:f308b1656d9c | 654 | // valve_pos.ref = VALVE_POS_RAW_POS_FB + (float) VALVE_CENTER; |
Lightvalve | 57: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 | 55: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 | 49:3c630b5eba9f | 673 | // case MODE_VALVE_GAIN_SETTING: { |
Lightvalve | 49:3c630b5eba9f | 674 | // if (TMR3_COUNT_FLOWRATE == 0) { |
Lightvalve | 49:3c630b5eba9f | 675 | // if (pos_plus_end == pos_minus_end) need_enc_init = true; |
Lightvalve | 49:3c630b5eba9f | 676 | // else { |
Lightvalve | 49:3c630b5eba9f | 677 | // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 49:3c630b5eba9f | 678 | // temp_time = (int) (0.5f * (float) TMR_FREQ_5k); |
Lightvalve | 49:3c630b5eba9f | 679 | // } |
Lightvalve | 49:3c630b5eba9f | 680 | // } |
Lightvalve | 49:3c630b5eba9f | 681 | // if (need_enc_init) { |
Lightvalve | 49:3c630b5eba9f | 682 | // if (TMR3_COUNT_FLOWRATE < (int) (0.5f * (float) TMR_FREQ_5k)) { |
Lightvalve | 49:3c630b5eba9f | 683 | // V_out = VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 49:3c630b5eba9f | 684 | // pos_plus_end = pos.sen; |
Lightvalve | 49:3c630b5eba9f | 685 | // } else if (TMR3_COUNT_FLOWRATE < TMR_FREQ_5k) { |
Lightvalve | 49:3c630b5eba9f | 686 | // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 49:3c630b5eba9f | 687 | // pos_minus_end = pos.sen; |
Lightvalve | 49:3c630b5eba9f | 688 | // } else if (TMR3_COUNT_FLOWRATE == TMR_FREQ_5k) { |
Lightvalve | 49:3c630b5eba9f | 689 | // need_enc_init = false; |
Lightvalve | 49:3c630b5eba9f | 690 | // check_vel_pos_init = (int) (0.9f * (float) (pos_plus_end - pos_minus_end)); |
Lightvalve | 49:3c630b5eba9f | 691 | // check_vel_pos_fin = (int) (0.95f * (float) (pos_plus_end - pos_minus_end)); |
Lightvalve | 49:3c630b5eba9f | 692 | // check_vel_pos_interv = check_vel_pos_fin - check_vel_pos_init; |
Lightvalve | 49:3c630b5eba9f | 693 | // } |
Lightvalve | 49:3c630b5eba9f | 694 | // temp_time = TMR_FREQ_5k; |
Lightvalve | 49:3c630b5eba9f | 695 | // } |
Lightvalve | 49:3c630b5eba9f | 696 | // TMR3_COUNT_FLOWRATE++; |
Lightvalve | 49:3c630b5eba9f | 697 | // if (TMR3_COUNT_FLOWRATE > temp_time) { |
Lightvalve | 49:3c630b5eba9f | 698 | // if (flag_flowrate % 2 == 0) { // (+) |
Lightvalve | 49:3c630b5eba9f | 699 | // VALVE_VOLTAGE = 1000.0f * (float) (flag_flowrate / 2 + 1); |
Lightvalve | 49:3c630b5eba9f | 700 | // V_out = VALVE_VOLTAGE; |
Lightvalve | 49:3c630b5eba9f | 701 | // if (pos.sen > (pos_minus_end + check_vel_pos_init) && pos.sen < (pos_minus_end + check_vel_pos_fin)) { |
Lightvalve | 49:3c630b5eba9f | 702 | // fl_temp_cnt++; |
Lightvalve | 49:3c630b5eba9f | 703 | // } else if (pos.sen >= (pos_minus_end + check_vel_pos_fin) && CUR_VELOCITY == 0) { |
Lightvalve | 49: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 | 49: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 | 49:3c630b5eba9f | 706 | // fl_temp_cnt2++; |
Lightvalve | 49:3c630b5eba9f | 707 | // } |
Lightvalve | 49:3c630b5eba9f | 708 | // } else if (flag_flowrate % 2 == 1) { // (-) |
Lightvalve | 49:3c630b5eba9f | 709 | // VALVE_VOLTAGE = -1. * (float) (flag_flowrate / 2 + 1); |
Lightvalve | 49:3c630b5eba9f | 710 | // V_out = VALVE_VOLTAGE; |
Lightvalve | 49:3c630b5eba9f | 711 | // if (pos.sen < (pos_plus_end - check_vel_pos_init) && pos.sen > (pos_plus_end - check_vel_pos_fin)) { |
Lightvalve | 49:3c630b5eba9f | 712 | // fl_temp_cnt++; |
Lightvalve | 49:3c630b5eba9f | 713 | // } else if (pos.sen <= (pos_plus_end - check_vel_pos_fin) && CUR_VELOCITY == 0) { |
Lightvalve | 49: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 | 49: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 | 49:3c630b5eba9f | 716 | // fl_temp_cnt2++; |
Lightvalve | 49:3c630b5eba9f | 717 | // } |
Lightvalve | 49:3c630b5eba9f | 718 | // } |
Lightvalve | 49:3c630b5eba9f | 719 | // if (fl_temp_cnt2 == 100) { |
Lightvalve | 49:3c630b5eba9f | 720 | // |
Lightvalve | 49:3c630b5eba9f | 721 | // ROM_RESET_DATA(); |
Lightvalve | 49:3c630b5eba9f | 722 | // |
Lightvalve | 49:3c630b5eba9f | 723 | // //spi_eeprom_write(RID_VALVE_GAIN_PLUS_1 + flag_flowrate, (int16_t) (VALVE_GAIN_LPM_PER_V[flag_flowrate] * 100.0f)); |
Lightvalve | 49:3c630b5eba9f | 724 | // cur_vel_sum = 0; |
Lightvalve | 49:3c630b5eba9f | 725 | // fl_temp_cnt = 0; |
Lightvalve | 49:3c630b5eba9f | 726 | // fl_temp_cnt2 = 0; |
Lightvalve | 49:3c630b5eba9f | 727 | // flag_flowrate++; |
Lightvalve | 49:3c630b5eba9f | 728 | // } |
Lightvalve | 49:3c630b5eba9f | 729 | // if (flag_flowrate == 10) { |
Lightvalve | 49:3c630b5eba9f | 730 | // V_out = 0; |
Lightvalve | 49:3c630b5eba9f | 731 | // flag_flowrate = 0; |
Lightvalve | 49:3c630b5eba9f | 732 | // TMR3_COUNT_FLOWRATE = 0; |
Lightvalve | 49:3c630b5eba9f | 733 | // valve_gain_repeat_cnt++; |
Lightvalve | 49:3c630b5eba9f | 734 | // if (valve_gain_repeat_cnt >= 1) { |
Lightvalve | 49:3c630b5eba9f | 735 | // CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 49:3c630b5eba9f | 736 | // valve_gain_repeat_cnt = 0; |
Lightvalve | 49:3c630b5eba9f | 737 | // } |
Lightvalve | 49:3c630b5eba9f | 738 | // |
Lightvalve | 49:3c630b5eba9f | 739 | // } |
Lightvalve | 49:3c630b5eba9f | 740 | // break; |
Lightvalve | 49:3c630b5eba9f | 741 | // } |
Lightvalve | 49:3c630b5eba9f | 742 | // |
Lightvalve | 49: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 | 56:2eade98630e2 | 757 | PRES_A_VREF = PRES_A_VREF + VREF_NullingGain * CUR_PRES_A_mean; |
Lightvalve | 56: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 | 55: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 | 49:3c630b5eba9f | 786 | // case MODE_PRESSURE_SENSOR_CALIB: { |
Lightvalve | 49:3c630b5eba9f | 787 | // if (TMR3_COUNT_PRES_CALIB < 2 * TMR_FREQ_5k) { |
Lightvalve | 49:3c630b5eba9f | 788 | // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 49:3c630b5eba9f | 789 | // if (TMR3_COUNT_PRES_CALIB >= TMR_FREQ_5k) { |
Lightvalve | 49:3c630b5eba9f | 790 | // CUR_PRES_A_sum += CUR_PRES_A; |
Lightvalve | 49:3c630b5eba9f | 791 | // } |
Lightvalve | 49:3c630b5eba9f | 792 | // } else if (TMR3_COUNT_PRES_CALIB < 4 * TMR_FREQ_5k) { |
Lightvalve | 49:3c630b5eba9f | 793 | // V_out = VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 49:3c630b5eba9f | 794 | // if (TMR3_COUNT_PRES_CALIB >= 3 * TMR_FREQ_5k) { |
Lightvalve | 49:3c630b5eba9f | 795 | // CUR_PRES_B_sum += CUR_PRES_B; |
Lightvalve | 49:3c630b5eba9f | 796 | // } |
Lightvalve | 49:3c630b5eba9f | 797 | // } else { |
Lightvalve | 49:3c630b5eba9f | 798 | // CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 49:3c630b5eba9f | 799 | // TMR3_COUNT_PRES_CALIB = 0; |
Lightvalve | 49:3c630b5eba9f | 800 | // V_out = 0; |
Lightvalve | 49:3c630b5eba9f | 801 | // PRES_SENSOR_A_PULSE_PER_BAR = CUR_PRES_A_sum / ((float) TMR_FREQ_5k - 1.0f) - PRES_A_NULL; |
Lightvalve | 49:3c630b5eba9f | 802 | // PRES_SENSOR_A_PULSE_PER_BAR = PRES_SENSOR_A_PULSE_PER_BAR / ((float) PRES_SUPPLY - 1.0f); |
Lightvalve | 49:3c630b5eba9f | 803 | // PRES_SENSOR_B_PULSE_PER_BAR = CUR_PRES_B_sum / ((float) TMR_FREQ_5k - 1.0f) - PRES_B_NULL; |
Lightvalve | 49:3c630b5eba9f | 804 | // PRES_SENSOR_B_PULSE_PER_BAR = PRES_SENSOR_B_PULSE_PER_BAR / ((float) PRES_SUPPLY - 1.0f); |
Lightvalve | 49:3c630b5eba9f | 805 | // CUR_PRES_A_sum = 0; |
Lightvalve | 49:3c630b5eba9f | 806 | // CUR_PRES_B_sum = 0; |
Lightvalve | 49:3c630b5eba9f | 807 | // CUR_PRES_A_mean = 0; |
Lightvalve | 49:3c630b5eba9f | 808 | // CUR_PRES_B_mean = 0; |
Lightvalve | 49:3c630b5eba9f | 809 | // |
Lightvalve | 49:3c630b5eba9f | 810 | // ROM_RESET_DATA(); |
Lightvalve | 49:3c630b5eba9f | 811 | // |
Lightvalve | 49:3c630b5eba9f | 812 | // //spi_eeprom_write(RID_PRES_SENSOR_A_PULSE_PER_BAR, (int16_t) (PRES_SENSOR_A_PULSE_PER_BAR * 100.0f)); |
Lightvalve | 49:3c630b5eba9f | 813 | // //spi_eeprom_write(RID_PRES_SENSOR_B_PULSE_PER_BAR, (int16_t) (PRES_SENSOR_B_PULSE_PER_BAR * 100.0f)); |
Lightvalve | 49:3c630b5eba9f | 814 | // } |
Lightvalve | 49:3c630b5eba9f | 815 | // TMR3_COUNT_PRES_CALIB++; |
Lightvalve | 49:3c630b5eba9f | 816 | // break; |
Lightvalve | 49:3c630b5eba9f | 817 | // } |
Lightvalve | 19:23b7c1ad8683 | 818 | |
Lightvalve | 49:3c630b5eba9f | 819 | // case MODE_ROTARY_FRICTION_TUNING: { |
Lightvalve | 49: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 | 49:3c630b5eba9f | 821 | // V_out = PWM_out * sin(2 * 3.14159f * freq_fric_tune * TMR3_COUNT_ROTARY_FRIC_TUNE * 0.0001f); |
Lightvalve | 49:3c630b5eba9f | 822 | // if (V_out > 0) V_out = VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 49:3c630b5eba9f | 823 | // else V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 49:3c630b5eba9f | 824 | // TMR3_COUNT_ROTARY_FRIC_TUNE++; |
Lightvalve | 49:3c630b5eba9f | 825 | // if (TMR3_COUNT_ROTARY_FRIC_TUNE > TUNING_TIME * TMR_FREQ_5k) { |
Lightvalve | 49:3c630b5eba9f | 826 | // TMR3_COUNT_ROTARY_FRIC_TUNE = 0; |
Lightvalve | 49:3c630b5eba9f | 827 | // V_out = 0.0f; |
Lightvalve | 49:3c630b5eba9f | 828 | // CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 49:3c630b5eba9f | 829 | // } |
Lightvalve | 49:3c630b5eba9f | 830 | // break; |
Lightvalve | 49:3c630b5eba9f | 831 | // } |
Lightvalve | 14:8e7590227d22 | 832 | |
Lightvalve | 14:8e7590227d22 | 833 | case MODE_DDV_POS_VS_PWM_ID: { |
Lightvalve | 57: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 | 58:64181f1d3e60 | 838 | Vout.ref = 3000.0f * sin(2.0f*3.14f*VALVE_ID_timer/TMR_FREQ_5k * 100.0f); |
Lightvalve | 14:8e7590227d22 | 839 | } else if(VALVE_ID_timer < TMR_FREQ_5k*2) { |
Lightvalve | 57: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 | 57:f308b1656d9c | 848 | Vout.ref = 0.0f; |
Lightvalve | 57: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 | 57:f308b1656d9c | 868 | ROM_RESET_DATA(); |
Lightvalve | 13:747daba9cf59 | 869 | ID_index = 0; |
Lightvalve | 55: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 | 57: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 | 57: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 | 57: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 | 57: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 | 57: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 | 57: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 | 57: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 | 57: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 | 58:64181f1d3e60 | 911 | |
Lightvalve | 30:8d561f16383b | 912 | } else if(VALVE_DZ_timer == (int) (6.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 913 | valve_pos.ref = DDV_POS_AVG; |
Lightvalve | 14:8e7590227d22 | 914 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 14:8e7590227d22 | 915 | FINAL_POS = pos.sen; |
Lightvalve | 14:8e7590227d22 | 916 | |
Lightvalve | 58:64181f1d3e60 | 917 | if((FINAL_POS - START_POS)>200) { |
Lightvalve | 13:747daba9cf59 | 918 | DZ_case = 1; |
Lightvalve | 58:64181f1d3e60 | 919 | } else if((FINAL_POS - START_POS)<-200) { |
Lightvalve | 13:747daba9cf59 | 920 | DZ_case = -1; |
Lightvalve | 14:8e7590227d22 | 921 | } else { |
Lightvalve | 13:747daba9cf59 | 922 | DZ_case = 0; |
Lightvalve | 13:747daba9cf59 | 923 | } |
Lightvalve | 57:f308b1656d9c | 924 | |
Lightvalve | 58:64181f1d3e60 | 925 | CAN_TX_PRES((int16_t) (DZ_case), (int16_t) (6)); |
Lightvalve | 49:3c630b5eba9f | 926 | |
Lightvalve | 13:747daba9cf59 | 927 | first_check = 1; |
Lightvalve | 13:747daba9cf59 | 928 | DZ_DIRECTION = 1; |
Lightvalve | 13:747daba9cf59 | 929 | VALVE_DZ_timer = 0; |
Lightvalve | 13:747daba9cf59 | 930 | Ref_Valve_Pos_Old = DDV_POS_AVG; |
Lightvalve | 13:747daba9cf59 | 931 | DZ_NUM = 1; |
Lightvalve | 13:747daba9cf59 | 932 | DZ_index = 1; |
Lightvalve | 14:8e7590227d22 | 933 | |
Lightvalve | 13:747daba9cf59 | 934 | } |
Lightvalve | 19:23b7c1ad8683 | 935 | } else { |
Lightvalve | 14:8e7590227d22 | 936 | if((DZ_case == -1 && DZ_NUM == 1) | (DZ_case == 1 && DZ_NUM == 1)) { |
Lightvalve | 30:8d561f16383b | 937 | if(VALVE_DZ_timer < (int) (1.0 * (float) TMR_FREQ_5k)) { |
Lightvalve | 58:64181f1d3e60 | 938 | 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 | 58:64181f1d3e60 | 939 | //pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end; |
Lightvalve | 58:64181f1d3e60 | 940 | //CONTROL_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 30:8d561f16383b | 941 | } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 942 | START_POS = pos.sen; |
Lightvalve | 30:8d561f16383b | 943 | } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 944 | valve_pos.ref = Ref_Valve_Pos_Old - DZ_case * DZ_DIRECTION * 64 / DZ_index; |
Lightvalve | 14:8e7590227d22 | 945 | if(valve_pos.ref <= VALVE_MIN_POS) { |
Lightvalve | 14:8e7590227d22 | 946 | valve_pos.ref = VALVE_MIN_POS; |
Lightvalve | 14:8e7590227d22 | 947 | } else if(valve_pos.ref >= VALVE_MAX_POS) { |
Lightvalve | 14:8e7590227d22 | 948 | valve_pos.ref = VALVE_MAX_POS; |
Lightvalve | 13:747daba9cf59 | 949 | } |
Lightvalve | 14:8e7590227d22 | 950 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 58:64181f1d3e60 | 951 | |
Lightvalve | 30:8d561f16383b | 952 | } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 953 | Ref_Valve_Pos_Old = valve_pos.ref; |
Lightvalve | 14:8e7590227d22 | 954 | FINAL_POS = pos.sen; |
Lightvalve | 14:8e7590227d22 | 955 | |
Lightvalve | 14:8e7590227d22 | 956 | if((FINAL_POS - START_POS)>100) { |
Lightvalve | 14:8e7590227d22 | 957 | DZ_DIRECTION = 1 * DZ_case; |
Lightvalve | 14:8e7590227d22 | 958 | } else if((FINAL_POS - START_POS)<-100) { |
Lightvalve | 14:8e7590227d22 | 959 | DZ_DIRECTION = -1 * DZ_case; |
Lightvalve | 14:8e7590227d22 | 960 | } else { |
Lightvalve | 13:747daba9cf59 | 961 | DZ_DIRECTION = 1 * DZ_case; |
Lightvalve | 13:747daba9cf59 | 962 | } |
Lightvalve | 14:8e7590227d22 | 963 | |
Lightvalve | 13:747daba9cf59 | 964 | VALVE_DZ_timer = 0; |
Lightvalve | 13:747daba9cf59 | 965 | DZ_index= DZ_index *2; |
Lightvalve | 14:8e7590227d22 | 966 | if(DZ_index >= 128) { |
Lightvalve | 14:8e7590227d22 | 967 | FIRST_DZ = valve_pos.ref; |
Lightvalve | 13:747daba9cf59 | 968 | DZ_NUM = 2; |
Lightvalve | 13:747daba9cf59 | 969 | Ref_Valve_Pos_Old = FIRST_DZ; |
Lightvalve | 13:747daba9cf59 | 970 | DZ_index = 1; |
Lightvalve | 13:747daba9cf59 | 971 | DZ_DIRECTION = 1; |
Lightvalve | 13:747daba9cf59 | 972 | } |
Lightvalve | 13:747daba9cf59 | 973 | } |
Lightvalve | 14:8e7590227d22 | 974 | } else if((DZ_case == -1 && DZ_NUM == 2) | (DZ_case == 1 && DZ_NUM == 2)) { |
Lightvalve | 30:8d561f16383b | 975 | if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 58:64181f1d3e60 | 976 | 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 | 58:64181f1d3e60 | 977 | //pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end; |
Lightvalve | 58:64181f1d3e60 | 978 | //CONTROL_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 30:8d561f16383b | 979 | } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 980 | START_POS = pos.sen; |
Lightvalve | 30:8d561f16383b | 981 | } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 58:64181f1d3e60 | 982 | valve_pos.ref = Ref_Valve_Pos_Old - DZ_case * DZ_DIRECTION * 64 / DZ_index; |
Lightvalve | 14:8e7590227d22 | 983 | if(valve_pos.ref <= VALVE_MIN_POS) { |
Lightvalve | 14:8e7590227d22 | 984 | valve_pos.ref = VALVE_MIN_POS; |
Lightvalve | 14:8e7590227d22 | 985 | } else if(valve_pos.ref >= VALVE_MAX_POS) { |
Lightvalve | 14:8e7590227d22 | 986 | valve_pos.ref = VALVE_MAX_POS; |
Lightvalve | 13:747daba9cf59 | 987 | } |
Lightvalve | 14:8e7590227d22 | 988 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 58:64181f1d3e60 | 989 | |
Lightvalve | 30:8d561f16383b | 990 | } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 57:f308b1656d9c | 991 | Vout.ref = 0.0f; |
Lightvalve | 57:f308b1656d9c | 992 | } else if(VALVE_DZ_timer > (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 993 | Ref_Valve_Pos_Old = valve_pos.ref; |
Lightvalve | 14:8e7590227d22 | 994 | FINAL_POS = pos.sen; |
Lightvalve | 14:8e7590227d22 | 995 | |
Lightvalve | 14:8e7590227d22 | 996 | if((FINAL_POS - START_POS)>100) { |
Lightvalve | 58:64181f1d3e60 | 997 | DZ_DIRECTION = 1 * DZ_case; |
Lightvalve | 14:8e7590227d22 | 998 | } else if((FINAL_POS - START_POS)<-100) { |
Lightvalve | 58:64181f1d3e60 | 999 | DZ_DIRECTION = -1 * DZ_case; |
Lightvalve | 14:8e7590227d22 | 1000 | } else { |
Lightvalve | 58:64181f1d3e60 | 1001 | DZ_DIRECTION = -1 * DZ_case; |
Lightvalve | 13:747daba9cf59 | 1002 | } |
Lightvalve | 14:8e7590227d22 | 1003 | |
Lightvalve | 13:747daba9cf59 | 1004 | VALVE_DZ_timer = 0; |
Lightvalve | 58:64181f1d3e60 | 1005 | DZ_index= DZ_index * 2; |
Lightvalve | 14:8e7590227d22 | 1006 | if(DZ_index >= 128) { |
Lightvalve | 14:8e7590227d22 | 1007 | SECOND_DZ = valve_pos.ref; |
Lightvalve | 55:f4819de54e7a | 1008 | VALVE_CENTER = (int) (0.5f * (float) (FIRST_DZ) + 0.5f * (float) (SECOND_DZ)); |
Lightvalve | 13:747daba9cf59 | 1009 | first_check = 0; |
Lightvalve | 33:91b17819ec30 | 1010 | VALVE_DEADZONE_MINUS = (float) FIRST_DZ; |
Lightvalve | 33:91b17819ec30 | 1011 | VALVE_DEADZONE_PLUS = (float) SECOND_DZ; |
Lightvalve | 19:23b7c1ad8683 | 1012 | |
Lightvalve | 16:903b5a4433b4 | 1013 | ROM_RESET_DATA(); |
Lightvalve | 34:bb2ca2fc2a8e | 1014 | |
Lightvalve | 58:64181f1d3e60 | 1015 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 13:747daba9cf59 | 1016 | DZ_index = 1; |
Lightvalve | 13:747daba9cf59 | 1017 | } |
Lightvalve | 13:747daba9cf59 | 1018 | } |
Lightvalve | 14:8e7590227d22 | 1019 | } else if(DZ_case == 0 && DZ_NUM ==1) { |
Lightvalve | 30:8d561f16383b | 1020 | if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 58:64181f1d3e60 | 1021 | 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 | 58:64181f1d3e60 | 1022 | //pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end; |
Lightvalve | 58:64181f1d3e60 | 1023 | //CONTROL_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 30:8d561f16383b | 1024 | } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1025 | START_POS = pos.sen; |
Lightvalve | 30:8d561f16383b | 1026 | } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1027 | valve_pos.ref = Ref_Valve_Pos_Old - DZ_DIRECTION * 64 / DZ_index; |
Lightvalve | 14:8e7590227d22 | 1028 | if(valve_pos.ref <= VALVE_MIN_POS) { |
Lightvalve | 14:8e7590227d22 | 1029 | valve_pos.ref = VALVE_MIN_POS; |
Lightvalve | 14:8e7590227d22 | 1030 | } else if(valve_pos.ref >= VALVE_MAX_POS) { |
Lightvalve | 14:8e7590227d22 | 1031 | valve_pos.ref = VALVE_MAX_POS; |
Lightvalve | 13:747daba9cf59 | 1032 | } |
Lightvalve | 14:8e7590227d22 | 1033 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 58:64181f1d3e60 | 1034 | |
Lightvalve | 30:8d561f16383b | 1035 | } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1036 | Ref_Valve_Pos_Old = valve_pos.ref; |
Lightvalve | 14:8e7590227d22 | 1037 | FINAL_POS = pos.sen; |
Lightvalve | 14:8e7590227d22 | 1038 | |
Lightvalve | 14:8e7590227d22 | 1039 | if((FINAL_POS - START_POS)>100) { |
Lightvalve | 14:8e7590227d22 | 1040 | DZ_DIRECTION = 1; |
Lightvalve | 14:8e7590227d22 | 1041 | } else if((FINAL_POS - START_POS)<-100) { |
Lightvalve | 14:8e7590227d22 | 1042 | DZ_DIRECTION = -1; |
Lightvalve | 14:8e7590227d22 | 1043 | } else { |
Lightvalve | 13:747daba9cf59 | 1044 | DZ_DIRECTION = 1; |
Lightvalve | 13:747daba9cf59 | 1045 | } |
Lightvalve | 13:747daba9cf59 | 1046 | VALVE_DZ_timer = 0; |
Lightvalve | 13:747daba9cf59 | 1047 | DZ_index= DZ_index *2; |
Lightvalve | 14:8e7590227d22 | 1048 | if(DZ_index >= 128) { |
Lightvalve | 14:8e7590227d22 | 1049 | FIRST_DZ = valve_pos.ref; |
Lightvalve | 13:747daba9cf59 | 1050 | DZ_NUM = 2; |
Lightvalve | 13:747daba9cf59 | 1051 | Ref_Valve_Pos_Old = FIRST_DZ; |
Lightvalve | 13:747daba9cf59 | 1052 | DZ_index = 1; |
Lightvalve | 13:747daba9cf59 | 1053 | DZ_DIRECTION = 1; |
Lightvalve | 13:747daba9cf59 | 1054 | } |
Lightvalve | 13:747daba9cf59 | 1055 | } |
Lightvalve | 14:8e7590227d22 | 1056 | } else { |
Lightvalve | 30:8d561f16383b | 1057 | if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 58:64181f1d3e60 | 1058 | 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 | 58:64181f1d3e60 | 1059 | //pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end; |
Lightvalve | 58:64181f1d3e60 | 1060 | //CONTROL_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 30:8d561f16383b | 1061 | } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1062 | START_POS = pos.sen; |
Lightvalve | 30:8d561f16383b | 1063 | } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1064 | valve_pos.ref = Ref_Valve_Pos_Old + DZ_DIRECTION * 64 / DZ_index; |
Lightvalve | 14:8e7590227d22 | 1065 | if(valve_pos.ref <= VALVE_MIN_POS) { |
Lightvalve | 14:8e7590227d22 | 1066 | valve_pos.ref = VALVE_MIN_POS; |
Lightvalve | 14:8e7590227d22 | 1067 | } else if(valve_pos.ref > VALVE_MAX_POS) { |
Lightvalve | 14:8e7590227d22 | 1068 | valve_pos.ref = VALVE_MAX_POS - 1; |
Lightvalve | 13:747daba9cf59 | 1069 | } |
Lightvalve | 14:8e7590227d22 | 1070 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 58:64181f1d3e60 | 1071 | |
Lightvalve | 30:8d561f16383b | 1072 | } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 57:f308b1656d9c | 1073 | Vout.ref = 0.0f; |
Lightvalve | 57:f308b1656d9c | 1074 | } else if(VALVE_DZ_timer > (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1075 | Ref_Valve_Pos_Old = valve_pos.ref; |
Lightvalve | 14:8e7590227d22 | 1076 | FINAL_POS = pos.sen; |
Lightvalve | 58:64181f1d3e60 | 1077 | |
Lightvalve | 58:64181f1d3e60 | 1078 | if((FINAL_POS - START_POS)>100) { |
Lightvalve | 13:747daba9cf59 | 1079 | DZ_DIRECTION = 1; |
Lightvalve | 58:64181f1d3e60 | 1080 | } else if((FINAL_POS - START_POS)<-100) { |
Lightvalve | 58:64181f1d3e60 | 1081 | DZ_DIRECTION = -1; |
Lightvalve | 58:64181f1d3e60 | 1082 | } else { |
Lightvalve | 13:747daba9cf59 | 1083 | DZ_DIRECTION = 1; |
Lightvalve | 58:64181f1d3e60 | 1084 | } |
Lightvalve | 14:8e7590227d22 | 1085 | |
Lightvalve | 13:747daba9cf59 | 1086 | VALVE_DZ_timer = 0; |
Lightvalve | 13:747daba9cf59 | 1087 | DZ_index= DZ_index *2; |
Lightvalve | 14:8e7590227d22 | 1088 | if(DZ_index >= 128) { |
Lightvalve | 14:8e7590227d22 | 1089 | SECOND_DZ = valve_pos.ref; |
Lightvalve | 55:f4819de54e7a | 1090 | VALVE_CENTER = (int) (0.5f * (float) (FIRST_DZ) + 0.5f * (float) (SECOND_DZ)); |
Lightvalve | 13:747daba9cf59 | 1091 | first_check = 0; |
Lightvalve | 33:91b17819ec30 | 1092 | VALVE_DEADZONE_MINUS = (float) FIRST_DZ; |
Lightvalve | 33:91b17819ec30 | 1093 | VALVE_DEADZONE_PLUS = (float) SECOND_DZ; |
Lightvalve | 57:f308b1656d9c | 1094 | |
Lightvalve | 57:f308b1656d9c | 1095 | ROM_RESET_DATA(); |
Lightvalve | 34:bb2ca2fc2a8e | 1096 | |
Lightvalve | 55:f4819de54e7a | 1097 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 13:747daba9cf59 | 1098 | DZ_index = 1; |
Lightvalve | 13:747daba9cf59 | 1099 | } |
Lightvalve | 13:747daba9cf59 | 1100 | } |
Lightvalve | 13:747daba9cf59 | 1101 | } |
Lightvalve | 14:8e7590227d22 | 1102 | } |
Lightvalve | 13:747daba9cf59 | 1103 | break; |
Lightvalve | 13:747daba9cf59 | 1104 | } |
Lightvalve | 14:8e7590227d22 | 1105 | |
Lightvalve | 14:8e7590227d22 | 1106 | case MODE_DDV_POS_VS_FLOWRATE: { |
Lightvalve | 57:f308b1656d9c | 1107 | CONTROL_MODE = MODE_VALVE_OPEN_LOOP; |
Lightvalve | 13:747daba9cf59 | 1108 | VALVE_FR_timer = VALVE_FR_timer + 1; |
Lightvalve | 14:8e7590227d22 | 1109 | if(first_check == 0) { |
Lightvalve | 30:8d561f16383b | 1110 | if(VALVE_FR_timer < (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 57:f308b1656d9c | 1111 | Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 23:59218d4a256d | 1112 | //CAN_TX_PRES((int16_t) (VALVE_FR_timer), (int16_t) (6)); |
Lightvalve | 30:8d561f16383b | 1113 | } else if(VALVE_FR_timer == (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 57:f308b1656d9c | 1114 | Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 14:8e7590227d22 | 1115 | pos_plus_end = pos.sen; |
Lightvalve | 14:8e7590227d22 | 1116 | // CAN_TX_PRES((int16_t) (V_out), (int16_t) (7)); |
Lightvalve | 30:8d561f16383b | 1117 | } else if(VALVE_FR_timer < (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 57:f308b1656d9c | 1118 | Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 30:8d561f16383b | 1119 | } else if(VALVE_FR_timer == (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1120 | // CAN_TX_PRES((int16_t) (V_out), (int16_t) (8)); |
Lightvalve | 57:f308b1656d9c | 1121 | Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 14:8e7590227d22 | 1122 | pos_minus_end = pos.sen; |
Lightvalve | 13:747daba9cf59 | 1123 | first_check = 1; |
Lightvalve | 13:747daba9cf59 | 1124 | VALVE_FR_timer = 0; |
Lightvalve | 55:f4819de54e7a | 1125 | valve_pos.ref = (float) VALVE_CENTER; |
Lightvalve | 13:747daba9cf59 | 1126 | ID_index = 0; |
Lightvalve | 13:747daba9cf59 | 1127 | max_check = 0; |
Lightvalve | 13:747daba9cf59 | 1128 | min_check = 0; |
Lightvalve | 13:747daba9cf59 | 1129 | } |
Lightvalve | 14:8e7590227d22 | 1130 | } else { |
Lightvalve | 30:8d561f16383b | 1131 | if(VALVE_FR_timer < (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 57:f308b1656d9c | 1132 | //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 | 57:f308b1656d9c | 1133 | pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end; |
Lightvalve | 57:f308b1656d9c | 1134 | CONTROL_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 30:8d561f16383b | 1135 | } else if(VALVE_FR_timer == (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 13:747daba9cf59 | 1136 | data_num = 0; |
Lightvalve | 55:f4819de54e7a | 1137 | valve_pos.ref = 10.0f*((float) ID_index_array[ID_index]) + (float) VALVE_CENTER; |
Lightvalve | 14:8e7590227d22 | 1138 | |
Lightvalve | 14:8e7590227d22 | 1139 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 14:8e7590227d22 | 1140 | START_POS = pos.sen; |
Lightvalve | 30:8d561f16383b | 1141 | } else if(VALVE_FR_timer < (int) (5.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 55:f4819de54e7a | 1142 | valve_pos.ref = 10.0f*((float) ID_index_array[ID_index]) + (float) VALVE_CENTER; |
Lightvalve | 14:8e7590227d22 | 1143 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 13:747daba9cf59 | 1144 | data_num = data_num + 1; |
Lightvalve | 30:8d561f16383b | 1145 | if(abs(0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen) > 20000.0f) { |
Lightvalve | 14:8e7590227d22 | 1146 | FINAL_POS = pos.sen; |
Lightvalve | 14:8e7590227d22 | 1147 | one_period_end = 1; |
Lightvalve | 13:747daba9cf59 | 1148 | } |
Lightvalve | 30:8d561f16383b | 1149 | } else if(VALVE_FR_timer == (int) (5.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1150 | FINAL_POS = pos.sen; |
Lightvalve | 13:747daba9cf59 | 1151 | one_period_end = 1; |
Lightvalve | 57:f308b1656d9c | 1152 | V_out = 0.0f; |
Lightvalve | 13:747daba9cf59 | 1153 | } |
Lightvalve | 14:8e7590227d22 | 1154 | |
Lightvalve | 14:8e7590227d22 | 1155 | if(one_period_end == 1) { |
Lightvalve | 14:8e7590227d22 | 1156 | if(valve_pos.ref > VALVE_MAX_POS) { |
Lightvalve | 13:747daba9cf59 | 1157 | max_check = 1; |
Lightvalve | 14:8e7590227d22 | 1158 | } else if(valve_pos.ref < VALVE_MIN_POS) { |
Lightvalve | 13:747daba9cf59 | 1159 | min_check = 1; |
Lightvalve | 13:747daba9cf59 | 1160 | } |
Lightvalve | 13:747daba9cf59 | 1161 | JOINT_VEL[ID_index] = (FINAL_POS - START_POS) / data_num * TMR_FREQ_5k; // pulse/sec |
Lightvalve | 19:23b7c1ad8683 | 1162 | |
Lightvalve | 13:747daba9cf59 | 1163 | VALVE_FR_timer = 0; |
Lightvalve | 13:747daba9cf59 | 1164 | one_period_end = 0; |
Lightvalve | 13:747daba9cf59 | 1165 | ID_index= ID_index +1; |
Lightvalve | 30:8d561f16383b | 1166 | V_out = 0.0f; |
Lightvalve | 13:747daba9cf59 | 1167 | } |
Lightvalve | 14:8e7590227d22 | 1168 | |
Lightvalve | 14:8e7590227d22 | 1169 | if(max_check == 1 && min_check == 1) { |
Lightvalve | 19:23b7c1ad8683 | 1170 | |
Lightvalve | 13:747daba9cf59 | 1171 | VALVE_POS_NUM = ID_index; |
Lightvalve | 57:f308b1656d9c | 1172 | ROM_RESET_DATA(); |
Lightvalve | 13:747daba9cf59 | 1173 | ID_index = 0; |
Lightvalve | 13:747daba9cf59 | 1174 | first_check = 0; |
Lightvalve | 13:747daba9cf59 | 1175 | VALVE_FR_timer = 0; |
Lightvalve | 55:f4819de54e7a | 1176 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 48:e7bcfc244d40 | 1177 | // CAN_TX_PRES((int16_t) (VALVE_FR_timer), (int16_t) (6)); |
Lightvalve | 13:747daba9cf59 | 1178 | } |
Lightvalve | 13:747daba9cf59 | 1179 | } |
Lightvalve | 13:747daba9cf59 | 1180 | break; |
Lightvalve | 13:747daba9cf59 | 1181 | } |
Lightvalve | 56:2eade98630e2 | 1182 | |
Lightvalve | 55:f4819de54e7a | 1183 | case MODE_SYSTEM_ID: { |
Lightvalve | 55:f4819de54e7a | 1184 | freq_sysid_Iref = (double) cnt_sysid * DT_TMR3 * 3.; |
Lightvalve | 55:f4819de54e7a | 1185 | valve_pos.ref = 2500.0f * sin(2.0f * 3.14159f * freq_sysid_Iref * (double) cnt_sysid * DT_TMR3); |
Lightvalve | 55:f4819de54e7a | 1186 | CONTROL_MODE = MODE_VALVE_OPEN_LOOP; |
Lightvalve | 55:f4819de54e7a | 1187 | cnt_sysid++; |
Lightvalve | 55:f4819de54e7a | 1188 | if (freq_sysid_Iref >= 300) { |
Lightvalve | 55:f4819de54e7a | 1189 | cnt_sysid = 0; |
Lightvalve | 55:f4819de54e7a | 1190 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 55:f4819de54e7a | 1191 | } |
Lightvalve | 55:f4819de54e7a | 1192 | break; |
Lightvalve | 55:f4819de54e7a | 1193 | } |
Lightvalve | 56:2eade98630e2 | 1194 | |
Lightvalve | 56:2eade98630e2 | 1195 | |
Lightvalve | 55:f4819de54e7a | 1196 | |
Lightvalve | 55:f4819de54e7a | 1197 | default: |
Lightvalve | 55:f4819de54e7a | 1198 | break; |
Lightvalve | 55:f4819de54e7a | 1199 | } |
Lightvalve | 55:f4819de54e7a | 1200 | |
Lightvalve | 55:f4819de54e7a | 1201 | // CONTROL MODE ------------------------------------------------------------ |
Lightvalve | 55:f4819de54e7a | 1202 | |
Lightvalve | 55:f4819de54e7a | 1203 | switch (CONTROL_MODE) { |
Lightvalve | 55:f4819de54e7a | 1204 | case MODE_NO_ACT: { |
Lightvalve | 55:f4819de54e7a | 1205 | V_out = 0.0f; |
Lightvalve | 55:f4819de54e7a | 1206 | break; |
Lightvalve | 55:f4819de54e7a | 1207 | } |
Lightvalve | 55:f4819de54e7a | 1208 | |
Lightvalve | 55:f4819de54e7a | 1209 | case MODE_VALVE_POSITION_CONTROL: { |
Lightvalve | 55:f4819de54e7a | 1210 | if (((OPERATING_MODE&0b110)>>1) == 0) { //Moog Valve |
Lightvalve | 55:f4819de54e7a | 1211 | I_REF = valve_pos.ref; |
Lightvalve | 55:f4819de54e7a | 1212 | } else if (((OPERATING_MODE&0b110)>>1) == 1) { //KNR Valve |
Lightvalve | 55:f4819de54e7a | 1213 | V_out = valve_pos.ref; |
Lightvalve | 55:f4819de54e7a | 1214 | } else { //SW Valve |
Lightvalve | 55:f4819de54e7a | 1215 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 57:f308b1656d9c | 1216 | V_out = Vout.ref; |
Lightvalve | 55:f4819de54e7a | 1217 | } |
Lightvalve | 56:2eade98630e2 | 1218 | |
Lightvalve | 55:f4819de54e7a | 1219 | break; |
Lightvalve | 55:f4819de54e7a | 1220 | } |
Lightvalve | 55:f4819de54e7a | 1221 | |
Lightvalve | 55:f4819de54e7a | 1222 | case MODE_JOINT_CONTROL: { |
Lightvalve | 55:f4819de54e7a | 1223 | double torq_ref = 0.0f; |
Lightvalve | 55:f4819de54e7a | 1224 | pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm] |
Lightvalve | 55:f4819de54e7a | 1225 | vel.err = (vel.ref - vel.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm/s] |
Lightvalve | 55:f4819de54e7a | 1226 | pos.err_sum += pos.err/(float) TMR_FREQ_5k; //[mm] |
Lightvalve | 55:f4819de54e7a | 1227 | torq_ref = torq.ref + (K_SPRING * pos.err * 0.01f + D_DAMPER * vel.err * 0.0001f) / ENC_PULSE_PER_POSITION; //[N] |
Lightvalve | 55:f4819de54e7a | 1228 | |
Lightvalve | 55:f4819de54e7a | 1229 | // torque feedback |
Lightvalve | 56:2eade98630e2 | 1230 | torq.err = (torq_ref)/(float)(TORQUE_SENSOR_PULSE_PER_TORQUE) - torq.sen; //[N] |
Lightvalve | 55:f4819de54e7a | 1231 | torq.err_sum += torq.err/(float) TMR_FREQ_5k; //[N] |
Lightvalve | 55:f4819de54e7a | 1232 | |
Lightvalve | 55:f4819de54e7a | 1233 | if (((OPERATING_MODE&0b110)>>1) == 0 || ((OPERATING_MODE&0b110)>>1) == 1) { |
Lightvalve | 56:2eade98630e2 | 1234 | |
Lightvalve | 55:f4819de54e7a | 1235 | double I_REF_POS = 0.0f; |
Lightvalve | 55:f4819de54e7a | 1236 | double I_REF_FORCE_FB = 0.0f; // I_REF by Force Feedback |
Lightvalve | 55:f4819de54e7a | 1237 | double I_REF_VC = 0.0f; // I_REF for velocity compensation |
Lightvalve | 55:f4819de54e7a | 1238 | |
Lightvalve | 55:f4819de54e7a | 1239 | double temp_vel_pos = 0.0f; |
Lightvalve | 55:f4819de54e7a | 1240 | double temp_vel_torq = 0.0f; |
Lightvalve | 55:f4819de54e7a | 1241 | double wn_Pos = 2.0f * PI * 5.0f; // f_cut : 5Hz Position Control |
Lightvalve | 56:2eade98630e2 | 1242 | |
Lightvalve | 55:f4819de54e7a | 1243 | if ((OPERATING_MODE && 0x01) == 0) { // Rotary Mode |
Lightvalve | 55:f4819de54e7a | 1244 | 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 | 55:f4819de54e7a | 1245 | // L when P-gain = 100, f_cut = 10Hz L feedforward velocity |
Lightvalve | 55:f4819de54e7a | 1246 | } else if ((OPERATING_MODE && 0x01) == 1) { |
Lightvalve | 55:f4819de54e7a | 1247 | 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 | 55:f4819de54e7a | 1248 | // L when P-gain = 100, f_cut = 10Hz L feedforward velocity |
Lightvalve | 55:f4819de54e7a | 1249 | } |
Lightvalve | 55:f4819de54e7a | 1250 | 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 | 55:f4819de54e7a | 1251 | else I_REF_POS = temp_vel_pos * ((double) PISTON_AREA_B * 0.00006f / (K_v * sqrt(2.0f / (alpha3 + 1.0f)))); |
Lightvalve | 55:f4819de54e7a | 1252 | |
Lightvalve | 55:f4819de54e7a | 1253 | // velocity compensation for torque control |
Lightvalve | 55:f4819de54e7a | 1254 | if ((OPERATING_MODE && 0x01) == 0) { // Rotary Mode |
Lightvalve | 55:f4819de54e7a | 1255 | I_REF_FORCE_FB = 0.001f * ((double) P_GAIN_JOINT_TORQUE * torq.err + (double) I_GAIN_JOINT_TORQUE * torq.err_sum); |
Lightvalve | 55:f4819de54e7a | 1256 | // temp_vel_torq = (0.01 * (double) VELOCITY_COMP_GAIN * (double) CUR_VELOCITY / (double) ENC_PULSE_PER_POSITION) * PI / 180.0; // rad/s |
Lightvalve | 55:f4819de54e7a | 1257 | temp_vel_torq = (0.01f * (double) VELOCITY_COMP_GAIN * vel.ref / (double) ENC_PULSE_PER_POSITION) * PI / 180.0f; // rad/s |
Lightvalve | 55:f4819de54e7a | 1258 | // L feedforward velocity |
Lightvalve | 55:f4819de54e7a | 1259 | } else if ((OPERATING_MODE && 0x01) == 1) { |
Lightvalve | 55:f4819de54e7a | 1260 | 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 | 55:f4819de54e7a | 1261 | // temp_vel_torq = (0.01 * (double) VELOCITY_COMP_GAIN * (double) CUR_VELOCITY / (double) ENC_PULSE_PER_POSITION); // mm/s |
Lightvalve | 55:f4819de54e7a | 1262 | temp_vel_torq = (0.01f * (double) VELOCITY_COMP_GAIN * vel.ref / (double) ENC_PULSE_PER_POSITION); // mm/s |
Lightvalve | 55:f4819de54e7a | 1263 | // L feedforward velocity |
Lightvalve | 55:f4819de54e7a | 1264 | } |
Lightvalve | 55:f4819de54e7a | 1265 | 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 | 55:f4819de54e7a | 1266 | else I_REF_VC = temp_vel_torq * ((double) PISTON_AREA_B * 0.00006f / (K_v * sqrt(2.0f / (alpha3 + 1.0f)))); |
Lightvalve | 55:f4819de54e7a | 1267 | // L velocity(rad/s or mm/s) >> I_ref(mA) |
Lightvalve | 55:f4819de54e7a | 1268 | // Ref_Joint_FT_dot = (Ref_Joint_FT_Nm - Ref_Joint_FT_Nm_old) / TMR_DT_5k; |
Lightvalve | 55:f4819de54e7a | 1269 | // Ref_Joint_FT_Nm_old = Ref_Joint_FT_Nm; |
Lightvalve | 55:f4819de54e7a | 1270 | |
Lightvalve | 55:f4819de54e7a | 1271 | I_REF = (1.0f - alpha_trans) * I_REF_POS + alpha_trans * (I_REF_VC + I_REF_FORCE_FB); |
Lightvalve | 55:f4819de54e7a | 1272 | |
Lightvalve | 55:f4819de54e7a | 1273 | } else { |
Lightvalve | 55:f4819de54e7a | 1274 | float VALVE_POS_RAW_FORCE_FB = 0.0f; |
Lightvalve | 55:f4819de54e7a | 1275 | |
Lightvalve | 55:f4819de54e7a | 1276 | 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 | 55:f4819de54e7a | 1277 | + 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 | 55:f4819de54e7a | 1278 | |
Lightvalve | 55:f4819de54e7a | 1279 | if (VALVE_POS_RAW_FORCE_FB >= 0) { |
Lightvalve | 55:f4819de54e7a | 1280 | valve_pos.ref = VALVE_POS_RAW_FORCE_FB + VALVE_DEADZONE_PLUS; |
Lightvalve | 55:f4819de54e7a | 1281 | } else { |
Lightvalve | 55:f4819de54e7a | 1282 | valve_pos.ref = VALVE_POS_RAW_FORCE_FB + VALVE_DEADZONE_MINUS; |
Lightvalve | 55:f4819de54e7a | 1283 | } |
Lightvalve | 55:f4819de54e7a | 1284 | |
Lightvalve | 55:f4819de54e7a | 1285 | if(I_GAIN_JOINT_TORQUE != 0) { |
Lightvalve | 55:f4819de54e7a | 1286 | double Ka = 1.0f / (double) I_GAIN_JOINT_TORQUE * 100.0f; |
Lightvalve | 55:f4819de54e7a | 1287 | if(valve_pos.ref>VALVE_MAX_POS) { |
Lightvalve | 55:f4819de54e7a | 1288 | double valve_pos_rem = valve_pos.ref - VALVE_MAX_POS; |
Lightvalve | 55:f4819de54e7a | 1289 | valve_pos_rem = valve_pos_rem * Ka; |
Lightvalve | 55:f4819de54e7a | 1290 | valve_pos.ref = VALVE_MAX_POS; |
Lightvalve | 55:f4819de54e7a | 1291 | torq.err_sum = torq.err_sum - valve_pos_rem/(float) TMR_FREQ_5k; |
Lightvalve | 55:f4819de54e7a | 1292 | } else if(valve_pos.ref < VALVE_MIN_POS) { |
Lightvalve | 55:f4819de54e7a | 1293 | double valve_pos_rem = valve_pos.ref - VALVE_MIN_POS; |
Lightvalve | 55:f4819de54e7a | 1294 | valve_pos_rem = valve_pos_rem * Ka; |
Lightvalve | 55:f4819de54e7a | 1295 | valve_pos.ref = VALVE_MIN_POS; |
Lightvalve | 55:f4819de54e7a | 1296 | torq.err_sum = torq.err_sum - valve_pos_rem/(float) TMR_FREQ_5k; |
Lightvalve | 55:f4819de54e7a | 1297 | } |
Lightvalve | 55:f4819de54e7a | 1298 | } |
Lightvalve | 58:64181f1d3e60 | 1299 | |
Lightvalve | 55:f4819de54e7a | 1300 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 58:64181f1d3e60 | 1301 | |
Lightvalve | 58:64181f1d3e60 | 1302 | Vout.ref = (float) P_GAIN_JOINT_POSITION * 0.01f * ((float) pos.err); |
Lightvalve | 58:64181f1d3e60 | 1303 | V_out = (float) Vout.ref; |
Lightvalve | 55:f4819de54e7a | 1304 | |
Lightvalve | 55:f4819de54e7a | 1305 | } |
Lightvalve | 56:2eade98630e2 | 1306 | |
Lightvalve | 55:f4819de54e7a | 1307 | break; |
Lightvalve | 55:f4819de54e7a | 1308 | } |
Lightvalve | 56:2eade98630e2 | 1309 | |
Lightvalve | 55:f4819de54e7a | 1310 | case MODE_VALVE_OPEN_LOOP: { |
Lightvalve | 55:f4819de54e7a | 1311 | V_out = (float) Vout.ref; |
Lightvalve | 55:f4819de54e7a | 1312 | break; |
Lightvalve | 55:f4819de54e7a | 1313 | } |
Lightvalve | 14:8e7590227d22 | 1314 | |
Lightvalve | 12:6f2531038ea4 | 1315 | default: |
Lightvalve | 12:6f2531038ea4 | 1316 | break; |
Lightvalve | 12:6f2531038ea4 | 1317 | } |
Lightvalve | 14:8e7590227d22 | 1318 | |
Lightvalve | 55:f4819de54e7a | 1319 | |
Lightvalve | 55:f4819de54e7a | 1320 | if (((OPERATING_MODE&0b110)>>1) == 0 || ((OPERATING_MODE&0b110)>>1) == 1) { //Moog Valve or KNR Valve |
Lightvalve | 56:2eade98630e2 | 1321 | |
Lightvalve | 55:f4819de54e7a | 1322 | //////////////////////////////////////////////////////////////////////////// |
Lightvalve | 55:f4819de54e7a | 1323 | //////////////////////////// CURRENT CONTROL ////////////////////////////// |
Lightvalve | 55:f4819de54e7a | 1324 | //////////////////////////////////////////////////////////////////////////// |
Lightvalve | 55:f4819de54e7a | 1325 | if (CURRENT_CONTROL_MODE) { |
Lightvalve | 55:f4819de54e7a | 1326 | double alpha_update_Iref = 1.0f / (1.0f + TMR_FREQ_5k / (2.0f * 3.14f * 300.0f)); // f_cutoff : 500Hz |
Lightvalve | 55:f4819de54e7a | 1327 | I_REF_fil = (1.0f - alpha_update_Iref) * I_REF_fil + alpha_update_Iref*I_REF; |
Lightvalve | 55:f4819de54e7a | 1328 | |
Lightvalve | 55:f4819de54e7a | 1329 | I_ERR = I_REF_fil - cur.sen; |
Lightvalve | 55:f4819de54e7a | 1330 | I_ERR_INT = I_ERR_INT + (I_ERR) * DT_TMR3; |
Lightvalve | 55:f4819de54e7a | 1331 | |
Lightvalve | 55:f4819de54e7a | 1332 | |
Lightvalve | 55:f4819de54e7a | 1333 | // Moog Valve Current Control Gain |
Lightvalve | 55:f4819de54e7a | 1334 | double R_model = 539.0f; // ohm |
Lightvalve | 55:f4819de54e7a | 1335 | double L_model = 1.2f; |
Lightvalve | 55:f4819de54e7a | 1336 | double w0 = 2.0f * 3.14f * 150.0f; |
Lightvalve | 55:f4819de54e7a | 1337 | double KP_I = 0.1f * L_model*w0; |
Lightvalve | 55:f4819de54e7a | 1338 | double KI_I = 0.1f * R_model*w0; |
Lightvalve | 55:f4819de54e7a | 1339 | |
Lightvalve | 55:f4819de54e7a | 1340 | // KNR Valve Current Control Gain |
Lightvalve | 55:f4819de54e7a | 1341 | if (((OPERATING_MODE & 0b110)>>1) == 1) { // KNR Valve |
Lightvalve | 55:f4819de54e7a | 1342 | R_model = 163.0f; // ohm |
Lightvalve | 55:f4819de54e7a | 1343 | L_model = 1.0f; |
Lightvalve | 55:f4819de54e7a | 1344 | w0 = 2.0f * 3.14f * 80.0f; |
Lightvalve | 55:f4819de54e7a | 1345 | KP_I = 1.0f * L_model*w0; |
Lightvalve | 55:f4819de54e7a | 1346 | KI_I = 0.08f * R_model*w0; |
Lightvalve | 55:f4819de54e7a | 1347 | } |
Lightvalve | 55:f4819de54e7a | 1348 | |
Lightvalve | 55:f4819de54e7a | 1349 | double FF_gain = 1.0f; |
Lightvalve | 55:f4819de54e7a | 1350 | |
Lightvalve | 55:f4819de54e7a | 1351 | VALVE_PWM_RAW = KP_I * I_ERR + KI_I * I_ERR_INT; |
Lightvalve | 55:f4819de54e7a | 1352 | // VALVE_PWM_RAW = VALVE_PWM_RAW + FF_gain * (R_model*I_REF); // Unit : mV |
Lightvalve | 55:f4819de54e7a | 1353 | I_REF_fil_diff = I_REF_fil - I_REF_fil_old; |
Lightvalve | 55:f4819de54e7a | 1354 | I_REF_fil_old = I_REF_fil; |
Lightvalve | 55:f4819de54e7a | 1355 | 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 | 55:f4819de54e7a | 1356 | double V_MAX = 12000.; // Maximum Voltage : 12V = 12000mV |
Lightvalve | 55:f4819de54e7a | 1357 | |
Lightvalve | 55:f4819de54e7a | 1358 | double Ka = 3.0f / KP_I; |
Lightvalve | 55:f4819de54e7a | 1359 | if (VALVE_PWM_RAW > V_MAX) { |
Lightvalve | 55:f4819de54e7a | 1360 | V_rem = VALVE_PWM_RAW - V_MAX; |
Lightvalve | 55:f4819de54e7a | 1361 | V_rem = Ka*V_rem; |
Lightvalve | 55:f4819de54e7a | 1362 | VALVE_PWM_RAW = V_MAX; |
Lightvalve | 55:f4819de54e7a | 1363 | I_ERR_INT = I_ERR_INT - V_rem * DT_TMR3; |
Lightvalve | 55:f4819de54e7a | 1364 | } else if (VALVE_PWM_RAW < -V_MAX) { |
Lightvalve | 55:f4819de54e7a | 1365 | V_rem = VALVE_PWM_RAW - (-V_MAX); |
Lightvalve | 55:f4819de54e7a | 1366 | V_rem = Ka*V_rem; |
Lightvalve | 55:f4819de54e7a | 1367 | VALVE_PWM_RAW = -V_MAX; |
Lightvalve | 55:f4819de54e7a | 1368 | I_ERR_INT = I_ERR_INT - V_rem * DT_TMR3; |
Lightvalve | 55:f4819de54e7a | 1369 | } |
Lightvalve | 55:f4819de54e7a | 1370 | Cur_Valve_Open_pulse = cur.sen / mA_PER_pulse; |
Lightvalve | 55:f4819de54e7a | 1371 | } else { |
Lightvalve | 55:f4819de54e7a | 1372 | VALVE_PWM_RAW = I_REF * mV_PER_mA; |
Lightvalve | 55:f4819de54e7a | 1373 | Cur_Valve_Open_pulse = I_REF / mA_PER_pulse; |
Lightvalve | 55:f4819de54e7a | 1374 | } |
Lightvalve | 55:f4819de54e7a | 1375 | |
Lightvalve | 55:f4819de54e7a | 1376 | //////////////////////////////////////////////////////////////////////////// |
Lightvalve | 55:f4819de54e7a | 1377 | ///////////////// Dead Zone Cancellation & Linearization ////////////////// |
Lightvalve | 55:f4819de54e7a | 1378 | //////////////////////////////////////////////////////////////////////////// |
Lightvalve | 55:f4819de54e7a | 1379 | // Dead Zone Cancellation (Mechanical Valve dead-zone) |
Lightvalve | 55:f4819de54e7a | 1380 | if (FLAG_VALVE_DEADZONE) { |
Lightvalve | 55:f4819de54e7a | 1381 | if (VALVE_PWM_RAW > 0) VALVE_PWM_RAW = VALVE_PWM_RAW + VALVE_DEADZONE_PLUS * mV_PER_pulse; // unit: mV |
Lightvalve | 55:f4819de54e7a | 1382 | else if (VALVE_PWM_RAW < 0) VALVE_PWM_RAW = VALVE_PWM_RAW + VALVE_DEADZONE_MINUS * mV_PER_pulse; // unit: mV |
Lightvalve | 55:f4819de54e7a | 1383 | |
Lightvalve | 55:f4819de54e7a | 1384 | VALVE_PWM_VALVE_DZ = VALVE_PWM_RAW + (double)VALVE_CENTER * mV_PER_pulse; // unit: mV |
Lightvalve | 55:f4819de54e7a | 1385 | |
Lightvalve | 55:f4819de54e7a | 1386 | } else { |
Lightvalve | 55:f4819de54e7a | 1387 | VALVE_PWM_VALVE_DZ = VALVE_PWM_RAW; |
Lightvalve | 55:f4819de54e7a | 1388 | } |
Lightvalve | 55:f4819de54e7a | 1389 | |
Lightvalve | 55:f4819de54e7a | 1390 | // Output Voltage Linearization |
Lightvalve | 55:f4819de54e7a | 1391 | double CUR_PWM_nonlin = VALVE_PWM_VALVE_DZ; // Unit : mV |
Lightvalve | 55:f4819de54e7a | 1392 | double CUR_PWM_lin = PWM_duty_byLT(CUR_PWM_nonlin); |
Lightvalve | 55:f4819de54e7a | 1393 | |
Lightvalve | 55:f4819de54e7a | 1394 | // Dead Zone Cancellation (Electrical dead-zone) |
Lightvalve | 55:f4819de54e7a | 1395 | if (CUR_PWM_lin > 0) V_out = (int) CUR_PWM_lin + 140; |
Lightvalve | 55:f4819de54e7a | 1396 | else if (CUR_PWM_lin < 0) V_out = (int) CUR_PWM_lin - 140; |
Lightvalve | 55:f4819de54e7a | 1397 | else V_out = CUR_PWM_lin; |
Lightvalve | 55:f4819de54e7a | 1398 | } |
Lightvalve | 56:2eade98630e2 | 1399 | |
Lightvalve | 56:2eade98630e2 | 1400 | |
Lightvalve | 14:8e7590227d22 | 1401 | |
jobuuu | 7:e9086c72bb22 | 1402 | /******************************************************* |
jobuuu | 7:e9086c72bb22 | 1403 | *** PWM |
jobuuu | 7:e9086c72bb22 | 1404 | ********************************************************/ |
Lightvalve | 48:e7bcfc244d40 | 1405 | if (V_out >= VALVE_VOLTAGE_LIMIT*1000.0f) { |
Lightvalve | 48:e7bcfc244d40 | 1406 | V_out = VALVE_VOLTAGE_LIMIT*1000.0f; |
Lightvalve | 48:e7bcfc244d40 | 1407 | } else if(V_out<=-VALVE_VOLTAGE_LIMIT*1000.0f) { |
Lightvalve | 48:e7bcfc244d40 | 1408 | V_out = -VALVE_VOLTAGE_LIMIT*1000.0f; |
Lightvalve | 27:a2254a485f23 | 1409 | } |
Lightvalve | 48:e7bcfc244d40 | 1410 | PWM_out= V_out/(SUPPLY_VOLTAGE*1000.0f); // Full duty : 12000.0mV |
Lightvalve | 13:747daba9cf59 | 1411 | |
Lightvalve | 19:23b7c1ad8683 | 1412 | // Saturation of output voltage to 12.0V |
Lightvalve | 30:8d561f16383b | 1413 | if(PWM_out > 1.0f) PWM_out=1.0f; |
Lightvalve | 30:8d561f16383b | 1414 | else if (PWM_out < -1.0f) PWM_out=-1.0f; |
Lightvalve | 13:747daba9cf59 | 1415 | |
Lightvalve | 30:8d561f16383b | 1416 | if (PWM_out>0.0f) { |
Lightvalve | 30:8d561f16383b | 1417 | dtc_v=0.0f; |
jobuuu | 1:e04e563be5ce | 1418 | dtc_w=PWM_out; |
jobuuu | 1:e04e563be5ce | 1419 | } else { |
jobuuu | 2:a1c0a37df760 | 1420 | dtc_v=-PWM_out; |
Lightvalve | 30:8d561f16383b | 1421 | dtc_w=0.0f; |
jobuuu | 1:e04e563be5ce | 1422 | } |
Lightvalve | 13:747daba9cf59 | 1423 | |
jobuuu | 1:e04e563be5ce | 1424 | //pwm |
Lightvalve | 30:8d561f16383b | 1425 | TIM4->CCR2 = (PWM_ARR)*(1.0f-dtc_v); |
Lightvalve | 30:8d561f16383b | 1426 | TIM4->CCR1 = (PWM_ARR)*(1.0f-dtc_w); |
Lightvalve | 13:747daba9cf59 | 1427 | |
Lightvalve | 20:806196fda269 | 1428 | |
Lightvalve | 55:f4819de54e7a | 1429 | if (TMR2_COUNT_CAN_TX % (int) ((int) TMR_FREQ_5k/CAN_FREQ) == 0) { |
Lightvalve | 20:806196fda269 | 1430 | |
Lightvalve | 52:647072f5307a | 1431 | // Position, Velocity, and Torque (ID:1200) |
Lightvalve | 54:6f50d9d3bfee | 1432 | if (flag_data_request[0] == HIGH) { |
Lightvalve | 55:f4819de54e7a | 1433 | if ((OPERATING_MODE & 0b01) == 0) { // Rotary Actuator |
Lightvalve | 52:647072f5307a | 1434 | if (SENSING_MODE == 0) { |
Lightvalve | 56:2eade98630e2 | 1435 | CAN_TX_POSITION_FT((int16_t) (pos.sen), (int16_t) (vel.sen/10.0f), (int16_t) (pres_A.sen*10.0f)); |
Lightvalve | 52:647072f5307a | 1436 | } else if (SENSING_MODE == 1) { |
Lightvalve | 56:2eade98630e2 | 1437 | 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 | 52:647072f5307a | 1438 | } |
Lightvalve | 55:f4819de54e7a | 1439 | } else if ((OPERATING_MODE & 0b01) == 1) { // Linear Actuator |
Lightvalve | 52:647072f5307a | 1440 | if (SENSING_MODE == 0) { |
Lightvalve | 58:64181f1d3e60 | 1441 | CAN_TX_POSITION_FT((int16_t) (pos.sen/4.0f), (int16_t) (vel.sen/100.0f), (int16_t) (pres_A.sen)); |
Lightvalve | 52:647072f5307a | 1442 | } else if (SENSING_MODE == 1) { |
Lightvalve | 56:2eade98630e2 | 1443 | 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 | 52:647072f5307a | 1444 | } |
Lightvalve | 51:8ea76864368a | 1445 | } |
Lightvalve | 51:8ea76864368a | 1446 | } |
Lightvalve | 54:6f50d9d3bfee | 1447 | if (flag_data_request[1] == HIGH) { |
Lightvalve | 52:647072f5307a | 1448 | //valve position |
Lightvalve | 52:647072f5307a | 1449 | double t_value = 0; |
Lightvalve | 55:f4819de54e7a | 1450 | // if(value>=(float) VALVE_CENTER) { |
Lightvalve | 55:f4819de54e7a | 1451 | // t_value = 10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER); |
Lightvalve | 55:f4819de54e7a | 1452 | // } else { |
Lightvalve | 55:f4819de54e7a | 1453 | // t_value = -10000.0f*((double)value - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER); |
Lightvalve | 55:f4819de54e7a | 1454 | // } |
Lightvalve | 56:2eade98630e2 | 1455 | if(OPERATING_MODE==5) { |
Lightvalve | 55:f4819de54e7a | 1456 | t_value = (double) value; |
Lightvalve | 56:2eade98630e2 | 1457 | } else if(CURRENT_CONTROL_MODE==1) { |
Lightvalve | 55:f4819de54e7a | 1458 | t_value = cur.sen; |
Lightvalve | 56:2eade98630e2 | 1459 | } else { |
Lightvalve | 55:f4819de54e7a | 1460 | t_value = V_out; |
Lightvalve | 52:647072f5307a | 1461 | } |
Lightvalve | 58:64181f1d3e60 | 1462 | CAN_TX_TORQUE((int16_t) (t_value), (int16_t) (V_out)); //1300 |
Lightvalve | 52:647072f5307a | 1463 | } |
Lightvalve | 56:2eade98630e2 | 1464 | |
Lightvalve | 56:2eade98630e2 | 1465 | |
Lightvalve | 54:6f50d9d3bfee | 1466 | if (flag_data_request[2] == HIGH) { |
Lightvalve | 54:6f50d9d3bfee | 1467 | //pressure A and B |
Lightvalve | 55:f4819de54e7a | 1468 | CAN_TX_PRES((int16_t) (pres_A.sen), (int16_t) (pres_B.sen)); // CUR_PRES_X : 0(0bar)~4096(210bar) //1400 |
Lightvalve | 53:b25725257569 | 1469 | } |
Lightvalve | 56:2eade98630e2 | 1470 | |
Lightvalve | 55:f4819de54e7a | 1471 | //If it doesn't rest, below can can not work. |
Lightvalve | 55:f4819de54e7a | 1472 | for (can_rest = 0; can_rest < 10000; can_rest++) { |
Lightvalve | 55:f4819de54e7a | 1473 | ; |
Lightvalve | 55:f4819de54e7a | 1474 | } |
Lightvalve | 56:2eade98630e2 | 1475 | |
Lightvalve | 54:6f50d9d3bfee | 1476 | if (flag_data_request[3] == HIGH) { |
Lightvalve | 52:647072f5307a | 1477 | //PWM |
Lightvalve | 55:f4819de54e7a | 1478 | CAN_TX_PWM((int16_t) cur.sen); //1500 |
Lightvalve | 52:647072f5307a | 1479 | } |
Lightvalve | 55:f4819de54e7a | 1480 | //for (i = 0; i < 10000; i++) { |
Lightvalve | 55:f4819de54e7a | 1481 | // ; |
Lightvalve | 55:f4819de54e7a | 1482 | // } |
Lightvalve | 54:6f50d9d3bfee | 1483 | if (flag_data_request[4] == HIGH) { |
Lightvalve | 52:647072f5307a | 1484 | //valve position |
Lightvalve | 55:f4819de54e7a | 1485 | CAN_TX_VALVE_POSITION((int16_t) (CAN_FREQ), (int16_t) (D_DAMPER), (int16_t) OPERATING_MODE); //1600 |
Lightvalve | 52:647072f5307a | 1486 | } |
Lightvalve | 20:806196fda269 | 1487 | |
Lightvalve | 52:647072f5307a | 1488 | // Others : Reference position, Reference FT, PWM, Current (ID:1300) |
Lightvalve | 51:8ea76864368a | 1489 | // if (flag_data_request[1] == HIGH) { |
Lightvalve | 51:8ea76864368a | 1490 | // CAN_TX_SOMETHING((int) (FORCE_VREF), (int16_t) (1), (int16_t) (2), (int16_t) (3)); |
Lightvalve | 51:8ea76864368a | 1491 | // } |
Lightvalve | 52:647072f5307a | 1492 | //if (flag_delay_test == 1){ |
Lightvalve | 56:2eade98630e2 | 1493 | //CAN_TX_PRES((int16_t) (0),(int16_t) torq_ref); |
Lightvalve | 52:647072f5307a | 1494 | //} |
Lightvalve | 51:8ea76864368a | 1495 | |
Lightvalve | 52:647072f5307a | 1496 | TMR2_COUNT_CAN_TX = 0; |
Lightvalve | 52:647072f5307a | 1497 | } |
Lightvalve | 52:647072f5307a | 1498 | TMR2_COUNT_CAN_TX++; |
Lightvalve | 51:8ea76864368a | 1499 | |
Lightvalve | 20:806196fda269 | 1500 | } |
Lightvalve | 51:8ea76864368a | 1501 | TIM3->SR = 0x0; // reset the status register |
Lightvalve | 51:8ea76864368a | 1502 | |
Lightvalve | 56:2eade98630e2 | 1503 | } |