PID (Proportional-Integral-Derivative) control is a more advanced method of closed-loop control that allows for smoother and more accurate adjustments compared to on-off (bang-bang) control. While the math behind PID can be complex, we can understand its basic principles and see how it improves our robot's performance.
Cruise Control in Cars: Adjusts the throttle smoothly to maintain a constant speed.
Quadcopter Drones: Keeps the drone stable and level during flight, even in windy conditions.
Industrial Ovens: Maintains a precise temperature for consistent product quality.
Robot Arm in Manufacturing: Moves smoothly and accurately to perform precise tasks.
PID control uses three components to calculate how to adjust the system:
Responds to the current error.
Like turning your steering wheel more when you're further from the center of the lane.
Responds to accumulated error over time.
Like noticing you've been off-center for a while and making a bigger correction.
Responds to how fast the error is changing.
Like slowing down your steering when you're getting close to the center of the lane.
By combining these three responses, PID control can make smooth, accurate adjustments to keep the system at the desired state.
While we've simplified PID control above, it's helpful to understand that there is some underlying mathematics.
Here's a brief explanation of how PID control is calculated:
The PID control algorithm calculates an output value (u) based on the error (e) between the desired setpoint and the actual measured value. The formula for PID control is:
u(t) = Kp * e(t) + Ki * ∫ e(τ) dτ + Kd * de/dt
Where:
u(t) is the control output at time t
e(t) is the error at time t
Kp is the proportional gain
Ki is the integral gain
Kd is the derivative gain
∫ e(τ) dτ is the integral of the error over time
de/dt is the rate of change of the error
Let's break this down:
This term produces an output proportional to the current error.
A larger error results in a larger correction.
This term sums up the error over time.
It helps eliminate steady-state errors and can compensate for constant disturbances.
This term predicts future error based on its current rate of change.
It helps reduce overshoot and improves stability.
Use the following code:
from microbit import *
from maqueenplusv2 import *
init_maqueen()
# PID constants (adjust these)
Kp = 50 # Proportional gain
Ki = 10 # Integral gain
Kd = 10 # Derivative gain
# Other constants
BASE_SPEED = 50 # Base speed for the robot
dt = 0.01 # Time step (10 ms)
# Global variables for PID calculation
previous_error = 0
integral = 0
def read_line_error():
# Read the line sensors and calculate the error.
# Positive error means the line is to the right, negative means it's to the left.
left = read_line_sensor(L1)
right = read_line_sensor(R1)
# Calculate error based on the difference between sensors
# Normalize to a range of -1 to 1
error = (right - left) / 1023
return error
def calculate_pid(error):
# Calculate the PID output based on the error.
global previous_error, integral
# Proportional term
proportional = error
# Integral term
integral += error * dt
# Derivative term
derivative = (error - previous_error) / dt
# Calculate total PID output
output = Kp * proportional + Ki * integral + Kd * derivative
# Update previous_error for the next iteration
previous_error = error
return output * 1
def apply_motor_speeds(adjustment):
# Apply the calculated adjustment to the motor speeds.
#print(adjustment)
left_speed = BASE_SPEED - adjustment
right_speed = BASE_SPEED + adjustment
# Ensure speeds are within valid range (0-255)
left_speed = max(0, min(255, int(left_speed)))
right_speed = max(0, min(255, int(right_speed)))
#print("Left:" + str(left_speed))
#print("Right:" + str(right_speed))
# Set motor speeds
motors(left_speed, FORWARD, right_speed, FORWARD)
# Main loop
while True:
error = read_line_error()
adjustment = calculate_pid(error)
apply_motor_speeds(adjustment)
sleep(int(dt * 1000)) # Convert dt to milliseconds
Your goal is to tune the PID constants (Kp, Ki, Kd) to achieve smooth and fast line following with your Maqueen robot.
Visit the PID Trainer simulator: http://codinglab.blogspot.com/2016/04/online-pdi-trainer.html
Experiment with different values for Kp, Ki, and Kd. Observe how changing each constant affects the system's response.
Try to achieve the following:
a) A quick response with minimal overshoot
b) Stable behavior with no oscillations
c) Fast recovery from disturbances
Now, apply what you've learned to tune the PID constants for your Maqueen robot.
Start with the PID implementation we've developed:
# PID constants (you need to adjust these)
Kp = 0.1 # Proportional gain
Ki = 0.01 # Integral gain
Kd = 0.05 # Derivative gain
# Other constants
BASE_SPEED = 50 # Base speed for the robot
Hints:
Begin with low values for all constants (e.g., Kp = 0.1, Ki = 0, Kd = 0).
Gradually increase Kp until the robot starts to oscillate around the line.
Reduce Kp slightly, then slowly increase Kd to dampen the oscillations.
If the robot isn't centering perfectly on the line, gradually increase Ki.
Fine-tune all three constants to achieve smooth line following.
Once you have smooth following, try increasing the BASE_SPEED.
Adjust the PID constants as needed to maintain smooth performance at higher speeds.
Smoothness: How smoothly does the robot follow the line? (Minimal zig-zagging)
Speed: How fast can your robot follow the line while maintaining accuracy?
📖 In your workbook, compare and contrast the bang-bang algorithm with PID control. In your answer:
Explain how each method works.
Describe the strengths and weaknesses of each approach.
Provide example use-cases where each method would be appropriate.