Fly with Python - Mission 8: Drone Director
Drone Director
Students combine the task scheduler with formal state handlers to fly precision flight profiles, using the flight data recorder, non-blocking flight commands, and event injection to direct CodeAIR like a film director directs a scene.
Overview
Mission 8 brings together the task scheduler from Mission 7 with the flight commands from Unit 2. Students build TaskSeq using state variables and the flight data recorder, then refactor it into FormalStates using state handler functions registered with the StateMachine. Along the way they learn non-blocking flight commands (fly.start_up(), fly.start_down()), how to log to and read from the on-board flight recorder, and how to inject sensor-data events into the state machine for precision altitude control.
🎯 Mission Goal: Students will program a drone, using the scheduler and state handlers, to fly intelligently while simultaneously managing other operations.
Learning Targets
- I can apply the scheduler to manage sequential flight.
- I can record to the flight data recorder and print the data to the console.
- I can implement non-blocking flight commands within state handlers.
- I can write functions for state handlers for formal flight states.
- I can integrate fly.steady() into the flight profile.
- I can design and execute timed flight profiles that combine precise altitude control with concurrent background tasks.
Key Concepts
- Flight tasks and the scheduler were introduced separately. This mission combines them for powerful drone control.
- The drone has a flight data recorder on board that acts like its "black box." It saves important information that prints to the console. Data from the previous run is printed, so the code must be run a second time to see results of the first run.
- Formal states are more efficient than state variables. Formal states use state handler functions that each perform their own tasks. State handlers must be added to the state machine.
- Sensor data can be scheduled as a task and run simultaneously with a flight profile, enabling the drone to use data for precise control.
Assessment Opportunities
- Quiz after Objective 3
- Quiz after Objective 5
- Complete the programs TaskSeq and FormalStates
- Mission 8 Assignment
- Mission 8 Review Questions
Success Criteria
- Use the task scheduler for flight tasks
- Use the flight recorder to save and print data
- End a flight task by one-time scheduling a state transition callback
- Schedule flight tasks using state handlers
- Schedule the non-blocking fly.steady() to run every 0.1 seconds
- Inject an event handler for sensor data updated
Digital Resources
Classroom Materials
- ▸Laptop/computer with Chrome browser
- ▸CodeAIR drone and USB cable
Real-World Applications
Extensions & Cross-Curricular
🔤
Vocabulary
▾
🐍
New Python Code
▾
state = 'ascend'
# more lines of code hereUse a state variable to control program flow. The state variable needs to be updated when a state changes.
while state != 'end':
sm.run_tasks()Run the scheduled tasks until the state is
'end' (not an infinite loop).
sm.enable_debug(True, rec.log)Add the second parameter to log information to the flight data recorder.
rec.log(state)Logs a message to the flight data recorder. Logs current state to the flight data recorder.
global state
state = 'ascend'Define a callback function that changes the state. This function can be scheduled to run after a delay without blocking.
finished) to end the loop.
event_data argument.
📐
Standards
▾
CSTA Standards — Grades 6–8
CSTA Standards — Grades 9–10
CSTA Standards — Grades 11–12
- Reserve a clear flight space - this mission has students fly the drone for the first time in Unit 3.
- Charge all CodeAIR drones fully. Have spare batteries ready.
- Review the flight data recorder workflow - students will need to run their code twice to view recorded data, since the recorder prints data from the previous run.
- Be ready to explain the deliberate "bugs" in Objectives 1-3 - they are intentional, and students will fix them when they refactor into formal state handlers in Objectives 4-6.
- Starting with Objective 2, students need to run the code and fly the drone. Then connect the drone again and run again, but this time instead of flying the drone they check the recorder log on the console panel. This is an important step! Meeting the goals of the objective aren't just typing in the code, but also looking at the flight recorder data. Don't let your students skip this!
- Several objectives give code that meets the goals but won't fly the drone correctly. This is intentional. When a drone doesn't fly as expected, check the code thoroughly and check the flight recorder data. If everything is following the instructions, just move on. The "bug" will be fixed in the next objective.
- A lot of code in Objectives 1-3 will be replaced with state handlers in Objectives 4-6.
- 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 10.
Lesson Outline
Students access prior knowledge by answering questions in the pre-mission section of the assignment doc.
Connect state machines and flight directors to real-world examples through a brief discussion.
- Ask: "How does a film director coordinate a complex scene - actors, lighting, sound, camera - all at once? How is that different from one person trying to do it all sequentially?"
- Ask: "In Mission 7 we scheduled tasks. What was missing if we wanted the drone to perform a sequence of flight steps in a particular order?"
Front-load the key new programming concepts before students start coding.
- Introduce the flight data recorder - explain that it records to a file and the data from the previous run prints when the program starts.
- Demonstrate the state-variable approach (Objectives 1-3) - using a
whileloop withifbranches. - Preview the formal-state approach (Objectives 4-6) - state handler functions registered with
sm.add_state(). - Introduce non-blocking flight commands:
fly.start_up(),fly.start_down(), andfly.steady()with no parameter. - Set expectations: this is a 45-60 minute mission with two programs - TaskSeq and FormalStates.
Students build two programs that progress from a state-variable approach to a formal-state-handler architecture. As they work through the objectives, they should take notes and answer questions in their assignment doc.
-
TaskSeq - state variables (Objectives 1-3; quiz after Objective 3): use a state variable in a
whileloop, log to the flight recorder withrec.log(), and end a flight task by one-time-scheduling a state-transition callback. - Run twice to see recorder output - the recorder prints data from the previous run, so every objective requires a fly-then-check cycle. Don't skip the check.
-
FormalStates - state handlers (Objectives 4-5; quiz after Objective 5): refactor TaskSeq into state handler functions (
def begin(sm, event_type, event_data, st_time):), register each withsm.add_state(), and usesm.next_state()for transitions. -
Add non-blocking flight - schedule
fly.steady()as a 0.1-second task so the flight controller stays engaged while other tasks run. -
Inject a sensor-data event (Objective 6) - extend the state handler to respond to
'sensor_data_updated'events for precision altitude control.
Bring the class together to compare the two architectures and reinforce the design principles.
- Ask: "What was the advantage of refactoring TaskSeq into FormalStates? What did formal state handlers make easier?"
- Ask: "What is separation of concerns? How does using state handlers demonstrate it?"
- Ask: "How did the flight data recorder help you debug?"
Students answer the reflection question in the assignment doc and then submit.
Use the Mission 8 Review Questions through a preferred method - class discussion, Kahoot!, or LMS quiz.