Fly with Python - Mission 6: Navigate

Mission 6 Lesson Plan

Navigate

Students program CodeAIR to fly autonomously - using the flow sensor for position tracking, sensor fusion for accuracy, binary LED patterns for a custom UI, and try/except blocks to handle errors gracefully.

⏱ 120-180 min 🎯 Grades 6-12+ 💻 CodeSpace 🚁 CodeAIR 🐍 Python
View Lesson Outline
📋

Overview

Mission 6 wraps up Unit 2 by giving CodeAIR full autonomous navigation. Students explore the flow sensor for x/y position tracking, fly precise square routes to test accuracy, and learn how multiple sensors combine for stable flight. Along the way they pick up f-strings for clean console output, binary patterns for a custom LED user interface, and try / except blocks for graceful error handling. The mission culminates in RouteSelect, a selectable-operations program that lets the user choose a flight pattern at runtime.

🎯 Mission Goal: Students will program CodeAIR to fly autonomously using sensor data.

🎯

Learning Targets

  • I can explore positioning systems with the flow sensor for x, y tracking.
  • I can observe and analyze flow sensor accuracy by flying CodeAIR in a square.
  • I can conduct a battery check to ensure safe and sustained flight.
  • I can customize selectable operations to control code behavior during runtime.
  • I can experiment with flight parameters including height, distance, and velocity.
💡

Key Concepts

  • The flow sensor is an optical device that can discern patterns on the ground and report movement in two directions.
  • The sensor tracks motion by summing up the changes in x and y over time.
  • You can use an f-string to determine how information is printed.
  • Data from multiple sensors is needed for accurate flight control.
  • CodeAIR's battery is charging whenever it is plugged in.
  • The 8 LEDs can use binary patterns to display integers between 0 and 255.
  • Handle exceptions in code using a try block.

Assessment Opportunities

  • Quiz after Objective 2
  • Quiz after Objective 4
  • Quiz after Objective 6
  • Complete the programs FlowTracker, SquareUp, SquareTurns, BattTest, utility.py, and RouteSelect
  • Mission 6 Assignment
  • Mission 6 Review Questions
☑️

Success Criteria

  • Use flow sensor data to track position
  • Fly CodeAIR in a square
  • Perform a battery check
  • Create a user interface using binary patterns
  • Handle exceptions using a try block
  • RouteSelect works correctly and runs without errors or bugs
  • Complete Mission 6 Assignment
🧰

Classroom Materials

  • Laptop/computer with Chrome browser
  • CodeAIR drone and USB cable
  • Metric tape measure or ruler (for measuring flight accuracy)
  • Open floor space with even lighting and a slight ground pattern (helps the flow sensor)
🌍

Real-World Applications

🛰️Optical flow sensors - the same kind students use here - power professional drones, autonomous robots, and even computer mice. They are how machines "see" their own motion without GPS.
🔀Sensor fusion is a foundational concept in self-driving cars, augmented reality headsets, and aerospace systems - combining data from multiple sensors gives a more accurate picture than any single sensor alone.
🔢Binary numbers underpin every digital system. The same 8-bit pattern students display on the LEDs is how a CPU stores a single byte of data.
🛠️Exception handling with try / except is required in any production software - banks, hospitals, and aviation systems all rely on graceful error handling so a single bug doesn't crash the entire system.
🚀

Extensions & Cross-Curricular

ExtensionFly the drone in a different shape: triangle, hexagon, etc.
ExtensionCreate a more elaborate battery check that shows the charging amp when plugged in or the battery charge when disconnected.
ExtensionHave students do a code review. Pick any of the six programs to review.
ExtensionWrite algorithms for the functions, or explain what each line of code does. Discuss parameters and arguments.
MathThe code uses built-in math functions like abs() and min(). Discuss other built-in math functions and practice using them in practical problems.
MathObjective 5 introduces binary numbers. Practice converting binary and decimal numbers. Explore other number systems.
MathSeveral Objectives test the drone's flight capabilities. The last Objective uses a chart to document tests. Do an in-depth analysis of the data.
Lang ArtsMake a list of troubleshooting strategies used to identify and fix errors.
🔤

Vocabulary

Positioning Systems -A system for determining the position of an object in space.
Flow Sensor -A sensor used to track horizontal movement across a surface; essential for stable hover and precise navigation.
Delta -A change in position, symbol from the Greek letter Δ, used in math and science to represent change.
String -A data type that is a sequence of characters strung together. Numbers, letters, spaces - whatever fits.
Format String -A template for printing a string using replacement fields designated with {curly braces}, allowing actual arguments to be inserted.
MotionCommander Interface -An interface between Python code and the flight controller that provides high-level flight control and uses onboard sensors to maintain stability.
Sensor Fusion -When data from multiple sensors is combined - for example, combining altitude data from the laser ranger with data from the flow sensor.
Under Load -When a battery is powering peripherals like CodeAIR's motors, it is under load.
Binary Numbers -How computers represent digits. Two states (on and off) are represented with 1 and 0.
Bit -A single binary digit (1 or 0).
Byte -An 8-bit number.
Selectable Operations UI -A user interface that uses one button to scroll through a menu and another button to confirm and start the action.
Exceptions -Errors that might happen during your program execution.
External Positioning System -A positioning system that uses something outside the drone to determine location, such as GPS or a fixed-location beacon.
🐍

New Python Code

dx, dy = get_data(FLOW)Read data from the flow sensor; returns the change in x direction and y direction.
print(x, y)Simple print statement that converts data to strings and displays on the console.
print("Flow Sensor Output")Print a string of text on the console.
print(f"x={x}, y={y}")F-string with replacement fields in curly braces.
abs(x)Returns the absolute value of x.
vbatt = power.battery_voltage(10)Read battery voltage, averaged over 10 samples.
amps = power.charger_current()Read charging current when connected via USB.
usb_connected = power.is_usb()Returns True if currently powered by USB.
value = 0b1001Set the value to 9 using binary.
leds.set_mask(255, 50)Set BYTE LEDs to 255 (all on) at brightness 50.
leds.set_mask(0b10101010, 50)Set BYTE LEDs using a binary pattern.
if __name__ == '__main__':Detects when this program is being run as the main program instead of an import.
try:A block of code that executes when no error occurs, or until an error occurs.
except:A block of code that lets your program respond to an error without crashing.
📐

Standards

CSTA Standards — Grades 6–8

2-CS-02 2-CS-03 2-DA-07 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

CSTA Standards — Grades 9–10

3A-CS-01 3A-CS-03 3A-DA-09 3A-AP-13 3A-AP-14 3A-AP-16 3A-AP-17 3A-AP-18 3A-AP-21 3A-IC-26

CSTA Standards — Grades 11–12

3B-CS-02 3B-DA-06 3B-DA-07 3B-AP-10 3B-AP-14 3B-AP-15 3B-AP-16 3B-AP-21 3B-AP-22 3B-AP-23
📝
Preparing for the Lesson
  • Reserve a flight space with plenty of unobstructed room - clear furniture or use a hallway. Flooring with a slight pattern works best for the flow sensor.
  • Charge all CodeAIR batteries fully. The flight tests in this mission are longer than previous missions.
  • Have a metric tape measure or ruler available so students can compare commanded flight distances with measured actual distances.
  • Decide whether students will log Objective 7 flight data on the printed chart in the assignment doc, or in the optional spreadsheet (linked in Digital Resources) for deeper analysis.
  • Plan for multiple class periods - this mission can easily span a class period per objective if students experiment with parameters.

🧑‍🏫
Teacher Notes
  • The last objective requires students to create a chart and log the results of several test flights. The assignment document has a chart they can use. Alternatively, students can use a spreadsheet (one is provided) and use the spreadsheet for more extensive data analysis.
  • This mission involves several programs, and each one can be used for experimentation. Don't rush, and give students plenty of time to try their code with different values and in different environments. You could easily spend a class period on almost every single objective.
  • Objective 4 has some important instructions after the goal is met. Students need to add their battery check to safety.py and then run the code to reinstall it on CodeAIR. Don't let students skip this part!
  • The test flights during the Objectives will need plenty of unobstructed room. Clear out as much furniture as you can, or use hallways. Also check the flooring. Use flooring with a pattern for best results.
  • Review questions can be used as a class review, made into a Kahoot!, or used to create an exam in your learning management system.
  • Extensions and cross-curricular projects are included to enhance the concepts in the mission. You can use the extensions to extend students' programming experience. A remix is planned after Mission 6.
🗺️

Lesson Outline

🗣️Warm-up / Hook

Students access prior knowledge by answering questions in the pre-mission section of the assignment doc.

Connect autonomous navigation to real-world examples through a brief discussion.

  • Ask: "How does a self-driving car know where it is in the world? What about a robot vacuum?"
  • Ask: "What's the difference between knowing your absolute location (like GPS) and tracking your motion from a starting point?" - preview dead reckoning vs. external positioning.
Teaching tip: Show a quick video of an optical computer mouse on a transparent surface to illustrate why the flow sensor needs ground texture to work.
📖Introduce the Mission

Front-load the key new programming concepts and lay out the mission's scope.

  • Introduce the flow sensor - explain that it sums up changes in x and y over time to estimate position.
  • Demonstrate f-strings - show how curly-brace replacement fields make console output cleaner than concatenating strings.
  • Preview binary numbers and how an 8-bit byte maps directly onto the 8 LEDs.
  • Introduce try / except with a simple example - what happens if the user enters bad input?
  • Set expectations: this is a 120-180 minute mission with six programs and is designed to span several class periods.
Teaching tip: Review the CodeAIR Flying Guide and safety procedures with students before any drone leaves the ground.
💻Coding Time

Students work through six programs that progress from sensor exploration to a full selectable-operations program. As they work through the objectives, they should take notes and answer questions in their assignment doc.

  1. FlowTracker - read flow sensor data and use f-strings to print x/y position to the console (Objectives 1-2; quiz after Objective 2).
  2. SquareUp & SquareTurns - fly precise square routes to test sensor accuracy and observe the effects of distance and velocity parameters (Objective 3).
  3. BattTest - perform a battery check using power.battery_voltage(), then add the check to safety.py and reinstall it on CodeAIR (Objective 4; quiz after Objective 4).
  4. Binary LED UI - use leds.set_mask() with binary literals (0b10101010) to display patterns on the 8 LEDs (Objective 5).
  5. utility.py - build a selectable-operations user interface with try / except error handling (Objective 6; quiz after Objective 6).
  6. RouteSelect - combine everything into an autonomous flight program where the user chooses a route at runtime; log test data in a chart for analysis (Objective 7).
Teaching tip: Don't let students skip the Objective 4 step where they reinstall safety.py with the battery check added - later programs depend on it.
Teaching tip: Use the optional flight data spreadsheet for Objective 7 if you want students to do deeper data analysis (mean, median, range, comparison charts).
Teaching tip: Always start the drone from the floor in clear space - flow sensors need an unobstructed view of the ground to track position.
🧑‍🤝‍🧑Class Debrief

Bring the class together to consolidate the big programming ideas from this mission and prepare for the Unit 2 Remix.

  • Ask: "What is sensor fusion? Why does CodeAIR need more than one sensor to fly accurately?"
  • Ask: "Why is try / except better than just letting a program crash?"
  • Ask: "How could you redesign RouteSelect to fly your own custom route?" - preview the Unit 2 Remix.
Teaching tip: Have students share the most surprising result from their flight data charts - this builds intuition about sensor accuracy and drift.