0
0
Drone Programmingprogramming~20 mins

Line following with camera in Drone Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Line Following Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of the line detection function?

Given the following function that processes a camera frame to detect a line's position, what will be the returned value when the line is centered?

Drone Programming
def detect_line_position(frame):
    # frame is a 2D list representing pixel brightness
    width = len(frame[0])
    line_pixels = [x for x in range(width) if frame[len(frame)//2][x] > 200]
    if not line_pixels:
        return None
    position = sum(line_pixels) / len(line_pixels)
    center = width / 2
    offset = position - center
    return offset

# Example frame: center line pixels at positions 4,5,6 with brightness 255
frame = [[0]*10 for _ in range(10)]
for x in [4,5,6]:
    frame[5][x] = 255
print(detect_line_position(frame))
A1.0
B0.0
C-1.0
DNone
Attempts:
2 left
💡 Hint

Think about how the average position of the bright pixels compares to the center of the frame.

🧠 Conceptual
intermediate
1:30remaining
Which sensor data is most useful for line following?

For a drone following a line on the ground using a camera, which type of data is most important to extract from the camera feed?

AThe position of the line relative to the drone's center
BThe color histogram of the entire frame
CThe total brightness of the frame
DThe GPS coordinates of the drone
Attempts:
2 left
💡 Hint

Think about what helps the drone stay on the line.

🔧 Debug
advanced
2:00remaining
Why does this line following code cause a crash?

Consider this code snippet for line following. It crashes with an error. What is the cause?

Drone Programming
def follow_line(frame):
    width = len(frame[0])
    line_pos = None
    for x in range(width):
        if frame[len(frame)//2][x] > 200:
            line_pos += x
    return line_pos
Aline_pos is None and cannot be added to an integer
Bframe is empty causing index error
CThe loop variable x is not defined
DThe function returns before the loop completes
Attempts:
2 left
💡 Hint

Check the initial value of line_pos and how it is used inside the loop.

📝 Syntax
advanced
1:30remaining
Which option fixes the syntax error in this line detection code?

Fix the syntax error in this dictionary comprehension that maps pixel positions to brightness if brightness is above 200:

{x: frame[5][x] if frame[5][x] > 200 for x in range(10)}
A{x: frame[5][x] if frame[5][x] > 200 for x in range(10)}
B{x: frame[5][x] if frame[5][x] > 200 else 0 for x in range(10)}
C{x: frame[5][x] for x in range(10) if frame[5][x] > 200}
D{x: frame[5][x] for x in range(10) if frame[5][x] < 200}
Attempts:
2 left
💡 Hint

Remember the correct order of for and if in comprehensions.

🚀 Application
expert
2:00remaining
How many control adjustments are made in this line following loop?

Consider this drone control loop that adjusts steering based on line position offsets detected from the camera. How many times will the drone adjust its steering if the offsets list is [0.1, -0.2, 0.0, 0.3, -0.1]?

Drone Programming
offsets = [0.1, -0.2, 0.0, 0.3, -0.1]
adjustments = 0
for offset in offsets:
    if abs(offset) > 0.05:
        adjustments += 1
print(adjustments)
A2
B3
C5
D4
Attempts:
2 left
💡 Hint

Count how many offsets have an absolute value greater than 0.05.