### What’s the difference and why does it matter?

Euler angles are generally what most people consider when they picture 3D space. Each value represents the rotation in degrees (it could technically be in any units) around one of the 3 axes in 3D space. Most of the time you will want to create angles using Euler angles because they are conceptually the easier to understand. The flaw is that Euler angles have a problem known as the gimbal lock that prevents certain rotations when two axes align. The solution: quaternions.

### How to convert between Quaternion and Euler angles?

Define coordinate system ‘x_{o}y_{o}z_{o}‘ to be fixed to the carrier object where ‘x_{o}‘ axis is lateral and directed to the right, ‘y_{o}’ axis is longitudinal and directed forward, ‘z_{o}‘ axis is normal and directed vertical. At usual installation of the unit on carrier object the units appropriate axes should be parallel to the axes as above Fig. 1. shows. Also, it is possible to install the unit in any known position relative to the object with known alignment angles (see APPENDIX B of the units Interface Control Document (ICD) for details).

The Inertial Labs^{TM} unit calculates orientation of the coordinate system ‘x_{o}y_{o}z_{o}’ fixed to the carrier object with respect to Cartesian geographical reference frame ‘xyz’ where axes ‘x’ and ‘y’ are in the level and directed to the East and North, and ‘z’ axis is directed up. Such reference frame is also known as ENU (East-North-Up) Earth-level frame.

Measured angles are the standard __Euler angles__ of rotation from the Earth-level frame to the object frame: heading *K* is first, then pitch *q*, and then roll *g* — see Fig. 2.

**Notes:**

- Positive direction of heading is clock-wise. So heading
*K*is shown with minus sign on Fig. 2. - In different applications “heading” is also known as “azimuth” or “yaw”; “pitch” is also known as “elevation” or “tilt”; “roll” is also known as “bank”.

Due to the definition of Euler angles there is a mathematical singularity when the object longitudinal y_{0}-axis is pointed up or down (i.e. pitch approaches ±90°). This singularity is not present in the quaternion or directional cosine matrix (rotation matrix) presentation.

**Directional Cosine Matrix** (DCM) is the rotation matrix * C* from the object body reference frame ‘x

_{o}y

_{o}z

_{o}‘ to the geographical reference frame ‘xyz’

*.*According to Fig. 1, DCM can be represented through Euler angles as:

\boldsymbol{C}=\left[\begin{array}{ccc} \cos K \cos \gamma+\sin K \sin \gamma \sin \theta & \sin K \cos \theta & \cos K \sin \gamma-\sin K \cos \gamma \sin \theta \\ -\sin K \cos \gamma+\cos K \sin \gamma \sin \theta & \cos K \cos \theta & -\sin K \sin \gamma-\cos K \cos \gamma \sin \theta \\ -\cos \theta \sin \gamma & \sin \theta & \cos \theta \cos \gamma \end{array}\right]

Equation 1

Or, Euler angles can be calculated from elements *c*_{ij} of directional cosine matrix * C*:

K=\arctan \frac{C_{12}}{C_{22}} ; \theta=\arcsin C_{32} ; \gamma=-\arctan \frac{C_{31}}{C_{33}} .

Equation 2

^{TM}unit provides orientation output in

**quaternion**

*form which is a hyper-complex number with four components*

**Q**\boldsymbol{Q}=\left(q_0, q_1, q_2, q_3\right)

Equation 3

where *q*_{0} is real part, *q*_{1}, *q*_{2}, *q*_{3} are vector part. In other words,* q*_{0} represents the magnitude of the rotation, and the other three components represent the axis about which that rotation takes place.

With only four components, quaternion representation of orientation is computationally efficient. However, manipulation of quaternions is not intuitive, so their use in place of directional cosine matrices may increase the chances of mistakes being made.

Quaternion * Q* is converted to directional cosine matrix

*using the next expressions:*

**C**C=\left[\begin{array}{ccc} q_0^2+q_1^2-q_2^2-q_3^2 & 2\left(q_1 q_2-q_0 q_3\right) & 2\left(q_1 q_3+q_0 q_2\right) \\ 2\left(q_1 q_2+q_0 q_3\right) & q_0^2+q_2^2-q_1^2-q_3^2 & 2\left(q_2 q_3-q_0 q_1\right) \\ 2\left(q_1 q_3-q_0 q_2\right) & 2\left(q_2 q_3+q_0 q_1\right) & q_0^2+q_3^2-q_1^2-q_2^2 \end{array}\right]

Equation 4

The reverse conversion from directional cosine matrix * C* to quaternion

*is following:*

**Q**\begin{gathered} q_0=\frac{1}{2} \sqrt{1+c_{11}+c_{22}+c_{33}} \\\\ q_1=\frac{c_{32}-c_{23}}{4 q_0}\\\\ q_2=\frac{c_{13}-c_{31}}{4 q_0}\\\\ q_3=\frac{c_{21}-c_{12}}{4 q_0} \end{gathered}

Equation 5

The expressions above in Equation 5 are widely used but they have singularity at *q*_{0} = 0. Therefore the Inertial Labs^{TM} unit uses other expressions that have no singularity:

\begin{gathered} q_0=\frac{1}{2} \sqrt{1+c_{11}+c_{22}+c_{33}} \\\\ q_1=\frac{1}{2} \sqrt{1+c_{11}-c_{22}-c_{33}} \cdot \sin \left(c_{32}-c_{23}\right) \\\\ q_2=\frac{1}{2} \sqrt{1-c_{11}+c_{22}-c_{33}} \cdot \sin \left(c_{13}-c_{31}\right) \\\\ q_3=\frac{1}{2} \sqrt{1-c_{11}-c_{22}+c_{33}} \cdot \sin \left(c_{21}-c_{12}\right) \end{gathered}

Equation 6

At necessity to calculate Euler angles from quaternion, calculate elements *c*_{12}, *c*_{22}, *c*_{31}, *c*_{32}, *c*_{33}, according to Equation 6, and then use Equation 2:

\begin{gathered} K=\arctan \frac{2\left(q_1 q_2-q_0 q_3\right)}{q_0^2+q_2^2-q_1^2-q_3^2}\\\\ \theta=\arcsin \left(2 q_2 q_3+2 q_0 q_1\right) \\\\ \gamma=-\arctan \frac{2\left(q_1 q_3-q_0 q_2\right)}{q_0^2+q_3^2-q_1^2-q_2^2} \end{gathered}

Equation 7

where arctan is four-quadrant inverse tangent.