~gabe/flashlight-firmware/anduril2

« back to all changes in this revision

Viewing changes to loneoceans/gxb172/PID_v1.h

  • Committer: Selene Scriven
  • Date: 2019-09-18 22:18:49 UTC
  • mfrom: (188.4.19 trunk)
  • Revision ID: bzr@toykeeper.net-20190918221849-7h4hcvl07ea3b6g6
merged updates from trunk, including gcc7/8/9 compatibility

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef PID_v1_h
 
2
#define PID_v1_h
 
3
#define LIBRARY_VERSION 1.2.1
 
4
 
 
5
class PID
 
6
{
 
7
 
 
8
 
 
9
  public:
 
10
 
 
11
  //Constants used in some of the functions below
 
12
  #define AUTOMATIC     1
 
13
  #define MANUAL        0
 
14
  #define DIRECT  0
 
15
  #define REVERSE  1
 
16
  #define P_ON_M 0
 
17
  #define P_ON_E 1
 
18
 
 
19
  //commonly used functions **************************************************************************
 
20
    PID(double*, double*, double*,        // * constructor.  links the PID to the Input, Output, and 
 
21
        double, double, double, int, int);//   Setpoint.  Initial tuning parameters are also set here.
 
22
                                          //   (overload for specifying proportional mode)
 
23
 
 
24
    PID(double*, double*, double*,        // * constructor.  links the PID to the Input, Output, and 
 
25
        double, double, double, int);     //   Setpoint.  Initial tuning parameters are also set here
 
26
        
 
27
    void SetMode(uint8_t Mode);               // * sets PID to either Manual (0) or Auto (non-0)
 
28
 
 
29
    bool Compute();                       // * performs the PID calculation.  it should be
 
30
                                          //   called every time loop() cycles. ON/OFF and
 
31
                                          //   calculation frequency can be set using SetMode
 
32
                                          //   SetSampleTime respectively
 
33
 
 
34
    void SetOutputLimits(double, double); // * clamps the output to a specific range. 0-255 by default, but
 
35
                                                                                                      //   it's likely the user will want to change this depending on
 
36
                                                                                                      //   the application
 
37
        
 
38
 
 
39
 
 
40
  //available but not commonly used functions ********************************************************
 
41
    void SetTunings(double, double,       // * While most users will set the tunings once in the 
 
42
                    double);                //   constructor, this function gives the user the option
 
43
                                          //   of changing tunings during runtime for Adaptive control
 
44
    void SetTunings(double, double,       // * overload for specifying proportional mode
 
45
                    double, int);                 
 
46
 
 
47
    void SetControllerDirection(int);     // * Sets the Direction, or "Action" of the controller. DIRECT
 
48
                                                                                  //   means the output will increase when error is positive. REVERSE
 
49
                                                                                  //   means the opposite.  it's very unlikely that this will be needed
 
50
                                                                                  //   once it is set in the constructor.
 
51
    void SetSampleTime(int);              // * sets the frequency, in Milliseconds, with which 
 
52
                                          //   the PID calculation is performed.  default is 100
 
53
                                                                                  
 
54
                                                                                  
 
55
                                                                                  
 
56
  //Display functions ****************************************************************
 
57
        double GetKp();                                           // These functions query the pid for interal values.
 
58
        double GetKi();                                           //  they were created mainly for the pid front-end,
 
59
        double GetKd();                                           // where it's important to know what is actually 
 
60
        int GetMode();                                            //  inside the PID.
 
61
        int GetDirection();                                       //
 
62
 
 
63
  //private:
 
64
        void Initialize();
 
65
 
 
66
        double dispKp;                          // * we'll hold on to the tuning parameters in user-entered 
 
67
        double dispKi;                          //   format for display purposes
 
68
        double dispKd;                          //
 
69
    
 
70
        double kp;                  // * (P)roportional Tuning Parameter
 
71
  double ki;                  // * (I)ntegral Tuning Parameter
 
72
  double kd;                  // * (D)erivative Tuning Parameter
 
73
 
 
74
        int controllerDirection;
 
75
        int pOn;
 
76
 
 
77
  double *myInput;              // * Pointers to the Input, Output, and Setpoint variables
 
78
  double *myOutput;             //   This creates a hard link between the variables and the 
 
79
  double *mySetpoint;           //   PID, freeing the user from having to constantly tell us
 
80
                                  //   what these values are.  with pointers we'll just know.
 
81
                          
 
82
        unsigned long lastTime;
 
83
        double outputSum, lastInput;
 
84
 
 
85
        unsigned long SampleTime;
 
86
        double outMin, outMax;
 
87
        bool inAuto, pOnE;
 
88
};
 
89
#endif
 
90