root/simulator_zdenek/simulator.cpp @ 40

Revision 40, 7.8 kB (checked in by smidl, 16 years ago)

Opravy simulatoru: premenovani na prefix pmsmsim_ + preklad na linuxu

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