root/pmsm/simulator_zdenek/simulator.cpp @ 130

Revision 130, 9.6 kB (checked in by smidl, 16 years ago)

saturace na zadanem napeti v PWM + ukaldani "skutecnych" napeti do KalmanObs?

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