ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

when is the callback function called

asked 2015-04-20 09:30:44 -0500

Naman gravatar image

Hi all,

I am slightly confused about when the callback functions are called from ROS Subscriber. Taking the following code as example:

#include "ros/ros.h"
#include <geometry_msgs/Vector3.h>

void callback_fn(const geometry_msgs::Vector3::ConstPtr& ticks)

int main(int argc, char **argv)
    ros::NodeHandle n;
    ros::Subscriber re_msg = n.subscribe("re_ticks",1000,callback_fn);

So my question is whenever ros::spin() is called, does it call all the callback functions or only those for which the value of the message on the corresponding topic has changed? So in the above example, Is callback_fn called every time ros::spin() is executed or only when the message on the topic "re_ticks" has changed?

Thanks in advance.
Naman Kumar

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2015-04-20 09:37:24 -0500

dornhege gravatar image

Messages don't "change". Messages are send from someone and the received. So, the callback will be called every time a new message is received. This is independent from how fast the loop inside ros::spin goes (unless messages arrive faster than it can handle them) and from the values of the message, i.e., if someone sends the same message 5 times with the same unchanged values, the callback should be called 5 times.

edit flag offensive delete link more


yes..that makes sense..Thanks!

Naman gravatar image Naman  ( 2015-04-20 09:58:02 -0500 )edit

Question Tools

1 follower


Asked: 2015-04-20 09:30:44 -0500

Seen: 1,695 times

Last updated: Apr 20 '15