Stage 08 · Lesson 02/15

Kinematics & Inverse Kinematics · Lesson 02

Coordinate Frames & Reference Frames

Est. 45–60 min

Why

Why this lesson matters

In Lesson 1 you said “the tip is at (12, 6)”. But (12, 6) measured from where, and along which directions? A position only means something when you name its frame. A robot has many frames — the base, every link, the tool, the camera, the table — and kinematics is mostly about moving numbers correctly from one frame to another.

By the end of this lesson you will be able to: say what a coordinate frame is (an origin plus axes); use the right-hand rule to check a 3D frame; name the common robot frames (world, base, link, flange, tool, camera, sensor); convert a point from one frame to another with p_B = R·p_A + o; and turn a real distance-sensor reading into a point in the robot’s base frame.

A young man looking through a theodolite on a blue tripod on an open sandy field, with buildings in the background
A student measuring with a theodolite in the field. Surveyors always measure from a known reference point and direction — a frame — so that everyone’s map numbers agree.Photo: Kskhh · CC BY-SA 4.0 · Source: Wikimedia Commons · resized, converted to WebP

Concept

What a frame is

A frame = an origin + axes

A coordinate frame is a point called the origin plus a set of axes: x and y on a flat table, or x, y and z in 3D. The axes are at right angles to each other and have a unit length. Any point can then be written as numbers along those axes. Change the origin or turn the axes, and the *same* point gets *different* numbers.

We name frames with curly brackets: {B} for the robot base, {C} for a camera, {T} for the tool. We also write the frame as a subscript: p_B means “point p, written in frame {B}”. Never write x, y, z without saying which frame — that is the most common mistake in robotics notebooks.

Right-handed frames

In 3D there are two ways to arrange three axes, and they are mirror images. Robotics (and ROS) uses right-handed frames: curl the fingers of your right hand from +x towards +y and your thumb points along +z. Or: thumb = x, index finger = y, middle finger = z. ROS also suggests a habit for bodies like a mobile robot or a camera mount: x forward, y left, z up.

Two hands drawn side by side, each with thumb, index finger and middle finger showing x, y and z axes; the left is labelled left-handed coordinates and the right is labelled right-handed coordinates
Left-handed vs right-handed coordinates. Robot frames are always right-handed. If your frame only works with your left hand, one axis is flipped.Photo: Primalshell · CC BY-SA 3.0 · Source: Wikimedia Commons · resized, converted to WebP

The frames on a robot arm

FrameWhere its origin isUsed for
World {W}A fixed point in the room or on the tableWhere everything is, shared by all robots and cameras
Base {B}The bottom of the robot, on the first joint axisThe robot’s own ‘home’ frame; FK usually gives the tool in {B}
Link / joint {1}, {2}, …On each joint axisBuilding FK one link at a time (Lessons 6–9)
Flange {F}The mounting plate at the end of the last linkWhere tools are bolted on
Tool {T}The working point: pen tip, gripper centre, weld tipWhat the task cares about
Camera {C} / sensor {S}Inside the camera or sensorWhere measurements are first written

Moving a point between frames (2D)

Suppose frame {A} sits inside frame {B}: its origin is at o (measured in {B}) and it is turned by an angle yaw. A point written in {A} as p_A becomes, in {B}:

From frame {A} to frame {B} · Text
p_B = R(yaw) · p_A + o

R(yaw) = [ cos(yaw)  −sin(yaw) ]
         [ sin(yaw)   cos(yaw) ]

Read it in two steps: first rotate the point by yaw so its numbers line up with {B}’s axes, then add the origin o to slide it into place. To go back from {B} to {A}, do the opposite in the opposite order: subtract o, then rotate by −yaw. In Lessons 4–5 you will pack R and o into one matrix.

Visual

Visualise it: the frame explorer

Drag and turn the tool frame. The point does not move — only its numbers in each frame change.

Interactive · Coordinate-frame explorer

Base, joint, and end-effector frames
XbYbTool {T}
p_B = R(θ)·p_T + o → (92.0, 87.3)

Code lab

Code lab: one cup, three frames

frames.py has a camera {C} on the table, 30 cm in front of the robot base and 10 cm to its left, turned 180° so it looks back at the robot. The camera sees a cup. The code converts the cup into the base frame, converts it back, shows how far the arm would miss if you forgot the frame, and finally finds the pen tip {T} from the flange {F}.

frames.py · Python
# frames.py - one cup, three frames, three different sets of numbers (top view, 2D, cm).
# A frame = an origin + x and y axes. To move a point from frame {A} into frame {B}:
#     p_B = R(yaw) * p_A + o      (o = where A's origin is, measured in B; yaw = how A is turned)
import math


def rot(yaw_deg):
    c, s = math.cos(math.radians(yaw_deg)), math.sin(math.radians(yaw_deg))
    return ((c, -s), (s, c))


def a_to_b(p, yaw_deg, origin):
    """Point p given in frame A -> the same point in frame B."""
    (r00, r01), (r10, r11) = rot(yaw_deg)
    return (r00 * p[0] + r01 * p[1] + origin[0], r10 * p[0] + r11 * p[1] + origin[1])


def b_to_a(p, yaw_deg, origin):
    """The reverse: subtract the origin, then rotate back by -yaw."""
    q = (p[0] - origin[0], p[1] - origin[1])
    return a_to_b(q, -yaw_deg, (0.0, 0.0))


# Frame {C}: a camera on the table, 30 cm in front of the robot base {B} and 10 cm to the left,
# turned 180 deg so it looks back at the robot.
CAM_YAW, CAM_ORIGIN = 180.0, (30.0, 10.0)
# Frame {T}: the tool (pen tip) on the arm's flange {F}: 5 cm further along the flange's x axis.
TOOL_IN_FLANGE = (5.0, 0.0)

cup_in_cam = (12.0, 4.0)                    # what the camera reports
cup_in_base = a_to_b(cup_in_cam, CAM_YAW, CAM_ORIGIN)
print("1) The same cup in two frames")
print(f"   camera {{C}} says: x = {cup_in_cam[0]:6.2f}, y = {cup_in_cam[1]:6.2f}")
print(f"   base   {{B}} says: x = {cup_in_base[0]:6.2f}, y = {cup_in_base[1]:6.2f}")
back = b_to_a(cup_in_base, CAM_YAW, CAM_ORIGIN)
print(f"   and back to {{C}}: x = {back[0]:6.2f}, y = {back[1]:6.2f}   (round trip works)")

print("\n2) Forgetting the frame: using the camera numbers as if they were base numbers")
err = math.dist(cup_in_cam, cup_in_base)
print(f"   the arm would miss the cup by {err:.2f} cm")

print("\n3) Flange {F} vs tool {T}: where is the pen tip?")
for flange_yaw, flange_origin in ((0.0, (18.0, 0.0)), (90.0, (0.0, 18.0)), (45.0, (12.73, 12.73))):
    tip = a_to_b(TOOL_IN_FLANGE, flange_yaw, flange_origin)
    print(f"   flange at ({flange_origin[0]:5.2f}, {flange_origin[1]:5.2f}) yaw {flange_yaw:4.0f}°"
          f" -> pen tip at ({tip[0]:5.2f}, {tip[1]:5.2f})")

Output (run with python3 frames.py)

1) The same cup in two frames
   camera {C} says: x =  12.00, y =   4.00
   base   {B} says: x =  18.00, y =   6.00
   and back to {C}: x =  12.00, y =   4.00   (round trip works)

2) Forgetting the frame: using the camera numbers as if they were base numbers
   the arm would miss the cup by 6.32 cm

3) Flange {F} vs tool {T}: where is the pen tip?
   flange at (18.00,  0.00) yaw    0° -> pen tip at (23.00,  0.00)
   flange at ( 0.00, 18.00) yaw   90° -> pen tip at ( 0.00, 23.00)
   flange at (12.73, 12.73) yaw   45° -> pen tip at (16.27, 16.27)

Try it: move and turn the tool frame and watch the same point get new numbers in the base frame.

Interactive · Coordinate-frame explorer

Base, joint, and end-effector frames
XbYbTool {T}
p_B = R(θ)·p_T + o → (92.0, 87.3)

Real world

Frames in the real world

  • Robot cells: a vision camera reports parts in its own frame; a one-time calibration finds the camera’s pose in the robot base frame so every part can be converted.
  • ROS: every robot is a tree of named frames (base_link, tool0, camera_link…) and a library called tf2 converts points between them. ROS frames are right-handed, with x forward, y left, z up for bodies.
  • Surveying and maps: land surveyors and GPS all measure against agreed reference frames so maps from different teams fit together.
  • Tool changes: when a robot swaps a gripper for a screwdriver, only the flange-to-tool offset changes — the rest of the kinematics stays the same.
A right hand with thumb, index finger and middle finger held at right angles, with arrows drawn along each labelled x, y and z
The right-hand rule with three fingers: thumb, index and middle finger give the x, y and z axes of a right-handed frame (the labels also show a physics use of the same rule).Photo: Abdull · CC BY-SA 3.0 · Source: Wikimedia Commons · resized, converted to WebP

Bench build

Bench build: a sensor that reports in the robot’s frame

An HC-SR04 ultrasonic sensor measures distance straight ahead, so in its own frame {S} an object is always at (d, 0). Place the sensor on the table at a measured position and angle in the robot base frame {B}, and sensor_to_base.ino prints the object in {B} — numbers the arm could use directly.

Wiring
HC-SR04 pinArduinoNote
VCC5 VThe sensor needs 5 V
GNDGND—
TRIGPin 9Arduino sends a 10 µs pulse
ECHOPin 10Sensor answers with a pulse as long as the echo time
  1. Draw the base frame

    Tape a sheet of chart paper on the table. Mark the base origin and draw +x (forward) and +y (left) with a ruler.

  2. Place the sensor

    Put the sensor at, say, x = 25 cm, y = −10 cm, facing +y (yaw 90°). Type those numbers into SENSOR_X, SENSOR_Y and SENSOR_YAW_DEG.

  3. Upload and read

    Open Serial Monitor at 115200. Put a box in front of the sensor and note x_B, y_B.

  4. Check with a ruler

    Measure the box’s front face from the base origin. It should agree within about 1 cm.

  5. Break it on purpose

    Turn the sensor 30° without changing SENSOR_YAW_DEG. How wrong do the numbers get? Now fix the yaw in the code.

sensor_to_base.ino · Arduino C++
// sensor_to_base.ino - turn a distance-sensor reading into a point in the ROBOT BASE frame.
// An HC-SR04 ultrasonic sensor sits on the table at a known place and angle (its own frame {S}).
// It measures distance d straight ahead (along its x axis), so in {S} the object is at (d, 0).
// Then p_B = R(yaw) * p_S + o  gives the object in the base frame {B} - the frame the arm uses.
// Wiring: VCC -> 5 V, GND -> GND, TRIG -> pin 9, ECHO -> pin 10. No motors in this build.
const int TRIG = 9, ECHO = 10;
// Where the sensor is, measured in the BASE frame with a ruler (cm, degrees). CHANGE THESE.
const float SENSOR_X = 25.0, SENSOR_Y = -10.0, SENSOR_YAW_DEG = 90.0;

float readCm() {
  digitalWrite(TRIG, LOW); delayMicroseconds(2);
  digitalWrite(TRIG, HIGH); delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  unsigned long us = pulseIn(ECHO, HIGH, 30000UL);   // time-out after 30 ms (about 5 m)
  if (us == 0) return -1;                           // nothing in range
  return us * 0.0343 / 2.0;                         // speed of sound ~343 m/s, there and back
}

void setup() {
  pinMode(TRIG, OUTPUT); pinMode(ECHO, INPUT);
  Serial.begin(115200);
  Serial.println(F("d_cm,  x_S, y_S,  ->  x_B, y_B"));
}

void loop() {
  float d = readCm();
  if (d < 0) { Serial.println(F("no echo")); delay(300); return; }
  float xs = d, ys = 0;                             // the point in the SENSOR frame
  float a = radians(SENSOR_YAW_DEG);
  float xb = cos(a) * xs - sin(a) * ys + SENSOR_X;  // rotate, then add the sensor's origin
  float yb = sin(a) * xs + cos(a) * ys + SENSOR_Y;
  Serial.print(d, 1); Serial.print(F(",  ")); Serial.print(xs, 1); Serial.print(F(", "));
  Serial.print(ys, 1); Serial.print(F(",  ->  ")); Serial.print(xb, 1); Serial.print(F(", "));
  Serial.println(yb, 1);
  delay(300);
}

Debugging

Debugging frames

SymptomLikely causeFix
Point is mirrored left–rightOne axis points the wrong way (left-handed frame)Check with the right-hand rule; flip that axis
Error grows the further the object isYaw is wrongMeasure the sensor or camera angle again
Error is the same everywhereOrigin o is wrongRe-measure where the frame’s origin is
Round trip does not give the start point backInverse done in the wrong orderSubtract o first, then rotate by −yaw
Numbers look right but the arm missesMixed tool frame and flange frameAdd the tool offset (code Part 3)

Think

Think about it

Think

A camera is bolted to the robot’s wrist instead of the table. The arm moves. Do the numbers you need to convert camera points into the base frame stay the same?

Experiment

Try it in the lab

About 30 minutes with the Python file, the frame explorer and (optionally) the sensor bench.

Experiment

Frames on your desk

You will need

  • `frames.py` and Python 3
  • Frame explorer (in your browser)
  • Chart paper, ruler, protractor
  • Optional: Arduino UNO, HC-SR04 sensor, jumper wires, a small box

Steps

  1. Hold up your right hand and make a right-handed frame. Point x at the door — where does z point?
  2. Run `frames.py`. Check the cup’s base-frame numbers by hand: rotate (12, 4) by 180°, then add (30, 10).
  3. Change `CAM_YAW` to 90° and predict the new base-frame position before running.
  4. Add a second camera on the other side of the table and convert the same cup from it. Do both cameras agree in {B}?
  5. In the frame explorer, find a tool pose where the point’s tool-frame numbers are (0, 0).
  6. Optional: build the sensor bench and check three box positions with a ruler.

Interactive · Coordinate-frame explorer

Base, joint, and end-effector frames
XbYbTool {T}
p_B = R(θ)·p_T + o → (92.0, 87.3)

A wrong origin shifts every point by the same amount; a wrong yaw makes the error grow with distance. Both give believable numbers, which is why you must always name and check your frames.

Knowledge check

Check your understanding

Knowledge check

1. A coordinate frame is made of…

2. p_B = R·p_A + o converts a point…

3. In a right-handed frame, curling your right fingers from +x to +y makes your thumb point along…

4. In frames.py the camera says (12, 4) and the base says (18, 6). How far would the arm miss if you used the camera numbers directly?

5. Your converted points are wrong by more when the object is farther away. The most likely cause is…

6. The tool frame {T} is different from the flange frame {F} when…

7. ROS’s habit for a body frame is…

Mini assignment

Hands-on assignment

Mini assignment

Frame map of your robot desk

Draw every frame on your robot desk and convert one object through them.

Deliverables

  • A top-view sketch with {W}, {B}, {F}, {T} and one sensor or camera frame, each with origin and x, y axes (and z by the right-hand rule).
  • A table listing each frame’s origin and yaw measured in {B}.
  • One object converted from the sensor/camera frame into {B} by hand, and checked with `frames.py`.
  • One sentence each: what goes wrong with a wrong origin, and with a wrong yaw.
  • Optional: three sensor-bench readings next to ruler measurements.

Do this, step by step

  1. Run python3 frames.py. Check Part 1 by hand: rotate the camera point by the camera’s yaw, then add the camera origin.
  2. Measure your own desk: put a sheet of graph paper down as {B} and mark where {W}, the flange {F}, the tool {T} and one sensor sit. Write each origin and yaw in a table.
  3. Pick one object (a coin or eraser). Measure it in the sensor frame with a ruler, convert it to {B} by hand, then change the numbers in frames.py and confirm.
  4. On purpose, use a wrong yaw (add 10°) and then a wrong origin (add 2 cm). Write down how far the converted point moves each time.
  5. In the frame explorer, set the same yaw and origin as your sensor and check the drawn point matches your table.

Stuck? Hints, one at a time

NoteTry the task first. Read one hint, try again, and only then read the next one.
  1. Rotate first, then add the origin. Doing it the other way round moves the point wrongly.
  2. Yaw is measured from the {B} x axis, anticlockwise positive when seen from above (z up).
  3. If the round trip does not return the same point, check the sign of the yaw in b_to_a().

Challenges (pick by level)

  • 🟢 Easy: Use your right hand to label z for three different x, y pairs drawn by a friend.
  • 🟢 Easy: Convert (10, 0) from a frame with yaw 90° and origin (5, 5) into {B} by hand.
  • 🟡 Medium: Add a second camera to frames.py and show both cameras give the same base-frame cup.
  • 🟠 Hard-ish: Write chain(p, frames) that converts a point through a list of (yaw, origin) frames, e.g. tool → flange → base.
  • 🔴 Hard: Extend frames.py to 3D with a rotation about z only, and check your frame with the right-hand rule.
  • ⚫ Research: Read ROS REP 103 and list the three conventions it sets for body frames and camera (optical) frames.

Next step

A frame tells you *where from*. In Lesson 3 — Position, Orientation & Pose you will describe both *where* the tool is and *which way it points* — together called its pose.

Look back: Lesson 1: Why Robots Need Kinematics. Next: Lesson 3: Position, Orientation & Pose. Related: Stage 1: Frames & Reference Frames and Stage 11 — Computer Vision.

Sources

Self-attested for now — keep artifacts in your lab notebook.

Complete

Today's goal

0 of 1 done today

0-day streak
Daily goal:lessons, quizzes or assignments a day

Mark this lesson complete when you have worked the experiment, knowledge check, and assignment (even roughly).

Stuck somewhere?

Ask a doubt about this lesson

Saved with this lesson's id and your email so we can reply. Privacy · or email contact@aihubstation.in