# ros::Rate::sleep() equivalent in rosserial

I am building a rosserial application.

I would like to use a construct such as the following

ros::Rate loop_rate(30);

while (ros::ok())
{
update();
ros::spinOnce();
loop_rate.sleep();
}


rate.h does not appear to be included in rosserial. With ros, I can effectively achieve the same thing using ros::Duration, which also has a sleep function. While duration.h exists in rosserial, it does not appear to have a sleep function...

Is there an equivalent of ros::Rate or ros::Duration in rosserial which allows me to set a looping frequency in this way?

The best equivalent I can find is something like the following:

while (1)
{
update();
nh.spinOnce();
sleep(1./30.);
}


This is not exactly equivalent, though, as it sleeps for a constant time, whereas ros::Rate::sleep and ros::Duration::sleep only sleep for the remaining time in the cycle.

edit retag close merge delete

I think implementing a simple approximation of ros::Rate::sleep would be pretty straightforward, though. The function would take a timestamp at each call, establish the time elapsed from the previous call, and then sleep for desired_period - elapsed_time.