it works!
Dependencies: mbed-dev-f303 FastPWM3
FOC/foc.cpp@47:e1196a851f76, 2018-12-05 (annotated)
- Committer:
- benkatz
- Date:
- Wed Dec 05 04:07:46 2018 +0000
- Revision:
- 47:e1196a851f76
- Parent:
- 46:2d4b1dafcfe3
- Child:
- 48:74a40481740c
albert revision;
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 | 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 | 47:e1196a851f76 | 52 | |
benkatz | 25:f5741040c4bb | 53 | void zero_current(int *offset_1, int *offset_2){ // Measure zero-offset of the current sensors |
benkatz | 22:60276ba87ac6 | 54 | int adc1_offset = 0; |
benkatz | 22:60276ba87ac6 | 55 | int adc2_offset = 0; |
benkatz | 22:60276ba87ac6 | 56 | int n = 1024; |
benkatz | 25:f5741040c4bb | 57 | for (int i = 0; i<n; i++){ // Average n samples of the ADC |
benkatz | 27:501fee691e0e | 58 | TIM1->CCR3 = (PWM_ARR>>1)*(1.0f); // Write duty cycles |
benkatz | 27:501fee691e0e | 59 | TIM1->CCR2 = (PWM_ARR>>1)*(1.0f); |
benkatz | 27:501fee691e0e | 60 | TIM1->CCR1 = (PWM_ARR>>1)*(1.0f); |
benkatz | 26:2b865c00d7e9 | 61 | ADC1->CR2 |= 0x40000000; // Begin sample and conversion |
benkatz | 22:60276ba87ac6 | 62 | wait(.001); |
benkatz | 22:60276ba87ac6 | 63 | adc2_offset += ADC2->DR; |
benkatz | 22:60276ba87ac6 | 64 | adc1_offset += ADC1->DR; |
benkatz | 22:60276ba87ac6 | 65 | } |
benkatz | 22:60276ba87ac6 | 66 | *offset_1 = adc1_offset/n; |
benkatz | 22:60276ba87ac6 | 67 | *offset_2 = adc2_offset/n; |
benkatz | 22:60276ba87ac6 | 68 | } |
benkatz | 45:26801179208e | 69 | |
benkatz | 45:26801179208e | 70 | void init_controller_params(ControllerStruct *controller){ |
benkatz | 45:26801179208e | 71 | controller->ki_d = KI_D; |
benkatz | 45:26801179208e | 72 | controller->ki_q = KI_Q; |
benkatz | 45:26801179208e | 73 | controller->k_d = K_SCALE*I_BW; |
benkatz | 45:26801179208e | 74 | controller->k_q = K_SCALE*I_BW; |
benkatz | 45:26801179208e | 75 | controller->alpha = 1.0f - 1.0f/(1.0f - DT*I_BW*2.0f*PI); |
benkatz | 45:26801179208e | 76 | |
benkatz | 45:26801179208e | 77 | } |
benkatz | 22:60276ba87ac6 | 78 | |
benkatz | 22:60276ba87ac6 | 79 | void reset_foc(ControllerStruct *controller){ |
benkatz | 28:8c7e29f719c5 | 80 | TIM1->CCR3 = (PWM_ARR>>1)*(0.5f); |
benkatz | 28:8c7e29f719c5 | 81 | TIM1->CCR1 = (PWM_ARR>>1)*(0.5f); |
benkatz | 28:8c7e29f719c5 | 82 | TIM1->CCR2 = (PWM_ARR>>1)*(0.5f); |
benkatz | 28:8c7e29f719c5 | 83 | controller->i_d_ref = 0; |
benkatz | 28:8c7e29f719c5 | 84 | controller->i_q_ref = 0; |
benkatz | 28:8c7e29f719c5 | 85 | controller->i_d = 0; |
benkatz | 28:8c7e29f719c5 | 86 | controller->i_q = 0; |
benkatz | 37:c0f352d6e8e3 | 87 | controller->i_q_filt = 0; |
benkatz | 22:60276ba87ac6 | 88 | controller->q_int = 0; |
benkatz | 22:60276ba87ac6 | 89 | controller->d_int = 0; |
benkatz | 28:8c7e29f719c5 | 90 | controller->v_q = 0; |
benkatz | 28:8c7e29f719c5 | 91 | controller->v_d = 0; |
benkatz | 47:e1196a851f76 | 92 | |
benkatz | 22:60276ba87ac6 | 93 | } |
benkatz | 45:26801179208e | 94 | |
benkatz | 45:26801179208e | 95 | void limit_current_ref (ControllerStruct *controller){ |
benkatz | 45:26801179208e | 96 | float i_q_max_limit = (0.5774f*controller->v_bus - controller->dtheta_elec*WB)/R_PHASE; |
benkatz | 45:26801179208e | 97 | float i_q_min_limit = (-0.5774f*controller->v_bus - controller->dtheta_elec*WB)/R_PHASE; |
benkatz | 45:26801179208e | 98 | controller->i_q_ref = fmaxf(fminf(i_q_max_limit, controller->i_q_ref), i_q_min_limit); |
benkatz | 45:26801179208e | 99 | } |
benkatz | 22:60276ba87ac6 | 100 | |
benkatz | 22:60276ba87ac6 | 101 | |
benkatz | 47:e1196a851f76 | 102 | void commutate(ControllerStruct *controller, GPIOStruct *gpio, float theta){ |
benkatz | 47:e1196a851f76 | 103 | |
benkatz | 25:f5741040c4bb | 104 | /// Commutation Loop /// |
benkatz | 25:f5741040c4bb | 105 | controller->loop_count ++; |
benkatz | 25:f5741040c4bb | 106 | if(PHASE_ORDER){ // Check current sensor ordering |
benkatz | 25:f5741040c4bb | 107 | controller->i_b = I_SCALE*(float)(controller->adc2_raw - controller->adc2_offset); // Calculate phase currents from ADC readings |
benkatz | 22:60276ba87ac6 | 108 | controller->i_c = I_SCALE*(float)(controller->adc1_raw - controller->adc1_offset); |
benkatz | 22:60276ba87ac6 | 109 | } |
benkatz | 22:60276ba87ac6 | 110 | else{ |
benkatz | 25:f5741040c4bb | 111 | controller->i_b = I_SCALE*(float)(controller->adc1_raw - controller->adc1_offset); |
benkatz | 22:60276ba87ac6 | 112 | controller->i_c = I_SCALE*(float)(controller->adc2_raw - controller->adc2_offset); |
benkatz | 22:60276ba87ac6 | 113 | } |
benkatz | 26:2b865c00d7e9 | 114 | controller->i_a = -controller->i_b - controller->i_c; |
benkatz | 22:60276ba87ac6 | 115 | |
benkatz | 26:2b865c00d7e9 | 116 | float s = FastSin(theta); |
benkatz | 26:2b865c00d7e9 | 117 | float c = FastCos(theta); |
benkatz | 44:8040fa2fcb0d | 118 | 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 | 119 | //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 | 120 | //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 | 121 | |
benkatz | 37:c0f352d6e8e3 | 122 | controller->i_q_filt = 0.95f*controller->i_q_filt + 0.05f*controller->i_q; |
benkatz | 47:e1196a851f76 | 123 | controller->i_d_filt = 0.95f*controller->i_d_filt + 0.05f*controller->i_d; |
benkatz | 37:c0f352d6e8e3 | 124 | |
benkatz | 45:26801179208e | 125 | |
benkatz | 45:26801179208e | 126 | // Filter the current references to the desired closed-loopbandwidth |
benkatz | 45:26801179208e | 127 | // Allows calculation of desired di/dt for inductance, etc |
benkatz | 45:26801179208e | 128 | controller->did_dt = controller->i_d_ref_filt; |
benkatz | 45:26801179208e | 129 | controller->diq_dt = controller->i_q_ref_filt; |
benkatz | 45:26801179208e | 130 | controller->i_d_ref_filt = (1.0f-controller->alpha)*controller->i_d_ref_filt + controller->alpha*controller->i_d_ref; |
benkatz | 45:26801179208e | 131 | controller->i_q_ref_filt = (1.0f-controller->alpha)*controller->i_q_ref_filt + controller->alpha*controller->i_q_ref; |
benkatz | 45:26801179208e | 132 | controller->did_dt = (controller->i_d_ref_filt - controller->did_dt)/DT; |
benkatz | 45:26801179208e | 133 | controller->diq_dt = (controller->i_q_ref_filt - controller->diq_dt)/DT; |
benkatz | 25:f5741040c4bb | 134 | |
benkatz | 47:e1196a851f76 | 135 | |
benkatz | 47:e1196a851f76 | 136 | /// Field Weakening /// |
benkatz | 47:e1196a851f76 | 137 | /* |
benkatz | 47:e1196a851f76 | 138 | controller->fw_int += .001*(0.5f*OVERMODULATION*controller->v_bus - controller->v_ref); |
benkatz | 47:e1196a851f76 | 139 | controller->fw_int = fmaxf(fminf(controller->fw_int, 0.0f), -I_MAX_FW); |
benkatz | 47:e1196a851f76 | 140 | controller->i_d_ref = controller->fw_int; |
benkatz | 47:e1196a851f76 | 141 | float i_cmd_mag_sq = controller->i_d_ref*controller->i_d_ref + controller->i_q_ref*controller->i_q_ref; |
benkatz | 47:e1196a851f76 | 142 | limit_norm(&controller->i_d_ref, &controller->i_q_ref, I_MAX); |
benkatz | 47:e1196a851f76 | 143 | |
benkatz | 47:e1196a851f76 | 144 | */ |
benkatz | 47:e1196a851f76 | 145 | |
benkatz | 25:f5741040c4bb | 146 | /// PI Controller /// |
benkatz | 22:60276ba87ac6 | 147 | float i_d_error = controller->i_d_ref - controller->i_d; |
benkatz | 45:26801179208e | 148 | float i_q_error = controller->i_q_ref - controller->i_q;// + cogging_current; |
benkatz | 37:c0f352d6e8e3 | 149 | |
benkatz | 45:26801179208e | 150 | // Calculate feed-forward voltages // |
benkatz | 47:e1196a851f76 | 151 | float v_d_ff = 2.0f*(1.0f*controller->i_d_ref*R_PHASE - controller->dtheta_elec*L_Q*controller->i_q); //feed-forward voltages |
benkatz | 47:e1196a851f76 | 152 | float v_q_ff = 2.0f*(1.0f*controller->i_q_ref*R_PHASE + controller->dtheta_elec*(L_D*controller->i_d + 1.0f*WB)); |
benkatz | 37:c0f352d6e8e3 | 153 | |
benkatz | 45:26801179208e | 154 | // Integrate Error // |
benkatz | 47:e1196a851f76 | 155 | controller->d_int += controller->k_d*controller->ki_d*i_d_error; |
benkatz | 47:e1196a851f76 | 156 | controller->q_int += controller->k_q*controller->ki_q*i_q_error; |
benkatz | 47:e1196a851f76 | 157 | |
benkatz | 47:e1196a851f76 | 158 | controller->d_int = fmaxf(fminf(controller->d_int, OVERMODULATION*controller->v_bus), - OVERMODULATION*controller->v_bus); |
benkatz | 47:e1196a851f76 | 159 | controller->q_int = fmaxf(fminf(controller->q_int, OVERMODULATION*controller->v_bus), - OVERMODULATION*controller->v_bus); |
benkatz | 47:e1196a851f76 | 160 | |
benkatz | 47:e1196a851f76 | 161 | //limit_norm(&controller->d_int, &controller->q_int, OVERMODULATION*controller->v_bus); |
benkatz | 47:e1196a851f76 | 162 | controller->v_d = controller->k_d*i_d_error + controller->d_int ;//+ v_d_ff; |
benkatz | 47:e1196a851f76 | 163 | controller->v_q = controller->k_q*i_q_error + controller->q_int ;//+ v_q_ff; |
benkatz | 47:e1196a851f76 | 164 | |
benkatz | 47:e1196a851f76 | 165 | controller->v_ref = sqrt(controller->v_d*controller->v_d + controller->v_q*controller->v_q); |
benkatz | 22:60276ba87ac6 | 166 | |
benkatz | 44:8040fa2fcb0d | 167 | limit_norm(&controller->v_d, &controller->v_q, OVERMODULATION*controller->v_bus); // Normalize voltage vector to lie within curcle of radius v_bus |
benkatz | 47:e1196a851f76 | 168 | 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 | 169 | 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 | 170 | |
benkatz | 25:f5741040c4bb | 171 | if(PHASE_ORDER){ // Check which phase order to use, |
benkatz | 31:61eb6ae28215 | 172 | TIM1->CCR3 = (PWM_ARR)*(1.0f-controller->dtc_u); // Write duty cycles |
benkatz | 31:61eb6ae28215 | 173 | TIM1->CCR2 = (PWM_ARR)*(1.0f-controller->dtc_v); |
benkatz | 31:61eb6ae28215 | 174 | TIM1->CCR1 = (PWM_ARR)*(1.0f-controller->dtc_w); |
benkatz | 22:60276ba87ac6 | 175 | } |
benkatz | 22:60276ba87ac6 | 176 | else{ |
benkatz | 32:ccac5da77844 | 177 | TIM1->CCR3 = (PWM_ARR)*(1.0f-controller->dtc_u); |
benkatz | 32:ccac5da77844 | 178 | TIM1->CCR1 = (PWM_ARR)*(1.0f-controller->dtc_v); |
benkatz | 32:ccac5da77844 | 179 | TIM1->CCR2 = (PWM_ARR)*(1.0f-controller->dtc_w); |
benkatz | 22:60276ba87ac6 | 180 | } |
benkatz | 25:f5741040c4bb | 181 | |
benkatz | 25:f5741040c4bb | 182 | controller->theta_elec = theta; //For some reason putting this at the front breaks thins |
benkatz | 22:60276ba87ac6 | 183 | |
benkatz | 22:60276ba87ac6 | 184 | } |
benkatz | 26:2b865c00d7e9 | 185 | |
benkatz | 26:2b865c00d7e9 | 186 | |
benkatz | 26:2b865c00d7e9 | 187 | void torque_control(ControllerStruct *controller){ |
benkatz | 28:8c7e29f719c5 | 188 | 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 | 189 | //float torque_ref = -.1*(controller->p_des - controller->theta_mech); |
benkatz | 26:2b865c00d7e9 | 190 | controller->i_q_ref = torque_ref/KT_OUT; |
benkatz | 37:c0f352d6e8e3 | 191 | controller->i_d_ref = 0.0f; |
benkatz | 26:2b865c00d7e9 | 192 | } |
benkatz | 47:e1196a851f76 | 193 |