Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

GaitedFootsteps.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_GaitedFootsteps_h_
00003 #define INCLUDED_GaitedFootsteps_h_
00004 
00005 /*
00006  analyze gait
00007   footstep limits
00008   group legs
00009  save q for each leg, link within group
00010  expand for each candidate
00011   foot contact, body motion
00012   body motion implied by contact chosen?
00013   contacts filtered by reachability
00014  if a leg has obstacles:
00015   select from other groups (make up new ones?)
00016   test stability
00017   increment cost of motion?
00018 */
00019 
00020 #include "Shared/fmat.h"
00021 #include "Shared/RobotInfo.h"
00022 #include "Motion/XWalkParameters.h"
00023 #include "Planners/PlannerObstacles.h"
00024 #include <tr1/functional>
00025 
00026 class KinematicJoint;
00027 
00028 class GaitedFootsteps {
00029 public:
00030   GaitedFootsteps()
00031     : speed(0), flightDuration(0), stepDuration(), relaxation(1), rotDist(0), stepReorient(true), stepRehab(true),
00032     kinematics(NULL), ncand(), p(), ground(), gravity(), neutrals(), groups(), obstacles()
00033   {}
00034   
00035   struct State {
00036     State() : pos(), oriRot(fmat::Matrix<2,2>::identity()), oriAngle(0), phase(0) {}
00037     fmat::Column<2> pos;
00038     fmat::Matrix<2,2> oriRot;
00039     fmat::fmatReal oriAngle;
00040     fmat::Column<2> footPos[NumLegs];
00041     float legJoints[NumLegJoints];
00042     unsigned int phase;
00043     static fmat::fmatReal ROUND; //!< UGLY HACK - this is assigned in GaitedFootsteps::setGait
00044     static const fmat::fmatReal ORIROUND;
00045     size_t hash() const {
00046       const long x = static_cast<long>(pos[0]*ROUND);
00047       const long y = static_cast<long>(pos[1]*ROUND);
00048       const long a = static_cast<long>(oriAngle*ORIROUND);
00049       // with some trial and error, this distributes very nicely, no deeper understanding...
00050       return std::tr1::hash<fmat::fmatReal>()((x<<16) + (a<<8) + y);
00051     }
00052     /*bool operator<(const State& st) const {
00053       const float * a = &pos[0];
00054       const float * b = &st.pos[0];
00055       for(size_t i=0; i<sizeof(State)/sizeof(float); ++i) {
00056         if(a[i]<b[i])
00057           return true;
00058         if(a[i]>b[i])
00059           return false;
00060       }
00061       return false;
00062     }*/
00063     bool operator==(const State& st) const {
00064       // This causes crashes (at unordered_set destruction, but probably anytime)
00065       //  because unordered_set requires that equivalent values have equivalent hash:
00066       //return ((pos-st.pos).norm()<ROUND);
00067       // Instead, restrict equivalency to the same rounding method used in hash
00068       const long mx = static_cast<long>(pos[0]*ROUND);
00069       const long my = static_cast<long>(pos[1]*ROUND);
00070       const long ma = static_cast<long>(oriAngle*ORIROUND);
00071       const long stx = static_cast<long>(st.pos[0]*ROUND);
00072       const long sty = static_cast<long>(st.pos[1]*ROUND);
00073       const long sta = static_cast<long>(st.oriAngle*ORIROUND);
00074       return mx==stx && my==sty && ma==sta;
00075     }
00076     friend std::ostream& operator<<(std::ostream& os, const State& st) {
00077       return os << &st << ": " << st.pos << " @ "  << st.oriAngle << '\n'
00078       << fmat::SubMatrix<2,NumLegs,const float>(&st.footPos[0][0]) << std::endl;
00079     }
00080   };
00081   
00082   ~GaitedFootsteps();
00083   
00084   //! returns true if the state does not collide with any obstacles
00085   bool validate(const State& st) const;
00086   
00087   //! The heuristic function accepts the current state, and goal state, and should return an admissable (aka optimistic) estimate of the remaining cost
00088   float heuristic(const State& st, const State& goal) const;
00089   
00090   //! Generates a vector of successor states, paired with the cost to get to that state from @a st
00091   /*! Note that this implementation returns a reference to a static instance, which is not thread safe but slightly faster */
00092   const std::vector<std::pair<float,GaitedFootsteps::State> >& expand(const State* parent, const State& st, const State& goal) const;
00093   
00094   void setGait(const KinematicJoint& kj, const XWalkParameters& xp, size_t discretization);
00095   
00096   void addObstacle(PlannerObstacle2D* obs) { obstacles.push_back(obs); }
00097   std::vector< PlannerObstacle2D* >& getObstacles() { return obstacles; }
00098   const std::vector< PlannerObstacle2D* >& getObstacles() const { return obstacles; }
00099   
00100   float speed;
00101   float flightDuration;
00102   float stepDuration;
00103   float relaxation;
00104   float rotDist;
00105   
00106   bool stepReorient;
00107   bool stepRehab;
00108   
00109   KinematicJoint * kinematics;
00110   KinematicJoint* childMap[NumReferenceFrames];
00111   struct StepData {
00112     StepData() : pos() {}
00113     fmat::Column<3> pos;
00114         #if defined(TGT_IS_MANTIS)
00115         static const int JointsPerLeg = 4; // hack! fix it later
00116         #endif
00117     float legJoints[JointsPerLeg];
00118 //                float legJoints[4];
00119   };
00120   size_t ncand;
00121   XWalkParameters p;
00122   fmat::Column<3> ground;
00123   fmat::Column<3> gravity;
00124   std::vector< fmat::Column<2> > neutrals;
00125   std::vector< std::set<size_t> > groups;
00126   std::vector< PlannerObstacle2D* > obstacles;
00127   
00128   PlannerObstacle2D* checkObstacles(const fmat::Column<2>& pt) const;
00129   bool addRotation(const State& st, float strideAngle, float strideDist, const fmat::Column<2>& strideDir, const fmat::Column<2>& stride, std::vector<std::pair<float,GaitedFootsteps::State> >& candidates, float maxDist) const;
00130   void addCandidate(const State* parent, const State& st, float angle, std::vector<std::pair<float,GaitedFootsteps::State> >& candidates, float maxDist=0) const;
00131   
00132 private:
00133   GaitedFootsteps(const GaitedFootsteps&); //!< do not use
00134   GaitedFootsteps& operator=(const GaitedFootsteps&); //!< do not use
00135 };
00136 
00137 #endif

Tekkotsu v5.1CVS
Generated Mon May 9 04:58:41 2016 by Doxygen 1.6.3