Alvee Ahmed
/
Hobbyking_Cheetah_Compact_DRV8323_14bit
customizing code for Aliexpress 14 bit version with MA702
FOC/foc.cpp@45:26801179208e, 2018-06-27 (annotated)
- Committer:
- benkatz
- Date:
- Wed Jun 27 03:44:44 2018 +0000
- Revision:
- 45:26801179208e
- Parent:
- 44:8040fa2fcb0d
- Child:
- 46:2d4b1dafcfe3
Lots of changes, seems to have broken calibration
Who changed what in which revision?
User | Revision | Line number | New 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 | 45:26801179208e | 59 | |
benkatz | 45:26801179208e | 60 | void init_controller_params(ControllerStruct *controller){ |
benkatz | 45:26801179208e | 61 | controller->ki_d = KI_D; |
benkatz | 45:26801179208e | 62 | controller->ki_q = KI_Q; |
benkatz | 45:26801179208e | 63 | controller->k_d = K_SCALE*I_BW; |
benkatz | 45:26801179208e | 64 | controller->k_q = K_SCALE*I_BW; |
benkatz | 45:26801179208e | 65 | controller->alpha = 1.0f - 1.0f/(1.0f - DT*I_BW*2.0f*PI); |
benkatz | 45:26801179208e | 66 | |
benkatz | 45:26801179208e | 67 | } |
benkatz | 22:60276ba87ac6 | 68 | |
benkatz | 22:60276ba87ac6 | 69 | void reset_foc(ControllerStruct *controller){ |
benkatz | 28:8c7e29f719c5 | 70 | TIM1->CCR3 = (PWM_ARR>>1)*(0.5f); |
benkatz | 28:8c7e29f719c5 | 71 | TIM1->CCR1 = (PWM_ARR>>1)*(0.5f); |
benkatz | 28:8c7e29f719c5 | 72 | TIM1->CCR2 = (PWM_ARR>>1)*(0.5f); |
benkatz | 28:8c7e29f719c5 | 73 | controller->i_d_ref = 0; |
benkatz | 28:8c7e29f719c5 | 74 | controller->i_q_ref = 0; |
benkatz | 28:8c7e29f719c5 | 75 | controller->i_d = 0; |
benkatz | 28:8c7e29f719c5 | 76 | controller->i_q = 0; |
benkatz | 37:c0f352d6e8e3 | 77 | controller->i_q_filt = 0; |
benkatz | 22:60276ba87ac6 | 78 | controller->q_int = 0; |
benkatz | 22:60276ba87ac6 | 79 | controller->d_int = 0; |
benkatz | 28:8c7e29f719c5 | 80 | controller->v_q = 0; |
benkatz | 28:8c7e29f719c5 | 81 | controller->v_d = 0; |
benkatz | 22:60276ba87ac6 | 82 | } |
benkatz | 45:26801179208e | 83 | |
benkatz | 45:26801179208e | 84 | void limit_current_ref (ControllerStruct *controller){ |
benkatz | 45:26801179208e | 85 | float i_q_max_limit = (0.5774f*controller->v_bus - controller->dtheta_elec*WB)/R_PHASE; |
benkatz | 45:26801179208e | 86 | float i_q_min_limit = (-0.5774f*controller->v_bus - controller->dtheta_elec*WB)/R_PHASE; |
benkatz | 45:26801179208e | 87 | controller->i_q_ref = fmaxf(fminf(i_q_max_limit, controller->i_q_ref), i_q_min_limit); |
benkatz | 45:26801179208e | 88 | } |
benkatz | 22:60276ba87ac6 | 89 | |
benkatz | 22:60276ba87ac6 | 90 | |
benkatz | 37:c0f352d6e8e3 | 91 | void commutate(ControllerStruct *controller, ObserverStruct *observer, GPIOStruct *gpio, float theta){ |
benkatz | 37:c0f352d6e8e3 | 92 | /// Observer Prediction /// |
benkatz | 37:c0f352d6e8e3 | 93 | observer->i_d_est += DT*(observer->i_d_dot); |
benkatz | 37:c0f352d6e8e3 | 94 | observer->i_q_est += DT*(observer->i_q_dot); |
benkatz | 37:c0f352d6e8e3 | 95 | |
benkatz | 25:f5741040c4bb | 96 | /// Commutation Loop /// |
benkatz | 25:f5741040c4bb | 97 | controller->loop_count ++; |
benkatz | 25:f5741040c4bb | 98 | if(PHASE_ORDER){ // Check current sensor ordering |
benkatz | 25:f5741040c4bb | 99 | controller->i_b = I_SCALE*(float)(controller->adc2_raw - controller->adc2_offset); // Calculate phase currents from ADC readings |
benkatz | 22:60276ba87ac6 | 100 | controller->i_c = I_SCALE*(float)(controller->adc1_raw - controller->adc1_offset); |
benkatz | 22:60276ba87ac6 | 101 | } |
benkatz | 22:60276ba87ac6 | 102 | else{ |
benkatz | 25:f5741040c4bb | 103 | controller->i_b = I_SCALE*(float)(controller->adc1_raw - controller->adc1_offset); |
benkatz | 22:60276ba87ac6 | 104 | controller->i_c = I_SCALE*(float)(controller->adc2_raw - controller->adc2_offset); |
benkatz | 22:60276ba87ac6 | 105 | } |
benkatz | 26:2b865c00d7e9 | 106 | controller->i_a = -controller->i_b - controller->i_c; |
benkatz | 22:60276ba87ac6 | 107 | |
benkatz | 26:2b865c00d7e9 | 108 | float s = FastSin(theta); |
benkatz | 26:2b865c00d7e9 | 109 | float c = FastCos(theta); |
benkatz | 44:8040fa2fcb0d | 110 | 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 | 111 | //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 | 112 | //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 | 113 | |
benkatz | 37:c0f352d6e8e3 | 114 | controller->i_q_filt = 0.95f*controller->i_q_filt + 0.05f*controller->i_q; |
benkatz | 37:c0f352d6e8e3 | 115 | observer->i_d_m = controller->i_d; |
benkatz | 37:c0f352d6e8e3 | 116 | observer->i_q_m = controller->i_q; |
benkatz | 37:c0f352d6e8e3 | 117 | |
benkatz | 37:c0f352d6e8e3 | 118 | observer->e_d = observer->i_d_m - observer->i_d_est; |
benkatz | 37:c0f352d6e8e3 | 119 | observer->e_q = observer->i_q_m - observer->i_q_est; |
benkatz | 37:c0f352d6e8e3 | 120 | observer->e_d_int += observer->e_d; |
benkatz | 37:c0f352d6e8e3 | 121 | observer->e_q_int += observer->e_q; |
benkatz | 37:c0f352d6e8e3 | 122 | |
benkatz | 37:c0f352d6e8e3 | 123 | observer->i_d_est += K_O*observer->e_d + .001f*observer->e_d_int; |
benkatz | 37:c0f352d6e8e3 | 124 | observer->i_q_est += K_O*observer->e_q + .001f*observer->e_q_int; |
benkatz | 37:c0f352d6e8e3 | 125 | |
benkatz | 37:c0f352d6e8e3 | 126 | |
benkatz | 45:26801179208e | 127 | //float scog12 = FastSin(12.0f*theta); |
benkatz | 45:26801179208e | 128 | //float scog1 = s; |
benkatz | 45:26801179208e | 129 | //float cogging_current = 0.25f*scog1 - 0.3f*scog12; |
benkatz | 45:26801179208e | 130 | |
benkatz | 45:26801179208e | 131 | // Filter the current references to the desired closed-loopbandwidth |
benkatz | 45:26801179208e | 132 | // Allows calculation of desired di/dt for inductance, etc |
benkatz | 45:26801179208e | 133 | controller->did_dt = controller->i_d_ref_filt; |
benkatz | 45:26801179208e | 134 | controller->diq_dt = controller->i_q_ref_filt; |
benkatz | 45:26801179208e | 135 | controller->i_d_ref_filt = (1.0f-controller->alpha)*controller->i_d_ref_filt + controller->alpha*controller->i_d_ref; |
benkatz | 45:26801179208e | 136 | controller->i_q_ref_filt = (1.0f-controller->alpha)*controller->i_q_ref_filt + controller->alpha*controller->i_q_ref; |
benkatz | 45:26801179208e | 137 | controller->did_dt = (controller->i_d_ref_filt - controller->did_dt)/DT; |
benkatz | 45:26801179208e | 138 | controller->diq_dt = (controller->i_q_ref_filt - controller->diq_dt)/DT; |
benkatz | 25:f5741040c4bb | 139 | |
benkatz | 25:f5741040c4bb | 140 | /// PI Controller /// |
benkatz | 22:60276ba87ac6 | 141 | float i_d_error = controller->i_d_ref - controller->i_d; |
benkatz | 45:26801179208e | 142 | float i_q_error = controller->i_q_ref - controller->i_q;// + cogging_current; |
benkatz | 37:c0f352d6e8e3 | 143 | |
benkatz | 45:26801179208e | 144 | // Calculate feed-forward voltages // |
benkatz | 45:26801179208e | 145 | float v_d_ff = 2.0f*(controller->i_d_ref*R_PHASE + L_D*controller->did_dt - controller->dtheta_elec*L_Q*controller->i_q_ref); //feed-forward voltages |
benkatz | 45:26801179208e | 146 | float v_q_ff = 2.0f*(controller->i_q_ref*R_PHASE + L_Q*controller->diq_dt + controller->dtheta_elec*(L_D*controller->i_d_ref + WB)); |
benkatz | 37:c0f352d6e8e3 | 147 | |
benkatz | 45:26801179208e | 148 | // Integrate Error // |
benkatz | 22:60276ba87ac6 | 149 | controller->d_int += i_d_error; |
benkatz | 22:60276ba87ac6 | 150 | controller->q_int += i_q_error; |
benkatz | 45:26801179208e | 151 | |
benkatz | 45:26801179208e | 152 | limit_norm(&controller->d_int, &controller->q_int, V_BUS/(controller->k_q*controller->ki_q)); |
benkatz | 45:26801179208e | 153 | controller->v_d = controller->k_d*(i_d_error + controller->ki_d*controller->d_int);// + v_d_ff; |
benkatz | 45:26801179208e | 154 | controller->v_q = controller->k_q*(i_q_error + controller->ki_q*controller->q_int);// + v_q_ff; |
benkatz | 22:60276ba87ac6 | 155 | |
benkatz | 44:8040fa2fcb0d | 156 | limit_norm(&controller->v_d, &controller->v_q, OVERMODULATION*controller->v_bus); // Normalize voltage vector to lie within curcle of radius v_bus |
benkatz | 45:26801179208e | 157 | 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 | 22:60276ba87ac6 | 158 | 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 | 159 | |
benkatz | 37:c0f352d6e8e3 | 160 | 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 | 161 | 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 | 162 | |
benkatz | 45:26801179208e | 163 | //controller->dtc_u = 0.5f; |
benkatz | 45:26801179208e | 164 | //controller->dtc_v = 0.6f; |
benkatz | 45:26801179208e | 165 | //controller->dtc_w = 0.5f; |
benkatz | 25:f5741040c4bb | 166 | if(PHASE_ORDER){ // Check which phase order to use, |
benkatz | 31:61eb6ae28215 | 167 | TIM1->CCR3 = (PWM_ARR)*(1.0f-controller->dtc_u); // Write duty cycles |
benkatz | 31:61eb6ae28215 | 168 | TIM1->CCR2 = (PWM_ARR)*(1.0f-controller->dtc_v); |
benkatz | 31:61eb6ae28215 | 169 | TIM1->CCR1 = (PWM_ARR)*(1.0f-controller->dtc_w); |
benkatz | 22:60276ba87ac6 | 170 | } |
benkatz | 22:60276ba87ac6 | 171 | else{ |
benkatz | 32:ccac5da77844 | 172 | TIM1->CCR3 = (PWM_ARR)*(1.0f-controller->dtc_u); |
benkatz | 32:ccac5da77844 | 173 | TIM1->CCR1 = (PWM_ARR)*(1.0f-controller->dtc_v); |
benkatz | 32:ccac5da77844 | 174 | TIM1->CCR2 = (PWM_ARR)*(1.0f-controller->dtc_w); |
benkatz | 22:60276ba87ac6 | 175 | } |
benkatz | 25:f5741040c4bb | 176 | |
benkatz | 25:f5741040c4bb | 177 | controller->theta_elec = theta; //For some reason putting this at the front breaks thins |
benkatz | 22:60276ba87ac6 | 178 | |
benkatz | 22:60276ba87ac6 | 179 | |
benkatz | 31:61eb6ae28215 | 180 | if(controller->loop_count >400){ |
benkatz | 22:60276ba87ac6 | 181 | //controller->i_q_ref = -controller->i_q_ref; |
benkatz | 26:2b865c00d7e9 | 182 | controller->loop_count = 0; |
benkatz | 22:60276ba87ac6 | 183 | |
benkatz | 28:8c7e29f719c5 | 184 | //printf("%.2f %.2f %.2f\n\r", controller->i_a, controller->i_b, controller->i_c); |
benkatz | 31:61eb6ae28215 | 185 | //printf("%f\n\r", controller->dtheta_mech*GR); |
benkatz | 22:60276ba87ac6 | 186 | //pc.printf("%f %f %f\n\r", controller->i_a, controller->i_b, controller->i_c); |
benkatz | 45:26801179208e | 187 | //printf("%f %f\n\r", controller->k_d, controller->k_q); |
benkatz | 22:60276ba87ac6 | 188 | //pc.printf("%d %d\n\r", controller->adc1_raw, controller->adc2_raw); |
benkatz | 26:2b865c00d7e9 | 189 | } |
benkatz | 22:60276ba87ac6 | 190 | } |
benkatz | 26:2b865c00d7e9 | 191 | |
benkatz | 26:2b865c00d7e9 | 192 | |
benkatz | 26:2b865c00d7e9 | 193 | void torque_control(ControllerStruct *controller){ |
benkatz | 28:8c7e29f719c5 | 194 | 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 | 195 | //float torque_ref = -.1*(controller->p_des - controller->theta_mech); |
benkatz | 26:2b865c00d7e9 | 196 | controller->i_q_ref = torque_ref/KT_OUT; |
benkatz | 37:c0f352d6e8e3 | 197 | controller->i_d_ref = 0.0f; |
benkatz | 26:2b865c00d7e9 | 198 | } |
benkatz | 26:2b865c00d7e9 | 199 | |
benkatz | 26:2b865c00d7e9 | 200 | |
benkatz | 22:60276ba87ac6 | 201 | /* |
benkatz | 22:60276ba87ac6 | 202 | void zero_encoder(ControllerStruct *controller, GPIOStruct *gpio, ){ |
benkatz | 22:60276ba87ac6 | 203 | |
benkatz | 22:60276ba87ac6 | 204 | } |
benkatz | 22:60276ba87ac6 | 205 | */ |