ROS2: How to get the sender of a message?

asked 2022-04-04 04:19:14 -0500

tompe17 gravatar image

updated 2022-04-04 07:44:05 -0500

Is it possible in ROS2 (using rclpy) to get the sender of a message in a callback? It was possible in ROS1 and I used it for forwarding data between agents using another mechanism than ROS.

The use case I have is that I want to have topics like /unit_info and different units (with different namespace) can get that data either using ROS2 or using another mechanism and having different DOMAIN ID for the different units.

So my ROS1 solution was to filter away the /unit_info messages not generated on the current unit when doing the forwarding of messages to other units. But I cannot find how to do that in ROS2...

What I have is one node receiving messages in a non ROS way and translating it to ROS messages and put it on topics. So I need to be able to detect a messages generated by this node in other nodes.

edit retag flag offensive close merge delete

Comments

If it is a completely custom message that you are passing, you can try including an extra field in the message file where you add in the node name. In the publisher side, you can fill in the node name field by using self.get_name()(ref).

In the subscriber side, you do as you said and filter out the messages by this new field.

sampreets3 gravatar image sampreets3  ( 2022-04-06 04:57:43 -0500 )edit

I am also having standard message like for tf. I might use a wrapper but the biggest issue with that solution is that I need to be compatible with an existing ROS1 system. I could change the old ROS1 code but I decided it was to much work. Now I instead in the ROS2 program i have a flag if I need to use this non ROS communication and I push the messages that should go to other agents on another topic. In my example /ext/unit_info. It seems to be a reasonable solution and I avoid having to receive a message to check if it should be ignored.

tompe17 gravatar image tompe17  ( 2022-04-06 05:43:43 -0500 )edit