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

Can someone explain callbacks?

asked 2012-07-18 02:52:00 -0500

this post is marked as community wiki

This post is a wiki. Anyone with karma >75 is welcome to improve it.

There seems to be a lack of information on callbacks, or at least hard to find, so I was wondering if someone here could understand them. Right now, they seem like magical functions. I'd never heard of them before, and the Internet hasn't been entirely helpful.

edit retag flag offensive close merge delete


Thanks, that's basically what I've discovered. I had just never heard of it before and they're incredibly useful.

IFLORbot gravatar image IFLORbot  ( 2012-07-18 10:36:11 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2012-07-18 03:05:54 -0500

this post is marked as community wiki

This post is a wiki. Anyone with karma >75 is welcome to improve it.

A callback is a function that you do not call yourself, but define it yourself. Usually you pass the function pointer to another component that will call your function when it seems appropriate.

In the ROS setting a callback in most cases is a message handler. You define the message handler function and give it to subscribe. You never call it yourself, but whenever a message arrives ROS will call you message handler and pass it the new message, so you can deal with that.

edit flag offensive delete link more



Wikipedia article:

Lorenz gravatar image Lorenz  ( 2012-07-18 03:08:39 -0500 )edit

Note that ROS won't call your callback automatically. You have to tell it to spin.

PerkinsJames gravatar image PerkinsJames  ( 2012-07-24 10:59:05 -0500 )edit

I have a question: what's the convenience of a callback function, I think I still can write normal code to realise the functionality of callback. So, why do we have to use callback?

ningmang208 gravatar image ningmang208  ( 2020-10-08 14:29:56 -0500 )edit

answered 2012-07-24 06:42:05 -0500

this post is marked as community wiki

This post is a wiki. Anyone with karma >75 is welcome to improve it.

Hello, This thread was also helpful to me. However I have a follow-up question:

I'm using Python to implement a navigation interface between the ROS nav stack and my own motor controllers. I have created a Driver object that subscribes to the 'cmd_vel' topic and receives Twist messages from this topic. My callback function from the subscriber determines motor commands based on the info in each Twist message it receives.

A confusing problem I've found is that I need to hand my callback function not just the Twist message but also the 'self' object, so that the callback function has access to some global parameters associated with the Driver object. I cannot find a way to pass both pieces of information to the callback function when it's called within the Subscriber command.


class Driver:
    def __init__(self):
         # node initialized and constants described here

    def subscribe_cmd_vel(self):
          rospy.Subscriber("cmd_vel", Twist, self.callback(self))
    def callback(self, msg):
            print "Linear x of Twist message: " + str(msg.linear.x)
            # do some calculations with contents of msg

if __name__ == "__main__"
    dr = Driver()

(end code)

I am consistently only passing the self object to my callback, not the Twist message. When this code hits the print line in the callback function, I get an error saying something like "The Driver object has no 'linear' field.". This means that my msg pointer is being pointed to the self object and my actual Twist message is being lost. Please tell me if you know what mistakes I've made here.

Many thanks, Khiya

edit flag offensive delete link more



[you should ask this in a separate question]

Bill Smart gravatar image Bill Smart  ( 2012-07-24 13:26:55 -0500 )edit

Question Tools


Asked: 2012-07-18 02:52:00 -0500

Seen: 21,862 times

Last updated: Jul 24 '12