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