ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

how to use Timer

asked 2014-12-18 05:07:17 -0600

cros gravatar image

I have read the tutorial on webpage, but still don't know how to use it,special don't know how to write the funtion void callback( ). I hope someone could tell me how to write and use it more detailed. It's better if there are some examples.

edit retag flag offensive close merge delete


Which timer and which tutorial do you mean?

BennyRe gravatar image BennyRe  ( 2014-12-18 05:55:36 -0600 )edit

1 Answer

Sort by » oldest newest most voted

answered 2014-12-18 06:24:24 -0600

Andromeda gravatar image

updated 2014-12-18 07:53:31 -0600

Let's say you know how a function call works as in the following example:

int main(int argc, char **argv) {

 ros::Rate loop(50);

  while(ros::ok()) {
   callmyfunction();   // <= do that until the end of the world or ros kaputt


void callmyfunction() {
// function body

is your function defined somewhere in your code. That's the most usual way

Ok, now let's say you have a situation where you need to do other things in your main while() or for some reasons you don't want to call that function every loop (maybe because it has another rate or frequency). An example could be a function that publishes points to RViz. It does every 0.5 seconds but yur main routine (your robot) works much more faster (with a frequency of 100 Hz for istance).

In this case you can use timers and your code changes like following:

ros::Timer my_desired_frequency;

int main(int argc, char **argv) {

my_desired_frequency = nh_.createTimer( ros::Duration(2.0), callmyfunction); // Automatically calls every 2.0 seconds the function

 ros::Rate loop(50);

  while(ros::ok()) {

   // Here the main code for your Terminator T1000

   /* callmyfunction(); */ // Not needed anymore since it runs indipendently
   ros::spinOnce;  // NEEDED since callbacks for timers, publishers and listeners must be executed

void callmyfunction(const ros::TimerEvent& event) {  // <= New signature here... for the same function
// function body same as above

Using that timer the function is going to be called indipendently and at another (or you want the same) frequency than your main routine. It doesn't need to be with the main synchronized. Keep it simple: you can think it is running in background and you don't need to call the function directly in your code.

UPDATE: this is not the only way to use a timer. You can create a timer as a member function/variable in a class or as a functor. For the right syntax can you check the following link and ask if yu have questions.

Hope my explanation helps. Regards

edit flag offensive delete link more


One note, for clarity: since it's dependent on the callback mechanism, the timer cannot run faster than the spin rate. Slower, as you show, or the same rate, but not faster.

kramer gravatar image kramer  ( 2014-12-18 07:50:24 -0600 )edit

Thanks kramer!

Andromeda gravatar image Andromeda  ( 2014-12-18 07:51:39 -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


Asked: 2014-12-18 05:07:17 -0600

Seen: 3,342 times

Last updated: Dec 18 '14