Alvee Ahmed
/
Hobbyking_Cheetah_Compact_DRV8323_14bit
customizing code for Aliexpress 14 bit version with MA702
FOC/foc.cpp@48:74a40481740c, 2019-03-03 (annotated)
- Committer:
- benkatz
- Date:
- Sun Mar 03 02:51:51 2019 +0000
- Revision:
- 48:74a40481740c
- Parent:
- 47:e1196a851f76
- Child:
- 49:83d83040ea51
Working on the new hardware
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 | 48:74a40481740c | 36 | float v_offset = (fminf3(u, v, w) + fmaxf3(u, v, w))*0.5f; |
benkatz | 47:e1196a851f76 | 37 | |
benkatz | 31:61eb6ae28215 | 38 | *dtc_u = fminf(fmaxf(((u -v_offset)/v_bus + .5f), DTC_MIN), DTC_MAX); |
benkatz | 31:61eb6ae28215 | 39 | *dtc_v = fminf(fmaxf(((v -v_offset)/v_bus + .5f), DTC_MIN), DTC_MAX); |
benkatz | 47:e1196a851f76 | 40 | *dtc_w = fminf(fmaxf(((w -v_offset)/v_bus + .5f), DTC_MIN), DTC_MAX); |
benkatz | 47:e1196a851f76 | 41 | |
benkatz | 47:e1196a851f76 | 42 | /* |
benkatz | 47:e1196a851f76 | 43 | sinusoidal pwm |
benkatz | 47:e1196a851f76 | 44 | *dtc_u = fminf(fmaxf((u/v_bus + .5f), DTC_MIN), DTC_MAX); |
benkatz | 47:e1196a851f76 | 45 | *dtc_v = fminf(fmaxf((v/v_bus + .5f), DTC_MIN), DTC_MAX); |
benkatz | 47:e1196a851f76 | 46 | *dtc_w = fminf(fmaxf((w/v_bus + .5f), DTC_MIN), DTC_MAX); |
benkatz | 47:e1196a851f76 | 47 | */ |
benkatz | 47:e1196a851f76 | 48 | |
benkatz | 22:60276ba87ac6 | 49 | |
benkatz | 22:60276ba87ac6 | 50 | } |
benkatz | 22:60276ba87ac6 | 51 | |
benkatz | 48:74a40481740c | 52 | void linearize_dtc(float *dtc){ |
benkatz | 48:74a40481740c | 53 | /// linearizes the output of the inverter, which is not linear for small duty cycles /// |
benkatz | 48:74a40481740c | 54 | float sgn = 1.0f-(2.0f*(dtc<0)); |
benkatz | 48:74a40481740c | 55 | if(abs(*dtc) >= .01f){ |
benkatz | 48:74a40481740c | 56 | *dtc = *dtc*.986f+.014f*sgn; |
benkatz | 48:74a40481740c | 57 | } |
benkatz | 48:74a40481740c | 58 | else{ |
benkatz | 48:74a40481740c | 59 | *dtc = 2.5f*(*dtc); |
benkatz | 48:74a40481740c | 60 | } |
benkatz | 48:74a40481740c | 61 | |
benkatz | 48:74a40481740c | 62 | } |
benkatz | 48:74a40481740c | 63 | |
benkatz | 48:74a40481740c | 64 | |
benkatz | 25:f5741040c4bb | 65 | void zero_current(int *offset_1, int *offset_2){ // Measure zero-offset of the current sensors |
benkatz | 22:60276ba87ac6 | 66 | int adc1_offset = 0; |
benkatz | 22:60276ba87ac6 | 67 | int adc2_offset = 0; |
benkatz | 22:60276ba87ac6 | 68 | int n = 1024; |
benkatz | 25:f5741040c4bb | 69 | for (int i = 0; i<n; i++){ // Average n samples of the ADC |
benkatz | 27:501fee691e0e | 70 | TIM1->CCR3 = (PWM_ARR>>1)*(1.0f); // Write duty cycles |
benkatz | 27:501fee691e0e | 71 | TIM1->CCR2 = (PWM_ARR>>1)*(1.0f); |
benkatz | 27:501fee691e0e | 72 | TIM1->CCR1 = (PWM_ARR>>1)*(1.0f); |
benkatz | 26:2b865c00d7e9 | 73 | ADC1->CR2 |= 0x40000000; // Begin sample and conversion |
benkatz | 22:60276ba87ac6 | 74 | wait(.001); |
benkatz | 22:60276ba87ac6 | 75 | adc2_offset += ADC2->DR; |
benkatz | 22:60276ba87ac6 | 76 | adc1_offset += ADC1->DR; |
benkatz | 22:60276ba87ac6 | 77 | } |
benkatz | 22:60276ba87ac6 | 78 | *offset_1 = adc1_offset/n; |
benkatz | 22:60276ba87ac6 | 79 | *offset_2 = adc2_offset/n; |
benkatz | 22:60276ba87ac6 | 80 | } |
benkatz | 45:26801179208e | 81 | |
benkatz | 45:26801179208e | 82 | void init_controller_params(ControllerStruct *controller){ |
benkatz | 45:26801179208e | 83 | controller->ki_d = KI_D; |
benkatz | 45:26801179208e | 84 | controller->ki_q = KI_Q; |
benkatz | 45:26801179208e | 85 | controller->k_d = K_SCALE*I_BW; |
benkatz | 45:26801179208e | 86 | controller->k_q = K_SCALE*I_BW; |
benkatz | 45:26801179208e | 87 | controller->alpha = 1.0f - 1.0f/(1.0f - DT*I_BW*2.0f*PI); |
benkatz | 45:26801179208e | 88 | |
benkatz | 45:26801179208e | 89 | } |
benkatz | 22:60276ba87ac6 | 90 | |
benkatz | 22:60276ba87ac6 | 91 | void reset_foc(ControllerStruct *controller){ |
benkatz | 28:8c7e29f719c5 | 92 | TIM1->CCR3 = (PWM_ARR>>1)*(0.5f); |
benkatz | 28:8c7e29f719c5 | 93 | TIM1->CCR1 = (PWM_ARR>>1)*(0.5f); |
benkatz | 28:8c7e29f719c5 | 94 | TIM1->CCR2 = (PWM_ARR>>1)*(0.5f); |
benkatz | 28:8c7e29f719c5 | 95 | controller->i_d_ref = 0; |
benkatz | 28:8c7e29f719c5 | 96 | controller->i_q_ref = 0; |
benkatz | 28:8c7e29f719c5 | 97 | controller->i_d = 0; |
benkatz | 28:8c7e29f719c5 | 98 | controller->i_q = 0; |
benkatz | 37:c0f352d6e8e3 | 99 | controller->i_q_filt = 0; |
benkatz | 22:60276ba87ac6 | 100 | controller->q_int = 0; |
benkatz | 22:60276ba87ac6 | 101 | controller->d_int = 0; |
benkatz | 28:8c7e29f719c5 | 102 | controller->v_q = 0; |
benkatz | 28:8c7e29f719c5 | 103 | controller->v_d = 0; |
benkatz | 47:e1196a851f76 | 104 | |
benkatz | 22:60276ba87ac6 | 105 | } |
benkatz | 45:26801179208e | 106 | |
benkatz | 48:74a40481740c | 107 | void reset_observer(ObserverStruct *observer){ |
benkatz | 48:74a40481740c | 108 | observer->temperature = 25.0f; |
benkatz | 48:74a40481740c | 109 | observer->resistance = .1f; |
benkatz | 48:74a40481740c | 110 | } |
benkatz | 48:74a40481740c | 111 | |
benkatz | 45:26801179208e | 112 | void limit_current_ref (ControllerStruct *controller){ |
benkatz | 45:26801179208e | 113 | float i_q_max_limit = (0.5774f*controller->v_bus - controller->dtheta_elec*WB)/R_PHASE; |
benkatz | 45:26801179208e | 114 | float i_q_min_limit = (-0.5774f*controller->v_bus - controller->dtheta_elec*WB)/R_PHASE; |
benkatz | 45:26801179208e | 115 | controller->i_q_ref = fmaxf(fminf(i_q_max_limit, controller->i_q_ref), i_q_min_limit); |
benkatz | 45:26801179208e | 116 | } |
benkatz | 22:60276ba87ac6 | 117 | |
benkatz | 22:60276ba87ac6 | 118 | |
benkatz | 48:74a40481740c | 119 | void commutate(ControllerStruct *controller, ObserverStruct *observer, GPIOStruct *gpio, float theta){ |
benkatz | 48:74a40481740c | 120 | |
benkatz | 48:74a40481740c | 121 | /// Update observer estimates /// |
benkatz | 48:74a40481740c | 122 | // Resistance observer // |
benkatz | 48:74a40481740c | 123 | // Temperature Observer // |
benkatz | 48:74a40481740c | 124 | float t_rise = (float)observer->temperature - 25.0f; |
benkatz | 48:74a40481740c | 125 | float q_th_in = (1.0f + .00393f*t_rise)*(controller->i_d*controller->i_d*R_PHASE*1.5f + controller->i_q*controller->i_q*R_PHASE*1.5f); |
benkatz | 48:74a40481740c | 126 | float q_th_out = t_rise*R_TH; |
benkatz | 48:74a40481740c | 127 | observer->temperature += INV_M_TH*DT*(q_th_in-q_th_out); |
benkatz | 48:74a40481740c | 128 | |
benkatz | 48:74a40481740c | 129 | //observer->resistance = (controller->v_q - 2.0f*controller->dtheta_elec*(WB + L_D*controller->i_d))/controller->i_q; |
benkatz | 48:74a40481740c | 130 | observer->resistance = controller->v_q/controller->i_q; |
benkatz | 48:74a40481740c | 131 | if(isnan(observer->resistance)){observer->resistance = R_PHASE;} |
benkatz | 48:74a40481740c | 132 | observer->temperature2 = (double)(25.0f + ((observer->resistance*6.0606f)-1.0f)*275.5f); |
benkatz | 48:74a40481740c | 133 | double e = observer->temperature - observer->temperature2; |
benkatz | 48:74a40481740c | 134 | observer->temperature -= .001*e; |
benkatz | 48:74a40481740c | 135 | //printf("%.3f\n\r", e); |
benkatz | 48:74a40481740c | 136 | |
benkatz | 47:e1196a851f76 | 137 | |
benkatz | 25:f5741040c4bb | 138 | /// Commutation Loop /// |
benkatz | 25:f5741040c4bb | 139 | controller->loop_count ++; |
benkatz | 25:f5741040c4bb | 140 | if(PHASE_ORDER){ // Check current sensor ordering |
benkatz | 25:f5741040c4bb | 141 | controller->i_b = I_SCALE*(float)(controller->adc2_raw - controller->adc2_offset); // Calculate phase currents from ADC readings |
benkatz | 22:60276ba87ac6 | 142 | controller->i_c = I_SCALE*(float)(controller->adc1_raw - controller->adc1_offset); |
benkatz | 22:60276ba87ac6 | 143 | } |
benkatz | 22:60276ba87ac6 | 144 | else{ |
benkatz | 25:f5741040c4bb | 145 | controller->i_b = I_SCALE*(float)(controller->adc1_raw - controller->adc1_offset); |
benkatz | 22:60276ba87ac6 | 146 | controller->i_c = I_SCALE*(float)(controller->adc2_raw - controller->adc2_offset); |
benkatz | 22:60276ba87ac6 | 147 | } |
benkatz | 26:2b865c00d7e9 | 148 | controller->i_a = -controller->i_b - controller->i_c; |
benkatz | 22:60276ba87ac6 | 149 | |
benkatz | 26:2b865c00d7e9 | 150 | float s = FastSin(theta); |
benkatz | 26:2b865c00d7e9 | 151 | float c = FastCos(theta); |
benkatz | 44:8040fa2fcb0d | 152 | 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 | 153 | //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 | 154 | //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 | 155 | |
benkatz | 37:c0f352d6e8e3 | 156 | controller->i_q_filt = 0.95f*controller->i_q_filt + 0.05f*controller->i_q; |
benkatz | 47:e1196a851f76 | 157 | controller->i_d_filt = 0.95f*controller->i_d_filt + 0.05f*controller->i_d; |
benkatz | 37:c0f352d6e8e3 | 158 | |
benkatz | 45:26801179208e | 159 | |
benkatz | 45:26801179208e | 160 | // Filter the current references to the desired closed-loopbandwidth |
benkatz | 45:26801179208e | 161 | controller->i_d_ref_filt = (1.0f-controller->alpha)*controller->i_d_ref_filt + controller->alpha*controller->i_d_ref; |
benkatz | 45:26801179208e | 162 | controller->i_q_ref_filt = (1.0f-controller->alpha)*controller->i_q_ref_filt + controller->alpha*controller->i_q_ref; |
benkatz | 48:74a40481740c | 163 | |
benkatz | 48:74a40481740c | 164 | |
benkatz | 48:74a40481740c | 165 | /// Field Weakening /// |
benkatz | 48:74a40481740c | 166 | |
benkatz | 48:74a40481740c | 167 | controller->fw_int += .001f*(0.5f*OVERMODULATION*controller->v_bus - controller->v_ref); |
benkatz | 48:74a40481740c | 168 | controller->fw_int = fmaxf(fminf(controller->fw_int, 0.0f), -I_MAX_FW); |
benkatz | 48:74a40481740c | 169 | controller->i_d_ref = controller->fw_int; |
benkatz | 48:74a40481740c | 170 | //float i_cmd_mag_sq = controller->i_d_ref*controller->i_d_ref + controller->i_q_ref*controller->i_q_ref; |
benkatz | 48:74a40481740c | 171 | limit_norm(&controller->i_d_ref, &controller->i_q_ref, I_MAX); |
benkatz | 25:f5741040c4bb | 172 | |
benkatz | 47:e1196a851f76 | 173 | |
benkatz | 47:e1196a851f76 | 174 | |
benkatz | 25:f5741040c4bb | 175 | /// PI Controller /// |
benkatz | 22:60276ba87ac6 | 176 | float i_d_error = controller->i_d_ref - controller->i_d; |
benkatz | 45:26801179208e | 177 | float i_q_error = controller->i_q_ref - controller->i_q;// + cogging_current; |
benkatz | 37:c0f352d6e8e3 | 178 | |
benkatz | 45:26801179208e | 179 | // Calculate feed-forward voltages // |
benkatz | 48:74a40481740c | 180 | float v_d_ff = SQRT3*(1.0f*controller->i_d_ref*R_PHASE - controller->dtheta_elec*L_Q*controller->i_q); //feed-forward voltages |
benkatz | 48:74a40481740c | 181 | float v_q_ff = SQRT3*(1.0f*controller->i_q_ref*R_PHASE + controller->dtheta_elec*(L_D*controller->i_d + 1.0f*WB)); |
benkatz | 37:c0f352d6e8e3 | 182 | |
benkatz | 45:26801179208e | 183 | // Integrate Error // |
benkatz | 47:e1196a851f76 | 184 | controller->d_int += controller->k_d*controller->ki_d*i_d_error; |
benkatz | 47:e1196a851f76 | 185 | controller->q_int += controller->k_q*controller->ki_q*i_q_error; |
benkatz | 47:e1196a851f76 | 186 | |
benkatz | 47:e1196a851f76 | 187 | controller->d_int = fmaxf(fminf(controller->d_int, OVERMODULATION*controller->v_bus), - OVERMODULATION*controller->v_bus); |
benkatz | 47:e1196a851f76 | 188 | controller->q_int = fmaxf(fminf(controller->q_int, OVERMODULATION*controller->v_bus), - OVERMODULATION*controller->v_bus); |
benkatz | 47:e1196a851f76 | 189 | |
benkatz | 47:e1196a851f76 | 190 | //limit_norm(&controller->d_int, &controller->q_int, OVERMODULATION*controller->v_bus); |
benkatz | 47:e1196a851f76 | 191 | controller->v_d = controller->k_d*i_d_error + controller->d_int ;//+ v_d_ff; |
benkatz | 47:e1196a851f76 | 192 | controller->v_q = controller->k_q*i_q_error + controller->q_int ;//+ v_q_ff; |
benkatz | 47:e1196a851f76 | 193 | |
benkatz | 47:e1196a851f76 | 194 | controller->v_ref = sqrt(controller->v_d*controller->v_d + controller->v_q*controller->v_q); |
benkatz | 22:60276ba87ac6 | 195 | |
benkatz | 44:8040fa2fcb0d | 196 | limit_norm(&controller->v_d, &controller->v_q, OVERMODULATION*controller->v_bus); // Normalize voltage vector to lie within curcle of radius v_bus |
benkatz | 48:74a40481740c | 197 | float dtc_d = controller->v_d/controller->v_bus; |
benkatz | 48:74a40481740c | 198 | float dtc_q = controller->v_q/controller->v_bus; |
benkatz | 48:74a40481740c | 199 | linearize_dtc(&dtc_d); |
benkatz | 48:74a40481740c | 200 | linearize_dtc(&dtc_q); |
benkatz | 48:74a40481740c | 201 | controller->v_d = dtc_d*controller->v_bus; |
benkatz | 48:74a40481740c | 202 | controller->v_q = dtc_q*controller->v_bus; |
benkatz | 47:e1196a851f76 | 203 | abc(controller->theta_elec + 0.0f*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 | 22:60276ba87ac6 | 204 | 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 | 205 | |
benkatz | 25:f5741040c4bb | 206 | if(PHASE_ORDER){ // Check which phase order to use, |
benkatz | 31:61eb6ae28215 | 207 | TIM1->CCR3 = (PWM_ARR)*(1.0f-controller->dtc_u); // Write duty cycles |
benkatz | 31:61eb6ae28215 | 208 | TIM1->CCR2 = (PWM_ARR)*(1.0f-controller->dtc_v); |
benkatz | 31:61eb6ae28215 | 209 | TIM1->CCR1 = (PWM_ARR)*(1.0f-controller->dtc_w); |
benkatz | 22:60276ba87ac6 | 210 | } |
benkatz | 22:60276ba87ac6 | 211 | else{ |
benkatz | 32:ccac5da77844 | 212 | TIM1->CCR3 = (PWM_ARR)*(1.0f-controller->dtc_u); |
benkatz | 32:ccac5da77844 | 213 | TIM1->CCR1 = (PWM_ARR)*(1.0f-controller->dtc_v); |
benkatz | 32:ccac5da77844 | 214 | TIM1->CCR2 = (PWM_ARR)*(1.0f-controller->dtc_w); |
benkatz | 22:60276ba87ac6 | 215 | } |
benkatz | 25:f5741040c4bb | 216 | |
benkatz | 48:74a40481740c | 217 | controller->theta_elec = theta; |
benkatz | 22:60276ba87ac6 | 218 | |
benkatz | 22:60276ba87ac6 | 219 | } |
benkatz | 26:2b865c00d7e9 | 220 | |
benkatz | 26:2b865c00d7e9 | 221 | |
benkatz | 26:2b865c00d7e9 | 222 | void torque_control(ControllerStruct *controller){ |
benkatz | 28:8c7e29f719c5 | 223 | 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 | 224 | //float torque_ref = -.1*(controller->p_des - controller->theta_mech); |
benkatz | 26:2b865c00d7e9 | 225 | controller->i_q_ref = torque_ref/KT_OUT; |
benkatz | 37:c0f352d6e8e3 | 226 | controller->i_d_ref = 0.0f; |
benkatz | 26:2b865c00d7e9 | 227 | } |
benkatz | 47:e1196a851f76 | 228 |