Skip to main content

Lesson 3-4 · Final Project

Module 3 · Grid Driving

Lesson 3-4 · Final Project

50–60 minCapstoneDrive a squareSlides (PowerPoint)
👩‍🏫

Teacher mode is on. Toggle it off (bottom-right) to preview the student view.

Time to tie the module together. Your robot will drive a complete square on the grid — two intersections per side, turn right at each corner, four times — and end up right back where it started. Same robot.py, no new Python: just your sections, put to work.

Learning Objectives

By the end of this project you will be able to:

  • Design a test function that drives a closed square path on the grid
  • Use a for loop to repeat a drive-and-turn sequence
  • Test and tune a real robot until the path closes
  • Verify the robot returns to its starting position

The mission​

Make the robot drive a square: each side is 2 intersections, with a right turn at each of the 4 corners. Done right, it finishes where it began.

An animated top-down view of the 3×3 grid. The robot starts parked on the bottom-left corner, nearest you, facing away. It follows the line to the next intersection, stops with its sensors on the cross, drives forward to park its wheels on it, and does that once more to reach the corner. There it turns right in place until a sensor finds the cross street. It repeats that for all four sides and ends back on the start corner facing the way it began. A panel beside the grid shows which step the program is on: Ready, Side N of 4, Turning right, Square complete.
What your finished project should do. Each side is two arrivals at an intersection; each corner is one right turn.

Your program must: use the LINE TRACKING section and drive_intersections() unchanged, drive the square with a for loop (not four copied blocks), print progress, and return to start.

This lesson's skeleton​

Paste this into robot.py above RUN, at the end of the GRID DRIVING section. One setting and two tests; the second test is the project.

# --- Lesson 3-4: final project, the square ---
# The module project: four sides of SIDE intersections with a
# right turn at every corner, back to the start. Built from
# drive_intersections() and one turn; nothing new is defined.
SIDE = 2 # intersections per side

def test_one_side():
# one side of the square: drive SIDE intersections, then
# turn right
pass

def test_square():
# the project. A for loop that runs one side per pass,
# printing which side it is on; then the finish message
pass

SIDE is a setting, in capitals like the others, so the size of the square is one number at the top of the sub-section and not a literal buried in a loop.

One loop, four sides​

The whole square is a four-iteration loop: each pass drives one side, then turns the corner. The piece that drives a side is drive_intersections(count) — you built and tested it in Lesson 3-2 — so the loop body is a call to it and a turn.

Sketch the loop body in English first: what happens once per side? Then write it. The loop body is two calls and a print or two; if yours is longer, ask what the extra lines are for. There is nothing between the drive and the turn: the helper leaves the robot parked on the corner, and the turn ends with a sensor on the next side's line.

Four right turns add up to a full 360°, which is why the robot comes back around to its starting heading as well as its starting intersection.

Knowledge Check

The square loop runs turn_clockwise() once per side, four times. How many total degrees does the robot turn?

Activity · Test one side at a time​

Don't run all four sides first. Build up, one test at a time:

  1. One side — write test_one_side() and set the run line to it. The robot should drive SIDE intersections, pivot on the corner, and stop facing along the next side. Don't go on until it does.
  2. Two sides — write test_square() with range(2) for now. What shape should that give you? Predict, then run.
  3. Full square — change it to range(4); does it close back to the start?
Knowledge Check

A student wrote `for leg in range(3):` and added drivetrain.straight(8.75, 0.5) right before turn_clockwise(). What are the two bugs?

Activity · Extend it​

Once the square works, try a variation:

Bigger

3-per-side square

Set SIDE = 3 for a larger square. Does it still close?

Rectangle

Alternating legs

Write test_rectangle(): a list [2, 3, 2, 3] indexed by the loop variable, so opposite sides differ.

Mirror

Left-turn square

Swap in turn_counterclockwise() to drive the square the other way around.

An animation, seen from above, of the three extensions on a 4 by 4 taped grid. First a bigger square with SIDE = 3 from the bottom-left corner. Then a rectangle with sides of 2, 3, 2 and 3 intersections. Then a square driven with left turns, starting from the bottom-right corner and going round counterclockwise. Each ends back on its start corner facing the way it began. Beside it, the loop for each run, with the line running highlighted.
The three extensions, one after another. Each changes one thing and still closes on its start corner.

Activity · Reflect​

Demo your square and think back:

  • What was hardest to get working, and how did you fix it?
  • Why is the for loop better than copying the side code four times?
  • How is this different from the polygon function in Module 1? (There you set distances and angles; here you count intersections and follow real lines.)

Nice work — Module 3 done 🎉​

You reused functions you wrote weeks ago on a brand-new surface, learned to count intersections, and sequenced drives and turns into real paths — and robot.py grew a GRID DRIVING section with one function and a record of every test it passed. Every route here was one you planned and hard-coded. That's about to change.

Resources​