Skip to the lesson
FixTheRobot Explore Robotics / Linear Algebra / M7 Rotations and Rigid-Body Transformations / M7-T8

Linear Algebra · Theory · M7

M7-T8 — Euler Angles and Quaternions

Matrices are exact but bulky. Euler angles are readable but singular. Quaternions are compact, smooth and the default inside estimators.

  • Lesson 8 of 9
  • Module 7: Rotations and Rigid-Body Transformations
  • Rotations and rigid bodies
  • About 9 minutes to read

A rotation matrix has nine numbers for three degrees of freedom. Engineers use more compact forms for input, display and filtering. Each representation has its own trade-off, and gimbal lock, the loss of a degree of freedom in Euler angles, is the classic failure that every robotics engineer must recognise.

Learning objectives

After this lesson, you should be able to:

  • Describe Euler angles, axis-angle and unit quaternions and convert between them and rotation matrices.
  • Explain gimbal lock with a numeric example and say when it threatens a system.
  • Rotate a vector with a quaternion and compose two rotations by quaternion multiplication.
  • Choose the right representation for input, storage, interpolation and filtering.

Introduction and motivation

A flight controller reports roll, pitch and yaw because people can read them. Internally the attitude filter uses quaternions because they never become singular and they integrate gyro data cheaply. If a drone pitches to 90° during an aggressive manoeuvre and the code runs on Euler angles, roll and yaw become indistinguishable and the estimate can spin wildly. Knowing the failure mode and the standard remedy is the difference between a stable flight and a crash.

The intuition

Euler's theorem says every rotation is one turn about one axis. Axis-angle stores exactly that. A quaternion packages the same information in four numbers so that composing turns is a simple product. Euler angles instead split the rotation into three sequential turns about coordinate axes, which is easy to read but can lose a degree of freedom: when the middle turn points the first axis along the last, the first and last turns do the same thing.

Four ways to describe an orientationA comparison table of rotation matrix, Euler angles, axis angle and unit quaternion: number of parameters, whether singularities exist, and when each is used.Representation: how many numbers, any trouble? Rotation matrix9 numbers6 constraintsno singularityrotating vectors,composing Euler angles3 numbersroll, pitch, yawgimbal lock atpitch = ±90°display, input Axis-angle4 numbers (3)axis n and angle θaxis undefinedat θ = 0intuition Unit quaternion4 numbers, |q| = 11 constraintno singularity;q and −q equalfilters, interpolation
Representations of orientation: size, singularities and typical use.

Interactive figure. A wireframe box with roll, pitch and yaw sliders. At pitch of ninety degrees the readout warns of gimbal lock, because roll and yaw then rotate about nearly the same axis. This figure needs JavaScript to be interactive.

Set pitch to 90°. Now move roll and yaw: the box turns about essentially the same axis whichever slider you use, so one degree of freedom is lost: gimbal lock. The readout flags it. Compare with pitch = 20°, where the two sliders do different things.

Geometric and physical interpretation

Geometric view. With the ZYX convention R = Rz(ψ)Ry(θ)Rx(φ) and θ = 90°, the matrix depends only on ψ − φ: two angles produce one rotation, so one degree of freedom has vanished. A unit quaternion q = (cos(θ/2), n sin(θ/2)) lies on the unit sphere in four dimensions; rotating by θ about n is a point there, and q and −q give the same rotation (the sphere double-covers SO(3)). Physical view. A three-gimbal gyroscope physically jams when two gimbal axes align; the mathematics of Euler angles has the same flaw.

The mathematics

(φ, θ, ψ)
roll, pitch, yaw; R = Rz(ψ)Ry(θ)Rx(φ) (ZYX convention).
(n, θ)
axis-angle: unit axis n and angle θ.
q = (w, v)
unit quaternion with scalar part w and vector part v = (x, y, z); w² + x² + y² + z² = 1.
q ⊗ p
the quaternion (Hamilton) product.

Axis-angle to quaternion

q = ( cos(θ/2),  n sin(θ/2) ),     ‖q‖ = 1

What it means. The half-angle appears because a quaternion acts on a vector from both sides (q p q*), which doubles the effective angle. The identity rotation is (1, 0, 0, 0).

Rotating a vector and composing

v′ = q ⊗ (0, v) ⊗ q*          q₃ = q₁ ⊗ q₂ corresponds to R₃ = R₁R₂

What it means. Embed the vector as a pure quaternion, multiply by q on the left and by the conjugate q* = (w, −v) on the right, and read off the vector part. The product of quaternions matches the product of matrices, in the same order.

Gimbal lock

θ = ±90°:   Rz(ψ) Ry(±90°) Rx(φ) depends only on ψ ∓ φ

What it means. At the singular pitch, roll and yaw rotate about the same axis. The map from angles to rotations stops being locally one-to-one, so attitude rates in Euler angles (φ̇, θ̇, ψ̇) blow up.

Euler angles: readable, three numbers, gimbal lock at pitch ±90°. Quaternions: four numbers with norm 1, no singularity, product matches the rotation product. q and −q are the same rotation.

Step-by-step derivation: gimbal lock and the quaternion action

Gimbal lock. Set θ = 90° in Rz(ψ)Ry(θ)Rx(φ). Then Ry(90°) = [[0, 0, 1], [0, 1, 0], [−1, 0, 0]], and multiplying out gives a matrix whose entries depend only on ψ − φ (for example the first row is (0, −sin(ψ − φ), cos(ψ − φ))). Concretely Rz(30°)Ry(90°)Rx(10°) = Rz(40°)Ry(90°)Rx(20°) because both have ψ − φ = 20°. Two different angle triples give the same orientation, and no triple can reach the three-parameter family of other orientations from that point.

  1. Axis-angle to quaternion. Define q = (cos(θ/2), n sin(θ/2)); then w² + |v|² = cos² + sin² = 1.
  2. Rotating a vector. Decompose p into the component along n (unchanged by the turn) and the component perpendicular to n. The quaternion sandwich q (0, p) q* multiplies the perpendicular part by cos θ and adds sin θ (n × p), which is Rodrigues' formula: p′ = p cos θ + (n × p) sin θ + n (n·p)(1 − cos θ). The half angle reappears doubled: cos²(θ/2) − sin²(θ/2) = cos θ and 2 sin(θ/2) cos(θ/2) = sin θ.
  3. Composition. Rotating by q₂ then by q₁ is q₁(q₂ p q₂*)q₁* = (q₁q₂) p (q₁q₂)*, using (q₁q₂)* = q₂*q₁*. So the product q₁ ⊗ q₂ represents the composed rotation, and the cost is 16 multiplications versus 27 for a matrix product.
  4. Double cover. (−q) p (−q)* = q p q*, so q and −q are the same rotation: the unit sphere S³ maps two-to-one onto SO(3). In code, choose the sign with w ≥ 0, or the one closest to the previous quaternion, so the signal does not jump.
  5. To a matrix. For q = (w, x, y, z): R = [[1−2(y²+z²), 2(xy − wz), 2(xz + wy)], [2(xy + wz), 1−2(x²+z²), 2(yz − wx)], [2(xz − wy), 2(yz + wx), 1−2(x²+y²)]]. The angle is θ = 2 arccos w.
  6. Normalisation. Rounding makes ‖q‖ ≠ 1; dividing by the norm restores a valid rotation. This is much cheaper than re-projecting a matrix onto SO(3), one reason filters integrate quaternions.

Interpolation. Spherical linear interpolation (slerp) between two unit quaternions gives constant-speed rotation along the shortest arc; interpolating matrix entries or Euler angles does not.

A worked example: gimbal lock and a quaternion rotation

  • Gimbal lock. Rz(30°)Ry(90°)Rx(10°) and Rz(40°)Ry(90°)Rx(20°) are the same matrix, [[0, −0.342, 0.940], [0, 0.940, 0.342], [−1, 0, 0]]: differences ψ − φ = 20° in both.
  • Quaternion for 90° about z. q = (cos 45°, 0, 0, sin 45°) = (0.7071, 0, 0, 0.7071).
  • Rotating a vector. q ⊗ (0, 1, 0, 0) ⊗ q* gives the pure quaternion (0, 0, 1, 0): the vector (1, 0, 0) goes to (0, 1, 0), as Rz(90°) does.
  • Composing. With qz = (0.7071, 0, 0, 0.7071) and qx = (0.7071, 0.7071, 0, 0) (90° about x), qz ⊗ qx = (0.5, 0.5, 0.5, 0.5). Rotating (1, 0, 0) with it gives (0, 1, 0), matching Rz(90°)Rx(90°)e₁ from the earlier lesson.
  • Axis-angle of the product. θ = 2 arccos(0.5) = 120°, axis (1, 1, 1)/√3: the three-fold rotation about the cube diagonal.
  • Double cover. −q = (−0.5, −0.5, −0.5, −0.5) gives the identical rotation of (1, 0, 0).

Robotics application

UAV attitude estimation. Filters store a unit quaternion and update it with the gyro rate; Euler angles are computed only for display. The error-state filter perturbs the quaternion by a small 3-vector; see quaternion-based orientation, attitude representation and the error-state Kalman filter.

SLAM. Pose-graph back ends parameterise rotations with quaternions or the Lie algebra and avoid Euler angles for optimisation; see rotation matrices, quaternions and poses and rotations and transforms in 3D.

Mobile robots. Only the yaw matters on flat ground, so a single heading is enough; 3D SLAM on slopes needs the full representation.

Gimbals, cameras, manipulators. Joint-angle interfaces and teleoperation use Euler angles for operators; the controller converts to quaternions or matrices internally and interpolates paths with slerp.

Engineering implications and limitations

Practical rules

  • Store orientation as a unit quaternion or rotation matrix, not Euler angles. Convert to Euler only at the user interface.
  • Document the quaternion convention. Hamilton (w first) versus JPL (w last), active versus passive, and the order of multiplication differ between libraries (Eigen, ROS tf2, Ceres, JPL). Mixing conventions silently inverts rotations.
  • Normalise after each update. Divide by the norm; and fix the sign (w ≥ 0 or continuity) before interpolating or comparing.
  • Use slerp, not linear blending. And for small steps, normalised linear interpolation (nlerp) is a close, cheaper approximation.
  • Watch pitch near ±90° with Euler angles. Limit or switch to quaternions, as aerobatic UAVs must; for ground robots with small pitch it is rarely a problem.
  • Wrap Euler angle differences. A yaw error of 359° is −1°; and never subtract Euler angles to get an error, use the relative rotation.

Limits

Quaternions have no singularity but are not human readable, and filters that treat the four components as independent states violate the unit-norm constraint. Use error-state or Lie-algebra formulations (Module 10) for estimation.

Common misconceptions

“Gimbal lock is a physical defect of the robot.”

It is a property of the Euler-angle parameterisation. The orientation itself is perfectly fine; the three angles just stop being independent.

“A quaternion is a rotation by the angle stored in it.”

The scalar part is cos(θ/2): the half angle. A full 360° turn gives q = (−1, 0, 0, 0), which is the same rotation as the identity (1, 0, 0, 0).

“q and −q are different orientations.”

They represent the same rotation (double cover), so signs must be fixed before comparing or interpolating.

“Quaternion multiplication commutes.”

It does not, matching 3D rotations: qz ⊗ qx ≠ qx ⊗ qz.

Engineering takeaways

  1. Euler angles (3 numbers) are readable but suffer gimbal lock at pitch ±90°, where two angles produce the same rotation.
  2. A unit quaternion q = (cos θ/2, n sin θ/2) has four numbers with norm 1, no singularity and a product matching rotation composition.
  3. v′ = q ⊗ (0, v) ⊗ q*; q and −q are the same rotation.
  4. Store matrices or quaternions internally, convert to Euler angles only for display, and normalise and fix the sign after updates.
  5. Always document the convention (order, Hamilton versus JPL, active versus passive).

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

Give the quaternion for a 180° rotation about the x-axis and use it to rotate the vector (0, 1, 0).

Explanation

q = (cos 90°, sin 90°·(1, 0, 0)) = (0, 1, 0, 0). A 180° turn about x flips y and z: (0, 1, 0) goes to (0, −1, 0). The sandwich q (0, 0, 1, 0) q* gives (0, 0, −1, 0), i.e. the vector (0, −1, 0).

Question 2

A drone reports roll 10°, pitch 90°, yaw 50°. Another reports roll 30°, pitch 90°, yaw 70°. Are these different attitudes?

Explanation

No. At pitch 90° the rotation depends only on ψ − φ: 50 − 10 = 40° and 70 − 30 = 40°, so both are the same orientation. This is gimbal lock: the angles are not independent there.

Question 3

Why can a quaternion with components (−0.5, −0.5, −0.5, −0.5) be replaced by (0.5, 0.5, 0.5, 0.5) without changing behaviour, and when might the sign still matter?

Explanation

q and −q represent the same rotation (the sphere double-covers SO(3)), so the rotation of any vector is identical. The sign matters for interpolation and averaging: interpolating from q to −q goes the long way round, so choose the sign with positive dot product with the previous quaternion.

What’s next

With frames, rotations, transforms and their compact forms in hand, the last lesson of this module puts them to work: moving real sensor measurements, a LiDAR scan and a drone camera point, step by step from the sensor frame to the world.

Next lesson

M7-T9 — Transforming Points Between Frames

Move points and measurements from a sensor frame to the world frame and back, step by step.

Nothing below is required to finish this lesson.