GCOP  1.0
body3ddemcontroller.h
Go to the documentation of this file.
00001 #ifndef GCOP_BODY3DDEMCONTROLLER_H
00002 #define GCOP_BODY3DDEMCONTROLLER_H
00003 
00004 
00005 #include "dsl/gridsearch.h"
00006 #include "dsl/distedgecost.h"
00007 #include "body3davoidcontroller.h"
00008 #include "pqpdem.h"
00009 #include "controller.h"
00010 
00011 namespace gcop {
00012 
00013   using namespace dsl;  
00014   using namespace std;
00015   using namespace Eigen;
00016 
00017   typedef Matrix<double, 5, 1> Vector5d;
00018    
00019   typedef PqpDem<Body3dState, 12, 6> Body3dPqpDem;
00020   
00028   template <int nu = 6> class Body3dDemController : public Controller<Body3dState, Matrix<double, nu, 1>, Vector5d, Body3dState > {
00029   public:
00030   
00031   typedef Matrix<double, nu, 1> Vectorcd;
00032   
00041   Body3dDemController(const Body3d<nu> &sys,
00042                       const Body3dState &x0,
00043                       Body3dState *xf,
00044                       Body3dPqpDem &dem,
00045                       double vd = 20);
00046   
00047   virtual bool Set(Vectorcd &u, double t, const Body3dState &x);
00048   
00049   virtual bool SetParams(const Vector5d &s);
00050   
00051   virtual bool SetContext(const Body3dState &x);
00052   
00053   //  virtual bool SetContext(const Body3dPqpDem &c);
00054   
00055   void GetTraj(vector<Body3dState> &xds, const Dem &dem, GridSearch &gdsl, const Body3dState &x0, const Body3dState &xf, double vd);
00056   
00057   const Body3dState &x0;               
00058   Body3dPqpDem &dem;                   
00059   Body3dAvoidController<nu> localCtrl; 
00060   GridSearch dsl;                      
00061   vector<Body3dState> xds;             
00062   double vd;                           
00063   int j;                               
00064   double wpRadius;                     
00065   };
00066 
00067   
00068   template <int nu>   
00069     void Body3dDemController<nu>::GetTraj(vector<Body3dState> &xds, const Dem &dem, GridSearch &gdsl, const Body3dState &x0, const Body3dState &xf, double vd) {
00070     
00071     int i0,j0,ig,jg;
00072     dem.Point2Index(i0, j0, x0.second[0], x0.second[1]);
00073     dem.Point2Index(ig, jg, xf.second[0], xf.second[1]);
00074     gdsl.SetStart(j0, i0);
00075     gdsl.SetGoal(jg, ig);
00076     GridPath path, optPath;
00077     gdsl.Plan(path);
00078     gdsl.OptPath(path, optPath, 2);
00079     for (int i = 0; i < optPath.count; ++i) {
00080       Body3dState x = xf;
00081       dem.Index2Point(x.second[0], x.second[1], optPath.pos[2*i+1], optPath.pos[2*i]);
00082       x.second[2] = x0.second[2];
00083       
00084       // if not last point
00085       Vector3d v(0,0,0);
00086       if (i > 0) {
00087         Vector3d pa;
00088         Vector3d pb;
00089         dem.Index2Point(pa[0], pa[1], optPath.pos[2*i-1], optPath.pos[2*i-2]);
00090         dem.Index2Point(pb[0], pb[1], optPath.pos[2*i+1], optPath.pos[2*i]);
00091         pa[2] = x0.second[2];
00092         pb[2] = x0.second[2];
00093         v = pb - pa;
00094         v = v/v.norm();
00095         v = v*vd;
00096       }
00097       x.second.tail<3>() = v;      
00098       xds.push_back(x);
00099     }
00100   }
00101 
00102 
00103   template <int nu>   
00104     Body3dDemController<nu>::Body3dDemController(const Body3d<nu> &sys,
00105                                                  const Body3dState &x0,
00106                                                  Body3dState *xf,
00107                                                  Body3dPqpDem &dem,
00108                                                  double vd) :
00109     x0(x0), dem(dem), localCtrl(sys, xf, 0, &dem), dsl(dem.dem.nj, dem.dem.ni, new DistEdgeCost(), dem.dem.data), vd(vd), j(0), wpRadius(10)
00110     {
00111     }
00112 
00113   template <int nu>   
00114     bool Body3dDemController<nu>::Body3dDemController::Set(Vectorcd &u, 
00115                                                            double t, 
00116                                                            const Body3dState &x)
00117     {    
00118       if (!xds.size()) {
00119         cout << "[W] Body3dDemController::Set: no desired states set yet!" << endl;
00120         return false;
00121       }
00122 
00123       assert(j < xds.size());
00124       
00125       Vector3d d = x.second.head<3>() - xds[j].second.head<3>();//localCtrl.stabCtrl.xd->second.head(3);
00126       if (d.norm() < wpRadius && j < xds.size() - 1)
00127         ++j;
00128       
00129       // set current waypoint
00130       localCtrl.stabCtrl.xd = &xds[j];
00131 
00132       // get local control
00133       localCtrl.Set(u, t, x);
00134       this->localCtrl.stabCtrl.sys.U.Clip(u);
00135                   
00136       return true;
00137     }
00138   
00139   template <int nu>   
00140     bool Body3dDemController<nu>::SetParams(const Vector5d &s) {
00141     return localCtrl.SetParams(s);
00142   }
00143   
00144   template <int nu>   
00145     bool Body3dDemController<nu>::SetContext(const Body3dState &xf) {
00146 
00147     // make sure it is free
00148     Matrix<double, 1, 1> g;
00149     dem(g, 0, xf);
00150     if (g[0] > 0)
00151       return false;
00152 
00153     xds.clear();
00154     // compute a new sequence of waypoint
00155     GetTraj(xds, dem.dem, dsl, x0, xf, vd);
00156     // reset index
00157     j = 0;
00158 
00159     return true;
00160   }
00161 
00162 };
00163 
00164 #endif