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