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