I think I was struggling with this problem because I am not familiar with Python, but it wasn't too hard after reading the rostopic source code for a couple of hours.
I also encountered problems with rostopic hz because I did not have a list of topic names beforehand. Run the sample ros node below to understand how to retrieve all of this data. The myhz() method will run the ros loop and continuously print out the rate of the sometopic, and the mygraph() function will print all nodes and edges (with to and from information).
#!/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()
try:
while 1:
g.update()
if not g.nn_nodes and not g.srvs:
print("empty")
else:
print('\n')
if g.nn_nodes:
print('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:
print('Services:')
for s in g.srvs:
print(' ' + s)
except KeyboardInterrupt:
pass
def myhz(topic, window_size=-1, filter_expr=None):
rospy.loginfo("check if roscore is up")
while !rospy.is_shutdown(): thegraph(); ros::sleep(10);
return
rospy.init_node("myname", anonymous=True)
rospy.loginfo("registered node name")
rt = ROSTopicHz(window_size, filter_expr=filter_expr)
# have to subscribe with topic_type
sub = rospy.Subscriber(topic, rospy.AnyMsg, rt.callback_hz)
print("subscribed to [%s]"%topic)
while not rospy.is_shutdown():
rospy.loginfo(rt.times)
if __name__ == '__main__':
try:
mygraph()
#myhz("sometopic")
except rospy.ROSInterruptException: pass