ROS Resources: Documentation | Support | Discussion Forum | Service Status | Q&A answers.ros.org

# Quaternion transformations in Python

I'm trying to place some Markers in RViz, using a node written in Python. To this end, I need to create a geometry_msgs.mgs.Pose with an orientation Quaternion.

But I can't for the life of me find the utility and conversion functions that I need for Quaternions. There are some in tf.transformations, but those produce numpy Quaternions, which I would have to manually split up and throw into the geometry_msgs.msg.Quaternion constructor.

Considering that Quaternions are of little use without a set of functions to work with them, my question is: Where are the proper conversion functions for Quaternions? That is, functions to create and modify geometry_msgs.msg.Quaternion or to convert between a numpy Quaternion (4-tuple) and geometry_msgs.msg.Quaternion (object with x,y,z,w attributes).

My environment is Ubuntu 12.04 Precise, with ROS fuerte (using fuerte is currently mandatory for this project; I'm already looking into upgrading, but that's not at all easy).

edit retag close merge delete

Sort by ยป oldest newest most voted

I also remember having problems with this a few months back, the documentation is not overly clear; but in the end it is very simple.

To convert from euler to quaternion:

quaternion = tf.transformations.quaternion_from_euler(roll, pitch, yaw)
#type(pose) = geometry_msgs.msg.Pose
pose.orientation.x = quaternion[0]
pose.orientation.y = quaternion[1]
pose.orientation.z = quaternion[2]
pose.orientation.w = quaternion[3]


And to convert from quaternion to euler:

#type(pose) = geometry_msgs.msg.Pose
quaternion = (
pose.orientation.x,
pose.orientation.y,
pose.orientation.z,
pose.orientation.w)
euler = tf.transformations.euler_from_quaternion(quaternion)
roll = euler[0]
pitch = euler[1]
yaw = euler[2]

more

5

As an update in 2016, it could be written as:

pose.orientation = geometry_msgs.msg.Quaternion(*tf_conversions.transformations.quaternion_from_euler(roll, pitch, yaw))

( 2016-08-16 04:28:19 -0500 )edit

To convert from a numpy array to a Quaternion message type, it's just:

q = numpy.array([.5, .5, .5, .5])
from geometry_msgs.msg import Quaternion
pub.publish(Quaternion(*q))


Converting from the message type to a numpy array is harder and I could never find a provided function, so I usually do:

xyzw_array = lambda o: numpy.array([o.x, o.y, o.z, o.w])
print xyz_array(msg.pose.orientation)

more

The way you make this work is:

explicit_quat = [quaternion.x, quaternion.y, quaternion.z, quaternion.w]

your_euler = tf.transformations.euler_from_quaternion(explicit_quat)

Hope this helps

more