Nav2 Navigation Stack - jazzy  jazzy
ROS 2 Navigation Stack
node_lattice.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_LATTICE_HPP_
16 #define NAV2_SMAC_PLANNER__NODE_LATTICE_HPP_
17 
18 #include <math.h>
19 
20 #include <vector>
21 #include <cmath>
22 #include <iostream>
23 #include <functional>
24 #include <queue>
25 #include <memory>
26 #include <utility>
27 #include <limits>
28 #include <string>
29 
30 #include "nlohmann/json.hpp"
31 #include "ompl/base/StateSpace.h"
32 #include "angles/angles.h"
33 
34 #include "nav2_smac_planner/constants.hpp"
35 #include "nav2_smac_planner/types.hpp"
36 #include "nav2_smac_planner/collision_checker.hpp"
37 #include "nav2_smac_planner/node_hybrid.hpp"
38 #include "nav2_smac_planner/utils.hpp"
39 
40 namespace nav2_smac_planner
41 {
42 
43 // forward declare
44 class NodeLattice;
45 class NodeHybrid;
46 
52 {
57 
63  void initMotionModel(
64  unsigned int & size_x_in,
65  SearchInfo & search_info);
66 
73  MotionPrimitivePtrs getMotionPrimitives(
74  const NodeLattice * node,
75  unsigned int & direction_change_index);
76 
84  static LatticeMetadata getLatticeMetadata(const std::string & lattice_filepath);
85 
91  unsigned int getClosestAngularBin(const double & theta);
92 
98  float & getAngleFromBin(const unsigned int & bin_idx);
99 
105  double getAngle(const double & theta);
106 
107  unsigned int size_x;
108  unsigned int num_angle_quantization;
109  float change_penalty;
110  float non_straight_penalty;
111  float cost_penalty;
112  float reverse_penalty;
113  float travel_distance_reward;
114  float rotation_penalty;
115  float min_turning_radius;
116  bool allow_reverse_expansion;
117  std::vector<std::vector<MotionPrimitive>> motion_primitives;
118  ompl::base::StateSpacePtr state_space;
119  std::vector<TrigValues> trig_values;
120  std::string current_lattice_filepath;
121  LatticeMetadata lattice_metadata;
122  MotionModel motion_model = MotionModel::UNKNOWN;
123 };
124 
130 {
131 public:
132  typedef NodeLattice * NodePtr;
133  typedef std::unique_ptr<std::vector<NodeLattice>> Graph;
134  typedef std::vector<NodePtr> NodeVector;
136  typedef NodeHybrid::CoordinateVector CoordinateVector;
137 
142  explicit NodeLattice(const uint64_t index);
143 
147  ~NodeLattice();
148 
154  bool operator==(const NodeLattice & rhs)
155  {
156  return this->_index == rhs._index;
157  }
158 
163  inline void setPose(const Coordinates & pose_in)
164  {
165  pose = pose_in;
166  }
167 
171  void reset();
172 
178  {
179  _motion_primitive = prim;
180  }
181 
187  {
188  return _motion_primitive;
189  }
190 
195  inline float getAccumulatedCost()
196  {
197  return _accumulated_cost;
198  }
199 
204  inline void setAccumulatedCost(const float & cost_in)
205  {
206  _accumulated_cost = cost_in;
207  }
208 
213  inline float getCost()
214  {
215  return _cell_cost;
216  }
217 
222  inline bool wasVisited()
223  {
224  return _was_visited;
225  }
226 
230  inline void visited()
231  {
232  _was_visited = true;
233  }
234 
239  inline uint64_t getIndex()
240  {
241  return _index;
242  }
243 
247  inline void backwards(bool back = true)
248  {
249  _backwards = back;
250  }
251 
256  inline bool isBackward()
257  {
258  return _backwards;
259  }
260 
271  bool isNodeValid(
272  const bool & traverse_unknown,
273  GridCollisionChecker * collision_checker,
274  MotionPrimitive * primitive = nullptr,
275  bool is_backwards = false);
276 
282  float getTraversalCost(const NodePtr & child);
283 
291  static inline uint64_t getIndex(
292  const unsigned int & x, const unsigned int & y, const unsigned int & angle)
293  {
294  // Hybrid-A* and State Lattice share a coordinate system
295  return NodeHybrid::getIndex(
296  x, y, angle, motion_table.size_x,
297  motion_table.num_angle_quantization);
298  }
299 
307  static inline Coordinates getCoords(
308  const uint64_t & index,
309  const unsigned int & width, const unsigned int & angle_quantization)
310  {
311  // Hybrid-A* and State Lattice share a coordinate system
313  (index / angle_quantization) % width, // x
314  index / (angle_quantization * width), // y
315  index % angle_quantization); // theta
316  }
317 
324  static float getHeuristicCost(
325  const Coordinates & node_coords,
326  const Coordinates & goal_coordinates);
327 
336  static void initMotionModel(
337  const MotionModel & motion_model,
338  unsigned int & size_x,
339  unsigned int & size_y,
340  unsigned int & angle_quantization,
341  SearchInfo & search_info);
342 
351  static void precomputeDistanceHeuristic(
352  const float & lookup_table_dim,
353  const MotionModel & motion_model,
354  const unsigned int & dim_3_size,
355  const SearchInfo & search_info);
356 
363  std::shared_ptr<nav2_costmap_2d::Costmap2DROS> costmap_ros,
364  const unsigned int & start_x, const unsigned int & start_y,
365  const unsigned int & goal_x, const unsigned int & goal_y)
366  {
367  // State Lattice and Hybrid-A* share this heuristics
368  NodeHybrid::resetObstacleHeuristic(costmap_ros, start_x, start_y, goal_x, goal_y);
369  }
370 
377  static float getObstacleHeuristic(
378  const Coordinates & node_coords,
379  const Coordinates & goal_coords,
380  const double & cost_penalty)
381  {
382  return NodeHybrid::getObstacleHeuristic(node_coords, goal_coords, cost_penalty);
383  }
384 
393  static float getDistanceHeuristic(
394  const Coordinates & node_coords,
395  const Coordinates & goal_coords,
396  const float & obstacle_heuristic);
397 
405  void getNeighbors(
406  std::function<bool(const uint64_t &,
407  nav2_smac_planner::NodeLattice * &)> & validity_checker,
408  GridCollisionChecker * collision_checker,
409  const bool & traverse_unknown,
410  NodeVector & neighbors);
411 
417  bool backtracePath(CoordinateVector & path);
418 
423  void addNodeToPath(NodePtr current_node, CoordinateVector & path);
424 
425  NodeLattice * parent;
426  Coordinates pose;
427  static LatticeMotionTable motion_table;
428  // Dubin / Reeds-Shepp lookup and size for dereferencing
429  static LookupTable dist_heuristic_lookup_table;
430  static float size_lookup;
431 
432 private:
433  float _cell_cost;
434  float _accumulated_cost;
435  uint64_t _index;
436  bool _was_visited;
437  MotionPrimitive * _motion_primitive;
438  bool _backwards;
439  bool _is_node_valid{false};
440 };
441 
442 } // namespace nav2_smac_planner
443 
444 #endif // NAV2_SMAC_PLANNER__NODE_LATTICE_HPP_
A costmap grid collision checker.
uint64_t getIndex()
Gets cell index.
static float getObstacleHeuristic(const Coordinates &node_coords, const Coordinates &goal_coords, const float &cost_penalty)
Compute the Obstacle heuristic.
static void resetObstacleHeuristic(std::shared_ptr< nav2_costmap_2d::Costmap2DROS > costmap_ros, const unsigned int &start_x, const unsigned int &start_y, const unsigned int &goal_x, const unsigned int &goal_y)
reset the obstacle heuristic state
NodeLattice implementation for graph, Hybrid-A*.
void getNeighbors(std::function< bool(const uint64_t &, nav2_smac_planner::NodeLattice *&)> &validity_checker, GridCollisionChecker *collision_checker, const bool &traverse_unknown, NodeVector &neighbors)
Retrieve all valid neighbors of a node.
void backwards(bool back=true)
Sets that this primitive is moving in reverse.
static Coordinates getCoords(const uint64_t &index, const unsigned int &width, const unsigned int &angle_quantization)
Get coordinates at index.
void setAccumulatedCost(const float &cost_in)
Sets the accumulated cost at this node.
uint64_t getIndex()
Gets cell index.
~NodeLattice()
A destructor for nav2_smac_planner::NodeLattice.
float getCost()
Gets the costmap cost at this node.
static float getHeuristicCost(const Coordinates &node_coords, const Coordinates &goal_coordinates)
Get cost of heuristic of node.
void reset()
Reset method for new search.
bool backtracePath(CoordinateVector &path)
Set the starting pose for planning, as a node index.
float getTraversalCost(const NodePtr &child)
Get traversal cost of parent node to child node.
void visited()
Sets if cell has been visited in search.
bool isNodeValid(const bool &traverse_unknown, GridCollisionChecker *collision_checker, MotionPrimitive *primitive=nullptr, bool is_backwards=false)
Check if this node is valid.
void addNodeToPath(NodePtr current_node, CoordinateVector &path)
add node to the path
bool isBackward()
Gets if this primitive is moving in reverse.
bool wasVisited()
Gets if cell has been visited in search.
static void initMotionModel(const MotionModel &motion_model, unsigned int &size_x, unsigned int &size_y, unsigned int &angle_quantization, SearchInfo &search_info)
Initialize motion models.
MotionPrimitive *& getMotionPrimitive()
Gets the motion primitive used to achieve node in search.
static void precomputeDistanceHeuristic(const float &lookup_table_dim, const MotionModel &motion_model, const unsigned int &dim_3_size, const SearchInfo &search_info)
Compute the SE2 distance heuristic.
bool operator==(const NodeLattice &rhs)
operator== for comparisons
static void resetObstacleHeuristic(std::shared_ptr< nav2_costmap_2d::Costmap2DROS > costmap_ros, const unsigned int &start_x, const unsigned int &start_y, const unsigned int &goal_x, const unsigned int &goal_y)
Compute the wavefront heuristic.
static float getObstacleHeuristic(const Coordinates &node_coords, const Coordinates &goal_coords, const double &cost_penalty)
Compute the Obstacle heuristic.
void setMotionPrimitive(MotionPrimitive *prim)
Sets the motion primitive used to achieve node in search.
static uint64_t getIndex(const unsigned int &x, const unsigned int &y, const unsigned int &angle)
Get index at coordinates.
NodeLattice(const uint64_t index)
A constructor for nav2_smac_planner::NodeLattice.
static float getDistanceHeuristic(const Coordinates &node_coords, const Coordinates &goal_coords, const float &obstacle_heuristic)
Compute the Distance heuristic.
float getAccumulatedCost()
Gets the accumulated cost at this node.
void setPose(const Coordinates &pose_in)
setting continuous coordinate search poses (in partial-cells)
A struct of all lattice metadata.
Definition: types.hpp:165
A table of motion primitives and related functions.
static LatticeMetadata getLatticeMetadata(const std::string &lattice_filepath)
Get file metadata needed.
unsigned int getClosestAngularBin(const double &theta)
Get the angular bin to use from a raw orientation.
void initMotionModel(unsigned int &size_x_in, SearchInfo &search_info)
Initializing state lattice planner's motion model.
MotionPrimitivePtrs getMotionPrimitives(const NodeLattice *node, unsigned int &direction_change_index)
Get projections of motion models.
float & getAngleFromBin(const unsigned int &bin_idx)
Get the raw orientation from an angular bin.
double getAngle(const double &theta)
Get the angular bin to use from a raw orientation.
LatticeMotionTable()
A constructor for nav2_smac_planner::LatticeMotionTable.
A struct of all motion primitive data.
Definition: types.hpp:179
NodeHybrid implementation of coordinate structure.
Search properties and penalties.
Definition: types.hpp:36