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, - |
| 23 | //! indeces of logged variables |
| 24 | int L_x, L_ou, L_oy, L_iu, L_optu; |
| 25 | //! Setpoints of omega in timespans given by dt_prof |
| 26 | vec profileWw; |
| 27 | //! Setpoints of Mz in timespans given by dt_prof |
| 28 | vec profileMz; |
| 29 | //! time-step for profiles |
| 30 | double dt_prof; |
| 31 | //! Number of miliseconds per discrete time step |
| 32 | int Dt; |
| 33 | //! options for logging, - log predictions of 'true' voltage |
| 34 | bool opt_modu; |
| 35 | //! options for logging, - |
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);} |
| 37 | //! Constructor with fixed sampling period |
| 38 | pmsmDS () |
| 39 | { |
| 40 | Dt=125; |
| 41 | Drv=RV ( "{o_ua o_ub o_ia o_ib t_ua t_ub o_om o_th Mz }" ); |
| 42 | } |
| 43 | void set_parameters ( double Rs0, double Ls0, double Fmag0, double Bf0, double p0, double kp0, double J0, double Uc0, double DT0, double dt0 ) |
| 44 | { |
| 45 | pmsmsim_set_parameters ( Rs0, Ls0, Fmag0, Bf0, p0, kp0, J0, Uc0, DT0, dt0 ); |
| 46 | } |
| 47 | //! parse options: "modelu" => opt_modu=true; |
| 48 | void set_options ( string &opt ) |
| 49 | { |
| 50 | opt_modu = ( opt.find ( "modelu" ) !=string::npos ); |
| 51 | } |
| 52 | void getdata ( vec &dt ) |
| 53 | { |
| 54 | dt.set_subvector(0,vec ( KalmanObs,6 )); |
| 55 | dt(6)=x[2]; |
| 56 | dt(7)=x[3]; |
| 57 | dt(8)=x[8]; |
| 58 | } |
| 59 | void write ( vec &ut ) {} |
| 60 | |
| 61 | void step() |
| 62 | { |
| 63 | static int ind=0; |
| 64 | static double dW; // increase of W |
| 65 | static double Ww; // W |
| 66 | static double Mz; // W |
| 67 | if ( t>=dt_prof*ind ) |
| 68 | { |
| 69 | ind++; |
| 70 | // check omega profile and set dW |
| 71 | if ( ind<profileWw.length() ) |
| 72 | { |
| 73 | //linear increase |
| 74 | if ( profileWw.length() ==1 ) |
| 75 | { |
| 76 | Ww=profileWw ( 0 ); |
| 77 | dW=0.0; |
| 78 | } |
| 79 | else |
| 80 | { |
| 81 | dW = profileWw ( ind )-profileWw ( ind-1 ); |
| 82 | dW *=125e-6/dt_prof; |
| 83 | } |
| 84 | } |
| 85 | else |
| 86 | { |
| 87 | dW = 0; |
| 88 | } |
| 89 | // Check Mz profile and set Mz |
| 90 | if ( ind<profileMz.length() ) |
| 91 | { |
| 92 | //sudden increase |
| 93 | Mz = profileMz(ind); |
| 94 | } |
| 95 | else |
| 96 | { |
| 97 | Mz = 0; |
| 98 | } |
| 99 | } |
| 100 | Ww += dW; |
| 101 | //Simulate Dt seconds! |
| 102 | for ( int i=0; i<Dt; i++ ) |
| 103 | { |
| 104 | pmsmsim_step ( Ww , Mz); |
| 105 | } |
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;} |
| 107 | |
| 108 | //discretization |
| 109 | double ustep=1.2; |
| 110 | KalmanObs [ 0 ] = ustep*itpp::round( KalmanObs [ 0 ]/ ustep) ; |
| 111 | KalmanObs [ 1 ] = ustep*itpp::round(KalmanObs [ 1 ]/ ustep); |
| 112 | double istep=0.085; |
| 113 | KalmanObs [ 2 ] = istep*itpp::round( KalmanObs [ 2 ]/ istep) ; |
| 114 | KalmanObs [ 3 ] = istep*itpp::round(KalmanObs [ 3 ]/ istep); |
| 115 | |
| 116 | }; |
| 117 | |
| 118 | void log_add ( logger &L ) |
| 119 | { |
| 120 | L_x = L.add ( rx, "x" ); |
| 121 | L_oy = L.add ( ry, "o" ); |
| 122 | L_ou = L.add ( ru, "o" ); |
| 123 | L_iu = L.add ( ru, "t" ); |
| 124 | // log differences |
| 125 | if ( opt_modu ) |
| 126 | { |
| 127 | L_optu = L.add ( ru, "model" ); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | void logit ( logger &L ) |
| 132 | { |
| 133 | L.logit ( L_x, vec ( x,4 ) ); |
| 134 | L.logit ( L_oy, vec_2 ( KalmanObs[2],KalmanObs[3] ) ); |
| 135 | L.logit ( L_ou, vec_2 ( KalmanObs[0],KalmanObs[1] ) ); |
| 136 | L.logit ( L_iu, vec_2 ( KalmanObs[4],KalmanObs[5] ) ); |
| 137 | if ( opt_modu ) |
| 138 | { |
| 139 | double sq3=sqrt ( 3.0 ); |
| 140 | double ua,ub; |
| 141 | double i1=x[0]; |
| 142 | double i2=0.5* ( -i1+sq3*x[1] ); |
| 143 | double i3=0.5* ( -i1-sq3*x[1] ); |
| 144 | double u1=KalmanObs[0]; |
| 145 | double u2=0.5* ( -u1+sq3*KalmanObs[1] ); |
| 146 | double u3=0.5* ( -u1-sq3*KalmanObs[1] ); |
| 147 | |
| 148 | double du1=1.4* ( double ( i1>0.3 ) - double ( i1<-0.3 ) ) +0.2*i1; |
| 149 | double du2=1.4* ( double ( i2>0.3 ) - double ( i2<-0.3 ) ) +0.2*i2; |
| 150 | double du3=1.4* ( double ( i3>0.3 ) - double ( i3<-0.3 ) ) +0.2*i3; |
| 151 | ua = ( 2.0* ( u1-du1 )- ( u2-du2 )- ( u3-du3 ) ) /3.0; |
| 152 | ub = ( ( u2-du2 )- ( u3-du3 ) ) /sq3; |
| 153 | L.logit ( L_optu , vec_2 ( ua,ub ) ); |
| 154 | } |
| 155 | |
| 156 | } |
| 157 | |
| 158 | void set_profile ( double dt, const vec &Ww, const vec &Mz ) |
| 159 | { |
| 160 | dt_prof=dt; |
| 161 | profileWw=Ww; |
| 162 | profileMz=Mz; |
| 163 | } |
| 164 | |
| 165 | void from_setting( const Setting &root ) |
| 166 | { |
| 167 | set_parameters ( root["params"]["Rs"], root["params"]["Ls"], root["params"]["Fmag"], \ |
| 168 | root["params"]["Bf"], root["params"]["p"], root["params"]["kp"], \ |
| 169 | root["params"]["J"], root["params"]["Uc"], root["params"]["DT"], 1.0e-6 ); |
| 170 | |
| 171 | // Default values of profiles for omega and Mz |
| 172 | vec profW=vec("1.0"); |
| 173 | vec profM=vec("0.0"); |
| 174 | double tstep=1.0; |
| 175 | root.lookupValue( "tstep", tstep ); |
| 176 | UI::get( profW, root, "profileW" ); |
| 177 | UI::get( profM, root, "profileM" ); |
| 178 | set_profile (tstep , profW, profM); |
| 179 | |
| 180 | string opts; |
| 181 | if ( root.lookupValue( "options", opts ) ) |
| 182 | set_options(opts); |
| 183 | } |
| 184 | |
| 185 | // TODO dodelat void to_setting( Setting &root ) const; |
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 | | } |
| 192 | class pmsmCRB : public EKFfull |
| 193 | { |
| 194 | protected: |
| 195 | vec interr; |
| 196 | vec old_true; |
| 197 | vec secder; |
| 198 | int L_CRB; |
| 199 | int L_err; |
| 200 | int L_sec; |
| 201 | public: |
| 202 | //! constructor |
| 203 | pmsmCRB():EKFfull() |
| 204 | { |
| 205 | old_true=zeros(6); |
| 206 | } |
| 207 | |
| 208 | void bayes(const vec &dt) |
| 209 | { |
| 210 | static vec umin(2); |
| 211 | vec u(2); |
| 212 | //assume we know state exactly: |
| 213 | vec true_state=vec(x,4); // read from pmsm |
| 214 | E.set_mu(true_state); |
| 215 | mu=true_state; |
| 216 | |
| 217 | //integration error |
| 218 | old_true(4)=KalmanObs[4]; |
| 219 | old_true(5)=KalmanObs[5];// add U |
| 220 | u(0) = KalmanObs[0]; // use the required value for derivatives |
| 221 | u(1) = KalmanObs[1]; |
| 222 | interr = (true_state - pfxu->eval(old_true)); |
| 223 | |
| 224 | //second derivative |
| 225 | IMpmsm2o* pf = dynamic_cast<IMpmsm2o*>(pfxu); |
| 226 | if (pf) |
| 227 | { |
| 228 | secder=pf->eval2o(u-umin); |
| 229 | } |
| 230 | |
| 231 | umin =u; |
| 232 | EKFfull::bayes(dt); |
| 233 | old_true.set_subvector(0,true_state); |
| 234 | } |
| 235 | |
| 236 | void log_add(logger &L, const string &name="" ) |
| 237 | { |
| 238 | L_CRB=L.add(rx,"crb"); |
| 239 | L_err=L.add(rx,"err"); |
| 240 | L_sec=L.add(rx,"d2"); |
| 241 | } |
| 242 | void logit(logger &L) |
| 243 | { |
| 244 | L.logit(L_err, interr); |
| 245 | L.logit(L_CRB,diag(_R())); |
| 246 | L.logit(L_sec,secder); |
| 247 | } |
| 248 | |
| 249 | void from_setting( const Setting &root ) |
| 250 | { |
| 251 | diffbifn* IM = UI::build<diffbifn>(root, "IM"); |
| 252 | diffbifn* OM = UI::build<diffbifn>(root, "OM"); |
| 253 | |
| 254 | //parameters |
| 255 | |
| 256 | //statistics |
| 257 | int dim=IM->dimension(); |
| 258 | vec mu0; |
| 259 | if (!UI::get( mu0, root, "mu0") ) |
| 260 | mu0=zeros(dim); |
| 261 | mat P0; |
| 262 | vec dP0; |
| 263 | |
| 264 | if (UI::get(dP0,root, "dP0") ) |
| 265 | P0=diag(dP0); |
| 266 | else if (!UI::get(P0,root, "P0") ) |
| 267 | P0=eye(dim); |
| 268 | |
| 269 | set_statistics(mu0,P0); |
| 270 | |
| 271 | vec dQ; |
| 272 | UI::get( dQ, root, "dQ"); |
| 273 | vec dR; |
| 274 | UI::get( dR, root, "dR"); |
| 275 | set_parameters(IM, OM, diag(dQ) , diag(dR)); |
| 276 | |
| 277 | //connect |
| 278 | RV* drv = UI::build<RV>(root, "drv"); |
| 279 | set_drv(*drv); |
| 280 | RV* rv = UI::build<RV>(root, "rv"); |
| 281 | set_rv(*rv); |
| 282 | } |
| 283 | |
| 284 | // TODO dodelat void to_setting( Setting &root ) const; |
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 | | } |
| 302 | vec true_state(5); |
| 303 | true_state.set_subvector(0,vec(x,4)); // read from pmsm |
| 304 | true_state(4)=x[8]; |
| 305 | |
| 306 | E.set_mu(true_state); |
| 307 | mu = true_state; |
| 308 | //hack for ut |
| 309 | EKFfull::bayes(dt); |
| 310 | } |
| 311 | |
| 312 | void log_add(logger &L, const string &name="" ) |
| 313 | { |
| 314 | L_CRB=L.add(concat(rx,RV("Mz",1,0)),"crbz"); |
| 315 | } |
| 316 | void logit(logger &L) |
| 317 | { |
| 318 | L.logit(L_CRB,diag(_R())); |
| 319 | } |
| 320 | |
| 321 | void from_setting( const Setting &root ) |
| 322 | { |
| 323 | diffbifn* IM = UI::build<diffbifn>(root,"IM"); |
| 324 | diffbifn* OM = UI::build<diffbifn>(root,"OM"); |
| 325 | |
| 326 | //statistics |
| 327 | int dim=IM->dimension(); |
| 328 | vec mu0; |
| 329 | vec dP0; |
| 330 | mat P0; |
| 331 | if (!UI::get(mu0, root, "mu0")) |
| 332 | mu0=zeros(dim); |
| 333 | if (!UI::get( P0, root, "P0" )) |
| 334 | if (UI::get(dP0, root, "dP0")) |
| 335 | P0=diag(dP0); |
| 336 | else |
| 337 | P0=eye(dim); |
| 338 | |
| 339 | set_statistics(mu0,P0); |
| 340 | |
| 341 | vec dQ; |
| 342 | UI::get(dQ, root, "dQ"); |
| 343 | vec dR; |
| 344 | UI::get(dR, root, "dR"); |
| 345 | set_parameters(IM, OM, diag(dQ), diag(dR)); |
| 346 | |
| 347 | //connect |
| 348 | RV* drv = UI::build<RV>(root, "drv"); |
| 349 | set_drv(*drv); |
| 350 | RV* rv = UI::build<RV>(root, "rv"); |
| 351 | set_rv(*rv); |
| 352 | } |
| 353 | |
| 354 | // TODO dodelat void to_setting( Setting &root ) const; |