Fork and fix for mwork

Dependencies:   mbed-dev-f303 FastPWM3 millis

Committer:
annhandt09
Date:
Fri Jun 26 01:26:53 2020 +0000
Revision:
55:fee62d8fd8fb
Parent:
54:3e056b097c52
Child:
56:eaf75bcad361
change serial printf

Who changed what in which revision?

UserRevisionLine numberNew contents of line
benkatz 22:60276ba87ac6 1 /// high-bandwidth 3-phase motor control, for robots
benkatz 45:26801179208e 2 /// Written by benkatz, with much inspiration from Bayley Wang, Nick Kirkby, Shane Colton, David Otten, and others
benkatz 22:60276ba87ac6 3 /// Hardware documentation can be found at build-its.blogspot.com
benkatz 22:60276ba87ac6 4 /// Written for the STM32F446, but can be implemented on other STM32 MCU's with some further register-diddling
benkatz 47:e1196a851f76 5 /// Version for the TI DRV8323 Everything Chip
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
bdring 54:3e056b097c52 13 #define VERSION_NUM "2.0.0"
benkatz 26:2b865c00d7e9 14
bdring 54:3e056b097c52 15 // this sets up the 2 IO modes of the UART connector (UART and Step/Dir)
annhandt09 55:fee62d8fd8fb 16
bdring 54:3e056b097c52 17 #define UART_TX PA_2 // define the pins on the connector
bdring 54:3e056b097c52 18 #define UART_RX PA_3 // define the pins on the connector
bdring 54:3e056b097c52 19 #define UART_BAUD 230400
annhandt09 55:fee62d8fd8fb 20 // Ints stored in flash. Includes position sensor calibration lookup table
benkatz 17:3c5df2982199 21
benkatz 0:4e1c4df6aabd 22 #include "mbed.h"
benkatz 0:4e1c4df6aabd 23 #include "PositionSensor.h"
benkatz 20:bf9ea5125d52 24 #include "structs.h"
benkatz 20:bf9ea5125d52 25 #include "foc.h"
benkatz 22:60276ba87ac6 26 #include "calibration.h"
benkatz 20:bf9ea5125d52 27 #include "hw_setup.h"
benkatz 23:2adf23ee0305 28 #include "math_ops.h"
benkatz 20:bf9ea5125d52 29 #include "current_controller_config.h"
benkatz 20:bf9ea5125d52 30 #include "hw_config.h"
benkatz 20:bf9ea5125d52 31 #include "motor_config.h"
benkatz 23:2adf23ee0305 32 #include "stm32f4xx_flash.h"
benkatz 23:2adf23ee0305 33 #include "FlashWriter.h"
benkatz 23:2adf23ee0305 34 #include "user_config.h"
benkatz 23:2adf23ee0305 35 #include "PreferenceWriter.h"
benkatz 42:738fa01b0346 36 #include "CAN_com.h"
benkatz 44:8040fa2fcb0d 37 #include "DRV.h"
annhandt09 55:fee62d8fd8fb 38 #include "mode.h"
annhandt09 55:fee62d8fd8fb 39
annhandt09 55:fee62d8fd8fb 40 int io_mode = IO_MODE_NONE; // the default mode is serial
annhandt09 55:fee62d8fd8fb 41
annhandt09 55:fee62d8fd8fb 42 float __float_reg[64]; // Floats stored in flash
annhandt09 55:fee62d8fd8fb 43 int __int_reg[256];
benkatz 23:2adf23ee0305 44 PreferenceWriter prefs(6);
benkatz 9:d7eb815cb057 45
benkatz 20:bf9ea5125d52 46 GPIOStruct gpio;
benkatz 20:bf9ea5125d52 47 ControllerStruct controller;
benkatz 48:74a40481740c 48 ObserverStruct observer;
benkatz 20:bf9ea5125d52 49 COMStruct com;
bdring 54:3e056b097c52 50
bdring 54:3e056b097c52 51
bdring 54:3e056b097c52 52 Serial *pc = NULL;
bdring 54:3e056b097c52 53 InterruptIn *step = NULL;
bdring 54:3e056b097c52 54 DigitalIn *dir = NULL;
benkatz 9:d7eb815cb057 55
benkatz 17:3c5df2982199 56
benkatz 45:26801179208e 57 CAN can(PB_8, PB_9, 1000000); // 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 44:8040fa2fcb0d 61 SPI drv_spi(PA_7, PA_6, PA_5);
benkatz 44:8040fa2fcb0d 62 DigitalOut drv_cs(PA_4);
benkatz 44:8040fa2fcb0d 63 DRV832x drv(&drv_spi, &drv_cs);
benkatz 8:10ae7bc88d6e 64
benkatz 26:2b865c00d7e9 65 PositionSensorAM5147 spi(16384, 0.0, NPP);
benkatz 20:bf9ea5125d52 66
benkatz 23:2adf23ee0305 67 volatile int count = 0;
benkatz 23:2adf23ee0305 68 volatile int state = REST_MODE;
benkatz 23:2adf23ee0305 69 volatile int state_change;
benkatz 20:bf9ea5125d52 70
bdring 54:3e056b097c52 71
bdring 54:3e056b097c52 72 void serial_interrupt(void);
bdring 54:3e056b097c52 73 void stepInt();
bdring 54:3e056b097c52 74 void cond_printf(const char *format, ...);
bdring 54:3e056b097c52 75
bdring 54:3e056b097c52 76 // set the current mode of the UART connector
bdring 54:3e056b097c52 77 void set_io_mode(int new_io_mode) {
bdring 54:3e056b097c52 78 if (new_io_mode == IO_MODE_SERIAL) {
bdring 54:3e056b097c52 79 cond_printf("UART Mode\n\r");
bdring 54:3e056b097c52 80 io_mode = IO_MODE_SERIAL;
bdring 54:3e056b097c52 81 if (step != NULL)
bdring 54:3e056b097c52 82 step->rise(NULL);
bdring 54:3e056b097c52 83 delete step;
bdring 54:3e056b097c52 84 delete dir;
bdring 54:3e056b097c52 85 pc = new Serial(UART_TX, UART_RX);
bdring 54:3e056b097c52 86 pc->baud(UART_BAUD);
bdring 54:3e056b097c52 87 pc->attach(&serial_interrupt);
bdring 54:3e056b097c52 88 wait(100);
bdring 54:3e056b097c52 89
bdring 54:3e056b097c52 90 }
bdring 54:3e056b097c52 91 else if (new_io_mode == IO_MODE_STEP_DIR){
bdring 54:3e056b097c52 92 cond_printf("STEP/DIR Mode\n\r");
bdring 54:3e056b097c52 93 wait(1000);
bdring 54:3e056b097c52 94
bdring 54:3e056b097c52 95 io_mode = IO_MODE_STEP_DIR;
bdring 54:3e056b097c52 96
bdring 54:3e056b097c52 97 if (pc != NULL) {
bdring 54:3e056b097c52 98 pc->attach(NULL);
bdring 54:3e056b097c52 99 }
bdring 54:3e056b097c52 100 delete pc;
bdring 54:3e056b097c52 101
bdring 54:3e056b097c52 102 step = new InterruptIn(UART_RX);
bdring 54:3e056b097c52 103 step->rise(&stepInt);
bdring 54:3e056b097c52 104 dir = new DigitalIn(UART_TX);
bdring 54:3e056b097c52 105 }
bdring 54:3e056b097c52 106
bdring 54:3e056b097c52 107
bdring 54:3e056b097c52 108 }
bdring 54:3e056b097c52 109
annhandt09 55:fee62d8fd8fb 110 /*
bdring 54:3e056b097c52 111 // Checks to see if in Serial mode before printing
bdring 54:3e056b097c52 112 void cond_printf(const char *format, ...)
bdring 54:3e056b097c52 113 {
bdring 54:3e056b097c52 114 if (io_mode != IO_MODE_SERIAL) {
bdring 54:3e056b097c52 115 return;
bdring 54:3e056b097c52 116 }
bdring 54:3e056b097c52 117 char loc_buf[64];
bdring 54:3e056b097c52 118 char * temp = loc_buf;
bdring 54:3e056b097c52 119 va_list arg;
bdring 54:3e056b097c52 120 va_list copy;
bdring 54:3e056b097c52 121 va_start(arg, format);
bdring 54:3e056b097c52 122 va_copy(copy, arg);
bdring 54:3e056b097c52 123 size_t len = vsnprintf(NULL, 0, format, arg);
bdring 54:3e056b097c52 124 va_end(copy);
bdring 54:3e056b097c52 125 if(len >= sizeof(loc_buf)){
bdring 54:3e056b097c52 126 temp = new char[len+1];
bdring 54:3e056b097c52 127 if(temp == NULL) {
bdring 54:3e056b097c52 128 return;
bdring 54:3e056b097c52 129 }
bdring 54:3e056b097c52 130 }
bdring 54:3e056b097c52 131 len = vsnprintf(temp, len+1, format, arg);
bdring 54:3e056b097c52 132 pc->printf( temp);
bdring 54:3e056b097c52 133 va_end(arg);
bdring 54:3e056b097c52 134 if(len > 64){
bdring 54:3e056b097c52 135 delete[] temp;
bdring 54:3e056b097c52 136 }
bdring 54:3e056b097c52 137 }
bdring 54:3e056b097c52 138
annhandt09 55:fee62d8fd8fb 139 */
annhandt09 55:fee62d8fd8fb 140
bdring 54:3e056b097c52 141 // Interupt function for receiving step signal
bdring 54:3e056b097c52 142 void stepInt() {
bdring 54:3e056b097c52 143 if (dir) {
bdring 54:3e056b097c52 144 controller.p_des += RADS_PER_STEP;
bdring 54:3e056b097c52 145 } else {
bdring 54:3e056b097c52 146 controller.p_des -= RADS_PER_STEP;
bdring 54:3e056b097c52 147 }
bdring 54:3e056b097c52 148 }
bdring 54:3e056b097c52 149
bdring 54:3e056b097c52 150 // CAN message received
benkatz 26:2b865c00d7e9 151 void onMsgReceived() {
annhandt09 55:fee62d8fd8fb 152 //msgAvailable = true; //0A FF FF FF FF FF
bdring 54:3e056b097c52 153 //cond_printf("%df\n\r", rxMsg.id);
benkatz 26:2b865c00d7e9 154 can.read(rxMsg);
benkatz 28:8c7e29f719c5 155 if((rxMsg.id == CAN_ID)){
benkatz 28:8c7e29f719c5 156 controller.timeout = 0;
benkatz 28:8c7e29f719c5 157 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 158 state = MOTOR_MODE;
benkatz 28:8c7e29f719c5 159 state_change = 1;
benkatz 28:8c7e29f719c5 160 }
benkatz 28:8c7e29f719c5 161 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 162 state = REST_MODE;
benkatz 28:8c7e29f719c5 163 state_change = 1;
benkatz 37:c0f352d6e8e3 164 gpio.led->write(0);;
benkatz 28:8c7e29f719c5 165 }
benkatz 28:8c7e29f719c5 166 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 167 spi.ZeroPosition();
benkatz 28:8c7e29f719c5 168 }
bdring 54:3e056b097c52 169 // new commands ....
bdring 54:3e056b097c52 170 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]==0xFB))){
bdring 54:3e056b097c52 171 io_mode = IO_MODE_STEP_DIR;
bdring 54:3e056b097c52 172 //if (pc != NULL) {
bdring 54:3e056b097c52 173 pc->attach(NULL);
bdring 54:3e056b097c52 174 //}
bdring 54:3e056b097c52 175 step = new InterruptIn(UART_RX);
bdring 54:3e056b097c52 176 step->rise(&stepInt);
bdring 54:3e056b097c52 177 dir = new DigitalIn(UART_TX);
bdring 54:3e056b097c52 178 }
bdring 54:3e056b097c52 179 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]==0xFA))){
bdring 54:3e056b097c52 180 io_mode = IO_MODE_SERIAL;
bdring 54:3e056b097c52 181 if (step != NULL)
bdring 54:3e056b097c52 182 step->rise(NULL);
bdring 54:3e056b097c52 183 delete step;
bdring 54:3e056b097c52 184 delete dir;
bdring 54:3e056b097c52 185 pc = new Serial(UART_TX, UART_RX);
bdring 54:3e056b097c52 186 pc->baud(UART_BAUD);
bdring 54:3e056b097c52 187 pc->attach(&serial_interrupt);
bdring 54:3e056b097c52 188 wait(100);
bdring 54:3e056b097c52 189 }
benkatz 28:8c7e29f719c5 190 else if(state == MOTOR_MODE){
benkatz 28:8c7e29f719c5 191 unpack_cmd(rxMsg, &controller);
annhandt09 55:fee62d8fd8fb 192 }
annhandt09 55:fee62d8fd8fb 193 cond_printf("CAN BUS: %f ,%f, %f\n\r", rxMsg.data[0], rxMsg.data[1], rxMsg.data[2]);
benkatz 37:c0f352d6e8e3 194 pack_reply(&txMsg, controller.theta_mech, controller.dtheta_mech, controller.i_q_filt*KT_OUT);
benkatz 37:c0f352d6e8e3 195 can.write(txMsg);
bdring 54:3e056b097c52 196 }
benkatz 26:2b865c00d7e9 197 }
benkatz 26:2b865c00d7e9 198
benkatz 23:2adf23ee0305 199 void enter_menu_state(void){
benkatz 44:8040fa2fcb0d 200 drv.disable_gd();
benkatz 47:e1196a851f76 201 //gpio.enable->write(0);
bdring 54:3e056b097c52 202 cond_printf("\n\r\n\r\n\r");
bdring 54:3e056b097c52 203 cond_printf(" Commands:\n\r");
benkatz 44:8040fa2fcb0d 204 wait_us(10);
bdring 54:3e056b097c52 205 cond_printf(" m - Motor Mode\n\r");
benkatz 44:8040fa2fcb0d 206 wait_us(10);
bdring 54:3e056b097c52 207 cond_printf(" c - Calibrate Encoder\n\r");
benkatz 44:8040fa2fcb0d 208 wait_us(10);
bdring 54:3e056b097c52 209 cond_printf(" s - Setup\n\r");
benkatz 44:8040fa2fcb0d 210 wait_us(10);
bdring 54:3e056b097c52 211 cond_printf(" e - Display Encoder\n\r");
benkatz 44:8040fa2fcb0d 212 wait_us(10);
bdring 54:3e056b097c52 213 cond_printf(" z - Set Zero Position\n\r");
benkatz 44:8040fa2fcb0d 214 wait_us(10);
bdring 54:3e056b097c52 215 cond_printf(" f - Move Forward\n\r");
bdring 53:349304b6d937 216 wait_us(10);
bdring 54:3e056b097c52 217 cond_printf(" b - Move Back\n\r");
bdring 53:349304b6d937 218 wait_us(10);
bdring 54:3e056b097c52 219 cond_printf(" p - current posiiton\n\r");
bdring 52:cf8b2abf811d 220 wait_us(10);
bdring 54:3e056b097c52 221 cond_printf(" esc - Exit to Menu\n\r");
benkatz 44:8040fa2fcb0d 222 wait_us(10);
benkatz 23:2adf23ee0305 223 state_change = 0;
benkatz 37:c0f352d6e8e3 224 gpio.led->write(0);
benkatz 23:2adf23ee0305 225 }
benkatz 24:58c2d7571207 226
benkatz 24:58c2d7571207 227 void enter_setup_state(void){
bdring 54:3e056b097c52 228 cond_printf("\n\r\n\r Configuration Options \n\r\n\n");
benkatz 44:8040fa2fcb0d 229 wait_us(10);
bdring 54:3e056b097c52 230 cond_printf(" %-4s %-31s %-5s %-6s %-2s\n\r\n\r", "prefix", "parameter", "min", "max", "current value");
benkatz 44:8040fa2fcb0d 231 wait_us(10);
bdring 54:3e056b097c52 232 cond_printf(" %-4s %-31s %-5s %-6s %.1f\n\r", "b", "Current Bandwidth (Hz)", "100", "2000", I_BW);
benkatz 44:8040fa2fcb0d 233 wait_us(10);
bdring 54:3e056b097c52 234 cond_printf(" %-4s %-31s %-5s %-6s %-5i\n\r", "i", "CAN ID", "0", "127", CAN_ID);
benkatz 44:8040fa2fcb0d 235 wait_us(10);
bdring 54:3e056b097c52 236 cond_printf(" %-4s %-31s %-5s %-6s %-5i\n\r", "m", "CAN Master ID", "0", "127", CAN_MASTER);
benkatz 44:8040fa2fcb0d 237 wait_us(10);
bdring 54:3e056b097c52 238 cond_printf(" %-4s %-31s %-5s %-6s %.1f\n\r", "l", "Current Limit (A)", "0.0", "40.0", I_MAX);
benkatz 51:6cd89bd6fcaa 239 wait_us(10);
bdring 54:3e056b097c52 240 cond_printf(" %-4s %-31s %-5s %-6s %.1f\n\r", "f", "FW Current Limit (A)", "0.0", "33.0", I_FW_MAX);
benkatz 44:8040fa2fcb0d 241 wait_us(10);
bdring 54:3e056b097c52 242 cond_printf(" %-4s %-31s %-5s %-6s %d\n\r", "t", "CAN Timeout (cycles)(0 = none)", "0", "100000", CAN_TIMEOUT);
benkatz 44:8040fa2fcb0d 243 wait_us(10);
bdring 54:3e056b097c52 244 cond_printf("\n\r To change a value, type 'prefix''value''ENTER'\n\r i.e. 'b1000''ENTER'\n\r\n\r");
benkatz 44:8040fa2fcb0d 245 wait_us(10);
benkatz 24:58c2d7571207 246 state_change = 0;
benkatz 24:58c2d7571207 247 }
benkatz 22:60276ba87ac6 248
benkatz 23:2adf23ee0305 249 void enter_torque_mode(void){
benkatz 44:8040fa2fcb0d 250 drv.enable_gd();
benkatz 47:e1196a851f76 251 //gpio.enable->write(1);
benkatz 37:c0f352d6e8e3 252 controller.ovp_flag = 0;
benkatz 28:8c7e29f719c5 253 reset_foc(&controller); // Tesets integrators, and other control loop parameters
benkatz 28:8c7e29f719c5 254 wait(.001);
benkatz 23:2adf23ee0305 255 controller.i_d_ref = 0;
benkatz 50:ba72df25d10f 256 controller.i_q_ref = 0; // Current Setpoints
benkatz 37:c0f352d6e8e3 257 gpio.led->write(1); // Turn on status LED
benkatz 25:f5741040c4bb 258 state_change = 0;
bdring 54:3e056b097c52 259 cond_printf("\n\r Entering Motor Mode \n\r");
benkatz 23:2adf23ee0305 260 }
benkatz 22:60276ba87ac6 261
benkatz 23:2adf23ee0305 262 void calibrate(void){
benkatz 44:8040fa2fcb0d 263 drv.enable_gd();
benkatz 47:e1196a851f76 264 //gpio.enable->write(1);
benkatz 37:c0f352d6e8e3 265 gpio.led->write(1); // Turn on status LED
benkatz 25:f5741040c4bb 266 order_phases(&spi, &gpio, &controller, &prefs); // Check phase ordering
benkatz 25:f5741040c4bb 267 calibrate(&spi, &gpio, &controller, &prefs); // Perform calibration procedure
benkatz 37:c0f352d6e8e3 268 gpio.led->write(0);; // Turn off status LED
benkatz 23:2adf23ee0305 269 wait(.2);
bdring 54:3e056b097c52 270 cond_printf("\n\r Calibration complete. Press 'esc' to return to menu\n\r");
benkatz 44:8040fa2fcb0d 271 drv.disable_gd();
benkatz 47:e1196a851f76 272 //gpio.enable->write(0);
benkatz 23:2adf23ee0305 273 state_change = 0;
benkatz 23:2adf23ee0305 274 }
benkatz 23:2adf23ee0305 275
benkatz 23:2adf23ee0305 276 void print_encoder(void){
bdring 54:3e056b097c52 277 cond_printf(" Mechanical Angle: %f Electrical Angle: %f Raw: %d\n\r", spi.GetMechPosition(), spi.GetElecPosition(), spi.GetRawPosition());
bdring 54:3e056b097c52 278 //cond_printf("%d\n\r", spi.GetRawPosition());
benkatz 47:e1196a851f76 279 wait(.001);
benkatz 22:60276ba87ac6 280 }
benkatz 20:bf9ea5125d52 281
benkatz 23:2adf23ee0305 282 /// Current Sampling Interrupt ///
benkatz 23:2adf23ee0305 283 /// This runs at 40 kHz, regardless of of the mode the controller is in ///
benkatz 2:8724412ad628 284 extern "C" void TIM1_UP_TIM10_IRQHandler(void) {
benkatz 2:8724412ad628 285 if (TIM1->SR & TIM_SR_UIF ) {
benkatz 23:2adf23ee0305 286
benkatz 23:2adf23ee0305 287 ///Sample current always ///
benkatz 25:f5741040c4bb 288 ADC1->CR2 |= 0x40000000; // Begin sample and conversion
benkatz 22:60276ba87ac6 289 //volatile int delay;
benkatz 20:bf9ea5125d52 290 //for (delay = 0; delay < 55; delay++);
benkatz 45:26801179208e 291
benkatz 47:e1196a851f76 292 spi.Sample(DT); // sample position sensor
benkatz 37:c0f352d6e8e3 293 controller.adc2_raw = ADC2->DR; // Read ADC Data Registers
benkatz 23:2adf23ee0305 294 controller.adc1_raw = ADC1->DR;
benkatz 37:c0f352d6e8e3 295 controller.adc3_raw = ADC3->DR;
benkatz 37:c0f352d6e8e3 296 controller.theta_elec = spi.GetElecPosition();
benkatz 37:c0f352d6e8e3 297 controller.theta_mech = (1.0f/GR)*spi.GetMechPosition();
benkatz 37:c0f352d6e8e3 298 controller.dtheta_mech = (1.0f/GR)*spi.GetMechVelocity();
benkatz 37:c0f352d6e8e3 299 controller.dtheta_elec = spi.GetElecVelocity();
benkatz 51:6cd89bd6fcaa 300 controller.v_bus = 0.95f*controller.v_bus + 0.05f*((float)controller.adc3_raw)*V_SCALE; //filter the dc link voltage measurement
benkatz 23:2adf23ee0305 301 ///
benkatz 20:bf9ea5125d52 302
benkatz 23:2adf23ee0305 303 /// Check state machine state, and run the appropriate function ///
benkatz 23:2adf23ee0305 304 switch(state){
benkatz 37:c0f352d6e8e3 305 case REST_MODE: // Do nothing
benkatz 23:2adf23ee0305 306 if(state_change){
benkatz 23:2adf23ee0305 307 enter_menu_state();
benkatz 23:2adf23ee0305 308 }
benkatz 23:2adf23ee0305 309 break;
benkatz 22:60276ba87ac6 310
benkatz 23:2adf23ee0305 311 case CALIBRATION_MODE: // Run encoder calibration procedure
benkatz 23:2adf23ee0305 312 if(state_change){
benkatz 23:2adf23ee0305 313 calibrate();
benkatz 23:2adf23ee0305 314 }
benkatz 23:2adf23ee0305 315 break;
benkatz 23:2adf23ee0305 316
benkatz 26:2b865c00d7e9 317 case MOTOR_MODE: // Run torque control
benkatz 25:f5741040c4bb 318 if(state_change){
benkatz 25:f5741040c4bb 319 enter_torque_mode();
benkatz 28:8c7e29f719c5 320 count = 0;
benkatz 25:f5741040c4bb 321 }
benkatz 28:8c7e29f719c5 322 else{
benkatz 37:c0f352d6e8e3 323 /*
benkatz 37:c0f352d6e8e3 324 if(controller.v_bus>28.0f){ //Turn of gate drive if bus voltage is too high, to prevent FETsplosion if the bus is cut during regen
benkatz 47:e1196a851f76 325 gpio.
benkatz 47:e1196a851f76 326 ->write(0);
benkatz 37:c0f352d6e8e3 327 controller.ovp_flag = 1;
benkatz 37:c0f352d6e8e3 328 state = REST_MODE;
benkatz 37:c0f352d6e8e3 329 state_change = 1;
bdring 54:3e056b097c52 330 cond_printf("OVP Triggered!\n\r");
benkatz 37:c0f352d6e8e3 331 }
benkatz 37:c0f352d6e8e3 332 */
benkatz 37:c0f352d6e8e3 333
benkatz 28:8c7e29f719c5 334 if((controller.timeout > CAN_TIMEOUT) && (CAN_TIMEOUT > 0)){
benkatz 28:8c7e29f719c5 335 controller.i_d_ref = 0;
benkatz 28:8c7e29f719c5 336 controller.i_q_ref = 0;
benkatz 37:c0f352d6e8e3 337 controller.kp = 0;
benkatz 37:c0f352d6e8e3 338 controller.kd = 0;
benkatz 37:c0f352d6e8e3 339 controller.t_ff = 0;
benkatz 28:8c7e29f719c5 340 }
benkatz 47:e1196a851f76 341
benkatz 50:ba72df25d10f 342 torque_control(&controller);
benkatz 49:83d83040ea51 343 commutate(&controller, &observer, &gpio, controller.theta_elec); // Run current loop
benkatz 49:83d83040ea51 344
benkatz 49:83d83040ea51 345 controller.timeout++;
benkatz 49:83d83040ea51 346 count++;
benkatz 37:c0f352d6e8e3 347
benkatz 37:c0f352d6e8e3 348 }
benkatz 23:2adf23ee0305 349 break;
benkatz 23:2adf23ee0305 350 case SETUP_MODE:
benkatz 23:2adf23ee0305 351 if(state_change){
benkatz 24:58c2d7571207 352 enter_setup_state();
benkatz 23:2adf23ee0305 353 }
benkatz 23:2adf23ee0305 354 break;
benkatz 23:2adf23ee0305 355 case ENCODER_MODE:
benkatz 23:2adf23ee0305 356 print_encoder();
benkatz 23:2adf23ee0305 357 break;
benkatz 37:c0f352d6e8e3 358 }
benkatz 2:8724412ad628 359 }
benkatz 23:2adf23ee0305 360 TIM1->SR = 0x0; // reset the status register
benkatz 2:8724412ad628 361 }
benkatz 0:4e1c4df6aabd 362
benkatz 25:f5741040c4bb 363
benkatz 24:58c2d7571207 364 char cmd_val[8] = {0};
benkatz 24:58c2d7571207 365 char cmd_id = 0;
benkatz 25:f5741040c4bb 366 char char_count = 0;
benkatz 24:58c2d7571207 367
benkatz 25:f5741040c4bb 368 /// Manage state machine with commands from serial terminal or configurator gui ///
benkatz 25:f5741040c4bb 369 /// Called when data received over serial ///
bdring 54:3e056b097c52 370
benkatz 23:2adf23ee0305 371 void serial_interrupt(void){
bdring 54:3e056b097c52 372 while(pc->readable()){
bdring 54:3e056b097c52 373 char c = pc->getc();
benkatz 25:f5741040c4bb 374 if(c == 27){
benkatz 25:f5741040c4bb 375 state = REST_MODE;
benkatz 25:f5741040c4bb 376 state_change = 1;
benkatz 25:f5741040c4bb 377 char_count = 0;
benkatz 25:f5741040c4bb 378 cmd_id = 0;
benkatz 37:c0f352d6e8e3 379 gpio.led->write(0);;
benkatz 25:f5741040c4bb 380 for(int i = 0; i<8; i++){cmd_val[i] = 0;}
benkatz 25:f5741040c4bb 381 }
benkatz 24:58c2d7571207 382 if(state == REST_MODE){
benkatz 23:2adf23ee0305 383 switch (c){
benkatz 23:2adf23ee0305 384 case 'c':
benkatz 23:2adf23ee0305 385 state = CALIBRATION_MODE;
benkatz 23:2adf23ee0305 386 state_change = 1;
benkatz 23:2adf23ee0305 387 break;
benkatz 26:2b865c00d7e9 388 case 'm':
benkatz 26:2b865c00d7e9 389 state = MOTOR_MODE;
benkatz 23:2adf23ee0305 390 state_change = 1;
benkatz 23:2adf23ee0305 391 break;
benkatz 23:2adf23ee0305 392 case 'e':
benkatz 23:2adf23ee0305 393 state = ENCODER_MODE;
benkatz 23:2adf23ee0305 394 state_change = 1;
bdring 53:349304b6d937 395 break;
benkatz 23:2adf23ee0305 396 case 's':
benkatz 23:2adf23ee0305 397 state = SETUP_MODE;
benkatz 23:2adf23ee0305 398 state_change = 1;
benkatz 23:2adf23ee0305 399 break;
benkatz 37:c0f352d6e8e3 400 case 'z':
benkatz 37:c0f352d6e8e3 401 spi.SetMechOffset(0);
benkatz 47:e1196a851f76 402 spi.Sample(DT);
benkatz 37:c0f352d6e8e3 403 wait_us(20);
benkatz 37:c0f352d6e8e3 404 M_OFFSET = spi.GetMechPosition();
benkatz 37:c0f352d6e8e3 405 if (!prefs.ready()) prefs.open();
benkatz 37:c0f352d6e8e3 406 prefs.flush(); // Write new prefs to flash
benkatz 37:c0f352d6e8e3 407 prefs.close();
benkatz 37:c0f352d6e8e3 408 prefs.load();
benkatz 37:c0f352d6e8e3 409 spi.SetMechOffset(M_OFFSET);
bdring 54:3e056b097c52 410 cond_printf("\n\r Saved new zero position: %.4f\n\r\n\r", M_OFFSET);
benkatz 37:c0f352d6e8e3 411
benkatz 37:c0f352d6e8e3 412 break;
benkatz 37:c0f352d6e8e3 413 }
benkatz 37:c0f352d6e8e3 414
benkatz 24:58c2d7571207 415 }
benkatz 24:58c2d7571207 416 else if(state == SETUP_MODE){
benkatz 25:f5741040c4bb 417 if(c == 13){
benkatz 24:58c2d7571207 418 switch (cmd_id){
benkatz 24:58c2d7571207 419 case 'b':
benkatz 24:58c2d7571207 420 I_BW = fmaxf(fminf(atof(cmd_val), 2000.0f), 100.0f);
benkatz 24:58c2d7571207 421 break;
benkatz 24:58c2d7571207 422 case 'i':
benkatz 24:58c2d7571207 423 CAN_ID = atoi(cmd_val);
benkatz 24:58c2d7571207 424 break;
benkatz 26:2b865c00d7e9 425 case 'm':
benkatz 26:2b865c00d7e9 426 CAN_MASTER = atoi(cmd_val);
benkatz 26:2b865c00d7e9 427 break;
benkatz 24:58c2d7571207 428 case 'l':
benkatz 51:6cd89bd6fcaa 429 I_MAX = fmaxf(fminf(atof(cmd_val), 40.0f), 0.0f);
benkatz 51:6cd89bd6fcaa 430 break;
benkatz 51:6cd89bd6fcaa 431 case 'f':
benkatz 51:6cd89bd6fcaa 432 I_FW_MAX = fmaxf(fminf(atof(cmd_val), 33.0f), 0.0f);
benkatz 24:58c2d7571207 433 break;
benkatz 28:8c7e29f719c5 434 case 't':
benkatz 28:8c7e29f719c5 435 CAN_TIMEOUT = atoi(cmd_val);
benkatz 28:8c7e29f719c5 436 break;
benkatz 24:58c2d7571207 437 default:
bdring 54:3e056b097c52 438 cond_printf("\n\r '%c' Not a valid command prefix\n\r\n\r", cmd_id);
benkatz 24:58c2d7571207 439 break;
benkatz 24:58c2d7571207 440 }
benkatz 24:58c2d7571207 441
benkatz 24:58c2d7571207 442 if (!prefs.ready()) prefs.open();
benkatz 24:58c2d7571207 443 prefs.flush(); // Write new prefs to flash
benkatz 24:58c2d7571207 444 prefs.close();
benkatz 24:58c2d7571207 445 prefs.load();
benkatz 24:58c2d7571207 446 state_change = 1;
benkatz 24:58c2d7571207 447 char_count = 0;
benkatz 24:58c2d7571207 448 cmd_id = 0;
benkatz 24:58c2d7571207 449 for(int i = 0; i<8; i++){cmd_val[i] = 0;}
benkatz 24:58c2d7571207 450 }
benkatz 24:58c2d7571207 451 else{
benkatz 24:58c2d7571207 452 if(char_count == 0){cmd_id = c;}
benkatz 24:58c2d7571207 453 else{
benkatz 24:58c2d7571207 454 cmd_val[char_count-1] = c;
benkatz 24:58c2d7571207 455
benkatz 24:58c2d7571207 456 }
bdring 54:3e056b097c52 457 pc->putc(c);
benkatz 24:58c2d7571207 458 char_count++;
benkatz 23:2adf23ee0305 459 }
benkatz 23:2adf23ee0305 460 }
benkatz 24:58c2d7571207 461 else if (state == ENCODER_MODE){
benkatz 24:58c2d7571207 462 switch (c){
benkatz 24:58c2d7571207 463 case 27:
benkatz 24:58c2d7571207 464 state = REST_MODE;
benkatz 24:58c2d7571207 465 state_change = 1;
benkatz 24:58c2d7571207 466 break;
benkatz 24:58c2d7571207 467 }
benkatz 24:58c2d7571207 468 }
benkatz 49:83d83040ea51 469 else if (state == MOTOR_MODE){
benkatz 49:83d83040ea51 470 switch (c){
benkatz 49:83d83040ea51 471 case 'd':
benkatz 49:83d83040ea51 472 controller.i_q_ref = 0;
benkatz 49:83d83040ea51 473 controller.i_d_ref = 0;
bdring 53:349304b6d937 474 break;
bdring 53:349304b6d937 475 case 'f': // move forward
annhandt09 55:fee62d8fd8fb 476 controller.p_des += 0.2f;
bdring 54:3e056b097c52 477 cond_printf("p_des: %.3f\r\n", controller.p_des);
bdring 53:349304b6d937 478 break;
annhandt09 55:fee62d8fd8fb 479 case 'b': // move back
annhandt09 55:fee62d8fd8fb 480 controller.p_des -= 0.2f;
bdring 54:3e056b097c52 481 cond_printf("p_des: %.3f\r\n", controller.p_des);
bdring 53:349304b6d937 482 break;
bdring 53:349304b6d937 483 case 'p': // show posiiton
bdring 54:3e056b097c52 484 cond_printf("Pos: %.3f Vel: %.3f Cur: %.3f\r\n", controller.theta_mech, controller.dtheta_mech, controller.i_q_filt*KT_OUT);
bdring 53:349304b6d937 485 break;
annhandt09 55:fee62d8fd8fb 486 case 's': // show posiiton
annhandt09 55:fee62d8fd8fb 487 pack_reply(&txMsg, 0, 0 ,1);
annhandt09 55:fee62d8fd8fb 488 can.write(txMsg);
annhandt09 55:fee62d8fd8fb 489 break;
benkatz 49:83d83040ea51 490 }
bdring 53:349304b6d937 491
benkatz 49:83d83040ea51 492 }
benkatz 24:58c2d7571207 493
benkatz 24:58c2d7571207 494 }
benkatz 22:60276ba87ac6 495 }
bdring 54:3e056b097c52 496
benkatz 0:4e1c4df6aabd 497
benkatz 0:4e1c4df6aabd 498 int main() {
benkatz 20:bf9ea5125d52 499 controller.v_bus = V_BUS;
benkatz 22:60276ba87ac6 500 controller.mode = 0;
benkatz 23:2adf23ee0305 501 Init_All_HW(&gpio); // Setup PWM, ADC, GPIO
benkatz 44:8040fa2fcb0d 502 wait(.1);
benkatz 44:8040fa2fcb0d 503
benkatz 44:8040fa2fcb0d 504 gpio.enable->write(1);
benkatz 44:8040fa2fcb0d 505 wait_us(100);
benkatz 45:26801179208e 506 drv.calibrate();
benkatz 45:26801179208e 507 wait_us(100);
benkatz 44:8040fa2fcb0d 508 drv.write_DCR(0x0, 0x0, 0x0, PWM_MODE_3X, 0x0, 0x0, 0x0, 0x0, 0x1);
benkatz 44:8040fa2fcb0d 509 wait_us(100);
benkatz 46:2d4b1dafcfe3 510 drv.write_CSACR(0x0, 0x1, 0x0, CSA_GAIN_40, 0x0, 0x0, 0x0, 0x0, SEN_LVL_1_0);
benkatz 44:8040fa2fcb0d 511 wait_us(100);
benkatz 49:83d83040ea51 512 drv.write_OCPCR(TRETRY_4MS, DEADTIME_200NS, OCP_RETRY, OCP_DEG_8US, VDS_LVL_1_88);
benkatz 45:26801179208e 513
benkatz 45:26801179208e 514 //drv.enable_gd();
benkatz 44:8040fa2fcb0d 515 zero_current(&controller.adc1_offset, &controller.adc2_offset); // Measure current sensor zero-offset
benkatz 47:e1196a851f76 516 drv.disable_gd();
benkatz 20:bf9ea5125d52 517
benkatz 9:d7eb815cb057 518 wait(.1);
benkatz 44:8040fa2fcb0d 519 /*
benkatz 26:2b865c00d7e9 520 gpio.enable->write(1);
benkatz 26:2b865c00d7e9 521 TIM1->CCR3 = 0x708*(1.0f); // Write duty cycles
benkatz 26:2b865c00d7e9 522 TIM1->CCR2 = 0x708*(1.0f);
benkatz 26:2b865c00d7e9 523 TIM1->CCR1 = 0x708*(1.0f);
benkatz 26:2b865c00d7e9 524 gpio.enable->write(0);
benkatz 44:8040fa2fcb0d 525 */
benkatz 23:2adf23ee0305 526 reset_foc(&controller); // Reset current controller
benkatz 48:74a40481740c 527 reset_observer(&observer); // Reset observer
benkatz 26:2b865c00d7e9 528 TIM1->CR1 ^= TIM_CR1_UDIS;
benkatz 26:2b865c00d7e9 529 //TIM1->CR1 |= TIM_CR1_UDIS; //enable interrupt
benkatz 20:bf9ea5125d52 530
benkatz 20:bf9ea5125d52 531 wait(.1);
benkatz 37:c0f352d6e8e3 532 NVIC_SetPriority(TIM1_UP_TIM10_IRQn, 2); // commutation > communication
benkatz 43:dfb72608639c 533
benkatz 37:c0f352d6e8e3 534 NVIC_SetPriority(CAN1_RX0_IRQn, 3);
benkatz 26:2b865c00d7e9 535 can.filter(CAN_ID<<21, 0xFFE00004, CANStandard, 0);
benkatz 43:dfb72608639c 536
benkatz 28:8c7e29f719c5 537 txMsg.id = CAN_MASTER;
benkatz 28:8c7e29f719c5 538 txMsg.len = 6;
benkatz 26:2b865c00d7e9 539 rxMsg.len = 8;
benkatz 43:dfb72608639c 540 can.attach(&onMsgReceived); // attach 'CAN receive-complete' interrupt handler
benkatz 23:2adf23ee0305 541
benkatz 48:74a40481740c 542 // If preferences haven't been user configured yet, set defaults
benkatz 25:f5741040c4bb 543 prefs.load(); // Read flash
benkatz 37:c0f352d6e8e3 544 if(isnan(E_OFFSET)){E_OFFSET = 0.0f;}
benkatz 37:c0f352d6e8e3 545 if(isnan(M_OFFSET)){M_OFFSET = 0.0f;}
benkatz 48:74a40481740c 546 if(isnan(I_BW) || I_BW==-1){I_BW = 1000;}
benkatz 51:6cd89bd6fcaa 547 if(isnan(I_MAX) || I_MAX ==-1){I_MAX=40;}
benkatz 51:6cd89bd6fcaa 548 if(isnan(I_FW_MAX) || I_FW_MAX ==-1){I_FW_MAX=0;}
benkatz 48:74a40481740c 549 if(isnan(CAN_ID) || CAN_ID==-1){CAN_ID = 1;}
benkatz 48:74a40481740c 550 if(isnan(CAN_MASTER) || CAN_MASTER==-1){CAN_MASTER = 0;}
benkatz 48:74a40481740c 551 if(isnan(CAN_TIMEOUT) || CAN_TIMEOUT==-1){CAN_TIMEOUT = 0;}
benkatz 25:f5741040c4bb 552 spi.SetElecOffset(E_OFFSET); // Set position sensor offset
benkatz 37:c0f352d6e8e3 553 spi.SetMechOffset(M_OFFSET);
benkatz 23:2adf23ee0305 554 int lut[128] = {0};
benkatz 23:2adf23ee0305 555 memcpy(&lut, &ENCODER_LUT, sizeof(lut));
benkatz 25:f5741040c4bb 556 spi.WriteLUT(lut); // Set potision sensor nonlinearity lookup table
benkatz 45:26801179208e 557 init_controller_params(&controller);
benkatz 45:26801179208e 558
bdring 54:3e056b097c52 559
annhandt09 55:fee62d8fd8fb 560 set_io_mode(IO_MODE_STEP_DIR);
bdring 52:cf8b2abf811d 561 wait_us(100);
bdring 54:3e056b097c52 562 cond_printf("\n\r\n\r Hobby King Cheetah\n\r\n\r");
bdring 52:cf8b2abf811d 563 wait_us(10);
bdring 54:3e056b097c52 564 cond_printf("\n\r Debug Info:\n\r");
bdring 54:3e056b097c52 565 cond_printf(" Firmware Version: %s\n\r", VERSION_NUM);
bdring 54:3e056b097c52 566 cond_printf(" ADC1 Offset: %d ADC2 Offset: %d\n\r", controller.adc1_offset, controller.adc2_offset);
bdring 54:3e056b097c52 567 cond_printf(" Position Sensor Electrical Offset: %.4f\n\r", E_OFFSET);
bdring 54:3e056b097c52 568 cond_printf(" Output Zero Position: %.4f\n\r", M_OFFSET);
bdring 54:3e056b097c52 569 cond_printf(" Gear Ratio %.4f:1\r\n", GR);
bdring 54:3e056b097c52 570 cond_printf(" Mapped Position %.4f to %.4f Radians\n\r", P_MIN, P_MAX);
bdring 54:3e056b097c52 571 cond_printf(" CAN ID: %d\n\r", CAN_ID);
bdring 54:3e056b097c52 572
benkatz 23:2adf23ee0305 573 state_change = 1;
benkatz 50:ba72df25d10f 574
bdring 54:3e056b097c52 575 //int counter = 0;
benkatz 0:4e1c4df6aabd 576 while(1) {
benkatz 47:e1196a851f76 577 drv.print_faults();
benkatz 50:ba72df25d10f 578 wait(.1);
bdring 54:3e056b097c52 579
benkatz 47:e1196a851f76 580
benkatz 0:4e1c4df6aabd 581 }
benkatz 0:4e1c4df6aabd 582 }