Ask Your Question
4

Passing arguments to callback in Python

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

steinaak gravatar image

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

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
15

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

lucasw gravatar image

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

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 imagesteinaak ( 2016-04-09 08:42:46 -0600 )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 imagegvdhoorn ( 2016-04-09 08:53:21 -0600 )edit

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

Rodrigo gravatar imageRodrigo ( 2016-07-29 11:24:16 -0600 )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 imagegvdhoorn ( 2016-08-13 11:48:25 -0600 )edit

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

yossi_ov gravatar imageyossi_ov ( 2017-11-09 07:57:06 -0600 )edit

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

TrinhTUHH gravatar imageTrinhTUHH ( 2017-11-30 09:31:08 -0600 )edit
2

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

fbelmonteklein gravatar image

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

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

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

bruno.rosa gravatar imagebruno.rosa ( 2019-06-17 04:10:36 -0600 )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

3 followers

Stats

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

Seen: 13,511 times

Last updated: Mar 11