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.
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.
| Representation | Numbers | Strength | Weakness |
|---|---|---|---|
| Rotation matrix | 9 (3 dof) | Exact, products and transposes are trivial, no seam | Redundant; must stay orthonormal |
| Euler angles (roll, pitch, yaw) | 3 | Human readable, matches vehicle intuition | Gimbal lock; many conventions; not unique |
| Axis-angle / rotation vector | 3 | Minimal, natural for small increments and optimisation | Seam at angle π; composition is awkward |
| Unit quaternion | 4 (3 dof) | Compact, no singularity, cheap to compose and interpolate | Double 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
- 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
- A 90° turn about z. θ = 90°, n = (0, 0, 1). q = (cos 45°, 0, 0, sin 45°) = (0.7071, 0, 0, 0.7071).
- 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).
- 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. ✓
- 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.
- 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
| Job | Use | Why |
|---|---|---|
| Store and exchange poses | Quaternion (stated convention) or matrix | Compact, no singularity, standard in ROS and file formats |
| Compose many rotations | Quaternion or matrix | Cheap products; renormalise periodically |
| Transform many points | Matrix | One matrix-vector product per point |
| Interpolate | Quaternion slerp | Constant angular velocity, shortest path |
| Optimise or define an error | Rotation vector (local increment) | Three unconstrained numbers around the current estimate |
| Show to a human or set a limit | Euler angles, at the boundary only | Readable; 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
- Every representation of a 3D rotation has a seam; choose by where you work and what you do.
- Euler angles lose a degree of freedom at ±90° pitch (gimbal lock): use them only at the human boundary.
- A unit quaternion rotates by q ⊗ v ⊗ q*, composes by multiplication, and has the double cover q ≡ −q.
- Conventions differ: component order, Hamilton versus JPL, Euler sequence. State and test them.
- Optimise with small rotation-vector increments; store and exchange with quaternions or matrices.
References and further reading
Primary papers and authoritative documentation.
- Diebel, J. (2006). Representing Attitude: Euler Angles, Unit Quaternions, and Rotation Vectors. Stanford University technical report.
- Solà, J. (2017). Quaternion kinematics for the error-state Kalman filter. arXiv:1711.02508: Hamilton and JPL conventions side by side.
- Shuster, M. D. (1993). A survey of attitude representations. The Journal of the Astronautical Sciences 41(4).
- Kuipers, J. B. (1999). Quaternions and Rotation Sequences. Princeton University Press.
- ROS REP 103 (Standard Units of Measure and Coordinate Conventions) and Eigen documentation, Geometry module: quaternion storage order.
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.
Related content
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.
- BeforeM2-T2: 3D Rotations and Rigid-Body Transformations
Extend to SO(3) and SE(3): composition order, non-commutativity and the exponential map.
- LabM2-L2: 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.
- Linear AlgebraEuler Angles and Quaternions
The three parameterisations side by side, with their singularities.
- Linear AlgebraSO(3) and Its Lie Algebra
Rotation vectors and skew-symmetric matrices: the minimal, singularity-free way to express a small rotation.
- Linear AlgebraThe Exponential Map
How a rotation vector becomes a rotation matrix, used whenever an optimiser updates an orientation.
Get notified when the next lesson and new labs are released.
One email per new lesson or lab.