Motor control for robots. More compact, less object-oriented revision.

Dependencies:   FastPWM3 mbed-dev-f303

Fork of Hobbyking_Cheetah_V1 by Ben Katz

Committer:
benkatz
Date:
Fri May 25 15:16:42 2018 +0000
Revision:
42:738fa01b0346
Parent:
41:304ac2affb23
Child:
43:dfb72608639c
separated CAN stuff out of main

Who changed what in which revision?

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