35 #include "dwb_critics/obstacle_footprint.hpp"
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"
47 Footprint getOrientedFootprint(
48 const geometry_msgs::msg::Pose2D & pose,
49 const Footprint & footprint_spec)
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;
60 return oriented_footprint;
64 const geometry_msgs::msg::Pose2D &,
const nav_2d_msgs::msg::Twist2D &,
65 const geometry_msgs::msg::Pose2D &,
const nav_2d_msgs::msg::Path2D &)
67 footprint_spec_ = costmap_ros_->getRobotFootprint();
68 if (footprint_spec_.size() == 0) {
70 rclcpp::get_logger(
"ObstacleFootprintCritic"),
71 "Footprint spec is empty, maybe missing call to setFootprint?");
79 unsigned int cell_x, cell_y;
80 if (!costmap_->
worldToMap(pose.x, pose.y, cell_x, cell_y)) {
82 IllegalTrajectoryException(name_,
"Trajectory Goes Off Grid.");
84 return scorePose(pose, getOrientedFootprint(pose, footprint_spec_));
88 const geometry_msgs::msg::Pose2D &,
89 const Footprint & footprint)
92 unsigned int x0, x1, y0, y1;
93 double line_cost = 0.0;
94 double footprint_cost = 0.0;
97 for (
unsigned int i = 0; i < footprint.size() - 1; ++i) {
99 if (!costmap_->
worldToMap(footprint[i].x, footprint[i].y, x0, y0)) {
101 IllegalTrajectoryException(name_,
"Footprint Goes Off Grid.");
105 if (!costmap_->
worldToMap(footprint[i + 1].x, footprint[i + 1].y, x1, y1)) {
107 IllegalTrajectoryException(name_,
"Footprint Goes Off Grid.");
110 line_cost =
lineCost(x0, x1, y0, y1);
111 footprint_cost = std::max(line_cost, footprint_cost);
116 if (!costmap_->
worldToMap(footprint.back().x, footprint.back().y, x0, y0)) {
118 IllegalTrajectoryException(name_,
"Footprint Goes Off Grid.");
122 if (!costmap_->
worldToMap(footprint.front().x, footprint.front().y, x1, y1)) {
124 IllegalTrajectoryException(name_,
"Footprint Goes Off Grid.");
127 line_cost =
lineCost(x0, x1, y0, y1);
128 footprint_cost = std::max(line_cost, footprint_cost);
131 return footprint_cost;
136 double line_cost = 0.0;
137 double point_cost = -1.0;
139 for (
LineIterator line(x0, y0, x1, y1); line.isValid(); line.advance()) {
140 point_cost =
pointCost(line.getX(), line.getY());
142 if (line_cost < point_cost) {
143 line_cost = point_cost;
152 unsigned char cost = costmap_->
getCost(x, y);
154 if (cost == nav2_costmap_2d::LETHAL_OBSTACLE) {
156 IllegalTrajectoryException(name_,
"Trajectory Hits Obstacle.");
157 }
else if (cost == nav2_costmap_2d::NO_INFORMATION) {
159 IllegalTrajectoryException(name_,
"Trajectory Hits Unknown Region.");
Evaluates a Trajectory2D to produce a score.
unsigned char getCost(unsigned int mx, unsigned int my) const
Get the cost of a cell in the costmap.
bool worldToMap(double wx, double wy, unsigned int &mx, unsigned int &my) const
Convert from world coordinates to map coordinates.