FRDM-K64F Code Share / PID

Dependents:   PIDHeater Printer PIDHeater82 UltiSaverController

Fork of PID by Arnaud Suire

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PID.h Source File

PID.h

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