VR - Mission 13: Going the Distance!

Mission 13 Lesson Plan

Going the Distance!

Students use wheel encoders to calculate distance and speed, then write functions to drive CodeBot an exact distance, turn a precise angle, and maintain a set speed.

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

Overview

In this mission, students get to know CodeBot's wheel encoders: discs with slots that let an IR beam pulse through as the wheel turns. Students count those pulses to calculate distance and speed, then put the math to work, writing a drive() function that moves CodeBot an exact distance, a rotate() function that turns it a precise angle, a speed function with "cruise control" to hold a steady pace, and finally an arc() function that reads both encoders at once to drive a curve at a given speed.

🎯 Mission Goal: Students will use wheel encoders to make CodeBot move exact distances and angles.

🎯

Learning Targets

  • I can get to know the wheel encoders.
  • I can calculate speed in cm/sec.
  • I can create a function to move CodeBot an exact distance using the wheel encoders.
  • I can write a function to rotate CodeBot using the wheel encoders.
  • I can write a function that calculates the speed of the 'bot from wheel encoder data.
  • I can write a function that enables the 'bot to maintain a speed.
  • I can write a function that reads both wheel encoders and uses the data to move in an arc at a given speed.
πŸ’‘

Key Concepts

  • Each wheel encoder consists of a disc with slots that allow IR light to pass through. Counting the light-dark and dark-light intervals tracks distance exactly.
  • Knowing the distance lets the 'bot move precisely a given distance at a given speed.
  • The encoders can also be used to calculate a turning angle.
  • Using lists for the data helps manage the information for both wheels.
  • The time library has built-in functions that can be used to track time.
  • Data from the wheel encoders can be used to maintain a given speed.
βœ…

Assessment Opportunities

  • Quiz after Objective 3
  • Quiz after Objective 6
  • Quiz after Objective 10
  • Code Tracing Chart as a debugging tool
  • Use the Debugger to track variables and lists
  • Give students code snippets from an objective and have them explain what it does
  • Give extra practice with math calculations for distance
  • Submit the encoder_check.py program
  • Submit the enc_drive.py program
  • Submit final enc_speed.py program
  • Mission 13 Review Kahoot!
β˜‘οΈ

Success Criteria

  • Write code to create a bar chart visualization for wheel encoder data.
  • Define a drive() function to move CodeBot an exact distance, based on the counts of the wheel encoder.
  • Define a rotate() function that turns the 'bot a specified angle, based on wheel encoder data.
  • Calculate the speed of CodeBot and display the data in a bar chart visualization.
  • Write "cruise control" code to maintain a set speed over any terrain.
  • Define an arc() function to drive the 'bot around a free throw ring.
🧰

Classroom Materials

  • β–ΈComputer or Chromebook with internet access
🌍

Real-World Applications

πŸŽ›οΈRotary encoders are used in any device with a "knob," like vehicles and appliances. A car odometer and speedometer use these same calculations to display distance traveled and speed. A 3D printer's control system uses a control loop to measure distance and speed to ensure a good print.
πŸš—Cruise control! In this mission you learned to have the 'bot automatically maintain a speed. The same sort of feedback loops are used to control motors, heaters, and other systems you use daily in the real world.
πŸš€

Extensions & Cross-Curricular

ExtensionUse the drive or the speed program on a physical CodeBot to run a track or maze.
ExtensionAdd LEDs and sounds to indicate CodeBot's progress. Use LEDs or beeps to indicate speed or a turn.
Lang ArtsHave students write a short description of the 'bot and its wheel encoder capabilities for an online shopping store.
ScienceEncoders use IR light. Have a lesson about light: how it is emitted, reflected, and detected.
MathThe lesson uses the geometry of circles. Have a lesson or activities involving circles, diameters, and circumference.
MathThe lesson calculates a turn ratio. Discuss ratios, or calculate the distance traveled by the 'bot while it's turning, or the angle turned given a ratio.
πŸ”€

Vocabulary

β–Ύ
Wheel encoders:A disc with slots that rotates with a wheel so that an IR light beam can pass through its slots. The pulses of light can be counted to see how much the wheel has rotated.
Integer division:The // operator, which gives the quotient.
Wheel track:The circular path wheels take when the 'bot rotates in place: distance = circumference x (angle / 360). Use a positive left / negative right wheel power to turn clockwise, and a negative left / positive right wheel power to turn counterclockwise.
Speed:Distance / Time (can be any measure; we will use cm/second and also counts/second).
Closed loop control:Automates control of a system by sensing the output state and comparing it to the desired state (input).
Feedback loop:Continuously adjusts the system to keep the error, or difference between input and output, close to zero. In this mission, feedback comes from the encoders, the input is the desired speed, and the output is the actual speed. Disturbance can be friction, surface type, etc.
🐍

New Python Code

β–Ύ
val = enc.read(LEFT) Reads the encoder's analog value (can be LEFT or RIGHT).
if enc_state != slot: The != is the "not equals" comparison operator.
n = val // 100 Integer division (// operator).
print(val, n * "*") Create a repeated character string. In this example, "n" number of "*" is printed.
val = enc.read(LEFT) is_slot = val > SLOT_THRESH Use a Boolean to indicate a slot (True or False).
WHEEL_CIRC_CM = math.pi*WHEEL_DIAM_CM Calculate the wheel circumference.
counts += 1 Augmented assignment to update (increment) counts.
cm * (COUNTS_PER_REV / WHEEL_CIRC_CM) Calculate the counts needed for a given distance.
direction = deg / abs(deg) Get a positive or negative direction (-1 or +1).
t_poll = ticks_ms() + POLL_MS Current time-tick count in milliseconds (ticks_ms starts at 0).
print(f'speed = {speed:.1f} cm/s') F-string formatting with one decimal place for a float.
counts * WHEEL_CIRC_CM / COUNTS_PER_REV Calculate distance for the given counts.
err = target_speed - cur_speed power += err * Kp motors.run(LEFT, power) motors.run(RIGHT, power) Feedback loop.
πŸ“

Standards

β–Ύ

CSTA Standards - Grades 9-10

3A-CS-03 3A-DA-11 3A-DA-12 3A-AP-13 3A-AP-14 3A-AP-16 3A-AP-17 3A-AP-18 3A-AP-21 3A-AP-22 3A-AP-26

CSTA Standards - Grades 11-12

3B-CS-02 3B-DA-05 3B-DA-06 3B-DA-07 3B-AP-10 3B-AP-12 3B-AP-14 3B-AP-15 3B-AP-16 3B-AP-17 3B-AP-21 3B-AP-22 3B-AP-23

Certiport IT Specialist: Python Standards

1.1 1.2 1.3 1.4 2.1 2.2 3.2 4.1 4.2 5.1 6.2

PCEP: Certified Entry-Level Python Programmer

1.1 1.2 1.3 1.4 1.5 2.1 2.2 3.4 4.1 4.2 4.3
πŸ“
Preparing for the Lesson
  • This mission is math-heavy: wheel circumference, counts-per-revolution, and distance/angle/speed calculations. Review integer division and consider working a few example problems on paper before starting.
  • Encoder behavior varies 'bot to 'bot and track to track, so the distance, angle, and speed values in CodeTrek are starting points. Expect some trial and error to hit the goals, especially for Objectives 8, 11, and 12.
  • Preview the objective walkthrough videos (Obj 2, 4, 5, 6, 8, and 10) so you can point students to the right one when they get stuck.
  • Plan checkpoints for the three program submissions: encoder_check.py, enc_drive.py, and the final enc_speed.py.

πŸ§‘β€πŸ«
Teacher Notes
  • For Objective 8, the number given in CodeTrek for the distance is a starting point. It will need to be changed to meet the goal.
  • For Objective 11, remember to change the arguments for drive_speed(). The values given in CodeTrek will not meet the goal.
  • For Objective 12, the global designation is only needed in the init_drive_state() function. Lists are global and can be updated anywhere. The function needs the global designation because the lists originate there and cannot be local.
  • You may need to adjust values to meet the goal. Slower speeds work better.
  • There is a lot of information given in this lesson. Take your time with each objective and review the concepts frequently.
  • There is a lot of math in this lesson. Review important concepts as needed, and maybe even do some practice problems on paper.
πŸ—ΊοΈ

Lesson Outline

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

Ask students how a car's odometer or speedometer "knows" how far or how fast it's traveled without measuring the road directly. Introduce wheel encoders as the sensor that makes this possible, and connect it to the mission: CodeBot has to use its own wheel encoders to go the exact distance, and at the exact speed, it's told.

1️⃣Objectives 1-3: Getting to Know the Wheel Encoders
  • Read raw encoder values, use a Boolean threshold to detect a slot, and build a bar chart visualization of the encoder counts.
  • Reference Mission 13 Obj 2 as needed.
  • Have students submit their encoder_check.py program as an early check that the encoders are working.
  • Administer the Quiz after Objective 3.
Teaching tip: Use the Code Tracing Chart here, or give students printed code from an objective and have them explain what it does.
2️⃣Objectives 4-6: Calculating Distance and Speed
  • Calculate the wheel circumference, convert counts to distance (and distance to counts), and calculate speed in cm/sec.
  • Reference Mission 13 Obj 4, Obj 5, and Obj 6 as needed.
  • Administer the Quiz after Objective 6.
Teaching tip: Give extra practice with the math calculations for distance before moving on - there's a lot of math in this lesson.
3️⃣Objectives 7-8: Driving an Exact Distance
  • Define a drive() function that moves CodeBot an exact distance, based on wheel encoder counts.
  • Reference Mission 13 Obj 8 as needed.
Teaching tip: The starting distance value given in CodeTrek for Objective 8 is just a starting point - students will need to adjust it to meet the goal.
4️⃣Objectives 9-10: Rotating a Precise Angle
  • Define a rotate() function that turns CodeBot a specified angle, using wheel encoder data and a positive/negative direction.
  • Reference Mission 13 Obj 10 as needed.
  • Have students submit their enc_drive.py program, combining drive() and rotate().
  • Administer the Quiz after Objective 10.
Teaching tip: Use the Debugger to track variables and lists as students build these functions.
5️⃣Objectives 11-12: Cruise Control and Driving an Arc
  • Write a "cruise control" function using a feedback loop to maintain a set speed over any terrain.
  • Define an arc() function that reads both wheel encoders and uses the data to drive the 'bot around a free throw ring at a given speed.
  • Have students submit their final enc_speed.py program.
Teaching tip: For Objective 11, remind students to change the arguments for drive_speed() - the CodeTrek values won't meet the goal as given. For Objective 12, the global designation is only needed inside init_drive_state(), since that's where the lists originate. Slower speeds tend to work better.
πŸŽ‰Wrap-up / Review

Close with the Level-1 Mission 13 Review Kahoot! to reinforce wheel encoder distance, angle, and speed calculations before moving on to the Unit 5 Remix & Assessment.