Get ROS2 message fields from Python messages?
Can I get the message fields (names and their types) of ROS2 Python messages dynamically like in ROS1 (via __slots__
)?
ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
Can I get the message fields (names and their types) of ROS2 Python messages dynamically like in ROS1 (via __slots__
)?
Looking at the latest ROS2 master code, it looks like you can currently get the names of the message fields from __slots__
, but not the types:
>>> import std_msgs.msg
>>> x = std_msgs.msg.Header()
>>> dir(x)
['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', '_frame_id', '_stamp', 'frame_id', 'stamp']
>>> print(x.__slots__)
['_stamp', '_frame_id']
>>>
@thinwybk I'll add that there's no reason for the lack of these methods on the message structures, you should be able to get the field names and types, so we just need to add these methods in the code, see: https://github.com/ros2/rosidl_python...
Also, they're all properties, so you can look at the properties of the class as well: https://stackoverflow.com/a/1215428/6... But that still only gives you the names.
ROS 2 Crystal does have that information: MessageClass.get_fields_and_field_types()
(see https://github.com/ros2/rosidl_python... )
Note that the API and returned information for this kind of introspection is likely going to change in the next ROS 2 release (Dashing) so check the migration in the future to check for information.
Asked: 2018-09-29 12:26:41 -0600
Seen: 2,009 times
Last updated: Feb 19 '19
Is there an example command for remote discovery?
Is there a release date of ros 2 or more informations about it?
Is the planned ROS1 to ROS2/DDS bridge available yet?
ROS2: content-based topic subscriptions?
Building Ros 2.0 without OpenCV?
ros2 rttest example build error