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