customizing code for Aliexpress 14 bit version with MA702

Dependencies:   mbed FastPWM3

Committer:
benkatz
Date:
Wed Dec 05 04:07:46 2018 +0000
Revision:
47:e1196a851f76
Parent:
46:2d4b1dafcfe3
Child:
48:74a40481740c
albert revision;

Who changed what in which revision?

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