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 <llvm/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:
32  PIDSubsystem(const llvm::Twine& name, double p, double i, double d);
33  PIDSubsystem(const llvm::Twine& name, double p, double i, double d, double f);
34  PIDSubsystem(const llvm::Twine& name, double p, double i, double d, double f,
35  double period);
36  PIDSubsystem(double p, double i, double d);
37  PIDSubsystem(double p, double i, double d, double f);
38  PIDSubsystem(double p, double i, double d, double f, double period);
39  ~PIDSubsystem() override = default;
40 
41  void Enable();
42  void Disable();
43 
44  // PIDOutput interface
45  void PIDWrite(double output) override;
46 
47  // PIDSource interface
48  double PIDGet() override;
49  void SetSetpoint(double setpoint);
50  void SetSetpointRelative(double deltaSetpoint);
51  void SetInputRange(double minimumInput, double maximumInput);
52  void SetOutputRange(double minimumOutput, double maximumOutput);
53  double GetSetpoint();
54  double GetPosition();
55  double GetRate();
56 
57  virtual void SetAbsoluteTolerance(double absValue);
58  virtual void SetPercentTolerance(double percent);
59  virtual bool OnTarget() const;
60 
61  protected:
62  std::shared_ptr<PIDController> GetPIDController();
63 
64  virtual double ReturnPIDInput() = 0;
65  virtual void UsePIDOutput(double output) = 0;
66 
67  private:
68  // The internal PIDController
69  std::shared_ptr<PIDController> m_controller;
70 };
71 
72 } // namespace frc
Definition: RobotController.cpp:14
void SetSetpoint(double setpoint)
Sets the setpoint to the given value.
Definition: PIDSubsystem.cpp:146
virtual void SetPercentTolerance(double percent)
Set the percentage error which is considered tolerable for use with OnTarget().
Definition: PIDSubsystem.cpp:204
void SetOutputRange(double minimumOutput, double maximumOutput)
Sets the maximum and minimum values to write.
Definition: PIDSubsystem.cpp:184
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:223
double GetSetpoint()
Return the current setpoint.
Definition: PIDSubsystem.cpp:166
std::shared_ptr< PIDController > GetPIDController()
Returns the PIDController used by this PIDSubsystem.
Definition: PIDSubsystem.cpp:134
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
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:120
void SetInputRange(double minimumInput, double maximumInput)
Sets the maximum and minimum values expected from the input.
Definition: PIDSubsystem.cpp:174
PIDSubsystem(const llvm::Twine &name, double p, double i, double d)
Instantiates a PIDSubsystem that will use the given P, I, and D values.
Definition: PIDSubsystem.cpp:22
void SetSetpointRelative(double deltaSetpoint)
Adds the given value to the setpoint.
Definition: PIDSubsystem.cpp:157
void Disable()
Disables the internal PIDController.
Definition: PIDSubsystem.cpp:125
double GetRate()
Returns the current rate.
Definition: PIDSubsystem.cpp:237
virtual void SetAbsoluteTolerance(double absValue)
Set the absolute error which is considered tolerable for use with OnTarget.
Definition: PIDSubsystem.cpp:194
double GetPosition()
Returns the current position.
Definition: PIDSubsystem.cpp:230