root/tests/pmsm_sim2.cpp @ 62

Revision 62, 5.4 kB (checked in by smidl, 16 years ago)

nova simulace s EKFfixed a novy EKF na plnych maticich

  • 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#include <itpp/itbase.h>
14#include <estim/libKF.h>
15#include <estim/libPF.h>
16#include <stat/libFN.h>
17
18#include "pmsm.h"
19#include "simulator.h"
20
21#include "iopom.h"
22
23using namespace itpp;
24//!Extended Kalman filter with unknown \c Q
25class EKF_unQ : public EKFCh , public BMcond {
26public:
27        //! Default constructor
28        EKF_unQ ( RV rx, RV ry,RV ru,RV rQ ) :EKFCh ( rx,ry,ru ),BMcond ( rQ ) {};
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};
35
36void set_simulator_t(double &Ww) {
37
38        if (t>0.2) x[8]=1.2;    // 1A //0.2ZP
39        if (t>0.4) x[8]=10.8;   // 9A
40        if (t>0.6) x[8]=25.2;  // 21A
41
42        if (t>0.7) Ww=2.*M_PI*10.;
43        if (t>1.0) x[8]=1.2;    // 1A
44        if (t>1.2) x[8]=10.8;   // 9A
45        if (t>1.4) x[8]=25.2;  // 21A
46
47        if (t>1.6) Ww=2.*M_PI*50.;
48        if (t>1.9) x[8]=1.2;    // 1A
49        if (t>2.1) x[8]=10.8;   // 9A
50        if (t>2.3) x[8]=25.2;  // 21A
51
52        if (t>2.5) Ww=2.*M_PI*100;
53        if (t>2.8) x[8]=1.2;    // 1A
54        if (t>3.0) x[8]=10.8;   // 9A
55        if (t>3.2) x[8]=25.2;  // 21A
56
57        if (t>3.4) Ww=2.*M_PI*150;
58        if (t>3.7) x[8]=1.2;    // 1A
59        if (t>3.9) x[8]=10.8;   // 9A
60        if (t>4.1) x[8]=25.2;  // 21A
61
62        if (t>4.3) Ww=2.*M_PI*0;
63        if (t>4.8) x[8]=-1.2;    // 1A
64        if (t>5.0) x[8]=-10.8;   // 9A
65        if (t>5.2) x[8]=-25.2;  // 21A
66
67        if (t>5.4) Ww=2.*M_PI*(-10.);
68        if (t>5.7) x[8]=-1.2;    // 1A
69        if (t>5.9) x[8]=-10.8;   // 9A
70        if (t>6.1) x[8]=-25.2;  // 21A
71
72        if (t>6.3) Ww=2.*M_PI*(-50.);
73        if (t>6.7) x[8]=-1.2;    // 1A
74        if (t>6.9) x[8]=-10.8;   // 9A
75        if (t>7.1) x[8]=-25.2;  // 21A
76
77        if (t>7.3) Ww=2.*M_PI*(-100.);
78        if (t>7.7) x[8]=-1.2;    // 1A
79        if (t>7.9) x[8]=-10.8;   // 9A
80        if (t>8.1) x[8]=-25.2;  // 21A
81        if (t>8.3) x[8]=10.8;   // 9A
82        if (t>8.5) x[8]=25.2;  // 21A
83       
84//        x[8]=0.0;
85}
86
87int main() {
88        // Kalman filter
89        int Ndat = 90000;
90        double h = 1e-6;
91        int Nsimstep = 125;
92        int Npart = 100;
93       
94        //StrSim:06:
95        vec SSAT(Ndat);
96
97        // internal model
98        IMpmsm fxu;
99        //                  Rs    Ls        dt           Fmag(Ypm) kp   p    J     Bf(Mz)
100        fxu.set_parameters ( 0.28, 0.003465, Nsimstep*h, 0.1989,   1.5 ,4.0, 0.04, 0.0 );
101        // observation model
102        OMpmsm hxu;
103
104        vec mu0= "0.0 0.0 0.0 0.0";
105//      vec Qdiag ( "0.01 0.01 0.01 0.0001" ); //zdenek: 0.01 0.01 0.0001 0.0001
106        vec Qdiag ( "18 18 157.9 0.0001" ); //zdenek: 0.01 0.01 0.0001 0.0001
107        vec Rdiag ( "90 90" ); //var(diff(xth)) = "0.034 0.034"
108        chmat Q ( Qdiag );
109        chmat R ( Rdiag );
110        EKFCh KFE ( rx,ry,ru );
111        KFE.set_est ( mu0, ( 1*eye ( 4 ) ) );
112        KFE.set_parameters ( &fxu,&hxu,Qdiag,Rdiag);
113
114        RV rQ ( "100","{Q}","4","0" );
115        EKF_unQ KFEp ( rx,ry,ru,rQ );
116        KFEp.set_est ( mu0, chmat ( 1*ones ( 4 ) ) );
117        KFEp.set_parameters ( &fxu,&hxu,Q,R );
118
119        mgamma_fix evolQ ( rQ,rQ );
120        MPF<EKF_unQ> M ( rx,rQ,evolQ,evolQ,Npart,KFEp );
121        // initialize
122        evolQ.set_parameters ( 1000.0 ,Qdiag, 0.5); //sigma = 1/10 mu
123        evolQ.condition ( Qdiag ); //Zdenek default
124        epdf& pfinit=evolQ._epdf();
125        M.set_est ( pfinit );
126        evolQ.set_parameters ( 5000.0, Qdiag, 0.9999 );
127
128        //
129
130        epdf& KFEep = KFE._epdf();
131        epdf& Mep = M._epdf();
132
133        mat Xt=zeros ( Ndat ,9 ); //true state from simulator
134        mat Dt=zeros ( Ndat,4+2 ); //observation
135        mat XtE=zeros ( Ndat, 4 );
136        mat XtM=zeros ( Ndat,4+4 ); //Q + x
137
138        // SET SIMULATOR
139        pmsmsim_set_parameters ( 0.28,0.003465,0.1989,0.0,4,1.5,0.04, 200., 3e-6, h );
140        double Ww=0.0;
141        vec dt ( 2 );
142        vec ut ( 2 );
143
144        for ( int tK=1;tK<Ndat;tK++ ) {
145                //Number of steps of a simulator for one step of Kalman
146                for ( int ii=0; ii<Nsimstep;ii++ ) {
147                        //simulator
148                        set_simulator_t(Ww);
149                        pmsmsim_step ( Ww );
150                };
151                // collect data
152                ut ( 0 ) = KalmanObs[0];
153                ut ( 1 ) = KalmanObs[1];
154                dt ( 0 ) = KalmanObs[2];
155                dt ( 1 ) = KalmanObs[3];
156
157                //estimator
158                KFE.bayes ( concat ( dt,ut ) );
159                M.bayes ( concat ( dt,ut ) );
160                SSAT(tK) = M.SSAT;
161               
162                Xt.set_row ( tK,vec ( x,9 ) ); //vec from C-array
163                Dt.set_row ( tK, concat ( dt,ut,vec_1(sqrt(pow(ut(0),2)+pow(ut(1),2))), vec_1(sqrt(pow(dt(0),2)+pow(dt(1),2))) ) );
164                XtE.set_row ( tK,KFEep.mean() );
165                XtM.set_row ( tK,Mep.mean() );
166        }
167
168        it_file fou ( "pmsm_sim.it" );
169
170        fou << Name ( "xth" ) << Xt;
171        fou << Name ( "Dt" ) << Dt;
172        fou << Name ( "xthE" ) << XtE;
173        fou << Name ( "xthM" ) << XtM;
174        fou << Name ( "SSAT" ) << SSAT;
175        //Exit program:
176
177        char tmpstr[200];
178        sprintf(tmpstr,"%s/%s","here/","format");
179        ofstream  form(tmpstr);
180        form << "# Experimetal header file"<< endl;
181        dirfile_write(form,"here/",Xt,"X","{isa isb om th }" );
182        dirfile_write ( form,"here",XtM,"XtM","{q1 q2 q3 q4 isa isb om th }" );
183        dirfile_write ( form,"here",XtE,"XE","{isa isb om th }" );
184        dirfile_write ( form,"here",Dt,"Dt","{isa isb ua ub }" );
185
186        ////////////////
187        // Just Testing
188/*      NcFile nc ( "pmsm_sim2.nc", NcFile::Replace ); // Create and leave in define mode
189        if ( ! nc.is_valid() ) {        std::cerr << "creation of NCFile failed."<<endl;}
190
191        write_to_nc ( nc,Xt,"X","{isa isb om th }" );
192        write_to_nc ( nc,XtM,"XtM","{q1 q2 q3 q4 isa isb om th }" );
193        write_to_nc ( nc,XtE,"XE","{isa isb om th }" );
194        write_to_nc ( nc,Dt,"Dt","{isa isb ua ub }" );*/
195        return 0;
196}
Note: See TracBrowser for help on using the browser.