November 24, 2024

Rotation Matrix Tutorial for Robotics and Aerospace Engineering – Z-Axis Rotation Matrix


In this robotics and aerospace tutorial, we explain the concept of rotation matrices. The rotation matrices are very important in robotics and aerospace engineering. In this tutorial, we derive the expression for the Z-axis rotation matrix.

We noticed that it is a common misconception among students studying robotics and aerospace that rotation matrices are actually rotating vectors. They are not rotating vectors, they are the consequence of two coordinate systems rotated with respect to each other. Usually, the so-called body coordinate system is fixed to the rotating object, and rotation matrices are used to transform the coordinates of a vector from the body frame to some other (usually inertial) frame. There is another mathematical interpretation in which the rotation matrices are rotating vectors in the same coordinate system, however, this interpretation is not used in robotics and aerospace.

Here is the tutorial we made that explains

  1. What are the rotation matrices.
  2. How to derive the rotation matrices
  3. How the rotation matrices are used to transform coordinates from one coordinate system to another.

The YouTube tutorial accompanying this tutorial is given below.

We consider the situation shown in the figure below.

Figure 1: Rotation of coordinate systems.

We have two coordinate systems A and B, with the coordinates (x_{A},y_{A},z_{A}) and (x_{B},y_{B},z_{B}). The coordinate systems are also called frames. The coordinate system B is rotated with respect to the coordinate system A around the axis z_{A} for the angle \theta. The unit vectors of the coordinate system A are \mathbf{i}_{A},\mathbf{j}_{A} and \mathbf{k}_{A}. The unit vectors of the coordinate system B are \mathbf{i}_{B},\mathbf{j}_{B} and \mathbf{k}_{B}. While reading this post, the reader should keep in mind that vectors are denoted by using bold letters.

Next, let us consider a vector \mathbf{p} shown in the figure below.

Figure 2: Rotated coordinate system with the vector \mathbf{p}.

Problem: Knowing the coordinates of the vector \mathbf{p} in the coordinate system B, and the angle of rotation \theta, find the coordinates of the vector \mathbf{p} in the coordinate system A.

To solve this problem, let us introduce the following notation. The notation

(1)   \begin{align*}\mathbf{p}^{A}=\begin{bmatrix} p_{x_{A}} \\ p_{y_{A}} \\ p_{z_{A}} \end{bmatrix}\end{align*}

denotes the vector \mathbf{p} expressed in the coordinate system A. Similarly, the notation

(2)   \begin{align*}\mathbf{p}^{B}=\begin{bmatrix} p_{x_{B}} \\ p_{y_{B}} \\ p_{z_{B}} \end{bmatrix}\end{align*}

denotes the vector \mathbf{p} expressed in the coordinate system B.

It should be kept in mind that the vectors \mathbf{p}^{A} and \mathbf{p}^{B} are actually denoting the same vector \mathbf{p} only expressed in different coordinate systems. The vector \mathbf{p} expressed in the coordinate system A is

(3)   \begin{align*}\mathbf{p}^{A} =p_{x_{A}} \mathbf{i}_{A}+p_{y_{A}} \mathbf{j}_{A}+p_{z_{A}}\mathbf{k}_{A}\end{align*}

The vector \mathbf{p} expressed in the coordinate system B is

(4)   \begin{align*}\mathbf{p}^{B} =p_{x_{B}} \mathbf{i}_{B}+p_{y_{B}} \mathbf{j}_{B}+p_{z_{B}}\mathbf{k}_{B}\end{align*}

Here one thing should always be kept in mind. The unit vectors of both coordinate systems A and B are actually expressed in the same basis. That is, they are expressed by using coordinates of some other coordinate system. That is why we can mathematically write:

(5)   \begin{align*}\mathbf{p}=\mathbf{p}^{A}=\mathbf{p}^{B}\end{align*}

That is, we have

(6)   \begin{align*}\mathbf{p}^{A}=\mathbf{p}^{B}\end{align*}

By substituting (3) and (4) in (6), we have

(7)   \begin{align*}p_{x_{A}} \mathbf{i}_{A}+p_{y_{A}} \mathbf{j}_{A}+p_{z_{A}}\mathbf{k}_{A}=p_{x_{B}} \mathbf{i}_{B}+p_{y_{B}} \mathbf{j}_{B}+p_{z_{B}}\mathbf{k}_{B}\end{align*}

By scalarly multiplying the equation (7) with \mathbf{i}_{A}, we obtain

(8)   \begin{align*}p_{x_{A}} =p_{x_{B}} \mathbf{i}_{B}\cdot \mathbf{i}_{A}+p_{y_{B}} \mathbf{j}_{B}\cdot \mathbf{i}_{A}+p_{z_{B}}\mathbf{k}_{B}\cdot \mathbf{i}_{A}\end{align*}

By scalarly multiplying the equation (7) with \mathbf{j}_{A}, we obtain

(9)   \begin{align*}p_{y_{A}} =p_{x_{B}} \mathbf{i}_{B}\cdot \mathbf{j}_{A}+p_{y_{B}} \mathbf{j}_{B}\cdot \mathbf{j}_{A}+p_{z_{B}}\mathbf{k}_{B}\cdot \mathbf{j}_{A}\end{align*}

By scalarly multiplying the equation (7) with \mathbf{k}_{A}, we obtain

(10)   \begin{align*}p_{z_{A}} =p_{x_{B}} \mathbf{i}_{B}\cdot \mathbf{k}_{A}+p_{y_{B}} \mathbf{j}_{B}\cdot \mathbf{k}_{A}+p_{z_{B}}\mathbf{k}_{B}\cdot \mathbf{k}_{A}\end{align*}

Let us write the equations (8), (9), and (10) together

(11)   \begin{align*}p_{x_{A}} =p_{x_{B}} \mathbf{i}_{B}\cdot \mathbf{i}_{A}+p_{y_{B}} \mathbf{j}_{B}\cdot \mathbf{i}_{A}+p_{z_{B}}\mathbf{k}_{B}\cdot \mathbf{i}_{A} \\p_{y_{A}} =p_{x_{B}} \mathbf{i}_{B}\cdot \mathbf{j}_{A}+p_{y_{B}} \mathbf{j}_{B}\cdot \mathbf{j}_{A}+p_{z_{B}}\mathbf{k}_{B}\cdot \mathbf{j}_{A} \\p_{z_{A}} =p_{x_{B}} \mathbf{i}_{B}\cdot \mathbf{k}_{A}+p_{y_{B}} \mathbf{j}_{B}\cdot \mathbf{k}_{A}+p_{z_{B}}\mathbf{k}_{B}\cdot \mathbf{k}_{A}\end{align*}

The last three equations can be written in the matrix form as follows

(12)   \begin{align*}\begin{bmatrix}p_{x_{A}} \\p_{y_{A}} \\p_{z_{A}} \end{bmatrix} =\begin{bmatrix}  \mathbf{i}_{B}\cdot \mathbf{i}_{A} & \mathbf{j}_{B}\cdot \mathbf{i}_{A} & \mathbf{k}_{B}\cdot \mathbf{i}_{A} \\ \mathbf{i}_{B}\cdot \mathbf{j}_{A} & \mathbf{j}_{B}\cdot \mathbf{j}_{A} & \mathbf{k}_{B}\cdot \mathbf{j}_{A} \\  \mathbf{i}_{B}\cdot \mathbf{k}_{A} & \mathbf{j}_{B}\cdot \mathbf{k}_{A} & \mathbf{k}_{B}\cdot \mathbf{k}_{A}\end{bmatrix}\begin{bmatrix} p_{x_{B}} \\ p_{y_{B}}  \\ p_{z_{B}} \end{bmatrix}\end{align*}

The last equation can be written compactly

(13)   \begin{align*}\mathbf{p}^{A}=R_{B}^{A}\cdot  \mathbf{p}^{B}\end{align*}

where

(14)   \begin{align*}\mathbf{p}^{A}=\begin{bmatrix}p_{x_{A}} \\p_{y_{A}} \\p_{z_{A}} \end{bmatrix},\;\; \mathbf{p}^{B}=\begin{bmatrix} p_{x_{B}} \\ p_{y_{B}}  \\ p_{z_{B}} \end{bmatrix},\;\; R_{B}^{A} =\begin{bmatrix}  \mathbf{i}_{B}\cdot \mathbf{i}_{A} & \mathbf{j}_{B}\cdot \mathbf{i}_{A} & \mathbf{k}_{B}\cdot \mathbf{i}_{A} \\ \mathbf{i}_{B}\cdot \mathbf{j}_{A} & \mathbf{j}_{B}\cdot \mathbf{j}_{A} & \mathbf{k}_{B}\cdot \mathbf{j}_{A} \\  \mathbf{i}_{B}\cdot \mathbf{k}_{A} & \mathbf{j}_{B}\cdot \mathbf{k}_{A} & \mathbf{k}_{B}\cdot \mathbf{k}_{A}\end{bmatrix}\end{align*}

The matrix R_{B}^{A} is the rotation matrix. The superscript and subscript notations in R_{B}^{A} mean that the rotation matrix transforms projections of a vector from the coordinate system B into the coordinate system A. Let us observe the figure shown below. This figure shows the top view of the unit vectors of the coordinate systems A and B.

Figure 3: Top view of unit vectors of two rotated coordinate systems.

Since the vectors \mathbf{i}_{A},\mathbf{j}_{A},\mathbf{k}_{A} and \mathbf{i}_{B},\mathbf{j}_{B},\mathbf{k}_{B} are unit vectors, we have

(15)   \begin{align*}& \mathbf{i}_{B}\cdot \mathbf{i}_{A}=\cos(\theta),\;\; \mathbf{j}_{B}\cdot \mathbf{i}_{A}= -\sin(\theta) ,\;\; \mathbf{k}_{B}\cdot \mathbf{i}_{A} =0 \\& \mathbf{i}_{B}\cdot \mathbf{j}_{A}= \sin(\theta) ,\;\; \mathbf{j}_{B}\cdot \mathbf{j}_{A}=\cos(\theta) ,\;\; \mathbf{k}_{B}\cdot \mathbf{j}_{A} =0 \\& \mathbf{i}_{B}\cdot \mathbf{k}_{A}= 0  ,\;\; \mathbf{j}_{B}\cdot \mathbf{k}_{A} = 0 ,\;\; \mathbf{k}_{B}\cdot \mathbf{k}_{A} = 1\end{align*}

By substituting (15) in (14), we have

(16)   \begin{align*} R_{B}^{A} =\begin{bmatrix} \cos(\theta) &  -\sin(\theta) & 0 \\\sin(\theta) & \cos(\theta)& 0 \\ 0 & 0 & 1 \end{bmatrix}\end{align*}

The matrix defined in (16) is the rotation matrix defining the rotation of two coordinate systems around the z_{A} axis.

To summarize, the expression

(17)   \begin{align*}\mathbf{p}^{A}=R_{B}^{A}\cdot  \mathbf{p}^{B}\end{align*}

implements a mapping. It transforms the projections of the vector p expressed in the coordinate system B, into the projections of the vector p expressed in the coordinate system A.

Another important property of the rotation matrices is that they are orthogonal, that is,

(18)   \begin{align*}\big(R_{B}^{A}\big)^{-1}=\big( R_{B}^{A} \big)^{T}=R_{A}^{B}\end{align*}

This is important since from (17), we can write

(19)   \begin{align*}\mathbf{p}^{A}=R_{B}^{A}\cdot  \mathbf{p}^{B} \\\big( R_{B}^{A}\big)^{-1}\mathbf{p}^{A}=\mathbf{p}^{B} \\\big( R_{B}^{A}\big)^{T}\mathbf{p}^{A}=\mathbf{p}^{B} \\\mathbf{p}^{B}= R_{A}^{B}\mathbf{p}^{A}    ,\;\;  R_{A}^{B} =\big( R_{B}^{A}\big)^{T}\end{align*}

More about the properties of the rotation matrices can be found here.