https://en.wikipedia.org/wiki/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".
Contents
[hide]Definition[edit]
For the rest of this article, the JPL quaternion convention[1] shall be used. A unit quaternion can be described as:
-
q = [ q 0 q 1 q 2 q 3 ] T = [ q w q x q y q z ] T {\displaystyle \mathbf {q} ={\begin{bmatrix}q_{0}&q_{1}&q_{2}&q_{3}\end{bmatrix}}^{T}={\begin{bmatrix}q_{w}&q_{x}&q_{y}&q_{z}\end{bmatrix}}^{T}}
-
| q | 2 = q 0 2 + q 1 2 + q 2 2 + q 3 2 = q w 2 + q x 2 + q y 2 + q z 2 = 1 {\displaystyle |\mathbf {q} |^{2}=q_{0}^{2}+q_{1}^{2}+q_{2}^{2}+q_{3}^{2}=q_{w}^{2}+q_{x}^{2}+q_{y}^{2}+q_{z}^{2}=1}
We can associate a quaternion with a rotation around an axis by the following expression
-
q 0 = q w = cos ( α / 2 ) {\displaystyle \mathbf {q} _{0}=\mathbf {q} _{w}=\cos(\alpha /2)}
-
q 1 = q x = sin ( α / 2 ) cos ( β x ) {\displaystyle \mathbf {q} _{1}=\mathbf {q} _{x}=\sin(\alpha /2)\cos(\beta _{x})}
-
q 2 = q y = sin ( α / 2 ) cos ( β y ) {\displaystyle \mathbf {q} _{2}=\mathbf {q} _{y}=\sin(\alpha /2)\cos(\beta _{y})}
-
q 3 = q z = sin ( α / 2 ) cos ( β z ) {\displaystyle \mathbf {q} _{3}=\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" locating the axis of rotation (Euler's Theorem).
Tait–Bryan angles[edit]
Similarly for Euler angles, we use the Tait Bryan angles (in terms of flight dynamics):
- Bank – ϕ {\displaystyle \phi }
: rotation about the new X-axis
- Attitude – θ {\displaystyle \theta }
: rotation about the new Y-axis
- Heading – ψ {\displaystyle \psi }
: rotation about the Z-axis
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 heading, attitude, bank (about intrinsic axes).
Rotation matrices[edit]
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 + i q 1 + j q 2 + k q 3 {\displaystyle q=q_{0}+iq_{1}+jq_{2}+kq_{3}} is given by the inhomogeneous expression:
-
R = [ 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 ) ] {\displaystyle 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 = [ 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 ] {\displaystyle 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 + i q 1 + j q 2 + k q 3 {\displaystyle 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:[2]
-
[ 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 to Quaternion 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 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.[2]
Source Code[edit]
Below code in C++ illustrates above conversion:
Quaterniond toQuaternion(double pitch, double roll, double yaw)
{
Quaterniond q;
// Abbreviations for the various angular functions
double cy = cos(yaw * 0.5);
double sy = sin(yaw * 0.5);
double cr = cos(roll * 0.5);
double sr = sin(roll * 0.5);
double cp = cos(pitch * 0.5);
double sp = sin(pitch * 0.5);
q.w() = cy * cr * cp + sy * sr * sp;
q.x() = cy * sr * cp - sy * cr * sp;
q.y() = cy * cr * sp + sy * sr * cp;
q.z() = sy * cr * cp - cy * sr * sp;
return q;
}
Quaternion to Euler Angles Conversion[edit]
The Euler angles can be obtained from the quaternions via the relations:[3]
-
[ ϕ θ ψ ] = [ arctan 2 ( q 0 q 1 + q 2 q 3 ) 1 − 2 ( q 1 2 + q 2 2 ) arcsin ( 2 ( q 0 q 2 − q 3 q 1 ) ) arctan 2 ( q 0 q 3 + q 1 q 2 ) 1 − 2 ( q 2 2 + q 3 2 ) ] {\displaystyle {\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:
-
[ ϕ θ ψ ] = [ atan2 ( 2 ( q 0 q 1 + q 2 q 3 ) , 1 − 2 ( q 1 2 + q 2 2 ) ) asin ( 2 ( q 0 q 2 − q 3 q 1 ) ) atan2 ( 2 ( q 0 q 3 + q 1 q 2 ) , 1 − 2 ( q 2 2 + q 3 2 ) ) ] {\displaystyle {\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{asin}}(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}}}
Source Code[edit]
The following C++ program illustrates conversion above:
static void toEulerAngle(const Quaterniond& q, double& roll, double& pitch, double& yaw)
{
// roll (x-axis rotation)
double sinr = +2.0 * (q.w() * q.x() + q.y() * q.z());
double cosr = +1.0 - 2.0 * (q.x() * q.x() + q.y() * q.y());
roll = atan2(sinr, cosr);
// pitch (y-axis rotation)
double sinp = +2.0 * (q.w() * q.y() - q.z() * q.x());
if (fabs(sinp) >= 1)
pitch = copysign(M_PI / 2, sinp); // use 90 degrees if out of range
else
pitch = asin(sinp);
// yaw (z-axis rotation)
double siny = +2.0 * (q.w() * q.z() + q.x() * q.y());
double cosy = +1.0 - 2.0 * (q.y() * q.y() + q.z() * q.z());
yaw = atan2(siny, cosy);
}
The following Python program illustrates conversion above:
import math
def quaternion_to_euler_angle(w, x, y, z):
ysqr = y * y
t0 = +2.0 * (w * x + y * z)
t1 = +1.0 - 2.0 * (x * x + ysqr)
X = math.degrees(math.atan2(t0, t1))
t2 = +2.0 * (w * y - z * x)
t2 = +1.0 if t2 > +1.0 else t2
t2 = -1.0 if t2 < -1.0 else t2
Y = math.degrees(math.asin(t2))
t3 = +2.0 * (w * z + x * y)
t4 = +1.0 - 2.0 * (ysqr + z * z)
Z = math.degrees(math.atan2(t3, t4))
return X, Y, Z
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
Vector Rotation[edit]
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
-
p ′ = q p q ∗ {\displaystyle \mathbf {p} ^{\,\prime }=\mathbf {qpq} ^{\ast }}
where p = ( 0 , v → ) = 0 + i v 1 + j v 2 + k v 3 {\displaystyle \mathbf {p} =(0,{\vec {v}})=0+iv_{1}+jv_{2}+kv_{3}} is a quaternion containing the embedded vector v → {\displaystyle {\vec {v}}}
, q ∗ {\displaystyle q^{\ast }}
is a conjugate quaternion, and p ′ = ( 0 , v → ′ ) {\displaystyle \mathbf {p} ^{\,\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 0 t → + q → × t → {\displaystyle {\vec {v}}^{\,\prime }={\vec {v}}+q_{0}{\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% [4] faster than the original for vector rotation.
Proof[edit]
The general rule for quaternion multiplication involving scalar and vector parts is given by
-
q 1 q 2 = ( r 1 , v → 1 ) ( r 2 , v → 2 ) = ( r 1 r 2 − v → 1 ⋅ v → 2 , r 1 v → 2 + r 2 v → 1 + v → 1 × v → 2 ) {\displaystyle {\begin{aligned}\mathbf {q_{1}q_{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{aligned}}}
Using this relation one finds for p = ( 0 , v → ) {\displaystyle \mathbf {p} =(0,{\vec {v}})} that
-
p q ∗ = ( 0 , v → ) ( q 0 , − q → ) = ( v → ⋅ q → , q 0 v → − v → × q → ) {\displaystyle {\begin{aligned}\mathbf {pq^{\ast }} &=(0,{\vec {v}})(q_{0},-{\vec {q}})\\&=({\vec {v}}\cdot {\vec {q}},q_{0}{\vec {v}}-{\vec {v}}\times {\vec {q}})\\\end{aligned}}}
and upon substitution for the triple product
-
q p q ∗ = ( q 0 , q → ) ( v → ⋅ q → , q 0 v → − v → × q → ) = ( 0 , q 0 2 v → + q 0 q → × v → + ( v → ⋅ q → ) q → + q 0 q → × v → + q → × ( q → × v → ) ) {\displaystyle {\begin{aligned}\mathbf {qpq^{\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}}+q_{0}{\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 0 2 = 1 − q → ⋅ q → {\displaystyle q_{0}^{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
-
p ′ = q p q ∗ = ( 0 , v → + 2 q 0 q → × v → + 2 q → × ( q → × v → ) ) {\displaystyle {\begin{aligned}\mathbf {p} ^{\prime }&=\mathbf {qpq^{\ast }} =(0,{\vec {v}}+2q_{0}{\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 0 t → + q → × t → ) . {\displaystyle (0,{\vec {v}}^{\,\prime })=(0,{\vec {v}}+q_{0}{\vec {t}}+{\vec {q}}\times {\vec {t}}).}
See also[edit]
- Rotation operator (vector space)
- Quaternions and spatial rotation
- Euler Angles
- Rotation matrix
- Rotation formalisms in three dimensions
References[edit]
- Jump up ^ W. G. Breckenridge, "Quaternions proposed standard conventions," NASA Jet Propulsion Laboratory, Technical Report, Oct. 1979.
- ^ Jump up to: a b NASA Mission Planning and Analysis Division. "Euler Angles, Quaternions, and Transformation Matrices" (PDF). NASA. Retrieved 12 January 2013.
- Jump up ^ Blanco, Jose-Luis (2010). "A tutorial on se (3) transformation parameterizations and on-manifold optimization". University of Malaga, Tech. Rep.
- Jump up ^ 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. PMC 4435132
.
External links[edit]
- Q60. How do I convert Euler rotation angles to a quaternion? and related questions at The Matrix and Quaternions FAQ