Gazebo | Ignition | Community
Ask Your Question

Revision history [back]

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();
}

Cheers, Andrei

click to hide/show revision 2
update, how to get the parameter from sdf

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