What is the difference between using a ROS timer and CPU timer?

asked 2017-02-21 19:37:39 -0500

vinmean gravatar image

I want a while loop to run at 400 Hz. I used the below format for my loop

ros::Rate loop_rate(400)
while(ros::ok())
{
  //do stuffs
  ros::spinOnce();
  loop_rate.sleep();
}

I even published a topic within the loop and when the topic is subscribed to, it shows 400Hz. But when I use a cpu clock based timer, the loop seems to run much faster. So when I use a ros::Rate::sleep() is there no guarantee that the individual steps run at the set frequency?

boost::timer t;
while(ros::ok())
{
  //do stuffs
  std::cout<< t.elapsed() << std::endl;
  t.restart();
  ros::spinOnce();
  loop_rate.sleep();
}

The output of the timer is as below (in Secs) which should have been around 0.0025 for a 400 Hz loop.

0.000118
0.000109
0.000138
0.000123
.
.
edit retag flag offensive close merge delete