It is possible to know what ROS2 topic subscribe the message ?

asked 2022-02-18 11:48:59 -0500

Hello,

I'm working in a project using ROS2 and i would like to know if it is possible to know the topic o publish the message. For example, imagine that i have 3 subscribers (Sub1, Sub2and Sub3) and the callback function is just printing the data of the message, if i publish a message to Sub3 can i know in the callback function what topic as used ?

self.test_sub1 = self.create_subscription(Bool, "Sub1", self.callback, 10)
self.test_sub2 = self.create_subscription(Bool, "Sub2", self.callback, 10)
self.test_sub3 = self.create_subscription(Bool, "Sub3", self.callback, 10)

def callback(self, msg):
    print(msg.data)
    print(topic_name)       -> can i know this ?
edit retag flag offensive close merge delete

Comments

See the previously asked question Reading data from different topics, using the same callback

If you want to use "one" callback for many topics and know which topic sent the message, you can do it using a closure.

abhishek47 gravatar image abhishek47  ( 2022-02-19 02:36:51 -0500 )edit