Nav2 Navigation Stack - jazzy  jazzy
ROS 2 Navigation Stack
obstacle_footprint.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/obstacle_footprint.hpp"
36 #include <algorithm>
37 #include <vector>
38 #include "dwb_critics/line_iterator.hpp"
39 #include "dwb_core/exceptions.hpp"
40 #include "pluginlib/class_list_macros.hpp"
41 #include "nav2_costmap_2d/cost_values.hpp"
42 
44 
45 namespace dwb_critics
46 {
47 Footprint getOrientedFootprint(
48  const geometry_msgs::msg::Pose2D & pose,
49  const Footprint & footprint_spec)
50 {
51  std::vector<geometry_msgs::msg::Point> oriented_footprint;
52  oriented_footprint.resize(footprint_spec.size());
53  double cos_th = cos(pose.theta);
54  double sin_th = sin(pose.theta);
55  for (unsigned int i = 0; i < footprint_spec.size(); ++i) {
56  geometry_msgs::msg::Point & new_pt = oriented_footprint[i];
57  new_pt.x = pose.x + footprint_spec[i].x * cos_th - footprint_spec[i].y * sin_th;
58  new_pt.y = pose.y + footprint_spec[i].x * sin_th + footprint_spec[i].y * cos_th;
59  }
60  return oriented_footprint;
61 }
62 
64  const geometry_msgs::msg::Pose2D &, const nav_2d_msgs::msg::Twist2D &,
65  const geometry_msgs::msg::Pose2D &, const nav_2d_msgs::msg::Path2D &)
66 {
67  footprint_spec_ = costmap_ros_->getRobotFootprint();
68  if (footprint_spec_.size() == 0) {
69  RCLCPP_ERROR(
70  rclcpp::get_logger("ObstacleFootprintCritic"),
71  "Footprint spec is empty, maybe missing call to setFootprint?");
72  return false;
73  }
74  return true;
75 }
76 
77 double ObstacleFootprintCritic::scorePose(const geometry_msgs::msg::Pose2D & pose)
78 {
79  unsigned int cell_x, cell_y;
80  if (!costmap_->worldToMap(pose.x, pose.y, cell_x, cell_y)) {
81  throw dwb_core::
82  IllegalTrajectoryException(name_, "Trajectory Goes Off Grid.");
83  }
84  return scorePose(pose, getOrientedFootprint(pose, footprint_spec_));
85 }
86 
88  const geometry_msgs::msg::Pose2D &,
89  const Footprint & footprint)
90 {
91  // now we really have to lay down the footprint in the costmap grid
92  unsigned int x0, x1, y0, y1;
93  double line_cost = 0.0;
94  double footprint_cost = 0.0;
95 
96  // we need to rasterize each line in the footprint
97  for (unsigned int i = 0; i < footprint.size() - 1; ++i) {
98  // get the cell coord of the first point
99  if (!costmap_->worldToMap(footprint[i].x, footprint[i].y, x0, y0)) {
100  throw dwb_core::
101  IllegalTrajectoryException(name_, "Footprint Goes Off Grid.");
102  }
103 
104  // get the cell coord of the second point
105  if (!costmap_->worldToMap(footprint[i + 1].x, footprint[i + 1].y, x1, y1)) {
106  throw dwb_core::
107  IllegalTrajectoryException(name_, "Footprint Goes Off Grid.");
108  }
109 
110  line_cost = lineCost(x0, x1, y0, y1);
111  footprint_cost = std::max(line_cost, footprint_cost);
112  }
113 
114  // we also need to connect the first point in the footprint to the last point
115  // get the cell coord of the last point
116  if (!costmap_->worldToMap(footprint.back().x, footprint.back().y, x0, y0)) {
117  throw dwb_core::
118  IllegalTrajectoryException(name_, "Footprint Goes Off Grid.");
119  }
120 
121  // get the cell coord of the first point
122  if (!costmap_->worldToMap(footprint.front().x, footprint.front().y, x1, y1)) {
123  throw dwb_core::
124  IllegalTrajectoryException(name_, "Footprint Goes Off Grid.");
125  }
126 
127  line_cost = lineCost(x0, x1, y0, y1);
128  footprint_cost = std::max(line_cost, footprint_cost);
129 
130  // if all line costs are legal... then we can return that the footprint is legal
131  return footprint_cost;
132 }
133 
134 double ObstacleFootprintCritic::lineCost(int x0, int x1, int y0, int y1)
135 {
136  double line_cost = 0.0;
137  double point_cost = -1.0;
138 
139  for (LineIterator line(x0, y0, x1, y1); line.isValid(); line.advance()) {
140  point_cost = pointCost(line.getX(), line.getY()); // Score the current point
141 
142  if (line_cost < point_cost) {
143  line_cost = point_cost;
144  }
145  }
146 
147  return line_cost;
148 }
149 
151 {
152  unsigned char cost = costmap_->getCost(x, y);
153  // if the cell is in an obstacle the path is invalid or unknown
154  if (cost == nav2_costmap_2d::LETHAL_OBSTACLE) {
155  throw dwb_core::
156  IllegalTrajectoryException(name_, "Trajectory Hits Obstacle.");
157  } else if (cost == nav2_costmap_2d::NO_INFORMATION) {
158  throw dwb_core::
159  IllegalTrajectoryException(name_, "Trajectory Hits Unknown Region.");
160  }
161 
162  return cost;
163 }
164 
165 } // namespace dwb_critics
Evaluates a Trajectory2D to produce a score.
Uses costmap 2d to assign negative costs if robot footprint is in obstacle on any point of the trajec...
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...
double pointCost(int x, int y)
Checks the cost of a point in the costmap.
double lineCost(int x0, int x1, int y0, int y1)
Rasterizes a line in the costmap grid and checks for collisions.
double scorePose(const geometry_msgs::msg::Pose2D &pose) override
Return the obstacle score for a particular pose.
unsigned char getCost(unsigned int mx, unsigned int my) const
Get the cost of a cell in the costmap.
Definition: costmap_2d.cpp:264
bool worldToMap(double wx, double wy, unsigned int &mx, unsigned int &my) const
Convert from world coordinates to map coordinates.
Definition: costmap_2d.cpp:285