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

Few basic questions regarding some ROS basic code

asked 2011-12-16 02:20:42 -0600

alfa_80 gravatar image

updated 2011-12-16 13:58:15 -0600

joq gravatar image

I have gone through the basic ROS tutorial but I am not still getting some parts of it. Could someone please provide me some explanations about them.

My confusions:

  1. Why most of the time I see, the way people who are embracing object-oriented paradigm code does not invoke a specific or every member function, instead, they just instantiate an object, then call ros::spin() and done (eg. a code found here)
  2. What is the use of ros::spin() and its effect of using it to the callback function?
  3. Why people tend to use callback function a lot, what is pros of it and when is it appropriate to utilize it?
  4. Can in a class contain more than one callback function?

Thanks in advance..

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2011-12-16 03:29:04 -0600

Bill Smart gravatar image
  1. Using a class is a convenient way of encapsulating the code and of having persistent storage across callback invocations. Generally, subscriptions are set up in the constructor for the class. The callbacks are triggered by incoming messages, so no explicit calls to them have to be made.
  2. ros::spin() gives control over to ROS, and waits until the node shuts down.
  3. Callbacks are one of the main design elements of ROS code. This is the only way to respond to messages coming in on topics.
  4. Yes, although you will need to subscribe to a topic for each of the callbacks you want to be active.
edit flag offensive delete link more


Regarding no.1, the strange thing is that when I don't call explicitly a member function that is supposed to callback subscribing some messages on a topic, it does not work. It seems that, sometimes, I need to explicitly call it. What is the possible culprit?
alfa_80 gravatar image alfa_80  ( 2011-12-16 04:28:26 -0600 )edit
You should make sure that messages are actually being published on the topic you're subscribed to. Calling the function explicitly won't make sense, since the message is passed to the function by the callback mechanism. Calling it explicitly, you don't have the message to supply as a parameter.
Bill Smart gravatar image Bill Smart  ( 2011-12-16 06:10:23 -0600 )edit
Yes, it exists, perhaps other reasons could be in my case. Thanks a lot anyway.
alfa_80 gravatar image alfa_80  ( 2011-12-16 06:55:00 -0600 )edit
@Bill Smart: If I declare one of the methods not as a callback function, will it be invoked implicitly? I just understand, I could be the case one of my methods not called, maybe it's not of callback one, am I right in this regard?
alfa_80 gravatar image alfa_80  ( 2011-12-16 18:06:18 -0600 )edit
alfa_80 gravatar image alfa_80  ( 2011-12-17 06:16:42 -0600 )edit

answered 2011-12-16 03:40:45 -0600

joq gravatar image

updated 2011-12-16 23:40:05 -0600

  1. Some nodes only declare their callbacks and handle messages when they arrive. That is quite common.

  2. The callback is invoked from within the ros::spin() method.

  3. Callbacks are a good way to do real-time message handling. Most ROS interfaces are organized that way.

  4. Yes. You many need to subscribe to multiple topics, with different message types. Each will have a separate callback method.

EDIT: If your node has additional work outside its callbacks, the usual approach is to invoke ros::spinOnce() within your main processing loop.

Or, you could define a ros::Timer callback to do the extra work.

edit flag offensive delete link more


@joq: ros::spin() calls all the callback functions implicitly as you have said. That implies, if the method is not of callback, then we, need to explicitly call it right? Or what is the best practise to do it?
alfa_80 gravatar image alfa_80  ( 2011-12-16 23:24:45 -0600 )edit
@joq: Perhaps you would like to give a respond on this one here ( since my problem on this matter still persists..
alfa_80 gravatar image alfa_80  ( 2011-12-17 06:15:25 -0600 )edit
The answer by @Eric Perko looks good to me. What did you not understand about it?
joq gravatar image joq  ( 2011-12-17 07:05:11 -0600 )edit
Thanks, it's answered there..
alfa_80 gravatar image alfa_80  ( 2011-12-17 07:25:35 -0600 )edit

Question Tools


Asked: 2011-12-16 02:20:42 -0600

Seen: 738 times

Last updated: Dec 16 '11