Skip to the lesson
FixTheRobot Explore Robotics / Localization & SLAM / M2 Mathematical Foundations / M2-T3

Localization & SLAM · Theory · M2

M2-T3 — Rotation Matrices, Quaternions, and Pose Representation

Four ways to write one rotation. Each is the right tool for a different job and the wrong tool for another.

  • Lesson 3 of 7
  • Module 2: Mathematical Foundations
  • Theory
  • About 8 minutes to read

A SLAM system stores, multiplies, interpolates and optimises rotations. No single representation is best for all four. This lesson explains what each representation costs and gives you the conversions, the conventions that differ between libraries, and a rule for choosing.

Learning objectives

After this lesson, you should be able to:

  • Explain gimbal lock and show it with a concrete Euler-angle example.
  • Rotate a vector and compose rotations with a unit quaternion.
  • Convert between axis-angle, quaternion and matrix and state the sign and double-cover subtleties.
  • Choose a representation for storage, composition, interpolation and optimisation.

The intuition

A rotation has three degrees of freedom, and that is the root of all the trouble. You cannot map three numbers onto the set of rotations without a seam somewhere: some orientations have no clean three-number description, or two descriptions that mean the same thing. The different representations choose where to put the seam.

Pose representations and what each is good forA hub-and-spoke diagram: rotation matrix in the centre, linked to Euler angles (human readable, singular), axis-angle (three numbers, used for increments) and quaternion (four numbers, compact, no singularity).Rotation matrix9 numbers, exact Euler anglesreadable, gimbal lock Axis-angle3 numbers, increments Unit quaternion4 numbers, no singularity All four describe the same three degrees of freedom.
The representations are views of the same rotation. Choose the one whose seam is far from where you work.
Rotation representations compared
RepresentationNumbersStrengthWeakness
Rotation matrix9 (3 dof)Exact, products and transposes are trivial, no seamRedundant; must stay orthonormal
Euler angles (roll, pitch, yaw)3Human readable, matches vehicle intuitionGimbal lock; many conventions; not unique
Axis-angle / rotation vector3Minimal, natural for small increments and optimisationSeam at angle π; composition is awkward
Unit quaternion4 (3 dof)Compact, no singularity, cheap to compose and interpolateDouble cover (q and −q); must be normalised; convention-dependent

The mathematics

Euler angles and gimbal lock

Euler angles describe a rotation as three turns about chosen axes in order, for example yaw about z, then pitch about y, then roll about x (the common “ZYX” aerospace sequence): R = Rz(ψ) Ry(θ) Rx(φ). When the pitch is ±90°, the first and third axes line up, and only the sum or difference of roll and yaw is determined. One degree of freedom is lost: this is gimbal lock. Near it, a small change of orientation needs a huge change of the angles, and numerical results become unstable.

Unit quaternions

A quaternion q = (w, x, y, z) = w + xi + yj + zk with unit length represents a rotation by angle θ about unit axis n as

Quaternion from axis and angle
q=(cos⁡(θ2), nx sin⁡(θ2), ny sin⁡(θ2), nz sin⁡(θ2))q=(\cos (\frac{\theta }{2}),\,n_{x}\,\sin (\frac{\theta }{2}),\,n_{y}\,\sin (\frac{\theta }{2}),\,n_{z}\,\sin (\frac{\theta }{2}))
∣q∣=1\lvert q\rvert =1
w
the scalar part, cos(θ/2). Some libraries (ROS geometry_msgs, Eigen’s coefficient order) store it last as (x, y, z, w).
(x, y, z)
the vector part: the axis scaled by sin(θ/2).
q*
the conjugate (w, −x, −y, −z): for a unit quaternion this is the inverse.
⊗
the Hamilton product of two quaternions: composition of rotations, with the same order as matrix products.

The rotation of a vector v is q ⊗ (0, v) ⊗ q*. Composing two rotations is q1 ⊗ q2. Because q and −q describe the same rotation (a double cover), equality tests must allow for the sign, and interpolation must pick the shorter path by flipping the sign if q1·q2 < 0.

Interpolation and averaging

Spherical linear interpolation (slerp) moves along the great arc at constant angular speed: q(t) = sin((1−t)Ω)/sinΩ · q1 + sin(tΩ)/sinΩ · q2, where cosΩ = q1·q2. Interpolating Euler angles, by contrast, does not give a constant-speed turn and can pass through gimbal lock.

Conventions that bite

  • Component order: (w, x, y, z) in Eigen’s constructor and in most papers; (x, y, z, w) in ROS messages and Eigen’s raw coefficient array.
  • Hamilton versus JPL: the two quaternion algebras differ in the sign of the ij = ±k rule and therefore in multiplication order and in what the quaternion means (frame-to-world or world-to-frame). Never mix them in one system. Solà (2017) documents both.
  • Euler sequence and frame: ZYX body-fixed is not the same as XYZ fixed-axis. State it where the angles are stored.

A worked example: quaternion and gimbal lock

  1. A 90° turn about z. θ = 90°, n = (0, 0, 1). q = (cos 45°, 0, 0, sin 45°) = (0.7071, 0, 0, 0.7071).
  2. Rotate the vector (1, 0, 0). The answer must be (0, 1, 0) because x turns into y. Using the matrix equivalent from the previous lesson, Rz(90°)(1, 0, 0) = (0, 1, 0). The quaternion formula gives the same by q ⊗ (0, 1, 0, 0) ⊗ q*, which evaluates to (0, 0, 1, 0), that is, the vector (0, 1, 0).
  3. Compose two such turns. q ⊗ q = (w² − z², 0, 0, 2wz) = (0.5 − 0.5, 0, 0, 2 · 0.7071 · 0.7071) = (0, 0, 0, 1): a 180° turn about z, since cos 90° = 0 and sin 90° = 1. ✓
  4. The double cover. −q = (−0.7071, 0, 0, −0.7071) rotates every vector exactly as q does. A test that compares quaternions elementwise would call them different; a test that compares |q1·q2| ≈ 1 would not.
  5. Gimbal lock. With ZYX Euler angles, take pitch θ = 90°. Then R = Rz(ψ) Ry(90°) Rx(φ), and one can check that R depends only on ψ − φ. The pairs (ψ, φ) = (30°, 0°) and (60°, 30°) give the same rotation. A system that stores Euler angles cannot tell them apart, and a controller that differentiates them sees a spike.

The quaternion for (30°, 0°) and (60°, 30°) with pitch 90° are identical to numerical precision, which is exactly what “one degree of freedom lost” looks like in a quaternion: no problem is visible, because the quaternion has no seam there.

Engineering implications and limitations

How to choose

Choosing a rotation representation for each job
JobUseWhy
Store and exchange posesQuaternion (stated convention) or matrixCompact, no singularity, standard in ROS and file formats
Compose many rotationsQuaternion or matrixCheap products; renormalise periodically
Transform many pointsMatrixOne matrix-vector product per point
InterpolateQuaternion slerpConstant angular velocity, shortest path
Optimise or define an errorRotation vector (local increment)Three unconstrained numbers around the current estimate
Show to a human or set a limitEuler angles, at the boundary onlyReadable; convert on the way in and out

Engineering rules

  • Normalise a quaternion after each product or integration step. A drift of 1e-3 in length slowly stretches rotated vectors.
  • Convert Euler angles at the edge of the system and never inside the estimator or the optimiser.
  • Write one test per convention: a known rotation about each axis, compared in all representations. The 90° turn about z above is a good first one.
  • Always compare orientations by the angle of the relative rotation, 2 acos(|q1·q2|), not by differences of components.

Limits

Quaternions fix the singularity, but they cost a constraint (unit length) and a sign ambiguity. They are also not natural for covariance: uncertainty about orientation is described as a small three-vector perturbation in the tangent space, not as a four-by-four covariance of the quaternion (Module 4 and 6).

Common misconceptions

“Quaternions are four independent numbers.”

A rotation quaternion is constrained to unit length: it has three degrees of freedom, like any rotation.

“Gimbal lock is a mechanical problem only.”

It is a property of the three-angle parameterisation. A purely software representation using Euler angles loses a degree of freedom at the same orientation.

“Euler angles are fine if I avoid ±90° pitch.”

That works for a ground vehicle with small tilt. A drone, a handheld camera or a robot arm will visit that region.

“q and −q are different orientations.”

They are the same rotation. Treat them as equal, and flip the sign before interpolating or averaging.

Engineering takeaways

  1. Every representation of a 3D rotation has a seam; choose by where you work and what you do.
  2. Euler angles lose a degree of freedom at ±90° pitch (gimbal lock): use them only at the human boundary.
  3. A unit quaternion rotates by q ⊗ v ⊗ q*, composes by multiplication, and has the double cover q ≡ −q.
  4. Conventions differ: component order, Hamilton versus JPL, Euler sequence. State and test them.
  5. Optimise with small rotation-vector increments; store and exchange with quaternions or matrices.

References and further reading

Primary papers and authoritative documentation.

Knowledge check

Three conceptual questions. Write an answer, then reveal the explanation. Your answers stay in your browser.

Question 1

A log file stores orientation as (0.0, 0.0, 0.7071, 0.7071). Reading it with the wrong component order gives a different rotation. How could you detect which order is used?

Explanation

Check the norm and a known pose. In (x, y, z, w) order this is a 90° rotation about z; read as (w, x, y, z) it has w = 0 and a vector part (0, 0.7071, 0.7071), which is a 180° rotation about an axis tilted between y and z. If the robot is known to start with no rotation, the identity quaternion (0, 0, 0, 1) versus (1, 0, 0, 0) shows the order immediately.

Question 2

A flight controller stores roll, pitch and yaw and the aircraft pitches up to 90°. What goes wrong, and what would you store instead?

Explanation

At 90° pitch roll and yaw describe the same axis, so only their combination is determined (gimbal lock). Derived rates spike and conversions become unstable. Store a quaternion or a rotation matrix internally and convert to Euler angles only for display.

Question 3

Two orientations are q and −q. You average them component-wise and get (0, 0, 0, 0). What went wrong?

Explanation

The two quaternions are the same rotation but opposite points on the double cover, so they cancel in a linear average. Flip one sign so that their dot product is positive before averaging (and renormalise), or average through a rotation vector in the tangent space.

What’s next

With frames and rotations settled, the next lessons deal with what a robot does not know: probability and uncertainty. Start with Gaussians and covariance.

Practice this lesson

Diagnose a Pose-Transformation Error

A pose chain gives a wrong result: find the composition-order, inverse or angle-wrapping error among 3D and 2D variants.

Next lesson

M2-T4 — Probability and Uncertainty

Describe uncertainty with Gaussians and covariances, propagate it through a function and read an uncertainty ellipse.

Nothing below is required to finish this lesson. The last 3 entries are lessons of other courses on this site that teach the background or go further.