@@ -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
- #if (GAZEBO_MAJOR_VERSION == 11 && GAZEBO_MINOR_VERSION > 1) || (GAZEBO_MAJOR_VERSION == 9 && GAZEBO_MINOR_VERSION > 14)
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 ();
@@ -208,7 +206,7 @@ void GazeboRosApiPlugin::onResponse(ConstResponsePtr &response)
208
206
{
209
207
210
208
}
211
- #if (GAZEBO_MAJOR_VERSION == 11 && GAZEBO_MINOR_VERSION > 1) || (GAZEBO_MAJOR_VERSION == 9 && GAZEBO_MINOR_VERSION > 14)
209
+ #ifdef GAZEBO_ROS_HAS_PERFORMANCE_METRICS
212
210
void GazeboRosApiPlugin::onPerformanceMetrics (const boost::shared_ptr<gazebo::msgs::PerformanceMetrics const > &msg)
213
211
{
214
212
gazebo_msgs::PerformanceMetrics msg_ros;
@@ -385,9 +383,15 @@ void GazeboRosApiPlugin::advertiseServices()
385
383
ros::VoidPtr (), &gazebo_queue_);
386
384
pub_model_states_ = nh_->advertise (pub_model_states_ao);
387
385
388
- #if (GAZEBO_MAJOR_VERSION == 11 && GAZEBO_MINOR_VERSION > 1) || (GAZEBO_MAJOR_VERSION == 9 && GAZEBO_MINOR_VERSION > 14)
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,14 +565,37 @@ 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_--;
567
594
if (pub_link_states_connection_count_ <= 0 ) // disconnect with no subscribers
568
595
{
569
596
pub_link_states_event_.reset ();
570
597
if (pub_link_states_connection_count_ < 0 ) // should not be possible
571
- ROS_ERROR_NAMED (" api_plugin" , " One too mandy disconnect from pub_link_states_ in gazebo_ros.cpp? something weird" );
598
+ ROS_ERROR_NAMED (" api_plugin" , " One too many disconnect from pub_link_states_ in gazebo_ros.cpp? something weird" );
572
599
}
573
600
}
574
601
@@ -579,7 +606,7 @@ void GazeboRosApiPlugin::onModelStatesDisconnect()
579
606
{
580
607
pub_model_states_event_.reset ();
581
608
if (pub_model_states_connection_count_ < 0 ) // should not be possible
582
- ROS_ERROR_NAMED (" api_plugin" , " One too mandy disconnect from pub_model_states_ in gazebo_ros.cpp? something weird" );
609
+ ROS_ERROR_NAMED (" api_plugin" , " One too many disconnect from pub_model_states_ in gazebo_ros.cpp? something weird" );
583
610
}
584
611
}
585
612
0 commit comments