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