Conversion between quaternions and Euler angles

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

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

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

R = \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:

R = \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 coordinates to the original Lab xyz coordinates for a clockwise/lefthand rotation) corresponding to a post-multiply Body 3-2-1 sequence with Euler angles (ψ, θ, φ) is given by:[1]


\begin{align}
\begin{bmatrix}
x & y & z
\end{bmatrix}
& =
\begin{bmatrix}
X & Y & Z
\end{bmatrix}
R_z(\psi) R_y(\theta) R_x(\phi) \\
& = 
\begin{bmatrix}
X & Y & Z
\end{bmatrix}
\begin{bmatrix}
\cos\psi & -\sin\psi & 0 \\
\sin\psi & \cos\psi & 0 \\
0 & 0 & 1 \\
\end{bmatrix}
\begin{bmatrix}
\cos\theta & 0 & \sin\theta \\
0 & 1 & 0 \\
-\sin\theta & 0 & \cos\theta \\
\end{bmatrix}
\begin{bmatrix}
1 & 0 & 0 \\
0 & \cos\phi & -\sin\phi \\
0 & \sin\phi & \cos\phi \\
\end{bmatrix}
\\
& = 
\begin{bmatrix}
X & Y & Z
\end{bmatrix}
\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} \\
\end{align}
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.

Conversion

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 onto 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 (lB = lab2Body):


\begin{align}
\mathbf{q_{lB}} & = 
\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} \\
\end{align}

Other rotation sequences use different conventions.[1]

Euler Angles from Quaternion

The Euler angles can be obtained from the quaternions via the relations:

\begin{bmatrix}
\phi \\ \theta \\ \psi
\end{bmatrix} =
\begin{bmatrix}
\mbox{arctan} \frac {2(q_0 q_1 + q_2 q_3)} {1 - 2(q_1^2 + q_2^2)} \\
\mbox{arcsin} (2(q_0 q_2 - q_3 q_1)) \\
\mbox{arctan} \frac {2(q_0 q_3 + q_1 q_2)} {1 - 2(q_2^2 + q_3^2)}
\end{bmatrix}

Note, however, that the arctan and arcsin functions implemented in computer languages only produce results between −π/2 and π/2, and for three rotations between −π/2 and π/2 one does not obtain all possible orientations. To generate all the orientations one needs to replace the arctan functions in computer code by atan2:

\begin{bmatrix}
\phi \\ \theta \\ \psi
\end{bmatrix} =
\begin{bmatrix}
\mbox{atan2}  (2(q_0 q_1 + q_2 q_3),1 - 2(q_1^2 + q_2^2)) \\
\mbox{arcsin} (2(q_0 q_2 - q_3 q_1)) \\
\mbox{atan2}  (2(q_0 q_3 + q_1 q_2),1 - 2(q_2^2 + q_3^2))
\end{bmatrix}

Relationship with Tait–Bryan angles

Tait–Bryan angles. z-y′-x″ sequence (intrinsic rotations; N coincides with y’). The angle rotation sequence is ψ, θ, Ф. Note that in this case ψ > 90° and θ is a negative angle.

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

where the X-axis points forward, Y-axis to the right and Z-axis downward with angles defined for clockwise/lefthand rotation. In the conversion example above the rotation occurs in the order yaw, pitch, roll (about body-fixed axes).

Singularities

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

Vector Rotation

Note that the canonical way to rotate a three-dimensional vector \vec{v} by a quaternion q defining an Euler rotation is via the formula

\mathbf{p}^{\,\prime} = \mathbf{qpq}^\ast

where \mathbf{p} = (0,\vec{v}) = 0+iv_1+jv_2+kv_3 is a quaternion containing the embedded vector \vec{v}, q^\ast is a conjugate quaternion, and \mathbf{p}^{\,\prime} = (0,\vec{v}^{\,\prime}) is the rotated vector \vec{v}^{\,\prime}. In computational implementations this requires two quaternion multiplications. An alternative approach is to apply the pair of relations

\vec{t} = 2\vec{q} \times \vec{v}
\vec{v}^{\,\prime} = \vec{v} + \frac{1}{2}q_0 \vec{t} + \vec{q} \times \vec{t}

where \times indicates a three-dimensional vector cross product. This involves fewer multiplications and is therefore computationally faster. Numerical tests indicate this latter approach is about 35% faster than the original for vector rotation.

Proof

The general rule for quaternion multiplication involving scalar and vector parts is given by


\begin{align}
\mathbf{q_1q_2} & = (r_1,\vec{v}_1)(r_2,\vec{v}_2) \\
& = (r_1 r_2 - \vec{v}_1 \cdot \vec{v}_2, r_1 \vec{v}_2 + r_2 \vec{v}_1 + 
\vec{v}_1 \times \vec{v}_2) \\
\end{align}

Using this relation one finds for \mathbf{p} = (0,\vec{v}) that


\begin{align}
\mathbf{p q^\ast} & = (0,\vec{v})(q_0,-\vec{q}) \\
& = (\vec{v} \cdot \vec{q}, q_0 \vec{v} - \vec{v} \times \vec{q}) \\
\end{align}

and upon substitution for the triple product


\begin{align}
\mathbf{q p q^\ast} & = (q_0,\vec{q})(\vec{v} \cdot \vec{q}, q_0 \vec{v} - \vec{v} \times \vec{q}) \\
& = (0, q_0^2 \vec{v} + q_0 \vec{q} \times \vec{v} + (\vec{v} \cdot \vec{q}) \vec{q} +
\vec{q}\times\vec{q}\times\vec{v} ) \\
\end{align}

where anti-commutivity of cross product and \vec{q}\cdot \vec{v} \times \vec{q} = 0 has been applied. By next exploiting the property that \mathbf{q} is a unit quaternion so that q_0^2 = 1 - \vec{q}\cdot\vec{q}, along with the standard vector identity


\vec{q}\times\vec{q}\times\vec{v} = (\vec{q}\cdot\vec{v})\vec{q} - (\vec{q}\cdot\vec{q})\vec{v}

one obtains


\begin{align}
\mathbf{p}^\prime & = \mathbf{q p q^\ast} = (0, \vec{v} + q_0 \vec{q} \times \vec{v} + 
2\vec{q}\times\vec{q}\times\vec{v} ) \\
\end{align}

which upon defining \vec{t} = 2\vec{q} \times \vec{v} can be written in terms of scalar and vector parts as


(0, \vec{v}^{\,\prime}) = (0, \vec{v} + \frac{1}{2}q_0 \vec{t} + \vec{q} \times \vec{t} ).

See also

References

  1. 1 2 NASA Mission Planning and Analysis Division. "Euler Angles, Quaternions, and Transformation Matrices" (PDF). NASA. Retrieved 12 January 2013.

External links

This article is issued from Wikipedia - version of the Wednesday, March 30, 2016. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.