Ask Your Question
0

timing function for while loop

asked 2012-08-28 03:45:27 -0500

Arash gravatar image

updated 2014-04-20 14:09:36 -0500

ngrennan gravatar image

I was just wondering, if I want to write a while loop which says while x seconds hasn't reach execute this statement, how would I go about it? I tried using while(ros::Duration(x).sleep()){ .... } but it doesn't work. also, tried creating a counter but there is no way to measure the count in seconds

thanks,

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
6

answered 2012-08-28 03:58:08 -0500

Lorenz gravatar image

updated 2012-08-28 05:15:05 -0500

Example code that should do what you want:

ros::Time start_time = ros::Time::now();
ros::Duration timeout(2.0); // Timeout of 2 seconds
while(ros::Time::now() - start_time < timeout) {
  // do your stuff
}
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2012-08-28 03:45:27 -0500

Seen: 1,969 times

Last updated: Aug 28 '12