Clara Keng / Mbed 2 deprecated FreeFlyerROS_clarakhl

Dependencies:   mbed ros_lib_kinetic

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PID.h Source File

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      * Set hard controller input limits
00099      *
00100      * @param coutMin The real world value placing a lower bound within the input span.
00101      * @param coutMax The real world value placing an upper bound within the input span.
00102      *
00103     void setConInputLimits(float cinMin, float cinMax);
00104     */
00105 
00106     /**
00107      * Calculate PID constants.
00108      *
00109      * Allows parameters to be changed on the fly without ruining calculations.
00110      *
00111      * @param Kc - Tuning parameter
00112      * @param tauI - Tuning parameter
00113      * @param tauD - Tuning parameter
00114      */
00115     void setTunings(float Kc, float tauI, float tauD);
00116 
00117     /**
00118      * Reinitializes controller internals. Automatically
00119      * called on a manual to auto transition.
00120      */
00121     void reset(void);
00122     
00123     /**
00124      * Set PID to manual or auto mode.
00125      *
00126      * @param mode        0 -> Manual
00127      *             Non-zero -> Auto
00128      */
00129     void setMode(int mode);
00130     
00131     /**
00132      * Set how fast the PID loop is run.
00133      *
00134      * @param interval PID calculation peformed every interval seconds.
00135      */
00136     void setInterval(float interval);
00137     
00138     /**
00139      * Set the set point.
00140      *
00141      * @param sp The set point as a real world value.
00142      */
00143     void setSetPoint(float sp);
00144     
00145     /**
00146      * Set the process value.
00147      *
00148      * @param pv The process value as a real world value.
00149      */
00150     void setProcessValue(float pv);
00151     
00152     /**
00153      * Set the bias.
00154      *
00155      * @param bias The bias for the controller output.
00156      */
00157     void setBias(float bias);
00158     
00159     void setAccLimit(float accLimit);
00160 
00161     /**
00162      * PID calculation.
00163      *
00164      * @return The controller output as a float between outMin and outMax.
00165      */
00166     float compute(void);
00167 
00168     //Getters.
00169     float getInMin();
00170     float getInMax();
00171     float getOutMin();
00172     float getOutMax();
00173     float getInterval();
00174     float getPParam();
00175     float getIParam();
00176     float getDParam();
00177     float getAccLimit();
00178     float getAccError();
00179     float getSetPoint();
00180 
00181 private:
00182 
00183     bool usingFeedForward;
00184     bool inAuto;
00185 
00186     //Actual tuning parameters used in PID calculation.
00187     float Kc_;
00188     float tauR_;
00189     float tauD_;
00190     
00191     //Raw tuning parameters.
00192     float pParam_;
00193     float iParam_;
00194     float dParam_;
00195     
00196     // The point we want to reach.
00197     float setPoint_;         
00198     //The thing we measure.
00199     float processVariable_;  
00200     float prevProcessVariable_;
00201     //The output that affects the process variable.
00202     float controllerOutput_; 
00203     float prevControllerOutput_;
00204 
00205     //We work in % for calculations so these will scale from
00206     //real world values to 0-100% and back again.
00207     float inMin_;
00208     float inMax_;
00209     float inSpan_;
00210     float outMin_;
00211     float outMax_;
00212     float outSpan_;
00213 
00214     //The accumulated error, i.e. integral.
00215     float accError_;
00216     float accLimit_;
00217     float error_;
00218     //The controller output bias.
00219     float bias_;
00220 
00221     //The interval between samples.
00222     float tSample_;          
00223 
00224     //Controller output as a real world value.
00225     volatile float realOutput_;
00226 
00227 };
00228 
00229 #endif /* PID_H */