1
$\begingroup$

Rosanswers logo

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


Originally posted by steinaak on ROS Answers with karma: 151 on 2016-04-09

Post score: 14

$\endgroup$

2 Answers 2

0
$\begingroup$

Rosanswers logo

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/how-to-deliver-arguments-to-a-callback-function/

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


Originally posted by lucasw with karma: 8729 on 2016-04-09

This answer was ACCEPTED on the original site

Post score: 36


Original comments

Comment by steinaak on 2016-04-09:
THANK YOU SO MUCH!!!!! exactly what I wanted!

Comment by gvdhoorn on 2016-04-09:
@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.

Comment by Rodrigo on 2016-07-29:
This is exactly what I was looking for, thanks a ton!!

Comment by gvdhoorn on 2016-08-13:
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.

Comment by yossi_ov on 2017-11-09:
Could you please point to an example with class and members?

Comment by TrinhNC on 2017-11-30:
If I want to use this with message_filters.Subscriber() then how should I do it?

Comment by Rahul S on 2021-03-05:
how to read 'args' parameters?

$\endgroup$
0
$\begingroup$

Rosanswers logo

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/html/rospy.topics.Subscriber-class.html) implemented yet.


Originally posted by fbelmonteklein with karma: 140 on 2019-03-11

This answer was NOT ACCEPTED on the original site

Post score: 7


Original comments

Comment by bruno.rosa on 2019-06-17:
This a very elegant solution and also thanks for pointing out it solves the issue on hydro.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.