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 <stat/loggers.h> |
---|
22 | |
---|
23 | using namespace itpp; |
---|
24 | //!Extended Kalman filter with unknown \c Q |
---|
25 | |
---|
26 | void 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 | |
---|
77 | int main() { |
---|
78 | // Kalman filter |
---|
79 | int Ndat = 90000; |
---|
80 | double h = 1e-6; |
---|
81 | int Nsimstep = 125; |
---|
82 | |
---|
83 | dirfilelog L("exp/sim_var",1000); |
---|
84 | //memlog L(Ndat); |
---|
85 | |
---|
86 | // SET SIMULATOR |
---|
87 | pmsmsim_set_parameters ( 0.28,0.003465,0.1989,0.0,4,1.5,0.04, 200., 3e-6, h ); |
---|
88 | double Ww = 0.0; |
---|
89 | vec dt ( 2 ); |
---|
90 | vec ut ( 2 ); |
---|
91 | vec xtm=zeros ( 4 ); |
---|
92 | vec xdif=zeros ( 4 ); |
---|
93 | vec xt ( 4 ); |
---|
94 | vec ddif=zeros(2); |
---|
95 | IMpmsm fxu; |
---|
96 | // Rs Ls dt Fmag(Ypm) kp p J Bf(Mz) |
---|
97 | fxu.set_parameters ( 0.28, 0.003465, Nsimstep*h, 0.1989, 1.5 ,4.0, 0.04, 0.0 ); |
---|
98 | OMpmsm hxu; |
---|
99 | mat Qt=zeros ( 4,4 ); |
---|
100 | mat Rt=zeros ( 2,2 ); |
---|
101 | |
---|
102 | // ESTIMATORS |
---|
103 | vec mu0= "0.0 0.0 0.0 0.0"; |
---|
104 | vec Qdiag ( "62 66 454 0.03" ); //zdenek: 0.01 0.01 0.0001 0.0001 |
---|
105 | vec Rdiag ( "100 100" ); //var(diff(xth)) = "0.034 0.034" |
---|
106 | mat Q =diag( Qdiag ); |
---|
107 | mat R =diag ( Rdiag ); |
---|
108 | EKFfull Efix ( rx,ry,ru ); |
---|
109 | Efix.set_est ( mu0, 1*eye ( 4 ) ); |
---|
110 | Efix.set_parameters ( &fxu,&hxu,Q,R); |
---|
111 | |
---|
112 | EKFfull Eop ( rx,ry,ru ); |
---|
113 | Eop.set_est ( mu0, 1*eye ( 4 ) ); |
---|
114 | Eop.set_parameters ( &fxu,&hxu,Q,R); |
---|
115 | |
---|
116 | EKFfull Edi ( rx,ry,ru ); |
---|
117 | Edi.set_est ( mu0, 1*eye ( 4 ) ); |
---|
118 | Edi.set_parameters ( &fxu,&hxu,Q,R); |
---|
119 | |
---|
120 | epdf& Efix_ep = Efix._epdf(); |
---|
121 | epdf& Eop_ep = Eop._epdf(); |
---|
122 | epdf& Edi_ep = Edi._epdf(); |
---|
123 | |
---|
124 | //LOG |
---|
125 | RV rQ("10", "{Q }", "16","0"); |
---|
126 | RV rR("11", "{R }", "4","0"); |
---|
127 | RV rUD("12 13 14 15", "{u_isa u_isb i_isa i_isb }", ones_i(4),zeros_i(4)); |
---|
128 | int X_log = L.add(rx,"X"); |
---|
129 | int Efix_log = L.add(rx,"XF"); |
---|
130 | int Eop_log = L.add(rx,"XO"); |
---|
131 | int Edi_log = L.add(rx,"XD"); |
---|
132 | int Q_log = L.add(rQ,"Q"); |
---|
133 | int R_log = L.add(rR,"R"); |
---|
134 | int D_log = L.add(rUD,"D"); |
---|
135 | L.init(); |
---|
136 | |
---|
137 | for ( int tK=1;tK<Ndat;tK++ ) { |
---|
138 | //Number of steps of a simulator for one step of Kalman |
---|
139 | for ( int ii=0; ii<Nsimstep;ii++ ) { |
---|
140 | set_simulator_t ( Ww ); |
---|
141 | pmsmsim_step ( Ww ); |
---|
142 | }; |
---|
143 | // simulation via deterministic model |
---|
144 | ut ( 0 ) = KalmanObs[0]; |
---|
145 | ut ( 1 ) = KalmanObs[1]; |
---|
146 | dt ( 0 ) = KalmanObs[2]; |
---|
147 | dt ( 1 ) = KalmanObs[3]; |
---|
148 | |
---|
149 | xt = fxu.eval ( xtm,ut ); |
---|
150 | //Results: |
---|
151 | // in xt we have simulaton according to the model |
---|
152 | // in x we have "reality" |
---|
153 | xtm ( 0 ) =x[0];xtm ( 1 ) =x[1];xtm ( 2 ) =x[2];xtm ( 3 ) =x[3]; |
---|
154 | xdif = xtm-xt; |
---|
155 | if ( xdif ( 3 ) >pi ) xdif ( 3 )-=2*pi; |
---|
156 | if ( xdif ( 3 ) <-pi ) xdif ( 3 ) +=2*pi; |
---|
157 | |
---|
158 | ddif = hxu.eval(xtm,ut) - dt; |
---|
159 | |
---|
160 | //Rt = 0.9*Rt + xdif^2 |
---|
161 | Qt*=0.01; |
---|
162 | Qt += outer_product ( xdif,xdif ); //(x-xt)^2 |
---|
163 | Rt*=0.01; |
---|
164 | Rt += outer_product ( ddif,ddif ); //(x-xt)^2 |
---|
165 | |
---|
166 | //ESTIMATE |
---|
167 | Efix.bayes(concat(dt,ut)); |
---|
168 | // |
---|
169 | Eop.set_parameters ( &fxu,&hxu,(Qt+1e-16*eye(4)),(Rt+1e-3*eye(2))); |
---|
170 | Eop.bayes(concat(dt,ut)); |
---|
171 | // |
---|
172 | Edi.set_parameters ( &fxu,&hxu,(diag(diag(Qt))+1e-16*eye(4)), (diag(diag(Rt))+1e-3*eye(2))); |
---|
173 | Edi.bayes(concat(dt,ut)); |
---|
174 | |
---|
175 | //LOG |
---|
176 | L.logit(X_log, vec(x,4)); //vec from C-array |
---|
177 | L.logit(Efix_log, Efix_ep.mean() ); |
---|
178 | L.logit(Eop_log, Eop_ep.mean() ); |
---|
179 | L.logit(Edi_log, Edi_ep.mean() ); |
---|
180 | L.logit(Q_log, vec(Qt._data(),16) ); |
---|
181 | L.logit(R_log, vec(Rt._data(),4) ); |
---|
182 | L.logit(D_log, vec(KalmanObs,4) ); |
---|
183 | |
---|
184 | L.step(false); |
---|
185 | } |
---|
186 | |
---|
187 | L.step(true); |
---|
188 | //L.itsave("sim_var.it"); |
---|
189 | |
---|
190 | |
---|
191 | return 0; |
---|
192 | } |
---|