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

Is there any function in ROS which specifies time duration?

asked 2018-12-09 02:17:40 -0600

Ibrahim_aerospace gravatar image

Hello all ! I want to ask you is there any function for specifying time-duration and we pass that duration as argument to while loop and ask the loop to apply specific commands inside the loop for that specified duration of time. If yes, what is that and how use it please do comment. Bunch of thanks

edit retag flag offensive close merge delete

Comments

Are you (implicitly) asking about task scheduling?

gvdhoorn gravatar image gvdhoorn  ( 2018-12-09 02:45:05 -0600 )edit

@gvdhoorn I am not sure about task scheduling but what I want to do is to apply a command for specific interval of time let say 5 seconds, this is what I want to do.

Ibrahim_aerospace gravatar image Ibrahim_aerospace  ( 2018-12-09 06:03:46 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2018-12-09 11:09:09 -0600

If I'm understanding your question you want run a while loop for a specific period of time to repeatedly performing some processing or action. This is perfectly possible, although it's not really a function since this is part of the structure of your own program you need to write.

Using ros::Time you can record the time before your while loop, then continue iterating until a certain amount of time has passed. This structure would look like this:

ros::Time startTime = ros::Time::now();
ros::Duration loopDuration(5.0); // 5 seconds
while (ros::Time::now() < startTime+loopDuration)
{
  // do your processing here
}

Since ROS is an event driven system it's not a good idea to execute a block of code for a long period of time without checking for messages, so it's good practice to add a call to spinOnce in there to keep up-to date with message processing.

ros::Time startTime = ros::Time::now();
ros::Duration loopDuration(5.0); // 5 seconds
while (ros::Time::now() < startTime+loopDuration)
{
  // do your processing here

  ros::spinOnce(); //  check for messages
}

You may also want your loop run at a particular frequency as opposed to going 'flat out' as the previous two will. Using the ros::Rate object you can control this as below:

ros::Time startTime = ros::Time::now();
ros::Duration loopDuration(5.0); // 5 seconds
ros::Rate loopRate(30.0); // 30 Hz
while (ros::Time::now() < startTime+loopDuration)
{
  // do your processing here

  ros::spinOnce(); //  check for messages

  loopRate.sleep(); // delay loop so it doesn't run faster than 30 Hz
}

Hope this helps you get this working the way you want.

edit flag offensive delete link more

Comments

@PeteBlackerThe3rd Thanks this is what I want.

Ibrahim_aerospace gravatar image Ibrahim_aerospace  ( 2018-12-09 22:16:38 -0600 )edit

Great, can you accept this answer by clicking the tick next to it. Thanks.

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2018-12-10 02:14:51 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2018-12-09 02:17:40 -0600

Seen: 917 times

Last updated: Dec 09 '18