Definition
A quaternion \(\mathbf{q}\in\mathbb{H}\) is an extended number system for complex numbers. It may be represented as a vector: \[ \mathbf{q} = \left[ \begin{array}{cccc} q_0 & q_1 & q_2 & q_3 \end{array}\right]^T = \left[ \begin{array}{c} q_0 \ \mathbf{q}_{1:3} \end{array} \right] \]
where \(q_0\) is the real scalar part and \(q_1,q_2\) and \(q_3\) are the real components of the quaternion vector unit components \(i,j\) and \(k\).
A unit quaternion can be used to represent the attitude of a rigid body or a rotation transformation. The attitude can be represented as an rotation axis (the vector part of the quaternion) while the scalar part of the quaternion represents the rotation angle around the rotation axis.
The quaternion can be formed by the relationship:
\[ \mathbf{q} = \left[\begin{array}{c} \cos{\left(\frac{1}{2}D\right)}\\ \cos{\left(A\right)}\sin{\left(\frac{1}{2}D\right)}\\ \cos{\left(B\right)}\sin{\left(\frac{1}{2}D\right)}\\ \cos{\left(C\right)}\sin{\left(\frac{1}{2}D\right)}\end{array}\right] \]
where \(A,B\) and \(C\) are the angles between the rotation axis vector and the \(x,y\) and \(z\) axes respectively, and \(D\) is the rotation angle around the rotation axis vector.
With rotations, reversing the axis of rotation or reversing the angle of rotation, reverses the transformation. Reversing both at the same time leaves the total transformation unchanged.
\[ \mathbf{q}:\mathcal{F}^a \rightarrow \mathcal{F}^b \] \[ -\mathbf{q}:\mathcal{F}^a \rightarrow \mathcal{F}^b \] \[ \bar{\mathbf{q}}:\mathcal{F}^a \leftarrow \mathcal{F}^b\] \[ -\bar{\mathbf{q}}:\mathcal{F}^a \leftarrow \mathcal{F}^b\]
The rotation transformation can be applied using the following relationship. Consider a vector \(\vec{x}\in\mathbb{R}^3\), with \(\vec{x}^a\) being the vector in frame \(\mathcal{F}^a\) and \(\vec{x}^b\) being the same vector represented in frame \(\mathcal{F}^b\), then the following relationship holds:
\[ \left[\begin{array}{c}0\\ \vec{x}^b\end{array}\right] = \mathbf{q} \cdot\left[\begin{array}{c}0\\ \vec{x}^a\end{array}\right]\cdot\mathbf{q}^{-1} \]
where \(\mathbf{q}\) is the unit quaternion that represents the rotation between frame \(\mathcal{F}^a\) and frame \(\mathcal{F}^b\).
Unit Quaternion and DCM Conversions
The quaternion rotation transformation relationship can be expanded to find the equivalent DCM matrix in terms of the quaternion components:
\[ \mathbf{R}(\mathbf{q}) = \left[\begin{array}{ccc} \left(q_0^2+q_1^2-q_2^2-q_3^2\right) & 2q_1q_2+2q_0q_3 & 2q_1q_3-2q_0q_2\\ 2q_1q_2-2q_0q_3 & \left(q_0^2-q_1^2+q_2^2-q_3^2\right) & 2q_2q_3+2q_0q_1 \\ 2q_1q_3+2q_0q_2 & 2q_2q_3-2q_0q_1 & \left(q_0^2-q_1^2-q_2^2+q_3^2\right)\end{array}\right] \]
The reverse transformation mapping is slightly more complicated. Inspection of the DCM matrix yields the following inverse mappings for the quaternion components:
\[ \mathbf{q}_0(\mathbf{R}) = \frac{1}{2}\left[\begin{array}{c} \sqrt{\left(1+r_{11}+r_{22}+r_{33}\right)} \\ \left(r_{23}-r_{32}\right)/\sqrt{\left(1+r_{11}+r_{22}+r_{33}\right)} \\ \left(r_{31}-r_{13}\right)/\sqrt{\left(1+r_{11}+r_{22}+r_{33}\right)} \\ \left(r_{12}-r_{21}\right)/\sqrt{\left(1+r_{11}+r_{22}+r_{33}\right)} \end{array}\right] \]
\[ \mathbf{q}_1(\mathbf{R})=\frac{1}{2} \left[\begin{array}{c} \left(r_{23}-r_{32}\right)/\sqrt{\left(1+r_{11}-r_{22}-r_{33}\right)}\\ \sqrt{\left(1+r_{11}-r_{22}-r_{33}\right)}\\ \left(r_{12}+r_{21}\right)/\sqrt{\left(1+r_{11}-r_{22}-r_{33}\right)}\\ \left(r_{31}+r_{13}\right)/\sqrt{\left(1+r_{11}-r_{22}-r_{33}\right)} \end{array}\right] \]
\[ \mathbf{q}_2(\mathbf{R}) = \frac{1}{2} \left[\begin{array}{c} \left(r_{31}-r_{13}\right)/\sqrt{\left(1-r_{11}+r_{22}-r_{33}\right)} \\ \left(r_{12}+r_{21}\right)/\sqrt{\left(1-r_{11}+r_{22}-r_{33}\right)} \\ \sqrt{\left(1-r_{11}+r_{22}-r_{33}\right)} \\ \left(r_{23}+r_{32}\right)/\sqrt{\left(1-r_{11}+r_{22}-r_{33}\right)} \end{array}\right] \]
\[ \mathbf{q}_3(\mathbf{R})=\frac{1}{2} \left[\begin{array}{c} \left(r_{12}-r_{21}\right)/\sqrt{\left(1-r_{11}-r_{22}+r_{33}\right)}\\ \left(r_{31}+r_{13}\right)/\sqrt{\left(1-r_{11}-r_{22}+r_{33}\right)}\\ \left(r_{23}+r_{32}\right)/\sqrt{\left(1-r_{11}-r_{22}+r_{33}\right)}\\ \sqrt{\left(1-r_{11}-r_{22}+r_{33}\right)} \end{array}\right] \]
The selection of which \(\mathbf{q}_i(\mathbf{R})\) to use depends on the values in \(\mathbf{R}\) as some combinations will give complex results. The correct \(\mathbf{q}_i(\mathbf{R})\) mapping to use is decided using the following function: \[ \mathbf{q}(\mathbf{R}) = \begin{cases} \mathbf{q}_0(\mathbf{R}) & \text{if } r_{22} > -r_{33},\; r_{11} > -r_{22},\; r_{11} > -r_{33}\\ \mathbf{q}_1(\mathbf{R}) & \text{if } r_{22} < -r_{33},\; r_{11} > \hphantom{-}r_{22},\; r_{11} > \hphantom{-} r_{33}\\ \mathbf{q}_2(\mathbf{R}) & \text{if } r_{22} > \hphantom{-} r_{33},\; r_{11} < \hphantom{-}r_{22},\; r_{11} < -r_{33}\\ \mathbf{q}_3(\mathbf{R}) & \text{if } r_{22} < \hphantom{-} r_{33},\; r_{11} < -r_{22},\; r_{11} < \hphantom{-} r_{33} \end{cases} \]
Unit Quaternion and Euler Angle Conversions
The conversion to Euler Angles from a unit quaternion can be found by substituting the unit quaternion to DCM conversion into the appropriate DCM to Euler Angle conversion with the desired Euler Angle sequence such that: \[ \mathbf{\Phi}_{ijk} = \mathbf{\Phi}_{ijk} \left( \mathbf{R} \left( \mathbf{q} \right) \right) \]
The conversion of a unit Quaternion from Euler Angles can be found in a similar way as generating a DCM matrix from Euler Angles by chaining successive single axis rotations:
\[ \mathbf{q}\left(\mathbf{\Phi}_{ijk}\right) = \mathbf{q}_i\left(\phi\right) \cdot \mathbf{q}_j\left(\theta\right) \cdot \mathbf{q}_k\left(\psi\right) \]
where the single axis rotations are: \[ \mathbf{q}_x\left(\theta\right) = \left[\begin{array}{c}\cos(\frac{\theta}{2})\\sin(\frac{\theta}{2})\\0\\0\end{array}\right] \]
\[ \mathbf{q}_y\left(\theta\right) = \left[\begin{array}{c}\cos(\frac{\theta}{2})\\0\\sin(\frac{\theta}{2})\\0\end{array}\right] \]
\[ \mathbf{q}_z\left(\theta\right) = \left[\begin{array}{c}\cos(\frac{\theta}{2})\\0\\0\\sin(\frac{\theta}{2})\end{array}\right] \]
So the conversion of Euler Angles to a unit quaternion is:
\[\mathbf{q}\left(\mathbf{\Phi}_{xyz}\right) =\left[\begin{array}{c} c\left(\frac{\phi}{2}\right)c\left(\frac{\theta}{2}\right)c\left(\frac{\psi}{2}\right) + s\left(\frac{\phi}{2}\right)s\left(\frac{\theta}{2}\right)s\left(\frac{\psi}{2}\right)\\ s\left(\frac{\phi}{2}\right)c\left(\frac{\theta}{2}\right)c\left(\frac{\psi}{2}\right) - c\left(\frac{\phi}{2}\right)s\left(\frac{\theta}{2}\right)s\left(\frac{\psi}{2}\right) \\ c\left(\frac{\phi}{2}\right)s\left(\frac{\theta}{2}\right)c\left(\frac{\psi}{2}\right) + s\left(\frac{\phi}{2}\right)c\left(\frac{\theta}{2}\right)s\left(\frac{\psi}{2}\right) \\ c\left(\frac{\phi}{2}\right)c\left(\frac{\theta}{2}\right)s\left(\frac{\psi}{2}\right) - s\left(\frac{\phi}{2}\right)s\left(\frac{\theta}{2}\right)c\left(\frac{\psi}{2}\right) \end{array}\right] \]
The conversion to Euler Angles from a unit quaternion is then:
\[ \mathbf{\Phi}_{xyz}\left(\mathbf{q}\right) =\left[\begin{array}{c}\text{arctan2}\left({2q_2q_3+2q_0q_1},\;{q_0^2-q_1^2-q_2^2+q_3^2}\right)\\ -\arcsin{\left(2q_1q_3-2q_0q_2\right)}\\ \text{arctan2}\left({2q_1q_2+2q_0q_3},\;{q_0^2+q_1^2-q_2^2-q_3^2}\right)\end{array}\right] \]