Nav2 Navigation Stack - rolling  main
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::Pose & 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 theta = tf2::getYaw(pose.orientation);
54  double cos_th = cos(theta);
55  double sin_th = sin(theta);
56  for (unsigned int i = 0; i < footprint_spec.size(); ++i) {
57  geometry_msgs::msg::Point & new_pt = oriented_footprint[i];
58  new_pt.x = pose.position.x + footprint_spec[i].x * cos_th - footprint_spec[i].y * sin_th;
59  new_pt.y = pose.position.y + footprint_spec[i].x * sin_th + footprint_spec[i].y * cos_th;
60  }
61  return oriented_footprint;
62 }
63 
65  const geometry_msgs::msg::Pose &, const nav_2d_msgs::msg::Twist2D &,
66  const geometry_msgs::msg::Pose &, const nav_msgs::msg::Path &)
67 {
68  footprint_spec_ = costmap_ros_->getRobotFootprint();
69  if (footprint_spec_.size() == 0) {
70  RCLCPP_ERROR(
71  rclcpp::get_logger("ObstacleFootprintCritic"),
72  "Footprint spec is empty, maybe missing call to setFootprint?");
73  return false;
74  }
75  return true;
76 }
77 
78 double ObstacleFootprintCritic::scorePose(const geometry_msgs::msg::Pose & pose)
79 {
80  unsigned int cell_x, cell_y;
81  if (!costmap_->worldToMap(pose.position.x, pose.position.y, cell_x, cell_y)) {
82  throw dwb_core::
83  IllegalTrajectoryException(name_, "Trajectory Goes Off Grid.");
84  }
85  return scorePose(pose, getOrientedFootprint(pose, footprint_spec_));
86 }
87 
89  const geometry_msgs::msg::Pose &,
90  const Footprint & footprint)
91 {
92  // now we really have to lay down the footprint in the costmap grid
93  unsigned int x0, x1, y0, y1;
94  double line_cost = 0.0;
95  double footprint_cost = 0.0;
96 
97  // we need to rasterize each line in the footprint
98  for (unsigned int i = 0; i < footprint.size() - 1; ++i) {
99  // get the cell coord of the first point
100  if (!costmap_->worldToMap(footprint[i].x, footprint[i].y, x0, y0)) {
101  throw dwb_core::
102  IllegalTrajectoryException(name_, "Footprint Goes Off Grid.");
103  }
104 
105  // get the cell coord of the second point
106  if (!costmap_->worldToMap(footprint[i + 1].x, footprint[i + 1].y, x1, y1)) {
107  throw dwb_core::
108  IllegalTrajectoryException(name_, "Footprint Goes Off Grid.");
109  }
110 
111  line_cost = lineCost(x0, x1, y0, y1);
112  footprint_cost = std::max(line_cost, footprint_cost);
113  }
114 
115  // we also need to connect the first point in the footprint to the last point
116  // get the cell coord of the last point
117  if (!costmap_->worldToMap(footprint.back().x, footprint.back().y, x0, y0)) {
118  throw dwb_core::
119  IllegalTrajectoryException(name_, "Footprint Goes Off Grid.");
120  }
121 
122  // get the cell coord of the first point
123  if (!costmap_->worldToMap(footprint.front().x, footprint.front().y, x1, y1)) {
124  throw dwb_core::
125  IllegalTrajectoryException(name_, "Footprint Goes Off Grid.");
126  }
127 
128  line_cost = lineCost(x0, x1, y0, y1);
129  footprint_cost = std::max(line_cost, footprint_cost);
130 
131  // if all line costs are legal... then we can return that the footprint is legal
132  return footprint_cost;
133 }
134 
135 double ObstacleFootprintCritic::lineCost(int x0, int x1, int y0, int y1)
136 {
137  double line_cost = 0.0;
138  double point_cost = -1.0;
139 
140  for (LineIterator line(x0, y0, x1, y1); line.isValid(); line.advance()) {
141  point_cost = pointCost(line.getX(), line.getY()); // Score the current point
142 
143  if (line_cost < point_cost) {
144  line_cost = point_cost;
145  }
146  }
147 
148  return line_cost;
149 }
150 
152 {
153  unsigned char cost = costmap_->getCost(x, y);
154  // if the cell is in an obstacle the path is invalid or unknown
155  if (cost == nav2_costmap_2d::LETHAL_OBSTACLE) {
156  throw dwb_core::
157  IllegalTrajectoryException(name_, "Trajectory Hits Obstacle.");
158  } else if (cost == nav2_costmap_2d::NO_INFORMATION) {
159  throw dwb_core::
160  IllegalTrajectoryException(name_, "Trajectory Hits Unknown Region.");
161  }
162 
163  return cost;
164 }
165 
166 } // 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...
double scorePose(const geometry_msgs::msg::Pose &pose) override
Return the obstacle score for a particular pose.
double pointCost(int x, int y)
Checks the cost of a point in the costmap.
bool prepare(const geometry_msgs::msg::Pose &pose, const nav_2d_msgs::msg::Twist2D &vel, const geometry_msgs::msg::Pose &goal, const nav_msgs::msg::Path &global_plan) override
Prior to evaluating any trajectories, look at contextual information constant across all trajectories...
double lineCost(int x0, int x1, int y0, int y1)
Rasterizes a line in the costmap grid and checks for collisions.
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:291