Lesson 3-1 · Introduction to the Grid
Lesson 3-1 · Introduction to the Grid
Teacher mode is on — the gray boxes are yours only. Flip the switch (bottom-right) to preview the student view.
You spent Module 2 teaching the robot to follow a line and detect a cross. Now
you'll put a whole grid of lines on the floor — and here's the good part: the
LINE TRACKING section of robot.py already knows how to drive it. No new driving
code needed; this lesson opens a new section of the file and fills it with tests.
Learning Objectives
By the end of this lesson you will be able to:
- Describe how a grid is built from lines and intersections
- Explain how a grid intersection is just the "cross" from Module 2
- Use
track_to_intersection()to arrive at the next intersection - Start the GRID DRIVING section of
robot.py, and write its first two tests from a description - Place the robot on the grid and reach the first intersection
A grid is just a lot of intersections
A grid is a set of taped lines running two directions — rows and columns. Wherever two lines cross is an intersection, and to the robot's two sensors an intersection looks exactly like the taped cross from Module 2: both sensors go high at once.
That's the whole insight: you already built everything needed to drive a grid. The LINE TRACKING section you finished in Lesson 2-9 has the three functions that do it all:
track_to_intersection()— follow the line to the next intersection and stop with the robot's center on it.turn_clockwise()— turn onto the perpendicular line to the right.turn_counterclockwise()— turn onto the perpendicular line to the left.
Do you need to change your driving toolkit to drive on the grid?
This lesson's skeleton
A new kind of driving gets a new section. Paste this into robot.py above RUN — it
goes after the whole LINE TRACKING section, and it starts with no functions of its
own, only tests, because there is nothing new to write yet.
# ===== GRID DRIVING =====
# Drives the taped grid one intersection at a time, using only
# the LINE TRACKING functions: a grid intersection is the
# cross from Module 2. To ARRIVE at an intersection is
# track_to_intersection(), which stops with the robot's
# center on it; a turn happens parked there. Nothing here
# touches a sensor.
def test_first_intersection():
# from a line, arrive at the next intersection and say so
pass
def test_arrive_and_turn():
# arrive at the intersection, then turn right onto the
# crossing line
pass
Two things are different from Module 2's skeletons. The tests have no bodies — the
comment says what the test should do and you write it, which from here on is how
every test arrives. And pass holds the place, as it did in Lesson 2-1, so the file
runs before you've written anything.
Drive to the first intersection
Place the robot centered on a line, facing toward the next intersection. One call,
track_to_intersection(), follows the line to the intersection and stops with the
robot's center on it, as it did on your circle. That one call is what "arrive at an
intersection" means for the rest of the course. Parked there, a turn spins the robot
in place on the cross, and the sensors come down on the crossing line.
Which function drives the robot from where it is to the next intersection?
Activity · Reach it, then turn
Step 1 · test_first_intersection()
Write the body: a print saying what it's about to do, track_to_intersection(), and a
print saying it got there. Set the run line to test_first_intersection(), put the robot on
a line segment facing the next intersection, and run. It should stop with its center
on the intersection. Try it from a few starting spots. Don't go on until it does.
Step 2 · test_arrive_and_turn()
Once you're parked on an intersection you can turn onto the crossing line. Write the
body: track_to_intersection(), then turn_clockwise(), with a print before the turn
and one after. Set the run line to it and run. The robot should arrive, pivot on the
intersection, and stop facing along the crossing line with a sensor on the tape.
One thing to watch in both tests: if the robot detects an intersection immediately, it started sitting right on one — nudge it back onto the line first.
You run the program and the robot reports 'Intersection reached!' instantly without moving. Why?
Real-world connections
Navigating a grid of intersections is how a lot of real automation gets around:
Fulfillment robots
Warehouse robots roll along floor grids, stopping at labeled intersections to pick shelves.
Street navigation
Self-driving cars think in intersections and blocks — drive to the next junction, then decide.
Grid worlds
Countless games move characters on a grid, one tile at a time, exactly like this.
Wrap-up
- What is a grid intersection, to the robot? (The same both-sensors-high "cross" from Module 2.)
- Which function drives one segment to the next intersection? (
track_to_intersection(), which stops with the robot's center on it.) - Why didn't you have to write any new driving code? (LINE TRACKING already handles lines and crosses — reuse. Only the tests are new.)