Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
a_star.hpp
1 // Copyright (c) 2020, Samsung Research America
2 // Copyright (c) 2020, Applied Electric Vehicles Pty Ltd
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License. Reserved.
15 
16 #ifndef NAV2_SMAC_PLANNER__A_STAR_HPP_
17 #define NAV2_SMAC_PLANNER__A_STAR_HPP_
18 
19 #include <functional>
20 #include <memory>
21 #include <queue>
22 #include <tuple>
23 #include <utility>
24 #include <vector>
25 
26 #include "nav2_costmap_2d/costmap_2d.hpp"
27 #include "nav2_core/planner_exceptions.hpp"
28 
29 #include "nav2_smac_planner/thirdparty/robin_hood.h"
30 #include "nav2_smac_planner/analytic_expansion.hpp"
31 #include "nav2_smac_planner/node_2d.hpp"
32 #include "nav2_smac_planner/node_hybrid.hpp"
33 #include "nav2_smac_planner/node_lattice.hpp"
34 #include "nav2_smac_planner/node_basic.hpp"
35 #include "nav2_smac_planner/goal_manager.hpp"
36 #include "nav2_smac_planner/types.hpp"
37 #include "nav2_smac_planner/constants.hpp"
38 
39 namespace nav2_smac_planner
40 {
41 
46 template<typename NodeT>
48 {
49 public:
50  typedef NodeT * NodePtr;
52  typedef std::vector<NodePtr> NodeVector;
53  typedef std::pair<float, NodeBasic<NodeT>> NodeElement;
54  typedef typename NodeT::Coordinates Coordinates;
55  typedef typename NodeT::CoordinateVector CoordinateVector;
56  typedef typename NodeVector::iterator NeighborIterator;
57  typedef std::function<bool (const uint64_t &, NodeT * &)> NodeGetter;
59  using NodeContext = typename NodeT::NodeContext;
60 
61 
67  {
68  bool operator()(const NodeElement & a, const NodeElement & b) const
69  {
70  return a.first > b.first;
71  }
72  };
73 
74  typedef std::priority_queue<NodeElement, std::vector<NodeElement>, NodeComparator> NodeQueue;
75 
79  explicit AStarAlgorithm(const MotionModel & motion_model, const SearchInfo & search_info);
80 
85 
100  void initialize(
101  const bool & allow_unknown,
102  int & max_iterations,
103  const int & max_on_approach_iterations,
104  const int & terminal_checking_interval,
105  const double & max_planning_time,
106  const float & lookup_table_size,
107  const unsigned int & dim_3_size);
108 
118  bool createPath(
119  CoordinateVector & path, int & num_iterations, const float & tolerance,
120  std::function<bool()> cancel_checker,
121  std::vector<std::tuple<float, float, float>> * expansions_log = nullptr);
122 
127  void setSearchInfo(const SearchInfo & search_info) {_search_info = search_info;}
128 
133  void setCollisionChecker(GridCollisionChecker * collision_checker);
134 
143  void setGoal(
144  const float & mx,
145  const float & my,
146  const unsigned int & dim_3,
147  const GoalHeadingMode & goal_heading_mode = GoalHeadingMode::DEFAULT,
148  const int & coarse_search_resolution = 1);
149 
156  void setStart(
157  const float & mx,
158  const float & my,
159  const unsigned int & dim_3);
160 
165  int & getMaxIterations();
166 
171  NodePtr & getStart();
172 
178 
183  float & getToleranceHeuristic();
184 
189  unsigned int & getSizeX();
190 
195  unsigned int & getSizeY();
196 
201  unsigned int & getSizeDim3();
202 
207  unsigned int getCoarseSearchResolution();
208 
213  GoalManagerT getGoalManager();
214 
219  NodeContext * getContext();
220 
221 protected:
226  inline NodePtr getNextNode();
227 
233  inline void addNode(const float & cost, NodePtr & node);
234 
239  inline NodePtr addToGraph(const uint64_t & index);
240 
246  inline float getHeuristicCost(const NodePtr & node);
247 
252  inline bool areInputsValid();
253 
259  inline bool getClosestPathWithinTolerance(CoordinateVector & path);
260 
264  inline void clearQueue();
265 
269  inline void clearGraph();
270 
278  inline uint64_t getIndex(
279  const unsigned int & x, const unsigned int & y, const unsigned int & dim3);
280 
286  inline bool onVisitationCheckNode(const NodePtr & node);
287 
293  inline void populateExpansionsLog(
294  const NodePtr & node, std::vector<std::tuple<float, float, float>> * expansions_log);
295 
296  bool _traverse_unknown;
297  bool _is_initialized;
298  int _max_iterations;
299  int _max_on_approach_iterations;
300  int _terminal_checking_interval;
301  double _max_planning_time;
302  float _tolerance;
303  unsigned int _x_size;
304  unsigned int _y_size;
305  unsigned int _dim3_size;
306  unsigned int _coarse_search_resolution;
307  SearchInfo _search_info;
308 
309  NodePtr _start;
310  GoalManagerT _goal_manager;
311  Graph _graph;
312  NodeQueue _queue;
313 
314  MotionModel _motion_model;
315  NodeHeuristicPair _best_heuristic_node;
316 
317  GridCollisionChecker * _collision_checker;
318  nav2_costmap_2d::Costmap2D * _costmap;
319  std::unique_ptr<AnalyticExpansion<NodeT>> _expander;
320  std::shared_ptr<NodeContext> _shared_ctx;
321 };
322 
323 } // namespace nav2_smac_planner
324 
325 #include "nav2_smac_planner/a_star_impl.hpp" // NOLINT
326 
327 #endif // NAV2_SMAC_PLANNER__A_STAR_HPP_
A 2D costmap provides a mapping between points in the world and their associated "costs".
Definition: costmap_2d.hpp:69
An A* implementation for planning in a costmap. Templated based on the Node type.
Definition: a_star.hpp:48
~AStarAlgorithm()
A destructor for nav2_smac_planner::AStarAlgorithm.
Definition: a_star_impl.hpp:57
unsigned int & getSizeDim3()
Get number of angle quantization bins (SE2) or Z coordinate (XYZ)
unsigned int getCoarseSearchResolution()
Get the resolution of the coarse search.
bool createPath(CoordinateVector &path, int &num_iterations, const float &tolerance, std::function< bool()> cancel_checker, std::vector< std::tuple< float, float, float >> *expansions_log=nullptr)
Creating path from given costmap, start, and goal.
int & getOnApproachMaxIterations()
Get maximum number of on-approach iterations after within threshold.
bool onVisitationCheckNode(const NodePtr &node)
Check if node has been visited.
void setCollisionChecker(GridCollisionChecker *collision_checker)
Sets the collision checker to use.
void initialize(const bool &allow_unknown, int &max_iterations, const int &max_on_approach_iterations, const int &terminal_checking_interval, const double &max_planning_time, const float &lookup_table_size, const unsigned int &dim_3_size)
Initialization of the planner with defaults.
Definition: a_star_impl.hpp:62
NodeContext * getContext()
Get pointer to shared node context.
NodePtr getNextNode()
Get pointer to next goal in open set.
bool areInputsValid()
Check if inputs to planner are valid.
void clearQueue()
Clear heuristic queue of nodes to search.
void setSearchInfo(const SearchInfo &search_info)
Update the search info used by the algorithm.
Definition: a_star.hpp:127
AStarAlgorithm(const MotionModel &motion_model, const SearchInfo &search_info)
A constructor for nav2_smac_planner::AStarAlgorithm.
Definition: a_star_impl.hpp:38
void populateExpansionsLog(const NodePtr &node, std::vector< std::tuple< float, float, float >> *expansions_log)
Populate a debug log of expansions for Hybrid-A* for visualization.
bool getClosestPathWithinTolerance(CoordinateVector &path)
Get the closest path within tolerance if available.
int & getMaxIterations()
Get maximum number of iterations to plan.
uint64_t getIndex(const unsigned int &x, const unsigned int &y, const unsigned int &dim3)
Get index at coordinates.
void clearGraph()
Clear graph of nodes searched.
unsigned int & getSizeY()
Get size of graph in Y.
void setStart(const float &mx, const float &my, const unsigned int &dim_3)
Set the starting pose for planning, as a node index.
void addNode(const float &cost, NodePtr &node)
Add a node to the open set.
GoalManagerT getGoalManager()
Get the goals manager class.
unsigned int & getSizeX()
Get size of graph in X.
float getHeuristicCost(const NodePtr &node)
Get cost of heuristic of node.
float & getToleranceHeuristic()
Get tolerance, in node nodes.
NodePtr & getStart()
Get pointer reference to starting node.
NodePtr addToGraph(const uint64_t &index)
Adds node to graph.
void setGoal(const float &mx, const float &my, const unsigned int &dim_3, const GoalHeadingMode &goal_heading_mode=GoalHeadingMode::DEFAULT, const int &coarse_search_resolution=1)
Set the goal for planning, as a node index.
Responsible for managing multiple variables storing information on the goal.
A costmap grid collision checker.
Search properties and penalties.
Definition: types.hpp:38