Skip to main content

robot.py templates

Reference · Modules 2 to 6

robot.py templates

One file for the courseTwo tracksPick up at any lesson

From Lesson 2-1 to the end of the course you keep one program file, robot.py. Every lesson hands you a skeleton to paste into it: a banner naming the section, a few comment lines saying what the section does, its settings in capitals, the outlines of the functions you are about to write, and that lesson's test functions. The file grows a section at a time, and by Module 5 the banner comments are its table of contents.

Two rules keep it working all year. New code goes above the RUN block, never below it — Python runs a file top to bottom, so the run block has to be the last thing in the file and everything it calls has to be defined above it. And the run block calls one test function at a time: change the call, don't stack them.

What a template is​

Each file below is robot.py as a student who has pasted every skeleton through that lesson, and written nothing yet, would have it: all the banners, settings and test functions, and every function outline ending in pass. None of the bodies you are asked to write are in it. If you join the course late, lost your file, or want a clean start for a lesson, download the file for the previous lesson, rename it robot.py, and go on from there. The end-of-module file is the same as the module's last lesson.

Two tracks, because the course has two. The function track is the course everyone does: each section is plain functions and settings. The class track is for students who take every optional extension — LineSensor (2-7), LineTrack (2-9), Manhattan (4-3), Navigator (4-6), BFS (5-5), the class harness (5-6) and Dijkstra (6-5). On that track a class skeleton replaces the function section, the object is created right under its class, and the tests call the object (tracker.track_to_intersection(), planner.compute_path(...)). Up to Lesson 2-6 the two files are identical. If you have built a class, stay on the class track; if you have not, use the function track.

Module 2 · Line Tracking​

After lessonFunction trackClass track
2-1 · The Reflectance Sensorrobot-after-2-1.pyrobot-after-2-1.py (identical)
2-2 · Drive to the Edge and Stoprobot-after-2-2.pyrobot-after-2-2.py (identical)
2-3 · Bounce Drivingrobot-after-2-3.pyrobot-after-2-3.py (identical)
2-4 · Random Turnsrobot-after-2-4.pyrobot-after-2-4.py (identical)
2-5 · Proportional Controlrobot-after-2-5.pyrobot-after-2-5.py (identical)
2-6 · Two-Sensor Line Followingrobot-after-2-6.pyrobot-after-2-6.py (identical)
2-7 · Finishing the Sensor Toolkitrobot-after-2-7.pyrobot-after-2-7.py
2-8 · Detecting Intersectionsrobot-after-2-8.pyrobot-after-2-8.py
2-9 · Line-Tracking Functionsrobot-after-2-9.pyrobot-after-2-9.py
2-10 · Final Projectrobot-after-2-10.pyrobot-after-2-10.py
End of Module 2robot-end-of-module-2.pyrobot-end-of-module-2.py

Module 3 · Grid Driving​

After lessonFunction trackClass track
3-1 · Introduction to the Gridrobot-after-3-1.pyrobot-after-3-1.py
3-2 · Driving Multiple Intersectionsrobot-after-3-2.pyrobot-after-3-2.py
3-3 · Turning on the Gridrobot-after-3-3.pyrobot-after-3-3.py
3-4 · Final Projectrobot-after-3-4.pyrobot-after-3-4.py
End of Module 3robot-end-of-module-3.pyrobot-end-of-module-3.py

Module 4 · Manhattan Navigation​

After lessonFunction trackClass track
4-1 · The Main Programrobot-after-4-1.pyrobot-after-4-1.py
4-2 · The Planner: A Path That Growsrobot-after-4-2.pyrobot-after-4-2.py
4-3 · The Planner: Every Directionrobot-after-4-3.pyrobot-after-4-3.py
4-4 · The Driverrobot-after-4-4.pyrobot-after-4-4.py
4-5 · Turningrobot-after-4-5.pyrobot-after-4-5.py
4-6 · On the Robotrobot-after-4-6.pyrobot-after-4-6.py
End of Module 4robot-end-of-module-4.pyrobot-end-of-module-4.py

Module 5 · Pathfinding Around Obstacles​

After lessonFunction trackClass track
5-2 · Dictionariesrobot-after-5-2.pyrobot-after-5-2.py
5-4 · The Pathfinding Functionsrobot-after-5-4.pyrobot-after-5-4.py
5-5 · Implementing compute_bfs_path()robot-after-5-5.pyrobot-after-5-5.py
5-6 · Testing and Swappingrobot-after-5-6.pyrobot-after-5-6.py
5-7 · Obstacle Detection with the Rangefinderrobot-after-5-7.pyrobot-after-5-7.py
5-8 · Building Experiencerobot-after-5-8.pyrobot-after-5-8.py
5-9 · Capstone Projectrobot-after-5-9.pyrobot-after-5-9.py
End of Module 5robot-end-of-module-5.pyrobot-end-of-module-5.py

Module 6 · Dijkstra's Algorithm (optional)​

After lessonFunction trackClass track
6-2 · Weights in the Graphrobot-after-6-2.pyrobot-after-6-2.py
6-3 · From BFS to Dijkstrarobot-after-6-3.pyrobot-after-6-3.py
6-4 · Drive Itrobot-after-6-4.pyrobot-after-6-4.py
6-5 · Three Planners, One Harnessno changerobot-after-6-5.py
End of Module 6robot-end-of-module-6.pyrobot-end-of-module-6.py