root/pmsm/simulator_zdenek/simulator.cpp @ 129

Revision 108, 7.9 kB (checked in by smidl, 16 years ago)

Fixes for compilation on Win32 platform

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// output for EKF (voltages and measured currents, which are fed to KalmanObs)
61double KalmanObs[4]={0.,0.,0.,0.};      // usx, usy, Isx, Isy
62
63// real-time
64double t=0.; //VS removed static due to clash with export in .h
65
66void pmsmsim_set_parameters(double Rs0, double Ls0, double Fmag0, double Bf0, double p0, double kp0, double J0, double Uc0, double DT0, double dt0)
67{
68  int tmp_i;
69
70  // simulator parameters setup
71  Rs=Rs0;
72  Ls=Ls0;
73  Fmag=Fmag0;
74  Bf=Bf0;
75  p=p0;
76  kp=kp0;
77  J=J0;
78  Ucn=600.;
79  Uc=Uc0;
80  DT=DT0;
81
82  // control setup
83  Urm_max=0.95;
84
85  // simulator sampling - fixed setup
86  h=dt0;
87  h_reg=125e-6;         // fpwm = 4kHz
88  h_reg_counter_mez=(int)(h_reg/h);         // emulation of operation of DSP timer
89  h_reg_counter=h_reg_counter_mez;
90  h_reg_real=h_reg_counter_mez*h;           // real sampling period
91
92  // reset of the system state variables
93  for (tmp_i=0;tmp_i<9;tmp_i++)
94    x[tmp_i]=0.;
95
96  // emulation of the first measure
97  Isx=0.;Isy=0.;theta=x[3];speed=x[2];
98
99// === init of particular modules of simulator ===
100  // PWM init
101  smer=-1; smer2=-1;
102  citac=0;
103  citac2=abs(0-(int)(DT/h)); //VS: oprava, je to spravne?
104  citac_PR=h_reg_counter_mez;
105
106  modulace=1;           // THIPWM
107  if (modulace==1)
108    U_modulace=Ucn/sqrt(3.);
109  else
110    U_modulace=Ucn/2.;
111
112  // PMSM model init
113  dIsx=0;dIsx1=0;dIsx2=0;dIsx3=0;dIsy=0;dIsy1=0;dIsy2=0;dIsy3=0;
114  dTheta=0;dTheta1=0;dTheta2=0;dTheta3=0;
115  dw=0;dw1=0;dw2=0;dw3=0;
116
117  init_regulace(Ls,Fmag,kp,p,h_reg_real);
118}
119
120
121static void pwm(unsigned int mod)
122// mod ... mod=0 - sinusoidal PWM; mod=1 - PWM with injected 3rd harmonic
123{
124  unsigned int i;
125  double iabc[3], ur[3],ustr[3],ua,ub,uc;
126  double dtr[3],dd[3];
127  double Um, beta;
128  double U3;
129  double up, up2;
130
131  Um=*u;
132  beta=*(u+1);
133
134  // emulation of carrier - timer
135  up=((double)citac/citac_PR-0.5)*Ucn;
136  up2=((double)citac2/citac_PR-0.5)*Ucn;
137
138  iabc[0]=*x;
139  iabc[1]=(-*x+sqrt(3.)**(x+1))/2.;
140  iabc[2]=(-*x-sqrt(3.)**(x+1))/2.;
141
142  if (mod==0)   // sin. PWM
143  {
144    ur[0]=Um*cos(beta);
145    ur[1]=Um*cos(beta-2./3.*M_PI);
146    ur[2]=Um*cos(beta+2./3.*M_PI);
147  }
148  else                  // PWM with injected 3rd harmonic
149  {
150    U3=0.17*cos(3.*beta);
151    ur[0]=Um*(cos(beta)-U3);
152    ur[1]=Um*(cos(beta-2./3.*M_PI)-U3);
153    ur[2]=Um*(cos(beta+2./3.*M_PI)-U3);
154  }
155
156  for (i=0;i<3;i++)
157  { dtr[i]=ubytek(fabs(iabc[i]));
158    dd[i]=dtr[i]*.73;
159  }
160
161  // implementation of voltage drops and dead-times
162  for (i=0;i<3;i++)
163    if (iabc[i]>=0)
164      if ((ur[i]>up) && (ur[i]>up2))
165        ustr[i]=Uc/2-dtr[i];
166      else
167        ustr[i]=-(Uc/2+dd[i]);
168    else
169      if ((ur[i]<up) && (ur[i]<up2))
170        ustr[i]=-(Uc/2-dtr[i]);
171      else
172        ustr[i]=Uc/2+dd[i];
173
174// phase voltages
175  ua=(2.*ustr[0]-ustr[1]-ustr[2])/3.;
176  ub=(2.*ustr[1]-ustr[0]-ustr[2])/3.;
177  uc=(2.*ustr[2]-ustr[0]-ustr[1])/3.;
178
179// voltage vector in stationary reference frame (x,y)
180  *us=(2.*ua-ub-uc)/3.;
181  *(us+1)=(ub-uc)/sqrt(3.);
182
183  // emulation of DSP timers
184  if ((citac==citac_PR)||(citac==0)) smer*=-1;
185  if ((citac2==citac_PR)||(citac2==0)) smer2*=-1;
186  citac+=smer;
187  citac2+=smer2;
188}
189
190static double ubytek(double I)
191{
192  unsigned int ii;
193  double delta_u;
194
195  ii=0;
196  while ((*(va_char+ii)<I) && (ii<(pocet-1)))
197    ii++;
198
199  if (ii==(pocet-1))
200    delta_u=*(va_char+ii+pocet);
201  else
202    if (ii==0)
203      delta_u=0;
204    else
205      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));
206
207  return delta_u;
208}
209
210
211static void pmsm_model(unsigned int mod)
212// mod<5...Euler, mod>4 ... Adams of 4th order
213{
214  double usx, usy;
215
216  usx=*us;
217  usy=*(us+1);
218
219  dIsx=-Rs/Ls*x[0]+Fmag/Ls*x[2]*sin(x[3])+usx/Ls;
220  dIsy=-Rs/Ls*x[1]-Fmag/Ls*x[2]*cos(x[3])+usy/Ls;
221  dTheta=x[2];
222
223  if (J>0)
224    dw=kp*p*p*Fmag/J*(x[1]*cos(x[3])-x[0]*sin(x[3]))-Bf/J*x[2]-p/J*x[8];
225  else
226    dw=0;
227
228  // integration
229  if (mod<5)  // Euler
230  { x[0]+=dIsx*h;
231    x[1]+=dIsy*h;
232    x[2]+=dw*h;
233    x[3]+=dTheta*h;
234  }
235  else                  // Adams (4th order)
236  { x[0]+=h/24.*(55.*dIsx-59.*dIsx1+37.*dIsx2-9.*dIsx3);
237    x[1]+=h/24.*(55.*dIsy-59.*dIsy1+37.*dIsy2-9.*dIsy3);
238    x[2]+=h/24.*(55.*dw-59.*dw1+37.*dw2-9.*dw3);
239    x[3]+=h/24.*(55.*dTheta-59.*dTheta1+37.*dTheta2-9.*dTheta3);
240  }
241
242  // saturation of theta to (-pi,pi)
243  if (x[3]>M_PI) x[3]-=(2*M_PI);
244  if (x[3]<-M_PI) x[3]+=(2*M_PI);
245
246  // diff. shift - Adams
247  dIsx3=dIsx2;dIsx2=dIsx1;dIsx1=dIsx;
248  dIsy3=dIsy2;dIsy2=dIsy1;dIsy1=dIsy;
249  dTheta3=dTheta2;dTheta2=dTheta1;dTheta1=dTheta;
250  dw3=dw2;dw2=dw1;dw1=dw;
251
252  // calculation of Isd, Isq
253  x[6]=x[0]*cos(x[3])+x[1]*sin(x[3]);         // Isd
254  x[7]=x[1]*cos(x[3])-x[0]*sin(x[3]);         // Isq
255
256  // Fsd ... d-component of stator flux
257  x[5]=Ls*x[6]+Fmag;
258
259  // Torque
260  x[4]=kp*p*Fmag*(x[1]*cos(x[3])-x[0]*sin(x[3]));
261}
262
263//////////////////////////////////////////////////////////////////////////////////////////////////////
264void pmsmsim_step(double Ww)            // you must link array KalmanObs[] to EKF modul
265{
266  double Umk, ua, ub;
267
268//  while (t<=t_end)
269  {
270    pwm(modulace);
271//    *us=KalmanObs[0]; *(us+1)=KalmanObs[1];
272    pmsm_model(5);
273
274    if (h_reg_counter>=h_reg_counter_mez)           // pocatek ISR
275    {
276      // voltages and measured currents for EKF
277      Umk=*u*Uc/Ucn;
278      ua=Umk*cos(*(u+1));
279      ub=Umk*cos(*(u+1)-2./3.*M_PI);
280      KalmanObs[0]=ua;                     // usx
281      KalmanObs[1]=(ua+2.*ub)/sqrt(3.);    // usy
282      KalmanObs[2]=Isx;
283      KalmanObs[3]=Isy;
284
285      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
286                                                                                                                // rezim=2 ... Iqw=sqrt(Imax^2-Idw^2)
287      // emulation of the real sampling of A/D converter
288      Isx=x[0];Isy=x[1];speed=x[2];theta=x[3];
289
290      h_reg_counter=0;
291    }
292
293    t+=h;
294    h_reg_counter++;
295  }
296}
297//////////////////////////////////////////////////////////////////////////////////////////////////////
Note: See TracBrowser for help on using the browser.