Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
node_2d.cpp
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 #include "nav2_smac_planner/node_2d.hpp"
17 
18 #include <vector>
19 #include <limits>
20 
21 namespace nav2_smac_planner
22 {
23 
24 Node2D::Node2D(const uint64_t index, NodeContext * ctx)
25 : parent(nullptr),
26  _cell_cost(std::numeric_limits<float>::quiet_NaN()),
27  _accumulated_cost(std::numeric_limits<float>::max()),
28  _index(index),
29  _was_visited(false),
30  _is_queued(false),
31  _in_collision(false),
32  _ctx(ctx)
33 {
34 }
35 
37 {
38  parent = nullptr;
39 }
40 
42 {
43  parent = nullptr;
44  _cell_cost = std::numeric_limits<float>::quiet_NaN();
45  _accumulated_cost = std::numeric_limits<float>::max();
46  _was_visited = false;
47  _is_queued = false;
48  _in_collision = false;
49 }
50 
52  const bool & traverse_unknown,
53  GridCollisionChecker * collision_checker)
54 {
55  // Already found, we can return the result
56  if (!std::isnan(_cell_cost)) {
57  return !_in_collision;
58  }
59 
60  _in_collision = collision_checker->inCollision(this->getIndex(), traverse_unknown);
61  _cell_cost = collision_checker->getCost();
62  return !_in_collision;
63 }
64 
65 float Node2D::getTraversalCost(const NodePtr & child)
66 {
67  float normalized_cost = child->getCost() / 252.0;
68  const Coordinates A = getCoords(child->getIndex());
69  const Coordinates B = getCoords(this->getIndex());
70  const float & dx = A.x - B.x;
71  const float & dy = A.y - B.y;
72  static float sqrt_2 = sqrt(2);
73 
74  // If a diagonal move, travel cost is sqrt(2) not 1.0.
75  if ((dx * dx + dy * dy) > 1.05) {
76  return sqrt_2 * (1.0 + _ctx->cost_travel_multiplier * normalized_cost);
77  }
78 
79  // Length = 1.0
80  return 1.0 + _ctx->cost_travel_multiplier * normalized_cost;
81 }
82 
84  const Coordinates & node_coords,
85  const CoordinateVector & goals_coords)
86 {
87  // Using Moore distance as it more accurately represents the distances
88  // even a Van Neumann neighborhood robot can navigate.
89  auto dx = goals_coords[0].x - node_coords.x;
90  auto dy = goals_coords[0].y - node_coords.y;
91  return std::sqrt(dx * dx + dy * dy);
92 }
93 
95  NodeContext * ctx,
96  const MotionModel & motion_model,
97  unsigned int & x_size_uint,
98  unsigned int & /*size_y*/,
99  unsigned int & /*num_angle_quantization*/,
100  SearchInfo & search_info)
101 {
102  if (motion_model != MotionModel::TWOD) {
103  throw std::runtime_error("Invalid motion model for 2D node.");
104  }
105 
106  int x_size = static_cast<int>(x_size_uint);
107  ctx->cost_travel_multiplier = search_info.cost_penalty;
108  ctx->neighbors_grid_offsets = {-1, +1, -x_size, +x_size, -x_size - 1,
109  -x_size + 1, +x_size - 1, +x_size + 1};
110 }
111 
113  std::function<bool(const uint64_t &,
114  nav2_smac_planner::Node2D * &)> & NeighborGetter,
115  GridCollisionChecker * collision_checker,
116  const bool & traverse_unknown,
117  NodeVector & neighbors)
118 {
119  // NOTE(stevemacenski): Irritatingly, the order here matters. If you start in free
120  // space and then expand 8-connected, the first set of neighbors will be all cost
121  // 1.0. Then its expansion will all be 2 * 1.0 but now multiple
122  // nodes are touching that node so the last cell to update the back pointer wins.
123  // Thusly, the ordering ends with the cardinal directions for both sets such that
124  // behavior is consistent in large free spaces between them.
125  // 100 50 0
126  // 100 50 50
127  // 100 100 100 where lower-middle '100' is visited with same cost by both bottom '50' nodes
128  // Therefore, it is valuable to have some low-potential across the entire map
129  // rather than a small inflation around the obstacles
130  uint64_t index;
131  NodePtr neighbor;
132  uint64_t node_i = this->getIndex();
133  const Coordinates coord_parent = getCoords(this->getIndex());
134  Coordinates child;
135 
136  for (unsigned int i = 0; i != _ctx->neighbors_grid_offsets.size(); ++i) {
137  index = node_i + _ctx->neighbors_grid_offsets[i];
138 
139  // Check for wrap around conditions
140  child = getCoords(index);
141  if (fabs(coord_parent.x - child.x) > 1 || fabs(coord_parent.y - child.y) > 1) {
142  continue;
143  }
144 
145  if (NeighborGetter(index, neighbor)) {
146  if (neighbor->isNodeValid(traverse_unknown, collision_checker) && !neighbor->wasVisited()) {
147  neighbors.push_back(neighbor);
148  }
149  }
150  }
151 }
152 
153 bool Node2D::backtracePath(CoordinateVector & path)
154 {
155  if (!this->parent) {
156  return false;
157  }
158 
159  NodePtr current_node = this;
160 
161  while (current_node->parent) {
162  path.push_back(
163  Node2D::getCoords(current_node->getIndex()));
164  current_node = current_node->parent;
165  }
166 
167  // add the start pose
168  path.push_back(Node2D::getCoords(current_node->getIndex()));
169 
170  return true;
171 }
172 
173 } // namespace nav2_smac_planner
A costmap grid collision checker.
bool inCollision(const float &x, const float &y, const float &theta, const bool &traverse_unknown)
Check if in collision with costmap and footprint at pose.
float getCost()
Get cost at footprint pose in costmap.
Node2D implementation for graph.
Definition: node_2d.hpp:36
bool wasVisited()
Gets if cell has been visited in search.
Definition: node_2d.hpp:124
bool isNodeValid(const bool &traverse_unknown, GridCollisionChecker *collision_checker)
Check if this node is valid.
Definition: node_2d.cpp:51
bool backtracePath(CoordinateVector &path)
Set the starting pose for planning, as a node index.
Definition: node_2d.cpp:153
float getTraversalCost(const NodePtr &child)
get traversal cost from this node to child node
Definition: node_2d.cpp:65
static Coordinates getCoords(const uint64_t &index, const unsigned int &width, const unsigned int &angles)
Get index.
Definition: node_2d.hpp:200
uint64_t getIndex()
Gets cell index.
Definition: node_2d.hpp:159
void getNeighbors(std::function< bool(const uint64_t &, nav2_smac_planner::Node2D *&)> &validity_checker, GridCollisionChecker *collision_checker, const bool &traverse_unknown, NodeVector &neighbors)
Retrieve all valid neighbors of a node.
Definition: node_2d.cpp:112
~Node2D()
A destructor for nav2_smac_planner::Node2D.
Definition: node_2d.cpp:36
static void initMotionModel(NodeContext *ctx, const MotionModel &motion_model, unsigned int &size_x, unsigned int &size_y, unsigned int &num_angle_quantization, SearchInfo &search_info)
Initialize the neighborhood to be used in A* We support 4-connect (VON_NEUMANN) and 8-connect (MOORE)
Definition: node_2d.cpp:94
float getHeuristicCost(const Coordinates &node_coords, const CoordinateVector &goals_coords)
Get cost of heuristic of node.
Definition: node_2d.cpp:83
Node2D(const uint64_t index, NodeContext *ctx)
A constructor for nav2_smac_planner::Node2D.
Definition: node_2d.cpp:24
void reset()
Reset method for new search.
Definition: node_2d.cpp:41
float getCost()
Gets the costmap cost at this node.
Definition: node_2d.hpp:106
Search properties and penalties.
Definition: types.hpp:38