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

Dependencies:   FastPWM3 mbed-dev-f303

Fork of Hobbyking_Cheetah_V1 by Ben Katz

Committer:
benkatz
Date:
Thu May 24 15:03:25 2018 +0000
Revision:
41:304ac2affb23
Parent:
40:cd7e837b2b93
Child:
42:738fa01b0346
back to normal

Who changed what in which revision?

UserRevisionLine numberNew contents of line
benkatz 22:60276ba87ac6 1 /// high-bandwidth 3-phase motor control, for robots
benkatz 22:60276ba87ac6 2 /// Written by benkatz, with much inspiration from bayleyw, nkirkby, scolton, David Otten, and others
benkatz 22:60276ba87ac6 3 /// Hardware documentation can be found at build-its.blogspot.com
benkatz 22:60276ba87ac6 4
benkatz 22:60276ba87ac6 5 /// Written for the STM32F446, but can be implemented on other STM32 MCU's with some further register-diddling
benkatz 22:60276ba87ac6 6
benkatz 23:2adf23ee0305 7 #define REST_MODE 0
benkatz 23:2adf23ee0305 8 #define CALIBRATION_MODE 1
benkatz 26:2b865c00d7e9 9 #define MOTOR_MODE 2
benkatz 23:2adf23ee0305 10 #define SETUP_MODE 4
benkatz 23:2adf23ee0305 11 #define ENCODER_MODE 5
benkatz 22:60276ba87ac6 12
benkatz 38:67e4e1453a4b 13 #define VERSION_NUM "1.5"
benkatz 26:2b865c00d7e9 14
benkatz 18:f1d56f4acb39 15
benkatz 26:2b865c00d7e9 16 float __float_reg[64]; // Floats stored in flash
benkatz 26:2b865c00d7e9 17 int __int_reg[256]; // Ints stored in flash. Includes position sensor calibration lookup table
benkatz 17:3c5df2982199 18
benkatz 0:4e1c4df6aabd 19 #include "mbed.h"
benkatz 0:4e1c4df6aabd 20 #include "PositionSensor.h"
benkatz 20:bf9ea5125d52 21 #include "structs.h"
benkatz 20:bf9ea5125d52 22 #include "foc.h"
benkatz 22:60276ba87ac6 23 #include "calibration.h"
benkatz 20:bf9ea5125d52 24 #include "hw_setup.h"
benkatz 23:2adf23ee0305 25 #include "math_ops.h"
benkatz 20:bf9ea5125d52 26 #include "current_controller_config.h"
benkatz 20:bf9ea5125d52 27 #include "hw_config.h"
benkatz 20:bf9ea5125d52 28 #include "motor_config.h"
benkatz 23:2adf23ee0305 29 #include "stm32f4xx_flash.h"
benkatz 23:2adf23ee0305 30 #include "FlashWriter.h"
benkatz 23:2adf23ee0305 31 #include "user_config.h"
benkatz 23:2adf23ee0305 32 #include "PreferenceWriter.h"
benkatz 23:2adf23ee0305 33
benkatz 37:c0f352d6e8e3 34
benkatz 26:2b865c00d7e9 35
benkatz 23:2adf23ee0305 36 PreferenceWriter prefs(6);
benkatz 9:d7eb815cb057 37
benkatz 20:bf9ea5125d52 38 GPIOStruct gpio;
benkatz 20:bf9ea5125d52 39 ControllerStruct controller;
benkatz 20:bf9ea5125d52 40 COMStruct com;
benkatz 37:c0f352d6e8e3 41 ObserverStruct observer;
benkatz 9:d7eb815cb057 42
benkatz 26:2b865c00d7e9 43 //using namespace CANnucleo;
benkatz 17:3c5df2982199 44
benkatz 26:2b865c00d7e9 45 CAN can(PB_8, PB_9); // CAN Rx pin name, CAN Tx pin name
benkatz 26:2b865c00d7e9 46 CANMessage rxMsg;
benkatz 26:2b865c00d7e9 47 CANMessage txMsg;
benkatz 23:2adf23ee0305 48
benkatz 20:bf9ea5125d52 49
benkatz 9:d7eb815cb057 50 Serial pc(PA_2, PA_3);
benkatz 8:10ae7bc88d6e 51
benkatz 26:2b865c00d7e9 52 PositionSensorAM5147 spi(16384, 0.0, NPP);
benkatz 37:c0f352d6e8e3 53 //PositionSensorEncoder encoder(4096, 0, NPP);
benkatz 26:2b865c00d7e9 54
benkatz 26:2b865c00d7e9 55
benkatz 37:c0f352d6e8e3 56 DigitalOut toggle(PC_8);
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 #define P_MIN -12.5f
benkatz 26:2b865c00d7e9 63 #define P_MAX 12.5f
benkatz 41:304ac2affb23 64 #define V_MIN -45.0f
benkatz 41:304ac2affb23 65 #define V_MAX 45.0f
benkatz 26:2b865c00d7e9 66 #define KP_MIN 0.0f
benkatz 26:2b865c00d7e9 67 #define KP_MAX 500.0f
benkatz 26:2b865c00d7e9 68 #define KD_MIN 0.0f
benkatz 28:8c7e29f719c5 69 #define KD_MAX 5.0f
benkatz 26:2b865c00d7e9 70 #define T_MIN -18.0f
benkatz 26:2b865c00d7e9 71 #define T_MAX 18.0f
benkatz 26:2b865c00d7e9 72
benkatz 26:2b865c00d7e9 73
benkatz 26:2b865c00d7e9 74 /// CAN Reply Packet Structure ///
benkatz 26:2b865c00d7e9 75 /// 16 bit position, between -4*pi and 4*pi
benkatz 26:2b865c00d7e9 76 /// 12 bit velocity, between -30 and + 30 rad/s
benkatz 26:2b865c00d7e9 77 /// 12 bit current, between -40 and 40;
benkatz 26:2b865c00d7e9 78 /// CAN Packet is 5 8-bit words
benkatz 26:2b865c00d7e9 79 /// Formatted as follows. For each quantity, bit 0 is LSB
benkatz 26:2b865c00d7e9 80 /// 0: [position[15-8]]
benkatz 26:2b865c00d7e9 81 /// 1: [position[7-0]]
benkatz 26:2b865c00d7e9 82 /// 2: [velocity[11-4]]
benkatz 26:2b865c00d7e9 83 /// 3: [velocity[3-0], current[11-8]]
benkatz 26:2b865c00d7e9 84 /// 4: [current[7-0]]
benkatz 34:51647c6c500d 85 void pack_reply(CANMessage *msg, float p, float v, float t){
benkatz 26:2b865c00d7e9 86 int p_int = float_to_uint(p, P_MIN, P_MAX, 16);
benkatz 26:2b865c00d7e9 87 int v_int = float_to_uint(v, V_MIN, V_MAX, 12);
benkatz 34:51647c6c500d 88 int t_int = float_to_uint(t, -T_MAX, T_MAX, 12);
benkatz 28:8c7e29f719c5 89 msg->data[0] = CAN_ID;
benkatz 28:8c7e29f719c5 90 msg->data[1] = p_int>>8;
benkatz 28:8c7e29f719c5 91 msg->data[2] = p_int&0xFF;
benkatz 28:8c7e29f719c5 92 msg->data[3] = v_int>>4;
benkatz 34:51647c6c500d 93 msg->data[4] = ((v_int&0xF)<<4) + (t_int>>8);
benkatz 34:51647c6c500d 94 msg->data[5] = t_int&0xFF;
benkatz 26:2b865c00d7e9 95 }
benkatz 26:2b865c00d7e9 96
benkatz 26:2b865c00d7e9 97 /// CAN Command Packet Structure ///
benkatz 26:2b865c00d7e9 98 /// 16 bit position command, between -4*pi and 4*pi
benkatz 26:2b865c00d7e9 99 /// 12 bit velocity command, between -30 and + 30 rad/s
benkatz 26:2b865c00d7e9 100 /// 12 bit kp, between 0 and 500 N-m/rad
benkatz 26:2b865c00d7e9 101 /// 12 bit kd, between 0 and 100 N-m*s/rad
benkatz 26:2b865c00d7e9 102 /// 12 bit feed forward torque, between -18 and 18 N-m
benkatz 26:2b865c00d7e9 103 /// CAN Packet is 8 8-bit words
benkatz 26:2b865c00d7e9 104 /// Formatted as follows. For each quantity, bit 0 is LSB
benkatz 26:2b865c00d7e9 105 /// 0: [position[15-8]]
benkatz 26:2b865c00d7e9 106 /// 1: [position[7-0]]
benkatz 26:2b865c00d7e9 107 /// 2: [velocity[11-4]]
benkatz 26:2b865c00d7e9 108 /// 3: [velocity[3-0], kp[11-8]]
benkatz 26:2b865c00d7e9 109 /// 4: [kp[7-0]]
benkatz 26:2b865c00d7e9 110 /// 5: [kd[11-4]]
benkatz 26:2b865c00d7e9 111 /// 6: [kd[3-0], torque[11-8]]
benkatz 26:2b865c00d7e9 112 /// 7: [torque[7-0]]
benkatz 26:2b865c00d7e9 113 void unpack_cmd(CANMessage msg, ControllerStruct * controller){
benkatz 28:8c7e29f719c5 114 int p_int = (msg.data[0]<<8)|msg.data[1];
benkatz 28:8c7e29f719c5 115 int v_int = (msg.data[2]<<4)|(msg.data[3]>>4);
benkatz 28:8c7e29f719c5 116 int kp_int = ((msg.data[3]&0xF)<<8)|msg.data[4];
benkatz 28:8c7e29f719c5 117 int kd_int = (msg.data[5]<<4)|(msg.data[6]>>4);
benkatz 28:8c7e29f719c5 118 int t_int = ((msg.data[6]&0xF)<<8)|msg.data[7];
benkatz 28:8c7e29f719c5 119
benkatz 28:8c7e29f719c5 120 controller->p_des = uint_to_float(p_int, P_MIN, P_MAX, 16);
benkatz 28:8c7e29f719c5 121 controller->v_des = uint_to_float(v_int, V_MIN, V_MAX, 12);
benkatz 28:8c7e29f719c5 122 controller->kp = uint_to_float(kp_int, KP_MIN, KP_MAX, 12);
benkatz 28:8c7e29f719c5 123 controller->kd = uint_to_float(kd_int, KD_MIN, KD_MAX, 12);
benkatz 28:8c7e29f719c5 124 controller->t_ff = uint_to_float(t_int, T_MIN, T_MAX, 12);
benkatz 28:8c7e29f719c5 125 //printf("Received ");
benkatz 28:8c7e29f719c5 126 //printf("%.3f %.3f %.3f %.3f %.3f %.3f", controller->p_des, controller->v_des, controller->kp, controller->kd, controller->t_ff, controller->i_q_ref);
benkatz 28:8c7e29f719c5 127 //printf("\n\r");
benkatz 26:2b865c00d7e9 128 }
benkatz 26:2b865c00d7e9 129
benkatz 26:2b865c00d7e9 130 void onMsgReceived() {
benkatz 26:2b865c00d7e9 131 //msgAvailable = true;
benkatz 26:2b865c00d7e9 132 //printf("%.3f %.3f %.3f\n\r", controller.theta_mech, controller.dtheta_mech, controller.i_q);
benkatz 26:2b865c00d7e9 133 can.read(rxMsg);
benkatz 28:8c7e29f719c5 134 if((rxMsg.id == CAN_ID)){
benkatz 28:8c7e29f719c5 135 controller.timeout = 0;
benkatz 28:8c7e29f719c5 136 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 137 state = MOTOR_MODE;
benkatz 28:8c7e29f719c5 138 state_change = 1;
benkatz 28:8c7e29f719c5 139 }
benkatz 28:8c7e29f719c5 140 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 141 state = REST_MODE;
benkatz 28:8c7e29f719c5 142 state_change = 1;
benkatz 37:c0f352d6e8e3 143 gpio.led->write(0);;
benkatz 28:8c7e29f719c5 144 }
benkatz 28:8c7e29f719c5 145 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 146 spi.ZeroPosition();
benkatz 28:8c7e29f719c5 147 }
benkatz 28:8c7e29f719c5 148 else if(state == MOTOR_MODE){
benkatz 28:8c7e29f719c5 149 unpack_cmd(rxMsg, &controller);
benkatz 28:8c7e29f719c5 150 }
benkatz 37:c0f352d6e8e3 151 pack_reply(&txMsg, controller.theta_mech, controller.dtheta_mech, controller.i_q_filt*KT_OUT);
benkatz 37:c0f352d6e8e3 152 can.write(txMsg);
benkatz 28:8c7e29f719c5 153 }
benkatz 26:2b865c00d7e9 154
benkatz 26:2b865c00d7e9 155 }
benkatz 26:2b865c00d7e9 156
benkatz 23:2adf23ee0305 157 void enter_menu_state(void){
benkatz 23:2adf23ee0305 158 printf("\n\r\n\r\n\r");
benkatz 23:2adf23ee0305 159 printf(" Commands:\n\r");
benkatz 26:2b865c00d7e9 160 printf(" m - Motor Mode\n\r");
benkatz 23:2adf23ee0305 161 printf(" c - Calibrate Encoder\n\r");
benkatz 23:2adf23ee0305 162 printf(" s - Setup\n\r");
benkatz 23:2adf23ee0305 163 printf(" e - Display Encoder\n\r");
benkatz 37:c0f352d6e8e3 164 printf(" z - Set Zero Position\n\r");
benkatz 23:2adf23ee0305 165 printf(" esc - Exit to Menu\n\r");
benkatz 23:2adf23ee0305 166 state_change = 0;
benkatz 25:f5741040c4bb 167 gpio.enable->write(0);
benkatz 37:c0f352d6e8e3 168 gpio.led->write(0);
benkatz 23:2adf23ee0305 169 }
benkatz 24:58c2d7571207 170
benkatz 24:58c2d7571207 171 void enter_setup_state(void){
benkatz 24:58c2d7571207 172 printf("\n\r\n\r Configuration Options \n\r\n\n");
benkatz 28:8c7e29f719c5 173 printf(" %-4s %-31s %-5s %-6s %-5s\n\r\n\r", "prefix", "parameter", "min", "max", "current value");
benkatz 28:8c7e29f719c5 174 printf(" %-4s %-31s %-5s %-6s %.1f\n\r", "b", "Current Bandwidth (Hz)", "100", "2000", I_BW);
benkatz 28:8c7e29f719c5 175 printf(" %-4s %-31s %-5s %-6s %-5i\n\r", "i", "CAN ID", "0", "127", CAN_ID);
benkatz 28:8c7e29f719c5 176 printf(" %-4s %-31s %-5s %-6s %-5i\n\r", "m", "CAN Master ID", "0", "127", CAN_MASTER);
benkatz 28:8c7e29f719c5 177 printf(" %-4s %-31s %-5s %-6s %.1f\n\r", "l", "Torque Limit (N-m)", "0.0", "18.0", TORQUE_LIMIT);
benkatz 28:8c7e29f719c5 178 printf(" %-4s %-31s %-5s %-6s %d\n\r", "t", "CAN Timeout (cycles)(0 = none)", "0", "100000", CAN_TIMEOUT);
benkatz 24:58c2d7571207 179 printf("\n\r To change a value, type 'prefix''value''ENTER'\n\r i.e. 'b1000''ENTER'\n\r\n\r");
benkatz 24:58c2d7571207 180 state_change = 0;
benkatz 24:58c2d7571207 181 }
benkatz 22:60276ba87ac6 182
benkatz 23:2adf23ee0305 183 void enter_torque_mode(void){
benkatz 37:c0f352d6e8e3 184 controller.ovp_flag = 0;
benkatz 28:8c7e29f719c5 185 gpio.enable->write(1); // Enable gate drive
benkatz 28:8c7e29f719c5 186 reset_foc(&controller); // Tesets integrators, and other control loop parameters
benkatz 28:8c7e29f719c5 187 wait(.001);
benkatz 23:2adf23ee0305 188 controller.i_d_ref = 0;
benkatz 28:8c7e29f719c5 189 controller.i_q_ref = 0; // Current Setpoints
benkatz 37:c0f352d6e8e3 190 gpio.led->write(1); // Turn on status LED
benkatz 25:f5741040c4bb 191 state_change = 0;
benkatz 28:8c7e29f719c5 192 printf("\n\r Entering Motor Mode \n\r");
benkatz 23:2adf23ee0305 193 }
benkatz 22:60276ba87ac6 194
benkatz 23:2adf23ee0305 195 void calibrate(void){
benkatz 25:f5741040c4bb 196 gpio.enable->write(1); // Enable gate drive
benkatz 37:c0f352d6e8e3 197 gpio.led->write(1); // Turn on status LED
benkatz 25:f5741040c4bb 198 order_phases(&spi, &gpio, &controller, &prefs); // Check phase ordering
benkatz 25:f5741040c4bb 199 calibrate(&spi, &gpio, &controller, &prefs); // Perform calibration procedure
benkatz 37:c0f352d6e8e3 200 gpio.led->write(0);; // Turn off status LED
benkatz 23:2adf23ee0305 201 wait(.2);
benkatz 25:f5741040c4bb 202 gpio.enable->write(0); // Turn off gate drive
benkatz 23:2adf23ee0305 203 printf("\n\r Calibration complete. Press 'esc' to return to menu\n\r");
benkatz 23:2adf23ee0305 204 state_change = 0;
benkatz 23:2adf23ee0305 205 }
benkatz 23:2adf23ee0305 206
benkatz 23:2adf23ee0305 207 void print_encoder(void){
benkatz 23:2adf23ee0305 208 printf(" Mechanical Angle: %f Electrical Angle: %f Raw: %d\n\r", spi.GetMechPosition(), spi.GetElecPosition(), spi.GetRawPosition());
benkatz 23:2adf23ee0305 209 wait(.05);
benkatz 22:60276ba87ac6 210 }
benkatz 20:bf9ea5125d52 211
benkatz 23:2adf23ee0305 212 /// Current Sampling Interrupt ///
benkatz 23:2adf23ee0305 213 /// This runs at 40 kHz, regardless of of the mode the controller is in ///
benkatz 2:8724412ad628 214 extern "C" void TIM1_UP_TIM10_IRQHandler(void) {
benkatz 2:8724412ad628 215 if (TIM1->SR & TIM_SR_UIF ) {
benkatz 23:2adf23ee0305 216
benkatz 23:2adf23ee0305 217 ///Sample current always ///
benkatz 25:f5741040c4bb 218 ADC1->CR2 |= 0x40000000; // Begin sample and conversion
benkatz 22:60276ba87ac6 219 //volatile int delay;
benkatz 20:bf9ea5125d52 220 //for (delay = 0; delay < 55; delay++);
benkatz 37:c0f352d6e8e3 221 controller.adc2_raw = ADC2->DR; // Read ADC Data Registers
benkatz 23:2adf23ee0305 222 controller.adc1_raw = ADC1->DR;
benkatz 37:c0f352d6e8e3 223 controller.adc3_raw = ADC3->DR;
benkatz 37:c0f352d6e8e3 224 spi.Sample(); // sample position sensor
benkatz 37:c0f352d6e8e3 225 controller.theta_elec = spi.GetElecPosition();
benkatz 37:c0f352d6e8e3 226 controller.theta_mech = (1.0f/GR)*spi.GetMechPosition();
benkatz 37:c0f352d6e8e3 227 controller.dtheta_mech = (1.0f/GR)*spi.GetMechVelocity();
benkatz 37:c0f352d6e8e3 228 controller.dtheta_elec = spi.GetElecVelocity();
benkatz 37:c0f352d6e8e3 229 controller.v_bus = 0.95f*controller.v_bus + 0.05f*((float)controller.adc3_raw)*V_SCALE;
benkatz 23:2adf23ee0305 230 ///
benkatz 20:bf9ea5125d52 231
benkatz 23:2adf23ee0305 232 /// Check state machine state, and run the appropriate function ///
benkatz 23:2adf23ee0305 233 switch(state){
benkatz 37:c0f352d6e8e3 234 case REST_MODE: // Do nothing
benkatz 23:2adf23ee0305 235 if(state_change){
benkatz 23:2adf23ee0305 236 enter_menu_state();
benkatz 23:2adf23ee0305 237 }
benkatz 23:2adf23ee0305 238 break;
benkatz 22:60276ba87ac6 239
benkatz 23:2adf23ee0305 240 case CALIBRATION_MODE: // Run encoder calibration procedure
benkatz 23:2adf23ee0305 241 if(state_change){
benkatz 23:2adf23ee0305 242 calibrate();
benkatz 23:2adf23ee0305 243 }
benkatz 23:2adf23ee0305 244 break;
benkatz 23:2adf23ee0305 245
benkatz 26:2b865c00d7e9 246 case MOTOR_MODE: // Run torque control
benkatz 25:f5741040c4bb 247 if(state_change){
benkatz 25:f5741040c4bb 248 enter_torque_mode();
benkatz 28:8c7e29f719c5 249 count = 0;
benkatz 25:f5741040c4bb 250 }
benkatz 28:8c7e29f719c5 251 else{
benkatz 37:c0f352d6e8e3 252 /*
benkatz 37:c0f352d6e8e3 253 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 254 gpio.enable->write(0);
benkatz 37:c0f352d6e8e3 255 controller.ovp_flag = 1;
benkatz 37:c0f352d6e8e3 256 state = REST_MODE;
benkatz 37:c0f352d6e8e3 257 state_change = 1;
benkatz 37:c0f352d6e8e3 258 printf("OVP Triggered!\n\r");
benkatz 37:c0f352d6e8e3 259 }
benkatz 37:c0f352d6e8e3 260 */
benkatz 37:c0f352d6e8e3 261
benkatz 40:cd7e837b2b93 262 torque_control(&controller);
benkatz 28:8c7e29f719c5 263 if((controller.timeout > CAN_TIMEOUT) && (CAN_TIMEOUT > 0)){
benkatz 28:8c7e29f719c5 264 controller.i_d_ref = 0;
benkatz 28:8c7e29f719c5 265 controller.i_q_ref = 0;
benkatz 37:c0f352d6e8e3 266 controller.kp = 0;
benkatz 37:c0f352d6e8e3 267 controller.kd = 0;
benkatz 37:c0f352d6e8e3 268 controller.t_ff = 0;
benkatz 28:8c7e29f719c5 269 }
benkatz 37:c0f352d6e8e3 270 commutate(&controller, &observer, &gpio, controller.theta_elec); // Run current loop
benkatz 28:8c7e29f719c5 271 controller.timeout += 1;
benkatz 38:67e4e1453a4b 272
benkatz 39:3580a907ef93 273 /*
benkatz 37:c0f352d6e8e3 274 count++;
benkatz 40:cd7e837b2b93 275 if(count == 4000){
benkatz 40:cd7e837b2b93 276 printf("%.4f\n\r", controller.dtheta_mech);
benkatz 32:ccac5da77844 277 count = 0;
benkatz 23:2adf23ee0305 278 }
benkatz 39:3580a907ef93 279 */
benkatz 38:67e4e1453a4b 280
benkatz 37:c0f352d6e8e3 281
benkatz 37:c0f352d6e8e3 282 }
benkatz 23:2adf23ee0305 283 break;
benkatz 23:2adf23ee0305 284 case SETUP_MODE:
benkatz 23:2adf23ee0305 285 if(state_change){
benkatz 24:58c2d7571207 286 enter_setup_state();
benkatz 23:2adf23ee0305 287 }
benkatz 23:2adf23ee0305 288 break;
benkatz 23:2adf23ee0305 289 case ENCODER_MODE:
benkatz 23:2adf23ee0305 290 print_encoder();
benkatz 23:2adf23ee0305 291 break;
benkatz 37:c0f352d6e8e3 292 }
benkatz 2:8724412ad628 293 }
benkatz 23:2adf23ee0305 294 TIM1->SR = 0x0; // reset the status register
benkatz 2:8724412ad628 295 }
benkatz 0:4e1c4df6aabd 296
benkatz 25:f5741040c4bb 297
benkatz 24:58c2d7571207 298 char cmd_val[8] = {0};
benkatz 24:58c2d7571207 299 char cmd_id = 0;
benkatz 25:f5741040c4bb 300 char char_count = 0;
benkatz 24:58c2d7571207 301
benkatz 25:f5741040c4bb 302 /// Manage state machine with commands from serial terminal or configurator gui ///
benkatz 25:f5741040c4bb 303 /// Called when data received over serial ///
benkatz 23:2adf23ee0305 304 void serial_interrupt(void){
benkatz 23:2adf23ee0305 305 while(pc.readable()){
benkatz 23:2adf23ee0305 306 char c = pc.getc();
benkatz 25:f5741040c4bb 307 if(c == 27){
benkatz 25:f5741040c4bb 308 state = REST_MODE;
benkatz 25:f5741040c4bb 309 state_change = 1;
benkatz 25:f5741040c4bb 310 char_count = 0;
benkatz 25:f5741040c4bb 311 cmd_id = 0;
benkatz 37:c0f352d6e8e3 312 gpio.led->write(0);;
benkatz 25:f5741040c4bb 313 for(int i = 0; i<8; i++){cmd_val[i] = 0;}
benkatz 25:f5741040c4bb 314 }
benkatz 24:58c2d7571207 315 if(state == REST_MODE){
benkatz 23:2adf23ee0305 316 switch (c){
benkatz 23:2adf23ee0305 317 case 'c':
benkatz 23:2adf23ee0305 318 state = CALIBRATION_MODE;
benkatz 23:2adf23ee0305 319 state_change = 1;
benkatz 23:2adf23ee0305 320 break;
benkatz 26:2b865c00d7e9 321 case 'm':
benkatz 26:2b865c00d7e9 322 state = MOTOR_MODE;
benkatz 23:2adf23ee0305 323 state_change = 1;
benkatz 23:2adf23ee0305 324 break;
benkatz 23:2adf23ee0305 325 case 'e':
benkatz 23:2adf23ee0305 326 state = ENCODER_MODE;
benkatz 23:2adf23ee0305 327 state_change = 1;
benkatz 23:2adf23ee0305 328 break;
benkatz 23:2adf23ee0305 329 case 's':
benkatz 23:2adf23ee0305 330 state = SETUP_MODE;
benkatz 23:2adf23ee0305 331 state_change = 1;
benkatz 23:2adf23ee0305 332 break;
benkatz 37:c0f352d6e8e3 333 case 'z':
benkatz 37:c0f352d6e8e3 334 spi.SetMechOffset(0);
benkatz 37:c0f352d6e8e3 335 spi.Sample();
benkatz 37:c0f352d6e8e3 336 wait_us(20);
benkatz 37:c0f352d6e8e3 337 M_OFFSET = spi.GetMechPosition();
benkatz 37:c0f352d6e8e3 338 if (!prefs.ready()) prefs.open();
benkatz 37:c0f352d6e8e3 339 prefs.flush(); // Write new prefs to flash
benkatz 37:c0f352d6e8e3 340 prefs.close();
benkatz 37:c0f352d6e8e3 341 prefs.load();
benkatz 37:c0f352d6e8e3 342 spi.SetMechOffset(M_OFFSET);
benkatz 37:c0f352d6e8e3 343 printf("\n\r Saved new zero position: %.4f\n\r\n\r", M_OFFSET);
benkatz 37:c0f352d6e8e3 344
benkatz 37:c0f352d6e8e3 345 break;
benkatz 37:c0f352d6e8e3 346 }
benkatz 37:c0f352d6e8e3 347
benkatz 24:58c2d7571207 348 }
benkatz 24:58c2d7571207 349 else if(state == SETUP_MODE){
benkatz 25:f5741040c4bb 350 if(c == 13){
benkatz 24:58c2d7571207 351 switch (cmd_id){
benkatz 24:58c2d7571207 352 case 'b':
benkatz 24:58c2d7571207 353 I_BW = fmaxf(fminf(atof(cmd_val), 2000.0f), 100.0f);
benkatz 24:58c2d7571207 354 break;
benkatz 24:58c2d7571207 355 case 'i':
benkatz 24:58c2d7571207 356 CAN_ID = atoi(cmd_val);
benkatz 24:58c2d7571207 357 break;
benkatz 26:2b865c00d7e9 358 case 'm':
benkatz 26:2b865c00d7e9 359 CAN_MASTER = atoi(cmd_val);
benkatz 26:2b865c00d7e9 360 break;
benkatz 24:58c2d7571207 361 case 'l':
benkatz 24:58c2d7571207 362 TORQUE_LIMIT = fmaxf(fminf(atof(cmd_val), 18.0f), 0.0f);
benkatz 24:58c2d7571207 363 break;
benkatz 28:8c7e29f719c5 364 case 't':
benkatz 28:8c7e29f719c5 365 CAN_TIMEOUT = atoi(cmd_val);
benkatz 28:8c7e29f719c5 366 break;
benkatz 24:58c2d7571207 367 default:
benkatz 24:58c2d7571207 368 printf("\n\r '%c' Not a valid command prefix\n\r\n\r", cmd_id);
benkatz 24:58c2d7571207 369 break;
benkatz 24:58c2d7571207 370 }
benkatz 24:58c2d7571207 371
benkatz 24:58c2d7571207 372 if (!prefs.ready()) prefs.open();
benkatz 24:58c2d7571207 373 prefs.flush(); // Write new prefs to flash
benkatz 24:58c2d7571207 374 prefs.close();
benkatz 24:58c2d7571207 375 prefs.load();
benkatz 24:58c2d7571207 376 state_change = 1;
benkatz 24:58c2d7571207 377 char_count = 0;
benkatz 24:58c2d7571207 378 cmd_id = 0;
benkatz 24:58c2d7571207 379 for(int i = 0; i<8; i++){cmd_val[i] = 0;}
benkatz 24:58c2d7571207 380 }
benkatz 24:58c2d7571207 381 else{
benkatz 24:58c2d7571207 382 if(char_count == 0){cmd_id = c;}
benkatz 24:58c2d7571207 383 else{
benkatz 24:58c2d7571207 384 cmd_val[char_count-1] = c;
benkatz 24:58c2d7571207 385
benkatz 24:58c2d7571207 386 }
benkatz 24:58c2d7571207 387 pc.putc(c);
benkatz 24:58c2d7571207 388 char_count++;
benkatz 23:2adf23ee0305 389 }
benkatz 23:2adf23ee0305 390 }
benkatz 24:58c2d7571207 391 else if (state == ENCODER_MODE){
benkatz 24:58c2d7571207 392 switch (c){
benkatz 24:58c2d7571207 393 case 27:
benkatz 24:58c2d7571207 394 state = REST_MODE;
benkatz 24:58c2d7571207 395 state_change = 1;
benkatz 24:58c2d7571207 396 break;
benkatz 24:58c2d7571207 397 }
benkatz 24:58c2d7571207 398 }
benkatz 24:58c2d7571207 399
benkatz 24:58c2d7571207 400 }
benkatz 22:60276ba87ac6 401 }
benkatz 0:4e1c4df6aabd 402
benkatz 0:4e1c4df6aabd 403 int main() {
benkatz 23:2adf23ee0305 404
benkatz 20:bf9ea5125d52 405 controller.v_bus = V_BUS;
benkatz 22:60276ba87ac6 406 controller.mode = 0;
benkatz 23:2adf23ee0305 407 Init_All_HW(&gpio); // Setup PWM, ADC, GPIO
benkatz 20:bf9ea5125d52 408
benkatz 9:d7eb815cb057 409 wait(.1);
benkatz 26:2b865c00d7e9 410 gpio.enable->write(1);
benkatz 26:2b865c00d7e9 411 TIM1->CCR3 = 0x708*(1.0f); // Write duty cycles
benkatz 26:2b865c00d7e9 412 TIM1->CCR2 = 0x708*(1.0f);
benkatz 26:2b865c00d7e9 413 TIM1->CCR1 = 0x708*(1.0f);
benkatz 23:2adf23ee0305 414 zero_current(&controller.adc1_offset, &controller.adc2_offset); // Measure current sensor zero-offset
benkatz 26:2b865c00d7e9 415 gpio.enable->write(0);
benkatz 23:2adf23ee0305 416 reset_foc(&controller); // Reset current controller
benkatz 26:2b865c00d7e9 417 TIM1->CR1 ^= TIM_CR1_UDIS;
benkatz 26:2b865c00d7e9 418 //TIM1->CR1 |= TIM_CR1_UDIS; //enable interrupt
benkatz 20:bf9ea5125d52 419
benkatz 20:bf9ea5125d52 420 wait(.1);
benkatz 37:c0f352d6e8e3 421 NVIC_SetPriority(TIM1_UP_TIM10_IRQn, 2); // commutation > communication
benkatz 37:c0f352d6e8e3 422 NVIC_SetPriority(CAN1_RX0_IRQn, 3);
benkatz 22:60276ba87ac6 423
benkatz 26:2b865c00d7e9 424
benkatz 23:2adf23ee0305 425 can.frequency(1000000); // set bit rate to 1Mbps
benkatz 26:2b865c00d7e9 426 can.filter(CAN_ID<<21, 0xFFE00004, CANStandard, 0);
benkatz 26:2b865c00d7e9 427 //can.filter(CAN_ID, 0xF, CANStandard, 0);
benkatz 26:2b865c00d7e9 428 can.attach(&onMsgReceived); // attach 'CAN receive-complete' interrupt handler
benkatz 28:8c7e29f719c5 429 txMsg.id = CAN_MASTER;
benkatz 28:8c7e29f719c5 430 txMsg.len = 6;
benkatz 26:2b865c00d7e9 431 rxMsg.len = 8;
benkatz 23:2adf23ee0305 432
benkatz 25:f5741040c4bb 433 prefs.load(); // Read flash
benkatz 37:c0f352d6e8e3 434 if(isnan(E_OFFSET)){E_OFFSET = 0.0f;}
benkatz 37:c0f352d6e8e3 435 if(isnan(M_OFFSET)){M_OFFSET = 0.0f;}
benkatz 25:f5741040c4bb 436 spi.SetElecOffset(E_OFFSET); // Set position sensor offset
benkatz 37:c0f352d6e8e3 437 spi.SetMechOffset(M_OFFSET);
benkatz 23:2adf23ee0305 438 int lut[128] = {0};
benkatz 23:2adf23ee0305 439 memcpy(&lut, &ENCODER_LUT, sizeof(lut));
benkatz 25:f5741040c4bb 440 spi.WriteLUT(lut); // Set potision sensor nonlinearity lookup table
benkatz 23:2adf23ee0305 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 23:2adf23ee0305 452
benkatz 23:2adf23ee0305 453 pc.attach(&serial_interrupt); // attach serial interrupt
benkatz 22:60276ba87ac6 454
benkatz 23:2adf23ee0305 455 state_change = 1;
benkatz 20:bf9ea5125d52 456
benkatz 22:60276ba87ac6 457
benkatz 0:4e1c4df6aabd 458 while(1) {
benkatz 11:c83b18d41e54 459
benkatz 0:4e1c4df6aabd 460 }
benkatz 0:4e1c4df6aabd 461 }