WPILib 2012
WPILibRoboticsLibraryforFRC
|
00001 /*----------------------------------------------------------------------------*/ 00002 /* Copyright (c) FIRST 2008. All Rights Reserved. */ 00003 /* Open Source Software - may be modified and shared by FRC teams. The code */ 00004 /* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */ 00005 /*----------------------------------------------------------------------------*/ 00006 00007 #ifndef PIDCONTROLLER_H_ 00008 #define PIDCONTROLLER_H_ 00009 00010 #include "Base.h" 00011 #include "semLib.h" 00012 00013 class PIDOutput; 00014 class PIDSource; 00015 class Notifier; 00016 00024 class PIDController 00025 { 00026 public: 00027 PIDController(float p, float i, float d, 00028 PIDSource *source, PIDOutput *output, 00029 float period = 0.05); 00030 virtual ~PIDController(); 00031 virtual float Get(); 00032 virtual void SetContinuous(bool continuous = true); 00033 virtual void SetInputRange(float minimumInput, float maximumInput); 00034 virtual void SetOutputRange(float mimimumOutput, float maximumOutput); 00035 virtual void SetPID(float p, float i, float d); 00036 virtual float GetP(); 00037 virtual float GetI(); 00038 virtual float GetD(); 00039 00040 virtual void SetSetpoint(float setpoint); 00041 virtual float GetSetpoint(); 00042 00043 virtual float GetError(); 00044 00045 virtual void SetTolerance(float percent); 00046 virtual bool OnTarget(); 00047 00048 virtual void Enable(); 00049 virtual void Disable(); 00050 virtual bool IsEnabled(); 00051 00052 virtual void Reset(); 00053 00054 private: 00055 float m_P; // factor for "proportional" control 00056 float m_I; // factor for "integral" control 00057 float m_D; // factor for "derivative" control 00058 float m_maximumOutput; // |maximum output| 00059 float m_minimumOutput; // |minimum output| 00060 float m_maximumInput; // maximum input - limit setpoint to this 00061 float m_minimumInput; // minimum input - limit setpoint to this 00062 bool m_continuous; // do the endpoints wrap around? eg. Absolute encoder 00063 bool m_enabled; //is the pid controller enabled 00064 float m_prevError; // the prior sensor input (used to compute velocity) 00065 double m_totalError; //the sum of the errors for use in the integral calc 00066 float m_tolerance; //the percetage error that is considered on target 00067 float m_setpoint; 00068 float m_error; 00069 float m_result; 00070 float m_period; 00071 00072 SEM_ID m_semaphore; 00073 00074 PIDSource *m_pidInput; 00075 PIDOutput *m_pidOutput; 00076 Notifier *m_controlLoop; 00077 00078 static void CallCalculate(void *controller); 00079 void Calculate(); 00080 DISALLOW_COPY_AND_ASSIGN(PIDController); 00081 }; 00082 00083 #endif