Euler-Quaternion Conversions

The equations listed in the previous section, 2. Angles & Rotations: Euler Angles, for $\phi$, $\theta$, and $\psi$ can be used to initialize the Quaternion used in the EKF. This will help by decreasing the chances of divergence of the EKF. The Quaternion values may be determined from the Euler Angles as shown below:

$$ \vec{q} = \begin{bmatrix} q_0 \\[0.5em] q_1 \\[0.5em] q_2 \\[0.5em] q_3 \\[0.5em] \end{bmatrix} = \begin{bmatrix} \cos{\frac{\phi}{2}} \cos{\frac{\theta}{2}} \cos{\frac{\psi}{2}} + \sin{\frac{\phi}{2}} \sin{\frac{\theta}{2}} \sin{\frac{\psi}{2}} \\[0.5em] \sin{\frac{\phi}{2}} \cos{\frac{\theta}{2}} \cos{\frac{\psi}{2}} - \cos{\frac{\phi}{2}} \sin{\frac{\theta}{2}} \sin{\frac{\psi}{2}} \\[0.5em] \cos{\frac{\phi}{2}} \sin{\frac{\theta}{2}} \cos{\frac{\psi}{2}} + \sin{\frac{\phi}{2}} \cos{\frac{\theta}{2}} \sin{\frac{\psi}{2}} \\[0.5em] \cos{\frac{\phi}{2}} \cos{\frac{\theta}{2}} \sin{\frac{\psi}{2}} - \sin{\frac{\phi}{2}} \sin{\frac{\theta}{2}} \cos{\frac{\psi}{2}} \\[0.5em] \end{bmatrix} $$

Conversely, the Euler angles can be calculated from the Quaternion values using the following equations:

$$ Roll: \phi = \arctan \left( {\frac{2(q_0q_1 + q_2q_3)}{1 - 2(q_1^2 + q_2^2)}} \right) $$

$$ Pitch: \theta = \arcsin \left( 2(q_0q_2 - q_1q_3) \right) $$

$$ Yaw/Heading : \psi = \arctan \left( {\frac{2(q_0q_3 + q_1q_2)}{1 - 2(q_2^2 + q_3^2)}} \right) $$