Fly with Python - Mission 9: Attitude Control
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.
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
Digital Resources
Classroom Materials
- ▸Laptop/computer with Chrome browser
- ▸CodeAIR drone and USB cable
Real-World Applications
pipe.chart() pattern students learn here is used in professional telemetry systems.
Extensions & Cross-Curricular
handle_attitude_feedback() function to play a different sound for a left tilt vs a right tilt.
🔤
Vocabulary
▾
🐍
New Python Code
▾
pipe.chart("Roll Rate", roll_rate)Function for live-plotting data. Two parameters: chart name and data.
pixels.set(i, RED)Set all pixels in the tuple
LEFT_PIXELS to RED.
set_param(motor, MOTOR_POWER)Set all the motors in
motors to MOTOR_POWER.
pixels.set(pixel, RED)Set all the pixels in
pixels_to_light to RED.
set_param(motor, 0)Iterate over the
ALL_MOTORS tuple to set all motor power to 0 (off).
📐
Standards
▾
CSTA Standards - Grades 6-8
CSTA Standards - Grades 9-10
CSTA Standards - Grades 11-12
- 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.
- 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
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?"
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.
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.
-
Explore GYRO in REPL (Objective 1) - read
get_data(GYRO)interactively and see the roll, pitch, yaw values change as the drone is tilted. -
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. -
Redirect to the flight recorder - use
pipe.connect(rec)to capture chart data during an untethered tilt, then reconnect to view the recorded data. - DipDetector - visual feedback (Objective 4) - light up the LEFT or RIGHT pixel group when the roll rate exceeds a threshold.
- Add haptic feedback with motors (Objective 5) - spin the corner motors as a tactile response to tilt.
-
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.
finally: block to ensure motors always shut off when the program ends.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.
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.