1
Dependencies: mbed-dev-f303 FastPWM3
FOC/foc.cpp@57:059bd1333ba4, 2020-10-16 (annotated)
- Committer:
- shaorui
- Date:
- Fri Oct 16 09:08:47 2020 +0000
- Revision:
- 57:059bd1333ba4
- Parent:
- 55:c4c9fec8539c
1
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 | 55:c4c9fec8539c | 32 | void svm(float v_bus, float u, float v, float w, int i_sector, 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 | 55:c4c9fec8539c | 38 | // Dead-time compensation |
benkatz | 55:c4c9fec8539c | 39 | float u_comp = DTC_COMP*(-(i_sector==4) + (i_sector==3)); |
benkatz | 55:c4c9fec8539c | 40 | float v_comp = DTC_COMP*(-(i_sector==2) + (i_sector==5)); |
benkatz | 55:c4c9fec8539c | 41 | float w_comp = DTC_COMP*((i_sector==6) - (i_sector==1)); |
benkatz | 55:c4c9fec8539c | 42 | |
benkatz | 55:c4c9fec8539c | 43 | |
benkatz | 55:c4c9fec8539c | 44 | *dtc_u = fminf(fmaxf((.5f*(u -v_offset)/(v_bus*(DTC_MAX-DTC_MIN)) + (DTC_MAX+DTC_MIN)*.5f + u_comp), DTC_MIN), DTC_MAX); |
benkatz | 55:c4c9fec8539c | 45 | *dtc_v = fminf(fmaxf((.5f*(v -v_offset)/(v_bus*(DTC_MAX-DTC_MIN)) + (DTC_MAX+DTC_MIN)*.5f + v_comp), DTC_MIN), DTC_MAX); |
benkatz | 55:c4c9fec8539c | 46 | *dtc_w = fminf(fmaxf((.5f*(w -v_offset)/(v_bus*(DTC_MAX-DTC_MIN)) + (DTC_MAX+DTC_MIN)*.5f + w_comp), DTC_MIN), DTC_MAX); |
benkatz | 47:e1196a851f76 | 47 | |
benkatz | 47:e1196a851f76 | 48 | /* |
benkatz | 47:e1196a851f76 | 49 | sinusoidal pwm |
benkatz | 47:e1196a851f76 | 50 | *dtc_u = fminf(fmaxf((u/v_bus + .5f), DTC_MIN), DTC_MAX); |
benkatz | 47:e1196a851f76 | 51 | *dtc_v = fminf(fmaxf((v/v_bus + .5f), DTC_MIN), DTC_MAX); |
benkatz | 47:e1196a851f76 | 52 | *dtc_w = fminf(fmaxf((w/v_bus + .5f), DTC_MIN), DTC_MAX); |
benkatz | 47:e1196a851f76 | 53 | */ |
benkatz | 47:e1196a851f76 | 54 | |
benkatz | 22:60276ba87ac6 | 55 | |
benkatz | 22:60276ba87ac6 | 56 | } |
benkatz | 22:60276ba87ac6 | 57 | |
benkatz | 25:f5741040c4bb | 58 | void zero_current(int *offset_1, int *offset_2){ // Measure zero-offset of the current sensors |
benkatz | 22:60276ba87ac6 | 59 | int adc1_offset = 0; |
benkatz | 22:60276ba87ac6 | 60 | int adc2_offset = 0; |
benkatz | 22:60276ba87ac6 | 61 | int n = 1024; |
benkatz | 25:f5741040c4bb | 62 | for (int i = 0; i<n; i++){ // Average n samples of the ADC |
benkatz | 27:501fee691e0e | 63 | TIM1->CCR3 = (PWM_ARR>>1)*(1.0f); // Write duty cycles |
benkatz | 27:501fee691e0e | 64 | TIM1->CCR2 = (PWM_ARR>>1)*(1.0f); |
benkatz | 27:501fee691e0e | 65 | TIM1->CCR1 = (PWM_ARR>>1)*(1.0f); |
benkatz | 26:2b865c00d7e9 | 66 | ADC1->CR2 |= 0x40000000; // Begin sample and conversion |
benkatz | 22:60276ba87ac6 | 67 | wait(.001); |
benkatz | 22:60276ba87ac6 | 68 | adc2_offset += ADC2->DR; |
benkatz | 22:60276ba87ac6 | 69 | adc1_offset += ADC1->DR; |
benkatz | 22:60276ba87ac6 | 70 | } |
benkatz | 22:60276ba87ac6 | 71 | *offset_1 = adc1_offset/n; |
benkatz | 22:60276ba87ac6 | 72 | *offset_2 = adc2_offset/n; |
benkatz | 22:60276ba87ac6 | 73 | } |
benkatz | 45:26801179208e | 74 | |
benkatz | 45:26801179208e | 75 | void init_controller_params(ControllerStruct *controller){ |
benkatz | 45:26801179208e | 76 | controller->ki_d = KI_D; |
benkatz | 45:26801179208e | 77 | controller->ki_q = KI_Q; |
benkatz | 45:26801179208e | 78 | controller->k_d = K_SCALE*I_BW; |
benkatz | 45:26801179208e | 79 | controller->k_q = K_SCALE*I_BW; |
benkatz | 45:26801179208e | 80 | controller->alpha = 1.0f - 1.0f/(1.0f - DT*I_BW*2.0f*PI); |
benkatz | 55:c4c9fec8539c | 81 | for(int i = 0; i<128; i++) |
benkatz | 55:c4c9fec8539c | 82 | { |
benkatz | 55:c4c9fec8539c | 83 | controller->inverter_tab[i] = 1.0f + 1.2f*exp(-0.0078125f*i/.032f); |
benkatz | 55:c4c9fec8539c | 84 | } |
benkatz | 45:26801179208e | 85 | } |
benkatz | 22:60276ba87ac6 | 86 | |
benkatz | 22:60276ba87ac6 | 87 | void reset_foc(ControllerStruct *controller){ |
benkatz | 28:8c7e29f719c5 | 88 | TIM1->CCR3 = (PWM_ARR>>1)*(0.5f); |
benkatz | 28:8c7e29f719c5 | 89 | TIM1->CCR1 = (PWM_ARR>>1)*(0.5f); |
benkatz | 28:8c7e29f719c5 | 90 | TIM1->CCR2 = (PWM_ARR>>1)*(0.5f); |
benkatz | 28:8c7e29f719c5 | 91 | controller->i_d_ref = 0; |
benkatz | 28:8c7e29f719c5 | 92 | controller->i_q_ref = 0; |
benkatz | 28:8c7e29f719c5 | 93 | controller->i_d = 0; |
benkatz | 28:8c7e29f719c5 | 94 | controller->i_q = 0; |
benkatz | 37:c0f352d6e8e3 | 95 | controller->i_q_filt = 0; |
benkatz | 22:60276ba87ac6 | 96 | controller->q_int = 0; |
benkatz | 22:60276ba87ac6 | 97 | controller->d_int = 0; |
benkatz | 28:8c7e29f719c5 | 98 | controller->v_q = 0; |
benkatz | 28:8c7e29f719c5 | 99 | controller->v_d = 0; |
benkatz | 55:c4c9fec8539c | 100 | controller->otw_flag = 0; |
benkatz | 47:e1196a851f76 | 101 | |
benkatz | 22:60276ba87ac6 | 102 | } |
benkatz | 45:26801179208e | 103 | |
benkatz | 48:74a40481740c | 104 | void reset_observer(ObserverStruct *observer){ |
benkatz | 55:c4c9fec8539c | 105 | |
benkatz | 48:74a40481740c | 106 | observer->temperature = 25.0f; |
benkatz | 55:c4c9fec8539c | 107 | observer->temp_measured = 25.0f; |
benkatz | 55:c4c9fec8539c | 108 | //observer->resistance = .1f; |
benkatz | 48:74a40481740c | 109 | } |
benkatz | 48:74a40481740c | 110 | |
benkatz | 45:26801179208e | 111 | void limit_current_ref (ControllerStruct *controller){ |
benkatz | 45:26801179208e | 112 | float i_q_max_limit = (0.5774f*controller->v_bus - controller->dtheta_elec*WB)/R_PHASE; |
benkatz | 45:26801179208e | 113 | float i_q_min_limit = (-0.5774f*controller->v_bus - controller->dtheta_elec*WB)/R_PHASE; |
benkatz | 45:26801179208e | 114 | controller->i_q_ref = fmaxf(fminf(i_q_max_limit, controller->i_q_ref), i_q_min_limit); |
benkatz | 45:26801179208e | 115 | } |
benkatz | 22:60276ba87ac6 | 116 | |
benkatz | 55:c4c9fec8539c | 117 | void update_observer(ControllerStruct *controller, ObserverStruct *observer) |
benkatz | 55:c4c9fec8539c | 118 | { |
benkatz | 55:c4c9fec8539c | 119 | /// Update observer estimates /// |
benkatz | 55:c4c9fec8539c | 120 | // Resistance observer // |
benkatz | 55:c4c9fec8539c | 121 | // Temperature Observer // |
benkatz | 55:c4c9fec8539c | 122 | observer->delta_t = (float)observer->temperature - T_AMBIENT; |
benkatz | 55:c4c9fec8539c | 123 | float i_sq = controller->i_d*controller->i_d + controller->i_q*controller->i_q; |
benkatz | 55:c4c9fec8539c | 124 | observer->q_in = (R_NOMINAL*1.5f)*(1.0f + .00393f*observer->delta_t)*i_sq; |
benkatz | 55:c4c9fec8539c | 125 | observer->q_out = observer->delta_t*R_TH; |
benkatz | 55:c4c9fec8539c | 126 | observer->temperature += (INV_M_TH*DT)*(observer->q_in-observer->q_out); |
benkatz | 55:c4c9fec8539c | 127 | |
benkatz | 55:c4c9fec8539c | 128 | //float r_d = (controller->v_d*(DTC_MAX-DTC_MIN) + SQRT3*controller->dtheta_elec*(L_Q*controller->i_q))/(controller->i_d*SQRT3); |
benkatz | 55:c4c9fec8539c | 129 | float r_q = (controller->v_q*(DTC_MAX-DTC_MIN) - SQRT3*controller->dtheta_elec*(L_D*controller->i_d + WB))/(controller->i_q*SQRT3); |
benkatz | 55:c4c9fec8539c | 130 | observer->resistance = r_q;//(r_d*controller->i_d + r_q*controller->i_q)/(controller->i_d + controller->i_q); // voltages more accurate at higher duty cycles |
benkatz | 55:c4c9fec8539c | 131 | |
benkatz | 55:c4c9fec8539c | 132 | //observer->resistance = controller->v_q/controller->i_q; |
benkatz | 55:c4c9fec8539c | 133 | if(isnan(observer->resistance) || isinf(observer->resistance)){observer->resistance = R_NOMINAL;} |
benkatz | 55:c4c9fec8539c | 134 | float t_raw = ((T_AMBIENT + ((observer->resistance/R_NOMINAL) - 1.0f)*254.5f)); |
benkatz | 55:c4c9fec8539c | 135 | if(t_raw > 200.0f){t_raw = 200.0f;} |
benkatz | 55:c4c9fec8539c | 136 | else if(t_raw < 0.0f){t_raw = 0.0f;} |
benkatz | 55:c4c9fec8539c | 137 | observer->temp_measured = .999f*observer->temp_measured + .001f*t_raw; |
benkatz | 55:c4c9fec8539c | 138 | float e = (float)observer->temperature - observer->temp_measured; |
benkatz | 55:c4c9fec8539c | 139 | observer->trust = (1.0f - .004f*fminf(abs(controller->dtheta_elec), 250.0f)) * (.01f*(fminf(i_sq, 100.0f))); |
benkatz | 55:c4c9fec8539c | 140 | observer->temperature -= observer->trust*.0001f*e; |
benkatz | 55:c4c9fec8539c | 141 | //printf("%.3f\n\r", e); |
benkatz | 55:c4c9fec8539c | 142 | |
benkatz | 55:c4c9fec8539c | 143 | if(observer->temperature > TEMP_MAX){controller->otw_flag = 1;} |
benkatz | 55:c4c9fec8539c | 144 | else{controller->otw_flag = 0;} |
benkatz | 55:c4c9fec8539c | 145 | } |
benkatz | 22:60276ba87ac6 | 146 | |
benkatz | 55:c4c9fec8539c | 147 | float linearize_dtc(ControllerStruct *controller, float dtc) |
benkatz | 55:c4c9fec8539c | 148 | { |
benkatz | 55:c4c9fec8539c | 149 | float duty = fmaxf(fminf(abs(dtc), .999f), 0.0f);; |
benkatz | 55:c4c9fec8539c | 150 | int index = (int) (duty*127.0f); |
benkatz | 55:c4c9fec8539c | 151 | float val1 = controller->inverter_tab[index]; |
benkatz | 55:c4c9fec8539c | 152 | float val2 = controller->inverter_tab[index+1]; |
benkatz | 55:c4c9fec8539c | 153 | return val1 + (val2 - val1)*(duty*128.0f - (float)index); |
benkatz | 55:c4c9fec8539c | 154 | } |
benkatz | 47:e1196a851f76 | 155 | |
benkatz | 55:c4c9fec8539c | 156 | void field_weaken(ControllerStruct *controller) |
benkatz | 55:c4c9fec8539c | 157 | { |
benkatz | 55:c4c9fec8539c | 158 | /// Field Weakening /// |
benkatz | 55:c4c9fec8539c | 159 | |
benkatz | 55:c4c9fec8539c | 160 | controller->fw_int += .001f*(0.5f*OVERMODULATION*controller->v_bus - controller->v_ref); |
benkatz | 55:c4c9fec8539c | 161 | controller->fw_int = fmaxf(fminf(controller->fw_int, 0.0f), -I_FW_MAX); |
benkatz | 55:c4c9fec8539c | 162 | controller->i_d_ref = controller->fw_int; |
benkatz | 55:c4c9fec8539c | 163 | float q_max = sqrt(controller->i_max*controller->i_max - controller->i_d_ref*controller->i_d_ref); |
benkatz | 55:c4c9fec8539c | 164 | controller->i_q_ref = fmaxf(fminf(controller->i_q_ref, q_max), -q_max); |
benkatz | 55:c4c9fec8539c | 165 | //float i_cmd_mag_sq = controller->i_d_ref*controller->i_d_ref + controller->i_q_ref*controller->i_q_ref; |
benkatz | 55:c4c9fec8539c | 166 | |
benkatz | 55:c4c9fec8539c | 167 | } |
benkatz | 55:c4c9fec8539c | 168 | void commutate(ControllerStruct *controller, ObserverStruct *observer, GPIOStruct *gpio, float theta) |
benkatz | 55:c4c9fec8539c | 169 | { |
benkatz | 25:f5741040c4bb | 170 | /// Commutation Loop /// |
benkatz | 25:f5741040c4bb | 171 | controller->loop_count ++; |
benkatz | 25:f5741040c4bb | 172 | if(PHASE_ORDER){ // Check current sensor ordering |
benkatz | 25:f5741040c4bb | 173 | controller->i_b = I_SCALE*(float)(controller->adc2_raw - controller->adc2_offset); // Calculate phase currents from ADC readings |
benkatz | 22:60276ba87ac6 | 174 | controller->i_c = I_SCALE*(float)(controller->adc1_raw - controller->adc1_offset); |
benkatz | 22:60276ba87ac6 | 175 | } |
benkatz | 22:60276ba87ac6 | 176 | else{ |
benkatz | 25:f5741040c4bb | 177 | controller->i_b = I_SCALE*(float)(controller->adc1_raw - controller->adc1_offset); |
benkatz | 22:60276ba87ac6 | 178 | controller->i_c = I_SCALE*(float)(controller->adc2_raw - controller->adc2_offset); |
benkatz | 22:60276ba87ac6 | 179 | } |
benkatz | 26:2b865c00d7e9 | 180 | controller->i_a = -controller->i_b - controller->i_c; |
benkatz | 55:c4c9fec8539c | 181 | if((abs(controller->i_b) > 41.0f)|(abs(controller->i_c) > 41.0f)|(abs(controller->i_a) > 41.0f)){controller->oc_flag = 1;} |
benkatz | 22:60276ba87ac6 | 182 | |
benkatz | 26:2b865c00d7e9 | 183 | float s = FastSin(theta); |
benkatz | 26:2b865c00d7e9 | 184 | float c = FastCos(theta); |
benkatz | 44:8040fa2fcb0d | 185 | 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 | 186 | //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 | 187 | //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 | 188 | |
benkatz | 37:c0f352d6e8e3 | 189 | controller->i_q_filt = 0.95f*controller->i_q_filt + 0.05f*controller->i_q; |
benkatz | 47:e1196a851f76 | 190 | controller->i_d_filt = 0.95f*controller->i_d_filt + 0.05f*controller->i_d; |
benkatz | 37:c0f352d6e8e3 | 191 | |
benkatz | 45:26801179208e | 192 | |
benkatz | 49:83d83040ea51 | 193 | // Filter the current references to the desired closed-loop bandwidth |
benkatz | 55:c4c9fec8539c | 194 | //controller->i_d_ref_filt = (1.0f-controller->alpha)*controller->i_d_ref_filt + controller->alpha*controller->i_d_ref; |
benkatz | 55:c4c9fec8539c | 195 | //controller->i_q_ref_filt = (1.0f-controller->alpha)*controller->i_q_ref_filt + controller->alpha*controller->i_q_ref; |
benkatz | 55:c4c9fec8539c | 196 | |
benkatz | 55:c4c9fec8539c | 197 | controller->i_max = I_MAX*(!controller->otw_flag) + I_MAX_CONT*controller->otw_flag; |
benkatz | 55:c4c9fec8539c | 198 | |
benkatz | 55:c4c9fec8539c | 199 | // Temperature Controller // |
benkatz | 55:c4c9fec8539c | 200 | /* |
benkatz | 55:c4c9fec8539c | 201 | if(observer->temperature > TEMP_MAX) |
benkatz | 55:c4c9fec8539c | 202 | { |
benkatz | 55:c4c9fec8539c | 203 | float qdot_des = 1.0f*(TEMP_MAX - observer->temperature); |
benkatz | 55:c4c9fec8539c | 204 | float i_limit = sqrt((qdot_des + observer->q_out)/(R_NOMINAL*1.5f)); |
benkatz | 55:c4c9fec8539c | 205 | controller->i_max = fmaxf(fminf(i_limit, I_MAX), I_MAX_CONT); |
benkatz | 55:c4c9fec8539c | 206 | } |
benkatz | 55:c4c9fec8539c | 207 | else{controller->i_max = I_MAX;} |
benkatz | 55:c4c9fec8539c | 208 | */ |
benkatz | 55:c4c9fec8539c | 209 | |
benkatz | 55:c4c9fec8539c | 210 | limit_norm(&controller->i_d_ref, &controller->i_q_ref, controller->i_max); |
benkatz | 48:74a40481740c | 211 | |
benkatz | 25:f5741040c4bb | 212 | /// PI Controller /// |
benkatz | 22:60276ba87ac6 | 213 | float i_d_error = controller->i_d_ref - controller->i_d; |
benkatz | 45:26801179208e | 214 | float i_q_error = controller->i_q_ref - controller->i_q;// + cogging_current; |
benkatz | 37:c0f352d6e8e3 | 215 | |
benkatz | 45:26801179208e | 216 | // Calculate feed-forward voltages // |
benkatz | 55:c4c9fec8539c | 217 | float v_d_ff = SQRT3*(0.0f*controller->i_d_ref*R_PHASE - controller->dtheta_elec*L_Q*controller->i_q); //feed-forward voltages |
benkatz | 55:c4c9fec8539c | 218 | float v_q_ff = SQRT3*(0.0f*controller->i_q_ref*R_PHASE + controller->dtheta_elec*(L_D*controller->i_d + 0.0f*WB)); |
benkatz | 37:c0f352d6e8e3 | 219 | |
benkatz | 45:26801179208e | 220 | // Integrate Error // |
benkatz | 47:e1196a851f76 | 221 | controller->d_int += controller->k_d*controller->ki_d*i_d_error; |
benkatz | 47:e1196a851f76 | 222 | controller->q_int += controller->k_q*controller->ki_q*i_q_error; |
benkatz | 47:e1196a851f76 | 223 | |
benkatz | 47:e1196a851f76 | 224 | controller->d_int = fmaxf(fminf(controller->d_int, OVERMODULATION*controller->v_bus), - OVERMODULATION*controller->v_bus); |
benkatz | 47:e1196a851f76 | 225 | controller->q_int = fmaxf(fminf(controller->q_int, OVERMODULATION*controller->v_bus), - OVERMODULATION*controller->v_bus); |
benkatz | 47:e1196a851f76 | 226 | |
benkatz | 47:e1196a851f76 | 227 | //limit_norm(&controller->d_int, &controller->q_int, OVERMODULATION*controller->v_bus); |
benkatz | 55:c4c9fec8539c | 228 | controller->v_d = controller->k_d*i_d_error + controller->d_int;// + v_d_ff; |
benkatz | 55:c4c9fec8539c | 229 | controller->v_q = controller->k_q*i_q_error + controller->q_int;// + v_q_ff; |
benkatz | 55:c4c9fec8539c | 230 | //controller->v_q = 0.0f; |
benkatz | 55:c4c9fec8539c | 231 | //controller->v_d = 1.0f*controller->v_bus; |
benkatz | 47:e1196a851f76 | 232 | controller->v_ref = sqrt(controller->v_d*controller->v_d + controller->v_q*controller->v_q); |
benkatz | 22:60276ba87ac6 | 233 | |
benkatz | 44:8040fa2fcb0d | 234 | limit_norm(&controller->v_d, &controller->v_q, OVERMODULATION*controller->v_bus); // Normalize voltage vector to lie within curcle of radius v_bus |
benkatz | 55:c4c9fec8539c | 235 | float dtc = controller->v_ref/controller->v_bus; |
benkatz | 55:c4c9fec8539c | 236 | float scale = linearize_dtc(controller, dtc); |
benkatz | 55:c4c9fec8539c | 237 | //controller->v_d = scale*controller->v_d; |
benkatz | 55:c4c9fec8539c | 238 | //controller->v_q = scale*controller->v_q; |
benkatz | 53:e85efce8c1eb | 239 | //float dtc_q = controller->v_q/controller->v_bus; |
benkatz | 55:c4c9fec8539c | 240 | |
benkatz | 53:e85efce8c1eb | 241 | //linearize_dtc(&dtc_q); |
benkatz | 53:e85efce8c1eb | 242 | //controller->v_d = dtc_d*controller->v_bus; |
benkatz | 53:e85efce8c1eb | 243 | //controller->v_q = dtc_q*controller->v_bus; |
benkatz | 55:c4c9fec8539c | 244 | abc(controller->theta_elec + 0.0f*DT*controller->dtheta_elec, scale*controller->v_d, scale*controller->v_q, &controller->v_u, &controller->v_v, &controller->v_w); //inverse dq0 transform on voltages |
benkatz | 55:c4c9fec8539c | 245 | controller->current_sector = ((controller->i_a>0)<<2)|((controller->i_b>0)<<1)|(controller->i_c>0); |
benkatz | 55:c4c9fec8539c | 246 | svm(controller->v_bus, controller->v_u, controller->v_v, controller->v_w, controller->current_sector, &controller->dtc_u, &controller->dtc_v, &controller->dtc_w); //space vector modulation |
benkatz | 55:c4c9fec8539c | 247 | |
benkatz | 55:c4c9fec8539c | 248 | |
benkatz | 55:c4c9fec8539c | 249 | |
benkatz | 22:60276ba87ac6 | 250 | |
benkatz | 25:f5741040c4bb | 251 | if(PHASE_ORDER){ // Check which phase order to use, |
benkatz | 31:61eb6ae28215 | 252 | TIM1->CCR3 = (PWM_ARR)*(1.0f-controller->dtc_u); // Write duty cycles |
benkatz | 31:61eb6ae28215 | 253 | TIM1->CCR2 = (PWM_ARR)*(1.0f-controller->dtc_v); |
benkatz | 31:61eb6ae28215 | 254 | TIM1->CCR1 = (PWM_ARR)*(1.0f-controller->dtc_w); |
benkatz | 22:60276ba87ac6 | 255 | } |
benkatz | 22:60276ba87ac6 | 256 | else{ |
benkatz | 32:ccac5da77844 | 257 | TIM1->CCR3 = (PWM_ARR)*(1.0f-controller->dtc_u); |
benkatz | 32:ccac5da77844 | 258 | TIM1->CCR1 = (PWM_ARR)*(1.0f-controller->dtc_v); |
benkatz | 32:ccac5da77844 | 259 | TIM1->CCR2 = (PWM_ARR)*(1.0f-controller->dtc_w); |
benkatz | 22:60276ba87ac6 | 260 | } |
benkatz | 25:f5741040c4bb | 261 | |
benkatz | 48:74a40481740c | 262 | controller->theta_elec = theta; |
benkatz | 22:60276ba87ac6 | 263 | |
benkatz | 22:60276ba87ac6 | 264 | } |
benkatz | 26:2b865c00d7e9 | 265 | |
benkatz | 26:2b865c00d7e9 | 266 | |
benkatz | 26:2b865c00d7e9 | 267 | void torque_control(ControllerStruct *controller){ |
benkatz | 28:8c7e29f719c5 | 268 | 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 | 269 | //float torque_ref = -.1*(controller->p_des - controller->theta_mech); |
benkatz | 26:2b865c00d7e9 | 270 | controller->i_q_ref = torque_ref/KT_OUT; |
benkatz | 37:c0f352d6e8e3 | 271 | controller->i_d_ref = 0.0f; |
benkatz | 26:2b865c00d7e9 | 272 | } |
benkatz | 47:e1196a851f76 | 273 |