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