Amber Tang / PID02

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  * @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 /**
00048  * Includes
00049  */
00050 #include "mbed.h"
00051 
00052 class PID
00053 {
00054 
00055 
00056   public:
00057 
00058   //Constants used in some of the functions below
00059   #define AUTOMATIC 1
00060   #define MANUAL  0
00061   #define DIRECT  0
00062   #define REVERSE  1
00063 
00064     
00065   //commonly used functions **************************************************************************
00066     PID(double*, double*, double*,        // * constructor.  links the PID to the Input, Output, and 
00067         double, double, double, int, float*, float*);     //   Setpoint.  Initial tuning parameters are also set here
00068   
00069     void SetMode(int Mode);               // * sets PID to either Manual (0) or Auto (non-0)
00070 
00071     double Compute(float*);                       // * performs the PID calculation.  it should be
00072                                           //   called every time loop() cycles. ON/OFF and
00073                                           //   calculation frequency can be set using SetMode
00074                                           //   SetSampleTime respectively
00075 
00076     void SetOutputLimits(double, double); //clamps the output to a specific range. 0-255 by default, but
00077                       //it's likely the user will want to change this depending on
00078                       //the application
00079   
00080 
00081 
00082   //available but not commonly used functions ********************************************************
00083     void SetTunings(double, double,       // * While most users will set the tunings once in the 
00084                     double);            //   constructor, this function gives the user the option
00085                                           //   of changing tunings during runtime for Adaptive control
00086   void SetControllerDirection(int);   // * Sets the Direction, or "Action" of the controller. DIRECT
00087                       //   means the output will increase when error is positive. REVERSE
00088                       //   means the opposite.  it's very unlikely that this will be needed
00089                       //   once it is set in the constructor.
00090     void SetSampleTime(int);              // * sets the frequency, in Milliseconds, with which 
00091                                           //   the PID calculation is performed.  default is 100
00092                       
00093                       
00094                       
00095   //Display functions ****************************************************************
00096   double GetKp();             // These functions query the pid for interal values.
00097   double GetKi();             //  they were created mainly for the pid front-end,
00098   double GetKd();             // where it's important to know what is actually 
00099   int GetMode();              //  inside the PID.
00100   int GetDirection();           //
00101 
00102   private:
00103   void Initialize();
00104   
00105   double dispKp;        // * we'll hold on to the tuning parameters in user-entered 
00106   double dispKi;        //   format for display purposes
00107   double dispKd;        //
00108     
00109   double kp;                  // * (P)roportional Tuning Parameter
00110     double ki;                  // * (I)ntegral Tuning Parameter
00111     double kd;                  // * (D)erivative Tuning Parameter
00112 
00113   int controllerDirection;
00114 
00115     double *myInput;              // * Pointers to the Input, Output, and Setpoint variables
00116     double *myOutput;             //   This creates a hard link between the variables and the 
00117     double *mySetpoint;           //   PID, freeing the user from having to constantly tell us
00118                                   //   what these values are.  with pointers we'll just know.
00119         
00120   float lastTime;
00121   double ITerm, lastInput;
00122 
00123   float SampleTime;
00124   double outMin, outMax;
00125   bool inAuto;
00126 };