Nav2 Navigation Stack - humble  humble
ROS 2 Navigation Stack
goal_dist.cpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2017, Locus Robotics
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include "dwb_critics/goal_dist.hpp"
36 #include <vector>
37 #include "pluginlib/class_list_macros.hpp"
38 #include "nav_2d_utils/path_ops.hpp"
39 #include "nav2_costmap_2d/cost_values.hpp"
40 
41 namespace dwb_critics
42 {
44  const geometry_msgs::msg::Pose2D &, const nav_2d_msgs::msg::Twist2D &,
45  const geometry_msgs::msg::Pose2D &,
46  const nav_2d_msgs::msg::Path2D & global_plan)
47 {
48  reset();
49 
50  unsigned int local_goal_x, local_goal_y;
51  if (!getLastPoseOnCostmap(global_plan, local_goal_x, local_goal_y)) {
52  return false;
53  }
54 
55  // Enqueue just the last pose
56  int index = costmap_->getIndex(local_goal_x, local_goal_y);
57  cell_values_[index] = 0.0;
58  queue_->enqueueCell(local_goal_x, local_goal_y);
59 
61 
62  return true;
63 }
64 
65 bool GoalDistCritic::getLastPoseOnCostmap(
66  const nav_2d_msgs::msg::Path2D & global_plan,
67  unsigned int & x, unsigned int & y)
68 {
69  nav_2d_msgs::msg::Path2D adjusted_global_plan = nav_2d_utils::adjustPlanResolution(
70  global_plan,
71  costmap_->getResolution());
72  bool started_path = false;
73 
74  // skip global path points until we reach the border of the local map
75  for (unsigned int i = 0; i < adjusted_global_plan.poses.size(); ++i) {
76  double g_x = adjusted_global_plan.poses[i].x;
77  double g_y = adjusted_global_plan.poses[i].y;
78  unsigned int map_x, map_y;
79  if (costmap_->worldToMap(
80  g_x, g_y, map_x,
81  map_y) && costmap_->getCost(map_x, map_y) != nav2_costmap_2d::NO_INFORMATION)
82  {
83  // Still on the costmap. Continue.
84  x = map_x;
85  y = map_y;
86  started_path = true;
87  } else if (started_path) {
88  // Off the costmap after being on the costmap. Return the last saved indices.
89  return true;
90  }
91  // else, we have not yet found a point on the costmap, so we just continue
92  }
93 
94  if (started_path) {
95  return true;
96  } else {
97  RCLCPP_ERROR(
98  rclcpp::get_logger(
99  "GoalDistCritic"), "None of the points of the global plan were in the local costmap.");
100  return false;
101  }
102 }
103 
104 } // namespace dwb_critics
105 
Evaluates a Trajectory2D to produce a score.
Scores trajectories based on how far along the global path they end up.
Definition: goal_dist.hpp:51
bool prepare(const geometry_msgs::msg::Pose2D &pose, const nav_2d_msgs::msg::Twist2D &vel, const geometry_msgs::msg::Pose2D &goal, const nav_2d_msgs::msg::Path2D &global_plan) override
Prior to evaluating any trajectories, look at contextual information constant across all trajectories...
Definition: goal_dist.cpp:43
void reset() override
Clear the queuDWB_CRITICS_MAP_GRID_He and set cell_values_ to the appropriate number of unreachableCe...
Definition: map_grid.cpp:99
void propogateManhattanDistances()
Go through the queue and set the cells to the Manhattan distance from their parents.
Definition: map_grid.cpp:108
unsigned int getIndex(unsigned int mx, unsigned int my) const
Given two map coordinates... compute the associated index.
Definition: costmap_2d.hpp:211
unsigned char getCost(unsigned int mx, unsigned int my) const
Get the cost of a cell in the costmap.
Definition: costmap_2d.cpp:266
bool worldToMap(double wx, double wy, unsigned int &mx, unsigned int &my) const
Convert from world coordinates to map coordinates.
Definition: costmap_2d.cpp:287
double getResolution() const
Accessor for the resolution of the costmap.
Definition: costmap_2d.cpp:531