Skip to content

Commit 8de883f

Browse files
committed
publishing only with subscriptions, except for transient local publishers
Signed-off-by: Steve Macenski <[email protected]>
1 parent 362d4b1 commit 8de883f

File tree

20 files changed

+146
-83
lines changed

20 files changed

+146
-83
lines changed

nav2_amcl/src/amcl_node.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -873,7 +873,7 @@ void
873873
AmclNode::publishParticleCloud(const pf_sample_set_t * set)
874874
{
875875
// If initial pose is not known, AMCL does not know the current pose
876-
if (!initial_pose_is_known_) {return;}
876+
if (!initial_pose_is_known_ || particle_cloud_pub_->get_subscription_count() < 1) {return;}
877877
auto cloud_with_weights_msg = std::make_unique<nav2_msgs::msg::ParticleCloud>();
878878
cloud_with_weights_msg->header.stamp = this->now();
879879
cloud_with_weights_msg->header.frame_id = global_frame_id_;

nav2_behavior_tree/include/nav2_behavior_tree/ros_topic_logger.hpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,12 @@ class RosTopicLogger : public BT::StatusChangeLogger
8989
void flush() override
9090
{
9191
if (!event_log_.empty()) {
92-
auto log_msg = std::make_unique<nav2_msgs::msg::BehaviorTreeLog>();
93-
log_msg->timestamp = clock_->now();
94-
log_msg->event_log = event_log_;
95-
log_pub_->publish(std::move(log_msg));
92+
if (log_pub_->get_subscription_count() > 0) {
93+
auto log_msg = std::make_unique<nav2_msgs::msg::BehaviorTreeLog>();
94+
log_msg->timestamp = clock_->now();
95+
log_msg->event_log = event_log_;
96+
log_pub_->publish(std::move(log_msg));
97+
}
9698
event_log_.clear();
9799
}
98100
}

nav2_collision_monitor/src/collision_detector_node.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,9 @@ void CollisionDetector::process()
376376
collision_points) >= polygon->getMinPoints());
377377
}
378378

379-
state_pub_->publish(std::move(state_msg));
379+
if (state_pub_->get_subscription_count() > 0) {
380+
state_pub_->publish(std::move(state_msg));
381+
}
380382

381383
// Publish polygons for better visualization
382384
publishPolygons();

nav2_collision_monitor/src/polygon.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,9 @@ void Polygon::publish()
310310

311311
// Actualize the time to current and publish the polygon
312312
polygon_.header.stamp = node->now();
313-
auto msg = std::make_unique<geometry_msgs::msg::PolygonStamped>(polygon_);
314-
polygon_pub_->publish(std::move(msg));
313+
if (polygon_pub_->get_subscription_count() > 0) {
314+
polygon_pub_->publish(std::make_unique<geometry_msgs::msg::PolygonStamped>(polygon_));
315+
}
315316
}
316317

317318
bool Polygon::getCommonParameters(

nav2_costmap_2d/plugins/costmap_filters/binary_filter.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,12 @@ void BinaryFilter::changeState(const bool state)
255255
}
256256

257257
// Forming and publishing new BinaryState message
258-
std::unique_ptr<std_msgs::msg::Bool> msg =
259-
std::make_unique<std_msgs::msg::Bool>();
260-
msg->data = state;
261-
binary_state_pub_->publish(std::move(msg));
258+
if (binary_state_pub_->get_subscription_count() > 0) {
259+
std::unique_ptr<std_msgs::msg::Bool> msg =
260+
std::make_unique<std_msgs::msg::Bool>();
261+
msg->data = state;
262+
binary_state_pub_->publish(std::move(msg));
263+
}
262264
}
263265

264266
} // namespace nav2_costmap_2d

nav2_costmap_2d/plugins/costmap_filters/speed_filter.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -247,13 +247,15 @@ void SpeedFilter::process(
247247
}
248248

249249
// Forming and publishing new SpeedLimit message
250-
std::unique_ptr<nav2_msgs::msg::SpeedLimit> msg =
251-
std::make_unique<nav2_msgs::msg::SpeedLimit>();
252-
msg->header.frame_id = global_frame_;
253-
msg->header.stamp = clock_->now();
254-
msg->percentage = percentage_;
255-
msg->speed_limit = speed_limit_;
256-
speed_limit_pub_->publish(std::move(msg));
250+
if (speed_limit_pub_->get_subscription_count() > 0) {
251+
std::unique_ptr<nav2_msgs::msg::SpeedLimit> msg =
252+
std::make_unique<nav2_msgs::msg::SpeedLimit>();
253+
msg->header.frame_id = global_frame_;
254+
msg->header.stamp = clock_->now();
255+
msg->percentage = percentage_;
256+
msg->speed_limit = speed_limit_;
257+
speed_limit_pub_->publish(std::move(msg));
258+
}
257259

258260
speed_limit_prev_ = speed_limit_;
259261
}

nav2_costmap_2d/plugins/voxel_layer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ void VoxelLayer::updateBounds(
237237
}
238238
}
239239

240-
if (publish_voxel_) {
240+
if (publish_voxel_ && voxel_pub_->get_subscription_count() > 0) {
241241
auto grid_msg = std::make_unique<nav2_msgs::msg::VoxelGrid>();
242242
unsigned int size = voxel_grid_.sizeX() * voxel_grid_.sizeY();
243243
grid_msg->size_x = voxel_grid_.sizeX();
@@ -406,7 +406,7 @@ void VoxelLayer::raytraceFreespace(
406406
}
407407
}
408408

409-
if (publish_clearing_points) {
409+
if (publish_clearing_points && clearing_endpoints_pub_->get_subscription_count() > 0) {
410410
clearing_endpoints_->header.frame_id = global_frame_;
411411
clearing_endpoints_->header.stamp = clearing_observation.cloud_->header.stamp;
412412

nav2_costmap_2d/src/costmap_2d_cloud.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,14 @@ void voxelCallback(const nav2_msgs::msg::VoxelGrid::ConstSharedPtr grid)
193193
pcl_header.frame_id = frame_id;
194194
pcl_header.stamp = stamp;
195195

196+
if (pub_marked->get_subscription_count() > 0)
196197
{
197198
auto cloud = std::make_unique<sensor_msgs::msg::PointCloud2>();
198199
pointCloud2Helper(cloud, num_marked, pcl_header, g_marked);
199200
pub_marked->publish(std::move(cloud));
200201
}
201202

203+
if (pub_unknown->get_subscription_count() > 0)
202204
{
203205
auto cloud = std::make_unique<sensor_msgs::msg::PointCloud2>();
204206
pointCloud2Helper(cloud, num_unknown, pcl_header, g_unknown);

nav2_costmap_2d/src/costmap_2d_markers.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,9 @@ void voxelCallback(const nav2_msgs::msg::VoxelGrid::ConstSharedPtr grid)
138138
p.z = c.z;
139139
}
140140

141-
pub->publish(std::move(m));
141+
if (pub->get_subscription_count() > 0) {
142+
pub->publish(std::move(m));
143+
}
142144

143145
timer.end();
144146
RCLCPP_INFO(

nav2_costmap_2d/src/costmap_2d_ros.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,9 @@ Costmap2DROS::updateMap()
583583
transformFootprint(x, y, yaw, padded_footprint_, *footprint);
584584

585585
RCLCPP_DEBUG(get_logger(), "Publishing footprint");
586-
footprint_pub_->publish(std::move(footprint));
586+
if (footprint_pub_->get_subscription_count() > 0) {
587+
footprint_pub_->publish(std::move(footprint));
588+
}
587589
initialized_ = true;
588590
}
589591
}

0 commit comments

Comments
 (0)