ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
1

Get list of all topics within c++ node

asked 2019-08-30 05:34:57 -0600

Choco93 gravatar image

updated 2019-08-30 05:53:16 -0600

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?

edit retag flag offensive close merge delete

Comments

2

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.

gvdhoorn gravatar image gvdhoorn  ( 2019-08-30 05:54:20 -0600 )edit

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

Choco93 gravatar image Choco93  ( 2019-09-04 06:34:39 -0600 )edit

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.

gvdhoorn gravatar image gvdhoorn  ( 2019-09-04 07:00:21 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2019-08-30 05:50:55 -0600

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.

edit flag offensive delete link more

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.

Choco93 gravatar image Choco93  ( 2019-08-30 05:56:37 -0600 )edit

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.

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2019-08-30 06:24:17 -0600 )edit

Just that it will make code a bit messier, for now will make do with advertised topics, might add subscribers in future

Choco93 gravatar image Choco93  ( 2019-09-04 06:36:56 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2019-08-30 05:34:57 -0600

Seen: 926 times

Last updated: Aug 30 '19