Best way to save ROS 2 topic messages to text file in Python callback?
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)