Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed-dev-f303 FastPWM3
foc.cpp
00001 /* ----- 00002 移除速度控制,把位置控制改成PID 00003 00004 log file: 00005 11/05: 把現在的控制架構改成PID(torque_control) 00006 ----- */ 00007 00008 00009 #include "foc.h" 00010 00011 //#define POS_MAX 6.283185f //359.9999 deg 00012 using namespace FastMath; 00013 00014 00015 void abc( float theta, float d, float q, float *a, float *b, float *c){ 00016 /// Inverse DQ0 Transform /// 00017 ///Phase current amplitude = lengh of dq vector/// 00018 ///i.e. iq = 1, id = 0, peak phase current of 1/// 00019 float cf = FastCos(theta); 00020 float sf = FastSin(theta); 00021 00022 *a = cf*d - sf*q; // Faster Inverse DQ0 transform 00023 *b = (0.86602540378f*sf-.5f*cf)*d - (-0.86602540378f*cf-.5f*sf)*q; 00024 *c = (-0.86602540378f*sf-.5f*cf)*d - (0.86602540378f*cf-.5f*sf)*q; 00025 } 00026 00027 00028 void dq0(float theta, float a, float b, float c, float *d, float *q){ 00029 /// DQ0 Transform /// 00030 ///Phase current amplitude = lengh of dq vector/// 00031 ///i.e. iq = 1, id = 0, peak phase current of 1/// 00032 00033 float cf = FastCos(theta); 00034 float sf = FastSin(theta); 00035 00036 *d = 0.6666667f*(cf*a + (0.86602540378f*sf-.5f*cf)*b + (-0.86602540378f*sf-.5f*cf)*c); ///Faster DQ0 Transform 00037 *q = 0.6666667f*(-sf*a - (-0.86602540378f*cf-.5f*sf)*b - (0.86602540378f*cf-.5f*sf)*c); 00038 00039 } 00040 00041 void svm(float v_bus, float u, float v, float w, float *dtc_u, float *dtc_v, float *dtc_w){ 00042 /// Space Vector Modulation /// 00043 /// u,v,w amplitude = v_bus for full modulation depth /// 00044 00045 float v_offset = (fminf3(u, v, w) + fmaxf3(u, v, w))*0.5f; 00046 00047 *dtc_u = fminf(fmaxf(((u -v_offset)/v_bus + .5f), DTC_MIN), DTC_MAX); 00048 *dtc_v = fminf(fmaxf(((v -v_offset)/v_bus + .5f), DTC_MIN), DTC_MAX); 00049 *dtc_w = fminf(fmaxf(((w -v_offset)/v_bus + .5f), DTC_MIN), DTC_MAX); 00050 00051 /* 00052 sinusoidal pwm 00053 *dtc_u = fminf(fmaxf((u/v_bus + .5f), DTC_MIN), DTC_MAX); 00054 *dtc_v = fminf(fmaxf((v/v_bus + .5f), DTC_MIN), DTC_MAX); 00055 *dtc_w = fminf(fmaxf((w/v_bus + .5f), DTC_MIN), DTC_MAX); 00056 */ 00057 00058 00059 } 00060 00061 void linearize_dtc(float *dtc){ 00062 /// linearizes the output of the inverter, which is not linear for small duty cycles /// 00063 float sgn = 1.0f-(2.0f*(dtc<0)); 00064 if(abs(*dtc) >= .01f){ 00065 *dtc = *dtc*.986f+.014f*sgn; 00066 } 00067 else{ 00068 *dtc = 2.5f*(*dtc); 00069 } 00070 00071 } 00072 00073 00074 void zero_current(int *offset_1, int *offset_2){ // Measure zero-offset of the current sensors 00075 int adc1_offset = 0; 00076 int adc2_offset = 0; 00077 int n = 1024; 00078 for (int i = 0; i<n; i++){ // Average n samples of the ADC 00079 TIM1->CCR3 = (PWM_ARR>>1)*(1.0f); // Write duty cycles 00080 TIM1->CCR2 = (PWM_ARR>>1)*(1.0f); 00081 TIM1->CCR1 = (PWM_ARR>>1)*(1.0f); 00082 ADC1->CR2 |= 0x40000000; // Begin sample and conversion 00083 wait(.001); 00084 adc2_offset += ADC2->DR; 00085 adc1_offset += ADC1->DR; 00086 } 00087 *offset_1 = adc1_offset/n; 00088 *offset_2 = adc2_offset/n; 00089 } 00090 00091 void init_controller_params(ControllerStruct *controller){ 00092 controller->ki_d = KI_D; 00093 controller->ki_q = KI_Q; 00094 controller->k_d = K_SCALE*I_BW; 00095 controller->k_q = K_SCALE*I_BW; 00096 controller->alpha = 1.0f - 1.0f/(1.0f - DT*I_BW*2.0f*PI); 00097 00098 } 00099 00100 void reset_foc(ControllerStruct *controller){ 00101 TIM1->CCR3 = (PWM_ARR>>1)*(0.5f); 00102 TIM1->CCR1 = (PWM_ARR>>1)*(0.5f); 00103 TIM1->CCR2 = (PWM_ARR>>1)*(0.5f); 00104 controller->i_d_ref = 0; 00105 controller->i_q_ref = 0; 00106 controller->i_d = 0; 00107 controller->i_q = 0; 00108 controller->i_q_filt = 0; 00109 controller->q_int = 0; 00110 controller->d_int = 0; 00111 controller->v_q = 0; 00112 controller->v_d = 0; 00113 00114 } 00115 00116 void reset_observer(ObserverStruct *observer){ 00117 observer->temperature = 25.0f; 00118 observer->resistance = .1f; 00119 } 00120 00121 void limit_current_ref (ControllerStruct *controller){ 00122 float i_q_max_limit = (0.5774f*controller->v_bus - controller->dtheta_elec*WB)/R_PHASE; 00123 float i_q_min_limit = (-0.5774f*controller->v_bus - controller->dtheta_elec*WB)/R_PHASE; 00124 controller->i_q_ref = fmaxf(fminf(i_q_max_limit, controller->i_q_ref), i_q_min_limit); 00125 } 00126 00127 00128 void commutate(ControllerStruct *controller, ObserverStruct *observer, GPIOStruct *gpio, float theta){ 00129 00130 /// Update observer estimates /// 00131 // Resistance observer // 00132 // Temperature Observer // 00133 float t_rise = (float)observer->temperature - 25.0f; 00134 float q_th_in = (1.0f + .00393f*t_rise)*(controller->i_d*controller->i_d*R_PHASE*SQRT3 + controller->i_q*controller->i_q*R_PHASE*SQRT3); 00135 float q_th_out = t_rise*R_TH; 00136 observer->temperature += INV_M_TH*DT*(q_th_in-q_th_out); 00137 00138 observer->resistance = (controller->v_q - SQRT3*controller->dtheta_elec*(WB))/controller->i_q; 00139 //observer->resistance = controller->v_q/controller->i_q; 00140 if(isnan(observer->resistance)){observer->resistance = R_PHASE;} 00141 observer->temperature2 = (double)(25.0f + ((observer->resistance*6.0606f)-1.0f)*275.5f); 00142 double e = observer->temperature - observer->temperature2; 00143 observer->temperature -= .001*e; 00144 //printf("%.3f\n\r", e); 00145 00146 00147 /// Commutation Loop /// 00148 controller->loop_count ++; 00149 if(PHASE_ORDER){ // Check current sensor ordering 00150 controller->i_b = I_SCALE*(float)(controller->adc2_raw - controller->adc2_offset); // Calculate phase currents from ADC readings 00151 controller->i_c = I_SCALE*(float)(controller->adc1_raw - controller->adc1_offset); 00152 } 00153 else{ 00154 controller->i_b = I_SCALE*(float)(controller->adc1_raw - controller->adc1_offset); 00155 controller->i_c = I_SCALE*(float)(controller->adc2_raw - controller->adc2_offset); 00156 } 00157 controller->i_a = -controller->i_b - controller->i_c; 00158 00159 float s = FastSin(theta); 00160 float c = FastCos(theta); 00161 dq0(controller->theta_elec, controller->i_a, controller->i_b, controller->i_c, &controller->i_d, &controller->i_q); //dq0 transform on currents 00162 //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 00163 //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); 00164 00165 controller->i_q_filt = 0.95f*controller->i_q_filt + 0.05f*controller->i_q; 00166 controller->i_d_filt = 0.95f*controller->i_d_filt + 0.05f*controller->i_d; 00167 00168 00169 // Filter the current references to the desired closed-loop bandwidth 00170 controller->i_d_ref_filt = (1.0f-controller->alpha)*controller->i_d_ref_filt + controller->alpha*controller->i_d_ref; 00171 controller->i_q_ref_filt = (1.0f-controller->alpha)*controller->i_q_ref_filt + controller->alpha*controller->i_q_ref; 00172 00173 00174 /// Field Weakening /// 00175 00176 controller->fw_int += .001f*(0.5f*OVERMODULATION*controller->v_bus - controller->v_ref); 00177 controller->fw_int = fmaxf(fminf(controller->fw_int, 0.0f), -I_FW_MAX); 00178 controller->i_d_ref = controller->fw_int; 00179 //float i_cmd_mag_sq = controller->i_d_ref*controller->i_d_ref + controller->i_q_ref*controller->i_q_ref; 00180 limit_norm(&controller->i_d_ref, &controller->i_q_ref, I_MAX); 00181 00182 00183 00184 /// PI Controller /// 00185 float i_d_error = controller->i_d_ref - controller->i_d; 00186 float i_q_error = controller->i_q_ref - controller->i_q;// + cogging_current; 00187 00188 // Calculate feed-forward voltages // 00189 float v_d_ff = SQRT3*(1.0f*controller->i_d_ref*R_PHASE - controller->dtheta_elec*L_Q*controller->i_q); //feed-forward voltages 00190 float v_q_ff = SQRT3*(1.0f*controller->i_q_ref*R_PHASE + controller->dtheta_elec*(L_D*controller->i_d + 1.0f*WB)); 00191 00192 // Integrate Error // 00193 controller->d_int += controller->k_d*controller->ki_d*i_d_error; 00194 controller->q_int += controller->k_q*controller->ki_q*i_q_error; 00195 00196 controller->d_int = fmaxf(fminf(controller->d_int, OVERMODULATION*controller->v_bus), - OVERMODULATION*controller->v_bus); 00197 controller->q_int = fmaxf(fminf(controller->q_int, OVERMODULATION*controller->v_bus), - OVERMODULATION*controller->v_bus); 00198 00199 //limit_norm(&controller->d_int, &controller->q_int, OVERMODULATION*controller->v_bus); 00200 controller->v_d = controller->k_d*i_d_error + controller->d_int ;//+ v_d_ff; 00201 controller->v_q = controller->k_q*i_q_error + controller->q_int ;//+ v_q_ff; 00202 00203 controller->v_ref = sqrt(controller->v_d*controller->v_d + controller->v_q*controller->v_q); 00204 00205 limit_norm(&controller->v_d, &controller->v_q, OVERMODULATION*controller->v_bus); // Normalize voltage vector to lie within curcle of radius v_bus 00206 //float dtc_d = controller->v_d/controller->v_bus; 00207 //float dtc_q = controller->v_q/controller->v_bus; 00208 //linearize_dtc(&dtc_d); 00209 //linearize_dtc(&dtc_q); 00210 //controller->v_d = dtc_d*controller->v_bus; 00211 //controller->v_q = dtc_q*controller->v_bus; 00212 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 00213 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 00214 00215 if(PHASE_ORDER){ // Check which phase order to use, 00216 TIM1->CCR3 = (PWM_ARR)*(1.0f-controller->dtc_u); // Write duty cycles 00217 TIM1->CCR2 = (PWM_ARR)*(1.0f-controller->dtc_v); 00218 TIM1->CCR1 = (PWM_ARR)*(1.0f-controller->dtc_w); 00219 } 00220 else{ 00221 TIM1->CCR3 = (PWM_ARR)*(1.0f-controller->dtc_u); 00222 TIM1->CCR1 = (PWM_ARR)*(1.0f-controller->dtc_v); 00223 TIM1->CCR2 = (PWM_ARR)*(1.0f-controller->dtc_w); 00224 } 00225 00226 controller->theta_elec = theta; 00227 00228 } 00229 00230 00231 void torque_control(ControllerStruct *controller){ 00232 /*----- convert theta_mech to 0~359.9999deg -----*/ 00233 static float pos, round; 00234 pos = controller->theta_mech; 00235 modf(pos/(2*PI),&round); 00236 pos = pos - round*2*PI; 00237 if(abs(pos) <= 0.001) 00238 pos = abs(pos); 00239 else if(pos < 0) 00240 pos = pos + 2*PI; 00241 /*-----------------------------------------------*/ 00242 00243 /*----- position PID control -----*/ 00244 static float in_err = 0, err = 0; //integral of position error 00245 if(controller->p_des < pos) 00246 if((controller->p_des + 2*PI - pos) < (pos - controller->p_des)) 00247 err = 2*PI - pos + controller->p_des; 00248 else 00249 err = controller->p_des - pos; 00250 else 00251 if((pos + 2*PI - controller->p_des) < (controller->p_des - pos)) 00252 err = controller->p_des - 2*PI - pos; 00253 else 00254 err = controller->p_des - pos; 00255 00256 in_err = in_err + err; 00257 /* 00258 err = controller->p_des - pos; 00259 in_err = in_err + err; 00260 */ 00261 float torque_ref = controller->kp*(err) + controller->t_ff + controller->ki*(in_err) + controller->kd*(-controller->dtheta_mech); 00262 /*--------------------------------*/ 00263 00264 //float torque_ref = controller->kp*(controller->p_des - controller->theta_mech) + controller->t_ff + controller->kd*(controller->v_des - controller->dtheta_mech); 00265 //float torque_ref = -.1*(controller->p_des - controller->theta_mech); 00266 controller->i_q_ref = torque_ref/KT_OUT; 00267 controller->i_d_ref = 0.0f; 00268 } 00269
Generated on Thu Jul 21 2022 06:45:42 by
1.7.2