BLDC motor driver
Dependencies: mbed-dev-f303 FastPWM3
main.cpp@43:dfb72608639c, 2018-05-25 (annotated)
- Committer:
- benkatz
- Date:
- Fri May 25 16:10:46 2018 +0000
- Revision:
- 43:dfb72608639c
- Parent:
- 42:738fa01b0346
- Child:
- 44:8040fa2fcb0d
re-ordered main() so CAN powerup order doesn't break things;
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 | 22:60276ba87ac6 | 2 | /// Written by benkatz, with much inspiration from bayleyw, nkirkby, scolton, 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 | 22:60276ba87ac6 | 5 | |
benkatz | 23:2adf23ee0305 | 6 | #define REST_MODE 0 |
benkatz | 23:2adf23ee0305 | 7 | #define CALIBRATION_MODE 1 |
benkatz | 26:2b865c00d7e9 | 8 | #define MOTOR_MODE 2 |
benkatz | 23:2adf23ee0305 | 9 | #define SETUP_MODE 4 |
benkatz | 23:2adf23ee0305 | 10 | #define ENCODER_MODE 5 |
benkatz | 22:60276ba87ac6 | 11 | |
benkatz | 38:67e4e1453a4b | 12 | #define VERSION_NUM "1.5" |
benkatz | 26:2b865c00d7e9 | 13 | |
benkatz | 18:f1d56f4acb39 | 14 | |
benkatz | 26:2b865c00d7e9 | 15 | float __float_reg[64]; // Floats stored in flash |
benkatz | 26:2b865c00d7e9 | 16 | int __int_reg[256]; // Ints stored in flash. Includes position sensor calibration lookup table |
benkatz | 17:3c5df2982199 | 17 | |
benkatz | 0:4e1c4df6aabd | 18 | #include "mbed.h" |
benkatz | 0:4e1c4df6aabd | 19 | #include "PositionSensor.h" |
benkatz | 20:bf9ea5125d52 | 20 | #include "structs.h" |
benkatz | 20:bf9ea5125d52 | 21 | #include "foc.h" |
benkatz | 22:60276ba87ac6 | 22 | #include "calibration.h" |
benkatz | 20:bf9ea5125d52 | 23 | #include "hw_setup.h" |
benkatz | 23:2adf23ee0305 | 24 | #include "math_ops.h" |
benkatz | 20:bf9ea5125d52 | 25 | #include "current_controller_config.h" |
benkatz | 20:bf9ea5125d52 | 26 | #include "hw_config.h" |
benkatz | 20:bf9ea5125d52 | 27 | #include "motor_config.h" |
benkatz | 23:2adf23ee0305 | 28 | #include "stm32f4xx_flash.h" |
benkatz | 23:2adf23ee0305 | 29 | #include "FlashWriter.h" |
benkatz | 23:2adf23ee0305 | 30 | #include "user_config.h" |
benkatz | 23:2adf23ee0305 | 31 | #include "PreferenceWriter.h" |
benkatz | 42:738fa01b0346 | 32 | #include "CAN_com.h" |
benkatz | 37:c0f352d6e8e3 | 33 | |
benkatz | 26:2b865c00d7e9 | 34 | |
benkatz | 23:2adf23ee0305 | 35 | PreferenceWriter prefs(6); |
benkatz | 9:d7eb815cb057 | 36 | |
benkatz | 20:bf9ea5125d52 | 37 | GPIOStruct gpio; |
benkatz | 20:bf9ea5125d52 | 38 | ControllerStruct controller; |
benkatz | 20:bf9ea5125d52 | 39 | COMStruct com; |
benkatz | 37:c0f352d6e8e3 | 40 | ObserverStruct observer; |
benkatz | 43:dfb72608639c | 41 | Serial pc(PA_2, PA_3); |
benkatz | 9:d7eb815cb057 | 42 | |
benkatz | 17:3c5df2982199 | 43 | |
benkatz | 42:738fa01b0346 | 44 | CAN can(PB_8, PB_9); // CAN Rx pin name, CAN Tx pin name |
benkatz | 26:2b865c00d7e9 | 45 | CANMessage rxMsg; |
benkatz | 26:2b865c00d7e9 | 46 | CANMessage txMsg; |
benkatz | 23:2adf23ee0305 | 47 | |
benkatz | 20:bf9ea5125d52 | 48 | |
benkatz | 8:10ae7bc88d6e | 49 | |
benkatz | 26:2b865c00d7e9 | 50 | PositionSensorAM5147 spi(16384, 0.0, NPP); |
benkatz | 20:bf9ea5125d52 | 51 | |
benkatz | 23:2adf23ee0305 | 52 | volatile int count = 0; |
benkatz | 23:2adf23ee0305 | 53 | volatile int state = REST_MODE; |
benkatz | 23:2adf23ee0305 | 54 | volatile int state_change; |
benkatz | 20:bf9ea5125d52 | 55 | |
benkatz | 26:2b865c00d7e9 | 56 | void onMsgReceived() { |
benkatz | 26:2b865c00d7e9 | 57 | //msgAvailable = true; |
benkatz | 26:2b865c00d7e9 | 58 | //printf("%.3f %.3f %.3f\n\r", controller.theta_mech, controller.dtheta_mech, controller.i_q); |
benkatz | 26:2b865c00d7e9 | 59 | can.read(rxMsg); |
benkatz | 28:8c7e29f719c5 | 60 | if((rxMsg.id == CAN_ID)){ |
benkatz | 28:8c7e29f719c5 | 61 | controller.timeout = 0; |
benkatz | 28:8c7e29f719c5 | 62 | 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 | 63 | state = MOTOR_MODE; |
benkatz | 28:8c7e29f719c5 | 64 | state_change = 1; |
benkatz | 28:8c7e29f719c5 | 65 | } |
benkatz | 28:8c7e29f719c5 | 66 | 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 | 67 | state = REST_MODE; |
benkatz | 28:8c7e29f719c5 | 68 | state_change = 1; |
benkatz | 37:c0f352d6e8e3 | 69 | gpio.led->write(0);; |
benkatz | 28:8c7e29f719c5 | 70 | } |
benkatz | 28:8c7e29f719c5 | 71 | 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 | 72 | spi.ZeroPosition(); |
benkatz | 28:8c7e29f719c5 | 73 | } |
benkatz | 28:8c7e29f719c5 | 74 | else if(state == MOTOR_MODE){ |
benkatz | 28:8c7e29f719c5 | 75 | unpack_cmd(rxMsg, &controller); |
benkatz | 28:8c7e29f719c5 | 76 | } |
benkatz | 37:c0f352d6e8e3 | 77 | pack_reply(&txMsg, controller.theta_mech, controller.dtheta_mech, controller.i_q_filt*KT_OUT); |
benkatz | 37:c0f352d6e8e3 | 78 | can.write(txMsg); |
benkatz | 28:8c7e29f719c5 | 79 | } |
benkatz | 26:2b865c00d7e9 | 80 | |
benkatz | 26:2b865c00d7e9 | 81 | } |
benkatz | 26:2b865c00d7e9 | 82 | |
benkatz | 23:2adf23ee0305 | 83 | void enter_menu_state(void){ |
benkatz | 23:2adf23ee0305 | 84 | printf("\n\r\n\r\n\r"); |
benkatz | 23:2adf23ee0305 | 85 | printf(" Commands:\n\r"); |
benkatz | 26:2b865c00d7e9 | 86 | printf(" m - Motor Mode\n\r"); |
benkatz | 23:2adf23ee0305 | 87 | printf(" c - Calibrate Encoder\n\r"); |
benkatz | 23:2adf23ee0305 | 88 | printf(" s - Setup\n\r"); |
benkatz | 23:2adf23ee0305 | 89 | printf(" e - Display Encoder\n\r"); |
benkatz | 37:c0f352d6e8e3 | 90 | printf(" z - Set Zero Position\n\r"); |
benkatz | 23:2adf23ee0305 | 91 | printf(" esc - Exit to Menu\n\r"); |
benkatz | 23:2adf23ee0305 | 92 | state_change = 0; |
benkatz | 25:f5741040c4bb | 93 | gpio.enable->write(0); |
benkatz | 37:c0f352d6e8e3 | 94 | gpio.led->write(0); |
benkatz | 23:2adf23ee0305 | 95 | } |
benkatz | 24:58c2d7571207 | 96 | |
benkatz | 24:58c2d7571207 | 97 | void enter_setup_state(void){ |
benkatz | 24:58c2d7571207 | 98 | printf("\n\r\n\r Configuration Options \n\r\n\n"); |
benkatz | 28:8c7e29f719c5 | 99 | printf(" %-4s %-31s %-5s %-6s %-5s\n\r\n\r", "prefix", "parameter", "min", "max", "current value"); |
benkatz | 28:8c7e29f719c5 | 100 | printf(" %-4s %-31s %-5s %-6s %.1f\n\r", "b", "Current Bandwidth (Hz)", "100", "2000", I_BW); |
benkatz | 28:8c7e29f719c5 | 101 | printf(" %-4s %-31s %-5s %-6s %-5i\n\r", "i", "CAN ID", "0", "127", CAN_ID); |
benkatz | 28:8c7e29f719c5 | 102 | printf(" %-4s %-31s %-5s %-6s %-5i\n\r", "m", "CAN Master ID", "0", "127", CAN_MASTER); |
benkatz | 28:8c7e29f719c5 | 103 | printf(" %-4s %-31s %-5s %-6s %.1f\n\r", "l", "Torque Limit (N-m)", "0.0", "18.0", TORQUE_LIMIT); |
benkatz | 28:8c7e29f719c5 | 104 | printf(" %-4s %-31s %-5s %-6s %d\n\r", "t", "CAN Timeout (cycles)(0 = none)", "0", "100000", CAN_TIMEOUT); |
benkatz | 24:58c2d7571207 | 105 | printf("\n\r To change a value, type 'prefix''value''ENTER'\n\r i.e. 'b1000''ENTER'\n\r\n\r"); |
benkatz | 24:58c2d7571207 | 106 | state_change = 0; |
benkatz | 24:58c2d7571207 | 107 | } |
benkatz | 22:60276ba87ac6 | 108 | |
benkatz | 23:2adf23ee0305 | 109 | void enter_torque_mode(void){ |
benkatz | 37:c0f352d6e8e3 | 110 | controller.ovp_flag = 0; |
benkatz | 28:8c7e29f719c5 | 111 | gpio.enable->write(1); // Enable gate drive |
benkatz | 28:8c7e29f719c5 | 112 | reset_foc(&controller); // Tesets integrators, and other control loop parameters |
benkatz | 28:8c7e29f719c5 | 113 | wait(.001); |
benkatz | 23:2adf23ee0305 | 114 | controller.i_d_ref = 0; |
benkatz | 28:8c7e29f719c5 | 115 | controller.i_q_ref = 0; // Current Setpoints |
benkatz | 37:c0f352d6e8e3 | 116 | gpio.led->write(1); // Turn on status LED |
benkatz | 25:f5741040c4bb | 117 | state_change = 0; |
benkatz | 28:8c7e29f719c5 | 118 | printf("\n\r Entering Motor Mode \n\r"); |
benkatz | 23:2adf23ee0305 | 119 | } |
benkatz | 22:60276ba87ac6 | 120 | |
benkatz | 23:2adf23ee0305 | 121 | void calibrate(void){ |
benkatz | 25:f5741040c4bb | 122 | gpio.enable->write(1); // Enable gate drive |
benkatz | 37:c0f352d6e8e3 | 123 | gpio.led->write(1); // Turn on status LED |
benkatz | 25:f5741040c4bb | 124 | order_phases(&spi, &gpio, &controller, &prefs); // Check phase ordering |
benkatz | 25:f5741040c4bb | 125 | calibrate(&spi, &gpio, &controller, &prefs); // Perform calibration procedure |
benkatz | 37:c0f352d6e8e3 | 126 | gpio.led->write(0);; // Turn off status LED |
benkatz | 23:2adf23ee0305 | 127 | wait(.2); |
benkatz | 25:f5741040c4bb | 128 | gpio.enable->write(0); // Turn off gate drive |
benkatz | 23:2adf23ee0305 | 129 | printf("\n\r Calibration complete. Press 'esc' to return to menu\n\r"); |
benkatz | 23:2adf23ee0305 | 130 | state_change = 0; |
benkatz | 23:2adf23ee0305 | 131 | } |
benkatz | 23:2adf23ee0305 | 132 | |
benkatz | 23:2adf23ee0305 | 133 | void print_encoder(void){ |
benkatz | 23:2adf23ee0305 | 134 | printf(" Mechanical Angle: %f Electrical Angle: %f Raw: %d\n\r", spi.GetMechPosition(), spi.GetElecPosition(), spi.GetRawPosition()); |
benkatz | 23:2adf23ee0305 | 135 | wait(.05); |
benkatz | 22:60276ba87ac6 | 136 | } |
benkatz | 20:bf9ea5125d52 | 137 | |
benkatz | 23:2adf23ee0305 | 138 | /// Current Sampling Interrupt /// |
benkatz | 23:2adf23ee0305 | 139 | /// This runs at 40 kHz, regardless of of the mode the controller is in /// |
benkatz | 2:8724412ad628 | 140 | extern "C" void TIM1_UP_TIM10_IRQHandler(void) { |
benkatz | 2:8724412ad628 | 141 | if (TIM1->SR & TIM_SR_UIF ) { |
benkatz | 23:2adf23ee0305 | 142 | |
benkatz | 23:2adf23ee0305 | 143 | ///Sample current always /// |
benkatz | 25:f5741040c4bb | 144 | ADC1->CR2 |= 0x40000000; // Begin sample and conversion |
benkatz | 22:60276ba87ac6 | 145 | //volatile int delay; |
benkatz | 20:bf9ea5125d52 | 146 | //for (delay = 0; delay < 55; delay++); |
benkatz | 37:c0f352d6e8e3 | 147 | controller.adc2_raw = ADC2->DR; // Read ADC Data Registers |
benkatz | 23:2adf23ee0305 | 148 | controller.adc1_raw = ADC1->DR; |
benkatz | 37:c0f352d6e8e3 | 149 | controller.adc3_raw = ADC3->DR; |
benkatz | 37:c0f352d6e8e3 | 150 | spi.Sample(); // sample position sensor |
benkatz | 37:c0f352d6e8e3 | 151 | controller.theta_elec = spi.GetElecPosition(); |
benkatz | 37:c0f352d6e8e3 | 152 | controller.theta_mech = (1.0f/GR)*spi.GetMechPosition(); |
benkatz | 37:c0f352d6e8e3 | 153 | controller.dtheta_mech = (1.0f/GR)*spi.GetMechVelocity(); |
benkatz | 37:c0f352d6e8e3 | 154 | controller.dtheta_elec = spi.GetElecVelocity(); |
benkatz | 37:c0f352d6e8e3 | 155 | controller.v_bus = 0.95f*controller.v_bus + 0.05f*((float)controller.adc3_raw)*V_SCALE; |
benkatz | 23:2adf23ee0305 | 156 | /// |
benkatz | 20:bf9ea5125d52 | 157 | |
benkatz | 23:2adf23ee0305 | 158 | /// Check state machine state, and run the appropriate function /// |
benkatz | 23:2adf23ee0305 | 159 | switch(state){ |
benkatz | 37:c0f352d6e8e3 | 160 | case REST_MODE: // Do nothing |
benkatz | 23:2adf23ee0305 | 161 | if(state_change){ |
benkatz | 23:2adf23ee0305 | 162 | enter_menu_state(); |
benkatz | 23:2adf23ee0305 | 163 | } |
benkatz | 23:2adf23ee0305 | 164 | break; |
benkatz | 22:60276ba87ac6 | 165 | |
benkatz | 23:2adf23ee0305 | 166 | case CALIBRATION_MODE: // Run encoder calibration procedure |
benkatz | 23:2adf23ee0305 | 167 | if(state_change){ |
benkatz | 23:2adf23ee0305 | 168 | calibrate(); |
benkatz | 23:2adf23ee0305 | 169 | } |
benkatz | 23:2adf23ee0305 | 170 | break; |
benkatz | 23:2adf23ee0305 | 171 | |
benkatz | 26:2b865c00d7e9 | 172 | case MOTOR_MODE: // Run torque control |
benkatz | 25:f5741040c4bb | 173 | if(state_change){ |
benkatz | 25:f5741040c4bb | 174 | enter_torque_mode(); |
benkatz | 28:8c7e29f719c5 | 175 | count = 0; |
benkatz | 25:f5741040c4bb | 176 | } |
benkatz | 28:8c7e29f719c5 | 177 | else{ |
benkatz | 37:c0f352d6e8e3 | 178 | /* |
benkatz | 37:c0f352d6e8e3 | 179 | 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 | 37:c0f352d6e8e3 | 180 | gpio.enable->write(0); |
benkatz | 37:c0f352d6e8e3 | 181 | controller.ovp_flag = 1; |
benkatz | 37:c0f352d6e8e3 | 182 | state = REST_MODE; |
benkatz | 37:c0f352d6e8e3 | 183 | state_change = 1; |
benkatz | 37:c0f352d6e8e3 | 184 | printf("OVP Triggered!\n\r"); |
benkatz | 37:c0f352d6e8e3 | 185 | } |
benkatz | 37:c0f352d6e8e3 | 186 | */ |
benkatz | 37:c0f352d6e8e3 | 187 | |
benkatz | 40:cd7e837b2b93 | 188 | torque_control(&controller); |
benkatz | 28:8c7e29f719c5 | 189 | if((controller.timeout > CAN_TIMEOUT) && (CAN_TIMEOUT > 0)){ |
benkatz | 28:8c7e29f719c5 | 190 | controller.i_d_ref = 0; |
benkatz | 28:8c7e29f719c5 | 191 | controller.i_q_ref = 0; |
benkatz | 37:c0f352d6e8e3 | 192 | controller.kp = 0; |
benkatz | 37:c0f352d6e8e3 | 193 | controller.kd = 0; |
benkatz | 37:c0f352d6e8e3 | 194 | controller.t_ff = 0; |
benkatz | 28:8c7e29f719c5 | 195 | } |
benkatz | 37:c0f352d6e8e3 | 196 | commutate(&controller, &observer, &gpio, controller.theta_elec); // Run current loop |
benkatz | 28:8c7e29f719c5 | 197 | controller.timeout += 1; |
benkatz | 38:67e4e1453a4b | 198 | |
benkatz | 39:3580a907ef93 | 199 | /* |
benkatz | 37:c0f352d6e8e3 | 200 | count++; |
benkatz | 40:cd7e837b2b93 | 201 | if(count == 4000){ |
benkatz | 40:cd7e837b2b93 | 202 | printf("%.4f\n\r", controller.dtheta_mech); |
benkatz | 32:ccac5da77844 | 203 | count = 0; |
benkatz | 23:2adf23ee0305 | 204 | } |
benkatz | 39:3580a907ef93 | 205 | */ |
benkatz | 38:67e4e1453a4b | 206 | |
benkatz | 37:c0f352d6e8e3 | 207 | |
benkatz | 37:c0f352d6e8e3 | 208 | } |
benkatz | 23:2adf23ee0305 | 209 | break; |
benkatz | 23:2adf23ee0305 | 210 | case SETUP_MODE: |
benkatz | 23:2adf23ee0305 | 211 | if(state_change){ |
benkatz | 24:58c2d7571207 | 212 | enter_setup_state(); |
benkatz | 23:2adf23ee0305 | 213 | } |
benkatz | 23:2adf23ee0305 | 214 | break; |
benkatz | 23:2adf23ee0305 | 215 | case ENCODER_MODE: |
benkatz | 23:2adf23ee0305 | 216 | print_encoder(); |
benkatz | 23:2adf23ee0305 | 217 | break; |
benkatz | 37:c0f352d6e8e3 | 218 | } |
benkatz | 2:8724412ad628 | 219 | } |
benkatz | 23:2adf23ee0305 | 220 | TIM1->SR = 0x0; // reset the status register |
benkatz | 2:8724412ad628 | 221 | } |
benkatz | 0:4e1c4df6aabd | 222 | |
benkatz | 25:f5741040c4bb | 223 | |
benkatz | 24:58c2d7571207 | 224 | char cmd_val[8] = {0}; |
benkatz | 24:58c2d7571207 | 225 | char cmd_id = 0; |
benkatz | 25:f5741040c4bb | 226 | char char_count = 0; |
benkatz | 24:58c2d7571207 | 227 | |
benkatz | 25:f5741040c4bb | 228 | /// Manage state machine with commands from serial terminal or configurator gui /// |
benkatz | 25:f5741040c4bb | 229 | /// Called when data received over serial /// |
benkatz | 23:2adf23ee0305 | 230 | void serial_interrupt(void){ |
benkatz | 23:2adf23ee0305 | 231 | while(pc.readable()){ |
benkatz | 23:2adf23ee0305 | 232 | char c = pc.getc(); |
benkatz | 25:f5741040c4bb | 233 | if(c == 27){ |
benkatz | 25:f5741040c4bb | 234 | state = REST_MODE; |
benkatz | 25:f5741040c4bb | 235 | state_change = 1; |
benkatz | 25:f5741040c4bb | 236 | char_count = 0; |
benkatz | 25:f5741040c4bb | 237 | cmd_id = 0; |
benkatz | 37:c0f352d6e8e3 | 238 | gpio.led->write(0);; |
benkatz | 25:f5741040c4bb | 239 | for(int i = 0; i<8; i++){cmd_val[i] = 0;} |
benkatz | 25:f5741040c4bb | 240 | } |
benkatz | 24:58c2d7571207 | 241 | if(state == REST_MODE){ |
benkatz | 23:2adf23ee0305 | 242 | switch (c){ |
benkatz | 23:2adf23ee0305 | 243 | case 'c': |
benkatz | 23:2adf23ee0305 | 244 | state = CALIBRATION_MODE; |
benkatz | 23:2adf23ee0305 | 245 | state_change = 1; |
benkatz | 23:2adf23ee0305 | 246 | break; |
benkatz | 26:2b865c00d7e9 | 247 | case 'm': |
benkatz | 26:2b865c00d7e9 | 248 | state = MOTOR_MODE; |
benkatz | 23:2adf23ee0305 | 249 | state_change = 1; |
benkatz | 23:2adf23ee0305 | 250 | break; |
benkatz | 23:2adf23ee0305 | 251 | case 'e': |
benkatz | 23:2adf23ee0305 | 252 | state = ENCODER_MODE; |
benkatz | 23:2adf23ee0305 | 253 | state_change = 1; |
benkatz | 23:2adf23ee0305 | 254 | break; |
benkatz | 23:2adf23ee0305 | 255 | case 's': |
benkatz | 23:2adf23ee0305 | 256 | state = SETUP_MODE; |
benkatz | 23:2adf23ee0305 | 257 | state_change = 1; |
benkatz | 23:2adf23ee0305 | 258 | break; |
benkatz | 37:c0f352d6e8e3 | 259 | case 'z': |
benkatz | 37:c0f352d6e8e3 | 260 | spi.SetMechOffset(0); |
benkatz | 37:c0f352d6e8e3 | 261 | spi.Sample(); |
benkatz | 37:c0f352d6e8e3 | 262 | wait_us(20); |
benkatz | 37:c0f352d6e8e3 | 263 | M_OFFSET = spi.GetMechPosition(); |
benkatz | 37:c0f352d6e8e3 | 264 | if (!prefs.ready()) prefs.open(); |
benkatz | 37:c0f352d6e8e3 | 265 | prefs.flush(); // Write new prefs to flash |
benkatz | 37:c0f352d6e8e3 | 266 | prefs.close(); |
benkatz | 37:c0f352d6e8e3 | 267 | prefs.load(); |
benkatz | 37:c0f352d6e8e3 | 268 | spi.SetMechOffset(M_OFFSET); |
benkatz | 37:c0f352d6e8e3 | 269 | printf("\n\r Saved new zero position: %.4f\n\r\n\r", M_OFFSET); |
benkatz | 37:c0f352d6e8e3 | 270 | |
benkatz | 37:c0f352d6e8e3 | 271 | break; |
benkatz | 37:c0f352d6e8e3 | 272 | } |
benkatz | 37:c0f352d6e8e3 | 273 | |
benkatz | 24:58c2d7571207 | 274 | } |
benkatz | 24:58c2d7571207 | 275 | else if(state == SETUP_MODE){ |
benkatz | 25:f5741040c4bb | 276 | if(c == 13){ |
benkatz | 24:58c2d7571207 | 277 | switch (cmd_id){ |
benkatz | 24:58c2d7571207 | 278 | case 'b': |
benkatz | 24:58c2d7571207 | 279 | I_BW = fmaxf(fminf(atof(cmd_val), 2000.0f), 100.0f); |
benkatz | 24:58c2d7571207 | 280 | break; |
benkatz | 24:58c2d7571207 | 281 | case 'i': |
benkatz | 24:58c2d7571207 | 282 | CAN_ID = atoi(cmd_val); |
benkatz | 24:58c2d7571207 | 283 | break; |
benkatz | 26:2b865c00d7e9 | 284 | case 'm': |
benkatz | 26:2b865c00d7e9 | 285 | CAN_MASTER = atoi(cmd_val); |
benkatz | 26:2b865c00d7e9 | 286 | break; |
benkatz | 24:58c2d7571207 | 287 | case 'l': |
benkatz | 24:58c2d7571207 | 288 | TORQUE_LIMIT = fmaxf(fminf(atof(cmd_val), 18.0f), 0.0f); |
benkatz | 24:58c2d7571207 | 289 | break; |
benkatz | 28:8c7e29f719c5 | 290 | case 't': |
benkatz | 28:8c7e29f719c5 | 291 | CAN_TIMEOUT = atoi(cmd_val); |
benkatz | 28:8c7e29f719c5 | 292 | break; |
benkatz | 24:58c2d7571207 | 293 | default: |
benkatz | 24:58c2d7571207 | 294 | printf("\n\r '%c' Not a valid command prefix\n\r\n\r", cmd_id); |
benkatz | 24:58c2d7571207 | 295 | break; |
benkatz | 24:58c2d7571207 | 296 | } |
benkatz | 24:58c2d7571207 | 297 | |
benkatz | 24:58c2d7571207 | 298 | if (!prefs.ready()) prefs.open(); |
benkatz | 24:58c2d7571207 | 299 | prefs.flush(); // Write new prefs to flash |
benkatz | 24:58c2d7571207 | 300 | prefs.close(); |
benkatz | 24:58c2d7571207 | 301 | prefs.load(); |
benkatz | 24:58c2d7571207 | 302 | state_change = 1; |
benkatz | 24:58c2d7571207 | 303 | char_count = 0; |
benkatz | 24:58c2d7571207 | 304 | cmd_id = 0; |
benkatz | 24:58c2d7571207 | 305 | for(int i = 0; i<8; i++){cmd_val[i] = 0;} |
benkatz | 24:58c2d7571207 | 306 | } |
benkatz | 24:58c2d7571207 | 307 | else{ |
benkatz | 24:58c2d7571207 | 308 | if(char_count == 0){cmd_id = c;} |
benkatz | 24:58c2d7571207 | 309 | else{ |
benkatz | 24:58c2d7571207 | 310 | cmd_val[char_count-1] = c; |
benkatz | 24:58c2d7571207 | 311 | |
benkatz | 24:58c2d7571207 | 312 | } |
benkatz | 24:58c2d7571207 | 313 | pc.putc(c); |
benkatz | 24:58c2d7571207 | 314 | char_count++; |
benkatz | 23:2adf23ee0305 | 315 | } |
benkatz | 23:2adf23ee0305 | 316 | } |
benkatz | 24:58c2d7571207 | 317 | else if (state == ENCODER_MODE){ |
benkatz | 24:58c2d7571207 | 318 | switch (c){ |
benkatz | 24:58c2d7571207 | 319 | case 27: |
benkatz | 24:58c2d7571207 | 320 | state = REST_MODE; |
benkatz | 24:58c2d7571207 | 321 | state_change = 1; |
benkatz | 24:58c2d7571207 | 322 | break; |
benkatz | 24:58c2d7571207 | 323 | } |
benkatz | 24:58c2d7571207 | 324 | } |
benkatz | 24:58c2d7571207 | 325 | |
benkatz | 24:58c2d7571207 | 326 | } |
benkatz | 22:60276ba87ac6 | 327 | } |
benkatz | 0:4e1c4df6aabd | 328 | |
benkatz | 0:4e1c4df6aabd | 329 | int main() { |
benkatz | 43:dfb72608639c | 330 | can.frequency(1000000); // set bit rate to 1Mbps |
benkatz | 20:bf9ea5125d52 | 331 | controller.v_bus = V_BUS; |
benkatz | 22:60276ba87ac6 | 332 | controller.mode = 0; |
benkatz | 23:2adf23ee0305 | 333 | Init_All_HW(&gpio); // Setup PWM, ADC, GPIO |
benkatz | 20:bf9ea5125d52 | 334 | |
benkatz | 9:d7eb815cb057 | 335 | wait(.1); |
benkatz | 26:2b865c00d7e9 | 336 | gpio.enable->write(1); |
benkatz | 26:2b865c00d7e9 | 337 | TIM1->CCR3 = 0x708*(1.0f); // Write duty cycles |
benkatz | 26:2b865c00d7e9 | 338 | TIM1->CCR2 = 0x708*(1.0f); |
benkatz | 26:2b865c00d7e9 | 339 | TIM1->CCR1 = 0x708*(1.0f); |
benkatz | 23:2adf23ee0305 | 340 | zero_current(&controller.adc1_offset, &controller.adc2_offset); // Measure current sensor zero-offset |
benkatz | 26:2b865c00d7e9 | 341 | gpio.enable->write(0); |
benkatz | 23:2adf23ee0305 | 342 | reset_foc(&controller); // Reset current controller |
benkatz | 26:2b865c00d7e9 | 343 | TIM1->CR1 ^= TIM_CR1_UDIS; |
benkatz | 26:2b865c00d7e9 | 344 | //TIM1->CR1 |= TIM_CR1_UDIS; //enable interrupt |
benkatz | 20:bf9ea5125d52 | 345 | |
benkatz | 20:bf9ea5125d52 | 346 | wait(.1); |
benkatz | 37:c0f352d6e8e3 | 347 | NVIC_SetPriority(TIM1_UP_TIM10_IRQn, 2); // commutation > communication |
benkatz | 43:dfb72608639c | 348 | |
benkatz | 37:c0f352d6e8e3 | 349 | NVIC_SetPriority(CAN1_RX0_IRQn, 3); |
benkatz | 26:2b865c00d7e9 | 350 | can.filter(CAN_ID<<21, 0xFFE00004, CANStandard, 0); |
benkatz | 26:2b865c00d7e9 | 351 | //can.filter(CAN_ID, 0xF, CANStandard, 0); |
benkatz | 43:dfb72608639c | 352 | |
benkatz | 28:8c7e29f719c5 | 353 | txMsg.id = CAN_MASTER; |
benkatz | 28:8c7e29f719c5 | 354 | txMsg.len = 6; |
benkatz | 26:2b865c00d7e9 | 355 | rxMsg.len = 8; |
benkatz | 43:dfb72608639c | 356 | can.attach(&onMsgReceived); // attach 'CAN receive-complete' interrupt handler |
benkatz | 23:2adf23ee0305 | 357 | |
benkatz | 25:f5741040c4bb | 358 | prefs.load(); // Read flash |
benkatz | 37:c0f352d6e8e3 | 359 | if(isnan(E_OFFSET)){E_OFFSET = 0.0f;} |
benkatz | 37:c0f352d6e8e3 | 360 | if(isnan(M_OFFSET)){M_OFFSET = 0.0f;} |
benkatz | 25:f5741040c4bb | 361 | spi.SetElecOffset(E_OFFSET); // Set position sensor offset |
benkatz | 37:c0f352d6e8e3 | 362 | spi.SetMechOffset(M_OFFSET); |
benkatz | 23:2adf23ee0305 | 363 | int lut[128] = {0}; |
benkatz | 23:2adf23ee0305 | 364 | memcpy(&lut, &ENCODER_LUT, sizeof(lut)); |
benkatz | 25:f5741040c4bb | 365 | spi.WriteLUT(lut); // Set potision sensor nonlinearity lookup table |
benkatz | 23:2adf23ee0305 | 366 | |
benkatz | 26:2b865c00d7e9 | 367 | pc.baud(921600); // set serial baud rate |
benkatz | 20:bf9ea5125d52 | 368 | wait(.01); |
benkatz | 23:2adf23ee0305 | 369 | pc.printf("\n\r\n\r HobbyKing Cheetah\n\r\n\r"); |
benkatz | 20:bf9ea5125d52 | 370 | wait(.01); |
benkatz | 23:2adf23ee0305 | 371 | printf("\n\r Debug Info:\n\r"); |
benkatz | 32:ccac5da77844 | 372 | printf(" Firmware Version: %s\n\r", VERSION_NUM); |
benkatz | 23:2adf23ee0305 | 373 | printf(" ADC1 Offset: %d ADC2 Offset: %d\n\r", controller.adc1_offset, controller.adc2_offset); |
benkatz | 23:2adf23ee0305 | 374 | printf(" Position Sensor Electrical Offset: %.4f\n\r", E_OFFSET); |
benkatz | 37:c0f352d6e8e3 | 375 | printf(" Output Zero Position: %.4f\n\r", M_OFFSET); |
benkatz | 24:58c2d7571207 | 376 | printf(" CAN ID: %d\n\r", CAN_ID); |
benkatz | 23:2adf23ee0305 | 377 | |
benkatz | 23:2adf23ee0305 | 378 | pc.attach(&serial_interrupt); // attach serial interrupt |
benkatz | 22:60276ba87ac6 | 379 | |
benkatz | 23:2adf23ee0305 | 380 | state_change = 1; |
benkatz | 20:bf9ea5125d52 | 381 | |
benkatz | 22:60276ba87ac6 | 382 | |
benkatz | 0:4e1c4df6aabd | 383 | while(1) { |
benkatz | 11:c83b18d41e54 | 384 | |
benkatz | 0:4e1c4df6aabd | 385 | } |
benkatz | 0:4e1c4df6aabd | 386 | } |