WPILibC++  2020.3.2
PIDSubsystem.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2011-2020 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #pragma once
9 
10 #include <memory>
11 
12 #include <wpi/Twine.h>
13 
14 #include "frc/PIDController.h"
15 #include "frc/PIDOutput.h"
16 #include "frc/PIDSource.h"
17 #include "frc/commands/Subsystem.h"
18 
19 namespace frc {
20 
30 class PIDSubsystem : public Subsystem, public PIDOutput, public PIDSource {
31  public:
40  PIDSubsystem(const wpi::Twine& name, double p, double i, double d);
41 
51  PIDSubsystem(const wpi::Twine& name, double p, double i, double d, double f);
52 
66  PIDSubsystem(const wpi::Twine& name, double p, double i, double d, double f,
67  double period);
68 
78  PIDSubsystem(double p, double i, double d);
79 
90  PIDSubsystem(double p, double i, double d, double f);
91 
104  PIDSubsystem(double p, double i, double d, double f, double period);
105 
106  ~PIDSubsystem() override = default;
107 
108  PIDSubsystem(PIDSubsystem&&) = default;
109  PIDSubsystem& operator=(PIDSubsystem&&) = default;
110 
114  void Enable();
115 
119  void Disable();
120 
121  // PIDOutput interface
122  void PIDWrite(double output) override;
123 
124  // PIDSource interface
125 
126  double PIDGet() override;
127 
136  void SetSetpoint(double setpoint);
137 
146  void SetSetpointRelative(double deltaSetpoint);
147 
154  void SetInputRange(double minimumInput, double maximumInput);
155 
162  void SetOutputRange(double minimumOutput, double maximumOutput);
163 
169  double GetSetpoint();
170 
176  double GetPosition();
177 
183  double GetRate();
184 
191  virtual void SetAbsoluteTolerance(double absValue);
192 
199  virtual void SetPercentTolerance(double percent);
200 
216  virtual bool OnTarget() const;
217 
218  protected:
226  std::shared_ptr<PIDController> GetPIDController();
227 
228  virtual double ReturnPIDInput() = 0;
229  virtual void UsePIDOutput(double output) = 0;
230 
231  private:
232  // The internal PIDController
233  std::shared_ptr<PIDController> m_controller;
234 };
235 
236 } // namespace frc
frc::PIDSource
PIDSource interface is a generic sensor source for the PID class.
Definition: PIDSource.h:20
frc::PIDSubsystem::OnTarget
virtual bool OnTarget() const
Return true if the error is within the percentage of the total input range, determined by SetToleranc...
frc::PIDSubsystem::Enable
void Enable()
Enables the internal PIDController.
frc::PIDSubsystem
This class is designed to handle the case where there is a Subsystem which uses a single PIDControlle...
Definition: PIDSubsystem.h:30
frc::PIDSubsystem::SetSetpoint
void SetSetpoint(double setpoint)
Sets the setpoint to the given value.
frc::PIDSubsystem::SetPercentTolerance
virtual void SetPercentTolerance(double percent)
Set the percentage error which is considered tolerable for use with OnTarget().
frc::PIDSubsystem::Disable
void Disable()
Disables the internal PIDController.
frc::PIDSubsystem::SetSetpointRelative
void SetSetpointRelative(double deltaSetpoint)
Adds the given value to the setpoint.
frc::PIDSubsystem::SetOutputRange
void SetOutputRange(double minimumOutput, double maximumOutput)
Sets the maximum and minimum values to write.
frc::PIDSubsystem::GetRate
double GetRate()
Returns the current rate.
frc::PIDSubsystem::SetInputRange
void SetInputRange(double minimumInput, double maximumInput)
Sets the maximum and minimum values expected from the input.
frc::Subsystem
Definition: Subsystem.h:24
frc::PIDSubsystem::GetPosition
double GetPosition()
Returns the current position.
frc
A class that enforces constraints on the differential drive kinematics.
Definition: SPIAccelerometerSim.h:16
frc::PIDSubsystem::SetAbsoluteTolerance
virtual void SetAbsoluteTolerance(double absValue)
Set the absolute error which is considered tolerable for use with OnTarget.
frc::PIDOutput
PIDOutput interface is a generic output for the PID class.
Definition: PIDOutput.h:20
wpi::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:85
frc::PIDSubsystem::GetPIDController
std::shared_ptr< PIDController > GetPIDController()
Returns the PIDController used by this PIDSubsystem.
frc::PIDSubsystem::GetSetpoint
double GetSetpoint()
Return the current setpoint.
frc::PIDSubsystem::PIDSubsystem
PIDSubsystem(const wpi::Twine &name, double p, double i, double d)
Instantiates a PIDSubsystem that will use the given P, I, and D values.