Mission 6-2: Line Follower - PwR MS

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

Line Follower Lesson 2: ls.check() and Logical Operators

Students will create a line follower that uses two sensors for turning and three sensors for moving forward.

⏰ 40-45 min 🎯 Grades 6-8 πŸ’» CodeSpace πŸ€– CodeBot 🐍 Python
View Lesson Outline
πŸ“‹

Overview

Mission 6-2 builds on the line-following code from Lesson 1. Students replace the check_lines() function with the faster botcore function ls.check(), learn about tuples, and use logical operators to build a more reliable line follower. By the end of the lesson, students have a working two-sensor turning / three-sensor forward-moving program.

🎯 Mission Goal: Students will create a line follower that uses two sensors for turning and three sensors for moving forward.

🎯

Learning Targets

  • I can call the ls.check() function to use the ADC hardware channel scanning feature.
  • I can use an item in a tuple as a condition.
  • I can use logic operators to increase the reliability of the 'bot.
πŸ’‘

Key Concepts

  • The line follower 'bot will need to continuously check for the presence of a line beneath all five sensors.
  • A list and a tuple are similar, but different. A list is mutable, or can change, and a tuple is immutable, or cannot be modified.
  • Pre-coded functions from the botcore library can use the ADC hardware, making your program even faster!
βœ…

Assessment Opportunities

β˜‘οΈ

Success Criteria

  • Call the ls.check() pre-coded function
  • Use an item from the vals tuple as a condition
  • Turn if an outside sensor detects a line
  • Use the logical operator "or" to check multiple conditions
  • Move forward if a line is detected
πŸ“š

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 great
🌎

Real-World Applications

πŸ€–Self-driving cars, autonomous flying drones, and other computing systems that navigate on their own share basic principles with CodeBot. Whether writing code for a vehicle with a high-powered vision processing system or for CodeBot's low-power sensors, you will face many of the same challenges to stay on the path. Robots that zip through warehouse distribution centers often follow lines as they pick up and pack items ordered online.
πŸš€

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, and a different sound (or silence) when no line is detected.
ScienceLook up the schematics for the CodeBot. Find the ADC and study the electronics chart to learn more about analog-to-digital conversion.
MathCompare line sensor readings for check_lines() and ls.check() using different shades of gray and surfaces. Create a table from the data, then make a graph.
Lang ArtsSupports language arts through reading instructions, guided notes, and reflection writing.
πŸ”€

Vocabulary

β–Ύ
Review
int - A value that is an integer; designated by int in Python; can be positive or negative.
float - A value that is a decimal, also known as a floating point; can be positive or negative.
New
Tuple - Read-only form of list.
Immutable - Unable to be changed.
Logical operators - And, or, and not - operators that allow for multiple conditions in a comparison. Logical operators handle combinations of Boolean results.
Or - Allows for multiple conditions where only one must be true for the entire comparison to be true.
🐍

New Python Code

β–Ύ
vals = ls.check(thresh, is_reflective)Botcore line sensor function - faster than check_lines(). thresh is the threshold for detecting a line. Pass False for a black line, True for a white line. Returns a tuple of bools. Example: vals = ls.check(2000, False)
ls.check(0)Returns the line sensor readings when entered directly in the Console Panel.
elif vals[1] or vals[2] or vals[3]:Uses the logical operator "or" for multiple conditions. If any one condition is true, the statement will evaluate to true.
πŸ“

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.
  • Be familiar with the mission log assignment and the questions they will answer. Prepare the assignment to give through your LMS.
  • Have the Test Surfaces paper available for each student or programming pair. They will also want a small black paper to act as a line. This can be a piece of black electrical tape on a piece of white paper.
  • Students will need paths for their 'bot to follow. You can make paths on your floor, or use white posterboard with black electrical tape for the line. You will want a variety of paths so the students can test their code.
  • If you have a word wall, or another form of vocabulary presentation, prepare the new terms.

πŸ§‘β€πŸ«
Teacher Notes
  • The examples given in the slides are more inline with variables and examples from Mission 5.
  • The code used in the slides is similar to CodeTrek, but a little different. It keeps consistent variables and sections of code. All goals will be met.
  • Slides go into a lot more detail about tuples, giving more examples.
  • Students will need lines for their 'bots to follow. You can get white posterboard from Dollar Tree and black electrical tape. Give the posterboard and tape to the students to make their own paths. You want a variety of paths for testing.
πŸ—ΊοΈ

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 lists and using bools to turn on/off LEDs. Students can share their answers, or compare with each other.

  • Question: Complete the tables for lists and LEDs.
πŸ’»Mission 6 Lesson 2 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 second lesson focuses on the first two goals.
Teaching tip: Data Type Review -- slide 6. The slide gives a brief review of data types and refers to the mission log for practice. Do any extra review as needed.
Teaching tip: Objective #4 -- slide 7. Students start by reviewing their code from Objective 3, specifically the function check_lines().
Teaching tip: Objective #4 -- slides 8-11. The new pre-coded function ls.check() is introduced. It will replace the check_lines() function moving forward.
Teaching tip: Objective #4 -- slides 12-14. These slides discuss the data type tuple. It is similar to a list, but read-only. Examples are given, which are not included in the CodeSpace instructions.
Teaching tip: Objective #4 Activity -- slides 15-19. The first part of this activity is to use ls.check() in the Console Panel to see the results. Students will do this two different ways: ls.check(0) and ls.check(). They record the results in their mission log. Students will use the Test Surfaces document and a small black piece of paper. The second part of the activity is to start a new file called "LineFollow". The code on the slide is basically the same as CodeTrek. Test the program with the 'bot on the white rectangle and use the small black piece of paper. The LEDs should turn on when the black paper is underneath the line sensors.
Teaching tip: Objective #5 -- slides 20-21. This objective discusses adding movement to the code. The algorithm is given for the program.
Teaching tip: Objective #5 Activity -- slides 22-27. The code on the slides is slightly different from CodeTrek. It keeps the current code and its organization. Students add a constant for the speed. Then they add the safety feature of a while loop waiting for the button press. Then they enable the motors and add an if statement to the program. This is a good time for testing. Students start the 'bot on their own path. Then they can try other students' paths. They can also vary the speed and see what happens. They will record their observations in their mission log.
Teaching tip: Objective #6 -- slides 28-35. This objective discusses a way to improve the program. Currently the 'bot can get off track fairly easily and remain off track. Students will modify the else portion of the block for better accuracy. Logical operators are discussed as a new concept. Truth tables are displayed for "and" and "or". You might want to take some time to go over these.
Teaching tip: Objective #6 Activity -- slides 36-38. This is a very small change to the program code. Students can then test their code again, similar to Objective 5, using several different paths and speeds. They will again record their observations in their mission log. At the end of this objective, students should have a working program to turn in.
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 completely optional.

πŸ—οΈ Optional: Mission 6 Obj 4-6 Kahoot! Review. A review Kahoot! is available for these three objectives.

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

The post-mission reflection asks students to review the new ls.check() function. This question is a good one 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 2 Log.