Teacher Resources - Fly with Python Mission 12
Line Patrol
Students turn raw camera pixels into machine vision, teaching CodeAIR to see a black line by its brightness and follow it on its own, then land when the line ends.
Overview
Mission 12 is CodeAIR's first step into machine vision. Students start by reading every pixel in a camera frame and building a new image from it, first a negative, then a greyscale version, then a black and white image made with a threshold.
From there they scan just a few rows of the image for speed, find the center of the dark line in each row, and turn those centers into a heading angle and a position error. In the final objective they close the loop: the error feeds the flight controller using proportional control, and CodeAIR follows a black line on its own and lands gracefully when it is done.
🎯 Mission Goal: Students will program a drone to follow a black line using machine vision.
Learning Targets
- I can look at a camera frame and read every pixel.
- I can reduce a color image to greyscale.
- I can reduce a greyscale image to black & white.
- I can set a threshold to distinguish a black line from a background.
- I can find the center of a line by scanning a few rows for speed.
- I can turn centers into a heading and a centering error.
- I can close the loop by feeding the error to the flight controller and following the line.
Key Concepts
- The drone's camera frame is a bitmap of all the pixels used to create the image. The image size is 160x120 pixels. Each pixel packs red, green and blue into 16 bits in a format called RGB565.
- The camera frame is read-only, but the data can be used to build a new image. Use nested loops to access every pixel.
- Hexadecimal numbers are base 16. Using hex is a compact way to write bit patterns, since each digit represents 4 bits.
- Bitwise operators work with individual bits. They can be used when working with images to flip bits and shift bits either left or right.
- The continue command can be used to return to the top of a loop, skipping instructions below the command.
Assessment Opportunities
- Quiz after Objective 3
- Quiz after Objective 6
- Complete the program LinePatrol_Vision
- Complete the program LinePatrol_Scan
- Complete the program LinePatrol_Fly
- Mission 12 Assignment
- Mission 12 Review Questions
Success Criteria
- Snap a photo and display the negative
- Snap a photo and display the greyscale image
- Use a threshold to make an image black and white
- Snap a photo, scanning only every other pixel in three lines - top, middle and bottom
- Display the top, middle and bottom line centers
- Compute the heading angle and position error
- Use format specifiers to display decimal numbers
- Program the CodeAIR to follow a line and land gracefully when done
Digital Resources
- ▸CodeAIR Mission 12 Assignment
- ▸CodeAIR Mission 12 Review Questions
- ▸Code Solutions and Assignment Answers - Teacher Dashboard > Educator Resources button
Classroom Materials
- ▸Laptop/computer with Chrome browser
- ▸CodeAIR drone and USB cable
- ▸White sheet of paper with a line made with ¾-inch black electrical tape
- ▸Light floor or posterboard with a line made with ¾-inch black electrical tape
Real-World Applications
Extensions & Cross-Curricular
🔤
Vocabulary
▾
🐍
New Python Code
▾
for j in range(frame.height):
pixel = frame[i, j]Use two nested loops to access every pixel.
g = (pixel >> 5) & 0x3F
b = pixel & 0x1FUse bit shifts and masks to pull out each color. Red and blue are 5 bits (0x1F). Green gets 6 bits (0x3F).
g8 = (g << 2) | (g >> 4)
b8 = (b << 3) | (b >> 2)Scale the color values to from 0-255.
out_g = grey8 >> 2
out_b = grey8 >> 3
grey565 = (out_r << 11) | (out_g << 5) | out_b
grey565 = ((grey565<<8) & 0xFF00) | (grey565>>8)
out_bitmap[i, j] = grey565Pack the grey value back into a displayable pixel. This includes swapping the bytes again on the way out.
heading: {heading_angle:.1f} deg
pos_error: {pos_error:.0f} pxFormat specifier. These examples will display with one number past the decimal, and as an integer.
📐
Standards
▾
CSTA Standards - Middle School
CSTA Standards - High School
CSTA Standards - Physical Computing
- Make a practice line for each station: a white sheet of paper with a line made with ¾-inch black electrical tape. The paper can sit on a desk for Objectives 1 through 6.
- Set up a light floor area or posterboard with a taped line for the Objective 7 flight, and test the line before class.
- Reserve space with good lighting for the flight - the camera needs to see the black line clearly.
- Charge all CodeAIR drones fully and have spare batteries ready.
- Be familiar with the three programs (LinePatrol_Vision, LinePatrol_Scan, LinePatrol_Fly) and the assignment and review questions before class.
- The drone doesn't fly until the last objective. For Objectives 4 through 6, you can use a white sheet of paper with a black line taped to it, and the paper can sit on a desk.
- You can download an image from the console, and then copy/paste it into the assignment document.
- Objective 1: Watch the indenting! The pipe command is outside any loop. Students can take multiple images without restarting the code.
- Objective 2: Remember to delete the line of code that inverts the image.
- Objective 3: Use a piece of paper and tape a black line on it. That way the paper can sit on the desk.
- Objective 4: Run the code several times with different threshold values. You will not see an image, just white dots where a line is detected. You can also try the drone at different distances from the line.
- Objective 7: Some of the code in the first function is the same code from Obj. 6. You can copy/paste from one program to the next. There are differences, though, so step through CodeTrek carefully!
Lesson Outline
Students access prior knowledge by answering the pre-mission questions in the assignment doc.
- Ask: "What are the three axes a drone moves along, and what does each movement look like?"
- Ask: "When you look at a photo, how do you know where a road or a line is? What would a computer need to do the same thing?"
Front-load the key new concepts before students start coding.
- Introduce the camera frame as a 160x120 bitmap, and walk through how each pixel packs red, green and blue into 16 bits (RGB565).
- Introduce hexadecimal and the bitwise operators. Each hex digit represents 4 bits, and XOR can flip every bit in a pixel.
- Preview the pipeline: color image to greyscale, greyscale to black and white with a threshold, then scan a few rows to find the line center.
- Preview the steering idea: centers become a heading angle and a position error, and proportional control makes bigger corrections for bigger errors.
- Set expectations: three programs across seven objectives, with quizzes after Objective 3 and Objective 6. The drone only flies in the last objective.
Students build three programs that progress from reading pixels to flying the line. As they work through the objectives, they should take notes and answer questions in their assignment doc.
- LinePatrol_Vision (Objectives 1-3, quiz after Objective 3) - read every pixel with nested loops, display a negative and a greyscale image, then use a threshold to make the image black and white.
- LinePatrol_Scan (Objectives 4-6, quiz after Objective 6) - scan only a few rows of the frame, find the center of the line in each row, then compute the heading angle and position error and display them with format specifiers.
- LinePatrol_Fly (Objective 7) - feed the error to the flight controller with proportional control, follow the line, and land when the line ends.
Bring the class together to connect the pieces of the pipeline.
- Ask: "Why do we reduce a color image to greyscale and then to black and white before looking for a line?"
- Ask: "Why scan only a few rows instead of every pixel in the frame?"
- Ask: "What two pieces of information does a line-following program need, and how does the drone use them?"
- Ask: "What happens to the drone's correction as the error gets bigger? Where have you seen that idea before?"
Students answer the reflection question in the assignment doc and then submit.
Use the Mission 12 Review Questions through a preferred method - class discussion or an LMS quiz.