Ask Your Question

How Timer Callback Function Does the Timing?

asked 2017-05-03 06:20:19 -0500

Winston gravatar image

updated 2017-05-05 08:44:44 -0500

I want to know how accurate ROS timer is. So for example, I create the timer as follows:


There is a callback function here HWController::Timer1_Process, which takes an argument of C++ class ros::TimerEvent. There are several member variables in this class, one of which is current_expected_. I want to know when current_expected_ is calculated. In ROS documentation, the explanation of this variable is

In a perfect world, this is when the current callback should be happening.

However, no further information is given. I want this accurate value because I need to calculate the time error as shown below.

void HWController::Timer1_Process(const ros::TimerEvent &e)
    ros::Duration time_err = e.current_real_ - e.current_expected_;

So, to be more specific, is current_expected_ calculated after this callback function exits (i.e. after the '}' symbol) where it adds the timer period (in this case, 0.0002s) to the current ROS time upon exiting the function? Or is it calculated at the beginning of the function? Actually I prefer to the first explanation because it should not take into account the execution time of this function. I hope someone could clarify this by providing some source codes of its implementation rather than random guessing. I tried to find its implementation but failed. :(

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2017-05-05 16:39:51 -0500

current_expected is computed after the callback is called. But that is only relevant when the timer jumps. In normal operation, the value is computed incrementally from the time the timer was started.

Keep in mind that ros timers and controllers don't go hand in hand. The ros timer's rate varies, and if you dare to set a high rate, the actual rate is going to vary by a lot.

edit flag offensive delete link more


Here is event.current_expected = current_expected_;. It only assigns the value of event.current_expected but I cannot find where it computes the value of current_expected which confused me.

Winston gravatar image Winston  ( 2017-05-05 20:00:02 -0500 )edit

It's next_expected that gets updated. current_expected gets its value from next_expected when a TimerQueueCallback instance is created.

nlamprian gravatar image nlamprian  ( 2017-05-06 00:32:46 -0500 )edit

Please fix your last link. So in this way, does it mean that once the timer loop exits, a TimerQueueCallback instance is created, which means the current_expected updates once the timer loop exits?

Winston gravatar image Winston  ( 2017-05-06 08:35:41 -0500 )edit

Here is the last link. A TimerQueueCallback instance is created each time an iteration is scheduled for execution.

nlamprian gravatar image nlamprian  ( 2017-05-06 11:10:18 -0500 )edit

"A TimerQueueCallback instance is created each time an iteration is scheduled for execution." Could you show me how did you come to this conclusion from the source code? Is it because threadFunc(), but I could not see where it is called?

Winston gravatar image Winston  ( 2017-05-06 19:34:05 -0500 )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



Asked: 2017-05-03 06:20:19 -0500

Seen: 1,276 times

Last updated: May 05 '17