Gazebo | Ignition | Community
Ask Your Question

How to control publish rate in ROS-enabled plugins? [closed]

asked 2013-03-25 10:50:56 -0500

Boris gravatar image

Are there any standard option for this? It used to be <updateRate> parameter, but it seems not to work anymore. Although there is <update_rate>, it works only for sensor plugins.

At the moment my model plugin publishes data in OnUpdate() function without any additional time control.

Should I publish the data in parallel thread and achieve necessary rate with something like ros::Rate::sleep() or there is a better option?


edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by scpeters
close date 2014-04-23 12:38:01.211476

1 Answer

Sort by ยป oldest newest most voted

answered 2013-03-25 12:43:34 -0500

AndreiHaidu gravatar image

updated 2013-03-26 02:58:36 -0500


I don't think there is a dedicated function for this, but you can easily use it like this:

// set the update rate (you can also set it as a parameter from the sdf file)
this->updateRate = common::Time(0, common::Time::SecToNano(0.75));

// initialize the prevUpdateTime
this->prevUpdateTime = common::Time::GetWallTime();

void PluginClass::OnUpdate()
  if (common::Time::GetWallTime() - this->prevUpdateTime < this->updateRate)


  this->prevUpdateTime = common::Time::GetWallTime();

in the sdf file:

<plugin name='plugin_name' filename=''>

in the plugin:

void PluginName::Load(physics::ModelPtr _parent, sdf::ElementPtr _sdf)
double param_update_rate;

if (!_sdf->HasElement("sdf_parameter_update_rate"))
    // if parameter tag does NOT exist
    std::cout << "Missing parameter <sdf_parameter_update_rate> in PluginName, default to 0" << std::endl;
    param_update_rate = 0;
    // if parameter tag exists, get its value
else param_update_rate = _sdf->GetElement("sdf_parameter_update_rate")->GetValueDouble();

and then you use this param_update_rate in setting the updateRate value:

this->updateRate = common::Time(0, common::Time::SecToNano(param_update_rate));

Cheers, Andrei

edit flag offensive delete link more


Thanks, will give it a try. Does this->updateRate load from <updateRate> or <update_rate> parameter?

Boris gravatar imageBoris ( 2013-03-25 23:46:14 -0500 )edit

you set it in the code yourself ( common::Time(0, common::Time::SecToNano(0.75)); ), or you can load it as a parameter from the sdf file, I will update the answer to see how that is done

AndreiHaidu gravatar imageAndreiHaidu ( 2013-03-26 02:48:37 -0500 )edit

Yeah, I know how to get parameters from SDF, but thanks for the code snippets anyway! I was wondering if some of the legacy parameters that use to load automagically are still persist... Looks like not.

Boris gravatar imageBoris ( 2013-03-26 08:51:50 -0500 )edit

Finally added this functionality to my plugin. The only concern is the `SecToNano()` function, see details [here](

Boris gravatar imageBoris ( 2013-04-11 01:19:30 -0500 )edit

Question Tools


Asked: 2013-03-25 10:50:56 -0500

Seen: 4,494 times

Last updated: Mar 26 '13