Fly with Python - Mission 9: Attitude Control

Mission 9 Lesson Plan

Attitude Control

Students explore CodeAIR's gyroscope to build a feedback system, charting roll rate live, redirecting chart data to the flight recorder, and using LEDs and motors to give the drone visual and haptic responses to tilt.

⏱ 45-60 min 🎯 Grades 6-12+ 💻 CodeSpace 🚀 CodeAIR 🐍 Python
View Lesson Outline
📋

Overview

Mission 9 takes students inside the gyroscope - the sensor at the heart of the CodeAIR's stability system. Students begin by exploring gyroscope data interactively in the REPL, then build a live chart of the drone's roll rate using the data pipe. Next, they redirect sensor data to the flight recorder for analysis without a USB connection. The mission wraps up with DipDetector, a haptic feedback program that lights pixels and spins corner motors in response to tilt - culminating in four-corner attitude control using a data structure.

🎯 Mission Goal: Students will build a feedback system for the drone that uses data from the gyroscope for visual feedback and haptic responses.

🎯

Learning Targets

  • I can explore the gyroscope interactively using REPL.
  • I can chart the drone's roll rate in real-time.
  • I can redirect chart data to the flight recorder for analysis.
  • I can build a dip detector that provides visual feedback with LEDs and motors.
  • I can combine roll and pitch data to command individual corner motors and LEDs.
💡

Key Concepts

  • The CodeAIR uses a gyroscope to stay perfectly balanced. The gyroscope constantly measures its rate of rotation (not position). The three axes are roll, pitch, and yaw.
  • You can visualize the drone's data using the data pipe and data panel. All sorts of data can be visualized in real time using pipe.chart() as the go-to for live-plotting data. The data can also be sent to the flight data recorder for later analysis.
  • The drone can use haptic feedback by turning on pixels and motors as a result of data from the sensors.

Assessment Opportunities

  • Quiz after Objective 3
  • Complete the programs AttitudeChart and DipDetector
  • Mission 9 Assignment
  • Mission 9 Review Questions
☑️

Success Criteria

  • Read the data from GYRO to the console
  • Schedule a task that uses sensor data from GYRO to display a live chart
  • Redirect the GYRO reading to the flight data recorder to capture untethered data
  • Have the drone react to a tilt by lighting pixels
  • Spin the motors as a reaction to a tilt
  • Give the drone four-corner control using a data structure
🧰

Classroom Materials

  • Laptop/computer with Chrome browser
  • CodeAIR drone and USB cable
🌍

Real-World Applications

📱Gyroscopes are inside every smartphone, gaming controller, and VR headset - they detect tilt and rotation for orientation-based features like screen auto-rotate and motion controls.
🚁Real drones use the same gyro-driven feedback loop students build here. Without it, a quadcopter can't stay level - millisecond-by-millisecond corrections from the gyro keep it stable in the air.
📊Live data charting is how engineers monitor satellites, race cars, and industrial robots in real time - the same pipe.chart() pattern students learn here is used in professional telemetry systems.
🎮Haptic feedback - vibration in game controllers, taptic engines in phones, force-feedback steering wheels - uses the same sensor-driven actuator pattern students implement in DipDetector.
🚀

Extensions & Cross-Curricular

ExtensionModify the handle_attitude_feedback() function to play a different sound for a left tilt vs a right tilt.
ExtensionHave different thresholds for a slow tilt (different pixel color and motor power) and a faster tilt (different pixel color and motor power).
ExtensionMake the haptic feedback proportional. Adjust the motor power according to the data.
ExtensionUse the "selectable ops" concept from Mission 6 to add options - have a selection for testing the feedback loop and another that prints a chart to the console.
MathAnalyze a chart after tilting the drone. Can you calculate the rate of rotation?
ScienceThe drone has both an accelerometer and a gyroscope. Research the difference between the two sensors.
Lang ArtsWrite a paper that compares and contrasts the gyroscope and accelerometer (or another sensor).
🔤

Vocabulary

Attitude -The drone's orientation in 3D space.
Gyroscope -A sensor that measures the active rate of rotation around three axes.
Rolling -Tilting side to side.
Pitching -Tilting front to back.
Yaw -Turning left or right.
Data Panel -Shown in REPL: the mission control dashboard.
Data Pipe -High-speed data link that feeds information directly from the drone.
Haptic Feedback -Communication through the sense of touch.
Data Structure -A specific way of organizing and managing data so it can be accessed efficiently; lists and tuples are examples.
Refactoring -The process of improving the design of your code.
🐍

New Python Code

get_data(GYRO)Reads data from the GYRO - active rotation around three axes.
pipe.chart()
pipe.chart("Roll Rate", roll_rate)
Function for live-plotting data. Two parameters: chart name and data.
data = get_data(GYRO, wait=False)Reads data from the GYRO, unblocked.
pipe.connect(rec)Redirect the pipe to send its data to the flight data recorder.
recorder.print_report()Print the file from the last flight.
LEFT_PIXELS = (TOP_FRONT_LEFT, TOP_REAR_LEFT)Group pixels into a tuple.
for i in LEFT_PIXELS:
    pixels.set(i, RED)
Set all pixels in the tuple LEFT_PIXELS to RED.
for motor in motors:
    set_param(motor, MOTOR_POWER)
Set all the motors in motors to MOTOR_POWER.
for pixel in pixels_to_light:
    pixels.set(pixel, RED)
Set all the pixels in pixels_to_light to RED.
finally:The block of code is guaranteed to run when the program stops.
ALL_MOTORS = (MOTOR_FR, MOTOR_FL, MOTOR_RR, MOTOR_RL)Group all motors into a tuple.
for motor in ALL_MOTORS:
    set_param(motor, 0)
Iterate over the ALL_MOTORS tuple to set all motor power to 0 (off).
📐

Standards

CSTA Standards - Grades 6-8

2-CS-022-DA-082-DA-092-AP-122-AP-132-AP-142-AP-162-AP-17

CSTA Standards - Grades 9-10

3A-CS-033A-DA-103A-DA-113A-DA-123A-AP-163A-AP-173A-AP-18

CSTA Standards - Grades 11-12

3B-DA-053B-DA-063B-DA-073B-AP-103B-AP-123B-AP-143B-AP-153B-AP-163B-AP-21
📝
Preparing for the Lesson
  • This mission stays mostly hands-on with the drone - no flight space needed. Students hold and tilt the drone to test the gyroscope feedback.
  • Charge CodeAIR drones and have USB cables ready - Objective 3 requires a brief untethered moment to capture flight recorder data.
  • Open the data panel in REPL ahead of class and demonstrate the "CLEAR DATA" button - students will need to clear data between runs to keep charts readable.
  • Review the difference between roll, pitch, and yaw - students often confuse these when reading gyro data.

🧑‍🏫
Teacher Notes
  • The data in the chart can be cleared by clicking "CLEAR DATA" at the bottom of the window before running code, so only the most recent data is displayed.
  • Objective 3 requires untethered movement and then reconnecting to see the chart from the flight recorder. Meeting the goals of the objective aren't just typing in the code, but also looking at the flight recorder data. The validator won't check this. You could ask students to include a screenshot of their table to make sure they don't skip this step.
  • Objectives 5 and 6 use the drone's motors for feedback. Students will not be flying the drone. You can increase the motor power if needed to get feedback, but keep it low so the drone doesn't take off in your hands. It may take a little time for the feedback loop to work. Give it some time before increasing the power.
  • 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

🗣️Warm-up / Hook

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

Connect attitude sensing to real-world examples through a brief discussion.

  • Ask: "How does your phone know to rotate the screen when you tilt it? What sensor is doing that?"
  • Ask: "What would happen to a quadcopter if there was no sensor watching for tilt? Why does it need to constantly correct itself?"
Teaching tip: Have students stand up and try to balance on one foot. The constant micro-corrections their inner ear and muscles make are exactly what the gyroscope and motors do for the drone.
📖Introduce the Mission

Front-load the key new programming concepts before students start coding.

  • Introduce the gyroscope and the three axes of rotation: roll, pitch, and yaw.
  • Demonstrate get_data(GYRO) in REPL - show how reading the sensor returns three rotation rates.
  • Preview pipe.chart() for live data visualization in the data panel.
  • Show how pipe.connect(rec) redirects pipe data to the flight recorder for capturing untethered runs.
  • Set expectations: this is a 45-60 minute mission with two programs - AttitudeChart and DipDetector.
Teaching tip: Show students the data panel before they start coding - clearing data between runs keeps charts readable.
💻Coding Time

Students build two programs that progressively use the gyroscope for visualization and feedback. As they work through the objectives, they should take notes and answer questions in their assignment doc.

  1. Explore GYRO in REPL (Objective 1) - read get_data(GYRO) interactively and see the roll, pitch, yaw values change as the drone is tilted.
  2. AttitudeChart - live charting (Objectives 2-3; quiz after Objective 3) - schedule a task that reads GYRO and feeds pipe.chart("Roll Rate", roll_rate) to plot live data.
  3. Redirect to the flight recorder - use pipe.connect(rec) to capture chart data during an untethered tilt, then reconnect to view the recorded data.
  4. DipDetector - visual feedback (Objective 4) - light up the LEFT or RIGHT pixel group when the roll rate exceeds a threshold.
  5. Add haptic feedback with motors (Objective 5) - spin the corner motors as a tactile response to tilt.
  6. Refactor with tuples for four-corner control (Objective 6) - group pixels and motors into tuples (e.g., LEFT_PIXELS, ALL_MOTORS) and combine roll and pitch data for clean four-corner control.
Teaching tip: For Objective 3, students need to take the drone untethered, tilt it, and reconnect. Have them log a screenshot of the recorded chart since the validator can't check this step.
Teaching tip: In Objectives 5-6, students will hold the drone - keep motor power low to prevent take-off, and use a finally: block to ensure motors always shut off when the program ends.
🧑‍🤝‍🧑Class Debrief

Bring the class together to consolidate the feedback-loop and data-structure ideas.

  • Ask: "What does it mean to say a gyroscope measures 'rate of rotation' instead of position? Why does that matter?"
  • Ask: "How did using a tuple of motors change your code? Why is that better than four separate set_param() calls?"
  • Ask: "What other devices could you build using the same sensor to reaction feedback loop?" - connect to haptic feedback in real-world devices.
Teaching tip: Have students share their charts from Objective 3 - comparing different tilt speeds is a great visual reinforcement of "rate."
✏️Wrap-up & Review

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

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

Teaching tip: Mission 10 will combine the gyro feedback ideas with full autonomous flight - set the stage by previewing how all of Unit 3's tools will come together.