root/pmsm/simulator_zdenek/simulator.cpp @ 80

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

Zmena v simulatoru pro pouziti v pmsm_sim

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