Modified Motor Driver Firmware to include Flash + Thermal
Dependencies: FastPWM3 mbed-dev-STM-lean
main.cpp@70:2ea6c555a2cb, 2021-04-01 (annotated)
- Committer:
- elijahsj
- Date:
- Thu Apr 01 18:23:53 2021 +0000
- Revision:
- 70:2ea6c555a2cb
- Parent:
- 68:edfdbd0db876
- Child:
- 71:b314182de37f
revert dyno changes;
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); |
benkatz | 24:58c2d7571207 | 138 | printf("\n\r To change a value, type 'prefix''value''ENTER'\n\r i.e. 'b1000''ENTER'\n\r\n\r"); |
benkatz | 44:8040fa2fcb0d | 139 | wait_us(10); |
benkatz | 24:58c2d7571207 | 140 | state_change = 0; |
benkatz | 24:58c2d7571207 | 141 | } |
benkatz | 22:60276ba87ac6 | 142 | |
benkatz | 23:2adf23ee0305 | 143 | void enter_torque_mode(void){ |
benkatz | 44:8040fa2fcb0d | 144 | drv.enable_gd(); |
benkatz | 47:e1196a851f76 | 145 | //gpio.enable->write(1); |
benkatz | 37:c0f352d6e8e3 | 146 | controller.ovp_flag = 0; |
benkatz | 28:8c7e29f719c5 | 147 | reset_foc(&controller); // Tesets integrators, and other control loop parameters |
benkatz | 28:8c7e29f719c5 | 148 | wait(.001); |
benkatz | 23:2adf23ee0305 | 149 | controller.i_d_ref = 0; |
benkatz | 50:ba72df25d10f | 150 | controller.i_q_ref = 0; // Current Setpoints |
benkatz | 37:c0f352d6e8e3 | 151 | gpio.led->write(1); // Turn on status LED |
benkatz | 25:f5741040c4bb | 152 | state_change = 0; |
benkatz | 28:8c7e29f719c5 | 153 | printf("\n\r Entering Motor Mode \n\r"); |
benkatz | 23:2adf23ee0305 | 154 | } |
benkatz | 22:60276ba87ac6 | 155 | |
benkatz | 23:2adf23ee0305 | 156 | void calibrate(void){ |
benkatz | 44:8040fa2fcb0d | 157 | drv.enable_gd(); |
benkatz | 47:e1196a851f76 | 158 | //gpio.enable->write(1); |
benkatz | 37:c0f352d6e8e3 | 159 | gpio.led->write(1); // Turn on status LED |
benkatz | 25:f5741040c4bb | 160 | order_phases(&spi, &gpio, &controller, &prefs); // Check phase ordering |
benkatz | 25:f5741040c4bb | 161 | calibrate(&spi, &gpio, &controller, &prefs); // Perform calibration procedure |
benkatz | 37:c0f352d6e8e3 | 162 | gpio.led->write(0);; // Turn off status LED |
benkatz | 55:c4c9fec8539c | 163 | wait(.05); |
benkatz | 55:c4c9fec8539c | 164 | R_NOMINAL = 0; |
benkatz | 55:c4c9fec8539c | 165 | state = INIT_TEMP_MODE; |
saloutos | 58:32e8927fe39f | 166 | printf("\n\r Calibration complete. Press 'esc' to return to menu\n\r"); |
benkatz | 55:c4c9fec8539c | 167 | //drv.disable_gd(); |
saloutos | 58:32e8927fe39f | 168 | state_change = 0; |
benkatz | 55:c4c9fec8539c | 169 | |
benkatz | 23:2adf23ee0305 | 170 | } |
benkatz | 23:2adf23ee0305 | 171 | |
benkatz | 23:2adf23ee0305 | 172 | void print_encoder(void){ |
benkatz | 48:74a40481740c | 173 | printf(" Mechanical Angle: %f Electrical Angle: %f Raw: %d\n\r", spi.GetMechPosition(), spi.GetElecPosition(), spi.GetRawPosition()); |
benkatz | 48:74a40481740c | 174 | //printf("%d\n\r", spi.GetRawPosition()); |
benkatz | 47:e1196a851f76 | 175 | wait(.001); |
benkatz | 22:60276ba87ac6 | 176 | } |
benkatz | 20:bf9ea5125d52 | 177 | |
benkatz | 23:2adf23ee0305 | 178 | /// Current Sampling Interrupt /// |
benkatz | 23:2adf23ee0305 | 179 | /// This runs at 40 kHz, regardless of of the mode the controller is in /// |
benkatz | 55:c4c9fec8539c | 180 | //float testing[1000]; |
benkatz | 55:c4c9fec8539c | 181 | //float testing2[1000]; |
benkatz | 2:8724412ad628 | 182 | extern "C" void TIM1_UP_TIM10_IRQHandler(void) { |
benkatz | 2:8724412ad628 | 183 | if (TIM1->SR & TIM_SR_UIF ) { |
benkatz | 55:c4c9fec8539c | 184 | //gpio.led->write(1); |
benkatz | 23:2adf23ee0305 | 185 | ///Sample current always /// |
benkatz | 25:f5741040c4bb | 186 | ADC1->CR2 |= 0x40000000; // Begin sample and conversion |
benkatz | 22:60276ba87ac6 | 187 | //volatile int delay; |
benkatz | 20:bf9ea5125d52 | 188 | //for (delay = 0; delay < 55; delay++); |
benkatz | 55:c4c9fec8539c | 189 | |
benkatz | 47:e1196a851f76 | 190 | spi.Sample(DT); // sample position sensor |
benkatz | 56:fe5056ac6740 | 191 | /* |
benkatz | 56:fe5056ac6740 | 192 | if(count < 10){printf("%d\n\r", spi.GetRawPosition());} |
benkatz | 56:fe5056ac6740 | 193 | count ++; |
benkatz | 56:fe5056ac6740 | 194 | */ |
benkatz | 37:c0f352d6e8e3 | 195 | controller.adc2_raw = ADC2->DR; // Read ADC Data Registers |
benkatz | 23:2adf23ee0305 | 196 | controller.adc1_raw = ADC1->DR; |
benkatz | 37:c0f352d6e8e3 | 197 | controller.adc3_raw = ADC3->DR; |
benkatz | 37:c0f352d6e8e3 | 198 | controller.theta_elec = spi.GetElecPosition(); |
saloutos | 58:32e8927fe39f | 199 | controller.theta_mech = (1.0f/GR)*spi.GetMechPosition(); // should these be divided by gear ratio??? |
saloutos | 58:32e8927fe39f | 200 | controller.dtheta_mech = (1.0f/GR)*spi.GetMechVelocity(); // mech pos isn't pre-multiplied by GR in position sensor function... |
benkatz | 37:c0f352d6e8e3 | 201 | controller.dtheta_elec = spi.GetElecVelocity(); |
benkatz | 51:6cd89bd6fcaa | 202 | 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 | 203 | |
benkatz | 55:c4c9fec8539c | 204 | |
benkatz | 55:c4c9fec8539c | 205 | |
benkatz | 23:2adf23ee0305 | 206 | /// |
benkatz | 20:bf9ea5125d52 | 207 | |
benkatz | 23:2adf23ee0305 | 208 | /// Check state machine state, and run the appropriate function /// |
benkatz | 23:2adf23ee0305 | 209 | switch(state){ |
benkatz | 37:c0f352d6e8e3 | 210 | case REST_MODE: // Do nothing |
benkatz | 23:2adf23ee0305 | 211 | if(state_change){ |
benkatz | 23:2adf23ee0305 | 212 | enter_menu_state(); |
benkatz | 23:2adf23ee0305 | 213 | } |
benkatz | 55:c4c9fec8539c | 214 | update_observer(&controller, &observer); |
benkatz | 23:2adf23ee0305 | 215 | break; |
benkatz | 22:60276ba87ac6 | 216 | |
benkatz | 23:2adf23ee0305 | 217 | case CALIBRATION_MODE: // Run encoder calibration procedure |
benkatz | 23:2adf23ee0305 | 218 | if(state_change){ |
benkatz | 23:2adf23ee0305 | 219 | calibrate(); |
benkatz | 23:2adf23ee0305 | 220 | } |
benkatz | 23:2adf23ee0305 | 221 | break; |
benkatz | 55:c4c9fec8539c | 222 | case INIT_TEMP_MODE: |
benkatz | 55:c4c9fec8539c | 223 | if(state_change){ |
benkatz | 55:c4c9fec8539c | 224 | enter_torque_mode(); |
benkatz | 55:c4c9fec8539c | 225 | count = 0; |
benkatz | 55:c4c9fec8539c | 226 | observer.resistance = 0.0f; |
benkatz | 55:c4c9fec8539c | 227 | } |
benkatz | 55:c4c9fec8539c | 228 | controller.i_d_ref = -10.0f; |
benkatz | 55:c4c9fec8539c | 229 | controller.i_q_ref = 0.0f; |
benkatz | 55:c4c9fec8539c | 230 | commutate(&controller, &observer, &gpio, controller.theta_elec); |
benkatz | 55:c4c9fec8539c | 231 | |
benkatz | 55:c4c9fec8539c | 232 | if(count > 200) |
benkatz | 55:c4c9fec8539c | 233 | { |
benkatz | 55:c4c9fec8539c | 234 | float r_meas = controller.v_d*(DTC_MAX-DTC_MIN)/(controller.i_d*SQRT3); |
benkatz | 55:c4c9fec8539c | 235 | //testing2[count-100] = controller.i_d; |
benkatz | 55:c4c9fec8539c | 236 | observer.resistance += .001f*r_meas; |
benkatz | 55:c4c9fec8539c | 237 | } |
benkatz | 55:c4c9fec8539c | 238 | if(count > 1200) |
benkatz | 55:c4c9fec8539c | 239 | { |
benkatz | 55:c4c9fec8539c | 240 | count = 0; |
benkatz | 55:c4c9fec8539c | 241 | state = REST_MODE; |
benkatz | 55:c4c9fec8539c | 242 | state_change = 1; |
benkatz | 55:c4c9fec8539c | 243 | gpio.led->write(0); |
benkatz | 55:c4c9fec8539c | 244 | observer.temperature = (double)(T_AMBIENT + ((observer.resistance/R_NOMINAL) - 1.0f)*254.5f); |
benkatz | 55:c4c9fec8539c | 245 | printf("Winding Resistance: %f\n\r", observer.resistance); |
benkatz | 55:c4c9fec8539c | 246 | printf("Winding Temperature: %f\n\r", observer.temperature); |
benkatz | 55:c4c9fec8539c | 247 | |
benkatz | 55:c4c9fec8539c | 248 | if(R_NOMINAL==0) |
benkatz | 55:c4c9fec8539c | 249 | { |
benkatz | 55:c4c9fec8539c | 250 | printf("Saving winding resistance\n\r"); |
benkatz | 55:c4c9fec8539c | 251 | R_NOMINAL = observer.resistance; |
benkatz | 55:c4c9fec8539c | 252 | if (!prefs.ready()) prefs.open(); |
benkatz | 55:c4c9fec8539c | 253 | prefs.flush(); // write offset and lookup table to flash |
benkatz | 55:c4c9fec8539c | 254 | prefs.close(); |
benkatz | 55:c4c9fec8539c | 255 | } |
benkatz | 55:c4c9fec8539c | 256 | //for(int i = 0; i<1000; i++){printf("%f \n\r", testing[i]);} |
benkatz | 55:c4c9fec8539c | 257 | } |
benkatz | 55:c4c9fec8539c | 258 | |
benkatz | 55:c4c9fec8539c | 259 | count++; |
benkatz | 55:c4c9fec8539c | 260 | break; |
benkatz | 26:2b865c00d7e9 | 261 | case MOTOR_MODE: // Run torque control |
benkatz | 25:f5741040c4bb | 262 | if(state_change){ |
benkatz | 25:f5741040c4bb | 263 | enter_torque_mode(); |
benkatz | 28:8c7e29f719c5 | 264 | count = 0; |
benkatz | 25:f5741040c4bb | 265 | } |
benkatz | 28:8c7e29f719c5 | 266 | else{ |
benkatz | 37:c0f352d6e8e3 | 267 | /* |
benkatz | 37:c0f352d6e8e3 | 268 | 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 | 269 | gpio. |
benkatz | 47:e1196a851f76 | 270 | ->write(0); |
benkatz | 37:c0f352d6e8e3 | 271 | controller.ovp_flag = 1; |
benkatz | 37:c0f352d6e8e3 | 272 | state = REST_MODE; |
benkatz | 37:c0f352d6e8e3 | 273 | state_change = 1; |
benkatz | 37:c0f352d6e8e3 | 274 | printf("OVP Triggered!\n\r"); |
benkatz | 37:c0f352d6e8e3 | 275 | } |
benkatz | 37:c0f352d6e8e3 | 276 | */ |
benkatz | 37:c0f352d6e8e3 | 277 | |
benkatz | 28:8c7e29f719c5 | 278 | if((controller.timeout > CAN_TIMEOUT) && (CAN_TIMEOUT > 0)){ |
benkatz | 28:8c7e29f719c5 | 279 | controller.i_d_ref = 0; |
benkatz | 28:8c7e29f719c5 | 280 | controller.i_q_ref = 0; |
benkatz | 37:c0f352d6e8e3 | 281 | controller.kp = 0; |
benkatz | 37:c0f352d6e8e3 | 282 | controller.kd = 0; |
benkatz | 37:c0f352d6e8e3 | 283 | controller.t_ff = 0; |
benkatz | 28:8c7e29f719c5 | 284 | } |
benkatz | 55:c4c9fec8539c | 285 | |
benkatz | 50:ba72df25d10f | 286 | torque_control(&controller); |
benkatz | 55:c4c9fec8539c | 287 | update_observer(&controller, &observer); |
benkatz | 55:c4c9fec8539c | 288 | field_weaken(&controller); |
benkatz | 49:83d83040ea51 | 289 | commutate(&controller, &observer, &gpio, controller.theta_elec); // Run current loop |
benkatz | 49:83d83040ea51 | 290 | controller.timeout++; |
benkatz | 55:c4c9fec8539c | 291 | |
benkatz | 55:c4c9fec8539c | 292 | if(controller.otw_flag) |
benkatz | 55:c4c9fec8539c | 293 | { |
benkatz | 55:c4c9fec8539c | 294 | state = REST_MODE; |
benkatz | 55:c4c9fec8539c | 295 | state_change = 1; |
benkatz | 55:c4c9fec8539c | 296 | gpio.led->write(0); |
benkatz | 55:c4c9fec8539c | 297 | } |
benkatz | 55:c4c9fec8539c | 298 | |
benkatz | 49:83d83040ea51 | 299 | count++; |
benkatz | 55:c4c9fec8539c | 300 | } |
benkatz | 55:c4c9fec8539c | 301 | |
benkatz | 55:c4c9fec8539c | 302 | |
benkatz | 55:c4c9fec8539c | 303 | break; |
benkatz | 37:c0f352d6e8e3 | 304 | |
benkatz | 23:2adf23ee0305 | 305 | case SETUP_MODE: |
benkatz | 23:2adf23ee0305 | 306 | if(state_change){ |
benkatz | 24:58c2d7571207 | 307 | enter_setup_state(); |
benkatz | 23:2adf23ee0305 | 308 | } |
benkatz | 23:2adf23ee0305 | 309 | break; |
benkatz | 23:2adf23ee0305 | 310 | case ENCODER_MODE: |
benkatz | 23:2adf23ee0305 | 311 | print_encoder(); |
benkatz | 23:2adf23ee0305 | 312 | break; |
benkatz | 37:c0f352d6e8e3 | 313 | } |
benkatz | 2:8724412ad628 | 314 | } |
benkatz | 55:c4c9fec8539c | 315 | //gpio.led->write(0); |
benkatz | 23:2adf23ee0305 | 316 | TIM1->SR = 0x0; // reset the status register |
benkatz | 2:8724412ad628 | 317 | } |
benkatz | 0:4e1c4df6aabd | 318 | |
benkatz | 25:f5741040c4bb | 319 | |
benkatz | 24:58c2d7571207 | 320 | char cmd_val[8] = {0}; |
benkatz | 24:58c2d7571207 | 321 | char cmd_id = 0; |
benkatz | 25:f5741040c4bb | 322 | char char_count = 0; |
benkatz | 24:58c2d7571207 | 323 | |
benkatz | 25:f5741040c4bb | 324 | /// Manage state machine with commands from serial terminal or configurator gui /// |
benkatz | 25:f5741040c4bb | 325 | /// Called when data received over serial /// |
benkatz | 23:2adf23ee0305 | 326 | void serial_interrupt(void){ |
benkatz | 23:2adf23ee0305 | 327 | while(pc.readable()){ |
benkatz | 23:2adf23ee0305 | 328 | char c = pc.getc(); |
benkatz | 25:f5741040c4bb | 329 | if(c == 27){ |
benkatz | 25:f5741040c4bb | 330 | state = REST_MODE; |
benkatz | 25:f5741040c4bb | 331 | state_change = 1; |
benkatz | 25:f5741040c4bb | 332 | char_count = 0; |
benkatz | 25:f5741040c4bb | 333 | cmd_id = 0; |
benkatz | 55:c4c9fec8539c | 334 | gpio.led->write(0);; |
benkatz | 25:f5741040c4bb | 335 | for(int i = 0; i<8; i++){cmd_val[i] = 0;} |
benkatz | 25:f5741040c4bb | 336 | } |
benkatz | 24:58c2d7571207 | 337 | if(state == REST_MODE){ |
benkatz | 23:2adf23ee0305 | 338 | switch (c){ |
benkatz | 23:2adf23ee0305 | 339 | case 'c': |
benkatz | 23:2adf23ee0305 | 340 | state = CALIBRATION_MODE; |
benkatz | 23:2adf23ee0305 | 341 | state_change = 1; |
benkatz | 23:2adf23ee0305 | 342 | break; |
benkatz | 26:2b865c00d7e9 | 343 | case 'm': |
benkatz | 26:2b865c00d7e9 | 344 | state = MOTOR_MODE; |
benkatz | 23:2adf23ee0305 | 345 | state_change = 1; |
benkatz | 23:2adf23ee0305 | 346 | break; |
benkatz | 23:2adf23ee0305 | 347 | case 'e': |
benkatz | 23:2adf23ee0305 | 348 | state = ENCODER_MODE; |
benkatz | 23:2adf23ee0305 | 349 | state_change = 1; |
benkatz | 23:2adf23ee0305 | 350 | break; |
benkatz | 23:2adf23ee0305 | 351 | case 's': |
benkatz | 23:2adf23ee0305 | 352 | state = SETUP_MODE; |
benkatz | 23:2adf23ee0305 | 353 | state_change = 1; |
benkatz | 23:2adf23ee0305 | 354 | break; |
benkatz | 37:c0f352d6e8e3 | 355 | case 'z': |
benkatz | 37:c0f352d6e8e3 | 356 | spi.SetMechOffset(0); |
benkatz | 47:e1196a851f76 | 357 | spi.Sample(DT); |
benkatz | 37:c0f352d6e8e3 | 358 | wait_us(20); |
benkatz | 37:c0f352d6e8e3 | 359 | M_OFFSET = spi.GetMechPosition(); |
benkatz | 37:c0f352d6e8e3 | 360 | if (!prefs.ready()) prefs.open(); |
benkatz | 37:c0f352d6e8e3 | 361 | prefs.flush(); // Write new prefs to flash |
benkatz | 37:c0f352d6e8e3 | 362 | prefs.close(); |
benkatz | 37:c0f352d6e8e3 | 363 | prefs.load(); |
benkatz | 37:c0f352d6e8e3 | 364 | spi.SetMechOffset(M_OFFSET); |
benkatz | 37:c0f352d6e8e3 | 365 | printf("\n\r Saved new zero position: %.4f\n\r\n\r", M_OFFSET); |
benkatz | 37:c0f352d6e8e3 | 366 | |
benkatz | 37:c0f352d6e8e3 | 367 | break; |
benkatz | 37:c0f352d6e8e3 | 368 | } |
benkatz | 37:c0f352d6e8e3 | 369 | |
benkatz | 24:58c2d7571207 | 370 | } |
benkatz | 24:58c2d7571207 | 371 | else if(state == SETUP_MODE){ |
benkatz | 25:f5741040c4bb | 372 | if(c == 13){ |
benkatz | 24:58c2d7571207 | 373 | switch (cmd_id){ |
benkatz | 24:58c2d7571207 | 374 | case 'b': |
benkatz | 24:58c2d7571207 | 375 | I_BW = fmaxf(fminf(atof(cmd_val), 2000.0f), 100.0f); |
benkatz | 24:58c2d7571207 | 376 | break; |
benkatz | 24:58c2d7571207 | 377 | case 'i': |
benkatz | 24:58c2d7571207 | 378 | CAN_ID = atoi(cmd_val); |
benkatz | 24:58c2d7571207 | 379 | break; |
benkatz | 26:2b865c00d7e9 | 380 | case 'm': |
benkatz | 26:2b865c00d7e9 | 381 | CAN_MASTER = atoi(cmd_val); |
benkatz | 26:2b865c00d7e9 | 382 | break; |
benkatz | 24:58c2d7571207 | 383 | case 'l': |
elijahsj | 66:3947255b18c5 | 384 | I_MAX = fmaxf(fminf(atof(cmd_val), 65.0f), 0.0f); |
benkatz | 51:6cd89bd6fcaa | 385 | break; |
benkatz | 51:6cd89bd6fcaa | 386 | case 'f': |
benkatz | 51:6cd89bd6fcaa | 387 | I_FW_MAX = fmaxf(fminf(atof(cmd_val), 33.0f), 0.0f); |
benkatz | 24:58c2d7571207 | 388 | break; |
benkatz | 28:8c7e29f719c5 | 389 | case 't': |
benkatz | 28:8c7e29f719c5 | 390 | CAN_TIMEOUT = atoi(cmd_val); |
benkatz | 28:8c7e29f719c5 | 391 | break; |
benkatz | 55:c4c9fec8539c | 392 | case 'h': |
benkatz | 55:c4c9fec8539c | 393 | TEMP_MAX = fmaxf(fminf(atof(cmd_val), 150.0f), 0.0f); |
benkatz | 55:c4c9fec8539c | 394 | break; |
benkatz | 55:c4c9fec8539c | 395 | case 'c': |
benkatz | 55:c4c9fec8539c | 396 | I_MAX_CONT = fmaxf(fminf(atof(cmd_val), 40.0f), 0.0f); |
benkatz | 55:c4c9fec8539c | 397 | break; |
benkatz | 24:58c2d7571207 | 398 | default: |
benkatz | 24:58c2d7571207 | 399 | printf("\n\r '%c' Not a valid command prefix\n\r\n\r", cmd_id); |
benkatz | 24:58c2d7571207 | 400 | break; |
benkatz | 24:58c2d7571207 | 401 | } |
benkatz | 24:58c2d7571207 | 402 | |
benkatz | 24:58c2d7571207 | 403 | if (!prefs.ready()) prefs.open(); |
benkatz | 24:58c2d7571207 | 404 | prefs.flush(); // Write new prefs to flash |
benkatz | 24:58c2d7571207 | 405 | prefs.close(); |
benkatz | 24:58c2d7571207 | 406 | prefs.load(); |
benkatz | 24:58c2d7571207 | 407 | state_change = 1; |
benkatz | 24:58c2d7571207 | 408 | char_count = 0; |
benkatz | 24:58c2d7571207 | 409 | cmd_id = 0; |
benkatz | 24:58c2d7571207 | 410 | for(int i = 0; i<8; i++){cmd_val[i] = 0;} |
benkatz | 24:58c2d7571207 | 411 | } |
benkatz | 24:58c2d7571207 | 412 | else{ |
benkatz | 24:58c2d7571207 | 413 | if(char_count == 0){cmd_id = c;} |
benkatz | 24:58c2d7571207 | 414 | else{ |
benkatz | 24:58c2d7571207 | 415 | cmd_val[char_count-1] = c; |
benkatz | 24:58c2d7571207 | 416 | |
benkatz | 24:58c2d7571207 | 417 | } |
benkatz | 24:58c2d7571207 | 418 | pc.putc(c); |
benkatz | 24:58c2d7571207 | 419 | char_count++; |
benkatz | 23:2adf23ee0305 | 420 | } |
benkatz | 23:2adf23ee0305 | 421 | } |
benkatz | 24:58c2d7571207 | 422 | else if (state == ENCODER_MODE){ |
benkatz | 24:58c2d7571207 | 423 | switch (c){ |
benkatz | 24:58c2d7571207 | 424 | case 27: |
benkatz | 24:58c2d7571207 | 425 | state = REST_MODE; |
benkatz | 24:58c2d7571207 | 426 | state_change = 1; |
benkatz | 24:58c2d7571207 | 427 | break; |
benkatz | 24:58c2d7571207 | 428 | } |
benkatz | 24:58c2d7571207 | 429 | } |
benkatz | 49:83d83040ea51 | 430 | else if (state == MOTOR_MODE){ |
benkatz | 49:83d83040ea51 | 431 | switch (c){ |
benkatz | 49:83d83040ea51 | 432 | case 'd': |
benkatz | 49:83d83040ea51 | 433 | controller.i_q_ref = 0; |
benkatz | 49:83d83040ea51 | 434 | controller.i_d_ref = 0; |
benkatz | 49:83d83040ea51 | 435 | } |
benkatz | 49:83d83040ea51 | 436 | } |
benkatz | 24:58c2d7571207 | 437 | |
benkatz | 24:58c2d7571207 | 438 | } |
benkatz | 22:60276ba87ac6 | 439 | } |
benkatz | 0:4e1c4df6aabd | 440 | |
benkatz | 0:4e1c4df6aabd | 441 | int main() { |
benkatz | 20:bf9ea5125d52 | 442 | controller.v_bus = V_BUS; |
benkatz | 22:60276ba87ac6 | 443 | controller.mode = 0; |
benkatz | 23:2adf23ee0305 | 444 | Init_All_HW(&gpio); // Setup PWM, ADC, GPIO |
benkatz | 55:c4c9fec8539c | 445 | wait_us(100); |
benkatz | 44:8040fa2fcb0d | 446 | |
benkatz | 44:8040fa2fcb0d | 447 | gpio.enable->write(1); |
benkatz | 44:8040fa2fcb0d | 448 | wait_us(100); |
benkatz | 45:26801179208e | 449 | drv.calibrate(); |
benkatz | 45:26801179208e | 450 | wait_us(100); |
benkatz | 55:c4c9fec8539c | 451 | drv.write_DCR(0x0, DIS_GDF_DIS, 0x0, PWM_MODE_3X, 0x0, 0x0, 0x0, 0x0, 0x1); |
benkatz | 55:c4c9fec8539c | 452 | wait_us(100); |
benkatz | 55:c4c9fec8539c | 453 | drv.write_CSACR(0x0, 0x1, 0x0, CSA_GAIN_40, 0x0, 0x1, 0x1, 0x1, SEN_LVL_1_0); // calibrate shunt amplifiers |
benkatz | 44:8040fa2fcb0d | 454 | wait_us(100); |
benkatz | 55:c4c9fec8539c | 455 | zero_current(&controller.adc1_offset, &controller.adc2_offset); |
benkatz | 44:8040fa2fcb0d | 456 | wait_us(100); |
benkatz | 55:c4c9fec8539c | 457 | drv.write_CSACR(0x0, 0x1, 0x0, CSA_GAIN_40, 0x1, 0x0, 0x0, 0x0, SEN_LVL_1_0); |
benkatz | 55:c4c9fec8539c | 458 | wait_us(100); |
benkatz | 55:c4c9fec8539c | 459 | drv.write_OCPCR(TRETRY_50US, DEADTIME_50NS, OCP_NONE, OCP_DEG_8US, VDS_LVL_1_88); |
benkatz | 45:26801179208e | 460 | |
benkatz | 45:26801179208e | 461 | //drv.enable_gd(); |
benkatz | 47:e1196a851f76 | 462 | drv.disable_gd(); |
benkatz | 55:c4c9fec8539c | 463 | //zero_current(&controller.adc1_offset, &controller.adc2_offset); // Measure current sensor zero-offset |
benkatz | 55:c4c9fec8539c | 464 | //drv.enable_gd(); |
benkatz | 20:bf9ea5125d52 | 465 | |
benkatz | 55:c4c9fec8539c | 466 | wait_us(100); |
benkatz | 55:c4c9fec8539c | 467 | |
benkatz | 23:2adf23ee0305 | 468 | reset_foc(&controller); // Reset current controller |
benkatz | 48:74a40481740c | 469 | reset_observer(&observer); // Reset observer |
benkatz | 26:2b865c00d7e9 | 470 | //TIM1->CR1 |= TIM_CR1_UDIS; //enable interrupt |
benkatz | 20:bf9ea5125d52 | 471 | |
benkatz | 55:c4c9fec8539c | 472 | wait_us(100); |
benkatz | 37:c0f352d6e8e3 | 473 | NVIC_SetPriority(TIM1_UP_TIM10_IRQn, 2); // commutation > communication |
benkatz | 37:c0f352d6e8e3 | 474 | NVIC_SetPriority(CAN1_RX0_IRQn, 3); |
benkatz | 53:e85efce8c1eb | 475 | // attach 'CAN receive-complete' interrupt handler |
benkatz | 53:e85efce8c1eb | 476 | |
benkatz | 53:e85efce8c1eb | 477 | // If preferences haven't been user configured yet, set defaults |
benkatz | 53:e85efce8c1eb | 478 | prefs.load(); // Read flash |
benkatz | 55:c4c9fec8539c | 479 | can.filter(CAN_ID , 0xFFF, CANStandard, 0); |
benkatz | 28:8c7e29f719c5 | 480 | txMsg.id = CAN_MASTER; |
benkatz | 28:8c7e29f719c5 | 481 | txMsg.len = 6; |
benkatz | 26:2b865c00d7e9 | 482 | rxMsg.len = 8; |
benkatz | 53:e85efce8c1eb | 483 | can.attach(&onMsgReceived); |
benkatz | 23:2adf23ee0305 | 484 | |
benkatz | 37:c0f352d6e8e3 | 485 | if(isnan(E_OFFSET)){E_OFFSET = 0.0f;} |
benkatz | 37:c0f352d6e8e3 | 486 | if(isnan(M_OFFSET)){M_OFFSET = 0.0f;} |
benkatz | 48:74a40481740c | 487 | if(isnan(I_BW) || I_BW==-1){I_BW = 1000;} |
benkatz | 51:6cd89bd6fcaa | 488 | if(isnan(I_MAX) || I_MAX ==-1){I_MAX=40;} |
benkatz | 55:c4c9fec8539c | 489 | if(isnan(I_FW_MAX) || I_FW_MAX ==-1){I_FW_MAX=12;} |
benkatz | 48:74a40481740c | 490 | if(isnan(CAN_ID) || CAN_ID==-1){CAN_ID = 1;} |
benkatz | 48:74a40481740c | 491 | if(isnan(CAN_MASTER) || CAN_MASTER==-1){CAN_MASTER = 0;} |
benkatz | 55:c4c9fec8539c | 492 | if(isnan(CAN_TIMEOUT) || CAN_TIMEOUT==-1){CAN_TIMEOUT = 1000;} |
benkatz | 55:c4c9fec8539c | 493 | if(isnan(R_NOMINAL) || R_NOMINAL==-1){R_NOMINAL = 0.0f;} |
benkatz | 55:c4c9fec8539c | 494 | if(isnan(TEMP_MAX) || TEMP_MAX==-1){TEMP_MAX = 125.0f;} |
benkatz | 55:c4c9fec8539c | 495 | if(isnan(I_MAX_CONT) || I_MAX_CONT==-1){I_MAX_CONT = 14.0f;} |
benkatz | 25:f5741040c4bb | 496 | spi.SetElecOffset(E_OFFSET); // Set position sensor offset |
benkatz | 37:c0f352d6e8e3 | 497 | spi.SetMechOffset(M_OFFSET); |
benkatz | 56:fe5056ac6740 | 498 | spi.Sample(1.0f); |
benkatz | 56:fe5056ac6740 | 499 | 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 | 500 | else if (spi.GetMechPosition() < -PI){spi.SetMechOffset(M_OFFSET-2.0f*PI);} |
benkatz | 56:fe5056ac6740 | 501 | |
benkatz | 23:2adf23ee0305 | 502 | int lut[128] = {0}; |
benkatz | 23:2adf23ee0305 | 503 | memcpy(&lut, &ENCODER_LUT, sizeof(lut)); |
benkatz | 25:f5741040c4bb | 504 | spi.WriteLUT(lut); // Set potision sensor nonlinearity lookup table |
benkatz | 45:26801179208e | 505 | init_controller_params(&controller); |
benkatz | 55:c4c9fec8539c | 506 | |
benkatz | 45:26801179208e | 507 | |
benkatz | 26:2b865c00d7e9 | 508 | pc.baud(921600); // set serial baud rate |
benkatz | 20:bf9ea5125d52 | 509 | wait(.01); |
benkatz | 23:2adf23ee0305 | 510 | pc.printf("\n\r\n\r HobbyKing Cheetah\n\r\n\r"); |
benkatz | 20:bf9ea5125d52 | 511 | wait(.01); |
benkatz | 23:2adf23ee0305 | 512 | printf("\n\r Debug Info:\n\r"); |
benkatz | 32:ccac5da77844 | 513 | printf(" Firmware Version: %s\n\r", VERSION_NUM); |
benkatz | 23:2adf23ee0305 | 514 | printf(" ADC1 Offset: %d ADC2 Offset: %d\n\r", controller.adc1_offset, controller.adc2_offset); |
benkatz | 23:2adf23ee0305 | 515 | printf(" Position Sensor Electrical Offset: %.4f\n\r", E_OFFSET); |
benkatz | 37:c0f352d6e8e3 | 516 | printf(" Output Zero Position: %.4f\n\r", M_OFFSET); |
benkatz | 24:58c2d7571207 | 517 | printf(" CAN ID: %d\n\r", CAN_ID); |
benkatz | 44:8040fa2fcb0d | 518 | |
benkatz | 44:8040fa2fcb0d | 519 | |
benkatz | 55:c4c9fec8539c | 520 | TIM1->CR1 ^= TIM_CR1_UDIS; |
benkatz | 44:8040fa2fcb0d | 521 | |
benkatz | 44:8040fa2fcb0d | 522 | |
benkatz | 23:2adf23ee0305 | 523 | pc.attach(&serial_interrupt); // attach serial interrupt |
benkatz | 50:ba72df25d10f | 524 | |
benkatz | 20:bf9ea5125d52 | 525 | |
benkatz | 44:8040fa2fcb0d | 526 | int counter = 0; |
benkatz | 0:4e1c4df6aabd | 527 | while(1) { |
benkatz | 55:c4c9fec8539c | 528 | //drv.print_faults(); |
benkatz | 53:e85efce8c1eb | 529 | wait(.1); |
benkatz | 57:0795d2add37e | 530 | |
benkatz | 55:c4c9fec8539c | 531 | if(controller.otw_flag){gpio.led->write(!gpio.led->read());} |
benkatz | 55:c4c9fec8539c | 532 | /* |
benkatz | 47:e1196a851f76 | 533 | if(state == MOTOR_MODE) |
benkatz | 47:e1196a851f76 | 534 | { |
benkatz | 55:c4c9fec8539c | 535 | if(controller.otw_flag){gpio.led->write(!gpio.led->read());} |
benkatz | 55:c4c9fec8539c | 536 | //printf("%f %f\n\r", controller.dtheta_mech, controller.i_d_ref); |
benkatz | 48:74a40481740c | 537 | //printf("%.3f %.3f %.3f\n\r", (float)observer.temperature, (float)observer.temperature2, observer.resistance); |
benkatz | 49:83d83040ea51 | 538 | //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 | 539 | //printf("%.3f %.3f %.3f %.3f\n\r", controller.dtheta_elec, observer.resistance, observer.temperature, observer.temp_measured); |
benkatz | 55:c4c9fec8539c | 540 | //printf("%.3f %.3f\n\r" , observer.temperature, observer.temp_measured); |
benkatz | 47:e1196a851f76 | 541 | } |
benkatz | 55:c4c9fec8539c | 542 | |
benkatz | 50:ba72df25d10f | 543 | */ |
benkatz | 47:e1196a851f76 | 544 | |
benkatz | 0:4e1c4df6aabd | 545 | } |
benkatz | 0:4e1c4df6aabd | 546 | } |