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

Using Rostopic hz in a ros node

asked 2012-11-19 03:52:13 -0600

amittleider gravatar image

updated 2012-11-19 04:02:25 -0600

Hello all,

Is there a way to get rostopic hz information from within a ros node? I can't find an API for this. I did find the source code (https://github.com/ros/ros_comm/blob/master/tools/rostopic/src/rostopic/__init__.py), but I'm not sure how to use this.

Thanks

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2012-11-19 09:54:54 -0600

amittleider gravatar image

updated 2012-11-19 09:57:04 -0600

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
edit flag offensive delete link more

Comments

What about a C++ program?

kunaltyagi gravatar image kunaltyagi  ( 2014-07-28 00:54:54 -0600 )edit

Question Tools

Stats

Asked: 2012-11-19 03:52:13 -0600

Seen: 2,868 times

Last updated: Nov 19 '12