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