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

Passing arguments to callback in Python

asked 2016-04-09 06:29:08 -0500

steinaak gravatar image

updated 2016-04-09 07:42:45 -0500

gvdhoorn gravatar image


I have a problem passing arguments to a callback function when Subscribing a ROStopic. The code below is what I do have now. But I only received error messages when I tried to pass arguments to the callback function. I want to send two dictionaries to the callback function together with the text received on the topic called "text" in the subscriber function below. How to pass these arguments? So far I have only been receiving thousands of error messages.

def callback(data):

def subscriber():
    dict_1 = {.....}
    content = rospy.Subscriber("text",String,callback)

Hoping for good answers!


edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2016-04-09 08:06:14 -0500

lucasw gravatar image

updated 2016-04-09 08:12:00 -0500

You want to use the optional callback_args parameter to Subscriber:

def callback(data, args): 
  dict_1 = args[0]
  dict_2 = args[1]
sub = rospy.Subscriber("text", String, callback, (dict_1, dict_2))

Or you could put these into a class, and have the dictionaries be class member variables.

The C++ equivalent uses boost::bind

Cut and paste a reasonable amount of your error messages into the question body if you need help with errors.

edit flag offensive delete link more


THANK YOU SO MUCH!!!!! exactly what I wanted!

steinaak gravatar image steinaak  ( 2016-04-09 08:42:46 -0500 )edit

@steinaak: why did you delete the question? If @lucasw has answered your question, please just accept the answer by ticking the checkmark to the left of the answer.

gvdhoorn gravatar image gvdhoorn  ( 2016-04-09 08:53:21 -0500 )edit

This is exactly what I was looking for, thanks a ton!!

Rodrigo gravatar image Rodrigo  ( 2016-07-29 11:24:16 -0500 )edit

Would have been nice if @steinaak could've done it himself, but I've accepted the answer by @lucasw, as @steinaak (and @Rodrigo) has reported that it worked for him.

gvdhoorn gravatar image gvdhoorn  ( 2016-08-13 11:48:25 -0500 )edit

Could you please point to an example with class and members?

yossi_ov gravatar image yossi_ov  ( 2017-11-09 07:57:06 -0500 )edit

If I want to use this with message_filters.Subscriber() then how should I do it?

TrinhNC gravatar image TrinhNC  ( 2017-11-30 09:31:08 -0500 )edit

how to read 'args' parameters?

Rahul S gravatar image Rahul S  ( 2021-03-05 11:21:38 -0500 )edit

answered 2019-03-11 09:53:36 -0500

fbelmonteklein gravatar image

updated 2019-03-11 09:54:49 -0500

Additionally to the answer presented you can use a lambda like:

def callback(data, dict_1, dict_2):
callback_lambda = lambda x: callback(x,dict_1,dict_2)
sub = rospy.Subscriber("text", String, callback_lambda)

This was useful to me since the version of ROS I was using (hydro) did not seem to have callback_args (see ) implemented yet.

edit flag offensive delete link more



This a very elegant solution and also thanks for pointing out it solves the issue on hydro.

bruno.rosa gravatar image bruno.rosa  ( 2019-06-17 04:10:36 -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: 2016-04-09 06:29:08 -0500

Seen: 29,767 times

Last updated: Mar 11 '19