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

In Python script, how determine what topics a node is publishing?

asked 2021-06-03 14:59:36 -0600

BesterJester gravatar image

updated 2021-06-07 16:03:47 -0600

I'm using the packages rosnode and rospy in my Python script. Given a node's name, how do I get the topics that node is publishing?

I'm able to get lots of information that is close.

  • I'm able to get a list of all topics being published using rospy.get_published_topics

  • I'm able to get a list of all active nodes using rosnode.rosnode_ping_all

  • I'm able to get the info about a node using rosnode.get_node_info_description(node_name), but this returns one gigantic string. The name of the topics is in the string, but it isn't easy to pull the topic names out (like it would be if the topic names were in a list).

I had the idea to use get_node_info from the NodeInfo class (see below code).

import rospy, rosnode
from rqt_top.node_info import NodeInfo
rospy.init_node('example_node')
node_class = NodeInfo()
node_info = node_class.get_node_info('/rosout')
print(node_info)

However, the node info it returns and prints (see below) doesn't seem to list the node's topics (as I thought it would).

psutil.Process(pid=6866, name='rosout', started='12:14:15')

I can't seem to find a tool, which I can place in my Python script, that links a node to the specific topics that node publishes.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-06-08 01:19:35 -0600

James NT gravatar image

updated 2021-06-08 02:08:23 -0600

The command line tool

rosnode info /<your_node>

will give you the information you are after. You can embed that or alternatively you can import it through python as you are doing:

>>> import rosnode 
>>> # print(rosnode.get_node_info_description("<your_node>")
>>> print(rosnode.get_node_info_description("/laser_assembler"))  # Example from my current project

Node [/laser_assembler]
Publications: 
 * /rosout [rosgraph_msgs/Log]

Subscriptions: 
 * /bogus [unknown type]
 * /laser_scan [sensor_msgs/LaserScan]
 * /tf [tf2_msgs/TFMessage]
 * /tf_static [tf2_msgs/TFMessage]

Services: 
 * /assemble_scans
 * /assemble_scans2
 * /build_cloud
 * /build_cloud2
 * /laser_assembler/get_loggers
 * /laser_assembler/set_logger_level

Similarly you can import rostopic

A quick way to see the member functions is:

>>> import rostopic
>>> print(getmembers(rostopic, isfunction))
edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2021-06-03 14:59:36 -0600

Seen: 1,596 times

Last updated: Jun 08 '21