## Attitude Representations

## Understanding Direct Cosine Matrices, Euler Angles and Quaternions

## _____

There is quite a lot of information presented on this page. To truly become a

on this content, I recommend checking out this course: Complete Guide to Attitude Representation and Transformations for Engineers and Programmerssubject matter expert

# Attitude Representations

An attitude representation allows the orientation of an object to be mathematically described relative to a reference frame. This mathematical description is a set of parameters or a transformation that describes the orientation of a reference frame with respect to another reference frame. When we describe the attitude, we two reference frames:

(denoted by \(\mathcal{F}^w\)): which acts as a static reference that says fixed in location and attitude)*World/Fixed Reference Frame*(denoted by \(\mathcal{F}^b\)): which is rigidly attached to the object whose attitude we would like to describe relative to the world frame.*Body/Rotated Reference Frame*

Now let \(\mathbf{\Phi}\) represent the attitude of an object or frame such that it maps quantities in one frame to the other frame:

\( \mathbf{\Phi} : \mathcal{F}^w \leftrightarrow \mathcal{F}^b \)

There are two different conventions that could be adopted, we could define that:

- The attitude \(\mathbf{\Phi}\) expresses the rotation of the rotated frame \(\mathcal{F}^b\) relative to the reference frame \(\mathcal{F}^w\) , such that: \( \mathbf{\Phi}_1: \mathcal{F}^w \rightarrow \mathcal{F}^b \)
- The attitude \(\mathbf{\Phi}\) expresses the rotation of the reference frame \(\mathcal{F}^w\) relative to the rotated frame \(\mathcal{F}^b\) , such that: \( \mathbf{\Phi}_2: \mathcal{F}^b \rightarrow \mathcal{F}^w \)

In either convention, that the attitude can be inverted to find the opposite frame mapping or inverse rotation:

\( \mathbf{\Phi}_1: \mathcal{F}^w \rightarrow \mathcal{F}^b \)

\( \mathbf{\Phi}_1^{-1}: \mathcal{F}^w \leftarrow \mathcal{F}^b \)

For the rest of the page it is assumed that the first convention \(\mathbf{\Phi}: \mathcal{F}^w \rightarrow \mathcal{F}^b\) is used.

There are multiple ways to express and store an attitude representation, with the main three being:

**Direct Cosine Matrix (DCM)**– Uses a 3×3 matrix to represent the linear transform mapping from one coordinate frame to another rotated coordinate frame.**Euler Angles**– Uses 3 component rotation angles to parameterize the attitude.**Quaternion**– Uses a unit quaternion to parameterize the attitude.

Each representation method has advantages and disadvantages:

- Ease of User Interpretation and Interaction (can you mentally picture it and describe it)
- Storage (size in memory)
- Numerical Issues (stability, computation performance, uniqueness)
- Integration and Kinematics (how to describe a rotating object or changing attitude)
- Interpolation (computer graphics and animation, smoothly changing between two orientations)

## Rotation Matrices

## Definition

The direction cosine matrix is one of the ways to mathematically represent attitude. It is a special 3×3 matrix that when multiplied by a vector in one reference frame transforms that vector to a second rotated reference frame. For this rotation matrix \(\mathbf{R}\) to be a valid rotation transformation then it must be orthogonal, such that:

\( \det{(\mathbf{R})} = 1 \)

\( \mathbf{R}^{-1} = \mathbf{R}^T \)

So that the transformed vector stays the same length and that the inverse mapping is simply the opposite operation so that the inverse matrix is just the transpose of the matrix.

This allows a rotation transformation matrix to be defined that encodes the attitude \(\mathbf{\Phi}\) information, so following the attitude convention, it gives:

\( \mathbf{R}^b_w = f(\mathbf{\Phi}) \)

Where \(\mathbf{R}^b_w\) transforms a column vector in the reference or world coordinate frame \(\mathcal{F}^w\) to the body or rotated coordinate frame \(\mathcal{F}^w\) when pre-multiplied by the rotation matrix. So let \(\mathbf{R}^b_a\) be a rotation matrix that maps a vector from \(\mathcal{F}^a\) to \(\mathcal{F}^b\), such that:

\( \vec{v}^b = \mathbf{R}^b_a \vec{v}^a \)

## Direct Cosine Matrix (DCM)

The rotation transformation matrix can be described as an operation that projects the reference frame axes onto the rotated frame axes, so that it forms a mapping of how much each axis is transferred to all the other axes. This is called a * Direct Cosine Matrix* (DCM) because each element of the matrix is the cosine of the unsigned angle between the frame axes.

\( \mathbf{R}^b_a = \left[\begin{array}{ccc} \cos(\theta_{x^a,x^b}) & \cos(\theta_{y^a,x^b}) & \cos(\theta_{z^a,x^b}) \\ \cos(\theta_{x^a,y^b}) & \cos(\theta_{y^a,y^b}) & \cos(\theta_{z^a,y^b}) \\ \cos(\theta_{x^a,z^b}) & \cos(\theta_{y^a,z^b}) & \cos(\theta_{z^a,z^b}) \\ \end{array}\right]\)

where \(\theta_{x^a,x^b}\) is the unsigned angle between the \(x^a\) axis and the \(x^b\) axis. This process effectively rotates the reference frame via the projection, it does ** not** rotate the vector, rather it views the same vector from a different frame.

## Single Axis Transformations

The single axis frame rotation transformations are given by:

\( \mathbf{R}_x(\theta) = \left[\begin{array}{ccc} 1 & 0 & 0 \\ 0 & \cos(\theta) & \sin(\theta) \\ 0 & -\sin(\theta) & \cos(\theta) \end{array}\right] \)

\( \mathbf{R}_y(\theta) = \left[\begin{array}{ccc} \cos(\theta) & 0 & -\sin(\theta) \\ 0 & 1 & 0 \\ \sin(\theta) & 0 & \cos(\theta) \end{array}\right] \)

\( \mathbf{R}_z(\theta) = \left[\begin{array}{ccc} \cos(\theta) & \sin(\theta) & 0 \\ -\sin(\theta) & \cos(\theta) & 0 \\ 0 & 0 & 1 \end{array}\right] \)

It should be noted again that these transformations rotate the *reference frame*, they ** do not** rotate the

*vector*itself.

## Active or Passive Rotations

A rotation can be considered active or passive based on if it is the frame rotates (passive) or if the vector itself rotates (active). When the frame rotates, it is considered a passive rotation since the vector does not change, rather it is the frame of reference that rotates. This is the standard used in this page, as it is most closely aligned with engineering concepts.

Rotations have no absolute quantities, they are always relative to a reference. Therefore an active rotation can be just considered as a passive rotation, but in the opposite direction. So an passive rotation by \(\theta\) is the same as an active rotation by \(-\theta\).

## Chaining Multiple Rotations

Multiple rotations can be chained together by multiplying the rotation matrices in order (right to left). Consider the rotation from \(\mathcal{F}^a\) to \(\mathcal{F}^c\) via the intermediate frame \(\mathcal{F}^b\):

\(

\vec{x}^c = \mathbf{R}^c_b \vec{x}^b \\

\vec{x}^b = \mathbf{R}^b_a \vec{x}^a \\

\vec{x}^c = \mathbf{R}^c_b\left(\mathbf{R}^b_a \vec{x}^a\right) \\

\vec{x}^c = \mathbf{R}^c_a \vec{x}^a

\)

So the composite rotation matrix \(\mathbf{R}^c_a\) is formed by chaining the rotations together \(\mathbf{R}^c_a = \mathbf{R}^c_b\mathbf{R}^b_a\) by multiplying the subsequent rotations on the left.

## Intrinsic vs Extrinsic Rotations

When multiple rotations are chained together by sequential operations, there are two different ways of viewing the operation, there are intrinsic or extrinsic rotation sequences.

In the * intrinsic* definition, each sequential operation is applied to the

*intermediate*frame from the previous rotation. Consider the example:

\( \left(x,y,z\right) \rightarrow \mathbf{R}_z\rightarrow \left(x’,y’,z \right) \rightarrow\mathbf{R}_{y’}\rightarrow \left(x”,y’,z” \right) \)

The first rotation \(\mathbf{R}_z\) rotates the \((x,y,z)\)-axes around the \(z\)-axis, so that the \(z\)-axis stays the same and the \((x\),\(y)\)-axes shift. The next rotation \(\mathbf{R}_{y’}\) is about the **new** \(y’\)-axis, so the \((x’\),\(z)\)-axes now shift. This is the convention used so far in this page.

In the **extrinsic** definition, each sequential operation is applied to the **original** non-rotated frame. Consider the example:

\( \left(x,y,z\right) \rightarrow\mathbf{R}_z\rightarrow \left(x’,y’,z \right) \rightarrow\mathbf{R}_y\rightarrow \left(x”,y”,z” \right) \)

The first rotation \(\mathbf{R}_z\) rotates the \((x,y,z)\)-axes around the \(z\)-axis, while the next rotation \(\mathbf{R}_{y}\) is about the **original** \(y\)-axis, causing all the \((x,y,z)\)-axes to change.

To convert the chained

intrinsicrotation sequence \(\mathbf{R} = \mathbf{R}_i\mathbf{R}_j\mathbf{R}_k\) into anextrinsicrotation sequence, the rotation sequence can be reversed \(\mathbf{R’} = \mathbf{R}_k\mathbf{R}_j\mathbf{R}_i\). The opposite is also true, to convert anextrinsicrotation sequence into anintrinsicrotation sequence, reverse the sequence order of the rotations.

## Euler Angles

# Definition

Euler angles describe any arbitrary rotation using a set of 3 angles, each angle representing a single axis rotation that is applied in an intrinsic sequence. Let \(\mathbf{\Phi}\) be a set of Euler Angles consisting of the angles \(\{\phi, \theta, \psi \}\). The rotation matrix is calculated with:

\( \mathbf{R}_{ijk}\left(\mathbf{\Phi}\right) = \mathbf{R}_i\left(\phi\right) \mathbf{R}_j\left(\theta\right) \mathbf{R}_k\left(\psi\right) \)

where \(i,j,k = \{x,y,z\}\). There are 12 valid rotation sequences, that can be broken up into ** Proper Euler Angles** which have a rotation axis repeated in the sequence and

*which have 3 distinct rotation axes.*

**Tait-Bryan Angles**The Euler sequence * XYZ* is commonly called ‘Cardan Angles’ or ‘Nautical Angles’. It is also the sequence that is typically used when people refer to ‘Euler Angles’. The angles in this sequence \((\phi,\theta,\psi)\) are commonly called Roll, Pitch and Yaw. This is the sequence we will use for the rest of this page.

**Angle Conventions and Ranges**

Angles are defined according to the right hand rule. Positive values are clockwise when viewed along the axis direction.

The range for \(\phi\) and \(\psi\) are defined by *modulo* \(2\pi\) so that:

\( -\pi \le \{ \phi,\psi\} \le \pi \;\;\;\text{or}\;\;\; 0 \le \{ \phi,\psi\} \le 2\pi \)

The range for \(\theta\) is defined between the ranges:

\( -\frac{\pi}{2} \le \theta \le \frac{\pi}{2} \;\;\;\text{or}\;\;\; 0 \le \theta \le \pi \)

# Euler Angle and DCM Conversions

The following are the conversion functions between Euler Angles and Direct Cosine Matrix attitude representations. Let \(c_{(\cdot)}\) and \(s_{(\cdot)}\) be the \(\sin(\cdot)\) and \(\cos(\cdot)\) operations respectively.

\( \mathbf{R}_{xyz} = \mathbf{R}_x\left(\phi\right) \mathbf{R}_y\left(\theta\right) \mathbf{R}_z\left(\psi\right) \)

\( \mathbf{R}_{xyz} = \left[ \begin{array}{ccc} c_\theta c_\psi & c_\theta s_\psi & -s_\theta \\ s_\phi s_\theta c_\psi – c_\phi s_\psi & s_\phi s_\theta s_\psi + c_\phi c_\psi & c_\theta s_\phi \\ c_\phi s_\theta c_\psi + s_\phi s_\psi & c_\phi s_\theta s_\psi – s_\phi c_\psi & c_\theta c_\phi \end{array} \right] \)

\( \mathbf{\Phi}_{xyz} = \left[ \begin{array}{c} \text{arctan2}(r_{23},r_{33}) \\ -\arcsin(r_{13})\\ \text{arctan2}(r_{12},r_{11})\end{array} \right] \)

## Quaternions

# 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] \)