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