ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

Does rospy have a get_subscribed_topics() function?

asked 2011-11-09 10:04:31 -0600

updated 2011-11-09 10:04:43 -0600

rospy has rospy.get_published_topics() but doesn't seem to have the analogous function for subscribed topics. Am I missing something?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2013-01-15 14:29:26 -0600

amittleider gravatar image

updated 2013-01-15 14:48:31 -0600

This code should help accomplish what you want. It will simply print out the entire ROSGraph nodes and edges. You can infer subscribers from here.

 #!/usr/bin/env python import roslib; 
 roslib.load_manifest('ssnode') import 
 rospy import roslib 
 import roslib.message
 import rosgraph
 import rostopic 
 from rostopic import ROSTopicHz 
 from rosgraph.impl import graph

 def mygraph():
     g = graph.Graph()
         while 1:

           if not g.nn_nodes and not g.srvs:
           if g.nn_nodes:
               for n in g.nn_nodes:
                   prefix = n + '|'
                   print('  ' + n + ' :')
                   print('    Inbound:')
                   for k in g.nn_edges.edges_by_end.iterkeys():
                       if k.startswith(prefix):
                           for c in g.nn_edges.edges_by_end[k]:
                               print('      ' + c.start + ' label = ' + c.label)
                   print('    Outbound:')
                   for k in g.nn_edges.edges_by_start.iterkeys():
                       if k.startswith(prefix):
                           for c in g.nn_edges.edges_by_start[k]:
                               print('      ' + c.end + ' label = ' + c.label)
           if g.srvs:
               for s in g.srvs:
                   print('  ' + s)
     except KeyboardInterrupt:
if __name__ == '__main__':
    except rospy.ROSInterruptException: pass
edit flag offensive delete link more

answered 2021-09-02 06:04:10 -0600

heuristicus gravatar image

updated 2021-09-02 06:05:54 -0600

An alternative way of doing this without having to look at the graph is to use

    from rosgraph.masterapi import Master
    master = Master("/my_node")
    all_topics = [topic_tuple[0] for topic_tuple in master.getTopicTypes()]
    published_topics = [topic_tuple[0] for topic_tuple in master.getPublishedTopics("/")]
    subscribed_topics = list(set(all_topics) - set(published_topics))
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2011-11-09 10:04:31 -0600

Seen: 487 times

Last updated: Sep 02 '21