Content deleted Content added
Ebernardes (talk | contribs) |
Link suggestions feature: 3 links added. |
||
(33 intermediate revisions by 16 users not shown) | |||
Line 1:
{{Short description|Mathematical strategy}}
{{cleanup rewrite|date=January 2024}}
[[Rotation formalisms in three dimensions|Spatial rotations in three dimensions]] can be [[Coordinate system|parametrized]] using both [[Euler angles]] and [[Quaternions and spatial rotation|unit quaternions]]. This article explains how to convert between the two representations. Actually this simple use of "quaternions" was first presented by [[Leonhard Euler|Euler]] some seventy years earlier than [[William Rowan Hamilton|Hamilton]] to solve the problem of [[magic square]]s. For this reason the dynamics community commonly refers to quaternions in this application as "Euler parameters".
==Definition==
There are [[Quaternions_and_spatial_rotation#Alternative_conventions|two representations]] of quaternions. This article uses the more popular Hamilton.
A quaternion has 4 real values: {{mvar|q<sub>w</sub>}} (the real part or the scalar part) and {{mvar|q<sub>x</sub> q<sub>y</sub> q<sub>z</sub>}} (the imaginary part).
:<math>|\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</math>▼
Defining the [[Quaternion#Conjugation,_the_norm,_and_reciprocal|norm of the quaternion]] as follows:
▲
A ''unit quaternion'' satisfies:
<math display=block>\lVert q \rVert = 1</math>
We can associate a [[quaternion]] with a rotation around an axis by the following expression
:<math>
:<math>
:<math>
:<math>
where α is a simple rotation angle (the value in radians of the [[angle of rotation]]) and cos(β<sub>''x''</sub>), cos(β<sub>''y''</sub>) and cos(β<sub>''z''</sub>) are the "[[direction cosine]]s" of the angles between the three coordinate axes and the axis of rotation. ([[Euler's rotation theorem|Euler's Rotation Theorem]]).
==Intuition==
To better understand how "[[direction cosine]]s" work with quaternions:
:<math>\begin{array}{lcr}
If the [[axis of rotation]] is the ''x''-axis:
:<math>\begin{array}{lcr}
If the [[axis of rotation]] is the ''y''-axis:
:<math>\begin{array}{lcr}
If the [[axis of rotation]] is the ''z''-axis:
:<math>\begin{array}{lcr}
If the [[axis of rotation]] is a [[Vector_(mathematics_and_physics)|vector]] located 45° ({{sfrac|{{pi}}|4}} radians) between the ''x'' and ''y'' axes:
:<math>\begin{array}{lcr}
Therefore, the ''x'' and ''y'' axes "share" influence over the new [[axis of rotation]].
Line 62 ⟶ 70:
== Rotation matrices ==
The [[orthogonal matrix]] (post-multiplying a [[Row and column vectors|column vector]]) corresponding to a clockwise/[[Right-hand rule|left-handed]] (looking along positive axis to origin) rotation by the unit [[quaternion]] <math>q=
:<math>R = \begin{bmatrix}
1- 2(
2(
2(
\end{bmatrix}</math>
Line 73 ⟶ 81:
:<math>R = \begin{bmatrix}
2(
2(
\end{bmatrix}</math>
If <math>
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:<ref name=nasa-rotation>{{cite web|last=NASA Mission Planning and Analysis Division|title=Euler Angles, Quaternions, and Transformation Matrices|date=July 1977 |url=https://ntrs.nasa.gov/citations/19770024290|publisher=[[NASA]]|accessdate=24 May 2021}}</ref>
Line 135 ⟶ 143:
[[Image:Eulerangles.svg|right|thumb|150px|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'''):
Line 165 ⟶ 173:
};
// This is not in game format, it is in mathematical format.
Quaternion ToQuaternion(double roll, double pitch, double yaw) // roll (x), pitch (
{
// Abbreviations for the various angular functions
Line 186 ⟶ 195:
</syntaxhighlight>
== 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.<ref>{{cite journal |last1=Bernardes |first1=Evandro |last2=Viollet |first2=Stéphane |title=Quaternion to Euler angles conversion: A direct, general and computationally efficient method |journal=PLOS ONE |date=10 November 2022 |volume=17 |issue=11 |pages=e0276302 |doi=10.1371/journal.pone.0276302 |
If the quaternion is properly '''normalized''', the Euler angles can be obtained from the quaternions via the relations:
Line 195 ⟶ 204:
\end{bmatrix} =
\begin{bmatrix}
\mbox{
- \pi/2 + 2 \, \mbox{
\mbox{
\end{bmatrix} </math>
Note
:<math> \theta = \mbox{arcsin} (2(
=== Source code ===
Line 233 ⟶ 231:
// this implementation assumes normalized quaternion
// converts to Euler angles in 3-2-1 sequence
EulerAngles ToEulerAngles(Quaternion
EulerAngles angles;
Line 242 ⟶ 240:
// pitch (y-axis rotation)
double sinp = std::sqrt(1 + 2 * (q.w * q.
double cosp = std::sqrt(1 - 2 * (q.w * q.
angles.pitch = 2 * std::atan2(sinp, cosp) - M_PI / 2;
Line 262 ⟶ 260:
== Vector rotation ==
Let us define scalar <math>
Note that the canonical way to rotate a three-dimensional vector <math>\vec{v}</math> by a quaternion <math>q</math> defining an [[
:<math>\mathbf{
where <math>\mathbf{
:<math>\vec{t} = 2\vec{q} \times \vec{v}</math>
:<math>\vec{v}^{\,\prime} = \vec{v} +
where <math>\times</math> 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% <ref>{{cite journal |pmc=4435132|year=2015|last1=Janota|first1=A|title=Improving the Precision and Speed of Euler Angles Computation from Low-Cost Rotation Sensor Data|journal=Sensors|volume=15|issue=3|pages=7016–7039|last2=Šimák|first2=V|last3=Nemec|first3=D|last4=Hrbček|first4=J|doi=10.3390/s150307016|pmid=25806874|bibcode=2015Senso..15.7016J |doi-access=free}}</ref> faster than the original for vector rotation.
=== Proof ===
Line 275 ⟶ 273:
:<math>
\begin{align}
\mathbf{
& = (
\vec{
\end{align}
</math>
Using this relation one finds for <math>\mathbf{
:<math>
\begin{align}
\mathbf{
& = (\vec{v} \cdot \vec{q},
\end{align}
</math>
Line 290 ⟶ 288:
:<math>
\begin{align}
\mathbf{q
& = (0,
\vec{q}\times(\vec{q}\times\vec{v} )) \\
\end{align}
</math>
where anti-commutivity of cross product and <math>\vec{q}\cdot \vec{v} \times \vec{q} = 0</math> has been applied. By next exploiting the property that <math>\mathbf{q}</math> is a [[
:<math>
\vec{q}\times(\vec{q}\times\vec{v}) = (\vec{q}\cdot\vec{v})\vec{q} - (\vec{q}\cdot\vec{q})\vec{v}
Line 302 ⟶ 300:
:<math>
\begin{align}
\mathbf{
2\vec{q}\times (\vec{q}\times\vec{v} )) \\
\end{align}
Line 308 ⟶ 306:
which upon defining <math>\vec{t} = 2\vec{q} \times \vec{v}</math> can be written in terms of scalar and vector parts as
:<math>
(0, \vec{v}^{\,\prime}) = (0, \vec{v} +
</math>
|