Skip to main content

Lesson 3-1 · Introduction to the Grid

Module 3 · Grid Driving

Lesson 3-1 · Introduction to the Grid

40–50 minCode reuseFrom circle to gridSlides (PowerPoint)
👩‍🏫

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.

A grid of three vertical and three horizontal taped lines. The nine places where they cross are circled: intersections. The XRP sits on the middle row between the first and second intersections, facing the next one. Labels: an intersection is where two lines cross and looks to the sensors just like the cross from Module 2; a segment is the stretch of line from one intersection to the next; the robot on a segment, facing the next intersection, is taken there by track_to_intersection().
Rows and columns of tape. Every place they cross is an intersection — a spot the robot can stop and turn.

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.
Knowledge Check

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.

Knowledge Check

Which function drives the robot from where it is to the next intersection?

Activity · Reach it, then turn​

An animation, seen from above, of both tests on a 3 by 3 taped grid. In step 1 the robot starts on a segment of the middle column, facing the next intersection. It follows the line until both sensors reach the intersection, then drives forward until its center sits on it, and stops. In step 2 it does the same from the same spot, then turns clockwise in place on the intersection and stops facing along the crossing line to the right. Beside it, the steps of each test in plain words, with the one running highlighted.
What both steps should look like: track_to_intersection() parks the robot on the intersection, and from there one turn lands on the crossing line.

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.

Knowledge Check

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:

Warehouses

Fulfillment robots

Warehouse robots roll along floor grids, stopping at labeled intersections to pick shelves.

Cities

Street navigation

Self-driving cars think in intersections and blocks — drive to the next junction, then decide.

Games

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.)

Resources​