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 | |
---|
17 | #include <estim/libKF.h> |
---|
18 | #include <estim/libPF.h> |
---|
19 | #include <stat/libFN.h> |
---|
20 | |
---|
21 | #include "pmsm.h" |
---|
22 | #include "simulator.h" |
---|
23 | #include "sim_profiles.h" |
---|
24 | |
---|
25 | using namespace bdm; |
---|
26 | |
---|
27 | //!Extended Kalman filter with unknown \c Q and delta u |
---|
28 | class EKFCh_du_kQ : public EKFCh , public BMcond { |
---|
29 | chmat Qref; |
---|
30 | public: |
---|
31 | //! Default constructor |
---|
32 | EKFCh_du_kQ ( ) :EKFCh ( ),BMcond (),Qref() {}; |
---|
33 | void set_ref(const chmat &Qref0){Qref=Qref0;} |
---|
34 | void condition ( const vec &val ) { |
---|
35 | pfxu->condition ( val.left(2) ); |
---|
36 | |
---|
37 | Q = Qref*std::abs(val(2)); |
---|
38 | preA.set_submatrix ( dimy+dimx,dimy,Q._Ch() ); |
---|
39 | }; |
---|
40 | }; |
---|
41 | |
---|
42 | class IMpmsm_delta : public IMpmsm { |
---|
43 | protected: |
---|
44 | vec ud; |
---|
45 | public: |
---|
46 | IMpmsm_delta() :IMpmsm(),ud ( 2 ) {ud=zeros ( 2 );}; |
---|
47 | //! Set mechanical and electrical variables |
---|
48 | |
---|
49 | void condition ( const vec &val ) {ud = val;} |
---|
50 | vec eval ( const vec &x0, const vec &u0 ) { |
---|
51 | // last state |
---|
52 | double iam = x0 ( 0 ); |
---|
53 | double ibm = x0 ( 1 ); |
---|
54 | double omm = x0 ( 2 ); |
---|
55 | double thm = x0 ( 3 ); |
---|
56 | double uam = u0 ( 0 ); |
---|
57 | double ubm = u0 ( 1 ); |
---|
58 | |
---|
59 | vec xk=zeros ( 4 ); |
---|
60 | //ia |
---|
61 | xk ( 0 ) = ( 1.0- Rs/Ls*dt ) * iam + Ypm/Ls*dt*omm * sin ( thm ) + ( uam+ud ( 0 ) ) *dt/Ls; |
---|
62 | //ib |
---|
63 | xk ( 1 ) = ( 1.0- Rs/Ls*dt ) * ibm - Ypm/Ls*dt*omm * cos ( thm ) + ( ubm+ud ( 1 ) ) *dt/Ls; |
---|
64 | //om |
---|
65 | xk ( 2 ) = omm;// + kp*p*p * Ypm/J*dt* ( ibm * cos ( thm )-iam * sin ( thm ) ) - p/J*dt*Mz; |
---|
66 | //th |
---|
67 | xk ( 3 ) = thm + omm*dt; // <0..2pi> |
---|
68 | if ( xk ( 3 ) >pi ) xk ( 3 )-=2*pi; |
---|
69 | if ( xk ( 3 ) <-pi ) xk ( 3 ) +=2*pi; |
---|
70 | return xk; |
---|
71 | } |
---|
72 | |
---|
73 | }; |
---|
74 | |
---|
75 | |
---|
76 | int main() { |
---|
77 | // Kalman filter |
---|
78 | int Ndat = 9000; |
---|
79 | double h = 1e-6; |
---|
80 | int Nsimstep = 125; |
---|
81 | int Npart = 20; |
---|
82 | |
---|
83 | mat Rnoise = randn ( 2,Ndat ); |
---|
84 | |
---|
85 | // internal model |
---|
86 | IMpmsm2o fxu0; |
---|
87 | IMpmsm_delta fxu; |
---|
88 | // Rs Ls dt Fmag(Ypm) kp p J Bf(Mz) |
---|
89 | fxu.set_parameters ( 0.28, 0.003465, Nsimstep*h, 0.1989, 1.5 ,4.0, 0.04, 0.0 ); |
---|
90 | fxu0.set_parameters ( 0.28, 0.003465, Nsimstep*h, 0.1989, 1.5 ,4.0, 0.04, 0.0 ); |
---|
91 | // observation model |
---|
92 | OMpmsm hxu; |
---|
93 | |
---|
94 | vec mu0= "0.0 0.0 0.0 0.0"; |
---|
95 | vec Qdiag ( "0.4 0.4 0.1 0.000001" ); //zdenek: 0.01 0.01 0.0001 0.0001 |
---|
96 | vec Rdiag ( "0.1 0.1" ); //var(diff(xth)) = "0.034 0.034" |
---|
97 | chmat Q ( Qdiag ); |
---|
98 | mat Q2o=diag(Qdiag); |
---|
99 | chmat R ( Rdiag ); |
---|
100 | EKFCh KFE ; |
---|
101 | KFE.set_parameters ( &fxu0,&hxu,Q,R ); |
---|
102 | KFE.set_est ( mu0, chmat ( ones ( 4 ) ) ); |
---|
103 | |
---|
104 | RV rUd ( "{ud k}", "2 1" ); |
---|
105 | EKFCh_du_kQ KFEp ; |
---|
106 | KFEp.set_parameters ( &fxu,&hxu,Q,R ); |
---|
107 | KFEp.set_ref(Q); |
---|
108 | KFEp.set_est ( mu0, chmat ( ones ( 4 ) ) ); |
---|
109 | |
---|
110 | mlnorm<ldmat> evolUd ; |
---|
111 | MPF<EKFCh_du_kQ> M ( &evolUd,&evolUd,Npart,KFEp ); |
---|
112 | // initialize |
---|
113 | vec Ud0="0 0 1.0"; |
---|
114 | evolUd.set_parameters ( eye ( 3 ), zeros(3), ldmat ( vec( "1e-4 1e-4 10e-4" ))); |
---|
115 | evolUd.condition ( Ud0 ); |
---|
116 | epdf& pfinit=evolUd._epdf(); |
---|
117 | M.set_est ( pfinit ); |
---|
118 | evolUd.set_parameters ( eye ( 3 ), zeros(3), ldmat ( vec(" 7e-4 7e-4 1e-5" ))); |
---|
119 | |
---|
120 | mat Xt=zeros ( Ndat ,4 ); //true state from simulator |
---|
121 | mat Dt=zeros ( Ndat,2+2+2 ); //observation |
---|
122 | mat XtE=zeros ( Ndat, 4 ); |
---|
123 | mat Qtr=zeros ( Ndat, 4 ); |
---|
124 | mat XtM=zeros ( Ndat, 3+4 ); //W + x |
---|
125 | mat XtMTh=zeros ( Ndat,1 ); |
---|
126 | |
---|
127 | // SET SIMULATOR |
---|
128 | pmsmsim_set_parameters ( 0.28,0.003465,0.1989,0.0,4,1.5,0.04, 200., 3e-6, h ); |
---|
129 | vec dt ( 2 ); |
---|
130 | vec ut ( 2 ); |
---|
131 | vec utm ( 2 ); |
---|
132 | vec xt ( 4 ); |
---|
133 | vec xtm=zeros ( 4 ); |
---|
134 | double Ww=0.0; |
---|
135 | vec vecW="1 2 4 8 4 2 0 -4 -9 -16 -4 0 0 0"; |
---|
136 | vecW*=10.0; |
---|
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 | //simulator |
---|
142 | sim_profile_vec01t ( Ww,vecW ); |
---|
143 | pmsmsim_step ( Ww ); |
---|
144 | }; |
---|
145 | ut ( 0 ) = KalmanObs[0]; |
---|
146 | ut ( 1 ) = KalmanObs[1]; |
---|
147 | dt ( 0 ) = KalmanObs[2]+0.3*Rnoise ( 0,t ); |
---|
148 | dt ( 1 ) = KalmanObs[3]+0.3*Rnoise ( 1,t ); |
---|
149 | xt = vec ( x,4 ); |
---|
150 | |
---|
151 | vec x2o = fxu0.eval2o(utm-ut); |
---|
152 | utm =ut; |
---|
153 | |
---|
154 | Q2o*=0.99; |
---|
155 | Q2o+=outer_product(x2o,x2o); |
---|
156 | //estimator |
---|
157 | KFE.bayes ( concat ( dt,ut ) ); |
---|
158 | for (int ii=0; ii<Npart; ii++){dynamic_cast<EKFCh_du_kQ*>(M._BM(ii))->set_ref(chmat(elem_mult(Qdiag,Qdiag)));} |
---|
159 | M.bayes ( concat ( dt,ut ) ); |
---|
160 | |
---|
161 | Xt.set_row ( tK, xt ); //vec from C-array |
---|
162 | Dt.set_row ( tK, concat ( dt,ut.left(2), vec_2 ( KalmanObs[4],KalmanObs[5] ) ) ); |
---|
163 | Qtr.set_row ( tK, Qdiag ); |
---|
164 | XtE.set_row ( tK,KFE._e()->mean() ); |
---|
165 | XtM.set_row ( tK,M._e()->mean() ); |
---|
166 | // correction for theta |
---|
167 | |
---|
168 | { |
---|
169 | double sumSin=0.0; |
---|
170 | double sumCos=0.0; |
---|
171 | vec mea ( 4 ); |
---|
172 | vec* _w; |
---|
173 | |
---|
174 | for ( int p=0; p<Npart;p++ ) { |
---|
175 | mea = M._BM ( p )->_e()->mean(); |
---|
176 | _w = M.__w(); |
---|
177 | sumSin += ( *_w ) ( p ) *sin ( mea ( 3 ) ); |
---|
178 | sumCos += ( *_w ) ( p ) *cos ( mea ( 3 ) ); |
---|
179 | } |
---|
180 | double Th = atan2 ( sumSin, sumCos ); |
---|
181 | |
---|
182 | XtMTh.set_row ( tK,vec_1 ( Th ) ); |
---|
183 | } |
---|
184 | |
---|
185 | } |
---|
186 | |
---|
187 | it_file fou ( "mpf_u_delta.it" ); |
---|
188 | |
---|
189 | fou << Name ( "xth" ) << Xt; |
---|
190 | fou << Name ( "Dt" ) << Dt; |
---|
191 | fou << Name ( "Qtr" ) << Qtr; |
---|
192 | fou << Name ( "xthE" ) << XtE; |
---|
193 | fou << Name ( "xthM" ) << XtM; |
---|
194 | fou << Name ( "xthMTh" ) << XtMTh; |
---|
195 | //Exit program: |
---|
196 | |
---|
197 | return 0; |
---|
198 | } |
---|