GCOP  1.0
unscentedcorrector.h
Go to the documentation of this file.
00001 #ifndef GCOP_UNSCENTEDCORRECTOR_H
00002 #define GCOP_UNSCENTEDCORRECTOR_H
00003 
00004 #include "corrector.h"
00005 #include "unscentedbase.h"
00006 
00007 namespace gcop {
00008   
00009   using namespace std;
00010   
00011   template <typename T = VectorXd, 
00012     int _nx = Dynamic, 
00013     int _nu = Dynamic, 
00014     int _np = Dynamic, 
00015     typename Tz = VectorXd,
00016     int _nz = Dynamic> class UnscentedCorrector : public Corrector<T, _nx, _nu, _np, Tz, _nz>, public UnscentedBase<T, _nx> {
00017   public:
00018   
00019   typedef Matrix<double, _nx, 1> Vectornd;
00020   typedef Matrix<double, _nu, 1> Vectorcd;
00021   typedef Matrix<double, _np, 1> Vectormd;
00022 
00023   typedef Matrix<double, _nx, _nx> Matrixnd;
00024   typedef Matrix<double, _nx, _nu> Matrixncd;
00025   typedef Matrix<double, _nx, _nz> Matrixnrd;
00026   typedef Matrix<double, _nu, _nx> Matrixcnd;
00027   typedef Matrix<double, _nu, _nu> Matrixcd;
00028   
00029   typedef Matrix<double, _np, _np> Matrixmd;
00030   typedef Matrix<double, _nx, _np> Matrixnmd;
00031   typedef Matrix<double, _np, _nx> Matrixmnd;
00032 
00033   typedef Matrix<double, _nz, 1> Vectorrd;
00034   typedef Matrix<double, _nz, _nz> Matrixrd;
00035   typedef Matrix<double, _nz, _nx> Matrixrnd;
00036   typedef Matrix<double, _nz, _nu> Matrixrcd;
00037   typedef Matrix<double, _nz, _np> Matrixrmd;
00038 
00039   UnscentedCorrector(System<T, _nx, _nu, _np> &sys,
00040                      Sensor<T, _nx, _nu, _np, Tz, _nz> &sensor);
00041   
00042   virtual ~UnscentedCorrector();
00043   
00044   virtual bool Correct(T& xb, double t, const T &xa, 
00045                        const Vectorcd &u, const Tz &z, 
00046                        const Vectormd *p = 0, bool cov = true);    
00047 
00048   protected:
00049 
00050   vector<Tz> Zs;  
00051   Matrixrd Pzz;   
00052   Matrixnrd Pxz;  
00053   
00054   };
00055   
00056   
00057   template <typename T, int _nx, int _nu, int _np, typename Tz, int _nz> 
00058     UnscentedCorrector<T, _nx, _nu, _np, Tz, _nz>::UnscentedCorrector(System<T, _nx, _nu, _np>  &sys, 
00059                                                                       Sensor<T, _nx, _nu, _np, Tz, _nz> &sensor) : 
00060     Corrector<T, _nx, _nu, _np, Tz, _nz>(sys, sensor),
00061     UnscentedBase<T, _nx>(sys.X),
00062     Zs(2*sys.X.n + 1) {
00063     
00064     if (_nz == Dynamic) {     
00065       Pzz.resize(sensor.Z.n, sensor.Z.n);
00066     }      
00067     if (_nx == Dynamic || _nz == Dynamic) {     
00068       Pxz.resize(sys.X.n, sensor.Z.n);        
00069     }
00070   }
00071   
00072   template <typename T, int _nx, int _nu, int _np, typename Tz, int _nz> 
00073     UnscentedCorrector<T, _nx, _nu, _np, Tz, _nz>::~UnscentedCorrector() {
00074   }  
00075   
00076 
00077   template <typename T, int _nx, int _nu, int _np, typename Tz, int _nz> 
00078     bool UnscentedCorrector<T, _nx, _nu, _np, Tz, _nz>::Correct(T& xb, double t, const T &xa, 
00079                                                                 const Vectorcd &u, const Tz &z, 
00080                                                                 const Vectormd *p, bool cov) {
00081     
00082     this->Points(this->Xs, xa);
00083 
00084     //    VectorXd xm = VectorXd::Zero(x.size());
00085     Vectorrd zm;
00086     zm.setZero();
00087 
00088     Vectornd dx = Vectornd::Zero();
00089     vector<Vectornd> dxs(2*this->L+1);
00090 
00091     for (int i = 0; i < 2*this->L+1; ++i) {
00092       sensor(Zs[i], t, this->Xs[i], u, p);
00093       zm = zm + this->Ws[i]*Zs[i];
00094       //      dx = dx + Ws[i]*dxs[i];
00095       //cout << "Zs["<<i<<"]=" << Zs[i].transpose() << endl;
00096       //      cout << "Ws["<<i<<"]=" << Ws[i] << endl;
00097     }
00098 
00099     cout << "z(0)=" << Zs[0].transpose() <<endl;
00100     cout << "zm=" << zm.transpose() << endl;
00101 
00102     Pzz.setZero();
00103     Pxz.setZero();    
00104 
00105     //    T xm;
00106     //    this->sys.X.Retract(xm, xa, dx);
00107 
00108     for (int i = 0; i < 2*this->L+1; ++i) {
00109       Vectorrd dz = Zs[i] - zm;
00110       this->sys.X.Lift(dxs[i], xa, this->Xs[i]);      // variations from mean
00111       /*
00112       Vector3d rpy;
00113       SO3::Instance().g2q(rpy, Xs[i].R);
00114       cout << "rpy=" << rpy.transpose() << endl;
00115 
00116       cout << "dxs["<<i<<"]=" << dxs[i].transpose() << endl;
00117       */
00118 
00119       //      this->sys.X.Lift(dxs[i], xm, Xs[i]);
00120       Pzz = Pzz + this->Wc[i]*dz*dz.transpose();
00121       Pxz = Pxz + this->Wc[i]*dxs[i]*dz.transpose();
00122     }
00123     cout <<"Pzz=" << Pzz << endl;
00124     cout <<"Pxz=" << Pxz << endl;
00125 
00126     // Pxz is like P*H'
00127 
00128     //    cout << "z=" << z.transpose() << endl;
00129 
00130     Pzz = Pzz + this->sensor.R;
00131 
00132     Matrixnrd K = Pxz*Pzz.inverse();
00133     cout << "K=" << K << endl;
00134     
00135     dx = K*(z - zm);
00136     this->sys.X.Retract(xb, xa, dx);
00137     
00138     xb.P = xa.P - K*Pzz*K.transpose();
00139     //    xb.P = xa.P - K*Pxz.transpose();
00140   
00141     return true;
00142   }
00143 }
00144 
00145 
00146 #endif