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 <stat/libFN.h> |
---|
20 | |
---|
21 | #include "pmsm.h" |
---|
22 | #include "simulator.h" |
---|
23 | #include "sim_profiles.h" |
---|
24 | |
---|
25 | using namespace itpp; |
---|
26 | |
---|
27 | //!Extended Kalman filter with unknown \c Q |
---|
28 | class EKFCh_cond : public EKFCh , public BMcond { |
---|
29 | public: |
---|
30 | //! Default constructor |
---|
31 | EKFCh_cond ( RV rx, RV ry,RV ru,RV rC ) :EKFCh ( rx,ry,ru ),BMcond ( rC ) {}; |
---|
32 | void condition ( const vec &val ) { |
---|
33 | pfxu->condition( val ); |
---|
34 | }; |
---|
35 | }; |
---|
36 | |
---|
37 | class IMpmsm_delta : public IMpmsm { |
---|
38 | protected: |
---|
39 | vec ud; |
---|
40 | public: |
---|
41 | IMpmsm_delta() :IMpmsm(),ud(2) {}; |
---|
42 | //! Set mechanical and electrical variables |
---|
43 | |
---|
44 | void condition(const vec &val){ud = val;} |
---|
45 | vec eval ( const vec &x0, const vec &u0 ) { |
---|
46 | // last state |
---|
47 | double iam = x0 ( 0 ); |
---|
48 | double ibm = x0 ( 1 ); |
---|
49 | double omm = x0 ( 2 ); |
---|
50 | double thm = x0 ( 3 ); |
---|
51 | double uam = u0 ( 0 ); |
---|
52 | double ubm = u0 ( 1 ); |
---|
53 | |
---|
54 | vec xk=zeros ( 4 ); |
---|
55 | //ia |
---|
56 | xk ( 0 ) = ( 1.0- Rs/Ls*dt ) * iam + Ypm/Ls*dt*omm * sin ( thm ) + (uam+ud(0))*dt/Ls; |
---|
57 | //ib |
---|
58 | xk ( 1 ) = ( 1.0- Rs/Ls*dt ) * ibm - Ypm/Ls*dt*omm * cos ( thm ) + (ubm+ud(1))*dt/Ls; |
---|
59 | //om |
---|
60 | xk ( 2 ) = omm + kp*p*p * Ypm/J*dt* ( ibm * cos ( thm )-iam * sin ( thm ) ) - p/J*dt*Mz; |
---|
61 | //th |
---|
62 | xk ( 3 ) = thm + omm*dt; // <0..2pi> |
---|
63 | if ( xk ( 3 ) >pi ) xk ( 3 )-=2*pi; |
---|
64 | if ( xk ( 3 ) <-pi ) xk ( 3 ) +=2*pi; |
---|
65 | return xk; |
---|
66 | } |
---|
67 | |
---|
68 | }; |
---|
69 | |
---|
70 | |
---|
71 | int main() { |
---|
72 | // Kalman filter |
---|
73 | int Ndat = 9000; |
---|
74 | double h = 1e-6; |
---|
75 | int Nsimstep = 125; |
---|
76 | int Npart = 200; |
---|
77 | |
---|
78 | // internal model |
---|
79 | IMpmsm_delta fxu; |
---|
80 | // Rs Ls dt Fmag(Ypm) kp p J Bf(Mz) |
---|
81 | fxu.set_parameters ( 0.28, 0.003465, Nsimstep*h, 0.1989, 1.5 ,4.0, 0.04, 0.0 ); |
---|
82 | // observation model |
---|
83 | OMpmsm hxu; |
---|
84 | |
---|
85 | vec mu0= "0.0 0.0 0.0 0.0"; |
---|
86 | vec Qdiag ( "0.6 0.6 0.001 0.000001" ); //zdenek: 0.01 0.01 0.0001 0.0001 |
---|
87 | vec Rdiag ( "1e-8 1e-8" ); //var(diff(xth)) = "0.034 0.034" |
---|
88 | chmat Q ( Qdiag ); |
---|
89 | chmat R ( Rdiag ); |
---|
90 | EKFCh KFE ( rx,ry,ru ); |
---|
91 | KFE.set_parameters ( &fxu,&hxu,Q,R ); |
---|
92 | KFE.set_est ( mu0, chmat ( zeros ( 4 ) ) ); |
---|
93 | |
---|
94 | RV rUd ( "{ud }", "2" ); |
---|
95 | EKFCh_cond KFEp ( rx,ry,ru,rUd ); |
---|
96 | KFEp.set_parameters ( &fxu,&hxu,Q,R ); |
---|
97 | KFEp.set_est ( mu0, chmat ( zeros ( 4 ) ) ); |
---|
98 | |
---|
99 | mlnorm<ldmat> evolUd ( rUd,rUd ); |
---|
100 | MPF<EKFCh_cond> M ( rx,rUd,evolUd,evolUd,Npart,KFEp ); |
---|
101 | // initialize |
---|
102 | vec Ud0="0 0"; |
---|
103 | evolUd.set_parameters ( eye(2), vec_2(0.0,0.0), ldmat(eye(2)) ); |
---|
104 | evolUd.condition (Ud0 ); |
---|
105 | epdf& pfinit=evolUd._epdf(); |
---|
106 | M.set_est ( pfinit ); |
---|
107 | evolUd.set_parameters ( eye(2), vec_2(0.0,0.0), ldmat(0.1*eye(2)) ); |
---|
108 | |
---|
109 | mat Xt=zeros ( Ndat ,4 ); //true state from simulator |
---|
110 | mat Dt=zeros ( Ndat,2+2 ); //observation |
---|
111 | mat XtE=zeros ( Ndat, 4 ); |
---|
112 | mat Qtr=zeros ( Ndat, 4 ); |
---|
113 | mat XtM=zeros ( Ndat,2+4 ); //W + x |
---|
114 | |
---|
115 | // SET SIMULATOR |
---|
116 | pmsmsim_set_parameters ( 0.28,0.003465,0.1989,0.0,4,1.5,0.04, 200., 3e-6, h ); |
---|
117 | vec dt ( 2 ); |
---|
118 | vec ut ( 2 ); |
---|
119 | vec xt ( 4 ); |
---|
120 | vec xtm=zeros(4); |
---|
121 | double Ww=0.0; |
---|
122 | vec vecW="1 2 4 9 4 2 0 -4 -9 -16 -4 0 0 0"; |
---|
123 | vecW*=10.0; |
---|
124 | |
---|
125 | for ( int tK=1;tK<Ndat;tK++ ) { |
---|
126 | //Number of steps of a simulator for one step of Kalman |
---|
127 | for ( int ii=0; ii<Nsimstep;ii++ ) { |
---|
128 | //simulator |
---|
129 | sim_profile_vec01t(Ww,vecW); |
---|
130 | pmsmsim_step ( Ww ); |
---|
131 | }; |
---|
132 | ut(0) = KalmanObs[0]; |
---|
133 | ut(1) = KalmanObs[1]; |
---|
134 | dt(0) = KalmanObs[2]; |
---|
135 | dt(1) = KalmanObs[3]; |
---|
136 | xt = vec(x,4); |
---|
137 | |
---|
138 | //estimator |
---|
139 | KFE.bayes ( concat ( dt,ut ) ); |
---|
140 | M.bayes ( concat ( dt,ut ) ); |
---|
141 | |
---|
142 | Xt.set_row ( tK, xt); //vec from C-array |
---|
143 | Dt.set_row ( tK, concat ( dt,ut)); |
---|
144 | Qtr.set_row ( tK, Qdiag); |
---|
145 | XtE.set_row ( tK,KFE._e()->mean() ); |
---|
146 | XtM.set_row ( tK,M._e()->mean() ); |
---|
147 | } |
---|
148 | |
---|
149 | it_file fou ( "mpf_u_delta.it" ); |
---|
150 | |
---|
151 | fou << Name ( "xth" ) << Xt; |
---|
152 | fou << Name ( "Dt" ) << Dt; |
---|
153 | fou << Name ( "Qtr" ) << Qtr; |
---|
154 | fou << Name ( "xthE" ) << XtE; |
---|
155 | fou << Name ( "xthM" ) << XtM; |
---|
156 | //Exit program: |
---|
157 | |
---|
158 | return 0; |
---|
159 | } |
---|