Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
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 void resetAccError(); 00167 00168 private: 00169 00170 bool usingFeedForward; 00171 bool inAuto; 00172 00173 //Actual tuning parameters used in PID calculation. 00174 float Kc_; 00175 float tauR_; 00176 float tauD_; 00177 00178 //Raw tuning parameters. 00179 float pParam_; 00180 float iParam_; 00181 float dParam_; 00182 00183 //The point we want to reach. 00184 float setPoint_; 00185 //The thing we measure. 00186 float processVariable_; 00187 float prevProcessVariable_; 00188 //The output that affects the process variable. 00189 float controllerOutput_; 00190 float prevControllerOutput_; 00191 00192 //We work in % for calculations so these will scale from 00193 //real world values to 0-100% and back again. 00194 float inMin_; 00195 float inMax_; 00196 float inSpan_; 00197 float outMin_; 00198 float outMax_; 00199 float outSpan_; 00200 00201 //The accumulated error, i.e. integral. 00202 float accError_; 00203 //The controller output bias. 00204 float bias_; 00205 00206 //The interval between samples. 00207 float tSample_; 00208 00209 //Controller output as a real world value. 00210 volatile float realOutput_; 00211 00212 float scaledPV; 00213 float scaledSP; 00214 float error; 00215 00216 }; 00217 00218 #endif /* PID_H */
Generated on Thu Jul 21 2022 21:59:59 by
