root/applications/pmsm/pmsmDS.h @ 342

Revision 342, 5.4 kB (checked in by smidl, 15 years ago)

Barcelona

  • Property svn:eol-style set to native
RevLine 
[258]1/*!
2  \file
3  \brief DataSource for experiments with realistic simulator of the PMSM model
4  \author Vaclav Smidl.
5
6  -----------------------------------
7  BDM++ - C++ library for Bayesian Decision Making under Uncertainty
8
9  Using IT++ for numerical operations
10  -----------------------------------
11*/
12
13#include <stat/loggers.h>
[318]14#include <estim/libKF.h>
[260]15#include "simulator.h"
[258]16#include "pmsm.h"
17
18//! Simulator of PMSM machine with predefined profile on omega
19class pmsmDS : public DS {
20
21protected:
22        //! indeces of logged variables
[260]23        int L_x, L_ou, L_oy, L_iu, L_optu;
[258]24        //! Setpoints of omega in timespans given by dt_prof
25        vec profileWw;
[318]26        //! Setpoints of Mz in timespans given by dt_prof
27        vec profileMz;
[258]28        //! time-step for profiles
29        double dt_prof;
30        //! Number of miliseconds per discrete time step
31        int Dt;
[280]32        //! options for logging, - log predictions of 'true' voltage
33        bool opt_modu;
[318]34        //! options for logging, -
[258]35public:
[260]36        //! Constructor with fixed sampling period
[342]37        pmsmDS ()   {Dt=125; Drv=RV ( "{o_ua o_ub o_ia o_ib t_ua t_ub o_om o_th Mz }" );}
[258]38        void set_parameters ( double Rs0, double Ls0, double Fmag0, double Bf0, double p0, double kp0, double J0, double Uc0, double DT0, double dt0 ) {
39                pmsmsim_set_parameters ( Rs0, Ls0, Fmag0, Bf0, p0, kp0, J0, Uc0, DT0, dt0 );
40        }
[280]41        //! parse options: "modelu" => opt_modu=true;
42        void set_options ( string &opt ) {
[342]43                opt_modu = ( opt.find ( "modelu" ) !=string::npos );
[280]44        }
[342]45        void getdata ( vec &dt ) {dt.set_subvector(0,vec ( KalmanObs,6 ));dt(6)=x[2];dt(7)=x[3];dt(8)=x[8];}
[258]46        void write ( vec &ut ) {}
47
48        void step() {
49                static int ind=0;
50                static double dW; // increase of W
51                static double Ww; // W
[318]52                static double Mz; // W
[258]53                if ( t>=dt_prof*ind ) {
[260]54                        ind++;
[318]55                        // check omega profile and set dW
[258]56                        if ( ind<profileWw.length() ) {
57                                //linear increase
[260]58                                if ( profileWw.length() ==1 ) {
[280]59                                        Ww=profileWw ( 0 ); dW=0.0;
60                                }
[260]61                                else {
62                                        dW = profileWw ( ind )-profileWw ( ind-1 );
63                                        dW *=125e-6/dt_prof;
64                                }
[258]65                        }
66                        else {
67                                dW = 0;
68                        }
[318]69                        // Check Mz profile and set Mz
70                        if ( ind<profileMz.length() ) {
71                                //sudden increase
72                                Mz = profileMz(ind);
73                        }
74                        else {
75                                Mz = 0;
76                        }
[258]77                }
78                Ww += dW;
79                //Simulate Dt seconds!
[318]80                for ( int i=0;i<Dt;i++ ) {      pmsmsim_step ( Ww , Mz);}
[326]81//              for ( int i=0;i<Dt;i++ ) {      pmsmsim_noreg_step ( Ww , Mz);}
[332]82               
83                //discretization
[342]84                double ustep=1.2;
[332]85                KalmanObs [ 0 ] = ustep*itpp::round( KalmanObs [ 0 ]/ ustep) ;
86                KalmanObs [ 1 ] = ustep*itpp::round(KalmanObs [ 1 ]/ ustep);
87                double istep=0.085;
88                KalmanObs [ 2 ] = istep*itpp::round( KalmanObs [ 2 ]/ istep) ;
89                KalmanObs [ 3 ] = istep*itpp::round(KalmanObs [ 3 ]/ istep);
90
[258]91        };
92
93        void log_add ( logger &L ) {
94                L_x = L.add ( rx, "x" );
[280]95                L_oy = L.add ( ry, "o" );
96                L_ou = L.add ( ru, "o" );
97                L_iu = L.add ( ru, "t" );
[260]98                // log differences
[280]99                if ( opt_modu ) {
100                        L_optu = L.add ( ru, "model" );
[260]101                }
[258]102        }
103
104        void logit ( logger &L ) {
105                L.logit ( L_x, vec ( x,4 )      );
106                L.logit ( L_oy, vec_2 ( KalmanObs[2],KalmanObs[3] ) );
107                L.logit ( L_ou, vec_2 ( KalmanObs[0],KalmanObs[1] ) );
108                L.logit ( L_iu, vec_2 ( KalmanObs[4],KalmanObs[5] ) );
[280]109                if ( opt_modu ) {
110                        double sq3=sqrt ( 3.0 );
[260]111                        double ua,ub;
112                        double i1=x[0];
[280]113                        double i2=0.5* ( -i1+sq3*x[1] );
114                        double i3=0.5* ( -i1-sq3*x[1] );
[260]115                        double u1=KalmanObs[0];
[280]116                        double u2=0.5* ( -u1+sq3*KalmanObs[1] );
117                        double u3=0.5* ( -u1-sq3*KalmanObs[1] );
118
[332]119                        double du1=1.4* ( double ( i1>0.3 ) - double ( i1<-0.3 ) ) +0.2*i1;
120                        double du2=1.4* ( double ( i2>0.3 ) - double ( i2<-0.3 ) ) +0.2*i2;
121                        double du3=1.4* ( double ( i3>0.3 ) - double ( i3<-0.3 ) ) +0.2*i3;
[280]122                        ua = ( 2.0* ( u1-du1 )- ( u2-du2 )- ( u3-du3 ) ) /3.0;
123                        ub = ( ( u2-du2 )- ( u3-du3 ) ) /sq3;
124                        L.logit ( L_optu , vec_2 ( ua,ub ) );
[260]125                }
[318]126               
[280]127        }
[258]128
[318]129        void set_profile ( double dt, const vec &Ww, const vec &Mz ) {dt_prof=dt; profileWw=Ww; profileMz=Mz;}
[258]130};
[318]131
132//! This class behaves like BM but it is evaluating EKF
[326]133class pmsmCRB : public EKFfull{
[318]134        protected:
135                vec interr;
136                vec old_true;
137                vec secder;
138                int L_CRB;
139                int L_err;
140                int L_sec;
141        public:
142        //! constructor
[326]143        pmsmCRB():EKFfull(){old_true=zeros(6);}
[318]144
145        void bayes(const vec &dt){
[332]146                static vec umin(2);
147                vec u(2); 
[318]148                //assume we know state exactly:
149                vec true_state=vec(x,4); // read from pmsm
[326]150                E.set_mu(true_state);
[318]151               
152                //integration error
153                old_true(4)=KalmanObs[4];
154                old_true(5)=KalmanObs[5];// add U
[332]155                u(0) = KalmanObs[0]; // use the required value for derivatives
156                u(1) = KalmanObs[1];
[318]157                interr = (true_state - pfxu->eval(old_true));
158               
159                //second derivative
160                IMpmsm2o* pf = dynamic_cast<IMpmsm2o*>(pfxu);
[332]161                if (pf) {secder=pf->eval2o(u-umin);}
[318]162                               
[332]163                umin =u;
[326]164                EKFfull::bayes(dt);
[318]165                old_true.set_subvector(0,true_state);
166        }
167       
168        void log_add(logger &L, const string &name="" ){
169                L_CRB=L.add(rx,"crb");
170                L_err=L.add(rx,"err");
171                L_sec=L.add(rx,"d2");
172        }
173        void logit(logger &L){
174                L.logit(L_err, interr);
[326]175                L.logit(L_CRB,diag(_R()));
[318]176                L.logit(L_sec,secder);
177        }
[332]178};
[342]179
180//! This class behaves like BM but it is evaluating EKF
181class pmsmCRBMz : public EKFfull{
182        protected:
183                int L_CRB;
184        public:
185        //! constructor
186                pmsmCRBMz():EKFfull(){}
187
188                void bayes(const vec &dt){
189//assume we know state exactly:
190                        vec true_state(5);
191                        true_state.set_subvector(0,vec(x,4)); // read from pmsm
192                        true_state(4)=x[8];
193                       
194                        E.set_mu(true_state);
195                        mu = true_state;
196                        //hack for ut
197                        EKFfull::bayes(dt);
198                }
199       
200                void log_add(logger &L, const string &name="" ){
201                        L_CRB=L.add(concat(rx,RV("Mz",1,0)),"crb");
202                }
203                void logit(logger &L){
204                        L.logit(L_CRB,diag(_R()));
205                }
206};
Note: See TracBrowser for help on using the browser.