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

Best way to save ROS 2 topic messages to text file in Python callback?

asked 2020-02-05 06:33:05 -0500

peterpolidoro gravatar image

I would like to save a ROS 2 topic message into a text file in a Python callback.

I can use a csv.DictWriter if I first convert the message into a dictionary. Is there a better way of converting a message into a dictionary rather than iterating over msg._fields_and_field_types? I cannot use vars() since the message does not have a dict attribute and uses slots instead. Is it safe to rely on _fields_and_field_types in future ROS distributions or is it considered private and it is not reliable to access outside the class?

Is there a better way of saving messages to a text file in a Python callback? Thanks!

def _callback(self, msg):
    msg_dict = {field: getattr(msg, field) for field in msg._fields_and_field_types}
    self.csv_dict_writer.writerow(msg_dict)
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2020-02-05 09:16:09 -0500

marguedas gravatar image

The package rosidl_runtime_py provides some utilities for converting messages

Some of them may be useful for your use case:

Is it safe to rely on _fields_and_field_types in future ROS distributions or is it considered private and it is not reliable to access outside the class?

My understanding is that these are considered private and thus discouraged for use outside the class.

edit flag offensive delete link more

Comments

Oh perfect, that is much better than converting it myself. That looks like a fantastic package, thank you so much!

peterpolidoro gravatar image peterpolidoro  ( 2020-02-05 09:48:02 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2020-02-05 06:33:05 -0500

Seen: 2,760 times

Last updated: Feb 05 '20