123487
Dependencies: mbed-dev-f303 FastPWM3
main.cpp@59:ed4b54437e02, 2020-12-25 (annotated)
- Committer:
- shaorui
- Date:
- Fri Dec 25 07:06:28 2020 +0000
- Revision:
- 59:ed4b54437e02
- Parent:
- 58:bed739c83fac
l
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
benkatz | 22:60276ba87ac6 | 1 | /// high-bandwidth 3-phase motor control, for robots |
benkatz | 45:26801179208e | 2 | /// Written by benkatz, with much inspiration from Bayley Wang, Nick Kirkby, Shane Colton, David Otten, and others |
benkatz | 22:60276ba87ac6 | 3 | /// Hardware documentation can be found at build-its.blogspot.com |
benkatz | 22:60276ba87ac6 | 4 | /// Written for the STM32F446, but can be implemented on other STM32 MCU's with some further register-diddling |
benkatz | 47:e1196a851f76 | 5 | /// Version for the TI DRV8323 Everything Chip |
benkatz | 22:60276ba87ac6 | 6 | |
benkatz | 23:2adf23ee0305 | 7 | #define REST_MODE 0 |
benkatz | 23:2adf23ee0305 | 8 | #define CALIBRATION_MODE 1 |
benkatz | 26:2b865c00d7e9 | 9 | #define MOTOR_MODE 2 |
benkatz | 23:2adf23ee0305 | 10 | #define SETUP_MODE 4 |
benkatz | 23:2adf23ee0305 | 11 | #define ENCODER_MODE 5 |
benkatz | 22:60276ba87ac6 | 12 | |
benkatz | 51:6cd89bd6fcaa | 13 | #define VERSION_NUM "1.9" |
benkatz | 26:2b865c00d7e9 | 14 | |
shaorui | 56:9353ebcf19c6 | 15 | int torque_count=0; //shaorui add |
benkatz | 26:2b865c00d7e9 | 16 | float __float_reg[64]; // Floats stored in flash |
benkatz | 26:2b865c00d7e9 | 17 | int __int_reg[256]; // Ints stored in flash. Includes position sensor calibration lookup table |
benkatz | 17:3c5df2982199 | 18 | |
benkatz | 0:4e1c4df6aabd | 19 | #include "mbed.h" |
benkatz | 0:4e1c4df6aabd | 20 | #include "PositionSensor.h" |
benkatz | 20:bf9ea5125d52 | 21 | #include "structs.h" |
benkatz | 20:bf9ea5125d52 | 22 | #include "foc.h" |
benkatz | 22:60276ba87ac6 | 23 | #include "calibration.h" |
benkatz | 20:bf9ea5125d52 | 24 | #include "hw_setup.h" |
benkatz | 23:2adf23ee0305 | 25 | #include "math_ops.h" |
benkatz | 20:bf9ea5125d52 | 26 | #include "current_controller_config.h" |
benkatz | 20:bf9ea5125d52 | 27 | #include "hw_config.h" |
benkatz | 20:bf9ea5125d52 | 28 | #include "motor_config.h" |
benkatz | 23:2adf23ee0305 | 29 | #include "stm32f4xx_flash.h" |
benkatz | 23:2adf23ee0305 | 30 | #include "FlashWriter.h" |
benkatz | 23:2adf23ee0305 | 31 | #include "user_config.h" |
benkatz | 23:2adf23ee0305 | 32 | #include "PreferenceWriter.h" |
benkatz | 42:738fa01b0346 | 33 | #include "CAN_com.h" |
benkatz | 44:8040fa2fcb0d | 34 | #include "DRV.h" |
benkatz | 26:2b865c00d7e9 | 35 | |
benkatz | 23:2adf23ee0305 | 36 | PreferenceWriter prefs(6); |
benkatz | 9:d7eb815cb057 | 37 | |
benkatz | 20:bf9ea5125d52 | 38 | GPIOStruct gpio; |
benkatz | 20:bf9ea5125d52 | 39 | ControllerStruct controller; |
benkatz | 48:74a40481740c | 40 | ObserverStruct observer; |
benkatz | 20:bf9ea5125d52 | 41 | COMStruct com; |
benkatz | 43:dfb72608639c | 42 | Serial pc(PA_2, PA_3); |
benkatz | 9:d7eb815cb057 | 43 | |
benkatz | 17:3c5df2982199 | 44 | |
benkatz | 45:26801179208e | 45 | CAN can(PB_8, PB_9, 1000000); // CAN Rx pin name, CAN Tx pin name |
benkatz | 26:2b865c00d7e9 | 46 | CANMessage rxMsg; |
benkatz | 26:2b865c00d7e9 | 47 | CANMessage txMsg; |
benkatz | 23:2adf23ee0305 | 48 | |
benkatz | 20:bf9ea5125d52 | 49 | |
benkatz | 44:8040fa2fcb0d | 50 | SPI drv_spi(PA_7, PA_6, PA_5); |
benkatz | 44:8040fa2fcb0d | 51 | DigitalOut drv_cs(PA_4); |
benkatz | 44:8040fa2fcb0d | 52 | //DigitalOut drv_en_gate(PA_11); |
benkatz | 44:8040fa2fcb0d | 53 | DRV832x drv(&drv_spi, &drv_cs); |
benkatz | 8:10ae7bc88d6e | 54 | |
benkatz | 26:2b865c00d7e9 | 55 | PositionSensorAM5147 spi(16384, 0.0, NPP); |
benkatz | 20:bf9ea5125d52 | 56 | |
benkatz | 23:2adf23ee0305 | 57 | volatile int count = 0; |
benkatz | 23:2adf23ee0305 | 58 | volatile int state = REST_MODE; |
benkatz | 23:2adf23ee0305 | 59 | volatile int state_change; |
Rushu | 54:d889fe62bc0c | 60 | volatile int counthjb = 0; |
benkatz | 20:bf9ea5125d52 | 61 | |
benkatz | 26:2b865c00d7e9 | 62 | void onMsgReceived() { |
benkatz | 26:2b865c00d7e9 | 63 | //msgAvailable = true; |
Rushu | 54:d889fe62bc0c | 64 | counthjb++; |
Rushu | 54:d889fe62bc0c | 65 | if(counthjb>100) |
Rushu | 54:d889fe62bc0c | 66 | {printf("%df\n\r", rxMsg.id); |
Rushu | 54:d889fe62bc0c | 67 | counthjb=0; |
Rushu | 54:d889fe62bc0c | 68 | } |
benkatz | 26:2b865c00d7e9 | 69 | can.read(rxMsg); |
benkatz | 28:8c7e29f719c5 | 70 | if((rxMsg.id == CAN_ID)){ |
benkatz | 28:8c7e29f719c5 | 71 | controller.timeout = 0; |
benkatz | 28:8c7e29f719c5 | 72 | if(((rxMsg.data[0]==0xFF) & (rxMsg.data[1]==0xFF) & (rxMsg.data[2]==0xFF) & (rxMsg.data[3]==0xFF) & (rxMsg.data[4]==0xFF) & (rxMsg.data[5]==0xFF) & (rxMsg.data[6]==0xFF) & (rxMsg.data[7]==0xFC))){ |
benkatz | 28:8c7e29f719c5 | 73 | state = MOTOR_MODE; |
benkatz | 28:8c7e29f719c5 | 74 | state_change = 1; |
benkatz | 28:8c7e29f719c5 | 75 | } |
benkatz | 28:8c7e29f719c5 | 76 | else if(((rxMsg.data[0]==0xFF) & (rxMsg.data[1]==0xFF) & (rxMsg.data[2]==0xFF) & (rxMsg.data[3]==0xFF) * (rxMsg.data[4]==0xFF) & (rxMsg.data[5]==0xFF) & (rxMsg.data[6]==0xFF) & (rxMsg.data[7]==0xFD))){ |
benkatz | 28:8c7e29f719c5 | 77 | state = REST_MODE; |
benkatz | 28:8c7e29f719c5 | 78 | state_change = 1; |
benkatz | 37:c0f352d6e8e3 | 79 | gpio.led->write(0);; |
benkatz | 28:8c7e29f719c5 | 80 | } |
benkatz | 28:8c7e29f719c5 | 81 | else if(((rxMsg.data[0]==0xFF) & (rxMsg.data[1]==0xFF) & (rxMsg.data[2]==0xFF) & (rxMsg.data[3]==0xFF) * (rxMsg.data[4]==0xFF) & (rxMsg.data[5]==0xFF) & (rxMsg.data[6]==0xFF) & (rxMsg.data[7]==0xFE))){ |
benkatz | 28:8c7e29f719c5 | 82 | spi.ZeroPosition(); |
benkatz | 28:8c7e29f719c5 | 83 | } |
benkatz | 28:8c7e29f719c5 | 84 | else if(state == MOTOR_MODE){ |
benkatz | 28:8c7e29f719c5 | 85 | unpack_cmd(rxMsg, &controller); |
benkatz | 28:8c7e29f719c5 | 86 | } |
benkatz | 37:c0f352d6e8e3 | 87 | pack_reply(&txMsg, controller.theta_mech, controller.dtheta_mech, controller.i_q_filt*KT_OUT); |
benkatz | 37:c0f352d6e8e3 | 88 | can.write(txMsg); |
benkatz | 28:8c7e29f719c5 | 89 | } |
benkatz | 26:2b865c00d7e9 | 90 | |
benkatz | 26:2b865c00d7e9 | 91 | } |
benkatz | 26:2b865c00d7e9 | 92 | |
benkatz | 23:2adf23ee0305 | 93 | void enter_menu_state(void){ |
benkatz | 44:8040fa2fcb0d | 94 | drv.disable_gd(); |
benkatz | 47:e1196a851f76 | 95 | //gpio.enable->write(0); |
benkatz | 23:2adf23ee0305 | 96 | printf("\n\r\n\r\n\r"); |
benkatz | 23:2adf23ee0305 | 97 | printf(" Commands:\n\r"); |
benkatz | 44:8040fa2fcb0d | 98 | wait_us(10); |
benkatz | 26:2b865c00d7e9 | 99 | printf(" m - Motor Mode\n\r"); |
benkatz | 44:8040fa2fcb0d | 100 | wait_us(10); |
benkatz | 23:2adf23ee0305 | 101 | printf(" c - Calibrate Encoder\n\r"); |
benkatz | 44:8040fa2fcb0d | 102 | wait_us(10); |
benkatz | 23:2adf23ee0305 | 103 | printf(" s - Setup\n\r"); |
benkatz | 44:8040fa2fcb0d | 104 | wait_us(10); |
benkatz | 23:2adf23ee0305 | 105 | printf(" e - Display Encoder\n\r"); |
benkatz | 44:8040fa2fcb0d | 106 | wait_us(10); |
benkatz | 37:c0f352d6e8e3 | 107 | printf(" z - Set Zero Position\n\r"); |
benkatz | 44:8040fa2fcb0d | 108 | wait_us(10); |
benkatz | 23:2adf23ee0305 | 109 | printf(" esc - Exit to Menu\n\r"); |
benkatz | 44:8040fa2fcb0d | 110 | wait_us(10); |
benkatz | 23:2adf23ee0305 | 111 | state_change = 0; |
benkatz | 37:c0f352d6e8e3 | 112 | gpio.led->write(0); |
benkatz | 23:2adf23ee0305 | 113 | } |
benkatz | 24:58c2d7571207 | 114 | |
benkatz | 24:58c2d7571207 | 115 | void enter_setup_state(void){ |
benkatz | 24:58c2d7571207 | 116 | printf("\n\r\n\r Configuration Options \n\r\n\n"); |
benkatz | 44:8040fa2fcb0d | 117 | wait_us(10); |
benkatz | 51:6cd89bd6fcaa | 118 | printf(" %-4s %-31s %-5s %-6s %-2s\n\r\n\r", "prefix", "parameter", "min", "max", "current value"); |
benkatz | 44:8040fa2fcb0d | 119 | wait_us(10); |
benkatz | 28:8c7e29f719c5 | 120 | printf(" %-4s %-31s %-5s %-6s %.1f\n\r", "b", "Current Bandwidth (Hz)", "100", "2000", I_BW); |
benkatz | 44:8040fa2fcb0d | 121 | wait_us(10); |
benkatz | 28:8c7e29f719c5 | 122 | printf(" %-4s %-31s %-5s %-6s %-5i\n\r", "i", "CAN ID", "0", "127", CAN_ID); |
benkatz | 44:8040fa2fcb0d | 123 | wait_us(10); |
benkatz | 28:8c7e29f719c5 | 124 | printf(" %-4s %-31s %-5s %-6s %-5i\n\r", "m", "CAN Master ID", "0", "127", CAN_MASTER); |
benkatz | 44:8040fa2fcb0d | 125 | wait_us(10); |
benkatz | 51:6cd89bd6fcaa | 126 | printf(" %-4s %-31s %-5s %-6s %.1f\n\r", "l", "Current Limit (A)", "0.0", "40.0", I_MAX); |
benkatz | 51:6cd89bd6fcaa | 127 | wait_us(10); |
benkatz | 51:6cd89bd6fcaa | 128 | printf(" %-4s %-31s %-5s %-6s %.1f\n\r", "f", "FW Current Limit (A)", "0.0", "33.0", I_FW_MAX); |
benkatz | 44:8040fa2fcb0d | 129 | wait_us(10); |
benkatz | 28:8c7e29f719c5 | 130 | printf(" %-4s %-31s %-5s %-6s %d\n\r", "t", "CAN Timeout (cycles)(0 = none)", "0", "100000", CAN_TIMEOUT); |
benkatz | 44:8040fa2fcb0d | 131 | wait_us(10); |
benkatz | 24:58c2d7571207 | 132 | printf("\n\r To change a value, type 'prefix''value''ENTER'\n\r i.e. 'b1000''ENTER'\n\r\n\r"); |
benkatz | 44:8040fa2fcb0d | 133 | wait_us(10); |
benkatz | 24:58c2d7571207 | 134 | state_change = 0; |
benkatz | 24:58c2d7571207 | 135 | } |
benkatz | 22:60276ba87ac6 | 136 | |
benkatz | 23:2adf23ee0305 | 137 | void enter_torque_mode(void){ |
benkatz | 44:8040fa2fcb0d | 138 | drv.enable_gd(); |
benkatz | 47:e1196a851f76 | 139 | //gpio.enable->write(1); |
benkatz | 37:c0f352d6e8e3 | 140 | controller.ovp_flag = 0; |
benkatz | 28:8c7e29f719c5 | 141 | reset_foc(&controller); // Tesets integrators, and other control loop parameters |
benkatz | 28:8c7e29f719c5 | 142 | wait(.001); |
benkatz | 23:2adf23ee0305 | 143 | controller.i_d_ref = 0; |
benkatz | 50:ba72df25d10f | 144 | controller.i_q_ref = 0; // Current Setpoints |
benkatz | 37:c0f352d6e8e3 | 145 | gpio.led->write(1); // Turn on status LED |
benkatz | 25:f5741040c4bb | 146 | state_change = 0; |
benkatz | 28:8c7e29f719c5 | 147 | printf("\n\r Entering Motor Mode \n\r"); |
benkatz | 23:2adf23ee0305 | 148 | } |
benkatz | 22:60276ba87ac6 | 149 | |
benkatz | 23:2adf23ee0305 | 150 | void calibrate(void){ |
benkatz | 44:8040fa2fcb0d | 151 | drv.enable_gd(); |
benkatz | 47:e1196a851f76 | 152 | //gpio.enable->write(1); |
benkatz | 37:c0f352d6e8e3 | 153 | gpio.led->write(1); // Turn on status LED |
benkatz | 25:f5741040c4bb | 154 | order_phases(&spi, &gpio, &controller, &prefs); // Check phase ordering |
benkatz | 25:f5741040c4bb | 155 | calibrate(&spi, &gpio, &controller, &prefs); // Perform calibration procedure |
benkatz | 37:c0f352d6e8e3 | 156 | gpio.led->write(0);; // Turn off status LED |
benkatz | 23:2adf23ee0305 | 157 | wait(.2); |
benkatz | 23:2adf23ee0305 | 158 | printf("\n\r Calibration complete. Press 'esc' to return to menu\n\r"); |
benkatz | 44:8040fa2fcb0d | 159 | drv.disable_gd(); |
benkatz | 47:e1196a851f76 | 160 | //gpio.enable->write(0); |
benkatz | 23:2adf23ee0305 | 161 | state_change = 0; |
benkatz | 23:2adf23ee0305 | 162 | } |
benkatz | 23:2adf23ee0305 | 163 | |
benkatz | 23:2adf23ee0305 | 164 | void print_encoder(void){ |
benkatz | 48:74a40481740c | 165 | printf(" Mechanical Angle: %f Electrical Angle: %f Raw: %d\n\r", spi.GetMechPosition(), spi.GetElecPosition(), spi.GetRawPosition()); |
benkatz | 48:74a40481740c | 166 | //printf("%d\n\r", spi.GetRawPosition()); |
benkatz | 47:e1196a851f76 | 167 | wait(.001); |
benkatz | 22:60276ba87ac6 | 168 | } |
benkatz | 20:bf9ea5125d52 | 169 | |
benkatz | 23:2adf23ee0305 | 170 | /// Current Sampling Interrupt /// |
benkatz | 23:2adf23ee0305 | 171 | /// This runs at 40 kHz, regardless of of the mode the controller is in /// |
benkatz | 2:8724412ad628 | 172 | extern "C" void TIM1_UP_TIM10_IRQHandler(void) { |
benkatz | 2:8724412ad628 | 173 | if (TIM1->SR & TIM_SR_UIF ) { |
benkatz | 23:2adf23ee0305 | 174 | |
benkatz | 23:2adf23ee0305 | 175 | ///Sample current always /// |
benkatz | 25:f5741040c4bb | 176 | ADC1->CR2 |= 0x40000000; // Begin sample and conversion |
benkatz | 22:60276ba87ac6 | 177 | //volatile int delay; |
benkatz | 20:bf9ea5125d52 | 178 | //for (delay = 0; delay < 55; delay++); |
benkatz | 45:26801179208e | 179 | |
benkatz | 47:e1196a851f76 | 180 | spi.Sample(DT); // sample position sensor |
benkatz | 37:c0f352d6e8e3 | 181 | controller.adc2_raw = ADC2->DR; // Read ADC Data Registers |
benkatz | 23:2adf23ee0305 | 182 | controller.adc1_raw = ADC1->DR; |
benkatz | 37:c0f352d6e8e3 | 183 | controller.adc3_raw = ADC3->DR; |
benkatz | 37:c0f352d6e8e3 | 184 | controller.theta_elec = spi.GetElecPosition(); |
benkatz | 37:c0f352d6e8e3 | 185 | controller.theta_mech = (1.0f/GR)*spi.GetMechPosition(); |
benkatz | 37:c0f352d6e8e3 | 186 | controller.dtheta_mech = (1.0f/GR)*spi.GetMechVelocity(); |
benkatz | 37:c0f352d6e8e3 | 187 | controller.dtheta_elec = spi.GetElecVelocity(); |
benkatz | 51:6cd89bd6fcaa | 188 | controller.v_bus = 0.95f*controller.v_bus + 0.05f*((float)controller.adc3_raw)*V_SCALE; //filter the dc link voltage measurement |
benkatz | 23:2adf23ee0305 | 189 | /// |
benkatz | 20:bf9ea5125d52 | 190 | |
benkatz | 23:2adf23ee0305 | 191 | /// Check state machine state, and run the appropriate function /// |
benkatz | 23:2adf23ee0305 | 192 | switch(state){ |
benkatz | 37:c0f352d6e8e3 | 193 | case REST_MODE: // Do nothing |
benkatz | 23:2adf23ee0305 | 194 | if(state_change){ |
benkatz | 23:2adf23ee0305 | 195 | enter_menu_state(); |
benkatz | 23:2adf23ee0305 | 196 | } |
benkatz | 23:2adf23ee0305 | 197 | break; |
benkatz | 22:60276ba87ac6 | 198 | |
benkatz | 23:2adf23ee0305 | 199 | case CALIBRATION_MODE: // Run encoder calibration procedure |
benkatz | 23:2adf23ee0305 | 200 | if(state_change){ |
benkatz | 23:2adf23ee0305 | 201 | calibrate(); |
benkatz | 23:2adf23ee0305 | 202 | } |
benkatz | 23:2adf23ee0305 | 203 | break; |
benkatz | 23:2adf23ee0305 | 204 | |
benkatz | 26:2b865c00d7e9 | 205 | case MOTOR_MODE: // Run torque control |
benkatz | 25:f5741040c4bb | 206 | if(state_change){ |
benkatz | 25:f5741040c4bb | 207 | enter_torque_mode(); |
benkatz | 28:8c7e29f719c5 | 208 | count = 0; |
benkatz | 25:f5741040c4bb | 209 | } |
benkatz | 28:8c7e29f719c5 | 210 | else{ |
benkatz | 37:c0f352d6e8e3 | 211 | /* |
benkatz | 37:c0f352d6e8e3 | 212 | if(controller.v_bus>28.0f){ //Turn of gate drive if bus voltage is too high, to prevent FETsplosion if the bus is cut during regen |
benkatz | 47:e1196a851f76 | 213 | gpio. |
benkatz | 47:e1196a851f76 | 214 | ->write(0); |
benkatz | 37:c0f352d6e8e3 | 215 | controller.ovp_flag = 1; |
benkatz | 37:c0f352d6e8e3 | 216 | state = REST_MODE; |
benkatz | 37:c0f352d6e8e3 | 217 | state_change = 1; |
benkatz | 37:c0f352d6e8e3 | 218 | printf("OVP Triggered!\n\r"); |
benkatz | 37:c0f352d6e8e3 | 219 | } |
benkatz | 37:c0f352d6e8e3 | 220 | */ |
benkatz | 37:c0f352d6e8e3 | 221 | |
benkatz | 28:8c7e29f719c5 | 222 | if((controller.timeout > CAN_TIMEOUT) && (CAN_TIMEOUT > 0)){ |
benkatz | 28:8c7e29f719c5 | 223 | controller.i_d_ref = 0; |
benkatz | 28:8c7e29f719c5 | 224 | controller.i_q_ref = 0; |
benkatz | 37:c0f352d6e8e3 | 225 | controller.kp = 0; |
benkatz | 37:c0f352d6e8e3 | 226 | controller.kd = 0; |
benkatz | 37:c0f352d6e8e3 | 227 | controller.t_ff = 0; |
benkatz | 28:8c7e29f719c5 | 228 | } |
benkatz | 47:e1196a851f76 | 229 | |
benkatz | 50:ba72df25d10f | 230 | torque_control(&controller); |
benkatz | 49:83d83040ea51 | 231 | commutate(&controller, &observer, &gpio, controller.theta_elec); // Run current loop |
benkatz | 49:83d83040ea51 | 232 | |
benkatz | 49:83d83040ea51 | 233 | controller.timeout++; |
benkatz | 49:83d83040ea51 | 234 | count++; |
shaorui | 56:9353ebcf19c6 | 235 | /***********shaorui add for torque_des**********/ |
shaorui | 56:9353ebcf19c6 | 236 | // torque_count++; |
shaorui | 56:9353ebcf19c6 | 237 | // if(torque_count>50000) |
shaorui | 56:9353ebcf19c6 | 238 | // { |
shaorui | 56:9353ebcf19c6 | 239 | // printf("the desired iq : %.3f\n\r",controller. i_q_ref); |
shaorui | 56:9353ebcf19c6 | 240 | // printf("the desired torque : %.3f\n\r",KT_OUT*controller. i_q_ref); |
shaorui | 56:9353ebcf19c6 | 241 | // torque_count=0; |
shaorui | 56:9353ebcf19c6 | 242 | // } |
shaorui | 56:9353ebcf19c6 | 243 | /***********shaorui endfor torque_des**********/ |
benkatz | 37:c0f352d6e8e3 | 244 | } |
benkatz | 23:2adf23ee0305 | 245 | break; |
benkatz | 23:2adf23ee0305 | 246 | case SETUP_MODE: |
benkatz | 23:2adf23ee0305 | 247 | if(state_change){ |
benkatz | 24:58c2d7571207 | 248 | enter_setup_state(); |
benkatz | 23:2adf23ee0305 | 249 | } |
benkatz | 23:2adf23ee0305 | 250 | break; |
benkatz | 23:2adf23ee0305 | 251 | case ENCODER_MODE: |
benkatz | 23:2adf23ee0305 | 252 | print_encoder(); |
benkatz | 23:2adf23ee0305 | 253 | break; |
benkatz | 37:c0f352d6e8e3 | 254 | } |
benkatz | 2:8724412ad628 | 255 | } |
benkatz | 23:2adf23ee0305 | 256 | TIM1->SR = 0x0; // reset the status register |
benkatz | 2:8724412ad628 | 257 | } |
benkatz | 0:4e1c4df6aabd | 258 | |
benkatz | 25:f5741040c4bb | 259 | |
benkatz | 24:58c2d7571207 | 260 | char cmd_val[8] = {0}; |
benkatz | 24:58c2d7571207 | 261 | char cmd_id = 0; |
benkatz | 25:f5741040c4bb | 262 | char char_count = 0; |
benkatz | 24:58c2d7571207 | 263 | |
benkatz | 25:f5741040c4bb | 264 | /// Manage state machine with commands from serial terminal or configurator gui /// |
benkatz | 25:f5741040c4bb | 265 | /// Called when data received over serial /// |
benkatz | 23:2adf23ee0305 | 266 | void serial_interrupt(void){ |
benkatz | 23:2adf23ee0305 | 267 | while(pc.readable()){ |
benkatz | 23:2adf23ee0305 | 268 | char c = pc.getc(); |
benkatz | 25:f5741040c4bb | 269 | if(c == 27){ |
Rushu | 54:d889fe62bc0c | 270 | //===============================================HJB added====================================================// |
Rushu | 54:d889fe62bc0c | 271 | wait_us(100); //HJB add, to clear fault |
Rushu | 54:d889fe62bc0c | 272 | drv.write_DCR(0x0, 0x0, 0x0, PWM_MODE_3X, 0x0, 0x0, 0x0, 0x0, 0x1); //HJB add, to clear fault |
Rushu | 54:d889fe62bc0c | 273 | //Init_pos = controller.theta_mech; //Input the local mechanical theta |
Rushu | 54:d889fe62bc0c | 274 | //===============================================HJB ended====================================================// |
benkatz | 25:f5741040c4bb | 275 | state = REST_MODE; |
benkatz | 25:f5741040c4bb | 276 | state_change = 1; |
benkatz | 25:f5741040c4bb | 277 | char_count = 0; |
benkatz | 25:f5741040c4bb | 278 | cmd_id = 0; |
benkatz | 37:c0f352d6e8e3 | 279 | gpio.led->write(0);; |
benkatz | 25:f5741040c4bb | 280 | for(int i = 0; i<8; i++){cmd_val[i] = 0;} |
benkatz | 25:f5741040c4bb | 281 | } |
benkatz | 24:58c2d7571207 | 282 | if(state == REST_MODE){ |
benkatz | 23:2adf23ee0305 | 283 | switch (c){ |
benkatz | 23:2adf23ee0305 | 284 | case 'c': |
benkatz | 23:2adf23ee0305 | 285 | state = CALIBRATION_MODE; |
benkatz | 23:2adf23ee0305 | 286 | state_change = 1; |
benkatz | 23:2adf23ee0305 | 287 | break; |
benkatz | 26:2b865c00d7e9 | 288 | case 'm': |
benkatz | 26:2b865c00d7e9 | 289 | state = MOTOR_MODE; |
benkatz | 23:2adf23ee0305 | 290 | state_change = 1; |
benkatz | 23:2adf23ee0305 | 291 | break; |
benkatz | 23:2adf23ee0305 | 292 | case 'e': |
benkatz | 23:2adf23ee0305 | 293 | state = ENCODER_MODE; |
benkatz | 23:2adf23ee0305 | 294 | state_change = 1; |
benkatz | 23:2adf23ee0305 | 295 | break; |
benkatz | 23:2adf23ee0305 | 296 | case 's': |
benkatz | 23:2adf23ee0305 | 297 | state = SETUP_MODE; |
benkatz | 23:2adf23ee0305 | 298 | state_change = 1; |
benkatz | 23:2adf23ee0305 | 299 | break; |
benkatz | 37:c0f352d6e8e3 | 300 | case 'z': |
benkatz | 37:c0f352d6e8e3 | 301 | spi.SetMechOffset(0); |
benkatz | 47:e1196a851f76 | 302 | spi.Sample(DT); |
benkatz | 37:c0f352d6e8e3 | 303 | wait_us(20); |
benkatz | 37:c0f352d6e8e3 | 304 | M_OFFSET = spi.GetMechPosition(); |
benkatz | 37:c0f352d6e8e3 | 305 | if (!prefs.ready()) prefs.open(); |
benkatz | 37:c0f352d6e8e3 | 306 | prefs.flush(); // Write new prefs to flash |
benkatz | 37:c0f352d6e8e3 | 307 | prefs.close(); |
benkatz | 37:c0f352d6e8e3 | 308 | prefs.load(); |
benkatz | 37:c0f352d6e8e3 | 309 | spi.SetMechOffset(M_OFFSET); |
benkatz | 37:c0f352d6e8e3 | 310 | printf("\n\r Saved new zero position: %.4f\n\r\n\r", M_OFFSET); |
benkatz | 37:c0f352d6e8e3 | 311 | |
benkatz | 37:c0f352d6e8e3 | 312 | break; |
benkatz | 37:c0f352d6e8e3 | 313 | } |
benkatz | 37:c0f352d6e8e3 | 314 | |
benkatz | 24:58c2d7571207 | 315 | } |
benkatz | 24:58c2d7571207 | 316 | else if(state == SETUP_MODE){ |
benkatz | 25:f5741040c4bb | 317 | if(c == 13){ |
benkatz | 24:58c2d7571207 | 318 | switch (cmd_id){ |
benkatz | 24:58c2d7571207 | 319 | case 'b': |
benkatz | 24:58c2d7571207 | 320 | I_BW = fmaxf(fminf(atof(cmd_val), 2000.0f), 100.0f); |
benkatz | 24:58c2d7571207 | 321 | break; |
benkatz | 24:58c2d7571207 | 322 | case 'i': |
benkatz | 24:58c2d7571207 | 323 | CAN_ID = atoi(cmd_val); |
benkatz | 24:58c2d7571207 | 324 | break; |
benkatz | 26:2b865c00d7e9 | 325 | case 'm': |
benkatz | 26:2b865c00d7e9 | 326 | CAN_MASTER = atoi(cmd_val); |
benkatz | 26:2b865c00d7e9 | 327 | break; |
benkatz | 24:58c2d7571207 | 328 | case 'l': |
benkatz | 51:6cd89bd6fcaa | 329 | I_MAX = fmaxf(fminf(atof(cmd_val), 40.0f), 0.0f); |
benkatz | 51:6cd89bd6fcaa | 330 | break; |
benkatz | 51:6cd89bd6fcaa | 331 | case 'f': |
benkatz | 51:6cd89bd6fcaa | 332 | I_FW_MAX = fmaxf(fminf(atof(cmd_val), 33.0f), 0.0f); |
benkatz | 24:58c2d7571207 | 333 | break; |
benkatz | 28:8c7e29f719c5 | 334 | case 't': |
benkatz | 28:8c7e29f719c5 | 335 | CAN_TIMEOUT = atoi(cmd_val); |
benkatz | 28:8c7e29f719c5 | 336 | break; |
benkatz | 24:58c2d7571207 | 337 | default: |
benkatz | 24:58c2d7571207 | 338 | printf("\n\r '%c' Not a valid command prefix\n\r\n\r", cmd_id); |
benkatz | 24:58c2d7571207 | 339 | break; |
benkatz | 24:58c2d7571207 | 340 | } |
benkatz | 24:58c2d7571207 | 341 | |
benkatz | 24:58c2d7571207 | 342 | if (!prefs.ready()) prefs.open(); |
benkatz | 24:58c2d7571207 | 343 | prefs.flush(); // Write new prefs to flash |
benkatz | 24:58c2d7571207 | 344 | prefs.close(); |
benkatz | 24:58c2d7571207 | 345 | prefs.load(); |
benkatz | 24:58c2d7571207 | 346 | state_change = 1; |
benkatz | 24:58c2d7571207 | 347 | char_count = 0; |
benkatz | 24:58c2d7571207 | 348 | cmd_id = 0; |
benkatz | 24:58c2d7571207 | 349 | for(int i = 0; i<8; i++){cmd_val[i] = 0;} |
benkatz | 24:58c2d7571207 | 350 | } |
benkatz | 24:58c2d7571207 | 351 | else{ |
benkatz | 24:58c2d7571207 | 352 | if(char_count == 0){cmd_id = c;} |
benkatz | 24:58c2d7571207 | 353 | else{ |
benkatz | 24:58c2d7571207 | 354 | cmd_val[char_count-1] = c; |
benkatz | 24:58c2d7571207 | 355 | |
benkatz | 24:58c2d7571207 | 356 | } |
benkatz | 24:58c2d7571207 | 357 | pc.putc(c); |
benkatz | 24:58c2d7571207 | 358 | char_count++; |
benkatz | 23:2adf23ee0305 | 359 | } |
benkatz | 23:2adf23ee0305 | 360 | } |
benkatz | 24:58c2d7571207 | 361 | else if (state == ENCODER_MODE){ |
benkatz | 24:58c2d7571207 | 362 | switch (c){ |
benkatz | 24:58c2d7571207 | 363 | case 27: |
benkatz | 24:58c2d7571207 | 364 | state = REST_MODE; |
benkatz | 24:58c2d7571207 | 365 | state_change = 1; |
benkatz | 24:58c2d7571207 | 366 | break; |
benkatz | 24:58c2d7571207 | 367 | } |
benkatz | 24:58c2d7571207 | 368 | } |
benkatz | 49:83d83040ea51 | 369 | else if (state == MOTOR_MODE){ |
benkatz | 49:83d83040ea51 | 370 | switch (c){ |
benkatz | 49:83d83040ea51 | 371 | case 'd': |
benkatz | 49:83d83040ea51 | 372 | controller.i_q_ref = 0; |
benkatz | 49:83d83040ea51 | 373 | controller.i_d_ref = 0; |
benkatz | 49:83d83040ea51 | 374 | } |
benkatz | 49:83d83040ea51 | 375 | } |
benkatz | 24:58c2d7571207 | 376 | |
benkatz | 24:58c2d7571207 | 377 | } |
benkatz | 22:60276ba87ac6 | 378 | } |
benkatz | 0:4e1c4df6aabd | 379 | |
benkatz | 0:4e1c4df6aabd | 380 | int main() { |
Rushu | 54:d889fe62bc0c | 381 | wait(1); //hjb added |
benkatz | 20:bf9ea5125d52 | 382 | controller.v_bus = V_BUS; |
benkatz | 22:60276ba87ac6 | 383 | controller.mode = 0; |
benkatz | 23:2adf23ee0305 | 384 | Init_All_HW(&gpio); // Setup PWM, ADC, GPIO |
benkatz | 44:8040fa2fcb0d | 385 | wait(.1); |
benkatz | 44:8040fa2fcb0d | 386 | |
benkatz | 44:8040fa2fcb0d | 387 | gpio.enable->write(1); |
benkatz | 44:8040fa2fcb0d | 388 | wait_us(100); |
benkatz | 45:26801179208e | 389 | drv.calibrate(); |
benkatz | 45:26801179208e | 390 | wait_us(100); |
benkatz | 44:8040fa2fcb0d | 391 | drv.write_DCR(0x0, 0x0, 0x0, PWM_MODE_3X, 0x0, 0x0, 0x0, 0x0, 0x1); |
benkatz | 44:8040fa2fcb0d | 392 | wait_us(100); |
benkatz | 46:2d4b1dafcfe3 | 393 | drv.write_CSACR(0x0, 0x1, 0x0, CSA_GAIN_40, 0x0, 0x0, 0x0, 0x0, SEN_LVL_1_0); |
benkatz | 44:8040fa2fcb0d | 394 | wait_us(100); |
benkatz | 49:83d83040ea51 | 395 | drv.write_OCPCR(TRETRY_4MS, DEADTIME_200NS, OCP_RETRY, OCP_DEG_8US, VDS_LVL_1_88); |
benkatz | 45:26801179208e | 396 | |
benkatz | 45:26801179208e | 397 | //drv.enable_gd(); |
benkatz | 44:8040fa2fcb0d | 398 | zero_current(&controller.adc1_offset, &controller.adc2_offset); // Measure current sensor zero-offset |
benkatz | 47:e1196a851f76 | 399 | drv.disable_gd(); |
benkatz | 20:bf9ea5125d52 | 400 | |
benkatz | 9:d7eb815cb057 | 401 | wait(.1); |
benkatz | 44:8040fa2fcb0d | 402 | /* |
benkatz | 26:2b865c00d7e9 | 403 | gpio.enable->write(1); |
benkatz | 26:2b865c00d7e9 | 404 | TIM1->CCR3 = 0x708*(1.0f); // Write duty cycles |
benkatz | 26:2b865c00d7e9 | 405 | TIM1->CCR2 = 0x708*(1.0f); |
benkatz | 26:2b865c00d7e9 | 406 | TIM1->CCR1 = 0x708*(1.0f); |
benkatz | 26:2b865c00d7e9 | 407 | gpio.enable->write(0); |
benkatz | 44:8040fa2fcb0d | 408 | */ |
benkatz | 23:2adf23ee0305 | 409 | reset_foc(&controller); // Reset current controller |
benkatz | 48:74a40481740c | 410 | reset_observer(&observer); // Reset observer |
benkatz | 26:2b865c00d7e9 | 411 | TIM1->CR1 ^= TIM_CR1_UDIS; |
benkatz | 26:2b865c00d7e9 | 412 | //TIM1->CR1 |= TIM_CR1_UDIS; //enable interrupt |
benkatz | 20:bf9ea5125d52 | 413 | |
benkatz | 20:bf9ea5125d52 | 414 | wait(.1); |
benkatz | 37:c0f352d6e8e3 | 415 | NVIC_SetPriority(TIM1_UP_TIM10_IRQn, 2); // commutation > communication |
benkatz | 43:dfb72608639c | 416 | |
benkatz | 37:c0f352d6e8e3 | 417 | NVIC_SetPriority(CAN1_RX0_IRQn, 3); |
benkatz | 26:2b865c00d7e9 | 418 | can.filter(CAN_ID<<21, 0xFFE00004, CANStandard, 0); |
benkatz | 43:dfb72608639c | 419 | |
benkatz | 28:8c7e29f719c5 | 420 | txMsg.id = CAN_MASTER; |
benkatz | 28:8c7e29f719c5 | 421 | txMsg.len = 6; |
benkatz | 26:2b865c00d7e9 | 422 | rxMsg.len = 8; |
benkatz | 43:dfb72608639c | 423 | can.attach(&onMsgReceived); // attach 'CAN receive-complete' interrupt handler |
benkatz | 23:2adf23ee0305 | 424 | |
benkatz | 48:74a40481740c | 425 | // If preferences haven't been user configured yet, set defaults |
benkatz | 25:f5741040c4bb | 426 | prefs.load(); // Read flash |
benkatz | 37:c0f352d6e8e3 | 427 | if(isnan(E_OFFSET)){E_OFFSET = 0.0f;} |
benkatz | 37:c0f352d6e8e3 | 428 | if(isnan(M_OFFSET)){M_OFFSET = 0.0f;} |
benkatz | 48:74a40481740c | 429 | if(isnan(I_BW) || I_BW==-1){I_BW = 1000;} |
benkatz | 51:6cd89bd6fcaa | 430 | if(isnan(I_MAX) || I_MAX ==-1){I_MAX=40;} |
benkatz | 51:6cd89bd6fcaa | 431 | if(isnan(I_FW_MAX) || I_FW_MAX ==-1){I_FW_MAX=0;} |
benkatz | 48:74a40481740c | 432 | if(isnan(CAN_ID) || CAN_ID==-1){CAN_ID = 1;} |
benkatz | 48:74a40481740c | 433 | if(isnan(CAN_MASTER) || CAN_MASTER==-1){CAN_MASTER = 0;} |
benkatz | 48:74a40481740c | 434 | if(isnan(CAN_TIMEOUT) || CAN_TIMEOUT==-1){CAN_TIMEOUT = 0;} |
benkatz | 25:f5741040c4bb | 435 | spi.SetElecOffset(E_OFFSET); // Set position sensor offset |
benkatz | 37:c0f352d6e8e3 | 436 | spi.SetMechOffset(M_OFFSET); |
benkatz | 23:2adf23ee0305 | 437 | int lut[128] = {0}; |
benkatz | 23:2adf23ee0305 | 438 | memcpy(&lut, &ENCODER_LUT, sizeof(lut)); |
benkatz | 25:f5741040c4bb | 439 | spi.WriteLUT(lut); // Set potision sensor nonlinearity lookup table |
benkatz | 45:26801179208e | 440 | init_controller_params(&controller); |
benkatz | 45:26801179208e | 441 | |
Rushu | 54:d889fe62bc0c | 442 | pc.baud(460800); // set serial baud rate |
benkatz | 20:bf9ea5125d52 | 443 | wait(.01); |
benkatz | 23:2adf23ee0305 | 444 | pc.printf("\n\r\n\r HobbyKing Cheetah\n\r\n\r"); |
benkatz | 20:bf9ea5125d52 | 445 | wait(.01); |
benkatz | 23:2adf23ee0305 | 446 | printf("\n\r Debug Info:\n\r"); |
benkatz | 32:ccac5da77844 | 447 | printf(" Firmware Version: %s\n\r", VERSION_NUM); |
benkatz | 23:2adf23ee0305 | 448 | printf(" ADC1 Offset: %d ADC2 Offset: %d\n\r", controller.adc1_offset, controller.adc2_offset); |
benkatz | 23:2adf23ee0305 | 449 | printf(" Position Sensor Electrical Offset: %.4f\n\r", E_OFFSET); |
benkatz | 37:c0f352d6e8e3 | 450 | printf(" Output Zero Position: %.4f\n\r", M_OFFSET); |
benkatz | 24:58c2d7571207 | 451 | printf(" CAN ID: %d\n\r", CAN_ID); |
benkatz | 44:8040fa2fcb0d | 452 | |
benkatz | 44:8040fa2fcb0d | 453 | |
benkatz | 44:8040fa2fcb0d | 454 | |
benkatz | 44:8040fa2fcb0d | 455 | |
benkatz | 47:e1196a851f76 | 456 | //printf(" %d\n\r", drv.read_register(DCR)); |
benkatz | 47:e1196a851f76 | 457 | //wait_us(100); |
benkatz | 47:e1196a851f76 | 458 | //printf(" %d\n\r", drv.read_register(CSACR)); |
benkatz | 47:e1196a851f76 | 459 | //wait_us(100); |
benkatz | 47:e1196a851f76 | 460 | //printf(" %d\n\r", drv.read_register(OCPCR)); |
benkatz | 47:e1196a851f76 | 461 | //drv.disable_gd(); |
benkatz | 44:8040fa2fcb0d | 462 | |
benkatz | 23:2adf23ee0305 | 463 | pc.attach(&serial_interrupt); // attach serial interrupt |
benkatz | 22:60276ba87ac6 | 464 | |
benkatz | 23:2adf23ee0305 | 465 | state_change = 1; |
benkatz | 50:ba72df25d10f | 466 | |
benkatz | 20:bf9ea5125d52 | 467 | |
benkatz | 44:8040fa2fcb0d | 468 | int counter = 0; |
Rushu | 54:d889fe62bc0c | 469 | //===============================================HJB added====================================================// |
Rushu | 54:d889fe62bc0c | 470 | wait_us(100); //HJB add, to clear fault |
Rushu | 54:d889fe62bc0c | 471 | drv.write_DCR(0x0, 0x0, 0x0, PWM_MODE_3X, 0x0, 0x0, 0x0, 0x0, 0x1); //HJB add, to clear fault |
Rushu | 54:d889fe62bc0c | 472 | //Init_pos = controller.theta_mech; //Input the local mechanical theta |
Rushu | 54:d889fe62bc0c | 473 | //===============================================HJB ended====================================================// |
benkatz | 0:4e1c4df6aabd | 474 | while(1) { |
benkatz | 47:e1196a851f76 | 475 | drv.print_faults(); |
shaorui | 58:bed739c83fac | 476 | wait(.1); |
benkatz | 48:74a40481740c | 477 | //printf("%.4f\n\r", controller.v_bus); |
shaorui | 58:bed739c83fac | 478 | // printf("adc1: %.3d\n\r",controller.adc2_raw = ADC1->DR); |
shaorui | 58:bed739c83fac | 479 | // printf("adc2: %.3d\n\r",controller.adc2_raw = ADC2->DR); |
shaorui | 58:bed739c83fac | 480 | // printf("adc3: %.3d\n\r",controller.adc2_raw = ADC3->DR); |
shaorui | 57:8200454194cf | 481 | wait(1); |
benkatz | 47:e1196a851f76 | 482 | if(state == MOTOR_MODE) |
benkatz | 47:e1196a851f76 | 483 | { |
benkatz | 48:74a40481740c | 484 | //printf("%.3f %.3f %.3f\n\r", (float)observer.temperature, (float)observer.temperature2, observer.resistance); |
benkatz | 49:83d83040ea51 | 485 | //printf("%.3f %.3f %.3f %.3f %.3f\n\r", controller.v_d, controller.v_q, controller.i_d_filt, controller.i_q_filt, controller.dtheta_elec); |
benkatz | 49:83d83040ea51 | 486 | //printf("%.3f\n\r", controller.dtheta_mech); |
shaorui | 56:9353ebcf19c6 | 487 | //wait(.002); |
shaorui | 56:9353ebcf19c6 | 488 | //printf(" True speed: %.3f\n\r", controller.dtheta_mech*60/(2*PI)); //shaorui ADD |
shaorui | 56:9353ebcf19c6 | 489 | //wait(2); |
shaorui | 56:9353ebcf19c6 | 490 | //printf("speed:%.3f\n\r", controller.dtheta_mech); |
shaorui | 57:8200454194cf | 491 | //printf("position:%.3f\n\r", controller.theta_mech*360/(2*PI)); |
shaorui | 58:bed739c83fac | 492 | //printf("adc1: %.3d\n\r",controller.adc2_raw = ADC1->DR); |
shaorui | 58:bed739c83fac | 493 | //printf("adc2: %.3d\n\r",controller.adc2_raw = ADC2->DR); |
shaorui | 58:bed739c83fac | 494 | //printf("adc3: %.3d\n\r",controller.adc2_raw = ADC3->DR); |
shaorui | 56:9353ebcf19c6 | 495 | //wait(.002); |
shaorui | 59:ed4b54437e02 | 496 | printf("vdes:%.3f real:%.3f\n\r",60*controller.v_des*GR/(2*PI) ,controller.dtheta_mech*GR*60/(2*PI)); |
shaorui | 59:ed4b54437e02 | 497 | printf("preal:%.3f\n\r",controller.theta_mech*350/2/PI); |
shaorui | 59:ed4b54437e02 | 498 | |
shaorui | 56:9353ebcf19c6 | 499 | wait(1); |
benkatz | 47:e1196a851f76 | 500 | } |
Rushu | 54:d889fe62bc0c | 501 | |
benkatz | 47:e1196a851f76 | 502 | |
benkatz | 0:4e1c4df6aabd | 503 | } |
benkatz | 0:4e1c4df6aabd | 504 | } |