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.
pid_controller.h
00001 //********************************************************************************* 00002 // Arduino PID Library Version 1.0.1 Modified Version for C++ 00003 // Platform Independent 00004 // 00005 // Revision: 1.1 00006 // 00007 // Description: The PID Controller module originally meant for Arduino made 00008 // platform independent. Some small bugs present in the original Arduino source 00009 // have been rectified as well. 00010 // 00011 // For a detailed explanation of the theory behind this library, go to: 00012 // http://brettbeauregard.com/blog/2011/04/improving-the-beginners-pid-introduction/ 00013 // 00014 // Revisions can be found here: 00015 // https://github.com/tcleg 00016 // 00017 // Modified by: Trent Cleghorn , <trentoncleghorn@gmail.com> 00018 // 00019 // Copyright (C) Brett Beauregard , <br3ttb@gmail.com> 00020 // 00021 // GPLv3 License 00022 // 00023 // This program is free software: you can redistribute it and/or modify it under 00024 // the terms of the GNU General Public License as published by the Free Software 00025 // Foundation, either version 3 of the License, or (at your option) any later 00026 // version. 00027 // 00028 // This program is distributed in the hope that it will be useful, but WITHOUT ANY 00029 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 00030 // PARTICULAR PURPOSE. See the GNU General Public License for more details. 00031 // 00032 // You should have received a copy of the GNU General Public License along with 00033 // this program. If not, see <http://www.gnu.org/licenses/>. 00034 //********************************************************************************* 00035 00036 // 00037 // Header Guard 00038 // 00039 #ifndef PID_CONTROLLER_H 00040 #define PID_CONTROLLER_H 00041 00042 //********************************************************************************* 00043 // Headers 00044 //********************************************************************************* 00045 #include <stdint.h> 00046 #include <stdbool.h> 00047 00048 //********************************************************************************* 00049 // Macros and Globals 00050 //********************************************************************************* 00051 00052 typedef enum 00053 { 00054 MANUAL, 00055 AUTOMATIC 00056 } 00057 PIDMode; 00058 00059 typedef enum 00060 { 00061 DIRECT, 00062 REVERSE 00063 } 00064 PIDDirection; 00065 00066 //********************************************************************************* 00067 // Class 00068 //********************************************************************************* 00069 00070 class 00071 PIDControl 00072 { 00073 public: 00074 // 00075 // Constructor 00076 // Description: 00077 // Initializes the PIDControl instantiation. This should be called at 00078 // least once before any other PID functions are called on the 00079 // instantiation. 00080 // Parameters: 00081 // kp - Positive P gain constant value. 00082 // ki - Positive I gain constant value. 00083 // kd - Positive D gain constant value. 00084 // sampleTimeSeconds - Interval in seconds on which PIDCompute will be 00085 // called. 00086 // minOutput - Constrain PID output to this minimum value. 00087 // maxOutput - Constrain PID output to this maximum value. 00088 // mode - Tells how the controller should respond if the user has 00089 // taken over manual control or not. 00090 // MANUAL: PID controller is off. User can manually control the 00091 // output. 00092 // AUTOMATIC: PID controller is on. PID controller controls the 00093 // output. 00094 // controllerDirection - The sense of direction of the controller 00095 // DIRECT: A positive setpoint gives a positive output. 00096 // REVERSE: A positive setpoint gives a negative output. 00097 // Returns: 00098 // Nothing. 00099 // 00100 PIDControl(float kp, float ki, float kd, float sampleTimeSeconds, 00101 float minOutput, float maxOutput, PIDMode mode, 00102 PIDDirection controllerDirection); 00103 00104 // 00105 // PID Compute 00106 // Description: 00107 // Should be called on a regular interval defined by sampleTimeSeconds. 00108 // Typically, PIDSetpointSet and PIDInputSet should be called 00109 // immediately before PIDCompute. 00110 // Parameters: 00111 // None. 00112 // Returns: 00113 // True if in AUTOMATIC. False if in MANUAL. 00114 // 00115 bool PIDCompute(); 00116 00117 // 00118 // PID Mode Set 00119 // Description: 00120 // Sets the PID controller to a new mode. Tells how the controller 00121 // should respond if the user has taken over manual control or not. 00122 // Parameters: 00123 // mode - 00124 // MANUAL: PID controller is off. User can manually control the 00125 // output. 00126 // AUTOMATIC: PID controller is on. PID controller controls the 00127 // output. 00128 // Returns: 00129 // Nothing. 00130 // 00131 void PIDModeSet(PIDMode mode); 00132 00133 // 00134 // PID Output Limits Set 00135 // Description: 00136 // Sets the new output limits. The new limits are applied to the PID 00137 // immediately. 00138 // Parameters: 00139 // min - Constrain PID output to this minimum value. 00140 // max - Constrain PID output to this maximum value. 00141 // Returns: 00142 // Nothing. 00143 // 00144 void PIDOutputLimitsSet(float min, float max); 00145 00146 // 00147 // PID Tunings Set 00148 // Description: 00149 // Sets the new gain constant values. 00150 // Parameters: 00151 // kp - Positive P gain constant value. 00152 // ki - Positive I gain constant value. 00153 // kd - Positive D gain constant value. 00154 // Returns: 00155 // Nothing. 00156 // 00157 void PIDTuningsSet(float kp, float ki, float kd); 00158 00159 // 00160 // PID Tuning Gain Constant P Set 00161 // Description: 00162 // Sets the proportional gain constant value. 00163 // Parameters: 00164 // kp - Positive P gain constant value. 00165 // Returns: 00166 // Nothing. 00167 // 00168 void PIDTuningKpSet(float kp); 00169 00170 // 00171 // PID Tuning Gain Constant I Set 00172 // Description: 00173 // Sets the proportional gain constant value. 00174 // Parameters: 00175 // ki - Positive I gain constant value. 00176 // Returns: 00177 // Nothing. 00178 // 00179 void PIDTuningKiSet(float ki); 00180 00181 // 00182 // PID Tuning Gain Constant D Set 00183 // Description: 00184 // Sets the proportional gain constant value. 00185 // Parameters: 00186 // kd - Positive D gain constant value. 00187 // Returns: 00188 // Nothing. 00189 // 00190 void PIDTuningKdSet(float kd); 00191 00192 // 00193 // PID Controller Direction Set 00194 // Description: 00195 // Sets the new controller direction. 00196 // Parameters: 00197 // controllerDirection - The sense of direction of the controller 00198 // DIRECT: A positive setpoint gives a positive output 00199 // REVERSE: A positive setpoint gives a negative output 00200 // Returns: 00201 // Nothing. 00202 // 00203 void PIDControllerDirectionSet(PIDDirection controllerDirection); 00204 00205 // 00206 // PID Sample Time Set 00207 // Description: 00208 // Sets the new sampling time (in seconds). 00209 // Parameters: 00210 // sampleTimeSeconds - Interval in seconds on which PIDCompute will be 00211 // called. 00212 // Returns: 00213 // Nothing. 00214 // 00215 void PIDSampleTimeSet(float sampleTimeSeconds); 00216 00217 // 00218 // PID Setpoint Set 00219 // Description: 00220 // Alters the setpoint the PID controller will try to achieve. 00221 // Parameters: 00222 // setpoint - The desired setpoint the PID controller will try to 00223 // obtain. 00224 // Returns: 00225 // Nothing. 00226 // 00227 inline void PIDSetpointSet(float setpoint) { this->setpoint = setpoint; } 00228 00229 // 00230 // PID Input Set 00231 // Description: 00232 // Should be called before calling PIDCompute so the PID controller 00233 // will have an updated input value to work with. 00234 // Parameters: 00235 // input - The value the controller will work with. 00236 // Returns: 00237 // Nothing. 00238 // 00239 inline void PIDInputSet(float input) { this->input = input; } 00240 00241 // 00242 // PID Output Get 00243 // Description: 00244 // Typically, this function is called after PIDCompute in order to 00245 // retrieve the output of the controller. 00246 // Parameters: 00247 // None. 00248 // Returns: 00249 // The output of the specific PID controller. 00250 // 00251 inline float PIDOutputGet() { return this->output; } 00252 00253 // 00254 // PID Proportional Gain Constant Get 00255 // Description: 00256 // Returns the proportional gain constant value the particular 00257 // controller is set to. 00258 // Parameters: 00259 // None. 00260 // Returns: 00261 // The proportional gain constant. 00262 // 00263 inline float PIDKpGet() { return this->dispKp; } 00264 00265 // 00266 // PID Integral Gain Constant Get 00267 // Description: 00268 // Returns the integral gain constant value the particular 00269 // controller is set to. 00270 // Parameters: 00271 // None. 00272 // Returns: 00273 // The integral gain constant. 00274 // 00275 inline float PIDKiGet() { return this->dispKi; } 00276 00277 // 00278 // PID Derivative Gain Constant Get 00279 // Description: 00280 // Returns the derivative gain constant value the particular 00281 // controller is set to. 00282 // Parameters: 00283 // None. 00284 // Returns: 00285 // The derivative gain constant. 00286 // 00287 inline float PIDKdGet() { return this->dispKd; } 00288 00289 // 00290 // PID Mode Get 00291 // Description: 00292 // Returns the mode the particular controller is set to. 00293 // Parameters: 00294 // None. 00295 // Returns: 00296 // MANUAL or AUTOMATIC depending on what the user set the 00297 // controller to. 00298 // 00299 inline PIDMode PIDModeGet() { return this->mode; } 00300 00301 // 00302 // PID Direction Get 00303 // Description: 00304 // Returns the direction the particular controller is set to. 00305 // Parameters: 00306 // None. 00307 // Returns: 00308 // DIRECT or REVERSE depending on what the user set the 00309 // controller to. 00310 // 00311 inline PIDDirection PIDDirectionGet() { return this->controllerDirection; } 00312 00313 private: 00314 // 00315 // Input to the PID Controller 00316 // 00317 float input; 00318 00319 // 00320 // Previous input to the PID Controller 00321 // 00322 float lastInput; 00323 00324 // 00325 // Output of the PID Controller 00326 // 00327 float output; 00328 00329 // 00330 // Gain constant values that were passed by the user 00331 // These are for display purposes 00332 // 00333 float dispKp; 00334 float dispKi; 00335 float dispKd; 00336 00337 // 00338 // Gain constant values that the controller alters for 00339 // its own use 00340 // 00341 float alteredKp; 00342 float alteredKi; 00343 float alteredKd; 00344 00345 // 00346 // The Integral Term 00347 // 00348 float iTerm; 00349 00350 // 00351 // The interval (in seconds) on which the PID controller 00352 // will be called 00353 // 00354 float sampleTime; 00355 00356 // 00357 // The values that the output will be constrained to 00358 // 00359 float outMin; 00360 float outMax; 00361 00362 // 00363 // The user chosen operating point 00364 // 00365 float setpoint; 00366 00367 // 00368 // The sense of direction of the controller 00369 // DIRECT: A positive setpoint gives a positive output 00370 // REVERSE: A positive setpoint gives a negative output 00371 // 00372 PIDDirection controllerDirection; 00373 00374 // 00375 // Tells how the controller should respond if the user has 00376 // taken over manual control or not 00377 // MANUAL: PID controller is off. 00378 // AUTOMATIC: PID controller is on. 00379 // 00380 PIDMode mode; 00381 }; 00382 00383 #endif // PID_CONTROLLER_H
Generated on Fri Jul 15 2022 09:44:08 by
1.7.2