Nav2 Navigation Stack - lyrical  lyrical
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 <functional>
19 #include <memory>
20 #include <string>
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/obstacle_heuristic.hpp"
29 #include "nav2_smac_planner/distance_heuristic.hpp"
30 #include "nav2_smac_planner/node_hybrid.hpp"
31 #include "nav2_smac_planner/utils.hpp"
32 
33 namespace nav2_smac_planner
34 {
35 
36 // forward declare
37 class NodeLattice;
38 class NodeHybrid;
39 
45 {
50 
56  void initMotionModel(
57  unsigned int & size_x_in,
58  SearchInfo & search_info);
59 
66  MotionPrimitivePtrs getMotionPrimitives(
67  const NodeLattice * node,
68  unsigned int & direction_change_index);
69 
77  static LatticeMetadata getLatticeMetadata(const std::string & lattice_filepath);
78 
84  unsigned int getClosestAngularBin(const double & theta);
85 
91  float & getAngleFromBin(const unsigned int & bin_idx);
92 
98  double getAngle(const double & theta);
99 
100  unsigned int size_x;
101  unsigned int num_angle_quantization;
102  float change_penalty;
103  float non_straight_penalty;
104  float cost_penalty;
105  float reverse_penalty;
106  float travel_distance_reward;
107  float rotation_penalty;
108  float min_turning_radius;
109  bool allow_reverse_expansion;
110  bool downsample_obstacle_heuristic;
111  bool use_quadratic_cost_penalty;
112  std::vector<std::vector<MotionPrimitive>> motion_primitives;
113  ompl::base::StateSpacePtr state_space;
114  std::vector<TrigValues> trig_values;
115  std::string current_lattice_filepath;
116  LatticeMetadata lattice_metadata;
117  MotionModel motion_model = MotionModel::UNKNOWN;
118 };
119 
125 {
126 public:
127  typedef NodeLattice * NodePtr;
128  typedef std::unique_ptr<std::vector<NodeLattice>> Graph;
129  typedef std::vector<NodePtr> NodeVector;
131  typedef NodeHybrid::CoordinateVector CoordinateVector;
132 
133  struct NodeContext
134  {
139  {
140  obstacle_heuristic = std::make_unique<ObstacleHeuristic>();
141  distance_heuristic = std::make_unique<DistanceHeuristic<NodeLattice>>();
142  }
143 
144  LatticeMotionTable motion_table;
145  std::unique_ptr<ObstacleHeuristic> obstacle_heuristic;
146  std::unique_ptr<DistanceHeuristic<NodeLattice>> distance_heuristic;
147  };
148 
153  explicit NodeLattice(const uint64_t index, NodeContext * ctx);
154 
158  ~NodeLattice();
159 
165  bool operator==(const NodeLattice & rhs) const
166  {
167  return this->_index == rhs._index;
168  }
169 
174  inline void setPose(const Coordinates & pose_in)
175  {
176  pose = pose_in;
177  }
178 
182  void reset();
183 
189  {
190  _motion_primitive = prim;
191  }
192 
198  {
199  return _motion_primitive;
200  }
201 
206  inline float getAccumulatedCost()
207  {
208  return _accumulated_cost;
209  }
210 
215  inline void setAccumulatedCost(const float & cost_in)
216  {
217  _accumulated_cost = cost_in;
218  }
219 
224  inline float getCost()
225  {
226  return _cell_cost;
227  }
228 
233  inline bool wasVisited()
234  {
235  return _was_visited;
236  }
237 
241  inline void visited()
242  {
243  _was_visited = true;
244  }
245 
250  inline uint64_t getIndex()
251  {
252  return _index;
253  }
254 
258  inline void backwards(bool back = true)
259  {
260  _backwards = back;
261  }
262 
267  inline bool isBackward()
268  {
269  return _backwards;
270  }
271 
282  bool isNodeValid(
283  const bool & traverse_unknown,
284  GridCollisionChecker * collision_checker,
285  MotionPrimitive * primitive = nullptr,
286  bool is_backwards = false);
287 
293  float getTraversalCost(const NodePtr & child);
294 
304  static inline uint64_t getIndex(
305  const unsigned int & x, const unsigned int & y, const unsigned int & angle,
306  const unsigned int & width, const unsigned int & angle_quantization)
307  {
308  // Hybrid-A* and State Lattice share a coordinate system
309  return NodeHybrid::getIndex(x, y, angle, width, angle_quantization);
310  }
311 
319  static inline Coordinates getCoords(
320  const uint64_t & index,
321  const unsigned int & width, const unsigned int & angle_quantization)
322  {
323  // Hybrid-A* and State Lattice share a coordinate system
325  (index / angle_quantization) % width, // x
326  index / (angle_quantization * width), // y
327  index % angle_quantization); // theta
328  }
329 
336  float getHeuristicCost(
337  const Coordinates & node_coords,
338  const CoordinateVector & goals_coords);
339 
348  static void initMotionModel(
349  NodeContext * ctx,
350  const MotionModel & motion_model,
351  unsigned int & size_x,
352  unsigned int & size_y,
353  unsigned int & angle_quantization,
354  SearchInfo & search_info);
355 
363  void getNeighbors(
364  std::function<bool(const uint64_t &,
365  nav2_smac_planner::NodeLattice * &)> & validity_checker,
366  GridCollisionChecker * collision_checker,
367  const bool & traverse_unknown,
368  NodeVector & neighbors);
369 
375  bool backtracePath(CoordinateVector & path);
376 
381  void addNodeToPath(NodePtr current_node, CoordinateVector & path);
382 
383  NodeLattice * parent;
384  Coordinates pose;
385 
386 private:
387  float _cell_cost;
388  float _accumulated_cost;
389  uint64_t _index;
390  bool _was_visited;
391  MotionPrimitive * _motion_primitive;
392  bool _backwards;
393  bool _is_node_valid{false};
394  NodeContext * _ctx = nullptr;
395 };
396 
397 } // namespace nav2_smac_planner
398 
399 #endif // NAV2_SMAC_PLANNER__NODE_LATTICE_HPP_
A costmap grid collision checker.
uint64_t getIndex()
Gets cell index.
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.
bool operator==(const NodeLattice &rhs) const
operator== for comparisons
uint64_t getIndex()
Gets cell index.
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.
~NodeLattice()
A destructor for nav2_smac_planner::NodeLattice.
float getCost()
Gets the costmap cost at this node.
void reset()
Reset method for new search.
NodeLattice(const uint64_t index, NodeContext *ctx)
A constructor for nav2_smac_planner::NodeLattice.
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.
float getHeuristicCost(const Coordinates &node_coords, const CoordinateVector &goals_coords)
Get cost of heuristic of node.
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.
MotionPrimitive *& getMotionPrimitive()
Gets 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, const unsigned int &width, const unsigned int &angle_quantization)
Get index at coordinates.
void setMotionPrimitive(MotionPrimitive *prim)
Sets the motion primitive used to achieve node in search.
float getAccumulatedCost()
Gets the accumulated cost at this node.
void setPose(const Coordinates &pose_in)
setting continuous coordinate search poses (in partial-cells)
Implementation of coordinate2d structure.
Definition: types.hpp:224
A struct of all lattice metadata.
Definition: types.hpp:155
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:169
NodeContext()
A constructor for nav2_smac_planner::NodeContext.
Search properties and penalties.
Definition: types.hpp:38