1 | /*! |
---|
2 | \file |
---|
3 | \brief Voltage U is multiplied by an unknown weight w which is estimated by MPF |
---|
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 |
---|
28 | class EKFCh_cond : public EKFCh , public BMcond { |
---|
29 | public: |
---|
30 | //! condition on value of pfxu |
---|
31 | void condition ( const vec &val ) { |
---|
32 | pfxu->condition ( val ); |
---|
33 | }; |
---|
34 | }; |
---|
35 | |
---|
36 | class IMpmsm_w : public IMpmsm { |
---|
37 | protected: |
---|
38 | double w; |
---|
39 | public: |
---|
40 | IMpmsm_w() :IMpmsm(),w ( 1.0 ) {}; |
---|
41 | //! Set mechanical and electrical variables |
---|
42 | |
---|
43 | void condition ( const vec &val ) {w = val ( 0 );} |
---|
44 | vec eval ( const vec &x0, const vec &u0 ) { |
---|
45 | // last state |
---|
46 | double iam = x0 ( 0 ); |
---|
47 | double ibm = x0 ( 1 ); |
---|
48 | double omm = x0 ( 2 ); |
---|
49 | double thm = x0 ( 3 ); |
---|
50 | double uam = u0 ( 0 ); |
---|
51 | double ubm = u0 ( 1 ); |
---|
52 | |
---|
53 | vec xk=zeros ( 4 ); |
---|
54 | //ia |
---|
55 | xk ( 0 ) = ( 1.0- Rs/Ls*dt ) * iam + Ypm/Ls*dt*omm * sin ( thm ) + w* uam*dt/Ls; |
---|
56 | //ib |
---|
57 | xk ( 1 ) = ( 1.0- Rs/Ls*dt ) * ibm - Ypm/Ls*dt*omm * cos ( thm ) + w* ubm*dt/Ls; |
---|
58 | //om |
---|
59 | xk ( 2 ) = omm + kp*p*p * Ypm/J*dt* ( ibm * cos ( thm )-iam * sin ( thm ) ) - p/J*dt*Mz; |
---|
60 | //th |
---|
61 | xk ( 3 ) = thm + omm*dt; // <0..2pi> |
---|
62 | if ( xk ( 3 ) >pi ) xk ( 3 )-=2*pi; |
---|
63 | if ( xk ( 3 ) <-pi ) xk ( 3 ) +=2*pi; |
---|
64 | return xk; |
---|
65 | } |
---|
66 | |
---|
67 | }; |
---|
68 | |
---|
69 | |
---|
70 | int main() { |
---|
71 | // Kalman filter |
---|
72 | int Ndat = 9000; |
---|
73 | double h = 1e-6; |
---|
74 | int Nsimstep = 125; |
---|
75 | int Npart = 200; |
---|
76 | |
---|
77 | mat Rnoise = randn ( 2,Ndat ); |
---|
78 | |
---|
79 | // internal model |
---|
80 | IMpmsm fxu0; |
---|
81 | IMpmsm_w fxu; |
---|
82 | // Rs Ls dt Fmag(Ypm) kp p J Bf(Mz) |
---|
83 | fxu.set_parameters ( 0.28, 0.003465, Nsimstep*h, 0.1989, 1.5 ,4.0, 0.04, 0.0 ); |
---|
84 | fxu0.set_parameters ( 0.28, 0.003465, Nsimstep*h, 0.1989, 1.5 ,4.0, 0.04, 0.0 ); |
---|
85 | // observation model |
---|
86 | OMpmsm hxu; |
---|
87 | |
---|
88 | vec mu0= "0.0 0.0 0.0 0.0"; |
---|
89 | vec Qdiag ( "0.1 0.1 0.001 0.000001" ); //zdenek: 0.01 0.01 0.0001 0.0001 |
---|
90 | vec Rdiag ( "0.1 0.1" ); //var(diff(xth)) = "0.034 0.034" |
---|
91 | chmat Q ( Qdiag ); |
---|
92 | chmat R ( Rdiag ); |
---|
93 | EKFCh KFE; |
---|
94 | KFE.set_parameters ( &fxu0,&hxu,Q,R ); |
---|
95 | KFE.set_est ( mu0, chmat ( ones ( 4 ) ) ); |
---|
96 | |
---|
97 | RV rW ( "{w }" ); |
---|
98 | EKFCh_cond KFEp; |
---|
99 | KFEp.set_parameters ( &fxu,&hxu,Q,R ); |
---|
100 | KFEp.set_est ( mu0, chmat ( ones ( 4 ) ) ); |
---|
101 | |
---|
102 | mgamma_fix evolW ; |
---|
103 | MPF<EKFCh_cond> M (&evolW,&evolW,Npart,KFEp ); |
---|
104 | // initialize |
---|
105 | vec W0="0.5"; |
---|
106 | evolW.set_parameters ( 10.0, W0, 1.0 ); //sigma = 1/10 mu |
---|
107 | evolW.condition ( W0 ); |
---|
108 | epdf& pfinit=evolW._epdf(); |
---|
109 | M.set_est ( pfinit ); |
---|
110 | evolW.set_parameters ( 100.0, W0, 0.99 ); //sigma = 1/10 mu |
---|
111 | |
---|
112 | mat Xt=zeros ( Ndat ,4 ); //true state from simulator |
---|
113 | mat Dt=zeros ( Ndat,2+2 ); //observation |
---|
114 | mat XtE=zeros ( Ndat, 4 ); |
---|
115 | mat Qtr=zeros ( Ndat, 4 ); |
---|
116 | mat XtM=zeros ( Ndat,1+4 ); //W + x |
---|
117 | mat XtMTh=zeros ( Ndat,1 ); //W + x |
---|
118 | |
---|
119 | // SET SIMULATOR |
---|
120 | pmsmsim_set_parameters ( 0.28,0.003465,0.1989,0.0,4,1.5,0.04, 200., 3e-6, h ); |
---|
121 | vec dt ( 2 ); |
---|
122 | vec ut ( 2 ); |
---|
123 | vec xt ( 4 ); |
---|
124 | vec xtm=zeros ( 4 ); |
---|
125 | double Ww=0.0; |
---|
126 | vec vecW="1 2 4 8 4 2 0 -4 -9 -16 -4 0 0 0"; |
---|
127 | vecW*=10.0; |
---|
128 | |
---|
129 | for ( int tK=1;tK<Ndat;tK++ ) { |
---|
130 | //Number of steps of a simulator for one step of Kalman |
---|
131 | for ( int ii=0; ii<Nsimstep;ii++ ) { |
---|
132 | //simulator |
---|
133 | sim_profile_vec01t ( Ww,vecW ); |
---|
134 | pmsmsim_step ( Ww ); |
---|
135 | }; |
---|
136 | ut ( 0 ) = KalmanObs[0]; |
---|
137 | ut ( 1 ) = KalmanObs[1]; |
---|
138 | dt ( 0 ) = KalmanObs[2]+0.3*Rnoise ( 0,t ); |
---|
139 | dt ( 1 ) = KalmanObs[3]+0.3*Rnoise ( 1,t ); |
---|
140 | xt = vec ( x,4 ); |
---|
141 | |
---|
142 | //estimator |
---|
143 | KFE.bayes ( concat ( dt,ut ) ); |
---|
144 | M.bayes ( concat ( dt,ut ) ); |
---|
145 | |
---|
146 | Xt.set_row ( tK, xt ); //vec from C-array |
---|
147 | Dt.set_row ( tK, concat ( dt,ut ) ); |
---|
148 | Qtr.set_row ( tK, Qdiag ); |
---|
149 | XtE.set_row ( tK,KFE._e()->mean() ); |
---|
150 | XtM.set_row ( tK,M._e()->mean() ); |
---|
151 | { |
---|
152 | double sumSin=0.0; |
---|
153 | double sumCos=0.0; |
---|
154 | vec mea ( 4 ); |
---|
155 | vec* _w; |
---|
156 | |
---|
157 | for ( int p=0; p<Npart;p++ ) { |
---|
158 | mea = M._BM ( p )->_e()->mean(); |
---|
159 | _w = M.__w(); |
---|
160 | sumSin += ( *_w ) ( p ) *sin ( mea ( 3 ) ); |
---|
161 | sumCos += ( *_w ) ( p ) *cos ( mea ( 3 ) ); |
---|
162 | } |
---|
163 | double Th = asin ( sumSin ); |
---|
164 | if ( sumCos<0 ) { |
---|
165 | if ( sumSin>0 ) { |
---|
166 | Th = M_PI-Th; |
---|
167 | } |
---|
168 | else { |
---|
169 | Th = -M_PI-Th; |
---|
170 | } |
---|
171 | } |
---|
172 | |
---|
173 | XtMTh.set_row ( tK,vec_1 ( Th ) ); |
---|
174 | } |
---|
175 | } |
---|
176 | |
---|
177 | it_file fou ( "mpf_u_weight.it" ); |
---|
178 | |
---|
179 | fou << Name ( "xth" ) << Xt; |
---|
180 | fou << Name ( "Dt" ) << Dt; |
---|
181 | fou << Name ( "Qtr" ) << Qtr; |
---|
182 | fou << Name ( "xthE" ) << XtE; |
---|
183 | fou << Name ( "xthM" ) << XtM; |
---|
184 | fou << Name ( "xthMTh" ) << XtMTh; |
---|
185 | //Exit program: |
---|
186 | |
---|
187 | return 0; |
---|
188 | } |
---|