30% off your first 3 months

Get the deal

Euler angles vs quaternions

by DuckyHelperNov 12, 20265 min read
Robotics

TL;DR:

  • Euler angles describe an orientation with three rotations applied in a fixed order. They are easy to read.

  • At gimbal lock, two of the three angles turn about the same axis, so one degree of freedom is lost and angles stop being unique.

  • A unit quaternion stores a rotation as an axis and angle in four numbers, composes cleanly and has no gimbal lock.

A drone points its nose straight up. Its flight software reports roll, pitch and yaw, and suddenly roll and yaw jump around wildly even though the drone is barely moving. Nothing is broken. The way of writing the orientation has a weak spot, and it is a reason many robots store rotation differently.

What orientation means on a robot

Position takes three numbers. Orientation in 3D also needs three numbers, because there are three independent ways to turn. Robots need orientation for the camera on an arm, a drone's attitude, the heading of a car and a gripper approaching a handle.

The same orientation can be written in several ways: a rotation matrix with nine numbers, Euler angles with three, an axis and an angle, or a quaternion with four. They are different notations for the same thing.

Three angles and their order

Euler angles build an orientation by turning about three axes, one after another. A common choice in robotics is yaw about z, then pitch about y, then roll about x. The order is part of the definition. Roll then pitch then yaw gives a different final orientation than yaw then pitch then roll, and whether each turn is about a fixed axis or the already-turned axis must also be agreed.

Euler angles are popular because people can read them. A pitch of 10 degrees means the nose is up by about that much. The trouble starts when two of the axes line up.

Worked example: why a rotation gets stuck at gimbal lock

Use yaw ψ about z, pitch θ about y, roll φ about x, applied as R = Rz(ψ) · Ry(θ) · Rx(φ), which means roll acts first on the vector. Set the pitch to 90 degrees, the nose pointing straight up. Multiplying out the three matrices, the first row of R becomes:

R11 = cos ψ · cos θ                                  = 0
R12 = cos ψ · sin θ · sin φ − sin ψ · cos φ          = sin(φ − ψ)
R13 = sin ψ · sin φ + cos ψ · sin θ · cos φ          = cos(φ − ψ)

With θ = 90°, cos θ = 0 and sin θ = 1, which gives the right-hand sides. Yaw and roll appear only as the difference φ − ψ. The other rows behave the same way.

Try two different pairs of numbers. Yaw 30° and roll 10° give φ − ψ = −20°. Yaw 50° and roll 30° also give −20°. With pitch at 90°, both settings are the same physical orientation. That is gimbal lock: one degree of freedom is gone, because yaw and roll now turn about the same axis.

The practical result is that near pitch = 90° the angles are not unique, and a tiny change in the real orientation can swing yaw and roll by large, opposite amounts. A controller that works with these angles can command huge motions for a small change. That is the drone symptom from the start.

Quaternions in plain words

A unit quaternion stores a rotation by an angle α about a unit axis (a, b, c) as four numbers (w, x, y, z) = (cos(α/2), a·sin(α/2), b·sin(α/2), c·sin(α/2)). The numbers always satisfy w² + x² + y² + z² = 1.

Take a 90° turn about z. Then α/2 = 45° and q = (0.7071, 0, 0, 0.7071). Check the length: 0.7071² + 0.7071² = 0.5 + 0.5 = 1. Doing it twice should give a 180° turn about z. For rotations about the same axis, multiplying gives w = w² − z² = 0.5 − 0.5 = 0 and z = 2wz = 2 × 0.5 = 1, so q² = (0, 0, 0, 1). That is the quaternion for 180° about z, since cos 90° = 0 and sin 90° = 1. It worked without any trigonometry on the whole rotation.

Quaternions compose by multiplication, and they can be blended smoothly between two orientations (called slerp), which is why animation and camera paths use them. They never lose a degree of freedom. One quirk: q and −q are the same rotation, so a program must not treat them as different.

Where it goes wrong

  • Unknown convention. Different libraries use different Euler orders and different quaternion orders, (w, x, y, z) or (x, y, z, w). Check which one you have.

  • Not normalized. After many multiplications, rounding errors make a quaternion's length drift from 1. Renormalize it.

  • Averaging angles. Averaging 359° and 1° as ordinary numbers gives 180°, not 0°.

  • Reading quaternion numbers by eye. They are hard for people to interpret, so convert to Euler angles only for display.

  • Degrees versus radians. Most code expects radians.

Safety: Arms have a related problem called a singularity, where small tool motions need huge joint speeds. When you test near such a pose, use slow speeds, limit joint velocity in software, and keep the emergency stop within reach.

How real robots use it

Most robot software stores and sends orientation as a quaternion, and the pose of one frame in another, as described in coordinate frames and transforms, is a position plus a quaternion. People read and type Euler angles, usually only at the edges: a config file, a display, a quick test. Inside, the math stays in quaternions or rotation matrices.

Try it yourself

Hold a book flat in front of you. Yaw it 30° left, pitch the nose up 90°, then roll it 10°. Now start again from flat with a 50° yaw, the same 90° pitch and a 30° roll. If you do the turns in the same order, the book should end up in the same place. A short script that converts both sets of angles into rotation matrices will confirm it to many decimal places.

Frequently asked questions

Why not use rotation matrices everywhere?

They work well but use nine numbers with strict rules to stay valid. A quaternion uses four, so it is smaller and easier to keep correct.

Does a quaternion have a gimbal lock?

No. Gimbal lock comes from stacking three separate axis turns. A quaternion describes the whole rotation at once.

Get started

DuckyHelper can
do more

It sees your screen and works in any app you use. Try it for 10 minutes, no card.

Get it for Mac, Windows or Linux

Share

Questions and jobs

Do anything

Ask it a question or hand it a job, in any app.

Doing jobs for you is in early access on Mac. Asking works on Mac, Windows and Linux.