Home | Tutorials | Wiki | Issues
Ask Your Question
1

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?

Thanks

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
1

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

AndreiHaidu gravatar image

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

Hi,

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)
    return;

  ...

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


========UPDATE, HOW TO GET THE PARAMETER EXAMPLE=========
in the sdf file:

<plugin name='plugin_name' filename='libplugin_name.so'>
    <sdf_parameter_update_rate>69.0</sdf_parameter_update_rate>
</plugin>

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

Comments

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](http://answers.gazebosim.org/question/2334/confusion-with-commontimesectonano/).

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

Question Tools

Stats

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

Seen: 3,689 times

Last updated: Mar 26 '13