Chapter 02 · Turn the maths into robot motion
Today in the field story
One problem, then the next
The crane logger stores roll, pitch, and yaw, while the ROS-shaped message sketch expects quaternion fields. Recreate one Camera-Crane Recovery orientation in both languages, demonstrate the Euler coordinate singularity with nested paper rings, and map named quaternion components rather than array positions. The physical camera never locks; only one descriptive coordinate system loses a unique direction.
- Why now
The mission must exchange orientation without confusing a representation singularity, axis order, or component order.
- Ignore today
Ignore quaternion interpolation and full attitude-control dynamics.
- Unlocks next
A normalized orientation block that can enter a complete rigid pose.
Understand
Build the physical picture first
Orientation is one physical fact with several possible coordinate languages. Euler angles are three ordered instructions; a quaternion is one normalized four-number description. A language can become awkward even when the object itself is perfectly movable.
Euler angles describe an orientation using three ordered rotations, often called roll, pitch, and yaw. The order and whether rotations use fixed or moving axes must be stated because different conventions produce different orientations from the same three numbers. Euler angles are attractive for human displays, but they are not three independent physical hinges.
For a common yaw-pitch-roll description, pitch near ±90° makes two descriptive axes align. One independent descriptive direction is lost; this is gimbal lock. The phone, drone, or gripper is not physically locked. The coordinate chart is singular, much like longitude becoming ambiguous at a geographic pole.
A unit quaternion represents orientation with four related values. In mathematical scalar-first notation q=(w,x,y,z), a turn θ about unit axis u uses w=cos(θ/2) and (x,y,z)=u sin(θ/2). The norm must be one. q and -q represent the same physical orientation, so component-by-component equality is not a safe orientation comparison.
Component order is a practical trap. ROS geometry messages store (x,y,z,w), while some mathematics texts and libraries display (w,x,y,z). Angles passed to common APIs are usually radians. Production code uses tested conversion utilities, normalizes when appropriate, records the convention, and checks the rotated direction rather than trusting four plausible numbers.
Words you need
Name each idea precisely
- Euler angles
Three ordered rotations used to describe an orientation.
Physical example:A display may show a phone's roll, pitch, and yaw.
- Gimbal lock
A singularity where two Euler rotation axes align and the description loses one independent direction.
Physical example:Nested rings at 90° pitch can place the first and third axes on the same line.
- Quaternion
A four-component orientation representation whose rotation form has unit norm.
Physical example:An IMU message may publish x, y, z, w rather than roll, pitch, yaw.
- Normalization
Scaling components so their combined norm is one.
Physical example:A quaternion sensor value drifting from unit length must be handled before use.
- Convention
An explicit agreement about axis order, signs, active/passive meaning, and component order.
Physical example:ROS uses quaternion message fields x, y, z, w; another library may print w first.
Math, one line at a time
Work through today’s relationship
Prerequisite rescue · optionalStart at zero: turns, triangles, and pose chains
A joint turn becomes a tool position only after we define the angle, split a link into horizontal and vertical parts, and follow frame order.
- θ
- theta: the amount a joint has turnedUnit: degrees (°) or radians (rad)
- cos θ, sin θ
- horizontal and vertical fractions of a turned unit linkUnit: no unit
- Tᴬ_B
- position and direction of frame B described by frame AUnit: unitless rotation + metres
A full turn is 360° = 2π rad, so 90° × π/180 = π/2 rad. π is about 3.1416.
For a 1 m link at 90°, x = 1 cos 90° = 0 m and y = 1 sin 90° = 1 m.
For several frames, follow the physical path in order. Multiply transforms only when the touching frame names match, then verify by reversing the path.
Like nested UI transforms, each child inherits its parent's transform; unlike UI, the order can move metal into an obstacle.
What are x and y for a 2 m link at 0 rad?
x = 2 cos 0 = 2 m; y = 2 sin 0 = 0 m.
A rotation of angle about unit axis has quaternion
Every symbol names an angle or axis part; unit length prevents scaling.
Encode a 90° yaw and place it in a ROS message
Represent a +90° rotation about z. Use scalar-first mathematics, then convert to ROS field order.
Convert θ=90° to θ=π/2 rad.
Take half the angle: θ/2=π/4.
Compute cos(π/4)=sin(π/4)≈0.7071.
Use axis u=(0,0,1): q_math=(w,x,y,z)=(0.7071,0,0,0.7071).
Check the norm: √(0.7071²+0.7071²)≈1.
Write ROS fields as (x,y,z,w)=(0,0,0.7071,0.7071) and verify that +x rotates toward +y.
The same orientation is q_math=(0.7071,0,0,0.7071) in wxyz notation and (0,0,0.7071,0.7071) in ROS xyzw fields.
A correct quaternion still fails integration if its component order, axis convention, or angle unit is misunderstood.
Physical examples
Where this appears in real life
Phone held straight up
Tilt a phone toward 90° pitch while observing a roll-pitch-yaw display.
The physical phone remains well-defined, but yaw and roll values may change sharply or become coupled because the Euler description is near its singularity.
Robot wrist crossing a vertical pose
A gripper moves smoothly through an orientation that makes a chosen Euler pitch 90°.
The planned physical rotation can remain smooth when represented and interpolated appropriately, while raw Euler components may jump.
Hands-on exercise
Make the idea observable
Use three paper rings or strips joined as nested gimbals, a marked cardboard phone, and a calculator.
Label the three ordered Euler axes and write the exact order you will use.
Set the middle ring to 0° and identify three visibly distinct axes.
Move the middle ring to 90° and identify the two axes that now align.
Rotate the cardboard phone slightly around the aligned direction and note how two Euler instructions can describe coupled motion.
Calculate the 90° z quaternion from half-angle sine and cosine.
Write it once in wxyz order and once in ROS xyzw order, then mark which value belongs to each field.
The nested-ring coordinate mechanism loses a descriptive direction at gimbal lock; the physical phone itself still has an orientation.
You can demonstrate the aligned axes, explain the coordinate singularity without calling it a motor lock, and produce a unit quaternion in both component orders.
Build today
Extend the browser notebook into a two-link arm visualizer with frame composition, forward kinematics, and a bounded numerical IK trace.
Evidence to save
DONE when the integrated “Euler angles, quaternions, and representation singularities” path is observable, cancelable, and leaves the prior baseline reproducible.
Common mistakes
Catch the wrong mental model
Calling gimbal lock a physical inability of the robot to rotate.
Describe it as a singularity of a particular Euler-angle coordinate convention.
Using an unstated roll-pitch-yaw order.
Document the exact axis/order convention at every interface and test a known orientation.
Sending a wxyz array directly into ROS xyzw fields.
Map named components, not array positions, and verify by rotating a known basis vector.
Treating q and -q as different physical orientations.
Use an orientation-aware angular comparison; both quaternions represent the same rotation.
Job connection
How this becomes employable evidence
A robot UI may display Euler angles for people while the ROS 2 stack exchanges quaternions. The engineer must convert safely, avoid discontinuous displays, preserve normalization, and match message/library component order.
Relevant target roles
- Robot HMI / Control & Monitoring Engineer
- Robotics Application / ROS 2 Integration Engineer
- Robotics Software Engineer — ROS 2 / AMR
Chapter 02 interview drill
Interview questions: Euler angles, quaternions, and representation singularities
Practise a 60–90 second answer: define the idea, connect it to a physical robot, state assumptions, frames, and units when relevant, then finish with the failure signal or evidence you would inspect.
Primary interview scenario
A pose looks correct until pitch approaches 90°, then yaw jumps. Explain whether the robot is locked, how you would represent the orientation internally, and which ROS component-order check you would make.
Answer shape: clarify the situation → trace the physical and software path → test the most likely boundaries → name the evidence that would confirm the result.
Technical follow-up questions
Q1What is actually lost at Euler gimbal lock?
One independent direction in that coordinate description; the rigid body itself remains free to rotate subject to its mechanism.
Q2What is the identity quaternion in ROS message order?
In ROS message order it is (x,y,z,w)=(0,0,0,1): the vector part is zero and the unit scalar represents no rotation.
Q3Why does the quaternion formula use half the physical rotation angle?
That is the unit-quaternion rotation parameterization: scalar and vector parts are cos(θ/2) and u sin(θ/2).