PID controller in flight stabilization in Drone Programming - Time & Space Complexity
We want to understand how the time it takes for a PID controller to stabilize a drone grows as the number of control cycles increases.
How does the work done by the controller change when we run it more times?
Analyze the time complexity of the following code snippet.
// PID controller update for drone stabilization
function updatePID(setpoint, measured, prevError, integral, dt) {
error = setpoint - measured
integral = integral + error * dt
derivative = (error - prevError) / dt
output = Kp * error + Ki * integral + Kd * derivative
return {output, error, integral}
}
// Called every control cycle
for (let cycle of controlCycles) {
result = updatePID(targetAngle, currentAngle, lastError, integralSum, deltaTime)
lastError = result.error
integralSum = result.integral
applyOutput(result.output)
}
This code runs a PID controller update every control cycle to adjust the drone's angle for stable flight.
- Primary operation: The PID update function runs once per control cycle.
- How many times: It repeats for each control cycle, so the number of cycles determines repetitions.
Each control cycle runs the same fixed steps in the PID update.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 PID updates |
| 100 | 100 PID updates |
| 1000 | 1000 PID updates |
Pattern observation: The total work grows directly with the number of control cycles.
Time Complexity: O(n)
This means the time to run the PID controller grows linearly with the number of control cycles.
[X] Wrong: "The PID controller's time grows faster than the number of cycles because it does complex calculations each time."
[OK] Correct: Each PID update does a fixed small number of steps, so time grows only with how many times it runs, not faster.
Understanding how control loops scale helps you explain performance in real-time systems, a useful skill in many technical roles.
"What if the PID controller also processed a list of sensor readings each cycle? How would the time complexity change?"
Practice
Solution
Step 1: Understand the role of PID controller
A PID controller uses error, integral, and derivative to adjust movements smoothly.Step 2: Connect PID function to drone stabilization
This adjustment helps keep the drone balanced and stable during flight.Final Answer:
To keep the drone balanced by adjusting its movements smoothly -> Option AQuick Check:
PID controls balance = To keep drone balanced [OK]
- Thinking PID only controls speed
- Confusing PID with battery management
- Assuming PID makes drone fly straight without control
Solution
Step 1: Recall derivative formula in PID
The derivative term measures how fast error changes: (current_error - previous_error) / delta_time.Step 2: Match formula with options
derivative = (current_error - previous_error) / delta_time matches the correct derivative calculation formula exactly.Final Answer:
derivative = (current_error - previous_error) / delta_time -> Option DQuick Check:
Derivative = error change over time [OK]
- Adding errors instead of subtracting
- Multiplying errors incorrectly
- Ignoring division by delta_time
error = target_angle - current_angle integral += error * dt derivative = (error - last_error) / dt output = Kp * error + Ki * integral + Kd * derivative last_error = error print(round(output, 2))
If
target_angle = 10, current_angle = 7, last_error = 2, integral = 5, dt = 1, Kp = 1, Ki = 0.1, and Kd = 0.05, what is the printed output?Solution
Step 1: Calculate error, integral, and derivative
error = 10 - 7 = 3
integral = 5 + 3 * 1 = 8
derivative = (3 - 2) / 1 = 1Step 2: Calculate output using PID formula
output = 1 * 3 + 0.1 * 8 + 0.05 * 1 = 3 + 0.8 + 0.05 = 3.85
print(round(3.85, 2)) prints 3.85Final Answer:
3.85 -> Option CQuick Check:
PID output = 3.85 [OK]
- Forgetting to update integral before output
- Mixing up last_error and error in derivative
- Rounding errors or ignoring dt
integral = 0 dt = 0.1 error = target - current integral += error output = Kp * error + Ki * integral * dt + Kd * (error - last_error) / dt
Solution
Step 1: Check integral update
Integral should accumulate error over time, so multiply error by dt when adding: integral += error * dt.Step 2: Verify output formula
Ki term multiplies integral, which already includes dt, so no extra dt needed in output.Final Answer:
Multiply integral by dt before adding error: integral += error * dt -> Option AQuick Check:
Integral update needs dt multiplication [OK]
- Adding error directly to integral without dt
- Multiplying integral by dt twice
- Changing derivative term incorrectly
Solution
Step 1: Understand tuning effects
Kp controls speed of response, Ki removes steady error, Kd reduces overshoot by damping.Step 2: Choose tuning to speed up and reduce overshoot
Increasing Kd helps reduce overshoot, moderate Kp keeps response fast but stable, reducing Ki avoids windup.Final Answer:
Increase Kd, keep Kp moderate, reduce Ki -> Option BQuick Check:
More Kd reduces overshoot, moderate Kp speeds response [OK]
- Increasing Kp too much causing oscillations
- Ignoring Kd's role in damping
- Setting all constants to zero initially
Ready to prove it?
Pass the test and this pattern moves unknown
