Agent skill
pid-controller
Use this skill when implementing PID control loops for adaptive cruise control, vehicle speed regulation, throttle/brake management, or any feedback control system requiring proportional-integral-derivative control.
Install this agent skill to your Project
npx add-skill https://github.com/benchflow-ai/skillsbench/tree/main/tasks-no-skills/adaptive-cruise-control/environment/skills/pid-controller
SKILL.md
PID Controller Implementation
Overview
A PID (Proportional-Integral-Derivative) controller is a feedback control mechanism used in industrial control systems. It continuously calculates an error value and applies a correction based on proportional, integral, and derivative terms.
Control Law
output = Kp * error + Ki * integral(error) + Kd * derivative(error)
Where:
error= setpoint - measured_valueKp= proportional gain (reacts to current error)Ki= integral gain (reacts to accumulated error)Kd= derivative gain (reacts to rate of change)
Discrete-Time Implementation
class PIDController:
def __init__(self, kp, ki, kd, output_min=None, output_max=None):
self.kp = kp
self.ki = ki
self.kd = kd
self.output_min = output_min
self.output_max = output_max
self.integral = 0.0
self.prev_error = 0.0
def reset(self):
"""Clear controller state."""
self.integral = 0.0
self.prev_error = 0.0
def compute(self, error, dt):
"""Compute control output given error and timestep."""
# Proportional term
p_term = self.kp * error
# Integral term
self.integral += error * dt
i_term = self.ki * self.integral
# Derivative term
derivative = (error - self.prev_error) / dt if dt > 0 else 0.0
d_term = self.kd * derivative
self.prev_error = error
# Total output
output = p_term + i_term + d_term
# Output clamping (optional)
if self.output_min is not None:
output = max(output, self.output_min)
if self.output_max is not None:
output = min(output, self.output_max)
return output
Anti-Windup
Integral windup occurs when output saturates but integral keeps accumulating. Solutions:
- Clamping: Limit integral term magnitude
- Conditional Integration: Only integrate when not saturated
- Back-calculation: Reduce integral when output is clamped
Tuning Guidelines
Manual Tuning:
- Set Ki = Kd = 0
- Increase Kp until acceptable response speed
- Add Ki to eliminate steady-state error
- Add Kd to reduce overshoot
Effect of Each Gain:
- Higher Kp -> faster response, more overshoot
- Higher Ki -> eliminates steady-state error, can cause oscillation
- Higher Kd -> reduces overshoot, sensitive to noise
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
csv-processing
Use this skill when reading sensor data from CSV files, writing simulation results to CSV, processing time-series data with pandas, or handling missing values in datasets.
yaml-config
Use this skill when reading or writing YAML configuration files, loading vehicle parameters, or handling config file parsing with proper error handling.
simulation-metrics
Use this skill when calculating control system performance metrics such as rise time, overshoot percentage, steady-state error, or settling time for evaluating simulation results.
vehicle-dynamics
Use this skill when simulating vehicle motion, calculating safe following distances, time-to-collision, speed/position updates, or implementing vehicle state machines for cruise control modes.
web-interface-guidelines
Vercel's comprehensive UI guidelines for building accessible, performant web interfaces. Use this skill when reviewing or building UI components for compliance with best practices around accessibility, performance, animations, and visual stability.
browser-testing
VERIFY your changes work. Measure CLS, detect theme flicker, test visual stability, check performance. Use BEFORE and AFTER making changes to confirm fixes. Includes ready-to-run scripts: measure-cls.ts, detect-flicker.ts
Didn't find tool you were looking for?