Ben Katz / Hobbyking_Cheetah_Compact

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

Dependencies:   FastPWM3 mbed

Fork of Hobbyking_Cheetah_V1 by Ben Katz

Committer:
benkatz
Date:
Mon May 01 15:22:58 2017 +0000
Revision:
26:2b865c00d7e9
Parent:
25:f5741040c4bb
- Added CAN Send/Receive; - Updated to most recent MBED (Broke SPI 16-bit read/write.  Hacked to work with 2 8-bit writes now); - Removed most sine/cos calculations; - Fixed sign error in autocalibration routine

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