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

How to obtain list of all available topics [python]

asked 2017-03-06 10:30:42 -0500

vvyogi gravatar image

In my gazebo model I have 9 differential drive vehicles.
When I execute rostopic list i get

...    and so on

I wish to find out all the odom topics in python script and subscribe to all of them using the message_filter

However, I am not being able to find/locate the getTopics() function as is mentioned here Kindly let me know what package do I import to get the getTopics() function working.

Any help is appreciated. Thanks.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2017-03-06 10:50:56 -0500

The getTopics function is C++ only. The equivalent function in Python is get_published_topics.

edit flag offensive delete link more



Note that this only gives topics that are advertised while rostopic list also shows topics that are only subscribed to by some node.

Felix Widmaier gravatar image Felix Widmaier  ( 2018-02-09 02:53:01 -0500 )edit

answered 2022-01-23 11:26:04 -0500

lucasw gravatar image

updated 2022-01-23 12:38:42 -0500

Adapted this out of (maybe if I looked a little more there's a more convenient function and I wouldn't need to populate a dict)

import rosgraph
import rostopic

# not sure if any better or different that master=None
master = rosgraph.Master('/rostopic')
pubs, subs = rostopic.get_topic_list(master=master)
topic_data = {}
print(f"subs {len(subs)}")
for topic in pubs:
    name = topic[0]
    if name not in topic_data:
        topic_data[name] = {}
        topic_data[name]['type'] = topic[1]
    topic_data[name]['publishers'] = topic[2]
print(f"subs {len(pubs)}")
for topic in subs:
    name = topic[0]
    if name not in topic_data:
        topic_data[name] = {}
        topic_data[name]['type'] = topic[1]
    topic_data[name]['subscribers'] = topic[2]

for topic_name in sorted(topic_data.keys()):
edit flag offensive delete link more

Question Tools

1 follower


Asked: 2017-03-06 10:28:05 -0500

Seen: 9,182 times

Last updated: Jan 23 '22