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. 25.4.2009 |
---|
10 | |
---|
11 | */ |
---|
12 | |
---|
13 | #define _USE_MATH_DEFINES |
---|
14 | |
---|
15 | #include <stdio.h> |
---|
16 | #include <math.h> |
---|
17 | #include <stdlib.h> //na linuxu je abs v stdlib |
---|
18 | #include "regulace.h" |
---|
19 | #include "simulator.h" |
---|
20 | |
---|
21 | |
---|
22 | #define REZIM_REGULACE 1 // 0...reg. momentu, 1...reg.rychlosti, 2... Isqw=sqrt(Imax^2-Id^2) - max. moment |
---|
23 | |
---|
24 | void pmsmsim_set_parameters(double Rs0, double Ls0, double Fmag0, double Bf0, double p0, double kp0, double J0, double Uc0, double DT0, double dt0); |
---|
25 | void pmsmsim_step(double Ww); |
---|
26 | |
---|
27 | // local functions |
---|
28 | static void pwm(unsigned int mod); |
---|
29 | static double ubytek(double I); |
---|
30 | static void pmsm_model(unsigned int mod); |
---|
31 | |
---|
32 | |
---|
33 | // simulator properties /////////////////////// |
---|
34 | static double Rs,Ls,Fmag,Bf,p,kp,J; // parameters of PMSM model |
---|
35 | static double Ucn,Uc,DT,U_modulace; // dc-link voltage and dead-time |
---|
36 | static double Urm_max; // field weakening |
---|
37 | static double h,h_reg,h_reg_real; // simulation step and sampling of employed loops |
---|
38 | unsigned int h_reg_counter,h_reg_counter_mez; // emulation of DSP operation |
---|
39 | |
---|
40 | 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 |
---|
41 | static unsigned int pocet=8; // velikost VA-charky |
---|
42 | |
---|
43 | // system state |
---|
44 | double x[9]; // (isx,isy,wme,theta_e,M,Fsd,Isd,Isq,Mz) |
---|
45 | |
---|
46 | // internal variables of PWM module |
---|
47 | static int smer, smer2, citac, citac2, citac_PR, modulace; |
---|
48 | |
---|
49 | // internal variables of PMSM model |
---|
50 | static double dIsx,dIsx1,dIsx2,dIsx3,dIsy,dIsy1,dIsy2,dIsy3; |
---|
51 | static double dTheta,dTheta1,dTheta2,dTheta3; |
---|
52 | static double dw,dw1,dw2,dw3; |
---|
53 | |
---|
54 | // system measures |
---|
55 | static double Isx, Isy, theta, speed; |
---|
56 | |
---|
57 | // control |
---|
58 | static double u[2]={0.,0.}; // format u={Um, beta} |
---|
59 | static double us[2]={0.,0.}; // format us={us_alfa, us_beta} |
---|
60 | |
---|
61 | // variables for calculation of mean values of stator voltage components |
---|
62 | static double usx_av=0., usy_av=0.,sum_usx_av=0.,sum_usy_av=0.; |
---|
63 | |
---|
64 | // variables for calculation of mean values of stator current components - (alfa, beta) |
---|
65 | static double isx_av=0., isy_av=0.,sum_isx_av=0.,sum_isy_av=0.; |
---|
66 | |
---|
67 | // stator voltage components filtering |
---|
68 | static double usxf=0.,usyf=0.,Tf=0.01; |
---|
69 | static unsigned int start_filter=1; |
---|
70 | |
---|
71 | // output for EKF (voltages and measured currents, which are fed to KalmanObs) |
---|
72 | double KalmanObs[10]={0.,0.,0.,0.,0.,0.,0.,0.,0.,0.}; // usx, usy, Isx, Isy, usx_av, usy_av |
---|
73 | |
---|
74 | // real-time |
---|
75 | double t=0.; //VS removed static due to clash with export in .h |
---|
76 | |
---|
77 | // stator voltage components in alfa beta (inluding impact of the real dc-link voltage) |
---|
78 | static double ualfa=0., ubeta=0.; |
---|
79 | |
---|
80 | // PWM - improvement of dead-time effect emulation |
---|
81 | static unsigned int start_pwm=1; |
---|
82 | static int uact[3]={1,1,1}; |
---|
83 | static int ureq[3]={1,1,1}; |
---|
84 | static unsigned int DT_counter[3]={0,0,0}; |
---|
85 | static unsigned int DT_counter_mez; |
---|
86 | |
---|
87 | // debug |
---|
88 | static double debug_pwm; |
---|
89 | FILE *fdebug; |
---|
90 | |
---|
91 | /////////////////// POMOCNE FUNKCE ////////////////////////////////// |
---|
92 | double uhel(double x, double y) |
---|
93 | { |
---|
94 | double th; |
---|
95 | |
---|
96 | if (x==0) |
---|
97 | if (y==0) th=0.; |
---|
98 | else if (y>0) th=M_PI/2.; |
---|
99 | else th=-M_PI/2.; |
---|
100 | else |
---|
101 | th=atan(y/x); |
---|
102 | |
---|
103 | if (x<0) th+=M_PI; |
---|
104 | |
---|
105 | return th; |
---|
106 | } |
---|
107 | ///////////////////////////////////////////////////////////////////// |
---|
108 | |
---|
109 | |
---|
110 | void pmsmsim_set_parameters(double Rs0, double Ls0, double Fmag0, double Bf0, double p0, double kp0, double J0, double Uc0, double DT0, double dt0) |
---|
111 | { |
---|
112 | int tmp_i; |
---|
113 | |
---|
114 | // simulator parameters setup |
---|
115 | Rs=Rs0; |
---|
116 | Ls=Ls0; |
---|
117 | Fmag=Fmag0; |
---|
118 | Bf=Bf0; |
---|
119 | p=p0; |
---|
120 | kp=kp0; |
---|
121 | J=J0; |
---|
122 | Ucn=600.; |
---|
123 | Uc=Uc0; |
---|
124 | DT=DT0; |
---|
125 | |
---|
126 | // control setup |
---|
127 | Urm_max=0.95; |
---|
128 | |
---|
129 | // simulator sampling - fixed setup |
---|
130 | h=dt0; |
---|
131 | h_reg=125e-6; // fpwm = 4kHz |
---|
132 | h_reg_counter_mez=(int)(h_reg/h); // emulation of operation of DSP timer |
---|
133 | //h_reg_counter=h_reg_counter_mez; |
---|
134 | h_reg_counter=1; |
---|
135 | h_reg_real=h_reg_counter_mez*h; // real sampling period |
---|
136 | |
---|
137 | // reset of the system state variables |
---|
138 | for (tmp_i=0;tmp_i<9;tmp_i++) |
---|
139 | x[tmp_i]=0.; |
---|
140 | |
---|
141 | // emulation of the first measure |
---|
142 | Isx=0.;Isy=0.;theta=x[3];speed=x[2]; |
---|
143 | |
---|
144 | // === init of particular modules of simulator === |
---|
145 | // PWM init |
---|
146 | smer=-1; smer2=-1; |
---|
147 | citac=0; |
---|
148 | citac2=abs(0-(int)(DT/h)); //VS: oprava, je to spravne? |
---|
149 | citac_PR=h_reg_counter_mez; |
---|
150 | DT_counter_mez = (int)(DT/h); |
---|
151 | |
---|
152 | // first interrupt occur after first period match => add 1 to both counter registers |
---|
153 | citac++;smer=1; |
---|
154 | citac2--; |
---|
155 | |
---|
156 | modulace=0; // THIPWM |
---|
157 | if (modulace==1) |
---|
158 | U_modulace=Ucn/sqrt(3.); |
---|
159 | else |
---|
160 | U_modulace=Ucn/2.; |
---|
161 | |
---|
162 | // PMSM model init |
---|
163 | dIsx=0;dIsx1=0;dIsx2=0;dIsx3=0;dIsy=0;dIsy1=0;dIsy2=0;dIsy3=0; |
---|
164 | dTheta=0;dTheta1=0;dTheta2=0;dTheta3=0; |
---|
165 | dw=0;dw1=0;dw2=0;dw3=0; |
---|
166 | |
---|
167 | init_regulace(Ls,Fmag,kp,p,h_reg_real); |
---|
168 | |
---|
169 | // !!d!! |
---|
170 | // fdebug=fopen("data_debug.txt","w"); |
---|
171 | } |
---|
172 | |
---|
173 | |
---|
174 | static void pwm(unsigned int mod) |
---|
175 | // mod ... mod=0 - sinusoidal PWM; mod=1 - PWM with injected 3rd harmonic |
---|
176 | { |
---|
177 | unsigned int i; |
---|
178 | double iabc[3], ur[3],ustr[3],ua,ub,uc; |
---|
179 | double dtr[3],dd[3]; |
---|
180 | double Um, beta; |
---|
181 | double U3; |
---|
182 | double up, up2; |
---|
183 | |
---|
184 | Um=*u; |
---|
185 | beta=*(u+1); |
---|
186 | |
---|
187 | // emulation of carrier - timer |
---|
188 | up=((double)citac/citac_PR-0.5)*Ucn; |
---|
189 | |
---|
190 | iabc[0]=*x; |
---|
191 | iabc[1]=(-*x+sqrt(3.)**(x+1))/2.; |
---|
192 | iabc[2]=(-*x-sqrt(3.)**(x+1))/2.; |
---|
193 | |
---|
194 | if (mod==0) // sin. PWM |
---|
195 | { |
---|
196 | ur[0]=Um*cos(beta); |
---|
197 | ur[1]=Um*cos(beta-2./3.*M_PI); |
---|
198 | ur[2]=Um*cos(beta+2./3.*M_PI); |
---|
199 | } |
---|
200 | else // PWM with injected 3rd harmonic |
---|
201 | { |
---|
202 | U3=0.17*cos(3.*beta); |
---|
203 | ur[0]=Um*(cos(beta)-U3); |
---|
204 | ur[1]=Um*(cos(beta-2./3.*M_PI)-U3); |
---|
205 | ur[2]=Um*(cos(beta+2./3.*M_PI)-U3); |
---|
206 | } |
---|
207 | |
---|
208 | for (i=0;i<3;i++) |
---|
209 | { dtr[i]=ubytek(fabs(iabc[i])); |
---|
210 | dd[i]=dtr[i]*.73; |
---|
211 | } |
---|
212 | |
---|
213 | // mitigation of the voltage drops |
---|
214 | /* for (i=0;i<3;i++) |
---|
215 | { |
---|
216 | dtr[i]=0.; |
---|
217 | dd[i]=0.; |
---|
218 | } |
---|
219 | /**/ |
---|
220 | |
---|
221 | // PWM compare emulation |
---|
222 | for (i=0;i<3;i++) |
---|
223 | if (ur[i]>up) |
---|
224 | ureq[i]=1; |
---|
225 | else |
---|
226 | ureq[i]=-1; |
---|
227 | |
---|
228 | // First entrace into PWM - actual switching combination is initiated with required switching state |
---|
229 | if (start_pwm == 1) |
---|
230 | { |
---|
231 | for (i=0;i<3;i++) |
---|
232 | uact[i]=ureq[i]; |
---|
233 | start_pwm=0; |
---|
234 | } |
---|
235 | |
---|
236 | // Dead-time effect has no impact on switching combination, when switching from transistor to diode |
---|
237 | for (i=0;i<3;i++) |
---|
238 | { |
---|
239 | if ((iabc[i]>0.5) && (uact[i]==1)) |
---|
240 | uact[i]=ureq[i]; |
---|
241 | |
---|
242 | if ((iabc[i]<-0.5) && (uact[i]==-1)) |
---|
243 | uact[i]=ureq[i]; |
---|
244 | } |
---|
245 | /**/ |
---|
246 | |
---|
247 | /* for (i=0;i<3;i++) |
---|
248 | uact[i]=ureq[i];*/ |
---|
249 | |
---|
250 | // Dead-time effect emulation |
---|
251 | for (i=0;i<3;i++) |
---|
252 | if ((uact[i]!=ureq[i]) && (DT_counter[i]<DT_counter_mez)) |
---|
253 | DT_counter[i]++; |
---|
254 | else |
---|
255 | { |
---|
256 | uact[i]=ureq[i]; |
---|
257 | DT_counter[i]=0; |
---|
258 | } |
---|
259 | |
---|
260 | // inverter phase voltage computation |
---|
261 | for (i=0;i<3;i++) |
---|
262 | ustr[i]=uact[i]*Uc/2.; |
---|
263 | |
---|
264 | // implementation of voltage drops |
---|
265 | for (i=0;i<3;i++) |
---|
266 | if (iabc[i]>0) |
---|
267 | if (uact[i]==1) |
---|
268 | ustr[i]-=dtr[i]; |
---|
269 | else |
---|
270 | ustr[i]-=dd[i]; |
---|
271 | else |
---|
272 | if (uact[i]==-1) |
---|
273 | ustr[i]+=dtr[i]; |
---|
274 | else |
---|
275 | ustr[i]+=dd[i]; |
---|
276 | /**/ |
---|
277 | // phase voltages |
---|
278 | ua=(2.*ustr[0]-ustr[1]-ustr[2])/3.; |
---|
279 | ub=(2.*ustr[1]-ustr[0]-ustr[2])/3.; |
---|
280 | uc=(2.*ustr[2]-ustr[0]-ustr[1])/3.; |
---|
281 | |
---|
282 | // voltage vector in stationary reference frame (x,y) |
---|
283 | *us=(2.*ua-ub-uc)/3.; |
---|
284 | *(us+1)=(ub-uc)/sqrt(3.); |
---|
285 | |
---|
286 | // sinusoidal inverter!!!! |
---|
287 | // *us=ur[0]; |
---|
288 | // *(us+1)=(ur[1]-ur[2])/sqrt(3.); |
---|
289 | |
---|
290 | // emulation of DSP timers |
---|
291 | if ((citac==citac_PR)||(citac==0)) |
---|
292 | { |
---|
293 | smer*=-1; |
---|
294 | // calculation of stator voltage components mean values |
---|
295 | usx_av=h/h_reg*sum_usx_av; |
---|
296 | usy_av=h/h_reg*sum_usy_av; |
---|
297 | // reset of sum accumulators |
---|
298 | sum_usx_av=0.; |
---|
299 | sum_usy_av=0.; |
---|
300 | |
---|
301 | // stator current components mean values - reference frame (alfa, beta) |
---|
302 | isx_av=h/h_reg*sum_isx_av; |
---|
303 | isy_av=h/h_reg*sum_isy_av; |
---|
304 | // reset of sum accumulators |
---|
305 | sum_isx_av=0.; |
---|
306 | sum_isy_av=0.; |
---|
307 | } |
---|
308 | if ((citac2==citac_PR)||(citac2==0)) smer2*=-1; |
---|
309 | citac+=smer; |
---|
310 | citac2+=smer2; |
---|
311 | |
---|
312 | // calculation of stator voltage components mean values - sum |
---|
313 | sum_usx_av+=*us; |
---|
314 | sum_usy_av+=*(us+1); |
---|
315 | |
---|
316 | // stator voltage components filtering |
---|
317 | //if (start_filter==1) |
---|
318 | usxf+=(*us-usxf)*h/h_reg; |
---|
319 | usyf+=(*(us+1)-usyf)*h/h_reg; |
---|
320 | |
---|
321 | // stator current components mean values - reference frame (alfa, beta) |
---|
322 | sum_isx_av+=*x; |
---|
323 | sum_isy_av+=*(x+1); |
---|
324 | |
---|
325 | debug_pwm = ur[0]; // !!!! |
---|
326 | // !!d!! |
---|
327 | // fprintf(fdebug,"%f %f %f \n",ustr[0],*us,x[0]); |
---|
328 | } |
---|
329 | |
---|
330 | static double ubytek(double I) |
---|
331 | { |
---|
332 | unsigned int ii; |
---|
333 | double delta_u; |
---|
334 | |
---|
335 | ii=0; |
---|
336 | while ((*(va_char+ii)<I) && (ii<(pocet-1))) |
---|
337 | ii++; |
---|
338 | |
---|
339 | if (ii==(pocet-1)) |
---|
340 | delta_u=*(va_char+ii+pocet); |
---|
341 | else |
---|
342 | if (ii==0) |
---|
343 | delta_u=0; |
---|
344 | else |
---|
345 | 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)); |
---|
346 | |
---|
347 | return delta_u; |
---|
348 | } |
---|
349 | |
---|
350 | |
---|
351 | static void pmsm_model(unsigned int mod) |
---|
352 | // mod<5...Euler, mod>4 ... Adams of 4th order |
---|
353 | { |
---|
354 | double usx, usy; |
---|
355 | |
---|
356 | usx=*us; |
---|
357 | usy=*(us+1); |
---|
358 | |
---|
359 | dIsx=-Rs/Ls*x[0]+Fmag/Ls*x[2]*sin(x[3])+usx/Ls; |
---|
360 | dIsy=-Rs/Ls*x[1]-Fmag/Ls*x[2]*cos(x[3])+usy/Ls; |
---|
361 | dTheta=x[2]; |
---|
362 | |
---|
363 | if (J>0) |
---|
364 | dw=kp*p*p*Fmag/J*(x[1]*cos(x[3])-x[0]*sin(x[3]))-Bf/J*x[2]-p/J*x[8]; |
---|
365 | else |
---|
366 | dw=0; |
---|
367 | |
---|
368 | // integration |
---|
369 | if (mod<5) // Euler |
---|
370 | { x[0]+=dIsx*h; |
---|
371 | x[1]+=dIsy*h; |
---|
372 | x[2]+=dw*h; |
---|
373 | x[3]+=dTheta*h; |
---|
374 | } |
---|
375 | else // Adams (4th order) |
---|
376 | { x[0]+=h/24.*(55.*dIsx-59.*dIsx1+37.*dIsx2-9.*dIsx3); |
---|
377 | x[1]+=h/24.*(55.*dIsy-59.*dIsy1+37.*dIsy2-9.*dIsy3); |
---|
378 | x[2]+=h/24.*(55.*dw-59.*dw1+37.*dw2-9.*dw3); |
---|
379 | x[3]+=h/24.*(55.*dTheta-59.*dTheta1+37.*dTheta2-9.*dTheta3); |
---|
380 | } |
---|
381 | |
---|
382 | // saturation of theta to (-pi,pi) |
---|
383 | if (x[3]>M_PI) x[3]-=(2*M_PI); |
---|
384 | if (x[3]<-M_PI) x[3]+=(2*M_PI); |
---|
385 | |
---|
386 | // diff. shift - Adams |
---|
387 | dIsx3=dIsx2;dIsx2=dIsx1;dIsx1=dIsx; |
---|
388 | dIsy3=dIsy2;dIsy2=dIsy1;dIsy1=dIsy; |
---|
389 | dTheta3=dTheta2;dTheta2=dTheta1;dTheta1=dTheta; |
---|
390 | dw3=dw2;dw2=dw1;dw1=dw; |
---|
391 | |
---|
392 | // calculation of Isd, Isq |
---|
393 | x[6]=x[0]*cos(x[3])+x[1]*sin(x[3]); // Isd |
---|
394 | x[7]=x[1]*cos(x[3])-x[0]*sin(x[3]); // Isq |
---|
395 | |
---|
396 | // Fsd ... d-component of stator flux |
---|
397 | x[5]=Ls*x[6]+Fmag; |
---|
398 | |
---|
399 | // Torque |
---|
400 | x[4]=kp*p*Fmag*(x[1]*cos(x[3])-x[0]*sin(x[3])); |
---|
401 | } |
---|
402 | |
---|
403 | ////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
404 | void pmsmsim_step(double Ww, double Mz) // you must link array KalmanObs[] to EKF modul |
---|
405 | { |
---|
406 | double Umk, ub, uc; |
---|
407 | |
---|
408 | // while (t<=t_end) |
---|
409 | { |
---|
410 | pwm(modulace); |
---|
411 | // *us=KalmanObs[0]; *(us+1)=KalmanObs[1]; |
---|
412 | // *us=ualfa; *(us+1)=ubeta; |
---|
413 | //Mz |
---|
414 | x[8]= Mz; |
---|
415 | |
---|
416 | pmsm_model(5); |
---|
417 | |
---|
418 | if (h_reg_counter>=h_reg_counter_mez) // pocatek ISR |
---|
419 | { |
---|
420 | // voltages and measured currents for EKF |
---|
421 | // Umk=*u*Uc/Ucn; |
---|
422 | // ualfa=Umk*cos(*(u+1)); |
---|
423 | // ub=Umk*cos(*(u+1)-2./3.*M_PI); |
---|
424 | KalmanObs[0]=ualfa; //debug_pwm; // usx |
---|
425 | //KalmanObs[1]=(ualfa+2.*ub)/sqrt(3.); // usy |
---|
426 | KalmanObs[1]=ubeta; // usy |
---|
427 | |
---|
428 | // real sampling - considered transport delay equal to the sampling period |
---|
429 | /* KalmanObs[2]=Isx; |
---|
430 | KalmanObs[3]=Isy;*/ |
---|
431 | // ideal sampling |
---|
432 | KalmanObs[2]=x[0]; |
---|
433 | KalmanObs[3]=x[1]; |
---|
434 | |
---|
435 | // diagnostic - mean values of stator voltage components - pwm() |
---|
436 | KalmanObs[4]=usx_av; |
---|
437 | KalmanObs[5]=usy_av; |
---|
438 | KalmanObs[6]=usxf; |
---|
439 | KalmanObs[7]=usyf; |
---|
440 | KalmanObs[8]=isx_av; |
---|
441 | KalmanObs[9]=isy_av; |
---|
442 | |
---|
443 | 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 |
---|
444 | // rezim=2 ... Iqw=sqrt(Imax^2-Idw^2) |
---|
445 | |
---|
446 | /* *u=2*15.0; |
---|
447 | *(u+1)+=2*0.00157;*/ |
---|
448 | // emulation of the real sampling of A/D converter |
---|
449 | Isx=x[0];Isy=x[1];speed=x[2];theta=x[3]; |
---|
450 | |
---|
451 | // include ideal commanded stator voltage |
---|
452 | Umk=*u*Uc/Ucn;// !!!! |
---|
453 | ualfa=Umk*cos(*(u+1)); // usx = usa |
---|
454 | ub=Umk*cos(*(u+1)-2./3.*M_PI); |
---|
455 | ubeta=(ualfa+2.*ub)/sqrt(3.); // usy |
---|
456 | // uc=-ualfa-ub; |
---|
457 | // ubeta=(ub-uc)/sqrt(3.); |
---|
458 | |
---|
459 | h_reg_counter=0; |
---|
460 | } |
---|
461 | |
---|
462 | t+=h; |
---|
463 | h_reg_counter++; |
---|
464 | } |
---|
465 | } |
---|
466 | |
---|
467 | void pmsmsim_noreg_step(double ua, double ub) // you must link array KalmanObs[] to EKF modul |
---|
468 | { |
---|
469 | double kor_Uc; |
---|
470 | |
---|
471 | *u=sqrt(ua*ua+ub*ub); |
---|
472 | *(u+1)=uhel(ua,ub); |
---|
473 | |
---|
474 | /* *u=5.0; |
---|
475 | *(u+1)+=0.00157;*/ |
---|
476 | // uprava velikosti vzhledem k Uc!=Ucn |
---|
477 | kor_Uc=Ucn/230.; |
---|
478 | *u*=kor_Uc; /**/ |
---|
479 | |
---|
480 | pwm(modulace); |
---|
481 | |
---|
482 | // *us=*u*cos(*(u+1)); |
---|
483 | // *(us+1)=*u*sin(*(u+1));; |
---|
484 | |
---|
485 | pmsm_model(5); |
---|
486 | } |
---|
487 | ////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
488 | ////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|