Programming lesson
Mastering PID Control and Wall Following with LIDAR in Webots
Learn to implement a PID controller for wall following and forward wall stop using LIDAR sensors in the Webots FAIRIS robot simulation. Step-by-step tutorial with Python code examples.
Introduction to PID Control and Wall Following
In robotics, navigating environments while maintaining a safe distance from walls is a fundamental skill. This tutorial guides you through implementing a PID controller for a FAIRIS Lite robot in Webots 2023b, using LIDAR sensor data to perform forward wall stop and wall following tasks. By the end, you'll understand how to tune PID gains for smooth, responsive motion—a skill applicable to autonomous vehicles, warehouse robots, and even AI-driven navigation in games like Mario Kart (where power-ups mimic PID adjustments).
Understanding the LIDAR Sensor
The FAIRIS Lite robot's LIDAR provides a 360-degree scan with 800 distance measurements. The array is indexed as follows: index 0 = behind, 200 = left, 400 = front, 600 = right. This is crucial for extracting the front and side distances needed for PID control. For example, robot.get_lidar_range_image()[400] gives the forward distance.
PID Controller Basics
A PID controller computes a control output u(t) based on the error e(t) = r(t) - y(t), where r(t) is the desired distance and y(t) is the current measurement. The three terms are:
- Proportional (P):
Kp * e(t)– reacts to current error. - Integral (I):
Ki * sum(e(t) * dt)– eliminates steady-state error. - Derivative (D):
Kd * (e(t) - e(t-1)) / dt– anticipates future error, reducing overshoot.
The output is saturated between cmin and cmax to limit motor speeds. In Webots, the time step dt is typically 0.032 seconds.
Task 1: Forward Wall Stop
Your robot must stop 1 meter from the front wall. Use the front LIDAR reading (index 400) as y(t) and set r(t) = 1.0. The PID output controls the forward velocity of both wheels equally. The robot should also reverse if placed closer than 1 meter. Experiment with gain values like 0.001, 0.01, 0.5, 1.0, 5.0, 10.0 for Kp, Ki, Kd separately. Optimal gains might be Kp=1.0, Ki=0.1, Kd=0.5 for a smooth stop. Below is a skeleton code:
import math
from controller import Robot
robot = Robot()
timestep = int(robot.getBasicTimeStep())
lidar = robot.getDevice('lidar')
lidar.enable(timestep)
motor_left = robot.getDevice('left wheel motor')
motor_right = robot.getDevice('right wheel motor')
motor_left.setPosition(float('inf'))
motor_right.setPosition(float('inf'))
Kp = 1.0
Ki = 0.1
Kd = 0.5
dt = 0.032
desired = 1.0
integral = 0.0
prev_error = 0.0
while robot.step(timestep) != -1:
ranges = lidar.getRangeImage()
front_distance = ranges[400]
error = desired - front_distance
integral += error * dt
derivative = (error - prev_error) / dt
u = Kp * error + Ki * integral + Kd * derivative
# Saturate u between -10 and 10
u = max(-10, min(10, u))
motor_left.setVelocity(u)
motor_right.setVelocity(u)
prev_error = error
print(f"Front: {front_distance:.2f}, Error: {error:.2f}")Test with the robot starting 6.5m away. If dragged closer than 1m, the negative error should cause negative velocity (reverse).
Task 2: Wall Following
In this task, the robot follows a wall (left or right) while avoiding obstacles. Use side LIDAR readings: left at index 200, right at index 600. The strategy is to maintain a constant distance to the wall (e.g., 0.5m) using a PID controller on the side distance, while moving forward. When a front wall is detected (front distance < threshold), rotate 90 degrees toward the wall you are following. For left wall following, rotate left (counterclockwise) to keep the left wall 'touching'.
Example code structure for left wall following:
# Inside main loop
left_dist = ranges[200]
front_dist = ranges[400]
error_side = desired_side - left_dist
# PID for side control
u_side = Kp_side * error_side + Ki_side * integral_side + Kd_side * derivative_side
# Base forward speed
base_speed = 5.0
# Adjust wheel velocities
motor_left.setVelocity(base_speed + u_side)
motor_right.setVelocity(base_speed - u_side)
# If wall ahead, rotate
if front_dist < 0.8:
# Rotate 90 degrees left
# Use a timer or gyro for precise angle
passTest on mazes maze3.xml and maze4.xml. Some maze/mode combinations may loop indefinitely—identify which ones.
Tips for Tuning PID Gains
- Start with
Kponly, increase until oscillation, then addKdto dampen. - Use small
Kito correct steady-state error without causing instability. - For wall following, side PID gains may differ from forward gains. Typical values:
Kp=0.5, Ki=0.01, Kd=0.2.
Real-World Connections
PID controllers are everywhere: from cruise control in cars to stabilizing drones like the DJI Phantom. In gaming, AI bots use similar algorithms to follow walls in Doom or Minecraft. Even in finance, PID concepts appear in algorithmic trading to maintain a portfolio's target allocation.
Conclusion
By completing this lab, you've gained hands-on experience with PID control and LIDAR-based navigation. These skills are foundational for autonomous robotics and are highly sought after in industries like self-driving cars and warehouse automation. Keep experimenting with gain values to see how they affect performance—it's the key to mastering PID.