Principle:CARLA simulator Carla PID Vehicle Control
Overview
PID Vehicle Control in CARLA implements a dual Proportional-Integral-Derivative (PID) controller architecture for autonomous vehicle actuation. A longitudinal PID controller regulates vehicle speed by computing throttle and brake commands, while a lateral PID controller steers the vehicle to follow a target waypoint. Together they produce a complete carla.VehicleControl command at each simulation step.
Description
The CARLA VehiclePIDController decomposes vehicle control into two independent feedback loops:
Longitudinal Control (Speed):
- Error signal: difference between target speed and current vehicle speed
- Output: throttle value [0, 1] when positive, brake value [0, 1] when negative
- Maintains a sliding window of past errors for integral and derivative terms
Lateral Control (Steering):
- Error signal: angular deviation between the vehicle's forward vector and the direction to the target waypoint
- Output: steering value [-1, 1] where negative is left and positive is right
- Uses the same PID formulation with its own gain parameters
Both controllers are updated every simulation tick, and their outputs are combined into a single carla.VehicleControl message.
Usage
Use PID vehicle control when you need to:
- Convert high-level waypoint tracking into low-level vehicle actuation
- Implement a simple, well-understood control baseline for autonomous driving
- Tune vehicle response characteristics via gain parameters without modifying the control structure
- Prototype driving behaviors before moving to more advanced controllers (e.g., MPC, Stanley, Pure Pursuit)
Theoretical Basis
PID Control Law
The PID controller computes a control output u(t) based on the error e(t) between the desired setpoint and the measured process variable:
u(t) = K_P * e(t) + K_I * integral(e(tau), 0, t) + K_D * de(t)/dt
where:
- K_P (Proportional gain): Produces output proportional to the current error. Higher K_P increases responsiveness but can cause oscillation.
- K_I (Integral gain): Produces output proportional to the accumulated error over time. Eliminates steady-state error but can cause overshoot and integral windup.
- K_D (Derivative gain): Produces output proportional to the rate of change of error. Dampens oscillations and improves stability but amplifies noise.
Discrete-Time Implementation
In the CARLA controller, the continuous PID law is approximated in discrete time with a fixed time step dt:
e[k] = setpoint - measurement # Proportional term P = K_P * e[k] # Integral term (trapezoidal approximation over error buffer) I = K_I * dt * sum(error_buffer) # Derivative term (backward difference) D = K_D * (e[k] - e[k-1]) / dt u[k] = P + I + D
The error buffer is a fixed-size deque (default size: 10) that stores recent error values for integral accumulation and derivative estimation.
Longitudinal Controller
e_speed = target_speed - current_speed (km/h)
u = PID(e_speed, K_P_longitudinal, K_I_longitudinal, K_D_longitudinal)
if u >= 0:
throttle = min(u, max_throttle) # Clamp to [0, max_throttle]
brake = 0.0
else:
throttle = 0.0
brake = min(|u|, max_brake) # Clamp to [0, max_brake]
Lateral Controller
v_forward = vehicle.get_transform().get_forward_vector() # Vehicle heading v_target = waypoint.location - vehicle.location # Direction to target # Compute signed angle between vectors (cross product gives sign) angle = arctan2(cross(v_forward, v_target).z, dot(v_forward, v_target)) e_steering = angle u = PID(e_steering, K_P_lateral, K_I_lateral, K_D_lateral) steering = clamp(u, -max_steering, max_steering)
Default Gain Parameters
| Controller | K_P | K_I | K_D | dt |
|---|---|---|---|---|
| Longitudinal | 1.0 | 0.05 | 0.0 | 0.03 |
| Lateral | 1.95 | 0.05 | 0.2 | 0.03 |
These defaults are tuned for moderate driving speeds (20-40 km/h). Higher speeds or different vehicle dynamics may require re-tuning.
Stability Considerations
- Integral windup: The fixed-size error buffer implicitly limits integral accumulation, providing a basic anti-windup mechanism.
- Derivative kick: The derivative term operates on the error signal directly. Sudden setpoint changes (e.g., new target waypoint) can cause spikes. The short buffer length mitigates this.
- Sampling rate: The controller assumes a consistent simulation tick rate. Variable tick rates can degrade performance; CARLA's synchronous mode is recommended.