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

ROS2/create_subscription: How to pass callback arguments

asked 2020-10-06 15:57:29 -0600

afar gravatar image

In ROS 1, I was able to pass arguments to the callback function of a subscriber: rospy.Subscriber(topic_name, type, call_back, call_back_arg)

How can I do this in ROS2? My goal is to pass the name of the topic. Because I have a single call back for many topics and when the callback is invoked, I need to know what topic called it. It there a better way to find this? Thank you

edit retag flag offensive close merge delete

Comments

Hey,I am also kind of stuck at same situation. can you post the answer if you have already.

akashbhavsar gravatar image akashbhavsar  ( 2021-12-27 02:50:05 -0600 )edit

Please see the below answer

afar gravatar image afar  ( 2021-12-27 16:31:29 -0600 )edit

1 Answer

Sort by » oldest newest most voted
1

answered 2021-12-27 15:51:43 -0600

afar gravatar image

One of the ROS2 developers, "Ivan Santiago Paunovic", answered this question as following:

You can easily solve this using a lambda:

node.create_subscription(std_msgs.msg.String, "my_topic", lambda msg: common_callback(msg, other_args), 10)

Reference in Github

edit flag offensive delete link more

Question Tools

3 followers

Stats

Asked: 2020-10-06 15:57:29 -0600

Seen: 2,374 times

Last updated: Dec 27 '21