root/pmsm/simulator_zdenek/ekf_example/ekf_obj.cpp @ 80

Revision 80, 4.0 kB (checked in by smidl, 16 years ago)

oprava kvuli zmene v enorm

  • Property svn:eol-style set to native
Line 
1#include <itpp/itbase.h>
2#include <estim/libKF.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
9///////////////
10void EKFfixed::bayes(const vec &dt){
11        ekf(dt(2),dt(3),dt(0),dt(1));
12        vec xhat(4);   
13        //UGLY HACK!!! reliance on a predictor!!
14        xhat(0)=zprevod(x_est[0],Qm)*Iref;
15        xhat(1)=zprevod(x_est[1],Qm)*Iref;
16        xhat(2)=zprevod(x_est[2],Qm)*Wref;
17        xhat(3)=zprevod(x_est[3],15);
18       
19        E.set_mu(xhat);
20       
21        if ( BM::evalll ) {
22                //from enorm
23                vec xdif(x,4);//first 4 of x
24                //UGLY HACK!!! reliance on a predictor!!
25/*              xdif(0)=x[0]-zprevod(x_pred[0],Qm)*Iref;
26                xdif(1)=x[1]-zprevod(x_pred[1],Qm)*Iref;
27                xdif(2)=x[2]-zprevod(x_pred[2],Qm)*Wref;
28                xdif(3)=x[3]-zprevod(x_pred[3],15);*/
29               
30                xdif -=xhat; //(xdif=x-xhat)
31               
32                mat Pfull(4,4);
33                double* Pp=Pfull._data();
34                for(int i=0;i<16;i++){*(Pp++) = zprevod(P_est[i],15);}
35               
36                BM::ll = -0.5* ( 4 * 1.83787706640935 +log ( det ( Pfull ) ) +xdif* ( inv(Pfull)*xdif ) );
37        }
38};
39
40
41void EKFfixed::update_psi(void)
42{
43  int t_sin,t_cos,tmp;
44
45  // implementace v PC
46  t_sin=prevod(sin(Thetaref*x_est[3]/32768.),15);
47  t_cos=prevod(cos(Thetaref*x_est[3]/32768.),15);
48
49  PSI[2]=((long)cB*t_sin)>>15;
50  tmp=((long)cH*x_est[2])>>15;
51  PSI[3]=((long)tmp*t_cos)>>15;
52  PSI[6]=-((long)cB*t_cos)>>15;
53  PSI[7]=((long)tmp*t_sin)>>15;
54}
55
56
57void EKFfixed::prediction(int *ux)
58{
59  int t_sin,t_cos, tmp;
60
61  // implementace v PC
62  t_sin=prevod(sin(Thetaref*x_est[3]/32768.),15);
63  t_cos=prevod(cos(Thetaref*x_est[3]/32768.),15);
64
65  tmp=((long)cB*x_est[2])>>15;
66  x_pred[0]=((long)cA*x_est[0]+(long)tmp*t_sin+(long)cC*ux[0])>>15;
67  x_pred[1]=((long)cA*x_est[1]-(long)tmp*t_cos+(long)cC*ux[1])>>15;
68  x_pred[2]=x_est[2];
69  x_pred[3]=(((long)x_est[3]<<15)+(long)cG*x_est[2])>>15;
70
71  update_psi();
72
73  mmult(PSI,P_est,temp15a,3,3,3);
74//  mtrans(PSI,temp15b,5,5);
75  mmultt(temp15a,PSI,P_pred,3,3,3);
76  maddD(P_pred,Q,3,3);
77}
78
79void EKFfixed::correction(void)
80{
81  int Y_error[2];
82  long temp30a[4]; /* matrix [2,2] - temporary matrix for inversion */
83
84  choice_P(P_pred,temp15a,3);
85  maddD(temp15a,R,1,1);
86  minv2(temp15a,temp30a);
87        Ry(0,0)=zprevod(temp15a[0],15);
88        Ry(0,1)=zprevod(temp15a[1],15);
89        Ry(1,0)=zprevod(temp15a[2],15);
90        Ry(1,1)=zprevod(temp15a[3],15);
91 
92  mmultDr(P_pred,temp15a,3,3,1,1);
93  mmult1530(temp15a,temp30a,Kalm,3,1,1);
94
95
96  /* estimate the state system */
97  choice_x(x_pred, temp15a);
98  msub(Y_mes,temp15a,Y_error,1,0);
99  mmult(Kalm,Y_error,temp15a,3,1,0);
100  madd(x_pred,temp15a,x_est,3,0);
101
102  /* matrix of covariances - version without MMULTDL() */
103
104/* Version with MMULTDL() */
105  mmultDl(P_pred,temp15a,1,3,3,1);
106
107  mmult(Kalm,temp15a,P_est,3,1,3);
108  msub(P_pred,P_est,P_est,3,3);
109/* END */
110}
111
112
113void EKFfixed::ekf(double ux, double uy, double isxd, double isyd)
114{
115  // vypocet napeti v systemu (x,y)
116  ukalm[0]=prevod(ux/Uref,Qm);
117  ukalm[1]=prevod(uy/Uref,Qm);
118
119  // zadani mereni
120  Y_mes[0]=prevod(isxd/Iref,Qm);
121  Y_mes[1]=prevod(isyd/Iref,Qm);
122
123  ////// vlastni rutina EKF /////////////////////////
124  prediction(ukalm);
125  correction();
126
127  // navrat estimovanych hodnot regulatoru
128  vec& mu = E._mu();
129  (mu)(0)=zprevod(x_est[0],Qm)*Iref;
130  (mu)(1)=zprevod(x_est[1],Qm)*Iref;
131  (mu)(2)=zprevod(x_est[2],Qm)*Wref;
132  (mu)(3)=zprevod(x_est[3],15)*Thetaref;
133}
134
135void EKFfixed::init_ekf(double Tv)
136{
137  // Tuning of matrix Q
138  Q[0]=prevod(.01,15);       // 0.05
139  Q[5]=Q[0];
140  Q[10]=prevod(0.0001,15);      // 1e-3
141  Q[15]=prevod(0.0001,15);      // 1e-3
142
143  // Tuning of matrix R
144  R[0]=prevod(0.05,15);         // 0.05
145  R[3]=R[0];
146
147  // Motor model parameters
148  cA=prevod(1-Tv*Rs/Ls,15);
149  cB=prevod(Tv*Wref*Fmag/Iref/Ls,15);
150  cC=prevod(Tv/Ls/Iref*Uref,15);
151//  cD=prevod(1-Tv*Bf/J,15);
152//  cE=prevod(kp*p*p*Tv*Fmag*Iref/J/Wref,15);
153//  cF=prevod(p*Tv*Mref/J/Wref,15);
154  cG=prevod(Tv*Wref*4/Thetaref,15);
155  cH=prevod(Tv*Wref*Fmag/Iref/Ls*Thetaref,15);
156//  cI=prevod(kp*p*p*Tv*Fmag*Iref/J/Wref*Thetaref);
157
158  /* Init matrix PSI with permanently constant terms */
159  PSI[0]=cA;
160  PSI[5]=PSI[0];
161  PSI[10]=0x7FFF;
162  PSI[14]=cG;
163  PSI[15]=0x7FFF;
164}
Note: See TracBrowser for help on using the browser.