root/applications/pmsm/pmsmDS.h @ 319

Revision 318, 4.3 kB (checked in by smidl, 15 years ago)

pmsm CRB model

  • Property svn:eol-style set to native
Line 
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>
14#include <estim/libKF.h>
15#include "simulator.h"
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
23        int L_x, L_ou, L_oy, L_iu, L_optu;
24        //! Setpoints of omega in timespans given by dt_prof
25        vec profileWw;
26        //! Setpoints of Mz in timespans given by dt_prof
27        vec profileMz;
28        //! time-step for profiles
29        double dt_prof;
30        //! Number of miliseconds per discrete time step
31        int Dt;
32        //! options for logging, - log predictions of 'true' voltage
33        bool opt_modu;
34        //! options for logging, -
35public:
36        //! Constructor with fixed sampling period
37        pmsmDS ()   {Dt=125; Drv=RV ( "{o_ua o_ub o_ia o_ib t_ua t_ub }" );}
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        }
41        //! parse options: "modelu" => opt_modu=true;
42        void set_options ( string &opt ) {
43                opt_modu = ( opt.find ( "modelu" ) ==string::npos );
44        }
45        void getdata ( vec &dt ) {dt=vec ( KalmanObs,6 );}
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
52                static double Mz; // W
53                if ( t>=dt_prof*ind ) {
54                        ind++;
55                        // check omega profile and set dW
56                        if ( ind<profileWw.length() ) {
57                                //linear increase
58                                if ( profileWw.length() ==1 ) {
59                                        Ww=profileWw ( 0 ); dW=0.0;
60                                }
61                                else {
62                                        dW = profileWw ( ind )-profileWw ( ind-1 );
63                                        dW *=125e-6/dt_prof;
64                                }
65                        }
66                        else {
67                                dW = 0;
68                        }
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                        }
77                }
78                Ww += dW;
79                //Simulate Dt seconds!
80                for ( int i=0;i<Dt;i++ ) {      pmsmsim_step ( Ww , Mz);}
81        };
82
83        void log_add ( logger &L ) {
84                L_x = L.add ( rx, "x" );
85                L_oy = L.add ( ry, "o" );
86                L_ou = L.add ( ru, "o" );
87                L_iu = L.add ( ru, "t" );
88                // log differences
89                if ( opt_modu ) {
90                        L_optu = L.add ( ru, "model" );
91                }
92        }
93
94        void logit ( logger &L ) {
95                L.logit ( L_x, vec ( x,4 )      );
96                L.logit ( L_oy, vec_2 ( KalmanObs[2],KalmanObs[3] ) );
97                L.logit ( L_ou, vec_2 ( KalmanObs[0],KalmanObs[1] ) );
98                L.logit ( L_iu, vec_2 ( KalmanObs[4],KalmanObs[5] ) );
99                if ( opt_modu ) {
100                        double sq3=sqrt ( 3.0 );
101                        double ua,ub;
102                        double i1=x[0];
103                        double i2=0.5* ( -i1+sq3*x[1] );
104                        double i3=0.5* ( -i1-sq3*x[1] );
105                        double u1=KalmanObs[0];
106                        double u2=0.5* ( -u1+sq3*KalmanObs[1] );
107                        double u3=0.5* ( -u1-sq3*KalmanObs[1] );
108
109                        double du1=0.7* ( double ( i1>0.1 ) - double ( i1<-0.1 ) ) +0.05*i1;
110                        double du2=0.7* ( double ( i2>0.1 ) - double ( i2<-0.1 ) ) +0.05*i2;
111                        double du3=0.7* ( double ( i3>0.1 ) - double ( i3<-0.1 ) ) +0.05*i3;
112                        ua = ( 2.0* ( u1-du1 )- ( u2-du2 )- ( u3-du3 ) ) /3.0;
113                        ub = ( ( u2-du2 )- ( u3-du3 ) ) /sq3;
114                        L.logit ( L_optu , vec_2 ( ua,ub ) );
115                }
116               
117        }
118
119        void set_profile ( double dt, const vec &Ww, const vec &Mz ) {dt_prof=dt; profileWw=Ww; profileMz=Mz;}
120};
121
122//! This class behaves like BM but it is evaluating EKF
123class pmsmCRB : public EKFCh{
124        protected:
125                vec interr;
126                vec old_true;
127                vec secder;
128                int L_CRB;
129                int L_err;
130                int L_sec;
131        public:
132        //! constructor
133        pmsmCRB():EKFCh(){old_true=zeros(6);}
134
135        void bayes(const vec &dt){
136                //assume we know state exactly:
137                vec true_state=vec(x,4); // read from pmsm
138                est.set_mu(true_state);
139               
140                //integration error
141                old_true(4)=KalmanObs[4];
142                old_true(5)=KalmanObs[5];// add U
143                interr = (true_state - pfxu->eval(old_true));
144               
145                //second derivative
146                IMpmsm2o* pf = dynamic_cast<IMpmsm2o*>(pfxu);
147                if (pf) {secder=pf->eval2o(vec_2(KalmanObs[4],KalmanObs[5]));}
148                               
149                EKFCh::bayes(dt);
150                old_true.set_subvector(0,true_state);
151        }
152       
153        void log_add(logger &L, const string &name="" ){
154                L_CRB=L.add(rx,"crb");
155                L_err=L.add(rx,"err");
156                L_sec=L.add(rx,"d2");
157        }
158        void logit(logger &L){
159                L.logit(L_err, interr);
160                L.logit(L_CRB,diag(est._R().to_mat()));
161                L.logit(L_sec,secder);
162        }
163};
Note: See TracBrowser for help on using the browser.