![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
For LIGHT
main.cpp@66:d385a08132d7, 2020-05-21 (annotated)
- Committer:
- Lightvalve
- Date:
- Thu May 21 02:08:49 2020 +0000
- Revision:
- 66:d385a08132d7
- Parent:
- 65:443a5f0e8ee1
- Child:
- 67:cfc88fd0591e
homepos i_inte delete
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 | 66:d385a08132d7 | 618 | I_ERR_INT = 0.0f; |
Lightvalve | 29:69f3f5445d6d | 619 | FINDHOME_STAGE = FINDHOME_GOTOLIMIT; |
Lightvalve | 29:69f3f5445d6d | 620 | } else if (FINDHOME_STAGE == FINDHOME_GOTOLIMIT) { |
Lightvalve | 62:851cf7b7aa7a | 621 | int cnt_check_enc = (TMR_FREQ_5k/20); |
Lightvalve | 29:69f3f5445d6d | 622 | if(cnt_findhome%cnt_check_enc == 0) { |
Lightvalve | 29:69f3f5445d6d | 623 | FINDHOME_POSITION = pos.sen; |
Lightvalve | 29:69f3f5445d6d | 624 | FINDHOME_VELOCITY = FINDHOME_POSITION - FINDHOME_POSITION_OLD; |
Lightvalve | 29:69f3f5445d6d | 625 | FINDHOME_POSITION_OLD = FINDHOME_POSITION; |
Lightvalve | 29:69f3f5445d6d | 626 | } |
Lightvalve | 29:69f3f5445d6d | 627 | cnt_findhome++; |
Lightvalve | 14:8e7590227d22 | 628 | |
Lightvalve | 29:69f3f5445d6d | 629 | if (abs(FINDHOME_VELOCITY) <= 1) { |
Lightvalve | 29:69f3f5445d6d | 630 | cnt_vel_findhome = cnt_vel_findhome + 1; |
Lightvalve | 29:69f3f5445d6d | 631 | } else { |
Lightvalve | 29:69f3f5445d6d | 632 | cnt_vel_findhome = 0; |
Lightvalve | 29:69f3f5445d6d | 633 | } |
Lightvalve | 19:23b7c1ad8683 | 634 | |
Lightvalve | 62:851cf7b7aa7a | 635 | if ((cnt_vel_findhome < 3*TMR_FREQ_5k) && cnt_findhome < 10*TMR_FREQ_5k) { // wait for 3sec |
Lightvalve | 29:69f3f5445d6d | 636 | //REFERENCE_MODE = MODE_REF_NO_ACT; |
Lightvalve | 62:851cf7b7aa7a | 637 | if (HOMEPOS_OFFSET > 0) pos.ref = pos.ref + 12.0f; |
Lightvalve | 62:851cf7b7aa7a | 638 | else pos.ref = pos.ref - 12.0f; |
Lightvalve | 34:bb2ca2fc2a8e | 639 | |
Lightvalve | 66:d385a08132d7 | 640 | pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm] |
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 temp_vel_pos = 0.0f; |
Lightvalve | 65:443a5f0e8ee1 | 646 | double wn_Pos = 2.0f * PI * 5.0f; // f_cut : 5Hz Position Control |
Lightvalve | 65:443a5f0e8ee1 | 647 | |
Lightvalve | 65:443a5f0e8ee1 | 648 | if ((OPERATING_MODE && 0x01) == 0) { // Rotary Mode |
Lightvalve | 66:d385a08132d7 | 649 | temp_vel_pos = 0.01f * (double) P_GAIN_JOINT_POSITION * wn_Pos * pos.err; // rad/s |
Lightvalve | 65:443a5f0e8ee1 | 650 | } else if ((OPERATING_MODE && 0x01) == 1) { |
Lightvalve | 66:d385a08132d7 | 651 | temp_vel_pos = 0.01f * (double) P_GAIN_JOINT_POSITION * wn_Pos * pos.err; // mm/s |
Lightvalve | 65:443a5f0e8ee1 | 652 | } |
Lightvalve | 65:443a5f0e8ee1 | 653 | 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 | 654 | else I_REF_POS = temp_vel_pos * ((double) PISTON_AREA_B * 0.00006f / (K_v * sqrt(2.0f / (alpha3 + 1.0f)))); |
Lightvalve | 65:443a5f0e8ee1 | 655 | |
Lightvalve | 65:443a5f0e8ee1 | 656 | I_REF = I_REF_POS; |
Lightvalve | 66:d385a08132d7 | 657 | I_ERR_INT = 0.0f; |
Lightvalve | 66:d385a08132d7 | 658 | |
Lightvalve | 66:d385a08132d7 | 659 | |
Lightvalve | 66:d385a08132d7 | 660 | } else { |
Lightvalve | 66:d385a08132d7 | 661 | float VALVE_POS_RAW_FORCE_FB = 0.0f; |
Lightvalve | 66:d385a08132d7 | 662 | 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 | 66:d385a08132d7 | 663 | |
Lightvalve | 66:d385a08132d7 | 664 | if (VALVE_POS_RAW_FORCE_FB >= 0) { |
Lightvalve | 66:d385a08132d7 | 665 | valve_pos.ref = VALVE_POS_RAW_FORCE_FB + VALVE_DEADZONE_PLUS; |
Lightvalve | 66:d385a08132d7 | 666 | } else { |
Lightvalve | 66:d385a08132d7 | 667 | valve_pos.ref = VALVE_POS_RAW_FORCE_FB + VALVE_DEADZONE_MINUS; |
Lightvalve | 66:d385a08132d7 | 668 | } |
Lightvalve | 66:d385a08132d7 | 669 | |
Lightvalve | 66:d385a08132d7 | 670 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 66:d385a08132d7 | 671 | |
Lightvalve | 66:d385a08132d7 | 672 | V_out = (float) Vout.ref; |
Lightvalve | 66:d385a08132d7 | 673 | |
Lightvalve | 66:d385a08132d7 | 674 | } |
Lightvalve | 66:d385a08132d7 | 675 | |
Lightvalve | 66:d385a08132d7 | 676 | |
Lightvalve | 66:d385a08132d7 | 677 | |
Lightvalve | 66:d385a08132d7 | 678 | // pos.err = pos.ref_home_pos - pos.sen; |
Lightvalve | 66:d385a08132d7 | 679 | // float VALVE_POS_RAW_POS_FB = 0.0f; |
Lightvalve | 66:d385a08132d7 | 680 | // VALVE_POS_RAW_POS_FB = (float) P_GAIN_JOINT_POSITION * pos.err/(float) ENC_PULSE_PER_POSITION * 0.01f; |
Lightvalve | 66:d385a08132d7 | 681 | // valve_pos.ref = VALVE_POS_RAW_POS_FB + (float) VALVE_CENTER; |
Lightvalve | 66:d385a08132d7 | 682 | // VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 66:d385a08132d7 | 683 | |
Lightvalve | 66:d385a08132d7 | 684 | cnt_findhome_wait = 0; |
Lightvalve | 66:d385a08132d7 | 685 | // CONTROL_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 66:d385a08132d7 | 686 | alpha_trans = 0.0f; |
Lightvalve | 66:d385a08132d7 | 687 | |
Lightvalve | 66:d385a08132d7 | 688 | |
Lightvalve | 66:d385a08132d7 | 689 | } else { |
Lightvalve | 66:d385a08132d7 | 690 | ENC_SET(HOMEPOS_OFFSET); |
Lightvalve | 66:d385a08132d7 | 691 | // ENC_SET_ZERO(); |
Lightvalve | 66:d385a08132d7 | 692 | INIT_REF_POS = HOMEPOS_OFFSET; |
Lightvalve | 66:d385a08132d7 | 693 | REF_POSITION = 0; |
Lightvalve | 66:d385a08132d7 | 694 | REF_VELOCITY = 0; |
Lightvalve | 66:d385a08132d7 | 695 | FINDHOME_POSITION = 0; |
Lightvalve | 66:d385a08132d7 | 696 | FINDHOME_POSITION_OLD = 0; |
Lightvalve | 66:d385a08132d7 | 697 | FINDHOME_VELOCITY = 0; |
Lightvalve | 66:d385a08132d7 | 698 | |
Lightvalve | 66:d385a08132d7 | 699 | cnt_findhome = 0; |
Lightvalve | 66:d385a08132d7 | 700 | cnt_vel_findhome = 0; |
Lightvalve | 66:d385a08132d7 | 701 | FINDHOME_STAGE = FINDHOME_ZEROPOSE; |
Lightvalve | 66:d385a08132d7 | 702 | |
Lightvalve | 66:d385a08132d7 | 703 | pos.ref = 0.0f; |
Lightvalve | 66:d385a08132d7 | 704 | vel.ref = 0.0f; |
Lightvalve | 66:d385a08132d7 | 705 | pos.ref_home_pos = 0.0f; |
Lightvalve | 66:d385a08132d7 | 706 | vel.ref_home_pos = 0.0f; |
Lightvalve | 66:d385a08132d7 | 707 | |
Lightvalve | 66:d385a08132d7 | 708 | I_ERR_INT = 0.0f; |
Lightvalve | 66:d385a08132d7 | 709 | |
Lightvalve | 66:d385a08132d7 | 710 | // cnt_findhome_wait = 0; |
Lightvalve | 66:d385a08132d7 | 711 | // FINDHOME_STAGE = FINDHOME_INIT; |
Lightvalve | 66:d385a08132d7 | 712 | // CONTROL_UTILITY_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 66:d385a08132d7 | 713 | } |
Lightvalve | 66:d385a08132d7 | 714 | |
Lightvalve | 66:d385a08132d7 | 715 | // } else if (FINDHOME_STAGE == FINDHOME_WAIT) { |
Lightvalve | 66:d385a08132d7 | 716 | // cnt_findhome_wait = cnt_findhome_wait + 1; |
Lightvalve | 66:d385a08132d7 | 717 | // if(cnt_findhome_wait > 3*TMR_FREQ_5k){ |
Lightvalve | 66:d385a08132d7 | 718 | // FINDHOME_STAGE = FINDHOME_ZEROPOSE; |
Lightvalve | 66:d385a08132d7 | 719 | // pos.sen = 0.0f; |
Lightvalve | 66:d385a08132d7 | 720 | // vel.sen = 0.0f; |
Lightvalve | 66:d385a08132d7 | 721 | // } |
Lightvalve | 66:d385a08132d7 | 722 | |
Lightvalve | 66:d385a08132d7 | 723 | } else if (FINDHOME_STAGE == FINDHOME_ZEROPOSE) { |
Lightvalve | 66:d385a08132d7 | 724 | int T_move = 2*TMR_FREQ_5k; |
Lightvalve | 66:d385a08132d7 | 725 | 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 | 66:d385a08132d7 | 726 | vel.ref = 0.0f; |
Lightvalve | 66:d385a08132d7 | 727 | |
Lightvalve | 66:d385a08132d7 | 728 | // input for position control |
Lightvalve | 66:d385a08132d7 | 729 | |
Lightvalve | 66:d385a08132d7 | 730 | // CONTROL_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 66:d385a08132d7 | 731 | pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm] |
Lightvalve | 66:d385a08132d7 | 732 | |
Lightvalve | 66:d385a08132d7 | 733 | if (((OPERATING_MODE&0b110)>>1) == 0 || ((OPERATING_MODE&0b110)>>1) == 1) { |
Lightvalve | 66:d385a08132d7 | 734 | |
Lightvalve | 66:d385a08132d7 | 735 | double I_REF_POS = 0.0f; |
Lightvalve | 66:d385a08132d7 | 736 | |
Lightvalve | 66:d385a08132d7 | 737 | double temp_vel_pos = 0.0f; |
Lightvalve | 66:d385a08132d7 | 738 | double wn_Pos = 2.0f * PI * 5.0f; // f_cut : 5Hz Position Control |
Lightvalve | 66:d385a08132d7 | 739 | |
Lightvalve | 66:d385a08132d7 | 740 | if ((OPERATING_MODE && 0x01) == 0) { // Rotary Mode |
Lightvalve | 66:d385a08132d7 | 741 | temp_vel_pos = 0.01f * (double) P_GAIN_JOINT_POSITION * wn_Pos * pos.err; // rad/s |
Lightvalve | 66:d385a08132d7 | 742 | } else if ((OPERATING_MODE && 0x01) == 1) { |
Lightvalve | 66:d385a08132d7 | 743 | temp_vel_pos = 0.01f * (double) P_GAIN_JOINT_POSITION * wn_Pos * pos.err; // mm/s |
Lightvalve | 66:d385a08132d7 | 744 | } |
Lightvalve | 66:d385a08132d7 | 745 | 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 | 66:d385a08132d7 | 746 | else I_REF_POS = temp_vel_pos * ((double) PISTON_AREA_B * 0.00006f / (K_v * sqrt(2.0f / (alpha3 + 1.0f)))); |
Lightvalve | 66:d385a08132d7 | 747 | |
Lightvalve | 66:d385a08132d7 | 748 | I_REF = I_REF_POS; |
Lightvalve | 66:d385a08132d7 | 749 | I_ERR_INT = 0.0f; |
Lightvalve | 65:443a5f0e8ee1 | 750 | |
Lightvalve | 65:443a5f0e8ee1 | 751 | |
Lightvalve | 65:443a5f0e8ee1 | 752 | |
Lightvalve | 65:443a5f0e8ee1 | 753 | } else { |
Lightvalve | 65:443a5f0e8ee1 | 754 | float VALVE_POS_RAW_FORCE_FB = 0.0f; |
Lightvalve | 65:443a5f0e8ee1 | 755 | 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 | 756 | |
Lightvalve | 65:443a5f0e8ee1 | 757 | if (VALVE_POS_RAW_FORCE_FB >= 0) { |
Lightvalve | 65:443a5f0e8ee1 | 758 | valve_pos.ref = VALVE_POS_RAW_FORCE_FB + VALVE_DEADZONE_PLUS; |
Lightvalve | 65:443a5f0e8ee1 | 759 | } else { |
Lightvalve | 65:443a5f0e8ee1 | 760 | valve_pos.ref = VALVE_POS_RAW_FORCE_FB + VALVE_DEADZONE_MINUS; |
Lightvalve | 65:443a5f0e8ee1 | 761 | } |
Lightvalve | 65:443a5f0e8ee1 | 762 | |
Lightvalve | 65:443a5f0e8ee1 | 763 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 65:443a5f0e8ee1 | 764 | |
Lightvalve | 65:443a5f0e8ee1 | 765 | V_out = (float) Vout.ref; |
Lightvalve | 65:443a5f0e8ee1 | 766 | |
Lightvalve | 65:443a5f0e8ee1 | 767 | } |
Lightvalve | 62:851cf7b7aa7a | 768 | |
Lightvalve | 62:851cf7b7aa7a | 769 | // pos.err = pos.ref - (float)pos.sen; |
Lightvalve | 62:851cf7b7aa7a | 770 | // float VALVE_POS_RAW_POS_FB = 0.0f; |
Lightvalve | 62:851cf7b7aa7a | 771 | // VALVE_POS_RAW_POS_FB = (float) P_GAIN_JOINT_POSITION * 0.01f * pos.err/(float) ENC_PULSE_PER_POSITION; |
Lightvalve | 62:851cf7b7aa7a | 772 | // valve_pos.ref = VALVE_POS_RAW_POS_FB + (float) VALVE_CENTER; |
Lightvalve | 62:851cf7b7aa7a | 773 | // VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 29:69f3f5445d6d | 774 | |
Lightvalve | 29:69f3f5445d6d | 775 | cnt_findhome++; |
Lightvalve | 29:69f3f5445d6d | 776 | if (cnt_findhome >= T_move) { |
Lightvalve | 29:69f3f5445d6d | 777 | //REFERENCE_MODE = MODE_REF_DIRECT; |
Lightvalve | 29:69f3f5445d6d | 778 | cnt_findhome = 0; |
Lightvalve | 30:8d561f16383b | 779 | pos.ref = 0.0f; |
Lightvalve | 30:8d561f16383b | 780 | vel.ref = 0.0f; |
Lightvalve | 30:8d561f16383b | 781 | pos.ref_home_pos = 0.0f; |
Lightvalve | 30:8d561f16383b | 782 | vel.ref_home_pos = 0.0f; |
Lightvalve | 29:69f3f5445d6d | 783 | FINDHOME_STAGE = FINDHOME_INIT; |
Lightvalve | 62:851cf7b7aa7a | 784 | CONTROL_UTILITY_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 29:69f3f5445d6d | 785 | } |
Lightvalve | 13:747daba9cf59 | 786 | } |
Lightvalve | 19:23b7c1ad8683 | 787 | |
Lightvalve | 13:747daba9cf59 | 788 | break; |
Lightvalve | 13:747daba9cf59 | 789 | } |
Lightvalve | 14:8e7590227d22 | 790 | |
Lightvalve | 50:3c630b5eba9f | 791 | // case MODE_VALVE_GAIN_SETTING: { |
Lightvalve | 50:3c630b5eba9f | 792 | // if (TMR3_COUNT_FLOWRATE == 0) { |
Lightvalve | 50:3c630b5eba9f | 793 | // if (pos_plus_end == pos_minus_end) need_enc_init = true; |
Lightvalve | 50:3c630b5eba9f | 794 | // else { |
Lightvalve | 50:3c630b5eba9f | 795 | // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 50:3c630b5eba9f | 796 | // temp_time = (int) (0.5f * (float) TMR_FREQ_5k); |
Lightvalve | 50:3c630b5eba9f | 797 | // } |
Lightvalve | 50:3c630b5eba9f | 798 | // } |
Lightvalve | 50:3c630b5eba9f | 799 | // if (need_enc_init) { |
Lightvalve | 50:3c630b5eba9f | 800 | // if (TMR3_COUNT_FLOWRATE < (int) (0.5f * (float) TMR_FREQ_5k)) { |
Lightvalve | 50:3c630b5eba9f | 801 | // V_out = VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 50:3c630b5eba9f | 802 | // pos_plus_end = pos.sen; |
Lightvalve | 50:3c630b5eba9f | 803 | // } else if (TMR3_COUNT_FLOWRATE < TMR_FREQ_5k) { |
Lightvalve | 50:3c630b5eba9f | 804 | // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 50:3c630b5eba9f | 805 | // pos_minus_end = pos.sen; |
Lightvalve | 50:3c630b5eba9f | 806 | // } else if (TMR3_COUNT_FLOWRATE == TMR_FREQ_5k) { |
Lightvalve | 50:3c630b5eba9f | 807 | // need_enc_init = false; |
Lightvalve | 50:3c630b5eba9f | 808 | // check_vel_pos_init = (int) (0.9f * (float) (pos_plus_end - pos_minus_end)); |
Lightvalve | 50:3c630b5eba9f | 809 | // check_vel_pos_fin = (int) (0.95f * (float) (pos_plus_end - pos_minus_end)); |
Lightvalve | 50:3c630b5eba9f | 810 | // check_vel_pos_interv = check_vel_pos_fin - check_vel_pos_init; |
Lightvalve | 50:3c630b5eba9f | 811 | // } |
Lightvalve | 50:3c630b5eba9f | 812 | // temp_time = TMR_FREQ_5k; |
Lightvalve | 50:3c630b5eba9f | 813 | // } |
Lightvalve | 50:3c630b5eba9f | 814 | // TMR3_COUNT_FLOWRATE++; |
Lightvalve | 50:3c630b5eba9f | 815 | // if (TMR3_COUNT_FLOWRATE > temp_time) { |
Lightvalve | 50:3c630b5eba9f | 816 | // if (flag_flowrate % 2 == 0) { // (+) |
Lightvalve | 50:3c630b5eba9f | 817 | // VALVE_VOLTAGE = 1000.0f * (float) (flag_flowrate / 2 + 1); |
Lightvalve | 50:3c630b5eba9f | 818 | // V_out = VALVE_VOLTAGE; |
Lightvalve | 50:3c630b5eba9f | 819 | // if (pos.sen > (pos_minus_end + check_vel_pos_init) && pos.sen < (pos_minus_end + check_vel_pos_fin)) { |
Lightvalve | 50:3c630b5eba9f | 820 | // fl_temp_cnt++; |
Lightvalve | 50:3c630b5eba9f | 821 | // } else if (pos.sen >= (pos_minus_end + check_vel_pos_fin) && CUR_VELOCITY == 0) { |
Lightvalve | 50:3c630b5eba9f | 822 | // 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 | 823 | // // 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 | 824 | // fl_temp_cnt2++; |
Lightvalve | 50:3c630b5eba9f | 825 | // } |
Lightvalve | 50:3c630b5eba9f | 826 | // } else if (flag_flowrate % 2 == 1) { // (-) |
Lightvalve | 50:3c630b5eba9f | 827 | // VALVE_VOLTAGE = -1. * (float) (flag_flowrate / 2 + 1); |
Lightvalve | 50:3c630b5eba9f | 828 | // V_out = VALVE_VOLTAGE; |
Lightvalve | 50:3c630b5eba9f | 829 | // if (pos.sen < (pos_plus_end - check_vel_pos_init) && pos.sen > (pos_plus_end - check_vel_pos_fin)) { |
Lightvalve | 50:3c630b5eba9f | 830 | // fl_temp_cnt++; |
Lightvalve | 50:3c630b5eba9f | 831 | // } else if (pos.sen <= (pos_plus_end - check_vel_pos_fin) && CUR_VELOCITY == 0) { |
Lightvalve | 50:3c630b5eba9f | 832 | // 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 | 833 | // // 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 | 834 | // fl_temp_cnt2++; |
Lightvalve | 50:3c630b5eba9f | 835 | // } |
Lightvalve | 50:3c630b5eba9f | 836 | // } |
Lightvalve | 50:3c630b5eba9f | 837 | // if (fl_temp_cnt2 == 100) { |
Lightvalve | 50:3c630b5eba9f | 838 | // |
Lightvalve | 50:3c630b5eba9f | 839 | // ROM_RESET_DATA(); |
Lightvalve | 50:3c630b5eba9f | 840 | // |
Lightvalve | 50:3c630b5eba9f | 841 | // //spi_eeprom_write(RID_VALVE_GAIN_PLUS_1 + flag_flowrate, (int16_t) (VALVE_GAIN_LPM_PER_V[flag_flowrate] * 100.0f)); |
Lightvalve | 50:3c630b5eba9f | 842 | // cur_vel_sum = 0; |
Lightvalve | 50:3c630b5eba9f | 843 | // fl_temp_cnt = 0; |
Lightvalve | 50:3c630b5eba9f | 844 | // fl_temp_cnt2 = 0; |
Lightvalve | 50:3c630b5eba9f | 845 | // flag_flowrate++; |
Lightvalve | 50:3c630b5eba9f | 846 | // } |
Lightvalve | 50:3c630b5eba9f | 847 | // if (flag_flowrate == 10) { |
Lightvalve | 50:3c630b5eba9f | 848 | // V_out = 0; |
Lightvalve | 50:3c630b5eba9f | 849 | // flag_flowrate = 0; |
Lightvalve | 50:3c630b5eba9f | 850 | // TMR3_COUNT_FLOWRATE = 0; |
Lightvalve | 50:3c630b5eba9f | 851 | // valve_gain_repeat_cnt++; |
Lightvalve | 50:3c630b5eba9f | 852 | // if (valve_gain_repeat_cnt >= 1) { |
Lightvalve | 50:3c630b5eba9f | 853 | // CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 50:3c630b5eba9f | 854 | // valve_gain_repeat_cnt = 0; |
Lightvalve | 50:3c630b5eba9f | 855 | // } |
Lightvalve | 50:3c630b5eba9f | 856 | // |
Lightvalve | 50:3c630b5eba9f | 857 | // } |
Lightvalve | 50:3c630b5eba9f | 858 | // break; |
Lightvalve | 50:3c630b5eba9f | 859 | // } |
Lightvalve | 50:3c630b5eba9f | 860 | // |
Lightvalve | 50:3c630b5eba9f | 861 | // } |
Lightvalve | 14:8e7590227d22 | 862 | case MODE_PRESSURE_SENSOR_NULLING: { |
Lightvalve | 13:747daba9cf59 | 863 | // DAC Voltage reference set |
Lightvalve | 14:8e7590227d22 | 864 | if (TMR3_COUNT_PRES_NULL < TMR_FREQ_5k * 2) { |
Lightvalve | 14:8e7590227d22 | 865 | CUR_PRES_A_sum += pres_A.sen; |
Lightvalve | 14:8e7590227d22 | 866 | CUR_PRES_B_sum += pres_B.sen; |
Lightvalve | 14:8e7590227d22 | 867 | |
Lightvalve | 14:8e7590227d22 | 868 | if (TMR3_COUNT_PRES_NULL % 10 == 0) { |
Lightvalve | 30:8d561f16383b | 869 | CUR_PRES_A_mean = CUR_PRES_A_sum / 10.0f; |
Lightvalve | 30:8d561f16383b | 870 | CUR_PRES_B_mean = CUR_PRES_B_sum / 10.0f; |
Lightvalve | 13:747daba9cf59 | 871 | CUR_PRES_A_sum = 0; |
Lightvalve | 13:747daba9cf59 | 872 | CUR_PRES_B_sum = 0; |
Lightvalve | 14:8e7590227d22 | 873 | |
Lightvalve | 38:118df027d851 | 874 | float VREF_NullingGain = 0.0003f; |
Lightvalve | 62:851cf7b7aa7a | 875 | PRES_A_VREF = PRES_A_VREF + VREF_NullingGain * CUR_PRES_A_mean; |
Lightvalve | 62:851cf7b7aa7a | 876 | PRES_B_VREF = PRES_B_VREF + VREF_NullingGain * CUR_PRES_B_mean; |
Lightvalve | 14:8e7590227d22 | 877 | |
Lightvalve | 30:8d561f16383b | 878 | if (PRES_A_VREF > 3.3f) PRES_A_VREF = 3.3f; |
Lightvalve | 30:8d561f16383b | 879 | if (PRES_A_VREF < 0.0f) PRES_A_VREF = 0.0f; |
Lightvalve | 30:8d561f16383b | 880 | if (PRES_B_VREF > 3.3f) PRES_B_VREF = 3.3f; |
Lightvalve | 30:8d561f16383b | 881 | if (PRES_B_VREF < 0.0f) PRES_B_VREF = 0.0f; |
Lightvalve | 19:23b7c1ad8683 | 882 | |
Lightvalve | 30:8d561f16383b | 883 | dac_1 = PRES_A_VREF / 3.3f; |
Lightvalve | 30:8d561f16383b | 884 | dac_2 = PRES_B_VREF / 3.3f; |
Lightvalve | 13:747daba9cf59 | 885 | } |
Lightvalve | 13:747daba9cf59 | 886 | } else { |
Lightvalve | 62:851cf7b7aa7a | 887 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 14:8e7590227d22 | 888 | TMR3_COUNT_PRES_NULL = 0; |
Lightvalve | 13:747daba9cf59 | 889 | CUR_PRES_A_sum = 0; |
Lightvalve | 13:747daba9cf59 | 890 | CUR_PRES_B_sum = 0; |
Lightvalve | 13:747daba9cf59 | 891 | CUR_PRES_A_mean = 0; |
Lightvalve | 13:747daba9cf59 | 892 | CUR_PRES_B_mean = 0; |
Lightvalve | 14:8e7590227d22 | 893 | |
Lightvalve | 16:903b5a4433b4 | 894 | ROM_RESET_DATA(); |
Lightvalve | 19:23b7c1ad8683 | 895 | |
Lightvalve | 30:8d561f16383b | 896 | dac_1 = PRES_A_VREF / 3.3f; |
Lightvalve | 30:8d561f16383b | 897 | dac_2 = PRES_B_VREF / 3.3f; |
Lightvalve | 17:1865016ca2e7 | 898 | //pc.printf("nulling end"); |
Lightvalve | 13:747daba9cf59 | 899 | } |
Lightvalve | 14:8e7590227d22 | 900 | TMR3_COUNT_PRES_NULL++; |
Lightvalve | 13:747daba9cf59 | 901 | break; |
Lightvalve | 13:747daba9cf59 | 902 | } |
Lightvalve | 14:8e7590227d22 | 903 | |
Lightvalve | 50:3c630b5eba9f | 904 | // case MODE_PRESSURE_SENSOR_CALIB: { |
Lightvalve | 50:3c630b5eba9f | 905 | // if (TMR3_COUNT_PRES_CALIB < 2 * TMR_FREQ_5k) { |
Lightvalve | 50:3c630b5eba9f | 906 | // V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 50:3c630b5eba9f | 907 | // if (TMR3_COUNT_PRES_CALIB >= TMR_FREQ_5k) { |
Lightvalve | 50:3c630b5eba9f | 908 | // CUR_PRES_A_sum += CUR_PRES_A; |
Lightvalve | 50:3c630b5eba9f | 909 | // } |
Lightvalve | 50:3c630b5eba9f | 910 | // } else if (TMR3_COUNT_PRES_CALIB < 4 * TMR_FREQ_5k) { |
Lightvalve | 50:3c630b5eba9f | 911 | // V_out = VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 50:3c630b5eba9f | 912 | // if (TMR3_COUNT_PRES_CALIB >= 3 * TMR_FREQ_5k) { |
Lightvalve | 50:3c630b5eba9f | 913 | // CUR_PRES_B_sum += CUR_PRES_B; |
Lightvalve | 50:3c630b5eba9f | 914 | // } |
Lightvalve | 50:3c630b5eba9f | 915 | // } else { |
Lightvalve | 50:3c630b5eba9f | 916 | // CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 50:3c630b5eba9f | 917 | // TMR3_COUNT_PRES_CALIB = 0; |
Lightvalve | 50:3c630b5eba9f | 918 | // V_out = 0; |
Lightvalve | 50:3c630b5eba9f | 919 | // PRES_SENSOR_A_PULSE_PER_BAR = CUR_PRES_A_sum / ((float) TMR_FREQ_5k - 1.0f) - PRES_A_NULL; |
Lightvalve | 50:3c630b5eba9f | 920 | // PRES_SENSOR_A_PULSE_PER_BAR = PRES_SENSOR_A_PULSE_PER_BAR / ((float) PRES_SUPPLY - 1.0f); |
Lightvalve | 50:3c630b5eba9f | 921 | // PRES_SENSOR_B_PULSE_PER_BAR = CUR_PRES_B_sum / ((float) TMR_FREQ_5k - 1.0f) - PRES_B_NULL; |
Lightvalve | 50:3c630b5eba9f | 922 | // PRES_SENSOR_B_PULSE_PER_BAR = PRES_SENSOR_B_PULSE_PER_BAR / ((float) PRES_SUPPLY - 1.0f); |
Lightvalve | 50:3c630b5eba9f | 923 | // CUR_PRES_A_sum = 0; |
Lightvalve | 50:3c630b5eba9f | 924 | // CUR_PRES_B_sum = 0; |
Lightvalve | 50:3c630b5eba9f | 925 | // CUR_PRES_A_mean = 0; |
Lightvalve | 50:3c630b5eba9f | 926 | // CUR_PRES_B_mean = 0; |
Lightvalve | 50:3c630b5eba9f | 927 | // |
Lightvalve | 50:3c630b5eba9f | 928 | // ROM_RESET_DATA(); |
Lightvalve | 50:3c630b5eba9f | 929 | // |
Lightvalve | 50:3c630b5eba9f | 930 | // //spi_eeprom_write(RID_PRES_SENSOR_A_PULSE_PER_BAR, (int16_t) (PRES_SENSOR_A_PULSE_PER_BAR * 100.0f)); |
Lightvalve | 50:3c630b5eba9f | 931 | // //spi_eeprom_write(RID_PRES_SENSOR_B_PULSE_PER_BAR, (int16_t) (PRES_SENSOR_B_PULSE_PER_BAR * 100.0f)); |
Lightvalve | 50:3c630b5eba9f | 932 | // } |
Lightvalve | 50:3c630b5eba9f | 933 | // TMR3_COUNT_PRES_CALIB++; |
Lightvalve | 50:3c630b5eba9f | 934 | // break; |
Lightvalve | 50:3c630b5eba9f | 935 | // } |
Lightvalve | 19:23b7c1ad8683 | 936 | |
Lightvalve | 50:3c630b5eba9f | 937 | // case MODE_ROTARY_FRICTION_TUNING: { |
Lightvalve | 50:3c630b5eba9f | 938 | // 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 | 939 | // V_out = PWM_out * sin(2 * 3.14159f * freq_fric_tune * TMR3_COUNT_ROTARY_FRIC_TUNE * 0.0001f); |
Lightvalve | 50:3c630b5eba9f | 940 | // if (V_out > 0) V_out = VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 50:3c630b5eba9f | 941 | // else V_out = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 50:3c630b5eba9f | 942 | // TMR3_COUNT_ROTARY_FRIC_TUNE++; |
Lightvalve | 50:3c630b5eba9f | 943 | // if (TMR3_COUNT_ROTARY_FRIC_TUNE > TUNING_TIME * TMR_FREQ_5k) { |
Lightvalve | 50:3c630b5eba9f | 944 | // TMR3_COUNT_ROTARY_FRIC_TUNE = 0; |
Lightvalve | 50:3c630b5eba9f | 945 | // V_out = 0.0f; |
Lightvalve | 50:3c630b5eba9f | 946 | // CONTROL_MODE = MODE_NO_ACT; |
Lightvalve | 50:3c630b5eba9f | 947 | // } |
Lightvalve | 50:3c630b5eba9f | 948 | // break; |
Lightvalve | 50:3c630b5eba9f | 949 | // } |
Lightvalve | 14:8e7590227d22 | 950 | |
Lightvalve | 14:8e7590227d22 | 951 | case MODE_DDV_POS_VS_PWM_ID: { |
Lightvalve | 62:851cf7b7aa7a | 952 | CONTROL_MODE = MODE_VALVE_OPEN_LOOP; |
Lightvalve | 13:747daba9cf59 | 953 | VALVE_ID_timer = VALVE_ID_timer + 1; |
Lightvalve | 14:8e7590227d22 | 954 | |
Lightvalve | 14:8e7590227d22 | 955 | if(VALVE_ID_timer < TMR_FREQ_5k*1) { |
Lightvalve | 62:851cf7b7aa7a | 956 | Vout.ref = 3000.0f * sin(2.0f*3.14f*VALVE_ID_timer/TMR_FREQ_5k * 100.0f); |
Lightvalve | 14:8e7590227d22 | 957 | } else if(VALVE_ID_timer < TMR_FREQ_5k*2) { |
Lightvalve | 62:851cf7b7aa7a | 958 | Vout.ref = 1000.0f*(ID_index_array[ID_index]); |
Lightvalve | 14:8e7590227d22 | 959 | } else if(VALVE_ID_timer == TMR_FREQ_5k*2) { |
Lightvalve | 13:747daba9cf59 | 960 | VALVE_POS_TMP = 0; |
Lightvalve | 13:747daba9cf59 | 961 | data_num = 0; |
Lightvalve | 14:8e7590227d22 | 962 | } else if(VALVE_ID_timer < TMR_FREQ_5k*3) { |
Lightvalve | 13:747daba9cf59 | 963 | data_num = data_num + 1; |
Lightvalve | 13:747daba9cf59 | 964 | VALVE_POS_TMP = VALVE_POS_TMP + value; |
Lightvalve | 14:8e7590227d22 | 965 | } else if(VALVE_ID_timer == TMR_FREQ_5k*3) { |
Lightvalve | 62:851cf7b7aa7a | 966 | Vout.ref = 0.0f; |
Lightvalve | 62:851cf7b7aa7a | 967 | } else { |
Lightvalve | 13:747daba9cf59 | 968 | VALVE_POS_AVG[ID_index] = VALVE_POS_TMP / data_num; |
Lightvalve | 13:747daba9cf59 | 969 | VALVE_ID_timer = 0; |
Lightvalve | 13:747daba9cf59 | 970 | ID_index= ID_index +1; |
Lightvalve | 13:747daba9cf59 | 971 | } |
Lightvalve | 14:8e7590227d22 | 972 | |
Lightvalve | 17:1865016ca2e7 | 973 | if(ID_index>=25) { |
Lightvalve | 13:747daba9cf59 | 974 | int i; |
Lightvalve | 13:747daba9cf59 | 975 | VALVE_POS_AVG_OLD = VALVE_POS_AVG[0]; |
Lightvalve | 17:1865016ca2e7 | 976 | for(i=0; i<25; i++) { |
Lightvalve | 13:747daba9cf59 | 977 | VALVE_POS_VS_PWM[i] = (int16_t) (VALVE_POS_AVG[i]); |
Lightvalve | 14:8e7590227d22 | 978 | if(VALVE_POS_AVG[i] > VALVE_POS_AVG_OLD) { |
Lightvalve | 13:747daba9cf59 | 979 | VALVE_MAX_POS = VALVE_POS_AVG[i]; |
Lightvalve | 13:747daba9cf59 | 980 | VALVE_POS_AVG_OLD = VALVE_MAX_POS; |
Lightvalve | 14:8e7590227d22 | 981 | } else if(VALVE_POS_AVG[i] < VALVE_POS_AVG_OLD) { |
Lightvalve | 13:747daba9cf59 | 982 | VALVE_MIN_POS = VALVE_POS_AVG[i]; |
Lightvalve | 13:747daba9cf59 | 983 | VALVE_POS_AVG_OLD = VALVE_MIN_POS; |
Lightvalve | 13:747daba9cf59 | 984 | } |
Lightvalve | 13:747daba9cf59 | 985 | } |
Lightvalve | 62:851cf7b7aa7a | 986 | ROM_RESET_DATA(); |
Lightvalve | 13:747daba9cf59 | 987 | ID_index = 0; |
Lightvalve | 62:851cf7b7aa7a | 988 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 13:747daba9cf59 | 989 | } |
Lightvalve | 14:8e7590227d22 | 990 | |
Lightvalve | 14:8e7590227d22 | 991 | |
Lightvalve | 13:747daba9cf59 | 992 | break; |
Lightvalve | 13:747daba9cf59 | 993 | } |
Lightvalve | 14:8e7590227d22 | 994 | |
Lightvalve | 14:8e7590227d22 | 995 | case MODE_DDV_DEADZONE_AND_CENTER: { |
Lightvalve | 62:851cf7b7aa7a | 996 | CONTROL_MODE = MODE_VALVE_OPEN_LOOP; |
Lightvalve | 13:747daba9cf59 | 997 | VALVE_DZ_timer = VALVE_DZ_timer + 1; |
Lightvalve | 14:8e7590227d22 | 998 | if(first_check == 0) { |
Lightvalve | 30:8d561f16383b | 999 | if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 62:851cf7b7aa7a | 1000 | Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 30:8d561f16383b | 1001 | } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 62:851cf7b7aa7a | 1002 | Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 14:8e7590227d22 | 1003 | pos_plus_end = pos.sen; |
Lightvalve | 30:8d561f16383b | 1004 | } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 62:851cf7b7aa7a | 1005 | Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 30:8d561f16383b | 1006 | } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 62:851cf7b7aa7a | 1007 | Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 14:8e7590227d22 | 1008 | pos_minus_end = pos.sen; |
Lightvalve | 30:8d561f16383b | 1009 | } else if(VALVE_DZ_timer < (int) (3.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 62:851cf7b7aa7a | 1010 | 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 | 1011 | } else if(VALVE_DZ_timer < (int) (4.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 62:851cf7b7aa7a | 1012 | 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 | 1013 | data_num = data_num + 1; |
Lightvalve | 13:747daba9cf59 | 1014 | VALVE_POS_TMP = VALVE_POS_TMP + value; |
Lightvalve | 30:8d561f16383b | 1015 | } else if(VALVE_DZ_timer == (int) (4.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 62:851cf7b7aa7a | 1016 | 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 | 1017 | DDV_POS_AVG = VALVE_POS_TMP / data_num; |
Lightvalve | 14:8e7590227d22 | 1018 | START_POS = pos.sen; |
Lightvalve | 13:747daba9cf59 | 1019 | VALVE_POS_TMP = 0; |
Lightvalve | 13:747daba9cf59 | 1020 | data_num = 0; |
Lightvalve | 14:8e7590227d22 | 1021 | |
Lightvalve | 30:8d561f16383b | 1022 | } else if(VALVE_DZ_timer < (int) (5.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1023 | valve_pos.ref = DDV_POS_AVG; |
Lightvalve | 14:8e7590227d22 | 1024 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 14:8e7590227d22 | 1025 | |
Lightvalve | 30:8d561f16383b | 1026 | } else if(VALVE_DZ_timer < (int) (6.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1027 | valve_pos.ref = DDV_POS_AVG; |
Lightvalve | 14:8e7590227d22 | 1028 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 62:851cf7b7aa7a | 1029 | |
Lightvalve | 30:8d561f16383b | 1030 | } else if(VALVE_DZ_timer == (int) (6.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1031 | valve_pos.ref = DDV_POS_AVG; |
Lightvalve | 14:8e7590227d22 | 1032 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 14:8e7590227d22 | 1033 | FINAL_POS = pos.sen; |
Lightvalve | 14:8e7590227d22 | 1034 | |
Lightvalve | 62:851cf7b7aa7a | 1035 | if((FINAL_POS - START_POS)>200) { |
Lightvalve | 13:747daba9cf59 | 1036 | DZ_case = 1; |
Lightvalve | 62:851cf7b7aa7a | 1037 | } else if((FINAL_POS - START_POS)<-200) { |
Lightvalve | 13:747daba9cf59 | 1038 | DZ_case = -1; |
Lightvalve | 14:8e7590227d22 | 1039 | } else { |
Lightvalve | 13:747daba9cf59 | 1040 | DZ_case = 0; |
Lightvalve | 13:747daba9cf59 | 1041 | } |
Lightvalve | 14:8e7590227d22 | 1042 | |
Lightvalve | 49:e7bcfc244d40 | 1043 | CAN_TX_PRES((int16_t) (DZ_case), (int16_t) (6)); |
Lightvalve | 50:3c630b5eba9f | 1044 | |
Lightvalve | 13:747daba9cf59 | 1045 | first_check = 1; |
Lightvalve | 13:747daba9cf59 | 1046 | DZ_DIRECTION = 1; |
Lightvalve | 13:747daba9cf59 | 1047 | VALVE_DZ_timer = 0; |
Lightvalve | 13:747daba9cf59 | 1048 | Ref_Valve_Pos_Old = DDV_POS_AVG; |
Lightvalve | 13:747daba9cf59 | 1049 | DZ_NUM = 1; |
Lightvalve | 13:747daba9cf59 | 1050 | DZ_index = 1; |
Lightvalve | 14:8e7590227d22 | 1051 | |
Lightvalve | 13:747daba9cf59 | 1052 | } |
Lightvalve | 19:23b7c1ad8683 | 1053 | } else { |
Lightvalve | 14:8e7590227d22 | 1054 | if((DZ_case == -1 && DZ_NUM == 1) | (DZ_case == 1 && DZ_NUM == 1)) { |
Lightvalve | 30:8d561f16383b | 1055 | if(VALVE_DZ_timer < (int) (1.0 * (float) TMR_FREQ_5k)) { |
Lightvalve | 62:851cf7b7aa7a | 1056 | 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 | 1057 | //pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end; |
Lightvalve | 62:851cf7b7aa7a | 1058 | //CONTROL_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 30:8d561f16383b | 1059 | } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1060 | START_POS = pos.sen; |
Lightvalve | 30:8d561f16383b | 1061 | } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1062 | valve_pos.ref = Ref_Valve_Pos_Old - DZ_case * DZ_DIRECTION * 64 / DZ_index; |
Lightvalve | 14:8e7590227d22 | 1063 | if(valve_pos.ref <= VALVE_MIN_POS) { |
Lightvalve | 14:8e7590227d22 | 1064 | valve_pos.ref = VALVE_MIN_POS; |
Lightvalve | 14:8e7590227d22 | 1065 | } else if(valve_pos.ref >= VALVE_MAX_POS) { |
Lightvalve | 14:8e7590227d22 | 1066 | valve_pos.ref = VALVE_MAX_POS; |
Lightvalve | 13:747daba9cf59 | 1067 | } |
Lightvalve | 14:8e7590227d22 | 1068 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 62:851cf7b7aa7a | 1069 | |
Lightvalve | 30:8d561f16383b | 1070 | } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1071 | Ref_Valve_Pos_Old = valve_pos.ref; |
Lightvalve | 14:8e7590227d22 | 1072 | FINAL_POS = pos.sen; |
Lightvalve | 14:8e7590227d22 | 1073 | |
Lightvalve | 14:8e7590227d22 | 1074 | if((FINAL_POS - START_POS)>100) { |
Lightvalve | 14:8e7590227d22 | 1075 | DZ_DIRECTION = 1 * DZ_case; |
Lightvalve | 14:8e7590227d22 | 1076 | } else if((FINAL_POS - START_POS)<-100) { |
Lightvalve | 14:8e7590227d22 | 1077 | DZ_DIRECTION = -1 * DZ_case; |
Lightvalve | 14:8e7590227d22 | 1078 | } else { |
Lightvalve | 13:747daba9cf59 | 1079 | DZ_DIRECTION = 1 * DZ_case; |
Lightvalve | 13:747daba9cf59 | 1080 | } |
Lightvalve | 14:8e7590227d22 | 1081 | |
Lightvalve | 13:747daba9cf59 | 1082 | VALVE_DZ_timer = 0; |
Lightvalve | 13:747daba9cf59 | 1083 | DZ_index= DZ_index *2; |
Lightvalve | 14:8e7590227d22 | 1084 | if(DZ_index >= 128) { |
Lightvalve | 14:8e7590227d22 | 1085 | FIRST_DZ = valve_pos.ref; |
Lightvalve | 13:747daba9cf59 | 1086 | DZ_NUM = 2; |
Lightvalve | 13:747daba9cf59 | 1087 | Ref_Valve_Pos_Old = FIRST_DZ; |
Lightvalve | 13:747daba9cf59 | 1088 | DZ_index = 1; |
Lightvalve | 13:747daba9cf59 | 1089 | DZ_DIRECTION = 1; |
Lightvalve | 13:747daba9cf59 | 1090 | } |
Lightvalve | 13:747daba9cf59 | 1091 | } |
Lightvalve | 14:8e7590227d22 | 1092 | } else if((DZ_case == -1 && DZ_NUM == 2) | (DZ_case == 1 && DZ_NUM == 2)) { |
Lightvalve | 30:8d561f16383b | 1093 | if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 62:851cf7b7aa7a | 1094 | 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 | 1095 | //pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end; |
Lightvalve | 62:851cf7b7aa7a | 1096 | //CONTROL_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 30:8d561f16383b | 1097 | } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1098 | START_POS = pos.sen; |
Lightvalve | 30:8d561f16383b | 1099 | } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 62:851cf7b7aa7a | 1100 | valve_pos.ref = Ref_Valve_Pos_Old - DZ_case * DZ_DIRECTION * 64 / DZ_index; |
Lightvalve | 14:8e7590227d22 | 1101 | if(valve_pos.ref <= VALVE_MIN_POS) { |
Lightvalve | 14:8e7590227d22 | 1102 | valve_pos.ref = VALVE_MIN_POS; |
Lightvalve | 14:8e7590227d22 | 1103 | } else if(valve_pos.ref >= VALVE_MAX_POS) { |
Lightvalve | 14:8e7590227d22 | 1104 | valve_pos.ref = VALVE_MAX_POS; |
Lightvalve | 13:747daba9cf59 | 1105 | } |
Lightvalve | 14:8e7590227d22 | 1106 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 62:851cf7b7aa7a | 1107 | |
Lightvalve | 30:8d561f16383b | 1108 | } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 62:851cf7b7aa7a | 1109 | Vout.ref = 0.0f; |
Lightvalve | 62:851cf7b7aa7a | 1110 | } else if(VALVE_DZ_timer > (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1111 | Ref_Valve_Pos_Old = valve_pos.ref; |
Lightvalve | 14:8e7590227d22 | 1112 | FINAL_POS = pos.sen; |
Lightvalve | 14:8e7590227d22 | 1113 | |
Lightvalve | 14:8e7590227d22 | 1114 | if((FINAL_POS - START_POS)>100) { |
Lightvalve | 62:851cf7b7aa7a | 1115 | DZ_DIRECTION = 1 * DZ_case; |
Lightvalve | 14:8e7590227d22 | 1116 | } else if((FINAL_POS - START_POS)<-100) { |
Lightvalve | 62:851cf7b7aa7a | 1117 | DZ_DIRECTION = -1 * DZ_case; |
Lightvalve | 14:8e7590227d22 | 1118 | } else { |
Lightvalve | 62:851cf7b7aa7a | 1119 | DZ_DIRECTION = -1 * DZ_case; |
Lightvalve | 13:747daba9cf59 | 1120 | } |
Lightvalve | 14:8e7590227d22 | 1121 | |
Lightvalve | 13:747daba9cf59 | 1122 | VALVE_DZ_timer = 0; |
Lightvalve | 62:851cf7b7aa7a | 1123 | DZ_index= DZ_index * 2; |
Lightvalve | 14:8e7590227d22 | 1124 | if(DZ_index >= 128) { |
Lightvalve | 14:8e7590227d22 | 1125 | SECOND_DZ = valve_pos.ref; |
Lightvalve | 62:851cf7b7aa7a | 1126 | VALVE_CENTER = (int) (0.5f * (float) (FIRST_DZ) + 0.5f * (float) (SECOND_DZ)); |
Lightvalve | 13:747daba9cf59 | 1127 | first_check = 0; |
Lightvalve | 33:91b17819ec30 | 1128 | VALVE_DEADZONE_MINUS = (float) FIRST_DZ; |
Lightvalve | 33:91b17819ec30 | 1129 | VALVE_DEADZONE_PLUS = (float) SECOND_DZ; |
Lightvalve | 19:23b7c1ad8683 | 1130 | |
Lightvalve | 16:903b5a4433b4 | 1131 | ROM_RESET_DATA(); |
Lightvalve | 34:bb2ca2fc2a8e | 1132 | |
Lightvalve | 62:851cf7b7aa7a | 1133 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 13:747daba9cf59 | 1134 | DZ_index = 1; |
Lightvalve | 13:747daba9cf59 | 1135 | } |
Lightvalve | 13:747daba9cf59 | 1136 | } |
Lightvalve | 14:8e7590227d22 | 1137 | } else if(DZ_case == 0 && DZ_NUM ==1) { |
Lightvalve | 30:8d561f16383b | 1138 | if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 62:851cf7b7aa7a | 1139 | 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 | 1140 | //pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end; |
Lightvalve | 62:851cf7b7aa7a | 1141 | //CONTROL_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 30:8d561f16383b | 1142 | } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1143 | START_POS = pos.sen; |
Lightvalve | 30:8d561f16383b | 1144 | } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1145 | valve_pos.ref = Ref_Valve_Pos_Old - DZ_DIRECTION * 64 / DZ_index; |
Lightvalve | 14:8e7590227d22 | 1146 | if(valve_pos.ref <= VALVE_MIN_POS) { |
Lightvalve | 14:8e7590227d22 | 1147 | valve_pos.ref = VALVE_MIN_POS; |
Lightvalve | 14:8e7590227d22 | 1148 | } else if(valve_pos.ref >= VALVE_MAX_POS) { |
Lightvalve | 14:8e7590227d22 | 1149 | valve_pos.ref = VALVE_MAX_POS; |
Lightvalve | 13:747daba9cf59 | 1150 | } |
Lightvalve | 14:8e7590227d22 | 1151 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 62:851cf7b7aa7a | 1152 | |
Lightvalve | 30:8d561f16383b | 1153 | } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1154 | Ref_Valve_Pos_Old = valve_pos.ref; |
Lightvalve | 14:8e7590227d22 | 1155 | FINAL_POS = pos.sen; |
Lightvalve | 14:8e7590227d22 | 1156 | |
Lightvalve | 14:8e7590227d22 | 1157 | if((FINAL_POS - START_POS)>100) { |
Lightvalve | 14:8e7590227d22 | 1158 | DZ_DIRECTION = 1; |
Lightvalve | 14:8e7590227d22 | 1159 | } else if((FINAL_POS - START_POS)<-100) { |
Lightvalve | 14:8e7590227d22 | 1160 | DZ_DIRECTION = -1; |
Lightvalve | 14:8e7590227d22 | 1161 | } else { |
Lightvalve | 13:747daba9cf59 | 1162 | DZ_DIRECTION = 1; |
Lightvalve | 13:747daba9cf59 | 1163 | } |
Lightvalve | 13:747daba9cf59 | 1164 | VALVE_DZ_timer = 0; |
Lightvalve | 13:747daba9cf59 | 1165 | DZ_index= DZ_index *2; |
Lightvalve | 14:8e7590227d22 | 1166 | if(DZ_index >= 128) { |
Lightvalve | 14:8e7590227d22 | 1167 | FIRST_DZ = valve_pos.ref; |
Lightvalve | 13:747daba9cf59 | 1168 | DZ_NUM = 2; |
Lightvalve | 13:747daba9cf59 | 1169 | Ref_Valve_Pos_Old = FIRST_DZ; |
Lightvalve | 13:747daba9cf59 | 1170 | DZ_index = 1; |
Lightvalve | 13:747daba9cf59 | 1171 | DZ_DIRECTION = 1; |
Lightvalve | 13:747daba9cf59 | 1172 | } |
Lightvalve | 13:747daba9cf59 | 1173 | } |
Lightvalve | 14:8e7590227d22 | 1174 | } else { |
Lightvalve | 30:8d561f16383b | 1175 | if(VALVE_DZ_timer < (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 62:851cf7b7aa7a | 1176 | 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 | 1177 | //pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end; |
Lightvalve | 62:851cf7b7aa7a | 1178 | //CONTROL_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 30:8d561f16383b | 1179 | } else if(VALVE_DZ_timer == (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1180 | START_POS = pos.sen; |
Lightvalve | 30:8d561f16383b | 1181 | } else if(VALVE_DZ_timer < (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1182 | valve_pos.ref = Ref_Valve_Pos_Old + DZ_DIRECTION * 64 / DZ_index; |
Lightvalve | 14:8e7590227d22 | 1183 | if(valve_pos.ref <= VALVE_MIN_POS) { |
Lightvalve | 14:8e7590227d22 | 1184 | valve_pos.ref = VALVE_MIN_POS; |
Lightvalve | 14:8e7590227d22 | 1185 | } else if(valve_pos.ref > VALVE_MAX_POS) { |
Lightvalve | 14:8e7590227d22 | 1186 | valve_pos.ref = VALVE_MAX_POS - 1; |
Lightvalve | 13:747daba9cf59 | 1187 | } |
Lightvalve | 14:8e7590227d22 | 1188 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 62:851cf7b7aa7a | 1189 | |
Lightvalve | 30:8d561f16383b | 1190 | } else if(VALVE_DZ_timer == (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 62:851cf7b7aa7a | 1191 | Vout.ref = 0.0f; |
Lightvalve | 62:851cf7b7aa7a | 1192 | } else if(VALVE_DZ_timer > (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1193 | Ref_Valve_Pos_Old = valve_pos.ref; |
Lightvalve | 14:8e7590227d22 | 1194 | FINAL_POS = pos.sen; |
Lightvalve | 62:851cf7b7aa7a | 1195 | |
Lightvalve | 62:851cf7b7aa7a | 1196 | if((FINAL_POS - START_POS)>100) { |
Lightvalve | 13:747daba9cf59 | 1197 | DZ_DIRECTION = 1; |
Lightvalve | 62:851cf7b7aa7a | 1198 | } else if((FINAL_POS - START_POS)<-100) { |
Lightvalve | 62:851cf7b7aa7a | 1199 | DZ_DIRECTION = -1; |
Lightvalve | 62:851cf7b7aa7a | 1200 | } else { |
Lightvalve | 13:747daba9cf59 | 1201 | DZ_DIRECTION = 1; |
Lightvalve | 62:851cf7b7aa7a | 1202 | } |
Lightvalve | 14:8e7590227d22 | 1203 | |
Lightvalve | 13:747daba9cf59 | 1204 | VALVE_DZ_timer = 0; |
Lightvalve | 13:747daba9cf59 | 1205 | DZ_index= DZ_index *2; |
Lightvalve | 14:8e7590227d22 | 1206 | if(DZ_index >= 128) { |
Lightvalve | 14:8e7590227d22 | 1207 | SECOND_DZ = valve_pos.ref; |
Lightvalve | 62:851cf7b7aa7a | 1208 | VALVE_CENTER = (int) (0.5f * (float) (FIRST_DZ) + 0.5f * (float) (SECOND_DZ)); |
Lightvalve | 13:747daba9cf59 | 1209 | first_check = 0; |
Lightvalve | 33:91b17819ec30 | 1210 | VALVE_DEADZONE_MINUS = (float) FIRST_DZ; |
Lightvalve | 33:91b17819ec30 | 1211 | VALVE_DEADZONE_PLUS = (float) SECOND_DZ; |
Lightvalve | 34:bb2ca2fc2a8e | 1212 | |
Lightvalve | 62:851cf7b7aa7a | 1213 | ROM_RESET_DATA(); |
Lightvalve | 62:851cf7b7aa7a | 1214 | |
Lightvalve | 62:851cf7b7aa7a | 1215 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 13:747daba9cf59 | 1216 | DZ_index = 1; |
Lightvalve | 13:747daba9cf59 | 1217 | } |
Lightvalve | 13:747daba9cf59 | 1218 | } |
Lightvalve | 13:747daba9cf59 | 1219 | } |
Lightvalve | 14:8e7590227d22 | 1220 | } |
Lightvalve | 13:747daba9cf59 | 1221 | break; |
Lightvalve | 13:747daba9cf59 | 1222 | } |
Lightvalve | 14:8e7590227d22 | 1223 | |
Lightvalve | 14:8e7590227d22 | 1224 | case MODE_DDV_POS_VS_FLOWRATE: { |
Lightvalve | 62:851cf7b7aa7a | 1225 | CONTROL_MODE = MODE_VALVE_OPEN_LOOP; |
Lightvalve | 13:747daba9cf59 | 1226 | VALVE_FR_timer = VALVE_FR_timer + 1; |
Lightvalve | 14:8e7590227d22 | 1227 | if(first_check == 0) { |
Lightvalve | 30:8d561f16383b | 1228 | if(VALVE_FR_timer < (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 62:851cf7b7aa7a | 1229 | Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 23:59218d4a256d | 1230 | //CAN_TX_PRES((int16_t) (VALVE_FR_timer), (int16_t) (6)); |
Lightvalve | 30:8d561f16383b | 1231 | } else if(VALVE_FR_timer == (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 62:851cf7b7aa7a | 1232 | Vout.ref = VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 14:8e7590227d22 | 1233 | pos_plus_end = pos.sen; |
Lightvalve | 14:8e7590227d22 | 1234 | // CAN_TX_PRES((int16_t) (V_out), (int16_t) (7)); |
Lightvalve | 30:8d561f16383b | 1235 | } else if(VALVE_FR_timer < (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 62:851cf7b7aa7a | 1236 | Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 30:8d561f16383b | 1237 | } else if(VALVE_FR_timer == (int) (2.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1238 | // CAN_TX_PRES((int16_t) (V_out), (int16_t) (8)); |
Lightvalve | 62:851cf7b7aa7a | 1239 | Vout.ref = -VALVE_VOLTAGE_LIMIT * 1000.0f; |
Lightvalve | 14:8e7590227d22 | 1240 | pos_minus_end = pos.sen; |
Lightvalve | 13:747daba9cf59 | 1241 | first_check = 1; |
Lightvalve | 13:747daba9cf59 | 1242 | VALVE_FR_timer = 0; |
Lightvalve | 62:851cf7b7aa7a | 1243 | valve_pos.ref = (float) VALVE_CENTER; |
Lightvalve | 13:747daba9cf59 | 1244 | ID_index = 0; |
Lightvalve | 13:747daba9cf59 | 1245 | max_check = 0; |
Lightvalve | 13:747daba9cf59 | 1246 | min_check = 0; |
Lightvalve | 13:747daba9cf59 | 1247 | } |
Lightvalve | 14:8e7590227d22 | 1248 | } else { |
Lightvalve | 30:8d561f16383b | 1249 | if(VALVE_FR_timer < (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 62:851cf7b7aa7a | 1250 | //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 | 1251 | pos.ref = 0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end; |
Lightvalve | 62:851cf7b7aa7a | 1252 | CONTROL_MODE = MODE_JOINT_CONTROL; |
Lightvalve | 30:8d561f16383b | 1253 | } else if(VALVE_FR_timer == (int) (1.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 13:747daba9cf59 | 1254 | data_num = 0; |
Lightvalve | 62:851cf7b7aa7a | 1255 | valve_pos.ref = 10.0f*((float) ID_index_array[ID_index]) + (float) VALVE_CENTER; |
Lightvalve | 14:8e7590227d22 | 1256 | |
Lightvalve | 14:8e7590227d22 | 1257 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 14:8e7590227d22 | 1258 | START_POS = pos.sen; |
Lightvalve | 30:8d561f16383b | 1259 | } else if(VALVE_FR_timer < (int) (5.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 62:851cf7b7aa7a | 1260 | valve_pos.ref = 10.0f*((float) ID_index_array[ID_index]) + (float) VALVE_CENTER; |
Lightvalve | 14:8e7590227d22 | 1261 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 13:747daba9cf59 | 1262 | data_num = data_num + 1; |
Lightvalve | 30:8d561f16383b | 1263 | if(abs(0.5f * (float) pos_plus_end + 0.5f * (float) pos_minus_end - (float) pos.sen) > 20000.0f) { |
Lightvalve | 14:8e7590227d22 | 1264 | FINAL_POS = pos.sen; |
Lightvalve | 14:8e7590227d22 | 1265 | one_period_end = 1; |
Lightvalve | 13:747daba9cf59 | 1266 | } |
Lightvalve | 30:8d561f16383b | 1267 | } else if(VALVE_FR_timer == (int) (5.0f * (float) TMR_FREQ_5k)) { |
Lightvalve | 14:8e7590227d22 | 1268 | FINAL_POS = pos.sen; |
Lightvalve | 13:747daba9cf59 | 1269 | one_period_end = 1; |
Lightvalve | 62:851cf7b7aa7a | 1270 | V_out = 0.0f; |
Lightvalve | 13:747daba9cf59 | 1271 | } |
Lightvalve | 14:8e7590227d22 | 1272 | |
Lightvalve | 14:8e7590227d22 | 1273 | if(one_period_end == 1) { |
Lightvalve | 14:8e7590227d22 | 1274 | if(valve_pos.ref > VALVE_MAX_POS) { |
Lightvalve | 13:747daba9cf59 | 1275 | max_check = 1; |
Lightvalve | 14:8e7590227d22 | 1276 | } else if(valve_pos.ref < VALVE_MIN_POS) { |
Lightvalve | 13:747daba9cf59 | 1277 | min_check = 1; |
Lightvalve | 13:747daba9cf59 | 1278 | } |
Lightvalve | 13:747daba9cf59 | 1279 | JOINT_VEL[ID_index] = (FINAL_POS - START_POS) / data_num * TMR_FREQ_5k; // pulse/sec |
Lightvalve | 19:23b7c1ad8683 | 1280 | |
Lightvalve | 13:747daba9cf59 | 1281 | VALVE_FR_timer = 0; |
Lightvalve | 13:747daba9cf59 | 1282 | one_period_end = 0; |
Lightvalve | 13:747daba9cf59 | 1283 | ID_index= ID_index +1; |
Lightvalve | 30:8d561f16383b | 1284 | V_out = 0.0f; |
Lightvalve | 13:747daba9cf59 | 1285 | } |
Lightvalve | 14:8e7590227d22 | 1286 | |
Lightvalve | 14:8e7590227d22 | 1287 | if(max_check == 1 && min_check == 1) { |
Lightvalve | 19:23b7c1ad8683 | 1288 | |
Lightvalve | 13:747daba9cf59 | 1289 | VALVE_POS_NUM = ID_index; |
Lightvalve | 62:851cf7b7aa7a | 1290 | ROM_RESET_DATA(); |
Lightvalve | 13:747daba9cf59 | 1291 | ID_index = 0; |
Lightvalve | 13:747daba9cf59 | 1292 | first_check = 0; |
Lightvalve | 13:747daba9cf59 | 1293 | VALVE_FR_timer = 0; |
Lightvalve | 62:851cf7b7aa7a | 1294 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 49:e7bcfc244d40 | 1295 | // CAN_TX_PRES((int16_t) (VALVE_FR_timer), (int16_t) (6)); |
Lightvalve | 13:747daba9cf59 | 1296 | } |
Lightvalve | 13:747daba9cf59 | 1297 | } |
Lightvalve | 13:747daba9cf59 | 1298 | break; |
Lightvalve | 13:747daba9cf59 | 1299 | } |
Lightvalve | 58:2eade98630e2 | 1300 | |
Lightvalve | 62:851cf7b7aa7a | 1301 | case MODE_SYSTEM_ID: { |
Lightvalve | 62:851cf7b7aa7a | 1302 | freq_sysid_Iref = (double) cnt_sysid * DT_TMR3 * 3.; |
Lightvalve | 62:851cf7b7aa7a | 1303 | valve_pos.ref = 2500.0f * sin(2.0f * 3.14159f * freq_sysid_Iref * (double) cnt_sysid * DT_TMR3); |
Lightvalve | 62:851cf7b7aa7a | 1304 | CONTROL_MODE = MODE_VALVE_OPEN_LOOP; |
Lightvalve | 62:851cf7b7aa7a | 1305 | cnt_sysid++; |
Lightvalve | 62:851cf7b7aa7a | 1306 | if (freq_sysid_Iref >= 300) { |
Lightvalve | 62:851cf7b7aa7a | 1307 | cnt_sysid = 0; |
Lightvalve | 62:851cf7b7aa7a | 1308 | CONTROL_UTILITY_MODE = MODE_NO_ACT; |
Lightvalve | 62:851cf7b7aa7a | 1309 | } |
Lightvalve | 62:851cf7b7aa7a | 1310 | break; |
Lightvalve | 62:851cf7b7aa7a | 1311 | } |
Lightvalve | 62:851cf7b7aa7a | 1312 | |
Lightvalve | 62:851cf7b7aa7a | 1313 | |
Lightvalve | 62:851cf7b7aa7a | 1314 | |
Lightvalve | 62:851cf7b7aa7a | 1315 | default: |
Lightvalve | 62:851cf7b7aa7a | 1316 | break; |
Lightvalve | 62:851cf7b7aa7a | 1317 | } |
Lightvalve | 62:851cf7b7aa7a | 1318 | |
Lightvalve | 62:851cf7b7aa7a | 1319 | // CONTROL MODE ------------------------------------------------------------ |
Lightvalve | 62:851cf7b7aa7a | 1320 | |
Lightvalve | 62:851cf7b7aa7a | 1321 | switch (CONTROL_MODE) { |
Lightvalve | 62:851cf7b7aa7a | 1322 | case MODE_NO_ACT: { |
Lightvalve | 62:851cf7b7aa7a | 1323 | V_out = 0.0f; |
Lightvalve | 62:851cf7b7aa7a | 1324 | break; |
Lightvalve | 62:851cf7b7aa7a | 1325 | } |
Lightvalve | 62:851cf7b7aa7a | 1326 | |
Lightvalve | 62:851cf7b7aa7a | 1327 | case MODE_VALVE_POSITION_CONTROL: { |
Lightvalve | 62:851cf7b7aa7a | 1328 | if (OPERATING_MODE == 5) { //SW Valve |
Lightvalve | 62:851cf7b7aa7a | 1329 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 62:851cf7b7aa7a | 1330 | V_out = Vout.ref; |
Lightvalve | 62:851cf7b7aa7a | 1331 | } else if (CURRENT_CONTROL_MODE == 0) { //PWM |
Lightvalve | 62:851cf7b7aa7a | 1332 | V_out = valve_pos.ref; |
Lightvalve | 62:851cf7b7aa7a | 1333 | } else { |
Lightvalve | 62:851cf7b7aa7a | 1334 | I_REF = valve_pos.ref * 0.001f; |
Lightvalve | 62:851cf7b7aa7a | 1335 | } |
Lightvalve | 62:851cf7b7aa7a | 1336 | |
Lightvalve | 62:851cf7b7aa7a | 1337 | break; |
Lightvalve | 62:851cf7b7aa7a | 1338 | } |
Lightvalve | 62:851cf7b7aa7a | 1339 | |
Lightvalve | 62:851cf7b7aa7a | 1340 | case MODE_JOINT_CONTROL: { |
Lightvalve | 62:851cf7b7aa7a | 1341 | double torq_ref = 0.0f; |
Lightvalve | 62:851cf7b7aa7a | 1342 | pos.err = (pos.ref - pos.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm] |
Lightvalve | 63:a3c7f31742c9 | 1343 | vel.err = (vel.ref - vel.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm/s] |
Lightvalve | 63:a3c7f31742c9 | 1344 | // vel.err = (0.0f - vel.sen)/(float)(ENC_PULSE_PER_POSITION); //[mm/s] |
Lightvalve | 62:851cf7b7aa7a | 1345 | pos.err_sum += pos.err/(float) TMR_FREQ_5k; //[mm] |
Lightvalve | 62:851cf7b7aa7a | 1346 | |
Lightvalve | 62:851cf7b7aa7a | 1347 | //K & D Low Pass Filter |
Lightvalve | 62:851cf7b7aa7a | 1348 | float alpha_K_D = 1.0f/(1.0f + 5000.0f/(2.0f*3.14f*30.0f)); // f_cutoff : 30Hz |
Lightvalve | 62:851cf7b7aa7a | 1349 | K_LPF = K_LPF*(1.0f-alpha_K_D)+K_SPRING*(alpha_K_D); |
Lightvalve | 62:851cf7b7aa7a | 1350 | D_LPF = D_LPF*(1.0f-alpha_K_D)+D_DAMPER*(alpha_K_D); |
Lightvalve | 62:851cf7b7aa7a | 1351 | |
Lightvalve | 62:851cf7b7aa7a | 1352 | torq_ref = torq.ref + K_LPF * pos.err - D_LPF * vel.sen / ENC_PULSE_PER_POSITION; //[N] |
Lightvalve | 62:851cf7b7aa7a | 1353 | |
Lightvalve | 62:851cf7b7aa7a | 1354 | // torque feedback |
Lightvalve | 63:a3c7f31742c9 | 1355 | torq.err = torq_ref - torq.sen; //[N] |
Lightvalve | 62:851cf7b7aa7a | 1356 | torq.err_sum += torq.err/(float) TMR_FREQ_5k; //[N] |
Lightvalve | 62:851cf7b7aa7a | 1357 | |
Lightvalve | 62:851cf7b7aa7a | 1358 | if (((OPERATING_MODE&0b110)>>1) == 0 || ((OPERATING_MODE&0b110)>>1) == 1) { |
Lightvalve | 62:851cf7b7aa7a | 1359 | |
Lightvalve | 62:851cf7b7aa7a | 1360 | double I_REF_POS = 0.0f; |
Lightvalve | 62:851cf7b7aa7a | 1361 | double I_REF_FORCE_FB = 0.0f; // I_REF by Force Feedback |
Lightvalve | 62:851cf7b7aa7a | 1362 | double I_REF_VC = 0.0f; // I_REF for velocity compensation |
Lightvalve | 62:851cf7b7aa7a | 1363 | |
Lightvalve | 62:851cf7b7aa7a | 1364 | double temp_vel_pos = 0.0f; |
Lightvalve | 62:851cf7b7aa7a | 1365 | double temp_vel_torq = 0.0f; |
Lightvalve | 62:851cf7b7aa7a | 1366 | double wn_Pos = 2.0f * PI * 5.0f; // f_cut : 5Hz Position Control |
Lightvalve | 62:851cf7b7aa7a | 1367 | |
Lightvalve | 62:851cf7b7aa7a | 1368 | if ((OPERATING_MODE && 0x01) == 0) { // Rotary Mode |
Lightvalve | 62:851cf7b7aa7a | 1369 | 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 | 1370 | // L when P-gain = 100, f_cut = 10Hz L feedforward velocity |
Lightvalve | 62:851cf7b7aa7a | 1371 | } else if ((OPERATING_MODE && 0x01) == 1) { |
Lightvalve | 62:851cf7b7aa7a | 1372 | 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 | 1373 | // L when P-gain = 100, f_cut = 10Hz L feedforward velocity |
Lightvalve | 62:851cf7b7aa7a | 1374 | } |
Lightvalve | 62:851cf7b7aa7a | 1375 | 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 | 1376 | else I_REF_POS = temp_vel_pos * ((double) PISTON_AREA_B * 0.00006f / (K_v * sqrt(2.0f / (alpha3 + 1.0f)))); |
Lightvalve | 62:851cf7b7aa7a | 1377 | |
Lightvalve | 62:851cf7b7aa7a | 1378 | // velocity compensation for torque control |
Lightvalve | 62:851cf7b7aa7a | 1379 | if ((OPERATING_MODE && 0x01) == 0) { // Rotary Mode |
Lightvalve | 62:851cf7b7aa7a | 1380 | I_REF_FORCE_FB = 0.001f * ((double) P_GAIN_JOINT_TORQUE * torq.err + (double) I_GAIN_JOINT_TORQUE * torq.err_sum); |
Lightvalve | 62:851cf7b7aa7a | 1381 | // 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 | 1382 | temp_vel_torq = (0.01f * (double) VELOCITY_COMP_GAIN * vel.ref / (double) ENC_PULSE_PER_POSITION) * PI / 180.0f; // rad/s |
Lightvalve | 62:851cf7b7aa7a | 1383 | // L feedforward velocity |
Lightvalve | 62:851cf7b7aa7a | 1384 | } else if ((OPERATING_MODE && 0x01) == 1) { |
Lightvalve | 62:851cf7b7aa7a | 1385 | 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 | 1386 | // temp_vel_torq = (0.01 * (double) VELOCITY_COMP_GAIN * (double) CUR_VELOCITY / (double) ENC_PULSE_PER_POSITION); // mm/s |
Lightvalve | 62:851cf7b7aa7a | 1387 | temp_vel_torq = (0.01f * (double) VELOCITY_COMP_GAIN * vel.ref / (double) ENC_PULSE_PER_POSITION); // mm/s |
Lightvalve | 62:851cf7b7aa7a | 1388 | // L feedforward velocity |
Lightvalve | 62:851cf7b7aa7a | 1389 | } |
Lightvalve | 62:851cf7b7aa7a | 1390 | 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 | 1391 | else I_REF_VC = temp_vel_torq * ((double) PISTON_AREA_B * 0.00006f / (K_v * sqrt(2.0f / (alpha3 + 1.0f)))); |
Lightvalve | 62:851cf7b7aa7a | 1392 | // L velocity(rad/s or mm/s) >> I_ref(mA) |
Lightvalve | 62:851cf7b7aa7a | 1393 | // Ref_Joint_FT_dot = (Ref_Joint_FT_Nm - Ref_Joint_FT_Nm_old) / TMR_DT_5k; |
Lightvalve | 62:851cf7b7aa7a | 1394 | // Ref_Joint_FT_Nm_old = Ref_Joint_FT_Nm; |
Lightvalve | 62:851cf7b7aa7a | 1395 | |
Lightvalve | 62:851cf7b7aa7a | 1396 | I_REF = (1.0f - alpha_trans) * I_REF_POS + alpha_trans * (I_REF_VC + I_REF_FORCE_FB); |
Lightvalve | 62:851cf7b7aa7a | 1397 | |
Lightvalve | 62:851cf7b7aa7a | 1398 | // Anti-windup for FT |
Lightvalve | 62:851cf7b7aa7a | 1399 | if (I_GAIN_JOINT_TORQUE != 0) { |
Lightvalve | 62:851cf7b7aa7a | 1400 | double I_MAX = 10.0f; // Maximum Current : 10mV |
Lightvalve | 62:851cf7b7aa7a | 1401 | double Ka = 2.0f / ((double) I_GAIN_JOINT_TORQUE * 0.001f); |
Lightvalve | 62:851cf7b7aa7a | 1402 | if (I_REF > I_MAX) { |
Lightvalve | 62:851cf7b7aa7a | 1403 | double I_rem = I_REF - I_MAX; |
Lightvalve | 62:851cf7b7aa7a | 1404 | I_rem = Ka*I_rem; |
Lightvalve | 62:851cf7b7aa7a | 1405 | I_REF = I_MAX; |
Lightvalve | 62:851cf7b7aa7a | 1406 | torq.err_sum = torq.err_sum - I_rem /(float) TMR_FREQ_5k; |
Lightvalve | 62:851cf7b7aa7a | 1407 | } else if (I_REF < -I_MAX) { |
Lightvalve | 62:851cf7b7aa7a | 1408 | double I_rem = I_REF - (-I_MAX); |
Lightvalve | 62:851cf7b7aa7a | 1409 | I_rem = Ka*I_rem; |
Lightvalve | 62:851cf7b7aa7a | 1410 | I_REF = -I_MAX; |
Lightvalve | 62:851cf7b7aa7a | 1411 | torq.err_sum = torq.err_sum - I_rem /(float) TMR_FREQ_5k; |
Lightvalve | 62:851cf7b7aa7a | 1412 | } |
Lightvalve | 62:851cf7b7aa7a | 1413 | } |
Lightvalve | 62:851cf7b7aa7a | 1414 | |
Lightvalve | 62:851cf7b7aa7a | 1415 | } else { |
Lightvalve | 62:851cf7b7aa7a | 1416 | float VALVE_POS_RAW_FORCE_FB = 0.0f; |
Lightvalve | 62:851cf7b7aa7a | 1417 | |
Lightvalve | 62:851cf7b7aa7a | 1418 | 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 | 1419 | + 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 | 1420 | |
Lightvalve | 62:851cf7b7aa7a | 1421 | if (VALVE_POS_RAW_FORCE_FB >= 0) { |
Lightvalve | 62:851cf7b7aa7a | 1422 | valve_pos.ref = VALVE_POS_RAW_FORCE_FB + VALVE_DEADZONE_PLUS; |
Lightvalve | 62:851cf7b7aa7a | 1423 | } else { |
Lightvalve | 62:851cf7b7aa7a | 1424 | valve_pos.ref = VALVE_POS_RAW_FORCE_FB + VALVE_DEADZONE_MINUS; |
Lightvalve | 62:851cf7b7aa7a | 1425 | } |
Lightvalve | 62:851cf7b7aa7a | 1426 | |
Lightvalve | 62:851cf7b7aa7a | 1427 | if(I_GAIN_JOINT_TORQUE != 0) { |
Lightvalve | 62:851cf7b7aa7a | 1428 | double Ka = 1.0f / (double) I_GAIN_JOINT_TORQUE * 100.0f; |
Lightvalve | 62:851cf7b7aa7a | 1429 | if(valve_pos.ref>VALVE_MAX_POS) { |
Lightvalve | 62:851cf7b7aa7a | 1430 | double valve_pos_rem = valve_pos.ref - VALVE_MAX_POS; |
Lightvalve | 62:851cf7b7aa7a | 1431 | valve_pos_rem = valve_pos_rem * Ka; |
Lightvalve | 62:851cf7b7aa7a | 1432 | valve_pos.ref = VALVE_MAX_POS; |
Lightvalve | 62:851cf7b7aa7a | 1433 | torq.err_sum = torq.err_sum - valve_pos_rem/(float) TMR_FREQ_5k; |
Lightvalve | 62:851cf7b7aa7a | 1434 | } else if(valve_pos.ref < VALVE_MIN_POS) { |
Lightvalve | 62:851cf7b7aa7a | 1435 | double valve_pos_rem = valve_pos.ref - VALVE_MIN_POS; |
Lightvalve | 62:851cf7b7aa7a | 1436 | valve_pos_rem = valve_pos_rem * Ka; |
Lightvalve | 62:851cf7b7aa7a | 1437 | valve_pos.ref = VALVE_MIN_POS; |
Lightvalve | 62:851cf7b7aa7a | 1438 | torq.err_sum = torq.err_sum - valve_pos_rem/(float) TMR_FREQ_5k; |
Lightvalve | 62:851cf7b7aa7a | 1439 | } |
Lightvalve | 62:851cf7b7aa7a | 1440 | } |
Lightvalve | 62:851cf7b7aa7a | 1441 | |
Lightvalve | 62:851cf7b7aa7a | 1442 | VALVE_POS_CONTROL(valve_pos.ref); |
Lightvalve | 62:851cf7b7aa7a | 1443 | |
Lightvalve | 62:851cf7b7aa7a | 1444 | // Vout.ref = (float) P_GAIN_JOINT_POSITION * 0.01f * ((float) pos.err); |
Lightvalve | 62:851cf7b7aa7a | 1445 | V_out = (float) Vout.ref; |
Lightvalve | 62:851cf7b7aa7a | 1446 | |
Lightvalve | 62:851cf7b7aa7a | 1447 | } |
Lightvalve | 62:851cf7b7aa7a | 1448 | |
Lightvalve | 62:851cf7b7aa7a | 1449 | break; |
Lightvalve | 62:851cf7b7aa7a | 1450 | } |
Lightvalve | 62:851cf7b7aa7a | 1451 | |
Lightvalve | 62:851cf7b7aa7a | 1452 | case MODE_VALVE_OPEN_LOOP: { |
Lightvalve | 62:851cf7b7aa7a | 1453 | V_out = (float) Vout.ref; |
Lightvalve | 62:851cf7b7aa7a | 1454 | break; |
Lightvalve | 62:851cf7b7aa7a | 1455 | } |
Lightvalve | 62:851cf7b7aa7a | 1456 | |
Lightvalve | 12:6f2531038ea4 | 1457 | default: |
Lightvalve | 12:6f2531038ea4 | 1458 | break; |
Lightvalve | 12:6f2531038ea4 | 1459 | } |
Lightvalve | 14:8e7590227d22 | 1460 | |
Lightvalve | 62:851cf7b7aa7a | 1461 | |
Lightvalve | 62:851cf7b7aa7a | 1462 | if (((OPERATING_MODE&0b110)>>1) == 0 || ((OPERATING_MODE&0b110)>>1) == 1) { //Moog Valve or KNR Valve |
Lightvalve | 62:851cf7b7aa7a | 1463 | |
Lightvalve | 62:851cf7b7aa7a | 1464 | //////////////////////////////////////////////////////////////////////////// |
Lightvalve | 62:851cf7b7aa7a | 1465 | //////////////////////////// CURRENT CONTROL ////////////////////////////// |
Lightvalve | 62:851cf7b7aa7a | 1466 | //////////////////////////////////////////////////////////////////////////// |
Lightvalve | 63:a3c7f31742c9 | 1467 | |
Lightvalve | 62:851cf7b7aa7a | 1468 | if (CURRENT_CONTROL_MODE) { |
Lightvalve | 63:a3c7f31742c9 | 1469 | |
Lightvalve | 62:851cf7b7aa7a | 1470 | // Moog Valve Current Control Gain |
Lightvalve | 62:851cf7b7aa7a | 1471 | double R_model = 500.0f; // ohm |
Lightvalve | 62:851cf7b7aa7a | 1472 | double L_model = 1.2f; |
Lightvalve | 62:851cf7b7aa7a | 1473 | double w0 = 2.0f * 3.14f * 150.0f; |
Lightvalve | 62:851cf7b7aa7a | 1474 | double KP_I = 0.1f * L_model*w0; |
Lightvalve | 62:851cf7b7aa7a | 1475 | double KI_I = 0.1f * R_model*w0; |
Lightvalve | 62:851cf7b7aa7a | 1476 | |
Lightvalve | 62:851cf7b7aa7a | 1477 | // KNR Valve Current Control Gain |
Lightvalve | 62:851cf7b7aa7a | 1478 | if (((OPERATING_MODE & 0b110)>>1) == 1) { // KNR Valve |
Lightvalve | 62:851cf7b7aa7a | 1479 | R_model = 163.0f; // ohm |
Lightvalve | 62:851cf7b7aa7a | 1480 | L_model = 1.0f; |
Lightvalve | 62:851cf7b7aa7a | 1481 | w0 = 2.0f * 3.14f * 80.0f; |
Lightvalve | 62:851cf7b7aa7a | 1482 | KP_I = 1.0f * L_model*w0; |
Lightvalve | 62:851cf7b7aa7a | 1483 | KI_I = 0.08f * R_model*w0; |
Lightvalve | 62:851cf7b7aa7a | 1484 | } |
Lightvalve | 63:a3c7f31742c9 | 1485 | |
Lightvalve | 63:a3c7f31742c9 | 1486 | double alpha_update_Iref = 1.0f / (1.0f + 5000.0f / (2.0f * 3.14f * 300.0f)); // f_cutoff : 500Hz |
Lightvalve | 63:a3c7f31742c9 | 1487 | I_REF_fil = (1.0f - alpha_update_Iref) * I_REF_fil + alpha_update_Iref*I_REF; |
Lightvalve | 63:a3c7f31742c9 | 1488 | |
Lightvalve | 63:a3c7f31742c9 | 1489 | FLAG_VALVE_DEADZONE = 1; |
Lightvalve | 63:a3c7f31742c9 | 1490 | |
Lightvalve | 63:a3c7f31742c9 | 1491 | if (FLAG_VALVE_DEADZONE) { |
Lightvalve | 63:a3c7f31742c9 | 1492 | if (I_REF_fil > 0) I_REF_fil_DZ = I_REF_fil + VALVE_DEADZONE_PLUS / 1000.0f; // unit: mA |
Lightvalve | 63:a3c7f31742c9 | 1493 | else if (I_REF_fil < 0) I_REF_fil_DZ = I_REF_fil + VALVE_DEADZONE_MINUS / 1000.0f; // unit: mA |
Lightvalve | 63:a3c7f31742c9 | 1494 | else I_REF_fil_DZ = I_REF_fil + (VALVE_DEADZONE_PLUS+VALVE_DEADZONE_MINUS)/2.0f / 1000.0f; // unit: mA |
Lightvalve | 63:a3c7f31742c9 | 1495 | } else { |
Lightvalve | 63:a3c7f31742c9 | 1496 | I_REF_fil_DZ = I_REF_fil; |
Lightvalve | 63:a3c7f31742c9 | 1497 | } |
Lightvalve | 63:a3c7f31742c9 | 1498 | |
Lightvalve | 63:a3c7f31742c9 | 1499 | I_ERR = I_REF_fil_DZ - cur.sen; |
Lightvalve | 63:a3c7f31742c9 | 1500 | I_ERR_INT = I_ERR_INT + (I_ERR) * 0.0002f; |
Lightvalve | 63:a3c7f31742c9 | 1501 | |
Lightvalve | 63:a3c7f31742c9 | 1502 | |
Lightvalve | 63:a3c7f31742c9 | 1503 | |
Lightvalve | 62:851cf7b7aa7a | 1504 | |
Lightvalve | 62:851cf7b7aa7a | 1505 | double FF_gain = 1.0f; |
Lightvalve | 62:851cf7b7aa7a | 1506 | |
Lightvalve | 62:851cf7b7aa7a | 1507 | VALVE_PWM_RAW = KP_I * 2.0f * I_ERR + KI_I * 2.0f* I_ERR_INT; |
Lightvalve | 62:851cf7b7aa7a | 1508 | // VALVE_PWM_RAW = VALVE_PWM_RAW + FF_gain * (R_model*I_REF); // Unit : mV |
Lightvalve | 63:a3c7f31742c9 | 1509 | I_REF_fil_diff = I_REF_fil_DZ - I_REF_fil_old; |
Lightvalve | 63:a3c7f31742c9 | 1510 | I_REF_fil_old = I_REF_fil_DZ; |
Lightvalve | 63:a3c7f31742c9 | 1511 | // 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 | 1512 | VALVE_PWM_RAW = VALVE_PWM_RAW + FF_gain * (R_model * I_REF_fil_DZ); // Unit : mV |
Lightvalve | 62:851cf7b7aa7a | 1513 | double V_MAX = 12000.0f; // Maximum Voltage : 12V = 12000mV |
Lightvalve | 14:8e7590227d22 | 1514 | |
Lightvalve | 62:851cf7b7aa7a | 1515 | double Ka = 3.0f / KP_I; |
Lightvalve | 62:851cf7b7aa7a | 1516 | if (VALVE_PWM_RAW > V_MAX) { |
Lightvalve | 62:851cf7b7aa7a | 1517 | V_rem = VALVE_PWM_RAW - V_MAX; |
Lightvalve | 62:851cf7b7aa7a | 1518 | V_rem = Ka*V_rem; |
Lightvalve | 62:851cf7b7aa7a | 1519 | VALVE_PWM_RAW = V_MAX; |
Lightvalve | 62:851cf7b7aa7a | 1520 | I_ERR_INT = I_ERR_INT - V_rem * 0.0002f; |
Lightvalve | 62:851cf7b7aa7a | 1521 | } else if (VALVE_PWM_RAW < -V_MAX) { |
Lightvalve | 62:851cf7b7aa7a | 1522 | V_rem = VALVE_PWM_RAW - (-V_MAX); |
Lightvalve | 62:851cf7b7aa7a | 1523 | V_rem = Ka*V_rem; |
Lightvalve | 62:851cf7b7aa7a | 1524 | VALVE_PWM_RAW = -V_MAX; |
Lightvalve | 62:851cf7b7aa7a | 1525 | I_ERR_INT = I_ERR_INT - V_rem * 0.0002f; |
Lightvalve | 62:851cf7b7aa7a | 1526 | } |
Lightvalve | 62:851cf7b7aa7a | 1527 | Cur_Valve_Open_pulse = cur.sen / mA_PER_pulse; |
Lightvalve | 62:851cf7b7aa7a | 1528 | } else { |
Lightvalve | 62:851cf7b7aa7a | 1529 | VALVE_PWM_RAW = I_REF * mV_PER_mA; |
Lightvalve | 62:851cf7b7aa7a | 1530 | Cur_Valve_Open_pulse = I_REF / mA_PER_pulse; |
Lightvalve | 62:851cf7b7aa7a | 1531 | } |
Lightvalve | 62:851cf7b7aa7a | 1532 | |
Lightvalve | 62:851cf7b7aa7a | 1533 | //////////////////////////////////////////////////////////////////////////// |
Lightvalve | 62:851cf7b7aa7a | 1534 | ///////////////// Dead Zone Cancellation & Linearization ////////////////// |
Lightvalve | 62:851cf7b7aa7a | 1535 | //////////////////////////////////////////////////////////////////////////// |
Lightvalve | 62:851cf7b7aa7a | 1536 | // Dead Zone Cancellation (Mechanical Valve dead-zone) |
Lightvalve | 63:a3c7f31742c9 | 1537 | // if (FLAG_VALVE_DEADZONE) { |
Lightvalve | 63:a3c7f31742c9 | 1538 | // if (VALVE_PWM_RAW > 0) VALVE_PWM_RAW = VALVE_PWM_RAW + VALVE_DEADZONE_PLUS * mV_PER_pulse; // unit: mV |
Lightvalve | 63:a3c7f31742c9 | 1539 | // else if (VALVE_PWM_RAW < 0) VALVE_PWM_RAW = VALVE_PWM_RAW + VALVE_DEADZONE_MINUS * mV_PER_pulse; // unit: mV |
Lightvalve | 63:a3c7f31742c9 | 1540 | // VALVE_PWM_VALVE_DZ = VALVE_PWM_RAW + (double)VALVE_CENTER * mV_PER_pulse; // unit: mV |
Lightvalve | 63:a3c7f31742c9 | 1541 | // } else { |
Lightvalve | 63:a3c7f31742c9 | 1542 | // VALVE_PWM_VALVE_DZ = VALVE_PWM_RAW; |
Lightvalve | 63:a3c7f31742c9 | 1543 | // } |
Lightvalve | 62:851cf7b7aa7a | 1544 | |
Lightvalve | 63:a3c7f31742c9 | 1545 | // VALVE_DEADZONE_PLUS : Current[mA] to start extraction |
Lightvalve | 63:a3c7f31742c9 | 1546 | // VALVE_DEADZONE_MINUS : Current[mA] to start contraction |
Lightvalve | 63:a3c7f31742c9 | 1547 | // double R_model = 500.0f; |
Lightvalve | 63:a3c7f31742c9 | 1548 | // FLAG_VALVE_DEADZONE = true; |
Lightvalve | 63:a3c7f31742c9 | 1549 | // if (FLAG_VALVE_DEADZONE) { |
Lightvalve | 63:a3c7f31742c9 | 1550 | // if (VALVE_PWM_RAW > 0) VALVE_PWM_VALVE_DZ = VALVE_PWM_RAW + VALVE_DEADZONE_PLUS * R_model / 1000.0f; // unit: mV |
Lightvalve | 63:a3c7f31742c9 | 1551 | // 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 | 1552 | // else VALVE_PWM_VALVE_DZ = VALVE_PWM_RAW + (VALVE_DEADZONE_PLUS+VALVE_DEADZONE_MINUS)/2.0f* R_model / 1000.0f; |
Lightvalve | 63:a3c7f31742c9 | 1553 | // } else { |
Lightvalve | 62:851cf7b7aa7a | 1554 | VALVE_PWM_VALVE_DZ = VALVE_PWM_RAW; |
Lightvalve | 63:a3c7f31742c9 | 1555 | // } |
Lightvalve | 62:851cf7b7aa7a | 1556 | |
Lightvalve | 62:851cf7b7aa7a | 1557 | // Output Voltage Linearization |
Lightvalve | 62:851cf7b7aa7a | 1558 | double CUR_PWM_nonlin = VALVE_PWM_VALVE_DZ; // Unit : mV |
Lightvalve | 62:851cf7b7aa7a | 1559 | double CUR_PWM_lin = PWM_duty_byLT(CUR_PWM_nonlin); // -8000~8000 |
Lightvalve | 62:851cf7b7aa7a | 1560 | |
Lightvalve | 62:851cf7b7aa7a | 1561 | // Dead Zone Cancellation (Electrical dead-zone) |
Lightvalve | 62:851cf7b7aa7a | 1562 | if (CUR_PWM_lin > 0) V_out = (float) (CUR_PWM_lin + 169.0f); |
Lightvalve | 62:851cf7b7aa7a | 1563 | else if (CUR_PWM_lin < 0) V_out = (float) (CUR_PWM_lin - 174.0f); |
Lightvalve | 62:851cf7b7aa7a | 1564 | else V_out = (float) (CUR_PWM_lin); |
Lightvalve | 62:851cf7b7aa7a | 1565 | } |
Lightvalve | 62:851cf7b7aa7a | 1566 | |
Lightvalve | 62:851cf7b7aa7a | 1567 | // if(V_out > 0.0f) V_out = (float) (V_out + 169.0f); |
Lightvalve | 62:851cf7b7aa7a | 1568 | // else if(V_out < 0.0f) V_out = (float) (V_out - 174.0f); |
Lightvalve | 62:851cf7b7aa7a | 1569 | // else V_out = V_out; |
Lightvalve | 62:851cf7b7aa7a | 1570 | |
jobuuu | 7:e9086c72bb22 | 1571 | /******************************************************* |
jobuuu | 7:e9086c72bb22 | 1572 | *** PWM |
jobuuu | 7:e9086c72bb22 | 1573 | ********************************************************/ |
Lightvalve | 62:851cf7b7aa7a | 1574 | if(DIR_VALVE<0){ |
Lightvalve | 62:851cf7b7aa7a | 1575 | V_out = -V_out; |
Lightvalve | 62:851cf7b7aa7a | 1576 | } |
Lightvalve | 62:851cf7b7aa7a | 1577 | |
Lightvalve | 49:e7bcfc244d40 | 1578 | if (V_out >= VALVE_VOLTAGE_LIMIT*1000.0f) { |
Lightvalve | 49:e7bcfc244d40 | 1579 | V_out = VALVE_VOLTAGE_LIMIT*1000.0f; |
Lightvalve | 49:e7bcfc244d40 | 1580 | } else if(V_out<=-VALVE_VOLTAGE_LIMIT*1000.0f) { |
Lightvalve | 49:e7bcfc244d40 | 1581 | V_out = -VALVE_VOLTAGE_LIMIT*1000.0f; |
Lightvalve | 27:a2254a485f23 | 1582 | } |
Lightvalve | 49:e7bcfc244d40 | 1583 | PWM_out= V_out/(SUPPLY_VOLTAGE*1000.0f); // Full duty : 12000.0mV |
Lightvalve | 13:747daba9cf59 | 1584 | |
Lightvalve | 19:23b7c1ad8683 | 1585 | // Saturation of output voltage to 12.0V |
Lightvalve | 30:8d561f16383b | 1586 | if(PWM_out > 1.0f) PWM_out=1.0f; |
Lightvalve | 30:8d561f16383b | 1587 | else if (PWM_out < -1.0f) PWM_out=-1.0f; |
Lightvalve | 13:747daba9cf59 | 1588 | |
Lightvalve | 30:8d561f16383b | 1589 | if (PWM_out>0.0f) { |
Lightvalve | 30:8d561f16383b | 1590 | dtc_v=0.0f; |
jobuuu | 1:e04e563be5ce | 1591 | dtc_w=PWM_out; |
jobuuu | 1:e04e563be5ce | 1592 | } else { |
jobuuu | 2:a1c0a37df760 | 1593 | dtc_v=-PWM_out; |
Lightvalve | 30:8d561f16383b | 1594 | dtc_w=0.0f; |
jobuuu | 1:e04e563be5ce | 1595 | } |
Lightvalve | 13:747daba9cf59 | 1596 | |
jobuuu | 1:e04e563be5ce | 1597 | //pwm |
Lightvalve | 30:8d561f16383b | 1598 | TIM4->CCR2 = (PWM_ARR)*(1.0f-dtc_v); |
Lightvalve | 30:8d561f16383b | 1599 | TIM4->CCR1 = (PWM_ARR)*(1.0f-dtc_w); |
Lightvalve | 13:747daba9cf59 | 1600 | |
Lightvalve | 20:806196fda269 | 1601 | |
Lightvalve | 62:851cf7b7aa7a | 1602 | if (TMR2_COUNT_CAN_TX % (int) ((int) TMR_FREQ_5k/CAN_FREQ) == 0) { |
Lightvalve | 20:806196fda269 | 1603 | |
Lightvalve | 54:647072f5307a | 1604 | // Position, Velocity, and Torque (ID:1200) |
Lightvalve | 56:6f50d9d3bfee | 1605 | if (flag_data_request[0] == HIGH) { |
Lightvalve | 62:851cf7b7aa7a | 1606 | if ((OPERATING_MODE & 0b01) == 0) { // Rotary Actuator |
Lightvalve | 54:647072f5307a | 1607 | if (SENSING_MODE == 0) { |
Lightvalve | 59:0ad14153b58f | 1608 | CAN_TX_POSITION_FT((int16_t) (pos.sen), (int16_t) (vel.sen/10.0f), (int16_t) (torq.sen*10.0f)); |
Lightvalve | 54:647072f5307a | 1609 | } else if (SENSING_MODE == 1) { |
Lightvalve | 62:851cf7b7aa7a | 1610 | 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 | 1611 | } |
Lightvalve | 62:851cf7b7aa7a | 1612 | } else if ((OPERATING_MODE & 0b01) == 1) { // Linear Actuator |
Lightvalve | 54:647072f5307a | 1613 | if (SENSING_MODE == 0) { |
Lightvalve | 63:a3c7f31742c9 | 1614 | 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 | 1615 | } else if (SENSING_MODE == 1) { |
Lightvalve | 62:851cf7b7aa7a | 1616 | 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 | 1617 | } |
Lightvalve | 52:8ea76864368a | 1618 | } |
Lightvalve | 52:8ea76864368a | 1619 | } |
Lightvalve | 56:6f50d9d3bfee | 1620 | if (flag_data_request[1] == HIGH) { |
Lightvalve | 54:647072f5307a | 1621 | //valve position |
Lightvalve | 54:647072f5307a | 1622 | double t_value = 0; |
Lightvalve | 62:851cf7b7aa7a | 1623 | // if(valve_pos.ref>=(float) VALVE_CENTER) { |
Lightvalve | 62:851cf7b7aa7a | 1624 | // t_value = 10000.0f*((double)valve_pos.ref - (double)VALVE_CENTER)/((double)VALVE_MAX_POS - (double)VALVE_CENTER); |
Lightvalve | 62:851cf7b7aa7a | 1625 | // } else { |
Lightvalve | 62:851cf7b7aa7a | 1626 | // t_value = -10000.0f*((double)valve_pos.ref - (double)VALVE_CENTER)/((double)VALVE_MIN_POS - (double)VALVE_CENTER); |
Lightvalve | 62:851cf7b7aa7a | 1627 | // } |
Lightvalve | 62:851cf7b7aa7a | 1628 | if(OPERATING_MODE==5) { |
Lightvalve | 62:851cf7b7aa7a | 1629 | t_value = (double) value; |
Lightvalve | 62:851cf7b7aa7a | 1630 | } else if(CURRENT_CONTROL_MODE==1) { |
Lightvalve | 62:851cf7b7aa7a | 1631 | t_value = cur.sen; |
Lightvalve | 58:2eade98630e2 | 1632 | } else { |
Lightvalve | 62:851cf7b7aa7a | 1633 | t_value = V_out; |
Lightvalve | 54:647072f5307a | 1634 | } |
Lightvalve | 63:a3c7f31742c9 | 1635 | // CAN_TX_TORQUE((int16_t) (I_REF_fil_DZ * 1000.0f)); //1300 |
Lightvalve | 63:a3c7f31742c9 | 1636 | CAN_TX_TORQUE((int16_t) (cur.sen * 1000.0f)); //1300 |
Lightvalve | 62:851cf7b7aa7a | 1637 | //CAN_TX_TORQUE((int16_t) (I_REF * 1000.0f)); //1300 |
Lightvalve | 54:647072f5307a | 1638 | } |
Lightvalve | 58:2eade98630e2 | 1639 | |
Lightvalve | 62:851cf7b7aa7a | 1640 | |
Lightvalve | 56:6f50d9d3bfee | 1641 | if (flag_data_request[2] == HIGH) { |
Lightvalve | 56:6f50d9d3bfee | 1642 | //pressure A and B |
Lightvalve | 62:851cf7b7aa7a | 1643 | CAN_TX_PRES((int16_t) (pres_A.sen), (int16_t) (pres_B.sen)); // CUR_PRES_X : 0(0bar)~4096(210bar) //1400 |
Lightvalve | 62:851cf7b7aa7a | 1644 | } |
Lightvalve | 62:851cf7b7aa7a | 1645 | |
Lightvalve | 62:851cf7b7aa7a | 1646 | //If it doesn't rest, below can can not work. |
Lightvalve | 62:851cf7b7aa7a | 1647 | for (can_rest = 0; can_rest < 10000; can_rest++) { |
Lightvalve | 62:851cf7b7aa7a | 1648 | ; |
Lightvalve | 57:f4819de54e7a | 1649 | } |
Lightvalve | 58:2eade98630e2 | 1650 | |
Lightvalve | 56:6f50d9d3bfee | 1651 | if (flag_data_request[3] == HIGH) { |
Lightvalve | 54:647072f5307a | 1652 | //PWM |
Lightvalve | 62:851cf7b7aa7a | 1653 | CAN_TX_PWM((int16_t) value); //1500 |
Lightvalve | 54:647072f5307a | 1654 | } |
Lightvalve | 62:851cf7b7aa7a | 1655 | //for (i = 0; i < 10000; i++) { |
Lightvalve | 62:851cf7b7aa7a | 1656 | // ; |
Lightvalve | 62:851cf7b7aa7a | 1657 | // } |
Lightvalve | 56:6f50d9d3bfee | 1658 | if (flag_data_request[4] == HIGH) { |
Lightvalve | 54:647072f5307a | 1659 | //valve position |
Lightvalve | 62:851cf7b7aa7a | 1660 | CAN_TX_VALVE_POSITION((int16_t) (CAN_FREQ), (int16_t) (D_DAMPER), (int16_t) OPERATING_MODE); //1600 |
Lightvalve | 54:647072f5307a | 1661 | } |
Lightvalve | 20:806196fda269 | 1662 | |
Lightvalve | 54:647072f5307a | 1663 | // Others : Reference position, Reference FT, PWM, Current (ID:1300) |
Lightvalve | 52:8ea76864368a | 1664 | // if (flag_data_request[1] == HIGH) { |
Lightvalve | 52:8ea76864368a | 1665 | // CAN_TX_SOMETHING((int) (FORCE_VREF), (int16_t) (1), (int16_t) (2), (int16_t) (3)); |
Lightvalve | 52:8ea76864368a | 1666 | // } |
Lightvalve | 54:647072f5307a | 1667 | //if (flag_delay_test == 1){ |
Lightvalve | 62:851cf7b7aa7a | 1668 | //CAN_TX_PRES((int16_t) (0),(int16_t) torq_ref); |
Lightvalve | 54:647072f5307a | 1669 | //} |
Lightvalve | 52:8ea76864368a | 1670 | |
Lightvalve | 54:647072f5307a | 1671 | TMR2_COUNT_CAN_TX = 0; |
Lightvalve | 54:647072f5307a | 1672 | } |
Lightvalve | 54:647072f5307a | 1673 | TMR2_COUNT_CAN_TX++; |
Lightvalve | 52:8ea76864368a | 1674 | |
Lightvalve | 20:806196fda269 | 1675 | } |
Lightvalve | 52:8ea76864368a | 1676 | TIM3->SR = 0x0; // reset the status register |
Lightvalve | 52:8ea76864368a | 1677 | |
Lightvalve | 59:0ad14153b58f | 1678 | } |
Lightvalve | 59:0ad14153b58f | 1679 | |
Lightvalve | 62:851cf7b7aa7a | 1680 | void SystemClock_Config(void) |
Lightvalve | 62:851cf7b7aa7a | 1681 | { |
Lightvalve | 62:851cf7b7aa7a | 1682 | RCC_OscInitTypeDef RCC_OscInitStruct = {0}; |
Lightvalve | 62:851cf7b7aa7a | 1683 | RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; |
Lightvalve | 59:0ad14153b58f | 1684 | |
Lightvalve | 62:851cf7b7aa7a | 1685 | /** Configure the main internal regulator output voltage |
Lightvalve | 62:851cf7b7aa7a | 1686 | */ |
Lightvalve | 62:851cf7b7aa7a | 1687 | __HAL_RCC_PWR_CLK_ENABLE(); |
Lightvalve | 62:851cf7b7aa7a | 1688 | __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); |
Lightvalve | 62:851cf7b7aa7a | 1689 | /** Initializes the CPU, AHB and APB busses clocks |
Lightvalve | 62:851cf7b7aa7a | 1690 | */ |
Lightvalve | 62:851cf7b7aa7a | 1691 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; |
Lightvalve | 62:851cf7b7aa7a | 1692 | RCC_OscInitStruct.HSIState = RCC_HSI_ON; |
Lightvalve | 62:851cf7b7aa7a | 1693 | RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; |
Lightvalve | 62:851cf7b7aa7a | 1694 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
Lightvalve | 62:851cf7b7aa7a | 1695 | RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; |
Lightvalve | 62:851cf7b7aa7a | 1696 | RCC_OscInitStruct.PLL.PLLM = 8; //8 |
Lightvalve | 62:851cf7b7aa7a | 1697 | RCC_OscInitStruct.PLL.PLLN = 180;//180 |
Lightvalve | 62:851cf7b7aa7a | 1698 | RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; |
Lightvalve | 62:851cf7b7aa7a | 1699 | RCC_OscInitStruct.PLL.PLLQ = 2; |
Lightvalve | 62:851cf7b7aa7a | 1700 | RCC_OscInitStruct.PLL.PLLR = 2; |
Lightvalve | 62:851cf7b7aa7a | 1701 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { |
Lightvalve | 62:851cf7b7aa7a | 1702 | //Error_Handler(); |
Lightvalve | 62:851cf7b7aa7a | 1703 | } |
Lightvalve | 62:851cf7b7aa7a | 1704 | /** Activate the Over-Drive mode |
Lightvalve | 62:851cf7b7aa7a | 1705 | */ |
Lightvalve | 62:851cf7b7aa7a | 1706 | if (HAL_PWREx_EnableOverDrive() != HAL_OK) { |
Lightvalve | 62:851cf7b7aa7a | 1707 | //Error_Handler(); |
Lightvalve | 62:851cf7b7aa7a | 1708 | } |
Lightvalve | 62:851cf7b7aa7a | 1709 | /** Initializes the CPU, AHB and APB busses clocks |
Lightvalve | 62:851cf7b7aa7a | 1710 | */ |
Lightvalve | 62:851cf7b7aa7a | 1711 | RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |
Lightvalve | 62:851cf7b7aa7a | 1712 | |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; |
Lightvalve | 62:851cf7b7aa7a | 1713 | RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; |
Lightvalve | 62:851cf7b7aa7a | 1714 | RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; |
Lightvalve | 62:851cf7b7aa7a | 1715 | RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4; |
Lightvalve | 62:851cf7b7aa7a | 1716 | RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; |
Lightvalve | 59:0ad14153b58f | 1717 | |
Lightvalve | 62:851cf7b7aa7a | 1718 | if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) { |
Lightvalve | 62:851cf7b7aa7a | 1719 | //Error_Handler(); |
Lightvalve | 59:0ad14153b58f | 1720 | } |
Lightvalve | 62:851cf7b7aa7a | 1721 | HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_HSI, RCC_MCODIV_1); |
Lightvalve | 62:851cf7b7aa7a | 1722 | HAL_RCC_MCOConfig(RCC_MCO2, RCC_MCO2SOURCE_SYSCLK, RCC_MCODIV_2); |
Lightvalve | 62:851cf7b7aa7a | 1723 | } |