blob: a2a196e65a421adb82c9dcafed6aa772d1ac73d3 [file] [log] [blame]
jerrymf1579332013-02-07 01:56:28 +00001/*----------------------------------------------------------------------------*/
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
15class PIDOutput;
16class PIDSource;
17class Notifier;
18
19/**
20 * Class implements a PID Control Loop.
21 *
22 * Creates a separate thread which reads the given PIDSource and takes
23 * care of the integral calculations, as well as writing the given
24 * PIDOutput
25 */
26class PIDController : public LiveWindowSendable, public Controller, public ITableListener
27{
28public:
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
65private:
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 void Calculate();
96
97 virtual ITable* GetTable();
98 virtual std::string GetSmartDashboardType();
99 virtual void ValueChanged(ITable* source, const std::string& key, EntryValue value, bool isNew);
100 virtual void UpdateTable();
101 virtual void StartLiveWindowMode();
102 virtual void StopLiveWindowMode();
103protected:
104 ITable* m_table;
105
106 DISALLOW_COPY_AND_ASSIGN(PIDController);
107};
108
109#endif