WPILibC++  unspecified
PIDSubsystem.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2011-2018 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 "Commands/Subsystem.h"
15 #include "PIDController.h"
16 #include "PIDOutput.h"
17 #include "PIDSource.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 
111  void Enable();
112 
116  void Disable();
117 
118  // PIDOutput interface
119  void PIDWrite(double output) override;
120 
121  // PIDSource interface
122 
123  double PIDGet() override;
124 
133  void SetSetpoint(double setpoint);
134 
143  void SetSetpointRelative(double deltaSetpoint);
144 
151  void SetInputRange(double minimumInput, double maximumInput);
152 
159  void SetOutputRange(double minimumOutput, double maximumOutput);
160 
166  double GetSetpoint();
167 
173  double GetPosition();
174 
180  double GetRate();
181 
188  virtual void SetAbsoluteTolerance(double absValue);
189 
196  virtual void SetPercentTolerance(double percent);
197 
213  virtual bool OnTarget() const;
214 
215  protected:
223  std::shared_ptr<PIDController> GetPIDController();
224 
225  virtual double ReturnPIDInput() = 0;
226  virtual void UsePIDOutput(double output) = 0;
227 
228  private:
229  // The internal PIDController
230  std::shared_ptr<PIDController> m_controller;
231 };
232 
233 } // namespace frc
Definition: Utility.cpp:119
void SetSetpoint(double setpoint)
Sets the setpoint to the given value.
Definition: PIDSubsystem.cpp:63
virtual void SetPercentTolerance(double percent)
Set the percentage error which is considered tolerable for use with OnTarget().
Definition: PIDSubsystem.cpp:89
void SetOutputRange(double minimumOutput, double maximumOutput)
Sets the maximum and minimum values to write.
Definition: PIDSubsystem.cpp:75
PIDSource interface is a generic sensor source for the PID class.
Definition: PIDSource.h:20
virtual bool OnTarget() const
Return true if the error is within the percentage of the total input range, determined by SetToleranc...
Definition: PIDSubsystem.cpp:93
double GetSetpoint()
Return the current setpoint.
Definition: PIDSubsystem.cpp:79
std::shared_ptr< PIDController > GetPIDController()
Returns the PIDController used by this PIDSubsystem.
Definition: PIDSubsystem.cpp:95
This class is designed to handle the case where there is a Subsystem which uses a single PIDControlle...
Definition: PIDSubsystem.h:30
Definition: Subsystem.h:23
PIDOutput interface is a generic output for the PID class.
Definition: PIDOutput.h:20
void Enable()
Enables the internal PIDController.
Definition: PIDSubsystem.cpp:55
void SetInputRange(double minimumInput, double maximumInput)
Sets the maximum and minimum values expected from the input.
Definition: PIDSubsystem.cpp:71
void SetSetpointRelative(double deltaSetpoint)
Adds the given value to the setpoint.
Definition: PIDSubsystem.cpp:67
PIDSubsystem(const wpi::Twine &name, double p, double i, double d)
Instantiates a PIDSubsystem that will use the given P, I, and D values.
Definition: PIDSubsystem.cpp:14
void Disable()
Disables the internal PIDController.
Definition: PIDSubsystem.cpp:57
double GetRate()
Returns the current rate.
Definition: PIDSubsystem.cpp:83
virtual void SetAbsoluteTolerance(double absValue)
Set the absolute error which is considered tolerable for use with OnTarget.
Definition: PIDSubsystem.cpp:85
double GetPosition()
Returns the current position.
Definition: PIDSubsystem.cpp:81
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79