1 | /*! |
---|
2 | \file |
---|
3 | \brief Simulation of disturbances in PMSM model, EKF runs with simulated covariances |
---|
4 | \author Vaclav Smidl. |
---|
5 | |
---|
6 | \ingroup PMSM |
---|
7 | ----------------------------------- |
---|
8 | BDM++ - C++ library for Bayesian Decision Making under Uncertainty |
---|
9 | |
---|
10 | Using IT++ for numerical operations |
---|
11 | ----------------------------------- |
---|
12 | */ |
---|
13 | |
---|
14 | |
---|
15 | #include <stat/libFN.h> |
---|
16 | #include <estim/libKF.h> |
---|
17 | //#include <estim/libPF.h> |
---|
18 | #include <math/chmat.h> |
---|
19 | |
---|
20 | #include "pmsm.h" |
---|
21 | #include "simulator.h" |
---|
22 | #include "sim_profiles.h" |
---|
23 | |
---|
24 | #include <stat/loggers.h> |
---|
25 | |
---|
26 | using namespace bdm; |
---|
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_var2",1000); |
---|
35 | //memlog L(Ndat); |
---|
36 | |
---|
37 | |
---|
38 | // SET SIMULATOR |
---|
39 | pmsmsim_set_parameters ( 0.28,0.003465,0.1989,0.0,4,1.5,0.04, 200., 3e-6, h ); |
---|
40 | double Ww = 0.0; |
---|
41 | vec dt ( 2 ); |
---|
42 | vec ut ( 2 ); |
---|
43 | vec utm ( 2 ); |
---|
44 | vec dut ( 2 ); |
---|
45 | vec dit (2); |
---|
46 | vec x2o(2); |
---|
47 | vec xtm=zeros ( 4 ); |
---|
48 | vec xte=zeros ( 4 ); |
---|
49 | vec xdif=zeros ( 4 ); |
---|
50 | vec xt ( 4 ); |
---|
51 | vec ddif=zeros(2); |
---|
52 | IMpmsm2o fxu; |
---|
53 | // Rs Ls dt Fmag(Ypm) kp p J Bf(Mz) |
---|
54 | fxu.set_parameters ( 0.28, 0.003465, Nsimstep*h, 0.1989, 1.5 ,4.0, 0.04, 0.0 ); |
---|
55 | OMpmsm hxu; |
---|
56 | mat Qt=zeros ( 4,4 ); |
---|
57 | mat Rt=zeros ( 2,2 ); |
---|
58 | |
---|
59 | // ESTIMATORS |
---|
60 | vec mu0= "0.0 0.0 0.0 0.0"; |
---|
61 | vec Qdiag ( "62 66 454 0.03" ); //zdenek: 0.01 0.01 0.0001 0.0001 |
---|
62 | vec Rdiag ( "100 100" ); //var(diff(xth)) = "0.034 0.034" |
---|
63 | mat Q =diag( Qdiag ); |
---|
64 | mat R =diag ( Rdiag ); |
---|
65 | EKFfull Efix ( rx,ry,ru ); |
---|
66 | Efix.set_est ( mu0, 1*eye ( 4 ) ); |
---|
67 | Efix.set_parameters ( &fxu,&hxu,Q,R); |
---|
68 | |
---|
69 | EKFfull Eop ( rx,ry,ru ); |
---|
70 | Eop.set_est ( mu0, 1*eye ( 4 ) ); |
---|
71 | Eop.set_parameters ( &fxu,&hxu,Q,R); |
---|
72 | |
---|
73 | EKFfull Edi ( rx,ry,ru ); |
---|
74 | Edi.set_est ( mu0, 1*eye ( 4 ) ); |
---|
75 | Edi.set_parameters ( &fxu,&hxu,Q,R); |
---|
76 | |
---|
77 | const epdf& Efix_ep = Efix._epdf(); |
---|
78 | const epdf& Eop_ep = Eop._epdf(); |
---|
79 | const epdf& Edi_ep = Edi._epdf(); |
---|
80 | |
---|
81 | //LOG |
---|
82 | RV rQ( "{Q }", "16"); |
---|
83 | RV rR( "{R }", "4"); |
---|
84 | RV rUD( "{u_isa u_isb i_isa i_isb }", ones_i(4)); |
---|
85 | RV rDu("{dux duy }",ones_i(2)); |
---|
86 | RV rDi("{disa disb }",ones_i(2)); |
---|
87 | int X_log = L.add(rx,"X"); |
---|
88 | int X2o_log = L.add(rx,"X2o"); |
---|
89 | int Xdf_log = L.add(rx,"Xdf"); |
---|
90 | int Efix_log = L.add(rx,"XF"); |
---|
91 | int Eop_log = L.add(rx,"XO"); |
---|
92 | int Edi_log = L.add(rx,"XD"); |
---|
93 | int Q_log = L.add(rQ,"Q"); |
---|
94 | int R_log = L.add(rR,"R"); |
---|
95 | int D_log = L.add(rUD,"D"); |
---|
96 | int Du_log = L.add(rDu,"Du"); |
---|
97 | int Di_log = L.add(rDi,"Di"); |
---|
98 | L.init(); |
---|
99 | |
---|
100 | for ( int tK=1;tK<Ndat;tK++ ) { |
---|
101 | //Number of steps of a simulator for one step of Kalman |
---|
102 | for ( int ii=0; ii<Nsimstep;ii++ ) { |
---|
103 | sim_profile_steps1 ( Ww , false); |
---|
104 | pmsmsim_step ( Ww ); |
---|
105 | }; |
---|
106 | // simulation via deterministic model |
---|
107 | ut ( 0 ) = KalmanObs[4]; |
---|
108 | ut ( 1 ) = KalmanObs[5]; |
---|
109 | |
---|
110 | x2o = fxu.eval2o(utm-ut); |
---|
111 | utm = ut; |
---|
112 | |
---|
113 | dt ( 0 ) = KalmanObs[2]; |
---|
114 | dt ( 1 ) = KalmanObs[3]; |
---|
115 | dut ( 0 ) = KalmanObs[4]; |
---|
116 | dut ( 1 ) = KalmanObs[5]; |
---|
117 | dit ( 0 ) = KalmanObs[8]; |
---|
118 | dit ( 1 ) = KalmanObs[9]; |
---|
119 | |
---|
120 | xte = fxu.eval ( xtm,ut ); |
---|
121 | //Results: |
---|
122 | // in xt we have simulation according to the model |
---|
123 | // in x we have "reality" |
---|
124 | xt ( 0 ) =x[0];xt ( 1 ) =x[1];xt ( 2 ) =x[2];xt ( 3 ) =x[3]; |
---|
125 | xdif = xt-xte; //xtm is a copy of x[] |
---|
126 | if (xdif(0)>3.0){ |
---|
127 | cout << "here" <<endl; |
---|
128 | } |
---|
129 | if ( xdif ( 3 ) >pi ) xdif ( 3 )-=2*pi; |
---|
130 | if ( xdif ( 3 ) <-pi ) xdif ( 3 ) +=2*pi; |
---|
131 | |
---|
132 | ddif = hxu.eval(xt,ut) - dit; |
---|
133 | |
---|
134 | //Rt = 0.9*Rt + xdif^2 |
---|
135 | Qt*=0.1; |
---|
136 | Qt += 10*outer_product ( xdif,xdif ); //(x-xt)^2 |
---|
137 | |
---|
138 | if (Qt(0,0)>3.0){ |
---|
139 | cout << "here" <<endl; |
---|
140 | } |
---|
141 | // For future ref. |
---|
142 | xtm = xt; |
---|
143 | |
---|
144 | Rt*=0.1; |
---|
145 | // Rt += 10*outer_product ( ddif,ddif ); //(x-xt)^2 |
---|
146 | |
---|
147 | //ESTIMATE |
---|
148 | Efix.bayes(concat(dt,ut)); |
---|
149 | // |
---|
150 | Eop.set_parameters ( &fxu,&hxu,(Qt+1e-8*eye(4)),(Rt+1e-6*eye(2))); |
---|
151 | Eop.bayes(concat(dt,ut)); |
---|
152 | // |
---|
153 | Edi.set_parameters ( &fxu,&hxu,(diag(diag(Qt))+1e-16*eye(4)), (diag(diag(Rt))+1e-3*eye(2))); |
---|
154 | Edi.bayes(concat(dt,ut)); |
---|
155 | |
---|
156 | //LOG |
---|
157 | L.logit(X_log, vec(x,4)); //vec from C-array |
---|
158 | L.logit(X2o_log, x2o); |
---|
159 | L.logit(Xdf_log, xdif); |
---|
160 | L.logit(Efix_log, Efix_ep.mean() ); |
---|
161 | L.logit(Eop_log, Eop_ep.mean() ); |
---|
162 | L.logit(Edi_log, Edi_ep.mean() ); |
---|
163 | L.logit(Q_log, vec(Qt._data(),16) ); |
---|
164 | L.logit(R_log, vec(Rt._data(),4) ); |
---|
165 | L.logit(D_log, vec(KalmanObs,4) ); |
---|
166 | L.logit(Du_log, dut ); |
---|
167 | L.logit(Di_log, dit ); |
---|
168 | |
---|
169 | L.step(); |
---|
170 | } |
---|
171 | |
---|
172 | L.finalize(); |
---|
173 | //L.itsave("sim_var.it"); |
---|
174 | |
---|
175 | |
---|
176 | return 0; |
---|
177 | } |
---|