Robotics StackExchange | Archived questions

ros::duration does not sleep the desired amount of time

I have this code in ROS-Kinetic

double time_left = time_required - (double)count_to_publish / (double)getPublishFrequency();

ROS_INFO("Time left : %f", time_left);
const std::clock_t begin_time = std::clock();
ros::Duration time_left_duration(time_left);
ROS_INFO("Time left Seconds : %d, Time Left nanoseconds : %d", time_left_duration.sec, time_left_duration.nsec);
time_left_duration.sleep();
std::cout << "Clock : " << float(std::clock () - begin_time) /  CLOCKS_PER_SEC << std::endl;

And then I have this output

[ INFO] [1593193377.141306475]: Time left : 0.153167

[ INFO] [1593193377.141379822]: Time left Seconds : 0, Time Left nanoseconds : 153166699

Clock : 0.000291

It seems to me that Clock should ouput something close to 0.15. Why is the thread not sleeping properly? This is code ran to execute the goal of a ROS action server if that is useful information.

Asked by MartensCedric on 2020-06-26 12:52:20 UTC

Comments

Shouldn't you use std::chrono::system_clock instead? std::clock (from here):

Returns the approximate processor time used by the process since the beginning of an implementation-defined era related to the program's execution.

processor time != wall time.

ros::duration does not sleep the desired amount of time

I don't believe you can draw this conclusion yet.


Edit: and I don't know what you're trying to do exactly, but would ros::Rate not be an easier way to do this?

Asked by gvdhoorn on 2020-06-26 13:18:55 UTC

Answers