customizing code for Aliexpress 14 bit version with MA702

Dependencies:   mbed FastPWM3

Committer:
benkatz
Date:
Mon Jun 11 00:04:06 2018 +0000
Revision:
44:8040fa2fcb0d
Parent:
38:67e4e1453a4b
Child:
45:26801179208e
new drv chip working;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
benkatz 37:c0f352d6e8e3 1
benkatz 22:60276ba87ac6 2 #include "foc.h"
benkatz 26:2b865c00d7e9 3 using namespace FastMath;
benkatz 22:60276ba87ac6 4
benkatz 22:60276ba87ac6 5
benkatz 22:60276ba87ac6 6 void abc( float theta, float d, float q, float *a, float *b, float *c){
benkatz 25:f5741040c4bb 7 /// Inverse DQ0 Transform ///
benkatz 22:60276ba87ac6 8 ///Phase current amplitude = lengh of dq vector///
benkatz 22:60276ba87ac6 9 ///i.e. iq = 1, id = 0, peak phase current of 1///
benkatz 37:c0f352d6e8e3 10 float cf = FastCos(theta);
benkatz 37:c0f352d6e8e3 11 float sf = FastSin(theta);
benkatz 37:c0f352d6e8e3 12
benkatz 37:c0f352d6e8e3 13 *a = cf*d - sf*q; // Faster Inverse DQ0 transform
benkatz 37:c0f352d6e8e3 14 *b = (0.86602540378f*sf-.5f*cf)*d - (-0.86602540378f*cf-.5f*sf)*q;
benkatz 37:c0f352d6e8e3 15 *c = (-0.86602540378f*sf-.5f*cf)*d - (0.86602540378f*cf-.5f*sf)*q;
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 37:c0f352d6e8e3 24 float cf = FastCos(theta);
benkatz 37:c0f352d6e8e3 25 float sf = FastSin(theta);
benkatz 26:2b865c00d7e9 26
benkatz 37:c0f352d6e8e3 27 *d = 0.6666667f*(cf*a + (0.86602540378f*sf-.5f*cf)*b + (-0.86602540378f*sf-.5f*cf)*c); ///Faster DQ0 Transform
benkatz 37:c0f352d6e8e3 28 *q = 0.6666667f*(-sf*a - (-0.86602540378f*cf-.5f*sf)*b - (0.86602540378f*cf-.5f*sf)*c);
benkatz 37:c0f352d6e8e3 29
benkatz 22:60276ba87ac6 30 }
benkatz 22:60276ba87ac6 31
benkatz 22:60276ba87ac6 32 void svm(float v_bus, float u, float v, float w, float *dtc_u, float *dtc_v, float *dtc_w){
benkatz 25:f5741040c4bb 33 /// Space Vector Modulation ///
benkatz 25:f5741040c4bb 34 /// u,v,w amplitude = v_bus for full modulation depth ///
benkatz 22:60276ba87ac6 35
benkatz 22:60276ba87ac6 36 float v_offset = (fminf3(u, v, w) + fmaxf3(u, v, w))/2.0f;
benkatz 31:61eb6ae28215 37 *dtc_u = fminf(fmaxf(((u -v_offset)/v_bus + .5f), DTC_MIN), DTC_MAX);
benkatz 31:61eb6ae28215 38 *dtc_v = fminf(fmaxf(((v -v_offset)/v_bus + .5f), DTC_MIN), DTC_MAX);
benkatz 31:61eb6ae28215 39 *dtc_w = fminf(fmaxf(((w -v_offset)/v_bus + .5f), DTC_MIN), DTC_MAX);
benkatz 22:60276ba87ac6 40
benkatz 22:60276ba87ac6 41 }
benkatz 22:60276ba87ac6 42
benkatz 25:f5741040c4bb 43 void zero_current(int *offset_1, int *offset_2){ // Measure zero-offset of the current sensors
benkatz 22:60276ba87ac6 44 int adc1_offset = 0;
benkatz 22:60276ba87ac6 45 int adc2_offset = 0;
benkatz 22:60276ba87ac6 46 int n = 1024;
benkatz 25:f5741040c4bb 47 for (int i = 0; i<n; i++){ // Average n samples of the ADC
benkatz 27:501fee691e0e 48 TIM1->CCR3 = (PWM_ARR>>1)*(1.0f); // Write duty cycles
benkatz 27:501fee691e0e 49 TIM1->CCR2 = (PWM_ARR>>1)*(1.0f);
benkatz 27:501fee691e0e 50 TIM1->CCR1 = (PWM_ARR>>1)*(1.0f);
benkatz 26:2b865c00d7e9 51 ADC1->CR2 |= 0x40000000; // Begin sample and conversion
benkatz 22:60276ba87ac6 52 wait(.001);
benkatz 22:60276ba87ac6 53 adc2_offset += ADC2->DR;
benkatz 22:60276ba87ac6 54 adc1_offset += ADC1->DR;
benkatz 22:60276ba87ac6 55 }
benkatz 22:60276ba87ac6 56 *offset_1 = adc1_offset/n;
benkatz 22:60276ba87ac6 57 *offset_2 = adc2_offset/n;
benkatz 22:60276ba87ac6 58 }
benkatz 22:60276ba87ac6 59
benkatz 22:60276ba87ac6 60 void reset_foc(ControllerStruct *controller){
benkatz 28:8c7e29f719c5 61 TIM1->CCR3 = (PWM_ARR>>1)*(0.5f);
benkatz 28:8c7e29f719c5 62 TIM1->CCR1 = (PWM_ARR>>1)*(0.5f);
benkatz 28:8c7e29f719c5 63 TIM1->CCR2 = (PWM_ARR>>1)*(0.5f);
benkatz 28:8c7e29f719c5 64 controller->i_d_ref = 0;
benkatz 28:8c7e29f719c5 65 controller->i_q_ref = 0;
benkatz 28:8c7e29f719c5 66 controller->i_d = 0;
benkatz 28:8c7e29f719c5 67 controller->i_q = 0;
benkatz 37:c0f352d6e8e3 68 controller->i_q_filt = 0;
benkatz 22:60276ba87ac6 69 controller->q_int = 0;
benkatz 22:60276ba87ac6 70 controller->d_int = 0;
benkatz 28:8c7e29f719c5 71 controller->v_q = 0;
benkatz 28:8c7e29f719c5 72 controller->v_d = 0;
benkatz 22:60276ba87ac6 73 }
benkatz 22:60276ba87ac6 74
benkatz 22:60276ba87ac6 75
benkatz 37:c0f352d6e8e3 76 void commutate(ControllerStruct *controller, ObserverStruct *observer, GPIOStruct *gpio, float theta){
benkatz 37:c0f352d6e8e3 77 /// Observer Prediction ///
benkatz 37:c0f352d6e8e3 78 observer->i_d_est += DT*(observer->i_d_dot);
benkatz 37:c0f352d6e8e3 79 observer->i_q_est += DT*(observer->i_q_dot);
benkatz 37:c0f352d6e8e3 80
benkatz 25:f5741040c4bb 81 /// Commutation Loop ///
benkatz 25:f5741040c4bb 82 controller->loop_count ++;
benkatz 25:f5741040c4bb 83 if(PHASE_ORDER){ // Check current sensor ordering
benkatz 25:f5741040c4bb 84 controller->i_b = I_SCALE*(float)(controller->adc2_raw - controller->adc2_offset); // Calculate phase currents from ADC readings
benkatz 22:60276ba87ac6 85 controller->i_c = I_SCALE*(float)(controller->adc1_raw - controller->adc1_offset);
benkatz 22:60276ba87ac6 86 }
benkatz 22:60276ba87ac6 87 else{
benkatz 25:f5741040c4bb 88 controller->i_b = I_SCALE*(float)(controller->adc1_raw - controller->adc1_offset);
benkatz 22:60276ba87ac6 89 controller->i_c = I_SCALE*(float)(controller->adc2_raw - controller->adc2_offset);
benkatz 22:60276ba87ac6 90 }
benkatz 26:2b865c00d7e9 91 controller->i_a = -controller->i_b - controller->i_c;
benkatz 22:60276ba87ac6 92
benkatz 26:2b865c00d7e9 93 float s = FastSin(theta);
benkatz 26:2b865c00d7e9 94 float c = FastCos(theta);
benkatz 44:8040fa2fcb0d 95 dq0(controller->theta_elec, controller->i_a, controller->i_b, controller->i_c, &controller->i_d, &controller->i_q); //dq0 transform on currents
benkatz 44:8040fa2fcb0d 96 //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); ///Faster DQ0 Transform
benkatz 44:8040fa2fcb0d 97 //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 37:c0f352d6e8e3 98
benkatz 37:c0f352d6e8e3 99 controller->i_q_filt = 0.95f*controller->i_q_filt + 0.05f*controller->i_q;
benkatz 37:c0f352d6e8e3 100 observer->i_d_m = controller->i_d;
benkatz 37:c0f352d6e8e3 101 observer->i_q_m = controller->i_q;
benkatz 37:c0f352d6e8e3 102
benkatz 37:c0f352d6e8e3 103 observer->e_d = observer->i_d_m - observer->i_d_est;
benkatz 37:c0f352d6e8e3 104 observer->e_q = observer->i_q_m - observer->i_q_est;
benkatz 37:c0f352d6e8e3 105 observer->e_d_int += observer->e_d;
benkatz 37:c0f352d6e8e3 106 observer->e_q_int += observer->e_q;
benkatz 37:c0f352d6e8e3 107
benkatz 37:c0f352d6e8e3 108 observer->i_d_est += K_O*observer->e_d + .001f*observer->e_d_int;
benkatz 37:c0f352d6e8e3 109 observer->i_q_est += K_O*observer->e_q + .001f*observer->e_q_int;
benkatz 37:c0f352d6e8e3 110
benkatz 37:c0f352d6e8e3 111
benkatz 38:67e4e1453a4b 112 float scog12 = FastSin(12.0f*theta);
benkatz 38:67e4e1453a4b 113 float scog1 = s;
benkatz 38:67e4e1453a4b 114 float cogging_current = 0.25f*scog1 - 0.3f*scog12;
benkatz 25:f5741040c4bb 115
benkatz 25:f5741040c4bb 116 /// PI Controller ///
benkatz 22:60276ba87ac6 117 float i_d_error = controller->i_d_ref - controller->i_d;
benkatz 38:67e4e1453a4b 118 float i_q_error = controller->i_q_ref - controller->i_q + cogging_current;
benkatz 37:c0f352d6e8e3 119
benkatz 37:c0f352d6e8e3 120 float v_d_ff = 2.0f*(controller->i_d_ref*R_PHASE - controller->dtheta_elec*L_Q*controller->i_q_ref); //feed-forward voltages
benkatz 37:c0f352d6e8e3 121 float v_q_ff = 2.0f*(controller->i_q_ref*R_PHASE + controller->dtheta_elec*(L_D*controller->i_d_ref + WB));
benkatz 37:c0f352d6e8e3 122
benkatz 22:60276ba87ac6 123 controller->d_int += i_d_error;
benkatz 22:60276ba87ac6 124 controller->q_int += i_q_error;
benkatz 22:60276ba87ac6 125
benkatz 22:60276ba87ac6 126 //v_d_ff = 0;
benkatz 22:60276ba87ac6 127 //v_q_ff = 0;
benkatz 22:60276ba87ac6 128
benkatz 31:61eb6ae28215 129 limit_norm(&controller->d_int, &controller->q_int, V_BUS/(K_SCALE*I_BW*KI_Q)); // Limit integrators to prevent windup
benkatz 31:61eb6ae28215 130 controller->v_d = K_SCALE*I_BW*i_d_error + K_SCALE*I_BW*KI_D*controller->d_int;// + v_d_ff;
benkatz 31:61eb6ae28215 131 controller->v_q = K_SCALE*I_BW*i_q_error + K_SCALE*I_BW*KI_Q*controller->q_int;// + v_q_ff;
benkatz 22:60276ba87ac6 132
benkatz 34:51647c6c500d 133 //controller->v_q = 4.0f;
benkatz 28:8c7e29f719c5 134 //controller->v_d = 0.0f;
benkatz 28:8c7e29f719c5 135
benkatz 22:60276ba87ac6 136 //controller->v_d = v_d_ff;
benkatz 22:60276ba87ac6 137 //controller->v_q = v_q_ff;
benkatz 22:60276ba87ac6 138
benkatz 44:8040fa2fcb0d 139 limit_norm(&controller->v_d, &controller->v_q, OVERMODULATION*controller->v_bus); // Normalize voltage vector to lie within curcle of radius v_bus
benkatz 44:8040fa2fcb0d 140 abc(controller->theta_elec+0.5f*DT*controller->dtheta_elec, controller->v_d, controller->v_q, &controller->v_u, &controller->v_v, &controller->v_w); //inverse dq0 transform on voltages
benkatz 26:2b865c00d7e9 141
benkatz 44:8040fa2fcb0d 142 //controller->v_u = c*controller->v_d - s*controller->v_q; // Faster Inverse DQ0 transform
benkatz 44:8040fa2fcb0d 143 //controller->v_v = (0.86602540378f*s-.5f*c)*controller->v_d - (-0.86602540378f*c-.5f*s)*controller->v_q;
benkatz 44:8040fa2fcb0d 144 //controller->v_w = (-0.86602540378f*s-.5f*c)*controller->v_d - (0.86602540378f*c-.5f*s)*controller->v_q;
benkatz 22:60276ba87ac6 145 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 146
benkatz 37:c0f352d6e8e3 147 observer->i_d_dot = 0.5f*(controller->v_d - 2.0f*(observer->i_d_est*R_PHASE - controller->dtheta_elec*L_Q*observer->i_q_est))/L_D; //feed-forward voltage
benkatz 37:c0f352d6e8e3 148 observer->i_q_dot = 0.5f*(controller->v_q - 2.0f*(observer->i_q_est*R_PHASE + controller->dtheta_elec*(L_D*observer->i_d_est + WB)))/L_Q;
benkatz 22:60276ba87ac6 149
benkatz 25:f5741040c4bb 150 if(PHASE_ORDER){ // Check which phase order to use,
benkatz 31:61eb6ae28215 151 TIM1->CCR3 = (PWM_ARR)*(1.0f-controller->dtc_u); // Write duty cycles
benkatz 31:61eb6ae28215 152 TIM1->CCR2 = (PWM_ARR)*(1.0f-controller->dtc_v);
benkatz 31:61eb6ae28215 153 TIM1->CCR1 = (PWM_ARR)*(1.0f-controller->dtc_w);
benkatz 22:60276ba87ac6 154 }
benkatz 22:60276ba87ac6 155 else{
benkatz 32:ccac5da77844 156 TIM1->CCR3 = (PWM_ARR)*(1.0f-controller->dtc_u);
benkatz 32:ccac5da77844 157 TIM1->CCR1 = (PWM_ARR)*(1.0f-controller->dtc_v);
benkatz 32:ccac5da77844 158 TIM1->CCR2 = (PWM_ARR)*(1.0f-controller->dtc_w);
benkatz 22:60276ba87ac6 159 }
benkatz 25:f5741040c4bb 160
benkatz 25:f5741040c4bb 161 controller->theta_elec = theta; //For some reason putting this at the front breaks thins
benkatz 22:60276ba87ac6 162
benkatz 22:60276ba87ac6 163
benkatz 31:61eb6ae28215 164 if(controller->loop_count >400){
benkatz 22:60276ba87ac6 165 //controller->i_q_ref = -controller->i_q_ref;
benkatz 26:2b865c00d7e9 166 controller->loop_count = 0;
benkatz 22:60276ba87ac6 167
benkatz 28:8c7e29f719c5 168 //printf("%.2f %.2f %.2f\n\r", controller->i_a, controller->i_b, controller->i_c);
benkatz 31:61eb6ae28215 169 //printf("%f\n\r", controller->dtheta_mech*GR);
benkatz 22:60276ba87ac6 170 //pc.printf("%f %f %f\n\r", controller->i_a, controller->i_b, controller->i_c);
benkatz 22:60276ba87ac6 171 //pc.printf("%f %f\n\r", controller->i_d, controller->i_q);
benkatz 22:60276ba87ac6 172 //pc.printf("%d %d\n\r", controller->adc1_raw, controller->adc2_raw);
benkatz 26:2b865c00d7e9 173 }
benkatz 22:60276ba87ac6 174 }
benkatz 26:2b865c00d7e9 175
benkatz 26:2b865c00d7e9 176
benkatz 26:2b865c00d7e9 177 void torque_control(ControllerStruct *controller){
benkatz 28:8c7e29f719c5 178 float torque_ref = controller->kp*(controller->p_des - controller->theta_mech) + controller->t_ff + controller->kd*(controller->v_des - controller->dtheta_mech);
benkatz 26:2b865c00d7e9 179 //float torque_ref = -.1*(controller->p_des - controller->theta_mech);
benkatz 26:2b865c00d7e9 180 controller->i_q_ref = torque_ref/KT_OUT;
benkatz 37:c0f352d6e8e3 181 controller->i_d_ref = 0.0f;
benkatz 26:2b865c00d7e9 182 }
benkatz 26:2b865c00d7e9 183
benkatz 26:2b865c00d7e9 184
benkatz 22:60276ba87ac6 185 /*
benkatz 22:60276ba87ac6 186 void zero_encoder(ControllerStruct *controller, GPIOStruct *gpio, ){
benkatz 22:60276ba87ac6 187
benkatz 22:60276ba87ac6 188 }
benkatz 22:60276ba87ac6 189 */