Fork and fix for mwork
Dependencies: mbed-dev-f303 FastPWM3 millis
main.cpp@53:349304b6d937, 2020-02-28 (annotated)
- Committer:
- bdring
- Date:
- Fri Feb 28 23:05:58 2020 +0000
- Revision:
- 53:349304b6d937
- Parent:
- 52:cf8b2abf811d
- Child:
- 54:3e056b097c52
- Added a bunch of debugging options from the main menu; - Testing step and direction ideas via f & r commands.
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 | 52:cf8b2abf811d | 13 | #define VERSION_NUM "1.9.1" |
benkatz | 26:2b865c00d7e9 | 14 | |
benkatz | 18:f1d56f4acb39 | 15 | |
benkatz | 26:2b865c00d7e9 | 16 | float __float_reg[64]; // Floats stored in flash |
benkatz | 26:2b865c00d7e9 | 17 | int __int_reg[256]; // Ints stored in flash. Includes position sensor calibration lookup table |
benkatz | 17:3c5df2982199 | 18 | |
benkatz | 0:4e1c4df6aabd | 19 | #include "mbed.h" |
benkatz | 0:4e1c4df6aabd | 20 | #include "PositionSensor.h" |
benkatz | 20:bf9ea5125d52 | 21 | #include "structs.h" |
benkatz | 20:bf9ea5125d52 | 22 | #include "foc.h" |
benkatz | 22:60276ba87ac6 | 23 | #include "calibration.h" |
benkatz | 20:bf9ea5125d52 | 24 | #include "hw_setup.h" |
benkatz | 23:2adf23ee0305 | 25 | #include "math_ops.h" |
benkatz | 20:bf9ea5125d52 | 26 | #include "current_controller_config.h" |
benkatz | 20:bf9ea5125d52 | 27 | #include "hw_config.h" |
benkatz | 20:bf9ea5125d52 | 28 | #include "motor_config.h" |
benkatz | 23:2adf23ee0305 | 29 | #include "stm32f4xx_flash.h" |
benkatz | 23:2adf23ee0305 | 30 | #include "FlashWriter.h" |
benkatz | 23:2adf23ee0305 | 31 | #include "user_config.h" |
benkatz | 23:2adf23ee0305 | 32 | #include "PreferenceWriter.h" |
benkatz | 42:738fa01b0346 | 33 | #include "CAN_com.h" |
benkatz | 44:8040fa2fcb0d | 34 | #include "DRV.h" |
benkatz | 26:2b865c00d7e9 | 35 | |
benkatz | 23:2adf23ee0305 | 36 | PreferenceWriter prefs(6); |
benkatz | 9:d7eb815cb057 | 37 | |
benkatz | 20:bf9ea5125d52 | 38 | GPIOStruct gpio; |
benkatz | 20:bf9ea5125d52 | 39 | ControllerStruct controller; |
benkatz | 48:74a40481740c | 40 | ObserverStruct observer; |
benkatz | 20:bf9ea5125d52 | 41 | COMStruct com; |
benkatz | 43:dfb72608639c | 42 | Serial pc(PA_2, PA_3); |
benkatz | 9:d7eb815cb057 | 43 | |
benkatz | 17:3c5df2982199 | 44 | |
benkatz | 45:26801179208e | 45 | CAN can(PB_8, PB_9, 1000000); // CAN Rx pin name, CAN Tx pin name |
benkatz | 26:2b865c00d7e9 | 46 | CANMessage rxMsg; |
benkatz | 26:2b865c00d7e9 | 47 | CANMessage txMsg; |
benkatz | 23:2adf23ee0305 | 48 | |
benkatz | 20:bf9ea5125d52 | 49 | |
benkatz | 44:8040fa2fcb0d | 50 | SPI drv_spi(PA_7, PA_6, PA_5); |
benkatz | 44:8040fa2fcb0d | 51 | DigitalOut drv_cs(PA_4); |
benkatz | 44:8040fa2fcb0d | 52 | //DigitalOut drv_en_gate(PA_11); |
benkatz | 44:8040fa2fcb0d | 53 | DRV832x drv(&drv_spi, &drv_cs); |
benkatz | 8:10ae7bc88d6e | 54 | |
benkatz | 26:2b865c00d7e9 | 55 | PositionSensorAM5147 spi(16384, 0.0, NPP); |
benkatz | 20:bf9ea5125d52 | 56 | |
benkatz | 23:2adf23ee0305 | 57 | volatile int count = 0; |
benkatz | 23:2adf23ee0305 | 58 | volatile int state = REST_MODE; |
benkatz | 23:2adf23ee0305 | 59 | volatile int state_change; |
benkatz | 20:bf9ea5125d52 | 60 | |
benkatz | 26:2b865c00d7e9 | 61 | void onMsgReceived() { |
benkatz | 26:2b865c00d7e9 | 62 | //msgAvailable = true; |
bdring | 52:cf8b2abf811d | 63 | //printf("%df\n\r", rxMsg.id); |
benkatz | 26:2b865c00d7e9 | 64 | can.read(rxMsg); |
benkatz | 28:8c7e29f719c5 | 65 | if((rxMsg.id == CAN_ID)){ |
benkatz | 28:8c7e29f719c5 | 66 | controller.timeout = 0; |
benkatz | 28:8c7e29f719c5 | 67 | 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 | 68 | state = MOTOR_MODE; |
benkatz | 28:8c7e29f719c5 | 69 | state_change = 1; |
benkatz | 28:8c7e29f719c5 | 70 | } |
benkatz | 28:8c7e29f719c5 | 71 | 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 | 72 | state = REST_MODE; |
benkatz | 28:8c7e29f719c5 | 73 | state_change = 1; |
benkatz | 37:c0f352d6e8e3 | 74 | gpio.led->write(0);; |
benkatz | 28:8c7e29f719c5 | 75 | } |
benkatz | 28:8c7e29f719c5 | 76 | 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 | 77 | spi.ZeroPosition(); |
benkatz | 28:8c7e29f719c5 | 78 | } |
benkatz | 28:8c7e29f719c5 | 79 | else if(state == MOTOR_MODE){ |
benkatz | 28:8c7e29f719c5 | 80 | unpack_cmd(rxMsg, &controller); |
benkatz | 28:8c7e29f719c5 | 81 | } |
benkatz | 37:c0f352d6e8e3 | 82 | pack_reply(&txMsg, controller.theta_mech, controller.dtheta_mech, controller.i_q_filt*KT_OUT); |
benkatz | 37:c0f352d6e8e3 | 83 | can.write(txMsg); |
benkatz | 28:8c7e29f719c5 | 84 | } |
benkatz | 26:2b865c00d7e9 | 85 | |
benkatz | 26:2b865c00d7e9 | 86 | } |
benkatz | 26:2b865c00d7e9 | 87 | |
benkatz | 23:2adf23ee0305 | 88 | void enter_menu_state(void){ |
benkatz | 44:8040fa2fcb0d | 89 | drv.disable_gd(); |
benkatz | 47:e1196a851f76 | 90 | //gpio.enable->write(0); |
benkatz | 23:2adf23ee0305 | 91 | printf("\n\r\n\r\n\r"); |
benkatz | 23:2adf23ee0305 | 92 | printf(" Commands:\n\r"); |
benkatz | 44:8040fa2fcb0d | 93 | wait_us(10); |
benkatz | 26:2b865c00d7e9 | 94 | printf(" m - Motor Mode\n\r"); |
benkatz | 44:8040fa2fcb0d | 95 | wait_us(10); |
benkatz | 23:2adf23ee0305 | 96 | printf(" c - Calibrate Encoder\n\r"); |
benkatz | 44:8040fa2fcb0d | 97 | wait_us(10); |
benkatz | 23:2adf23ee0305 | 98 | printf(" s - Setup\n\r"); |
benkatz | 44:8040fa2fcb0d | 99 | wait_us(10); |
benkatz | 23:2adf23ee0305 | 100 | printf(" e - Display Encoder\n\r"); |
benkatz | 44:8040fa2fcb0d | 101 | wait_us(10); |
benkatz | 37:c0f352d6e8e3 | 102 | printf(" z - Set Zero Position\n\r"); |
benkatz | 44:8040fa2fcb0d | 103 | wait_us(10); |
bdring | 53:349304b6d937 | 104 | printf(" f - Move Forward\n\r"); |
bdring | 53:349304b6d937 | 105 | wait_us(10); |
bdring | 53:349304b6d937 | 106 | printf(" b - Move Back\n\r"); |
bdring | 53:349304b6d937 | 107 | wait_us(10); |
bdring | 53:349304b6d937 | 108 | printf(" p - current posiiton\n\r"); |
bdring | 52:cf8b2abf811d | 109 | wait_us(10); |
benkatz | 23:2adf23ee0305 | 110 | printf(" esc - Exit to Menu\n\r"); |
benkatz | 44:8040fa2fcb0d | 111 | wait_us(10); |
benkatz | 23:2adf23ee0305 | 112 | state_change = 0; |
benkatz | 37:c0f352d6e8e3 | 113 | gpio.led->write(0); |
benkatz | 23:2adf23ee0305 | 114 | } |
benkatz | 24:58c2d7571207 | 115 | |
benkatz | 24:58c2d7571207 | 116 | void enter_setup_state(void){ |
benkatz | 24:58c2d7571207 | 117 | printf("\n\r\n\r Configuration Options \n\r\n\n"); |
benkatz | 44:8040fa2fcb0d | 118 | wait_us(10); |
benkatz | 51:6cd89bd6fcaa | 119 | printf(" %-4s %-31s %-5s %-6s %-2s\n\r\n\r", "prefix", "parameter", "min", "max", "current value"); |
benkatz | 44:8040fa2fcb0d | 120 | wait_us(10); |
benkatz | 28:8c7e29f719c5 | 121 | printf(" %-4s %-31s %-5s %-6s %.1f\n\r", "b", "Current Bandwidth (Hz)", "100", "2000", I_BW); |
benkatz | 44:8040fa2fcb0d | 122 | wait_us(10); |
benkatz | 28:8c7e29f719c5 | 123 | printf(" %-4s %-31s %-5s %-6s %-5i\n\r", "i", "CAN ID", "0", "127", CAN_ID); |
benkatz | 44:8040fa2fcb0d | 124 | wait_us(10); |
benkatz | 28:8c7e29f719c5 | 125 | printf(" %-4s %-31s %-5s %-6s %-5i\n\r", "m", "CAN Master ID", "0", "127", CAN_MASTER); |
benkatz | 44:8040fa2fcb0d | 126 | wait_us(10); |
benkatz | 51:6cd89bd6fcaa | 127 | printf(" %-4s %-31s %-5s %-6s %.1f\n\r", "l", "Current Limit (A)", "0.0", "40.0", I_MAX); |
benkatz | 51:6cd89bd6fcaa | 128 | wait_us(10); |
benkatz | 51:6cd89bd6fcaa | 129 | printf(" %-4s %-31s %-5s %-6s %.1f\n\r", "f", "FW Current Limit (A)", "0.0", "33.0", I_FW_MAX); |
benkatz | 44:8040fa2fcb0d | 130 | wait_us(10); |
benkatz | 28:8c7e29f719c5 | 131 | printf(" %-4s %-31s %-5s %-6s %d\n\r", "t", "CAN Timeout (cycles)(0 = none)", "0", "100000", CAN_TIMEOUT); |
benkatz | 44:8040fa2fcb0d | 132 | wait_us(10); |
benkatz | 24:58c2d7571207 | 133 | printf("\n\r To change a value, type 'prefix''value''ENTER'\n\r i.e. 'b1000''ENTER'\n\r\n\r"); |
benkatz | 44:8040fa2fcb0d | 134 | wait_us(10); |
benkatz | 24:58c2d7571207 | 135 | state_change = 0; |
benkatz | 24:58c2d7571207 | 136 | } |
benkatz | 22:60276ba87ac6 | 137 | |
benkatz | 23:2adf23ee0305 | 138 | void enter_torque_mode(void){ |
benkatz | 44:8040fa2fcb0d | 139 | drv.enable_gd(); |
benkatz | 47:e1196a851f76 | 140 | //gpio.enable->write(1); |
benkatz | 37:c0f352d6e8e3 | 141 | controller.ovp_flag = 0; |
benkatz | 28:8c7e29f719c5 | 142 | reset_foc(&controller); // Tesets integrators, and other control loop parameters |
benkatz | 28:8c7e29f719c5 | 143 | wait(.001); |
benkatz | 23:2adf23ee0305 | 144 | controller.i_d_ref = 0; |
benkatz | 50:ba72df25d10f | 145 | controller.i_q_ref = 0; // Current Setpoints |
benkatz | 37:c0f352d6e8e3 | 146 | gpio.led->write(1); // Turn on status LED |
benkatz | 25:f5741040c4bb | 147 | state_change = 0; |
benkatz | 28:8c7e29f719c5 | 148 | printf("\n\r Entering Motor Mode \n\r"); |
benkatz | 23:2adf23ee0305 | 149 | } |
benkatz | 22:60276ba87ac6 | 150 | |
benkatz | 23:2adf23ee0305 | 151 | void calibrate(void){ |
benkatz | 44:8040fa2fcb0d | 152 | drv.enable_gd(); |
benkatz | 47:e1196a851f76 | 153 | //gpio.enable->write(1); |
benkatz | 37:c0f352d6e8e3 | 154 | gpio.led->write(1); // Turn on status LED |
benkatz | 25:f5741040c4bb | 155 | order_phases(&spi, &gpio, &controller, &prefs); // Check phase ordering |
benkatz | 25:f5741040c4bb | 156 | calibrate(&spi, &gpio, &controller, &prefs); // Perform calibration procedure |
benkatz | 37:c0f352d6e8e3 | 157 | gpio.led->write(0);; // Turn off status LED |
benkatz | 23:2adf23ee0305 | 158 | wait(.2); |
benkatz | 23:2adf23ee0305 | 159 | printf("\n\r Calibration complete. Press 'esc' to return to menu\n\r"); |
benkatz | 44:8040fa2fcb0d | 160 | drv.disable_gd(); |
benkatz | 47:e1196a851f76 | 161 | //gpio.enable->write(0); |
benkatz | 23:2adf23ee0305 | 162 | state_change = 0; |
benkatz | 23:2adf23ee0305 | 163 | } |
benkatz | 23:2adf23ee0305 | 164 | |
benkatz | 23:2adf23ee0305 | 165 | void print_encoder(void){ |
benkatz | 48:74a40481740c | 166 | printf(" Mechanical Angle: %f Electrical Angle: %f Raw: %d\n\r", spi.GetMechPosition(), spi.GetElecPosition(), spi.GetRawPosition()); |
benkatz | 48:74a40481740c | 167 | //printf("%d\n\r", spi.GetRawPosition()); |
benkatz | 47:e1196a851f76 | 168 | wait(.001); |
benkatz | 22:60276ba87ac6 | 169 | } |
benkatz | 20:bf9ea5125d52 | 170 | |
benkatz | 23:2adf23ee0305 | 171 | /// Current Sampling Interrupt /// |
benkatz | 23:2adf23ee0305 | 172 | /// This runs at 40 kHz, regardless of of the mode the controller is in /// |
benkatz | 2:8724412ad628 | 173 | extern "C" void TIM1_UP_TIM10_IRQHandler(void) { |
benkatz | 2:8724412ad628 | 174 | if (TIM1->SR & TIM_SR_UIF ) { |
benkatz | 23:2adf23ee0305 | 175 | |
benkatz | 23:2adf23ee0305 | 176 | ///Sample current always /// |
benkatz | 25:f5741040c4bb | 177 | ADC1->CR2 |= 0x40000000; // Begin sample and conversion |
benkatz | 22:60276ba87ac6 | 178 | //volatile int delay; |
benkatz | 20:bf9ea5125d52 | 179 | //for (delay = 0; delay < 55; delay++); |
benkatz | 45:26801179208e | 180 | |
benkatz | 47:e1196a851f76 | 181 | spi.Sample(DT); // sample position sensor |
benkatz | 37:c0f352d6e8e3 | 182 | controller.adc2_raw = ADC2->DR; // Read ADC Data Registers |
benkatz | 23:2adf23ee0305 | 183 | controller.adc1_raw = ADC1->DR; |
benkatz | 37:c0f352d6e8e3 | 184 | controller.adc3_raw = ADC3->DR; |
benkatz | 37:c0f352d6e8e3 | 185 | controller.theta_elec = spi.GetElecPosition(); |
benkatz | 37:c0f352d6e8e3 | 186 | controller.theta_mech = (1.0f/GR)*spi.GetMechPosition(); |
benkatz | 37:c0f352d6e8e3 | 187 | controller.dtheta_mech = (1.0f/GR)*spi.GetMechVelocity(); |
benkatz | 37:c0f352d6e8e3 | 188 | controller.dtheta_elec = spi.GetElecVelocity(); |
benkatz | 51:6cd89bd6fcaa | 189 | 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 | 190 | /// |
benkatz | 20:bf9ea5125d52 | 191 | |
benkatz | 23:2adf23ee0305 | 192 | /// Check state machine state, and run the appropriate function /// |
benkatz | 23:2adf23ee0305 | 193 | switch(state){ |
benkatz | 37:c0f352d6e8e3 | 194 | case REST_MODE: // Do nothing |
benkatz | 23:2adf23ee0305 | 195 | if(state_change){ |
benkatz | 23:2adf23ee0305 | 196 | enter_menu_state(); |
benkatz | 23:2adf23ee0305 | 197 | } |
benkatz | 23:2adf23ee0305 | 198 | break; |
benkatz | 22:60276ba87ac6 | 199 | |
benkatz | 23:2adf23ee0305 | 200 | case CALIBRATION_MODE: // Run encoder calibration procedure |
benkatz | 23:2adf23ee0305 | 201 | if(state_change){ |
benkatz | 23:2adf23ee0305 | 202 | calibrate(); |
benkatz | 23:2adf23ee0305 | 203 | } |
benkatz | 23:2adf23ee0305 | 204 | break; |
benkatz | 23:2adf23ee0305 | 205 | |
benkatz | 26:2b865c00d7e9 | 206 | case MOTOR_MODE: // Run torque control |
benkatz | 25:f5741040c4bb | 207 | if(state_change){ |
benkatz | 25:f5741040c4bb | 208 | enter_torque_mode(); |
benkatz | 28:8c7e29f719c5 | 209 | count = 0; |
benkatz | 25:f5741040c4bb | 210 | } |
benkatz | 28:8c7e29f719c5 | 211 | else{ |
benkatz | 37:c0f352d6e8e3 | 212 | /* |
benkatz | 37:c0f352d6e8e3 | 213 | 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 | 214 | gpio. |
benkatz | 47:e1196a851f76 | 215 | ->write(0); |
benkatz | 37:c0f352d6e8e3 | 216 | controller.ovp_flag = 1; |
benkatz | 37:c0f352d6e8e3 | 217 | state = REST_MODE; |
benkatz | 37:c0f352d6e8e3 | 218 | state_change = 1; |
benkatz | 37:c0f352d6e8e3 | 219 | printf("OVP Triggered!\n\r"); |
benkatz | 37:c0f352d6e8e3 | 220 | } |
benkatz | 37:c0f352d6e8e3 | 221 | */ |
benkatz | 37:c0f352d6e8e3 | 222 | |
benkatz | 28:8c7e29f719c5 | 223 | if((controller.timeout > CAN_TIMEOUT) && (CAN_TIMEOUT > 0)){ |
benkatz | 28:8c7e29f719c5 | 224 | controller.i_d_ref = 0; |
benkatz | 28:8c7e29f719c5 | 225 | controller.i_q_ref = 0; |
benkatz | 37:c0f352d6e8e3 | 226 | controller.kp = 0; |
benkatz | 37:c0f352d6e8e3 | 227 | controller.kd = 0; |
benkatz | 37:c0f352d6e8e3 | 228 | controller.t_ff = 0; |
benkatz | 28:8c7e29f719c5 | 229 | } |
benkatz | 47:e1196a851f76 | 230 | |
benkatz | 50:ba72df25d10f | 231 | torque_control(&controller); |
benkatz | 49:83d83040ea51 | 232 | commutate(&controller, &observer, &gpio, controller.theta_elec); // Run current loop |
benkatz | 49:83d83040ea51 | 233 | |
benkatz | 49:83d83040ea51 | 234 | controller.timeout++; |
benkatz | 49:83d83040ea51 | 235 | count++; |
benkatz | 37:c0f352d6e8e3 | 236 | |
benkatz | 37:c0f352d6e8e3 | 237 | } |
benkatz | 23:2adf23ee0305 | 238 | break; |
benkatz | 23:2adf23ee0305 | 239 | case SETUP_MODE: |
benkatz | 23:2adf23ee0305 | 240 | if(state_change){ |
benkatz | 24:58c2d7571207 | 241 | enter_setup_state(); |
benkatz | 23:2adf23ee0305 | 242 | } |
benkatz | 23:2adf23ee0305 | 243 | break; |
benkatz | 23:2adf23ee0305 | 244 | case ENCODER_MODE: |
benkatz | 23:2adf23ee0305 | 245 | print_encoder(); |
benkatz | 23:2adf23ee0305 | 246 | break; |
benkatz | 37:c0f352d6e8e3 | 247 | } |
benkatz | 2:8724412ad628 | 248 | } |
benkatz | 23:2adf23ee0305 | 249 | TIM1->SR = 0x0; // reset the status register |
benkatz | 2:8724412ad628 | 250 | } |
benkatz | 0:4e1c4df6aabd | 251 | |
benkatz | 25:f5741040c4bb | 252 | |
benkatz | 24:58c2d7571207 | 253 | char cmd_val[8] = {0}; |
benkatz | 24:58c2d7571207 | 254 | char cmd_id = 0; |
benkatz | 25:f5741040c4bb | 255 | char char_count = 0; |
benkatz | 24:58c2d7571207 | 256 | |
benkatz | 25:f5741040c4bb | 257 | /// Manage state machine with commands from serial terminal or configurator gui /// |
benkatz | 25:f5741040c4bb | 258 | /// Called when data received over serial /// |
benkatz | 23:2adf23ee0305 | 259 | void serial_interrupt(void){ |
benkatz | 23:2adf23ee0305 | 260 | while(pc.readable()){ |
benkatz | 23:2adf23ee0305 | 261 | char c = pc.getc(); |
benkatz | 25:f5741040c4bb | 262 | if(c == 27){ |
benkatz | 25:f5741040c4bb | 263 | state = REST_MODE; |
benkatz | 25:f5741040c4bb | 264 | state_change = 1; |
benkatz | 25:f5741040c4bb | 265 | char_count = 0; |
benkatz | 25:f5741040c4bb | 266 | cmd_id = 0; |
benkatz | 37:c0f352d6e8e3 | 267 | gpio.led->write(0);; |
benkatz | 25:f5741040c4bb | 268 | for(int i = 0; i<8; i++){cmd_val[i] = 0;} |
benkatz | 25:f5741040c4bb | 269 | } |
benkatz | 24:58c2d7571207 | 270 | if(state == REST_MODE){ |
benkatz | 23:2adf23ee0305 | 271 | switch (c){ |
benkatz | 23:2adf23ee0305 | 272 | case 'c': |
benkatz | 23:2adf23ee0305 | 273 | state = CALIBRATION_MODE; |
benkatz | 23:2adf23ee0305 | 274 | state_change = 1; |
benkatz | 23:2adf23ee0305 | 275 | break; |
benkatz | 26:2b865c00d7e9 | 276 | case 'm': |
benkatz | 26:2b865c00d7e9 | 277 | state = MOTOR_MODE; |
benkatz | 23:2adf23ee0305 | 278 | state_change = 1; |
benkatz | 23:2adf23ee0305 | 279 | break; |
benkatz | 23:2adf23ee0305 | 280 | case 'e': |
benkatz | 23:2adf23ee0305 | 281 | state = ENCODER_MODE; |
benkatz | 23:2adf23ee0305 | 282 | state_change = 1; |
bdring | 53:349304b6d937 | 283 | break; |
benkatz | 23:2adf23ee0305 | 284 | case 's': |
benkatz | 23:2adf23ee0305 | 285 | state = SETUP_MODE; |
benkatz | 23:2adf23ee0305 | 286 | state_change = 1; |
benkatz | 23:2adf23ee0305 | 287 | break; |
benkatz | 37:c0f352d6e8e3 | 288 | case 'z': |
benkatz | 37:c0f352d6e8e3 | 289 | spi.SetMechOffset(0); |
benkatz | 47:e1196a851f76 | 290 | spi.Sample(DT); |
benkatz | 37:c0f352d6e8e3 | 291 | wait_us(20); |
benkatz | 37:c0f352d6e8e3 | 292 | M_OFFSET = spi.GetMechPosition(); |
benkatz | 37:c0f352d6e8e3 | 293 | if (!prefs.ready()) prefs.open(); |
benkatz | 37:c0f352d6e8e3 | 294 | prefs.flush(); // Write new prefs to flash |
benkatz | 37:c0f352d6e8e3 | 295 | prefs.close(); |
benkatz | 37:c0f352d6e8e3 | 296 | prefs.load(); |
benkatz | 37:c0f352d6e8e3 | 297 | spi.SetMechOffset(M_OFFSET); |
benkatz | 37:c0f352d6e8e3 | 298 | printf("\n\r Saved new zero position: %.4f\n\r\n\r", M_OFFSET); |
benkatz | 37:c0f352d6e8e3 | 299 | |
benkatz | 37:c0f352d6e8e3 | 300 | break; |
benkatz | 37:c0f352d6e8e3 | 301 | } |
benkatz | 37:c0f352d6e8e3 | 302 | |
benkatz | 24:58c2d7571207 | 303 | } |
benkatz | 24:58c2d7571207 | 304 | else if(state == SETUP_MODE){ |
benkatz | 25:f5741040c4bb | 305 | if(c == 13){ |
benkatz | 24:58c2d7571207 | 306 | switch (cmd_id){ |
benkatz | 24:58c2d7571207 | 307 | case 'b': |
benkatz | 24:58c2d7571207 | 308 | I_BW = fmaxf(fminf(atof(cmd_val), 2000.0f), 100.0f); |
benkatz | 24:58c2d7571207 | 309 | break; |
benkatz | 24:58c2d7571207 | 310 | case 'i': |
benkatz | 24:58c2d7571207 | 311 | CAN_ID = atoi(cmd_val); |
benkatz | 24:58c2d7571207 | 312 | break; |
benkatz | 26:2b865c00d7e9 | 313 | case 'm': |
benkatz | 26:2b865c00d7e9 | 314 | CAN_MASTER = atoi(cmd_val); |
benkatz | 26:2b865c00d7e9 | 315 | break; |
benkatz | 24:58c2d7571207 | 316 | case 'l': |
benkatz | 51:6cd89bd6fcaa | 317 | I_MAX = fmaxf(fminf(atof(cmd_val), 40.0f), 0.0f); |
benkatz | 51:6cd89bd6fcaa | 318 | break; |
benkatz | 51:6cd89bd6fcaa | 319 | case 'f': |
benkatz | 51:6cd89bd6fcaa | 320 | I_FW_MAX = fmaxf(fminf(atof(cmd_val), 33.0f), 0.0f); |
benkatz | 24:58c2d7571207 | 321 | break; |
benkatz | 28:8c7e29f719c5 | 322 | case 't': |
benkatz | 28:8c7e29f719c5 | 323 | CAN_TIMEOUT = atoi(cmd_val); |
benkatz | 28:8c7e29f719c5 | 324 | break; |
benkatz | 24:58c2d7571207 | 325 | default: |
benkatz | 24:58c2d7571207 | 326 | printf("\n\r '%c' Not a valid command prefix\n\r\n\r", cmd_id); |
benkatz | 24:58c2d7571207 | 327 | break; |
benkatz | 24:58c2d7571207 | 328 | } |
benkatz | 24:58c2d7571207 | 329 | |
benkatz | 24:58c2d7571207 | 330 | if (!prefs.ready()) prefs.open(); |
benkatz | 24:58c2d7571207 | 331 | prefs.flush(); // Write new prefs to flash |
benkatz | 24:58c2d7571207 | 332 | prefs.close(); |
benkatz | 24:58c2d7571207 | 333 | prefs.load(); |
benkatz | 24:58c2d7571207 | 334 | state_change = 1; |
benkatz | 24:58c2d7571207 | 335 | char_count = 0; |
benkatz | 24:58c2d7571207 | 336 | cmd_id = 0; |
benkatz | 24:58c2d7571207 | 337 | for(int i = 0; i<8; i++){cmd_val[i] = 0;} |
benkatz | 24:58c2d7571207 | 338 | } |
benkatz | 24:58c2d7571207 | 339 | else{ |
benkatz | 24:58c2d7571207 | 340 | if(char_count == 0){cmd_id = c;} |
benkatz | 24:58c2d7571207 | 341 | else{ |
benkatz | 24:58c2d7571207 | 342 | cmd_val[char_count-1] = c; |
benkatz | 24:58c2d7571207 | 343 | |
benkatz | 24:58c2d7571207 | 344 | } |
benkatz | 24:58c2d7571207 | 345 | pc.putc(c); |
benkatz | 24:58c2d7571207 | 346 | char_count++; |
benkatz | 23:2adf23ee0305 | 347 | } |
benkatz | 23:2adf23ee0305 | 348 | } |
benkatz | 24:58c2d7571207 | 349 | else if (state == ENCODER_MODE){ |
benkatz | 24:58c2d7571207 | 350 | switch (c){ |
benkatz | 24:58c2d7571207 | 351 | case 27: |
benkatz | 24:58c2d7571207 | 352 | state = REST_MODE; |
benkatz | 24:58c2d7571207 | 353 | state_change = 1; |
benkatz | 24:58c2d7571207 | 354 | break; |
benkatz | 24:58c2d7571207 | 355 | } |
benkatz | 24:58c2d7571207 | 356 | } |
benkatz | 49:83d83040ea51 | 357 | else if (state == MOTOR_MODE){ |
benkatz | 49:83d83040ea51 | 358 | switch (c){ |
benkatz | 49:83d83040ea51 | 359 | case 'd': |
benkatz | 49:83d83040ea51 | 360 | controller.i_q_ref = 0; |
benkatz | 49:83d83040ea51 | 361 | controller.i_d_ref = 0; |
bdring | 53:349304b6d937 | 362 | break; |
bdring | 53:349304b6d937 | 363 | case 'f': // move forward |
bdring | 53:349304b6d937 | 364 | controller.p_des += 0.02f; |
bdring | 53:349304b6d937 | 365 | printf("p_des: %.3f\r\n", controller.p_des); |
bdring | 53:349304b6d937 | 366 | break; |
bdring | 53:349304b6d937 | 367 | case 'r': // move back |
bdring | 53:349304b6d937 | 368 | controller.p_des -= 0.02f; |
bdring | 53:349304b6d937 | 369 | printf("p_des: %.3f\r\n", controller.p_des); |
bdring | 53:349304b6d937 | 370 | break; |
bdring | 53:349304b6d937 | 371 | case 'p': // show posiiton |
bdring | 53:349304b6d937 | 372 | printf("Pos: %.3f Vel: %.3f Cur: %.3f\r\n", controller.theta_mech, controller.dtheta_mech, controller.i_q_filt*KT_OUT); |
bdring | 53:349304b6d937 | 373 | break; |
benkatz | 49:83d83040ea51 | 374 | } |
bdring | 53:349304b6d937 | 375 | |
benkatz | 49:83d83040ea51 | 376 | } |
benkatz | 24:58c2d7571207 | 377 | |
benkatz | 24:58c2d7571207 | 378 | } |
benkatz | 22:60276ba87ac6 | 379 | } |
benkatz | 0:4e1c4df6aabd | 380 | |
benkatz | 0:4e1c4df6aabd | 381 | int main() { |
benkatz | 20:bf9ea5125d52 | 382 | controller.v_bus = V_BUS; |
benkatz | 22:60276ba87ac6 | 383 | controller.mode = 0; |
benkatz | 23:2adf23ee0305 | 384 | Init_All_HW(&gpio); // Setup PWM, ADC, GPIO |
benkatz | 44:8040fa2fcb0d | 385 | wait(.1); |
benkatz | 44:8040fa2fcb0d | 386 | |
benkatz | 44:8040fa2fcb0d | 387 | gpio.enable->write(1); |
benkatz | 44:8040fa2fcb0d | 388 | wait_us(100); |
benkatz | 45:26801179208e | 389 | drv.calibrate(); |
benkatz | 45:26801179208e | 390 | wait_us(100); |
benkatz | 44:8040fa2fcb0d | 391 | drv.write_DCR(0x0, 0x0, 0x0, PWM_MODE_3X, 0x0, 0x0, 0x0, 0x0, 0x1); |
benkatz | 44:8040fa2fcb0d | 392 | wait_us(100); |
benkatz | 46:2d4b1dafcfe3 | 393 | drv.write_CSACR(0x0, 0x1, 0x0, CSA_GAIN_40, 0x0, 0x0, 0x0, 0x0, SEN_LVL_1_0); |
benkatz | 44:8040fa2fcb0d | 394 | wait_us(100); |
benkatz | 49:83d83040ea51 | 395 | drv.write_OCPCR(TRETRY_4MS, DEADTIME_200NS, OCP_RETRY, OCP_DEG_8US, VDS_LVL_1_88); |
benkatz | 45:26801179208e | 396 | |
benkatz | 45:26801179208e | 397 | //drv.enable_gd(); |
benkatz | 44:8040fa2fcb0d | 398 | zero_current(&controller.adc1_offset, &controller.adc2_offset); // Measure current sensor zero-offset |
benkatz | 47:e1196a851f76 | 399 | drv.disable_gd(); |
benkatz | 20:bf9ea5125d52 | 400 | |
benkatz | 9:d7eb815cb057 | 401 | wait(.1); |
benkatz | 44:8040fa2fcb0d | 402 | /* |
benkatz | 26:2b865c00d7e9 | 403 | gpio.enable->write(1); |
benkatz | 26:2b865c00d7e9 | 404 | TIM1->CCR3 = 0x708*(1.0f); // Write duty cycles |
benkatz | 26:2b865c00d7e9 | 405 | TIM1->CCR2 = 0x708*(1.0f); |
benkatz | 26:2b865c00d7e9 | 406 | TIM1->CCR1 = 0x708*(1.0f); |
benkatz | 26:2b865c00d7e9 | 407 | gpio.enable->write(0); |
benkatz | 44:8040fa2fcb0d | 408 | */ |
benkatz | 23:2adf23ee0305 | 409 | reset_foc(&controller); // Reset current controller |
benkatz | 48:74a40481740c | 410 | reset_observer(&observer); // Reset observer |
benkatz | 26:2b865c00d7e9 | 411 | TIM1->CR1 ^= TIM_CR1_UDIS; |
benkatz | 26:2b865c00d7e9 | 412 | //TIM1->CR1 |= TIM_CR1_UDIS; //enable interrupt |
benkatz | 20:bf9ea5125d52 | 413 | |
benkatz | 20:bf9ea5125d52 | 414 | wait(.1); |
benkatz | 37:c0f352d6e8e3 | 415 | NVIC_SetPriority(TIM1_UP_TIM10_IRQn, 2); // commutation > communication |
benkatz | 43:dfb72608639c | 416 | |
benkatz | 37:c0f352d6e8e3 | 417 | NVIC_SetPriority(CAN1_RX0_IRQn, 3); |
benkatz | 26:2b865c00d7e9 | 418 | can.filter(CAN_ID<<21, 0xFFE00004, CANStandard, 0); |
benkatz | 43:dfb72608639c | 419 | |
benkatz | 28:8c7e29f719c5 | 420 | txMsg.id = CAN_MASTER; |
benkatz | 28:8c7e29f719c5 | 421 | txMsg.len = 6; |
benkatz | 26:2b865c00d7e9 | 422 | rxMsg.len = 8; |
benkatz | 43:dfb72608639c | 423 | can.attach(&onMsgReceived); // attach 'CAN receive-complete' interrupt handler |
benkatz | 23:2adf23ee0305 | 424 | |
benkatz | 48:74a40481740c | 425 | // If preferences haven't been user configured yet, set defaults |
benkatz | 25:f5741040c4bb | 426 | prefs.load(); // Read flash |
benkatz | 37:c0f352d6e8e3 | 427 | if(isnan(E_OFFSET)){E_OFFSET = 0.0f;} |
benkatz | 37:c0f352d6e8e3 | 428 | if(isnan(M_OFFSET)){M_OFFSET = 0.0f;} |
benkatz | 48:74a40481740c | 429 | if(isnan(I_BW) || I_BW==-1){I_BW = 1000;} |
benkatz | 51:6cd89bd6fcaa | 430 | if(isnan(I_MAX) || I_MAX ==-1){I_MAX=40;} |
benkatz | 51:6cd89bd6fcaa | 431 | if(isnan(I_FW_MAX) || I_FW_MAX ==-1){I_FW_MAX=0;} |
benkatz | 48:74a40481740c | 432 | if(isnan(CAN_ID) || CAN_ID==-1){CAN_ID = 1;} |
benkatz | 48:74a40481740c | 433 | if(isnan(CAN_MASTER) || CAN_MASTER==-1){CAN_MASTER = 0;} |
benkatz | 48:74a40481740c | 434 | if(isnan(CAN_TIMEOUT) || CAN_TIMEOUT==-1){CAN_TIMEOUT = 0;} |
benkatz | 25:f5741040c4bb | 435 | spi.SetElecOffset(E_OFFSET); // Set position sensor offset |
benkatz | 37:c0f352d6e8e3 | 436 | spi.SetMechOffset(M_OFFSET); |
benkatz | 23:2adf23ee0305 | 437 | int lut[128] = {0}; |
benkatz | 23:2adf23ee0305 | 438 | memcpy(&lut, &ENCODER_LUT, sizeof(lut)); |
benkatz | 25:f5741040c4bb | 439 | spi.WriteLUT(lut); // Set potision sensor nonlinearity lookup table |
benkatz | 45:26801179208e | 440 | init_controller_params(&controller); |
benkatz | 45:26801179208e | 441 | |
benkatz | 26:2b865c00d7e9 | 442 | pc.baud(921600); // set serial baud rate |
bdring | 52:cf8b2abf811d | 443 | wait_us(100); |
bdring | 52:cf8b2abf811d | 444 | pc.printf("\n\r\n\r Hobby King Cheetah\n\r\n\r"); |
bdring | 52:cf8b2abf811d | 445 | wait_us(10); |
benkatz | 23:2adf23ee0305 | 446 | printf("\n\r Debug Info:\n\r"); |
benkatz | 32:ccac5da77844 | 447 | printf(" Firmware Version: %s\n\r", VERSION_NUM); |
benkatz | 23:2adf23ee0305 | 448 | printf(" ADC1 Offset: %d ADC2 Offset: %d\n\r", controller.adc1_offset, controller.adc2_offset); |
benkatz | 23:2adf23ee0305 | 449 | printf(" Position Sensor Electrical Offset: %.4f\n\r", E_OFFSET); |
benkatz | 37:c0f352d6e8e3 | 450 | printf(" Output Zero Position: %.4f\n\r", M_OFFSET); |
bdring | 52:cf8b2abf811d | 451 | printf(" Gear Ratio %.4f:1\r\n", GR); |
bdring | 52:cf8b2abf811d | 452 | printf(" Mapped Position %.4f to %.4f Radians\n\r", P_MIN, P_MAX); |
benkatz | 24:58c2d7571207 | 453 | printf(" CAN ID: %d\n\r", CAN_ID); |
benkatz | 44:8040fa2fcb0d | 454 | |
benkatz | 44:8040fa2fcb0d | 455 | |
benkatz | 44:8040fa2fcb0d | 456 | |
benkatz | 44:8040fa2fcb0d | 457 | |
benkatz | 47:e1196a851f76 | 458 | //printf(" %d\n\r", drv.read_register(DCR)); |
benkatz | 47:e1196a851f76 | 459 | //wait_us(100); |
benkatz | 47:e1196a851f76 | 460 | //printf(" %d\n\r", drv.read_register(CSACR)); |
benkatz | 47:e1196a851f76 | 461 | //wait_us(100); |
benkatz | 47:e1196a851f76 | 462 | //printf(" %d\n\r", drv.read_register(OCPCR)); |
benkatz | 47:e1196a851f76 | 463 | //drv.disable_gd(); |
benkatz | 44:8040fa2fcb0d | 464 | |
benkatz | 23:2adf23ee0305 | 465 | pc.attach(&serial_interrupt); // attach serial interrupt |
benkatz | 22:60276ba87ac6 | 466 | |
benkatz | 23:2adf23ee0305 | 467 | state_change = 1; |
benkatz | 50:ba72df25d10f | 468 | |
benkatz | 20:bf9ea5125d52 | 469 | |
benkatz | 44:8040fa2fcb0d | 470 | int counter = 0; |
benkatz | 0:4e1c4df6aabd | 471 | while(1) { |
benkatz | 47:e1196a851f76 | 472 | drv.print_faults(); |
benkatz | 50:ba72df25d10f | 473 | wait(.1); |
benkatz | 48:74a40481740c | 474 | //printf("%.4f\n\r", controller.v_bus); |
benkatz | 50:ba72df25d10f | 475 | /* |
benkatz | 47:e1196a851f76 | 476 | if(state == MOTOR_MODE) |
benkatz | 47:e1196a851f76 | 477 | { |
benkatz | 48:74a40481740c | 478 | //printf("%.3f %.3f %.3f\n\r", (float)observer.temperature, (float)observer.temperature2, observer.resistance); |
benkatz | 49:83d83040ea51 | 479 | //printf("%.3f %.3f %.3f %.3f %.3f\n\r", controller.v_d, controller.v_q, controller.i_d_filt, controller.i_q_filt, controller.dtheta_elec); |
benkatz | 49:83d83040ea51 | 480 | //printf("%.3f\n\r", controller.dtheta_mech); |
benkatz | 49:83d83040ea51 | 481 | wait(.002); |
benkatz | 47:e1196a851f76 | 482 | } |
benkatz | 50:ba72df25d10f | 483 | */ |
benkatz | 47:e1196a851f76 | 484 | |
benkatz | 0:4e1c4df6aabd | 485 | } |
benkatz | 0:4e1c4df6aabd | 486 | } |