root/applications/pmsm/simulator_zdenek/ekf_example/ekf_obj.cpp @ 1180

Revision 1180, 13.8 kB (checked in by smidl, 14 years ago)

remove printouts

  • Property svn:eol-style set to native
Line 
1
2#include <estim/kalman.h>
3
4#include "ekf_obj.h"
5#include "../simulator.h"
6
7double minQ(double Q){if (Q>1.0){ return 1.0;} else {return Q;};};
8
9void mat_to_int(const imat &M, int *I){
10        for (int i=0;i<M.rows(); i++){
11                for (int j=0;j<M.cols(); j++){
12                        *I++ = M(i,j);
13                }
14        }
15}
16void vec_to_int(const ivec &v, int *I){
17        for (int i=0;i<v.length(); i++){
18                        *I++ = v(i);
19        }
20}
21
22///////////////
23void EKFfixed::bayes(const vec &yt, const vec &ut){
24        ekf(yt(0),yt(1),ut(0),ut(1));
25       
26        vec xhat(4);   
27        //UGLY HACK!!! reliance on a predictor!!
28        xhat(0)=zprevod(x_est[0],Qm)*Iref;
29        xhat(1)=zprevod(x_est[1],Qm)*Iref;
30        xhat(2)=zprevod(x_est[2],Qm)*Wref;
31        xhat(3)=zprevod(x_est[3],15)*Thetaref;
32       
33        E.set_mu(xhat);
34       
35        if ( BM::evalll ) {
36                //from enorm
37                vec xdif(x,4);//first 4 of x
38                //UGLY HACK!!! reliance on a predictor!!
39/*              xdif(0)=x[0]-zprevod(x_pred[0],Qm)*Iref;
40                xdif(1)=x[1]-zprevod(x_pred[1],Qm)*Iref;
41                xdif(2)=x[2]-zprevod(x_pred[2],Qm)*Wref;
42                xdif(3)=x[3]-zprevod(x_pred[3],15);*/
43               
44                xdif -=xhat; //(xdif=x-xhat)
45               
46                mat Pfull(4,4);
47                double* Pp=Pfull._data();
48                for(int i=0;i<16;i++){*(Pp++) = zprevod(P_est[i],15);}
49               
50                E._R()._M()=Pfull;
51               
52               
53                BM::ll = -0.5* ( 4 * 1.83787706640935 +log ( det ( Pfull ) ) +xdif* ( inv(Pfull)*xdif ) );
54        }
55};
56
57
58void EKFfixed::update_psi(void)
59{
60  int t_sin,t_cos,tmp;
61
62  // implementace v PC
63  t_sin=prevod(sin(Thetaref*x_est[3]/32768.),15);
64  t_cos=prevod(cos(Thetaref*x_est[3]/32768.),15);
65
66  PSI[2]=((long)cB*t_sin)>>15;
67  tmp=((long)cH*x_est[2])>>15;
68  PSI[3]=((long)tmp*t_cos)>>15;
69  PSI[6]=-((long)cB*t_cos)>>15;
70  PSI[7]=((long)tmp*t_sin)>>15;
71}
72
73
74void EKFfixed::prediction(int *ux)
75{
76  int t_sin,t_cos, tmp;
77
78  // implementace v PC
79  t_sin=prevod(sin(Thetaref*x_est[3]/32768.),15);
80  t_cos=prevod(cos(Thetaref*x_est[3]/32768.),15);
81
82  tmp=((long)cB*x_est[2])>>15;
83  x_pred[0]=((long)cA*x_est[0]+(long)tmp*t_sin+(long)cC*ux[0])>>15;
84  x_pred[1]=((long)cA*x_est[1]-(long)tmp*t_cos+(long)cC*ux[1])>>15;
85  x_pred[2]=x_est[2];
86  x_pred[3]=(((long)x_est[3]<<15)+(long)cG*x_est[2])>>15;
87
88  update_psi();
89
90  mmult(PSI,P_est,temp15a,3,3,3);
91//  mtrans(PSI,temp15b,5,5);
92  mmultt(temp15a,PSI,P_pred,3,3,3);
93  maddD(P_pred,Q,3,3);
94}
95
96void EKFfixed::correction(void)
97{
98  int Y_error[2];
99  long temp30a[4]; /* matrix [2,2] - temporary matrix for inversion */
100
101  choice_P(P_pred,temp15a,3);
102  maddD(temp15a,R,1,1);
103  minv2(temp15a,temp30a);
104        Ry(0,0)=zprevod(temp15a[0],15);
105        Ry(0,1)=zprevod(temp15a[1],15);
106        Ry(1,0)=zprevod(temp15a[2],15);
107        Ry(1,1)=zprevod(temp15a[3],15);
108 
109  mmultDr(P_pred,temp15a,3,3,1,1);
110  mmult1530(temp15a,temp30a,Kalm,3,1,1);
111
112
113  /* estimate the state system */
114  choice_x(x_pred, temp15a);
115  msub(Y_mes,temp15a,Y_error,1,0);
116  mmult(Kalm,Y_error,temp15a,3,1,0);
117  madd(x_pred,temp15a,x_est,3,0);
118
119  /* matrix of covariances - version without MMULTDL() */
120
121/* Version with MMULTDL() */
122  mmultDl(P_pred,temp15a,1,3,3,1);
123
124  mmult(Kalm,temp15a,P_est,3,1,3);
125  msub(P_pred,P_est,P_est,3,3);
126/* END */
127}
128
129
130void EKFfixed::ekf(double ux, double uy, double isxd, double isyd)
131{
132  // vypocet napeti v systemu (x,y)
133  ukalm[0]=prevod(ux/Uref,Qm);
134  ukalm[1]=prevod(uy/Uref,Qm);
135
136  // zadani mereni
137  Y_mes[0]=prevod(isxd/Iref,Qm);
138  Y_mes[1]=prevod(isyd/Iref,Qm);
139
140  ////// vlastni rutina EKF /////////////////////////
141  prediction(ukalm);
142  correction();
143
144  // navrat estimovanych hodnot regulatoru
145  vec& mu = E._mu();
146  (mu)(0)=zprevod(x_est[0],Qm)*Iref;
147  (mu)(1)=zprevod(x_est[1],Qm)*Iref;
148  (mu)(2)=zprevod(x_est[2],Qm)*Wref;
149  (mu)(3)=zprevod(x_est[3],15)*Thetaref;
150}
151
152void EKFfixed::init_ekf(double Tv)
153{
154  // Tuning of matrix Q
155  Q[0]=prevod(.01,15);       // 0.05
156  Q[5]=Q[0];
157  Q[10]=prevod(0.0001,15);      // 1e-3
158  Q[15]=prevod(0.0001,15);      // 1e-3
159
160  // Tuning of matrix R
161  R[0]=prevod(0.05,15);         // 0.05
162  R[3]=R[0];
163
164  // Motor model parameters
165  cA=prevod(1-Tv*Rs/Ls,15);
166  cB=prevod(Tv*Wref*Fmag/Iref/Ls,15);
167  cC=prevod(Tv/Ls/Iref*Uref,15);
168//  cD=prevod(1-Tv*Bf/J,15);
169//  cE=prevod(kp*p*p*Tv*Fmag*Iref/J/Wref,15);
170//  cF=prevod(p*Tv*Mref/J/Wref,15);
171  cG=prevod(Tv*Wref*4/Thetaref,15);
172  cH=prevod(Tv*Wref*Fmag/Iref/Ls*Thetaref,15);
173//  cI=prevod(kp*p*p*Tv*Fmag*Iref/J/Wref*Thetaref);
174
175  /* Init matrix PSI with permanently constant terms */
176  PSI[0]=cA;
177  PSI[5]=PSI[0];
178  PSI[10]=0x7FFF;
179  PSI[14]=cG;
180  PSI[15]=0x7FFF;
181}
182
183
184void EKF_UDfix::set_parameters ( const shared_ptr<diffbifn> &pfxu0, const shared_ptr<diffbifn> &phxu0, const mat Q0, const vec R0 ) {
185        pfxu = pfxu0;
186        phxu = phxu0;
187       
188        set_dim ( pfxu0->_dimx() );
189        dimy = phxu0->dimension();
190        dimc = pfxu0->_dimu();
191       
192        vec &_mu = est._mu();
193        // if mu is not set, set it to zeros, just for constant terms of A and C
194        if ( _mu.length() != dimension() ) _mu = zeros ( dimension() );
195        A = zeros ( dimension(), dimension() );
196        C = zeros ( dimy, dimension() );
197       
198        //initialize matrices A C, later, these will be only updated!
199        pfxu->dfdx_cond ( _mu, zeros ( dimc ), A, true );
200        //      pfxu->dfdu_cond ( *_mu,zeros ( dimu ),B,true );
201        phxu->dfdx_cond ( _mu, zeros ( dimc ), C, true );
202        //      phxu->dfdu_cond ( *_mu,zeros ( dimu ),D,true );
203       
204        R = R0;
205        Q = Q0;
206       
207        //
208}
209// aux fnc
210void UDtof(const mat &U, const vec &D, imat &Uf, ivec &Df, const vec &xref){
211        mat P= U*diag(D)*U.T();
212        mat T = diag(1.0/(xref));
213        mat Pf = T*P*T;
214       
215        ldmat Pld(Pf);
216       
217        mat Ut=Pld._L().T()*(1<<15); // U is in q15 -- diagonal is 0!!!
218        Uf=round_i(Ut);
219        Df=round_i(Pld._D()*(1<<15));
220        ivec zer=find(Df==0);
221        for(int i=0; i<zer.length(); i++) Df(zer(i))=1;
222}
223
224
225void EKF_UDfix::bayes ( const vec &yt, const vec &cond ) {
226        //preparatory
227        vec &_mu=est._mu();
228        const vec &u=cond;
229        int dim = dimension();
230       
231        U = est._R()._L().T();
232        D =  est._R()._D();
233       
234        ////////////
235       
236        pfxu->dfdx_cond ( _mu, u, A, false ); //update A by a derivative of fx
237        phxu->dfdx_cond ( _mu, u, C, false ); //update A by a derivative of fx
238       
239        mat PhiU = A*U;
240
241        //////
242        vec xref(5);
243        xref(0)= 30.0*1.4142;
244        xref(1)= 30.0*1.4142;
245        xref(2)= 6.283185*200.;
246        xref(3) = 3.141593;
247        xref(4) = 34.0;
248       
249        imat Utf;
250        ivec Dtf;
251        UDtof(U,D,Utf,Dtf,xref);
252        mat invxref=diag(1.0/xref);
253        imat Af=round_i(invxref*A*diag(xref)*(1<<15));
254        mat_to_int(Af, PSI);
255        mat_to_int(Utf,Uf);             
256        vec_to_int(Dtf, Df); 
257       
258        // A*U == PSI*U
259        mmultAU(PSI,Uf,PSIU,5,5);       
260/*      cout << ivec(PSIU,25) << endl;
261        cout << (Af*Utf)/(1<<15) <<endl;*/
262       
263        vec Din = D;
264        int i,j,k;
265        double sigma;
266        mat G = eye(dim);
267        //////// thorton
268       
269        //move mean;
270        _mu = pfxu->eval(_mu,u);
271       
272        for (i=dim-1; i>=0;i--){
273                sigma = 0.0; 
274                for (j=0; j<dim; j++) {
275                        sigma += PhiU(i,j)*PhiU(i,j) *Din(j); 
276                        sigma += G(i,j)*G(i,j) * Q(j,j);
277                }
278               
279/*              double sigma2= 0.0;
280                for (j=0; j<dim; j++) {
281                        sigma2 += PhiU(i,j)*PhiU(i,j) *Din(j);
282                }
283                sigma2 +=Q(i,i);//*G(i,i)=1.0
284                for (j=i+1; j<dim; j++) {
285                        sigma2 += G(i,j)*G(i,j) * Q(j,j);
286                }*/
287               
288/*              UDtof(U,D,Utf,Dtf,xref);
289                cout << "d=sig"<<endl;
290                cout << Utf << endl << Dtf << endl;
291                cout << G << endl << Din << endl<<endl;*/
292               
293                D(i) = sigma; 
294                for (j=0;j<i;j++){ 
295//                      cout << i << "," << j << endl;
296                        sigma = 0.0; 
297                        for (k=0;k<dim;k++){ 
298                                sigma += PhiU(i,k)*Din(k)*PhiU(j,k); 
299                        }
300                        for (k=0;k<dim;k++){ 
301                                sigma += G(i,k)*Q(k,k)*G(j,k); 
302                        }
303                        //
304                        U(j,i) = sigma/D(i); 
305                       
306/*                      cout << "U=sig/D"<<endl;
307                        UDtof(U,D,Utf,Dtf,xref);
308                        cout << Utf << endl << Dtf << endl;
309                        cout << G << endl << Din << endl<<endl;*/
310                       
311                        for (k=0;k<dim;k++){ 
312                                PhiU(j,k) = PhiU(j,k) - U(j,i)*PhiU(i,k); 
313                        }
314                        for (k=0;k<dim;k++){ 
315                                G(j,k) = G(j,k) - U(j,i)*G(i,k); 
316                        }
317               
318/*                      cout << "end"<<endl;
319                        UDtof(U,D,Utf,Dtf,xref);
320                        cout << G << endl << Din << endl;
321                        cout << Utf << endl << Dtf << endl<<endl;*/
322                }
323        }
324
325        int Qf[25];
326        imat Qrnd=round_i(diag(elem_div(diag(Q),pow(xref,2)))*(1<<15));
327//      cout << "Qr" << Qrnd<<endl;
328        mat_to_int(Qrnd,Qf);
329
330        thorton(Uf,Df,PSIU,Qf,Gf,Dfold,5);
331       
332        cout << "bierman double I" <<endl;
333        UDtof(U,D,Utf,Dtf,xref);
334        cout << "Uf: " << Utf << endl;
335        cout << "Df: " << Dtf << endl;
336        cout << "xf:" << round_i(elem_div(_mu,xref)*(1<<15)) <<endl;
337        mat_to_int(Utf,Uf);             
338        vec_to_int(Dtf, Df); 
339       
340        // bierman
341       
342        double dz,alpha,gamma,beta,lambda;
343        vec a;
344        vec b;
345        vec yp = phxu->eval(_mu,u);
346        vec xp=_mu; // used in bierman
347       
348        for (int iy=0; iy<dimy; iy++){
349                a     = U.T()*C.get_row(iy);    // a is not modified, but
350                b     = elem_mult(D,a);                          // b is modified to become unscaled Kalman gain.
351                dz    = yt(iy) - yp(iy); 
352               
353                alpha = R(iy); 
354                gamma = 1/alpha; 
355                for (j=0;j<dim;j++){
356                        beta   = alpha; 
357                        alpha  = alpha + a(j)*b(j); 
358                        lambda = -a(j)*gamma; 
359                        gamma  = 1.0/alpha; 
360                        D(j) = beta*gamma*D(j); 
361                       
362                        //                      cout << "a: " << alpha << "g: " << gamma << endl;
363                        for (i=0;i<j;i++){
364                                beta   = U(i,j); 
365                                U(i,j) = beta + b(i)*lambda; 
366                                b(i)   = b(i) + b(j)*beta; 
367                        }
368                }
369                double dzs = gamma*dz;  // apply scaling to innovations
370                _mu   = _mu + dzs*b; // multiply by unscaled Kalman gain
371                //cout << "Ub: " << U << endl;
372                //cout << "Db: " << D << endl <<endl;
373               
374        }
375
376UDtof(U,D,Utf,Dtf,xref);
377cout << "Uf: " << Utf << endl;
378cout << "Df: " << Dtf << endl;
379cout << "xf:" << round_i(elem_div(_mu,xref)*(1<<15)) <<endl;
380
381        int difz[2];
382        vec_to_int(round_i((yt-yp)/xref(0)*(1<<15)), difz);
383        int xf[5];
384        vec_to_int(round_i(elem_div(xp,xref)*(1<<15)), xf);
385        int Rf[2]={1,1};
386        //vec_to_int(round_i(R/pow(xref(0),2)*(1<<15)), Rf);
387        //beirman(int *difz, int *xp, int *U, int *D, int *R, unsigned int dimy, unsigned int dimx
388       
389        cout <<endl<< "bierman fixed" <<endl;
390        for (i=0; i<25;i++) cout << Uf[i] << ","; cout <<endl;
391        for (i=0; i<5;i++) cout << Df[i] << ","; cout << endl;
392        for (i=0; i<5;i++) cout << xf[i] << ","; cout << endl;
393
394        for (i=0; i<2;i++) cout << difz[i] << ","; cout << endl;
395
396        int xf_old[5];
397        vec_to_int(ivec(xf,5),xf_old);
398        bierman(difz,xf, Uf, Df, Rf, 2, 5);
399       
400        UDtof(U,D,Utf,Dtf,xref);
401        for (i=0; i<25;i++) cout << Uf[i] << ","; cout <<endl;
402        for (i=0; i<5;i++) cout << Df[i] << ","; cout << endl;
403        for (i=0; i<5;i++) cout << xf[i] << ","; cout << endl;
404        cout << endl;
405       
406       
407       
408        /////
409        est._R().__L()=U.T();
410        est._R().__D()=D;
411       
412        if ( evalll == true ) { //likelihood of observation y
413        }
414}
415
416void EKF_UDfix::from_setting ( const Setting &set ) {
417        BM::from_setting ( set );
418        shared_ptr<diffbifn> IM = UI::build<diffbifn> ( set, "IM", UI::compulsory );
419        shared_ptr<diffbifn> OM = UI::build<diffbifn> ( set, "OM", UI::compulsory );
420       
421        //statistics
422        int dim = IM->dimension();
423        vec mu0;
424        if ( !UI::get ( mu0, set, "mu0" ) )
425                mu0 = zeros ( dim );
426       
427        mat P0;
428        vec dP0;
429        if ( UI::get ( dP0, set, "dP0" ) )
430                P0 = diag ( dP0 );
431        else if ( !UI::get ( P0, set, "P0" ) )
432                P0 = eye ( dim );
433       
434        est._mu()=mu0;
435        est._R()=ldmat(P0);
436       
437        //parameters
438        vec dQ, dR;
439        UI::get ( dQ, set, "dQ", UI::compulsory );
440        UI::get ( dR, set, "dR", UI::compulsory );
441        set_parameters ( IM, OM, diag ( dQ ), dR  );
442       
443        UI::get(log_level, set, "log_level", UI::optional);
444}
445
446
447void EKFfixedUD::bayes(const itpp::vec& yt, const itpp::vec& ut)
448{
449ekf(ut[0],ut[1],yt[0],yt[1]);
450}
451
452
453void EKFfixedUD::ekf(double ux, double uy, double isxd, double isyd)
454{
455        // vypocet napeti v systemu (x,y)
456        int uf[2];
457        uf[0]=prevod(ux/Uref,Qm);
458        uf[1]=prevod(uy/Uref,Qm);
459       
460        int Y_mes[2];
461        // zadani mereni
462        Y_mes[0]=prevod(isxd/Iref,Qm);
463        Y_mes[1]=prevod(isyd/Iref,Qm);
464       
465        ////// vlastni rutina EKF -- /////////////////////////
466        int t_sin,t_cos, tmp;
467       
468        // implementace v PC
469        t_sin=prevod(sin(Thetaref*x_est[3]/32768.),15);
470        t_cos=prevod(cos(Thetaref*x_est[3]/32768.),15);
471       
472        tmp=((long)cB*x_est[2])>>15;
473        x_est[0]=((long)cA*x_est[0]+(long)tmp*t_sin+(long)cC*uf[0])>>15;
474        x_est[1]=((long)cA*x_est[1]-(long)tmp*t_cos+(long)cC*uf[1])>>15;
475        x_est[2]=x_est[2];
476        x_est[3]=(((long)x_est[3]<<15)+(long)cG*x_est[2])>>15;
477       
478        //void EKFfixed::update_psi(void)
479        {               
480                PSI[2]=((long)cB*t_sin)>>15;
481                tmp=((long)cH*x_est[2])>>15;
482                PSI[3]=((long)tmp*t_cos)>>15;
483                PSI[6]=-((long)cB*t_cos)>>15;
484                PSI[7]=((long)tmp*t_sin)>>15;
485        }
486       
487        ///////// end of copy ///////////////
488        mmultAU(PSI,Uf,PSIU,4,4);
489        //thorton(int *U, int *D, int *PSIU, int *Q, int *G, int *Dold, unsigned int dimx);
490        thorton(Uf,Df,PSIU,Q,G,Dfold,4);
491       
492        int difz[2];
493        difz[0]=Y_mes[0]-x_est[0];
494        difz[1]=Y_mes[1]-x_est[1];
495        //bierman(int *difz, int *xp, int *U, int *D, int *R, unsigned int dimy, unsigned int dimx );
496        int dR[2];dR[0]=R[0];dR[1]=R[3];
497        bierman(difz,x_est,Uf,Df,dR,2,4);
498       
499        // navrat estimovanych hodnot regulatoru
500        vec& mu = E._mu();
501        (mu)(0)=zprevod(x_est[0],Qm)*Iref;
502        (mu)(1)=zprevod(x_est[1],Qm)*Iref;
503        (mu)(2)=zprevod(x_est[2],Qm)*Wref;
504        (mu)(3)=zprevod(x_est[3],15)*Thetaref;
505       
506//      mat T=diag(concat(vec_2(Iref,Iref), vec_2(Wref,Thetaref)));
507}
508
509void EKFfixedUD::init_ekf(double Tv)
510{
511        // Tuning of matrix Q
512        Q[0]=prevod(.01,15);       // 0.05
513        Q[5]=Q[0];
514        Q[10]=prevod(0.0001,15);      // 1e-3
515        Q[15]=prevod(0.0001,15);      // 1e-3
516
517        Uf[0]=0x7FFF;       // 0.05
518        Uf[5]=0x7FFF;
519        Uf[10]=0x7FFF;      // 1e-3
520        Uf[15]=0x7FFF;      // 1e-3
521       
522        Df[0]=0x7FFF;
523        Df[1]=0x7FFF;
524        Df[2]=0x7FFF;
525        Df[3]=0x7FFF;
526       
527        // Tuning of matrix R
528        R[0]=prevod(0.05,15);         // 0.05
529        R[3]=R[0];
530       
531        // Motor model parameters
532        cA=prevod(1-Tv*Rs/Ls,15);
533        cB=prevod(Tv*Wref*Fmag/Iref/Ls,15);
534        cC=prevod(Tv/Ls/Iref*Uref,15);
535        //  cD=prevod(1-Tv*Bf/J,15);
536        //  cE=prevod(kp*p*p*Tv*Fmag*Iref/J/Wref,15);
537        //  cF=prevod(p*Tv*Mref/J/Wref,15);
538        cG=prevod(Tv*Wref*4/Thetaref,15);
539        cH=prevod(Tv*Wref*Fmag/Iref/Ls*Thetaref,15);
540        //  cI=prevod(kp*p*p*Tv*Fmag*Iref/J/Wref*Thetaref);
541       
542        /* Init matrix PSI with permanently constant terms */
543        PSI[0]=cA;
544        PSI[5]=PSI[0];
545        PSI[10]=0x7FFF;
546        PSI[14]=cG;
547        PSI[15]=0x7FFF;
548       
549        //////////////////////// =================
550        ///// TEST thorton vs. thorton_fast
551       
552/*      int Ut[16]={0x7FFF, 100, 200, 300, 0, 0x7FFF, 500, 600, 0,0,0x7FFF, 800, 0,0,0, 0x7FFF};
553        int Dt[4]={100,200,300,400};
554        int PSIu[16] = {100, 200,300, 400 , 500, 600,700,800, 900,1000,1100,1200, 1300,1400,1500,1600};
555        int Dold[4];
556       
557        thorton(Ut,Dt,PSIu,Q,G,Dold,4);
558        int Ut2[16]={0x7FFF, 100, 200, 300, 0, 0x7FFF, 500, 600, 0,0,0x7FFF, 800, 0,0,0, 0x7FFF};
559        int Dt2[4]={100,200,300,400};
560        int PSIu2[16] = {100, 200,300, 400 , 500, 600,700,800, 900,1000,1100,1200, 1300,1400,1500,1600};
561        thorton_fast(Ut2,Dt2,PSIu2,Q,G,Dold,4);
562        cout<< Q<<endl;*/
563}
Note: See TracBrowser for help on using the browser.