Quaternion Arithmetic

One of the main differences between quaternions and vectors is their multiplication operation. Specifically, the quaternion multiplication is performed using the quaternion product, typically shown by the symbol $\otimes$.

Given the two rotations, $\boldsymbol{p}$ and $\boldsymbol{q}$, the quaternion product operation, which represents the combined rotation of the two quaternions, is as follows:

$$ \boldsymbol{p} \otimes \boldsymbol{q} = \begin{bmatrix} p_0 \\ p_1 \\ p_2 \\ p_3 \end{bmatrix} \otimes \begin{bmatrix} q_0 \\ q_1 \\ q_2 \\ q_3 \end{bmatrix} = \begin{bmatrix} p_0q_0 - p_1q_1 - p_2q_2 - p_3q_3 \\ p_0q_1 + p_1q_1 + p_2q_3 - p_3q_2 \\ p_0q_2 - p_1q_3 + p_2q_0 + p_3q_1 \\ p_0q_3 + p_1q_2 - p_2q_1 + p_3q_0 \\ \end{bmatrix} = \begin{bmatrix} p_0q_0 - \boldsymbol{p}^T_v\boldsymbol{q}_v \\ p_0\boldsymbol{q}_v + q_0\boldsymbol{p}_v + \boldsymbol{p}_v \times \boldsymbol{q}_v \\ \end{bmatrix} $$

$$ \boldsymbol{p} \otimes \boldsymbol{q} = Q (\boldsymbol{p}) \boldsymbol{q} = \begin{bmatrix} \phantom{-}p_0 & -p_1 & -p_2 & -p_3 \\ \phantom{-}p_1 & \phantom{-}p_0 & -p_3 & \phantom{-}p_2 \\ \phantom{-}p_2 & \phantom{-}p_3 & \phantom{-}p_0 & -p_1 \\ \phantom{-}p_3 & -p_2 & \phantom{-}p_1 & p_0 \\ \end{bmatrix} \begin{bmatrix} q_0 \\ q_1 \\ q_2 \\ q_3 \\ \end{bmatrix} $$ $$ \boldsymbol{p} \otimes \boldsymbol{q} = \bar{Q}(\boldsymbol{q})\boldsymbol{p} = \begin{bmatrix} \phantom{-}q_0 & -q_1 & -q_2 & -q_3 \\ \phantom{-}q_1 & \phantom{-}q_0 & \phantom{-}q_3 & -q_2 \\ \phantom{-}q_2 & -q_3 & \phantom{-}q_0 & \phantom{-}q_1 \\ \phantom{-}q_3 & \phantom{-}q_2 & -q_1 & \phantom{-}q_0 \\ \end{bmatrix} \begin{bmatrix} p_0 \\ p_1 \\ p_2 \\ p_3 \\ \end{bmatrix} $$

The derivative of a Quaternion is defined as:

$$ \dot{q} = \lim_{\Delta \to t} \frac{q_\mathrm{k+1} - q_k}{\Delta t} $$

This definition can be re-written in terms of Rotation matrices, E and G, with respect to Global (G) and Body(B) vectors:

$$ \dot{q} = \frac{1}{2}E^T \omega_G = \frac{1}{2} G^T \omega_B $$

$$ \omega_G = 2E\dot{q} = -2E\dot{q} \qquad \omega_B = 2G\dot{q} = -2G\dot{q} $$

Where: $$ R = R^n_b = E G^T $$

$$ E = \begin{bmatrix} -q_1 & \phantom{-}q_0 & -q_3 & \phantom{-}q_2 \\ -q_2 & \phantom{-}q_3 & \phantom{-}q_0 & -q_1 \\ -q_3 & -q_2 & \phantom{-}q_1 & \phantom{-}q_0 \\ \end{bmatrix} \qquad E^T = \begin{bmatrix} -q_1 & -q_2 & -q_3 \\ \phantom{-}q_0 & \phantom{-}q_3 & -q_2 \\ -q_3 & \phantom{-}q_0 & \phantom{-}q_1 \\ \phantom{-}q_2 & -q_1 & \phantom{-}q_0 \\ \end{bmatrix} $$

$$ G = \begin{bmatrix} -q_1 & \phantom{-}q_0 & \phantom{-}q_3 & -q_2 \\ -q_2 & -q_3 & \phantom{-}q_0 & \phantom{-}q_1 \\ -q_3 & \phantom{-}q_2 & -q_1 & \phantom{-}q_0 \\ \end{bmatrix} \qquad G^T = \begin{bmatrix} -q_1 & -q_2 & -q_3 \\ \phantom{-}q_0 & -q_3 & \phantom{-}q_2 \\ \phantom{-}q_3 & \phantom{-}q_0 & -q_1 \\ -q_2 & \phantom{-}q_1 & \phantom{-}q_0 \\ \end{bmatrix} $$

The difference between two quaternions, $q_{diff}$, may be calculated by using the quaternion product on the reference quaternion, $q_\mathrm{ref}$, and conjugates of the measured quaternion, $q_\mathrm{meas}$, as shown:

$$ \boldsymbol{q}_{diff} = \boldsymbol{q}_{ref} \otimes \boldsymbol{q}_{meas} $$

A vector, $x$, may be rotated from a Local Frame(L) to a Global Frame(G) using two purely vector quaternions as defined:

$$ \boldsymbol{x}_{G} = \boldsymbol{q} \otimes \boldsymbol{x}_{L} \otimes \boldsymbol{q}^{*} = \boldsymbol{q}^{*} \otimes \boldsymbol{x}_L \otimes \boldsymbol{q} $$ $$ \boldsymbol{x}_G = [\boldsymbol{q}^{*}]_R [\boldsymbol{q}]_L \begin{bmatrix} 0 \\ \boldsymbol{x}_L \\ \end{bmatrix} = \begin{bmatrix} 0 \\[0.5em] \boldsymbol{R}^{G}_L \boldsymbol{x}_L \end{bmatrix} $$

$$ \boldsymbol{R}^G_L = \boldsymbol{R}^n_b \left(Local \mapsto Global \right) $$

Where $R^G_L$ is from the Local-Frame(Body-Frame) to the Global-Frame(NED-Frame) rotation transformation. Another format for the quaternion rotations is: $$ \boldsymbol{x}_G = \boldsymbol{q} \otimes \boldsymbol{x}_L \otimes \boldsymbol{q}^{*} = \begin{bmatrix} q_0(\boldsymbol{q} \cdot \boldsymbol{x}_L) - (q_0 \boldsymbol{x_L} - \boldsymbol{q} \times \boldsymbol{x_L}) \cdot \boldsymbol{q} \\ \left( (\boldsymbol{q} \cdot \boldsymbol{x}_L) \boldsymbol{q} + q_0 \left(q_0 \boldsymbol{x}_L - \boldsymbol{q} \times \boldsymbol{x}_L \right) + \left( q_0 \boldsymbol{x}_L - \boldsymbol{q} \times \boldsymbol{x}_L \right) \times \boldsymbol{q} \right) \end{bmatrix} = \begin{bmatrix} 0 \\ \left( 2\boldsymbol{q} (\boldsymbol{q} \cdot \boldsymbol{x}_L) + (q^2_0 - \boldsymbol{q} \cdot \boldsymbol{q})\boldsymbol{x}_L - 2q_0 (\boldsymbol{q} \times \boldsymbol{x}_L) \right) \end{bmatrix} $$

$$ \boldsymbol{x}_G = \left( \cos \frac{\theta}{2} + \boldsymbol{v}\sin\frac{\theta}{2} \right) \otimes \left(\boldsymbol{0 + x_L}\right) \otimes \left( \cos \frac{\theta}{2} - \boldsymbol{v}\sin\frac{\theta}{2} \right) $$

The same Local vs Global concept may be applied to determine the error quaternion of a system. We are able to determine the difference between a desired quaternion state, $q_{des}$, and the current quaternion of the system, $q$, by calculating an error quaternion $q_\mathrm{err}$, which is derived below:

$$ \boldsymbol{q} = \boldsymbol{q}_{des} \otimes \boldsymbol{q}_{err} \qquad \longrightarrow \qquad \boldsymbol{q}_{err} = \boldsymbol{q}^{-1}_{des} \otimes \boldsymbol{q} $$