WPILibC++  2019.3.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
frc::PIDBase Class Reference

Class implements a PID Control Loop. More...

#include <PIDBase.h>

Inheritance diagram for frc::PIDBase:
frc::SendableBase frc::PIDInterface frc::PIDOutput frc::Sendable frc::PIDController

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...
 
 PIDBase (PIDBase &&)=default
 
PIDBaseoperator= (PIDBase &&)=default
 
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::SendableBase
 SendableBase (bool addLiveWindow=true)
 Creates an instance of the sensor base. More...
 
 SendableBase (SendableBase &&rhs)
 
SendableBaseoperator= (SendableBase &&rhs)
 
std::string GetName () const final
 Gets the name of this Sendable object. More...
 
void SetName (const wpi::Twine &name) final
 Sets the name of this Sendable object. More...
 
std::string GetSubsystem () const final
 Gets the subsystem name of this Sendable object. More...
 
void SetSubsystem (const wpi::Twine &subsystem) final
 Sets the subsystem name of this Sendable object. More...
 
- Public Member Functions inherited from frc::Sendable
 Sendable (Sendable &&)=default
 
Sendableoperator= (Sendable &&)=default
 
void SetName (const wpi::Twine &subsystem, const wpi::Twine &name)
 Sets both the subsystem name and device name of this Sendable object. More...
 
- Public Member Functions inherited from frc::PIDInterface
 PIDInterface (PIDInterface &&)=default
 
PIDInterfaceoperator= (PIDInterface &&)=default
 

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::SendableBase
void AddChild (std::shared_ptr< Sendable > child)
 Add a child component. More...
 
void AddChild (void *child)
 Add a child component. More...
 
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...
 

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.

Constructor & Destructor Documentation

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
Kpthe proportional coefficient
Kithe integral coefficient
Kdthe derivative coefficient
sourceThe PIDSource object that is used to get values
outputThe PIDOutput object that is set to the output value
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
Kpthe proportional coefficient
Kithe integral coefficient
Kdthe derivative coefficient
sourceThe PIDSource object that is used to get values
outputThe PIDOutput object that is set to the output value

Member Function Documentation

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.

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

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
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
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
errorThe current error of the PID controller.
Returns
Error for continuous inputs.
double frc::PIDBase::GetD ( ) const
overridevirtual

Get the Differential coefficient.

Returns
differential coefficient

Implements frc::PIDInterface.

double frc::PIDBase::GetDeltaSetpoint ( ) const

Returns the change in setpoint over time of the PIDBase.

Returns
the change in setpoint over time
virtual double frc::PIDBase::GetError ( ) const
virtual

Returns the current difference of the input from the setpoint.

Returns
the current error
virtual double frc::PIDBase::GetF ( ) const
virtual

Get the Feed forward coefficient.

Returns
Feed forward coefficient
double frc::PIDBase::GetI ( ) const
overridevirtual

Get the Integral coefficient.

Returns
integral coefficient

Implements frc::PIDInterface.

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

Get the Proportional coefficient.

Returns
proportional coefficient

Implements frc::PIDInterface.

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

Returns the type of input the PID controller is using.

Returns
the PID controller input type
double frc::PIDBase::GetSetpoint ( ) const
overridevirtual

Returns the current setpoint of the PIDBase.

Returns
the current setpoint

Implements frc::PIDInterface.

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

Initializes this Sendable object.

Parameters
buildersendable builder

Implements frc::Sendable.

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.

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.

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

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

Parameters
percentageerror which is tolerable
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
continuoustrue turns on continuous, false turns off continuous
void frc::PIDBase::SetD ( double  d)

Set the Differential coefficient of the PID controller gain.

Parameters
ddifferential coefficient
void frc::PIDBase::SetF ( double  f)

Get the Feed forward coefficient of the PID controller gain.

Parameters
fFeed forward coefficient
void frc::PIDBase::SetI ( double  i)

Set the Integral coefficient of the PID controller gain.

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

Sets the maximum and minimum values expected from the input.

Parameters
minimumInputthe minimum value expected from the input
maximumInputthe maximum value expected from the output
virtual void frc::PIDBase::SetOutputRange ( double  minimumOutput,
double  maximumOutput 
)
virtual

Sets the minimum and maximum values to write.

Parameters
minimumOutputthe minimum value to write to the output
maximumOutputthe maximum value to write to the output
void frc::PIDBase::SetP ( double  p)

Set the Proportional coefficient of the PID controller gain.

Parameters
pproportional coefficient
virtual void frc::PIDBase::SetPercentTolerance ( double  percentValue)
virtual

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

Parameters
percentageerror which is tolerable
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
pProportional coefficient
iIntegral coefficient
dDifferential coefficient

Implements frc::PIDInterface.

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
pProportional coefficient
iIntegral coefficient
dDifferential coefficient
fFeed forward coefficient
void frc::PIDBase::SetSetpoint ( double  setpoint)
overridevirtual

Set the setpoint for the PIDBase.

Parameters
setpointthe desired setpoint

Implements frc::PIDInterface.

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

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

Parameters
percentageerror which is tolerable
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
bufLengthNumber of previous cycles to average. Defaults to 1.

The documentation for this class was generated from the following file: