Rotations with Quaternions
Transformation matrices are an essential component to TF2. They allow for all the different components in a robot to use different reference axes (or frames) and their relationships. A Concise Introduction to Robot Programming with ROS2 (Rico 2023, pg. 65-66) gives a good overview of the general process that TF2 uses:
Algebraically, [relating frames] is done using homogeneous coordinates of a point
in a frame
, this is
, we can calculate
in frame
using the transformation matrix
as follows:


TF2 uses messages of type tf2_msgs/msg/TFMessage, which holds the following data:
$ ros2 interface show tf2_msgs/msg/TFMessage
geometry_msgs/TransformStamped[] transforms
std_msgs/Header header
string child_frame_id
Transform transform
Vector3 translation
float64 x
float64 y
float64 z
Quaternion rotation
float64 x 0
float64 y 0
float64 z 0
float64 w 1
As transformations are propagated through a system’s TF tree, it is standard for the output header to match the input header. The child frame ID is the ID of the new frame to be created by a given transformation.
The transform itself consists of a translation and a rotation. The translation describes the Euclidean distance between the “origin” of each frame. For example, this could be the distance from the robot’s center to the center of one of its sensors. The rotation describes how the frame is rotated after the translation is applied. For example, two of RoboFlock’s ultrasonic sensor’s do not face the same direction as the robot’s “forward” direction, so a rotation would need to be applied.
This message definition contains an interesting term: quaternion. Quaternions are mathematical objects used to described 3D orientations and rotations. From Quaternions: Theory and Applications (Griffin 2017, pg.88-89):
[The] attitude of a rigid body can be represented by unit quaternion, consisting of a unit vector
known as the Euler axis, and a rotation angle
about this axis. The quaternion
is the defined as follows:

where
![\begin{align} \mathbb{H} &= \ \ \begin{array}{ll} \qquad \ \{ \ q \ | \ q_{0}^{2} + \vec{q}^{\ T} \ \vec{q} = 1 \\ q = [ \ q_{0} \ \vec{q}^{\ T} \ ]^{\ T}, \ q_{0} \in \mathbb{R}, \ \vec{q} \in \mathbb{R}^{3} \ \} \end{array} & (2) \end{align}](../../_images/math/414306d9437f99be7d0fb830e01f9f79f0e8a461.png)
and
are known as the vector and scalar parts of the quaternion respectively. In attitude control applications, the unit quaternion represents the rotation from an inertial coordinate systen
located at some point in the space (for instance, the earth NED frame), to the body coordinate system
located on the center of mass of a rigid body.
If
is a vector expressed in
, then its coordinates in
are expressed by:

where
and
are the quaternions associated to vectors
and
respectively.
denotes the quaternion multiplication and
is the conjugate quaternion multiplication of
, defined as:
![\begin{align} \overline{q} &= [q_{0} - \vec{q}^{\ T}]^{T} & (4) \end{align}](../../_images/math/541866b87147745643a7b943e39d97695b27f6ab.png)
The rotation matrix
corresponding to the attitude quaternion
, is computed as:
![\begin{align} C(q) &= (q_{0}^{2} - \vec{q}^{\ T} \ \vec{q})I_{3} + 2(\vec{q} \vec{q}^{\ T} - q_{0} [\vec{q}^{x}]) & (5) \end{align}](../../_images/math/182decbe0302e34a29175fa02e2376dc9b69f36b.png)
where
is the identity matrix and
is a skew symmetric tensor associated with the axis vector
:
![\begin{align} [\xi^{x}] &= \begin{pmatrix} \xi_{1} \\ \xi_{2} \\ \xi_{3} \end{pmatrix}^{x} = \begin{pmatrix} 0 & -\xi_{3} & \xi_{2} \\ \xi_{3} & 0 & -\xi_{1} \\ -\xi_{2} & \xi_{1} & 0 \end{pmatrix} & (6) \end{align}](../../_images/math/ff6b661c66463d6e8e09e83f6610a860faf5c7bd.png)
Thus, the coordinate of vector
expressed in the
frame is given by:

Let’s break this down, one equation at a time:
The Quaternion Definition
A quaternion
can also be described by
with
being its rotational scalar and
being the coefficients of its basis vectors. We can also visualize this as a
column vector, but we’ll use the given coefficients instead of
:

The Set of All Quaternions
We can think of
in a similar way as the definition of a unit circle,
. Remember that the dot prouct
(which is equivalent to
) produces a scalar value. If we expand this notation out, our equation looks like this:

The other rule,
, simply means that the quaternion
is a column vector in which the first entry is the rotational scalar
, and the following three entries corresponding to quaternion’s 3D vector.
Quaternion Multiplication
Quaternion multiplication is a non-commutative (i.e., order matters) operation that essentially applies each quaternion’s rotation in succession. Consider quaternions
and
. We define their product,
, as follows:

Conjugate Quaternion Multiplication
Here we refer to the quaternion’s conjugate, which is obtained by negating each of the vector’s entries, effectively inverting the direction of the quaternion’s vector component. In our case, the conjugate is:

Rotation Matrices
Rotation matrices are use in matrix multiplication with column vectors to enact some rotation in the vector’s plane by some angle. Here, we are given the rotation matrix
that is made up of four components:
results in an arbitrary scalar value that we’ll denote as
.
is the
identity matrix, i.e., its diagonal entries are all 1 and all other entries are 0.
is the dot product between the quaternion’s vector component and itself, which we will denote as
.
is a skew symmetric tensor with axis vector
.
Note
Tensors are a whole discussion of their own, so we won’t go into depth about them, but here’s a broad overview:
Tensors are algebraic objects that describe multilinear relationships (i.e., relationships between multiple linear variables) between sets of algebraic objects associated with a vector space, such as vectors, scalars, matrices, and other tensors.
Skew-symmetric tensors are tensors whose components change signs when any two indices are swapped, i.e., for some tensor
, we have
.
Given each of these components, the resulting rotation matrix should look like:

Notice the symmetry between the columns and the rows. Each axis moves in its own plane by the scalar
. In the remaining two planes, the axis moves by the scalar
times the respective vector component
. This is how quaternions end up describing 3D rotations! The rotation angle
, which controls the value of
, ultimately decides how a quaternion acts on a 3D object.
In the context of ROS2, we can think of our odom frame as being the Euler axis,
. Describing our translations between frames is essentially defining a value of
that can enact the necessary rotations between any two frames. By setting up the relationships between frames, we are giving TF2 the information it needs to use dynamic translations that update as the robot moves around. In other words, TF2 gives the robot a sense of its surroundings.
References
Griffin, S. (Ed.). (2017). Quaternions : Theory and applications. Nova Science Publishers, Incorporated.
Rico, F. (2023). A Concise Introduction to Robot Programming with ROS2. CRC Press.
results in an arbitrary scalar value that we’ll denote as
identity matrix, i.e., its diagonal entries are all 1 and all other entries are 0.
is the dot product between the quaternion’s vector component and itself, which we will denote as
.
is a skew symmetric tensor with axis vector
.