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