Back to Collision Avoidance

Differential-Drive Line Follower with PID Tracking and Reactive Avoidance

Closed-Loop Simulation · IR Array Sensing · Reactive Layer

Abstract

A differential-drive robot tracks a curving line under a 5-element infrared reflectance array, with PID control on the lateral line-position error and a reactive forward-arc proximity layer that overrides the tracking controller when an obstacle enters the cone. Nine reproducible environments exercise both the tracking and the avoidance layers. Telemetry plots accompany each run.

Robot Model

Differential drive with wheelbase L = 0.16 m, integrated by forward Euler at dt = 0.05 s (20 Hz). Wheel speeds saturate at ±0.40 m/s; the nominal forward speed is 0.20 m/s. The kinematics:

v   = (v_L + v_R) / 2
ω   = (v_R − v_L) / L
ẋ   = v cos(θ)
ẏ   = v sin(θ)
θ̇   = ω

Sensors

IR reflectance array (5 elements) at lateral offsets [−20, −10, 0, +10, +20] mm relative to the robot center, perpendicular to heading. Each element reports a hit when the line passes within ±5 mm of it. The lateral line-position estimate is the mean offset of the elements that hit, with N(0, 1 mm) noise. If no element sees the line, the controller holds the previous estimate and decays the integral term toward zero.

Forward-arc proximity sensor: cone of ±30° centered on heading, range 0.50 m, returning the closest obstacle inside the cone with N(0, 5 mm) noise. The signed bearing tells the avoidance layer which way to steer.

Controller

PID on the lateral line-position error, expressed as the robot's offset from the line:

ω_PID = −( Kp · e  +  Ki · ∫e dt  +  Kd · de/dt )

Kp = 12.0,   Ki = 0.5,   Kd = 1.5

The integral is clamped to ±0.5 m·s to prevent windup during sustained line losses.

The reactive avoidance layer overrides the PID when the proximity sensor reads below 0.25 m. The avoidance yaw rate is

ω_avoid = −sign(bearing) · K_avoid · max(0, 1 − d / 0.25 m)

K_avoid = 4.0 rad/s

The two yaw-rate commands are blended linearly with weight urgency = 1 − d / 0.25 m, so the controller hands smoothly from line tracking to avoidance and back as obstacles enter and leave the cone.

Headline Results

Env Steps Peak |line error| Avoidance
148821.21 mmfired
229621.70 mmfired
329521.93 mmfired
431222.34 mmfired
531420.87 mmfired
629922.01 mmfired
733421.92 mmfired
835422.14 mmidle
932520.49 mmfired

The peak errors cluster near 21 mm because that is the geometric limit of the IR array — only the outermost element (at 20 mm offset) detects the line when the robot is laterally displaced beyond ±20 mm. Pushing convergence below 5 mm steady-state would require a wider array or a continuous reflectance camera, which is the design tradeoff this hardware abstraction makes explicit. Avoidance fired in 8 of 9 environments; environment 8's obstacle layout left no obstacle inside the forward cone for long enough to trigger.

Per-Environment Animations

Environment 1

Animated robot trajectory in environment 1 showing line tracking and obstacle avoidance

Environment 2

Animated robot trajectory in environment 2 showing line tracking and obstacle avoidance

Environment 3

Animated robot trajectory in environment 3 showing line tracking and obstacle avoidance

Environment 4

Animated robot trajectory in environment 4 showing line tracking and obstacle avoidance

Environment 5

Animated robot trajectory in environment 5 showing line tracking and obstacle avoidance

Environment 6

Animated robot trajectory in environment 6 showing line tracking and obstacle avoidance

Environment 7

Animated robot trajectory in environment 7 showing line tracking and obstacle avoidance

Environment 8

Animated robot trajectory in environment 8 showing line tracking and obstacle avoidance

Environment 9

Animated robot trajectory in environment 9 showing line tracking and obstacle avoidance

Telemetry

Three-panel telemetry per environment: line-position error (mm), left and right wheel speeds (m/s), and nearest-obstacle distance (m) versus time. The avoidance trigger threshold is drawn as a dashed line on the obstacle-distance panel.

Telemetry plot for environment 1: line error, wheel speeds, and obstacle distance over time

Environment 1 telemetry

Telemetry plot for environment 2: line error, wheel speeds, and obstacle distance over time

Environment 2 telemetry

Telemetry plot for environment 3: line error, wheel speeds, and obstacle distance over time

Environment 3 telemetry

Telemetry plot for environment 4: line error, wheel speeds, and obstacle distance over time

Environment 4 telemetry

Telemetry plot for environment 5: line error, wheel speeds, and obstacle distance over time

Environment 5 telemetry

Telemetry plot for environment 6: line error, wheel speeds, and obstacle distance over time

Environment 6 telemetry

Telemetry plot for environment 7: line error, wheel speeds, and obstacle distance over time

Environment 7 telemetry

Telemetry plot for environment 8: line error, wheel speeds, and obstacle distance over time

Environment 8 telemetry

Telemetry plot for environment 9: line error, wheel speeds, and obstacle distance over time

Environment 9 telemetry