Thank you everyone for telling me good information, I implemented a sample code below using get_topic_names_and_types
.
### `sample_to_show_topic_list.py` ###
import rclpy
from rclpy.node import Node
def get_topic_list():
node_dummy = Node("_ros2cli_dummy_to_show_topic_list")
topic_list = node_dummy.get_topic_names_and_types()
node_dummy.destroy_node()
return topic_list
rclpy.init()
topic_list = get_topic_list()
for info in topic_list:
print(info[0])
rclpy.shutdown()
Execute this code, and get following output in the terminal.
$ ros2 topic pub --rate 1 hoge std_msgs/msg/String "{data: "hoge"}" # publish a string
$ python3 sample_to_show_topic_list.py # show topic list
/hoge
/parameter_events
/rosout
Originally posted by ksato-dev with karma: 83 on 2021-01-06
This answer was NOT ACCEPTED on the original site
Post score: 5