User:Snietfeld/TRIAD Algorithm

The following is a brief description of the TRIAD algorithm and how it is used to generate a 3-axis attitude solution from vector observations. The TRIAD algorithm is a well-known method of attitude determination, and is actively used in the aerospace industry. In practice, the TRIAD algorithm is used to determine the orientation of satellites or other craft from a set of sensor observations. While historically the problem of attitude determination has been relegated almost exclusively to military or aerospace endeavors, it has recently found relevance to 3-D gaming systems such as the Nintendo Wii. The example presented here will demonstrate how the TRIAD algorithm can be used to determine the orientation of a device using sensors that are widely available.

=Terminology= Throughout this paper we will be using the terms “reference frame”, “reference vector” and “rotation matrix”. These have very specialized meanings in the context of attitude determination and deserve some explanation.

Reference Frame - A coordinate system defined by three orthogonal axes orientated in a particular direction and located at a specific point in space. More simply, a reference frame is a particular point and orientation from which observations can be made. An example of a commonly-used reference frame is the Earth-Centered, Earth-Fixed (ECEF) reference frame depicted below:

In the example presented in the following pages, we will demonstrate how the TRIAD algorithm can be used to convert a vector in an arbitrary device reference frame, D, to its equivalent in the less arbitrary (and thus more useful) ECEF reference frame, E. This is equivalent to finding an attitude solution for the device.

Reference Vector – A 3x1 vector which describes the direction of something measured in a given reference frame. Examples of commonly-used reference vectors include:
 * The magnetic field of the Earth, as measured by a magnetometer.
 * The acceleration of the Earth's gravity, as measured by an accelerometer.
 * The direction of the Sun, as measured by a camera, solar sensor, or array of solar panels.

It should be noted that the above sources for reference vectors vary widely in their accuracy, and the best combination (the TRIAD algorithm only requires two) is largely dependent on the conditions in which they are to be used. For example, a satellite in orbit around the earth would measure no acceleration from the Earth's gravity in its own frame, rendering an accelerometer useless for attitude determination. In this example, we will be using the magnetic field of the Earth and a gravity vector.

Rotation Matrix - A 3x3 matrix which can be used to convert an arbitrary vector from one frame into its equivalent in another. They exhibit the following property:

Where and are corresponding vectors in reference frames D and S, respectively, and is a rotation matrix that translates vectors from the reference frame D to reference frame S. Obtaining a rotation matrix from one frame to another is equivalent to obtaining an attitude solution and is the goal of the TRIAD algorithm.

=The TRIAD Algorithm= The TRIAD algorithm takes as its input two pairs of reference vectors. Each pair of reference vectors represents some measured direction in both reference frames. For example, the first pair of reference vectors might be the measured direction of the sun in both the device frame of reference and the ECEF reference frame.

We will be creating three rotation matrices. Using the reference vectors measured in the device frame, we will create a rotation matrix, $$R_{D \rightarrow I}$$, that rotates vectors from the device frame into the intermediate frame. We will then create a second rotation matrix, $$R_{E \rightarrow I}$$, that rotates vectors from the ECEF frame into the intermediate frame. Finally, we will use these two intermediate rotation matrices to create a third rotation matrix, $$R_{D \rightarrow E}$$, that converts vectors from the device frame directly into the ECEF frame.

The TRIAD algorithm itself is relatively straightforward and will be divided into three steps:
 * 1) Obtain the reference vectors.
 * 2) Create the intermediate reference frame.
 * 3) Solve for a full attitude solution.

Obtain the Reference Vectors
Before we can begin, we must obtain two pairs of corresponding reference vectors. For each reference frame, we will use the measured magnetic field of the earth, $$\vec{B}$$, and the measured gravity vector, $$\vec{g}$$. For the device reference frame, these will be denoted $$\vec{B_D}$$ and $$\vec{G_D}$$, respectively, with the corresponding ECEF equivalents denoted by $$\vec{B_E}$$ and $$\vec{G_E}$$.

Create the Intermediate Reference Frame
Once both pairs of reference vectors are obtained, we will use them to create an intermediate reference frame that is common to both the device and ECEF reference frames. We will use the reference vectors in each frame to create a rotation matrix into this intermediate frame, I. This intermediate reference frame will be created using the relative orientations of the reference vectors. Because these relative orientations are the same in every reference frame, the intermediate frame will be the same regardless of which reference frame we create it from.

To define the intermediate frame, we must create three orthonormal axes ($$\vec{i}$$, $$\vec{j}$$, and $$\vec{k}$$) from the reference vectors in both the device and ECEF frames:

$$ \begin{array}{lll} \vec{i} = \frac{\vec{v_1}}{\left | \vec{v_1} \right |} & \vec{i_D} = \frac{\vec{B_D}}{\left | \vec{B_D} \right |} & \vec{i_E} = \frac{\vec{B_E}}{\left | \vec{B_E} \right |}\\\\ \vec{j} = \frac{{\vec{v_1}}\times{\vec{v_2}}}{\left | \vec{v_1} \right |} & \vec{j_D} = \frac{{\vec{B_D}}\times{\vec{G_D}}}{\left | \vec{B_D} \times \vec{G_D} \right |} & \vec{j_E} = \frac{{\vec{B_E}}\times{\vec{G_E}}}{\left | \vec{B_E} \times \vec{G_E} \right |}\\\\ \vec{k} = \vec{i} \times \vec{j} & \vec{k_D} = \vec{i_D} \times \vec{j_D} & \vec{k_E} = \vec{i_E} \times \vec{j_E} \end{array} $$ NOTE: The choice of as was arbitrary here, but for reasons that are beyond the scope of this paper, the more accurate reference vector should always be chosen as.

We then combine these three basis vectors to create our intermediate rotation matrices:



R_{D \rightarrow I} = \begin{bmatrix} i_{Dx} & j_{Dx} & k_{Dx} \\ i_{Dy} & j_{Dy} & k_{Dy} \\ i_{Dz} & j_{Dz} & k_{Dz} \end{bmatrix} $$



R_{E \rightarrow I} = \begin{bmatrix} i_{Ex} & j_{Ex} & k_{Ex} \\ i_{Ey} & j_{Ey} & k_{Ey} \\ i_{Ez} & j_{Ez} & k_{Ez} \end{bmatrix} $$

Solving for a Full Attitude Solution
We now have two rotation matrices, $$R_{D \rightarrow I}$$ and $$R_{E \rightarrow I}$$, that rotate from each reference frame into the intermediate frame. To complete the algorithm and find a full attitude solution, we must define a sequence of rotations that takes us from the device frame to the intermediate frame and then finally to the ECEF frame. The first half of this sequence is accomplished by $$R_{D \rightarrow I}$$, but the second rotation matrix, $$R_{E \rightarrow I}$$, is the inverse of what we want.

To obtain $$R_{I \rightarrow E}$$, we can simply take the transpose:


 * $$R_{I \rightarrow E} = (R_{E \rightarrow I})^{T}$$

Because the matrices and represent rotations, we can combine them in sequence by multiplying them together:


 * $$R_{D \rightarrow E} = R_{D \rightarrow I} \cdot R_{I \rightarrow E} $$

This final rotation matrix represents the full attitude solution. We can now freely convert vector observations made in the device frame to the standard ECEF frame and back again:


 * $$ \vec{v_D} \cdot R_{D \rightarrow E} = \vec{v_E} $$


 * $$ \vec{v_E} \cdot R_{E \rightarrow D} = \vec{v_D} $$

The TRIAD algorithm is a method of attitude determination that uses two pairs of corresponding vector observations to find a rotation matrix between two reference frames. For each frame, a rotation matrix to a common intermediate frame is created using the relative orientations of the reference vectors in that frame. Once these intermediate rotation matrices are found, they can be combined to create a single rotation matrix between the original two reference frames. The TRIAD algorithm is one of the simplest forms of attitude determination and is still in heavy use today.