Proportional, integral, derivative controller library. Ported from the Arduino PID library by Brett Beauregard.
Fork of PID by
PID.h
00001 /** 00002 * @author Aaron Berk 00003 * 00004 * @section LICENSE 00005 * 00006 * Copyright (c) 2010 ARM Limited 00007 * 00008 * Permission is hereby granted, free of charge, to any person obtaining a copy 00009 * of this software and associated documentation files (the "Software"), to deal 00010 * in the Software without restriction, including without limitation the rights 00011 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00012 * copies of the Software, and to permit persons to whom the Software is 00013 * furnished to do so, subject to the following conditions: 00014 * 00015 * The above copyright notice and this permission notice shall be included in 00016 * all copies or substantial portions of the Software. 00017 * 00018 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00019 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00020 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00021 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00022 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00023 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00024 * THE SOFTWARE. 00025 * 00026 * @section DESCRIPTION 00027 * 00028 * A PID controller is a widely used feedback controller commonly found in 00029 * industry. 00030 * 00031 * This library is a port of Brett Beauregard's Arduino PID library: 00032 * 00033 * http://www.arduino.cc/playground/Code/PIDLibrary 00034 * 00035 * The wikipedia article on PID controllers is a good place to start on 00036 * understanding how they work: 00037 * 00038 * http://en.wikipedia.org/wiki/PID_controller 00039 * 00040 * For a clear and elegant explanation of how to implement and tune a 00041 * controller, the controlguru website by Douglas J. Cooper (who also happened 00042 * to be Brett's controls professor) is an excellent reference: 00043 * 00044 * http://www.controlguru.com/ 00045 */ 00046 00047 #ifndef PID_H 00048 #define PID_H 00049 00050 /** 00051 * Includes 00052 */ 00053 #include "mbed.h" 00054 00055 /** 00056 * Defines 00057 */ 00058 #define MANUAL_MODE 0 00059 #define AUTO_MODE 1 00060 00061 /** 00062 * Proportional-integral-derivative controller. 00063 */ 00064 class PID { 00065 00066 public: 00067 00068 /** 00069 * Constructor. 00070 * 00071 * Sets default limits [0-3.3V], calculates tuning parameters, and sets 00072 * manual mode with no bias. 00073 * 00074 * @param Kc - Tuning parameter 00075 * @param tauI - Tuning parameter 00076 * @param tauD - Tuning parameter 00077 * @param interval PID calculation performed every interval seconds. 00078 */ 00079 PID(float Kc, float tauI, float tauD, float interval); 00080 00081 /** 00082 * Scale from inputs to 0-100%. 00083 * 00084 * @param InMin The real world value corresponding to 0%. 00085 * @param InMax The real world value corresponding to 100%. 00086 */ 00087 void setInputLimits(float inMin , float inMax); 00088 00089 /** 00090 * Scale from outputs to 0-100%. 00091 * 00092 * @param outMin The real world value corresponding to 0%. 00093 * @param outMax The real world value corresponding to 100%. 00094 */ 00095 void setOutputLimits(float outMin, float outMax); 00096 00097 /** 00098 * Calculate PID constants. 00099 * 00100 * Allows parameters to be changed on the fly without ruining calculations. 00101 * 00102 * @param Kc - Tuning parameter 00103 * @param tauI - Tuning parameter 00104 * @param tauD - Tuning parameter 00105 */ 00106 void setTunings(float Kc, float tauI, float tauD); 00107 00108 /** 00109 * Reinitializes controller internals. Automatically 00110 * called on a manual to auto transition. 00111 */ 00112 void reset(void); 00113 00114 /** 00115 * Set PID to manual or auto mode. 00116 * 00117 * @param mode 0 -> Manual 00118 * Non-zero -> Auto 00119 */ 00120 void setMode(int mode); 00121 00122 /** 00123 * Set how fast the PID loop is run. 00124 * 00125 * @param interval PID calculation peformed every interval seconds. 00126 */ 00127 void setInterval(float interval); 00128 00129 /** 00130 * Set the set point. 00131 * 00132 * @param sp The set point as a real world value. 00133 */ 00134 void setSetPoint(float sp); 00135 00136 /** 00137 * Set the process value. 00138 * 00139 * @param pv The process value as a real world value. 00140 */ 00141 void setProcessValue(float pv); 00142 00143 /** 00144 * Set the bias. 00145 * 00146 * @param bias The bias for the controller output. 00147 */ 00148 void setBias(float bias); 00149 00150 /** 00151 * PID calculation. 00152 * 00153 * @return The controller output as a float between outMin and outMax. 00154 */ 00155 float compute(void); 00156 00157 //Getters. 00158 float getInMin(); 00159 float getInMax(); 00160 float getOutMin(); 00161 float getOutMax(); 00162 float getInterval(); 00163 float getPParam(); 00164 float getIParam(); 00165 float getDParam(); 00166 00167 operator float(); 00168 PID& operator=(float sp); 00169 00170 private: 00171 00172 bool usingFeedForward; 00173 bool inAuto; 00174 00175 //Actual tuning parameters used in PID calculation. 00176 float Kc_; 00177 float tauR_; 00178 float tauD_; 00179 00180 //Raw tuning parameters. 00181 float pParam_; 00182 float iParam_; 00183 float dParam_; 00184 00185 //The point we want to reach. 00186 float setPoint_; 00187 //The thing we measure. 00188 float processVariable_; 00189 float prevProcessVariable_; 00190 //The output that affects the process variable. 00191 float controllerOutput_; 00192 float prevControllerOutput_; 00193 00194 //We work in % for calculations so these will scale from 00195 //real world values to 0-100% and back again. 00196 float inMin_; 00197 float inMax_; 00198 float inSpan_; 00199 float outMin_; 00200 float outMax_; 00201 float outSpan_; 00202 00203 //The accumulated error, i.e. integral. 00204 float accError_; 00205 //The controller output bias. 00206 float bias_; 00207 00208 //The interval between samples. 00209 float tSample_; 00210 00211 //Controller output as a real world value. 00212 volatile float realOutput_; 00213 00214 }; 00215 00216 #endif /* PID_H */
Generated on Wed Jul 13 2022 07:00:52 by 1.7.2