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