Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Coordinates and angles in robotics

After this lesson: I can describe where something is and which way it points using coordinates and an angle.

50 min lesson2 commandsJupyterLab data science lab: pandas, Matplotlib and scikit-learn in the browser

A coordinate frame says where a point is

A coordinate frame is a pair of axes and an origin that give every point in a plane a pair of numbers, x and y. Robotics starts here, because a statement such as "the hand is here and points that way" only means something inside a frame.

In this module everything is a simulation: no real robot is involved, only numbers and a short Python program. In this lesson you place points, measure angles and rotate a point about the origin.

By the end of this lesson

Note

I can describe where something is and which way it points using coordinates and an angle.

Key words

WordWhat it meansExample
Framea set of axes and an origin that numbers are measured from
Coordinatesthe numbers that say where a point is(3, 4)
Headingthe angle a thing points at, measured from the x axis
Posea position together with a heading
Radianan angle unit used by programming libraries; 180 degrees is about 3.1416 radians

The cells students fill in are left blank in this preview.

A point is a distance across and up

Draw a horizontal x axis and a vertical y axis that cross at the origin. A point is written (x, y): how far across, then how far up.

A negative x is to the left and a negative y is down. The point (3, 4) is three across and four up. Its straight-line distance from the origin comes from Pythagoras: the square root of 3 squared plus 4 squared, which is 5.

Worked example: four points in a frame

PointDistance from originHeadingWhere it is
(3, 4)553.13 degreesup and to the right
(0, 2)290 degreesstraight up
(-2, 0)2180 degreesstraight left
(1, 1)1.41445 degreesup and to the right, on the diagonal

The cells students fill in are left blank in this preview.

Measure a distance and a heading with NumPy

Command
docker exec jupyter python3 -c "import numpy as np; print(np.degrees(np.arctan2(1, 1)), np.hypot(3, 4))"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A pose is a position plus a heading

The Run above uses the lab as a calculator. np.hypot(3, 4) is the distance, 5.0. np.arctan2(1, 1) is the heading of the point (1, 1), and np.degrees turns it into 45.0.

A pose is a position and a heading together. A robot hand at (3, 4) pointing at 30 degrees has a different pose from one at (3, 4) pointing at 90 degrees.

Libraries measure angles in radians, not degrees

In this module angles are in degrees in the words and in the tables. Python's math functions use radians, where a half turn of 180 degrees is about 3.1416.

Convert once, at the door of each function: math.radians(degrees) goes in and math.degrees(radians) comes out. After that you never think about radians again.

Compute a distance and a heading

Code: python
import math

x = 3
y = 4
distance = math.sqrt(x * x + y * y)
heading = math.degrees(math.atan2(y, x))

print("distance from the origin:", round(distance, 2))
print("heading in degrees:", round(heading, 2))
# Change x to -3 and press Run.
# The heading jumps past 90 degrees. Which quarter of the plane is the point in now?

A common mistake

Warning

Many people pass degrees straight to math.sin or math.cos. Those functions expect radians, so math.sin(90) is not 1. Convert with math.radians(90) first, and the answer is 1.0.

Rotating a point turns it about the origin

To rotate a point (x, y) by an angle t, work out two new numbers. The new x is x times cos t, minus y times sin t. The new y is x times sin t, plus y times cos t.

The point stays the same distance from the origin and only its heading changes. A positive angle turns it anticlockwise, the usual direction in a maths diagram.

Worked example: rotating four points

StartTurnWorkingResult
(2, 0)90 degreesx = 2 cos 90 - 0, y = 2 sin 90 + 0(0, 2)
(1, 1)90 degreesx = cos 90 - sin 90, y = sin 90 + cos 90(-1, 1)
(2, 0)30 degreesx = 2 cos 30, y = 2 sin 30(1.732, 1)
(3, 4)180 degreescos 180 = -1 and sin 180 = 0(-3, -4)

The cells students fill in are left blank in this preview.

Rotate (2, 0) by 90 degrees with NumPy

Command
docker exec jupyter python3 -c "import numpy as np; t = np.radians(90); r = np.array([[np.cos(t), -np.sin(t)], [np.sin(t), np.cos(t)]]); print(np.round(r @ np.array([2, 0]), 3))"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Rotate a point in plain Python

Code: python
import math

def rotate(x, y, degrees):
    t = math.radians(degrees)
    new_x = x * math.cos(t) - y * math.sin(t)
    new_y = x * math.sin(t) + y * math.cos(t)
    return [round(new_x, 3), round(new_y, 3)]

print(rotate(2, 0, 90))
print(rotate(2, 0, 30))
# Try rotate(2, 0, 360). Where does the point end up, and why?

Going further

Open start.ipynb in JupyterLab and add a cell below the last one. Rotate the point (4, 0) by 45 degrees three times in a row.

Where does it land after the third turn, and is that the same as one turn of 135 degrees?

About this lab and its source: JupyterLab data science lab: pandas, Matplotlib and scikit-learn in the browser

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.