Lesson 3-3 · Turning on the Grid
Lesson 3-3 · Turning on the Grid
Teacher mode is on. Toggle it off (bottom-right) to preview the student view.
You can drive straight along the grid. Now you'll turn corners — sequencing drives and turns to trace an L-shaped path. Any route across a grid is really just a sequence of "drive some segments, turn, drive some more."
Learning Objectives
By the end of this lesson you will be able to:
- Sequence drive and turn operations into a path
- Turn at intersections with
turn_clockwise()andturn_counterclockwise(), and say why the turn pivots on the intersection - Navigate an L-shaped path, as a test function written from a description
- Plan a path on paper before you write the code
This lesson's skeleton
Paste this into robot.py above RUN, at the end of the GRID DRIVING section. No
new function this time — a path is calls to what you have — so it's two tests.
# --- Lesson 3-3: turning on the grid ---
# No new function: a path is drive_intersections(), a turn,
# drive_intersections() again. The turn works because the
# helper leaves the wheels parked on the intersection.
def test_l_shape():
# drive 2 intersections, turn right, drive 2 more, then
# print that the L is complete
pass
def test_my_path():
# the path you planned on paper, one leg per line
pass
Drive, turn, drive
Turning a corner on the grid is just like walking city streets: go to the
intersection, then turn. In code that's drive_intersections(), then a turn, then
drive_intersections() again — three lines for an L, one per leg or corner.
Which sequence drives an L-shape — two segments, a right turn, then two more segments?
Why the turn lands on the new line
Look at what drive_intersections() leaves behind: the robot parked with its center
over the last intersection, sensors just past it. That is exactly the position a turn
needs. The robot pivots about its center of rotation (Lesson 2-8), so with that
point on the cross it spins on the intersection, and the blind 60° plus the sensor-ended finish from Lesson 2-9
bring the sensors down on the crossing line. After the turn, the next
drive_intersections() starts with a line to follow — no extra step in between.
After turn_clockwise(), what has to happen before the next drive_intersections()?
Activity · The L, then a path of your own
Step 1 · test_l_shape()
Write the body from its comment — three calls and a print — set the run line to
test_l_shape(), and run it. Start the robot on a line segment with at least two
intersections ahead and room to the right. It should drive two segments, pivot on the
second intersection, drive two more along the crossing line, and stop parked on the
fourth intersection it met. Don't go on until it does.
Step 2 · Plan on paper, then test_my_path()
Before coding a path, sketch it: mark the start, the direction the robot faces, how
many segments each leg is, and where the turns go. A plan reads like directions —
leg 1: three segments; corner: left; leg 2: one segment — and translates one leg
or corner per line, with a comment saying which is which. Write that into
test_my_path(), set the run line to it, and run. Did the robot end where your
sketch says? If not, the sketch and the code disagree somewhere; find the line.
Starting at the bottom-left corner facing away from you, the robot runs: drive_intersections(3), turn_clockwise(), drive_intersections(2). Where does it end up?
Real-world connections
Turn-by-turn sequencing on a grid is exactly how routed navigation works:
Turn-by-turn directions
"Go 3 blocks, turn right, go 2 blocks" is the same drive/turn sequence you just wrote.
Pick routes
A warehouse robot's route is a list of segment-drives and turns between shelf intersections.
Toolpaths
CNC and pick-and-place machines follow planned sequences of moves and direction changes.
Wrap-up
- What's the basic shape of any grid path? (A sequence of drives and turns.)
- What has to come before a turn? (An arrival — the robot parked on the intersection.)
- What comes after a turn? (The next drive — a sensor is already on the new line.)