0
$\begingroup$

Rosanswers logo

Hi all,

when I subscribe to a topic, is there a way to get the name / namespace of the publishing node on reception of a message?

I know about ros::this_node::getName() and the like [Ref] but that only yields the name/-space of the current node. For example, assume I (manually) subscribe to /rosout and I am receiving a message send from the node /foo/bar/node1. Is there a way to get this information (/foo/bar/node1) in the callback?

Please note that I prefer a way to do this in roscpp.

Thanks in advance!

EDIT: Since I am dealing with custom messages here, I've simply added the namespace of the sender to my message definition. However, I thought there's probably some smarter way to do this (and I was hoping that the sender is already contained somewhere in the received message data).


Originally posted by CodeFinder on ROS Answers with karma: 100 on 2016-07-25

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

There aren't a lot of good reasons for wanting to do this, because it breaks the anonymity of the Anonymous publish/subscribe data flow model. It can be useful for debugging, but building functional logic that depends on the publisher name it is not usually a good idea.

Caveats aside, you can access the publisher name by subscribing to the MessageEvent<msgtype> object (templated on your actual message type), which contains additional metadata in addition to the message data. Have a look at the MessageEvent subsection of the ROSCPP Publishers and Subscribers page for the details.


Originally posted by ahendrix with karma: 47576 on 2016-07-25

This answer was ACCEPTED on the original site

Post score: 1


Original comments

Comment by CodeFinder on 2016-07-26:
Thanks for your answer, good point! What I am considering is: given a global topic /ping, all my robots (/nodes) send a regular msg to that topic. This way, all robots are able to detect other active robots by subscribing to /ping. This "detection" is somehow necessary as there comes ...

Comment by CodeFinder on 2016-07-26:
... a point where two (or more) robots need to interact (/communicate) with each other.

To start communication, a node needs to know the namespace which I add to all msgs send to /ping. IMHO, This retains anonymity until two nodes start (bidirectional) communication. What do you think about this?

Comment by ahendrix on 2016-07-26:
I like your approach because it keeps the anonymity in the pub/sub model, and because it explicitly communicates the namespaces to be used for future communication, instead of trying to infer the namespace from the node name.

Comment by CodeFinder on 2016-07-26:
Thanks, then I'll stick to that design. ;-)

$\endgroup$

Your Answer

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