Modifying the HKCC for no readily apparent reason

Dependencies:   FastPWM3

Committer:
benkatz
Date:
Fri Mar 02 15:24:00 2018 +0000
Revision:
36:d88fd41f60a6
Parent:
34:51647c6c500d
Child:
37:c0f352d6e8e3
updated mbed-dev to latest version, fixing 16-bit spi issues

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 36:d88fd41f60a6 13 #define VERSION_NUM "1.2"
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 26:2b865c00d7e9 34
benkatz 23:2adf23ee0305 35 PreferenceWriter prefs(6);
benkatz 9:d7eb815cb057 36
benkatz 20:bf9ea5125d52 37 GPIOStruct gpio;
benkatz 20:bf9ea5125d52 38 ControllerStruct controller;
benkatz 20:bf9ea5125d52 39 COMStruct com;
benkatz 22:60276ba87ac6 40 VelocityEstimatorStruct velocity;
benkatz 17:3c5df2982199 41
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 26:2b865c00d7e9 53 PositionSensorEncoder encoder(4096, 0, NPP);
benkatz 26:2b865c00d7e9 54
benkatz 26:2b865c00d7e9 55
benkatz 26:2b865c00d7e9 56 DigitalOut toggle(PA_0);
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 26:2b865c00d7e9 64 #define V_MIN -30.0f
benkatz 26:2b865c00d7e9 65 #define V_MAX 30.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 26:2b865c00d7e9 125
benkatz 26:2b865c00d7e9 126
benkatz 28:8c7e29f719c5 127 //printf("Received ");
benkatz 28:8c7e29f719c5 128 //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 129 //printf("\n\r");
benkatz 28:8c7e29f719c5 130
benkatz 26:2b865c00d7e9 131
benkatz 26:2b865c00d7e9 132 }
benkatz 26:2b865c00d7e9 133
benkatz 26:2b865c00d7e9 134 void onMsgReceived() {
benkatz 26:2b865c00d7e9 135 //msgAvailable = true;
benkatz 26:2b865c00d7e9 136 //printf("%.3f %.3f %.3f\n\r", controller.theta_mech, controller.dtheta_mech, controller.i_q);
benkatz 26:2b865c00d7e9 137 can.read(rxMsg);
benkatz 28:8c7e29f719c5 138
benkatz 28:8c7e29f719c5 139 if((rxMsg.id == CAN_ID)){
benkatz 28:8c7e29f719c5 140 controller.timeout = 0;
benkatz 28:8c7e29f719c5 141 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 142 state = MOTOR_MODE;
benkatz 28:8c7e29f719c5 143 state_change = 1;
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]==0xFD))){
benkatz 28:8c7e29f719c5 146 state = REST_MODE;
benkatz 28:8c7e29f719c5 147 state_change = 1;
benkatz 28:8c7e29f719c5 148 GPIOC->ODR &= !(1 << 5);
benkatz 28:8c7e29f719c5 149 }
benkatz 28:8c7e29f719c5 150 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 151 spi.ZeroPosition();
benkatz 28:8c7e29f719c5 152 }
benkatz 28:8c7e29f719c5 153 else if(state == MOTOR_MODE){
benkatz 28:8c7e29f719c5 154 unpack_cmd(rxMsg, &controller);
benkatz 34:51647c6c500d 155 pack_reply(&txMsg, controller.theta_mech, controller.dtheta_mech, controller.i_q_filt*KT_OUT);
benkatz 28:8c7e29f719c5 156 can.write(txMsg);
benkatz 28:8c7e29f719c5 157 }
benkatz 28:8c7e29f719c5 158 }
benkatz 26:2b865c00d7e9 159
benkatz 26:2b865c00d7e9 160 }
benkatz 26:2b865c00d7e9 161
benkatz 23:2adf23ee0305 162 void enter_menu_state(void){
benkatz 23:2adf23ee0305 163 printf("\n\r\n\r\n\r");
benkatz 23:2adf23ee0305 164 printf(" Commands:\n\r");
benkatz 26:2b865c00d7e9 165 printf(" m - Motor Mode\n\r");
benkatz 23:2adf23ee0305 166 printf(" c - Calibrate Encoder\n\r");
benkatz 23:2adf23ee0305 167 printf(" s - Setup\n\r");
benkatz 23:2adf23ee0305 168 printf(" e - Display Encoder\n\r");
benkatz 23:2adf23ee0305 169 printf(" esc - Exit to Menu\n\r");
benkatz 23:2adf23ee0305 170 state_change = 0;
benkatz 25:f5741040c4bb 171 gpio.enable->write(0);
benkatz 23:2adf23ee0305 172 }
benkatz 24:58c2d7571207 173
benkatz 24:58c2d7571207 174 void enter_setup_state(void){
benkatz 24:58c2d7571207 175 printf("\n\r\n\r Configuration Options \n\r\n\n");
benkatz 28:8c7e29f719c5 176 printf(" %-4s %-31s %-5s %-6s %-5s\n\r\n\r", "prefix", "parameter", "min", "max", "current value");
benkatz 28:8c7e29f719c5 177 printf(" %-4s %-31s %-5s %-6s %.1f\n\r", "b", "Current Bandwidth (Hz)", "100", "2000", I_BW);
benkatz 28:8c7e29f719c5 178 printf(" %-4s %-31s %-5s %-6s %-5i\n\r", "i", "CAN ID", "0", "127", CAN_ID);
benkatz 28:8c7e29f719c5 179 printf(" %-4s %-31s %-5s %-6s %-5i\n\r", "m", "CAN Master ID", "0", "127", CAN_MASTER);
benkatz 28:8c7e29f719c5 180 printf(" %-4s %-31s %-5s %-6s %.1f\n\r", "l", "Torque Limit (N-m)", "0.0", "18.0", TORQUE_LIMIT);
benkatz 28:8c7e29f719c5 181 printf(" %-4s %-31s %-5s %-6s %d\n\r", "t", "CAN Timeout (cycles)(0 = none)", "0", "100000", CAN_TIMEOUT);
benkatz 24:58c2d7571207 182 printf("\n\r To change a value, type 'prefix''value''ENTER'\n\r i.e. 'b1000''ENTER'\n\r\n\r");
benkatz 24:58c2d7571207 183 state_change = 0;
benkatz 24:58c2d7571207 184 }
benkatz 22:60276ba87ac6 185
benkatz 23:2adf23ee0305 186 void enter_torque_mode(void){
benkatz 28:8c7e29f719c5 187 gpio.enable->write(1); // Enable gate drive
benkatz 28:8c7e29f719c5 188 reset_foc(&controller); // Tesets integrators, and other control loop parameters
benkatz 28:8c7e29f719c5 189 wait(.001);
benkatz 23:2adf23ee0305 190 controller.i_d_ref = 0;
benkatz 28:8c7e29f719c5 191 controller.i_q_ref = 0; // Current Setpoints
benkatz 26:2b865c00d7e9 192 GPIOC->ODR |= (1 << 5); // Turn on status LED
benkatz 25:f5741040c4bb 193 state_change = 0;
benkatz 28:8c7e29f719c5 194 printf("\n\r Entering Motor Mode \n\r");
benkatz 23:2adf23ee0305 195 }
benkatz 22:60276ba87ac6 196
benkatz 23:2adf23ee0305 197 void calibrate(void){
benkatz 25:f5741040c4bb 198 gpio.enable->write(1); // Enable gate drive
benkatz 26:2b865c00d7e9 199 GPIOC->ODR |= (1 << 5); // Turn on status LED
benkatz 25:f5741040c4bb 200 order_phases(&spi, &gpio, &controller, &prefs); // Check phase ordering
benkatz 25:f5741040c4bb 201 calibrate(&spi, &gpio, &controller, &prefs); // Perform calibration procedure
benkatz 26:2b865c00d7e9 202 GPIOC->ODR &= !(1 << 5); // Turn off status LED
benkatz 23:2adf23ee0305 203 wait(.2);
benkatz 25:f5741040c4bb 204 gpio.enable->write(0); // Turn off gate drive
benkatz 23:2adf23ee0305 205 printf("\n\r Calibration complete. Press 'esc' to return to menu\n\r");
benkatz 23:2adf23ee0305 206 state_change = 0;
benkatz 23:2adf23ee0305 207
benkatz 23:2adf23ee0305 208 }
benkatz 23:2adf23ee0305 209
benkatz 23:2adf23ee0305 210 void print_encoder(void){
benkatz 23:2adf23ee0305 211 spi.Sample();
benkatz 23:2adf23ee0305 212 wait(.001);
benkatz 23:2adf23ee0305 213 printf(" Mechanical Angle: %f Electrical Angle: %f Raw: %d\n\r", spi.GetMechPosition(), spi.GetElecPosition(), spi.GetRawPosition());
benkatz 23:2adf23ee0305 214 wait(.05);
benkatz 22:60276ba87ac6 215 }
benkatz 20:bf9ea5125d52 216
benkatz 23:2adf23ee0305 217 /// Current Sampling Interrupt ///
benkatz 23:2adf23ee0305 218 /// This runs at 40 kHz, regardless of of the mode the controller is in ///
benkatz 2:8724412ad628 219 extern "C" void TIM1_UP_TIM10_IRQHandler(void) {
benkatz 2:8724412ad628 220 if (TIM1->SR & TIM_SR_UIF ) {
benkatz 20:bf9ea5125d52 221 //toggle = 1;
benkatz 23:2adf23ee0305 222
benkatz 23:2adf23ee0305 223 ///Sample current always ///
benkatz 25:f5741040c4bb 224 ADC1->CR2 |= 0x40000000; // Begin sample and conversion
benkatz 22:60276ba87ac6 225 //volatile int delay;
benkatz 20:bf9ea5125d52 226 //for (delay = 0; delay < 55; delay++);
benkatz 25:f5741040c4bb 227 controller.adc2_raw = ADC2->DR; // Read ADC1 and ADC2 Data Registers
benkatz 23:2adf23ee0305 228 controller.adc1_raw = ADC1->DR;
benkatz 23:2adf23ee0305 229 ///
benkatz 20:bf9ea5125d52 230
benkatz 23:2adf23ee0305 231 /// Check state machine state, and run the appropriate function ///
benkatz 23:2adf23ee0305 232 //printf("%d\n\r", state);
benkatz 23:2adf23ee0305 233 switch(state){
benkatz 23:2adf23ee0305 234 case REST_MODE: // Do nothing until
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 23:2adf23ee0305 252 count++;
benkatz 28:8c7e29f719c5 253 //toggle.write(1);
benkatz 26:2b865c00d7e9 254 controller.theta_elec = spi.GetElecPosition();
benkatz 28:8c7e29f719c5 255 controller.theta_mech = (1.0f/GR)*spi.GetMechPosition();
benkatz 28:8c7e29f719c5 256 controller.dtheta_mech = (1.0f/GR)*spi.GetMechVelocity();
benkatz 26:2b865c00d7e9 257 //TIM1->CCR3 = 0x708*(1.0f);
benkatz 26:2b865c00d7e9 258 //TIM1->CCR1 = 0x708*(1.0f);
benkatz 26:2b865c00d7e9 259 //TIM1->CCR2 = 0x708*(1.0f);
benkatz 26:2b865c00d7e9 260
benkatz 26:2b865c00d7e9 261 //controller.i_q_ref = controller.t_ff/KT_OUT;
benkatz 28:8c7e29f719c5 262
benkatz 28:8c7e29f719c5 263 torque_control(&controller);
benkatz 28:8c7e29f719c5 264 if((controller.timeout > CAN_TIMEOUT) && (CAN_TIMEOUT > 0)){
benkatz 28:8c7e29f719c5 265 controller.i_d_ref = 0;
benkatz 28:8c7e29f719c5 266 controller.i_q_ref = 0;
benkatz 28:8c7e29f719c5 267 }
benkatz 34:51647c6c500d 268 //controller.i_q_ref = .4f;
benkatz 23:2adf23ee0305 269 commutate(&controller, &gpio, controller.theta_elec); // Run current loop
benkatz 23:2adf23ee0305 270 spi.Sample(); // Sample position sensor
benkatz 28:8c7e29f719c5 271 //toggle.write(0);
benkatz 28:8c7e29f719c5 272 controller.timeout += 1;
benkatz 26:2b865c00d7e9 273
benkatz 34:51647c6c500d 274 if(count == 1000){
benkatz 32:ccac5da77844 275 count = 0;
benkatz 32:ccac5da77844 276 //wait(.001);
benkatz 34:51647c6c500d 277 //printf("%f\n\r", controller.theta_elec);
benkatz 23:2adf23ee0305 278 }
benkatz 28:8c7e29f719c5 279 }
benkatz 28:8c7e29f719c5 280
benkatz 23:2adf23ee0305 281 break;
benkatz 23:2adf23ee0305 282 case SETUP_MODE:
benkatz 23:2adf23ee0305 283 if(state_change){
benkatz 24:58c2d7571207 284 enter_setup_state();
benkatz 23:2adf23ee0305 285 }
benkatz 23:2adf23ee0305 286 break;
benkatz 23:2adf23ee0305 287 case ENCODER_MODE:
benkatz 23:2adf23ee0305 288 print_encoder();
benkatz 23:2adf23ee0305 289 break;
benkatz 23:2adf23ee0305 290 }
benkatz 26:2b865c00d7e9 291
benkatz 2:8724412ad628 292 }
benkatz 23:2adf23ee0305 293 TIM1->SR = 0x0; // reset the status register
benkatz 2:8724412ad628 294 }
benkatz 0:4e1c4df6aabd 295
benkatz 25:f5741040c4bb 296
benkatz 24:58c2d7571207 297 char cmd_val[8] = {0};
benkatz 24:58c2d7571207 298 char cmd_id = 0;
benkatz 25:f5741040c4bb 299 char char_count = 0;
benkatz 24:58c2d7571207 300
benkatz 25:f5741040c4bb 301 /// Manage state machine with commands from serial terminal or configurator gui ///
benkatz 25:f5741040c4bb 302 /// Called when data received over serial ///
benkatz 23:2adf23ee0305 303 void serial_interrupt(void){
benkatz 23:2adf23ee0305 304 while(pc.readable()){
benkatz 23:2adf23ee0305 305 char c = pc.getc();
benkatz 25:f5741040c4bb 306 if(c == 27){
benkatz 25:f5741040c4bb 307 state = REST_MODE;
benkatz 25:f5741040c4bb 308 state_change = 1;
benkatz 25:f5741040c4bb 309 char_count = 0;
benkatz 25:f5741040c4bb 310 cmd_id = 0;
benkatz 26:2b865c00d7e9 311 GPIOC->ODR &= !(1 << 5);
benkatz 25:f5741040c4bb 312 for(int i = 0; i<8; i++){cmd_val[i] = 0;}
benkatz 25:f5741040c4bb 313 }
benkatz 24:58c2d7571207 314 if(state == REST_MODE){
benkatz 23:2adf23ee0305 315 switch (c){
benkatz 23:2adf23ee0305 316 case 'c':
benkatz 23:2adf23ee0305 317 state = CALIBRATION_MODE;
benkatz 23:2adf23ee0305 318 state_change = 1;
benkatz 23:2adf23ee0305 319 break;
benkatz 26:2b865c00d7e9 320 case 'm':
benkatz 26:2b865c00d7e9 321 state = MOTOR_MODE;
benkatz 23:2adf23ee0305 322 state_change = 1;
benkatz 23:2adf23ee0305 323 break;
benkatz 23:2adf23ee0305 324 case 'e':
benkatz 23:2adf23ee0305 325 state = ENCODER_MODE;
benkatz 23:2adf23ee0305 326 state_change = 1;
benkatz 23:2adf23ee0305 327 break;
benkatz 23:2adf23ee0305 328 case 's':
benkatz 23:2adf23ee0305 329 state = SETUP_MODE;
benkatz 23:2adf23ee0305 330 state_change = 1;
benkatz 23:2adf23ee0305 331 break;
benkatz 24:58c2d7571207 332 }
benkatz 24:58c2d7571207 333 }
benkatz 24:58c2d7571207 334 else if(state == SETUP_MODE){
benkatz 25:f5741040c4bb 335 if(c == 13){
benkatz 24:58c2d7571207 336 switch (cmd_id){
benkatz 24:58c2d7571207 337 case 'b':
benkatz 24:58c2d7571207 338 I_BW = fmaxf(fminf(atof(cmd_val), 2000.0f), 100.0f);
benkatz 24:58c2d7571207 339 break;
benkatz 24:58c2d7571207 340 case 'i':
benkatz 24:58c2d7571207 341 CAN_ID = atoi(cmd_val);
benkatz 24:58c2d7571207 342 break;
benkatz 26:2b865c00d7e9 343 case 'm':
benkatz 26:2b865c00d7e9 344 CAN_MASTER = atoi(cmd_val);
benkatz 26:2b865c00d7e9 345 break;
benkatz 24:58c2d7571207 346 case 'l':
benkatz 24:58c2d7571207 347 TORQUE_LIMIT = fmaxf(fminf(atof(cmd_val), 18.0f), 0.0f);
benkatz 24:58c2d7571207 348 break;
benkatz 28:8c7e29f719c5 349 case 't':
benkatz 28:8c7e29f719c5 350 CAN_TIMEOUT = atoi(cmd_val);
benkatz 28:8c7e29f719c5 351 break;
benkatz 24:58c2d7571207 352 default:
benkatz 24:58c2d7571207 353 printf("\n\r '%c' Not a valid command prefix\n\r\n\r", cmd_id);
benkatz 24:58c2d7571207 354 break;
benkatz 24:58c2d7571207 355 }
benkatz 24:58c2d7571207 356
benkatz 24:58c2d7571207 357 if (!prefs.ready()) prefs.open();
benkatz 24:58c2d7571207 358 prefs.flush(); // Write new prefs to flash
benkatz 24:58c2d7571207 359 prefs.close();
benkatz 24:58c2d7571207 360 prefs.load();
benkatz 24:58c2d7571207 361 state_change = 1;
benkatz 24:58c2d7571207 362 char_count = 0;
benkatz 24:58c2d7571207 363 cmd_id = 0;
benkatz 24:58c2d7571207 364 for(int i = 0; i<8; i++){cmd_val[i] = 0;}
benkatz 24:58c2d7571207 365 }
benkatz 24:58c2d7571207 366 else{
benkatz 24:58c2d7571207 367 if(char_count == 0){cmd_id = c;}
benkatz 24:58c2d7571207 368 else{
benkatz 24:58c2d7571207 369 cmd_val[char_count-1] = c;
benkatz 24:58c2d7571207 370
benkatz 24:58c2d7571207 371 }
benkatz 24:58c2d7571207 372 pc.putc(c);
benkatz 24:58c2d7571207 373 char_count++;
benkatz 23:2adf23ee0305 374 }
benkatz 23:2adf23ee0305 375 }
benkatz 24:58c2d7571207 376 else if (state == ENCODER_MODE){
benkatz 24:58c2d7571207 377 switch (c){
benkatz 24:58c2d7571207 378 case 27:
benkatz 24:58c2d7571207 379 state = REST_MODE;
benkatz 24:58c2d7571207 380 state_change = 1;
benkatz 24:58c2d7571207 381 break;
benkatz 24:58c2d7571207 382 }
benkatz 24:58c2d7571207 383 }
benkatz 24:58c2d7571207 384
benkatz 24:58c2d7571207 385 }
benkatz 22:60276ba87ac6 386 }
benkatz 0:4e1c4df6aabd 387
benkatz 0:4e1c4df6aabd 388 int main() {
benkatz 23:2adf23ee0305 389
benkatz 20:bf9ea5125d52 390 controller.v_bus = V_BUS;
benkatz 22:60276ba87ac6 391 controller.mode = 0;
benkatz 23:2adf23ee0305 392 Init_All_HW(&gpio); // Setup PWM, ADC, GPIO
benkatz 20:bf9ea5125d52 393
benkatz 9:d7eb815cb057 394 wait(.1);
benkatz 26:2b865c00d7e9 395 gpio.enable->write(1);
benkatz 26:2b865c00d7e9 396 TIM1->CCR3 = 0x708*(1.0f); // Write duty cycles
benkatz 26:2b865c00d7e9 397 TIM1->CCR2 = 0x708*(1.0f);
benkatz 26:2b865c00d7e9 398 TIM1->CCR1 = 0x708*(1.0f);
benkatz 23:2adf23ee0305 399 zero_current(&controller.adc1_offset, &controller.adc2_offset); // Measure current sensor zero-offset
benkatz 26:2b865c00d7e9 400 gpio.enable->write(0);
benkatz 23:2adf23ee0305 401 reset_foc(&controller); // Reset current controller
benkatz 26:2b865c00d7e9 402 TIM1->CR1 ^= TIM_CR1_UDIS;
benkatz 26:2b865c00d7e9 403 //TIM1->CR1 |= TIM_CR1_UDIS; //enable interrupt
benkatz 20:bf9ea5125d52 404
benkatz 20:bf9ea5125d52 405 wait(.1);
benkatz 23:2adf23ee0305 406 NVIC_SetPriority(TIM5_IRQn, 2); // set interrupt priority
benkatz 22:60276ba87ac6 407
benkatz 26:2b865c00d7e9 408
benkatz 23:2adf23ee0305 409 can.frequency(1000000); // set bit rate to 1Mbps
benkatz 26:2b865c00d7e9 410 can.filter(CAN_ID<<21, 0xFFE00004, CANStandard, 0);
benkatz 26:2b865c00d7e9 411 //can.filter(CAN_ID, 0xF, CANStandard, 0);
benkatz 26:2b865c00d7e9 412 can.attach(&onMsgReceived); // attach 'CAN receive-complete' interrupt handler
benkatz 28:8c7e29f719c5 413 txMsg.id = CAN_MASTER;
benkatz 28:8c7e29f719c5 414 txMsg.len = 6;
benkatz 26:2b865c00d7e9 415 rxMsg.len = 8;
benkatz 23:2adf23ee0305 416
benkatz 25:f5741040c4bb 417 prefs.load(); // Read flash
benkatz 25:f5741040c4bb 418 spi.SetElecOffset(E_OFFSET); // Set position sensor offset
benkatz 23:2adf23ee0305 419 int lut[128] = {0};
benkatz 23:2adf23ee0305 420 memcpy(&lut, &ENCODER_LUT, sizeof(lut));
benkatz 25:f5741040c4bb 421 spi.WriteLUT(lut); // Set potision sensor nonlinearity lookup table
benkatz 23:2adf23ee0305 422
benkatz 26:2b865c00d7e9 423 pc.baud(921600); // set serial baud rate
benkatz 20:bf9ea5125d52 424 wait(.01);
benkatz 23:2adf23ee0305 425 pc.printf("\n\r\n\r HobbyKing Cheetah\n\r\n\r");
benkatz 20:bf9ea5125d52 426 wait(.01);
benkatz 23:2adf23ee0305 427 printf("\n\r Debug Info:\n\r");
benkatz 32:ccac5da77844 428 printf(" Firmware Version: %s\n\r", VERSION_NUM);
benkatz 23:2adf23ee0305 429 printf(" ADC1 Offset: %d ADC2 Offset: %d\n\r", controller.adc1_offset, controller.adc2_offset);
benkatz 23:2adf23ee0305 430 printf(" Position Sensor Electrical Offset: %.4f\n\r", E_OFFSET);
benkatz 24:58c2d7571207 431 printf(" CAN ID: %d\n\r", CAN_ID);
benkatz 23:2adf23ee0305 432
benkatz 23:2adf23ee0305 433 pc.attach(&serial_interrupt); // attach serial interrupt
benkatz 22:60276ba87ac6 434
benkatz 23:2adf23ee0305 435 state_change = 1;
benkatz 20:bf9ea5125d52 436
benkatz 22:60276ba87ac6 437
benkatz 0:4e1c4df6aabd 438 while(1) {
benkatz 11:c83b18d41e54 439
benkatz 0:4e1c4df6aabd 440 }
benkatz 0:4e1c4df6aabd 441 }