1

Fork of PID by Aaron Berk

Revision:
1:28542afc96d0
Parent:
0:6e12a3e5af19
--- a/PID.h	Thu Sep 02 16:48:10 2010 +0000
+++ b/PID.h	Wed Feb 15 12:33:10 2017 +0000
@@ -44,170 +44,83 @@
  *  http://www.controlguru.com/
  */
 
-#ifndef PID_H
-#define PID_H
-
 /**
  * Includes
  */
 #include "mbed.h"
 
-/**
- * Defines
- */
-#define MANUAL_MODE 0
-#define AUTO_MODE   1
-
-/**
- * Proportional-integral-derivative controller.
- */
-class PID {
-
-public:
+class PID
+{
 
-    /**
-     * Constructor.
-     *
-     * Sets default limits [0-3.3V], calculates tuning parameters, and sets
-     * manual mode with no bias.
-     *
-     * @param Kc - Tuning parameter
-     * @param tauI - Tuning parameter
-     * @param tauD - Tuning parameter
-     * @param interval PID calculation performed every interval seconds.
-     */
-    PID(float Kc, float tauI, float tauD, float interval);
 
-    /**
-     * Scale from inputs to 0-100%.
-     *
-     * @param InMin The real world value corresponding to 0%.
-     * @param InMax The real world value corresponding to 100%.
-     */
-    void setInputLimits(float inMin , float inMax);
+  public:
 
-    /**
-     * Scale from outputs to 0-100%.
-     *
-     * @param outMin The real world value corresponding to 0%.
-     * @param outMax The real world value corresponding to 100%.
-     */
-    void setOutputLimits(float outMin, float outMax);
-
-    /**
-     * Calculate PID constants.
-     *
-     * Allows parameters to be changed on the fly without ruining calculations.
-     *
-     * @param Kc - Tuning parameter
-     * @param tauI - Tuning parameter
-     * @param tauD - Tuning parameter
-     */
-    void setTunings(float Kc, float tauI, float tauD);
+  //Constants used in some of the functions below
+  #define AUTOMATIC 1
+  #define MANUAL  0
+  #define DIRECT  0
+  #define REVERSE  1
 
-    /**
-     * Reinitializes controller internals. Automatically
-     * called on a manual to auto transition.
-     */
-    void reset(void);
-    
-    /**
-     * Set PID to manual or auto mode.
-     *
-     * @param mode        0 -> Manual
-     *             Non-zero -> Auto
-     */
-    void setMode(int mode);
-    
-    /**
-     * Set how fast the PID loop is run.
-     *
-     * @param interval PID calculation peformed every interval seconds.
-     */
-    void setInterval(float interval);
     
-    /**
-     * Set the set point.
-     *
-     * @param sp The set point as a real world value.
-     */
-    void setSetPoint(float sp);
-    
-    /**
-     * Set the process value.
-     *
-     * @param pv The process value as a real world value.
-     */
-    void setProcessValue(float pv);
-    
-    /**
-     * Set the bias.
-     *
-     * @param bias The bias for the controller output.
-     */
-    void setBias(float bias);
+  //commonly used functions **************************************************************************
+    PID(double*, double*, double*,        // * constructor.  links the PID to the Input, Output, and 
+        double, double, double, int, float*, float*);     //   Setpoint.  Initial tuning parameters are also set here
+  
+    void SetMode(int Mode);               // * sets PID to either Manual (0) or Auto (non-0)
 
-    /**
-     * PID calculation.
-     *
-     * @return The controller output as a float between outMin and outMax.
-     */
-    float compute(void);
+    double Compute(float*);                       // * performs the PID calculation.  it should be
+                                          //   called every time loop() cycles. ON/OFF and
+                                          //   calculation frequency can be set using SetMode
+                                          //   SetSampleTime respectively
 
-    //Getters.
-    float getInMin();
-    float getInMax();
-    float getOutMin();
-    float getOutMax();
-    float getInterval();
-    float getPParam();
-    float getIParam();
-    float getDParam();
+    void SetOutputLimits(double, double); //clamps the output to a specific range. 0-255 by default, but
+                      //it's likely the user will want to change this depending on
+                      //the application
+  
 
-private:
-
-    bool usingFeedForward;
-    bool inAuto;
 
-    //Actual tuning parameters used in PID calculation.
-    float Kc_;
-    float tauR_;
-    float tauD_;
-    
-    //Raw tuning parameters.
-    float pParam_;
-    float iParam_;
-    float dParam_;
-    
-    //The point we want to reach.
-    float setPoint_;         
-    //The thing we measure.
-    float processVariable_;  
-    float prevProcessVariable_;
-    //The output that affects the process variable.
-    float controllerOutput_; 
-    float prevControllerOutput_;
+  //available but not commonly used functions ********************************************************
+    void SetTunings(double, double,       // * While most users will set the tunings once in the 
+                    double);            //   constructor, this function gives the user the option
+                                          //   of changing tunings during runtime for Adaptive control
+  void SetControllerDirection(int);   // * Sets the Direction, or "Action" of the controller. DIRECT
+                      //   means the output will increase when error is positive. REVERSE
+                      //   means the opposite.  it's very unlikely that this will be needed
+                      //   once it is set in the constructor.
+    void SetSampleTime(int);              // * sets the frequency, in Milliseconds, with which 
+                                          //   the PID calculation is performed.  default is 100
+                      
+                      
+                      
+  //Display functions ****************************************************************
+  double GetKp();             // These functions query the pid for interal values.
+  double GetKi();             //  they were created mainly for the pid front-end,
+  double GetKd();             // where it's important to know what is actually 
+  int GetMode();              //  inside the PID.
+  int GetDirection();           //
 
-    //We work in % for calculations so these will scale from
-    //real world values to 0-100% and back again.
-    float inMin_;
-    float inMax_;
-    float inSpan_;
-    float outMin_;
-    float outMax_;
-    float outSpan_;
+  private:
+  void Initialize();
+  
+  double dispKp;        // * we'll hold on to the tuning parameters in user-entered 
+  double dispKi;        //   format for display purposes
+  double dispKd;        //
+    
+  double kp;                  // * (P)roportional Tuning Parameter
+    double ki;                  // * (I)ntegral Tuning Parameter
+    double kd;                  // * (D)erivative Tuning Parameter
+
+  int controllerDirection;
 
-    //The accumulated error, i.e. integral.
-    float accError_;
-    //The controller output bias.
-    float bias_;
+    double *myInput;              // * Pointers to the Input, Output, and Setpoint variables
+    double *myOutput;             //   This creates a hard link between the variables and the 
+    double *mySetpoint;           //   PID, freeing the user from having to constantly tell us
+                                  //   what these values are.  with pointers we'll just know.
+        
+  float lastTime;
+  double ITerm, lastInput;
 
-    //The interval between samples.
-    float tSample_;          
-
-    //Controller output as a real world value.
-    volatile float realOutput_;
-
-};
-
-#endif /* PID_H */
+  float SampleTime;
+  double outMin, outMax;
+  bool inAuto;
+};
\ No newline at end of file