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

Dependencies:   FastPWM3 mbed-dev

Fork of Hobbyking_Cheetah_V1 by Ben Katz

Committer:
benkatz
Date:
Wed Aug 30 18:10:27 2017 +0000
Revision:
33:3ab3cce8b44d
Parent:
28:8c7e29f719c5
Added firmware version number to debug stream.  Fixed duty cycle mistake

Who changed what in which revision?

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