root/applications/pmsm/pmsm.h @ 666

Revision 666, 10.4 kB (checked in by smidl, 15 years ago)

pmsm details

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