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

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

Hi!

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):
    rospy.loginfo(data)

def subscriber():
    rospy.init_node("listen_for_conversation",anonymous=True)
    dict_1 = {.....}
    dict_2={......}
    content = rospy.Subscriber("text",String,callback)
    rospy.spin()

Hoping for good answers!

Thanks

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
36

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 http://answers.ros.org/question/12045...

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

Comments

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

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

@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
1

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
7

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 http://docs.ros.org/melodic/api/rospy... ) implemented yet.

edit flag offensive delete link more

Comments

1

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

Question Tools

4 followers

Stats

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

Seen: 32,317 times

Last updated: Mar 11 '19