root/pmsm/sim_var.cpp @ 114

Revision 108, 4.8 kB (checked in by smidl, 16 years ago)

Fixes for compilation on Win32 platform

  • 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 <stat/libFN.h>
15#include <estim/libKF.h>
16//#include <estim/libPF.h>
17#include <math/chmat.h>
18
19#include "pmsm.h"
20#include "simulator.h"
21
22#include <stat/loggers.h>
23
24using namespace itpp;
25//!Extended Kalman filter with unknown \c Q
26
27void set_simulator_t ( double &Ww ) {
28
29        if ( t>0.0002 ) x[8]=1.2;  // 1A //0.2ZP
30        if ( t>0.4 ) x[8]=10.8; // 9A
31        if ( t>0.6 ) x[8]=25.2;  // 21A
32
33        if ( t>0.7 ) Ww=2.*M_PI*10.;
34        if ( t>1.0 ) x[8]=1.2;  // 1A
35        if ( t>1.2 ) x[8]=10.8; // 9A
36        if ( t>1.4 ) x[8]=25.2;  // 21A
37
38        if ( t>1.6 ) Ww=2.*M_PI*50.;
39        if ( t>1.9 ) x[8]=1.2;  // 1A
40        if ( t>2.1 ) x[8]=10.8; // 9A
41        if ( t>2.3 ) x[8]=25.2;  // 21A
42
43        if ( t>2.5 ) Ww=2.*M_PI*100;
44        if ( t>2.8 ) x[8]=1.2;  // 1A
45        if ( t>3.0 ) x[8]=10.8; // 9A
46        if ( t>3.2 ) x[8]=25.2;  // 21A
47
48        if ( t>3.4 ) Ww=2.*M_PI*150;
49        if ( t>3.7 ) x[8]=1.2;  // 1A
50        if ( t>3.9 ) x[8]=10.8; // 9A
51        if ( t>4.1 ) x[8]=25.2;  // 21A
52
53        if ( t>4.3 ) Ww=2.*M_PI*0;
54        if ( t>4.8 ) x[8]=-1.2;  // 1A
55        if ( t>5.0 ) x[8]=-10.8; // 9A
56        if ( t>5.2 ) x[8]=-25.2;  // 21A
57
58        if ( t>5.4 ) Ww=2.*M_PI* ( -10. );
59        if ( t>5.7 ) x[8]=-1.2;  // 1A
60        if ( t>5.9 ) x[8]=-10.8; // 9A
61        if ( t>6.1 ) x[8]=-25.2;  // 21A
62
63        if ( t>6.3 ) Ww=2.*M_PI* ( -50. );
64        if ( t>6.7 ) x[8]=-1.2;  // 1A
65        if ( t>6.9 ) x[8]=-10.8; // 9A
66        if ( t>7.1 ) x[8]=-25.2;  // 21A
67
68        if ( t>7.3 ) Ww=2.*M_PI* ( -100. );
69        if ( t>7.7 ) x[8]=-1.2;  // 1A
70        if ( t>7.9 ) x[8]=-10.8; // 9A
71        if ( t>8.1 ) x[8]=-25.2;  // 21A
72        if ( t>8.3 ) x[8]=10.8; // 9A
73        if ( t>8.5 ) x[8]=25.2;  // 21A
74
75        x[8]=0.0;
76}
77
78int main() {
79        // Kalman filter
80        int Ndat = 90000;
81        double h = 1e-6;
82        int Nsimstep = 125;
83
84        dirfilelog L("exp/sim_var",1000);
85        //memlog L(Ndat);
86       
87        // SET SIMULATOR
88        pmsmsim_set_parameters ( 0.28,0.003465,0.1989,0.0,4,1.5,0.04, 200., 3e-6, h );
89        double Ww = 0.0;
90        vec dt ( 2 );
91        vec ut ( 2 );
92        vec xtm=zeros ( 4 );
93        vec xdif=zeros ( 4 );
94        vec xt ( 4 );
95        vec ddif=zeros(2);
96        IMpmsm fxu;
97        //                  Rs    Ls        dt           Fmag(Ypm) kp   p    J     Bf(Mz)
98        fxu.set_parameters ( 0.28, 0.003465, Nsimstep*h, 0.1989,   1.5 ,4.0, 0.04, 0.0 );
99        OMpmsm hxu;
100        mat Qt=zeros ( 4,4 );
101        mat Rt=zeros ( 2,2 );
102
103        // ESTIMATORS
104        vec mu0= "0.0 0.0 0.0 0.0";
105        vec Qdiag ( "62 66 454 0.03" ); //zdenek: 0.01 0.01 0.0001 0.0001
106        vec Rdiag ( "100 100" ); //var(diff(xth)) = "0.034 0.034"
107        mat Q =diag( Qdiag );
108        mat R =diag ( Rdiag );
109        EKFfull Efix ( rx,ry,ru );
110        Efix.set_est ( mu0, 1*eye ( 4 )  );
111        Efix.set_parameters ( &fxu,&hxu,Q,R);
112
113        EKFfull Eop ( rx,ry,ru );
114        Eop.set_est ( mu0, 1*eye ( 4 ) );
115        Eop.set_parameters ( &fxu,&hxu,Q,R);
116
117        EKFfull Edi ( rx,ry,ru );
118        Edi.set_est ( mu0, 1*eye ( 4 ) );
119        Edi.set_parameters ( &fxu,&hxu,Q,R);
120
121        epdf& Efix_ep = Efix._epdf();
122        epdf& Eop_ep = Eop._epdf();
123        epdf& Edi_ep = Edi._epdf();
124       
125        //LOG
126        RV rQ("10", "{Q }", "16","0");
127        RV rR("11", "{R }", "4","0");
128        RV rUD("12 13 14 15", "{u_isa u_isb i_isa i_isb }", ones_i(4),zeros_i(4));
129        int X_log = L.add(rx,"X");
130        int Efix_log = L.add(rx,"XF");
131        int Eop_log = L.add(rx,"XO");
132        int Edi_log = L.add(rx,"XD");
133        int Q_log = L.add(rQ,"Q");
134        int R_log = L.add(rR,"R");
135        int D_log = L.add(rUD,"D");
136        L.init();
137
138        for ( int tK=1;tK<Ndat;tK++ ) {
139                //Number of steps of a simulator for one step of Kalman
140                for ( int ii=0; ii<Nsimstep;ii++ ) {
141                        set_simulator_t ( Ww );
142                        pmsmsim_step ( Ww );
143                };
144                // simulation via deterministic model
145                ut ( 0 ) = KalmanObs[0];
146                ut ( 1 ) = KalmanObs[1];
147                dt ( 0 ) = KalmanObs[2];
148                dt ( 1 ) = KalmanObs[3];
149
150                xt = fxu.eval ( xtm,ut );
151                //Results:
152                // in xt we have simulaton according to the model
153                // in x we have "reality"
154                xtm ( 0 ) =x[0];xtm ( 1 ) =x[1];xtm ( 2 ) =x[2];xtm ( 3 ) =x[3];
155                xdif = xtm-xt;
156                if ( xdif ( 3 ) >pi ) xdif ( 3 )-=2*pi;
157                if ( xdif ( 3 ) <-pi ) xdif ( 3 ) +=2*pi;
158               
159                ddif = hxu.eval(xtm,ut) - dt;
160
161                //Rt = 0.9*Rt + xdif^2
162                Qt*=0.01;
163                Qt += outer_product ( xdif,xdif ); //(x-xt)^2
164                Rt*=0.01;
165                Rt += outer_product ( ddif,ddif ); //(x-xt)^2
166
167                //ESTIMATE
168                Efix.bayes(concat(dt,ut));
169                //
170                Eop.set_parameters ( &fxu,&hxu,(Qt+1e-16*eye(4)),(Rt+1e-3*eye(2)));
171                Eop.bayes(concat(dt,ut));
172                //
173                Edi.set_parameters ( &fxu,&hxu,(diag(diag(Qt))+1e-16*eye(4)), (diag(diag(Rt))+1e-3*eye(2)));
174                Edi.bayes(concat(dt,ut));
175               
176                //LOG
177                L.logit(X_log,  vec(x,4)); //vec from C-array
178                L.logit(Efix_log, Efix_ep.mean() ); 
179                L.logit(Eop_log,        Eop_ep.mean() ); 
180                L.logit(Edi_log,        Edi_ep.mean() ); 
181                L.logit(Q_log,  vec(Qt._data(),16) ); 
182                L.logit(R_log,          vec(Rt._data(),4) ); 
183                L.logit(D_log,  vec(KalmanObs,4) ); 
184               
185                L.step(false);
186        }
187
188        L.step(true);
189        //L.itsave("sim_var.it");       
190       
191
192        return 0;
193}
Note: See TracBrowser for help on using the browser.