# 1 Rotation

Rotation is critical to many applications, e.g., navigation. For 2D rotation, it is trivial. However, for 3D case, it is much more complicated and confusing. This tutorial tries to show some important concept in 3D rotation.

## 1.1 Rotate vector around axis

To rotate around the \(z\)-axis, point the thumb of right hand to the positive \(z\) direction, the curled fingers point to the positive rotation direction (i.e., right hand rule rotation; it is assumed in this tutorial, left hand rule rotation is very similar), i.e., from \(x\)-axis to \(y\)-axis, as show in following figure

Fig. (2) shows the 2D projection of the 3D coordinate system on \(x\)-\(y\) plane, when look from positive \(z\) direction.

For a unit vector on \(z\)-axis (i.e., \(\vec{r} = [0, 0, 1]^T\)), it is easy to see that its coordinate will be unchanged, after rotating around \(z\)-axis by arbitrary \(\psi\) degree (i.e., usually it is called yaw).

For a unit vector on \(x\)-axis, i.e., \(\vec{r} = [1, 0, 0]^T\), after rotating around the \(z\)-axis by \(\psi\) degree, its new coordinates can be easily read from Fig. (5) as \(\vec{r}^\prime = [\cos\psi, \sin\psi, 0]^T\).

Similarly, for a unit vector on \(y\)-axis (i.e,. \(\vec{r} = [0, 1, 0]^T\)), after rotating around \(z\)-axis by \(\psi\), its coordinate will be \(\vec{r}^\prime = [-\sin\psi, \cos\psi, 0]^T\).

For an arbitrary vector \(\vec{r}\), what will its coordinate be in the original coordinate system (i.e., \(xyz\)) after rotation?

Suppose the coordinates of a vector in \(xyz\) is \(\vec{r}=r_x\vec{i} + r_y\vec{j} + r_z\vec{k}\), when rotate it around \(z\)-axis by \(\psi\) degree, if the coordinate system is also rotated along with it (e.g., around \(z\)-axis by \(\psi\) degree) to \(x^\prime y^\prime z^\prime\), then its coordinates in \(x^\prime y^\prime z^\prime\) is same as before (it is straightforward as their relative location doesn't change), that is

If we know the coordinates of \(\vec{i}^\prime\), \(\vec{j}^\prime\) and \(\vec{k}^\prime\) in the original coordinate system \(xyz\), Eq. (\ref{eqn:rotate_z_arbitrary}) will tell us the corresponding coordinates of the rotated vector \(\vec{r}^\prime\) in \(xyz\). \(\vec{i}^\prime\) is just the rotated unit vector on \(x\)-axis (\([1,0, 0]^T\)). As discussed above, its coordinates in \(xyz\) is \([\cos\psi, \sin\psi, 0]^T\), that is

Similarly for \(\vec{j}^\prime\) and \(\vec{k}^\prime\),

Substitute Eqs. (\ref{eqn:rotate_z_x_axis}) and (\ref{eqn:rotate_z_yz_axis}) in Eq. (\ref{eqn:rotate_z_arbitrary}), we have

Thus the rotation matrix around \(z\)-axis is

It is easy to see that the \(1\)st column of \(\mathbf{R}_z\) is just the coordinate of rotated unit vector of \(x\)-axis, that is, after rotation, the coordinates of unit vector on \(x\)-axis (i.e.,\([1, 0, 0]^T\) before rotation) will become \([\cos\psi, \sin\psi, 0]^T\). Same for the \(2\)nd and \(3\)rd columns.

The rotation around \(x\)-axis (i.e., \(\phi\), usually it is called as roll) is very similar, as shown in Fig. (5).

Its rotation matrix is

It is easy to verify that its \(1\)st column is \([1, 0, 0]^T\), as rotation around \(x\)-axis doesn't impact the vector on \(x\)-axis; for the \(2\)nd column, rotate the unit vector on \(y\)-axis (i.e., \([0, 1, 0]^T\)) around \(x\)-axis by \(\phi\) degree, its coordinates will become \([0, \cos\phi, \sin\phi]^T\) as shown in Fig. (5); and \(3\)rd means that after rotating unit vector on \(z\)-axis (i.e., \([0, 0, 1]^T\)) around \(x\)-axis by \(\phi\) degree, its coordinates will become \([0, -\sin\phi, \cos\phi]^T\).

The rotation around \(y\) is also similar (\(\theta\), i.e., pitch), as shown in Fig. (6).

Its rotation matrix is

You may notice that the format of Eq. (\ref{eqn:rotate_y}) is slightly different with Eqs. (\ref{eqn:rotate_z}) & (\ref{eqn:rotate_x}), e.g., its \(1\)st row is \([\cos\theta, 0, \sin\theta]^T\), instead of \([\cos\theta, 0, \textcolor{red}{-}\sin\theta]^T\). That's not a mistake, as it is easy to check that for a unit vector on \(z\)-axis (i.e., \(\vec{r} = [0, 0, 1]^T\)), rotate it around \(y\)-axis by \(\theta\) degree, its coordinate will be \([\sin\theta, 0, \cos\theta]^T\), which is the \(3\)rd column of \(\mathbf{R}_y\). Same for its \(1\)st column.

For the discussion so far, we are rotating the vector \(\vec{r}\) to get the coordinates of \(\vec{r}^\prime\) in original coordinate system after rotation.
Such rotation is called **active rotation** (or **active transformation**).

On the other hand, we can also fix \(\vec{r}\), rotate the coordinate system, then calculate the coordinates of \(\vec{r}\) in the rotated coordinate system (i.e., \(x^\prime y^\prime z^\prime\)). Such rotation is called **passive rotation**. As show in Fig. (7), after rotating the coordinate system by \(-\psi\) degree, the coordinates of \(\vec{r}\) in the new coordinate system \(x^\prime y^\prime z^\prime\) is same as the one of \(\vec{r}^\prime\) in original coordinate system \(xyz\). In other words, for a vector \(\vec{r}\) in \(xyz\) coordinate system, the following two procedures are equivalent

- rotate the vector \(\vec{r}\) around some axis by degree \(\theta\),
- rotate the coordinate system \(xyz\) around some axis by degree \(-\theta\).

### 1.1.1 Multiple rotations

It is easy to see that if we rotate \(\vec{r}\) around \(z\) by \(\psi_1\) (\(\vec{r}^\prime\)), then rotate \(\vec{r}^\prime\) again around \(z\) by \(\psi_2\) (\(\vec{r}^{\prime\prime}\)), it is equivalent to rotate \(\vec{r}\) around \(z\) by \(\psi_1 + \psi_2\).

It can also been easily proved by matrix multiplication

Note when we do matrix multiplication, \(\mathbf{R}_z(\psi_2)\) is on the left side, and \(\mathbf{R}_z(\psi_1)\) is on the right side. This is because we rotate by \(\psi_1\) first, then \(\psi_2\). For this specific case, the order doesn't really matter as both rotation is around the same \(z\)-axis. However, in general, the rotation order matters as we will see shortly.

As shown in Fig. (9), if \(\vec{r}=[1, 0, 0]^T\),

- first rotate it around \(z\)-axis by 90 degree, we will get \(\vec{r}^\prime = [0, 1, 0]^T\);
- then rotate \(\vec{r}^\prime\) around \(x\)-axis by 90 degree, we will get \(\vec{r}^{\prime\prime} = [0, 0, 1]^T\).

However, if we switch the rotation order,

- first rotate \(\vec{r}\) around \(x\)-axis by 90 degree, we will get \(\vec{r}_2^\prime =[1, 0, 0]^T\);
- then rotate \(\vec{r}_2^\prime\) around \(z\)-axis by 90 degree, we will get \(\vec{r}_2^{\prime\prime} = [0, 1, 0]^T\).

Apparently \(\vec{r}^{\prime\prime}\) is not equal to \(\vec{r}_2^{\prime\prime}\). Do you see why this is the case? One observation is that in general, \(\mathbf{R}_2\mathbf{R}_1 \neq \mathbf{R}_1\mathbf{R}_2\).

The rotations in Fig. (9) are all around the axis in coordinate system \(xyz\). Such rotation is called **extrinsic rotation**. The other way is at each rotation, assume the coordinate system also rotate the same angle, and the next rotation will be around the corresponding axis of the rotated coordinate system. Such rotation is called **intrinsic rotation**.

For intrinsic rotation to achieve the same final coordinates (in original \(xyz\) coordinate), we need to reverse the rotation order. For the previous example, in extrinsic rotation

- rotate around \(z\)-axis by 90 degree, then
- rotate around \(x\)-axis by 90 degree,

and its rotation matrix is

Notice that the order of the rotation matrix, that is the \(1\)st rotation is on the rightest side, then the matrix from the \(2\)nd rotation. If there are more rotations, they will also be ordered from right to left.

While in intrinsic rotation, to get the same coordinates (\(\vec{r}^{\prime\prime}\)),

- rotate around \(x\)-axis by 90 degree, also rotate the coordinate system to get \(x^\prime y^\prime z^\prime\), as show in Fig. (10), then
- rotate around \(z^\prime\)-axis by 90 degree.

Fig. (10) shows that after the intrinsic rotations, the coordinate of \(\vec{r}^{\prime\prime}\) is same as the one in Fig. (9).

The rotation matrix from intrinsic rotation is

Eq. (\ref{eq:rotation_z90_x90_rotate_intrinsic}) is same as Eq. (\ref{eq:rotation_z90_x90_rotate}). It is expected as both rotation reached the same \(\vec{r}^{\prime\prime}\). However, different from extrinsic rotation, in intrinsic rotation, the rotation matrices are ordered from left to right, that is the \(1\)st intrinsic rotation is on the leftest side, then the \(2\)nd rotation matrix. If you are curious why the order is arranged this way, here is a good tutorial.

## 1.2 Rotate vector around a vector

For the discussion so far, we are talking about rotating a vector \(\vec{r}\) around some axis. How about rotating around arbitrary unit vector? Fortunately, there is a equation called Rodgrigues's rotation formula

where

and

So the rotation matrix can be written as

For example if \(\vec{k}=[0, 0, 1]^T\) (i.e., rotate around \(z\)-axis), then

and

it is same as Eq. (\ref{eqn:rotate_z}).

Eq. (\ref{eqn:rotate_vector}) is not the only way to represent the rotation with angle \(\theta\) around some unit vector \(\vec{k}\). Another way is the exponential map

which can be shown to be equivalent to Eq. (\ref{eqn:rotate_vector}) with Taylor expansion.

## 1.3 Vector coordinates in rotated coordinate systems

For the discussion so far, we are talking about rotating a vector \(\vec{r}\) to \(\vec{r}^\prime\), and get its coordinate in the original coordinate system. Another important problem is if we have

- coordinate system \(xyz\),
- coordinate system \(x^\prime y^\prime z^\prime\), which is rotated from \(xyz\),
- a vector, i.e., \(\vec{r}\), and its coordinates in one coordinate system (e.g., \(xyz\)),

how to get its coordinates in the other coordinate system (e.g., \(x^\prime y^\prime z^\prime\))?

For example, in Fig. (11), coordinate system \(x^\prime y^\prime z^\prime\) is got by rotating \(xyz\) around \(x\)-axis by 45 degree. It is easy to see \(\vec{x}^\prime=\vec{x}\).

Apparently, in \(xyz\) coordinate system, the coordinates of \(\vec{r}\) is \([0, 1, 0]^T\), and in \(x^\prime y^\prime z^\prime\) coordinate system, its coordinates are \([0, \frac{\sqrt{2}}{2}, -\frac{\sqrt{2}}{2}]^T\). For this toy example, it is trivial to get the coordinates in both coordinate systems. But in general, how do we get the coordinates in the other coordinate system?

Start with \(xyz\) coordinate system (the coordinates \(\vec{r}\) are known), rotate \(xyz\) around \(x\)-axis by **45** degree, we get the new coordinate system \(x^\prime y^\prime z^\prime\) (passive rotation). To get the coordinates of \(\vec{r}\) in this new coordinate system, it is equivalent to keep the \(xyz\) fixed, then rotate \(\vec{r}\) around \(x\)-axis by **-45** degree (active rotation), that is

That matches with our observation. Another way to solve this problem is in \(x^\prime y^\prime z^\prime\) coordinate system, if we rotate \(\vec{r}\) around \(x\)-axis by 45 degree, then we know its coordinate will be \([0, 1, 0]^T\), that is

which is same as Eq. (\ref{eqn:rotate_x45}).

Let's see what happens for multiple rotations. In Fig. (12), \(x^\prime y^\prime z^\prime\) from Fig. (11) is rotated 45 degree around \(z\)-axis to reach \(x^{\prime\prime}y^{\prime\prime}z^{\prime\prime}\). Apparently, in \(x^{\prime\prime}y^{\prime\prime}z^{\prime\prime}\), the coordinates of \(\vec{r}\) is \([\frac{\sqrt{2}}{2}, \frac{1}{2}, -\frac{1}{2}]^T\).

Same as before, let us start with \(x^\prime y^\prime z^\prime\) coordinate system (the coordinates in \(x^\prime y^\prime z^\prime\) (i.e., \(\vec{r}^\prime\)) are known as we shown above), rotate \(x^\prime y^\prime z^\prime\) around \(z\)-axis by **45** degree, we get the new coordinate system \(x^{\prime\prime} y^{\prime\prime} z^{\prime\prime}\). To get the coordinates of \(\vec{r}\) in this new coordinate system, it is equivalent to keep the \(x^\prime y^\prime z^\prime\) fixed, then rotate \(\vec{r}^\prime\) around \(z\)-axis by **-45** degree, that is

Or

However, such result doesn't match with our expectation. What's wrong here? The problem is that we want to get the coordinates of \(\vec{r}^\prime\) in \(x^{\prime\prime}y^{\prime\prime}z^{\prime\prime}\) coordinate system, to do that, we rotate \(x^\prime y^\prime z^\prime\) around \(z\)-axis, in \(x^\prime y^\prime z^\prime\), we are rotating the coordinate system around unit vector \(\vec{k}^\prime=[0, \frac{\sqrt{2}}{2}, \frac{\sqrt{2}}{2}]^T\) by 45 degree. Equivalently, if \(x^\prime y^\prime z^\prime\) is fixed, \(\vec{r}^\prime\) is rotated around \(\vec{k}^\prime\) by -45 degree. According to Eq. (\ref{eqn:rotate_vector}),

Then

which is same as the expected coordinates.

The other way to start with \(xyz\) (Fig. (12)), rotate it around \(z\)-axis 45 degree (along with \(\vec{r}\)), then rotate around the new \(x\)-axis (i.e, \(x^{\prime\prime}\)-axis) 45 degree (intrinsic rotation), after these rotations, \(xyz\) will overlap with \(x^{\prime\prime}y^{\prime\prime}z^{\prime\prime}\), and the coordinates of \(\vec{r}\) in \(x^{\prime\prime}y^{\prime\prime}z^{\prime\prime}\) is \([0, 1, 0]^T\),

Equivalently,

Let's see a real example, as shown in Fig. (13),

- the origin point is the earth center, \(xyz\) is the earth-centered, earth-fixed coordinate system (we will talk more about this coordinate system soon),
- an object (e.g., a satellite) is in the direction of \(\vec{r}\), which is represented by two angles (i.e., azimuth \(az\), and elevation \(el\)),
- the object has its own coordinate system \(x^\prime y^\prime z^\prime\) (rotate \(xyz\) around \(\vec{x}\)-axis by \(az\) degree, then rotate around the new \(y\)-axis by \(-el\) degree),

if we know coordinates of some vector (e.g., \(\vec{r}\)) in one coordinate system (e.g., \(x^\prime y^\prime z^\prime\)), how to find its corresponding coordinates in the other (e.g., \(xyz\))?

For example for unit vector \(\vec{r}\), we know its coordinates in \(x^\prime y^\prime z^\prime\) is \([1, 0, 0]^T\). We can

- Rotate \(\vec{r}\) around \(y^\prime\) by \(el\) degree, after that, \(z\) will overlap with \(z^\prime\),
- Then rotate \(\vec{r}\) again around \(z\) (or the rotated \(z^\prime\)) by \(-az\) degree, after that, the \(xyz\) coordinate system will perfectly overlap with \(x^\prime y^\prime z^\prime\),
- So after these rotations, the coordinates of \(\vec{r}\) in \(xyz\) coordinate system is \([1,0,0]^T\) (or \(\vec{r}\) in \(x^\prime y^\prime z^\prime\), denoted it as \(\vec{r}^\prime\)).

The process can be written as

which is the mapping from a coordinates in \(xyz\) coordinates to \(x^\prime y^\prime z^\prime\) coordinate system. Do you see why we layout the rotations from left to right (i.e.,\(1\)st rotation on the leftest side)?

And equivalently, to convert a coordinates in \(x^\prime y^\prime z^\prime\) to \(xyz\) coordinate system,

which is the same equation as Eq. (2-2) here (ignore the polarization).

To get the coordinate of \(\vec{r}\) in \(xyz\) coordinate system, another way is to start with a vector in \(xyz\) coordinate system with same coordinates of \(\vec{r}\) in \(x^\prime y^\prime z^\prime\) (i.e., \(\vec{r}^\prime=[1, 0, 0]\)),

- rotate the vector around \(z\)-axis by \(az\) degree. After the rotation, the new \(x\)-axis will align with the projection of \(\vec{r}\) on \(xy\) plane (\(xy\) plane doesn't change in this rotation).
- then rotate the vector around the new/rotated \(y\)-axis by \(-el\) degree.

After these rotations, the vector \([1,0,0]^T\) (same coordinates in \(x^\prime y^\prime z^\prime\), i.e., \(\vec{r}^\prime\)) in \(xyz\) will overlap with \(\vec{r}\), that is

# 2 Frame

Frame is just another word for coordinate system. In this section, we will introduce some common frames that are widely used in navigation.

## 2.1 ECEF

Earth-centered, Earth-fixed (ECEF) coordinate system is a convenient way to location any point relative to earth

- \(z\)-axis is from earth center to the north pole,
- \(x\)-axis is on the equator plane, from earth center to the prime meridian (longitude is \(0^\circ\)),
- \(y\)-axis is also on the equator plane, from earth center to \(90^\circ\) longitude.

## 2.2 GSC

Geographic coordinate system is to define locations on earth surface, which is used by satellite navigation (e.g., GPS), for example for some location \(A\)

- Longitude (\(\lambda\)) is the rotation angle between prime meridian plane and the meridian plane \(A\) is on,
- Latitude (\(\phi\)) is the angle between the equatorial plane and the line that passes \(A\) and is normal to the ellipsoid (note, the line may not point to the center of earth as the earth is not a sphere),
- Height (\(h\)) is the distance between the point of interest and the ellipsoid surface.

As shown in [1], to convert GSC to ECEF frame

where \(a\) and \(b\) are the equatorial radius (e.g., \(xz\) plane at \(y=0\)) and the polar radius (center to north/south polar), and

To see how the above conversion works, let's see a simplified case with \(\lambda=0\), as shown in Fig. (16).

As \(p_o\) is on the ellipse, so we have

Take the derivative over \(x\), we have

So

That's the slope of the tangent line at \(p_0\) to the ellipse. And line \(p \rightarrow p_o\) is orthogonal to the tangent line, so its slope is \(\frac{z_{p_o}a^2}{x_{p_o}b^2}\), that is

With Eqs. (\ref{eqn:gcs_meridian_ellipse}) and (\ref{eqn:gcs_meridian_slope}), \(x_{p_o}\) and \(y_{p_o}\) can be calculated as

The coordinates of \(p\) is straightforward

It is easy to see that Eq. (\ref{eqn:gsc_to_ecef2}) is same as Eq. (\ref{eqn:gsc_to_ecef}) when \(\lambda=0\).

The case when \(\lambda\) is not equal to \(0\) is straightforward, see here for more details.

The ellipsoid parameter for WGS84 is,

## 2.3 NED

North, east, down (NED) coordinate system is shown in Fig. (17),

- \(x\)-axis is tangent to meridians (to north),
- \(y\)-axis is tangent to parallels (circles from same latitude) (to east),
- \(z\)-axis is down to the earth center (to down).

Rotation from NED frame to ECEF frame,

where \(\vec{r}_{NED}\) is the coordinates in NED frame, \(\vec{r}_{ECEF}\) is the corresponding coordinates in ECEF frame, and \(\vec{o}_{ECEF}\) is the origin of the NED frame in ECEF frame. The rotation matrix can be written as

Or the rotation matrix from ECEF frame to NED frame is

NED frame is not well-defined at the poles. For example, at north pole, any direction is south, so no way to define **x**-axis that points to north.

## 2.4 ENU

East, north, up (ENU) coordinate system, as shown in Fig. (18), is very close to NED coordinate system.

- \(x\)-axis is tangent to parallels (circles from same same latitude) (to east),
- \(y\)-axis is tangent to meridians (to north),
- \(z\)-axis is up from the earth center (to up).

Rotation from ENU frame to ECEF frame,

where \(\vec{r}_{ENU}\) is the coordinates in ENU frame, \(\vec{r}_{ECEF}\) is the corresponding coordinates in ECEF frame, and \(\vec{o}_{ECEF}\) is the origin of the NED frame in ECEF frame. The rotation matrix can be written as

Or the rotation matrix from ECEF frame to ENU frame is

Similar to NED frame, ENU frame is also not well-defined at the poles. For example, at north pole, any direction is south, so no way to define **x**-axis that points to east.

## 2.5 Satellite

Satellite frame is shown in Fig. (19),

- \(x\)-axis is up from the earth center (to satellite),
- \(y\)-axis is in the polarization plane and along the electric field vector direction,
- \(z\)-axis is in the polarization plane and orthogonal to the \(y\)-axis (along the magnetic filed vector direction).

Rotation from satellite frame to ECEF frame,

where \(\vec{r}_{satellite}\) is the coordinates in satellite frame, \(\vec{r}_{ECEF}\) is the corresponding coordinates in ECEF frame, and \(\vec{o}_{ECEF}\) is the origin of the satellite frame in ECEF frame. The rotation matrix can be written as

Or the rotation matrix from ECEF frame to satellite frame is

## 2.6 Body

Body frame is the user coordinate system, usually user can arbitrarily choose the coordinate system, e.g., when it is leveled on ground, and facing north, the body frame is same as NED frame, that is

- \(x\)-axis points to the nose of the body (or heading direction),
- \(y\)-axis points to the right of the \(x\)-axis (when facing forward),
- \(z\)-axis points down through the bottom of the body.

In other words, in this case, when the body is facing north and leveled (yaw, pitch, roll are all \(0\)), its \(x\)-axis points north, \(y\)-axis points east, and \(z\)-axis points down to the earth center.

The main task for many applications is to estimate the body orientation (i.e., yaw, pitch and roll) (e.g., with reference to NED frame). And inertial measurement unit (IMU) is usually attached to the body to help estimating the body orientation. In many cases, the body frame may be same as the IMU frame.

### 2.6.1 Estimate pitch and roll with accelerometer

When the IMU is leveled, its reading in NED frame is

where \(g\) is the gravitational acceleration (e.g., \(\sim 9.81m/s^2\) on earth surface). It is easy to see that rotate around NED \(z\)-axis doesn't change the measured signal. The measurement is in IMU frame (or body frame)

And define the rotation from IMU frame (body frame) to NED frame as (assume no more acceleration besides gravitational acceleration \(g\))

Or equivalently,

then \(\theta\), \(\phi\) can be estimated as

### 2.6.2 Gyroscope

Gyroscope measures the angular velocity (e.g., \(deg/s\)), e.g.,

where \(\vec{\omega}\) is the true angular velocity, \(\vec{b}\) is the bias, and \(\vec{n}\) is the additive noise.

In practice, the gyroscope may send the measured angular velocity at some fixed rate (e.g., 200Hz), that is, every \(\delta_t\) seconds. How to use such information to update the orientation of the body/IMU frame? For example, at time index \(t-1\), we have the rotation matrix \(\mathbf{R}_{t-1}\), that maps arbitrary vector in body frame (\(\vec{r}_{t-1, body}\)) to NED frame (\(\vec{r}_{t-1, NED}\)),

or

And at time index \(t\) (i.e., \(\delta_t\) seconds after time index \(t-1\)), gyroscope sends out the angular velocity measurement \(\hat{\vec{w}}_t\). As \(\delta_t\) is so small, it is not unreasonable to think that \(\hat{\vec{w}}_t\) is constant over the last \(\delta_t\) seconds. And if we ignore the gryo bias \(\vec{b}\) for simplicity now, during the last \(\delta_t\) seconds, the body/IMU frame rotates with angle \(\hat{\vec{w}}_t \delta_t\). Thus the rotation from body/IMU frame of time \(t-1\) to \(t\) is

You may notice the negative sign before the rotation angle in the above equation. That's because the measured rotation is on the frame (coordination system) (i.e., passive rotation), if we want to apply the same rotation on the vector, it needs to be rotated in negative angle (active rotation).

or

The only remaining question is how to get the rotation matrix (e.g., \(\mathbf{R}(-\hat{\vec{w}}_t \delta_t)\)) from the gyro measurement. As shown in [2], for the measurement \(\hat{\vec{\omega}} = [\hat{\omega}_x, \hat{\omega}_y, \hat{\omega}_z]\), the corresponding rotation can be viewed as rotating around unit vector \(\frac{\hat{\vec{\omega}}}{\lVert \hat{\vec{\omega}}\rVert}\) by \(\lVert \hat{\vec{\omega}}\rVert \delta_t\) degree. The rotation matrix can be got with Eq. (\ref{eqn:rotate_vector}).

# 3 Rotation representation

## 3.1 Rotation matrix

Rotation matrix is a straightforward way to represent a rotation

It clearly states that its a mapping from one coordinate system to the other. For the discussion so far, we adopt the convention of expressing vectors a column. In this case, the vector is rotated by pre-multiplying the rotation matrix, that is

Rotation matrix has some nice properties. From Eq. (\ref{eqn:rodgrigues_matrix}), it is easy to see that

as \(\mathbf{R}(\theta)\mathbf{R}(-\theta) = \vec{I}\), and \(\mathbf{R}(\theta) = \mathbf{R}(-\theta)^T\).

In some literature, the vector is expressed as a row, and the rotation is applied by post-multiplying the rotation matrix,

In this case, the rotation matrix is transposed.

Direct cosine matrix is also very close to the rotation matrix defined above. The only difference is that it rotates the coordinate system, instead of the vector, that is

### 3.1.1 Rotation matrix to rotation vector and angle

Eq. (\ref{eqn:rodgrigues_matrix}) shows how to get the rotation matrix from the unit vector and the rotation angle. To get the rotation vector (rotation axis, i.e., \(\vec{k}\)) and the corresponding rotation angel (\(\theta\)) from the rotation matrix, you may observe that, if we rotate around the rotation vector, then its coordinates will not change,

which means that \(\vec{k}\) is the eigenvector of \(\mathbf{R}\), corresponding to eigenvalue 1.

And the trace of Eq. (\ref{eqn:rodgrigues_matrix}) is

## 3.2 Euler angle

### 3.2.1 Euler angle to rotation matrix

Suppose we have Euler angle \(\phi\), \(\theta\) and \(\psi\) that are rotation around \(x\), \(y\), and \(z\)-axis respectively. And if the rotation is in \(xyz\) order (i.e., \(x\)-axis first), then the rotation matrix can be written as

After simplification, it is [3]

### 3.2.2 Rotation matrix to Euler angle

With Eq. (\ref{eqn:rotation_euler}), if \(\mathbf{R}\) is known, \(\theta\) can be estimated with

that is \(\theta = -\arcsin(m_{20})\). And when \(\cos\theta\) is not \(0\), \(\phi\) and \(\psi\) can be estimated with

If \(\cos\theta\) is \(0\) (i.e., \(\theta=\pm 90^\circ\)), \(\phi\) and \(\psi\) will not have unique solution as we will show shortly.

### 3.2.3 Gimbal lock

In Eq. (\ref{eqn:rotation_euler}), if \(\theta=90^\circ\), the rotation matrix becomes

In this case, from the rotation matrix \(\mathbf{R}\), there is no way to identify both \(\phi\) and \(\psi\), and all we have is \(\phi-\psi\). In this case, we basically lose one degree of freedom (we can arbitrarily set \(\phi\) to be \(0\), then get the corresponding \(\psi\)).

Similarly when \(\theta=-90^\circ\), the rotation matrix becomes

## 3.3 Quaternion

The concept of quaternion is very close to complex number. The only difference is that it has 4 components, i.e.,

Its conjugate is

Its norm is

Its inverse is

Rotation around a unit vector \(\vec{k} = [k_x, k_y, k_z]^T\) by angle \(\theta\) can be represented by a quaternion as

It is easy to show that \(\lVert\vec{q}\rVert=1\). Let's see what we get by applying \(\vec{q}\vec{r}\vec{q}^*\),

Here we align the components of each quaternion, so we can apply the quaternion multiplication easily. After simplification, it can be written as

For example, for the top left item in the rotation matrix (the item corresponds to \(r_x\mathbf{i}\)),

It is easy to see that Eq. (\ref{eqn:rodgrigues_matrix_quat}) is same as the rotation matrix in Eq. (\ref{eqn:rodgrigues_matrix}).

### 3.3.1 Quaternion to rotation matrix [4]

For a general rotation quaternion \(\vec{q}=q_0 + q_1\mathbf{i} + q_2\mathbf{j} + q_3\mathbf{k}\),

### 3.3.2 Rotation matrix to quaternion

For general rotation matrix Eq. (\ref{eqn:rotation_matrix_general}), when compared it with Eq. (\ref{eqn:rotation_matrix_quat}), it is easy to see

Or

The specific case is \(q_0=0\). In this case, we can try some other solutions

Or

Or

Since for rotation quaternion, \(\lVert\vec{q}\rVert=1\), so \(q_0\), \(q_1\), \(q_2\) and \(q_3\) can't be all zero, so it is guaranteed that at least one of Eqs. (\ref{eqn:rotation_matrix_to_quat_q0}), (\ref{eqn:rotation_matrix_to_quat_q1}), (\ref{eqn:rotation_matrix_to_quat_q2}) and (\ref{eqn:rotation_matrix_to_quat_q3}) will work.

### 3.3.3 Exponential

For an imaginary quaternion, i.e., \(\vec{q}=q_1\mathbf{i} + q_2\mathbf{j} + q_3\mathbf{k}\), its exponential can be written as

The proof can be found here.

In Sec. 2.6.2, we mentioned that rotation according to the gyroscope measurement can be represented as rotating around unit vector \(\frac{\hat{\vec{\omega}}}{\lVert \hat{\vec{\omega}}\rVert}\) by \(\lVert \hat{\vec{\omega}}\rVert \delta_t\) degree. With quaternion, such rotation (i.e.,\(\mathbf{R}(-\hat{\vec{w}}_t \delta_t)\) in Eq. (\ref{eqn:rotate_gyro_ned2body})) can be written as

## 3.4 Rotation axis and angle

Obviously, rotation can be represented with a unit rotation vector (rotation axis, \(\vec{k}\)) and rotation angle (\(\theta\)). Put them together, the rotation can be represented as \([\theta, \vec{k}]\). Since \(\lVert \vec{k} \rVert = 1\), \(\theta\) can be combined into \(\vec{k}\), that is \(\theta\vec{k}\).

It is easy to convert it to quaternion

and according to Eq. (\ref{eqn:angle_vector_to_quat}), the quaternion can be constructed as

If \(\theta\) is positive, then \(\theta^\prime = \theta\) and \(\vec{k}^\prime = \vec{k}\), so the constructed rotation is exactly same as the original one.

If \(\theta\) is negative, then \(\theta^\prime = -\theta\) and \(\vec{k}^\prime = -\vec{k}\), it is easy to see that the constructed rotation is also same as the original one.