Fly with Python - Mission 5: Hovering Flight

Mission 5 Lesson Plan

Hovering Flight

Students take CodeAIR airborne for the first time, using the MotionCommander interface, laser rangers, custom modules, and variables to hover, navigate, and avoid walls.

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

Overview

Mission 5 puts CodeAIR in the air. Students start by saving Mission 4's safety code as a reusable custom module, then learn the MotionCommander interface - the high-level flight controller that lets the drone hover, fly forward and back, and respond to sensor data. Through six programs (Hover, Rangers, Ceiling, Theremin, HallMonitor, Avoidance), students explore blocking vs. non-blocking functions, unpack ranger sensor tuples into variables, and build a wall-avoidance algorithm that lets CodeAIR navigate autonomously.

🎯 Mission Goal: Students will program CodeAIR to avoid walls and seek an exit by utilizing its sensors.

🎯

Learning Targets

  • I can use the console output print() statement.
  • I can control CodeAIR with the MotionCommander interface.
  • I can use blocking and non-blocking functions.
  • I can measure distances with CodeAIR's laser rangers.
  • I can work with variables in Python.
💡

Key Concepts

  • You can save important functions as a custom module, then import and use the module in future programs.
  • CodeAIR uses a high-level flight control interface called MotionCommander that uses sensors to maintain stable flight.
  • Functions can be blocking or non-blocking.
  • When connected to CodeSpace, CodeAIR can print information to the console.
  • A variable is a name you attach to an object so your code can work with it.
  • You can use a variable to give your code memory. Update the variable to use it as a counter.

Assessment Opportunities

  • Quiz after Objective 5
  • Quiz after Objective 7
  • Quiz after Objective 9
  • Save safety.py as a custom module
  • Complete the programs Hover, Rangers, Ceiling, Theremin, HallMonitor, and Avoidance
  • Mission 5 Assignment
  • Mission 5 Review Questions
☑️

Success Criteria

  • Create the safety.py custom module
  • Use MotionCommander commands to fly
  • Read sensors and unpack their values into variables
  • Use the CodeAIR and variables as a people counter
  • Use the blue LEDs as counters
  • Avoidance works correctly and runs without errors or bugs
  • Complete Mission 5 Assignment
🧰

Classroom Materials

  • Laptop/computer with Chrome browser
  • CodeAIR drone and USB cable
  • Poster board or similar material to use as a "wall"
  • Open floor space with even lighting and a slight ground pattern (helps the flow sensor)
🌍

Real-World Applications

🤖Sensor-based navigation is the foundation of self-driving cars, warehouse robots, and search-and-rescue drones - all of which use the same observe-decide-act loop students implement here.
📦Custom Python modules are how professional developers organize code into reusable libraries - the same pattern is used in everything from web frameworks to NASA mission software.
🎚️Blocking vs. non-blocking functions are a core concept in robotics, embedded systems, and game development - knowing when each is appropriate is a real engineering skill.
📡The OODA loop ("observe, orient, decide, act") was developed by military strategists and is now used in autonomous vehicle navigation, cybersecurity response, and emergency operations.
🚀

Extensions & Cross-Curricular

ExtensionCreate a more elaborate security system after Objective 5 and/or Objective 6.
ExtensionCode a different ending to fix the bug after Objective 10.
ExtensionHave students do a code review. Pick any of the six programs to review.
ExtensionThe programs in this mission use algorithms to solve a problem. Discuss what an algorithm is. Practice writing algorithms.
MathIn Objective 3, the drone flies forward a given distance. Create a chart of distances entered in code and actual distance flown. Calculate the difference. Write an equation to predict future flights.
ScienceThe theremin was invented during research into proximity sensors. Study the physics involved in the musical instrument. Try building and playing your own theremin.
Lang ArtsWrite a paragraph that explains when to use a loop and when to use an if statement in code.
Lang ArtsEvaluate the ways computing impacts personal or cultural practices.
Lang ArtsMake a list of troubleshooting strategies used to identify and fix errors.
🔤

Vocabulary

Module -An external source of code that is outside your own source file; also known as a library.
Custom Module -Some code that is in the same folder as your program and can be accessed by importing it.
Docstring -A documentation string; a comment at the top of the file that explains what it does. Use triple quotes (''' or """) to start and stop a docstring.
Console -A window that lets you see output from print() statements.
Blocking Function -A function that runs one line at a time, blocking your code from continuing until it is finished. Examples: steady() and sleep().
Non-Blocking Function -A function that starts a movement, then returns to the code. Another command must be sent to change or stop the movement.
OODA Loop -"Observe, orient, decide, act" - a continuous loop run by the CPU to keep the drone flying at a desired altitude.
Variable -A name attached to an object so your code can work with it. The object can be any data: a number, text, tuple, etc.
Tuple -Ranger data - a set of three values indicated with parentheses (forward, up, down).
Polling -Repeatedly checking something to see if anything has changed.
Actuator -A device that receives signals and responds with a specific action. When flying a drone, the motors are actuators that receive input from sensors.
Updating a Variable -Changing the value of a variable with assignment. An example is to increment a count by 1.
Dead Reckoning -A type of navigation that calculates the vehicle's position based on its known starting point, speed, direction, and elapsed time.
Sensor-based Navigation -A type of navigation that is adaptive, relying on real-time data gathered by sensors to detect obstacles and adjust course accordingly.
REPL -Repeat-evaluate-print loop; using the console to interactively enter commands and view outputs in a text format.
🐍

New Python Code

'''This is a docstring'''Document string that should go at the top of any module.
fly.take_off(height_meters)Ascend to given height altitude.
fly.steady(seconds)Hover; allows code to pause while keeping the flight controller running.
fly.land()Descend to the floor.
fly.forward(distance, velocity)Distance in meters, velocity in meters per second (defaults to 0.2).
fly.back / left / right / up / down(distance, velocity)Distance in meters, velocity in meters per second (defaults to 0.2).
get_data(RANGERS)Returns the (forward, up, down) distance in millimeters.
if up < too_close:
    # sound alarm
If statement with a condition.
fwd, up, down = get_data(RANGERS)Unpack ranger data from a tuple into three variables.
ticks = timeout * 10
for i in range(ticks):
    fly.steady(0.1)
    fwd, up, down = get_data(RANGERS)
    if up < too_close:
        return True
return False
Polling algorithm with a blocking function. timeout is in seconds; polls 10 times per second.
speaker.beep(400, 0)Play the beep continuously. Requires speaker.off() to stop.
count = count + 1Increment / update a variable.
leds.set_mask(0, 0)Turn off all the blue LEDs.
fly.start_forward()Non-blocking function that starts moving forward at the default velocity and returns immediately so the next instruction can be executed.
fly.stop()Stop any motion and hover.
fly.turn_left(degrees)A blocking function that turns the drone degrees left.
if count == 8:Check if count equals 8; if so, run the indented branch.
📐

Standards

CSTA Standards — Grades 6–8

2-CS-02 2-CS-03 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-03 3A-DA-11 3A-AP-13 3A-AP-15 3A-AP-16 3A-AP-17 3A-AP-18 3A-AP-21 3A-IC-24 3A-IC-26

CSTA Standards — Grades 11–12

3B-DA-06 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
  • Identify a flight space with plenty of room and good, even lighting. The floor should have a slight pattern to help the flow sensor track movement - bare polished concrete or solid-color carpet can cause drift.
  • Ensure all CodeAIR drones are fully charged. Have spare batteries or charging cables available - flight drains batteries faster than ground exercises.
  • Cut or gather poster boards or cardboard pieces to use as walls for the wall-avoidance program.
  • Have a file folder or clipboard handy to use as the "ceiling obstacle" in Objective 6 - never use a hand near the propellers.
  • Plan to spread this mission across multiple class periods (potentially a full week) - there are six programs and many concepts.

🧑‍🏫
Teacher Notes
  • When students fly a drone, it is very important to have a lot of space and good lighting. The ground surface should have a slight pattern to help with flow sensor detection. Always fly the drone starting from the floor, with no obstacles around it. If the drone flies over a chair or table, the down ranger will detect it and bounce up, and it could crash into the ceiling. Safety first!
  • This mission is lengthy and includes several programs and concepts. Take your time, even an entire week. Using several days for the Objectives will give your students plenty of time to explore and try things.
  • In Objective 1, students add code to their custom module. The __name__ and '__main__' use two underscores. You may need to mention this to students to avoid errors.
  • The floor space and lighting really do make a difference in how well CodeAIR can track its movements. If you have a lot of drift, move to different flooring or add a pattern to the floor to help with navigation.
  • In Objective 6, think safety first! Wait until the LEDs are BLUE and the drone is holding steady before attempting the obstacle above the drone. Use a file folder or clipboard as an obstacle instead of a hand. You don't want fingers in the blades.
  • The CodeTrek in Objective 8 shows a step-by-step algorithm for counting people. This mission uses several algorithms. This is a good time to have a discussion about algorithms and practice writing them.
  • 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. 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 sensor-based navigation to real-world systems through a brief discussion.

  • Ask: "How does a Roomba avoid running into walls? How does a self-driving car stay in its lane?"
  • Ask: "If you had to walk across the room blindfolded, how would you avoid bumping into things?" - connect this to how the drone uses sensors.
Teaching tip: Introduce the OODA loop (observe, orient, decide, act) early - students will see this pattern in every program in this mission.
📖Introduce the Mission

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

  • Show students how to save Mission 4's safety.py as a custom module they will import and reuse.
  • Introduce the MotionCommander interface - explain that fly.take_off(), fly.steady(), and fly.land() handle the complex flight controller behind the scenes.
  • Preview the difference between blocking and non-blocking functions with a simple example.
  • Walk through the laser ranger sensors and tuple unpacking - fwd, up, down = get_data(RANGERS).
  • Set expectations: this is a 90-180 minute mission with six programs. It can 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 build progressively from a single hover to autonomous wall avoidance. As they work through the objectives, they should take notes and answer questions in their assignment doc.

  1. safety.py module - save Mission 4's safety code as a reusable custom module and import it (Objectives 1-2).
  2. Hover - first flight! Use fly.take_off(), fly.steady(), and fly.land() with the imported safety module (Objectives 3-5; quiz after Objective 5).
  3. Rangers - read sensor data and unpack tuples into variables; use print() to view data in the console (Objective 6).
  4. Ceiling - use polling and an if statement to detect an obstacle above the drone (Objective 7; quiz after Objective 7).
  5. Theremin & HallMonitor - combine sensors with the speaker and LEDs to build a proximity instrument and a people counter using variables (Objectives 8-9; quiz after Objective 9).
  6. Avoidance - combine non-blocking flight functions with sensor polling so CodeAIR avoids walls and seeks an exit autonomously (Objective 10).
Teaching tip: Always start the drone from the floor in clear space. If it crosses over a chair or table, the down ranger will detect it and the drone may rise unexpectedly.
Teaching tip: In Objective 6, use a file folder or clipboard as the ceiling obstacle - never a hand near the propellers.
Teaching tip: Watch for double-underscore mistakes - __name__ and '__main__' trip up many students.
🧑‍🤝‍🧑Class Debrief

Bring the class together to consolidate the big programming ideas from this mission.

  • Ask: "What is the difference between a blocking and a non-blocking function? When would you choose each?"
  • Ask: "Why is unpacking sensor data into variables more useful than just reading the tuple directly?"
  • Ask: "What is an algorithm? Walk through the wall-avoidance algorithm from Avoidance step by step."
Teaching tip: Use the CodeTrek in Objective 8 as a model for writing algorithms - students can reuse this pattern when they design their own programs in the Unit 2 Remix.
✏️Wrap-up & Review

Students answer the reflection question in the assignment doc and then submit.

Use the Mission 5 Review Questions through a preferred method - class discussion, Kahoot!, or LMS quiz.

Teaching tip: The Unit 2 Remix follows Mission 6 - encourage students to start thinking about how they might combine safety, hovering, and sensor-based navigation in their own program.