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

Revision history [back]

If you've converted the geometry_msg data type to string in a yaml format, you could read it back into a message type by first loading it into a dictionary using yaml.safe_load, and then filling a message object using rosidl_runtime_py.set_message_fields.

Here's an example of an object being converted to string, and then restored back into a new object:

#!/usr/bin/env python3

import yaml
from geometry_msgs.msg import Pose
from rosidl_runtime_py import message_to_yaml, set_message_fields

# Create Pose msg
original = Pose()
original.position.x = 1.0
original.position.y = 2.0
original.position.z = 3.0

# Convert to string
pose_string = message_to_yaml(original)
print("Pose is: \n", pose_string)

# Convert back to yaml
values_dictionary = yaml.safe_load(pose_string)
restored = Pose()
set_message_fields(restored, values_dictionary)

print("Restored x, y, z: ", restored.position.x, restored.position.y, restored.position.z)

If you've converted the geometry_msg data type msg to string in using a yaml format, you could read it back into a message type by first loading it into a dictionary using yaml.safe_load, and then filling a message object using rosidl_runtime_py.set_message_fields.

Here's an example of an object a geometry_msgs.msg.Pose being converted to string, and then restored back into a new object:

#!/usr/bin/env python3

import yaml
from geometry_msgs.msg import Pose
from rosidl_runtime_py import message_to_yaml, set_message_fields

# Create Pose msg
original = Pose()
original.position.x = 1.0
original.position.y = 2.0
original.position.z = 3.0

# Convert to string
pose_string = message_to_yaml(original)
print("Pose is: \n", pose_string)

# Convert back to yaml
values_dictionary = yaml.safe_load(pose_string)
restored = Pose()
set_message_fields(restored, values_dictionary)

print("Restored x, y, z: ", restored.position.x, restored.position.y, restored.position.z)