1 | #include <estim/kalman.h> |
---|
2 | #include "ekf_obj.h" |
---|
3 | //#include "../simulator.h" |
---|
4 | |
---|
5 | double minQ(double Q){if (Q>1.0){ return 1.0;} else {return Q;};}; |
---|
6 | |
---|
7 | void mat_to_int16(const imat &M, int16 *I){ |
---|
8 | for (int16 i=0;i<M.rows(); i++){ |
---|
9 | for (int16 j=0;j<M.cols(); j++){ |
---|
10 | *I++ = M(i,j); |
---|
11 | } |
---|
12 | } |
---|
13 | } |
---|
14 | void int16_to_mat(int16 *I, imat &M, int rows, int cols){ |
---|
15 | M.set_size(rows,cols); |
---|
16 | for (int16 i=0;i<M.rows(); i++){ |
---|
17 | for (int16 j=0;j<M.cols(); j++){ |
---|
18 | M(i,j) = *I++; |
---|
19 | } |
---|
20 | } |
---|
21 | } |
---|
22 | void int16_to_vec(int16 *I, ivec &v, int len){ |
---|
23 | v.set_size(len); |
---|
24 | for (int16 i=0;i<v.length(); i++){ |
---|
25 | v(i) = *I++; |
---|
26 | } |
---|
27 | } |
---|
28 | void vec_to_int16(const ivec &v, int16 *I){ |
---|
29 | for (int16 i=0;i<v.length(); i++){ |
---|
30 | *I++ = v(i); |
---|
31 | } |
---|
32 | } |
---|
33 | |
---|
34 | #ifdef XXX |
---|
35 | /////////////// |
---|
36 | void EKFfixed::bayes(const vec &yt, const vec &ut){ |
---|
37 | ekf(yt(0),yt(1),ut(0),ut(1)); |
---|
38 | |
---|
39 | vec xhat(4); |
---|
40 | //UGLY HACK!!! reliance on a predictor!! |
---|
41 | xhat(0)=zprevod(x_est[0],Qm)*Iref; |
---|
42 | xhat(1)=zprevod(x_est[1],Qm)*Iref; |
---|
43 | xhat(2)=zprevod(x_est[2],Qm)*Wref; |
---|
44 | xhat(3)=zprevod(x_est[3],15)*Thetaref; |
---|
45 | |
---|
46 | E.set_mu(xhat); |
---|
47 | |
---|
48 | if ( BM::evalll ) { |
---|
49 | /* //from enorm |
---|
50 | vec xdif(x,4);//first 4 of x |
---|
51 | //UGLY HACK!!! reliance on a predictor!! |
---|
52 | /* xdif(0)=x[0]-zprevod(x_pred[0],Qm)*Iref; |
---|
53 | xdif(1)=x[1]-zprevod(x_pred[1],Qm)*Iref; |
---|
54 | xdif(2)=x[2]-zprevod(x_pred[2],Qm)*Wref; |
---|
55 | xdif(3)=x[3]-zprevod(x_pred[3],15);*/ |
---|
56 | |
---|
57 | // xdif -=xhat; //(xdif=x-xhat) |
---|
58 | |
---|
59 | mat Pfull(4,4); |
---|
60 | double* Pp=Pfull._data(); |
---|
61 | for(int16 i=0;i<16;i++){*(Pp++) = zprevod(P_est[i],15);} |
---|
62 | |
---|
63 | E._R()._M()=Pfull; |
---|
64 | |
---|
65 | |
---|
66 | // BM::ll = -0.5* ( 4 * 1.83787706640935 +log ( det ( Pfull ) ) +xdif* ( inv(Pfull)*xdif ) );*/ |
---|
67 | } |
---|
68 | }; |
---|
69 | |
---|
70 | |
---|
71 | void EKFfixed::update_psi(void) |
---|
72 | { |
---|
73 | int16 t_sin,t_cos,tmp; |
---|
74 | |
---|
75 | // implementace v PC |
---|
76 | t_sin=prevod(sin(Thetaref*x_est[3]/32768.),15); |
---|
77 | t_cos=prevod(cos(Thetaref*x_est[3]/32768.),15); |
---|
78 | |
---|
79 | PSI[2]=((int32)cB*t_sin)>>15; |
---|
80 | tmp=((int32)cH*x_est[2])>>15; |
---|
81 | PSI[3]=((int32)tmp*t_cos)>>15; |
---|
82 | PSI[6]=-((int32)cB*t_cos)>>15; |
---|
83 | PSI[7]=((int32)tmp*t_sin)>>15; |
---|
84 | } |
---|
85 | |
---|
86 | |
---|
87 | void EKFfixed::prediction(int16 *ux) |
---|
88 | { |
---|
89 | int16 t_sin,t_cos, tmp; |
---|
90 | |
---|
91 | // implementace v PC |
---|
92 | //t_sin=prevod(sin(Thetaref*x_est[3]/32768.),15); |
---|
93 | //t_cos=prevod(cos(Thetaref*x_est[3]/32768.),15); |
---|
94 | |
---|
95 | t_sin=prevod(sin(Thetaref*x_est[3]/Qm),15); |
---|
96 | t_cos=prevod(cos(Thetaref*x_est[3]/Qm),15); |
---|
97 | |
---|
98 | tmp=((int32)cB*x_est[2])>>15; |
---|
99 | x_pred[0]=((int32)cA*x_est[0]+(int32)tmp*t_sin+(int32)cC*ux[0])>>15; |
---|
100 | x_pred[1]=((int32)cA*x_est[1]-(int32)tmp*t_cos+(int32)cC*ux[1])>>15; |
---|
101 | x_pred[2]=x_est[2]; |
---|
102 | x_pred[3]=(((int32)x_est[3]<<15)+(int32)cG*x_est[2])>>15; |
---|
103 | |
---|
104 | update_psi(); |
---|
105 | |
---|
106 | mmult(PSI,P_est,temp15a,3,3,3); |
---|
107 | // mtrans(PSI,temp15b,5,5); |
---|
108 | mmultt(temp15a,PSI,P_pred,3,3,3); |
---|
109 | maddD(P_pred,Q,3,3); |
---|
110 | } |
---|
111 | |
---|
112 | void EKFfixed::correction(void) |
---|
113 | { |
---|
114 | int16 Y_error[2]; |
---|
115 | int32 temp30a[4]; /* matrix [2,2] - temporary matrix for inversion */ |
---|
116 | |
---|
117 | choice_P(P_pred,temp15a,3); |
---|
118 | maddD(temp15a,R,1,1); |
---|
119 | minv2(temp15a,temp30a); |
---|
120 | Ry(0,0)=zprevod(temp15a[0],15); |
---|
121 | Ry(0,1)=zprevod(temp15a[1],15); |
---|
122 | Ry(1,0)=zprevod(temp15a[2],15); |
---|
123 | Ry(1,1)=zprevod(temp15a[3],15); |
---|
124 | |
---|
125 | mmultDr(P_pred,temp15a,3,3,1,1); |
---|
126 | mmult1530(temp15a,temp30a,Kalm,3,1,1); |
---|
127 | |
---|
128 | |
---|
129 | /* estimate the state system */ |
---|
130 | choice_x(x_pred, temp15a); |
---|
131 | msub(Y_mes,temp15a,Y_error,1,0); |
---|
132 | mmult(Kalm,Y_error,temp15a,3,1,0); |
---|
133 | madd(x_pred,temp15a,x_est,3,0); |
---|
134 | |
---|
135 | /* matrix of covariances - version without MMULTDL() */ |
---|
136 | |
---|
137 | /* Version with MMULTDL() */ |
---|
138 | mmultDl(P_pred,temp15a,1,3,3,1); |
---|
139 | |
---|
140 | mmult(Kalm,temp15a,P_est,3,1,3); |
---|
141 | msub(P_pred,P_est,P_est,3,3); |
---|
142 | /* END */ |
---|
143 | } |
---|
144 | |
---|
145 | |
---|
146 | void EKFfixed::ekf(double ux, double uy, double isxd, double isyd) |
---|
147 | { |
---|
148 | // vypocet napeti v systemu (x,y) |
---|
149 | ukalm[0]=prevod(ux/Uref,Qm); |
---|
150 | ukalm[1]=prevod(uy/Uref,Qm); |
---|
151 | |
---|
152 | // zadani mereni |
---|
153 | Y_mes[0]=prevod(isxd/Iref,Qm); |
---|
154 | Y_mes[1]=prevod(isyd/Iref,Qm); |
---|
155 | |
---|
156 | ////// vlastni rutina EKF ///////////////////////// |
---|
157 | prediction(ukalm); |
---|
158 | correction(); |
---|
159 | |
---|
160 | // navrat estimovanych hodnot regulatoru |
---|
161 | vec& mu = E._mu(); |
---|
162 | (mu)(0)=zprevod(x_est[0],Qm)*Iref; |
---|
163 | (mu)(1)=zprevod(x_est[1],Qm)*Iref; |
---|
164 | (mu)(2)=zprevod(x_est[2],Qm)*Wref; |
---|
165 | (mu)(3)=zprevod(x_est[3],15)*Thetaref; |
---|
166 | } |
---|
167 | |
---|
168 | void EKFfixed::init_ekf(double Tv) |
---|
169 | { |
---|
170 | // Tuning of matrix Q |
---|
171 | Q[0]=prevod(.05,15); // 0.05 |
---|
172 | Q[5]=Q[0]; |
---|
173 | Q[10]=prevod(0.0002,15); // 1e-3 |
---|
174 | Q[15]=prevod(0.001,15); // 1e-3 |
---|
175 | |
---|
176 | // Tuning of matrix R |
---|
177 | R[0]=prevod(0.1,15); // 0.05 |
---|
178 | R[3]=R[0]; |
---|
179 | |
---|
180 | // Motor model parameters |
---|
181 | cA=prevod(1-Tv*Rs/Ls,15); |
---|
182 | cB=prevod(Tv*Wref*Fmag/Iref/Ls,15); |
---|
183 | cC=prevod(Tv/Ls/Iref*Uref,15); |
---|
184 | // cD=prevod(1-Tv*Bf/J,15); |
---|
185 | // cE=prevod(kp*p*p*Tv*Fmag*Iref/J/Wref,15); |
---|
186 | // cF=prevod(p*Tv*Mref/J/Wref,15); |
---|
187 | cG=prevod(Tv*Wref*4/Thetaref,15); |
---|
188 | // cH=prevod(Tv*Wref*Fmag/Iref/Ls*Thetaref,15); |
---|
189 | cH=prevod(Tv*Wref*Fmag/Iref/Ls,17); //cB in q |
---|
190 | // cI=prevod(kp*p*p*Tv*Fmag*Iref/J/Wref*Thetaref); |
---|
191 | |
---|
192 | /* Init matrix PSI with permanently constant terms */ |
---|
193 | PSI[0]=cA; |
---|
194 | PSI[5]=PSI[0]; |
---|
195 | PSI[10]=0x7FFF; |
---|
196 | PSI[14]=cG; |
---|
197 | PSI[15]=0x7FFF; |
---|
198 | |
---|
199 | P_est[0]=0x7FFF; |
---|
200 | P_est[5]=0x7FFF; |
---|
201 | P_est[10]=0x7FFF; |
---|
202 | P_est[15]=0x7FFF; |
---|
203 | } |
---|
204 | #endif |
---|
205 | |
---|
206 | void EKF_UDfix::set_parameters ( const shared_ptr<diffbifn> &pfxu0, const shared_ptr<diffbifn> &phxu0, const mat Q0, const vec R0 ) { |
---|
207 | pfxu = pfxu0; |
---|
208 | phxu = phxu0; |
---|
209 | |
---|
210 | set_dim ( pfxu0->_dimx() ); |
---|
211 | dimy = phxu0->dimension(); |
---|
212 | dimc = pfxu0->_dimu(); |
---|
213 | |
---|
214 | vec &_mu = est._mu(); |
---|
215 | // if mu is not set, set it to zeros, just for constant terms of A and C |
---|
216 | if ( _mu.length() != dimension() ) _mu = zeros ( dimension() ); |
---|
217 | A = zeros ( dimension(), dimension() ); |
---|
218 | C = zeros ( dimy, dimension() ); |
---|
219 | |
---|
220 | //initialize matrices A C, later, these will be only updated! |
---|
221 | pfxu->dfdx_cond ( _mu, zeros ( dimc ), A, true ); |
---|
222 | // pfxu->dfdu_cond ( *_mu,zeros ( dimu ),B,true ); |
---|
223 | phxu->dfdx_cond ( _mu, zeros ( dimc ), C, true ); |
---|
224 | // phxu->dfdu_cond ( *_mu,zeros ( dimu ),D,true ); |
---|
225 | |
---|
226 | R = R0; |
---|
227 | Q = Q0; |
---|
228 | |
---|
229 | // |
---|
230 | } |
---|
231 | // aux fnc |
---|
232 | void UDtof(const mat &U, const vec &D, imat &Uf, ivec &Df, const vec &xref){ |
---|
233 | mat P= U*diag(D)*U.T(); |
---|
234 | mat T = diag(1.0/(xref)); |
---|
235 | mat Pf = T*P*T; |
---|
236 | |
---|
237 | ldmat Pld(Pf); |
---|
238 | |
---|
239 | mat Ut=Pld._L().T()*(1<<15); // U is in q15 -- diagonal is 0!!! |
---|
240 | Uf=round_i(Ut); |
---|
241 | Df=round_i(Pld._D()*(1<<15)); |
---|
242 | ivec zer=find(Df==0); |
---|
243 | for(int16 i=0; i<zer.length(); i++) Df(zer(i))=1; |
---|
244 | } |
---|
245 | |
---|
246 | |
---|
247 | void EKF_UDfix::bayes ( const vec &yt, const vec &cond ) { |
---|
248 | //preparatory |
---|
249 | vec &_mu=est._mu(); |
---|
250 | const vec &u=cond; |
---|
251 | int16 dim = dimension(); |
---|
252 | ///// !!!!!!!!!!!!!!!! |
---|
253 | U = est._R()._L().T(); |
---|
254 | D = est._R()._D(); |
---|
255 | |
---|
256 | //////////// |
---|
257 | |
---|
258 | pfxu->dfdx_cond ( _mu, u, A, false ); //update A by a derivative of fx |
---|
259 | phxu->dfdx_cond ( _mu, u, C, false ); //update A by a derivative of fx |
---|
260 | |
---|
261 | mat PhiU = A*U; |
---|
262 | |
---|
263 | ////// |
---|
264 | /* vec xref(4); |
---|
265 | xref(0)= 30.0*1.4142; |
---|
266 | xref(1)= 30.0*1.4142; |
---|
267 | xref(2)= 6.283185*200.; |
---|
268 | xref(3) = 3.141593;*/ |
---|
269 | //xref(4) = 34.0; |
---|
270 | |
---|
271 | |
---|
272 | vec Din = D; |
---|
273 | int16 i,j,k; |
---|
274 | double sigma; |
---|
275 | mat G = eye(dim); |
---|
276 | //////// thorton |
---|
277 | |
---|
278 | //move mean; |
---|
279 | _mu = pfxu->eval(_mu,u); |
---|
280 | |
---|
281 | for (i=dim-1; i>=0;i--){ |
---|
282 | sigma = 0.0; |
---|
283 | for (j=0; j<dim; j++) { |
---|
284 | sigma += PhiU(i,j)*PhiU(i,j) *Din(j); |
---|
285 | sigma += G(i,j)*G(i,j) * Q(j,j); |
---|
286 | } |
---|
287 | |
---|
288 | /* double sigma2= 0.0; |
---|
289 | for (j=0; j<dim; j++) { |
---|
290 | sigma2 += PhiU(i,j)*PhiU(i,j) *Din(j); |
---|
291 | } |
---|
292 | sigma2 +=Q(i,i);//*G(i,i)=1.0 |
---|
293 | for (j=i+1; j<dim; j++) { |
---|
294 | sigma2 += G(i,j)*G(i,j) * Q(j,j); |
---|
295 | }*/ |
---|
296 | D(i) = sigma; |
---|
297 | |
---|
298 | /* UDtof(U,D,Utf,Dtf,xref); |
---|
299 | cout << "d=sig"<<endl; |
---|
300 | cout << Dtf << endl; |
---|
301 | */ |
---|
302 | for (j=0;j<i;j++){ |
---|
303 | // cout << i << "," << j << endl; |
---|
304 | sigma = 0.0; |
---|
305 | for (k=0;k<dim;k++){ |
---|
306 | sigma += PhiU(i,k)*Din(k)*PhiU(j,k); |
---|
307 | } |
---|
308 | for (k=0;k<dim;k++){ |
---|
309 | sigma += G(i,k)*Q(k,k)*G(j,k); |
---|
310 | } |
---|
311 | // |
---|
312 | U(j,i) = sigma/D(i); |
---|
313 | |
---|
314 | /* cout << "U=sig/D"<<endl; |
---|
315 | UDtof(U,D,Utf,Dtf,xref); |
---|
316 | cout << Utf << endl << Dtf << endl; |
---|
317 | cout << G << endl << Din << endl<<endl;*/ |
---|
318 | |
---|
319 | for (k=0;k<dim;k++){ |
---|
320 | PhiU(j,k) = PhiU(j,k) - U(j,i)*PhiU(i,k); |
---|
321 | } |
---|
322 | for (k=0;k<dim;k++){ |
---|
323 | G(j,k) = G(j,k) - U(j,i)*G(i,k); |
---|
324 | } |
---|
325 | |
---|
326 | } |
---|
327 | } |
---|
328 | |
---|
329 | // bierman |
---|
330 | |
---|
331 | double dz,alpha,gamma,beta,lambda; |
---|
332 | vec a; |
---|
333 | vec b; |
---|
334 | vec yp = phxu->eval(_mu,u); |
---|
335 | vec xp=_mu; // used in bierman |
---|
336 | |
---|
337 | |
---|
338 | for (int16 iy=0; iy<dimy; iy++){ |
---|
339 | a = U.T()*C.get_row(iy); // a is not modified, but |
---|
340 | b = elem_mult(D,a); // b is modified to become unscaled Kalman gain. |
---|
341 | dz = yt(iy) - yp(iy); |
---|
342 | |
---|
343 | alpha = R(iy); |
---|
344 | gamma = 1/alpha; |
---|
345 | for (j=0;j<dim;j++){ |
---|
346 | beta = alpha; |
---|
347 | alpha = alpha + a(j)*b(j); |
---|
348 | lambda = -a(j)*gamma; |
---|
349 | gamma = 1.0/alpha; |
---|
350 | D(j) = beta*gamma*D(j); |
---|
351 | |
---|
352 | // cout << "a: " << alpha << "g: " << gamma << endl; |
---|
353 | for (i=0;i<j;i++){ |
---|
354 | beta = U(i,j); |
---|
355 | U(i,j) = beta + b(i)*lambda; |
---|
356 | b(i) = b(i) + b(j)*beta; |
---|
357 | } |
---|
358 | } |
---|
359 | double dzs = gamma*dz; // apply scaling to innovations |
---|
360 | _mu = _mu + dzs*b; // multiply by unscaled Kalman gain |
---|
361 | //cout << "Ub: " << U << endl; |
---|
362 | //cout << "Db: " << D << endl <<endl; |
---|
363 | |
---|
364 | } |
---|
365 | |
---|
366 | |
---|
367 | ///// |
---|
368 | est._R().__L()=U.T(); |
---|
369 | est._R().__D()=D; |
---|
370 | |
---|
371 | if ( evalll == true ) { //likelihood of observation y |
---|
372 | } |
---|
373 | } |
---|
374 | |
---|
375 | void EKF_UDfix::from_setting ( const Setting &set ) { |
---|
376 | BM::from_setting ( set ); |
---|
377 | shared_ptr<diffbifn> IM = UI::build<diffbifn> ( set, "IM", UI::compulsory ); |
---|
378 | shared_ptr<diffbifn> OM = UI::build<diffbifn> ( set, "OM", UI::compulsory ); |
---|
379 | |
---|
380 | //statistics |
---|
381 | int16 dim = IM->dimension(); |
---|
382 | vec mu0; |
---|
383 | if ( !UI::get ( mu0, set, "mu0" ) ) |
---|
384 | mu0 = zeros ( dim ); |
---|
385 | |
---|
386 | mat P0; |
---|
387 | vec dP0; |
---|
388 | if ( UI::get ( dP0, set, "dP0" ) ) |
---|
389 | P0 = diag ( dP0 ); |
---|
390 | else if ( !UI::get ( P0, set, "P0" ) ) |
---|
391 | P0 = eye ( dim ); |
---|
392 | |
---|
393 | est._mu()=mu0; |
---|
394 | est._R()=ldmat(P0); |
---|
395 | |
---|
396 | //parameters |
---|
397 | vec dQ, dR; |
---|
398 | UI::get ( dQ, set, "dQ", UI::compulsory ); |
---|
399 | UI::get ( dR, set, "dR", UI::compulsory ); |
---|
400 | set_parameters ( IM, OM, diag ( dQ ), dR ); |
---|
401 | |
---|
402 | UI::get(log_level, set, "log_level", UI::optional); |
---|
403 | } |
---|
404 | |
---|
405 | |
---|
406 | void EKFfixedUD::bayes(const itpp::vec& yt, const itpp::vec& ut) |
---|
407 | { |
---|
408 | ekf(ut[0],ut[1],yt[0],yt[1]); |
---|
409 | } |
---|
410 | |
---|
411 | |
---|
412 | void EKFfixedUD::ekf(double ux, double uy, double isxd, double isyd) |
---|
413 | { |
---|
414 | // vypocet napeti v systemu (x,y) |
---|
415 | int16 uf[2]; |
---|
416 | uf[0]=prevod(ux/Uref,15); |
---|
417 | uf[1]=prevod(uy/Uref,15); |
---|
418 | |
---|
419 | int16 Y_mes[2]; |
---|
420 | // zadani mereni |
---|
421 | Y_mes[0]=prevod(isxd/Iref,15); |
---|
422 | Y_mes[1]=prevod(isyd/Iref,15); |
---|
423 | |
---|
424 | ////// vlastni rutina EKF -- ///////////////////////// |
---|
425 | int16 t_sin,t_cos; |
---|
426 | int32 tmp; |
---|
427 | |
---|
428 | // implementace v PC |
---|
429 | /* t_sin=prevod(sin(Thetaref*x_est[3]/32768.),15); |
---|
430 | t_cos=prevod(cos(Thetaref*x_est[3]/32768.),15);*/ |
---|
431 | tmp=prevod(sin(Thetaref*x_est[3]/32768.),15); |
---|
432 | if (tmp>32767) t_sin =32767; else t_sin=tmp; |
---|
433 | tmp=prevod(cos(Thetaref*x_est[3]/32768.),15); |
---|
434 | if (tmp>32767) t_cos =32767; else t_cos=tmp; |
---|
435 | |
---|
436 | tmp=((int32)cB*x_est[2])>>15; // q15*q13 -> q13 |
---|
437 | // q15*q13 + q13*q15 + q15*q13?? |
---|
438 | x_est[0]=((int32)cA*x_est[0]+(int32)tmp*t_sin+(int32)cC*(uf[0]))>>15; // in q13 |
---|
439 | x_est[1]=((int32)cA*x_est[1]-(int32)tmp*t_cos+(int32)cC*(uf[1]))>>15; // q13 |
---|
440 | x_est[2]=x_est[2]; |
---|
441 | // q15 + q15*q13 |
---|
442 | x_est[3]=(((int32)x_est[3]<<15)+(int32)cG*x_est[2])>>15; |
---|
443 | |
---|
444 | if(x_est[3]>(1<<15)) x_est[3]-=2*(1<<15); |
---|
445 | if(x_est[3]<-(1<<15)) x_est[3]+=2*(1<<15); |
---|
446 | |
---|
447 | //void EKFfixed::update_psi(void) |
---|
448 | { |
---|
449 | PSI[2]=((int32)cB*t_sin)>>15; |
---|
450 | tmp=((int32)cH*x_est[2])>>15; // ! cH =cB with different scale!! |
---|
451 | PSI[3]=((int32)tmp*t_cos)>>15; |
---|
452 | PSI[6]=-((int32)cB*t_cos)>>15; |
---|
453 | PSI[7]=((int32)tmp*t_sin)>>15; |
---|
454 | } |
---|
455 | { |
---|
456 | int Ai[16]; |
---|
457 | for (int i=0;i<16; i++) Ai[i]=(int)(PSI[i]); |
---|
458 | ivec Ad(Ai,16); |
---|
459 | log_level.store(logA,get_from_ivec(Ad)); |
---|
460 | } |
---|
461 | |
---|
462 | ///////// end of copy /////////////// |
---|
463 | mmultAU(PSI,Uf,PSIU,4,4); |
---|
464 | //thorton(int16 *U, int16 *D, int16 *PSIU, int16 *Q, int16 *G, int16 *Dold, unsigned int16 dimx); |
---|
465 | thorton_fast(Uf,Df,PSIU,Q,G,Dfold,4); |
---|
466 | |
---|
467 | { |
---|
468 | int Ui[16]; for (int i=0;i<16; i++) Ui[i]=(int)Uf[i]; |
---|
469 | |
---|
470 | ivec Ud(Ui,16); |
---|
471 | log_level.store(logU,get_from_ivec(Ud)); |
---|
472 | |
---|
473 | int di[16]; for (int i=0;i<16; i++) di[i]=(int)Df[i]; |
---|
474 | ivec dd(di,4); |
---|
475 | imat U(Ui,4,4); |
---|
476 | mat U2=to_mat(U)/32768; |
---|
477 | mat PP=U2*diag(to_vec(dd))*U2.T(); |
---|
478 | vec pp(PP._data(),16); |
---|
479 | log_level.store(logP,pp); |
---|
480 | } |
---|
481 | { |
---|
482 | int Gi[16]; for (int i=0;i<16; i++) Gi[i]=(int)G[i]; |
---|
483 | ivec Gd(Gi,16); |
---|
484 | log_level.store(logG,get_from_ivec(Gd)); |
---|
485 | } |
---|
486 | |
---|
487 | |
---|
488 | int16 difz[2]; |
---|
489 | difz[0]=(Y_mes[0]-x_est[0]); // shift to q15!! |
---|
490 | difz[1]=(Y_mes[1]-x_est[1]);//<<2; |
---|
491 | |
---|
492 | { |
---|
493 | int Di[4]; for (int i=0;i<4; i++) Di[i]=(int)Df[i]; |
---|
494 | ivec dd(Di,4);//;dd(0)=Y_mes[0];dd(1)=Y_mes[1]; dd(2)=difz[0]; dd(3)=difz[1]; |
---|
495 | log_level.store(logD,get_from_ivec(dd)); |
---|
496 | } |
---|
497 | |
---|
498 | //bierman(int16 *difz, int16 *xp, int16 *U, int16 *D, int16 *R, unsigned int16 dimy, unsigned int16 dimx ); |
---|
499 | int16 dR[2];dR[0]=R[0];dR[1]=R[3]; |
---|
500 | //int16 xb[4]; xb[0]=x_est[0]<<2; xb[1]=x_est[1]<<2; xb[2]=x_est[2]<<2; xb[3]=x_est[3]; |
---|
501 | bierman_fast(difz,x_est,Uf,Df,dR,2,4); |
---|
502 | //x_est[0] = xb[0]>>2; x_est[1]=xb[1]>>2; x_est[2]=xb[2]>>2; x_est[3]=xb[3]; |
---|
503 | |
---|
504 | // navrat estimovanych hodnot regulatoru |
---|
505 | vec& mu = E._mu(); |
---|
506 | (mu)(0)=zprevod(x_est[0],15)*Iref; |
---|
507 | (mu)(1)=zprevod(x_est[1],15)*Iref; |
---|
508 | (mu)(2)=zprevod(x_est[2],15)*Wref; |
---|
509 | (mu)(3)=zprevod(x_est[3],15)*Thetaref; |
---|
510 | |
---|
511 | //x_est[2]=x[2]*32768/Wref; |
---|
512 | //x_est[3]=x[3]*32768/Thetaref; |
---|
513 | // mat T=diag(concat(vec_2(Iref,Iref), vec_2(Wref,Thetaref))); |
---|
514 | } |
---|
515 | |
---|
516 | void EKFfixedUD::init_ekf(double Tv) |
---|
517 | { |
---|
518 | // Tuning of matrix Q |
---|
519 | Q[0]=prevod(.001,15); // 0.05 |
---|
520 | Q[5]=Q[0]; |
---|
521 | Q[10]=prevod(0.0005,15); // 1e-3 |
---|
522 | Q[15]=prevod(0.0001,15); // 1e-3 |
---|
523 | |
---|
524 | Uf[0]=0x7FFF;// ! // 0.05 |
---|
525 | Uf[1]=Uf[2]=Uf[3]=Uf[4]=0; |
---|
526 | Uf[5]=0x7FFF;//! |
---|
527 | Uf[6]=Uf[6]=Uf[8]=Uf[9]=0; |
---|
528 | Uf[10]=0x7FFF;//! // 1e-3 |
---|
529 | Uf[11]=Uf[12]=Uf[13]=Uf[4]=0; |
---|
530 | Uf[15]=0x7FFF; // 1e-3 |
---|
531 | |
---|
532 | Df[0]=1<<14; |
---|
533 | Df[1]=1<<14; |
---|
534 | Df[2]=1<<14; |
---|
535 | Df[3]=1<<14; |
---|
536 | |
---|
537 | // Tuning of matrix R |
---|
538 | R[0]=prevod(0.05,15); // 0.05 |
---|
539 | R[3]=R[0]; |
---|
540 | |
---|
541 | // Motor model parameters |
---|
542 | cA=prevod(1-Tv*Rs/Ls,15); |
---|
543 | cB=prevod(Tv*Wref*Fmag/Iref/Ls,15); |
---|
544 | cC=prevod(Tv/Ls/Iref*Uref,15); |
---|
545 | // cD=prevod(1-Tv*Bf/J,15); |
---|
546 | // cE=prevod(kp*p*p*Tv*Fmag*Iref/J/Wref,15); |
---|
547 | // cF=prevod(p*Tv*Mref/J/Wref,15); |
---|
548 | cG=prevod(Tv*Wref/Thetaref,15); //in q15! |
---|
549 | //cH=prevod(Tv*Wref*Fmag/Iref/Ls*Thetaref,15); |
---|
550 | // cH=prevod(Tv*Wref*Thetaref*Fmag/Iref/Ls,15); // < =========== |
---|
551 | cH=prevod(Tv*Wref*Thetaref*Fmag/Iref/Ls,15); // |
---|
552 | // cI=prevod(kp*p*p*Tv*Fmag*Iref/J/Wref*Thetaref); |
---|
553 | |
---|
554 | /* Init matrix PSI with permanently constant terms */ |
---|
555 | PSI[0]=cA; |
---|
556 | PSI[5]=PSI[0]; |
---|
557 | PSI[10]=0x7FFF; |
---|
558 | PSI[14]=cG; |
---|
559 | PSI[15]=0x7FFF; |
---|
560 | |
---|
561 | } |
---|
562 | |
---|
563 | void EKFfixedUD2::bayes(const itpp::vec& yt, const itpp::vec& ut) |
---|
564 | { |
---|
565 | ekf2(ut[0],ut[1],yt[0],yt[1]); |
---|
566 | } |
---|
567 | |
---|
568 | |
---|
569 | void EKFfixedUD2::ekf2(double ux, double uy, double isxd, double isyd) |
---|
570 | { |
---|
571 | // vypocet napeti v systemu (x,y) |
---|
572 | int16 uf[2]; |
---|
573 | uf[0]=prevod(ux/Uref,15); |
---|
574 | uf[1]=prevod(uy/Uref,15); |
---|
575 | |
---|
576 | int16 Y_mes[2]; |
---|
577 | // zadani mereni |
---|
578 | Y_mes[0]=prevod(isxd/Iref,15); |
---|
579 | Y_mes[1]=prevod(isyd/Iref,15); |
---|
580 | |
---|
581 | ////// vlastni rutina EKF -- ///////////////////////// |
---|
582 | int16 t_sin,t_cos; |
---|
583 | int32 tmp; |
---|
584 | |
---|
585 | x_est[0]=x_est[0]; |
---|
586 | // q15 + q15*q13 |
---|
587 | x_est[1]=(((int32)x_est[1]<<15)+(int32)cG*x_est[0]+(1<<14))>>15; |
---|
588 | |
---|
589 | // if(x_est[1]>(1<<15)) x_est[1]-=2*(1<<15); |
---|
590 | // if(x_est[1]<-(1<<15)) x_est[1]+=2*(1<<15); |
---|
591 | |
---|
592 | |
---|
593 | ///////// end of copy /////////////// |
---|
594 | mmultAU(PSI,Uf,PSIU,2,2); |
---|
595 | |
---|
596 | //void EKFfixed::update_psi(void) |
---|
597 | { |
---|
598 | int Ai[4]; |
---|
599 | for (int i=0;i<4; i++) Ai[i]=(int)(PSI[i]); |
---|
600 | ivec Ad(Ai,4); |
---|
601 | log_level.store(logA,get_from_ivec(Ad)); |
---|
602 | } |
---|
603 | |
---|
604 | //thorton(int16 *U, int16 *D, int16 *PSIU, int16 *Q, int16 *G, int16 *Dold, unsigned int16 dimx); |
---|
605 | thorton_fast(Uf,Df,PSIU,Q,G,Dfold,2); |
---|
606 | |
---|
607 | // implementace v PC |
---|
608 | /* t_sin=prevod(sin(Thetaref*x_est[3]/32768.),15); |
---|
609 | * t_cos=prevod(cos(Thetaref*x_est[3]/32768.),15);*/ |
---|
610 | tmp=prevod(sin(Thetaref*x_est[1]/32768.),15); |
---|
611 | if (tmp>32767) t_sin =32767; else t_sin=tmp; |
---|
612 | tmp=prevod(cos(Thetaref*x_est[1]/32768.),15); |
---|
613 | if (tmp>32767) t_cos =32767; else t_cos=tmp; |
---|
614 | |
---|
615 | { |
---|
616 | C[0]=((int32)cB*t_sin)>>15; |
---|
617 | tmp=((int32)cH*x_est[0])>>15; // ! cH =cB with different scale!! |
---|
618 | C[1]=((int32)tmp*t_cos)>>15; |
---|
619 | C[2]=-((int32)cB*t_cos)>>15; |
---|
620 | C[3]=((int32)tmp*t_sin)>>15; |
---|
621 | } |
---|
622 | |
---|
623 | |
---|
624 | { |
---|
625 | int Ui[4]; for (int i=0;i<4; i++) Ui[i]=(int)Uf[i]; |
---|
626 | int Ci[4]; for (int i=0;i<4; i++) Ci[i]=(int)C[i]; |
---|
627 | |
---|
628 | imat Ud(Ui,2,2); |
---|
629 | imat Cd(Ci,2,2); |
---|
630 | // imat CU = Cd*Ud/(1<<15); |
---|
631 | log_level.store(logU,get_from_ivec(ivec(Ud._data(),Ud._datasize()))); |
---|
632 | |
---|
633 | int di[2]; for (int i=0;i<2; i++) di[i]=(int)Df[i]; |
---|
634 | ivec dd(di,2); |
---|
635 | imat U(Ui,2,2); |
---|
636 | mat U2=to_mat(U)/32768; |
---|
637 | mat PP=U2*diag(to_vec(dd))*U2.T(); |
---|
638 | vec pp(PP._data(),4); |
---|
639 | log_level.store(logP,pp); |
---|
640 | } |
---|
641 | { |
---|
642 | int Gi[4]; for (int i=0;i<4; i++) Gi[i]=(int)G[i]; |
---|
643 | ivec Gd(Gi,4); |
---|
644 | log_level.store(logG,get_from_ivec(Gd)); |
---|
645 | } |
---|
646 | |
---|
647 | |
---|
648 | tmp=((int32)cB*x_est[0])>>15; // q15*q13 -> q13 |
---|
649 | // q15*q13 + q13*q15 + q15*q13?? |
---|
650 | y_est[0]=((int32)cA*y_old[0]+(int32)tmp*t_sin+(int32)cC*(uf[0])+(1<<14))>>15; // in q13 |
---|
651 | y_est[1]=((int32)cA*y_old[1]-(int32)tmp*t_cos+(int32)cC*(uf[1])+(1<<14))>>15; // q13 |
---|
652 | |
---|
653 | |
---|
654 | int16 difz[2]; |
---|
655 | difz[0]=(Y_mes[0]-y_est[0]); // shift to q15!! |
---|
656 | difz[1]=(Y_mes[1]-y_est[1]);//<<2; |
---|
657 | |
---|
658 | y_old[0] = Y_mes[0]; |
---|
659 | y_old[1] = Y_mes[1]; |
---|
660 | { |
---|
661 | int Di[2]; for (int i=0;i<2; i++) Di[i]=(int)Df[i]; |
---|
662 | ivec dd(Di,2);//;dd(0)=Y_mes[0];dd(1)=Y_mes[1]; dd(2)=difz[0]; dd(3)=difz[1]; |
---|
663 | log_level.store(logD,get_from_ivec(dd)); |
---|
664 | int Ci[4]; for (int i=0;i<4; i++) Ci[i]=(int)C[i]; |
---|
665 | ivec CC(Ci,4);//;dd(0)=Y_mes[0];dd(1)=Y_mes[1]; dd(2)=difz[0]; dd(3)=difz[1]; |
---|
666 | log_level.store(logC,get_from_ivec(CC)); |
---|
667 | } |
---|
668 | |
---|
669 | //bierman(int16 *difz, int16 *xp, int16 *U, int16 *D, int16 *R, unsigned int16 dimy, unsigned int16 dimx ); |
---|
670 | int16 dR[2];dR[0]=R[0];dR[1]=R[3]; |
---|
671 | //int16 xb[4]; xb[0]=x_est[0]<<2; xb[1]=x_est[1]<<2; xb[2]=x_est[2]<<2; xb[3]=x_est[3]; |
---|
672 | bierman_fastC(difz,x_est,Uf,Df,C,dR,2,2); |
---|
673 | //x_est[0] = xb[0]>>2; x_est[1]=xb[1]>>2; x_est[2]=xb[2]>>2; x_est[3]=xb[3]; |
---|
674 | |
---|
675 | // navrat estimovanych hodnot regulatoru |
---|
676 | vec& mu = E._mu(); |
---|
677 | (mu)(0)=zprevod(x_est[0],15)*Wref; |
---|
678 | (mu)(1)=zprevod(x_est[1],15)*Thetaref; |
---|
679 | |
---|
680 | //x_est[2]=x[2]*32768/Wref; |
---|
681 | //x_est[3]=x[3]*32768/Thetaref; |
---|
682 | // mat T=diag(concat(vec_2(Iref,Iref), vec_2(Wref,Thetaref))); |
---|
683 | } |
---|
684 | |
---|
685 | void EKFfixedUD2::init_ekf2(double Tv) |
---|
686 | { |
---|
687 | // Tuning of matrix Q |
---|
688 | Q[0]=prevod(0.01,15); // 1e-3 |
---|
689 | Q[3]=prevod(0.01,15); // 1e-3 |
---|
690 | |
---|
691 | Uf[0]=0x7FFF;// ! // 0.05 |
---|
692 | Uf[1]=Uf[2]=0; |
---|
693 | Uf[3]=0x7FFF;//! |
---|
694 | |
---|
695 | Df[0]=1<<14; |
---|
696 | Df[1]=1<<14; |
---|
697 | |
---|
698 | // Tuning of matrix R |
---|
699 | R[0]=prevod(0.01,15); // 0.05 |
---|
700 | R[3]=R[0]; |
---|
701 | |
---|
702 | // Motor model parameters |
---|
703 | cA=prevod(1-Tv*Rs/Ls,15); |
---|
704 | cB=prevod(Tv*Wref*Fmag/Iref/Ls,15); |
---|
705 | cC=prevod(Tv/Ls/Iref*Uref,15); |
---|
706 | // cD=prevod(1-Tv*Bf/J,15); |
---|
707 | // cE=prevod(kp*p*p*Tv*Fmag*Iref/J/Wref,15); |
---|
708 | // cF=prevod(p*Tv*Mref/J/Wref,15); |
---|
709 | cG=prevod(Tv*Wref/Thetaref,15); //in q15! |
---|
710 | //cH=prevod(Tv*Wref*Fmag/Iref/Ls*Thetaref,15); |
---|
711 | // cH=prevod(Tv*Wref*Thetaref*Fmag/Iref/Ls,15); // < =========== |
---|
712 | cH=prevod(Tv*Wref*Fmag*Thetaref/Iref/Ls,15); // |
---|
713 | // cI=prevod(kp*p*p*Tv*Fmag*Iref/J/Wref*Thetaref); |
---|
714 | |
---|
715 | /* Init matrix PSI with permanently constant terms */ |
---|
716 | PSI[0]=0x7FFF; |
---|
717 | PSI[1]=0; |
---|
718 | PSI[2]=cG; |
---|
719 | PSI[3]=0x7FFF; |
---|
720 | } |
---|
721 | |
---|
722 | void EKFfixedUD3::bayes(const itpp::vec& yt, const itpp::vec& ut) |
---|
723 | { |
---|
724 | ekf3(ut[0],ut[1],yt[0],yt[1]); |
---|
725 | } |
---|
726 | |
---|
727 | |
---|
728 | void EKFfixedUD3::ekf3(double ux, double uy, double isxd, double isyd) |
---|
729 | { |
---|
730 | // vypocet napeti v systemu (x,y) |
---|
731 | int16 uf[2]; |
---|
732 | uf[0]=prevod(ux/Uref,15); |
---|
733 | uf[1]=prevod(uy/Uref,15); |
---|
734 | |
---|
735 | int16 Y_mes[2]; |
---|
736 | // zadani mereni |
---|
737 | Y_mes[0]=prevod(isxd/Iref,15); |
---|
738 | Y_mes[1]=prevod(isyd/Iref,15); |
---|
739 | |
---|
740 | ////// vlastni rutina EKF -- ///////////////////////// |
---|
741 | int16 t_sin,t_cos; |
---|
742 | int32 tmp; |
---|
743 | |
---|
744 | int16 Mm=x_est[2]; |
---|
745 | x_est[0]=(((int32)x_est[0]<<15) + (int32)cF*x_est[2] + (1<<14))>>15; |
---|
746 | // q15 + q15*q13 |
---|
747 | x_est[1]=(((int32)x_est[1]<<15)+(int32)cG*x_est[0] + (1<<14))>>15; |
---|
748 | x_est[2]=x_est[2]; |
---|
749 | |
---|
750 | // if(x_est[1]>(1<<15)) x_est[1]-=2*(1<<15); |
---|
751 | // if(x_est[1]<-(1<<15)) x_est[1]+=2*(1<<15); |
---|
752 | |
---|
753 | |
---|
754 | ///////// end of copy /////////////// |
---|
755 | mmultAU(PSI,Uf,PSIU,3,3); |
---|
756 | |
---|
757 | |
---|
758 | //thorton(int16 *U, int16 *D, int16 *PSIU, int16 *Q, int16 *G, int16 *Dold, unsigned int16 dimx); |
---|
759 | thorton_fast(Uf,Df,PSIU,Q,G,Dfold,3); |
---|
760 | |
---|
761 | // implementace v PC |
---|
762 | /* t_sin=prevod(sin(Thetaref*x_est[3]/32768.),15); |
---|
763 | * t_cos=prevod(cos(Thetaref*x_est[3]/32768.),15);*/ |
---|
764 | tmp=prevod(sin(Thetaref*x_est[1]/32768.),15); |
---|
765 | if (tmp>32767) t_sin =32767; else t_sin=tmp; |
---|
766 | tmp=prevod(cos(Thetaref*x_est[1]/32768.),15); |
---|
767 | if (tmp>32767) t_cos =32767; else t_cos=tmp; |
---|
768 | |
---|
769 | { |
---|
770 | C[0]=((int32)cB*t_sin)>>15; |
---|
771 | tmp=((int32)cH*x_est[0])>>15; // ! cH =cB with different scale!! |
---|
772 | C[1]=((int32)tmp*t_cos)>>15; |
---|
773 | C[2] = 0; |
---|
774 | |
---|
775 | C[3]=-((int32)cB*t_cos)>>15; |
---|
776 | C[4]=((int32)tmp*t_sin)>>15; |
---|
777 | C[5]=0; |
---|
778 | } |
---|
779 | |
---|
780 | tmp=((int32)cB*x_est[0])>>15; // q15*q13 -> q13 |
---|
781 | // q15*q13 + q13*q15 + q15*q13?? |
---|
782 | y_est[0]=((int32)cA*y_old[0]+(int32)tmp*t_sin+(int32)cC*(uf[0])+(1<<14))>>15; // in q13 |
---|
783 | y_est[1]=((int32)cA*y_old[1]-(int32)tmp*t_cos+(int32)cC*(uf[1])+(1<<14))>>15; // q13 |
---|
784 | |
---|
785 | |
---|
786 | int16 difz[2]; |
---|
787 | difz[0]=(Y_mes[0]-y_est[0]); // shift to q15!! |
---|
788 | difz[1]=(Y_mes[1]-y_est[1]);//<<2; |
---|
789 | |
---|
790 | y_old[0] = Y_mes[0]; |
---|
791 | y_old[1] = Y_mes[1]; |
---|
792 | |
---|
793 | //bierman(int16 *difz, int16 *xp, int16 *U, int16 *D, int16 *R, unsigned int16 dimy, unsigned int16 dimx ); |
---|
794 | int16 dR[2];dR[0]=R[0];dR[1]=R[3]; |
---|
795 | //int16 xb[4]; xb[0]=x_est[0]<<2; xb[1]=x_est[1]<<2; xb[2]=x_est[2]<<2; xb[3]=x_est[3]; |
---|
796 | bierman_fastC(difz,x_est,Uf,Df,C,dR,2,3); |
---|
797 | //x_est[0] = xb[0]>>2; x_est[1]=xb[1]>>2; x_est[2]=xb[2]>>2; x_est[3]=xb[3]; |
---|
798 | |
---|
799 | // navrat estimovanych hodnot regulatoru |
---|
800 | vec& mu = E._mu(); |
---|
801 | (mu)(0)=zprevod(x_est[0],15)*Wref; |
---|
802 | (mu)(1)=zprevod(x_est[1],15)*Thetaref; |
---|
803 | (mu)(2)=zprevod(x_est[2],15)*Mref; |
---|
804 | |
---|
805 | //x_est[2]=x[2]*32768/Wref; |
---|
806 | //x_est[3]=x[3]*32768/Thetaref; |
---|
807 | // mat T=diag(concat(vec_2(Iref,Iref), vec_2(Wref,Thetaref))); |
---|
808 | } |
---|
809 | |
---|
810 | void EKFfixedUD3::init_ekf3(double Tv) |
---|
811 | { |
---|
812 | // Tuning of matrix Q |
---|
813 | Q[0]=prevod(0.001,15); // 1e-3 |
---|
814 | Q[4]=prevod(0.001,15); // 1e-3 |
---|
815 | Q[8]=prevod(0.001,15); // 1e-3 |
---|
816 | |
---|
817 | Uf[0]=0x7FFF;// ! // 0.05 |
---|
818 | Uf[1]=Uf[2]=Uf[3]=0; |
---|
819 | Uf[4]=0x7FFF;//! |
---|
820 | Uf[5]=Uf[6]=Uf[7]=0; |
---|
821 | Uf[8]=0x7FFF;//! |
---|
822 | |
---|
823 | Df[0]=1<<14; |
---|
824 | Df[1]=1<<14; |
---|
825 | Df[2]=1<<14; |
---|
826 | |
---|
827 | // Tuning of matrix R |
---|
828 | R[0]=prevod(0.01,15); // 0.05 |
---|
829 | R[3]=R[0]; |
---|
830 | |
---|
831 | // Motor model parameters |
---|
832 | cA=prevod(1-Tv*Rs/Ls,15); |
---|
833 | cB=prevod(Tv*Wref*Fmag/Iref/Ls,15); |
---|
834 | cC=prevod(Tv/Ls/Iref*Uref,15); |
---|
835 | // cD=prevod(1-Tv*Bf/J,15); |
---|
836 | // cE=prevod(kp*p*p*Tv*Fmag*Iref/J/Wref,15); |
---|
837 | cF=prevod(-p*Tv*Mref/J/Wref,15); |
---|
838 | cG=prevod(Tv*Wref/Thetaref,15); //in q15! |
---|
839 | //cH=prevod(Tv*Wref*Fmag/Iref/Ls*Thetaref,15); |
---|
840 | // cH=prevod(Tv*Wref*Thetaref*Fmag/Iref/Ls,15); // < =========== |
---|
841 | cH=prevod(Tv*Wref*Fmag*Thetaref/Iref/Ls,15); // |
---|
842 | // cI=prevod(kp*p*p*Tv*Fmag*Iref/J/Wref*Thetaref); |
---|
843 | |
---|
844 | /* Init matrix PSI with permanently constant terms */ |
---|
845 | PSI[0]=0x7FFF; |
---|
846 | PSI[1]=0; |
---|
847 | PSI[2]= cF; |
---|
848 | |
---|
849 | PSI[3]=cG; |
---|
850 | PSI[4]=0x7FFF; |
---|
851 | PSI[5]=0; |
---|
852 | |
---|
853 | PSI[6]=0; |
---|
854 | PSI[7]=0; |
---|
855 | PSI[8]=0x7FFF; |
---|
856 | } |
---|
857 | |
---|
858 | |
---|
859 | void EKFfixedCh::bayes(const itpp::vec& yt, const itpp::vec& ut) |
---|
860 | { |
---|
861 | ekf(ut[0],ut[1],yt[0],yt[1]); |
---|
862 | } |
---|
863 | |
---|
864 | |
---|
865 | void EKFfixedCh::ekf(double ux, double uy, double isxd, double isyd) |
---|
866 | { |
---|
867 | // vypocet napeti v systemu (x,y) |
---|
868 | int16 uf[2]; |
---|
869 | uf[0]=prevod(ux/Uref,15); |
---|
870 | uf[1]=prevod(uy/Uref,15); |
---|
871 | |
---|
872 | int16 Y_mes[2]; |
---|
873 | // zadani mereni |
---|
874 | Y_mes[0]=prevod(isxd/Iref,15); |
---|
875 | Y_mes[1]=prevod(isyd/Iref,15); |
---|
876 | |
---|
877 | ////// vlastni rutina EKF -- ///////////////////////// |
---|
878 | int16 t_sin,t_cos, tmp; |
---|
879 | |
---|
880 | // implementace v PC |
---|
881 | /* t_sin=prevod(sin(Thetaref*x_est[3]/32768.),15); |
---|
882 | * t_cos=prevod(cos(Thetaref*x_est[3]/32768.),15);*/ |
---|
883 | t_sin=prevod(sin(Thetaref*x_est[3]/32768.),15); |
---|
884 | t_cos=prevod(cos(Thetaref*x_est[3]/32768.),15); |
---|
885 | |
---|
886 | tmp=((int32)cB*x_est[2])>>15; // q15*q13 -> q13 |
---|
887 | // q15*q13 + q13*q15 + q15*q13?? |
---|
888 | x_est[0]=((int32)cA*x_est[0]+(int32)tmp*t_sin+(int32)cC*(uf[0]))>>15; // in q13 |
---|
889 | x_est[1]=((int32)cA*x_est[1]-(int32)tmp*t_cos+(int32)cC*(uf[1]))>>15; // q13 |
---|
890 | x_est[2]=x_est[2]; |
---|
891 | // q15 + q15*q13 |
---|
892 | x_est[3]=(((int32)x_est[3]<<15)+(int32)cG*x_est[2])>>15; |
---|
893 | |
---|
894 | if(x_est[3]>(1<<15)) x_est[3]-=2*(1<<15); |
---|
895 | if(x_est[3]<-(1<<15)) x_est[3]+=2*(1<<15); |
---|
896 | |
---|
897 | //void EKFfixed::update_psi(void) |
---|
898 | { |
---|
899 | PSI[2]=((int32)cB*t_sin)>>15; |
---|
900 | tmp=((int32)cH*x_est[2])>>15; // ! cH =cB with different scale!! |
---|
901 | PSI[3]=((int32)tmp*t_cos)>>15; |
---|
902 | PSI[6]=-((int32)cB*t_cos)>>15; |
---|
903 | PSI[7]=((int32)tmp*t_sin)>>15; |
---|
904 | } |
---|
905 | { |
---|
906 | /* ivec Ad(PSI,16); |
---|
907 | log_level.store(logA,get_from_ivec(Ad));*/ |
---|
908 | } |
---|
909 | |
---|
910 | ///////// end of copy /////////////// |
---|
911 | mmultACh(PSI,Chf,PSICh,4,4); |
---|
912 | //thorton(int16 *U, int16 *D, int16 *PSIU, int16 *Q, int16 *G, int16 *Dold, unsigned int16 dimx); |
---|
913 | //householder(PSICh,Q,4); |
---|
914 | givens_fast(PSICh,Q,4); |
---|
915 | // COPY |
---|
916 | for (int16 ii=0; ii<16; ii++){Chf[ii]=PSICh[ii];} |
---|
917 | |
---|
918 | { |
---|
919 | |
---|
920 | int Chi[16]; for (int i=0;i<16;i++) Chi[i]=Chf[i]; |
---|
921 | ivec Chd(Chi,16); |
---|
922 | log_level.store(logCh,get_from_ivec(Chd)); |
---|
923 | imat mCh(Chd._data(), 4,4); |
---|
924 | imat P = mCh*mCh.T(); |
---|
925 | ivec iP(P._data(),16); |
---|
926 | log_level.store(logP,get_from_ivec(iP)); |
---|
927 | } |
---|
928 | |
---|
929 | |
---|
930 | int16 difz[2]; |
---|
931 | difz[0]=(Y_mes[0]-x_est[0]); // shift to q15!! |
---|
932 | difz[1]=(Y_mes[1]-x_est[1]);//<<2; |
---|
933 | |
---|
934 | |
---|
935 | // |
---|
936 | int16 dR[2];dR[0]=R[0];dR[1]=R[3]; |
---|
937 | //int16 xb[4]; xb[0]=x_est[0]<<2; xb[1]=x_est[1]<<2; xb[2]=x_est[2]<<2; xb[3]=x_est[3]; |
---|
938 | |
---|
939 | carlson_fast(difz,x_est,Chf,dR,2,4); |
---|
940 | //x_est[0] = xb[0]>>2; x_est[1]=xb[1]>>2; x_est[2]=xb[2]>>2; x_est[3]=xb[3]; |
---|
941 | |
---|
942 | // navrat estimovanych hodnot regulatoru |
---|
943 | vec& mu = E._mu(); |
---|
944 | (mu)(0)=zprevod(x_est[0],15)*Iref; |
---|
945 | (mu)(1)=zprevod(x_est[1],15)*Iref; |
---|
946 | (mu)(2)=zprevod(x_est[2],15)*Wref; |
---|
947 | (mu)(3)=zprevod(x_est[3],15)*Thetaref; |
---|
948 | |
---|
949 | //x_est[2]=x[2]*32768/Wref; |
---|
950 | //x_est[3]=x[3]*32768/Thetaref; |
---|
951 | // mat T=diag(concat(vec_2(Iref,Iref), vec_2(Wref,Thetaref))); |
---|
952 | } |
---|
953 | |
---|
954 | void EKFfixedCh::init_ekf(double Tv) |
---|
955 | { |
---|
956 | // Tuning of matrix Q |
---|
957 | Q[0]=prevod(.01,15); // 0.05 |
---|
958 | Q[5]=Q[0]; |
---|
959 | Q[10]=prevod(0.0005,15); // 1e-3 |
---|
960 | Q[15]=prevod(0.001,15); // 1e-3 |
---|
961 | |
---|
962 | Chf[0]=0x3FFF;// ! // 0.05 |
---|
963 | Chf[1]=Chf[2]=Chf[3]=Chf[4]=0; |
---|
964 | Chf[5]=0x3FFF;//! |
---|
965 | Chf[6]=Chf[6]=Chf[8]=Chf[9]=0; |
---|
966 | Chf[10]=0x3FFF;//! // 1e-3 |
---|
967 | Chf[11]=Chf[12]=Chf[13]=Chf[4]=0; |
---|
968 | Chf[15]=0x3FFF; // 1e-3 |
---|
969 | |
---|
970 | // Tuning of matrix R |
---|
971 | R[0]=prevod(0.05,15); // 0.05 |
---|
972 | R[3]=R[0]; |
---|
973 | |
---|
974 | // Motor model parameters |
---|
975 | cA=prevod(1-Tv*Rs/Ls,15); |
---|
976 | cB=prevod(Tv*Wref*Fmag/Iref/Ls,15); |
---|
977 | cC=prevod(Tv/Ls/Iref*Uref,15); |
---|
978 | // cD=prevod(1-Tv*Bf/J,15); |
---|
979 | // cE=prevod(kp*p*p*Tv*Fmag*Iref/J/Wref,15); |
---|
980 | // cF=prevod(p*Tv*Mref/J/Wref,15); |
---|
981 | cG=prevod(Tv*Wref/Thetaref,15); //in q15! |
---|
982 | //cH=prevod(Tv*Wref*Fmag/Iref/Ls*Thetaref,15); |
---|
983 | cH=prevod(Tv*Wref*Thetaref*Fmag/Iref/Ls,15); // <======= |
---|
984 | //cH=prevod(Tv*Wref*Fmag/Iref/Ls,15); // |
---|
985 | // cI=prevod(kp*p*p*Tv*Fmag*Iref/J/Wref*Thetaref); |
---|
986 | |
---|
987 | /* Init matrix PSI with permanently constant terms */ |
---|
988 | PSI[0]=cA; |
---|
989 | PSI[5]=PSI[0]; |
---|
990 | PSI[10]=0x7FFF; |
---|
991 | PSI[14]=cG; |
---|
992 | PSI[15]=0x7FFF; |
---|
993 | |
---|
994 | } |
---|
995 | |
---|
996 | void EKFfixedCh2::bayes(const itpp::vec& yt, const itpp::vec& ut) |
---|
997 | { |
---|
998 | ekf2(ut[0],ut[1],yt[0],yt[1]); |
---|
999 | } |
---|
1000 | |
---|
1001 | |
---|
1002 | void EKFfixedCh2::ekf2(double ux, double uy, double isxd, double isyd) |
---|
1003 | { |
---|
1004 | // vypocet napeti v systemu (x,y) |
---|
1005 | int16 uf[2]; |
---|
1006 | uf[0]=prevod(ux/Uref,15); |
---|
1007 | uf[1]=prevod(uy/Uref,15); |
---|
1008 | |
---|
1009 | int16 Y_mes[2]; |
---|
1010 | // zadani mereni |
---|
1011 | Y_mes[0]=prevod(isxd/Iref,15); |
---|
1012 | Y_mes[1]=prevod(isyd/Iref,15); |
---|
1013 | |
---|
1014 | ////// vlastni rutina EKF -- ///////////////////////// |
---|
1015 | int16 t_sin,t_cos; |
---|
1016 | int32 tmp; |
---|
1017 | |
---|
1018 | x_est[0]=x_est[0]; |
---|
1019 | // q15 + q15*q13 |
---|
1020 | x_est[1]=(((int32)x_est[1]<<15)+(int32)cG*x_est[0])>>15; |
---|
1021 | |
---|
1022 | // if(x_est[1]>(1<<15)) x_est[1]-=2*(1<<15); |
---|
1023 | // if(x_est[1]<-(1<<15)) x_est[1]+=2*(1<<15); |
---|
1024 | |
---|
1025 | |
---|
1026 | ///////// end of copy /////////////// |
---|
1027 | mmultACh(PSI,Chf,PSICh,2,2); |
---|
1028 | |
---|
1029 | //void EKFfixed::update_psi(void) |
---|
1030 | { |
---|
1031 | int Ai[4]; |
---|
1032 | for (int i=0;i<4; i++) Ai[i]=(int)(PSICh[i]); |
---|
1033 | ivec Ad(Ai,4); |
---|
1034 | log_level.store(logA,get_from_ivec(Ad)); |
---|
1035 | } |
---|
1036 | |
---|
1037 | //thorton(int16 *U, int16 *D, int16 *PSIU, int16 *Q, int16 *G, int16 *Dold, unsigned int16 dimx); |
---|
1038 | givens_fast(PSICh,Q,2); |
---|
1039 | for (int i=0;i<4;i++) Chf[i]=PSICh[i]; |
---|
1040 | |
---|
1041 | // implementace v PC |
---|
1042 | /* t_sin=prevod(sin(Thetaref*x_est[3]/32768.),15); |
---|
1043 | * t_cos=prevod(cos(Thetaref*x_est[3]/32768.),15);*/ |
---|
1044 | tmp=prevod(sin(Thetaref*x_est[1]/32768.),15); |
---|
1045 | if (tmp>32767) t_sin =32767; else t_sin=tmp; |
---|
1046 | tmp=prevod(cos(Thetaref*x_est[1]/32768.),15); |
---|
1047 | if (tmp>32767) t_cos =32767; else t_cos=tmp; |
---|
1048 | |
---|
1049 | { |
---|
1050 | C[0]=((int32)cB*t_sin)>>15; |
---|
1051 | tmp=((int32)cH*x_est[0])>>15; // ! cH =cB with different scale!! |
---|
1052 | C[1]=((int32)tmp*t_cos)>>15; |
---|
1053 | C[2]=-((int32)cB*t_cos)>>15; |
---|
1054 | C[3]=((int32)tmp*t_sin)>>15; |
---|
1055 | } |
---|
1056 | |
---|
1057 | { |
---|
1058 | int Chi[4]; for (int i=0;i<4; i++) Chi[i]=(int)Chf[i]; |
---|
1059 | // int Ci[4]; for (int i=0;i<4; i++) Ci[i]=(int)C[i]; |
---|
1060 | |
---|
1061 | imat Chd(Chi,2,2); |
---|
1062 | // imat Cd(Ci,2,2); |
---|
1063 | imat CCh = Chd; |
---|
1064 | log_level.store(logCh,get_from_ivec(ivec(CCh._data(),CCh._datasize()))); |
---|
1065 | } |
---|
1066 | |
---|
1067 | |
---|
1068 | tmp=((int32)cB*x_est[0])>>15; // q15*q13 -> q13 |
---|
1069 | // q15*q13 + q13*q15 + q15*q13?? |
---|
1070 | y_est[0]=((int32)cA*y_old[0]+(int32)tmp*t_sin+(int32)cC*(uf[0]))>>15; // in q13 |
---|
1071 | y_est[1]=((int32)cA*y_old[1]-(int32)tmp*t_cos+(int32)cC*(uf[1]))>>15; // q13 |
---|
1072 | |
---|
1073 | |
---|
1074 | int16 difz[2]; |
---|
1075 | difz[0]=(Y_mes[0]-y_est[0]); // shift to q15!! |
---|
1076 | difz[1]=(Y_mes[1]-y_est[1]);//<<2; |
---|
1077 | |
---|
1078 | y_old[0] = Y_mes[0]; |
---|
1079 | y_old[1] = Y_mes[1]; |
---|
1080 | { |
---|
1081 | int Ci[4]; for (int i=0;i<4; i++) Ci[i]=(int)C[i]; |
---|
1082 | ivec CC(Ci,4);//;dd(0)=Y_mes[0];dd(1)=Y_mes[1]; dd(2)=difz[0]; dd(3)=difz[1]; |
---|
1083 | log_level.store(logC,get_from_ivec(CC)); |
---|
1084 | } |
---|
1085 | |
---|
1086 | //bierman(int16 *difz, int16 *xp, int16 *U, int16 *D, int16 *R, unsigned int16 dimy, unsigned int16 dimx ); |
---|
1087 | int16 dR[2];dR[0]=R[0];dR[1]=R[3]; |
---|
1088 | //int16 xb[4]; xb[0]=x_est[0]<<2; xb[1]=x_est[1]<<2; xb[2]=x_est[2]<<2; xb[3]=x_est[3]; |
---|
1089 | int16 rem; |
---|
1090 | int32 detS; |
---|
1091 | carlson_fastC(difz,x_est,Chf,C,dR,2,2, &detS, &rem); |
---|
1092 | //x_est[0] = xb[0]>>2; x_est[1]=xb[1]>>2; x_est[2]=xb[2]>>2; x_est[3]=xb[3]; |
---|
1093 | |
---|
1094 | log_level.store(logDet,vec_1((double)detS)); |
---|
1095 | log_level.store(logRem,vec_1((double)rem)); |
---|
1096 | |
---|
1097 | // navrat estimovanych hodnot regulatoru |
---|
1098 | vec& mu = E._mu(); |
---|
1099 | (mu)(0)=zprevod(x_est[0],15)*Wref; |
---|
1100 | (mu)(1)=zprevod(x_est[1],15)*Thetaref; |
---|
1101 | |
---|
1102 | //x_est[2]=x[2]*32768/Wref; |
---|
1103 | //x_est[3]=x[3]*32768/Thetaref; |
---|
1104 | // mat T=diag(concat(vec_2(Iref,Iref), vec_2(Wref,Thetaref))); |
---|
1105 | } |
---|
1106 | |
---|
1107 | void EKFfixedCh2::init_ekf2(double Tv) |
---|
1108 | { |
---|
1109 | // Tuning of matrix Q |
---|
1110 | Q[0]=prevod(0.005,15); // 1e-3 |
---|
1111 | Q[3]=prevod(0.001,15); // 1e-3 |
---|
1112 | |
---|
1113 | Chf[0]=0x1FFF;// ! // 0.05 |
---|
1114 | Chf[1]=Chf[2]=0; |
---|
1115 | Chf[3]=0x1FFF;//! |
---|
1116 | |
---|
1117 | // Tuning of matrix R |
---|
1118 | R[0]=prevod(0.001,15); // 0.05 |
---|
1119 | R[3]=R[0]; |
---|
1120 | |
---|
1121 | // Motor model parameters |
---|
1122 | cA=prevod(1-Tv*Rs/Ls,15); |
---|
1123 | cB=prevod(Tv*Wref*Fmag/Iref/Ls,15); |
---|
1124 | cC=prevod(Tv/Ls/Iref*Uref,15); |
---|
1125 | // cD=prevod(1-Tv*Bf/J,15); |
---|
1126 | // cE=prevod(kp*p*p*Tv*Fmag*Iref/J/Wref,15); |
---|
1127 | // cF=prevod(p*Tv*Mref/J/Wref,15); |
---|
1128 | cG=prevod(Tv*Wref/Thetaref,15); //in q15! |
---|
1129 | //cH=prevod(Tv*Wref*Fmag/Iref/Ls*Thetaref,15); |
---|
1130 | // cH=prevod(Tv*Wref*Thetaref*Fmag/Iref/Ls,15); // < =========== |
---|
1131 | cH=prevod(Tv*Wref*Fmag/Iref/Ls,15); // |
---|
1132 | // cI=prevod(kp*p*p*Tv*Fmag*Iref/J/Wref*Thetaref); |
---|
1133 | |
---|
1134 | /* Init matrix PSI with permanently constant terms */ |
---|
1135 | PSI[0]=0x7FFF; |
---|
1136 | PSI[2]=cG; |
---|
1137 | PSI[3]=0x7FFF; |
---|
1138 | } |
---|