Rotations and Euler angles: orienting a robot in 3D space
Atlasbot · 17/8/2026 · 10 min read
2D mobile robots (all robots in this page's project showcase) only need a single number to know which direction they are "looking" at: heading angle θ. But even when there is an IMU measuring all three axes, or a robot with a manipulator/camera that can rotate many degrees of freedom, one angle alone is not enough — it needs to represent direction in full 3D space. The most intuitive way is Euler angles.
Key concepts
Roll, Pitch, Yaw — three rotation angles around three axes:
Roll (φ) — rotate around the x axis (front-back) — the robot "tilts" left/right
Pitch (θ) — rotation around the y axis (left-right) — the robot "bows/raises" its head
Yaw (ψ) — rotation around the z axis (up-down) — robot "spins" left/right — is θ in 2D robot
With mobile robots running on flat surfaces, Roll and Pitch are almost always approximately 0 (except when going over slopes/rough surfaces) — only Yaw changes continuously, which is why 2D robots only need to track a single angle. The IMU is a sensor that directly measures all three of these angles.
How it works
Euler angles are easy to visualize but have a serious drawback: as one of the three angles (usually Pitch) approaches 90°, the remaining two axes of rotation become effectively overlapping — losing one degree of rotational freedom. This phenomenon is called gimbal lock.
Pitch = 90° → Roll and Yaw rotation axes coincide
→ There is no longer any way to rotate separately along those two axes
Mathematically, some angle combinations cause unstable calculations when switching back and forth between Euler angles and other forms of representation — flying robots (drones) with strong tilt/bend movements are the most likely to encounter gimbal lock in practice.
In short: Gimbal lock is not a sensor error or a code error — it is an inherent mathematical limitation of the Euler angles representation. The only way to completely avoid it is to change to another representation that does not have this singularity — which is Quaternion (next article).
Technical details
Formulas
Each Euler angle corresponds to a rotation matrix around exactly one axis (extending the 2D rotation matrix formula learned in the Matrix lesson); Multiplying these three matrices in the correct order gives the resultant rotation matrix:
R = R_z(yaw) × R_y(pitch) × R_x(roll)
Multiplication order important — because matrix multiplication is not commutative, rotation in the order Yaw→Pitch→Roll gives a different result than Roll→Pitch→Yaw even though it has the same three angles.
Parameters
Euler angles ordering conventions vary across fields/libraries — the most common source of the "right angle number but wrong direction" error when concatenating data from multiple sources:
| Convention | Order of application | Or use it inside |
|---|---|---|
| ZYX (Yaw-Pitch-Roll) | Yaw first, Roll last | Aviation, ROS/robotics most popular |
| XYZ | Roll first, Yaw last | Some 3D graphics libraries/game engines |
| Intrinsic vs Extrinsic | Rotate around the rotated axis (intrinsic) or the original fixed axis (extrinsic) | Subtle differences, always causing confusion when not reading the document carefully |
Practical applications
When integrating an IMU from vendor A (which outputs Roll/Pitch/Yaw data according to the still 3 valid numbers, just the geometric meaning is wrong — the robot may appear to be tilting when it is actually rotating, causing mishandling without any obvious error to debug. This is the reason why when integrating direction sensors from third parties, you must always clearly confirm the axis order convention in the documentation before incorporating it into the control pipeline, do not assume "must be the same as ROS".
Practical example
import numpy as np
def rotation_matrix_zyx(yaw, pitch, roll):
Rz = np.array([[np.cos(yaw), -np.sin(yaw), 0],
[np.sin(yaw), np.cos(yaw), 0],
[0, 0, 1]])
Ry = np.array([[np.cos(pitch), 0, np.sin(pitch)],
[0, 1, 0],
[-np.sin(pitch), 0, np.cos(pitch)]])
Rx = np.array([[1, 0, 0],
[0, np.cos(roll), -np.sin(roll)],
[0, np.sin(roll), np.cos(roll)]])
return Rz @ Ry @ Rx
# Illustration of gimbal lock: pitch = 90 degrees
R1 = rotation_matrix_zyx(np.radians(30), np.radians(90), np.radians(0))
R2 = rotation_matrix_zyx(np.radians(0), np.radians(90), np.radians(30))
print("R1 == R2 (gimbal lock):", np.allclose(R1, R2))
Output:
R1 == R2 (gimbal lock): True
Two completely different angle combinations (yaw=30°,roll=0° and yaw=0°,roll=30°) give the same rotation matrix when pitch=90° — a visual demonstration of gimbal lock: at this singularity, the separate Roll and Yaw information have been "mixed" together, making it impossible to restore each original angle exactly.
Common mistakes
- Mistaken axis order convention between libraries — as mentioned in the Practical Application section, the data is still "numerically correct" but has the wrong geometric meaning if it does not match the convention, difficult to detect through normal inspection.
- Unstable control near gimbal lock point — control systems based directly on Euler angles may react erratically/jerkyly as Pitch approaches 90°, because the derivatives of the angles become unstable near the singularity.
- Linear interpolation between two sets of Euler angles — linearly adding/subtracting/averaged Euler angles does not yield the shortest/smoothest rotation path in real 3D space — this is the reason Quaternion (next article) has its own interpolation (SLERP) designed specifically for this problem.
- Mixing degrees and radians — error repeated throughout this section, always check angle units before passing to trigonometric functions.
Conclusion
Euler angles (Roll-Pitch-Yaw) are intuitive and easy to visualize but carry two inherent risks: gimbal lock at singular points, and ambiguity about axis ordering conventions between different libraries/domains. With a pure 2D robot, just tracking Yaw is enough; With full 3D robots (drones, manipulators), this limitation needs to be clearly understood before choosing the appropriate representation. The next article in the column is about Quaternion — a way to demonstrate rotation without gimbal lock, at the expense of being more difficult to visualize.
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.