Teacher Resources: Python with Robots MS Mission 7-2

Mission 7-2 Lesson Plan

Hot Pursuit Lesson 2: Auto-Calibration

Students write calibration functions so CodeBot can adapt its proximity sensor detection to any environment.

⏰ 45-50 min 🎯 Grades 6-8 πŸ’» CodeSpace πŸ₯‡ CodeBot 🐍 Python
View Lesson Outline
πŸ“‹

Overview

In this lesson, students build on the Hot Pursuit program by writing auto-calibration functions for the proximity sensors. They learn how to write a function that calibrates the detection sensitivity threshold and another that calibrates the power level, then test their code across different surfaces and record results. Students also explore the concept of global vs. local variables and use the global keyword when assigning values inside a function.

🎯 Mission Goal: Students can write calibration functions so CodeBot can adapt to its environment.

🎯

Learning Targets

  • I can write code that calibrates the detection sensitivity threshold.
  • I can write a function for the calibration.
  • I can write a function that calibrates the power level.
πŸ’‘

Key Concepts

  • Using auto-calibration functions for power and thresh allows CodeBot to adapt to a new environment.
  • An accepted programming convention is to group functions together, typically at the beginning of a program.
  • Print statements can take multiple arguments - it converts them to strings and prints them back-to-back to the console, with each argument separated by a comma.
βœ…

Assessment Opportunities

β˜‘οΈ

Success Criteria

  • Use an if statement to detect a button press
  • Use if statements to determine the best detection sensitivity threshold
  • Define a function to auto-calibrate thresh
  • Define a function to auto-calibrate power
  • Use the global command when assigning values to global variables inside a function
  • Use multiple arguments in a print statement
  • Test the program with multiple surfaces and record the results
🧰

Classroom Materials

  • β–ΈCodeBot and USB cable
  • β–Έ4 AA batteries
  • β–ΈDifferent testing surfaces (colors, textures, etc.)
  • β–ΈOptional: CodeBot Testing Surfaces
🌍

Real-World Applications

Many technical devices use auto-calibration to maintain accuracy.

πŸš—Adaptive cruise control on cars auto-calibrates to measure distances accurately and prevent ghost braking.
πŸ“±Smartphone accelerometers and gyroscopes automatically calibrate for orientation.
πŸ”¬In labs, automated pipettes perform self-calibration to ensure the volume of liquid matches the display.
πŸš€

Extensions & Cross-Curricular

ExtensionAfter Obj. 7, the code can still throw an error if the 'bot is not under a surface. Fix the bug by using an if statement.
Science/MathExperiment with the proximity sensor using different lighting, from very bright to pretty dark. See what range of IR light and sensitivity work best in different conditions. Make a chart of the power/thresh results.
MathThe proximity sensors can give a different reading even with the same surfaces and lighting. Run the auto-calibration sequence several times without moving the 'bot and record all the power/thresh values. Find the mean, median, and mode for each surface.
Lang ArtsSupports language arts through reading instructions, guided notes, and reflection writing.
πŸ”€

Vocabulary

β–Ύ
Calibrate - Using sensor readings to determine values of variables; adapting code to the environment using data.
Automate - Use technology, like a computer, to do a task automatically.
Default - A pre-selected option.
Globals - Variables defined outside of a function; they are available during the entire program and can be accessed throughout the entire program.
Locals - Variables defined inside a function; they only exist while the function is running and can only be accessed inside the function.
🐍

New Python Code

β–Ύ
if sensed[LEFT] > 0: det = sensed[LEFT] After reading the proximity sensors (sensed), check only the LEFT sensor. If it detected a reflection, assign its sensitivity value to a variable.
det = min(det, sensed[RIGHT]) The math function min() finds the lowest value of its arguments. This selects the lowest value of either the current det or the RIGHT sensor's sensitivity value.
if det > 100: thresh = 100 else: thresh = det - 5 Bug fix to make sure thresh is set to the correct value.
global thresh global power Keeps a global variable as global, even when it is assigned a value inside a function.
while power < 9: cal_thresh() if thresh < 100: break power = power + 1 Loop that cycles through the range of powers to determine the best power level for the proximity sensors.
print("Power=", power) Print statement with multiple arguments - each argument is converted to a string and printed back-to-back in the console.
πŸ“

Standards

β–Ύ

CSTA Standards - Grades 6-8

2-DA-08 2-DA-09 2-AP-10 2-AP-11 2-AP-12 2-AP-13 2-AP-14 2-AP-16 2-AP-17 2-AP-18 2-AP-19
πŸ“
Preparing for the Lesson
  • Look through the slides and decide what materials you want to use for presenting the lesson. The slides can be projected on a large screen.
  • Be familiar with the mission log assignment and the questions students will answer. Prepare the assignment to give through your LMS.
  • Have at least three colors/surfaces available for each student or programming pair. You can use the Testing Surfaces paper from Unit 3 as an option - any colors or surfaces will work.
  • If you have a word wall, or another form of vocabulary presentation, prepare the new terms.

πŸ§‘β€πŸ«
Teacher Notes
  • This lesson is all about auto-calibration, which was also introduced in Mission 6. If you skipped auto-calibration (Mission 6 Objective 8), all the information you need is included here. If your students completed Obj. 8, this will be review.
  • This lesson can run long if students do a lot of testing, or if calibration is a new topic. Consider running it over two class periods and including a cross-curricular activity or extension as well.
  • This lesson goes over global and local variables. If your students did Mission 6 Obj. 8, it will be review. Otherwise, take extra time here to make sure students understand the concept.
  • The code used during the lesson is slightly different from CodeTrek. All goals will be met.
  • Students will need different surfaces for testing. You can use the Testing Surfaces paper with black/white/gray options, or any color or surface.
  • There is a quiz after Objective 5. It doesn't really review the concepts from 4 and 5. You have the option of skipping the quiz, or going over it together as a class.
πŸ—ΊοΈ

Lesson Outline

πŸ’‘Lesson Tips and Tricks
Teaching tip: You can use a variety of discussion strategies to get the most engagement from your students. For example, you can have students write their answers before asking anyone for an answer. You can use one of many think-pair-share methods.
πŸ—£οΈWarm-up / Hook

Slide 2 - Students can write in their log first and then share, or discuss first and then write. The warm-up question reviews proximity sensors. Students can share answers or compare with each other.

  • Question: How is the proximity sensor similar to the line sensor?
πŸ’»Mission 7 Lesson 2 Activities

The Chrome browser works best, but other browsers also support CodeSpace. Each student will complete a Mission Log. Students can work in pairs or individually - there is a lot of testing in this mission, so it is a good one for pair programming.

Teaching tip - Mission Introduction (slides 3-6): This mission is divided into three lessons. This lesson completes the fourth goal. Students answer one question in their mission log.
Teaching tip - Objective 4 (slides 7-10): These slides introduce the topic of auto-calibration and give an example of how it should work.
Teaching tip - Objective 4 (slides 11-14): These slides go over the algorithm for the first auto-calibration and show the code that goes with each step. The last step includes printing to the console panel with a print statement that has multiple arguments. Students may have questions about this.
Teaching tip - Objective 4 Activity (slides 15-19): Students open their HotPursuit program and add code. The code is given on slide 17 - all of it except the if statement checking for a button press was shown on slides 11-14. Students need to open the console panel when running the code to see thresh values. They place the 'bot on several different surfaces and press BTN-1 each time they want a new reading. They don't need to stop running the code before changing the surface. They record results in the mission log.
Teaching tip - Objective 5 (slides 20-21): This objective fixes a bug in the code by adding another if statement.
Teaching tip - Objective 5 Activity (slides 22-23): Students modify their code by adding the if statement. Only one test is needed - with the 'bot pointed in the air or with no surface. Record the result in the mission log.
Teaching tip - Quiz (slide 24): Consider skipping the quiz or going over it together in class. It reviews concepts from Lesson 1, so it can be a good review.
Teaching tip - Objective 6 (slides 25-29): Students can organize their code by taking the block under the if statement and making it a function. Slides 27-29 cover global and local variables - if this is the first time doing a calibration function, take time to go over this carefully. If students did the calibration in Mission 6, this will be review and can be moved through more quickly.
Teaching tip - Objective 6 Activity (slides 31-34): Students define a function for calibrating thresh. The code is given on slides 32-33. There isn't specific testing for this activity - the code should work exactly the same as Obj. 5. The mission log questions review global and local variables.
Teaching tip - Objective 7 (slides 35-38): Discussion on defining another function for calibrating power. Slides 36-38 go over the algorithm for the second auto-calibration and show the code for each step.
Teaching tip - Objective 7 Activity (slides 39-42): Students add the second calibration function and perform testing. They need to open the console panel to see results, which they record in their mission log.
Teaching tip - Extension (slide 43): During testing for Obj. 7, students may point the 'bot in the air or have no surface underneath - this will throw a runtime error. The extension is to fix this error by adding another if statement. Code is provided.

πŸ—οΈ Optional: Mission 7 Obj. 4-7 Kahoot! Review - covers Objectives 4 through 7.

πŸ§‘β€πŸ€β€πŸ§‘Post-Mission Reflection

The post-mission reflection asks students to review what they learned during this lesson. Answers can vary widely depending on each student's experience.

You can use a cross-curricular activity for a post-mission activity.

End by collecting the Mission 7 Lesson 2 Log.