Atlas Robotics
Math for RoboticsAdvanced

What is a quaternion? Representing orientation without gimbal lock

Atlasbot · 17/8/2026 · 10 min read

What is a quaternion? Representing orientation without gimbal lock
Quaternion represents a rotation using the axis of rotation and the angle of rotation

Open any message nav_msgs/msg/Odometry or geometry_msgs/msg/PoseStamped in ROS2, the orientation does not store the familiar angle θ, but four numbers (x, y, z, w) — a quaternion. This is not a random choice: quaternion is the standard way of representing direction in the entire ROS2/ROS ecosystem, precisely because it solves the gimbal lock problem mentioned in the Rotation and Euler Angles article.

Key concepts

Instead of thinking "rotate 30° around x, then 45° around y, then 10° around z" (three sequential steps that easily cause gimbal lock), the quaternion represents any 3D rotation by just a single rotation — rotating an angle θ around a single axis:

q = (x, y, z, w)
  = (rotation axis × sin(θ/2), cos(θ/2))

(x, y, z) encodes the direction of the rotation axis, w encodes the rotation angle around that axis. For θ = 0 (no rotation), the quaternion is (0, 0, 0, 1) — the "identity" value.

In short: Since a rotation only requires exactly one axis + one angle, the quaternion has no singularity in the entire rotation space — this is the exact mathematical reason why it is "gimbal-lock free".

How it works

A quaternion has 4 components but 3D rotation has only 3 degrees of freedom — this difference is resolved by the constraint: a valid quaternion always has a length (norm) of 1:

x² + y² + z² + w² = 1

This "unit quaternion" constraint reduces the 4 numbers to exactly 3 independent degrees of freedom. A practical consequence: if quaternions are automatically accumulated over multiple steps (common when integrating angular velocities from the IMU over time), rounding errors can cause x²+y²+z²+w² to gradually deviate from 1 — requiring periodic normalization.

Smooth interpolation between two rotation directions using the SLERP (Spherical Linear Interpolation) technique on the quaternion results in rotation at a uniform angular speed — direct interpolation over the Euler angle easily produces non-uniform motion.

Technical details

Formulas

Combining 2 consecutive rotations (eg: the robot rotated by q1, now rotates by q2) is quaternion multiplication (Hamilton product), not commutative like a matrix — the order of multiplication determines the order in which the rotation is applied:

q_tong_hop = q2 ⊗ q1 (apply q1 first, q2 later)

Simple case — 2 rotations around the SAME axis: angle adds up directly
For example: rotate 30° around z then rotate 45° around z = rotate 75° around z

With 2 different rotation axes, the full formula is more complicated (multiply according to Hamilton's rule, not just adding angles) — most of the code actually uses the library's built-in functions (tf_transformations, scipy.spatial.transform) instead of implementing the multiplication formula yourself.

Parameters

Some common reference quaternion values when debugging — immediately recognizing these "standard" values helps quickly confirm whether the data is reasonable:

RotationQuaternion (x, y, z, w)
Identity(0, 0, 0, 1)
Rotate 90° around the z axis(0, 0, 0.707, 0.707)
Rotate 180° around the z axis(0, 0, 1, 0)

Practical applications

SLERP on quaternion is the standard technique for smooth motion planning for a pan-tilt mechanism (horizontal-vertical rotating camera) or end-effector direction of a manipulator — direct interpolation between the start direction and the end direction at a uniform angular speed, avoiding the phenomenon of "jerks" of uneven speed or going around further than necessary that linear interpolation on Euler angles is prone to (discussed in the Rotation article). Any smooth rotation in robotics — not just the display animation but also the actual movement trajectory of the manipulator — should use SLERP instead of linearly interpolating on the angle.

Practical example

from tf_transformations import euler_from_quaternion, quaternion_from_euler
import numpy as np

# Convert yaw = 90 degrees to quaternion
q = quaternion_from_euler(0, 0, np.radians(90))
print(f"Quaternion cho yaw=90: {[round(v, 3) for v in q]}")

# Switch back to check
roll, pitch, yaw = euler_from_quaternion(q)
print(f"Yaw doc lai: {np.degrees(yaw):.1f} do")
Output:
Quaternion cho yaw=90: [0.0, 0.0, 0.707, 0.707]
Yaw doc lai: 90.0 do

The result [0, 0, 0.707, 0.707] matches the reference table in the Parameters section — confirming that the formula and library are working properly, and that the Euler↔quaternion conversion does not result in loss of information (90° readback).

Common mistakes

  • Forgot to normalize the quaternion after accumulating many steps — as mentioned in the Principle of Operation section, the accumulated error causes x²+y²+z²+w² to deviate from 1, the quaternion is no longer valid, causing the rotation result to gradually become incorrect over time.
  • Mistaken order (x,y,z,w) with (w,x,y,z) between libraries — ROS2/ROS uses the order (x,y,z,w), but some other math libraries (eg Eigen in C++) use the order (w,x,y,z) — copying the wrong order between the two ecosystems results in quaternions with completely different meanings even though they have the same 4 number.
  • Don't know if q and -q represent the same rotation — quaternions have "double cover" properties (2 values with different signs but same geometric meaning) — direct comparison with == sign between 2 quaternions can say "different" even though they represent exactly 1 direction of rotation, need to compare via scalar product or norm sign before comparing.
  • Directly averaging the quaternion components for "interpolation" — adding then averaging each (x,y,z,w) component does not yield a valid quaternion (no longer normal = 1) and not a geometrically correct intermediate rotation — always use SLERP instead of naive averaging.

Conclusion

Quaternion completely solves the gimbal lock problem of Euler angles by representing all 3D rotations with just 1 axis + 1 angle, at the cost of making it more difficult to visualize visually — this is why ROS2 chose quaternion as the standard for storing orientation in all messages, although debug/display code is often temporarily converted to Euler for readability. This is also the last lesson in a group of 3 lessons about rotation (Matrix → Rotation → Quaternion). The next article in this category is about Transform — how to combine rotation and translation into a complete coordinate transformation.

Get new articles and lessons by email

When a new technical article or course module is published, Atlas Robotics sends you a short email. No spam.

Unsubscribe any time with the link in each email. See our Privacy policy.

What is a quaternion? Representing orientation without gimbal lock — Atlas Robotics