Class PidController
java.lang.Object
com.crossbotics.ftchelper.control.PidController
A PID controller: it works out how much power to give so a value reaches a target.
PID stands for Proportional, Integral, Derivative:
- P pushes harder when you are far from the target.
- I adds up small errors that last a long time (for example gravity on a lift).
- D slows the push when you move toward the target fast, so you do not overshoot.
- F (feedforward) is a fixed extra push, for example to hold an arm up.
The "error" is how far you still have to go: target minus where you are now. The "gains" (kP, kI, kD, kF) are the numbers you tune: how strongly each part pushes.
PidController liftPid = new PidController(0.005, 0, 0.0002)
.setOutputLimits(-0.6, 1.0)
.setTolerance(15);
// every loop:
double power = liftPid.calculate(targetTicks, lift.getCurrentPosition());
lift.setPower(power);
if (liftPid.atTarget()) { ... }
Tuning steps are in docs/modules/control.md. Tutorial 07 (docs/tutorials/07-pid-lift.md)
explains PID step by step on a lift.-
Constructor Summary
ConstructorsConstructorDescriptionPidController(double kP, double kI, double kD) Makes a controller with no feedforward.PidController(double kP, double kI, double kD, double kF) Makes a controller with a fixed feedforward push.PidController(double kP, double kI, double kD, double kF, LongSupplier clockNanos) Makes a controller with your own clock. -
Method Summary
Modifier and TypeMethodDescriptionbooleanatTarget()True if the last error was within the tolerance.doublecalculate(double target, double measured) Works out the output.doublegetKD()The D gain now in use.doublegetKF()The feedforward (F) now in use.doublegetKI()The I gain now in use.doublegetKP()The P gain now in use.doubleThe error from the lastcalculate(double, double)call (target minus measured).voidreset()Clears the memory (I and D).setAngleMode(boolean degrees) Turn on for headings in degrees.setGains(double kP, double kI, double kD, double kF) Changes the gains (for live tuning).setIntegralLimit(double maxIntegralOutput) Limits how big the I part can grow.setOutputLimits(double min, double max) Limits the output.setTolerance(double tolerance) How close counts as "at target" foratTarget().
-
Constructor Details
-
PidController
public PidController(double kP, double kI, double kD) Makes a controller with no feedforward.- Parameters:
kP- proportional gain (start small, e.g. 0.01)kI- integral gain (start at 0)kD- derivative gain (start at 0)
-
PidController
public PidController(double kP, double kI, double kD, double kF) Makes a controller with a fixed feedforward push.- Parameters:
kP- proportional gainkI- integral gainkD- derivative gainkF- power added every time (same sign always), e.g. 0.1 to hold up a lift
-
PidController
Makes a controller with your own clock. Used by tests.- Parameters:
clockNanos- gives the current time in nanoseconds
-
-
Method Details
-
setGains
Changes the gains (for live tuning). Does not reset the controller.- Returns:
- this controller, so you can chain more settings
-
setOutputLimits
Limits the output. Default -1 to 1 (motor power range). Example:setOutputLimits(-0.5, 1)lets a lift go down at half power only.- Parameters:
min- the lowest output (power), e.g. -1max- the highest output (power), e.g. 1- Returns:
- this controller, so you can chain more settings
-
setIntegralLimit
Limits how big the I part can grow. Stops "integral windup": a big build-up that makes the mechanism overshoot after it was stuck. Default: no limit.- Parameters:
maxIntegralOutput- the largest power the I part can add, e.g. 0.3- Returns:
- this controller, so you can chain more settings
-
setTolerance
How close counts as "at target" foratTarget(). Same units as your measurement.- Parameters:
tolerance- for example 15 (encoder ticks) or 2 (degrees)- Returns:
- this controller, so you can chain more settings
-
setAngleMode
Turn on for headings in degrees. The error then takes the short way around the circle (from 170 to -170 is +20, not -340).- Parameters:
degrees- true for headings in degrees, false for normal values (the default)- Returns:
- this controller, so you can chain more settings
-
calculate
public double calculate(double target, double measured) Works out the output. Call once per loop.- Parameters:
target- where you want to bemeasured- where you are now- Returns:
- the power to apply, inside the output limits
-
getLastError
public double getLastError()The error from the lastcalculate(double, double)call (target minus measured). NaN before the first call. -
atTarget
public boolean atTarget()True if the last error was within the tolerance. False before the first call. -
reset
public void reset()Clears the memory (I and D). Call when you set a new target or start the mechanism again. Without this, the I total built up for the old target would push the mechanism the wrong way. -
getKP
public double getKP()The P gain now in use. -
getKI
public double getKI()The I gain now in use. -
getKD
public double getKD()The D gain now in use. -
getKF
public double getKF()The feedforward (F) now in use.
-