1 | /*! |
---|
2 | \file |
---|
3 | \brief DataSource for experiments with realistic simulator of the PMSM model |
---|
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 <stat/loggers.h> |
---|
14 | #include <estim/libKF.h> |
---|
15 | #include "simulator_zdenek/simulator.h" |
---|
16 | #include "pmsm.h" |
---|
17 | |
---|
18 | //! Simulator of PMSM machine with predefined profile on omega |
---|
19 | class pmsmDS : public DS { |
---|
20 | |
---|
21 | protected: |
---|
22 | //! indeces of logged variables |
---|
23 | int L_x, L_ou, L_oy, L_iu, L_optu; |
---|
24 | //! Setpoints of omega in timespans given by dt_prof |
---|
25 | vec profileWw; |
---|
26 | //! Setpoints of Mz in timespans given by dt_prof |
---|
27 | vec profileMz; |
---|
28 | //! time-step for profiles |
---|
29 | double dt_prof; |
---|
30 | //! Number of miliseconds per discrete time step |
---|
31 | int Dt; |
---|
32 | //! options for logging, - log predictions of 'true' voltage |
---|
33 | bool opt_modu; |
---|
34 | //! options for logging, - |
---|
35 | public: |
---|
36 | //! Constructor with fixed sampling period |
---|
37 | pmsmDS () {Dt=125; Drv=RV ( "{o_ua o_ub o_ia o_ib t_ua t_ub o_om o_th Mz }" );} |
---|
38 | void set_parameters ( double Rs0, double Ls0, double Fmag0, double Bf0, double p0, double kp0, double J0, double Uc0, double DT0, double dt0 ) { |
---|
39 | pmsmsim_set_parameters ( Rs0, Ls0, Fmag0, Bf0, p0, kp0, J0, Uc0, DT0, dt0 ); |
---|
40 | } |
---|
41 | //! parse options: "modelu" => opt_modu=true; |
---|
42 | void set_options ( string &opt ) { |
---|
43 | opt_modu = ( opt.find ( "modelu" ) !=string::npos ); |
---|
44 | } |
---|
45 | void getdata ( vec &dt ) {dt.set_subvector(0,vec ( KalmanObs,6 ));dt(6)=x[2];dt(7)=x[3];dt(8)=x[8];} |
---|
46 | void write ( vec &ut ) {} |
---|
47 | |
---|
48 | void step() { |
---|
49 | static int ind=0; |
---|
50 | static double dW; // increase of W |
---|
51 | static double Ww; // W |
---|
52 | static double Mz; // W |
---|
53 | if ( t>=dt_prof*ind ) { |
---|
54 | ind++; |
---|
55 | // check omega profile and set dW |
---|
56 | if ( ind<profileWw.length() ) { |
---|
57 | //linear increase |
---|
58 | if ( profileWw.length() ==1 ) { |
---|
59 | Ww=profileWw ( 0 ); dW=0.0; |
---|
60 | } |
---|
61 | else { |
---|
62 | dW = profileWw ( ind )-profileWw ( ind-1 ); |
---|
63 | dW *=125e-6/dt_prof; |
---|
64 | } |
---|
65 | } |
---|
66 | else { |
---|
67 | dW = 0; |
---|
68 | } |
---|
69 | // Check Mz profile and set Mz |
---|
70 | if ( ind<profileMz.length() ) { |
---|
71 | //sudden increase |
---|
72 | Mz = profileMz(ind); |
---|
73 | } |
---|
74 | else { |
---|
75 | Mz = 0; |
---|
76 | } |
---|
77 | } |
---|
78 | Ww += dW; |
---|
79 | //Simulate Dt seconds! |
---|
80 | for ( int i=0;i<Dt;i++ ) { pmsmsim_step ( Ww , Mz);} |
---|
81 | // for ( int i=0;i<Dt;i++ ) { pmsmsim_noreg_step ( Ww , Mz);} |
---|
82 | |
---|
83 | //discretization |
---|
84 | double ustep=1.2; |
---|
85 | KalmanObs [ 0 ] = ustep*itpp::round( KalmanObs [ 0 ]/ ustep) ; |
---|
86 | KalmanObs [ 1 ] = ustep*itpp::round(KalmanObs [ 1 ]/ ustep); |
---|
87 | double istep=0.085; |
---|
88 | KalmanObs [ 2 ] = istep*itpp::round( KalmanObs [ 2 ]/ istep) ; |
---|
89 | KalmanObs [ 3 ] = istep*itpp::round(KalmanObs [ 3 ]/ istep); |
---|
90 | |
---|
91 | }; |
---|
92 | |
---|
93 | void log_add ( logger &L ) { |
---|
94 | L_x = L.add ( rx, "x" ); |
---|
95 | L_oy = L.add ( ry, "o" ); |
---|
96 | L_ou = L.add ( ru, "o" ); |
---|
97 | L_iu = L.add ( ru, "t" ); |
---|
98 | // log differences |
---|
99 | if ( opt_modu ) { |
---|
100 | L_optu = L.add ( ru, "model" ); |
---|
101 | } |
---|
102 | } |
---|
103 | |
---|
104 | void logit ( logger &L ) { |
---|
105 | L.logit ( L_x, vec ( x,4 ) ); |
---|
106 | L.logit ( L_oy, vec_2 ( KalmanObs[2],KalmanObs[3] ) ); |
---|
107 | L.logit ( L_ou, vec_2 ( KalmanObs[0],KalmanObs[1] ) ); |
---|
108 | L.logit ( L_iu, vec_2 ( KalmanObs[4],KalmanObs[5] ) ); |
---|
109 | if ( opt_modu ) { |
---|
110 | double sq3=sqrt ( 3.0 ); |
---|
111 | double ua,ub; |
---|
112 | double i1=x[0]; |
---|
113 | double i2=0.5* ( -i1+sq3*x[1] ); |
---|
114 | double i3=0.5* ( -i1-sq3*x[1] ); |
---|
115 | double u1=KalmanObs[0]; |
---|
116 | double u2=0.5* ( -u1+sq3*KalmanObs[1] ); |
---|
117 | double u3=0.5* ( -u1-sq3*KalmanObs[1] ); |
---|
118 | |
---|
119 | double du1=1.4* ( double ( i1>0.3 ) - double ( i1<-0.3 ) ) +0.2*i1; |
---|
120 | double du2=1.4* ( double ( i2>0.3 ) - double ( i2<-0.3 ) ) +0.2*i2; |
---|
121 | double du3=1.4* ( double ( i3>0.3 ) - double ( i3<-0.3 ) ) +0.2*i3; |
---|
122 | ua = ( 2.0* ( u1-du1 )- ( u2-du2 )- ( u3-du3 ) ) /3.0; |
---|
123 | ub = ( ( u2-du2 )- ( u3-du3 ) ) /sq3; |
---|
124 | L.logit ( L_optu , vec_2 ( ua,ub ) ); |
---|
125 | } |
---|
126 | |
---|
127 | } |
---|
128 | |
---|
129 | void set_profile ( double dt, const vec &Ww, const vec &Mz ) {dt_prof=dt; profileWw=Ww; profileMz=Mz;} |
---|
130 | }; |
---|
131 | |
---|
132 | //! This class behaves like BM but it is evaluating EKF |
---|
133 | class pmsmCRB : public EKFfull{ |
---|
134 | protected: |
---|
135 | vec interr; |
---|
136 | vec old_true; |
---|
137 | vec secder; |
---|
138 | int L_CRB; |
---|
139 | int L_err; |
---|
140 | int L_sec; |
---|
141 | public: |
---|
142 | //! constructor |
---|
143 | pmsmCRB():EKFfull(){old_true=zeros(6);} |
---|
144 | |
---|
145 | void bayes(const vec &dt){ |
---|
146 | static vec umin(2); |
---|
147 | vec u(2); |
---|
148 | //assume we know state exactly: |
---|
149 | vec true_state=vec(x,4); // read from pmsm |
---|
150 | E.set_mu(true_state); |
---|
151 | mu=true_state; |
---|
152 | |
---|
153 | //integration error |
---|
154 | old_true(4)=KalmanObs[4]; |
---|
155 | old_true(5)=KalmanObs[5];// add U |
---|
156 | u(0) = KalmanObs[0]; // use the required value for derivatives |
---|
157 | u(1) = KalmanObs[1]; |
---|
158 | interr = (true_state - pfxu->eval(old_true)); |
---|
159 | |
---|
160 | //second derivative |
---|
161 | IMpmsm2o* pf = dynamic_cast<IMpmsm2o*>(pfxu); |
---|
162 | if (pf) {secder=pf->eval2o(u-umin);} |
---|
163 | |
---|
164 | umin =u; |
---|
165 | EKFfull::bayes(dt); |
---|
166 | old_true.set_subvector(0,true_state); |
---|
167 | } |
---|
168 | |
---|
169 | void log_add(logger &L, const string &name="" ){ |
---|
170 | L_CRB=L.add(rx,"crb"); |
---|
171 | L_err=L.add(rx,"err"); |
---|
172 | L_sec=L.add(rx,"d2"); |
---|
173 | } |
---|
174 | void logit(logger &L){ |
---|
175 | L.logit(L_err, interr); |
---|
176 | L.logit(L_CRB,diag(_R())); |
---|
177 | L.logit(L_sec,secder); |
---|
178 | } |
---|
179 | }; |
---|
180 | |
---|
181 | //! This class behaves like BM but it is evaluating EKF |
---|
182 | class pmsmCRBMz : public EKFfull{ |
---|
183 | protected: |
---|
184 | int L_CRB; |
---|
185 | public: |
---|
186 | //! constructor |
---|
187 | pmsmCRBMz():EKFfull(){} |
---|
188 | |
---|
189 | void bayes(const vec &dt){ |
---|
190 | //assume we know state exactly: |
---|
191 | vec true_state(5); |
---|
192 | true_state.set_subvector(0,vec(x,4)); // read from pmsm |
---|
193 | true_state(4)=x[8]; |
---|
194 | |
---|
195 | E.set_mu(true_state); |
---|
196 | mu = true_state; |
---|
197 | //hack for ut |
---|
198 | EKFfull::bayes(dt); |
---|
199 | } |
---|
200 | |
---|
201 | void log_add(logger &L, const string &name="" ){ |
---|
202 | L_CRB=L.add(concat(rx,RV("Mz",1,0)),"crbz"); |
---|
203 | } |
---|
204 | void logit(logger &L){ |
---|
205 | L.logit(L_CRB,diag(_R())); |
---|
206 | } |
---|
207 | }; |
---|