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

ROS Subscribe to multiple topics with single function

asked 2022-10-11 06:26:58 -0600

FurkanEdizkan gravatar image

updated 2022-10-11 06:28:47 -0600

Thanks for feedback and help in advance,

I want to give input of topic names, initial data and data_type as dictionary to a function, and that function will subscribe to each topic and save data to dictionary. This is what I have done so far.

Example input

ros_topics = {"rosTopic1": {"data": None,
                            "type": "msg_type1"},
              "rosTopic2": {"data": None,
                            "type": "msg_type2"},
              "rosTopic3": {"data": None,
                            "type": "msg_type3"}}

I can subscribe to multiple topics and show their output message, however I couldn't save them into dictionary.

My function so far

def subscribe_topics(self):
    for key in self.ros_topics.keys():
        rospy.Subscriber(name=key,
                         data_class=self.ros_topics[key]["type"],
                         callback=self.common_callback)

def common_callback(self, msg):
    print("####")
    # Want to store msg.data to ros_topics[key]["data"], however I couldn't transfer **key**
    print(msg)
    print("####")

Stackoverflow Question Link

  • Python 3.8
  • ROS Noetic
  • Ubuntu 20.04
edit retag flag offensive close merge delete

Comments

As someone fairly new to ROS, the "msg_type1" within double inverted commas sort of threw me off. So for anyone else stuck there, it is apparently not supposed to be a string containing the name of the type of message (for e.g. NOT ... {"rosTopic1": {"data": None, "type": "nav_msgs/Odometry"} ...).

Rather, here is supposed to be the object itself, for eg ... {"rosTopic1": {"data": None, "type": Odometry} ...

This is with the assumption that you have already added the required import line. For this case: from nav_msgs.msg import Odometry

shrini96 gravatar image shrini96  ( 2023-05-23 14:38:34 -0600 )edit

Yes, you are correct, I write them in that way to just show any msg type

FurkanEdizkan gravatar image FurkanEdizkan  ( 2023-07-14 02:42:44 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-10-11 07:42:51 -0600

ravijoshi gravatar image

You need to use callback_args parameter of the Subscriber class.

In your case, you can set the key as shown below:

    rospy.Subscriber(name=key,
                     data_class=self.ros_topics[key]["type"],
                     callback=self.common_callback,
                     callback_args=key)

And then change the callback signature to accommodate key as shown below:

def common_callback(self, msg, args):
    key = args

I have not tested the above code. In case of an error, please inform and change the callback_args to a tuple.

For more information, please check this answer.

edit flag offensive delete link more

Comments

This question scratches the same itch: https://answers.ros.org/question/3985...

ljaniec gravatar image ljaniec  ( 2022-10-11 08:46:26 -0600 )edit

Yes, it perfectly works. Thank you

FurkanEdizkan gravatar image FurkanEdizkan  ( 2022-10-12 02:34:25 -0600 )edit

I can open an other question for this topic but since we are here, is this usage a good way to use ros subscriber?

FurkanEdizkan gravatar image FurkanEdizkan  ( 2022-10-12 02:35:11 -0600 )edit
1

@FurkanEdizkan I am glad it worked. Yes, it is all right to use callback_args. If you face any problems with it, feel free to open a new question, as you said.

ravijoshi gravatar image ravijoshi  ( 2022-10-12 05:55:09 -0600 )edit

Thank you for answer

FurkanEdizkan gravatar image FurkanEdizkan  ( 2022-10-12 06:01:53 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2022-10-11 06:26:58 -0600

Seen: 1,965 times

Last updated: Oct 11 '22