Mission 6-3: Line Follower - PwR MS

Mission 6-3: Line Follower Lesson 3 | Python with Robots MS
Mission 6-3 Lesson Plan

Line Follower Lesson 3: Proportional Control & Calibration

Students will create a line follower program that uses all five sensors for proportional steering control.

โฐ 40-45 min ๐ŸŽฏ Grades 6-8 ๐Ÿ’ป CodeSpace ๐Ÿค– CodeBot ๐Ÿ Python
View Lesson Outline
๐Ÿ“‹

Overview

Mission 6-3 covers the final two objectives in the Line Follower mission. Students write a drive() function with parameters for left and right wheel speeds, then build a proportional line-following algorithm using tuples of sensor readings. In Objective 8, students add a calibration routine that reads sensor values to automatically set the detection threshold, and learn about global vs. local variables.

๐ŸŽฏ Mission Goal: Students will create a line follower program that uses all five sensors for proportional steering control.

๐ŸŽฏ

Learning Targets

  • I can define a function for driving at variable speeds.
  • I can represent a tuple of bools with 1s and 0s.
  • I can compare a variable to a tuple.
  • I can use tuples to create proportional speed control.
  • I can calibrate the 'bot by setting the thresh and is_reflective variables.
  • I can use the global keyword to keep variables global.
๐Ÿ’ก

Key Concepts

  • A function with parameters for left and right wheel speeds enables flexible, reusable drive control.
  • A tuple of Boolean values (1s and 0s) can represent which sensors detect the line at any moment.
  • Comparing sensor readings to a tuple allows for proportional steering - the 'bot adjusts speed based on how far off the line it is.
  • Calibration uses real sensor readings to set a threshold value, making the program adapt to different surfaces and lighting.
  • The global keyword allows a function to modify variables defined outside it.
โœ…

Assessment Opportunities

โ˜‘๏ธ

Success Criteria

  • Define the drive() function with two parameters
  • Use cases of detection for proportional speed control
  • Compare a variable to a tuple
  • Use pre-coded math functions abs() and round()
  • Define the calibrate() function
  • Call the calibrate() function when a button is pressed
๐Ÿ“ฑ

Classroom Materials

  • โ–ธCodeBot
  • โ–ธUSB cable
  • โ–ธ4 AA batteries
  • โ–ธA track for the 'bot to follow - white poster board with a black line made from electrical tape works well
๐ŸŒ

Real-World Applications

๐Ÿ”ŒCalibration is vital to many physical devices. Calibrated items are tools and instruments that measure physical or electrical properties to ensure accuracy against a known standard. Common examples include temperature sensors, pressure gauges, weighing scales, calipers, micrometers, torque wrenches, pH meters, and electrical multimeters.
๐Ÿš€

Extensions & Cross-Curricular

ExtensionAdd code for pressing BTN-1 to stop the program.
ExtensionAdd a sound when the 'bot turns left, another when it turns right.
๐Ÿ”ค

Vocabulary

โ–พ
Calibrate - Using sensor readings to determine the values of variables; adapting code to the environment using data.
Locals - Variables defined inside a function; they only exist while the function is running and can only be accessed in the function.
Globals - Variables defined outside of a function; they are available during the entire program and can be accessed throughout the entire program.
๐Ÿ

New Python Code

โ–พ
def drive(left, right):
ย ย ย motors.run(LEFT, left)
ย ย ย motors.run(RIGHT, right)
Define a function for driving the 'bot that uses parameters for the left and right wheel speeds.
if vals == (1, 0, 0, 0, 0):
ย ย ย drive(0, 30)
Use the tuple in a comparison. Call the drive() function, selecting left and right speeds as the arguments.
sensors = ls.check(0)Return a tuple of integers for the line sensor readings.
if abs(gap) < 500:Use the absolute value function.
is_reflective = line < groundUse a condition to set a Boolean value.
thresh = round(ground - (gap/2))Use the round function; the result is an integer.
global thresh, is_reflectiveUsed at the beginning of a function, the "global" keyword makes the variables global instead of local. The variables can be listed in any order.
elif buttons.was_pressed(1):
ย ย ย calibrate()
Call a function when a button is pressed.
๐Ÿ“

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-19
๐Ÿ“
Preparing for the Lesson
  • Decide what materials you want to use for presenting the lesson. The slides can be projected on a large screen.
  • Decide if you want your students to complete objective 8. You can move on to the remix and next unit without it, and your students would have more time to try different tracks and have a competition. Objective 8 does introduce new programming concepts. So look over the entire lesson and decide what you want to do.

๐Ÿง‘โ€๐Ÿซ
Teacher Notes
  • This lesson covers objectives 7 and 8. Look over objective 8 on calibration and decide if you want your students to do it. The program works fine without it. If you do cover obj. 8, it gets into global and local variables and built-in math functions. The quiz is also optional. You should skip it if you don't do objective 8.
  • The slides for objective 7 go into more detail about the tuples used, and give additional examples.
  • The CodeSpace instructions for objective 8 have a lot of reading. The slides simplify the instructions but still complete the code from CodeTrek.
  • The extensions are the same as the previous lesson. Students may think of their own extension for the program.
๐Ÿ—บ๏ธ

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

The warm-up questions review the new concepts and code from Lesson 2.

  • Question: What code reads the line sensors and turns on/off the LEDs?
  • Question: What data type (information) does ls.check() return?
๐Ÿ’ปMission 6 Lesson 3 Activities

The Chrome browser works best, but other browsers also support CodeSpace. Each student will complete a Mission Log. Students could work in pairs through the lesson, or they can work individually.

Teaching tip: Mission Introduction -- slides 3-5. This mission is divided up into three lessons. The third lesson focuses on the last goal.
Teaching tip: Objective #7 -- slides 6-10. Students start by reviewing their code from Objective 6, specifically the if/elif block. Only one sensor reading is being used at a time. This objective discusses using several sensor readings at the same time.
Teaching tip: Objective #7 Activity -- slides 11-12. The first part of the activity is to add a function for driving to the line follower.
Teaching tip: Objective #7 -- slides 13-15. The slides discuss using several sensor readings at the same time for proportional control. Examples are given with the tuples, and using 1s and 0s for Boolean values. Students practice with problems in the mission log.
Teaching tip: Objective #7 Activity -- slides 16-20. Students use the tuples for proportional control. They change the if/elif block and call the drive() function. The code segment on slide 18 is incomplete. Students need to include the code for the right turns as well. Sample speeds are given in the code segments. Students can use their own speeds.
Teaching tip: Objective #7 Try It -- slide 21. Students test their code. They should try several different tracks, and they can modify the speeds to see how fast the 'bot can go while staying on the line. They should take the time to modify their code as needed, and can even add more case detections.
Teaching tip: Objective #8 -- slides 22-24. The slides introduce the topic of calibration and why they want to include it in their code. There are several steps to this objective.
Teaching tip: Objective #8 Activity -- slides 25-32. The slides start the new function for calibration. Line sensors are read and the gap is calculated and then used in a condition. If there isn't enough gap, a low beep is sounded.
Teaching tip: Objective #8 Activity -- slides 33-38. The slides add to the function by calculating values for is_reflective and thresh. Also, a two-tone beep is added to indicate calibration.
Teaching tip: Objective #8 -- slides 39-42. Discussion on global and local variables.
Teaching tip: Objective #8 Activity -- slides 43-46. The slides complete the calibrate() function and add code to call the function with a button press. Students test their code. They should try several different tracks, and take the time to modify their code as needed. At the end of this objective, students should have a working program to turn in.
Teaching tip: Quiz -- slide 47. The three quiz questions review accessing a value in a tuple, the definition of a tuple, and using absolute value. Quiz questions are posted below.
Teaching tip: Extension. Two extensions are offered for this lesson, for students who have extra time or want to try their programming skills. They are the same extensions as lesson 2 and are completely optional.

Optional: ๐Ÿ—๏ธ Mission 6 Obj 7-8 Kahoot! Review. A review Kahoot! is available for these two objectives.

๐Ÿง‘โ€๐Ÿคโ€๐Ÿง‘Post-Mission Reflection

The post-mission reflection asks students to reflect on their programming experience. Both questions are good for small group or whole class discussion.

You can use an extension or cross-curricular activity as post-mission activity.

End by collecting the Mission 6 Lesson 3 Log.