ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

You can use ros::Rate where frequency is the desired frequency.

// Run at the configured rate, discarding msgs if necessary

ros::Rate rate(frequency);

while (ros::ok())
  {
  ros::spinOnce();
  rate.sleep();

  if (std::abs((rate.expectedCycleTime() - ros::Duration(1.0 / frequency)).toSec()) > 0.001)
  {
      // Change rate dynamically; if must be above 0, as 0 will provoke a segfault on next spinOnce
      ROS_DEBUG("Changing frequency from %.2f to %.2f", 1.0 / rate.expectedCycleTime().toSec(), frequency);
      rate = ros::Rate(frequency);
  }

}

You can use ros::Rate where frequency is the desired frequency.

// Run at the configured rate, discarding msgs if necessary

ros::Rate rate(frequency);

//main loop

while (ros::ok())
  {
  ros::spinOnce();
  rate.sleep();

  if (std::abs((rate.expectedCycleTime() - ros::Duration(1.0 / frequency)).toSec()) > 0.001)
  {
      // Change rate dynamically; if must be above 0, as 0 will provoke a segfault on next spinOnce
      ROS_DEBUG("Changing frequency from %.2f to %.2f", 1.0 / rate.expectedCycleTime().toSec(), frequency);
      rate = ros::Rate(frequency);
  }

}