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