Kamil Foryszewski / PID

Fork of PID by Aaron Berk

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PID.h Source File

PID.h

00001 
00002 #ifndef PID_H
00003 #define PID_H
00004 
00005 /**
00006  * Includes
00007  */
00008 #include "mbed.h"
00009 
00010 /**
00011  * Defines
00012  */
00013 #define MANUAL_MODE 0
00014 #define AUTO_MODE   1
00015 
00016 /**
00017  * Proportional-integral-derivative controller.
00018  */
00019 class PID {
00020 
00021 public:
00022 
00023     /**
00024      * Constructor.
00025      *
00026      * Sets default limits , calculates tuning parameters, and sets
00027      * manual mode with no bias.
00028      *
00029      * @param Kc - Tuning parameter
00030      * @param tauI - Tuning parameter
00031      * @param tauD - Tuning parameter
00032      * @param interval PID calculation performed every interval seconds.
00033      */
00034     PID(float Kc, float tauI, float tauD, float interval);
00035 
00036     /**
00037      * Scale from inputs to 0-100%.
00038      *
00039      * @param InMin The real world value corresponding to 0%.
00040      * @param InMax The real world value corresponding to 100%.
00041      */
00042     void setInputLimits(float inMin , float inMax);
00043 
00044     /**
00045      * Scale from outputs to 0-100%.
00046      *
00047      * @param outMin The real world value corresponding to 0%.
00048      * @param outMax The real world value corresponding to 100%.
00049      */
00050     void setOutputLimits(float outMin, float outMax);
00051 
00052     /**
00053      * Calculate PID constants.
00054      *
00055      * Allows parameters to be changed on the fly without ruining calculations.
00056      *
00057      * @param Kc - Tuning parameter
00058      * @param tauI - Tuning parameter
00059      * @param tauD - Tuning parameter
00060      */
00061     void setTunings(float Kc, float tauI, float tauD);
00062 
00063     /**
00064      * Reinitializes controller internals. Automatically
00065      * called on a manual to auto transition.
00066      */
00067     void reset(void);
00068     
00069     /**
00070      * Set PID to manual or auto mode.
00071      *
00072      * @param mode        0 -> Manual
00073      *             Non-zero -> Auto
00074      */
00075     void setMode(int mode);
00076     
00077     /**
00078      * Set how fast the PID loop is run.
00079      *
00080      * @param interval PID calculation peformed every interval seconds.
00081      */
00082     void setInterval(float interval);
00083     
00084     /**
00085      * Set the set point.
00086      *
00087      * @param sp The set point as a real world value.
00088      */
00089     void setSetPoint(float sp);
00090     
00091     /**
00092      * Set the process value.
00093      *
00094      * @param pv The process value as a real world value.
00095      */
00096     void setProcessValue(float pv);
00097     
00098     /**
00099      * Set the bias.
00100      *
00101      * @param bias The bias for the controller output.
00102      */
00103     void setBias(float bias);
00104 
00105     /**
00106      * PID calculation.
00107      *
00108      * @return The controller output as a float between outMin and outMax.
00109      */
00110     float compute(void);
00111 
00112     //Getters.
00113     float getInMin();
00114     float getInMax();
00115     float getOutMin();
00116     float getOutMax();
00117     float getInterval();
00118     float getPParam();
00119     float getIParam();
00120     float getDParam();
00121 
00122 private:
00123 
00124     bool usingFeedForward;
00125     bool inAuto;
00126 
00127     //Actual tuning parameters used in PID calculation.
00128     float Kc_;
00129     float tauR_;
00130     float tauD_;
00131     
00132     //Raw tuning parameters.
00133     float pParam_;
00134     float iParam_;
00135     float dParam_;
00136     
00137     //The point we want to reach.
00138     float setPoint_;         
00139     //The thing we measure.
00140     float processVariable_;  
00141     float prevProcessVariable_;
00142     //The output that affects the process variable.
00143     float controllerOutput_; 
00144     float prevControllerOutput_;
00145 
00146     //We work in % for calculations so these will scale from
00147     //real world values to 0-100% and back again.
00148     float inMin_;
00149     float inMax_;
00150     float inSpan_;
00151     float outMin_;
00152     float outMax_;
00153     float outSpan_;
00154 
00155     //The accumulated error, i.e. integral.
00156     float accError_;
00157     //The controller output bias.
00158     float bias_;
00159 
00160     //The interval between samples.
00161     float tSample_;          
00162 
00163     //Controller output as a real world value.
00164     volatile float realOutput_;
00165 
00166 };
00167 
00168 #endif /* PID_H */