Bird
Raised Fist0
Drone Programmingprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1.

What is the main purpose of using a camera in a drone for line following?

easy
A. To detect the position of the line on the ground
B. To measure the drone's altitude
C. To record videos for entertainment
D. To communicate with other drones

Solution

  1. Step 1: Understand the role of the camera

    The camera is used to see the environment below the drone, especially the line it needs to follow.
  2. Step 2: Identify the main task for line following

    The drone must detect where the line is to adjust its path and stay on it.
  3. Final Answer:

    To detect the position of the line on the ground -> Option A
  4. Quick Check:

    Camera detects line position = B [OK]
Hint: Camera's job is to see the line, not altitude or communication [OK]
Common Mistakes:
  • Confusing camera use with altitude measurement
  • Thinking camera is for video recording only
  • Assuming camera is for drone communication
2.

Which of the following is the correct way to start capturing images from the drone's camera in Python?

camera = DroneCamera()
# What next?
easy
A. camera.begin()
B. camera.start_capture()
C. start.camera()
D. camera.capture_start()

Solution

  1. Step 1: Check common method names for starting capture

    Usually, methods to start camera capture are named with 'start' and 'capture' in order.
  2. Step 2: Match method names with options

    camera.start_capture() uses 'start_capture()', which is a common and clear method name.
  3. Final Answer:

    camera.start_capture() -> Option B
  4. Quick Check:

    Start capture method = start_capture() [OK]
Hint: Look for method names with 'start' and 'capture' in order [OK]
Common Mistakes:
  • Mixing method name order like capture_start()
  • Using invalid syntax like start.camera()
  • Guessing method names without checking conventions
3.

What will be the output of this code snippet that processes a camera frame to find the line position?

frame = camera.get_frame()
line_pos = detect_line_position(frame)
if line_pos < 0.4:
    print("Turn Left")
elif line_pos > 0.6:
    print("Turn Right")
else:
    print("Go Straight")

Assuming detect_line_position(frame) returns 0.3.

medium
A. Turn Left
B. Turn Right
C. Go Straight
D. No output

Solution

  1. Step 1: Understand the value of line_pos

    The function returns 0.3, which is less than 0.4.
  2. Step 2: Check the conditions in the if-elif-else

    Since 0.3 < 0.4, the first condition is true, so it prints "Turn Left".
  3. Final Answer:

    Turn Left -> Option A
  4. Quick Check:

    line_pos = 0.3 < 0.4 means Turn Left [OK]
Hint: Compare line_pos with 0.4 and 0.6 to decide direction [OK]
Common Mistakes:
  • Confusing less than and greater than signs
  • Ignoring the order of if-elif-else
  • Assuming else runs when first condition is true
4.

Identify the error in this code snippet for line following and select the fix:

frame = camera.get_frame()
line_pos = detect_line_position(frame)
if line_pos < 0.5
    drone.turn_left()
else:
    drone.turn_right()
medium
A. Replace 'else:' with 'elif line_pos > 0.5:'
B. Change 'turn_left()' to 'turn_left'
C. Add a colon ':' after the if condition
D. Remove the else block entirely

Solution

  1. Step 1: Check syntax of if statement

    The if statement is missing a colon ':' at the end, which is required in Python.
  2. Step 2: Verify other parts

    turn_left() is a method call and correct, else: is valid syntax, so no changes needed there.
  3. Final Answer:

    Add a colon ':' after the if condition -> Option C
  4. Quick Check:

    Missing ':' after if condition causes syntax error [OK]
Hint: Always put ':' after if conditions in Python [OK]
Common Mistakes:
  • Forgetting colon after if statement
  • Confusing method call syntax with attribute
  • Changing else to elif unnecessarily
5.

You want the drone to follow a black line on a white floor using the camera. The camera returns a grayscale image where the line pixels are darker (values near 0) and the floor is lighter (values near 255). Which approach best helps the drone decide how to move?

1. Calculate the average pixel value of the whole image.
2. Find the horizontal position of the darkest pixels (line).
3. Use the position to adjust drone's left or right movement.
4. Repeat continuously while flying.
hard
A. Use only the brightest pixels to follow the line
B. Use the average pixel value only to decide movement
C. Ignore pixel values and move randomly
D. Find darkest pixels' horizontal position and adjust movement accordingly

Solution

  1. Step 1: Understand the image data

    The line is darker pixels (near 0), so finding their horizontal position shows where the line is.
  2. Step 2: Use position to control drone movement

    Adjusting left or right movement based on line position keeps the drone following the line accurately.
  3. Final Answer:

    Find darkest pixels' horizontal position and adjust movement accordingly -> Option D
  4. Quick Check:

    Line position guides movement, not average or brightest pixels [OK]
Hint: Track darkest pixels' position to steer drone [OK]
Common Mistakes:
  • Using average pixel value which loses position info
  • Ignoring pixel brightness and moving randomly
  • Following brightest pixels instead of darkest
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard