ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Get ROS2 message fields from Python messages?

asked 2018-09-29 12:26:41 -0600

thinwybk gravatar image

updated 2018-10-02 02:44:16 -0600

Can I get the message fields (names and their types) of ROS2 Python messages dynamically like in ROS1 (via __slots__)?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2018-10-03 13:06:54 -0600

clalancette gravatar image

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']
>>>
edit flag offensive delete link more

Comments

1

@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...

William gravatar image William  ( 2018-10-05 09:19:47 -0600 )edit
1

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.

William gravatar image William  ( 2018-10-05 09:21:28 -0600 )edit

@William I would need the field names with their corresponding types. I could not find unit tests for the provided message meta info. From a functional point of view it's not required anyway. Would you accept a PR without tests for it?

thinwybk gravatar image thinwybk  ( 2018-10-08 02:43:16 -0600 )edit
0

answered 2019-02-19 15:20:47 -0600

Dirk Thomas gravatar image

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.

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2018-09-29 12:26:41 -0600

Seen: 2,009 times

Last updated: Feb 19 '19