Python Level-1 with Virtual Robotics - Mission 9: Line Following
Line Following
Students use sensor inputs to program the 'bot to follow a line course, building toward a PID controller that continuously calculates and corrects for error.
Overview
In this mission, students move from basic line sensing to building a full PID controller that keeps CodeBot precisely on a line course. They use the Debugger and REPL to inspect global and local variables in real time, learn to define and use dictionaries with the get() method to avoid KeyErrors, and combine these skills to define a PID controller function that continuously calculates an error value. Students also write a function to collect statistics on that error, tuning constants until CodeBot can complete a complex path in under five minutes.
π― Mission Goal: Students use sensor inputs to program the 'bot to follow a line course.
Learning Targets
- I can use the Debugger to track global and local variables.
- I can test code using REPL.
- I can define and use a dictionary.
-
I can avoid KeyErrors by using
get(). - I can define a function for a PID controller that continuously calculates the error value.
- I can define a function to collect stats on the error values.
- I can write code that makes the 'bot follow a complex path in under five minutes.
Key Concepts
- The REPL is a way to experiment with code.
- A tuple is a read-only list and is defined with parentheses instead of square brackets.
- A dictionary allows you to look up a value using a key, like a label.
- A default value can be returned if a key is not in the dictionary to avoid KeyError.
- A control loop mechanism that employs feedback by continuously calculating the error value is called a PID controller.
Assessment Opportunities
- Quiz after Objective 2
- Quiz after Objective 4
- Quiz after Objective 6
- Quiz after Objective 8
- Objective 5 Line Sensor Chart (in assignment)
- Code Tracing Chart as a debugging tool
- Give students printed code from an objective and have them explain each line
- Give extra practice with dictionaries
- Compare and contrast global / local variables
-
Submit the
line_scanner.pyprogram -
Submit the
bang_bang.pyprogram -
Submit final
line_follower.pyprogram - Level-1 Mission 9 Review Kahoot!
Success Criteria
- Use previous knowledge of the motors and line sensing to make CodeBot follow a path.
- Use the Debugger and REPL to track variables and test code.
- Define and use a dictionary without errors.
- Define functions that create a PID controller.
- CodeBot follows a complex path without veering off course.
- CodeBot stays on the white board for the entire course.
- CodeBot finishes the course within the time limit.
Digital Resources
Classroom Materials
- βΈComputer or Chromebook with internet access
Real-World Applications
Extensions & Cross-Curricular
π€
Vocabulary
βΎ
π
New Python Code
βΎ
!= is the "not equals" operator.
key : value. This example uses a tuple as the key and an integer as the value.
vals).
get() method to avoid a KeyError.
π
Standards
βΎ
CSTA Standards - Grades 9-10
CSTA Standards - Grades 11-12
Certiport IT Specialist: Python Standards
PCEP: Certified Entry-Level Python Programmer
- When using the Debugger, expand the list for Globals and Locals by clicking on the > to see them. You can also expand the window panel.
- You can use the Code Tracing Chart and have the class go through some code together - Obj 1 and/or Obj 4 are good choices.
- A chart for Objective 5 Line Sensor Readings is included as part of the assignment.
- Some new programming concepts are used in the later objectives - dictionaries, and global and local variables. Students may need extra help and/or practice with these.
- Objective 3 still works on Obj 4 without making the changes, but this is a good place for experimenting. Have students try making the speed faster when a line is detected (last elif branch) and see what happens.
- For the final objective, students need to tweak the speed and/or other constants. Encourage students to change one variable at a time and keep track of the changes so they can return to the previous value if needed - small changes can make a big difference.
Lesson Outline
Ask students where they've seen a machine or vehicle "follow a path" on its own - self-driving cars, warehouse robots, robotic vacuums, competition robots racing a track. Have a few students guess how a robot might sense where the line is and decide which way to turn. Connect this to the mission: CodeBot will use its line sensors, a dictionary lookup, and eventually a PID controller to stay on a line course.
- Introduce the Debugger to track global and local variables, and use the REPL to experiment with code directly.
- Introduce list comprehension as a shortcut for building a list, and use it to check all line sensors against a threshold at once.
- Administer the Quiz after Objective 2.
- Have students submit their
line_scanner.pyprogram.
- Use
ls.check(2000)to return a Boolean list from all line sensors, and compare readings with!=to detect a change in state. - Combine logical operators in a compound condition (
or) to react to multiple sensors at once. - Administer the Quiz after Objective 4.
- Have students submit their
bang_bang.pyprogram.
- Introduce tuples as read-only lists, and use one as a dictionary key:
ls_err = {(0,0,1,0,0): 0, ...}. - Look up an error value from the dictionary with
ls_err[vals], then useget()instead to return a default value and avoid a KeyError. - Have students complete the Objective 5 Line Sensor Chart in the assignment.
- Administer the Quiz after Objective 6.
- Define
apply_control(err), a PID controller function that continuously calculates the error value and steers the 'bot. - Define a function to collect stats on the error values, then tune constants like speed and turning ratio one at a time.
- Administer the Quiz after Objective 8.
- Have students submit their final
line_follower.pyprogram, aiming to complete the course in under five minutes.
Close with the Level-1 Mission 9 Review Kahoot! to reinforce the Debugger, REPL, dictionaries, get(), and PID controllers before wrapping up the unit.