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

cycleTime returning very incorrect times

asked 2017-03-08 13:53:22 -0500

jdenny gravatar image

I am trying to look at the timing actually performed by ros::Rate::sleep.

ros::Rate loop_rate{1};
while(ros::ok()) {
    //publish to a topic
    ros::spinOnce();
    loop_rate.sleep();
    ROS_INFO("time: %f \n", loop_rate.cycleTime().toSec());
}

I am doing this on the turtlebot platform running ros indigo.

What I am seeing is that the expected cylce time of 1 second holds true in output, however I see outputs of the actual cycle time of 0.0005 seconds on average. However, through

rostopic hz <my topic>

I see a very accurate frequency of 1hz for publishing.

Any ideas on how to fix this/where I am going wrong?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2017-03-08 13:59:14 -0500

gvdhoorn gravatar image

updated 2017-03-08 14:03:45 -0500

I think what you are seeing is actually expected behaviour.

From the documentation of cycleTime() (here):

Get the actual run time of a cycle from start to sleep.

See also here for where this is calculated in rostime/rate.cpp:

set the actual amount of time the loop took in case the user wants to know

So apparently your while-loop body takes 0.0005 seconds to execute. loop_rate.sleep() will just suspend your task/thread for the remainder of the second.

In other words: cycle time != number of iterations per second, but total time per iteration.

edit flag offensive delete link more

Comments

So to clarify, cycleTime() is approximately the time between calls to sleep, excluding the actual sleep-time. And by extension, what would be the best way to accurately time a sleep call? to get the frequency of publishing (actual time) in my cpp program -- expectedCycleTime() - cycleTime()?

jdenny gravatar image jdenny  ( 2017-03-08 14:03:43 -0500 )edit

You'll have to clarify what you mean with "time a sleep call". ros::Rate already takes time spend doing work into account when calculating the time to sleep so you hit / get near your desired loop rate.

gvdhoorn gravatar image gvdhoorn  ( 2017-03-08 14:05:25 -0500 )edit

I guess what I am asking is how do you accurately time the loop including the call to sleep with ros? I get that the loop_rate variable above already is close, but I am wanting to see actual time, not expected.

jdenny gravatar image jdenny  ( 2017-03-08 14:07:15 -0500 )edit

Nevermind, I understand it now. I was confusing myself over nothing. Thank you for your time and patience!

jdenny gravatar image jdenny  ( 2017-03-08 14:10:31 -0500 )edit

Question Tools

Stats

Asked: 2017-03-08 13:53:22 -0500

Seen: 1,195 times

Last updated: Mar 08 '17