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

Revision 1182, 14.0 kB (checked in by smidl, 14 years ago)

bug in sigma += Q(i) found

  • 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        if (false){
234                mat L;
235                ivec tmp;
236                mat X=randn(5,5);
237                mat XX=X*X.T();
238                mat T=diag(sqrt(1.0/diag(XX)));
239                XX= T*XX*T;
240               
241                ldmat ldd(XX);
242                U = ldd._L().T();
243                D = ldd._D()*0.9;
244        }
245               
246        ////////////
247       
248        pfxu->dfdx_cond ( _mu, u, A, false ); //update A by a derivative of fx
249        phxu->dfdx_cond ( _mu, u, C, false ); //update A by a derivative of fx
250       
251        mat PhiU = A*U;
252
253        //////
254        vec xref(5);
255        xref(0)= 30.0*1.4142;
256        xref(1)= 30.0*1.4142;
257        xref(2)= 6.283185*200.;
258        xref(3) = 3.141593;
259        xref(4) = 34.0;
260       
261        if (false){
262                xref = ones(5);
263        }
264       
265        imat Utf;
266        ivec Dtf;
267        UDtof(U,D,Utf,Dtf,xref);
268        mat invxref=diag(1.0/xref);
269        imat Af=round_i(invxref*A*diag(xref)*(1<<15));
270        mat_to_int(Af, PSI);
271        mat_to_int(Utf,Uf);             
272        vec_to_int(Dtf, Df); 
273
274//      cout << Dtf << endl;
275       
276        // A*U == PSI*U
277        mmultAU(PSI,Uf,PSIU,5,5);       
278/*      cout << ivec(PSIU,25) << endl;
279        cout << (Af*Utf)/(1<<15) <<endl;*/
280       
281        vec Din = D;
282        int i,j,k;
283        double sigma;
284        mat G = eye(dim);
285        //////// thorton
286       
287        //move mean;
288        _mu = pfxu->eval(_mu,u);
289       
290        for (i=dim-1; i>=0;i--){
291                sigma = 0.0; 
292                for (j=0; j<dim; j++) {
293                        sigma += PhiU(i,j)*PhiU(i,j) *Din(j); 
294                        sigma += G(i,j)*G(i,j) * Q(j,j);
295                }
296               
297/*              double sigma2= 0.0;
298                for (j=0; j<dim; j++) {
299                        sigma2 += PhiU(i,j)*PhiU(i,j) *Din(j);
300                }
301                sigma2 +=Q(i,i);//*G(i,i)=1.0
302                for (j=i+1; j<dim; j++) {
303                        sigma2 += G(i,j)*G(i,j) * Q(j,j);
304                }*/
305                D(i) = sigma; 
306               
307        /*      UDtof(U,D,Utf,Dtf,xref);
308                cout << "d=sig"<<endl;
309                cout << Dtf << endl;
310        */     
311                for (j=0;j<i;j++){ 
312//                      cout << i << "," << j << endl;
313                        sigma = 0.0; 
314                        for (k=0;k<dim;k++){ 
315                                sigma += PhiU(i,k)*Din(k)*PhiU(j,k); 
316                        }
317                        for (k=0;k<dim;k++){ 
318                                sigma += G(i,k)*Q(k,k)*G(j,k); 
319                        }
320                        //
321                        U(j,i) = sigma/D(i); 
322                       
323/*                      cout << "U=sig/D"<<endl;
324                        UDtof(U,D,Utf,Dtf,xref);
325                        cout << Utf << endl << Dtf << endl;
326                        cout << G << endl << Din << endl<<endl;*/
327                       
328                        for (k=0;k<dim;k++){ 
329                                PhiU(j,k) = PhiU(j,k) - U(j,i)*PhiU(i,k); 
330                        }
331                        for (k=0;k<dim;k++){ 
332                                G(j,k) = G(j,k) - U(j,i)*G(i,k); 
333                        }
334               
335                }
336                UDtof(U,D,Utf,Dtf,xref);
337                //      cout << G << endl << Din << endl;
338                imat Urw=Utf.T();
339                cout << ivec(Urw._data(),25) << endl << Dtf << endl;
340        }
341
342        int Qf[25];
343        imat Qrnd=round_i(diag(elem_div(diag(Q),pow(xref,2)))*(1<<15));
344//      cout << "Qr" << Qrnd<<endl;
345        mat_to_int(Qrnd,Qf);
346
347//      cout << Df <<endl;
348        thorton(Uf,Df,PSIU,Qf,Gf,Dfold,5);
349       
350/*      cout << "bierman double I" <<endl;
351        UDtof(U,D,Utf,Dtf,xref);
352        cout << "Uf: " << Utf << endl;
353        cout << "Df: " << Dtf << endl;
354        cout << "xf:" << round_i(elem_div(_mu,xref)*(1<<15)) <<endl;*/
355        mat_to_int(Utf,Uf);             
356        vec_to_int(Dtf, Df); 
357       
358        // bierman
359       
360        double dz,alpha,gamma,beta,lambda;
361        vec a;
362        vec b;
363        vec yp = phxu->eval(_mu,u);
364        vec xp=_mu; // used in bierman
365       
366        for (int iy=0; iy<dimy; iy++){
367                a     = U.T()*C.get_row(iy);    // a is not modified, but
368                b     = elem_mult(D,a);                          // b is modified to become unscaled Kalman gain.
369                dz    = yt(iy) - yp(iy); 
370               
371                alpha = R(iy); 
372                gamma = 1/alpha; 
373                for (j=0;j<dim;j++){
374                        beta   = alpha; 
375                        alpha  = alpha + a(j)*b(j); 
376                        lambda = -a(j)*gamma; 
377                        gamma  = 1.0/alpha; 
378                        D(j) = beta*gamma*D(j); 
379                       
380                        //                      cout << "a: " << alpha << "g: " << gamma << endl;
381                        for (i=0;i<j;i++){
382                                beta   = U(i,j); 
383                                U(i,j) = beta + b(i)*lambda; 
384                                b(i)   = b(i) + b(j)*beta; 
385                        }
386                }
387                double dzs = gamma*dz;  // apply scaling to innovations
388                _mu   = _mu + dzs*b; // multiply by unscaled Kalman gain
389                //cout << "Ub: " << U << endl;
390                //cout << "Db: " << D << endl <<endl;
391               
392        }
393
394// UDtof(U,D,Utf,Dtf,xref);
395// cout << "Uf: " << Utf << endl;
396// cout << "Df: " << Dtf << endl;
397// cout << "xf:" << round_i(elem_div(_mu,xref)*(1<<15)) <<endl;
398
399        int difz[2];
400        vec_to_int(round_i((yt-yp)/xref(0)*(1<<15)), difz);
401        int xf[5];
402        vec_to_int(round_i(elem_div(xp,xref)*(1<<15)), xf);
403        int Rf[2]={1,1};
404        //vec_to_int(round_i(R/pow(xref(0),2)*(1<<15)), Rf);
405        //beirman(int *difz, int *xp, int *U, int *D, int *R, unsigned int dimy, unsigned int dimx
406       
407//      cout <<endl<< "bierman fixed" <<endl;
408//      for (i=0; i<25;i++) cout << Uf[i] << ","; cout <<endl;
409//      for (i=0; i<5;i++) cout << Df[i] << ","; cout << endl;
410//      for (i=0; i<5;i++) cout << xf[i] << ","; cout << endl;
411//
412//      for (i=0; i<2;i++) cout << difz[i] << ","; cout << endl;
413
414        int xf_old[5];
415        vec_to_int(ivec(xf,5),xf_old);
416        bierman(difz,xf, Uf, Df, Rf, 2, 5);
417       
418        UDtof(U,D,Utf,Dtf,xref);
419/*      for (i=0; i<25;i++) cout << Uf[i] << ","; cout <<endl;
420        for (i=0; i<5;i++) cout << Df[i] << ","; cout << endl;
421        for (i=0; i<5;i++) cout << xf[i] << ","; cout << endl;
422        cout << endl;*/
423       
424       
425       
426        /////
427        est._R().__L()=U.T();
428        est._R().__D()=D;
429       
430        if ( evalll == true ) { //likelihood of observation y
431        }
432}
433
434void EKF_UDfix::from_setting ( const Setting &set ) {
435        BM::from_setting ( set );
436        shared_ptr<diffbifn> IM = UI::build<diffbifn> ( set, "IM", UI::compulsory );
437        shared_ptr<diffbifn> OM = UI::build<diffbifn> ( set, "OM", UI::compulsory );
438       
439        //statistics
440        int dim = IM->dimension();
441        vec mu0;
442        if ( !UI::get ( mu0, set, "mu0" ) )
443                mu0 = zeros ( dim );
444       
445        mat P0;
446        vec dP0;
447        if ( UI::get ( dP0, set, "dP0" ) )
448                P0 = diag ( dP0 );
449        else if ( !UI::get ( P0, set, "P0" ) )
450                P0 = eye ( dim );
451       
452        est._mu()=mu0;
453        est._R()=ldmat(P0);
454       
455        //parameters
456        vec dQ, dR;
457        UI::get ( dQ, set, "dQ", UI::compulsory );
458        UI::get ( dR, set, "dR", UI::compulsory );
459        set_parameters ( IM, OM, diag ( dQ ), dR  );
460       
461        UI::get(log_level, set, "log_level", UI::optional);
462}
463
464
465void EKFfixedUD::bayes(const itpp::vec& yt, const itpp::vec& ut)
466{
467ekf(ut[0],ut[1],yt[0],yt[1]);
468}
469
470
471void EKFfixedUD::ekf(double ux, double uy, double isxd, double isyd)
472{
473        // vypocet napeti v systemu (x,y)
474        int uf[2];
475        uf[0]=prevod(ux/Uref,Qm);
476        uf[1]=prevod(uy/Uref,Qm);
477       
478        int Y_mes[2];
479        // zadani mereni
480        Y_mes[0]=prevod(isxd/Iref,Qm);
481        Y_mes[1]=prevod(isyd/Iref,Qm);
482       
483        ////// vlastni rutina EKF -- /////////////////////////
484        int t_sin,t_cos, tmp;
485       
486        // implementace v PC
487        t_sin=prevod(sin(Thetaref*x_est[3]/32768.),15);
488        t_cos=prevod(cos(Thetaref*x_est[3]/32768.),15);
489       
490        tmp=((long)cB*x_est[2])>>15;
491        x_est[0]=((long)cA*x_est[0]+(long)tmp*t_sin+(long)cC*uf[0])>>15;
492        x_est[1]=((long)cA*x_est[1]-(long)tmp*t_cos+(long)cC*uf[1])>>15;
493        x_est[2]=x_est[2];
494        x_est[3]=(((long)x_est[3]<<15)+(long)cG*x_est[2])>>15;
495       
496        //void EKFfixed::update_psi(void)
497        {               
498                PSI[2]=((long)cB*t_sin)>>15;
499                tmp=((long)cH*x_est[2])>>15;
500                PSI[3]=((long)tmp*t_cos)>>15;
501                PSI[6]=-((long)cB*t_cos)>>15;
502                PSI[7]=((long)tmp*t_sin)>>15;
503        }
504       
505        ///////// end of copy ///////////////
506        mmultAU(PSI,Uf,PSIU,4,4);
507        //thorton(int *U, int *D, int *PSIU, int *Q, int *G, int *Dold, unsigned int dimx);
508        thorton(Uf,Df,PSIU,Q,G,Dfold,4);
509       
510        int difz[2];
511        difz[0]=Y_mes[0]-x_est[0];
512        difz[1]=Y_mes[1]-x_est[1];
513        //bierman(int *difz, int *xp, int *U, int *D, int *R, unsigned int dimy, unsigned int dimx );
514        int dR[2];dR[0]=R[0];dR[1]=R[3];
515        bierman(difz,x_est,Uf,Df,dR,2,4);
516       
517        // navrat estimovanych hodnot regulatoru
518        vec& mu = E._mu();
519        (mu)(0)=zprevod(x_est[0],Qm)*Iref;
520        (mu)(1)=zprevod(x_est[1],Qm)*Iref;
521        (mu)(2)=zprevod(x_est[2],Qm)*Wref;
522        (mu)(3)=zprevod(x_est[3],15)*Thetaref;
523       
524//      mat T=diag(concat(vec_2(Iref,Iref), vec_2(Wref,Thetaref)));
525}
526
527void EKFfixedUD::init_ekf(double Tv)
528{
529        // Tuning of matrix Q
530        Q[0]=prevod(.01,15);       // 0.05
531        Q[5]=Q[0];
532        Q[10]=prevod(0.0001,15);      // 1e-3
533        Q[15]=prevod(0.0001,15);      // 1e-3
534
535        Uf[0]=0x7FFF;       // 0.05
536        Uf[5]=0x7FFF;
537        Uf[10]=0x7FFF;      // 1e-3
538        Uf[15]=0x7FFF;      // 1e-3
539       
540        Df[0]=0x7FFF;
541        Df[1]=0x7FFF;
542        Df[2]=0x7FFF;
543        Df[3]=0x7FFF;
544       
545        // Tuning of matrix R
546        R[0]=prevod(0.05,15);         // 0.05
547        R[3]=R[0];
548       
549        // Motor model parameters
550        cA=prevod(1-Tv*Rs/Ls,15);
551        cB=prevod(Tv*Wref*Fmag/Iref/Ls,15);
552        cC=prevod(Tv/Ls/Iref*Uref,15);
553        //  cD=prevod(1-Tv*Bf/J,15);
554        //  cE=prevod(kp*p*p*Tv*Fmag*Iref/J/Wref,15);
555        //  cF=prevod(p*Tv*Mref/J/Wref,15);
556        cG=prevod(Tv*Wref*4/Thetaref,15);
557        cH=prevod(Tv*Wref*Fmag/Iref/Ls*Thetaref,15);
558        //  cI=prevod(kp*p*p*Tv*Fmag*Iref/J/Wref*Thetaref);
559       
560        /* Init matrix PSI with permanently constant terms */
561        PSI[0]=cA;
562        PSI[5]=PSI[0];
563        PSI[10]=0x7FFF;
564        PSI[14]=cG;
565        PSI[15]=0x7FFF;
566       
567        //////////////////////// =================
568        ///// TEST thorton vs. thorton_fast
569       
570/*      int Ut[16]={0x7FFF, 100, 200, 300, 0, 0x7FFF, 500, 600, 0,0,0x7FFF, 800, 0,0,0, 0x7FFF};
571        int Dt[4]={100,200,300,400};
572        int PSIu[16] = {100, 200,300, 400 , 500, 600,700,800, 900,1000,1100,1200, 1300,1400,1500,1600};
573        int Dold[4];
574       
575        thorton(Ut,Dt,PSIu,Q,G,Dold,4);
576        int Ut2[16]={0x7FFF, 100, 200, 300, 0, 0x7FFF, 500, 600, 0,0,0x7FFF, 800, 0,0,0, 0x7FFF};
577        int Dt2[4]={100,200,300,400};
578        int PSIu2[16] = {100, 200,300, 400 , 500, 600,700,800, 900,1000,1100,1200, 1300,1400,1500,1600};
579        thorton_fast(Ut2,Dt2,PSIu2,Q,G,Dold,4);
580        cout<< Q<<endl;*/
581}
Note: See TracBrowser for help on using the browser.