Get list of all topics within c++ node
I am trying to get a list of all topics (both published and subscribed) to see whether a topic is available or not.
Using method I can get list of all advertised topics but not the ones that are only subscribed to and don't have a publishers yet.
Is there a decent way to do it within a cpp node?
Asked by Choco93 on 2019-08-30 05:34:57 UTC
Answers
You can find out how many nodes are publishing and listening to a topic.
You'll need to create a subscriber to the topic, wait for a second or so for any publishers to connect then use the getNumPublishers() method to check that it's being published.
You can do the same trick with a subscriber and it's getNumSubscribers() method.
I don't know if there is a more direct approach, but I know this way will work.
Asked by PeteBlackerThe3rd on 2019-08-30 05:50:55 UTC
Comments
Yeah I went over that but it's a generic node and I don't want to make a publisher or a subscriber in my node.
Asked by Choco93 on 2019-08-30 05:56:37 UTC
You can use the ros_type_introspection package to create generic subscribers. It's a bit trickier for publishers. Why don't you want to make publishers and subscribers in your code? They would only be needed briefly to get the information you want.
Asked by PeteBlackerThe3rd on 2019-08-30 06:24:17 UTC
Just that it will make code a bit messier, for now will make do with advertised topics, might add subscribers in future
Asked by Choco93 on 2019-09-04 06:36:56 UTC
Comments
The Master API has a method called
getSystemState(..)
. That would return all publishers and subscribers. From that you should be able to derive the information you are looking for.Asked by gvdhoorn on 2019-08-30 05:54:20 UTC
that's a python API, I would prefer something in cpp so I can easily add it to my node, but might make a generic node out of it, thanks
Asked by Choco93 on 2019-09-04 06:34:39 UTC
That page documents the API, which is an XML-RPC API. It's language agnostic.
You can use any library capable of invoking XML-RPC methods. In ROS 1, xmlrpcpp is used.
There may already be some code that implements this, but I can't link you to a specific example.
Asked by gvdhoorn on 2019-09-04 07:00:21 UTC