root/pmsm/old/pmsm_sim2.cpp @ 279

Revision 279, 3.4 kB (checked in by smidl, 15 years ago)

Transition of pmsm and libKF

  • Property svn:eol-style set to native
Line 
1/*
2  \file
3  \brief Models for synchronous electric drive using IT++ and BDM
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
14#include <estim/libKF.h>
15#include <estim/libPF.h>
16#include <stat/libFN.h>
17
18#include <stat/loggers.h>
19
20#include "pmsm.h"
21#include "simulator.h"
22#include "sim_profiles.h"
23
24using namespace bdm;
25//!Extended Kalman filter with unknown \c Q
26class EKF_unQ : public EKFCh , public BMcond {
27public:
28        //! Default constructor
29        void condition ( const vec &Q0 ) {
30                Q.setD ( Q0,0 );
31                //from EKF
32                preA.set_submatrix ( dimy+dimx,dimy,Q._Ch() );
33        };
34        void bayes(const vec dt){
35        EKFCh::bayes(dt);
36               
37                vec xtrue(4);
38                //UGLY HACK!!! reliance on a predictor!!
39                xtrue(0)=x[0];
40                xtrue(1)=x[1];
41                xtrue(2)=x[2];
42                xtrue(3)=x[3];
43               
44                ll = -0.5* ( 4 * 1.83787706640935 +_P.logdet() +_P.invqform(xtrue));
45        }
46};
47class EKF_unQful : public EKFfull , public BMcond {
48public:
49        void condition ( const vec &Q0 ) {
50                Q=diag(Q0);
51        };
52        void bayes(const vec dt){
53        EKFfull::bayes(dt);
54               
55                vec xtrue(4);
56                //UGLY HACK!!! reliance on a predictor!!
57                xtrue(0)=x[0];
58                xtrue(1)=x[1];
59                xtrue(2)=x[2];
60                xtrue(3)=x[3];
61               
62                BM::ll = -0.5* ( 4 * 1.83787706640935 +log(det(P)) +xtrue* ( inv(P)*xtrue ) );
63        }
64};
65
66int main() {
67        // Kalman filter
68        int Ndat = 90000;
69        double h = 1e-6;
70        int Nsimstep = 125;
71        int Npart = 50;
72       
73        dirfilelog L("exp/pmsm_sim2",1000);
74       
75        // internal model
76        IMpmsm fxu;
77        //                  Rs    Ls        dt           Fmag(Ypm) kp   p    J     Bf(Mz)
78        fxu.set_parameters ( 0.28, 0.003465, Nsimstep*h, 0.1989,   1.5 ,4.0, 0.04, 0.0 );
79        // observation model
80        OMpmsm hxu;
81
82        vec mu0= "0.0 0.0 0.0 0.0";
83//      vec Qdiag ( "0.01 0.01 0.01 0.0001" ); //zdenek: 0.01 0.01 0.0001 0.0001
84        vec Qdiag ( "10 10 10 0.001" ); //zdenek: 0.01 0.01 0.0001 0.0001
85        vec Rdiag ( "100 100" ); //var(diff(xth)) = "0.034 0.034"
86        chmat Q ( Qdiag );
87        chmat R ( Rdiag );
88        EKFCh KFE ;
89        KFE.set_est ( mu0, chmat( 1*eye ( 4 ) ) );
90        KFE.set_parameters ( &fxu,&hxu,Q,R);
91
92        RV rQ ( "{Q}","4" );
93        EKF_unQful KFEp ;
94        KFEp.set_est ( mu0,  1*ones ( 4 ) );
95        KFEp.set_parameters ( &fxu,&hxu,diag(Qdiag),diag(Rdiag) );
96
97        mgamma_fix evolQ ;
98        MPF<EKF_unQful> M ( &evolQ,&evolQ,Npart,KFEp );
99        // initialize
100        evolQ.set_parameters ( 1000.0 ,Qdiag, 0.5); //sigma = 1/10 mu
101        evolQ.condition ( Qdiag ); //Zdenek default
102        epdf& pfinit=evolQ._epdf();
103        M.set_est ( pfinit );
104        evolQ.set_parameters ( 100000.0, Qdiag, 0.9999 );
105        //
106        const epdf& KFEep = KFE.posterior();
107        const epdf& Mep = M.posterior();
108
109        int X_log = L.add(rx,"X");
110        int Efix_log = L.add(rx,"XF");
111        RV tmp=concat(rQ,rx);
112        int M_log = L.add(tmp,"M");
113        L.init();
114
115        // SET SIMULATOR
116        pmsmsim_set_parameters ( 0.28,0.003465,0.1989,0.0,4,1.5,0.04, 200., 3e-6, h );
117        double Ww=0.0;
118        vec dt ( 2 );
119        vec ut ( 2 );
120
121        for ( int tK=1;tK<Ndat;tK++ ) {
122                //Number of steps of a simulator for one step of Kalman
123                for ( int ii=0; ii<Nsimstep;ii++ ) {
124                        //simulator
125                        sim_profile_steps1(Ww);
126                        pmsmsim_step ( Ww );
127                };
128                // collect data
129                ut ( 0 ) = KalmanObs[0];
130                ut ( 1 ) = KalmanObs[1];
131                dt ( 0 ) = KalmanObs[2];
132                dt ( 1 ) = KalmanObs[3];
133
134                //estimator
135                KFE.bayes ( concat ( dt,ut ) );
136                M.bayes ( concat ( dt,ut ) );
137               
138                L.logit(X_log,  vec(x,4)); //vec from C-array
139                L.logit(Efix_log, KFEep.mean() ); 
140                L.logit(M_log,  Mep.mean() ); 
141               
142                L.step();
143        }
144
145        L.finalize(); 
146       
147        return 0;
148}
Note: See TracBrowser for help on using the browser.