WPILibC++  2020.3.2-60-g3011ebe
frc::PIDBase Class Reference

Class implements a PID Control Loop. More...

#include <PIDBase.h>

Inheritance diagram for frc::PIDBase:

## Public Member Functions

PIDBase (double p, double i, double d, PIDSource &source, PIDOutput &output)
Allocate a PID object with the given constants for P, I, D. More...

PIDBase (double p, double i, double d, double f, PIDSource &source, PIDOutput &output)
Allocate a PID object with the given constants for P, I, D. More...

virtual double Get () const
Return the current PID result. More...

virtual void SetContinuous (bool continuous=true)
Set the PID controller to consider the input to be continuous,. More...

virtual void SetInputRange (double minimumInput, double maximumInput)
Sets the maximum and minimum values expected from the input. More...

virtual void SetOutputRange (double minimumOutput, double maximumOutput)
Sets the minimum and maximum values to write. More...

void SetPID (double p, double i, double d) override
Set the PID Controller gain parameters. More...

virtual void SetPID (double p, double i, double d, double f)
Set the PID Controller gain parameters. More...

void SetP (double p)
Set the Proportional coefficient of the PID controller gain. More...

void SetI (double i)
Set the Integral coefficient of the PID controller gain. More...

void SetD (double d)
Set the Differential coefficient of the PID controller gain. More...

void SetF (double f)
Get the Feed forward coefficient of the PID controller gain. More...

double GetP () const override
Get the Proportional coefficient. More...

double GetI () const override
Get the Integral coefficient. More...

double GetD () const override
Get the Differential coefficient. More...

virtual double GetF () const
Get the Feed forward coefficient. More...

void SetSetpoint (double setpoint) override
Set the setpoint for the PIDBase. More...

double GetSetpoint () const override
Returns the current setpoint of the PIDBase. More...

double GetDeltaSetpoint () const
Returns the change in setpoint over time of the PIDBase. More...

virtual double GetError () const
Returns the current difference of the input from the setpoint. More...

virtual double GetAvgError () const
Returns the current average of the error over the past few iterations. More...

virtual void SetPIDSourceType (PIDSourceType pidSource)
Sets what type of input the PID controller will use.

virtual PIDSourceType GetPIDSourceType () const
Returns the type of input the PID controller is using. More...

virtual void SetTolerance (double percent)
Set the percentage error which is considered tolerable for use with OnTarget. More...

virtual void SetAbsoluteTolerance (double absValue)
Set the absolute error which is considered tolerable for use with OnTarget. More...

virtual void SetPercentTolerance (double percentValue)
Set the percentage error which is considered tolerable for use with OnTarget. More...

virtual void SetToleranceBuffer (int buf=1)
Set the number of previous error samples to average for tolerancing. More...

virtual bool OnTarget () const
Return true if the error is within the percentage of the total input range, determined by SetTolerance. More...

void Reset () override
Reset the previous error, the integral term, and disable the controller.

void PIDWrite (double output) override
Passes the output directly to SetSetpoint(). More...

void InitSendable (SendableBuilder &builder) override
Initializes this Sendable object. More...

Public Member Functions inherited from frc::PIDInterface
PIDInterface (PIDInterface &&)=default

PIDInterfaceoperator= (PIDInterface &&)=default

Public Member Functions inherited from frc::SendableHelper< PIDBase >
SendableHelper (const SendableHelper &rhs)=default

SendableHelper (SendableHelper &&rhs)

SendableHelperoperator= (const SendableHelper &rhs)=default

SendableHelperoperator= (SendableHelper &&rhs)

std::string GetName () const
Gets the name of this Sendable object. More...

void SetName (const wpi::Twine &name)
Sets the name of this Sendable object. More...

void SetName (const wpi::Twine &subsystem, const wpi::Twine &name)
Sets both the subsystem name and device name of this Sendable object. More...

std::string GetSubsystem () const
Gets the subsystem name of this Sendable object. More...

void SetSubsystem (const wpi::Twine &subsystem)
Sets the subsystem name of this Sendable object. More...

## Protected Member Functions

virtual void Calculate ()
Read the input, calculate the output accordingly, and write to the output. More...

virtual double CalculateFeedForward ()
Calculate the feed forward term. More...

double GetContinuousError (double error) const
Wraps error around for continuous inputs. More...

Protected Member Functions inherited from frc::SendableHelper< PIDBase >
void SetName (const wpi::Twine &moduleType, int channel)
Sets the name of the sensor with a channel number. More...

void SetName (const wpi::Twine &moduleType, int moduleNumber, int channel)
Sets the name of the sensor with a module and channel number. More...

void AddChild (std::shared_ptr< Sendable > child)

## Protected Attributes

bool m_enabled = false

wpi::mutex m_thisMutex

wpi::mutex m_pidWriteMutex

PIDSourcem_pidInput

PIDOutputm_pidOutput

Timer m_setpointTimer

## Detailed Description

Class implements 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 given PIDOutput.

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.

Deprecated:
All APIs which use this have been deprecated.

## ◆ PIDBase() [1/2]

 frc::PIDBase::PIDBase ( double p, double i, double d, PIDSource & source, PIDOutput & output )

Allocate a PID object with the given constants for P, I, D.

Parameters
 Kp the proportional coefficient Ki the integral coefficient Kd the derivative coefficient source The PIDSource object that is used to get values output The PIDOutput object that is set to the output value

## ◆ PIDBase() [2/2]

 frc::PIDBase::PIDBase ( double p, double i, double d, double f, PIDSource & source, PIDOutput & output )

Allocate a PID object with the given constants for P, I, D.

Parameters
 Kp the proportional coefficient Ki the integral coefficient Kd the derivative coefficient source The PIDSource object that is used to get values output The PIDOutput object that is set to the output value

## ◆ Calculate()

 virtual void frc::PIDBase::Calculate ( )
protectedvirtual

Read the input, calculate the output accordingly, and write to the output.

This should only be called by the Notifier.

## ◆ CalculateFeedForward()

 virtual double frc::PIDBase::CalculateFeedForward ( )
protectedvirtual

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 PIDBase 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()

 virtual double frc::PIDBase::Get ( ) const
virtual

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()

 virtual double frc::PIDBase::GetAvgError ( ) const
virtual

Returns the current average of the error over the past few iterations.

You can specify the number of iterations to average with SetToleranceBuffer() (defaults to 1). This is the same value that is used for OnTarget().

Returns
the average error

## ◆ GetContinuousError()

 double frc::PIDBase::GetContinuousError ( double error ) const
protected

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.

## ◆ GetD()

 double frc::PIDBase::GetD ( ) const
overridevirtual

Get the Differential coefficient.

Returns
differential coefficient

Implements frc::PIDInterface.

## ◆ GetDeltaSetpoint()

 double frc::PIDBase::GetDeltaSetpoint ( ) const

Returns the change in setpoint over time of the PIDBase.

Returns
the change in setpoint over time

## ◆ GetError()

 virtual double frc::PIDBase::GetError ( ) const
virtual

Returns the current difference of the input from the setpoint.

Returns
the current error

## ◆ GetF()

 virtual double frc::PIDBase::GetF ( ) const
virtual

Get the Feed forward coefficient.

Returns
Feed forward coefficient

## ◆ GetI()

 double frc::PIDBase::GetI ( ) const
overridevirtual

Get the Integral coefficient.

Returns
integral coefficient

Implements frc::PIDInterface.

## ◆ GetP()

 double frc::PIDBase::GetP ( ) const
overridevirtual

Get the Proportional coefficient.

Returns
proportional coefficient

Implements frc::PIDInterface.

## ◆ GetPIDSourceType()

 virtual PIDSourceType frc::PIDBase::GetPIDSourceType ( ) const
virtual

Returns the type of input the PID controller is using.

Returns
the PID controller input type

## ◆ GetSetpoint()

 double frc::PIDBase::GetSetpoint ( ) const
overridevirtual

Returns the current setpoint of the PIDBase.

Returns
the current setpoint

Implements frc::PIDInterface.

## ◆ InitSendable()

 void frc::PIDBase::InitSendable ( SendableBuilder & builder )
overridevirtual

Initializes this Sendable object.

Parameters
 builder sendable builder

Implements frc::Sendable.

## ◆ OnTarget()

 virtual bool frc::PIDBase::OnTarget ( ) const
virtual

Return true if the error is within the percentage of the total input range, determined by SetTolerance.

This asssumes that the maximum and minimum input were set using SetInput.

Currently this just reports on target as the actual value passes through the setpoint. Ideally it should be based on being within the tolerance for some period of time.

This will return false until at least one input value has been computed.

## ◆ PIDWrite()

 void frc::PIDBase::PIDWrite ( double output )
overridevirtual

Passes the output directly to SetSetpoint().

PIDControllers can be nested by passing a PIDController as another PIDController's output. In that case, the output of the parent controller becomes the input (i.e., the reference) of the child.

It is the caller's responsibility to put the data into a valid form for SetSetpoint().

Implements frc::PIDOutput.

## ◆ SetAbsoluteTolerance()

 virtual void frc::PIDBase::SetAbsoluteTolerance ( double absValue )
virtual

Set the absolute error which is considered tolerable for use with OnTarget.

Parameters
 percentage error which is tolerable

## ◆ SetContinuous()

 virtual void frc::PIDBase::SetContinuous ( bool continuous = true )
virtual

Set the PID controller to consider the input to be continuous,.

Rather then using the max and min input range as constraints, it considers them to be the same point and automatically calculates the shortest route to the setpoint.

Parameters
 continuous true turns on continuous, false turns off continuous

## ◆ SetD()

 void frc::PIDBase::SetD ( double d )

Set the Differential coefficient of the PID controller gain.

Parameters
 d differential coefficient

## ◆ SetF()

 void frc::PIDBase::SetF ( double f )

Get the Feed forward coefficient of the PID controller gain.

Parameters
 f Feed forward coefficient

## ◆ SetI()

 void frc::PIDBase::SetI ( double i )

Set the Integral coefficient of the PID controller gain.

Parameters
 i integral coefficient

## ◆ SetInputRange()

 virtual void frc::PIDBase::SetInputRange ( double minimumInput, double maximumInput )
virtual

Sets the maximum and minimum values expected from the input.

Parameters
 minimumInput the minimum value expected from the input maximumInput the maximum value expected from the output

## ◆ SetOutputRange()

 virtual void frc::PIDBase::SetOutputRange ( double minimumOutput, double maximumOutput )
virtual

Sets the minimum and maximum values to write.

Parameters
 minimumOutput the minimum value to write to the output maximumOutput the maximum value to write to the output

## ◆ SetP()

 void frc::PIDBase::SetP ( double p )

Set the Proportional coefficient of the PID controller gain.

Parameters
 p proportional coefficient

## ◆ SetPercentTolerance()

 virtual void frc::PIDBase::SetPercentTolerance ( double percentValue )
virtual

Set the percentage error which is considered tolerable for use with OnTarget.

Parameters
 percentage error which is tolerable

## ◆ SetPID() [1/2]

 void frc::PIDBase::SetPID ( double p, double i, double d )
overridevirtual

Set the PID Controller gain parameters.

Set the proportional, integral, and differential coefficients.

Parameters
 p Proportional coefficient i Integral coefficient d Differential coefficient

Implements frc::PIDInterface.

## ◆ SetPID() [2/2]

 virtual void frc::PIDBase::SetPID ( double p, double i, double d, double f )
virtual

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

## ◆ SetSetpoint()

 void frc::PIDBase::SetSetpoint ( double setpoint )
overridevirtual

Set the setpoint for the PIDBase.

Parameters
 setpoint the desired setpoint

Implements frc::PIDInterface.

## ◆ SetTolerance()

 virtual void frc::PIDBase::SetTolerance ( double percent )
virtual

Set the percentage error which is considered tolerable for use with OnTarget.

Parameters
 percentage error which is tolerable

## ◆ SetToleranceBuffer()

 virtual void frc::PIDBase::SetToleranceBuffer ( int buf = 1 )
virtual

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 Number of previous cycles to average. Defaults to 1.

The documentation for this class was generated from the following file:
• /__w/1/s/wpilibc/src/main/native/include/frc/PIDBase.h