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