1 | /*! |
---|
2 | \file |
---|
3 | \brief TR 2525 file for testing Toy Problem of mpf for Covariance Estimation |
---|
4 | \author Vaclav Smidl. |
---|
5 | |
---|
6 | \ingroup PMSM |
---|
7 | |
---|
8 | ----------------------------------- |
---|
9 | BDM++ - C++ library for Bayesian Decision Making under Uncertainty |
---|
10 | |
---|
11 | Using IT++ for numerical operations |
---|
12 | ----------------------------------- |
---|
13 | */ |
---|
14 | |
---|
15 | |
---|
16 | #include <itpp/itbase.h> |
---|
17 | #include <estim/libKF.h> |
---|
18 | #include <estim/libPF.h> |
---|
19 | #include <estim/ekf_templ.h> |
---|
20 | #include <stat/libFN.h> |
---|
21 | |
---|
22 | #include "pmsm.h" |
---|
23 | #include "simulator.h" |
---|
24 | #include "sim_profiles.h" |
---|
25 | |
---|
26 | using namespace itpp; |
---|
27 | |
---|
28 | class IMpmsm_delta : public IMpmsm { |
---|
29 | protected: |
---|
30 | vec ud; |
---|
31 | public: |
---|
32 | IMpmsm_delta() :IMpmsm(),ud ( 2 ) {ud=zeros ( 2 );}; |
---|
33 | //! Set mechanical and electrical variables |
---|
34 | |
---|
35 | void condition ( const vec &val ) {ud = val;} |
---|
36 | vec eval ( const vec &x0, const vec &u0 ) { |
---|
37 | // last state |
---|
38 | double iam = x0 ( 0 ); |
---|
39 | double ibm = x0 ( 1 ); |
---|
40 | double omm = x0 ( 2 ); |
---|
41 | double thm = x0 ( 3 ); |
---|
42 | double uam = u0 ( 0 ); |
---|
43 | double ubm = u0 ( 1 ); |
---|
44 | |
---|
45 | vec xk=zeros ( 4 ); |
---|
46 | //ia |
---|
47 | xk ( 0 ) = ( 1.0- Rs/Ls*dt ) * iam + Ypm/Ls*dt*omm * sin ( thm ) + ( uam+ud ( 0 ) ) *dt/Ls; |
---|
48 | //ib |
---|
49 | xk ( 1 ) = ( 1.0- Rs/Ls*dt ) * ibm - Ypm/Ls*dt*omm * cos ( thm ) + ( ubm+ud ( 1 ) ) *dt/Ls; |
---|
50 | //om |
---|
51 | xk ( 2 ) = omm + kp*p*p * Ypm/J*dt* ( ibm * cos ( thm )-iam * sin ( thm ) ) - p/J*dt*Mz; |
---|
52 | //th |
---|
53 | xk ( 3 ) = thm + omm*dt; // <0..2pi> |
---|
54 | if ( xk ( 3 ) >pi ) xk ( 3 )-=2*pi; |
---|
55 | if ( xk ( 3 ) <-pi ) xk ( 3 ) +=2*pi; |
---|
56 | return xk; |
---|
57 | } |
---|
58 | |
---|
59 | }; |
---|
60 | |
---|
61 | |
---|
62 | int main() { |
---|
63 | // Kalman filter |
---|
64 | int Ndat = 8000; //1e6/125 |
---|
65 | double h = 1e-6; |
---|
66 | int Nsimstep = 125; |
---|
67 | int Npart = 20; |
---|
68 | |
---|
69 | mat Rnoise = randn ( 2,Ndat ); |
---|
70 | |
---|
71 | // internal model |
---|
72 | IMpmsm fxu0; |
---|
73 | IMpmsm_delta fxu; |
---|
74 | // Rs Ls dt Fmag(Ypm) kp p J Bf(Mz) |
---|
75 | fxu.set_parameters ( 0.28, 0.003465, Nsimstep*h, 0.1989, 1.5 ,4.0, 0.04, 0.0 ); |
---|
76 | fxu0.set_parameters ( 0.28, 0.003465, Nsimstep*h, 0.1989, 1.5 ,4.0, 0.04, 0.0 ); |
---|
77 | // observation model |
---|
78 | OMpmsm hxu; |
---|
79 | |
---|
80 | vec mu0= "0.0 0.0 0.0 0.0"; |
---|
81 | vec Qdiag ( "0.7 0.7 0.01 0.0001" ); //zdenek: 0.01 0.01 0.0001 0.0001 |
---|
82 | vec Rdiag ( "0.1 0.1" ); //var(diff(xth)) = "0.034 0.034" |
---|
83 | chmat Q ( Qdiag ); |
---|
84 | chmat R ( Rdiag ); |
---|
85 | EKFCh KFE ( rx,ry,ru ); |
---|
86 | KFE.set_parameters ( &fxu0,&hxu,Q,R ); |
---|
87 | KFE.set_est ( mu0, chmat ( ones ( 4 ) ) ); |
---|
88 | |
---|
89 | RV rUd ( "{ud }", "2" ); |
---|
90 | EKFCh_cond KFEp ( rx,ry,ru,rUd ); |
---|
91 | KFEp.set_parameters ( &fxu,&hxu,Q,R ); |
---|
92 | KFEp.set_est ( mu0, chmat ( ones ( 4 ) ) ); |
---|
93 | |
---|
94 | mlnorm<ldmat> evolUd ( rUd,rUd ); |
---|
95 | MPF<EKFCh_cond> M ( rx,rUd,evolUd,evolUd,Npart,KFEp ); |
---|
96 | // initialize |
---|
97 | vec Ud0="0 0"; |
---|
98 | evolUd.set_parameters ( eye ( 2 ), vec_2 ( 0.0,0.0 ), ldmat ( 10.0*eye ( 2 ) ) ); |
---|
99 | evolUd.condition ( Ud0 ); |
---|
100 | epdf& pfinit=evolUd._epdf(); |
---|
101 | M.set_est ( pfinit ); |
---|
102 | evolUd.set_parameters ( eye ( 2 ), vec_2 ( 0.0,0.0 ), ldmat ( 0.005*eye ( 2 ) ) ); |
---|
103 | |
---|
104 | mat Xt=zeros ( Ndat ,4 ); //true state from simulator |
---|
105 | mat Dt=zeros ( Ndat,2+2 ); //observation |
---|
106 | mat XtE=zeros ( Ndat, 4 ); |
---|
107 | mat Qtr=zeros ( Ndat, 4 ); |
---|
108 | mat XtM=zeros ( Ndat,2+4 ); //W + x |
---|
109 | mat XtMTh=zeros ( Ndat,1 ); |
---|
110 | |
---|
111 | // SET SIMULATOR |
---|
112 | pmsmsim_set_parameters ( 0.28,0.003465,0.1989,0.0,4,1.5,0.04, 200., 3e-6, h ); |
---|
113 | vec dt ( 2 ); |
---|
114 | vec ut ( 2 ); |
---|
115 | vec xt ( 4 ); |
---|
116 | vec xtm=zeros ( 4 ); |
---|
117 | double Ww=0.0; |
---|
118 | vec vecW="1 2 4 8 4 2 0 -4 -9 -16 -4 0 0 0"; |
---|
119 | vecW*=10.0; |
---|
120 | |
---|
121 | it_file RD("tec0006ALL.it"); |
---|
122 | mat RIsx, RIsy, RUsx, RUsy; |
---|
123 | RD >> Name("Isx") >> RIsx; |
---|
124 | RD >> Name("Isy") >> RIsy; |
---|
125 | RD >> Name("Usx") >> RUsx; |
---|
126 | RD >> Name("Usy") >> RUsy; |
---|
127 | |
---|
128 | for ( int tK=1;tK<Ndat;tK++ ) { |
---|
129 | //Number of steps of a simulator for one step of Kalman |
---|
130 | for ( int ii=0; ii<Nsimstep;ii++ ) { |
---|
131 | //simulator |
---|
132 | pmsmsim_noreg_step ( RUsx(125*tK+ii) , RUsy(125*tK+ii)); |
---|
133 | }; |
---|
134 | ut ( 0 ) = RUsx(125*tK); |
---|
135 | ut ( 1 ) = RUsy(125*tK); |
---|
136 | dt ( 0 ) = RIsx(125*tK); |
---|
137 | dt ( 1 ) = RIsy(125*tK); |
---|
138 | xt = vec ( x,4 ); |
---|
139 | |
---|
140 | //estimator |
---|
141 | KFE.bayes ( concat ( dt,ut ) ); |
---|
142 | M.bayes ( concat ( dt,ut ) ); |
---|
143 | |
---|
144 | Xt.set_row ( tK, xt ); //vec from C-array |
---|
145 | Dt.set_row ( tK, concat ( dt,ut)); |
---|
146 | XtE.set_row ( tK,KFE._e()->mean() ); |
---|
147 | XtM.set_row ( tK,M._e()->mean() ); |
---|
148 | // correction for theta |
---|
149 | |
---|
150 | { |
---|
151 | double sumSin=0.0; |
---|
152 | double sumCos=0.0; |
---|
153 | vec mea ( 4 ); |
---|
154 | vec* _w; |
---|
155 | |
---|
156 | for ( int p=0; p<Npart;p++ ) { |
---|
157 | mea = M._BM ( p )->_e()->mean(); |
---|
158 | _w = M.__w(); |
---|
159 | sumSin += ( *_w ) ( p ) *sin ( mea ( 3 ) ); |
---|
160 | sumCos += ( *_w ) ( p ) *cos ( mea ( 3 ) ); |
---|
161 | } |
---|
162 | double Th = atan2 ( sumSin, sumCos ); |
---|
163 | |
---|
164 | XtMTh.set_row ( tK,vec_1 ( Th ) ); |
---|
165 | } |
---|
166 | |
---|
167 | } |
---|
168 | |
---|
169 | it_file fou ( "mpf_u_delta_real.it" ); |
---|
170 | |
---|
171 | fou << Name ( "xth" ) << Xt; |
---|
172 | fou << Name ( "Dt" ) << Dt; |
---|
173 | fou << Name ( "xthE" ) << XtE; |
---|
174 | fou << Name ( "xthM" ) << XtM; |
---|
175 | fou << Name ( "xthMTh" ) << XtMTh; |
---|
176 | //Exit program: |
---|
177 | |
---|
178 | return 0; |
---|
179 | } |
---|