Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
node_hybrid.hpp
1 // Copyright (c) 2020, Samsung Research America
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License. Reserved.
14 
15 #ifndef NAV2_SMAC_PLANNER__NODE_HYBRID_HPP_
16 #define NAV2_SMAC_PLANNER__NODE_HYBRID_HPP_
17 
18 #include <functional>
19 #include <memory>
20 #include <utility>
21 #include <vector>
22 
23 #include "ompl/base/StateSpace.h"
24 
25 #include "nav2_smac_planner/constants.hpp"
26 #include "nav2_smac_planner/types.hpp"
27 #include "nav2_smac_planner/collision_checker.hpp"
28 #include "nav2_smac_planner/costmap_downsampler.hpp"
29 #include "nav2_smac_planner/obstacle_heuristic.hpp"
30 #include "nav2_smac_planner/distance_heuristic.hpp"
31 #include "nav2_costmap_2d/costmap_2d_ros.hpp"
32 #include "nav2_costmap_2d/inflation_layer.hpp"
33 
34 namespace nav2_smac_planner
35 {
36 
37 // Must forward declare
38 class NodeHybrid;
39 
45 {
50 
58  void initDubin(
59  unsigned int & size_x_in,
60  unsigned int & size_y_in,
61  unsigned int & angle_quantization_in,
62  SearchInfo & search_info);
63 
71  void initReedsShepp(
72  unsigned int & size_x_in,
73  unsigned int & size_y_in,
74  unsigned int & angle_quantization_in,
75  SearchInfo & search_info);
76 
82  MotionPoses getProjections(const NodeHybrid * node);
83 
89  unsigned int getClosestAngularBin(const double & theta);
90 
96  float getAngleFromBin(const unsigned int & bin_idx);
97 
103  double getAngle(const double & theta);
104 
105  MotionModel motion_model = MotionModel::UNKNOWN;
106  MotionPoses projections;
107  unsigned int size_x;
108  unsigned int num_angle_quantization;
109  float num_angle_quantization_float;
110  float min_turning_radius;
111  float bin_size;
112  float change_penalty;
113  float non_straight_penalty;
114  float cost_penalty;
115  float reverse_penalty;
116  float travel_distance_reward;
117  bool downsample_obstacle_heuristic;
118  bool use_quadratic_cost_penalty;
119  bool allow_primitive_interpolation;
120  ompl::base::StateSpacePtr state_space;
121  std::vector<std::vector<double>> delta_xs;
122  std::vector<std::vector<double>> delta_ys;
123  std::vector<TrigValues> trig_values;
124  std::vector<float> travel_costs;
125 };
126 
132 {
133 public:
134  typedef NodeHybrid * NodePtr;
135  typedef std::unique_ptr<std::vector<NodeHybrid>> Graph;
136  typedef std::vector<NodePtr> NodeVector;
138  typedef std::vector<Coordinates> CoordinateVector;
139 
140  struct NodeContext
141  {
146  {
147  obstacle_heuristic = std::make_unique<ObstacleHeuristic>();
148  distance_heuristic = std::make_unique<DistanceHeuristic<NodeHybrid>>();
149  }
150  HybridMotionTable motion_table;
151  std::unique_ptr<ObstacleHeuristic> obstacle_heuristic;
152  std::unique_ptr<DistanceHeuristic<NodeHybrid>> distance_heuristic;
153  };
158  explicit NodeHybrid(const uint64_t index, NodeContext * ctx);
159 
163  ~NodeHybrid();
164 
170  bool operator==(const NodeHybrid & rhs) const
171  {
172  return this->_index == rhs._index;
173  }
174 
179  inline void setPose(const Coordinates & pose_in)
180  {
181  pose = pose_in;
182  }
183 
187  void reset();
188 
193  inline float getAccumulatedCost()
194  {
195  return _accumulated_cost;
196  }
197 
202  inline void setAccumulatedCost(const float & cost_in)
203  {
204  _accumulated_cost = cost_in;
205  }
206 
211  inline void setMotionPrimitiveIndex(const unsigned int & idx, const TurnDirection & turn_dir)
212  {
213  _motion_primitive_index = idx;
214  _turn_dir = turn_dir;
215  }
216 
221  inline unsigned int & getMotionPrimitiveIndex()
222  {
223  return _motion_primitive_index;
224  }
225 
230  inline TurnDirection & getTurnDirection()
231  {
232  return _turn_dir;
233  }
234 
239  inline float getCost()
240  {
241  return _cell_cost;
242  }
243 
248  inline bool wasVisited()
249  {
250  return _was_visited;
251  }
252 
256  inline void visited()
257  {
258  _was_visited = true;
259  }
260 
265  inline uint64_t getIndex()
266  {
267  return _index;
268  }
269 
276  bool isNodeValid(
277  const bool & traverse_unknown,
278  GridCollisionChecker * collision_checker);
279 
285  float getTraversalCost(const NodePtr & child);
286 
296  static inline uint64_t getIndex(
297  const unsigned int & x, const unsigned int & y, const unsigned int & angle,
298  const unsigned int & width, const unsigned int & angle_quantization)
299  {
300  return static_cast<uint64_t>(angle) + static_cast<uint64_t>(x) *
301  static_cast<uint64_t>(angle_quantization) +
302  static_cast<uint64_t>(y) * static_cast<uint64_t>(width) *
303  static_cast<uint64_t>(angle_quantization);
304  }
305 
313  static inline Coordinates getCoords(
314  const uint64_t & index,
315  const unsigned int & width, const unsigned int & angle_quantization)
316  {
317  return Coordinates(
318  (index / angle_quantization) % width, // x
319  index / (angle_quantization * width), // y
320  index % angle_quantization); // theta
321  }
322 
329  float getHeuristicCost(
330  const Coordinates & node_coords,
331  const CoordinateVector & goals_coords);
332 
341  static void initMotionModel(
342  NodeContext * ctx,
343  const MotionModel & motion_model,
344  unsigned int & size_x,
345  unsigned int & size_y,
346  unsigned int & angle_quantization,
347  SearchInfo & search_info);
348 
356  void getNeighbors(
357  std::function<bool(const uint64_t &,
358  nav2_smac_planner::NodeHybrid * &)> & validity_checker,
359  GridCollisionChecker * collision_checker,
360  const bool & traverse_unknown,
361  NodeVector & neighbors);
362 
368  bool backtracePath(CoordinateVector & path);
369 
370  NodeHybrid * parent;
371  Coordinates pose;
372 
373 private:
374  float _cell_cost;
375  float _accumulated_cost;
376  uint64_t _index;
377  bool _was_visited;
378  unsigned int _motion_primitive_index;
379  TurnDirection _turn_dir;
380  bool _is_node_valid{false};
381  NodeContext * _ctx = nullptr;
382 };
383 
384 } // namespace nav2_smac_planner
385 
386 #endif // NAV2_SMAC_PLANNER__NODE_HYBRID_HPP_
A costmap grid collision checker.
NodeHybrid implementation for graph, Hybrid-A*.
uint64_t getIndex()
Gets cell index.
bool isNodeValid(const bool &traverse_unknown, GridCollisionChecker *collision_checker)
Check if this node is valid.
void setAccumulatedCost(const float &cost_in)
Sets the accumulated cost at this node.
float getTraversalCost(const NodePtr &child)
Get traversal cost of parent node to child node.
void setPose(const Coordinates &pose_in)
setting continuous coordinate search poses (in partial-cells)
~NodeHybrid()
A destructor for nav2_smac_planner::NodeHybrid.
NodeHybrid(const uint64_t index, NodeContext *ctx)
A constructor for nav2_smac_planner::NodeHybrid.
void getNeighbors(std::function< bool(const uint64_t &, nav2_smac_planner::NodeHybrid *&)> &validity_checker, GridCollisionChecker *collision_checker, const bool &traverse_unknown, NodeVector &neighbors)
Retrieve all valid neighbors of a node.
float getCost()
Gets the costmap cost at this node.
float getAccumulatedCost()
Gets the accumulated cost at this node.
void reset()
Reset method for new search.
bool operator==(const NodeHybrid &rhs) const
operator== for comparisons
static uint64_t getIndex(const unsigned int &x, const unsigned int &y, const unsigned int &angle, const unsigned int &width, const unsigned int &angle_quantization)
Get index at coordinates.
void setMotionPrimitiveIndex(const unsigned int &idx, const TurnDirection &turn_dir)
Sets the motion primitive index used to achieve node in search.
unsigned int & getMotionPrimitiveIndex()
Gets the motion primitive index used to achieve node in search.
static void initMotionModel(NodeContext *ctx, const MotionModel &motion_model, unsigned int &size_x, unsigned int &size_y, unsigned int &angle_quantization, SearchInfo &search_info)
Initialize motion models.
static Coordinates getCoords(const uint64_t &index, const unsigned int &width, const unsigned int &angle_quantization)
Get coordinates at index.
bool wasVisited()
Gets if cell has been visited in search.
float getHeuristicCost(const Coordinates &node_coords, const CoordinateVector &goals_coords)
Get cost of heuristic of node.
TurnDirection & getTurnDirection()
Gets the motion primitive turning direction used to achieve node in search.
bool backtracePath(CoordinateVector &path)
Set the starting pose for planning, as a node index.
void visited()
Sets if cell has been visited in search.
Implementation of coordinate2d structure.
Definition: types.hpp:224
A table of motion primitives and related functions.
Definition: node_hybrid.hpp:45
void initReedsShepp(unsigned int &size_x_in, unsigned int &size_y_in, unsigned int &angle_quantization_in, SearchInfo &search_info)
Initializing using Reeds-Shepp model.
MotionPoses getProjections(const NodeHybrid *node)
Get projections of motion models.
double getAngle(const double &theta)
Get the angle scaled across bins from a raw orientation.
HybridMotionTable()
A constructor for nav2_smac_planner::HybridMotionTable.
Definition: node_hybrid.hpp:49
float getAngleFromBin(const unsigned int &bin_idx)
Get the raw orientation from an angular bin.
unsigned int getClosestAngularBin(const double &theta)
Get the angular bin to use from a raw orientation.
void initDubin(unsigned int &size_x_in, unsigned int &size_y_in, unsigned int &angle_quantization_in, SearchInfo &search_info)
Initializing using Dubin model.
Definition: node_hybrid.cpp:46
NodeContext()
A constructor for nav2_smac_planner::NodeContext.
Search properties and penalties.
Definition: types.hpp:38