A quaternion is a four-dimensional complex number that can be used to represent rotaions in 3D-space. We use the definitions and sub-scripts/super-scripts notation used in Madgwick’s original work.

An arbitrary orientation of frame B relative to frame A can be achieved throug a rotation of angle θ around an unit vector ^A{\overrightarrow{r}}=[r_x \quad r_y \quad r_z] defined in frame A. The quaternion describing this orientation is:

^A_B{\bf\hat{q}}=\left[q_0 \quad q_1 \quad q_2 \quad q_3 \right]=\left[\cos{\frac{\theta}{2}} \quad -r_x\sin{\frac{\theta}{2}} \quad -r_y\sin{\frac{\theta}{2}} \quad -r_z\sin{\frac{\theta}{2}}\right]

A leading sub-script denotes the frame being described and a leading super-script denotes the frame this is with reference to. For example ^S_E{\bf\hat{q}} describes the orientation of frame E (earth) relative to frame S (stick) and ^S{\bf\overrightarrow{r}} is a vector described in frame S (stick).It is conventional for all quaternions describing an orientation to be of unit length 1 (nromalised).

The quaternion conjugate ^A_B{\bf\hat{q}}^*=^B_A{\bf\hat{q}}=\left[q_0 \quad -q_1 \quad -q_2 \quad -q_3 \right] can be used to swap the relative frames described by an orientation. Here ^A_B{\bf\hat{q}}^*=^B_A{\bf\hat{q}} describes the orientation of frame A relative to frame B.

The quaternion product \otimes defines compound orientations. For two orientations ^A_B{\bf\hat{q}} and ^B_C{\bf\hat{q}}, the compounded orientation ^A_C{\bf\hat{q}} is the quaternion product:

^A_C{\bf\hat{q}}={^B_C{\bf\hat{q}}} \otimes {^A_B{\bf\hat{q}}}

For two quaternions {\bf\hat{a}} and {\bf\hat{b}}, the quaternion product is

{\bf\hat{a}} \otimes {\bf\hat{b}} = \left[ a_0 \quad a_1 \quad a_2 \quad a_3 \right] \otimes \left[ b_0 \quad b_1 \quad b_2 \quad b_3 \right]=\left[ \begin{array}{}a_0b_0-a_1b_1-a_2b_2-a_3b_3\\ a_0b_1+a_1b_0+a_2b_3-a_3b_2\\a_0b_2-a_1b_3+a_2b_0+a_3b_1 \\a_0b_3+a_1b_2-a_2b_1+a_3b_0\end{array} \right]^T

A three dimensional vector can be rotated by a quaternion:

^B\overrightarrow{v}={^A_B{\bf\hat{q}}} \otimes {^A\overrightarrow{v}}\otimes{^A_B{\bf\hat{q}}^*}


^A{\bf\overrightarrow{v}} and ^B{\bf\overrightarrow{v}} are the same vector described in frame A and frame B respectively where each vector contains a 0 inserted as the first element to make them 4 element row vectors.