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