[ros-users] How to identify the subscriber or the topic name related to a callback

Jeremy Leibs leibs at willowgarage.com
Tue Feb 1 19:49:50 UTC 2011


On Tue, Feb 1, 2011 at 11:42 AM, Jeremy Leibs <leibs at willowgarage.com> wrote:
> On Tue, Feb 1, 2011 at 10:22 AM, Brian Gerkey <gerkey at willowgarage.com> wrote:
>> On Mon, Jan 31, 2011 at 5:46 AM, Markus Bader <markus.bader at tuwien.ac.at> wrote:
>>> I have multiple subscriber calling the same callback function. How can
>>> I determinate within my callback function the related topic name?
>>> getPublisherName gives me the related publisher but not the topic?
>>
>> hi Markus,
>>
>> At the time that you make the subscription, you can arrange for the
>> topic to be passed into the callback, using boost::bind.  Appended
>> below is a simple example that demonstrates this.
>
> While Brian's recommendation certainly works, and is a useful pattern
> for storing arbitrary data along with a generic callback, a different
> approach is to use the "MessageEvent" callback signature instead.
>
> http://www.ros.org/wiki/roscpp/Overview/Publishers%20and%20Subscribers#MessageEvent_.5BROS_1.1.2B-.5D
>
> http://www.ros.org/doc/api/roscpp/html/classros_1_1MessageEvent.html
>
> The connection_header is a std::map of std::strings, including keys,
> "topic" and "callerid" among others.  This can be very useful for
> getting more information about the context of your callback than just
> the message.
>

I apologize, I hadn't seen the full context of the original message,
just Brian's response.  I see you are already using The MessageEvent.
You can get the topic using something like:

ros::M_string connection_header = event.getConnectionHeader();
std::string topic = connection_header["topic"];

> --Jeremy
>



More information about the ros-users mailing list