16 #include "nav2_msgs/msg/costmap.hpp"
19 #include <tf2/time.hpp>
20 #include <tf2_ros/buffer.hpp>
21 #include <tf2_ros/transform_listener.hpp>
22 #include <nav2_ros_common/node_utils.hpp>
23 #include <nav2_ros_common/qos_profiles.hpp>
24 #include <nav2_costmap_2d/cost_values.hpp>
25 #include "nav2_ros_common/tf2_factories.hpp"
27 namespace nav2_collision_monitor
31 const nav2::LifecycleNode::WeakPtr & node,
32 const std::string & source_name,
33 const nav2::TransformBuffer::SharedPtr tf_buffer,
34 const std::string & base_frame_id,
35 const std::string & global_frame_id,
36 const tf2::Duration & transform_tolerance,
37 const rclcpp::Duration & source_timeout,
38 const bool base_shift_correction)
40 node, source_name, tf_buffer, base_frame_id, global_frame_id,
41 transform_tolerance, source_timeout, base_shift_correction),
58 auto node =
node_.lock();
60 throw std::runtime_error{
"Failed to lock node"};
62 std::string source_topic;
64 data_sub_ = node->create_subscription<nav2_msgs::msg::Costmap>(
66 std::bind(&CostmapSource::dataCallback,
this, std::placeholders::_1),
73 const rclcpp::Time & curr_time,
74 std::vector<Point> & data)
76 auto node =
node_.lock();
77 if (data_ ==
nullptr) {
84 tf2::Transform tf_transform; tf_transform.setIdentity();
85 const std::string src = data_->header.frame_id;
88 if (!
getTransform(curr_time, data_->header, tf_transform)) {
89 RCLCPP_WARN(
logger_,
"[%s] TF %s->%s unavailable at t=%.3f",
96 const auto & cm = *data_;
97 const auto & meta = cm.metadata;
99 for (
unsigned int y = 0; y < meta.size_y; ++y) {
100 for (
unsigned int x = 0; x < meta.size_x; ++x) {
101 const int idx = y * meta.size_x + x;
102 const uint8_t cell_cost = cm.data[idx];
103 const bool is_obstacle =
104 (cell_cost >= cost_threshold_ && cell_cost < nav2_costmap_2d::NO_INFORMATION) ||
105 (treat_unknown_as_obstacle_ && cell_cost == nav2_costmap_2d::NO_INFORMATION);
107 const double wx = meta.origin.position.x + (x + 0.5) * meta.resolution;
108 const double wy = meta.origin.position.y + (y + 0.5) * meta.resolution;
109 tf2::Vector3 p_v3_s(wx, wy, 0.0);
110 tf2::Vector3 p_v3_b = tf_transform * p_v3_s;
111 data.push_back({p_v3_b.x(), p_v3_b.y(), p_v3_b.z(),
source_name_});
112 RCLCPP_DEBUG_THROTTLE(
113 logger_, *node->get_clock(), 2000 ,
114 "[%s] Found obstacles in costmap",
source_name_.c_str());
124 auto node =
node_.lock();
126 throw std::runtime_error{
"Failed to lock node"};
131 const auto thresh_name =
source_name_ +
".cost_threshold";
133 int v = node->declare_or_get_parameter<
int>(thresh_name, 253);
134 v = std::max(0, std::min(255, v));
135 if (v != node->get_parameter(thresh_name).as_int()) {
136 RCLCPP_WARN(node->get_logger(),
"Clamping %s to %d", thresh_name.c_str(), v);
141 const auto unk_name =
source_name_ +
".treat_unknown_as_obstacle";
142 treat_unknown_as_obstacle_ = node->declare_or_get_parameter<
bool>(unk_name,
true);
145 void CostmapSource::dataCallback(nav2_msgs::msg::Costmap::ConstSharedPtr msg)
A QoS profile for standard reliable topics with a history of 10 messages.
bool configure()
Declare and get parameters; create the subscription.
void getParameters(std::string &source_topic)
Read parameters specific to the costmap source.
~CostmapSource()
Destructor.
bool getSourceData(const rclcpp::Time &curr_time, std::vector< Point > &data) override
Produce current obstacle points from the latest costmap.
CostmapSource(const nav2::LifecycleNode::WeakPtr &node, const std::string &source_name, const nav2::TransformBuffer::SharedPtr tf_buffer, const std::string &base_frame_id, const std::string &global_frame_id, const tf2::Duration &transform_tolerance, const rclcpp::Duration &source_timeout, const bool base_shift_correction)
Construct a CostmapSource.
nav2::LifecycleNode::WeakPtr node_
Collision Monitor node.
rclcpp::Logger logger_
Collision monitor node logger stored for further usage.
std::string base_frame_id_
Robot base frame ID.
bool getTransform(const rclcpp::Time &curr_time, const std_msgs::msg::Header &data_header, tf2::Transform &tf_transform) const
Obtain the transform to get data from source frame and time where it was received to the base frame a...
void getCommonParameters(std::string &source_topic)
Supporting routine obtaining ROS-parameters common for all data sources.
bool configure()
Source configuration routine.
std::string source_name_
Name of data source.
bool sourceValid(const rclcpp::Time &source_time, const rclcpp::Time &curr_time) const
Checks whether the source data might be considered as valid.
Observation source that converts a Nav2 costmap topic into 2D points for Collision Monitor.