1

Dependencies:   mbed-dev-f303 FastPWM3

Committer:
benkatz
Date:
Mon Jul 30 20:25:24 2018 +0000
Revision:
45:aadebe074af6
Parent:
44:efcde0af8390
Child:
47:55bdc4d5096b
fixed error in float_to_uint.  Honestly I'm not sure how it worked before;

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