root/pmsm/sim_var.cpp @ 81

Revision 81, 4.6 kB (checked in by smidl, 16 years ago)

opravy + pridani simulace kovarianci

  • 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
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
25
26void set_simulator_t ( double &Ww ) {
27
28        if ( t>0.0002 ) x[8]=1.2;  // 1A //0.2ZP
29        if ( t>0.4 ) x[8]=10.8; // 9A
30        if ( t>0.6 ) x[8]=25.2;  // 21A
31
32        if ( t>0.7 ) Ww=2.*M_PI*10.;
33        if ( t>1.0 ) x[8]=1.2;  // 1A
34        if ( t>1.2 ) x[8]=10.8; // 9A
35        if ( t>1.4 ) x[8]=25.2;  // 21A
36
37        if ( t>1.6 ) Ww=2.*M_PI*50.;
38        if ( t>1.9 ) x[8]=1.2;  // 1A
39        if ( t>2.1 ) x[8]=10.8; // 9A
40        if ( t>2.3 ) x[8]=25.2;  // 21A
41
42        if ( t>2.5 ) Ww=2.*M_PI*100;
43        if ( t>2.8 ) x[8]=1.2;  // 1A
44        if ( t>3.0 ) x[8]=10.8; // 9A
45        if ( t>3.2 ) x[8]=25.2;  // 21A
46
47        if ( t>3.4 ) Ww=2.*M_PI*150;
48        if ( t>3.7 ) x[8]=1.2;  // 1A
49        if ( t>3.9 ) x[8]=10.8; // 9A
50        if ( t>4.1 ) x[8]=25.2;  // 21A
51
52        if ( t>4.3 ) Ww=2.*M_PI*0;
53        if ( t>4.8 ) x[8]=-1.2;  // 1A
54        if ( t>5.0 ) x[8]=-10.8; // 9A
55        if ( t>5.2 ) x[8]=-25.2;  // 21A
56
57        if ( t>5.4 ) Ww=2.*M_PI* ( -10. );
58        if ( t>5.7 ) x[8]=-1.2;  // 1A
59        if ( t>5.9 ) x[8]=-10.8; // 9A
60        if ( t>6.1 ) x[8]=-25.2;  // 21A
61
62        if ( t>6.3 ) Ww=2.*M_PI* ( -50. );
63        if ( t>6.7 ) x[8]=-1.2;  // 1A
64        if ( t>6.9 ) x[8]=-10.8; // 9A
65        if ( t>7.1 ) x[8]=-25.2;  // 21A
66
67        if ( t>7.3 ) Ww=2.*M_PI* ( -100. );
68        if ( t>7.7 ) x[8]=-1.2;  // 1A
69        if ( t>7.9 ) x[8]=-10.8; // 9A
70        if ( t>8.1 ) x[8]=-25.2;  // 21A
71        if ( t>8.3 ) x[8]=10.8; // 9A
72        if ( t>8.5 ) x[8]=25.2;  // 21A
73
74        x[8]=0.0;
75}
76
77int main() {
78        // Kalman filter
79        int Ndat = 90000;
80        double h = 1e-6;
81        int Nsimstep = 125;
82
83        mat Xt=zeros ( Ndat ,9 ); //true state from simulator
84        mat XtM=zeros ( Ndat ,4 ); //true state from simulator
85        mat XtF=zeros ( Ndat ,4 ); //true state from simulator
86        mat XtO=zeros ( Ndat ,4 ); //true state from simulator
87        mat Dt=zeros ( Ndat,4 ); //observation
88        mat Qrec=zeros ( Ndat, 4*4 ); //full covariance matrix
89
90        // SET SIMULATOR
91        pmsmsim_set_parameters ( 0.28,0.003465,0.1989,0.0,4,1.5,0.04, 200., 3e-6, h );
92        double Ww = 0.0;
93        vec dt ( 2 );
94        vec ut ( 2 );
95        vec xtm=zeros ( 4 );
96        vec xdif=zeros ( 4 );
97        vec xt ( 4 );
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        OMpmsm hxu;
102        mat Qt=zeros ( 4,4 );
103
104        // ESTIMATORS
105        vec mu0= "0.0 0.0 0.0 0.0";
106        vec Qdiag ( "62 66 454 0.03" ); //zdenek: 0.01 0.01 0.0001 0.0001
107        vec Rdiag ( "100 100" ); //var(diff(xth)) = "0.034 0.034"
108        mat Q =diag( Qdiag );
109        mat R =diag ( Rdiag );
110        EKFfull Efix ( rx,ry,ru );
111        Efix.set_est ( mu0, 1*eye ( 4 )  );
112        Efix.set_parameters ( &fxu,&hxu,Q,R);
113
114        EKFfull Eop ( rx,ry,ru );
115        Eop.set_est ( mu0, 1*eye ( 4 ) );
116        Eop.set_parameters ( &fxu,&hxu,Q,R);
117
118        epdf& Efix_ep = Efix._epdf();
119        epdf& Eop_ep = Eop._epdf();
120
121
122        for ( int tK=1;tK<Ndat;tK++ ) {
123                //Number of steps of a simulator for one step of Kalman
124                for ( int ii=0; ii<Nsimstep;ii++ ) {
125                        set_simulator_t ( Ww );
126                        pmsmsim_step ( Ww );
127                };
128                // simulation via deterministic model
129                ut ( 0 ) = KalmanObs[0];
130                ut ( 1 ) = KalmanObs[1];
131                dt ( 0 ) = KalmanObs[2];
132                dt ( 1 ) = KalmanObs[3];
133
134                xt = fxu.eval ( xtm,ut );
135
136                //Results:
137                // in xt we have simulaton according to the model
138                // in x we have "reality"
139
140                xtm ( 0 ) =x[0];xtm ( 1 ) =x[1];xtm ( 2 ) =x[2];xtm ( 3 ) =x[3];
141                xdif=xtm-xt;
142                if ( xdif ( 3 ) >pi ) xdif ( 3 )-=2*pi;
143                if ( xdif ( 3 ) <-pi ) xdif ( 3 ) +=2*pi;
144
145                //Rt = 0.9*Rt + xdif^2
146                Qt*=0.0;
147                Qt += outer_product ( xdif,xdif ); //(x-xt)^2
148                Qrec.set_row ( tK, vec ( Qt._data(),16 ) );
149
150                //ESTIMATE
151                Efix.bayes(concat(dt,ut));
152                //
153                Eop.set_parameters ( &fxu,&hxu,(Qt+1e-16*eye(4)),(1*eye(2)));
154                Eop.bayes(concat(dt,ut));
155               
156                Xt.set_row ( tK,vec ( x,9 ) ); //vec from C-array
157                XtM.set_row ( tK,xt ); //vec from C-array
158                XtF.set_row ( tK,Efix_ep.mean() ); 
159                XtO.set_row ( tK,Eop_ep.mean() ); 
160                Dt.set_row ( tK, concat ( dt,ut ) );
161        }
162
163        char tmpstr[200];
164        sprintf ( tmpstr,"%s/%s","variance/","format" );
165        ofstream  form ( tmpstr );
166        form << "# Experimetal header file"<< endl;
167        dirfile_write ( form,"variance/",Xt,"X","{isa isb om th }" );
168        dirfile_write ( form,"variance/",XtM,"Xsim","{isa isb om th }" );
169        dirfile_write ( form,"variance/",XtO,"XO","{isa isb om th }" );
170        dirfile_write ( form,"variance/",XtF,"XF","{isa isb om th }" );
171        dirfile_write ( form,"variance/",Dt,"D","{isa isb usa usb }" );
172        dirfile_write ( form,"variance",Qrec,"Q","{ }" );
173
174        return 0;
175}
Note: See TracBrowser for help on using the browser.