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

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

/bot_0/cmd_vel
/bot_0/odom
/bot_1/cmd_vel
/bot_1/odom
/bot_2/cmd_vel
/bot_2/odom
/bot_3/cmd_vel
/bot_3/odom
/bot_4/cmd_vel
/bot_4/odom
/bot_5/cmd_vel
/bot_5/odom
/bot_6/cmd_vel
/bot_6/odom
/bot_7/cmd_vel
/bot_7/odom
/bot_8/cmd_vel
/bot_8/odom
/clock
/gazebo/link_states
...    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
4

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

Comments

3

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
1

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

lucasw gravatar image

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

Adapted this out of https://github.com/ros/ros_comm/blob/... (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()):
    print(topic_name)
edit flag offensive delete link more

Question Tools

1 follower

Stats

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

Seen: 8,944 times

Last updated: Jan 23 '22