TactivergeLite

Lite / Textbooks / Robotics From First Principles / Chapter 3

3. Vectors and frames

Version 1 · Technically checked (automated checks only) · 4 source lessons · Full interactive version

Introduction

A position is an arrow from somewhere. Change the somewhere and the same point gets a new address. Robots juggle frames constantly: the world frame on the map, the body frame on the chassis, a frame on every joint. This chapter adds and measures arrows, finds directions with atan2, and converts a point seen by the robot into a point on the map, which is how a robot knows where an obstacle really is.

Figure 3.1. One point, two addresses (described in words)

A robot at (2, 1) on a map, facing 90° (straight up the y axis). A point 1 m ahead in its body frame is at (2, 2) in the world, and a point 1 m to its left is at (1, 1).

Explanation

Built from 4 School lessons. Each part is the lesson's own explanation.

3.1 Vectors: arrows you can add

A drone flies 3 m east, then 4 m north. It is 5 m from where it started, not 7. Where did the other 2 m go?

Size and direction, written as components

A vector is a quantity with a size and a direction, such as a displacement, a velocity or a force. In 2D we write it as two components, v = (vx, vy): how far along x, then how far along y, measured in some coordinate frame. The same arrow drawn anywhere on the page is the same vector; only its components matter.

Adding: tip to tail, or part by part

To add a and b, draw b starting where a ends; the sum runs from the start of a to the end of b. Algebraically that is just a + b = (ax + bx, ay + by). Subtraction works the same way: a − b is the arrow from the tip of b to the tip of a. Scaling by a number k multiplies both parts: k·a = (k·ax, k·ay).

Magnitude: Pythagoras in disguise

The components form the two short sides of a right-angled triangle, so the length (magnitude) is |v| = √(vx² + vy²). Lengths do not add like components: |a + b| ≤ |a| + |b|, with equality only when a and b point the same way. That is the drone's missing 2 m: 3 east + 4 north is (3, 4), length 5.

Common mix-ups

Open the Lite lesson: Vectors: arrows you can add

3.2 Which way is it pointing? Direction with atan2

Your calculator says tan⁻¹(−1 ÷ −1) = 45°. But (−1, −1) points down and to the left. Who is wrong?

Two ways to write the same arrow

Components: (x, y). Polar form: length r and direction θ, measured anticlockwise from +x. To go from polar to components: x = r cos θ, y = r sin θ. To go back: r = √(x² + y²) and θ from the inverse tangent, carefully.

Why tan⁻¹(y/x) lies

tan θ = y/x, but dividing throws away the signs: (1, 1) and (−1, −1) both give y/x = 1. So tan⁻¹ can only answer between −90° and 90°: it is right for vectors pointing rightwards (x > 0) and exactly 180° out for vectors pointing leftwards. At x = 0 it divides by zero.

atan2: the angle that knows its quadrant

atan2(y, x) takes the two parts separately, checks their signs and returns an angle in (−180°, 180°]. Add 360° to negative answers if you want 0–360°. It is in every language: Python's math.atan2, JavaScript's Math.atan2, C's atan2. Note the order: y first. Robot code uses it to turn components back into headings and joint angles.

Common mix-ups

Open the Lite lesson: Which way is it pointing? Direction with atan2

3.3 One point, many addresses: coordinate frames

The gripper's camera says the cup is 10 cm ahead. The base says it is 52 cm to the right. They are both correct. How?

A frame is an origin plus axes

A coordinate frame is a chosen origin and a pair of perpendicular unit axes (x and y in 2D). A point's coordinates are its distances along those axes from that origin. Change the origin or turn the axes and the numbers change, even though the point hasn't moved.

Every part gets a frame

Robots typically use a world frame fixed to the room, a base frame on the robot, a frame on each link (x along the link) and a tool frame at the gripper tip. Sensors have frames too: a camera measures in a camera frame. We label points with the frame they're in, for example p_base or p_camera, and never mix them.

Translating between frames

If frame B's origin sits at t in frame A and B's axes are not rotated, then p_A = p_B + t. If B is also rotated, the axes no longer line up and we must rotate first: p_A = R·p_B + t. Rotations need matrices (week 13); the full move between frames is a transform (week 14). For now: always ask 'in which frame is this number?'.

Common mix-ups

Open the Lite lesson: One point, many addresses: coordinate frames

3.4 Body frame, world frame: how a vehicle sees the world

A pilot says 'traffic at two o'clock'. Air traffic control says 'traffic north-east of you'. Same aircraft. Why two languages?

Two frames, two jobs

The world frame is fixed: for an aircraft or rover, east (x) and north (y) on a flat map; for a spacecraft, axes pointing at chosen distant stars, because space has no 'north'. The body frame moves with the vehicle: x out of the nose, y out of the left side (and z up in 3D). Engines push along body axes; maps and waypoints are given in world axes.

Heading links them

Heading is the compass direction of the nose, in degrees clockwise from north. Maths angles go anticlockwise from +x (east). So the nose's maths angle is ψ = 90° − heading. A heading of 0° (north) is ψ = 90°; heading 90° (east) is ψ = 0°. Mixing the two conventions is a classic navigation bug.

What changes when you turn

Turn the vehicle and every body-frame coordinate of the outside world changes, while world-frame coordinates stay put. That is why a pilot's 'two o'clock' needs updating after every turn, while air traffic control's 'north-east' does not. Converting a world point into the body frame means: subtract the vehicle's position, then rotate by −ψ.

Common mix-ups

Open the Lite lesson: Body frame, world frame: how a vehicle sees the world

Worked examples

Vectors: arrows you can add: example 1

A rover drives a = (5, −2) m, then b = (−1, 6) m. Where does it end up, and how far is that from the start?

Solution

a + b = (5 − 1, −2 + 6) = (4, 4) m. Distance = √(4² + 4²) = √32 ≈ 5.66 m.

Which way is it pointing? Direction with atan2: example 1

Give the direction of (4, −9) in degrees from 0 to 360.

Solution

Fourth quadrant. atan2(−9, 4) ≈ −66.0°; add 360° → 294.0°. (Here tan⁻¹ happens to agree, −66°, because x > 0.)

Which way is it pointing? Direction with atan2: example 2

A 2.5 m link points at 200°. Where is its tip?

Solution

x = 2.5 cos 200° ≈ −2.349 m, y = 2.5 sin 200° ≈ −0.855 m. Both negative: third quadrant, as expected for 200°.

One point, many addresses: coordinate frames: example 1

A camera is mounted 0.1 m forward and 0.05 m up from a gripper's origin, not rotated. It sees a cup at (0.25, −0.05) m in its own frame. Where is the cup in the gripper frame?

Solution

p_gripper = p_camera + t = (0.25 + 0.1, −0.05 + 0.05) = (0.35, 0) m: dead ahead of the gripper.

Body frame, world frame: how a vehicle sees the world: example 1

A simulated drone is at (0, 0) heading 180° (south). A waypoint is at (4, 0) km (east). Where is it in the body frame?

Solution

ψ = 90° − 180° = −90°: the nose points along −y. d = (4, 0). Forward = d·(cos ψ, sin ψ) = 4 × 0 + 0 × (−1) = 0. Left = d·(−sin ψ, cos ψ) = 4 × 1 + 0 = 4 km. Facing south, east is on your left.

Interactive figures, in words

Figure 3.2 from Vectors: arrows you can add

In 'Add tip to tail', set a = (3, 1) and b = (1, 2.5). Check the green sum against your prediction. Now make b point exactly opposite to a. What happens to a + b? Then find a b for which |a + b| = |a| + |b|.

The vector playground draws vector a in blue from the origin, vector b in orange starting at the tip of a, and their sum in dashed green from the origin to the tip of b. The readout lists every component, the sum and its length, and the same information is in the text above.

Figure 3.3 from Body frame, world frame: how a vehicle sees the world

Start with the aircraft. Choose ALPHA and read its body-frame position. Now change only the heading, in steps of 45°. Which numbers in the readout change, and which stay the same? Switch to the spacecraft: what replaces 'north'?

The diagram shows a top-down map with a vehicle icon, its forward (blue) and left (green) body axes, and a diamond-shaped waypoint joined to it by a dashed line. The readout gives the waypoint's world coordinates, distance and bearing, plus its body-frame forward and left distances. Turning the vehicle changes only the body-frame numbers.

The interactive versions are in the full chapter.

Laboratory

These run in a browser on the full site (they need a faster connection). Simulations are simplified educational models.

Practice

Each question links to its Lite lesson, where your answer is checked on our server and a wrong answer gets a hint. Answers are never sent to your device.

  1. a = (2, −3) and b = (−5, 4). What is the x component of a + b?
    Answer it in “Vectors: arrows you can add”
  2. What is the magnitude of the vector (6, 8)?
    Answer it in “Vectors: arrows you can add”
  3. atan2(3, −4) is closest to which angle?
    Answer it in “Which way is it pointing? Direction with atan2”
  4. For which vector does tan⁻¹(y/x) give the correct direction?
    Answer it in “Which way is it pointing? Direction with atan2”
  5. Frame B's origin is at (3, −1) in frame A, with the same axis directions. A point is at (2, 4) in B. What is its x coordinate in A?
    Answer it in “One point, many addresses: coordinate frames”
  6. In a simulation, an aircraft is at (−4, 0) km with its nose pointing 135° anticlockwise from the world +x axis. A waypoint is at (4, −2) km. In the aircraft's body frame (x forward, y to the left), what is the waypoint's forward (x) coordinate, in km? (2 decimal places; negative means behind)
    Answer it in “Body frame, world frame: how a vehicle sees the world”
  7. A rover heads north (ψ = 90°) from (0, 0). A rock is at (−3, 5) m. How far to the LEFT of the rover is it, in metres? (Body frame: y is left.)
    Answer it in “Body frame, world frame: how a vehicle sees the world”

Engineering applications

Mapping obstacles

A lidar reports each obstacle as a distance and angle from the robot. To put it on a shared map, the robot rotates it by its own heading and adds its own position: a body-to-world conversion, many thousands of times a second.

Why atan2

The angle of an arrow from tan alone is ambiguous: (1, 1) and (−1, −1) give the same ratio. atan2 looks at both signs and returns the right quadrant, which is why navigation code uses it everywhere.

Watch (optional)

Streaming video uses a lot of data. Everything you need is in the text.

Summary

Glossary

Vector addition
Vectors are added component by component: the x parts add, the y parts add, and the result is the single vector with the same effect as both. Field Guide
Vector magnitude and direction
A 2D vector can be written as components (x, y) or as a magnitude and a direction angle; cosine and sine convert between the two. Field Guide
Coordinate frame
A coordinate frame is an origin plus a set of axes used to describe positions and directions. Robots use several: world, base, each joint, sensors and the tool. Field Guide
Pythagoras' theorem
In a right-angled triangle, the square of the hypotenuse equals the sum of the squares of the other two sides. Field Guide
Dot product
The dot product of two vectors multiplies matching components and adds them; it also equals the product of their lengths and the cosine of the angle between them. Field Guide

Project: From sensor to map

A robot at a known position and heading sees three obstacles, each given as a distance and bearing relative to its nose. Convert all three into world coordinates by hand and in code, then repeat after the robot turns 90°. Plot both sets and explain why the world positions did not change.

Deliverables

Write it up on paper or in your own notes. Lite never stores what you write.

References

Further reading from the lessons

← 2. Actuators and drives4. Rotations and transforms →