Lesson 22: GPS navigation
Where am I?
- Delivery robots, drones and phones use GPS to know where they are: a pair of numbers, x and y.
- If the robot knows where it is and where it must go, maths gives it the way: which direction to turn and how far to drive.
- In VEXcode,
location.position(X, MM)andlocation.position(Y, MM)give the robot's x and y in millimetres.
Words for this lesson
| Word | Meaning |
|---|---|
| coordinates | the pair (x, y) that says where a point is |
| GPS | a system that tells a machine its position |
| waypoint | a point on a route that the robot must pass |
| Pythagoras | in a right-angled triangle, c² = a² + b² |
The maths
The robot is at (x, y). The target is at (tx, ty).
| Step | Python |
|---|---|
| across: dx = tx − x | dx = tx - x |
| up: dy = ty − y | dy = ty - y |
| how far: √(dx² + dy²) | math.sqrt(dx*dx + dy*dy) |
| which way | math.atan2(dx, dy) |
atan2gives the angle in radians;math.degrees(...)turns it into degrees forturn_to_heading.
Example: from (−600, −700) to (0, 0): dx = 600, dy = 700, distance = √(360000 + 490000) ≈ 922 mm.
Try the maths
from vexcode_vr import *
import math
def main():
dx = 300
dy = 400
brain.print(math.sqrt(dx * dx + dy * dy))
brain.new_line()
brain.print(round(math.degrees(math.atan2(dx, dy))))
vr_thread(main)
- The brain prints 500.0 (a 3-4-5 triangle) and 37: turn to heading 37° to face the point.
Dificultad 4/5 · 280 monedas + un pequeño bono en el primer intento de hoy
The charger is in the middle of the field, at (0, 0). The robot starts in a different place on each map, but it knows where it is: location.position(X, MM) y location.position(Y, MM). Work out how far away the charger is with Pythagoras and print it, for example Distance: 922 mm (use round). Then turn towards the charger and drive there.
Haz clic en Ejecutar para ver la salida aquí.
Dificultad 5/5 · 400 monedas + un pequeño bono en el primer intento de hoy
A delivery drone flies to a list of waypoints. Write a function go_to(tx, ty) that drives the robot from where it is now to the point (tx, ty): the same maths as before, with tx and ty instead of 0. Then visit the points in the list, in order. The robot starts in a different place on each map.
Haz clic en Ejecutar para ver la salida aquí.