Conversion between quaternions and Euler angles

Short description: Mathematical strategy

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".

## Contents

* 1 Definition
* 2 Intuition
* 2.1 Tait–Bryan angles
* 3 Rotation matrices
* 4 Euler angles (in 3-2-1 sequence) to quaternion conversion
* 4.1 Source code
* 5 Quaternion to Euler angles (in 3-2-1 sequence) conversion
* 5.1 Source code
* 6 Singularities
* 7 Vector rotation
* 7.1 Proof
* 8 See also
* 9 References
* 10 External links

## Definition

There are two representations of quaternions. This article uses the more popular Hamilton.

A quaternion has 4 real values: qw (the real part or the scalar part) and qx qy qz (the imaginary part).

Defining the norm of the quaternion as follows: ‖q‖=qw2+qx2+qy2+qz2

A unit quaternion satisfies: ‖q‖=1

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

𝐪w=cos⁡(α/2)
𝐪x=sin⁡(α/2)cos⁡(βx)
𝐪y=sin⁡(α/2)cos⁡(βy)
𝐪z=sin⁡(α/2)cos⁡(β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" of the angles between the three coordinate axes and the axis of rotation. (Euler's Rotation Theorem).

## Intuition

To better understand how "direction cosines" work with quaternions:

𝐪w=cos⁡(rotation angle/2)𝐪x=sin⁡(rotation angle/2)cos⁡(angle between axis of rotation and x axis)𝐪y=sin⁡(rotation angle/2)cos⁡(angle between axis of rotation and y axis)𝐪z=sin⁡(rotation angle/2)cos⁡(angle between axis of rotation and z axis)

If the axis of rotation is the x-axis:

𝐪w=cos⁡(α/2)𝐪x=sin⁡(α/2)⋅1𝐪y=sin⁡(α/2)⋅0𝐪z=sin⁡(α/2)⋅0

If the axis of rotation is the y-axis:

𝐪w=cos⁡(α/2)𝐪x=sin⁡(α/2)⋅0𝐪y=sin⁡(α/2)⋅1𝐪z=sin⁡(α/2)⋅0

If the axis of rotation is the z-axis:

𝐪w=cos⁡(α/2)𝐪x=sin⁡(α/2)⋅0𝐪y=sin⁡(α/2)⋅0𝐪z=sin⁡(α/2)⋅1

If the axis of rotation is a vector located 45° (⁠π/4⁠ radians) between the x and y axes:

𝐪w=cos⁡(α/2)𝐪x=sin⁡(α/2)⋅0.707…𝐪y=sin⁡(α/2)⋅0.707…𝐪z=sin⁡(α/2)⋅0

Therefore, the x and y axes "share" influence over the new axis of rotation.

### 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):

* Heading – ψ: rotation about the Z-axis
* Pitch – θ: rotation about the new Y-axis
* Bank – ϕ: rotation about the new X-axis

where the X-axis points forward, Y-axis to the right and Z-axis downward. In the conversion example above the rotation occurs in the order heading, pitch, bank.

## 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=qw+iqx+jqy+kqz is given by the inhomogeneous expression:

R=[1−2(qy2+qz2)2(qxqy−qwqz)2(qwqy+qxqz)2(qxqy+qwqz)1−2(qx2+qz2)2(qyqz−qwqx)2(qxqz−qwqy)2(qwqx+qyqz)1−2(qx2+qy2)]

or equivalently, by the homogeneous expression:

R=[qw2+qx2−qy2−qz22(qxqy−qwqz)2(qwqy+qxqz)2(qxqy+qwqz)qw2−qx2+qy2−qz22(qyqz−qwqx)2(qxqz−qwqy)2(qwqx+qyqz)qw2−qx2−qy2+qz2]

If qw+iqx+jqy+kqz 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]

[xyz]=Rz(ψ)Ry(θ)Rx(ϕ)[XYZ]=[cos⁡ψ−sin⁡ψ0sin⁡ψcos⁡ψ0001][cos⁡θ0sin⁡θ010−sin⁡θ0cos⁡θ][1000cos⁡ϕ−sin⁡ϕ0sin⁡ϕcos⁡ϕ][XYZ]=[cos⁡θcos⁡ψ−cos⁡ϕsin⁡ψ+sin⁡ϕsin⁡θcos⁡ψsin⁡ϕsin⁡ψ+cos⁡ϕsin⁡θcos⁡ψcos⁡θsin⁡ψcos⁡ϕcos⁡ψ+sin⁡ϕsin⁡θsin⁡ψ−sin⁡ϕcos⁡ψ+cos⁡ϕsin⁡θsin⁡ψ−sin⁡θsin⁡ϕcos⁡θcos⁡ϕcos⁡θ][XYZ]
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.

## Euler angles (in 3-2-1 sequence) to quaternion 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):

q𝐥𝐁=[cos⁡(ψ/2)00sin⁡(ψ/2)][cos⁡(θ/2)0sin⁡(θ/2)0][cos⁡(ϕ/2)sin⁡(ϕ/2)00]=[cos⁡(ϕ/2)cos⁡(θ/2)cos⁡(ψ/2)+sin⁡(ϕ/2)sin⁡(θ/2)sin⁡(ψ/2)sin⁡(ϕ/2)cos⁡(θ/2)cos⁡(ψ/2)−cos⁡(ϕ/2)sin⁡(θ/2)sin⁡(ψ/2)cos⁡(ϕ/2)sin⁡(θ/2)cos⁡(ψ/2)+sin⁡(ϕ/2)cos⁡(θ/2)sin⁡(ψ/2)cos⁡(ϕ/2)cos⁡(θ/2)sin⁡(ψ/2)−sin⁡(ϕ/2)sin⁡(θ/2)cos⁡(ψ/2)]

Other rotation sequences use different conventions.[1]

### Source code

Below code in C++ illustrates above conversion:

struct Quaternion

double w, x, y, z;

// This is not in game format, it is in mathematical format.
Quaternion ToQuaternion(double roll, double pitch, double yaw) // roll (x), pitch (y), yaw (z), angles are in radians

// Abbreviations for the various angular functions

double cr = cos(roll * 0.5);
double sr = sin(roll * 0.5);
double cp = cos(pitch * 0.5);
double sp = sin(pitch * 0.5);
double cy = cos(yaw * 0.5);
double sy = sin(yaw * 0.5);

Quaternion q;
q.w = cr * cp * cy + sr * sp * sy;
q.x = sr * cp * cy - cr * sp * sy;
q.y = cr * sp * cy + sr * cp * sy;
q.z = cr * cp * sy - sr * sp * cy;

return q;

## Quaternion to Euler angles (in 3-2-1 sequence) conversion

A direct formula for the conversion from a quaternion to Euler angles in any of the 12 possible sequences exists.[2] For the rest of this section, the formula for the sequence Body 3-2-1 will be shown. If the quaternion is properly normalized, the Euler angles can be obtained from the quaternions via the relations:

[ϕθψ]=[atan2(2(qwqx+qyqz),1−2(qx2+qy2))−π/2+2atan2(1+2(qwqy−qxqz),1−2(qwqy−qxqz))atan2(2(qwqz+qxqy),1−2(qy2+qz2))]

Note that the arctan functions implemented in computer languages only produce results between −π/2 and π/2, which is why atan2 is used to generate all the correct orientations. Moreover, typical implementations of arctan also might have some numerical disadvantages near zero and one.

Some implementations use the equivalent expression:[3]

θ=arcsin(2(qwqy−qxqz))

### Source code

The following C++ program illustrates conversion above:

#define _USE_MATH_DEFINES
#include <cmath>

struct Quaternion {
double w, x, y, z;

struct EulerAngles {
double roll, pitch, yaw;

// this implementation assumes normalized quaternion
// converts to Euler angles in 3-2-1 sequence
EulerAngles ToEulerAngles(Quaternion q) {
EulerAngles angles;

// roll (x-axis rotation)
double sinr_cosp = 2 * (q.w * q.x + q.y * q.z);
double cosr_cosp = 1 - 2 * (q.x * q.x + q.y * q.y);
angles.roll = std::atan2(sinr_cosp, cosr_cosp);

// pitch (y-axis rotation)
double sinp = std::sqrt(1 + 2 * (q.w * q.y - q.x * q.z));
double cosp = std::sqrt(1 - 2 * (q.w * q.y - q.x * q.z));
angles.pitch = 2 * std::atan2(sinp, cosp) - M_PI / 2;

// yaw (z-axis rotation)
double siny_cosp = 2 * (q.w * q.z + q.x * q.y);
double cosy_cosp = 1 - 2 * (q.y * q.y + q.z * q.z);
angles.yaw = std::atan2(siny_cosp, cosy_cosp);

return angles;

## 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

Let us define scalar qw and vector q→ such that quaternion 𝐪=(qw,q→).

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

𝐯′=𝐪𝐯𝐪∗

where 𝐯=(0,v→) is a quaternion containing the embedded vector v→, 𝐪∗=(qw,−q→) is a conjugate quaternion, and 𝐯′=(0,v→′) is the rotated vector v→′. In computational implementations this requires two quaternion multiplications. An alternative approach is to apply the pair of relations

t→=2q→×v→
v→′=v→+qwt→+q→×t→

where × indicates a three-dimensional vector cross product. This involves fewer multiplications and is therefore computationally faster. Numerical tests indicate this latter approach may be up to 30% [4] faster than the original for vector rotation.

### Proof

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

𝐩𝐪=(pw,p→)(qw,q→)=(pwqw−p→⋅q→,pwq→+qwp→+p→×q→)

Using this relation one finds for 𝐯=(0,v→) that

𝐯𝐪∗=(0,v→)(qw,−q→)=(v→⋅q→,qwv→−v→×q→)

and upon substitution for the triple product

𝐪𝐯𝐪∗=(qw,q→)(v→⋅q→,qwv→−v→×q→)=(0,qw2v→+qwq→×v→+(v→⋅q→)q→+qwq→×v→+q→×(q→×v→))

where anti-commutivity of cross product and q→⋅v→×q→=0 has been applied. By next exploiting the property that 𝐪 is a unit quaternion so that qw2=1−q→⋅q→, along with the standard vector identity

q→×(q→×v→)=(q→⋅v→)q→−(q→⋅q→)v→

one obtains

𝐯′=𝐪𝐯𝐪∗=(0,v→+2qwq→×v→+2q→×(q→×v→))

which upon defining t→=2q→×v→ can be written in terms of scalar and vector parts as

(0,v→′)=(0,v→+qwt→+q→×t→).

## See also

* Rotation operator (vector space)
* Quaternions and spatial rotation
* Euler Angles
* Rotation matrix
* Rotation formalisms in three dimensions

## References

1. ↑ 1.0 1.1 NASA Mission Planning and Analysis Division (July 1977). "Euler Angles, Quaternions, and Transformation Matrices". NASA. https://ntrs.nasa.gov/citations/19770024290.
2. ↑ Bernardes, Evandro; Viollet, Stéphane (10 November 2022). "Quaternion to Euler angles conversion: A direct, general and computationally efficient method" (in en). PLOS ONE 17 (11). doi:10.1371/journal.pone.0276302. ISSN 1932-6203. PMID 36355707. Bibcode: 2022PLoSO..1776302B.
3. ↑ Blanco, Jose-Luis (2010). "A tutorial on se (3) transformation parameterizations and on-manifold optimization". University of Malaga, Tech. Rep.
4. ↑ Janota, A; Šimák, V; Nemec, D; Hrbček, J (2015). "Improving the Precision and Speed of Euler Angles Computation from Low-Cost Rotation Sensor Data". Sensors 15 (3): 7016–7039. doi:10.3390/s150307016. PMID 25806874. Bibcode: 2015Senso..15.7016J.

## External links

* Q60. How do I convert Euler rotation angles to a quaternion? and related questions at The Matrix and Quaternions FAQ

0.00

(0 votes)

Original source: https://en.wikipedia.org/wiki/Conversion between quaternions and Euler angles. Read more

|

Retrieved from "https://handwiki.org/wiki/index.php?title=Conversion_between_quaternions_and_Euler_angles&oldid=4150299"