Skip to content

Commit 5ab46cf

Browse files
scpetersjacobperron
authored andcommitted
ros2: Only subscribe to /gazebo/performance_metrics when necessary (#1205)
We are currently subscribing to the /gazebo/performance_metrics topic even if there are no subscribers to the ROS topic forwarding this data. This changes gazebo_ros_init to only subscribe to the gazebo topic if there are any subscribers to the corresponding ROS topic. While advertiser callbacks are used in ROS 1 but are not yet in ROS2, here we use polling in the GazeboRosInitPrivate::PublishSimTime callback to check for subscribers since it is called for each Gazebo time step. This also helps workaround the deadlock documented in #1175 and gazebosim/gazebo-classic#2902. This also adds a macro to reduce duplication of the version checking logic. Signed-off-by: Steve Peters <scpeters@openrobotics.org>
1 parent 015e5fc commit 5ab46cf

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

gazebo_ros/src/gazebo_ros_init.cpp

+25-12
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@
3333
#include <memory>
3434
#include <string>
3535

36+
#ifndef GAZEBO_ROS_HAS_PERFORMANCE_METRICS
37+
#if \
38+
(GAZEBO_MAJOR_VERSION == 11 && GAZEBO_MINOR_VERSION > 1) || \
39+
(GAZEBO_MAJOR_VERSION == 9 && GAZEBO_MINOR_VERSION > 14)
40+
#define GAZEBO_ROS_HAS_PERFORMANCE_METRICS
41+
#endif
42+
#endif // ifndef GAZEBO_ROS_HAS_PERFORMANCE_METRICS
43+
3644
namespace gazebo_ros
3745
{
3846

@@ -78,8 +86,7 @@ class GazeboRosInitPrivate
7886
std_srvs::srv::Empty::Request::SharedPtr req,
7987
std_srvs::srv::Empty::Response::SharedPtr res);
8088

81-
#if (GAZEBO_MAJOR_VERSION == 11 && GAZEBO_MINOR_VERSION > 1) || (GAZEBO_MAJOR_VERSION == 9 && \
82-
GAZEBO_MINOR_VERSION > 14)
89+
#ifdef GAZEBO_ROS_HAS_PERFORMANCE_METRICS
8390
/// \brief Subscriber callback for performance metrics. This will be send in the ROS network
8491
/// \param[in] msg Received PerformanceMetrics message
8592
void onPerformanceMetrics(ConstPerformanceMetricsPtr & msg);
@@ -157,8 +164,7 @@ void GazeboRosInit::Load(int argc, char ** argv)
157164
"/clock",
158165
rclcpp::QoS(rclcpp::KeepLast(10)).transient_local());
159166

160-
#if (GAZEBO_MAJOR_VERSION == 11 && GAZEBO_MINOR_VERSION > 1) || (GAZEBO_MAJOR_VERSION == 9 && \
161-
GAZEBO_MINOR_VERSION > 14)
167+
#ifdef GAZEBO_ROS_HAS_PERFORMANCE_METRICS
162168
impl_->performance_metrics_pub_ =
163169
impl_->ros_node_->create_publisher<gazebo_msgs::msg::PerformanceMetrics>(
164170
"performance_metrics", 10);
@@ -178,8 +184,7 @@ void GazeboRosInit::Load(int argc, char ** argv)
178184
std::bind(&GazeboRosInitPrivate::OnWorldCreated, impl_.get(), std::placeholders::_1));
179185
}
180186

181-
#if (GAZEBO_MAJOR_VERSION == 11 && GAZEBO_MINOR_VERSION > 1) || (GAZEBO_MAJOR_VERSION == 9 && \
182-
GAZEBO_MINOR_VERSION > 14)
187+
#ifdef GAZEBO_ROS_HAS_PERFORMANCE_METRICS
183188
void GazeboRosInitPrivate::onPerformanceMetrics(
184189
ConstPerformanceMetricsPtr & msg)
185190
{
@@ -238,14 +243,10 @@ void GazeboRosInitPrivate::OnWorldCreated(const std::string & _world_name)
238243
&GazeboRosInitPrivate::OnUnpause, this,
239244
std::placeholders::_1, std::placeholders::_2));
240245

241-
#if (GAZEBO_MAJOR_VERSION == 11 && GAZEBO_MINOR_VERSION > 1) || (GAZEBO_MAJOR_VERSION == 9 && \
242-
GAZEBO_MINOR_VERSION > 14)
243-
// Gazebo transport
246+
#ifdef GAZEBO_ROS_HAS_PERFORMANCE_METRICS
247+
// Initialize gazebo transport node
244248
gz_node_ = gazebo::transport::NodePtr(new gazebo::transport::Node());
245249
gz_node_->Init(world_->Name());
246-
performance_metric_sub_ = gz_node_->Subscribe(
247-
"/gazebo/performance_metrics",
248-
&GazeboRosInitPrivate::onPerformanceMetrics, this);
249250
#endif
250251
}
251252

@@ -263,6 +264,18 @@ void GazeboRosInitPrivate::PublishSimTime(const gazebo::common::UpdateInfo & _in
263264
rosgraph_msgs::msg::Clock clock;
264265
clock.clock = gazebo_ros::Convert<builtin_interfaces::msg::Time>(_info.simTime);
265266
clock_pub_->publish(clock);
267+
268+
#ifdef GAZEBO_ROS_HAS_PERFORMANCE_METRICS
269+
if (!performance_metric_sub_ && performance_metrics_pub_->get_subscription_count() > 0) {
270+
// Subscribe to gazebo performance_metrics topic if there are ros subscribers
271+
performance_metric_sub_ = gz_node_->Subscribe(
272+
"/gazebo/performance_metrics",
273+
&GazeboRosInitPrivate::onPerformanceMetrics, this);
274+
} else if (performance_metric_sub_ && performance_metrics_pub_->get_subscription_count() == 0) {
275+
// Unsubscribe from gazebo performance_metrics topic if there are no more ros subscribers
276+
performance_metric_sub_.reset();
277+
}
278+
#endif
266279
}
267280

268281
void GazeboRosInitPrivate::OnResetSimulation(

0 commit comments

Comments
 (0)