ugv_nav4d
EnvironmentXYZTheta.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <sbpl/discrete_space_information/environment.h>
4 #undef DEBUG //sbpl defines DEBUG 0 but the word debug is also used in base-logging which is included from TraversabilityGenerator3d
5 #include <traversability_generator3d/TraversabilityGenerator3d.hpp>
6 #include <maps/grid/TraversabilityMap3d.hpp>
7 #include <base/Pose.hpp>
8 #include "DiscreteTheta.hpp"
9 #include "PreComputedMotions.hpp"
10 #include <trajectory_follower/SubTrajectory.hpp>
11 
12 std::ostream& operator<< (std::ostream& stream, const DiscreteTheta& angle);
13 
14 namespace ugv_nav4d
15 {
16 
17  class StateCreationFailed : public std::runtime_error {using std::runtime_error::runtime_error;};
18  class NodeCreationFailed : public std::runtime_error {using std::runtime_error::runtime_error;};
19  class ObstacleCheckFailed : public std::runtime_error {using std::runtime_error::runtime_error;};
20  class OrientationNotAllowed : public std::runtime_error {using std::runtime_error::runtime_error;};
21 
22 
23 class EnvironmentXYZTheta : public DiscreteSpaceInformation
24 {
25 public:
26  typedef traversability_generator3d::TraversabilityGenerator3d::MLGrid MLGrid;
27 protected:
28  traversability_generator3d::TraversabilityGenerator3d travGen;
29  std::shared_ptr<MLGrid > mlsGrid;
30 
31  struct EnvironmentXYZThetaException : public SBPL_Exception
32  {
33  EnvironmentXYZThetaException(const std::string& what) :
34  msg("SBPL has encountered a fatal error: " + what){}
35 
36  virtual const char* what() const throw()
37  {
38  return msg.c_str();
39  }
40  const std::string msg;
41  };
42 
43 
45  struct ThetaNode
46  {
47  ThetaNode(const DiscreteTheta &t) :theta(t) {};
48  int id;
50  };
51 
53  struct PlannerData
54  {
55  PlannerData() : travNode(nullptr) {};
56 
58  traversability_generator3d::TravGenNode *travNode;
59 
63  std::map<DiscreteTheta, ThetaNode *> thetaToNodes;
64  };
65 
67  struct Distance
68  {
69  double distToStart = 0;
70  double distToGoal = 0;
71  Distance(double toStart, double toGoal) : distToStart(toStart), distToGoal(toGoal){}
72  };
73 
75  typedef maps::grid::TraversabilityNode<PlannerData> XYZNode;
76 
77  //search space without theta
78  maps::grid::TraversabilityMap3d<XYZNode *> searchGrid;
79 
81  struct Hash
82  {
84  {
85  }
88  };
89 
91  std::vector<Hash> idToHash;
92 
95  std::vector<Distance> travNodeIdToDistance;
96 
98 
100  XYZNode *startXYZNode; //part of the start state
102  XYZNode *goalXYZNode; //part of the goal state
103 
105  XYZNode *createNewXYZState(traversability_generator3d::TravGenNode* travNode);
106  ThetaNode *createNewStateFromPose(const std::string& name, const Eigen::Vector3d& pos, double theta, ugv_nav4d::EnvironmentXYZTheta::XYZNode** xyzBackNode);
107 
108  bool checkStartGoalNode(const std::string& name, traversability_generator3d::TravGenNode* node, double theta);
109 
110 public:
111 
113  bool obstacleCheck(const maps::grid::Vector3d& pos, double theta, const traversability_generator3d::TraversabilityGenerator3d& travGen,
114  const traversability_generator3d::TraversabilityConfig& travConf,
115  const sbpl_spline_primitives::SplinePrimitivesConfig& splineConf,
116  const std::string& nodeName="node");
117  Eigen::Vector3d robotHalfSize;
118 
121  EnvironmentXYZTheta(std::shared_ptr<MLGrid > mlsGrid,
122  const traversability_generator3d::TraversabilityConfig &travConf,
123  const sbpl_spline_primitives::SplinePrimitivesConfig &primitiveConfig,
124  const Mobility& mobilityConfig);
125 
126  virtual ~EnvironmentXYZTheta();
127 
128  void updateMap(std::shared_ptr<MLGrid > mlsGrid);
129  void setInitialPatch(const Eigen::Affine3d &ground2Mls, double patchRadius);
130 
131  virtual bool InitializeEnv(const char* sEnvFile);
132  virtual bool InitializeMDPCfg(MDPConfig* MDPCfg);
133 
134 
136  void expandMap(const std::vector<Eigen::Vector3d>& positions);
137 
144  std::shared_ptr<trajectory_follower::SubTrajectory> findTrajectoryOutOfObstacle(const Eigen::Vector3d& start, double theta,
145  const Eigen::Affine3d& ground2Body);
146 
147 
151  virtual int GetFromToHeuristic(int FromStateID, int ToStateID);
155  virtual int GetStartHeuristic(int stateID);
160  virtual int GetGoalHeuristic(int stateID);
161 
162  virtual void GetPreds(int TargetStateID, std::vector< int >* PredIDV, std::vector< int >* CostV);
163  virtual void GetSuccs(int SourceStateID, std::vector< int >* SuccIDV, std::vector< int >* CostV);
164  virtual void GetSuccs(int SourceStateID, std::vector< int >* SuccIDV, std::vector< int >* CostV, std::vector< size_t >& motionIdV);
165 
166  virtual void PrintEnv_Config(FILE* fOut);
167  virtual void PrintState(int stateID, bool bVerbose, FILE* fOut = 0);
168 
169  virtual void SetAllActionsandAllOutcomes(CMDPSTATE* state);
170  virtual void SetAllPreds(CMDPSTATE* state);
171  virtual int SizeofCreatedEnv();
172 
173  void setStart(const Eigen::Vector3d &startPos, double theta);
174  void setGoal(const Eigen::Vector3d &goalPos, double theta);
175 
176  maps::grid::Vector3d getStatePosition(const int stateID) const;
177 
178 
181  const Motion& getMotion(const int fromStateID, const int toStateID);
182 
183  const maps::grid::TraversabilityMap3d<traversability_generator3d::TravGenNode *> &getTraversabilityMap() const;
184 
185  traversability_generator3d::TraversabilityGenerator3d& getTravGen();
186 
187  const MLGrid &getMlsMap() const;
188 
189  std::vector<Motion> getMotions(const std::vector<int> &stateIDPath);
190 
191  void getTrajectory(const std::vector<int> &stateIDPath, std::vector<trajectory_follower::SubTrajectory> &result,
192  bool setZToZero, const Eigen::Vector3d &startPos, const Eigen::Vector3d &goalPos, const double& goalHeading, const Eigen::Affine3d &plan2Body = Eigen::Affine3d::Identity());
193 
195 
197  void clear();
198 
199  void setTravConfig(const traversability_generator3d::TraversabilityConfig& cfg);
200 
203  void dijkstraComputeCost(const traversability_generator3d::TravGenNode* source, std::vector<double> &outDistances,
204  const double maxDist) const;
205 
208  void enablePathStatistics(bool enable);
209 
210 private:
211 
214  traversability_generator3d::TravGenNode* checkTraversableHeuristic(const maps::grid::Index sourceIndex, traversability_generator3d::TravGenNode* sourceNode,
215  const ugv_nav4d::Motion& motion, const maps::grid::TraversabilityMap3d< traversability_generator3d::TravGenNode* >& trMap);
216 
220  bool checkOrientationAllowed(const traversability_generator3d::TravGenNode* node,
221  const base::Orientation2D& orientation) const;
222 
223 
225  void precomputeCost();
226 
228  double getAvgSlope(std::vector<const traversability_generator3d::TravGenNode*> path) const;
229 
231  double getMaxSlope(std::vector<const traversability_generator3d::TravGenNode*> path) const;
232 
233 
235  double getHeuristicDistance(const Eigen::Vector3d& a, const Eigen::Vector3d& b) const;
236 
241  traversability_generator3d::TravGenNode* movementPossible(traversability_generator3d::TravGenNode* fromTravNode, const maps::grid::Index& fromIdx, const maps::grid::Index& toIdx);
242 
246  bool checkExpandTreadSafe(traversability_generator3d::TravGenNode * node);
247 
248  bool usePathStatistics;
249 
250  traversability_generator3d::TraversabilityConfig travConf;
251  sbpl_spline_primitives::SplinePrimitivesConfig primitiveConfig;
252 
253  unsigned int numAngles;
254 
255  Mobility mobilityConfig;
256 };
257 
258 }
std::ostream & operator<<(std::ostream &stream, const DiscreteTheta &angle)
Definition: DiscreteTheta.cpp:3
Definition: DiscreteTheta.hpp:7
Definition: EnvironmentXYZTheta.hpp:24
Eigen::Vector3d robotHalfSize
Definition: EnvironmentXYZTheta.hpp:117
std::vector< Distance > travNodeIdToDistance
Definition: EnvironmentXYZTheta.hpp:95
void setTravConfig(const traversability_generator3d::TraversabilityConfig &cfg)
Definition: EnvironmentXYZTheta.cpp:1212
virtual void PrintState(int stateID, bool bVerbose, FILE *fOut=0)
Definition: EnvironmentXYZTheta.cpp:911
bool obstacleCheck(const maps::grid::Vector3d &pos, double theta, const traversability_generator3d::TraversabilityGenerator3d &travGen, const traversability_generator3d::TraversabilityConfig &travConf, const sbpl_spline_primitives::SplinePrimitivesConfig &splineConf, const std::string &nodeName="node")
Definition: EnvironmentXYZTheta.cpp:159
const maps::grid::TraversabilityMap3d< traversability_generator3d::TravGenNode * > & getTraversabilityMap() const
Definition: EnvironmentXYZTheta.cpp:1133
ThetaNode * startThetaNode
Definition: EnvironmentXYZTheta.hpp:99
std::vector< Motion > getMotions(const std::vector< int > &stateIDPath)
Definition: EnvironmentXYZTheta.cpp:926
virtual ~EnvironmentXYZTheta()
Definition: EnvironmentXYZTheta.cpp:94
virtual bool InitializeEnv(const char *sEnvFile)
Definition: EnvironmentXYZTheta.cpp:503
XYZNode * createNewXYZState(traversability_generator3d::TravGenNode *travNode)
Definition: EnvironmentXYZTheta.cpp:120
void dijkstraComputeCost(const traversability_generator3d::TravGenNode *source, std::vector< double > &outDistances, const double maxDist) const
void setStart(const Eigen::Vector3d &startPos, double theta)
Definition: EnvironmentXYZTheta.cpp:344
virtual int GetStartHeuristic(int stateID)
heuristic estimate from start state to state with stateID
Definition: EnvironmentXYZTheta.cpp:487
traversability_generator3d::TraversabilityGenerator3d & getTravGen()
Definition: EnvironmentXYZTheta.cpp:1207
virtual void SetAllPreds(CMDPSTATE *state)
Definition: EnvironmentXYZTheta.cpp:368
void clear()
Definition: EnvironmentXYZTheta.cpp:59
maps::grid::TraversabilityMap3d< XYZNode * > searchGrid
Definition: EnvironmentXYZTheta.hpp:78
std::shared_ptr< MLGrid > mlsGrid
Definition: EnvironmentXYZTheta.hpp:29
virtual int SizeofCreatedEnv()
Definition: EnvironmentXYZTheta.cpp:901
virtual void GetSuccs(int SourceStateID, std::vector< int > *SuccIDV, std::vector< int > *CostV)
traversability_generator3d::TraversabilityGenerator3d travGen
Definition: EnvironmentXYZTheta.hpp:28
void setInitialPatch(const Eigen::Affine3d &ground2Mls, double patchRadius)
Definition: EnvironmentXYZTheta.cpp:99
virtual void GetPreds(int TargetStateID, std::vector< int > *PredIDV, std::vector< int > *CostV)
Definition: EnvironmentXYZTheta.cpp:895
const PreComputedMotions & getAvailableMotions() const
Definition: EnvironmentXYZTheta.cpp:1143
maps::grid::TraversabilityNode< PlannerData > XYZNode
Definition: EnvironmentXYZTheta.hpp:75
virtual int GetFromToHeuristic(int FromStateID, int ToStateID)
heuristic estimate from state FromStateID to state ToStateID
Definition: EnvironmentXYZTheta.cpp:383
bool checkStartGoalNode(const std::string &name, traversability_generator3d::TravGenNode *node, double theta)
Definition: EnvironmentXYZTheta.cpp:220
XYZNode * startXYZNode
Definition: EnvironmentXYZTheta.hpp:100
virtual void PrintEnv_Config(FILE *fOut)
Definition: EnvironmentXYZTheta.cpp:906
virtual void SetAllActionsandAllOutcomes(CMDPSTATE *state)
Definition: EnvironmentXYZTheta.cpp:376
void enablePathStatistics(bool enable)
Definition: EnvironmentXYZTheta.cpp:483
void updateMap(std::shared_ptr< MLGrid > mlsGrid)
Definition: EnvironmentXYZTheta.cpp:104
void getTrajectory(const std::vector< int > &stateIDPath, std::vector< trajectory_follower::SubTrajectory > &result, bool setZToZero, const Eigen::Vector3d &startPos, const Eigen::Vector3d &goalPos, const double &goalHeading, const Eigen::Affine3d &plan2Body=Eigen::Affine3d::Identity())
Definition: EnvironmentXYZTheta.cpp:939
std::shared_ptr< trajectory_follower::SubTrajectory > findTrajectoryOutOfObstacle(const Eigen::Vector3d &start, double theta, const Eigen::Affine3d &ground2Body)
Definition: EnvironmentXYZTheta.cpp:1217
virtual bool InitializeMDPCfg(MDPConfig *MDPCfg)
Definition: EnvironmentXYZTheta.cpp:508
EnvironmentXYZTheta(std::shared_ptr< MLGrid > mlsGrid, const traversability_generator3d::TraversabilityConfig &travConf, const sbpl_spline_primitives::SplinePrimitivesConfig &primitiveConfig, const Mobility &mobilityConfig)
Definition: EnvironmentXYZTheta.cpp:33
virtual void GetSuccs(int SourceStateID, std::vector< int > *SuccIDV, std::vector< int > *CostV, std::vector< size_t > &motionIdV)
virtual int GetGoalHeuristic(int stateID)
heuristic estimate from state with stateID to goal state
Definition: EnvironmentXYZTheta.cpp:429
ThetaNode * goalThetaNode
Definition: EnvironmentXYZTheta.hpp:101
std::vector< Hash > idToHash
Definition: EnvironmentXYZTheta.hpp:91
void expandMap(const std::vector< Eigen::Vector3d > &positions)
Definition: EnvironmentXYZTheta.cpp:326
PreComputedMotions availableMotions
Definition: EnvironmentXYZTheta.hpp:97
void setGoal(const Eigen::Vector3d &goalPos, double theta)
Definition: EnvironmentXYZTheta.cpp:240
const Motion & getMotion(const int fromStateID, const int toStateID)
Definition: EnvironmentXYZTheta.cpp:398
traversability_generator3d::TraversabilityGenerator3d::MLGrid MLGrid
Definition: EnvironmentXYZTheta.hpp:26
const MLGrid & getMlsMap() const
Definition: EnvironmentXYZTheta.cpp:1138
ThetaNode * createNewStateFromPose(const std::string &name, const Eigen::Vector3d &pos, double theta, ugv_nav4d::EnvironmentXYZTheta::XYZNode **xyzBackNode)
Definition: EnvironmentXYZTheta.cpp:129
XYZNode * goalXYZNode
Definition: EnvironmentXYZTheta.hpp:102
ThetaNode * createNewState(const DiscreteTheta &curTheta, EnvironmentXYZTheta::XYZNode *curNode)
Definition: EnvironmentXYZTheta.cpp:520
maps::grid::Vector3d getStatePosition(const int stateID) const
Definition: EnvironmentXYZTheta.cpp:389
Definition: PreComputedMotions.hpp:29
Definition: EnvironmentXYZTheta.hpp:18
Definition: EnvironmentXYZTheta.hpp:19
Definition: EnvironmentXYZTheta.hpp:20
Definition: PreComputedMotions.hpp:88
Definition: EnvironmentXYZTheta.hpp:17
Definition: Dijkstra.cpp:8
Definition: EnvironmentXYZTheta.hpp:68
Distance(double toStart, double toGoal)
Definition: EnvironmentXYZTheta.hpp:71
double distToStart
Definition: EnvironmentXYZTheta.hpp:69
double distToGoal
Definition: EnvironmentXYZTheta.hpp:70
virtual const char * what() const
Definition: EnvironmentXYZTheta.hpp:36
EnvironmentXYZThetaException(const std::string &what)
Definition: EnvironmentXYZTheta.hpp:33
const std::string msg
Definition: EnvironmentXYZTheta.hpp:40
Definition: EnvironmentXYZTheta.hpp:82
Hash(XYZNode *node, ThetaNode *thetaNode)
Definition: EnvironmentXYZTheta.hpp:83
XYZNode * node
Definition: EnvironmentXYZTheta.hpp:86
ThetaNode * thetaNode
Definition: EnvironmentXYZTheta.hpp:87
Definition: EnvironmentXYZTheta.hpp:54
traversability_generator3d::TravGenNode * travNode
Definition: EnvironmentXYZTheta.hpp:55
PlannerData()
Definition: EnvironmentXYZTheta.hpp:55
std::map< DiscreteTheta, ThetaNode * > thetaToNodes
Definition: EnvironmentXYZTheta.hpp:63
Definition: EnvironmentXYZTheta.hpp:46
DiscreteTheta theta
Definition: EnvironmentXYZTheta.hpp:49
ThetaNode(const DiscreteTheta &t)
Definition: EnvironmentXYZTheta.hpp:47
int id
Definition: EnvironmentXYZTheta.hpp:47
Definition: Mobility.hpp:12