#################################### ##FILL ME IN #################################### ## links to any required tutorials ## note.0= [[roscpp_tutorials/Tutorials|roscpp tutorials]] ## descriptive title for the tutorial ## title = Understanding Timers ## multi-line description to be displayed in search ## description = This tutorial explains roscpp Timers, which allow you to schedule a callback to happen periodically. ## the next tutorial description (optional) ## next =[[roscpp_tutorials/Tutorials/Publisher%20and%20Subscriber%20with%20Parameters%20and%20Dynamic%20Reconfigure| Dynamic Reconfigure]] ## links to next tutorial (optional) ## next.0.link= ## next.1.link= ## what level user is this tutorial for ## level= IntermediateCategory #################################### <> <> == What are Timers? == Timers let you schedule a callback to happen at a specified rate. They are a more flexible and useful form of the `ros::Rate` used in the [[ROS/Tutorials/WritingPublisherSubscriber(c++)|Writing a Simple Publisher and Subscriber]] tutorial. Note that Timers are '''not''' a realtime thread/kernel replacement, and make no guarantees about how accurate they are, which can vary wildly because of system load/capabilities. See also: [[roscpp/Overview/Timers|roscpp Timers overview]] == Using Timers == Creating a Timer works very similar to creating a Subscriber. {{{#!cplusplus ros::Timer timer = n.createTimer(ros::Duration(0.1), timerCallback); }}} Timer callbacks take the form: {{{#!cplusplus void timerCallback(const ros::TimerEvent& e); }}} == A full example == Now that you've seen the basics, let's go through a larger example, with multiple Timers. === The code === <> === The code explained === I'll ignore parts that have been explained in previous tutorials, which really just leaves two lines: <> Here we create two timers, one which fires every 100 milliseconds, and one which fires every second. If you run this program, it should output something like the following: {{{ [ INFO] 1251854032.362376000: Callback 1 triggered [ INFO] 1251854032.462840000: Callback 1 triggered [ INFO] 1251854032.562464000: Callback 1 triggered [ INFO] 1251854032.662169000: Callback 1 triggered [ INFO] 1251854032.762649000: Callback 1 triggered [ INFO] 1251854032.862853000: Callback 1 triggered [ INFO] 1251854032.962642000: Callback 1 triggered [ INFO] 1251854033.063118000: Callback 1 triggered [ INFO] 1251854033.162221000: Callback 1 triggered [ INFO] 1251854033.262749000: Callback 1 triggered [ INFO] 1251854033.262864000: Callback 2 triggered [ INFO] 1251854033.362643000: Callback 1 triggered [ INFO] 1251854033.463158000: Callback 1 triggered ... }}} == The TimerEvent Structure == The `ros::TimerEvent` structure provides you information about the timing of the current timer. Here is its definition: {{{#!cplusplus struct TimerEvent { Time last_expected; ///< In a perfect world, this is when the last callback should have happened Time last_real; ///< When the last callback actually happened Time current_expected; ///< In a perfect world, this is when the current callback should be happening Time current_real; ///< This is when the current callback was actually called (Time::now() as of the beginning of the callback) struct { WallDuration last_duration; ///< How long the last callback ran for, always in wall-clock time } profile; }; }}} ## AUTOGENERATED DO NOT DELETE ## TutorialCategory ## FILL IN THE STACK TUTORIAL CATEGORY HERE