WPILib  2014.0
WPIRoboticsLibraryforFRC
 All Classes Functions Variables Pages
PIDController.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) FIRST 2008. 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 $(WIND_BASE)/WPILib. */
5 /*----------------------------------------------------------------------------*/
6 
7 #ifndef PIDCONTROLLER_H_
8 #define PIDCONTROLLER_H_
9 
10 #include "Base.h"
11 #include "semLib.h"
12 #include "Controller.h"
13 #include "LiveWindow/LiveWindow.h"
14 
15 class PIDOutput;
16 class PIDSource;
17 class Notifier;
18 
26 class PIDController : public LiveWindowSendable, public Controller, public ITableListener
27 {
28 public:
29  PIDController(float p, float i, float d,
30  PIDSource *source, PIDOutput *output,
31  float period = 0.05);
32  PIDController(float p, float i, float d, float f,
33  PIDSource *source, PIDOutput *output,
34  float period = 0.05);
35  virtual ~PIDController();
36  virtual float Get();
37  virtual void SetContinuous(bool continuous = true);
38  virtual void SetInputRange(float minimumInput, float maximumInput);
39  virtual void SetOutputRange(float mimimumOutput, float maximumOutput);
40  virtual void SetPID(float p, float i, float d);
41  virtual void SetPID(float p, float i, float d, float f);
42  virtual float GetP();
43  virtual float GetI();
44  virtual float GetD();
45  virtual float GetF();
46 
47  virtual void SetSetpoint(float setpoint);
48  virtual float GetSetpoint();
49 
50  virtual float GetError();
51 
52  virtual void SetTolerance(float percent);
53  virtual void SetAbsoluteTolerance(float absValue);
54  virtual void SetPercentTolerance(float percentValue);
55  virtual bool OnTarget();
56 
57  virtual void Enable();
58  virtual void Disable();
59  virtual bool IsEnabled();
60 
61  virtual void Reset();
62 
63  virtual void InitTable(ITable* table);
64 
65 private:
66  float m_P; // factor for "proportional" control
67  float m_I; // factor for "integral" control
68  float m_D; // factor for "derivative" control
69  float m_F; // factor for "feed forward" control
70  float m_maximumOutput; // |maximum output|
71  float m_minimumOutput; // |minimum output|
72  float m_maximumInput; // maximum input - limit setpoint to this
73  float m_minimumInput; // minimum input - limit setpoint to this
74  bool m_continuous; // do the endpoints wrap around? eg. Absolute encoder
75  bool m_enabled; //is the pid controller enabled
76  float m_prevError; // the prior sensor input (used to compute velocity)
77  double m_totalError; //the sum of the errors for use in the integral calc
78  enum {kAbsoluteTolerance, kPercentTolerance, kNoTolerance} m_toleranceType;
79  float m_tolerance; //the percetage or absolute error that is considered on target
80  float m_setpoint;
81  float m_error;
82  float m_result;
83  float m_period;
84 
85  SEM_ID m_semaphore;
86 
87  PIDSource *m_pidInput;
88  PIDOutput *m_pidOutput;
89  Notifier *m_controlLoop;
90 
91  void Initialize(float p, float i, float d, float f,
92  PIDSource *source, PIDOutput *output,
93  float period = 0.05);
94  static void CallCalculate(void *controller);
95 
96  virtual ITable* GetTable();
97  virtual std::string GetSmartDashboardType();
98  virtual void ValueChanged(ITable* source, const std::string& key, EntryValue value, bool isNew);
99  virtual void UpdateTable();
100  virtual void StartLiveWindowMode();
101  virtual void StopLiveWindowMode();
102 protected:
103  ITable* m_table;
104  void Calculate();
105 
106  DISALLOW_COPY_AND_ASSIGN(PIDController);
107 };
108 
109 #endif
virtual float GetSetpoint()
Definition: PIDController.cpp:402
Definition: Controller.h:18
Definition: PIDController.h:26
virtual float Get()
Definition: PIDController.cpp:307
virtual void SetContinuous(bool continuous=true)
Definition: PIDController.cpp:325
virtual float GetP()
Definition: PIDController.cpp:254
Definition: PIDOutput.h:18
Definition: Notifier.h:16
virtual float GetF()
Definition: PIDController.cpp:293
virtual void SetPID(float p, float i, float d)
Definition: PIDController.cpp:206
void Calculate()
Definition: PIDController.cpp:131
virtual bool IsEnabled()
Definition: PIDController.cpp:537
virtual float GetD()
Definition: PIDController.cpp:280
virtual float GetI()
Definition: PIDController.cpp:267
virtual void SetInputRange(float minimumInput, float maximumInput)
Definition: PIDController.cpp:341
virtual float GetError()
Definition: PIDController.cpp:417
Definition: PIDSource.h:15
virtual void Enable()
Definition: PIDController.cpp:504
virtual void SetOutputRange(float mimimumOutput, float maximumOutput)
Definition: PIDController.cpp:359
PIDController(float p, float i, float d, PIDSource *source, PIDOutput *output, float period=0.05)
Definition: PIDController.cpp:33
virtual void SetSetpoint(float setpoint)
Definition: PIDController.cpp:373
virtual void Disable()
Definition: PIDController.cpp:520
virtual void Reset()
Definition: PIDController.cpp:551
virtual ~PIDController()
Definition: PIDController.cpp:107