Nav2 Navigation Stack - jazzy  jazzy
ROS 2 Navigation Stack
node_lattice.cpp
1 // Copyright (c) 2021, 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 #include <math.h>
16 #include <chrono>
17 #include <vector>
18 #include <memory>
19 #include <algorithm>
20 #include <queue>
21 #include <limits>
22 #include <string>
23 #include <fstream>
24 #include <cmath>
25 
26 #include "ompl/base/ScopedState.h"
27 #include "ompl/base/spaces/DubinsStateSpace.h"
28 #include "ompl/base/spaces/ReedsSheppStateSpace.h"
29 #include "ompl/base/spaces/SE2StateSpace.h"
30 
31 #include "nav2_smac_planner/node_lattice.hpp"
32 
33 using namespace std::chrono; // NOLINT
34 
35 namespace nav2_smac_planner
36 {
37 
38 // defining static member for all instance to share
39 LatticeMotionTable NodeLattice::motion_table;
40 float NodeLattice::size_lookup = 25;
41 LookupTable NodeLattice::dist_heuristic_lookup_table;
42 
43 // Each of these tables are the projected motion models through
44 // time and space applied to the search on the current node in
45 // continuous map-coordinates (e.g. not meters but partial map cells)
46 // Currently, these are set to project *at minimum* into a neighboring
47 // cell. Though this could be later modified to project a certain
48 // amount of time or particular distance forward.
49 void LatticeMotionTable::initMotionModel(
50  unsigned int & size_x_in,
51  SearchInfo & search_info)
52 {
53  size_x = size_x_in;
54  change_penalty = search_info.change_penalty;
55  non_straight_penalty = search_info.non_straight_penalty;
56  cost_penalty = search_info.cost_penalty;
57  reverse_penalty = search_info.reverse_penalty;
58  travel_distance_reward = 1.0f - search_info.retrospective_penalty;
59  allow_reverse_expansion = search_info.allow_reverse_expansion;
60  rotation_penalty = search_info.rotation_penalty;
61  min_turning_radius = search_info.minimum_turning_radius;
62 
63  if (current_lattice_filepath == search_info.lattice_filepath) {
64  return;
65  }
66  current_lattice_filepath = search_info.lattice_filepath;
67 
68  // Get the metadata about this minimum control set
69  lattice_metadata = getLatticeMetadata(current_lattice_filepath);
70  std::ifstream latticeFile(current_lattice_filepath);
71  if (!latticeFile.is_open()) {
72  throw std::runtime_error("Could not open lattice file");
73  }
74  nlohmann::json json;
75  latticeFile >> json;
76  num_angle_quantization = lattice_metadata.number_of_headings;
77 
78  if (!state_space) {
79  if (lattice_metadata.motion_model == "omni") {
80  // Holonomic robots: straight-line analytic expansion
81  state_space = std::make_shared<ompl::base::SE2StateSpace>();
82  motion_model = MotionModel::OMNI;
83  } else if (!allow_reverse_expansion) {
84  state_space = std::make_shared<ompl::base::DubinsStateSpace>(
85  lattice_metadata.min_turning_radius);
86  motion_model = MotionModel::DUBIN;
87  } else {
88  state_space = std::make_shared<ompl::base::ReedsSheppStateSpace>(
89  lattice_metadata.min_turning_radius);
90  motion_model = MotionModel::REEDS_SHEPP;
91  }
92  }
93 
94  // Populate the motion primitives at each heading angle
95  float prev_start_angle = 0.0;
96  std::vector<MotionPrimitive> primitives;
97  nlohmann::json json_primitives = json["primitives"];
98  for (unsigned int i = 0; i < json_primitives.size(); ++i) {
99  MotionPrimitive new_primitive;
100  fromJsonToMotionPrimitive(json_primitives[i], new_primitive);
101 
102  if (prev_start_angle != new_primitive.start_angle) {
103  motion_primitives.push_back(primitives);
104  primitives.clear();
105  prev_start_angle = new_primitive.start_angle;
106  }
107  primitives.push_back(new_primitive);
108  }
109  motion_primitives.push_back(primitives);
110 
111  // Populate useful precomputed values to be leveraged
112  trig_values.reserve(lattice_metadata.number_of_headings);
113  for (unsigned int i = 0; i < lattice_metadata.heading_angles.size(); ++i) {
114  trig_values.emplace_back(
115  cos(lattice_metadata.heading_angles[i]),
116  sin(lattice_metadata.heading_angles[i]));
117  }
118 }
119 
120 MotionPrimitivePtrs LatticeMotionTable::getMotionPrimitives(
121  const NodeLattice * node,
122  unsigned int & direction_change_index)
123 {
124  MotionPrimitives & prims_at_heading = motion_primitives[node->pose.theta];
125  MotionPrimitivePtrs primitive_projection_list;
126  for (unsigned int i = 0; i != prims_at_heading.size(); i++) {
127  primitive_projection_list.push_back(&prims_at_heading[i]);
128  }
129 
130  // direction change index
131  direction_change_index = static_cast<unsigned int>(primitive_projection_list.size());
132 
133  if (allow_reverse_expansion) {
134  // Find normalized heading bin of the reverse expansion
135  double reserve_heading = node->pose.theta - (num_angle_quantization / 2);
136  if (reserve_heading < 0) {
137  reserve_heading += num_angle_quantization;
138  }
139  if (reserve_heading > num_angle_quantization) {
140  reserve_heading -= num_angle_quantization;
141  }
142 
143  MotionPrimitives & prims_at_reverse_heading = motion_primitives[reserve_heading];
144  for (unsigned int i = 0; i != prims_at_reverse_heading.size(); i++) {
145  primitive_projection_list.push_back(&prims_at_reverse_heading[i]);
146  }
147  }
148 
149  return primitive_projection_list;
150 }
151 
152 LatticeMetadata LatticeMotionTable::getLatticeMetadata(const std::string & lattice_filepath)
153 {
154  std::ifstream lattice_file(lattice_filepath);
155  if (!lattice_file.is_open()) {
156  throw std::runtime_error("Could not open lattice file!");
157  }
158 
159  nlohmann::json j;
160  lattice_file >> j;
161  LatticeMetadata metadata;
162  fromJsonToMetaData(j["lattice_metadata"], metadata);
163  return metadata;
164 }
165 
166 unsigned int LatticeMotionTable::getClosestAngularBin(const double & theta)
167 {
168  float min_dist = std::numeric_limits<float>::max();
169  unsigned int closest_idx = 0;
170  float dist = 0.0;
171  for (unsigned int i = 0; i != lattice_metadata.heading_angles.size(); i++) {
172  dist = fabs(angles::shortest_angular_distance(theta, lattice_metadata.heading_angles[i]));
173  if (dist < min_dist) {
174  min_dist = dist;
175  closest_idx = i;
176  }
177  }
178  return closest_idx;
179 }
180 
181 float & LatticeMotionTable::getAngleFromBin(const unsigned int & bin_idx)
182 {
183  return lattice_metadata.heading_angles[bin_idx];
184 }
185 
186 double LatticeMotionTable::getAngle(const double & theta)
187 {
188  return getClosestAngularBin(theta);
189 }
190 
191 NodeLattice::NodeLattice(const uint64_t index)
192 : parent(nullptr),
193  pose(0.0f, 0.0f, 0.0f),
194  _cell_cost(std::numeric_limits<float>::quiet_NaN()),
195  _accumulated_cost(std::numeric_limits<float>::max()),
196  _index(index),
197  _was_visited(false),
198  _motion_primitive(nullptr),
199  _backwards(false),
200  _is_node_valid(false)
201 {
202 }
203 
205 {
206  parent = nullptr;
207 }
208 
210 {
211  parent = nullptr;
212  _cell_cost = std::numeric_limits<float>::quiet_NaN();
213  _accumulated_cost = std::numeric_limits<float>::max();
214  _was_visited = false;
215  pose.x = 0.0f;
216  pose.y = 0.0f;
217  pose.theta = 0.0f;
218  _motion_primitive = nullptr;
219  _backwards = false;
220  _is_node_valid = false;
221 }
222 
224  const bool & traverse_unknown,
225  GridCollisionChecker * collision_checker,
226  MotionPrimitive * motion_primitive,
227  bool is_backwards)
228 {
229  // Already found, we can return the result
230  if (!std::isnan(_cell_cost)) {
231  return _is_node_valid;
232  }
233 
234  // Check primitive end pose
235  // Convert grid quantization of primitives to radians, then collision checker quantization
236  static const double bin_size = 2.0 * M_PI / collision_checker->getPrecomputedAngles().size();
237  const double angle = std::fmod(motion_table.getAngleFromBin(this->pose.theta),
238  2.0 * M_PI) / bin_size;
239  if (collision_checker->inCollision(
240  this->pose.x, this->pose.y, angle /*bin in collision checker*/, traverse_unknown))
241  {
242  _is_node_valid = false;
243  _cell_cost = collision_checker->getCost();
244  return false;
245  }
246 
247  // Set the cost of a node to the highest cost across the primitive
248  float max_cell_cost = collision_checker->getCost();
249 
250  // If valid motion primitives are set, check intermediary poses > 1 cell apart
251  if (motion_primitive) {
252  const float & grid_resolution = motion_table.lattice_metadata.grid_resolution;
253  const float & resolution_diag_sq = 2.0 * grid_resolution * grid_resolution;
254  MotionPose last_pose(1e9, 1e9, 1e9, TurnDirection::UNKNOWN);
255  MotionPose pose_dist(0.0, 0.0, 0.0, TurnDirection::UNKNOWN);
256 
257  // Back out the initial node starting point to move motion primitive relative to
258  MotionPose initial_pose, prim_pose;
259  initial_pose._x = this->pose.x - (motion_primitive->poses.back()._x / grid_resolution);
260  initial_pose._y = this->pose.y - (motion_primitive->poses.back()._y / grid_resolution);
261  initial_pose._theta = motion_table.getAngleFromBin(motion_primitive->start_angle);
262 
263  for (auto it = motion_primitive->poses.begin(); it != motion_primitive->poses.end(); ++it) {
264  // poses are in metric coordinates from (0, 0), not grid space yet
265  pose_dist = *it - last_pose;
266  // Avoid square roots by (hypot(x, y) > res) == (x*x+y*y > diag*diag)
267  if (pose_dist._x * pose_dist._x + pose_dist._y * pose_dist._y > resolution_diag_sq) {
268  last_pose = *it;
269  // Convert primitive pose into grid space if it should be checked
270  prim_pose._x = initial_pose._x + (it->_x / grid_resolution);
271  prim_pose._y = initial_pose._y + (it->_y / grid_resolution);
272  // If reversing, invert the angle because the robot is backing into the primitive
273  // not driving forward with it
274  if (is_backwards) {
275  prim_pose._theta = std::fmod(it->_theta + M_PI, 2.0 * M_PI);
276  } else {
277  prim_pose._theta = std::fmod(it->_theta, 2.0 * M_PI);
278  }
279  if (collision_checker->inCollision(
280  prim_pose._x,
281  prim_pose._y,
282  prim_pose._theta / bin_size /*bin in collision checker*/,
283  traverse_unknown))
284  {
285  _is_node_valid = false;
286  _cell_cost = std::max(max_cell_cost, collision_checker->getCost());
287  return false;
288  }
289  max_cell_cost = std::max(max_cell_cost, collision_checker->getCost());
290  }
291  }
292  }
293 
294  _cell_cost = max_cell_cost;
295  _is_node_valid = true;
296  return _is_node_valid;
297 }
298 
300 {
301  const float normalized_cost = child->getCost() / 252.0;
302  if (std::isnan(normalized_cost)) {
303  throw std::runtime_error(
304  "Node attempted to get traversal "
305  "cost without a known collision cost!");
306  }
307 
308  // this is the first node
309  MotionPrimitive * prim = this->getMotionPrimitive();
310  MotionPrimitive * transition_prim = child->getMotionPrimitive();
311  const float prim_length =
312  transition_prim->trajectory_length / motion_table.lattice_metadata.grid_resolution;
313  if (prim == nullptr) {
314  return prim_length;
315  }
316 
317  // Pure rotation in place 1 angular bin in either direction
318  if (transition_prim->trajectory_length < 1e-4) {
319  return motion_table.rotation_penalty * (1.0 + motion_table.cost_penalty * normalized_cost);
320  }
321 
322  float travel_cost = 0.0;
323  float travel_cost_raw = prim_length *
324  (motion_table.travel_distance_reward + motion_table.cost_penalty * normalized_cost);
325 
326  if (transition_prim->arc_length < 0.001) {
327  // New motion is a straight motion, no additional costs to be applied
328  travel_cost = travel_cost_raw;
329  } else {
330  if (prim->left_turn == transition_prim->left_turn) {
331  // Turning motion but keeps in same general direction: encourages to commit to actions
332  travel_cost = travel_cost_raw * motion_table.non_straight_penalty;
333  } else {
334  // Turning motion and velocity directions: penalizes wiggling.
335  travel_cost = travel_cost_raw *
336  (motion_table.non_straight_penalty + motion_table.change_penalty);
337  }
338  }
339 
340  // If backwards flag is set, this primitive is moving in reverse
341  if (child->isBackward()) {
342  // reverse direction
343  travel_cost *= motion_table.reverse_penalty;
344  }
345 
346  return travel_cost;
347 }
348 
350  const Coordinates & node_coords,
351  const Coordinates & goal_coords)
352 {
353  // get obstacle heuristic value
354  const float obstacle_heuristic = getObstacleHeuristic(
355  node_coords, goal_coords, motion_table.cost_penalty);
356  const float distance_heuristic =
357  getDistanceHeuristic(node_coords, goal_coords, obstacle_heuristic);
358  return std::max(obstacle_heuristic, distance_heuristic);
359 }
360 
362  const MotionModel & motion_model,
363  unsigned int & size_x,
364  unsigned int & /*size_y*/,
365  unsigned int & /*num_angle_quantization*/,
366  SearchInfo & search_info)
367 {
368  if (motion_model != MotionModel::STATE_LATTICE) {
369  throw std::runtime_error(
370  "Invalid motion model for Lattice node. Please select"
371  " STATE_LATTICE and provide a valid lattice file.");
372  }
373 
374  motion_table.initMotionModel(size_x, search_info);
375 }
376 
378  const Coordinates & node_coords,
379  const Coordinates & goal_coords,
380  const float & obstacle_heuristic)
381 {
382  // rotate and translate node_coords such that goal_coords relative is (0,0,0)
383  // Due to the rounding involved in exact cell increments for caching,
384  // this is not an exact replica of a live heuristic, but has bounded error.
385  // (Usually less than 1 cell length)
386 
387  // This angle is negative since we are de-rotating the current node
388  // by the goal angle; cos(-th) = cos(th) & sin(-th) = -sin(th)
389  const TrigValues & trig_vals = motion_table.trig_values[goal_coords.theta];
390  const float cos_th = trig_vals.first;
391  const float sin_th = -trig_vals.second;
392  const float dx = node_coords.x - goal_coords.x;
393  const float dy = node_coords.y - goal_coords.y;
394 
395  double dtheta_bin = node_coords.theta - goal_coords.theta;
396  if (dtheta_bin < 0) {
397  dtheta_bin += motion_table.num_angle_quantization;
398  }
399  if (dtheta_bin > motion_table.num_angle_quantization) {
400  dtheta_bin -= motion_table.num_angle_quantization;
401  }
402 
403  Coordinates node_coords_relative(
404  round(dx * cos_th - dy * sin_th),
405  round(dx * sin_th + dy * cos_th),
406  round(dtheta_bin));
407 
408  // Check if the relative node coordinate is within the localized window around the goal
409  // to apply the distance heuristic. Since the lookup table is contains only the positive
410  // X axis, we mirror the Y and theta values across the X axis to find the heuristic values.
411  float motion_heuristic = 0.0;
412  const int floored_size = floor(size_lookup / 2.0);
413  const int ceiling_size = ceil(size_lookup / 2.0);
414  const float mirrored_relative_y = abs(node_coords_relative.y);
415  if (abs(node_coords_relative.x) < floored_size && mirrored_relative_y < floored_size) {
416  // Need to mirror angle if Y coordinate was mirrored
417  int theta_pos;
418  if (node_coords_relative.y < 0.0) {
419  theta_pos = motion_table.num_angle_quantization - node_coords_relative.theta;
420  } else {
421  theta_pos = node_coords_relative.theta;
422  }
423  const int x_pos = node_coords_relative.x + floored_size;
424  const int y_pos = static_cast<int>(mirrored_relative_y);
425  const int index =
426  x_pos * ceiling_size * motion_table.num_angle_quantization +
427  y_pos * motion_table.num_angle_quantization +
428  theta_pos;
429  motion_heuristic = dist_heuristic_lookup_table[index];
430  } else if (obstacle_heuristic == 0.0) {
431  static ompl::base::ScopedState<> from(motion_table.state_space), to(motion_table.state_space);
432  to[0] = goal_coords.x;
433  to[1] = goal_coords.y;
434  to[2] = motion_table.getAngleFromBin(goal_coords.theta);
435  from[0] = node_coords.x;
436  from[1] = node_coords.y;
437  from[2] = motion_table.getAngleFromBin(node_coords.theta);
438  motion_heuristic = motion_table.state_space->distance(from(), to());
439  }
440 
441  return motion_heuristic;
442 }
443 
445  const float & lookup_table_dim,
446  const MotionModel & /*motion_model*/,
447  const unsigned int & dim_3_size,
448  const SearchInfo & search_info)
449 {
450  motion_table.lattice_metadata =
451  LatticeMotionTable::getLatticeMetadata(search_info.lattice_filepath);
452 
453  // Select state space based on motion model from lattice file
454  if (motion_table.lattice_metadata.motion_model == "omni") {
455  // Holonomic robots: Euclidean distance heuristic
456  motion_table.state_space = std::make_shared<ompl::base::SE2StateSpace>();
457  motion_table.motion_model = MotionModel::OMNI;
458  } else if (!search_info.allow_reverse_expansion) {
459  motion_table.state_space = std::make_shared<ompl::base::DubinsStateSpace>(
460  search_info.minimum_turning_radius);
461  motion_table.motion_model = MotionModel::DUBIN;
462  } else {
463  motion_table.state_space = std::make_shared<ompl::base::ReedsSheppStateSpace>(
464  search_info.minimum_turning_radius);
465  motion_table.motion_model = MotionModel::REEDS_SHEPP;
466  }
467 
468  ompl::base::ScopedState<> from(motion_table.state_space), to(motion_table.state_space);
469  to[0] = 0.0;
470  to[1] = 0.0;
471  to[2] = 0.0;
472  size_lookup = lookup_table_dim;
473  float motion_heuristic = 0.0;
474  unsigned int index = 0;
475  int dim_3_size_int = static_cast<int>(dim_3_size);
476 
477  // Create a lookup table of Dubin/Reeds-Shepp distances in a window around the goal
478  // to help drive the search towards admissible approaches. Deu to symmetries in the
479  // Heuristic space, we need to only store 2 of the 4 quadrants and simply mirror
480  // around the X axis any relative node lookup. This reduces memory overhead and increases
481  // the size of a window a platform can store in memory.
482  dist_heuristic_lookup_table.resize(size_lookup * ceil(size_lookup / 2.0) * dim_3_size_int);
483  for (float x = ceil(-size_lookup / 2.0); x <= floor(size_lookup / 2.0); x += 1.0) {
484  for (float y = 0.0; y <= floor(size_lookup / 2.0); y += 1.0) {
485  for (int heading = 0; heading != dim_3_size_int; heading++) {
486  from[0] = x;
487  from[1] = y;
488  from[2] = motion_table.getAngleFromBin(heading);
489  motion_heuristic = motion_table.state_space->distance(from(), to());
490  dist_heuristic_lookup_table[index] = motion_heuristic;
491  index++;
492  }
493  }
494  }
495 }
496 
498  std::function<bool(const uint64_t &,
499  nav2_smac_planner::NodeLattice * &)> & NeighborGetter,
500  GridCollisionChecker * collision_checker,
501  const bool & traverse_unknown,
502  NodeVector & neighbors)
503 {
504  uint64_t index = 0;
505  bool backwards = false;
506  NodePtr neighbor = nullptr;
507  Coordinates initial_node_coords, motion_projection;
508  unsigned int direction_change_index = 0;
509  MotionPrimitivePtrs motion_primitives = motion_table.getMotionPrimitives(
510  this,
511  direction_change_index);
512  const float & grid_resolution = motion_table.lattice_metadata.grid_resolution;
513 
514  for (unsigned int i = 0; i != motion_primitives.size(); i++) {
515  const MotionPose & end_pose = motion_primitives[i]->poses.back();
516  motion_projection.x = this->pose.x + (end_pose._x / grid_resolution);
517  motion_projection.y = this->pose.y + (end_pose._y / grid_resolution);
518  motion_projection.theta = motion_primitives[i]->end_angle /*this is the ending angular bin*/;
519 
520  // if i >= idx, then we're in a reversing primitive. In that situation,
521  // the orientation of the robot is mirrored from what it would otherwise
522  // appear to be from the motion primitives file. We want to take this into
523  // account in case the robot base footprint is asymmetric.
524  backwards = false;
525  if (i >= direction_change_index) {
526  backwards = true;
527  float opposite_heading_theta =
528  motion_projection.theta - (motion_table.num_angle_quantization / 2);
529  if (opposite_heading_theta < 0) {
530  opposite_heading_theta += motion_table.num_angle_quantization;
531  }
532  if (opposite_heading_theta > motion_table.num_angle_quantization) {
533  opposite_heading_theta -= motion_table.num_angle_quantization;
534  }
535  motion_projection.theta = opposite_heading_theta;
536  }
537 
538  index = NodeLattice::getIndex(
539  static_cast<unsigned int>(motion_projection.x),
540  static_cast<unsigned int>(motion_projection.y),
541  static_cast<unsigned int>(motion_projection.theta));
542 
543  if (NeighborGetter(index, neighbor) && !neighbor->wasVisited()) {
544  // Cache the initial pose in case it was visited but valid
545  // don't want to disrupt continuous coordinate expansion
546  initial_node_coords = neighbor->pose;
547 
548  neighbor->setPose(
549  Coordinates(
550  motion_projection.x,
551  motion_projection.y,
552  motion_projection.theta));
553 
554  // Using a special isNodeValid API here, giving the motion primitive to use to
555  // validity check the transition of the current node to the new node over
556  if (neighbor->isNodeValid(
557  traverse_unknown, collision_checker, motion_primitives[i], backwards))
558  {
559  neighbor->setMotionPrimitive(motion_primitives[i]);
560  // Marking if this search was obtained in the reverse direction
561  neighbor->backwards(backwards);
562  neighbors.push_back(neighbor);
563  } else {
564  neighbor->setPose(initial_node_coords);
565  }
566  }
567  }
568 }
569 
570 bool NodeLattice::backtracePath(CoordinateVector & path)
571 {
572  if (!this->parent) {
573  return false;
574  }
575 
576  NodePtr current_node = this;
577 
578  while (current_node->parent) {
579  addNodeToPath(current_node, path);
580  current_node = current_node->parent;
581  }
582 
583  // add start to path
584  addNodeToPath(current_node, path);
585 
586  return true;
587 }
588 
590  NodeLattice::NodePtr current_node,
591  NodeLattice::CoordinateVector & path)
592 {
593  Coordinates initial_pose, prim_pose;
594  MotionPrimitive * prim = nullptr;
595  const float & grid_resolution = NodeLattice::motion_table.lattice_metadata.grid_resolution;
596  prim = current_node->getMotionPrimitive();
597  // if motion primitive is valid, then was searched (rather than analytically expanded),
598  // include dense path of subpoints making up the primitive at grid resolution
599  if (prim) {
600  initial_pose.x = current_node->pose.x - (prim->poses.back()._x / grid_resolution);
601  initial_pose.y = current_node->pose.y - (prim->poses.back()._y / grid_resolution);
602  initial_pose.theta = NodeLattice::motion_table.getAngleFromBin(prim->start_angle);
603 
604  for (auto it = prim->poses.crbegin(); it != prim->poses.crend(); ++it) {
605  // Convert primitive pose into grid space if it should be checked
606  prim_pose.x = initial_pose.x + (it->_x / grid_resolution);
607  prim_pose.y = initial_pose.y + (it->_y / grid_resolution);
608  // If reversing, invert the angle because the robot is backing into the primitive
609  // not driving forward with it
610  if (current_node->isBackward()) {
611  prim_pose.theta = std::fmod(it->_theta + M_PI, 2.0 * M_PI);
612  } else {
613  prim_pose.theta = it->_theta;
614  }
615  path.push_back(prim_pose);
616  }
617  } else {
618  // For analytic expansion nodes where there is no valid motion primitive
619  path.push_back(current_node->pose);
620  path.back().theta = NodeLattice::motion_table.getAngleFromBin(path.back().theta);
621  }
622 }
623 
624 } // 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.
std::vector< float > & getPrecomputedAngles()
Get the angles of the precomputed footprint orientations.
float getCost()
Get cost at footprint pose in costmap.
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.
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.
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.
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 float getDistanceHeuristic(const Coordinates &node_coords, const Coordinates &goal_coords, const float &obstacle_heuristic)
Compute the Distance heuristic.
void setPose(const Coordinates &pose_in)
setting continuous coordinate search poses (in partial-cells)
A struct of all lattice metadata.
Definition: types.hpp:165
static LatticeMetadata getLatticeMetadata(const std::string &lattice_filepath)
Get file metadata needed.
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.
A struct for poses in motion primitives.
Definition: types.hpp:129
A struct of all motion primitive data.
Definition: types.hpp:179
NodeHybrid implementation of coordinate structure.
Search properties and penalties.
Definition: types.hpp:36