24 #include "tf2/utils.hpp"
26 #include "nav2_costmap_2d/costmap_topic_collision_checker.hpp"
28 #include "nav2_costmap_2d/cost_values.hpp"
29 #include "nav2_costmap_2d/exceptions.hpp"
30 #include "nav2_costmap_2d/footprint.hpp"
31 #include "nav2_util/line_iterator.hpp"
33 using namespace std::chrono_literals;
38 CostmapTopicCollisionChecker::CostmapTopicCollisionChecker(
43 costmap_sub_(costmap_sub),
44 footprint_sub_(&footprint_sub),
45 collision_checker_(nullptr)
50 std::string footprint_string,
53 costmap_sub_(costmap_sub),
54 collision_checker_(nullptr)
62 const geometry_msgs::msg::Pose & pose,
63 bool fetch_costmap_and_footprint)
66 if (
scorePose(pose, fetch_costmap_and_footprint) >= LETHAL_OBSTACLE) {
71 RCLCPP_ERROR(rclcpp::get_logger(name_),
"%s", e.what());
74 RCLCPP_ERROR(rclcpp::get_logger(name_),
"%s", e.what());
77 RCLCPP_ERROR(rclcpp::get_logger(name_),
"Failed to check pose score!");
83 const geometry_msgs::msg::Pose & pose,
84 bool fetch_costmap_and_footprint)
86 if (fetch_costmap_and_footprint) {
88 collision_checker_.setCostmap(costmap_sub_.
getCostmap());
89 }
catch (
const std::runtime_error & e) {
93 auto costmap = collision_checker_.getCostmap();
94 std::lock_guard<Costmap2D::mutex_t> costmap_lock(*costmap->getMutex());
96 unsigned int cell_x, cell_y;
97 if (!collision_checker_.worldToMap(pose.position.x, pose.position.y, cell_x, cell_y)) {
98 RCLCPP_DEBUG(rclcpp::get_logger(name_),
"Map Cell: [%d, %d]", cell_x, cell_y);
102 return collision_checker_.footprintCost(
getFootprint(pose, fetch_costmap_and_footprint));
106 const geometry_msgs::msg::Pose & pose,
107 bool fetch_latest_footprint)
109 if (fetch_latest_footprint) {
110 std_msgs::msg::Header header;
120 double x = pose.position.x;
121 double y = pose.position.y;
122 double theta = tf2::getYaw(pose.orientation);
Exceptions thrown if collision checker determines a pose is in collision with the environment costmap...
Subscribes to the costmap via a ros topic.
std::shared_ptr< Costmap2D > getCostmap()
Get current costmap.
double scorePose(const geometry_msgs::msg::Pose &pose, bool fetch_costmap_and_footprint=true)
Returns the obstacle footprint score for a particular pose.
Footprint getFootprint(const geometry_msgs::msg::Pose &pose, bool fetch_latest_footprint=true)
Get a footprint at a set pose.
bool isCollisionFree(const geometry_msgs::msg::Pose &pose, bool fetch_costmap_and_footprint=true)
Returns if a pose is collision free.
CostmapTopicCollisionChecker(CostmapSubscriber &costmap_sub, FootprintSubscriber &footprint_sub, std::string name="collision_checker")
A constructor.
Thrown when CollisionChecker encounters a fatal error.
void transformFootprint(double x, double y, double theta, const std::vector< geometry_msgs::msg::Point > &footprint_spec, std::vector< geometry_msgs::msg::Point > &oriented_footprint)
Given a pose and base footprint, build the oriented footprint of the robot (list of Points)
bool makeFootprintFromString(const std::string &footprint_string, std::vector< geometry_msgs::msg::Point > &footprint)
Make the footprint from the given string.