Modified Motor Driver Firmware to include Flash + Thermal
Dependencies: FastPWM3 mbed-dev-STM-lean
main.cpp@72:3163691b0851, 23 months ago (annotated)
- Committer:
- adimmit
- Date:
- Sun Aug 28 18:01:26 2022 +0000
- Revision:
- 72:3163691b0851
- Parent:
- 71:b314182de37f
- Child:
- 73:5676f1232a81
fixed printing;
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 | 55:c4c9fec8539c | 7 | #define REST_MODE 0 |
benkatz | 55:c4c9fec8539c | 8 | #define CALIBRATION_MODE 1 |
benkatz | 55:c4c9fec8539c | 9 | #define MOTOR_MODE 2 |
benkatz | 55:c4c9fec8539c | 10 | #define SETUP_MODE 4 |
benkatz | 55:c4c9fec8539c | 11 | #define ENCODER_MODE 5 |
benkatz | 55:c4c9fec8539c | 12 | #define INIT_TEMP_MODE 6 |
benkatz | 22:60276ba87ac6 | 13 | |
benkatz | 55:c4c9fec8539c | 14 | #define VERSION_NUM "1.10" |
benkatz | 26:2b865c00d7e9 | 15 | |
benkatz | 18:f1d56f4acb39 | 16 | |
benkatz | 26:2b865c00d7e9 | 17 | float __float_reg[64]; // Floats stored in flash |
benkatz | 26:2b865c00d7e9 | 18 | int __int_reg[256]; // Ints stored in flash. Includes position sensor calibration lookup table |
benkatz | 17:3c5df2982199 | 19 | |
benkatz | 0:4e1c4df6aabd | 20 | #include "mbed.h" |
benkatz | 0:4e1c4df6aabd | 21 | #include "PositionSensor.h" |
benkatz | 20:bf9ea5125d52 | 22 | #include "structs.h" |
benkatz | 55:c4c9fec8539c | 23 | #include "foc.h" |
benkatz | 22:60276ba87ac6 | 24 | #include "calibration.h" |
benkatz | 20:bf9ea5125d52 | 25 | #include "hw_setup.h" |
benkatz | 23:2adf23ee0305 | 26 | #include "math_ops.h" |
benkatz | 20:bf9ea5125d52 | 27 | #include "current_controller_config.h" |
benkatz | 20:bf9ea5125d52 | 28 | #include "hw_config.h" |
elijahsj | 70:2ea6c555a2cb | 29 | #include "motor_config_U12.h" // need to choose between two files here, one for U10 and one for U12 |
benkatz | 23:2adf23ee0305 | 30 | #include "stm32f4xx_flash.h" |
benkatz | 23:2adf23ee0305 | 31 | #include "FlashWriter.h" |
benkatz | 23:2adf23ee0305 | 32 | #include "user_config.h" |
benkatz | 23:2adf23ee0305 | 33 | #include "PreferenceWriter.h" |
benkatz | 42:738fa01b0346 | 34 | #include "CAN_com.h" |
benkatz | 44:8040fa2fcb0d | 35 | #include "DRV.h" |
benkatz | 26:2b865c00d7e9 | 36 | |
benkatz | 23:2adf23ee0305 | 37 | PreferenceWriter prefs(6); |
benkatz | 9:d7eb815cb057 | 38 | |
benkatz | 20:bf9ea5125d52 | 39 | GPIOStruct gpio; |
benkatz | 20:bf9ea5125d52 | 40 | ControllerStruct controller; |
benkatz | 48:74a40481740c | 41 | ObserverStruct observer; |
benkatz | 20:bf9ea5125d52 | 42 | COMStruct com; |
benkatz | 43:dfb72608639c | 43 | Serial pc(PA_2, PA_3); |
benkatz | 9:d7eb815cb057 | 44 | |
benkatz | 17:3c5df2982199 | 45 | |
benkatz | 45:26801179208e | 46 | CAN can(PB_8, PB_9, 1000000); // CAN Rx pin name, CAN Tx pin name |
benkatz | 26:2b865c00d7e9 | 47 | CANMessage rxMsg; |
benkatz | 26:2b865c00d7e9 | 48 | CANMessage txMsg; |
benkatz | 23:2adf23ee0305 | 49 | |
benkatz | 20:bf9ea5125d52 | 50 | |
benkatz | 44:8040fa2fcb0d | 51 | SPI drv_spi(PA_7, PA_6, PA_5); |
benkatz | 44:8040fa2fcb0d | 52 | DigitalOut drv_cs(PA_4); |
benkatz | 44:8040fa2fcb0d | 53 | //DigitalOut drv_en_gate(PA_11); |
benkatz | 44:8040fa2fcb0d | 54 | DRV832x drv(&drv_spi, &drv_cs); |
benkatz | 8:10ae7bc88d6e | 55 | |
saloutos | 58:32e8927fe39f | 56 | //PositionSensoriCMU spi(16777216, 0.0, NPP); // Read 24 bits off the sensor |
saloutos | 58:32e8927fe39f | 57 | PositionSensoriCMU spi(524288, 0.0, NPP); // Read 24 bits off the sensor, only 19bits are encoder data |
benkatz | 20:bf9ea5125d52 | 58 | |
benkatz | 23:2adf23ee0305 | 59 | volatile int count = 0; |
benkatz | 23:2adf23ee0305 | 60 | volatile int state = REST_MODE; |
benkatz | 23:2adf23ee0305 | 61 | volatile int state_change; |
benkatz | 20:bf9ea5125d52 | 62 | |
benkatz | 26:2b865c00d7e9 | 63 | void onMsgReceived() { |
benkatz | 26:2b865c00d7e9 | 64 | //msgAvailable = true; |
benkatz | 53:e85efce8c1eb | 65 | //printf("%d\n\r", rxMsg.id); |
saloutos | 58:32e8927fe39f | 66 | gpio.led->write(1); |
benkatz | 26:2b865c00d7e9 | 67 | can.read(rxMsg); |
benkatz | 28:8c7e29f719c5 | 68 | if((rxMsg.id == CAN_ID)){ |
benkatz | 28:8c7e29f719c5 | 69 | controller.timeout = 0; |
benkatz | 28:8c7e29f719c5 | 70 | 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 | 71 | state = MOTOR_MODE; |
benkatz | 28:8c7e29f719c5 | 72 | state_change = 1; |
benkatz | 28:8c7e29f719c5 | 73 | } |
benkatz | 28:8c7e29f719c5 | 74 | 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 | 75 | state = REST_MODE; |
benkatz | 28:8c7e29f719c5 | 76 | state_change = 1; |
benkatz | 37:c0f352d6e8e3 | 77 | gpio.led->write(0);; |
benkatz | 28:8c7e29f719c5 | 78 | } |
benkatz | 28:8c7e29f719c5 | 79 | 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 | 80 | spi.ZeroPosition(); |
benkatz | 28:8c7e29f719c5 | 81 | } |
benkatz | 28:8c7e29f719c5 | 82 | else if(state == MOTOR_MODE){ |
benkatz | 28:8c7e29f719c5 | 83 | unpack_cmd(rxMsg, &controller); |
benkatz | 28:8c7e29f719c5 | 84 | } |
elijahsj | 68:edfdbd0db876 | 85 | pack_reply(&txMsg, controller.theta_mech, controller.dtheta_mech, controller.i_q_filt*KT_OUT); // multiply theta, dtheta_mech by GR here? |
elijahsj | 68:edfdbd0db876 | 86 | //pack_reply(&txMsg, controller.v_q, controller.dtheta_mech, controller.i_q_filt*KT_OUT); |
saloutos | 58:32e8927fe39f | 87 | //pack_reply(&txMsg, 3.14159f, 6.28318f, 1.00f); // test values |
benkatz | 37:c0f352d6e8e3 | 88 | can.write(txMsg); |
benkatz | 28:8c7e29f719c5 | 89 | } |
saloutos | 58:32e8927fe39f | 90 | gpio.led->write(0); |
benkatz | 26:2b865c00d7e9 | 91 | |
benkatz | 26:2b865c00d7e9 | 92 | } |
benkatz | 26:2b865c00d7e9 | 93 | |
benkatz | 23:2adf23ee0305 | 94 | void enter_menu_state(void){ |
benkatz | 44:8040fa2fcb0d | 95 | drv.disable_gd(); |
benkatz | 55:c4c9fec8539c | 96 | reset_foc(&controller); |
benkatz | 47:e1196a851f76 | 97 | //gpio.enable->write(0); |
benkatz | 23:2adf23ee0305 | 98 | printf("\n\r\n\r\n\r"); |
benkatz | 23:2adf23ee0305 | 99 | printf(" Commands:\n\r"); |
benkatz | 44:8040fa2fcb0d | 100 | wait_us(10); |
benkatz | 26:2b865c00d7e9 | 101 | printf(" m - Motor Mode\n\r"); |
benkatz | 44:8040fa2fcb0d | 102 | wait_us(10); |
benkatz | 23:2adf23ee0305 | 103 | printf(" c - Calibrate Encoder\n\r"); |
benkatz | 44:8040fa2fcb0d | 104 | wait_us(10); |
benkatz | 23:2adf23ee0305 | 105 | printf(" s - Setup\n\r"); |
benkatz | 44:8040fa2fcb0d | 106 | wait_us(10); |
benkatz | 23:2adf23ee0305 | 107 | printf(" e - Display Encoder\n\r"); |
benkatz | 44:8040fa2fcb0d | 108 | wait_us(10); |
benkatz | 37:c0f352d6e8e3 | 109 | printf(" z - Set Zero Position\n\r"); |
benkatz | 44:8040fa2fcb0d | 110 | wait_us(10); |
benkatz | 23:2adf23ee0305 | 111 | printf(" esc - Exit to Menu\n\r"); |
benkatz | 44:8040fa2fcb0d | 112 | wait_us(10); |
benkatz | 23:2adf23ee0305 | 113 | state_change = 0; |
benkatz | 37:c0f352d6e8e3 | 114 | gpio.led->write(0); |
benkatz | 23:2adf23ee0305 | 115 | } |
benkatz | 24:58c2d7571207 | 116 | |
benkatz | 24:58c2d7571207 | 117 | void enter_setup_state(void){ |
benkatz | 24:58c2d7571207 | 118 | printf("\n\r\n\r Configuration Options \n\r\n\n"); |
benkatz | 44:8040fa2fcb0d | 119 | wait_us(10); |
benkatz | 51:6cd89bd6fcaa | 120 | printf(" %-4s %-31s %-5s %-6s %-2s\n\r\n\r", "prefix", "parameter", "min", "max", "current value"); |
benkatz | 44:8040fa2fcb0d | 121 | wait_us(10); |
benkatz | 28:8c7e29f719c5 | 122 | printf(" %-4s %-31s %-5s %-6s %.1f\n\r", "b", "Current Bandwidth (Hz)", "100", "2000", I_BW); |
benkatz | 44:8040fa2fcb0d | 123 | wait_us(10); |
benkatz | 28:8c7e29f719c5 | 124 | printf(" %-4s %-31s %-5s %-6s %-5i\n\r", "i", "CAN ID", "0", "127", CAN_ID); |
benkatz | 44:8040fa2fcb0d | 125 | wait_us(10); |
benkatz | 28:8c7e29f719c5 | 126 | printf(" %-4s %-31s %-5s %-6s %-5i\n\r", "m", "CAN Master ID", "0", "127", CAN_MASTER); |
benkatz | 44:8040fa2fcb0d | 127 | wait_us(10); |
elijahsj | 66:3947255b18c5 | 128 | printf(" %-4s %-31s %-5s %-6s %.1f\n\r", "l", "Current Limit (A)", "0.0", "65.0", I_MAX); |
benkatz | 51:6cd89bd6fcaa | 129 | wait_us(10); |
benkatz | 51:6cd89bd6fcaa | 130 | printf(" %-4s %-31s %-5s %-6s %.1f\n\r", "f", "FW Current Limit (A)", "0.0", "33.0", I_FW_MAX); |
benkatz | 44:8040fa2fcb0d | 131 | wait_us(10); |
benkatz | 28:8c7e29f719c5 | 132 | printf(" %-4s %-31s %-5s %-6s %d\n\r", "t", "CAN Timeout (cycles)(0 = none)", "0", "100000", CAN_TIMEOUT); |
benkatz | 44:8040fa2fcb0d | 133 | wait_us(10); |
benkatz | 55:c4c9fec8539c | 134 | printf(" %-4s %-31s %-5s %-6s %.1f\n\r", "h", "Temp Cutoff (C) (0 = none)", "0", "150", TEMP_MAX); |
benkatz | 55:c4c9fec8539c | 135 | wait_us(10); |
benkatz | 55:c4c9fec8539c | 136 | printf(" %-4s %-31s %-5s %-6s %.1f\n\r", "c", "Continuous Current (A)", "0", "40.0", I_MAX_CONT); |
benkatz | 55:c4c9fec8539c | 137 | wait_us(10); |
adimmit | 72:3163691b0851 | 138 | printf(" %-4s %-31s %-5s %-6s %f\n\r", "s", "Current Controller K_SCALE", "0", "0.1", K_SCALE); //ADDED --> FOR CURRENT CONTROL GAINS (KP) |
adimmit | 71:b314182de37f | 139 | wait_us(10); |
adimmit | 72:3163691b0851 | 140 | printf(" %-4s %-31s %-5s %-6s %f\n\r", "d", "Current Controller KI_D", "0", "1.0", KI_D); //ADDED --> FOR CURRENT CONTROL GAINS (KI D-Axis) |
adimmit | 71:b314182de37f | 141 | wait_us(10); |
adimmit | 72:3163691b0851 | 142 | printf(" %-4s %-31s %-5s %-6s %f\n\r", "q", "Current Controller KI_Q", "0", "1.0", KI_Q); //ADDED --> FOR CURRENT CONTROL GAINS (KI Q-Axis) |
adimmit | 71:b314182de37f | 143 | wait_us(10); |
benkatz | 24:58c2d7571207 | 144 | printf("\n\r To change a value, type 'prefix''value''ENTER'\n\r i.e. 'b1000''ENTER'\n\r\n\r"); |
benkatz | 44:8040fa2fcb0d | 145 | wait_us(10); |
benkatz | 24:58c2d7571207 | 146 | state_change = 0; |
benkatz | 24:58c2d7571207 | 147 | } |
benkatz | 22:60276ba87ac6 | 148 | |
benkatz | 23:2adf23ee0305 | 149 | void enter_torque_mode(void){ |
benkatz | 44:8040fa2fcb0d | 150 | drv.enable_gd(); |
benkatz | 47:e1196a851f76 | 151 | //gpio.enable->write(1); |
benkatz | 37:c0f352d6e8e3 | 152 | controller.ovp_flag = 0; |
benkatz | 28:8c7e29f719c5 | 153 | reset_foc(&controller); // Tesets integrators, and other control loop parameters |
benkatz | 28:8c7e29f719c5 | 154 | wait(.001); |
benkatz | 23:2adf23ee0305 | 155 | controller.i_d_ref = 0; |
benkatz | 50:ba72df25d10f | 156 | controller.i_q_ref = 0; // Current Setpoints |
benkatz | 37:c0f352d6e8e3 | 157 | gpio.led->write(1); // Turn on status LED |
benkatz | 25:f5741040c4bb | 158 | state_change = 0; |
benkatz | 28:8c7e29f719c5 | 159 | printf("\n\r Entering Motor Mode \n\r"); |
benkatz | 23:2adf23ee0305 | 160 | } |
benkatz | 22:60276ba87ac6 | 161 | |
benkatz | 23:2adf23ee0305 | 162 | void calibrate(void){ |
benkatz | 44:8040fa2fcb0d | 163 | drv.enable_gd(); |
benkatz | 47:e1196a851f76 | 164 | //gpio.enable->write(1); |
benkatz | 37:c0f352d6e8e3 | 165 | gpio.led->write(1); // Turn on status LED |
benkatz | 25:f5741040c4bb | 166 | order_phases(&spi, &gpio, &controller, &prefs); // Check phase ordering |
benkatz | 25:f5741040c4bb | 167 | calibrate(&spi, &gpio, &controller, &prefs); // Perform calibration procedure |
benkatz | 37:c0f352d6e8e3 | 168 | gpio.led->write(0);; // Turn off status LED |
benkatz | 55:c4c9fec8539c | 169 | wait(.05); |
benkatz | 55:c4c9fec8539c | 170 | R_NOMINAL = 0; |
benkatz | 55:c4c9fec8539c | 171 | state = INIT_TEMP_MODE; |
saloutos | 58:32e8927fe39f | 172 | printf("\n\r Calibration complete. Press 'esc' to return to menu\n\r"); |
benkatz | 55:c4c9fec8539c | 173 | //drv.disable_gd(); |
saloutos | 58:32e8927fe39f | 174 | state_change = 0; |
benkatz | 55:c4c9fec8539c | 175 | |
benkatz | 23:2adf23ee0305 | 176 | } |
benkatz | 23:2adf23ee0305 | 177 | |
benkatz | 23:2adf23ee0305 | 178 | void print_encoder(void){ |
benkatz | 48:74a40481740c | 179 | printf(" Mechanical Angle: %f Electrical Angle: %f Raw: %d\n\r", spi.GetMechPosition(), spi.GetElecPosition(), spi.GetRawPosition()); |
benkatz | 48:74a40481740c | 180 | //printf("%d\n\r", spi.GetRawPosition()); |
benkatz | 47:e1196a851f76 | 181 | wait(.001); |
benkatz | 22:60276ba87ac6 | 182 | } |
benkatz | 20:bf9ea5125d52 | 183 | |
benkatz | 23:2adf23ee0305 | 184 | /// Current Sampling Interrupt /// |
benkatz | 23:2adf23ee0305 | 185 | /// This runs at 40 kHz, regardless of of the mode the controller is in /// |
benkatz | 55:c4c9fec8539c | 186 | //float testing[1000]; |
benkatz | 55:c4c9fec8539c | 187 | //float testing2[1000]; |
benkatz | 2:8724412ad628 | 188 | extern "C" void TIM1_UP_TIM10_IRQHandler(void) { |
benkatz | 2:8724412ad628 | 189 | if (TIM1->SR & TIM_SR_UIF ) { |
benkatz | 55:c4c9fec8539c | 190 | //gpio.led->write(1); |
benkatz | 23:2adf23ee0305 | 191 | ///Sample current always /// |
benkatz | 25:f5741040c4bb | 192 | ADC1->CR2 |= 0x40000000; // Begin sample and conversion |
benkatz | 22:60276ba87ac6 | 193 | //volatile int delay; |
benkatz | 20:bf9ea5125d52 | 194 | //for (delay = 0; delay < 55; delay++); |
benkatz | 55:c4c9fec8539c | 195 | |
benkatz | 47:e1196a851f76 | 196 | spi.Sample(DT); // sample position sensor |
benkatz | 56:fe5056ac6740 | 197 | /* |
benkatz | 56:fe5056ac6740 | 198 | if(count < 10){printf("%d\n\r", spi.GetRawPosition());} |
benkatz | 56:fe5056ac6740 | 199 | count ++; |
benkatz | 56:fe5056ac6740 | 200 | */ |
benkatz | 37:c0f352d6e8e3 | 201 | controller.adc2_raw = ADC2->DR; // Read ADC Data Registers |
benkatz | 23:2adf23ee0305 | 202 | controller.adc1_raw = ADC1->DR; |
benkatz | 37:c0f352d6e8e3 | 203 | controller.adc3_raw = ADC3->DR; |
benkatz | 37:c0f352d6e8e3 | 204 | controller.theta_elec = spi.GetElecPosition(); |
saloutos | 58:32e8927fe39f | 205 | controller.theta_mech = (1.0f/GR)*spi.GetMechPosition(); // should these be divided by gear ratio??? |
saloutos | 58:32e8927fe39f | 206 | controller.dtheta_mech = (1.0f/GR)*spi.GetMechVelocity(); // mech pos isn't pre-multiplied by GR in position sensor function... |
benkatz | 37:c0f352d6e8e3 | 207 | controller.dtheta_elec = spi.GetElecVelocity(); |
benkatz | 51:6cd89bd6fcaa | 208 | controller.v_bus = 0.95f*controller.v_bus + 0.05f*((float)controller.adc3_raw)*V_SCALE; //filter the dc link voltage measurement |
benkatz | 55:c4c9fec8539c | 209 | |
benkatz | 55:c4c9fec8539c | 210 | |
benkatz | 55:c4c9fec8539c | 211 | |
benkatz | 23:2adf23ee0305 | 212 | /// |
benkatz | 20:bf9ea5125d52 | 213 | |
benkatz | 23:2adf23ee0305 | 214 | /// Check state machine state, and run the appropriate function /// |
benkatz | 23:2adf23ee0305 | 215 | switch(state){ |
benkatz | 37:c0f352d6e8e3 | 216 | case REST_MODE: // Do nothing |
benkatz | 23:2adf23ee0305 | 217 | if(state_change){ |
benkatz | 23:2adf23ee0305 | 218 | enter_menu_state(); |
benkatz | 23:2adf23ee0305 | 219 | } |
benkatz | 55:c4c9fec8539c | 220 | update_observer(&controller, &observer); |
benkatz | 23:2adf23ee0305 | 221 | break; |
benkatz | 22:60276ba87ac6 | 222 | |
benkatz | 23:2adf23ee0305 | 223 | case CALIBRATION_MODE: // Run encoder calibration procedure |
benkatz | 23:2adf23ee0305 | 224 | if(state_change){ |
benkatz | 23:2adf23ee0305 | 225 | calibrate(); |
benkatz | 23:2adf23ee0305 | 226 | } |
benkatz | 23:2adf23ee0305 | 227 | break; |
benkatz | 55:c4c9fec8539c | 228 | case INIT_TEMP_MODE: |
benkatz | 55:c4c9fec8539c | 229 | if(state_change){ |
benkatz | 55:c4c9fec8539c | 230 | enter_torque_mode(); |
benkatz | 55:c4c9fec8539c | 231 | count = 0; |
benkatz | 55:c4c9fec8539c | 232 | observer.resistance = 0.0f; |
benkatz | 55:c4c9fec8539c | 233 | } |
benkatz | 55:c4c9fec8539c | 234 | controller.i_d_ref = -10.0f; |
benkatz | 55:c4c9fec8539c | 235 | controller.i_q_ref = 0.0f; |
benkatz | 55:c4c9fec8539c | 236 | commutate(&controller, &observer, &gpio, controller.theta_elec); |
benkatz | 55:c4c9fec8539c | 237 | |
benkatz | 55:c4c9fec8539c | 238 | if(count > 200) |
benkatz | 55:c4c9fec8539c | 239 | { |
benkatz | 55:c4c9fec8539c | 240 | float r_meas = controller.v_d*(DTC_MAX-DTC_MIN)/(controller.i_d*SQRT3); |
benkatz | 55:c4c9fec8539c | 241 | //testing2[count-100] = controller.i_d; |
benkatz | 55:c4c9fec8539c | 242 | observer.resistance += .001f*r_meas; |
benkatz | 55:c4c9fec8539c | 243 | } |
benkatz | 55:c4c9fec8539c | 244 | if(count > 1200) |
benkatz | 55:c4c9fec8539c | 245 | { |
benkatz | 55:c4c9fec8539c | 246 | count = 0; |
benkatz | 55:c4c9fec8539c | 247 | state = REST_MODE; |
benkatz | 55:c4c9fec8539c | 248 | state_change = 1; |
benkatz | 55:c4c9fec8539c | 249 | gpio.led->write(0); |
benkatz | 55:c4c9fec8539c | 250 | observer.temperature = (double)(T_AMBIENT + ((observer.resistance/R_NOMINAL) - 1.0f)*254.5f); |
benkatz | 55:c4c9fec8539c | 251 | printf("Winding Resistance: %f\n\r", observer.resistance); |
benkatz | 55:c4c9fec8539c | 252 | printf("Winding Temperature: %f\n\r", observer.temperature); |
benkatz | 55:c4c9fec8539c | 253 | |
benkatz | 55:c4c9fec8539c | 254 | if(R_NOMINAL==0) |
benkatz | 55:c4c9fec8539c | 255 | { |
benkatz | 55:c4c9fec8539c | 256 | printf("Saving winding resistance\n\r"); |
benkatz | 55:c4c9fec8539c | 257 | R_NOMINAL = observer.resistance; |
benkatz | 55:c4c9fec8539c | 258 | if (!prefs.ready()) prefs.open(); |
benkatz | 55:c4c9fec8539c | 259 | prefs.flush(); // write offset and lookup table to flash |
benkatz | 55:c4c9fec8539c | 260 | prefs.close(); |
benkatz | 55:c4c9fec8539c | 261 | } |
benkatz | 55:c4c9fec8539c | 262 | //for(int i = 0; i<1000; i++){printf("%f \n\r", testing[i]);} |
benkatz | 55:c4c9fec8539c | 263 | } |
benkatz | 55:c4c9fec8539c | 264 | |
benkatz | 55:c4c9fec8539c | 265 | count++; |
benkatz | 55:c4c9fec8539c | 266 | break; |
benkatz | 26:2b865c00d7e9 | 267 | case MOTOR_MODE: // Run torque control |
benkatz | 25:f5741040c4bb | 268 | if(state_change){ |
benkatz | 25:f5741040c4bb | 269 | enter_torque_mode(); |
benkatz | 28:8c7e29f719c5 | 270 | count = 0; |
benkatz | 25:f5741040c4bb | 271 | } |
benkatz | 28:8c7e29f719c5 | 272 | else{ |
benkatz | 37:c0f352d6e8e3 | 273 | /* |
benkatz | 37:c0f352d6e8e3 | 274 | 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 | 275 | gpio. |
benkatz | 47:e1196a851f76 | 276 | ->write(0); |
benkatz | 37:c0f352d6e8e3 | 277 | controller.ovp_flag = 1; |
benkatz | 37:c0f352d6e8e3 | 278 | state = REST_MODE; |
benkatz | 37:c0f352d6e8e3 | 279 | state_change = 1; |
benkatz | 37:c0f352d6e8e3 | 280 | printf("OVP Triggered!\n\r"); |
benkatz | 37:c0f352d6e8e3 | 281 | } |
benkatz | 37:c0f352d6e8e3 | 282 | */ |
benkatz | 37:c0f352d6e8e3 | 283 | |
benkatz | 28:8c7e29f719c5 | 284 | if((controller.timeout > CAN_TIMEOUT) && (CAN_TIMEOUT > 0)){ |
benkatz | 28:8c7e29f719c5 | 285 | controller.i_d_ref = 0; |
benkatz | 28:8c7e29f719c5 | 286 | controller.i_q_ref = 0; |
benkatz | 37:c0f352d6e8e3 | 287 | controller.kp = 0; |
benkatz | 37:c0f352d6e8e3 | 288 | controller.kd = 0; |
benkatz | 37:c0f352d6e8e3 | 289 | controller.t_ff = 0; |
benkatz | 28:8c7e29f719c5 | 290 | } |
benkatz | 55:c4c9fec8539c | 291 | |
benkatz | 50:ba72df25d10f | 292 | torque_control(&controller); |
benkatz | 55:c4c9fec8539c | 293 | update_observer(&controller, &observer); |
benkatz | 55:c4c9fec8539c | 294 | field_weaken(&controller); |
benkatz | 49:83d83040ea51 | 295 | commutate(&controller, &observer, &gpio, controller.theta_elec); // Run current loop |
benkatz | 49:83d83040ea51 | 296 | controller.timeout++; |
benkatz | 55:c4c9fec8539c | 297 | |
benkatz | 55:c4c9fec8539c | 298 | if(controller.otw_flag) |
benkatz | 55:c4c9fec8539c | 299 | { |
benkatz | 55:c4c9fec8539c | 300 | state = REST_MODE; |
benkatz | 55:c4c9fec8539c | 301 | state_change = 1; |
benkatz | 55:c4c9fec8539c | 302 | gpio.led->write(0); |
benkatz | 55:c4c9fec8539c | 303 | } |
benkatz | 55:c4c9fec8539c | 304 | |
benkatz | 49:83d83040ea51 | 305 | count++; |
benkatz | 55:c4c9fec8539c | 306 | } |
benkatz | 55:c4c9fec8539c | 307 | |
benkatz | 55:c4c9fec8539c | 308 | |
benkatz | 55:c4c9fec8539c | 309 | break; |
benkatz | 37:c0f352d6e8e3 | 310 | |
benkatz | 23:2adf23ee0305 | 311 | case SETUP_MODE: |
benkatz | 23:2adf23ee0305 | 312 | if(state_change){ |
benkatz | 24:58c2d7571207 | 313 | enter_setup_state(); |
benkatz | 23:2adf23ee0305 | 314 | } |
benkatz | 23:2adf23ee0305 | 315 | break; |
benkatz | 23:2adf23ee0305 | 316 | case ENCODER_MODE: |
benkatz | 23:2adf23ee0305 | 317 | print_encoder(); |
benkatz | 23:2adf23ee0305 | 318 | break; |
benkatz | 37:c0f352d6e8e3 | 319 | } |
benkatz | 2:8724412ad628 | 320 | } |
benkatz | 55:c4c9fec8539c | 321 | //gpio.led->write(0); |
benkatz | 23:2adf23ee0305 | 322 | TIM1->SR = 0x0; // reset the status register |
benkatz | 2:8724412ad628 | 323 | } |
benkatz | 0:4e1c4df6aabd | 324 | |
benkatz | 25:f5741040c4bb | 325 | |
benkatz | 24:58c2d7571207 | 326 | char cmd_val[8] = {0}; |
benkatz | 24:58c2d7571207 | 327 | char cmd_id = 0; |
benkatz | 25:f5741040c4bb | 328 | char char_count = 0; |
benkatz | 24:58c2d7571207 | 329 | |
benkatz | 25:f5741040c4bb | 330 | /// Manage state machine with commands from serial terminal or configurator gui /// |
benkatz | 25:f5741040c4bb | 331 | /// Called when data received over serial /// |
benkatz | 23:2adf23ee0305 | 332 | void serial_interrupt(void){ |
benkatz | 23:2adf23ee0305 | 333 | while(pc.readable()){ |
benkatz | 23:2adf23ee0305 | 334 | char c = pc.getc(); |
benkatz | 25:f5741040c4bb | 335 | if(c == 27){ |
benkatz | 25:f5741040c4bb | 336 | state = REST_MODE; |
benkatz | 25:f5741040c4bb | 337 | state_change = 1; |
benkatz | 25:f5741040c4bb | 338 | char_count = 0; |
benkatz | 25:f5741040c4bb | 339 | cmd_id = 0; |
benkatz | 55:c4c9fec8539c | 340 | gpio.led->write(0);; |
benkatz | 25:f5741040c4bb | 341 | for(int i = 0; i<8; i++){cmd_val[i] = 0;} |
benkatz | 25:f5741040c4bb | 342 | } |
benkatz | 24:58c2d7571207 | 343 | if(state == REST_MODE){ |
benkatz | 23:2adf23ee0305 | 344 | switch (c){ |
benkatz | 23:2adf23ee0305 | 345 | case 'c': |
benkatz | 23:2adf23ee0305 | 346 | state = CALIBRATION_MODE; |
benkatz | 23:2adf23ee0305 | 347 | state_change = 1; |
benkatz | 23:2adf23ee0305 | 348 | break; |
benkatz | 26:2b865c00d7e9 | 349 | case 'm': |
benkatz | 26:2b865c00d7e9 | 350 | state = MOTOR_MODE; |
benkatz | 23:2adf23ee0305 | 351 | state_change = 1; |
benkatz | 23:2adf23ee0305 | 352 | break; |
benkatz | 23:2adf23ee0305 | 353 | case 'e': |
benkatz | 23:2adf23ee0305 | 354 | state = ENCODER_MODE; |
benkatz | 23:2adf23ee0305 | 355 | state_change = 1; |
benkatz | 23:2adf23ee0305 | 356 | break; |
benkatz | 23:2adf23ee0305 | 357 | case 's': |
benkatz | 23:2adf23ee0305 | 358 | state = SETUP_MODE; |
benkatz | 23:2adf23ee0305 | 359 | state_change = 1; |
benkatz | 23:2adf23ee0305 | 360 | break; |
benkatz | 37:c0f352d6e8e3 | 361 | case 'z': |
benkatz | 37:c0f352d6e8e3 | 362 | spi.SetMechOffset(0); |
benkatz | 47:e1196a851f76 | 363 | spi.Sample(DT); |
benkatz | 37:c0f352d6e8e3 | 364 | wait_us(20); |
benkatz | 37:c0f352d6e8e3 | 365 | M_OFFSET = spi.GetMechPosition(); |
benkatz | 37:c0f352d6e8e3 | 366 | if (!prefs.ready()) prefs.open(); |
benkatz | 37:c0f352d6e8e3 | 367 | prefs.flush(); // Write new prefs to flash |
benkatz | 37:c0f352d6e8e3 | 368 | prefs.close(); |
benkatz | 37:c0f352d6e8e3 | 369 | prefs.load(); |
benkatz | 37:c0f352d6e8e3 | 370 | spi.SetMechOffset(M_OFFSET); |
benkatz | 37:c0f352d6e8e3 | 371 | printf("\n\r Saved new zero position: %.4f\n\r\n\r", M_OFFSET); |
benkatz | 37:c0f352d6e8e3 | 372 | |
benkatz | 37:c0f352d6e8e3 | 373 | break; |
benkatz | 37:c0f352d6e8e3 | 374 | } |
benkatz | 37:c0f352d6e8e3 | 375 | |
benkatz | 24:58c2d7571207 | 376 | } |
benkatz | 24:58c2d7571207 | 377 | else if(state == SETUP_MODE){ |
benkatz | 25:f5741040c4bb | 378 | if(c == 13){ |
benkatz | 24:58c2d7571207 | 379 | switch (cmd_id){ |
benkatz | 24:58c2d7571207 | 380 | case 'b': |
benkatz | 24:58c2d7571207 | 381 | I_BW = fmaxf(fminf(atof(cmd_val), 2000.0f), 100.0f); |
benkatz | 24:58c2d7571207 | 382 | break; |
benkatz | 24:58c2d7571207 | 383 | case 'i': |
benkatz | 24:58c2d7571207 | 384 | CAN_ID = atoi(cmd_val); |
benkatz | 24:58c2d7571207 | 385 | break; |
benkatz | 26:2b865c00d7e9 | 386 | case 'm': |
benkatz | 26:2b865c00d7e9 | 387 | CAN_MASTER = atoi(cmd_val); |
benkatz | 26:2b865c00d7e9 | 388 | break; |
benkatz | 24:58c2d7571207 | 389 | case 'l': |
elijahsj | 66:3947255b18c5 | 390 | I_MAX = fmaxf(fminf(atof(cmd_val), 65.0f), 0.0f); |
benkatz | 51:6cd89bd6fcaa | 391 | break; |
benkatz | 51:6cd89bd6fcaa | 392 | case 'f': |
benkatz | 51:6cd89bd6fcaa | 393 | I_FW_MAX = fmaxf(fminf(atof(cmd_val), 33.0f), 0.0f); |
benkatz | 24:58c2d7571207 | 394 | break; |
benkatz | 28:8c7e29f719c5 | 395 | case 't': |
benkatz | 28:8c7e29f719c5 | 396 | CAN_TIMEOUT = atoi(cmd_val); |
benkatz | 28:8c7e29f719c5 | 397 | break; |
benkatz | 55:c4c9fec8539c | 398 | case 'h': |
benkatz | 55:c4c9fec8539c | 399 | TEMP_MAX = fmaxf(fminf(atof(cmd_val), 150.0f), 0.0f); |
benkatz | 55:c4c9fec8539c | 400 | break; |
benkatz | 55:c4c9fec8539c | 401 | case 'c': |
benkatz | 55:c4c9fec8539c | 402 | I_MAX_CONT = fmaxf(fminf(atof(cmd_val), 40.0f), 0.0f); |
benkatz | 55:c4c9fec8539c | 403 | break; |
adimmit | 71:b314182de37f | 404 | case 's': //CURRENT CONTROL K_SCALE |
adimmit | 71:b314182de37f | 405 | K_SCALE = fmaxf(fminf(atof(cmd_val), 0.1f), 0.0f); |
adimmit | 71:b314182de37f | 406 | break; |
adimmit | 71:b314182de37f | 407 | case 'd': //CURRENT CONTORL KI_D |
adimmit | 71:b314182de37f | 408 | KI_D = fmaxf(fminf(atof(cmd_val), 1.0f), 0.0f); |
adimmit | 71:b314182de37f | 409 | break; |
adimmit | 71:b314182de37f | 410 | case 'q': //CURRENT CONTORL KI_Q |
adimmit | 71:b314182de37f | 411 | KI_Q = fmaxf(fminf(atof(cmd_val), 1.0f), 0.0f); |
adimmit | 71:b314182de37f | 412 | break; |
benkatz | 24:58c2d7571207 | 413 | default: |
benkatz | 24:58c2d7571207 | 414 | printf("\n\r '%c' Not a valid command prefix\n\r\n\r", cmd_id); |
benkatz | 24:58c2d7571207 | 415 | break; |
benkatz | 24:58c2d7571207 | 416 | } |
benkatz | 24:58c2d7571207 | 417 | |
benkatz | 24:58c2d7571207 | 418 | if (!prefs.ready()) prefs.open(); |
benkatz | 24:58c2d7571207 | 419 | prefs.flush(); // Write new prefs to flash |
benkatz | 24:58c2d7571207 | 420 | prefs.close(); |
benkatz | 24:58c2d7571207 | 421 | prefs.load(); |
benkatz | 24:58c2d7571207 | 422 | state_change = 1; |
benkatz | 24:58c2d7571207 | 423 | char_count = 0; |
benkatz | 24:58c2d7571207 | 424 | cmd_id = 0; |
benkatz | 24:58c2d7571207 | 425 | for(int i = 0; i<8; i++){cmd_val[i] = 0;} |
benkatz | 24:58c2d7571207 | 426 | } |
benkatz | 24:58c2d7571207 | 427 | else{ |
benkatz | 24:58c2d7571207 | 428 | if(char_count == 0){cmd_id = c;} |
benkatz | 24:58c2d7571207 | 429 | else{ |
benkatz | 24:58c2d7571207 | 430 | cmd_val[char_count-1] = c; |
benkatz | 24:58c2d7571207 | 431 | |
benkatz | 24:58c2d7571207 | 432 | } |
benkatz | 24:58c2d7571207 | 433 | pc.putc(c); |
benkatz | 24:58c2d7571207 | 434 | char_count++; |
benkatz | 23:2adf23ee0305 | 435 | } |
benkatz | 23:2adf23ee0305 | 436 | } |
benkatz | 24:58c2d7571207 | 437 | else if (state == ENCODER_MODE){ |
benkatz | 24:58c2d7571207 | 438 | switch (c){ |
benkatz | 24:58c2d7571207 | 439 | case 27: |
benkatz | 24:58c2d7571207 | 440 | state = REST_MODE; |
benkatz | 24:58c2d7571207 | 441 | state_change = 1; |
benkatz | 24:58c2d7571207 | 442 | break; |
benkatz | 24:58c2d7571207 | 443 | } |
benkatz | 24:58c2d7571207 | 444 | } |
benkatz | 49:83d83040ea51 | 445 | else if (state == MOTOR_MODE){ |
benkatz | 49:83d83040ea51 | 446 | switch (c){ |
benkatz | 49:83d83040ea51 | 447 | case 'd': |
benkatz | 49:83d83040ea51 | 448 | controller.i_q_ref = 0; |
benkatz | 49:83d83040ea51 | 449 | controller.i_d_ref = 0; |
benkatz | 49:83d83040ea51 | 450 | } |
benkatz | 49:83d83040ea51 | 451 | } |
benkatz | 24:58c2d7571207 | 452 | |
benkatz | 24:58c2d7571207 | 453 | } |
benkatz | 22:60276ba87ac6 | 454 | } |
benkatz | 0:4e1c4df6aabd | 455 | |
benkatz | 0:4e1c4df6aabd | 456 | int main() { |
benkatz | 20:bf9ea5125d52 | 457 | controller.v_bus = V_BUS; |
benkatz | 22:60276ba87ac6 | 458 | controller.mode = 0; |
benkatz | 23:2adf23ee0305 | 459 | Init_All_HW(&gpio); // Setup PWM, ADC, GPIO |
benkatz | 55:c4c9fec8539c | 460 | wait_us(100); |
benkatz | 44:8040fa2fcb0d | 461 | |
benkatz | 44:8040fa2fcb0d | 462 | gpio.enable->write(1); |
benkatz | 44:8040fa2fcb0d | 463 | wait_us(100); |
benkatz | 45:26801179208e | 464 | drv.calibrate(); |
benkatz | 45:26801179208e | 465 | wait_us(100); |
benkatz | 55:c4c9fec8539c | 466 | drv.write_DCR(0x0, DIS_GDF_DIS, 0x0, PWM_MODE_3X, 0x0, 0x0, 0x0, 0x0, 0x1); |
benkatz | 55:c4c9fec8539c | 467 | wait_us(100); |
benkatz | 55:c4c9fec8539c | 468 | drv.write_CSACR(0x0, 0x1, 0x0, CSA_GAIN_40, 0x0, 0x1, 0x1, 0x1, SEN_LVL_1_0); // calibrate shunt amplifiers |
benkatz | 44:8040fa2fcb0d | 469 | wait_us(100); |
benkatz | 55:c4c9fec8539c | 470 | zero_current(&controller.adc1_offset, &controller.adc2_offset); |
benkatz | 44:8040fa2fcb0d | 471 | wait_us(100); |
benkatz | 55:c4c9fec8539c | 472 | drv.write_CSACR(0x0, 0x1, 0x0, CSA_GAIN_40, 0x1, 0x0, 0x0, 0x0, SEN_LVL_1_0); |
benkatz | 55:c4c9fec8539c | 473 | wait_us(100); |
benkatz | 55:c4c9fec8539c | 474 | drv.write_OCPCR(TRETRY_50US, DEADTIME_50NS, OCP_NONE, OCP_DEG_8US, VDS_LVL_1_88); |
benkatz | 45:26801179208e | 475 | |
benkatz | 45:26801179208e | 476 | //drv.enable_gd(); |
benkatz | 47:e1196a851f76 | 477 | drv.disable_gd(); |
benkatz | 55:c4c9fec8539c | 478 | //zero_current(&controller.adc1_offset, &controller.adc2_offset); // Measure current sensor zero-offset |
benkatz | 55:c4c9fec8539c | 479 | //drv.enable_gd(); |
benkatz | 20:bf9ea5125d52 | 480 | |
benkatz | 55:c4c9fec8539c | 481 | wait_us(100); |
benkatz | 55:c4c9fec8539c | 482 | |
benkatz | 23:2adf23ee0305 | 483 | reset_foc(&controller); // Reset current controller |
benkatz | 48:74a40481740c | 484 | reset_observer(&observer); // Reset observer |
benkatz | 26:2b865c00d7e9 | 485 | //TIM1->CR1 |= TIM_CR1_UDIS; //enable interrupt |
benkatz | 20:bf9ea5125d52 | 486 | |
benkatz | 55:c4c9fec8539c | 487 | wait_us(100); |
benkatz | 37:c0f352d6e8e3 | 488 | NVIC_SetPriority(TIM1_UP_TIM10_IRQn, 2); // commutation > communication |
benkatz | 37:c0f352d6e8e3 | 489 | NVIC_SetPriority(CAN1_RX0_IRQn, 3); |
benkatz | 53:e85efce8c1eb | 490 | // attach 'CAN receive-complete' interrupt handler |
benkatz | 53:e85efce8c1eb | 491 | |
benkatz | 53:e85efce8c1eb | 492 | // If preferences haven't been user configured yet, set defaults |
benkatz | 53:e85efce8c1eb | 493 | prefs.load(); // Read flash |
benkatz | 55:c4c9fec8539c | 494 | can.filter(CAN_ID , 0xFFF, CANStandard, 0); |
benkatz | 28:8c7e29f719c5 | 495 | txMsg.id = CAN_MASTER; |
benkatz | 28:8c7e29f719c5 | 496 | txMsg.len = 6; |
benkatz | 26:2b865c00d7e9 | 497 | rxMsg.len = 8; |
benkatz | 53:e85efce8c1eb | 498 | can.attach(&onMsgReceived); |
benkatz | 23:2adf23ee0305 | 499 | |
benkatz | 37:c0f352d6e8e3 | 500 | if(isnan(E_OFFSET)){E_OFFSET = 0.0f;} |
benkatz | 37:c0f352d6e8e3 | 501 | if(isnan(M_OFFSET)){M_OFFSET = 0.0f;} |
benkatz | 48:74a40481740c | 502 | if(isnan(I_BW) || I_BW==-1){I_BW = 1000;} |
benkatz | 51:6cd89bd6fcaa | 503 | if(isnan(I_MAX) || I_MAX ==-1){I_MAX=40;} |
benkatz | 55:c4c9fec8539c | 504 | if(isnan(I_FW_MAX) || I_FW_MAX ==-1){I_FW_MAX=12;} |
benkatz | 48:74a40481740c | 505 | if(isnan(CAN_ID) || CAN_ID==-1){CAN_ID = 1;} |
benkatz | 48:74a40481740c | 506 | if(isnan(CAN_MASTER) || CAN_MASTER==-1){CAN_MASTER = 0;} |
benkatz | 55:c4c9fec8539c | 507 | if(isnan(CAN_TIMEOUT) || CAN_TIMEOUT==-1){CAN_TIMEOUT = 1000;} |
benkatz | 55:c4c9fec8539c | 508 | if(isnan(R_NOMINAL) || R_NOMINAL==-1){R_NOMINAL = 0.0f;} |
benkatz | 55:c4c9fec8539c | 509 | if(isnan(TEMP_MAX) || TEMP_MAX==-1){TEMP_MAX = 125.0f;} |
benkatz | 55:c4c9fec8539c | 510 | if(isnan(I_MAX_CONT) || I_MAX_CONT==-1){I_MAX_CONT = 14.0f;} |
adimmit | 71:b314182de37f | 511 | if(isnan(K_SCALE) || K_SCALE==-1){K_SCALE = 0.00013310f;} //CURRENT CONTROLLER K_SCALE |
adimmit | 71:b314182de37f | 512 | if(isnan(KI_D) || KI_D==-1){KI_D = 0.0373f;} //CURRENT CONTROLLER KI_D |
adimmit | 71:b314182de37f | 513 | if(isnan(KI_Q) || KI_Q==-1){KI_Q = 0.0373f;} //CURRENT CONTROLLER KI_Q |
benkatz | 25:f5741040c4bb | 514 | spi.SetElecOffset(E_OFFSET); // Set position sensor offset |
benkatz | 37:c0f352d6e8e3 | 515 | spi.SetMechOffset(M_OFFSET); |
benkatz | 56:fe5056ac6740 | 516 | spi.Sample(1.0f); |
benkatz | 56:fe5056ac6740 | 517 | if(spi.GetMechPosition() > PI){spi.SetMechOffset(M_OFFSET+2.0f*PI);} // now zeroes to +- 30 degrees about nominal, independent of rollover point |
benkatz | 56:fe5056ac6740 | 518 | else if (spi.GetMechPosition() < -PI){spi.SetMechOffset(M_OFFSET-2.0f*PI);} |
benkatz | 56:fe5056ac6740 | 519 | |
benkatz | 23:2adf23ee0305 | 520 | int lut[128] = {0}; |
benkatz | 23:2adf23ee0305 | 521 | memcpy(&lut, &ENCODER_LUT, sizeof(lut)); |
benkatz | 25:f5741040c4bb | 522 | spi.WriteLUT(lut); // Set potision sensor nonlinearity lookup table |
benkatz | 45:26801179208e | 523 | init_controller_params(&controller); |
benkatz | 55:c4c9fec8539c | 524 | |
benkatz | 45:26801179208e | 525 | |
benkatz | 26:2b865c00d7e9 | 526 | pc.baud(921600); // set serial baud rate |
benkatz | 20:bf9ea5125d52 | 527 | wait(.01); |
benkatz | 23:2adf23ee0305 | 528 | pc.printf("\n\r\n\r HobbyKing Cheetah\n\r\n\r"); |
benkatz | 20:bf9ea5125d52 | 529 | wait(.01); |
benkatz | 23:2adf23ee0305 | 530 | printf("\n\r Debug Info:\n\r"); |
benkatz | 32:ccac5da77844 | 531 | printf(" Firmware Version: %s\n\r", VERSION_NUM); |
benkatz | 23:2adf23ee0305 | 532 | printf(" ADC1 Offset: %d ADC2 Offset: %d\n\r", controller.adc1_offset, controller.adc2_offset); |
benkatz | 23:2adf23ee0305 | 533 | printf(" Position Sensor Electrical Offset: %.4f\n\r", E_OFFSET); |
benkatz | 37:c0f352d6e8e3 | 534 | printf(" Output Zero Position: %.4f\n\r", M_OFFSET); |
benkatz | 24:58c2d7571207 | 535 | printf(" CAN ID: %d\n\r", CAN_ID); |
benkatz | 44:8040fa2fcb0d | 536 | |
benkatz | 44:8040fa2fcb0d | 537 | |
benkatz | 55:c4c9fec8539c | 538 | TIM1->CR1 ^= TIM_CR1_UDIS; |
benkatz | 44:8040fa2fcb0d | 539 | |
benkatz | 44:8040fa2fcb0d | 540 | |
benkatz | 23:2adf23ee0305 | 541 | pc.attach(&serial_interrupt); // attach serial interrupt |
benkatz | 50:ba72df25d10f | 542 | |
benkatz | 20:bf9ea5125d52 | 543 | |
benkatz | 44:8040fa2fcb0d | 544 | int counter = 0; |
benkatz | 0:4e1c4df6aabd | 545 | while(1) { |
benkatz | 55:c4c9fec8539c | 546 | //drv.print_faults(); |
benkatz | 53:e85efce8c1eb | 547 | wait(.1); |
benkatz | 57:0795d2add37e | 548 | |
benkatz | 55:c4c9fec8539c | 549 | if(controller.otw_flag){gpio.led->write(!gpio.led->read());} |
benkatz | 55:c4c9fec8539c | 550 | /* |
benkatz | 47:e1196a851f76 | 551 | if(state == MOTOR_MODE) |
benkatz | 47:e1196a851f76 | 552 | { |
benkatz | 55:c4c9fec8539c | 553 | if(controller.otw_flag){gpio.led->write(!gpio.led->read());} |
benkatz | 55:c4c9fec8539c | 554 | //printf("%f %f\n\r", controller.dtheta_mech, controller.i_d_ref); |
benkatz | 48:74a40481740c | 555 | //printf("%.3f %.3f %.3f\n\r", (float)observer.temperature, (float)observer.temperature2, observer.resistance); |
benkatz | 49:83d83040ea51 | 556 | //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 | 55:c4c9fec8539c | 557 | //printf("%.3f %.3f %.3f %.3f\n\r", controller.dtheta_elec, observer.resistance, observer.temperature, observer.temp_measured); |
benkatz | 55:c4c9fec8539c | 558 | //printf("%.3f %.3f\n\r" , observer.temperature, observer.temp_measured); |
benkatz | 47:e1196a851f76 | 559 | } |
benkatz | 55:c4c9fec8539c | 560 | |
benkatz | 50:ba72df25d10f | 561 | */ |
benkatz | 47:e1196a851f76 | 562 | |
benkatz | 0:4e1c4df6aabd | 563 | } |
benkatz | 0:4e1c4df6aabd | 564 | } |