From 8c67c2f9378aa6c422f45e24da76135e7ffe5518 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20=C3=89corchard?= Date: Thu, 6 Jan 2022 10:40:51 +0100 Subject: [PATCH] Add support for ROS2 topic type Required on Foxy. --- rosshow/rosshow.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/rosshow/rosshow.py b/rosshow/rosshow.py index 5eeb8a3..6c63579 100755 --- a/rosshow/rosshow.py +++ b/rosshow/rosshow.py @@ -21,6 +21,7 @@ VIEWER_MAPPING = { + # ROS1. "nav_msgs/Odometry": ("rosshow.viewers.nav_msgs.OdometryViewer", "OdometryViewer", {}), "nav_msgs/OccupancyGrid": ("rosshow.viewers.nav_msgs.OccupancyGridViewer", "OccupancyGridViewer", {}), "nav_msgs/Path": ("rosshow.viewers.nav_msgs.PathViewer", "PathViewer", {}), @@ -49,6 +50,14 @@ "geometry_msgs/Twist": ("rosshow.viewers.generic.MultiPlotViewer", "MultiPlotViewer", {"data_fields": ["linear.x", "linear.y", "linear.z", "angular.x", "angular.y", "angular.z"]}), } +# ROS2, e.g. nav_msgs/Odometry -> nav_msgs/msg/Odometry. +_viewer_mapping_ros2 = {} +for k, v in VIEWER_MAPPING.items(): + message_package, message_name = k.split("/", 2) + _viewer_mapping_ros2[f'{message_package}/msg/{message_name}'] = v +VIEWER_MAPPING.update(_viewer_mapping_ros2) + + def capture_key_loop(viewer): global getch while True: @@ -114,7 +123,7 @@ def main(): sys.exit(0) if topic_types[TOPIC] not in VIEWER_MAPPING: - print("Unsupported message type.") + print(f"Unsupported message type '{topic_types[TOPIC]}'.") exit() # Create the canvas and viewer accordingly @@ -127,7 +136,13 @@ def main(): viewer_class = getattr(__import__(module_name, fromlist=(class_name)), class_name) viewer = viewer_class(canvas, title = TOPIC, **viewer_kwargs) - message_package, message_name = topic_types[TOPIC].split("/", 2) + try: + # ROS1. + message_package, message_name = topic_types[TOPIC].split("/", 2) + except ValueError: + # ROS2. + message_package, _, message_name = topic_types[TOPIC].split("/", 3) + message_class = getattr(__import__(message_package + ".msg", fromlist=(message_name)), message_name) # Subscribe to the topic so the viewer actually gets the data