Python Level-1 with Virtual Robotics - Mission 8: Boundary Patrol
Boundary Patrol
Students use sensor inputs to program the 'bot to roam a fenced area, staying inside the lines using line sensors, thresholds and smart branching.
Overview
In this mission, students move from single-value sensor readings to reading and reacting to multiple line sensors at once. They define a threshold to tell a black line apart from a white surface, build a list of Boolean values to track every sensor at the same time, and use that list to control the 'bot's user LEDs as a live contact counter. From there, students write functions with default parameters and use if:elif:else branching with compound conditions to make the 'bot smart enough to steer itself back inside a fenced boundary.
π― Mission Goal: Students use sensor inputs to program the 'bot to roam a fenced area.
Learning Targets
- I can print values in the console panel while debugging to get real-time sensor values.
-
I can use
ls.read()to get real-time line sensor values. - I can use a list of Boolean values to indicate if a threshold has been reached for each sensor.
- I can define a function that returns a list of Boolean values.
- I can teach the 'bot to stay inside the lines.
- I can make a contact counter to show each line-detect on the user LEDs.
- I can use default parameters and multiple branching to make smarter decisions.
Key Concepts
- A threshold is a value in between two different sensor readings.
- Default parameters give functions flexibility.
- A list can be defined as empty, and new items can be appended to the end of the list.
-
A list name is usually plural, since it can hold multiple items. Note the difference between
valwhen a single sensor is read andvalswhen all sensors are read and stored in a list. - A list can hold Boolean items. Each item can indicate if a line sensor is above or below a threshold (True or False).
- A list of Boolean items can control LEDs. An if:elif:else statement gives multiple branches.
Assessment Opportunities
- Quiz after Objective 2
- Quiz after Objective 4
- Submit program code, or give students printed code and have them explain each line: Objective 2, Objective 3, Objective 4
- Give extra practice with lists
- Give extra practice with if:elif:else statements
- Give extra practice with logical operators
- Code Tracing Chart as a debugging tool
-
Submit final
boundary.pyprogram - Level-1 Mission 8 Review Kahoot!
Success Criteria
- Use a threshold to determine if the 'bot has reached a black line.
- Have the 'bot respond to a black line by moving in a different direction.
- Use a list to store Boolean values for each sensor reading.
- Use default parameters in a function.
- Use multiple branching and compound conditions to have the 'bot make smart decisions.
Digital Resources
Classroom Materials
- βΈComputer or Chromebook with internet access
Real-World Applications
Extensions & Cross-Curricular
π€
Vocabulary
βΎ
π
New Python Code
βΎ
Append the item in parenthesis to a list (sensors).
vals is a list of Booleans. Example: vals = [True, True, False, False, False]
any() function in an if statement. Returns True if any item in the list is True.
π
Standards
βΎ
CSTA Standards - Grades 9-10
CSTA Standards - Grades 11-12
Certiport IT Specialist: Python Standards
PCEP: Certified Entry-Level Python Programmer
- This mission introduces several new programming concepts that build on earlier missions: appending items to an empty list, using a list of Booleans to control LEDs, using logical operators to form compound conditions, using an if:elif:else statement for multiple branching, and default parameters. Plan extra time and practice for these.
- There's a lot of new information in this lesson and the objectives can move quickly. Take your time with each objective and review the concepts frequently rather than rushing through in one sitting.
- Preview the Code Tracing Chart before class - it works well as a debugging tool and for walking through code together as a class, particularly in the later objectives.
-
Watch the indenting when calling the
brake()function in Objective 3. It is NOT indented in the while loop. - The Debugger can be used to track variables in real time. You can also use the Code Tracing Chart and have the class go through some code together.
Lesson Outline
Ask students where they see lines used as boundaries in everyday life - roads, sports fields, sidewalks, warehouse floors, airport runways. Have a few students describe an algorithm a person or vehicle could use to stay inside a boundary. Connect this to what the 'bot will do in this mission: use its line sensors to detect a black boundary line and steer itself back inside.
- Introduce printing sensor values to the console panel for real-time debugging.
- Introduce
ls.read()to get real-time line sensor values, and define athresholdto tell a black line apart from a white surface. - Administer the Quiz after Objective 2.
- Have the 'bot respond to a black line by braking and turning to move in a different direction.
- Introduce default parameters with
go(left, right, delay=0)andback_turn(turn_power=50), and docstrings for documenting functions.
brake() - it should NOT be indented inside the while loop.- Define an empty list and append Boolean values as each sensor is checked against the threshold.
- Use the Boolean list to drive the user LEDs as a contact counter with
leds.ls(vals). - Administer the Quiz after Objective 4.
- Introduce
any()and if:elif:else statements for multiple branching. - Combine compound conditions with logical operators (
and,or,not) to make the 'bot choose the correct turn direction. - Have students submit their final
boundary.pyprogram.
Close with the Level-1 Mission 8 Review Kahoot! to reinforce thresholds, lists of Booleans, default parameters, and multiple branching before moving on to the next mission.