1 | /* |
---|
2 | Simulator of Vector Controlled PMSM Drive |
---|
3 | |
---|
4 | This module is background for PMSM drive object design and |
---|
5 | introduces basic functions ... set_parameters() and eval(). |
---|
6 | |
---|
7 | Z. Peroutka |
---|
8 | |
---|
9 | Rev. 16.3.2008 |
---|
10 | |
---|
11 | */ |
---|
12 | |
---|
13 | #include <math.h> |
---|
14 | #include "regulace.h" |
---|
15 | #include "simulator.h" |
---|
16 | |
---|
17 | #define REZIM_REGULACE 1 // 0...reg. momentu, 1...reg.rychlosti, 2... Isqw=sqrt(Imax^2-Id^2) - max. moment |
---|
18 | |
---|
19 | void set_parameters(double Rs0, double Ls0, double Fmag0, double Bf0, double p0, double kp0, double J0, double Uc0, double DT0, double dt0); |
---|
20 | void eval(double Ww); |
---|
21 | |
---|
22 | // local functions |
---|
23 | static void pwm(unsigned int mod); |
---|
24 | static double ubytek(double I); |
---|
25 | static void pmsm_model(unsigned int mod); |
---|
26 | |
---|
27 | |
---|
28 | // simulator properties /////////////////////// |
---|
29 | static double Rs,Ls,Fmag,Bf,p,kp,J; // parameters of PMSM model |
---|
30 | static double Ucn,Uc,DT,U_modulace; // dc-link voltage and dead-time |
---|
31 | static double Urm_max; // field weakening |
---|
32 | static double h,h_reg,h_reg_real; // simulation step and sampling of employed loops |
---|
33 | unsigned int h_reg_counter,h_reg_counter_mez; // emulation of DSP operation |
---|
34 | |
---|
35 | static double va_char[16]={0,10,50,100,200,300,500,1000, 0,1,1.8,2.4,3.2,3.8,4.8,6.8}; // ubytky |
---|
36 | static unsigned int pocet=8; // velikost VA-charky |
---|
37 | |
---|
38 | // system state |
---|
39 | double x[9]; // (isx,isy,wme,theta_e,M,Fsd,Isd,Isq,Mz) |
---|
40 | |
---|
41 | // internal variables of PWM module |
---|
42 | static int smer, smer2, citac, citac2, citac_PR, modulace; |
---|
43 | |
---|
44 | // internal variables of PMSM model |
---|
45 | static double dIsx,dIsx1,dIsx2,dIsx3,dIsy,dIsy1,dIsy2,dIsy3; |
---|
46 | static double dTheta,dTheta1,dTheta2,dTheta3; |
---|
47 | static double dw,dw1,dw2,dw3; |
---|
48 | |
---|
49 | // system measures |
---|
50 | static double Isx, Isy, theta, speed; |
---|
51 | |
---|
52 | // control |
---|
53 | static double u[2]={0.,0.}; // format u={Um, beta} |
---|
54 | static double us[2]={0.,0.}; // format us={us_alfa, us_beta} |
---|
55 | |
---|
56 | // output for EKF (voltages and measured currents, which are fed to KalmanObs) |
---|
57 | static double KalmanObs[4]={0.,0.,0.,0.}; // usx, usy, Isx, Isy |
---|
58 | |
---|
59 | // real-time |
---|
60 | static double t=0.; |
---|
61 | |
---|
62 | void set_parameters(double Rs0, double Ls0, double Fmag0, double Bf0, double p0, double kp0, double J0, double Uc0, double DT0, double dt0) |
---|
63 | { |
---|
64 | int tmp_i; |
---|
65 | |
---|
66 | // simulator parameters setup |
---|
67 | Rs=Rs0; |
---|
68 | Ls=Ls0; |
---|
69 | Fmag=Fmag0; |
---|
70 | Bf=Bf0; |
---|
71 | p=p0; |
---|
72 | kp=kp0; |
---|
73 | J=J0; |
---|
74 | Ucn=600.; |
---|
75 | Uc=Uc0; |
---|
76 | DT=DT0; |
---|
77 | |
---|
78 | // control setup |
---|
79 | Urm_max=0.95; |
---|
80 | |
---|
81 | // simulator sampling - fixed setup |
---|
82 | h=dt0; |
---|
83 | h_reg=125e-6; // fpwm = 4kHz |
---|
84 | h_reg_counter_mez=(int)(h_reg/h); // emulation of operation of DSP timer |
---|
85 | h_reg_counter=h_reg_counter_mez; |
---|
86 | h_reg_real=h_reg_counter_mez*h; // real sampling period |
---|
87 | |
---|
88 | // reset of the system state variables |
---|
89 | for (tmp_i=0;tmp_i<9;tmp_i++) |
---|
90 | x[tmp_i]=0.; |
---|
91 | |
---|
92 | // emulation of the first measure |
---|
93 | Isx=0.;Isy=0.;theta=x[3];speed=x[2]; |
---|
94 | |
---|
95 | // === init of particular modules of simulator === |
---|
96 | // PWM init |
---|
97 | smer=-1; smer2=-1; |
---|
98 | citac=0; |
---|
99 | citac2=abs(0-(DT/h)); |
---|
100 | citac_PR=h_reg_counter_mez; |
---|
101 | |
---|
102 | modulace=1; // THIPWM |
---|
103 | if (modulace==1) |
---|
104 | U_modulace=Ucn/sqrt(3.); |
---|
105 | else |
---|
106 | U_modulace=Ucn/2.; |
---|
107 | |
---|
108 | // PMSM model init |
---|
109 | dIsx=0;dIsx1=0;dIsx2=0;dIsx3=0;dIsy=0;dIsy1=0;dIsy2=0;dIsy3=0; |
---|
110 | dTheta=0;dTheta1=0;dTheta2=0;dTheta3=0; |
---|
111 | dw=0;dw1=0;dw2=0;dw3=0; |
---|
112 | |
---|
113 | init_regulace(Ls,Fmag,kp,p,h_reg_real); |
---|
114 | } |
---|
115 | |
---|
116 | |
---|
117 | static void pwm(unsigned int mod) |
---|
118 | // mod ... mod=0 - sinusoidal PWM; mod=1 - PWM with injected 3rd harmonic |
---|
119 | { |
---|
120 | unsigned int i; |
---|
121 | double iabc[3], ur[3],ustr[3],ua,ub,uc; |
---|
122 | double dtr[3],dd[3]; |
---|
123 | double Um, beta; |
---|
124 | double U3; |
---|
125 | double up, up2; |
---|
126 | |
---|
127 | Um=*u; |
---|
128 | beta=*(u+1); |
---|
129 | |
---|
130 | // emulation of carrier - timer |
---|
131 | up=((double)citac/citac_PR-0.5)*Ucn; |
---|
132 | up2=((double)citac2/citac_PR-0.5)*Ucn; |
---|
133 | |
---|
134 | iabc[0]=*x; |
---|
135 | iabc[1]=(-*x+sqrt(3.)**(x+1))/2.; |
---|
136 | iabc[2]=(-*x-sqrt(3.)**(x+1))/2.; |
---|
137 | |
---|
138 | if (mod==0) // sin. PWM |
---|
139 | { |
---|
140 | ur[0]=Um*cos(beta); |
---|
141 | ur[1]=Um*cos(beta-2./3.*M_PI); |
---|
142 | ur[2]=Um*cos(beta+2./3.*M_PI); |
---|
143 | } |
---|
144 | else // PWM with injected 3rd harmonic |
---|
145 | { |
---|
146 | U3=0.17*cos(3.*beta); |
---|
147 | ur[0]=Um*(cos(beta)-U3); |
---|
148 | ur[1]=Um*(cos(beta-2./3.*M_PI)-U3); |
---|
149 | ur[2]=Um*(cos(beta+2./3.*M_PI)-U3); |
---|
150 | } |
---|
151 | |
---|
152 | for (i=0;i<3;i++) |
---|
153 | { dtr[i]=ubytek(fabs(iabc[i])); |
---|
154 | dd[i]=dtr[i]*.73; |
---|
155 | } |
---|
156 | |
---|
157 | // implementation of voltage drops and dead-times |
---|
158 | for (i=0;i<3;i++) |
---|
159 | if (iabc[i]>=0) |
---|
160 | if ((ur[i]>up) && (ur[i]>up2)) |
---|
161 | ustr[i]=Uc/2-dtr[i]; |
---|
162 | else |
---|
163 | ustr[i]=-(Uc/2+dd[i]); |
---|
164 | else |
---|
165 | if ((ur[i]<up) && (ur[i]<up2)) |
---|
166 | ustr[i]=-(Uc/2-dtr[i]); |
---|
167 | else |
---|
168 | ustr[i]=Uc/2+dd[i]; |
---|
169 | |
---|
170 | // phase voltages |
---|
171 | ua=(2.*ustr[0]-ustr[1]-ustr[2])/3.; |
---|
172 | ub=(2.*ustr[1]-ustr[0]-ustr[2])/3.; |
---|
173 | uc=(2.*ustr[2]-ustr[0]-ustr[1])/3.; |
---|
174 | |
---|
175 | // voltage vector in stationary reference frame (x,y) |
---|
176 | *us=(2.*ua-ub-uc)/3.; |
---|
177 | *(us+1)=(ub-uc)/sqrt(3.); |
---|
178 | |
---|
179 | // emulation of DSP timers |
---|
180 | if ((citac==citac_PR)||(citac==0)) smer*=-1; |
---|
181 | if ((citac2==citac_PR)||(citac2==0)) smer2*=-1; |
---|
182 | citac+=smer; |
---|
183 | citac2+=smer2; |
---|
184 | } |
---|
185 | |
---|
186 | static double ubytek(double I) |
---|
187 | { |
---|
188 | unsigned int ii; |
---|
189 | double delta_u; |
---|
190 | |
---|
191 | ii=0; |
---|
192 | while ((*(va_char+ii)<I) && (ii<(pocet-1))) |
---|
193 | ii++; |
---|
194 | |
---|
195 | if (ii==(pocet-1)) |
---|
196 | delta_u=*(va_char+ii+pocet); |
---|
197 | else |
---|
198 | if (ii==0) |
---|
199 | delta_u=0; |
---|
200 | else |
---|
201 | delta_u=*(va_char+ii-1+pocet)+(I-*(va_char+ii-1))/(*(va_char+ii)-*(va_char+ii-1))*(*(va_char+ii+pocet)-*(va_char+ii-1+pocet)); |
---|
202 | |
---|
203 | return delta_u; |
---|
204 | } |
---|
205 | |
---|
206 | |
---|
207 | static void pmsm_model(unsigned int mod) |
---|
208 | // mod<5...Euler, mod>4 ... Adams of 4th order |
---|
209 | { |
---|
210 | double usx, usy; |
---|
211 | |
---|
212 | usx=*us; |
---|
213 | usy=*(us+1); |
---|
214 | |
---|
215 | dIsx=-Rs/Ls*x[0]+Fmag/Ls*x[2]*sin(x[3])+usx/Ls; |
---|
216 | dIsy=-Rs/Ls*x[1]-Fmag/Ls*x[2]*cos(x[3])+usy/Ls; |
---|
217 | dTheta=x[2]; |
---|
218 | |
---|
219 | if (J>0) |
---|
220 | dw=kp*p*p*Fmag/J*(x[1]*cos(x[3])-x[0]*sin(x[3]))-Bf/J*x[2]-p/J*x[8]; |
---|
221 | else |
---|
222 | dw=0; |
---|
223 | |
---|
224 | // integration |
---|
225 | if (mod<5) // Euler |
---|
226 | { x[0]+=dIsx*h; |
---|
227 | x[1]+=dIsy*h; |
---|
228 | x[2]+=dw*h; |
---|
229 | x[3]+=dTheta*h; |
---|
230 | } |
---|
231 | else // Adams (4th order) |
---|
232 | { x[0]+=h/24.*(55.*dIsx-59.*dIsx1+37.*dIsx2-9.*dIsx3); |
---|
233 | x[1]+=h/24.*(55.*dIsy-59.*dIsy1+37.*dIsy2-9.*dIsy3); |
---|
234 | x[2]+=h/24.*(55.*dw-59.*dw1+37.*dw2-9.*dw3); |
---|
235 | x[3]+=h/24.*(55.*dTheta-59.*dTheta1+37.*dTheta2-9.*dTheta3); |
---|
236 | } |
---|
237 | |
---|
238 | // saturation of theta to (-pi,pi) |
---|
239 | if (x[3]>M_PI) x[3]-=(2*M_PI); |
---|
240 | if (x[3]<-M_PI) x[3]+=(2*M_PI); |
---|
241 | |
---|
242 | // diff. shift - Adams |
---|
243 | dIsx3=dIsx2;dIsx2=dIsx1;dIsx1=dIsx; |
---|
244 | dIsy3=dIsy2;dIsy2=dIsy1;dIsy1=dIsy; |
---|
245 | dTheta3=dTheta2;dTheta2=dTheta1;dTheta1=dTheta; |
---|
246 | dw3=dw2;dw2=dw1;dw1=dw; |
---|
247 | |
---|
248 | // calculation of Isd, Isq |
---|
249 | x[6]=x[0]*cos(x[3])+x[1]*sin(x[3]); // Isd |
---|
250 | x[7]=x[1]*cos(x[3])-x[0]*sin(x[3]); // Isq |
---|
251 | |
---|
252 | // Fsd ... d-component of stator flux |
---|
253 | x[5]=Ls*x[6]+Fmag; |
---|
254 | |
---|
255 | // Torque |
---|
256 | x[4]=kp*p*Fmag*(x[1]*cos(x[3])-x[0]*sin(x[3])); |
---|
257 | } |
---|
258 | |
---|
259 | ////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
260 | void eval(double Ww) // you must link array KalmanObs[] to EKF modul |
---|
261 | { |
---|
262 | double Umk, ua, ub; |
---|
263 | |
---|
264 | // while (t<=t_end) |
---|
265 | { |
---|
266 | pwm(modulace); |
---|
267 | pmsm_model(5); |
---|
268 | |
---|
269 | if (h_reg_counter>=h_reg_counter_mez) // pocatek ISR |
---|
270 | { |
---|
271 | // voltages and measured currents for EKF |
---|
272 | Umk=*u*Uc/Ucn; |
---|
273 | ua=Umk*cos(*(u+1)); |
---|
274 | ub=Umk*cos(*(u+1)-2./3.*M_PI); |
---|
275 | KalmanObs[0]=ua; // usx |
---|
276 | KalmanObs[1]=(ua+2.*ub)/sqrt(3.); // usy |
---|
277 | KalmanObs[2]=Isx; |
---|
278 | KalmanObs[3]=Isy; |
---|
279 | |
---|
280 | vektor_regulace(0,0,Urm_max,Ww,u,Isx,Isy,theta,speed,U_modulace,Uc,Ucn,REZIM_REGULACE); // rezim=1 ... reg. rychlosti, rezim=0 ... reg. momentu |
---|
281 | // rezim=2 ... Iqw=sqrt(Imax^2-Idw^2) |
---|
282 | // emulation of the real sampling of A/D converter |
---|
283 | Isx=x[0];Isy=x[1];speed=x[2];theta=x[3]; |
---|
284 | |
---|
285 | h_reg_counter=0; |
---|
286 | } |
---|
287 | |
---|
288 | t+=h; |
---|
289 | h_reg_counter++; |
---|
290 | } |
---|
291 | } |
---|
292 | ////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|