Fly with Python - Mission 5: Hovering Flight
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.
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
Digital Resources
- ▸CodeAIR Mission 5 Assignment
- ▸CodeAIR Mission 5 Assignment Answers
- ▸CodeAIR Mission 5 Review Questions
- ▸safety_final Code Solution
- ▸Hover_obj2 Code Solution
- ▸Hover_obj3 Code Solution
- ▸Rangers Code Solution
- ▸Ceiling Code Solution
- ▸Theremin Code Solution
- ▸HallMonitor Code Solution
- ▸Avoidance Code Solution
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
Extensions & Cross-Curricular
🔤
Vocabulary
▾
🐍
New Python Code
▾
# sound alarmIf statement with a condition.
for i in range(ticks):
fly.steady(0.1)
fwd, up, down = get_data(RANGERS)
if up < too_close:
return True
return FalsePolling algorithm with a blocking function. timeout is in seconds; polls 10 times per second.
degrees left.
📐
Standards
▾
CSTA Standards — Grades 6–8
CSTA Standards — Grades 9–10
CSTA Standards — Grades 11–12
- 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.
- 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
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.
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(), andfly.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.
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.
- safety.py module - save Mission 4's safety code as a reusable custom module and import it (Objectives 1-2).
-
Hover - first flight! Use
fly.take_off(),fly.steady(), andfly.land()with the imported safety module (Objectives 3-5; quiz after Objective 5). -
Rangers - read sensor data and unpack tuples into variables; use
print()to view data in the console (Objective 6). -
Ceiling - use polling and an
ifstatement to detect an obstacle above the drone (Objective 7; quiz after Objective 7). - 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).
- Avoidance - combine non-blocking flight functions with sensor polling so CodeAIR avoids walls and seeks an exit autonomously (Objective 10).
__name__ and '__main__' trip up many students.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."
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.