Conversion between quaternions and Euler angles

From Wikipedia, the free encyclopedia
Jump to: navigation, search

Spatial rotations in three dimensions can be parametrized using both Euler angles and unit quaternions. This article explains how to convert between the two representations. Actually this simple use of "quaternions" was first presented by Euler some seventy years earlier than Hamilton to solve the problem of magic squares. For this reason the dynamics community commonly refers to quaternions in this application as "Euler parameters".

Definition[edit]

A unit quaternion can be described as:

\mathbf{q} = \begin{bmatrix} q_0 & q_1 & q_2 & q_3 \end{bmatrix}^T
|\mathbf{q}|^2 = q_0^2 + q_1^2 + q_2^2 + q_3^2 = 1

We can associate a quaternion with a rotation around an axis by the following expression

\mathbf{q}_0 = \cos(\alpha/2)
\mathbf{q}_1 = \sin(\alpha/2)\cos(\beta_x)
\mathbf{q}_2 = \sin(\alpha/2)\cos(\beta_y)
\mathbf{q}_3 = \sin(\alpha/2)\cos(\beta_z)

where α is a simple rotation angle (the value in radians of the angle of rotation) and cos(βx), cos(βy) and cos(βz) are the "direction cosines" locating the axis of rotation (Euler's Theorem).

Rotation matrices[edit]

Euler angles for Body 3-1-3 Sequence – The xyz (original fixed Lab) system is shown in blue, the XYZ (rotated final Body) system is shown in red. The line of nodes, labelled N and shown in green, is the intermediate Body X-axis around which the second rotation occurs.

The orthogonal matrix (post-multiplying a column vector) corresponding to a clockwise/left-handed rotation by the unit quaternion q=q_0+iq_1+jq_2+kq_3 is given by the inhomogeneous expression:

\begin{bmatrix}
 1- 2(q_2^2 + q_3^2) &  2(q_1 q_2 - q_0 q_3) &  2(q_0 q_2 + q_1 q_3) \\
2(q_1 q_2 + q_0 q_3) & 1 - 2(q_1^2 + q_3^2)  &  2(q_2 q_3 - q_0 q_1) \\
2(q_1 q_3 - q_0 q_2) & 2( q_0 q_1 + q_2 q_3) &  1 - 2(q_1^2 + q_2^2)
\end{bmatrix}

or equivalently, by the homogeneous expression:

\begin{bmatrix}
q_0^2 + q_1^2 - q_2^2 - q_3^2 &  2(q_1 q_2 - q_0 q_3) &  2(q_0 q_2 + q_1 q_3) \\
2(q_1 q_2 + q_0 q_3) & q_0^2 - q_1^2 + q_2^2 - q_3^2 &  2(q_2 q_3 - q_0 q_1) \\
2(q_1 q_3 - q_0 q_2) & 2( q_0 q_1 + q_2 q_3) & q_0^2 - q_1^2 - q_2^2 + q_3^2 
\end{bmatrix}

If q_0+iq_1+jq_2+kq_3 is not a unit quaternion then the homogeneous form is still a scalar multiple of a rotation matrix, while the inhomogeneous form is in general no longer an orthogonal matrix. This is why in numerical work the homogeneous form is to be preferred if distortion is to be avoided.

The direction cosine matrix (from the rotated Body XYZ coordiantes to the original Lab xyz coordinates) corresponding to a Body 3-2-1 sequence with Euler angles (ψ, θ, φ) is given by:[1]

\begin{bmatrix}
\cos\theta \cos\psi & -\cos\phi \sin\psi + \sin\phi \sin\theta \cos\psi &   \sin\phi \sin\psi + \cos\phi \sin\theta \cos\psi \\
\cos\theta \sin\psi &  \cos\phi \cos\psi + \sin\phi \sin\theta \sin\psi & -\sin\phi \cos\psi + \cos\phi \sin\theta \sin\psi \\
-\sin\theta             &  \sin\phi \cos\theta                                          &   \cos\phi \cos\theta \\
\end{bmatrix}

Conversion[edit]

By combining the quaternion representations of the Euler rotations we get for the Body 3-2-1 sequence, where the airplane first does yaw (Body-Z) turn during taxiing on the runway, then pitches (Body-Y) during take-off, and finally rolls (Body-X) in the air. The resulting orientation of Body 3-2-1 sequence (around the capitalized axis in the illustration of Tait–Bryan angles) is equivalent to that of lab 1-2-3 sequence (around the lower-cased axis), where the airplane is rolled first (lab-x axis), and then nosed up around the horizontal lab-y axis, and finally rotated around the vertical lab-z axis:

 \mathbf{q(Body2lab)} = 
\begin{bmatrix} \cos (\psi /2) \\ 0 \\ 0 \\ \sin (\psi /2) \\ \end{bmatrix}
\begin{bmatrix} \cos (\theta /2) \\ 0 \\ \sin (\theta /2) \\ 0 \\ \end{bmatrix}
\begin{bmatrix} \cos (\phi /2) \\ \sin (\phi /2) \\ 0 \\ 0 \\ \end{bmatrix}
 = \begin{bmatrix}
\cos (\phi /2) \cos (\theta /2) \cos (\psi /2) +  \sin (\phi /2) \sin (\theta /2) \sin (\psi /2) \\
\sin (\phi /2) \cos (\theta /2) \cos (\psi /2) -  \cos (\phi /2) \sin (\theta /2) \sin (\psi /2) \\
\cos (\phi /2) \sin (\theta /2) \cos (\psi /2) +  \sin (\phi /2) \cos (\theta /2) \sin (\psi /2) \\
\cos (\phi /2) \cos (\theta /2) \sin (\psi /2) -  \sin (\phi /2) \sin (\theta /2) \cos (\psi /2) \\
\end{bmatrix}

Other rotation sequences use different conventions.[1]

Relationship with Tait–Bryan angles[edit]

Tait–Bryan angles for an aircraft

Similarly for Euler angles, we use the Tait–Bryan angles (in terms of flight dynamics):

  • Roll – \phi: rotation about the X-axis
  • Pitch – \theta: rotation about the Y-axis
  • Yaw – \psi: rotation about the Z-axis

where the X-axis points forward, Y-axis to the right and Z-axis downward and in the example to follow the rotation occurs in the order yaw, pitch, roll (about body-fixed axes).

Singularities[edit]

One must be aware of singularities in the Euler angle parametrization when the pitch approaches ±90° (north/south pole). These cases must be handled specially. The common name for this situation is gimbal lock.

Code to handle the singularities is derived on this site: www.euclideanspace.com

See also[edit]

External links[edit]

References[edit]

  1. ^ a b NASA Mission Planning and Analysis Division. "Euler Angles, Quaternions, and Transformation Matrices". NASA. Retrieved 12 January 2013.