Nav2 Navigation Stack - humble  humble
ROS 2 Navigation Stack
path_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/path_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  bool started_path = false;
50 
51  nav_2d_msgs::msg::Path2D adjusted_global_plan =
52  nav_2d_utils::adjustPlanResolution(global_plan, costmap_->getResolution());
53 
54  if (adjusted_global_plan.poses.size() != global_plan.poses.size()) {
55  RCLCPP_DEBUG(
56  rclcpp::get_logger(
57  "PathDistCritic"), "Adjusted global plan resolution, added %zu points",
58  adjusted_global_plan.poses.size() - global_plan.poses.size());
59  }
60 
61  unsigned int i;
62  // put global path points into local map until we reach the border of the local map
63  for (i = 0; i < adjusted_global_plan.poses.size(); ++i) {
64  double g_x = adjusted_global_plan.poses[i].x;
65  double g_y = adjusted_global_plan.poses[i].y;
66  unsigned int map_x, map_y;
67  if (costmap_->worldToMap(
68  g_x, g_y, map_x,
69  map_y) && costmap_->getCost(map_x, map_y) != nav2_costmap_2d::NO_INFORMATION)
70  {
71  int index = costmap_->getIndex(map_x, map_y);
72  cell_values_[index] = 0.0;
73  queue_->enqueueCell(map_x, map_y);
74  started_path = true;
75  } else if (started_path) {
76  break;
77  }
78  }
79  if (!started_path) {
80  RCLCPP_ERROR(
81  rclcpp::get_logger("PathDistCritic"),
82  "None of the %d first of %zu (%zu) points of the global plan were in "
83  "the local costmap and free",
84  i, adjusted_global_plan.poses.size(), global_plan.poses.size());
85  return false;
86  }
87 
89 
90  return true;
91 }
92 
93 } // namespace dwb_critics
94 
Evaluates a Trajectory2D to produce a score.
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
Scores trajectories based on how far from the global path they end up.
Definition: path_dist.hpp:46
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: path_dist.cpp:43
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