1 | /*! |
---|
2 | \file |
---|
3 | \brief TR 2525 file for testing Wishart random walk on PMSM simulator |
---|
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 | |
---|
14 | |
---|
15 | #include <estim/libPF.h> |
---|
16 | #include <estim/ekf_templ.h> |
---|
17 | #include <stat/libFN.h> |
---|
18 | |
---|
19 | #include <stat/loggers_ui.h> |
---|
20 | #include <stat/libEF_ui.h> |
---|
21 | |
---|
22 | #include "../pmsm_ui.h" |
---|
23 | |
---|
24 | using namespace bdm; |
---|
25 | |
---|
26 | int main ( int argc, char* argv[] ) { |
---|
27 | const char *fname; |
---|
28 | if ( argc>1 ) {fname = argv[1]; } |
---|
29 | else { fname = "pmsm_wishart.cfg"; } |
---|
30 | UIFile F ( fname ); |
---|
31 | |
---|
32 | int Ndat; |
---|
33 | int Npart; |
---|
34 | double h = 1e-6; |
---|
35 | int Nsimstep = 125; |
---|
36 | |
---|
37 | vec Qdiag; |
---|
38 | vec Rdiag; |
---|
39 | |
---|
40 | pmsmDS* DS; |
---|
41 | |
---|
42 | double k; |
---|
43 | double l; |
---|
44 | // mpdf* evolQ ; |
---|
45 | try { |
---|
46 | // Kalman filter |
---|
47 | F.lookupValue ( "ndat", Ndat ); |
---|
48 | F.lookupValue ( "Npart",Npart ); |
---|
49 | |
---|
50 | F.lookupValue ( "k", k); |
---|
51 | F.lookupValue ( "l",l); |
---|
52 | |
---|
53 | // UIbuild ( F.lookup ( "Qrw" ),evolQ ); |
---|
54 | Qdiag= getvec ( F.lookup ( "dQ" ) ); //( "1e-6 1e-6 0.001 0.0001" ); //zdenek: 0.01 0.01 0.0001 0.0001 |
---|
55 | Rdiag=getvec ( F.lookup ( "dR" ) );// ( "1e-8 1e-8" ); //var(diff(xth)) = "0.034 0.034" |
---|
56 | |
---|
57 | UIbuild(F.lookup("system"),DS); |
---|
58 | } |
---|
59 | catch UICATCH; |
---|
60 | // internal model |
---|
61 | |
---|
62 | IMpmsm fxu; |
---|
63 | // Rs Ls dt Fmag(Ypm) kp p J Bf(Mz) |
---|
64 | fxu.set_parameters ( 0.28, 0.003465, Nsimstep*h, 0.1989, 1.5 ,4.0, 0.04, 0.0 ); |
---|
65 | // observation model |
---|
66 | OMpmsm hxu; |
---|
67 | |
---|
68 | vec mu0= "0.0 0.0 0.0 0.0"; |
---|
69 | chmat Q ( Qdiag ); |
---|
70 | chmat R ( Rdiag ); |
---|
71 | EKFCh KFE ; |
---|
72 | KFE.set_parameters ( &fxu,&hxu,Q,R ); |
---|
73 | KFE.set_est ( mu0, chmat ( 0.01*eye( 4 ) ) ); |
---|
74 | KFE.set_rv ( rx ); |
---|
75 | |
---|
76 | RV rQ ( "{Q }","16" ); |
---|
77 | EKFCh_chQ KFEp ; |
---|
78 | KFEp.set_parameters ( &fxu,&hxu,Q,R ); |
---|
79 | KFEp.set_est ( mu0, chmat (0.01* eye( 4 ) ) ); |
---|
80 | |
---|
81 | rwiWishartCh* evolQw = new rwiWishartCh; |
---|
82 | evolQw->set_parameters(4, k, sqrt(Qdiag),l); |
---|
83 | MPF<EKFCh_chQ> M; |
---|
84 | M.set_parameters ( evolQw,evolQw,Npart ); |
---|
85 | // initialize |
---|
86 | chmat Ch0(diag(Qdiag)); |
---|
87 | evolQw->condition ( vec(Ch0._Ch()._data(),16) ); //Zdenek default |
---|
88 | M.set_statistics ( evolQw->_e() , &KFEp ); |
---|
89 | // |
---|
90 | |
---|
91 | M.set_rv ( concat ( rQ,rx ) ); |
---|
92 | |
---|
93 | dirfilelog *L; UIbuild ( F.lookup ( "logger" ), L );// ( "exp/mpf_test",100 ); |
---|
94 | |
---|
95 | KFE.set_options ( "logbounds" ); |
---|
96 | KFE.log_add ( L,"KF" ); |
---|
97 | M.set_options ( "logbounds" ); |
---|
98 | M.log_add ( L,"M" ); |
---|
99 | DS->log_add(*L); |
---|
100 | L->init(); |
---|
101 | |
---|
102 | // SET SIMULATOR |
---|
103 | pmsmsim_set_parameters ( 0.28,0.003465,0.1989,0.0,4,1.5,0.04, 200., 3e-6, h ); |
---|
104 | vec dt ( 2 ); |
---|
105 | vec ut ( 2 ); |
---|
106 | vec xt ( 4 ); |
---|
107 | vec xtm=zeros ( 4 ); |
---|
108 | double Ww=0.0; |
---|
109 | |
---|
110 | vec obs(6); |
---|
111 | for ( int tK=1;tK<Ndat;tK++ ) { |
---|
112 | //Number of steps of a simulator for one step of Kalman |
---|
113 | DS->step(); |
---|
114 | DS->getdata(obs); |
---|
115 | |
---|
116 | dt=obs.left(2); |
---|
117 | ut=obs.right(2); |
---|
118 | //estimator |
---|
119 | KFE.bayes ( concat ( dt,ut ) ); |
---|
120 | M.bayes ( concat ( dt,ut ) ); |
---|
121 | |
---|
122 | DS->logit (*L); |
---|
123 | |
---|
124 | KFE.logit ( L ); |
---|
125 | M.logit ( L ); |
---|
126 | L->step(); |
---|
127 | } |
---|
128 | L->finalize(); |
---|
129 | //Exit program: |
---|
130 | |
---|
131 | delete L; |
---|
132 | return 0; |
---|
133 | } |
---|