Python Level-1 with Virtual Robotics - Mission 7: Line Sensors
Line Sensors
Students use sensor inputs to program the 'bot to navigate around lines.
Overview
Students use the CodeBot's line sensors to detect brightness and turn those readings into decisions. They read and print sensor values, use a threshold comparison operator to check whether a reading falls within a target range, and use abs() to compare two readings no matter which one is higher. From there, students bring together a constant, a matrix of compass directions and brightness readings, and a function that returns a value, so the CodeBot can find and respond to a specific color.
π― Mission Goal: Students use sensor inputs to program the 'bot to navigate around lines.
Learning Targets
- I can read a line sensor and print the value.
- I can print multiple numbers separated with a comma.
- I can use threshold comparison operators to make decisions with sensor data.
- I can break out of a loop.
-
I can use the built-in
abs()function. - I can define and use a constant.
- I can create a 2-dimensional array.
- I can define a function that returns a value.
- I can program the CodeBot to respond to a specified color.
Key Concepts
- The data from an analog sensor needs to be changed to digital numbers for computing.
- The CodeBot has five line sensors, and each one gives its own brightness reading.
- Python has many built-in functions for math. Absolute value is one of them.
- A threshold comparison operator lets you check if a value is within a range.
-
A user can input data using an
input()function and typing an answer on the console. - A matrix, or 2-dimensional array, can be used to store two pieces of data that are related, like a compass direction and brightness reading.
- A function can return a value to a variable.
Assessment Opportunities
- Quiz after Objective 1
- Quiz after Objective 3
- Quiz after Objective 6
- Obj. 2 Line Sensor Readings Chart (in assignment)
- Submit program code, or give students printed code and have them explain each line: Objective 3, Objective 5
- Give extra practice with functions that return
- Give extra practice with 2-dimensional arrays
- Code Tracing Chart as a debugging tool
-
Submit final
line_sense.pyprogram - Level-1 Mission 7 Review Kahoot!
Success Criteria
- Create a color detection program using line sensors that detect brightness.
- Display data on the console.
-
Use
abs()in a comparison. - Break out of a loop if a condition is met.
- Define and use a constant.
- Define and use a matrix.
- Get input from the user on the console.
- Program the CodeBot to respond to a specific brightness level (color).
Digital Resources
Classroom Materials
- βΈComputer or Chromebook with internet access
Real-World Applications
From robot housekeepers to self-driving cars, the sensing and control techniques used in this mission apply to all kinds of intelligent systems. Analog sensors are non-contact sensors used in many industrial and commercial applications - code like this impacts your life every day!
Extensions & Cross-Curricular
abs() function? What are some alternative solutions to getting a positive answer every time?
π€
Vocabulary
βΎ
int in Python; can be positive or negative.
π
New Python Code
βΎ
π
Standards
βΎ
CSTA Standards - Grades 9-10
CSTA Standards - Grades 11-12
Certiport IT Specialist: Python Standards
PCEP: Certified Entry-Level Python Programmer
- The Level-1 Mission 7 Assignment includes a chart for recording line sensor readings at each compass point, used in Objective 2 - have it ready to hand out.
-
Objective 6 requires importing the
sleep()function fromtime- remind students before they start. - For Objective 6, students will zoom out in the simulator to find the water bottle, then open the console log and type the correct compass direction, using a capital letter, when the program runs. Walk through this once yourself first.
- During Objective 4, an if statement is added to the code. The existing if statement gets nested inside the new one, so make sure students are indenting properly.
- A lot of code is added to Objective 6, and a lot is deleted. Students need to be very careful about their indenting, parentheses, typing, and spelling.
- A lot of new concepts are introduced very quickly in this mission. Take time to review as needed, and give extra practice if students need it.
Lesson Outline
Have students write or discuss their answers before revealing them.
- Question: What is a line sensor, and how could a robot use one to tell it has found a colored line or boundary?
- Question: Why might a program check whether a number falls within a range, instead of checking for one exact value?
Students read a line sensor and print its brightness value, seeing how the CodeBot's analog to digital converter (ADC) turns light readings into numbers a program can use.
Give the quiz after Objective 1.
Students print multiple sensor readings on the same line separated by commas, then use a threshold comparison operator to check whether a reading falls within a set range. Have students record their readings in the Objective 2 chart from the assignment.
Students add a break statement so the program exits the loop once a compass direction is found, then use the built-in abs() function to compare two sensor readings regardless of which one is higher, using MIN_DIFF as a constant threshold.
Have students submit their code, or hand them printed code to explain each line.
A lot of new concepts come together here: defining a constant, building a matrix (a 2-dimensional array) of compass directions and readings, writing a function that returns a value, and using input() to get a direction from the console. Together these let the CodeBot respond to a specific color.
Give the quiz after Objective 6.
Students combine reading the sensors, checking thresholds, breaking out of the loop, and the matrix and function logic into one complete program that finds and responds to a compass direction.
Have students submit their final "line_sense.py" program.
Use an extension or cross-curricular activity as a post-mission activity. New concepts were introduced quickly in this mission, so build in review time or extra practice with functions that return and with matrices as needed.
Wrap up with the Level-1 Mission 7 Review Kahoot!, which covers all Mission 7 objectives.