Python Level-1 with Virtual Robotics - Mission 8: Boundary Patrol

Mission 8 Lesson Plan

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.

⏰ 2-4 hours 🎯 Grades 9-12+ πŸ’» CodeSpace πŸ€– Virtual CodeBot 🐍 Python
View Lesson Outline
πŸ“‹

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 val when a single sensor is read and vals when 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.py program
  • 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.
🧰

Classroom Materials

  • β–ΈComputer or Chromebook with internet access
🌍

Real-World Applications

πŸš—Many real-world applications have lines to mark boundaries. People stay in bounds when driving on roads, playing sports, and at places like school, warehouses and airports. Select a situation and describe an algorithm to help a person stay inside boundaries.
πŸ€–What are some physical devices that must stay in boundaries? How do they accomplish this?
πŸš€

Extensions & Cross-Curricular

ExtensionAdd sound to the moving 'bot.
ExtensionAdd user LEDs to indicate what the 'bot is doing, like moving forward, braking or turning.
ExtensionAdd an animation when a boundary is reached before turning and moving, or instead of turning and moving.
Lang ArtsHave students write a short poem about their sumo 'bot.
ScienceIn this mission the 'bot must brake and then turn. Discuss the physics involved.
ScienceTurn some of the objectives into experiments. Make a hypothesis and go through the scientific method to prove or disprove it.
MathUse different speeds for driving forward and different times for braking. Change just one value at a time and make a chart of the data. Then graph the data.
πŸ”€

Vocabulary

β–Ύ
Threshold:A value halfway between two distinct sensor readings, like a white surface and black line.
Default parameter:A parameter that uses a default value if an argument is not passed for it specifically.
Globals:Variables defined outside a function. They are available throughout the entire program.
Parameter:A named variable that is listed in a function definition; the variables receive values from arguments.
Argument:Values passed when you call a function; the values correspond to parameters.
Positional argument:An argument that must be passed to a parameter in the correct order; it is assigned to a parameter based on its position.
Keyword argument:An argument assigned to a parameter by name instead of position.
Logical operators:Operators used to compare multiple conditions. "And" and "or" are logical operators, and "not" is a special kind of logical operator. When "or" is used, at least one condition must be true for the compound condition to be true. When "and" is used, all conditions must be true for the compound condition to be true.
Docstring:A way to document functions using triple quotes at the beginning and end of the documentation comments.
🐍

New Python Code

β–Ύ
threshold = 2000 Define a threshold.
if val > threshold: break Use threshold in an if statement.
def go(left, right, delay=0) def back_turn(turn_power=50) Function definition with default parameter (delay and turn_power).
if delay: sleep(delay) Use a default parameter in an if statement (acts like a Boolean).
'''A function to simplify commands''' Docstring (commenting a function using triple quotes).
sensors = [] Define an empty list.
sensors.append(is_line) Append the item in parenthesis to a list (sensors).
leds.ls(vals) Use a Boolean list to control LEDs. In this example, vals is a list of Booleans. Example: vals = [True, True, False, False, False]
if any(vals): Use the any() function in an if statement. Returns True if any item in the list is True.
if vals[0] and not vals[4]: back_turn(30) elif vals[4] and not vals[0]: back_turn(-30) else: back_turn() If:elif:else statement for multiple branches. This example also uses compound conditions with logical operators, and a function call with a default parameter.
πŸ“

Standards

β–Ύ

CSTA Standards - Grades 9-10

3A-CS-03 3A-DA-10 3A-DA-12 3A-AP-13 3A-AP-14 3A-AP-15 3A-AP-16 3A-AP-17 3A-AP-18 3A-AP-21 3A-AP-23 3A-AP-26

CSTA Standards - Grades 11-12

3B-DA-05 3B-DA-06 3B-AP-10 3B-AP-12 3B-AP-14 3B-AP-15 3B-AP-16 3B-AP-17 3B-AP-20 3B-AP-21 3B-AP-23

Certiport IT Specialist: Python Standards

1.1 1.2 1.3 1.4 2.1 2.2 4.1 4.2 5.1

PCEP: Certified Entry-Level Python Programmer

1.1 1.2 1.3 1.4 1.5 2.1 2.2 3.1 4.1 4.2
πŸ“
Preparing for the Lesson
  • 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.

πŸ§‘β€πŸ«
Teacher Notes
  • 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

πŸ—£οΈWarm-up / Hook

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.

1️⃣Objectives 1-2: Reading Sensors and Setting a Threshold
  • Introduce printing sensor values to the console panel for real-time debugging.
  • Introduce ls.read() to get real-time line sensor values, and define a threshold to tell a black line apart from a white surface.
  • Administer the Quiz after Objective 2.
Teaching tip:Use the Mission 8 Obj 2 reference video if students get stuck reading and interpreting raw sensor values.
2️⃣Objective 3: Responding to the Boundary
  • 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) and back_turn(turn_power=50), and docstrings for documenting functions.
Teaching tip:Watch students' indentation when they call brake() - it should NOT be indented inside the while loop.
3️⃣Objective 4: Lists of Booleans and the Contact Counter
  • 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.
Teaching tip:This is a good place to slow down. Appending to a list and using a list of Booleans to control LEDs are both new skills - give students extra practice before moving on.
4️⃣Objectives 5+: Multiple Branching and Smart Decisions
  • 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.py program.
πŸŽ‰Wrap-up / Review

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.