1

Dependencies:   mbed-dev-f303 FastPWM3

Committer:
shaorui
Date:
Fri Oct 16 09:08:47 2020 +0000
Revision:
57:059bd1333ba4
Parent:
56:fe5056ac6740
1

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 56:fe5056ac6740 186 /*
benkatz 56:fe5056ac6740 187 if(count < 10){printf("%d\n\r", spi.GetRawPosition());}
benkatz 56:fe5056ac6740 188 count ++;
benkatz 56:fe5056ac6740 189 */
benkatz 37:c0f352d6e8e3 190 controller.adc2_raw = ADC2->DR; // Read ADC Data Registers
benkatz 23:2adf23ee0305 191 controller.adc1_raw = ADC1->DR;
benkatz 37:c0f352d6e8e3 192 controller.adc3_raw = ADC3->DR;
benkatz 37:c0f352d6e8e3 193 controller.theta_elec = spi.GetElecPosition();
benkatz 37:c0f352d6e8e3 194 controller.theta_mech = (1.0f/GR)*spi.GetMechPosition();
benkatz 37:c0f352d6e8e3 195 controller.dtheta_mech = (1.0f/GR)*spi.GetMechVelocity();
benkatz 37:c0f352d6e8e3 196 controller.dtheta_elec = spi.GetElecVelocity();
benkatz 51:6cd89bd6fcaa 197 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 198
benkatz 55:c4c9fec8539c 199
benkatz 55:c4c9fec8539c 200
benkatz 23:2adf23ee0305 201 ///
benkatz 20:bf9ea5125d52 202
benkatz 23:2adf23ee0305 203 /// Check state machine state, and run the appropriate function ///
benkatz 23:2adf23ee0305 204 switch(state){
benkatz 37:c0f352d6e8e3 205 case REST_MODE: // Do nothing
benkatz 23:2adf23ee0305 206 if(state_change){
benkatz 23:2adf23ee0305 207 enter_menu_state();
benkatz 23:2adf23ee0305 208 }
benkatz 55:c4c9fec8539c 209 update_observer(&controller, &observer);
benkatz 23:2adf23ee0305 210 break;
benkatz 22:60276ba87ac6 211
benkatz 23:2adf23ee0305 212 case CALIBRATION_MODE: // Run encoder calibration procedure
benkatz 23:2adf23ee0305 213 if(state_change){
benkatz 23:2adf23ee0305 214 calibrate();
benkatz 23:2adf23ee0305 215 }
benkatz 23:2adf23ee0305 216 break;
benkatz 55:c4c9fec8539c 217 case INIT_TEMP_MODE:
benkatz 55:c4c9fec8539c 218 if(state_change){
benkatz 55:c4c9fec8539c 219 enter_torque_mode();
benkatz 55:c4c9fec8539c 220 count = 0;
benkatz 55:c4c9fec8539c 221 observer.resistance = 0.0f;
benkatz 55:c4c9fec8539c 222 }
benkatz 55:c4c9fec8539c 223 controller.i_d_ref = -10.0f;
benkatz 55:c4c9fec8539c 224 controller.i_q_ref = 0.0f;
benkatz 55:c4c9fec8539c 225 commutate(&controller, &observer, &gpio, controller.theta_elec);
benkatz 55:c4c9fec8539c 226
benkatz 55:c4c9fec8539c 227 if(count > 200)
benkatz 55:c4c9fec8539c 228 {
benkatz 55:c4c9fec8539c 229 float r_meas = controller.v_d*(DTC_MAX-DTC_MIN)/(controller.i_d*SQRT3);
benkatz 55:c4c9fec8539c 230 //testing2[count-100] = controller.i_d;
benkatz 55:c4c9fec8539c 231 observer.resistance += .001f*r_meas;
benkatz 55:c4c9fec8539c 232 }
benkatz 55:c4c9fec8539c 233 if(count > 1200)
benkatz 55:c4c9fec8539c 234 {
benkatz 55:c4c9fec8539c 235 count = 0;
benkatz 55:c4c9fec8539c 236 state = REST_MODE;
benkatz 55:c4c9fec8539c 237 state_change = 1;
benkatz 55:c4c9fec8539c 238 gpio.led->write(0);
shaorui 57:059bd1333ba4 239 //observer.temperature = (double)(T_AMBIENT + ((observer.resistance/R_NOMINAL) - 1.0f)*254.5f);
shaorui 57:059bd1333ba4 240 observer.temperature = (float)(T_AMBIENT + ((observer.resistance/R_NOMINAL) - 1.0f)*254.5f);
benkatz 55:c4c9fec8539c 241 printf("Winding Resistance: %f\n\r", observer.resistance);
benkatz 55:c4c9fec8539c 242 printf("Winding Temperature: %f\n\r", observer.temperature);
benkatz 55:c4c9fec8539c 243
benkatz 55:c4c9fec8539c 244 if(R_NOMINAL==0)
benkatz 55:c4c9fec8539c 245 {
benkatz 55:c4c9fec8539c 246 printf("Saving winding resistance\n\r");
benkatz 55:c4c9fec8539c 247 R_NOMINAL = observer.resistance;
benkatz 55:c4c9fec8539c 248 if (!prefs.ready()) prefs.open();
benkatz 55:c4c9fec8539c 249 prefs.flush(); // write offset and lookup table to flash
benkatz 55:c4c9fec8539c 250 prefs.close();
benkatz 55:c4c9fec8539c 251 }
benkatz 55:c4c9fec8539c 252 //for(int i = 0; i<1000; i++){printf("%f \n\r", testing[i]);}
benkatz 55:c4c9fec8539c 253 }
benkatz 55:c4c9fec8539c 254
benkatz 55:c4c9fec8539c 255 count++;
benkatz 55:c4c9fec8539c 256 break;
benkatz 26:2b865c00d7e9 257 case MOTOR_MODE: // Run torque control
benkatz 25:f5741040c4bb 258 if(state_change){
benkatz 25:f5741040c4bb 259 enter_torque_mode();
benkatz 28:8c7e29f719c5 260 count = 0;
benkatz 25:f5741040c4bb 261 }
benkatz 28:8c7e29f719c5 262 else{
benkatz 37:c0f352d6e8e3 263 /*
benkatz 37:c0f352d6e8e3 264 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 265 gpio.
benkatz 47:e1196a851f76 266 ->write(0);
benkatz 37:c0f352d6e8e3 267 controller.ovp_flag = 1;
benkatz 37:c0f352d6e8e3 268 state = REST_MODE;
benkatz 37:c0f352d6e8e3 269 state_change = 1;
benkatz 37:c0f352d6e8e3 270 printf("OVP Triggered!\n\r");
benkatz 37:c0f352d6e8e3 271 }
benkatz 37:c0f352d6e8e3 272 */
benkatz 37:c0f352d6e8e3 273
benkatz 28:8c7e29f719c5 274 if((controller.timeout > CAN_TIMEOUT) && (CAN_TIMEOUT > 0)){
benkatz 28:8c7e29f719c5 275 controller.i_d_ref = 0;
benkatz 28:8c7e29f719c5 276 controller.i_q_ref = 0;
benkatz 37:c0f352d6e8e3 277 controller.kp = 0;
benkatz 37:c0f352d6e8e3 278 controller.kd = 0;
benkatz 37:c0f352d6e8e3 279 controller.t_ff = 0;
benkatz 28:8c7e29f719c5 280 }
benkatz 55:c4c9fec8539c 281
benkatz 50:ba72df25d10f 282 torque_control(&controller);
benkatz 55:c4c9fec8539c 283 update_observer(&controller, &observer);
benkatz 55:c4c9fec8539c 284 field_weaken(&controller);
benkatz 49:83d83040ea51 285 commutate(&controller, &observer, &gpio, controller.theta_elec); // Run current loop
benkatz 49:83d83040ea51 286 controller.timeout++;
benkatz 55:c4c9fec8539c 287
benkatz 55:c4c9fec8539c 288 if(controller.otw_flag)
benkatz 55:c4c9fec8539c 289 {
benkatz 55:c4c9fec8539c 290 state = REST_MODE;
benkatz 55:c4c9fec8539c 291 state_change = 1;
benkatz 55:c4c9fec8539c 292 gpio.led->write(0);
benkatz 55:c4c9fec8539c 293 }
benkatz 55:c4c9fec8539c 294
benkatz 49:83d83040ea51 295 count++;
benkatz 55:c4c9fec8539c 296 }
benkatz 55:c4c9fec8539c 297
benkatz 55:c4c9fec8539c 298
benkatz 55:c4c9fec8539c 299 break;
benkatz 37:c0f352d6e8e3 300
benkatz 23:2adf23ee0305 301 case SETUP_MODE:
benkatz 23:2adf23ee0305 302 if(state_change){
benkatz 24:58c2d7571207 303 enter_setup_state();
benkatz 23:2adf23ee0305 304 }
benkatz 23:2adf23ee0305 305 break;
benkatz 23:2adf23ee0305 306 case ENCODER_MODE:
benkatz 23:2adf23ee0305 307 print_encoder();
benkatz 23:2adf23ee0305 308 break;
benkatz 37:c0f352d6e8e3 309 }
benkatz 2:8724412ad628 310 }
benkatz 55:c4c9fec8539c 311 //gpio.led->write(0);
benkatz 23:2adf23ee0305 312 TIM1->SR = 0x0; // reset the status register
benkatz 2:8724412ad628 313 }
benkatz 0:4e1c4df6aabd 314
benkatz 25:f5741040c4bb 315
benkatz 24:58c2d7571207 316 char cmd_val[8] = {0};
benkatz 24:58c2d7571207 317 char cmd_id = 0;
benkatz 25:f5741040c4bb 318 char char_count = 0;
benkatz 24:58c2d7571207 319
benkatz 25:f5741040c4bb 320 /// Manage state machine with commands from serial terminal or configurator gui ///
benkatz 25:f5741040c4bb 321 /// Called when data received over serial ///
benkatz 23:2adf23ee0305 322 void serial_interrupt(void){
benkatz 23:2adf23ee0305 323 while(pc.readable()){
benkatz 23:2adf23ee0305 324 char c = pc.getc();
benkatz 25:f5741040c4bb 325 if(c == 27){
benkatz 25:f5741040c4bb 326 state = REST_MODE;
benkatz 25:f5741040c4bb 327 state_change = 1;
benkatz 25:f5741040c4bb 328 char_count = 0;
benkatz 25:f5741040c4bb 329 cmd_id = 0;
benkatz 55:c4c9fec8539c 330 gpio.led->write(0);;
benkatz 25:f5741040c4bb 331 for(int i = 0; i<8; i++){cmd_val[i] = 0;}
benkatz 25:f5741040c4bb 332 }
benkatz 24:58c2d7571207 333 if(state == REST_MODE){
benkatz 23:2adf23ee0305 334 switch (c){
benkatz 23:2adf23ee0305 335 case 'c':
benkatz 23:2adf23ee0305 336 state = CALIBRATION_MODE;
benkatz 23:2adf23ee0305 337 state_change = 1;
benkatz 23:2adf23ee0305 338 break;
benkatz 26:2b865c00d7e9 339 case 'm':
benkatz 26:2b865c00d7e9 340 state = MOTOR_MODE;
benkatz 23:2adf23ee0305 341 state_change = 1;
benkatz 23:2adf23ee0305 342 break;
benkatz 23:2adf23ee0305 343 case 'e':
benkatz 23:2adf23ee0305 344 state = ENCODER_MODE;
benkatz 23:2adf23ee0305 345 state_change = 1;
benkatz 23:2adf23ee0305 346 break;
benkatz 23:2adf23ee0305 347 case 's':
benkatz 23:2adf23ee0305 348 state = SETUP_MODE;
benkatz 23:2adf23ee0305 349 state_change = 1;
benkatz 23:2adf23ee0305 350 break;
benkatz 37:c0f352d6e8e3 351 case 'z':
benkatz 37:c0f352d6e8e3 352 spi.SetMechOffset(0);
benkatz 47:e1196a851f76 353 spi.Sample(DT);
benkatz 37:c0f352d6e8e3 354 wait_us(20);
benkatz 37:c0f352d6e8e3 355 M_OFFSET = spi.GetMechPosition();
benkatz 37:c0f352d6e8e3 356 if (!prefs.ready()) prefs.open();
benkatz 37:c0f352d6e8e3 357 prefs.flush(); // Write new prefs to flash
benkatz 37:c0f352d6e8e3 358 prefs.close();
benkatz 37:c0f352d6e8e3 359 prefs.load();
benkatz 37:c0f352d6e8e3 360 spi.SetMechOffset(M_OFFSET);
benkatz 37:c0f352d6e8e3 361 printf("\n\r Saved new zero position: %.4f\n\r\n\r", M_OFFSET);
benkatz 37:c0f352d6e8e3 362
benkatz 37:c0f352d6e8e3 363 break;
benkatz 37:c0f352d6e8e3 364 }
benkatz 37:c0f352d6e8e3 365
benkatz 24:58c2d7571207 366 }
benkatz 24:58c2d7571207 367 else if(state == SETUP_MODE){
benkatz 25:f5741040c4bb 368 if(c == 13){
benkatz 24:58c2d7571207 369 switch (cmd_id){
benkatz 24:58c2d7571207 370 case 'b':
benkatz 24:58c2d7571207 371 I_BW = fmaxf(fminf(atof(cmd_val), 2000.0f), 100.0f);
benkatz 24:58c2d7571207 372 break;
benkatz 24:58c2d7571207 373 case 'i':
benkatz 24:58c2d7571207 374 CAN_ID = atoi(cmd_val);
benkatz 24:58c2d7571207 375 break;
benkatz 26:2b865c00d7e9 376 case 'm':
benkatz 26:2b865c00d7e9 377 CAN_MASTER = atoi(cmd_val);
benkatz 26:2b865c00d7e9 378 break;
benkatz 24:58c2d7571207 379 case 'l':
benkatz 51:6cd89bd6fcaa 380 I_MAX = fmaxf(fminf(atof(cmd_val), 40.0f), 0.0f);
benkatz 51:6cd89bd6fcaa 381 break;
benkatz 51:6cd89bd6fcaa 382 case 'f':
benkatz 51:6cd89bd6fcaa 383 I_FW_MAX = fmaxf(fminf(atof(cmd_val), 33.0f), 0.0f);
benkatz 24:58c2d7571207 384 break;
benkatz 28:8c7e29f719c5 385 case 't':
benkatz 28:8c7e29f719c5 386 CAN_TIMEOUT = atoi(cmd_val);
benkatz 28:8c7e29f719c5 387 break;
benkatz 55:c4c9fec8539c 388 case 'h':
benkatz 55:c4c9fec8539c 389 TEMP_MAX = fmaxf(fminf(atof(cmd_val), 150.0f), 0.0f);
benkatz 55:c4c9fec8539c 390 break;
benkatz 55:c4c9fec8539c 391 case 'c':
benkatz 55:c4c9fec8539c 392 I_MAX_CONT = fmaxf(fminf(atof(cmd_val), 40.0f), 0.0f);
benkatz 55:c4c9fec8539c 393 break;
benkatz 24:58c2d7571207 394 default:
benkatz 24:58c2d7571207 395 printf("\n\r '%c' Not a valid command prefix\n\r\n\r", cmd_id);
benkatz 24:58c2d7571207 396 break;
benkatz 24:58c2d7571207 397 }
benkatz 24:58c2d7571207 398
benkatz 24:58c2d7571207 399 if (!prefs.ready()) prefs.open();
benkatz 24:58c2d7571207 400 prefs.flush(); // Write new prefs to flash
benkatz 24:58c2d7571207 401 prefs.close();
benkatz 24:58c2d7571207 402 prefs.load();
benkatz 24:58c2d7571207 403 state_change = 1;
benkatz 24:58c2d7571207 404 char_count = 0;
benkatz 24:58c2d7571207 405 cmd_id = 0;
benkatz 24:58c2d7571207 406 for(int i = 0; i<8; i++){cmd_val[i] = 0;}
benkatz 24:58c2d7571207 407 }
benkatz 24:58c2d7571207 408 else{
benkatz 24:58c2d7571207 409 if(char_count == 0){cmd_id = c;}
benkatz 24:58c2d7571207 410 else{
benkatz 24:58c2d7571207 411 cmd_val[char_count-1] = c;
benkatz 24:58c2d7571207 412
benkatz 24:58c2d7571207 413 }
benkatz 24:58c2d7571207 414 pc.putc(c);
benkatz 24:58c2d7571207 415 char_count++;
benkatz 23:2adf23ee0305 416 }
benkatz 23:2adf23ee0305 417 }
benkatz 24:58c2d7571207 418 else if (state == ENCODER_MODE){
benkatz 24:58c2d7571207 419 switch (c){
benkatz 24:58c2d7571207 420 case 27:
benkatz 24:58c2d7571207 421 state = REST_MODE;
benkatz 24:58c2d7571207 422 state_change = 1;
benkatz 24:58c2d7571207 423 break;
benkatz 24:58c2d7571207 424 }
benkatz 24:58c2d7571207 425 }
benkatz 49:83d83040ea51 426 else if (state == MOTOR_MODE){
benkatz 49:83d83040ea51 427 switch (c){
benkatz 49:83d83040ea51 428 case 'd':
benkatz 49:83d83040ea51 429 controller.i_q_ref = 0;
benkatz 49:83d83040ea51 430 controller.i_d_ref = 0;
shaorui 57:059bd1333ba4 431 //printf("id_ref=iq_ref=0\n\r");
benkatz 49:83d83040ea51 432 }
benkatz 49:83d83040ea51 433 }
benkatz 24:58c2d7571207 434
benkatz 24:58c2d7571207 435 }
benkatz 22:60276ba87ac6 436 }
benkatz 0:4e1c4df6aabd 437
benkatz 0:4e1c4df6aabd 438 int main() {
benkatz 20:bf9ea5125d52 439 controller.v_bus = V_BUS;
benkatz 22:60276ba87ac6 440 controller.mode = 0;
benkatz 23:2adf23ee0305 441 Init_All_HW(&gpio); // Setup PWM, ADC, GPIO
benkatz 55:c4c9fec8539c 442 wait_us(100);
benkatz 44:8040fa2fcb0d 443
benkatz 44:8040fa2fcb0d 444 gpio.enable->write(1);
benkatz 44:8040fa2fcb0d 445 wait_us(100);
benkatz 45:26801179208e 446 drv.calibrate();
benkatz 45:26801179208e 447 wait_us(100);
benkatz 55:c4c9fec8539c 448 drv.write_DCR(0x0, DIS_GDF_DIS, 0x0, PWM_MODE_3X, 0x0, 0x0, 0x0, 0x0, 0x1);
benkatz 55:c4c9fec8539c 449 wait_us(100);
benkatz 55:c4c9fec8539c 450 drv.write_CSACR(0x0, 0x1, 0x0, CSA_GAIN_40, 0x0, 0x1, 0x1, 0x1, SEN_LVL_1_0); // calibrate shunt amplifiers
benkatz 44:8040fa2fcb0d 451 wait_us(100);
benkatz 55:c4c9fec8539c 452 zero_current(&controller.adc1_offset, &controller.adc2_offset);
benkatz 44:8040fa2fcb0d 453 wait_us(100);
benkatz 55:c4c9fec8539c 454 drv.write_CSACR(0x0, 0x1, 0x0, CSA_GAIN_40, 0x1, 0x0, 0x0, 0x0, SEN_LVL_1_0);
benkatz 55:c4c9fec8539c 455 wait_us(100);
benkatz 55:c4c9fec8539c 456 drv.write_OCPCR(TRETRY_50US, DEADTIME_50NS, OCP_NONE, OCP_DEG_8US, VDS_LVL_1_88);
benkatz 45:26801179208e 457
benkatz 45:26801179208e 458 //drv.enable_gd();
benkatz 47:e1196a851f76 459 drv.disable_gd();
benkatz 55:c4c9fec8539c 460 //zero_current(&controller.adc1_offset, &controller.adc2_offset); // Measure current sensor zero-offset
benkatz 55:c4c9fec8539c 461 //drv.enable_gd();
benkatz 20:bf9ea5125d52 462
benkatz 55:c4c9fec8539c 463 wait_us(100);
benkatz 55:c4c9fec8539c 464
benkatz 23:2adf23ee0305 465 reset_foc(&controller); // Reset current controller
benkatz 48:74a40481740c 466 reset_observer(&observer); // Reset observer
benkatz 26:2b865c00d7e9 467 //TIM1->CR1 |= TIM_CR1_UDIS; //enable interrupt
benkatz 20:bf9ea5125d52 468
benkatz 55:c4c9fec8539c 469 wait_us(100);
benkatz 37:c0f352d6e8e3 470 NVIC_SetPriority(TIM1_UP_TIM10_IRQn, 2); // commutation > communication
benkatz 37:c0f352d6e8e3 471 NVIC_SetPriority(CAN1_RX0_IRQn, 3);
benkatz 53:e85efce8c1eb 472 // attach 'CAN receive-complete' interrupt handler
benkatz 53:e85efce8c1eb 473
benkatz 53:e85efce8c1eb 474 // If preferences haven't been user configured yet, set defaults
benkatz 53:e85efce8c1eb 475 prefs.load(); // Read flash
benkatz 55:c4c9fec8539c 476 can.filter(CAN_ID , 0xFFF, CANStandard, 0);
benkatz 28:8c7e29f719c5 477 txMsg.id = CAN_MASTER;
benkatz 28:8c7e29f719c5 478 txMsg.len = 6;
benkatz 26:2b865c00d7e9 479 rxMsg.len = 8;
benkatz 53:e85efce8c1eb 480 can.attach(&onMsgReceived);
benkatz 23:2adf23ee0305 481
benkatz 37:c0f352d6e8e3 482 if(isnan(E_OFFSET)){E_OFFSET = 0.0f;}
benkatz 37:c0f352d6e8e3 483 if(isnan(M_OFFSET)){M_OFFSET = 0.0f;}
benkatz 48:74a40481740c 484 if(isnan(I_BW) || I_BW==-1){I_BW = 1000;}
benkatz 51:6cd89bd6fcaa 485 if(isnan(I_MAX) || I_MAX ==-1){I_MAX=40;}
benkatz 55:c4c9fec8539c 486 if(isnan(I_FW_MAX) || I_FW_MAX ==-1){I_FW_MAX=12;}
benkatz 48:74a40481740c 487 if(isnan(CAN_ID) || CAN_ID==-1){CAN_ID = 1;}
benkatz 48:74a40481740c 488 if(isnan(CAN_MASTER) || CAN_MASTER==-1){CAN_MASTER = 0;}
benkatz 55:c4c9fec8539c 489 if(isnan(CAN_TIMEOUT) || CAN_TIMEOUT==-1){CAN_TIMEOUT = 1000;}
benkatz 55:c4c9fec8539c 490 if(isnan(R_NOMINAL) || R_NOMINAL==-1){R_NOMINAL = 0.0f;}
benkatz 55:c4c9fec8539c 491 if(isnan(TEMP_MAX) || TEMP_MAX==-1){TEMP_MAX = 125.0f;}
benkatz 55:c4c9fec8539c 492 if(isnan(I_MAX_CONT) || I_MAX_CONT==-1){I_MAX_CONT = 14.0f;}
benkatz 25:f5741040c4bb 493 spi.SetElecOffset(E_OFFSET); // Set position sensor offset
benkatz 37:c0f352d6e8e3 494 spi.SetMechOffset(M_OFFSET);
benkatz 56:fe5056ac6740 495 spi.Sample(1.0f);
benkatz 56:fe5056ac6740 496 if(spi.GetMechPosition() > PI){spi.SetMechOffset(M_OFFSET+2.0f*PI);} // now zeroes to +- 30 degrees about nominal, independent of rollover point
benkatz 56:fe5056ac6740 497 else if (spi.GetMechPosition() < -PI){spi.SetMechOffset(M_OFFSET-2.0f*PI);}
benkatz 56:fe5056ac6740 498
benkatz 23:2adf23ee0305 499 int lut[128] = {0};
benkatz 23:2adf23ee0305 500 memcpy(&lut, &ENCODER_LUT, sizeof(lut));
benkatz 25:f5741040c4bb 501 spi.WriteLUT(lut); // Set potision sensor nonlinearity lookup table
benkatz 45:26801179208e 502 init_controller_params(&controller);
benkatz 55:c4c9fec8539c 503
benkatz 45:26801179208e 504
shaorui 57:059bd1333ba4 505 //pc.baud(921600);
shaorui 57:059bd1333ba4 506 pc.baud(460800); // set serial baud rate
benkatz 20:bf9ea5125d52 507 wait(.01);
benkatz 23:2adf23ee0305 508 pc.printf("\n\r\n\r HobbyKing Cheetah\n\r\n\r");
benkatz 20:bf9ea5125d52 509 wait(.01);
benkatz 23:2adf23ee0305 510 printf("\n\r Debug Info:\n\r");
benkatz 32:ccac5da77844 511 printf(" Firmware Version: %s\n\r", VERSION_NUM);
benkatz 23:2adf23ee0305 512 printf(" ADC1 Offset: %d ADC2 Offset: %d\n\r", controller.adc1_offset, controller.adc2_offset);
benkatz 23:2adf23ee0305 513 printf(" Position Sensor Electrical Offset: %.4f\n\r", E_OFFSET);
benkatz 37:c0f352d6e8e3 514 printf(" Output Zero Position: %.4f\n\r", M_OFFSET);
benkatz 24:58c2d7571207 515 printf(" CAN ID: %d\n\r", CAN_ID);
benkatz 44:8040fa2fcb0d 516
benkatz 44:8040fa2fcb0d 517
benkatz 55:c4c9fec8539c 518 TIM1->CR1 ^= TIM_CR1_UDIS;
benkatz 44:8040fa2fcb0d 519
benkatz 44:8040fa2fcb0d 520
benkatz 23:2adf23ee0305 521 pc.attach(&serial_interrupt); // attach serial interrupt
benkatz 50:ba72df25d10f 522
benkatz 20:bf9ea5125d52 523
benkatz 44:8040fa2fcb0d 524 int counter = 0;
benkatz 0:4e1c4df6aabd 525 while(1) {
benkatz 55:c4c9fec8539c 526 //drv.print_faults();
benkatz 53:e85efce8c1eb 527 wait(.1);
benkatz 55:c4c9fec8539c 528 //printf("%.3f %.3f\n\r" , observer.temperature, observer.q_in);
benkatz 55:c4c9fec8539c 529 if(controller.otw_flag){gpio.led->write(!gpio.led->read());}
shaorui 57:059bd1333ba4 530
benkatz 47:e1196a851f76 531 if(state == MOTOR_MODE)
benkatz 47:e1196a851f76 532 {
benkatz 55:c4c9fec8539c 533 if(controller.otw_flag){gpio.led->write(!gpio.led->read());}
benkatz 55:c4c9fec8539c 534 //printf("%f %f\n\r", controller.dtheta_mech, controller.i_d_ref);
benkatz 48:74a40481740c 535 //printf("%.3f %.3f %.3f\n\r", (float)observer.temperature, (float)observer.temperature2, observer.resistance);
benkatz 49:83d83040ea51 536 //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 537 //printf("%.3f %.3f %.3f %.3f\n\r", controller.dtheta_elec, observer.resistance, observer.temperature, observer.temp_measured);
benkatz 55:c4c9fec8539c 538 //printf("%.3f %.3f\n\r" , observer.temperature, observer.temp_measured);
shaorui 57:059bd1333ba4 539 printf("%.3f\n\r",controller.dtheta_mech);
benkatz 47:e1196a851f76 540 }
benkatz 55:c4c9fec8539c 541
shaorui 57:059bd1333ba4 542
benkatz 47:e1196a851f76 543
benkatz 0:4e1c4df6aabd 544 }
benkatz 0:4e1c4df6aabd 545 }