@@ -32,6 +32,7 @@ GazeboRosApiPlugin::GazeboRosApiPlugin() :
32
32
stop_ (false ),
33
33
pub_link_states_connection_count_ (0 ),
34
34
pub_model_states_connection_count_ (0 ),
35
+ pub_performance_metrics_connection_count_ (0 ),
35
36
physics_reconfigure_initialized_ (false ),
36
37
pub_clock_frequency_ (0 ),
37
38
world_created_ (false )
@@ -187,13 +188,10 @@ void GazeboRosApiPlugin::loadGazeboRosApiPlugin(std::string world_name)
187
188
light_modify_pub_ = gazebonode_->Advertise <gazebo::msgs::Light>(" ~/light/modify" );
188
189
request_pub_ = gazebonode_->Advertise <gazebo::msgs::Request>(" ~/request" );
189
190
response_sub_ = gazebonode_->Subscribe (" ~/response" ,&GazeboRosApiPlugin::onResponse, this );
190
- #ifdef GAZEBO_ROS_HAS_PERFORMANCE_METRICS
191
- performance_metric_sub_ = gazebonode_->Subscribe (" /gazebo/performance_metrics" ,
192
- &GazeboRosApiPlugin::onPerformanceMetrics, this );
193
- #endif
194
191
// reset topic connection counts
195
192
pub_link_states_connection_count_ = 0 ;
196
193
pub_model_states_connection_count_ = 0 ;
194
+ pub_performance_metrics_connection_count_ = 0 ;
197
195
198
196
// / \brief advertise all services
199
197
advertiseServices ();
@@ -387,7 +385,13 @@ void GazeboRosApiPlugin::advertiseServices()
387
385
388
386
#ifdef GAZEBO_ROS_HAS_PERFORMANCE_METRICS
389
387
// publish performance metrics
390
- pub_performance_metrics_ = nh_->advertise <gazebo_msgs::PerformanceMetrics>(" performance_metrics" , 10 );
388
+ ros::AdvertiseOptions pub_performance_metrics_ao =
389
+ ros::AdvertiseOptions::create<gazebo_msgs::PerformanceMetrics>(
390
+ " performance_metrics" ,10 ,
391
+ boost::bind (&GazeboRosApiPlugin::onPerformanceMetricsConnect,this ),
392
+ boost::bind (&GazeboRosApiPlugin::onPerformanceMetricsDisconnect,this ),
393
+ ros::VoidPtr (), &gazebo_queue_);
394
+ pub_performance_metrics_ = nh_->advertise (pub_performance_metrics_ao);
391
395
#endif
392
396
393
397
// Advertise more services on the custom queue
@@ -561,6 +565,29 @@ void GazeboRosApiPlugin::onModelStatesConnect()
561
565
pub_model_states_event_ = gazebo::event::Events::ConnectWorldUpdateBegin (boost::bind (&GazeboRosApiPlugin::publishModelStates,this ));
562
566
}
563
567
568
+ #ifdef GAZEBO_ROS_HAS_PERFORMANCE_METRICS
569
+ void GazeboRosApiPlugin::onPerformanceMetricsConnect ()
570
+ {
571
+ pub_performance_metrics_connection_count_++;
572
+ if (pub_performance_metrics_connection_count_ == 1 ) // connect on first subscriber
573
+ {
574
+ performance_metric_sub_ = gazebonode_->Subscribe (" /gazebo/performance_metrics" ,
575
+ &GazeboRosApiPlugin::onPerformanceMetrics, this );
576
+ }
577
+ }
578
+
579
+ void GazeboRosApiPlugin::onPerformanceMetricsDisconnect ()
580
+ {
581
+ pub_performance_metrics_connection_count_--;
582
+ if (pub_performance_metrics_connection_count_ <= 0 ) // disconnect with no subscribers
583
+ {
584
+ performance_metric_sub_.reset ();
585
+ if (pub_performance_metrics_connection_count_ < 0 ) // should not be possible
586
+ ROS_ERROR_NAMED (" api_plugin" , " One too many disconnect from pub_performance_metrics_ in gazebo_ros.cpp? something weird" );
587
+ }
588
+ }
589
+ #endif
590
+
564
591
void GazeboRosApiPlugin::onLinkStatesDisconnect ()
565
592
{
566
593
pub_link_states_connection_count_--;
0 commit comments