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