ROS Resources: Documentation | Support | Discussion Forum | Service Status | Q&A
Ask Your Question

Quaternion transformations in Python

asked 2013-07-29 21:21:59 -0500

nevik gravatar image

updated 2014-01-28 17:17:28 -0500

ngrennan gravatar image

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 flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted

answered 2013-07-30 00:15:34 -0500

Miguel S. gravatar image

updated 2013-07-30 00:19:17 -0500

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 = (
euler = tf.transformations.euler_from_quaternion(quaternion)
roll = euler[0]
pitch = euler[1]
yaw = euler[2]
edit flag offensive delete link more



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))
vtalpaert gravatar imagevtalpaert ( 2016-08-16 04:28:19 -0500 )edit

answered 2013-07-30 12:00:26 -0500

forrestv gravatar image

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

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

answered 2017-04-28 13:52:26 -0500

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

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2013-07-29 21:21:59 -0500

Seen: 24,565 times

Last updated: Apr 28