DRV8323RS Version
Dependencies: mbed-dev-f303 FastPWM3
main.cpp@37:c0f352d6e8e3, 2018-04-13 (annotated)
- Committer:
- benkatz
- Date:
- Fri Apr 13 13:50:54 2018 +0000
- Revision:
- 37:c0f352d6e8e3
- Parent:
- 36:d88fd41f60a6
- Child:
- 38:67e4e1453a4b
always samples position sensor
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 | 22:60276ba87ac6 | 2 | /// Written by benkatz, with much inspiration from bayleyw, nkirkby, scolton, David Otten, and others |
benkatz | 22:60276ba87ac6 | 3 | /// Hardware documentation can be found at build-its.blogspot.com |
benkatz | 22:60276ba87ac6 | 4 | |
benkatz | 22:60276ba87ac6 | 5 | /// Written for the STM32F446, but can be implemented on other STM32 MCU's with some further register-diddling |
benkatz | 22:60276ba87ac6 | 6 | |
benkatz | 23:2adf23ee0305 | 7 | #define REST_MODE 0 |
benkatz | 23:2adf23ee0305 | 8 | #define CALIBRATION_MODE 1 |
benkatz | 26:2b865c00d7e9 | 9 | #define MOTOR_MODE 2 |
benkatz | 23:2adf23ee0305 | 10 | #define SETUP_MODE 4 |
benkatz | 23:2adf23ee0305 | 11 | #define ENCODER_MODE 5 |
benkatz | 22:60276ba87ac6 | 12 | |
benkatz | 37:c0f352d6e8e3 | 13 | #define VERSION_NUM "1.4" |
benkatz | 26:2b865c00d7e9 | 14 | |
benkatz | 18:f1d56f4acb39 | 15 | |
benkatz | 26:2b865c00d7e9 | 16 | float __float_reg[64]; // Floats stored in flash |
benkatz | 26:2b865c00d7e9 | 17 | int __int_reg[256]; // Ints stored in flash. Includes position sensor calibration lookup table |
benkatz | 17:3c5df2982199 | 18 | |
benkatz | 0:4e1c4df6aabd | 19 | #include "mbed.h" |
benkatz | 0:4e1c4df6aabd | 20 | #include "PositionSensor.h" |
benkatz | 20:bf9ea5125d52 | 21 | #include "structs.h" |
benkatz | 20:bf9ea5125d52 | 22 | #include "foc.h" |
benkatz | 22:60276ba87ac6 | 23 | #include "calibration.h" |
benkatz | 20:bf9ea5125d52 | 24 | #include "hw_setup.h" |
benkatz | 23:2adf23ee0305 | 25 | #include "math_ops.h" |
benkatz | 20:bf9ea5125d52 | 26 | #include "current_controller_config.h" |
benkatz | 20:bf9ea5125d52 | 27 | #include "hw_config.h" |
benkatz | 20:bf9ea5125d52 | 28 | #include "motor_config.h" |
benkatz | 23:2adf23ee0305 | 29 | #include "stm32f4xx_flash.h" |
benkatz | 23:2adf23ee0305 | 30 | #include "FlashWriter.h" |
benkatz | 23:2adf23ee0305 | 31 | #include "user_config.h" |
benkatz | 23:2adf23ee0305 | 32 | #include "PreferenceWriter.h" |
benkatz | 23:2adf23ee0305 | 33 | |
benkatz | 37:c0f352d6e8e3 | 34 | |
benkatz | 26:2b865c00d7e9 | 35 | |
benkatz | 23:2adf23ee0305 | 36 | PreferenceWriter prefs(6); |
benkatz | 9:d7eb815cb057 | 37 | |
benkatz | 20:bf9ea5125d52 | 38 | GPIOStruct gpio; |
benkatz | 20:bf9ea5125d52 | 39 | ControllerStruct controller; |
benkatz | 20:bf9ea5125d52 | 40 | COMStruct com; |
benkatz | 37:c0f352d6e8e3 | 41 | ObserverStruct observer; |
benkatz | 9:d7eb815cb057 | 42 | |
benkatz | 26:2b865c00d7e9 | 43 | //using namespace CANnucleo; |
benkatz | 17:3c5df2982199 | 44 | |
benkatz | 26:2b865c00d7e9 | 45 | CAN can(PB_8, PB_9); // CAN Rx pin name, CAN Tx pin name |
benkatz | 26:2b865c00d7e9 | 46 | CANMessage rxMsg; |
benkatz | 26:2b865c00d7e9 | 47 | CANMessage txMsg; |
benkatz | 23:2adf23ee0305 | 48 | |
benkatz | 20:bf9ea5125d52 | 49 | |
benkatz | 9:d7eb815cb057 | 50 | Serial pc(PA_2, PA_3); |
benkatz | 8:10ae7bc88d6e | 51 | |
benkatz | 26:2b865c00d7e9 | 52 | PositionSensorAM5147 spi(16384, 0.0, NPP); |
benkatz | 37:c0f352d6e8e3 | 53 | //PositionSensorEncoder encoder(4096, 0, NPP); |
benkatz | 26:2b865c00d7e9 | 54 | |
benkatz | 26:2b865c00d7e9 | 55 | |
benkatz | 37:c0f352d6e8e3 | 56 | DigitalOut toggle(PC_8); |
benkatz | 20:bf9ea5125d52 | 57 | |
benkatz | 23:2adf23ee0305 | 58 | volatile int count = 0; |
benkatz | 23:2adf23ee0305 | 59 | volatile int state = REST_MODE; |
benkatz | 23:2adf23ee0305 | 60 | volatile int state_change; |
benkatz | 20:bf9ea5125d52 | 61 | |
benkatz | 26:2b865c00d7e9 | 62 | #define P_MIN -12.5f |
benkatz | 26:2b865c00d7e9 | 63 | #define P_MAX 12.5f |
benkatz | 37:c0f352d6e8e3 | 64 | #define V_MIN -45.0f |
benkatz | 37:c0f352d6e8e3 | 65 | #define V_MAX 45.0f |
benkatz | 26:2b865c00d7e9 | 66 | #define KP_MIN 0.0f |
benkatz | 26:2b865c00d7e9 | 67 | #define KP_MAX 500.0f |
benkatz | 26:2b865c00d7e9 | 68 | #define KD_MIN 0.0f |
benkatz | 28:8c7e29f719c5 | 69 | #define KD_MAX 5.0f |
benkatz | 26:2b865c00d7e9 | 70 | #define T_MIN -18.0f |
benkatz | 26:2b865c00d7e9 | 71 | #define T_MAX 18.0f |
benkatz | 26:2b865c00d7e9 | 72 | |
benkatz | 26:2b865c00d7e9 | 73 | |
benkatz | 26:2b865c00d7e9 | 74 | /// CAN Reply Packet Structure /// |
benkatz | 26:2b865c00d7e9 | 75 | /// 16 bit position, between -4*pi and 4*pi |
benkatz | 26:2b865c00d7e9 | 76 | /// 12 bit velocity, between -30 and + 30 rad/s |
benkatz | 26:2b865c00d7e9 | 77 | /// 12 bit current, between -40 and 40; |
benkatz | 26:2b865c00d7e9 | 78 | /// CAN Packet is 5 8-bit words |
benkatz | 26:2b865c00d7e9 | 79 | /// Formatted as follows. For each quantity, bit 0 is LSB |
benkatz | 26:2b865c00d7e9 | 80 | /// 0: [position[15-8]] |
benkatz | 26:2b865c00d7e9 | 81 | /// 1: [position[7-0]] |
benkatz | 26:2b865c00d7e9 | 82 | /// 2: [velocity[11-4]] |
benkatz | 26:2b865c00d7e9 | 83 | /// 3: [velocity[3-0], current[11-8]] |
benkatz | 26:2b865c00d7e9 | 84 | /// 4: [current[7-0]] |
benkatz | 34:51647c6c500d | 85 | void pack_reply(CANMessage *msg, float p, float v, float t){ |
benkatz | 26:2b865c00d7e9 | 86 | int p_int = float_to_uint(p, P_MIN, P_MAX, 16); |
benkatz | 26:2b865c00d7e9 | 87 | int v_int = float_to_uint(v, V_MIN, V_MAX, 12); |
benkatz | 34:51647c6c500d | 88 | int t_int = float_to_uint(t, -T_MAX, T_MAX, 12); |
benkatz | 28:8c7e29f719c5 | 89 | msg->data[0] = CAN_ID; |
benkatz | 28:8c7e29f719c5 | 90 | msg->data[1] = p_int>>8; |
benkatz | 28:8c7e29f719c5 | 91 | msg->data[2] = p_int&0xFF; |
benkatz | 28:8c7e29f719c5 | 92 | msg->data[3] = v_int>>4; |
benkatz | 34:51647c6c500d | 93 | msg->data[4] = ((v_int&0xF)<<4) + (t_int>>8); |
benkatz | 34:51647c6c500d | 94 | msg->data[5] = t_int&0xFF; |
benkatz | 26:2b865c00d7e9 | 95 | } |
benkatz | 26:2b865c00d7e9 | 96 | |
benkatz | 26:2b865c00d7e9 | 97 | /// CAN Command Packet Structure /// |
benkatz | 26:2b865c00d7e9 | 98 | /// 16 bit position command, between -4*pi and 4*pi |
benkatz | 26:2b865c00d7e9 | 99 | /// 12 bit velocity command, between -30 and + 30 rad/s |
benkatz | 26:2b865c00d7e9 | 100 | /// 12 bit kp, between 0 and 500 N-m/rad |
benkatz | 26:2b865c00d7e9 | 101 | /// 12 bit kd, between 0 and 100 N-m*s/rad |
benkatz | 26:2b865c00d7e9 | 102 | /// 12 bit feed forward torque, between -18 and 18 N-m |
benkatz | 26:2b865c00d7e9 | 103 | /// CAN Packet is 8 8-bit words |
benkatz | 26:2b865c00d7e9 | 104 | /// Formatted as follows. For each quantity, bit 0 is LSB |
benkatz | 26:2b865c00d7e9 | 105 | /// 0: [position[15-8]] |
benkatz | 26:2b865c00d7e9 | 106 | /// 1: [position[7-0]] |
benkatz | 26:2b865c00d7e9 | 107 | /// 2: [velocity[11-4]] |
benkatz | 26:2b865c00d7e9 | 108 | /// 3: [velocity[3-0], kp[11-8]] |
benkatz | 26:2b865c00d7e9 | 109 | /// 4: [kp[7-0]] |
benkatz | 26:2b865c00d7e9 | 110 | /// 5: [kd[11-4]] |
benkatz | 26:2b865c00d7e9 | 111 | /// 6: [kd[3-0], torque[11-8]] |
benkatz | 26:2b865c00d7e9 | 112 | /// 7: [torque[7-0]] |
benkatz | 26:2b865c00d7e9 | 113 | void unpack_cmd(CANMessage msg, ControllerStruct * controller){ |
benkatz | 28:8c7e29f719c5 | 114 | int p_int = (msg.data[0]<<8)|msg.data[1]; |
benkatz | 28:8c7e29f719c5 | 115 | int v_int = (msg.data[2]<<4)|(msg.data[3]>>4); |
benkatz | 28:8c7e29f719c5 | 116 | int kp_int = ((msg.data[3]&0xF)<<8)|msg.data[4]; |
benkatz | 28:8c7e29f719c5 | 117 | int kd_int = (msg.data[5]<<4)|(msg.data[6]>>4); |
benkatz | 28:8c7e29f719c5 | 118 | int t_int = ((msg.data[6]&0xF)<<8)|msg.data[7]; |
benkatz | 28:8c7e29f719c5 | 119 | |
benkatz | 28:8c7e29f719c5 | 120 | controller->p_des = uint_to_float(p_int, P_MIN, P_MAX, 16); |
benkatz | 28:8c7e29f719c5 | 121 | controller->v_des = uint_to_float(v_int, V_MIN, V_MAX, 12); |
benkatz | 28:8c7e29f719c5 | 122 | controller->kp = uint_to_float(kp_int, KP_MIN, KP_MAX, 12); |
benkatz | 28:8c7e29f719c5 | 123 | controller->kd = uint_to_float(kd_int, KD_MIN, KD_MAX, 12); |
benkatz | 28:8c7e29f719c5 | 124 | controller->t_ff = uint_to_float(t_int, T_MIN, T_MAX, 12); |
benkatz | 28:8c7e29f719c5 | 125 | //printf("Received "); |
benkatz | 28:8c7e29f719c5 | 126 | //printf("%.3f %.3f %.3f %.3f %.3f %.3f", controller->p_des, controller->v_des, controller->kp, controller->kd, controller->t_ff, controller->i_q_ref); |
benkatz | 28:8c7e29f719c5 | 127 | //printf("\n\r"); |
benkatz | 26:2b865c00d7e9 | 128 | } |
benkatz | 26:2b865c00d7e9 | 129 | |
benkatz | 26:2b865c00d7e9 | 130 | void onMsgReceived() { |
benkatz | 26:2b865c00d7e9 | 131 | //msgAvailable = true; |
benkatz | 26:2b865c00d7e9 | 132 | //printf("%.3f %.3f %.3f\n\r", controller.theta_mech, controller.dtheta_mech, controller.i_q); |
benkatz | 26:2b865c00d7e9 | 133 | can.read(rxMsg); |
benkatz | 28:8c7e29f719c5 | 134 | if((rxMsg.id == CAN_ID)){ |
benkatz | 28:8c7e29f719c5 | 135 | controller.timeout = 0; |
benkatz | 28:8c7e29f719c5 | 136 | 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 | 137 | state = MOTOR_MODE; |
benkatz | 28:8c7e29f719c5 | 138 | state_change = 1; |
benkatz | 28:8c7e29f719c5 | 139 | } |
benkatz | 28:8c7e29f719c5 | 140 | 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 | 141 | state = REST_MODE; |
benkatz | 28:8c7e29f719c5 | 142 | state_change = 1; |
benkatz | 37:c0f352d6e8e3 | 143 | gpio.led->write(0);; |
benkatz | 28:8c7e29f719c5 | 144 | } |
benkatz | 28:8c7e29f719c5 | 145 | 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 | 146 | spi.ZeroPosition(); |
benkatz | 28:8c7e29f719c5 | 147 | } |
benkatz | 28:8c7e29f719c5 | 148 | else if(state == MOTOR_MODE){ |
benkatz | 28:8c7e29f719c5 | 149 | unpack_cmd(rxMsg, &controller); |
benkatz | 28:8c7e29f719c5 | 150 | } |
benkatz | 37:c0f352d6e8e3 | 151 | pack_reply(&txMsg, controller.theta_mech, controller.dtheta_mech, controller.i_q_filt*KT_OUT); |
benkatz | 37:c0f352d6e8e3 | 152 | can.write(txMsg); |
benkatz | 28:8c7e29f719c5 | 153 | } |
benkatz | 26:2b865c00d7e9 | 154 | |
benkatz | 26:2b865c00d7e9 | 155 | } |
benkatz | 26:2b865c00d7e9 | 156 | |
benkatz | 23:2adf23ee0305 | 157 | void enter_menu_state(void){ |
benkatz | 23:2adf23ee0305 | 158 | printf("\n\r\n\r\n\r"); |
benkatz | 23:2adf23ee0305 | 159 | printf(" Commands:\n\r"); |
benkatz | 26:2b865c00d7e9 | 160 | printf(" m - Motor Mode\n\r"); |
benkatz | 23:2adf23ee0305 | 161 | printf(" c - Calibrate Encoder\n\r"); |
benkatz | 23:2adf23ee0305 | 162 | printf(" s - Setup\n\r"); |
benkatz | 23:2adf23ee0305 | 163 | printf(" e - Display Encoder\n\r"); |
benkatz | 37:c0f352d6e8e3 | 164 | printf(" z - Set Zero Position\n\r"); |
benkatz | 23:2adf23ee0305 | 165 | printf(" esc - Exit to Menu\n\r"); |
benkatz | 23:2adf23ee0305 | 166 | state_change = 0; |
benkatz | 25:f5741040c4bb | 167 | gpio.enable->write(0); |
benkatz | 37:c0f352d6e8e3 | 168 | gpio.led->write(0); |
benkatz | 23:2adf23ee0305 | 169 | } |
benkatz | 24:58c2d7571207 | 170 | |
benkatz | 24:58c2d7571207 | 171 | void enter_setup_state(void){ |
benkatz | 24:58c2d7571207 | 172 | printf("\n\r\n\r Configuration Options \n\r\n\n"); |
benkatz | 28:8c7e29f719c5 | 173 | printf(" %-4s %-31s %-5s %-6s %-5s\n\r\n\r", "prefix", "parameter", "min", "max", "current value"); |
benkatz | 28:8c7e29f719c5 | 174 | printf(" %-4s %-31s %-5s %-6s %.1f\n\r", "b", "Current Bandwidth (Hz)", "100", "2000", I_BW); |
benkatz | 28:8c7e29f719c5 | 175 | printf(" %-4s %-31s %-5s %-6s %-5i\n\r", "i", "CAN ID", "0", "127", CAN_ID); |
benkatz | 28:8c7e29f719c5 | 176 | printf(" %-4s %-31s %-5s %-6s %-5i\n\r", "m", "CAN Master ID", "0", "127", CAN_MASTER); |
benkatz | 28:8c7e29f719c5 | 177 | printf(" %-4s %-31s %-5s %-6s %.1f\n\r", "l", "Torque Limit (N-m)", "0.0", "18.0", TORQUE_LIMIT); |
benkatz | 28:8c7e29f719c5 | 178 | printf(" %-4s %-31s %-5s %-6s %d\n\r", "t", "CAN Timeout (cycles)(0 = none)", "0", "100000", CAN_TIMEOUT); |
benkatz | 24:58c2d7571207 | 179 | printf("\n\r To change a value, type 'prefix''value''ENTER'\n\r i.e. 'b1000''ENTER'\n\r\n\r"); |
benkatz | 24:58c2d7571207 | 180 | state_change = 0; |
benkatz | 24:58c2d7571207 | 181 | } |
benkatz | 22:60276ba87ac6 | 182 | |
benkatz | 23:2adf23ee0305 | 183 | void enter_torque_mode(void){ |
benkatz | 37:c0f352d6e8e3 | 184 | controller.ovp_flag = 0; |
benkatz | 28:8c7e29f719c5 | 185 | gpio.enable->write(1); // Enable gate drive |
benkatz | 28:8c7e29f719c5 | 186 | reset_foc(&controller); // Tesets integrators, and other control loop parameters |
benkatz | 28:8c7e29f719c5 | 187 | wait(.001); |
benkatz | 23:2adf23ee0305 | 188 | controller.i_d_ref = 0; |
benkatz | 28:8c7e29f719c5 | 189 | controller.i_q_ref = 0; // Current Setpoints |
benkatz | 37:c0f352d6e8e3 | 190 | gpio.led->write(1); // Turn on status LED |
benkatz | 25:f5741040c4bb | 191 | state_change = 0; |
benkatz | 28:8c7e29f719c5 | 192 | printf("\n\r Entering Motor Mode \n\r"); |
benkatz | 23:2adf23ee0305 | 193 | } |
benkatz | 22:60276ba87ac6 | 194 | |
benkatz | 23:2adf23ee0305 | 195 | void calibrate(void){ |
benkatz | 25:f5741040c4bb | 196 | gpio.enable->write(1); // Enable gate drive |
benkatz | 37:c0f352d6e8e3 | 197 | gpio.led->write(1); // Turn on status LED |
benkatz | 25:f5741040c4bb | 198 | order_phases(&spi, &gpio, &controller, &prefs); // Check phase ordering |
benkatz | 25:f5741040c4bb | 199 | calibrate(&spi, &gpio, &controller, &prefs); // Perform calibration procedure |
benkatz | 37:c0f352d6e8e3 | 200 | gpio.led->write(0);; // Turn off status LED |
benkatz | 23:2adf23ee0305 | 201 | wait(.2); |
benkatz | 25:f5741040c4bb | 202 | gpio.enable->write(0); // Turn off gate drive |
benkatz | 23:2adf23ee0305 | 203 | printf("\n\r Calibration complete. Press 'esc' to return to menu\n\r"); |
benkatz | 23:2adf23ee0305 | 204 | state_change = 0; |
benkatz | 23:2adf23ee0305 | 205 | } |
benkatz | 23:2adf23ee0305 | 206 | |
benkatz | 23:2adf23ee0305 | 207 | void print_encoder(void){ |
benkatz | 23:2adf23ee0305 | 208 | printf(" Mechanical Angle: %f Electrical Angle: %f Raw: %d\n\r", spi.GetMechPosition(), spi.GetElecPosition(), spi.GetRawPosition()); |
benkatz | 23:2adf23ee0305 | 209 | wait(.05); |
benkatz | 22:60276ba87ac6 | 210 | } |
benkatz | 20:bf9ea5125d52 | 211 | |
benkatz | 23:2adf23ee0305 | 212 | /// Current Sampling Interrupt /// |
benkatz | 23:2adf23ee0305 | 213 | /// This runs at 40 kHz, regardless of of the mode the controller is in /// |
benkatz | 2:8724412ad628 | 214 | extern "C" void TIM1_UP_TIM10_IRQHandler(void) { |
benkatz | 2:8724412ad628 | 215 | if (TIM1->SR & TIM_SR_UIF ) { |
benkatz | 23:2adf23ee0305 | 216 | |
benkatz | 23:2adf23ee0305 | 217 | ///Sample current always /// |
benkatz | 25:f5741040c4bb | 218 | ADC1->CR2 |= 0x40000000; // Begin sample and conversion |
benkatz | 22:60276ba87ac6 | 219 | //volatile int delay; |
benkatz | 20:bf9ea5125d52 | 220 | //for (delay = 0; delay < 55; delay++); |
benkatz | 37:c0f352d6e8e3 | 221 | controller.adc2_raw = ADC2->DR; // Read ADC Data Registers |
benkatz | 23:2adf23ee0305 | 222 | controller.adc1_raw = ADC1->DR; |
benkatz | 37:c0f352d6e8e3 | 223 | controller.adc3_raw = ADC3->DR; |
benkatz | 37:c0f352d6e8e3 | 224 | spi.Sample(); // sample position sensor |
benkatz | 37:c0f352d6e8e3 | 225 | controller.theta_elec = spi.GetElecPosition(); |
benkatz | 37:c0f352d6e8e3 | 226 | controller.theta_mech = (1.0f/GR)*spi.GetMechPosition(); |
benkatz | 37:c0f352d6e8e3 | 227 | controller.dtheta_mech = (1.0f/GR)*spi.GetMechVelocity(); |
benkatz | 37:c0f352d6e8e3 | 228 | controller.dtheta_elec = spi.GetElecVelocity(); |
benkatz | 37:c0f352d6e8e3 | 229 | controller.v_bus = 0.95f*controller.v_bus + 0.05f*((float)controller.adc3_raw)*V_SCALE; |
benkatz | 23:2adf23ee0305 | 230 | /// |
benkatz | 20:bf9ea5125d52 | 231 | |
benkatz | 23:2adf23ee0305 | 232 | /// Check state machine state, and run the appropriate function /// |
benkatz | 23:2adf23ee0305 | 233 | switch(state){ |
benkatz | 37:c0f352d6e8e3 | 234 | case REST_MODE: // Do nothing |
benkatz | 23:2adf23ee0305 | 235 | if(state_change){ |
benkatz | 23:2adf23ee0305 | 236 | enter_menu_state(); |
benkatz | 23:2adf23ee0305 | 237 | } |
benkatz | 23:2adf23ee0305 | 238 | break; |
benkatz | 22:60276ba87ac6 | 239 | |
benkatz | 23:2adf23ee0305 | 240 | case CALIBRATION_MODE: // Run encoder calibration procedure |
benkatz | 23:2adf23ee0305 | 241 | if(state_change){ |
benkatz | 23:2adf23ee0305 | 242 | calibrate(); |
benkatz | 23:2adf23ee0305 | 243 | } |
benkatz | 23:2adf23ee0305 | 244 | break; |
benkatz | 23:2adf23ee0305 | 245 | |
benkatz | 26:2b865c00d7e9 | 246 | case MOTOR_MODE: // Run torque control |
benkatz | 25:f5741040c4bb | 247 | if(state_change){ |
benkatz | 25:f5741040c4bb | 248 | enter_torque_mode(); |
benkatz | 28:8c7e29f719c5 | 249 | count = 0; |
benkatz | 25:f5741040c4bb | 250 | } |
benkatz | 28:8c7e29f719c5 | 251 | else{ |
benkatz | 37:c0f352d6e8e3 | 252 | /* |
benkatz | 37:c0f352d6e8e3 | 253 | 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 | 37:c0f352d6e8e3 | 254 | gpio.enable->write(0); |
benkatz | 37:c0f352d6e8e3 | 255 | controller.ovp_flag = 1; |
benkatz | 37:c0f352d6e8e3 | 256 | state = REST_MODE; |
benkatz | 37:c0f352d6e8e3 | 257 | state_change = 1; |
benkatz | 37:c0f352d6e8e3 | 258 | printf("OVP Triggered!\n\r"); |
benkatz | 37:c0f352d6e8e3 | 259 | } |
benkatz | 37:c0f352d6e8e3 | 260 | */ |
benkatz | 37:c0f352d6e8e3 | 261 | |
benkatz | 28:8c7e29f719c5 | 262 | torque_control(&controller); |
benkatz | 28:8c7e29f719c5 | 263 | if((controller.timeout > CAN_TIMEOUT) && (CAN_TIMEOUT > 0)){ |
benkatz | 28:8c7e29f719c5 | 264 | controller.i_d_ref = 0; |
benkatz | 28:8c7e29f719c5 | 265 | controller.i_q_ref = 0; |
benkatz | 37:c0f352d6e8e3 | 266 | controller.kp = 0; |
benkatz | 37:c0f352d6e8e3 | 267 | controller.kd = 0; |
benkatz | 37:c0f352d6e8e3 | 268 | controller.t_ff = 0; |
benkatz | 28:8c7e29f719c5 | 269 | } |
benkatz | 37:c0f352d6e8e3 | 270 | commutate(&controller, &observer, &gpio, controller.theta_elec); // Run current loop |
benkatz | 28:8c7e29f719c5 | 271 | controller.timeout += 1; |
benkatz | 37:c0f352d6e8e3 | 272 | /* |
benkatz | 37:c0f352d6e8e3 | 273 | count++; |
benkatz | 34:51647c6c500d | 274 | if(count == 1000){ |
benkatz | 32:ccac5da77844 | 275 | count = 0; |
benkatz | 23:2adf23ee0305 | 276 | } |
benkatz | 37:c0f352d6e8e3 | 277 | */ |
benkatz | 37:c0f352d6e8e3 | 278 | |
benkatz | 37:c0f352d6e8e3 | 279 | } |
benkatz | 23:2adf23ee0305 | 280 | break; |
benkatz | 23:2adf23ee0305 | 281 | case SETUP_MODE: |
benkatz | 23:2adf23ee0305 | 282 | if(state_change){ |
benkatz | 24:58c2d7571207 | 283 | enter_setup_state(); |
benkatz | 23:2adf23ee0305 | 284 | } |
benkatz | 23:2adf23ee0305 | 285 | break; |
benkatz | 23:2adf23ee0305 | 286 | case ENCODER_MODE: |
benkatz | 23:2adf23ee0305 | 287 | print_encoder(); |
benkatz | 23:2adf23ee0305 | 288 | break; |
benkatz | 37:c0f352d6e8e3 | 289 | } |
benkatz | 2:8724412ad628 | 290 | } |
benkatz | 23:2adf23ee0305 | 291 | TIM1->SR = 0x0; // reset the status register |
benkatz | 2:8724412ad628 | 292 | } |
benkatz | 0:4e1c4df6aabd | 293 | |
benkatz | 25:f5741040c4bb | 294 | |
benkatz | 24:58c2d7571207 | 295 | char cmd_val[8] = {0}; |
benkatz | 24:58c2d7571207 | 296 | char cmd_id = 0; |
benkatz | 25:f5741040c4bb | 297 | char char_count = 0; |
benkatz | 24:58c2d7571207 | 298 | |
benkatz | 25:f5741040c4bb | 299 | /// Manage state machine with commands from serial terminal or configurator gui /// |
benkatz | 25:f5741040c4bb | 300 | /// Called when data received over serial /// |
benkatz | 23:2adf23ee0305 | 301 | void serial_interrupt(void){ |
benkatz | 23:2adf23ee0305 | 302 | while(pc.readable()){ |
benkatz | 23:2adf23ee0305 | 303 | char c = pc.getc(); |
benkatz | 25:f5741040c4bb | 304 | if(c == 27){ |
benkatz | 25:f5741040c4bb | 305 | state = REST_MODE; |
benkatz | 25:f5741040c4bb | 306 | state_change = 1; |
benkatz | 25:f5741040c4bb | 307 | char_count = 0; |
benkatz | 25:f5741040c4bb | 308 | cmd_id = 0; |
benkatz | 37:c0f352d6e8e3 | 309 | gpio.led->write(0);; |
benkatz | 25:f5741040c4bb | 310 | for(int i = 0; i<8; i++){cmd_val[i] = 0;} |
benkatz | 25:f5741040c4bb | 311 | } |
benkatz | 24:58c2d7571207 | 312 | if(state == REST_MODE){ |
benkatz | 23:2adf23ee0305 | 313 | switch (c){ |
benkatz | 23:2adf23ee0305 | 314 | case 'c': |
benkatz | 23:2adf23ee0305 | 315 | state = CALIBRATION_MODE; |
benkatz | 23:2adf23ee0305 | 316 | state_change = 1; |
benkatz | 23:2adf23ee0305 | 317 | break; |
benkatz | 26:2b865c00d7e9 | 318 | case 'm': |
benkatz | 26:2b865c00d7e9 | 319 | state = MOTOR_MODE; |
benkatz | 23:2adf23ee0305 | 320 | state_change = 1; |
benkatz | 23:2adf23ee0305 | 321 | break; |
benkatz | 23:2adf23ee0305 | 322 | case 'e': |
benkatz | 23:2adf23ee0305 | 323 | state = ENCODER_MODE; |
benkatz | 23:2adf23ee0305 | 324 | state_change = 1; |
benkatz | 23:2adf23ee0305 | 325 | break; |
benkatz | 23:2adf23ee0305 | 326 | case 's': |
benkatz | 23:2adf23ee0305 | 327 | state = SETUP_MODE; |
benkatz | 23:2adf23ee0305 | 328 | state_change = 1; |
benkatz | 23:2adf23ee0305 | 329 | break; |
benkatz | 37:c0f352d6e8e3 | 330 | case 'z': |
benkatz | 37:c0f352d6e8e3 | 331 | spi.SetMechOffset(0); |
benkatz | 37:c0f352d6e8e3 | 332 | spi.Sample(); |
benkatz | 37:c0f352d6e8e3 | 333 | wait_us(20); |
benkatz | 37:c0f352d6e8e3 | 334 | M_OFFSET = spi.GetMechPosition(); |
benkatz | 37:c0f352d6e8e3 | 335 | if (!prefs.ready()) prefs.open(); |
benkatz | 37:c0f352d6e8e3 | 336 | prefs.flush(); // Write new prefs to flash |
benkatz | 37:c0f352d6e8e3 | 337 | prefs.close(); |
benkatz | 37:c0f352d6e8e3 | 338 | prefs.load(); |
benkatz | 37:c0f352d6e8e3 | 339 | spi.SetMechOffset(M_OFFSET); |
benkatz | 37:c0f352d6e8e3 | 340 | printf("\n\r Saved new zero position: %.4f\n\r\n\r", M_OFFSET); |
benkatz | 37:c0f352d6e8e3 | 341 | |
benkatz | 37:c0f352d6e8e3 | 342 | break; |
benkatz | 37:c0f352d6e8e3 | 343 | } |
benkatz | 37:c0f352d6e8e3 | 344 | |
benkatz | 24:58c2d7571207 | 345 | } |
benkatz | 24:58c2d7571207 | 346 | else if(state == SETUP_MODE){ |
benkatz | 25:f5741040c4bb | 347 | if(c == 13){ |
benkatz | 24:58c2d7571207 | 348 | switch (cmd_id){ |
benkatz | 24:58c2d7571207 | 349 | case 'b': |
benkatz | 24:58c2d7571207 | 350 | I_BW = fmaxf(fminf(atof(cmd_val), 2000.0f), 100.0f); |
benkatz | 24:58c2d7571207 | 351 | break; |
benkatz | 24:58c2d7571207 | 352 | case 'i': |
benkatz | 24:58c2d7571207 | 353 | CAN_ID = atoi(cmd_val); |
benkatz | 24:58c2d7571207 | 354 | break; |
benkatz | 26:2b865c00d7e9 | 355 | case 'm': |
benkatz | 26:2b865c00d7e9 | 356 | CAN_MASTER = atoi(cmd_val); |
benkatz | 26:2b865c00d7e9 | 357 | break; |
benkatz | 24:58c2d7571207 | 358 | case 'l': |
benkatz | 24:58c2d7571207 | 359 | TORQUE_LIMIT = fmaxf(fminf(atof(cmd_val), 18.0f), 0.0f); |
benkatz | 24:58c2d7571207 | 360 | break; |
benkatz | 28:8c7e29f719c5 | 361 | case 't': |
benkatz | 28:8c7e29f719c5 | 362 | CAN_TIMEOUT = atoi(cmd_val); |
benkatz | 28:8c7e29f719c5 | 363 | break; |
benkatz | 24:58c2d7571207 | 364 | default: |
benkatz | 24:58c2d7571207 | 365 | printf("\n\r '%c' Not a valid command prefix\n\r\n\r", cmd_id); |
benkatz | 24:58c2d7571207 | 366 | break; |
benkatz | 24:58c2d7571207 | 367 | } |
benkatz | 24:58c2d7571207 | 368 | |
benkatz | 24:58c2d7571207 | 369 | if (!prefs.ready()) prefs.open(); |
benkatz | 24:58c2d7571207 | 370 | prefs.flush(); // Write new prefs to flash |
benkatz | 24:58c2d7571207 | 371 | prefs.close(); |
benkatz | 24:58c2d7571207 | 372 | prefs.load(); |
benkatz | 24:58c2d7571207 | 373 | state_change = 1; |
benkatz | 24:58c2d7571207 | 374 | char_count = 0; |
benkatz | 24:58c2d7571207 | 375 | cmd_id = 0; |
benkatz | 24:58c2d7571207 | 376 | for(int i = 0; i<8; i++){cmd_val[i] = 0;} |
benkatz | 24:58c2d7571207 | 377 | } |
benkatz | 24:58c2d7571207 | 378 | else{ |
benkatz | 24:58c2d7571207 | 379 | if(char_count == 0){cmd_id = c;} |
benkatz | 24:58c2d7571207 | 380 | else{ |
benkatz | 24:58c2d7571207 | 381 | cmd_val[char_count-1] = c; |
benkatz | 24:58c2d7571207 | 382 | |
benkatz | 24:58c2d7571207 | 383 | } |
benkatz | 24:58c2d7571207 | 384 | pc.putc(c); |
benkatz | 24:58c2d7571207 | 385 | char_count++; |
benkatz | 23:2adf23ee0305 | 386 | } |
benkatz | 23:2adf23ee0305 | 387 | } |
benkatz | 24:58c2d7571207 | 388 | else if (state == ENCODER_MODE){ |
benkatz | 24:58c2d7571207 | 389 | switch (c){ |
benkatz | 24:58c2d7571207 | 390 | case 27: |
benkatz | 24:58c2d7571207 | 391 | state = REST_MODE; |
benkatz | 24:58c2d7571207 | 392 | state_change = 1; |
benkatz | 24:58c2d7571207 | 393 | break; |
benkatz | 24:58c2d7571207 | 394 | } |
benkatz | 24:58c2d7571207 | 395 | } |
benkatz | 24:58c2d7571207 | 396 | |
benkatz | 24:58c2d7571207 | 397 | } |
benkatz | 22:60276ba87ac6 | 398 | } |
benkatz | 0:4e1c4df6aabd | 399 | |
benkatz | 0:4e1c4df6aabd | 400 | int main() { |
benkatz | 23:2adf23ee0305 | 401 | |
benkatz | 20:bf9ea5125d52 | 402 | controller.v_bus = V_BUS; |
benkatz | 22:60276ba87ac6 | 403 | controller.mode = 0; |
benkatz | 23:2adf23ee0305 | 404 | Init_All_HW(&gpio); // Setup PWM, ADC, GPIO |
benkatz | 20:bf9ea5125d52 | 405 | |
benkatz | 9:d7eb815cb057 | 406 | wait(.1); |
benkatz | 26:2b865c00d7e9 | 407 | gpio.enable->write(1); |
benkatz | 26:2b865c00d7e9 | 408 | TIM1->CCR3 = 0x708*(1.0f); // Write duty cycles |
benkatz | 26:2b865c00d7e9 | 409 | TIM1->CCR2 = 0x708*(1.0f); |
benkatz | 26:2b865c00d7e9 | 410 | TIM1->CCR1 = 0x708*(1.0f); |
benkatz | 23:2adf23ee0305 | 411 | zero_current(&controller.adc1_offset, &controller.adc2_offset); // Measure current sensor zero-offset |
benkatz | 26:2b865c00d7e9 | 412 | gpio.enable->write(0); |
benkatz | 23:2adf23ee0305 | 413 | reset_foc(&controller); // Reset current controller |
benkatz | 26:2b865c00d7e9 | 414 | TIM1->CR1 ^= TIM_CR1_UDIS; |
benkatz | 26:2b865c00d7e9 | 415 | //TIM1->CR1 |= TIM_CR1_UDIS; //enable interrupt |
benkatz | 20:bf9ea5125d52 | 416 | |
benkatz | 20:bf9ea5125d52 | 417 | wait(.1); |
benkatz | 37:c0f352d6e8e3 | 418 | NVIC_SetPriority(TIM1_UP_TIM10_IRQn, 2); // commutation > communication |
benkatz | 37:c0f352d6e8e3 | 419 | NVIC_SetPriority(CAN1_RX0_IRQn, 3); |
benkatz | 22:60276ba87ac6 | 420 | |
benkatz | 26:2b865c00d7e9 | 421 | |
benkatz | 23:2adf23ee0305 | 422 | can.frequency(1000000); // set bit rate to 1Mbps |
benkatz | 26:2b865c00d7e9 | 423 | can.filter(CAN_ID<<21, 0xFFE00004, CANStandard, 0); |
benkatz | 26:2b865c00d7e9 | 424 | //can.filter(CAN_ID, 0xF, CANStandard, 0); |
benkatz | 26:2b865c00d7e9 | 425 | can.attach(&onMsgReceived); // attach 'CAN receive-complete' interrupt handler |
benkatz | 28:8c7e29f719c5 | 426 | txMsg.id = CAN_MASTER; |
benkatz | 28:8c7e29f719c5 | 427 | txMsg.len = 6; |
benkatz | 26:2b865c00d7e9 | 428 | rxMsg.len = 8; |
benkatz | 23:2adf23ee0305 | 429 | |
benkatz | 25:f5741040c4bb | 430 | prefs.load(); // Read flash |
benkatz | 37:c0f352d6e8e3 | 431 | if(isnan(E_OFFSET)){E_OFFSET = 0.0f;} |
benkatz | 37:c0f352d6e8e3 | 432 | if(isnan(M_OFFSET)){M_OFFSET = 0.0f;} |
benkatz | 25:f5741040c4bb | 433 | spi.SetElecOffset(E_OFFSET); // Set position sensor offset |
benkatz | 37:c0f352d6e8e3 | 434 | spi.SetMechOffset(M_OFFSET); |
benkatz | 23:2adf23ee0305 | 435 | int lut[128] = {0}; |
benkatz | 23:2adf23ee0305 | 436 | memcpy(&lut, &ENCODER_LUT, sizeof(lut)); |
benkatz | 25:f5741040c4bb | 437 | spi.WriteLUT(lut); // Set potision sensor nonlinearity lookup table |
benkatz | 23:2adf23ee0305 | 438 | |
benkatz | 26:2b865c00d7e9 | 439 | pc.baud(921600); // set serial baud rate |
benkatz | 20:bf9ea5125d52 | 440 | wait(.01); |
benkatz | 23:2adf23ee0305 | 441 | pc.printf("\n\r\n\r HobbyKing Cheetah\n\r\n\r"); |
benkatz | 20:bf9ea5125d52 | 442 | wait(.01); |
benkatz | 23:2adf23ee0305 | 443 | printf("\n\r Debug Info:\n\r"); |
benkatz | 32:ccac5da77844 | 444 | printf(" Firmware Version: %s\n\r", VERSION_NUM); |
benkatz | 23:2adf23ee0305 | 445 | printf(" ADC1 Offset: %d ADC2 Offset: %d\n\r", controller.adc1_offset, controller.adc2_offset); |
benkatz | 23:2adf23ee0305 | 446 | printf(" Position Sensor Electrical Offset: %.4f\n\r", E_OFFSET); |
benkatz | 37:c0f352d6e8e3 | 447 | printf(" Output Zero Position: %.4f\n\r", M_OFFSET); |
benkatz | 24:58c2d7571207 | 448 | printf(" CAN ID: %d\n\r", CAN_ID); |
benkatz | 23:2adf23ee0305 | 449 | |
benkatz | 23:2adf23ee0305 | 450 | pc.attach(&serial_interrupt); // attach serial interrupt |
benkatz | 22:60276ba87ac6 | 451 | |
benkatz | 23:2adf23ee0305 | 452 | state_change = 1; |
benkatz | 20:bf9ea5125d52 | 453 | |
benkatz | 22:60276ba87ac6 | 454 | |
benkatz | 0:4e1c4df6aabd | 455 | while(1) { |
benkatz | 11:c83b18d41e54 | 456 | |
benkatz | 0:4e1c4df6aabd | 457 | } |
benkatz | 0:4e1c4df6aabd | 458 | } |