1

Dependencies:   mbed-dev-f303 FastPWM3

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