File-By-File
PIDController

PIDController

A proportional, integral, derivative (PID) controller. This class extends AbstractController and provides a robust way to control a system by factoring in proportional, integral, and derivative components of the error.

Key Concepts

  • Proportional (P): Responds to the current error. A larger proportional gain (p) means a stronger response to the error.
  • Integral (I): Accounts for past errors, helping to eliminate steady-state errors. A larger integral gain (i) means a stronger response to accumulated error.
  • Derivative (D): Predicts future errors based on the rate of change of the current error, helping to dampen oscillations. A larger derivative gain (d) means a stronger response to the rate of change of error.
  • Feedforward (F): An optional component that provides an output based on the desired setpoint, without waiting for an error to occur. This can improve responsiveness.

Methods

PIDController(double p, double i, double d)

Constructs a new PIDController with specified proportional, integral, and derivative gains. The feedforward gain is defaulted to 0.02.

PIDController(double p, double i, double d, double f)

Constructs a new PIDController with specified proportional, integral, derivative, and feedforward gains.

calculate(double value)

Calculates the controller output based on the current value and the set target. The output is clipped to the controller's configured minimum and maximum output limits.

setP(double p)

Sets the proportional gain (p) of the PID controller. Returns this for method chaining.

setI(double i)

Sets the integral gain (i) of the PID controller. Returns this for method chaining.

setD(double d)

Sets the derivative gain (d) of the PID controller. Returns this for method chaining.

setF(double f)

Sets the feedforward gain (f) of the PID controller. Returns this for method chaining.

Inner Class: MiniPID

This is an internal static class that performs the core PID calculations. It is taken from an external project and is not written by the author of Pathfinder. For more details, refer to its source: https://github.com/tekdemo/MiniPID-Java/blob/master/src/com/stormbots/MiniPID.java (opens in a new tab)

MiniPID(double p, double i, double d)

Constructs a MiniPID instance with proportional, integral, and derivative gains.

MiniPID(double p, double i, double d, double f)

Constructs a MiniPID instance with proportional, integral, derivative, and feedforward gains.

setP(double p)

Sets the proportional gain.

setI(double i)

Sets the integral gain.

setDerivative(double derivative)

Sets the derivative gain.

setFeedForward(double feedForward)

Sets the feedforward gain.

setPID(double p, double i, double d)

Sets all PID gains.

setPID(double p, double i, double d, double f)

Sets all PID and feedforward gains.

setMaxIOutput(double maximum)

Sets the maximum output for the integral component.

setOutputLimits(double output)

Sets symmetric output limits.

setOutputLimits(double minimum, double maximum)

Sets asymmetric output limits.

setDirection(boolean reversed)

Sets whether the PID controller is reversed.

setSetpoint(double setpoint)

Sets the target setpoint for the controller.

getOutput(double actual, double setpoint)

Calculates the PID output based on the current actual value and the setpoint.

getOutput()

Calculates the PID output using the last actual value and the setpoint.

getOutput(double actual)

Calculates the PID output using the provided actual value and the setpoint.

reset()

Resets the internal state of the PID controller.

setOutputRampRate(double rate)

Sets the maximum rate of change for the output.

setSetpointRange(double range)

Sets the range around the setpoint within which the setpoint can be constrained.

setOutputFilter(double strength)

Sets the strength of the output filter.

constrain(double value, double min, double max)

Private helper method. Constrains a value within a given minimum and maximum.

bounded(double value, double min, double max)

Private helper method. Checks if a value is strictly between a minimum and maximum.

checkSigns()

Private helper method. Ensures the signs of the PID and feedforward gains are consistent with the controller's direction.