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 -0500
Seen: 1,712 times
Last updated: Feb 19 '19
how to organize data transfer between nodes on multiple PCs on ROS2
ROS2 Foxy composable node: Failed to load component due to undefined symbol
simulation with bag play data get different results
ROS vs ROS 2.0 vs ROS industrial
Why image not recieved using ROS2 functions in the node?
map_server: Original error: parameter 'yaml_filename' is not initialized