ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A answers.ros.org
Ask Your Question
0

Duration of 1 second during while(ros::ok())

asked 2018-07-04 08:17:20 -0600

kk2105 gravatar image

updated 2018-08-03 12:43:57 -0600

Hi All,

Below is the snippet of the code I have.

 int main()
 {
       //Declaration part
       ros::Rate rate(50);
       while(ros::ok())
       {
             //I need to run one condition or function at each second
             //Modify the message based on the condition in previous line
             //Publish the message
             rate.sleep();
             ros::spinOnce();
        }
 }

Can anybody please tell me how can I check for some condition at each second?

Thank you, KK

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2018-07-04 09:05:23 -0600

Delb gravatar image

You are looking for Timers, see the wiki

void callback(const ros::TimerEvent&)
{
  ROS_INFO("Callback triggered");
}

int main(int argc, char **argv)
{
  ros::init(argc, argv, "name");
  ros::NodeHandle n;
  ros::Timer timer = n.createTimer(ros::Duration(1.0), callback);
}

Here the callback will be triggered every second. You can put your condition or function inside the callback to have it called every second.

edit flag offensive delete link more

Comments

@Delb Thanks for suggestion.. I will check this option and get back to you ..

kk2105 gravatar image kk2105  ( 2018-07-05 00:03:54 -0600 )edit
0

answered 2018-07-04 10:38:38 -0600

updated 2018-07-04 10:40:12 -0600

You essentially have the code, you just need to modify your rate value. Right now you have 50 which will run at 50Hz (i.e. 50 times per second), so you should set that value to 1 so that it runs only once per second. Your code would be:

 int main()
 {
     //Declaration part
     ros::Rate rate(1); // periodic at 1 cycle per second
     while(ros::ok())
     {
         // Run your function and check condition

         // Modify the message based on the condition

         // Publish the message

         rate.sleep(); // runs out whatever duration is remaining for the 1 second interval
         ros::spinOnce();
     }
 }

Using the Rate will try to maintain your loop running at the period you specify. For example, if your function call took 0.5 seconds, the Rate will fill in the remaining 0.5 seconds, and your loop will take 1.0 seconds in total. This page gives a nice overview of your options.

edit flag offensive delete link more

Comments

@aconkey .. Thanks for the input... But in my case the loop rate should be 50Hz, in that loop at every 1 second I need to check the condition..

kk2105 gravatar image kk2105  ( 2018-07-05 00:03:03 -0600 )edit

Your Answer

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

Add Answer

Question Tools

1 follower

Stats

Asked: 2018-07-04 08:17:20 -0600

Seen: 1,524 times

Last updated: Jul 04 '18