Fork and fix for mwork

Dependencies:   mbed-dev-f303 FastPWM3 millis

Committer:
annhandt09
Date:
Sat Jun 27 07:50:17 2020 +0000
Revision:
57:c26b0093783b
Parent:
56:eaf75bcad361
Child:
58:fb799e99a5f7
Add default PID value

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