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 dut ( 2 ); |
---|
43 | vec dit (2); |
---|
44 | vec xtm=zeros ( 4 ); |
---|
45 | vec xte=zeros ( 4 ); |
---|
46 | vec xdif=zeros ( 4 ); |
---|
47 | vec xt ( 4 ); |
---|
48 | vec ddif=zeros(2); |
---|
49 | IMpmsm fxu; |
---|
50 | // Rs Ls dt Fmag(Ypm) kp p J Bf(Mz) |
---|
51 | fxu.set_parameters ( 0.28, 0.003465, Nsimstep*h, 0.1989, 1.5 ,4.0, 0.04, 0.0 ); |
---|
52 | OMpmsm hxu; |
---|
53 | mat Qt=zeros ( 4,4 ); |
---|
54 | mat Rt=zeros ( 2,2 ); |
---|
55 | |
---|
56 | // ESTIMATORS |
---|
57 | vec mu0= "0.0 0.0 0.0 0.0"; |
---|
58 | vec Qdiag ( "62 66 454 0.03" ); //zdenek: 0.01 0.01 0.0001 0.0001 |
---|
59 | vec Rdiag ( "100 100" ); //var(diff(xth)) = "0.034 0.034" |
---|
60 | mat Q =diag( Qdiag ); |
---|
61 | mat R =diag ( Rdiag ); |
---|
62 | EKFfull Efix ( rx,ry,ru ); |
---|
63 | Efix.set_est ( mu0, 1*eye ( 4 ) ); |
---|
64 | Efix.set_parameters ( &fxu,&hxu,Q,R); |
---|
65 | |
---|
66 | EKFfull Eop ( rx,ry,ru ); |
---|
67 | Eop.set_est ( mu0, 1*eye ( 4 ) ); |
---|
68 | Eop.set_parameters ( &fxu,&hxu,Q,R); |
---|
69 | |
---|
70 | EKFfull Edi ( rx,ry,ru ); |
---|
71 | Edi.set_est ( mu0, 1*eye ( 4 ) ); |
---|
72 | Edi.set_parameters ( &fxu,&hxu,Q,R); |
---|
73 | |
---|
74 | const epdf& Efix_ep = Efix._epdf(); |
---|
75 | const epdf& Eop_ep = Eop._epdf(); |
---|
76 | const epdf& Edi_ep = Edi._epdf(); |
---|
77 | |
---|
78 | //LOG |
---|
79 | RV rQ( "{Q }", "16"); |
---|
80 | RV rR( "{R }", "4"); |
---|
81 | RV rUD( "{u_isa u_isb i_isa i_isb }", ones_i(4)); |
---|
82 | RV rDu("{dux duy duxf duyf }",ones_i(4)); |
---|
83 | RV rDi("{disa disb }",ones_i(2)); |
---|
84 | int X_log = L.add(rx,"X"); |
---|
85 | int Efix_log = L.add(rx,"XF"); |
---|
86 | int Eop_log = L.add(rx,"XO"); |
---|
87 | int Edi_log = L.add(rx,"XD"); |
---|
88 | int Q_log = L.add(rQ,"Q"); |
---|
89 | int R_log = L.add(rR,"R"); |
---|
90 | int D_log = L.add(rUD,"D"); |
---|
91 | int Du_log = L.add(rDu,"Du"); |
---|
92 | int Di_log = L.add(rDi,"Di"); |
---|
93 | L.init(); |
---|
94 | |
---|
95 | for ( int tK=1;tK<Ndat;tK++ ) { |
---|
96 | //Number of steps of a simulator for one step of Kalman |
---|
97 | for ( int ii=0; ii<Nsimstep;ii++ ) { |
---|
98 | sim_profile_steps1 ( Ww , false); |
---|
99 | pmsmsim_step ( Ww ); |
---|
100 | }; |
---|
101 | // simulation via deterministic model |
---|
102 | ut ( 0 ) = KalmanObs[0]; |
---|
103 | ut ( 1 ) = KalmanObs[1]; |
---|
104 | dt ( 0 ) = KalmanObs[2]; |
---|
105 | dt ( 1 ) = KalmanObs[3]; |
---|
106 | dut ( 0 ) = KalmanObs[4]; |
---|
107 | dut ( 1 ) = KalmanObs[5]; |
---|
108 | dit ( 0 ) = KalmanObs[8]; |
---|
109 | dit ( 1 ) = KalmanObs[9]; |
---|
110 | |
---|
111 | xte = fxu.eval ( xtm,ut ); |
---|
112 | //Results: |
---|
113 | // in xt we have simulation according to the model |
---|
114 | // in x we have "reality" |
---|
115 | xt ( 0 ) =x[0];xt ( 1 ) =x[1];xt ( 2 ) =x[2];xt ( 3 ) =x[3]; |
---|
116 | xdif = xt-xte; //xtm is a copy of x[] |
---|
117 | if (xdif(0)>3.0){ |
---|
118 | cout << "here" <<endl; |
---|
119 | } |
---|
120 | if ( xdif ( 3 ) >pi ) xdif ( 3 )-=2*pi; |
---|
121 | if ( xdif ( 3 ) <-pi ) xdif ( 3 ) +=2*pi; |
---|
122 | |
---|
123 | ddif = hxu.eval(xt,ut) - dit; |
---|
124 | |
---|
125 | //Rt = 0.9*Rt + xdif^2 |
---|
126 | Qt*=0.1; |
---|
127 | Qt += 10*outer_product ( xdif,xdif ); //(x-xt)^2 |
---|
128 | |
---|
129 | if (Qt(0,0)>3.0){ |
---|
130 | cout << "here" <<endl; |
---|
131 | } |
---|
132 | // For future ref. |
---|
133 | xtm = xt; |
---|
134 | |
---|
135 | Rt*=0.1; |
---|
136 | // Rt += 10*outer_product ( ddif,ddif ); //(x-xt)^2 |
---|
137 | |
---|
138 | //ESTIMATE |
---|
139 | Efix.bayes(concat(dt,ut)); |
---|
140 | // |
---|
141 | Eop.set_parameters ( &fxu,&hxu,(Qt+1e-8*eye(4)),(Rt+1e-6*eye(2))); |
---|
142 | Eop.bayes(concat(dt,ut)); |
---|
143 | // |
---|
144 | Edi.set_parameters ( &fxu,&hxu,(diag(diag(Qt))+1e-16*eye(4)), (diag(diag(Rt))+1e-3*eye(2))); |
---|
145 | Edi.bayes(concat(dt,ut)); |
---|
146 | |
---|
147 | //LOG |
---|
148 | L.logit(X_log, vec(x,4)); //vec from C-array |
---|
149 | L.logit(Efix_log, Efix_ep.mean() ); |
---|
150 | L.logit(Eop_log, Eop_ep.mean() ); |
---|
151 | L.logit(Edi_log, Edi_ep.mean() ); |
---|
152 | L.logit(Q_log, vec(Qt._data(),16) ); |
---|
153 | L.logit(R_log, vec(Rt._data(),4) ); |
---|
154 | L.logit(D_log, vec(KalmanObs,4) ); |
---|
155 | L.logit(Du_log, dut ); |
---|
156 | L.logit(Di_log, dit ); |
---|
157 | |
---|
158 | L.step(); |
---|
159 | } |
---|
160 | |
---|
161 | L.finalize(); |
---|
162 | //L.itsave("sim_var.it"); |
---|
163 | |
---|
164 | |
---|
165 | return 0; |
---|
166 | } |
---|