it works!

Dependencies:   mbed-dev-f303 FastPWM3

Committer:
benkatz
Date:
Mon May 01 15:22:58 2017 +0000
Revision:
26:2b865c00d7e9
Parent:
25:f5741040c4bb
Child:
27:501fee691e0e
- Added CAN Send/Receive; - Updated to most recent MBED (Broke SPI 16-bit read/write.  Hacked to work with 2 8-bit writes now); - Removed most sine/cos calculations; - Fixed sign error in autocalibration routine

Who changed what in which revision?

UserRevisionLine numberNew contents of line
benkatz 24:58c2d7571207 1 #include "user_config.h"
benkatz 22:60276ba87ac6 2 #include "foc.h"
benkatz 22:60276ba87ac6 3
benkatz 26:2b865c00d7e9 4 #include "FastMath.h"
benkatz 26:2b865c00d7e9 5 using namespace FastMath;
benkatz 22:60276ba87ac6 6
benkatz 22:60276ba87ac6 7
benkatz 22:60276ba87ac6 8 void abc( float theta, float d, float q, float *a, float *b, float *c){
benkatz 25:f5741040c4bb 9 /// Inverse DQ0 Transform ///
benkatz 22:60276ba87ac6 10 ///Phase current amplitude = lengh of dq vector///
benkatz 22:60276ba87ac6 11 ///i.e. iq = 1, id = 0, peak phase current of 1///
benkatz 22:60276ba87ac6 12
benkatz 26:2b865c00d7e9 13 *a = d*cosf(theta) - q*sinf(theta);
benkatz 26:2b865c00d7e9 14 *b = d*cosf(theta - (2.0f*PI/3.0f)) - q*sinf(theta - (2.0f*PI/3.0f));
benkatz 26:2b865c00d7e9 15 *c = d*cosf(theta + (2.0f*PI/3.0f)) - q*sinf(theta +(2.0f*PI/3.0f));
benkatz 22:60276ba87ac6 16 }
benkatz 22:60276ba87ac6 17
benkatz 26:2b865c00d7e9 18
benkatz 22:60276ba87ac6 19 void dq0(float theta, float a, float b, float c, float *d, float *q){
benkatz 25:f5741040c4bb 20 /// DQ0 Transform ///
benkatz 22:60276ba87ac6 21 ///Phase current amplitude = lengh of dq vector///
benkatz 22:60276ba87ac6 22 ///i.e. iq = 1, id = 0, peak phase current of 1///
benkatz 22:60276ba87ac6 23
benkatz 26:2b865c00d7e9 24 //float cos = cosf(theta);
benkatz 26:2b865c00d7e9 25 //float sin = sinf(theta);
benkatz 26:2b865c00d7e9 26
benkatz 26:2b865c00d7e9 27 *d = (2.0f/3.0f)*(a*cosf(theta) + b*cosf(theta - (2.0f*PI/3.0f)) + c*cosf(theta + (2.0f*PI/3.0f)));
benkatz 26:2b865c00d7e9 28 *q = (2.0f/3.0f)*(-a*sinf(theta) - b*sinf(theta - (2.0f*PI/3.0f)) - c*sinf(theta + (2.0f*PI/3.0f)));
benkatz 22:60276ba87ac6 29 }
benkatz 22:60276ba87ac6 30
benkatz 22:60276ba87ac6 31 void svm(float v_bus, float u, float v, float w, float *dtc_u, float *dtc_v, float *dtc_w){
benkatz 25:f5741040c4bb 32 /// Space Vector Modulation ///
benkatz 25:f5741040c4bb 33 /// u,v,w amplitude = v_bus for full modulation depth ///
benkatz 22:60276ba87ac6 34
benkatz 22:60276ba87ac6 35 float v_offset = (fminf3(u, v, w) + fmaxf3(u, v, w))/2.0f;
benkatz 23:2adf23ee0305 36 *dtc_u = fminf(fmaxf(((u - v_offset)*0.5f/v_bus + ((DTC_MAX-DTC_MIN)/2)), DTC_MIN), DTC_MAX);
benkatz 23:2adf23ee0305 37 *dtc_v = fminf(fmaxf(((v - v_offset)*0.5f/v_bus + ((DTC_MAX-DTC_MIN)/2)), DTC_MIN), DTC_MAX);
benkatz 26:2b865c00d7e9 38 *dtc_w = fminf(fmaxf(((w - v_offset)*0.5f/v_bus + ((DTC_MAX-DTC_MIN)/2)), DTC_MIN), DTC_MAX);
benkatz 22:60276ba87ac6 39
benkatz 22:60276ba87ac6 40 }
benkatz 22:60276ba87ac6 41
benkatz 25:f5741040c4bb 42 void zero_current(int *offset_1, int *offset_2){ // Measure zero-offset of the current sensors
benkatz 22:60276ba87ac6 43 int adc1_offset = 0;
benkatz 22:60276ba87ac6 44 int adc2_offset = 0;
benkatz 22:60276ba87ac6 45 int n = 1024;
benkatz 25:f5741040c4bb 46 for (int i = 0; i<n; i++){ // Average n samples of the ADC
benkatz 26:2b865c00d7e9 47 TIM1->CCR3 = 0x708*(1.0f); // Write duty cycles
benkatz 26:2b865c00d7e9 48 TIM1->CCR2 = 0x708*(1.0f);
benkatz 26:2b865c00d7e9 49 TIM1->CCR1 = 0x708*(1.0f);
benkatz 26:2b865c00d7e9 50 ADC1->CR2 |= 0x40000000; // Begin sample and conversion
benkatz 22:60276ba87ac6 51 wait(.001);
benkatz 22:60276ba87ac6 52 adc2_offset += ADC2->DR;
benkatz 22:60276ba87ac6 53 adc1_offset += ADC1->DR;
benkatz 22:60276ba87ac6 54 }
benkatz 22:60276ba87ac6 55 *offset_1 = adc1_offset/n;
benkatz 22:60276ba87ac6 56 *offset_2 = adc2_offset/n;
benkatz 22:60276ba87ac6 57 }
benkatz 22:60276ba87ac6 58
benkatz 22:60276ba87ac6 59 void reset_foc(ControllerStruct *controller){
benkatz 22:60276ba87ac6 60 controller->q_int = 0;
benkatz 22:60276ba87ac6 61 controller->d_int = 0;
benkatz 22:60276ba87ac6 62 }
benkatz 22:60276ba87ac6 63
benkatz 22:60276ba87ac6 64
benkatz 22:60276ba87ac6 65 void commutate(ControllerStruct *controller, GPIOStruct *gpio, float theta){
benkatz 25:f5741040c4bb 66 /// Commutation Loop ///
benkatz 26:2b865c00d7e9 67
benkatz 25:f5741040c4bb 68 controller->loop_count ++;
benkatz 25:f5741040c4bb 69 if(PHASE_ORDER){ // Check current sensor ordering
benkatz 25:f5741040c4bb 70 controller->i_b = I_SCALE*(float)(controller->adc2_raw - controller->adc2_offset); // Calculate phase currents from ADC readings
benkatz 22:60276ba87ac6 71 controller->i_c = I_SCALE*(float)(controller->adc1_raw - controller->adc1_offset);
benkatz 22:60276ba87ac6 72 }
benkatz 22:60276ba87ac6 73 else{
benkatz 25:f5741040c4bb 74 controller->i_b = I_SCALE*(float)(controller->adc1_raw - controller->adc1_offset);
benkatz 22:60276ba87ac6 75 controller->i_c = I_SCALE*(float)(controller->adc2_raw - controller->adc2_offset);
benkatz 22:60276ba87ac6 76 }
benkatz 26:2b865c00d7e9 77 controller->i_a = -controller->i_b - controller->i_c;
benkatz 22:60276ba87ac6 78
benkatz 26:2b865c00d7e9 79 float s = FastSin(theta);
benkatz 26:2b865c00d7e9 80 float c = FastCos(theta);
benkatz 26:2b865c00d7e9 81 //dq0(controller->theta_elec, controller->i_a, controller->i_b, controller->i_c, &controller->i_d, &controller->i_q); //dq0 transform on currents
benkatz 26:2b865c00d7e9 82 controller->i_d = 0.6666667f*(c*controller->i_a + (0.86602540378f*s-.5f*c)*controller->i_b + (-0.86602540378f*s-.5f*c)*controller->i_c); ///Fast DQ0 Transform
benkatz 26:2b865c00d7e9 83 controller->i_q = 0.6666667f*(-s*controller->i_a - (-0.86602540378f*c-.5f*s)*controller->i_b - (0.86602540378f*c-.5f*s)*controller->i_c);
benkatz 25:f5741040c4bb 84 ///Cogging compensation lookup, doesn't actually work yet///
benkatz 23:2adf23ee0305 85 //int ind = theta * (128.0f/(2.0f*PI));
benkatz 23:2adf23ee0305 86 //float cogging_current = controller->cogging[ind];
benkatz 26:2b865c00d7e9 87 float cogging_current = 0.05f*s*controller->i_q_ref;
benkatz 25:f5741040c4bb 88
benkatz 25:f5741040c4bb 89 /// PI Controller ///
benkatz 22:60276ba87ac6 90 float i_d_error = controller->i_d_ref - controller->i_d;
benkatz 23:2adf23ee0305 91 float i_q_error = controller->i_q_ref - controller->i_q;// + cogging_current;
benkatz 22:60276ba87ac6 92 float v_d_ff = 2.0f*(2*controller->i_d_ref*R_PHASE); //feed-forward voltage
benkatz 26:2b865c00d7e9 93 float v_q_ff = controller->dtheta_elec*WB*1.73205081;
benkatz 22:60276ba87ac6 94 controller->d_int += i_d_error;
benkatz 22:60276ba87ac6 95 controller->q_int += i_q_error;
benkatz 22:60276ba87ac6 96
benkatz 22:60276ba87ac6 97 //v_d_ff = 0;
benkatz 22:60276ba87ac6 98 //v_q_ff = 0;
benkatz 22:60276ba87ac6 99
benkatz 25:f5741040c4bb 100 limit_norm(&controller->d_int, &controller->q_int, V_BUS/(K_Q*KI_Q)); // Limit integrators to prevent windup
benkatz 25:f5741040c4bb 101 controller->v_d = K_SCALE*I_BW*i_d_error + K_SCALE*I_BW*controller->d_int;// + v_d_ff;
benkatz 25:f5741040c4bb 102 controller->v_q = K_SCALE*I_BW*i_q_error + K_SCALE*I_BW*controller->q_int;// + v_q_ff;
benkatz 22:60276ba87ac6 103
benkatz 22:60276ba87ac6 104 //controller->v_d = v_d_ff;
benkatz 22:60276ba87ac6 105 //controller->v_q = v_q_ff;
benkatz 22:60276ba87ac6 106
benkatz 25:f5741040c4bb 107 limit_norm(&controller->v_d, &controller->v_q, controller->v_bus); // Normalize voltage vector to lie within curcle of radius v_bus
benkatz 26:2b865c00d7e9 108 //abc(controller->theta_elec, controller->v_d, controller->v_q, &controller->v_u, &controller->v_v, &controller->v_w); //inverse dq0 transform on voltages
benkatz 26:2b865c00d7e9 109
benkatz 26:2b865c00d7e9 110 controller->v_u = c*controller->v_d - s*controller->v_q;
benkatz 26:2b865c00d7e9 111 controller->v_v = (0.86602540378f*s-.5f*c)*controller->v_d - (-0.86602540378f*c-.5f*s)*controller->v_q;
benkatz 26:2b865c00d7e9 112 controller->v_w = (-0.86602540378f*s-.5f*c)*controller->v_d - (0.86602540378f*c-.5f*s)*controller->v_q;
benkatz 22:60276ba87ac6 113 svm(controller->v_bus, controller->v_u, controller->v_v, controller->v_w, &controller->dtc_u, &controller->dtc_v, &controller->dtc_w); //space vector modulation
benkatz 22:60276ba87ac6 114
benkatz 22:60276ba87ac6 115
benkatz 25:f5741040c4bb 116 if(PHASE_ORDER){ // Check which phase order to use,
benkatz 25:f5741040c4bb 117 TIM1->CCR3 = 0x708*(1.0f-controller->dtc_u); // Write duty cycles
benkatz 22:60276ba87ac6 118 TIM1->CCR2 = 0x708*(1.0f-controller->dtc_v);
benkatz 22:60276ba87ac6 119 TIM1->CCR1 = 0x708*(1.0f-controller->dtc_w);
benkatz 22:60276ba87ac6 120 }
benkatz 22:60276ba87ac6 121 else{
benkatz 22:60276ba87ac6 122 TIM1->CCR3 = 0x708*(1.0f-controller->dtc_u);
benkatz 22:60276ba87ac6 123 TIM1->CCR1 = 0x708*(1.0f-controller->dtc_v);
benkatz 22:60276ba87ac6 124 TIM1->CCR2 = 0x708*(1.0f-controller->dtc_w);
benkatz 22:60276ba87ac6 125 }
benkatz 25:f5741040c4bb 126
benkatz 25:f5741040c4bb 127 controller->theta_elec = theta; //For some reason putting this at the front breaks thins
benkatz 22:60276ba87ac6 128
benkatz 22:60276ba87ac6 129
benkatz 26:2b865c00d7e9 130 if(controller->loop_count >400){
benkatz 22:60276ba87ac6 131 //controller->i_q_ref = -controller->i_q_ref;
benkatz 26:2b865c00d7e9 132 controller->loop_count = 0;
benkatz 22:60276ba87ac6 133
benkatz 26:2b865c00d7e9 134 //printf("%.1f %.1f %.1f %.1f %.1f\n\r", controller->i_d, controller->i_q, controller->v_d, controller->v_q, controller->dtheta_mech);
benkatz 22:60276ba87ac6 135 //printf("%f\n\r", controller->theta_elec);
benkatz 22:60276ba87ac6 136 //pc.printf("%f %f %f\n\r", controller->i_a, controller->i_b, controller->i_c);
benkatz 22:60276ba87ac6 137 //pc.printf("%f %f\n\r", controller->i_d, controller->i_q);
benkatz 22:60276ba87ac6 138 //pc.printf("%d %d\n\r", controller->adc1_raw, controller->adc2_raw);
benkatz 26:2b865c00d7e9 139 }
benkatz 22:60276ba87ac6 140 }
benkatz 26:2b865c00d7e9 141
benkatz 26:2b865c00d7e9 142
benkatz 26:2b865c00d7e9 143 void torque_control(ControllerStruct *controller){
benkatz 26:2b865c00d7e9 144 float torque_ref = -controller->kp*(controller->p_des - controller->theta_mech) + controller->t_ff;// + controller->kd*(controller->v_des - GR*controller->dtheta_mech);
benkatz 26:2b865c00d7e9 145 //float torque_ref = -.1*(controller->p_des - controller->theta_mech);
benkatz 26:2b865c00d7e9 146 controller->i_q_ref = torque_ref/KT_OUT;
benkatz 26:2b865c00d7e9 147 controller->i_d_ref = 0;
benkatz 26:2b865c00d7e9 148 }
benkatz 26:2b865c00d7e9 149
benkatz 26:2b865c00d7e9 150
benkatz 22:60276ba87ac6 151 /*
benkatz 22:60276ba87ac6 152 void zero_encoder(ControllerStruct *controller, GPIOStruct *gpio, ){
benkatz 22:60276ba87ac6 153
benkatz 22:60276ba87ac6 154 }
benkatz 22:60276ba87ac6 155 */