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