PIDController

The PID Controller class provides proportional-integral-derivative control for robotics applications on the Echo microcontroller. PID control is a feedback mechanism that helps your robot reach and maintain a target value smoothly and accurately.

What is PID Control?

PID control is a method for automatically adjusting a system to reach a desired target. Think of it like a smart thermostat for your robot.

Simple analogy: Imagine you're trying to keep a ball balanced on a platform. You need to tilt the platform just the right amount:

  • Too little tilt (P): The ball moves slowly toward center

  • Add memory (I): Remember if you've been off-center too long and correct harder

  • Predict motion (D): See how fast the ball is rolling and adjust before it overshoots

The Three Components

P - Proportional: Reacts to current error

  • "How far am I from the target right now?"

  • Larger errors produce larger corrections

  • Like pressing the gas pedal harder when you're further from your destination

I - Integral: Reacts to accumulated error over time

  • "Have I been consistently off-target?"

  • Fixes persistent small errors that P alone can't eliminate

  • Like compensating for a constant uphill slope

D - Derivative: Reacts to rate of change

  • "How fast am I approaching the target?"

  • Slows down to prevent overshooting

  • Like applying brakes as you approach your destination


Key Features

  • Automatic target tracking with configurable tolerance

  • Built-in integral windup protection

  • Time-based calculation for consistent behavior

  • Zero output when within target tolerance

  • Pause and resume functionality

  • Easy parameter tuning


Overview

The PID Controller class implements a proportional-integral-derivative feedback control system for precise control of sensors and actuators.

Method
Description

PIDController(P, I, D, target, maxTol, minTol)

Creates a PID controller with specified gains and target setpoint

getOutput(sensorValue)

Calculates and returns control output based on current sensor reading

pause()

Pauses the controller and resets all error accumulation

setNewTarget(target, maxTol, minTol)

Updates the target setpoint and tolerance range

setNewVals(P, I, D)

Updates the PID gain constants

setIlimit(limit)

Sets the maximum allowed integral accumulation

isOnTarget()

Returns true if sensor value is within tolerance range


Quick Start


API Reference

PIDController(float P, float I, float D, float target, float maxTolerance, float minTolerance)

Creates a PID controller with specified gains and target parameters.

Parameters:

  • P: Proportional gain (typically 0.1 - 10.0)

  • I: Integral gain (typically 0.0 - 1.0)

  • D: Derivative gain (typically 0.0 - 1.0)

  • target: Desired setpoint value

  • maxTolerance: Upper tolerance from target (considered "on target")

  • minTolerance: Lower tolerance from target (considered "on target")

Example:


getOutput(float sensorValue)

Calculates the PID output based on the current sensor reading.

Parameters:

  • sensorValue: Current measurement from sensor

Returns:

  • float: Control output (can be positive or negative)

  • Returns 0 when within tolerance range

Example:


setNewTarget(float target, float maxTolerance, float minTolerance)

Updates the target setpoint and tolerance values during operation.

Parameters:

  • target: New target value

  • maxTolerance: New upper tolerance

  • minTolerance: New lower tolerance

Example:


setNewVals(float P, float I, float D)

Updates the PID gain values during operation. Useful for adaptive control or switching between different tuning profiles.

Parameters:

  • P: New proportional gain

  • I: New integral gain

  • D: New derivative gain

Example:


setIlimit(float limit)

Sets the integral windup limit. The integral term only accumulates when error is less than this limit.

Parameters:

  • limit: Maximum error for integral accumulation

Example:

Note: This prevents the integral term from becoming excessively large when far from target.


isOnTarget()

Returns whether the current sensor value is within the acceptable tolerance range.

Returns:

  • true: Sensor value is within target ± tolerance

  • false: Sensor value is outside tolerance range

Example:


pause()

Pauses the PID controller and resets all accumulated values. Use this when temporarily disabling control.

Example:


Tuning Your PID Controller

Start with these steps to tune your PID gains:

Step 1: Set I and D to Zero

Adjust P until the system responds but may oscillate slightly.

Step 2: Add Integral

Increase I slowly to eliminate steady-state error. Too much will cause instability.

Step 3: Add Derivative

Add D to reduce overshoot and oscillations. Start small.

Quick Guidelines

Proportional (P):

  • Too low: Slow response, never reaches target

  • Too high: Oscillates around target

  • Start with: 1.0

Integral (I):

  • Too low: Persistent small error remains

  • Too high: Oscillations increase, instability

  • Start with: 0.1

Derivative (D):

  • Too low: Overshoots target

  • Too high: Sluggish, noise sensitive

  • Start with: 0.05


Best Practices

Set Appropriate Tolerances

Define a realistic tolerance range where "close enough" is acceptable:

Use Integral Limits

Prevent integral windup by setting a reasonable limit:

Constrain Output

Always constrain PID output to safe ranges:

Reset When Necessary

Pause the PID when stopping or changing modes:


Common Issues

Oscillation Around Target

Cause: P gain too high or D gain too low

Solution:

  • Reduce P gain by 20-30%

  • Slightly increase D gain

Never Reaches Target

Cause: P gain too low or persistent external force

Solution:

  • Increase P gain

  • Add or increase I gain to handle constant offset

Overshoot and Slow Recovery

Cause: I gain accumulating too much error

Solution:

  • Reduce I gain

  • Set a lower integral limit with setIlimit()

Unstable or Erratic Behavior

Cause: Gains too high or sensor noise

Solution:

  • Reduce all gains by 50% and re-tune

  • Filter sensor readings before feeding to PID


Technical Details

Update Rate

The PID controller uses time-based calculations, automatically adjusting for varying loop speeds. However, consistent update rates produce better results:

  • Recommended: 10-100ms update interval

  • Minimum: 1ms (for very fast systems)

  • Maximum: 1000ms (for slow systems)

Internal Timer

The PID controller uses the Timer class internally for accurate time-based calculations. You don't need to manage this timer yourself.

Zero Output Zone

When the sensor value is within the tolerance range (target ± tolerance), the PID automatically returns zero output to prevent unnecessary corrections.


Integration with Timer Class

The PID controller automatically uses the Timer class for time-based calculations. You can still use separate Timer instances for other timing needs in your code:

Last updated