This is an old revision of this page, as edited by R0uge(talk | contribs) at 16:14, 9 January 2023(Undid revision 1128376317 by Lambiam (talk) passive vs active rotations is a technical term, and quaternion definitions across wikipedia are inconsistent. This needs to be called out). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Revision as of 16:14, 9 January 2023 by R0uge(talk | contribs)(Undid revision 1128376317 by Lambiam (talk) passive vs active rotations is a technical term, and quaternion definitions across wikipedia are inconsistent. This needs to be called out)
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
For the rest of this article, the "passive" JPLquaternion convention[1] shall be used. A unit quaternion can be described as:
We can associate a quaternion with a rotation around an axis by the following expression
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).
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.
If 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]
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):
Other rotation sequences use different conventions.[2]
Source code
Below code in C++ illustrates above conversion:
structQuaternion{doublew,x,y,z;};QuaternionToQuaternion(doubleroll,doublepitch,doubleyaw)// roll (x), pitch (Y), yaw (z){// Abbreviations for the various angular functionsdoublecr=cos(roll*0.5);doublesr=sin(roll*0.5);doublecp=cos(pitch*0.5);doublesp=sin(pitch*0.5);doublecy=cos(yaw*0.5);doublesy=sin(yaw*0.5);Quaternionq;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;returnq;}
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.[3] 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:
However the arctan functions implemented in computer languages only produce results between −π/2 and π/2, to generate all the orientations one needs to replace the arctan functions in computer code by atan2:
Moreover, typical implementations of arctan also might have some numerical disadvantages near zero and one. Some implementations use the equivalent expression:[4]
Source code
The following C++ program illustrates conversion above:
#define _USE_MATH_DEFINES#include<cmath>structQuaternion{doublew,x,y,z;};structEulerAngles{doubleroll,pitch,yaw;};// this implementation assumes normalized quaternion// converts to Euler angles in 3-2-1 sequenceEulerAnglesToEulerAngles(Quaternionq){EulerAnglesangles;// roll (x-axis rotation)doublesinr_cosp=2*(q.w*q.x+q.y*q.z);doublecosr_cosp=1-2*(q.x*q.x+q.y*q.y);angles.roll=std::atan2(sinr_cosp,cosr_cosp);// pitch (y-axis rotation)doublesinp=std::sqrt(1+2*(q.w*q.y-q.x*q.z));doublecosp=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)doublesiny_cosp=2*(q.w*q.z+q.x*q.y);doublecosy_cosp=1-2*(q.y*q.y+q.z*q.z);angles.yaw=std::atan2(siny_cosp,cosy_cosp);returnangles;}
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.
Note that the canonical way to rotate a three-dimensional vector by a quaternion defining an Euler rotation is via the formula
where is a quaternion containing the embedded vector , is a conjugate quaternion, and is the rotated vector . In computational implementations this requires two quaternion multiplications. An alternative approach is to apply the pair of relations
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% [5] faster than the original for vector rotation.
Proof
The general rule for quaternion multiplication involving scalar and vector parts is given by
Using this relation one finds for that
and upon substitution for the triple product
where anti-commutivity of cross product and has been applied. By next exploiting the property that is a unit quaternion so that , along with the standard vector identity
one obtains
which upon defining can be written in terms of scalar and vector parts as
^Blanco, Jose-Luis (2010). "A tutorial on se (3) transformation parameterizations and on-manifold optimization". University of Malaga, Tech. Rep. CiteSeerX10.1.1.468.5407.