root/applications/pmsm/pmsm.h @ 654

Revision 654, 10.2 kB (checked in by smidl, 15 years ago)

PMSM compiles again

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