1414// limitations under the License.
1515#include " nav2_msgs/msg/costmap.hpp"
1616#include " nav2_collision_monitor/costmap.hpp"
17- #include < functional>
1817#include < cmath>
1918#include < tf2/time.hpp>
2019#include < tf2_ros/buffer.hpp>
2120#include < tf2_ros/transform_listener.hpp>
22- #include < nav2_ros_common/lifecycle_node.hpp>
2321#include < nav2_ros_common/node_utils.hpp>
22+ #include < nav2_ros_common/qos_profiles.hpp>
23+ #include < nav2_costmap_2d/cost_values.hpp>
2424
2525namespace nav2_collision_monitor
2626{
27-
28-
2927CostmapSource::CostmapSource (
3028 const nav2::LifecycleNode::WeakPtr & node,
3129 const std::string & source_name,
@@ -43,7 +41,6 @@ CostmapSource::CostmapSource(
4341 RCLCPP_INFO (logger_, " [%s]: Creating CostmapSource" , source_name_.c_str ());
4442}
4543
46-
4744CostmapSource::~CostmapSource ()
4845{
4946 RCLCPP_INFO (logger_, " [%s]: Destroying CostmapSource" , source_name_.c_str ());
@@ -59,19 +56,17 @@ void CostmapSource::configure()
5956 }
6057 std::string source_topic;
6158 getParameters (source_topic);
62- rclcpp::QoS qos = rclcpp::SystemDefaultsQoS ();
63-
64-
6559 data_sub_ = node->create_subscription <nav2_msgs::msg::Costmap>(
6660 source_topic,
6761 std::bind (&CostmapSource::dataCallback, this , std::placeholders::_1),
68- qos);
62+ nav2:: qos::StandardTopicQoS () );
6963}
7064
7165bool CostmapSource::getData (
7266 const rclcpp::Time & curr_time,
7367 std::vector<Point> & data)
7468{
69+ auto node = node_.lock ();
7570 if (data_ == nullptr ) {
7671 return false ;
7772 }
@@ -89,7 +84,6 @@ bool CostmapSource::getData(
8984 }
9085 }
9186
92-
9387 // Extract lethal/inscribed cells and transform to base frame
9488 const auto & cm = *data_;
9589 const auto & meta = cm.metadata ;
@@ -98,17 +92,18 @@ bool CostmapSource::getData(
9892 for (unsigned int x = 0 ; x < meta.size_x ; ++x) {
9993 const int idx = y * meta.size_x + x;
10094
101- const uint8_t c = cm.data [idx];
102- const bool is_obstacle = (c >= cost_threshold_ && c < 255 ) ||
103- (treat_unknown_as_obstacle_ && c == 255 );
95+ const uint8_t cell_cost = cm.data [idx];
96+ const bool is_obstacle =
97+ (cell_cost >= cost_threshold_ && cell_cost < nav2_costmap_2d::NO_INFORMATION) ||
98+ (treat_unknown_as_obstacle_ && cell_cost == nav2_costmap_2d::NO_INFORMATION);
10499 if (is_obstacle) {
105100 const double wx = meta.origin .position .x + (x + 0.5 ) * meta.resolution ;
106101 const double wy = meta.origin .position .y + (y + 0.5 ) * meta.resolution ;
107102 tf2::Vector3 p_v3_s (wx, wy, 0.0 );
108103 tf2::Vector3 p_v3_b = tf_transform * p_v3_s;
109104 data.push_back ({p_v3_b.x (), p_v3_b.y ()});
110- RCLCPP_INFO (logger_, " [%s] Lethal cell at (%f, %f) " ,
111- source_name_.c_str (), wx, wy );
105+ RCLCPP_DEBUG_THROTTLE (logger_, *node-> get_clock (), 2000 /* ms */ ,
106+ " [%s] Found obstacles in costmap " , source_name_.c_str ());
112107 }
113108 }
114109 }
@@ -125,9 +120,8 @@ void CostmapSource::getParameters(std::string & source_topic)
125120
126121 // Cost threshold (0–255). 253 = inscribed 254 = lethal; 255 = NO_INFORMATION.
127122 const auto thresh_name = source_name_ + " .cost_threshold" ;
128- nav2::declare_parameter_if_not_declared (
129- node, thresh_name, rclcpp::ParameterValue (253 ));
130- int v = node->get_parameter (thresh_name).as_int ();
123+ // Minimal change (no range descriptor)
124+ int v = node->declare_or_get_parameter <int >(thresh_name, 253 ); // declare if missing, else get
131125 v = std::max (0 , std::min (255 , v)); // clamp
132126 if (v != node->get_parameter (thresh_name).as_int ()) {
133127 RCLCPP_WARN (node->get_logger (), " Clamping %s to %d" , thresh_name.c_str (), v);
@@ -136,9 +130,7 @@ void CostmapSource::getParameters(std::string & source_topic)
136130
137131 // Whether 255 (NO_INFORMATION) should be treated as an obstacle.
138132 const auto unk_name = source_name_ + " .treat_unknown_as_obstacle" ;
139- nav2::declare_parameter_if_not_declared (
140- node, unk_name, rclcpp::ParameterValue (true ));
141- treat_unknown_as_obstacle_ = node->get_parameter (unk_name).as_bool ();
133+ treat_unknown_as_obstacle_ = node->declare_or_get_parameter <bool >(unk_name, true );
142134}
143135
144136void CostmapSource::dataCallback (nav2_msgs::msg::Costmap::ConstSharedPtr msg)
0 commit comments