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 | #include <math/chmat.h> |
---|
18 | |
---|
19 | #include "pmsm.h" |
---|
20 | #include "simulator.h" |
---|
21 | #include "sim_profiles.h" |
---|
22 | |
---|
23 | #include <stat/loggers.h> |
---|
24 | |
---|
25 | using namespace itpp; |
---|
26 | //!Extended Kalman filter with unknown \c Q |
---|
27 | |
---|
28 | int main() { |
---|
29 | // Kalman filter |
---|
30 | int Ndat = 90000; |
---|
31 | double h = 1e-6; |
---|
32 | int Nsimstep = 125; |
---|
33 | |
---|
34 | dirfilelog L("exp/sim_var",1000); |
---|
35 | //memlog L(Ndat); |
---|
36 | |
---|
37 | // SET SIMULATOR |
---|
38 | pmsmsim_set_parameters ( 0.28,0.003465,0.1989,0.0,4,1.5,0.04, 200., 3e-6, h ); |
---|
39 | double Ww = 0.0; |
---|
40 | vec dt ( 2 ); |
---|
41 | vec ut ( 2 ); |
---|
42 | vec xtm=zeros ( 4 ); |
---|
43 | vec xdif=zeros ( 4 ); |
---|
44 | vec xt ( 4 ); |
---|
45 | vec ddif=zeros(2); |
---|
46 | IMpmsm fxu; |
---|
47 | // Rs Ls dt Fmag(Ypm) kp p J Bf(Mz) |
---|
48 | fxu.set_parameters ( 0.28, 0.003465, Nsimstep*h, 0.1989, 1.5 ,4.0, 0.04, 0.0 ); |
---|
49 | OMpmsm hxu; |
---|
50 | mat Qt=zeros ( 4,4 ); |
---|
51 | mat Rt=zeros ( 2,2 ); |
---|
52 | |
---|
53 | // ESTIMATORS |
---|
54 | vec mu0= "0.0 0.0 0.0 0.0"; |
---|
55 | vec Qdiag ( "62 66 454 0.03" ); //zdenek: 0.01 0.01 0.0001 0.0001 |
---|
56 | vec Rdiag ( "100 100" ); //var(diff(xth)) = "0.034 0.034" |
---|
57 | mat Q =diag( Qdiag ); |
---|
58 | mat R =diag ( Rdiag ); |
---|
59 | EKFfull Efix ( rx,ry,ru ); |
---|
60 | Efix.set_est ( mu0, 1*eye ( 4 ) ); |
---|
61 | Efix.set_parameters ( &fxu,&hxu,Q,R); |
---|
62 | |
---|
63 | EKFfull Eop ( rx,ry,ru ); |
---|
64 | Eop.set_est ( mu0, 1*eye ( 4 ) ); |
---|
65 | Eop.set_parameters ( &fxu,&hxu,Q,R); |
---|
66 | |
---|
67 | EKFfull Edi ( rx,ry,ru ); |
---|
68 | Edi.set_est ( mu0, 1*eye ( 4 ) ); |
---|
69 | Edi.set_parameters ( &fxu,&hxu,Q,R); |
---|
70 | |
---|
71 | epdf& Efix_ep = Efix._epdf(); |
---|
72 | epdf& Eop_ep = Eop._epdf(); |
---|
73 | epdf& Edi_ep = Edi._epdf(); |
---|
74 | |
---|
75 | //LOG |
---|
76 | RV rQ("10", "{Q }", "16","0"); |
---|
77 | RV rR("11", "{R }", "4","0"); |
---|
78 | RV rUD("12 13 14 15", "{u_isa u_isb i_isa i_isb }", ones_i(4),zeros_i(4)); |
---|
79 | int X_log = L.add(rx,"X"); |
---|
80 | int Efix_log = L.add(rx,"XF"); |
---|
81 | int Eop_log = L.add(rx,"XO"); |
---|
82 | int Edi_log = L.add(rx,"XD"); |
---|
83 | int Q_log = L.add(rQ,"Q"); |
---|
84 | int R_log = L.add(rR,"R"); |
---|
85 | int D_log = L.add(rUD,"D"); |
---|
86 | L.init(); |
---|
87 | |
---|
88 | for ( int tK=1;tK<Ndat;tK++ ) { |
---|
89 | //Number of steps of a simulator for one step of Kalman |
---|
90 | for ( int ii=0; ii<Nsimstep;ii++ ) { |
---|
91 | sim_profile_steps1 ( Ww , true); |
---|
92 | pmsmsim_step ( Ww ); |
---|
93 | }; |
---|
94 | // simulation via deterministic model |
---|
95 | ut ( 0 ) = KalmanObs[0]; |
---|
96 | ut ( 1 ) = KalmanObs[1]; |
---|
97 | dt ( 0 ) = KalmanObs[2]; |
---|
98 | dt ( 1 ) = KalmanObs[3]; |
---|
99 | |
---|
100 | xt = fxu.eval ( xtm,ut ); |
---|
101 | //Results: |
---|
102 | // in xt we have simulaton according to the model |
---|
103 | // in x we have "reality" |
---|
104 | xtm ( 0 ) =x[0];xtm ( 1 ) =x[1];xtm ( 2 ) =x[2];xtm ( 3 ) =x[3]; |
---|
105 | xdif = xtm-xt; |
---|
106 | if ( xdif ( 3 ) >pi ) xdif ( 3 )-=2*pi; |
---|
107 | if ( xdif ( 3 ) <-pi ) xdif ( 3 ) +=2*pi; |
---|
108 | |
---|
109 | ddif = hxu.eval(xtm,ut) - dt; |
---|
110 | |
---|
111 | //Rt = 0.9*Rt + xdif^2 |
---|
112 | Qt*=0.01; |
---|
113 | Qt += outer_product ( xdif,xdif ); //(x-xt)^2 |
---|
114 | Rt*=0.01; |
---|
115 | Rt += outer_product ( ddif,ddif ); //(x-xt)^2 |
---|
116 | |
---|
117 | //ESTIMATE |
---|
118 | Efix.bayes(concat(dt,ut)); |
---|
119 | // |
---|
120 | Eop.set_parameters ( &fxu,&hxu,(Qt+1e-16*eye(4)),(Rt+1e-3*eye(2))); |
---|
121 | Eop.bayes(concat(dt,ut)); |
---|
122 | // |
---|
123 | Edi.set_parameters ( &fxu,&hxu,(diag(diag(Qt))+1e-16*eye(4)), (diag(diag(Rt))+1e-3*eye(2))); |
---|
124 | Edi.bayes(concat(dt,ut)); |
---|
125 | |
---|
126 | //LOG |
---|
127 | L.logit(X_log, vec(x,4)); //vec from C-array |
---|
128 | L.logit(Efix_log, Efix_ep.mean() ); |
---|
129 | L.logit(Eop_log, Eop_ep.mean() ); |
---|
130 | L.logit(Edi_log, Edi_ep.mean() ); |
---|
131 | L.logit(Q_log, vec(Qt._data(),16) ); |
---|
132 | L.logit(R_log, vec(Rt._data(),4) ); |
---|
133 | L.logit(D_log, vec(KalmanObs,4) ); |
---|
134 | |
---|
135 | L.step(false); |
---|
136 | } |
---|
137 | |
---|
138 | L.step(true); |
---|
139 | //L.itsave("sim_var.it"); |
---|
140 | |
---|
141 | |
---|
142 | return 0; |
---|
143 | } |
---|