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