root/applications/pmsm/pmsm.h @ 349

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

Barcelona + mex for pmsm + results of pwm experiment for off-line use with pwm.m (>> pwm(0))

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