GCOP  1.0
unscentedpredictor.h
Go to the documentation of this file.
00001 #ifndef GCOP_UNSCENTEDPREDICTOR_H
00002 #define GCOP_UNSCENTEDPREDICTOR_H
00003 
00004 #include "predictor.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> class UnscentedPredictor : public Predictor<T, _nx, _nu, _np>, public UnscentedBase<T, _nx> {
00015   public:
00016   
00017   typedef Matrix<double, _nx, 1> Vectornd;
00018   typedef Matrix<double, _nu, 1> Vectorcd;
00019   typedef Matrix<double, _np, 1> Vectormd;
00020 
00021   typedef Matrix<double, _nx, _nx> Matrixnd;
00022   typedef Matrix<double, _nx, _nu> Matrixncd;
00023   typedef Matrix<double, _nu, _nx> Matrixcnd;
00024   typedef Matrix<double, _nu, _nu> Matrixcd;
00025   
00026   typedef Matrix<double, _np, _np> Matrixmd;
00027   typedef Matrix<double, _nx, _np> Matrixnmd;
00028   typedef Matrix<double, _np, _nx> Matrixmnd;
00029 
00030   //  typedef pair<T, Matrixnd > Belief; ///< belief state 
00031 
00032   UnscentedPredictor(System<T, _nx, _nu, _np> &sys);
00033   
00034   virtual ~UnscentedPredictor();
00035   
00036   virtual bool Predict(T& xb, double t, const T &xa,
00037                        const Vectorcd &u, double h, 
00038                        const Vectormd *p = 0, bool cov = true);
00039   
00040   
00041   protected:
00042 
00043   vector<T> Xps;  
00044   Matrixnd Q;     
00045   
00046   };
00047   
00048   
00049   template <typename T, int _nx, int _nu, int _np> 
00050     UnscentedPredictor<T, _nx, _nu, _np>::UnscentedPredictor(System<T, _nx, _nu, _np>  &sys) : 
00051     Predictor<T, _nx, _nu, _np>(sys),
00052     UnscentedBase<T, _nx>(sys.X),
00053     Xps(2*sys.X.n + 1) {
00054     
00055     if (_nx == Dynamic) {
00056       Q.resize(sys.X.n, sys.X.n);
00057     }
00058     Q.setZero();
00059   }
00060   
00061   template <typename T, int _nx, int _nu, int _np> 
00062     UnscentedPredictor<T, _nx, _nu, _np>::~UnscentedPredictor() {
00063   }  
00064   
00065   template <typename T, int _nx, int _nu, int _np> 
00066     bool UnscentedPredictor<T, _nx, _nu, _np>::Predict(T& xb, double t, const T &xa,
00067                                                        const Vectorcd &u, double h, 
00068                                                        const Vectormd *p, bool cov) {
00069     
00070     this->Points(this->Xs, xa);
00071     
00072     // average difference
00073     Vectornd dx;// = Vectornd::Zero();
00074     dx.setZero();
00075     vector<Vectornd> dxs(2*this->L+1);
00076     
00077     for (int i = 0; i < 2*this->L + 1; ++i) {
00078       this->sys.Step(Xps[i], t, this->Xs[i], u, h, p);
00079 
00080       /*
00081       Vector3d rpy;
00082       SO3::Instance().g2q(rpy, Xs[i].R);
00083       cout << "rpy=" << rpy.transpose() << endl;
00084 
00085       SO3::Instance().g2q(rpy, Xps[i].R);
00086       cout << "rpyp=" << rpy.transpose() << endl;
00087       */
00088 
00089 
00090       this->sys.X.Lift(dxs[i], Xps[0], Xps[i]);   
00091       dx = dx + this->Ws[i]*dxs[i];   
00092     }
00093 
00094     this->sys.X.Retract(xb, Xps[0], dx);
00095     xb.P.setZero(); // zero covariance
00096     
00097     for (int i = 0; i < 2*this->L+1; ++i) {
00098       this->sys.X.Lift(dxs[i], xb, Xps[i]);   
00099       xb.P = xb.P + this->Wc[i]*dxs[i]*dxs[i].transpose();
00100     }
00101 
00102     this->sys.Noise(Q, t, xa, u, h, p);
00103     xb.P = xb.P + Q;
00104 
00105     return true;
00106   }  
00107 }
00108 
00109 
00110 #endif