1

Dependencies:   mbed-dev-f303 FastPWM3

Committer:
benkatz
Date:
Wed May 17 14:53:22 2017 +0000
Revision:
27:501fee691e0e
Parent:
26:2b865c00d7e9
Child:
28:8c7e29f719c5
Setting PWM ARR value in config file now propagates everywhere.

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