In Python script, how determine what topics a node is publishing?
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 getnodeinfo 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.
Asked by BesterJester on 2021-06-03 14:59:36 UTC
Answers
The command line tool
rosnode info /
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))
Asked by James NT on 2021-06-08 01:19:35 UTC
Comments