Range finder for terrain following in Drone Programming - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When programming a drone to follow terrain using a range finder, it's important to understand how the time it takes to process sensor data grows as the drone moves.
We want to know how the program's work changes as it reads more distance points.
Analyze the time complexity of the following code snippet.
function followTerrain(distances) {
for (let i = 0; i < distances.length; i++) {
let distance = distances[i];
adjustAltitude(distance);
}
}
function adjustAltitude(distance) {
// Adjust drone altitude based on distance
}
This code reads a list of distance measurements from the range finder and adjusts the drone's altitude for each measurement.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the distances array to read each distance.
- How many times: Once for each distance measurement in the input list.
As the number of distance points increases, the program processes each one in order.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 adjustments |
| 100 | 100 adjustments |
| 1000 | 1000 adjustments |
Pattern observation: The work grows directly with the number of distance points; doubling points doubles work.
Time Complexity: O(n)
This means the time to adjust altitude grows in a straight line with the number of distance measurements.
[X] Wrong: "The program runs in constant time because it just adjusts altitude once."
[OK] Correct: The program adjusts altitude for every distance point, so more points mean more work, not just one adjustment.
Understanding how sensor data processing scales helps you write efficient drone control code and shows you can think about performance in real tasks.
"What if the adjustAltitude function itself had a loop over the input? How would the time complexity change?"
Practice
What is the main purpose of a range finder in drone terrain following?
Solution
Step 1: Understand the role of a range finder
A range finder is a sensor that measures how far the drone is from the ground below it.Step 2: Connect measurement to terrain following
This distance helps the drone adjust its height to follow the shape of the terrain safely.Final Answer:
To measure the distance between the drone and the ground -> Option AQuick Check:
Range finder = distance measurement [OK]
- Confusing range finder with speed sensor
- Thinking it measures battery or air obstacles
- Assuming it controls horizontal movement
Which of the following code snippets correctly reads a range finder sensor value in a drone program?
1. distance = range_finder.read()
2. distance = read.range_finder()
3. distance = rangeFinder.readValue()
4. distance = range_finder.get()Solution
Step 1: Identify correct method call syntax
In drone programming, sensors are often objects with a method calledread()to get current values.Step 2: Check each option for correct syntax
Option 1 usesrange_finder.read(), which is standard and correct. Others use incorrect method names or syntax.Final Answer:
1. distance = range_finder.read() -> Option CQuick Check:
Sensor read method = read() [OK]
- Using wrong method names like get() or readValue()
- Incorrect object.method order
- Confusing variable names with method calls
What will be the output of this code snippet controlling drone height?
distance = 5
if distance < 3:
action = "ascend"
elif distance > 7:
action = "descend"
else:
action = "hold"
print(action)Solution
Step 1: Evaluate the distance condition
The distance is 5. Check if 5 < 3 (false), then if 5 > 7 (false).Step 2: Determine the else branch
Since both conditions are false, the else branch runs, setting action to "hold".Final Answer:
"hold" -> Option BQuick Check:
Distance 5 triggers else = hold [OK]
- Choosing ascend or descend incorrectly
- Confusing comparison operators
- Assuming error due to syntax
Find the error in this drone height control code:
distance = range_finder.read()
if distance < 2
action = "ascend"
else:
action = "descend"
print(action)Solution
Step 1: Check syntax of if statement
The if statement is missing a colon (:) at the end of the condition line, which is required in Python-like syntax.Step 2: Verify other parts
Comparison operator and indentation are correct. The method call is valid assuming range_finder object exists.Final Answer:
Missing colon after if condition -> Option DQuick Check:
if statement needs colon : [OK]
- Forgetting colon after if
- Misaligning else indentation
- Changing comparison operators unnecessarily
You want the drone to maintain a height of 4 meters above ground using the range finder. Which code snippet correctly adjusts the drone's vertical speed based on the measured distance?
distance = range_finder.read()
if distance < 4:
vertical_speed = 1 # ascend
elif distance > 4:
vertical_speed = -1 # descend
else:
vertical_speed = 0 # hold steady
print(vertical_speed)Solution
Step 1: Understand the desired behavior
The drone should ascend if below 4m, descend if above 4m, and hold if exactly 4m.Step 2: Check code logic
If distance < 4, vertical_speed = 1 (ascend) is correct. If distance > 4, vertical_speed = -1 (descend) is correct. Else holds steady at 0.Final Answer:
This code correctly sets vertical_speed to keep 4m height -> Option AQuick Check:
Conditions match desired height control [OK]
- Reversing ascend and descend speeds
- Removing else block causing no hold state
- Using <= or >= unnecessarily
