root/applications/pmsm/pmsm.h @ 332

Revision 332, 9.6 kB (checked in by smidl, 15 years ago)

pmsm experiments for Barcelona
- improved model of voltage
- new Mz experiment

  • Property svn:eol-style set to native
Line 
1#ifndef PMSM_H
2#define PMSM_H
3
4#include <stat/libFN.h>
5
6/*! \defgroup PMSM
7@{
8*/
9
10using namespace bdm;
11
12//TODO hardcoded RVs!!!
13RV rx ( "{ia ib om th }");
14RV ru ( "{ua ub }");
15RV ry ( "{oia oib }");
16
17// class uipmsm : public uibase{
18//      double Rs, Ls, dt, Ypm, kp, p,  J, Mz;
19// };
20
21//! State evolution model for a PMSM drive and its derivative with respect to \f$x\f$
22class IMpmsm : public diffbifn {
23protected:
24        double Rs, Ls, dt, Ypm, kp, p,  J, Mz;
25
26public:
27        IMpmsm() :diffbifn ( ) {dimy=4; dimx = 4; dimu=2;};
28        //! Set mechanical and electrical variables
29        virtual void set_parameters ( double Rs0, double Ls0, double dt0, double Ypm0, double kp0, double p0, double J0, double Mz0 ) {Rs=Rs0; Ls=Ls0; dt=dt0; Ypm=Ypm0; kp=kp0; p=p0; J=J0; Mz=Mz0;}
30
31        void modelpwm(const vec &x0, const vec u0, double &ua, double &ub){
32                double sq3=sqrt ( 3.0 );
33                double i1=x0(0);
34                double i2=0.5* ( -i1+sq3*x0[1] );
35                double i3=0.5* ( -i1-sq3*x0[1] );
36                double u1=u0(0);
37                double u2=0.5* ( -u1+sq3*u0(1) );
38                double u3=0.5* ( -u1-sq3*u0(1) );
39
40                double du1=1.4* ( double ( i1>0.3 ) - double ( i1<-0.3 ) ) +0.2*i1;
41                double du2=1.4* ( double ( i2>0.3 ) - double ( i2<-0.3 ) ) +0.2*i2;
42                double du3=1.4* ( double ( i3>0.3 ) - double ( i3<-0.3 ) ) +0.2*i3;
43                ua = ( 2.0* ( u1-du1 )- ( u2-du2 )- ( u3-du3 ) ) /3.0;
44                ub = ( ( u2-du2 )- ( u3-du3 ) ) /sq3;
45        }
46
47        vec eval ( const vec &x0, const vec &u0 ) {
48                // last state
49                const double &iam = x0 ( 0 );
50                const double &ibm = x0 ( 1 );
51                const double &omm = x0 ( 2 );
52                const double &thm = x0 ( 3 );
53                double uam;
54                double ubm;
55
56                modelpwm(x0,u0,uam,ubm);
57               
58                vec xk( 4 );
59                //ia
60                xk ( 0 ) = ( 1.0- Rs/Ls*dt ) * iam + Ypm/Ls*dt*omm * sin ( thm ) + uam*dt/Ls;
61                //ib
62                xk ( 1 ) = ( 1.0- Rs/Ls*dt ) * ibm - Ypm/Ls*dt*omm * cos ( thm ) + ubm*dt/Ls;
63                //om
64                xk ( 2 ) = omm + kp*p*p * Ypm/J*dt* ( ibm * cos ( thm )-iam * sin ( thm ) ) - p/J*dt*Mz;
65                //th
66                xk ( 3 ) = thm + omm*dt; // <0..2pi>
67                if ( xk ( 3 ) >pi ) xk ( 3 )-=2*pi;
68                if ( xk ( 3 ) <-pi ) xk ( 3 ) +=2*pi;
69                return xk;
70        }
71
72        void dfdx_cond ( const vec &x0, const vec &u0, mat &A, bool full=true ) {
73                const double &iam = x0 ( 0 );
74                const double &ibm = x0 ( 1 );
75                const double &omm = x0 ( 2 );
76                const double &thm = x0 ( 3 );
77                // d ia
78                A ( 0,0 ) = ( 1.0- Rs/Ls*dt ); A ( 0,1 ) = 0.0;
79                A ( 0,2 ) = Ypm/Ls*dt* sin ( thm ); A ( 0,3 ) = Ypm/Ls*dt*omm * ( cos ( thm ) );
80                // d ib
81                A ( 1,0 ) = 0.0 ; A ( 1,1 ) = ( 1.0- Rs/Ls*dt );
82                A ( 1,2 ) = -Ypm/Ls*dt* cos ( thm ); A ( 1,3 ) = Ypm/Ls*dt*omm * ( sin ( thm ) );
83                // d om
84                A ( 2,0 ) = kp*p*p * Ypm/J*dt* ( - sin ( thm ) );
85                A ( 2,1 ) = kp*p*p * Ypm/J*dt* ( cos ( thm ) );
86                A ( 2,2 ) = 1.0;
87                A ( 2,3 ) = kp*p*p * Ypm/J*dt* ( -ibm * sin ( thm )-iam * cos ( thm ) );
88                // d th
89                A ( 3,0 ) = 0.0; A ( 3,1 ) = 0.0; A ( 3,2 ) = dt; A ( 3,3 ) = 1.0;
90        }
91
92        void dfdu_cond ( const vec &x0, const vec &u0, mat &A, bool full=true ) {it_error ( "not needed" );};
93};
94
95
96//! State evolution model for a PMSM drive and its derivative with respect to \f$x\f$
97class IMpmsm2o : public IMpmsm {
98        protected:
99//              double Rs, Ls, dt, Ypm, kp, p,  J, Mz;
100                //! store first derivatives for the use in second derivatives
101                double dia, dib, dom, dth;
102                //! d2t = dt^2/2, cth = cos(th), sth=sin(th)
103                double d2t, cth, sth;
104                double iam, ibm, omm, thm, uam, ubm;
105        public:
106                IMpmsm2o() :IMpmsm () {};
107        //! Set mechanical and electrical variables
108                void set_parameters ( double Rs0, double Ls0, double dt0, double Ypm0, double kp0, double p0, double J0, double Mz0 ) {Rs=Rs0; Ls=Ls0; dt=dt0; Ypm=Ypm0; kp=kp0; p=p0; J=J0; Mz=Mz0; d2t=dt*dt/2;}
109
110                vec eval ( const vec &x0, const vec &u0 ) {
111                // last state
112                        iam = x0 ( 0 );
113                        ibm = x0 ( 1 );
114                        omm = x0 ( 2 );
115                        thm = x0 ( 3 );
116                        uam = u0 ( 0 );
117                        ubm = u0 ( 1 );
118
119                        cth = cos(thm);
120                        sth = sin(thm);
121                       
122                        dia = (- Rs/Ls*iam +  Ypm/Ls*omm * sth + uam/Ls);
123                        dib = (- Rs/Ls*ibm -  Ypm/Ls*omm * cth + ubm/Ls);
124                        dom = kp*p*p * Ypm/J *( ibm * cth-iam * sth ) - p/J*Mz;
125                        dth = omm;
126                                               
127                        vec xk=zeros ( 4 );
128                        xk ( 0 ) =  iam + dt*dia;// +d2t*d2ia;
129                        xk ( 1 ) = ibm + dt*dib;// +d2t*d2ib;
130                        xk ( 2 ) = omm +dt*dom;// +d2t*d2om;
131                        xk ( 3 ) = thm + dt*dth;// +d2t*dom; // <0..2pi>
132                       
133                        if ( xk ( 3 ) >pi ) xk ( 3 )-=2*pi;
134                        if ( xk ( 3 ) <-pi ) xk ( 3 ) +=2*pi;
135                        return xk;
136                }
137
138                //! eval 2nd order Taylor expansion, MUST be used only as a follow up AFTER eval()!!
139                vec eval2o(const vec &du){
140                        double dua = du ( 0 )/dt;
141                        double dub = du ( 1 )/dt;
142                       
143                        vec xth2o(4);
144                        xth2o(0) = (- Rs/Ls*dia +  Ypm/Ls*(dom * sth + omm*cth) + dua/Ls);
145                        xth2o(1) = (- Rs/Ls*dib -  Ypm/Ls*(dom * cth - omm*sth) + dub/Ls);
146                        xth2o(2) = kp*p*p * Ypm/J *( dib * cth-ibm*sth - (dia * sth + iam *cth));
147                        xth2o(3) = dom;
148                        // multiply by dt^2/2
149                        xth2o*=d2t/2;
150                        return xth2o;
151                }
152                void dfdx_cond ( const vec &x0, const vec &u0, mat &A, bool full=true ) {
153                         iam = x0 ( 0 );
154                         ibm = x0 ( 1 );
155                         omm = x0 ( 2 );
156                         thm = x0 ( 3 );
157                // d ia
158                        A ( 0,0 ) = ( 1.0- Rs/Ls*dt ); A ( 0,1 ) = 0.0;
159                        A ( 0,2 ) = Ypm/Ls*dt* sin ( thm ); A ( 0,3 ) = Ypm/Ls*dt*omm * ( cos ( thm ) );
160                // d ib
161                        A ( 1,0 ) = 0.0 ; A ( 1,1 ) = ( 1.0- Rs/Ls*dt );
162                        A ( 1,2 ) = -Ypm/Ls*dt* cos ( thm ); A ( 1,3 ) = Ypm/Ls*dt*omm * ( sin ( thm ) );
163                // d om
164                        A ( 2,0 ) = kp*p*p * Ypm/J*dt* ( - sin ( thm ) );
165                        A ( 2,1 ) = kp*p*p * Ypm/J*dt* ( cos ( thm ) );
166                        A ( 2,2 ) = 1.0;
167                        A ( 2,3 ) = kp*p*p * Ypm/J*dt* ( -ibm * sin ( thm )-iam * cos ( thm ) );
168                // d th
169                        A ( 3,0 ) = 0.0; A ( 3,1 ) = 0.0; A ( 3,2 ) = dt; A ( 3,3 ) = 1.0;
170                        // FOR d2t*dom!!!!!!!!!
171/*                      A ( 3,0 ) = dt* kp*p*p * Ypm/J*dt* ( - sin ( thm ) );
172                        A ( 3,1 ) = dt* kp*p*p * Ypm/J*dt* ( cos ( thm ) );
173                        A ( 3,2 ) = dt;
174                        A ( 3,3 ) = 1.0 + dt* kp*p*p * Ypm/J*dt* ( -ibm * sin ( thm )-iam * cos ( thm ) );*/
175                }
176
177                void dfdu_cond ( const vec &x0, const vec &u0, mat &A, bool full=true ) {it_error ( "not needed" );};
178
179};
180
181//! State evolution model for a PMSM drive and its derivative with respect to \f$x\f$, equation for \f$\omega\f$ is omitted.$
182class IMpmsmStat : public IMpmsm {
183        public:
184        IMpmsmStat() :IMpmsm() {};
185        //! Set mechanical and electrical variables
186        void set_parameters ( double Rs0, double Ls0, double dt0, double Ypm0, double kp0, double p0, double J0, double Mz0 ) {Rs=Rs0; Ls=Ls0; dt=dt0; Ypm=Ypm0; kp=kp0; p=p0; J=J0; Mz=Mz0;}
187
188        vec eval ( const vec &x0, const vec &u0 ) {
189                // last state
190                double iam = x0 ( 0 );
191                double ibm = x0 ( 1 );
192                double omm = x0 ( 2 );
193                double thm = x0 ( 3 );
194                double uam = u0 ( 0 );
195                double ubm = u0 ( 1 );
196
197                vec xk=zeros ( 4 );
198                //ia
199                xk ( 0 ) = ( 1.0- Rs/Ls*dt ) * iam + Ypm/Ls*dt*omm * sin ( thm ) + uam*dt/Ls;
200                //ib
201                xk ( 1 ) = ( 1.0- Rs/Ls*dt ) * ibm - Ypm/Ls*dt*omm * cos ( thm ) + ubm*dt/Ls;
202                //om
203                xk ( 2 ) = omm - p/J*dt*Mz;// + kp*p*p * Ypm/J*dt* ( ibm * cos ( thm )-iam * sin ( thm ) );
204                //th
205                xk ( 3 ) = rem(thm + omm*dt,2*pi); // <0..2pi>
206                return xk;
207        }
208
209        void dfdx_cond ( const vec &x0, const vec &u0, mat &A, bool full=true ) {
210//              double iam = x0 ( 0 );
211//              double ibm = x0 ( 1 );
212                double omm = x0 ( 2 );
213                double thm = x0 ( 3 );
214                // d ia
215                A ( 0,0 ) = ( 1.0- Rs/Ls*dt ); A ( 0,1 ) = 0.0;
216                A ( 0,2 ) = Ypm/Ls*dt* sin ( thm ); A ( 0,3 ) = Ypm/Ls*dt*omm * ( cos ( thm ) );
217                // d ib
218                A ( 1,0 ) = 0.0 ; A ( 1,1 ) = ( 1.0- Rs/Ls*dt );
219                A ( 1,2 ) = -Ypm/Ls*dt* cos ( thm ); A ( 1,3 ) = Ypm/Ls*dt*omm * ( sin ( thm ) );
220                // d om
221                A ( 2,0 ) = 0.0;//kp*p*p * Ypm/J*dt* ( - sin ( thm ) );
222                A ( 2,1 ) = 0.0;//kp*p*p * Ypm/J*dt* ( cos ( thm ) );
223                A ( 2,2 ) = 1.0;
224                A ( 2,3 ) = 0.0;//kp*p*p * Ypm/J*dt* ( -ibm * sin ( thm )-iam * cos ( thm ) );
225                // d th
226                A ( 3,0 ) = 0.0; A ( 3,1 ) = 0.0; A ( 3,2 ) = dt; A ( 3,3 ) = 1.0;
227        }
228
229        void dfdu_cond ( const vec &x0, const vec &u0, mat &A, bool full=true ) {it_error ( "not needed" );};
230
231};
232
233//! State for PMSM with unknown Mz
234class IMpmsmMz: public IMpmsm{
235        public:
236                IMpmsmMz()  {dimy=5; dimx = 5; dimu=2;};
237        //! extend eval by Mz
238                vec eval ( const vec &x0, const vec &u0 ) {
239                        vec x(4);
240                        Mz = x0(4); //last of the state is Mz
241               
242                //teh first 4 states are same as before (given that Mz is set)
243                        x=IMpmsm::eval(x0,u0); // including model of drops!
244                        return concat(x,Mz);
245                }
246                void dfdx_cond ( const vec &x0, const vec &u0, mat &A, bool full=true ) {
247                //call initial
248                        if (full) A.clear();
249                        IMpmsm::dfdx_cond(x0,u0,A,full);
250                        A(2,4)=- p/J*dt;
251                        A(4,4)=1.0;
252                }       
253};
254
255//! State for PMSM with unknown Mz
256class IMpmsmStatMz: public IMpmsmStat{
257        public:
258                IMpmsmStatMz()  {dimy=5; dimx = 5; dimu=2;};
259        //! extend eval by Mz
260                vec eval ( const vec &x0, const vec &u0 ) {
261                        vec x(4);
262                        Mz = x0(4); //last of the state is Mz
263               
264                //teh first 4 states are same as before (given that Mz is set)
265                        x=IMpmsmStat::eval(x0,u0); // including model of drops!
266                        return concat(x,Mz);
267                }
268                void dfdx_cond ( const vec &x0, const vec &u0, mat &A, bool full=true ) {
269                //call initial
270                        if (full) A.clear();
271                        IMpmsmStat::dfdx_cond(x0,u0,A,full);
272                        A(2,4)=- p/J*dt;
273                        A(4,4)=1.0;
274                }       
275};
276
277
278//! Observation model for PMSM drive and its derivative with respect to \f$x\f$
279class OMpmsm: public diffbifn {
280public:
281        OMpmsm() :diffbifn () {dimy=2;dimx=4;dimu=2;};
282
283        vec eval ( const vec &x0, const vec &u0 ) {
284                vec y ( 2 );
285                y ( 0 ) = x0 ( 0 );
286                y ( 1 ) = x0 ( 1 );
287                return y;
288        }
289
290        void dfdx_cond ( const vec &x0, const vec &u0, mat &A, bool full=true ) {
291                A.clear();
292                A ( 0,0 ) = 1.0;
293                A ( 1,1 ) = 1.0;
294        }
295};
296
297//! Observation model for PMSM drive and its derivative with respect to \f$x\f$ for full vector of observations
298class OMpmsm4: public diffbifn {
299        public:
300                OMpmsm4() :diffbifn () {dimy=4;dimx=4;dimu=2;};
301
302                vec eval ( const vec &x0, const vec &u0 ) {
303                        vec y ( 4 );
304                        y  = x0 ;
305                        return y;
306                }
307
308                void dfdx_cond ( const vec &x0, const vec &u0, mat &A, bool full=true ) {
309                        if (full) A=eye(4);
310                }
311};
312
313/*!@}*/
314#endif //PMSM_H
Note: See TracBrowser for help on using the browser.