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

Revision history [back]

Adapted this out of https://github.com/ros/ros_comm/blob/noetic-devel/tools/rostopic/src/rostopic/__init__.py#L1223-L1231 (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 rospy
...

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)

Adapted this out of https://github.com/ros/ros_comm/blob/noetic-devel/tools/rostopic/src/rostopic/__init__.py#L1223-L1231 (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 rospy
rostopic
...

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)

Adapted this out of https://github.com/ros/ros_comm/blob/noetic-devel/tools/rostopic/src/rostopic/__init__.py#L1223-L1231 (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)