Aller au contenu principal

Conversion between quaternions and Euler angles


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

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

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

Defining the norm of the quaternion as follows:

A unit quaternion satisfies:

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

q w = cos ( α / 2 ) {\displaystyle \mathbf {q} _{w}=\cos(\alpha /2)}
q x = sin ( α / 2 ) cos ( β x ) {\displaystyle \mathbf {q} _{x}=\sin(\alpha /2)\cos(\beta _{x})}
q y = sin ( α / 2 ) cos ( β y ) {\displaystyle \mathbf {q} _{y}=\sin(\alpha /2)\cos(\beta _{y})}
q z = sin ( α / 2 ) cos ( β z ) {\displaystyle \mathbf {q} _{z}=\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" 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:

q w = cos ( rotation angle / 2 ) q x = sin ( rotation angle / 2 ) cos ( angle between axis of rotation and x axis ) q y = sin ( rotation angle / 2 ) cos ( angle between axis of rotation and y axis ) q z = sin ( rotation angle / 2 ) cos ( angle between axis of rotation and z axis ) {\displaystyle {\begin{array}{lcr}\mathbf {q} _{w}=\cos({\text{rotation angle}}/2)\\\mathbf {q} _{x}=\sin({\text{rotation angle}}/2)\cos({\text{angle between axis of rotation and x axis}})\\\mathbf {q} _{y}=\sin({\text{rotation angle}}/2)\cos({\text{angle between axis of rotation and y axis}})\\\mathbf {q} _{z}=\sin({\text{rotation angle}}/2)\cos({\text{angle between axis of rotation and z axis}})\end{array}}}

If the axis of rotation is the x-axis:

q w = cos ( α / 2 ) q x = sin ( α / 2 ) 1 q y = sin ( α / 2 ) 0 q z = sin ( α / 2 ) 0 {\displaystyle {\begin{array}{lcr}\mathbf {q} _{w}=\cos(\alpha /2)\\\mathbf {q} _{x}=\sin(\alpha /2)\cdot 1\\\mathbf {q} _{y}=\sin(\alpha /2)\cdot 0\\\mathbf {q} _{z}=\sin(\alpha /2)\cdot 0\end{array}}}

If the axis of rotation is the y-axis:

q w = cos ( α / 2 ) q x = sin ( α / 2 ) 0 q y = sin ( α / 2 ) 1 q z = sin ( α / 2 ) 0 {\displaystyle {\begin{array}{lcr}\mathbf {q} _{w}=\cos(\alpha /2)\\\mathbf {q} _{x}=\sin(\alpha /2)\cdot 0\\\mathbf {q} _{y}=\sin(\alpha /2)\cdot 1\\\mathbf {q} _{z}=\sin(\alpha /2)\cdot 0\end{array}}}

If the axis of rotation is the z-axis:

q w = cos ( α / 2 ) q x = sin ( α / 2 ) 0 q y = sin ( α / 2 ) 0 q z = sin ( α / 2 ) 1 {\displaystyle {\begin{array}{lcr}\mathbf {q} _{w}=\cos(\alpha /2)\\\mathbf {q} _{x}=\sin(\alpha /2)\cdot 0\\\mathbf {q} _{y}=\sin(\alpha /2)\cdot 0\\\mathbf {q} _{z}=\sin(\alpha /2)\cdot 1\end{array}}}

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

q w = cos ( α / 2 ) q x = sin ( α / 2 ) 0.707 q y = sin ( α / 2 ) 0.707 q z = sin ( α / 2 ) 0 {\displaystyle {\begin{array}{lcr}\mathbf {q} _{w}=\cos(\alpha /2)\\\mathbf {q} _{x}=\sin(\alpha /2)\cdot 0.707\ldots \\\mathbf {q} _{y}=\sin(\alpha /2)\cdot 0.707\ldots \\\mathbf {q} _{z}=\sin(\alpha /2)\cdot 0\end{array}}}

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

Tait–Bryan angles

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

  • Heading – ψ {\displaystyle \psi } : rotation about the Z-axis
  • Pitch – θ {\displaystyle \theta } : rotation about the new Y-axis
  • Bank – ϕ {\displaystyle \phi } : 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 = q w + i q x + j q y + k q z {\displaystyle q=q_{w}+iq_{x}+jq_{y}+kq_{z}} is given by the inhomogeneous expression:

R = [ 1 2 ( q y 2 + q z 2 ) 2 ( q x q y q w q z ) 2 ( q w q y + q x q z ) 2 ( q x q y + q w q z ) 1 2 ( q x 2 + q z 2 ) 2 ( q y q z q w q x ) 2 ( q x q z q w q y ) 2 ( q w q x + q y q z ) 1 2 ( q x 2 + q y 2 ) ] {\displaystyle R={\begin{bmatrix}1-2(q_{y}^{2}+q_{z}^{2})&2(q_{x}q_{y}-q_{w}q_{z})&2(q_{w}q_{y}+q_{x}q_{z})\\2(q_{x}q_{y}+q_{w}q_{z})&1-2(q_{x}^{2}+q_{z}^{2})&2(q_{y}q_{z}-q_{w}q_{x})\\2(q_{x}q_{z}-q_{w}q_{y})&2(q_{w}q_{x}+q_{y}q_{z})&1-2(q_{x}^{2}+q_{y}^{2})\end{bmatrix}}}

or equivalently, by the homogeneous expression:

R = [ q w 2 + q x 2 q y 2 q z 2 2 ( q x q y q w q z ) 2 ( q w q y + q x q z ) 2 ( q x q y + q w q z ) q w 2 q x 2 + q y 2 q z 2 2 ( q y q z q w q x ) 2 ( q x q z q w q y ) 2 ( q w q x + q y q z ) q w 2 q x 2 q y 2 + q z 2 ] {\displaystyle R={\begin{bmatrix}q_{w}^{2}+q_{x}^{2}-q_{y}^{2}-q_{z}^{2}&2(q_{x}q_{y}-q_{w}q_{z})&2(q_{w}q_{y}+q_{x}q_{z})\\2(q_{x}q_{y}+q_{w}q_{z})&q_{w}^{2}-q_{x}^{2}+q_{y}^{2}-q_{z}^{2}&2(q_{y}q_{z}-q_{w}q_{x})\\2(q_{x}q_{z}-q_{w}q_{y})&2(q_{w}q_{x}+q_{y}q_{z})&q_{w}^{2}-q_{x}^{2}-q_{y}^{2}+q_{z}^{2}\end{bmatrix}}}

If q w + i q x + j q y + k q z {\displaystyle q_{w}+iq_{x}+jq_{y}+kq_{z}} 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:

[ x y z ] = R z ( ψ ) R y ( θ ) R x ( ϕ ) [ X Y Z ] = [ cos ψ sin ψ 0 sin ψ cos ψ 0 0 0 1 ] [ cos θ 0 sin θ 0 1 0 sin θ 0 cos θ ] [ 1 0 0 0 cos ϕ sin ϕ 0 sin ϕ cos ϕ ] [ X Y Z ] = [ 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 θ ] [ X Y Z ] {\displaystyle {\begin{aligned}{\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}}{\begin{bmatrix}X\\Y\\Z\\\end{bmatrix}}\\\end{aligned}}}

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 l B = [ cos ( ψ / 2 ) 0 0 sin ( ψ / 2 ) ] [ cos ( θ / 2 ) 0 sin ( θ / 2 ) 0 ] [ cos ( ϕ / 2 ) sin ( ϕ / 2 ) 0 0 ] = [ 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 ) ] {\displaystyle {\begin{aligned}\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{aligned}}}

Other rotation sequences use different conventions.

Source code

Below code in C++ illustrates above conversion:

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. 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 ( q w q x + q y q z ) , 1 2 ( q x 2 + q y 2 ) ) π / 2 + 2 atan2 ( 1 + 2 ( q w q y q x q z ) , 1 2 ( q w q y q x q z ) ) atan2 ( 2 ( q w q z + q x q y ) , 1 2 ( q y 2 + q z 2 ) ) ] {\displaystyle {\begin{bmatrix}\phi \\\theta \\\psi \end{bmatrix}}={\begin{bmatrix}{\mbox{atan2}}\left(2(q_{w}q_{x}+q_{y}q_{z}),1-2(q_{x}^{2}+q_{y}^{2})\right)\\-\pi /2+2\,{\mbox{atan2}}\left({\sqrt {1+2(q_{w}q_{y}-q_{x}q_{z})}},{\sqrt {1-2(q_{w}q_{y}-q_{x}q_{z})}}\right)\\{\mbox{atan2}}\left(2(q_{w}q_{z}+q_{x}q_{y}),1-2(q_{y}^{2}+q_{z}^{2})\right)\end{bmatrix}}}

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:

θ = arcsin ( 2 ( q w q y q x q z ) ) {\displaystyle \theta ={\mbox{arcsin}}(2(q_{w}q_{y}-q_{x}q_{z}))}

Source code

The following C++ program illustrates conversion above:

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 q w {\displaystyle q_{w}} and vector q {\displaystyle {\vec {q}}} such that quaternion q = ( q w , q ) {\displaystyle \mathbf {q} =(q_{w},{\vec {q}})} .

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

v = q v q {\displaystyle \mathbf {v} ^{\,\prime }=\mathbf {qvq} ^{\ast }}

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

t = 2 q × v {\displaystyle {\vec {t}}=2{\vec {q}}\times {\vec {v}}}
v = v + q w t + q × t {\displaystyle {\vec {v}}^{\,\prime }={\vec {v}}+q_{w}{\vec {t}}+{\vec {q}}\times {\vec {t}}}

where × {\displaystyle \times } 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% faster than the original for vector rotation.

Proof

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

p q = ( p w , p ) ( q w , q ) = ( p w q w p q , p w q + q w p + p × q ) {\displaystyle {\begin{aligned}\mathbf {pq} &=(p_{w},{\vec {p}})(q_{w},{\vec {q}})\\&=(p_{w}q_{w}-{\vec {p}}\cdot {\vec {q}},p_{w}{\vec {q}}+q_{w}{\vec {p}}+{\vec {p}}\times {\vec {q}})\\\end{aligned}}}

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

v q = ( 0 , v ) ( q w , q ) = ( v q , q w v v × q ) {\displaystyle {\begin{aligned}\mathbf {vq^{\ast }} &=(0,{\vec {v}})(q_{w},-{\vec {q}})\\&=({\vec {v}}\cdot {\vec {q}},q_{w}{\vec {v}}-{\vec {v}}\times {\vec {q}})\\\end{aligned}}}

and upon substitution for the triple product

q v q = ( q w , q ) ( v q , q w v v × q ) = ( 0 , q w 2 v + q w q × v + ( v q ) q + q w q × v + q × ( q × v ) ) {\displaystyle {\begin{aligned}\mathbf {qvq^{\ast }} &=(q_{w},{\vec {q}})({\vec {v}}\cdot {\vec {q}},q_{w}{\vec {v}}-{\vec {v}}\times {\vec {q}})\\&=(0,q_{w}^{2}{\vec {v}}+q_{w}{\vec {q}}\times {\vec {v}}+({\vec {v}}\cdot {\vec {q}}){\vec {q}}+q_{w}{\vec {q}}\times {\vec {v}}+{\vec {q}}\times ({\vec {q}}\times {\vec {v}}))\\\end{aligned}}}

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

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

one obtains

v = q v q = ( 0 , v + 2 q w q × v + 2 q × ( q × v ) ) {\displaystyle {\begin{aligned}\mathbf {v} ^{\prime }&=\mathbf {qvq^{\ast }} =(0,{\vec {v}}+2q_{w}{\vec {q}}\times {\vec {v}}+2{\vec {q}}\times ({\vec {q}}\times {\vec {v}}))\\\end{aligned}}}

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

( 0 , v ) = ( 0 , v + q w t + q × t ) . {\displaystyle (0,{\vec {v}}^{\,\prime })=(0,{\vec {v}}+q_{w}{\vec {t}}+{\vec {q}}\times {\vec {t}}).}

See also

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

References

External links

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

Text submitted to CC-BY-SA license. Source: Conversion between quaternions and Euler angles by Wikipedia (Historical)


ghbass