PIDController¶
-
class
wpilib.
PIDController
(Kp, Ki, Kd, *args, **kwargs)[source]¶ Bases:
wpilib.SendableBase
Can be used to control devices via a PID Control Loop.
Creates a separate thread which reads the given
PIDSource
and takes care of the integral calculations, as well as writing the givenPIDOutput
.This feedback controller runs in discrete time, so time deltas are not used in the integral and derivative calculations. Therefore, the sample rate affects the controller’s behavior for a given set of PID constants.
Allocate a PID object with the given constants for P, I, D, and F
Arguments can be structured as follows:
- Kp, Ki, Kd, Kf, source, output, period
- Kp, Ki, Kd, source, output, period
- Kp, Ki, Kd, source, output
- Kp, Ki, Kd, Kf, source, output
Parameters: - Kp (float or int) – the proportional coefficient
- Ki (float or int) – the integral coefficient
- Kd (float or int) – the derivative coefficient
- Kf (float or int) – the feed forward term
- source (A function, or an object that implements
PIDSource
) – Called to get values - output (A function, or an object that implements
PIDOutput
) – Receives the output percentage - period (float or int) – the loop time for doing calculations. This particularly effects calculations of the integral and differential terms. The default is 50ms.
-
class
PIDSourceType
¶ Bases:
object
A description for the type of output value to provide to a
PIDController
-
kDisplacement
= 0¶
-
kRate
= 1¶
-
-
calculateFeedForward
()[source]¶ Calculate the feed forward term
Both of the provided feed forward calculations are velocity feed forwards. If a different feed forward calculation is desired, the user can override this function and provide his or her own. This function does no synchronization because the PIDController class only calls it in synchronized code, so be careful if calling it oneself.
If a velocity PID controller is being used, the F term should be set to 1 over the maximum setpoint for the output. If a position PID controller is being used, the F term should be set to 1 over the maximum speed for the output measured in setpoint units per this controller’s update period (see the default period in this class’s constructor).
-
get
()[source]¶ Return the current PID result. This is always centered on zero and constrained the the max and min outs.
Returns: the latest calculated output
-
getAvgError
()[source]¶ Returns the current difference of the error over the past few iterations. You can specify the number of iterations to average with setToleranceBuffer() (defaults to 1). getAvgError() is used for the onTarget() function.
Deprecated since version 2018.0.0: Use getError(), which is now already filtered.
Returns: the current average of the error
-
getContinuousError
(error)[source]¶ Wraps error around for continuous inputs. The original error is returned if continuous mode is disabled. This is an unsynchronized function.
Parameters: error – The current error of the PID controller. Returns: Error for continuous inputs.
-
getDeltaSetpoint
()[source]¶ Returns the change in setpoint over time of the PIDController
Returns: the change in setpoint over time
-
getError
()[source]¶ Returns the current difference of the input from the setpoint.
Returns: the current error
-
getPIDSourceType
()[source]¶ Returns the type of input the PID controller is using
Returns: the PID controller input type
-
getSetpoint
()[source]¶ Returns the current setpoint of the PIDController.
Returns: the current setpoint
-
instances
= 0¶
-
kDefaultPeriod
= 0.05¶
-
onTarget
()[source]¶ Return True if the error is within the percentage of the total input range, determined by setTolerance. This assumes that the maximum and minimum input were set using
setInput()
.Returns: True if the error is less than the tolerance
-
setAbsoluteTolerance
(absvalue)[source]¶ Set the absolute error which is considered tolerable for use with
onTarget()
.Parameters: absvalue – absolute error which is tolerable in the units of the input object
-
setContinuous
(continuous=True)[source]¶ Set the PID controller to consider the input to be continuous. Rather then using the max and min in as constraints, it considers them to be the same point and automatically calculates the shortest route to the setpoint.
Parameters: continuous – Set to True turns on continuous, False turns off continuous
-
setD
(d)[source]¶ Set the Differential coefficient of the PID controller gain.
Parameters: d – differential coefficient
-
setF
(f)[source]¶ Set the Feed forward coefficient of the PID controller gain.
Parameters: f – feed forward coefficient
-
setI
(i)[source]¶ Set the Integral coefficient of the PID controller gain.
Parameters: i – Integral coefficient
-
setInputRange
(minimumInput, maximumInput)[source]¶ Sets the maximum and minimum values expected from the input.
Parameters: - minimumInput – the minimum percentage expected from the input
- maximumInput – the maximum percentage expected from the output
-
setOutputRange
(minimumOutput, maximumOutput)[source]¶ Sets the minimum and maximum values to write.
Parameters: - minimumOutput – the minimum percentage to write to the output
- maximumOutput – the maximum percentage to write to the output
-
setP
(p)[source]¶ Set the Proportional coefficient of the PID controller gain.
Parameters: p – Proportional coefficient
-
setPID
(p, i, d, f=0.0)[source]¶ Set the PID Controller gain parameters. Set the proportional, integral, and differential coefficients.
Parameters: - p – Proportional coefficient
- i – Integral coefficient
- d – Differential coefficient
- f – Feed forward coefficient (optional, default is 0.0)
-
setPIDSourceType
(pidSourceType)[source]¶ Sets what type of input the PID controller will use
Parameters: pidSourceType – the type of input
-
setPercentTolerance
(percentage)[source]¶ Set the percentage error which is considered tolerable for use with
onTarget()
. (Input of 15.0 = 15 percent)Parameters: percentage – percent error which is tolerable
-
setSetpoint
(setpoint)[source]¶ Set the setpoint for the PIDController.
Parameters: setpoint – the desired setpoint
-
setToleranceBuffer
(bufLength)[source]¶ Set the number of previous error samples to average for tolerancing. When determining whether a mechanism is on target, the user may want to use a rolling average of previous measurements instead of a precise position or velocity. This is useful for noisy sensors which return a few erroneous measurements when the mechanism is on target. However, the mechanism will not register as on target for at least the specified bufLength cycles.
Parameters: bufLength (int) – Number of previous cycles to average. Deprecated since version 2018.0.0: Use a LinearDigitalFilter as the input