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 <estim/libKF.h> |
---|
15 | #include <estim/libPF.h> |
---|
16 | #include <stat/libFN.h> |
---|
17 | |
---|
18 | #include <stat/loggers.h> |
---|
19 | |
---|
20 | #include "pmsm.h" |
---|
21 | #include "simulator.h" |
---|
22 | #include "sim_profiles.h" |
---|
23 | |
---|
24 | using namespace itpp; |
---|
25 | int main() { |
---|
26 | // Kalman filter |
---|
27 | int Ndat = 9000; |
---|
28 | double h = 1e-6; |
---|
29 | int Nsimstep = 125; |
---|
30 | int Npart = 50; |
---|
31 | |
---|
32 | dirfilelog L("exp/pmsm_sim2",Ndat); |
---|
33 | |
---|
34 | // internal model |
---|
35 | IMpmsm fxu; |
---|
36 | // Rs Ls dt Fmag(Ypm) kp p J Bf(Mz) |
---|
37 | fxu.set_parameters ( 0.28, 0.003465, Nsimstep*h, 0.1989, 1.5 ,4.0, 0.04, 0.0 ); |
---|
38 | // observation model |
---|
39 | OMpmsm hxu; |
---|
40 | |
---|
41 | vec mu0= "0.0 0.0 0.0 0.0"; |
---|
42 | // vec Qdiag ( "0.01 0.01 0.01 0.0001" ); //zdenek: 0.01 0.01 0.0001 0.0001 |
---|
43 | vec Qdiag ( "0.07 0.056 0.0007 0.0007" ); //zdenek: 0.01 0.01 0.0001 0.0001 |
---|
44 | vec Rdiag ( "0.005 0.005" ); //var(diff(xth)) = "0.034 0.034" |
---|
45 | EKFfull KFE ( rx,ry,ru ); |
---|
46 | KFE.set_est ( mu0, diag ( vec("1 1 1 3.1415") ) ); |
---|
47 | KFE.set_parameters ( &fxu,&hxu,diag(Qdiag),diag(Rdiag)); |
---|
48 | |
---|
49 | |
---|
50 | int X_log = L.add(rx,"X"); |
---|
51 | int E_log = L.add(rx,"E"); |
---|
52 | int V_log = L.add(rx,"V"); |
---|
53 | int U_log = L.add(ru,"U"); |
---|
54 | int R_log = L.add(RV("{_ }","4"),"R"); |
---|
55 | // int O_log = L.add(RV("{_ }","16"),"O"); |
---|
56 | L.init(); |
---|
57 | |
---|
58 | // SET SIMULATOR |
---|
59 | pmsmsim_set_parameters ( 0.28,0.003465,0.1989,0.0,4,1.5,0.04, 200., 3e-6, h ); |
---|
60 | double Ww=0.0; |
---|
61 | vec dt ( 2 ); |
---|
62 | |
---|
63 | vec xm = zeros(4); |
---|
64 | vec xt = zeros(4); |
---|
65 | vec xp = zeros(4); |
---|
66 | vec u=zeros(2); |
---|
67 | ldmat R(eye(4),0.001*ones(4)); |
---|
68 | mat Ch=zeros(4,4); |
---|
69 | fsqmat eCh(4); |
---|
70 | for ( int tK=1;tK<Ndat;tK++ ) { |
---|
71 | //Number of steps of a simulator for one step of Kalman |
---|
72 | for ( int ii=0; ii<Nsimstep;ii++ ) { |
---|
73 | //simulator |
---|
74 | //sim_profile_steps1(Ww); |
---|
75 | sim_profile_2slowrevs(Ww); |
---|
76 | pmsmsim_step ( Ww ); |
---|
77 | }; |
---|
78 | |
---|
79 | u(0)= KalmanObs[0]; |
---|
80 | u(1)= KalmanObs[1]; |
---|
81 | dt(0)= KalmanObs[2]; |
---|
82 | dt(1)= KalmanObs[3]; |
---|
83 | // Try what our model would predict! |
---|
84 | xp=fxu.eval(xm,u); |
---|
85 | |
---|
86 | KFE.bayes(concat(dt,u)); |
---|
87 | // This is simulator prediction |
---|
88 | xt=vec(x,4); //vec from C-array |
---|
89 | //Covariance |
---|
90 | R*=0.7; |
---|
91 | R.opupdt(xt-xp,1.0); |
---|
92 | Ch = diag(sqrt(R._D()))*R._L(); |
---|
93 | //eCh = KFE._e()->_R(); |
---|
94 | eCh = KFE._R(); |
---|
95 | xm = xt; |
---|
96 | L.logit(X_log, xt ); |
---|
97 | L.logit(U_log, u ); |
---|
98 | L.logit(R_log, diag(Ch.T()*Ch) ); //3.33=1/(1-0.7) |
---|
99 | L.logit(V_log, diag(eCh.to_mat()) ); //3.33=1/(1-0.7) |
---|
100 | L.logit(E_log, KFE._e()->mean() ); |
---|
101 | // L.logit(O_log, vec(iCh._data(),16)); //3.33=1/(1-0.7) |
---|
102 | // L.logit(Efix_log, KFEep.mean() ); |
---|
103 | // L.logit(M_log, Mep.mean() ); |
---|
104 | |
---|
105 | L.step(); |
---|
106 | } |
---|
107 | |
---|
108 | L.finalize(); |
---|
109 | L.itsave("xxx.it"); |
---|
110 | return 0; |
---|
111 | } |
---|