123

Fork of PID by Aaron Berk

Committer:
Amber77
Date:
Wed Aug 30 02:08:29 2017 +0000
Revision:
1:32c9f196607d
Parent:
0:6e12a3e5af19
123

Who changed what in which revision?

UserRevisionLine numberNew contents of line
aberk 0:6e12a3e5af19 1 /**
aberk 0:6e12a3e5af19 2 * @author Aaron Berk
aberk 0:6e12a3e5af19 3 *
aberk 0:6e12a3e5af19 4 * @section LICENSE
aberk 0:6e12a3e5af19 5 *
aberk 0:6e12a3e5af19 6 * Copyright (c) 2010 ARM Limited
aberk 0:6e12a3e5af19 7 *
aberk 0:6e12a3e5af19 8 * Permission is hereby granted, free of charge, to any person obtaining a copy
aberk 0:6e12a3e5af19 9 * of this software and associated documentation files (the "Software"), to deal
aberk 0:6e12a3e5af19 10 * in the Software without restriction, including without limitation the rights
aberk 0:6e12a3e5af19 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
aberk 0:6e12a3e5af19 12 * copies of the Software, and to permit persons to whom the Software is
aberk 0:6e12a3e5af19 13 * furnished to do so, subject to the following conditions:
aberk 0:6e12a3e5af19 14 *
aberk 0:6e12a3e5af19 15 * The above copyright notice and this permission notice shall be included in
aberk 0:6e12a3e5af19 16 * all copies or substantial portions of the Software.
aberk 0:6e12a3e5af19 17 *
aberk 0:6e12a3e5af19 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
aberk 0:6e12a3e5af19 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
aberk 0:6e12a3e5af19 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
aberk 0:6e12a3e5af19 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
aberk 0:6e12a3e5af19 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
aberk 0:6e12a3e5af19 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
aberk 0:6e12a3e5af19 24 * THE SOFTWARE.
aberk 0:6e12a3e5af19 25 *
aberk 0:6e12a3e5af19 26 * @section DESCRIPTION
aberk 0:6e12a3e5af19 27 *
aberk 0:6e12a3e5af19 28 * A PID controller is a widely used feedback controller commonly found in
aberk 0:6e12a3e5af19 29 * industry.
aberk 0:6e12a3e5af19 30 *
aberk 0:6e12a3e5af19 31 * This library is a port of Brett Beauregard's Arduino PID library:
aberk 0:6e12a3e5af19 32 *
aberk 0:6e12a3e5af19 33 * http://www.arduino.cc/playground/Code/PIDLibrary
aberk 0:6e12a3e5af19 34 *
aberk 0:6e12a3e5af19 35 * The wikipedia article on PID controllers is a good place to start on
aberk 0:6e12a3e5af19 36 * understanding how they work:
aberk 0:6e12a3e5af19 37 *
aberk 0:6e12a3e5af19 38 * http://en.wikipedia.org/wiki/PID_controller
aberk 0:6e12a3e5af19 39 *
aberk 0:6e12a3e5af19 40 * For a clear and elegant explanation of how to implement and tune a
aberk 0:6e12a3e5af19 41 * controller, the controlguru website by Douglas J. Cooper (who also happened
aberk 0:6e12a3e5af19 42 * to be Brett's controls professor) is an excellent reference:
aberk 0:6e12a3e5af19 43 *
aberk 0:6e12a3e5af19 44 * http://www.controlguru.com/
aberk 0:6e12a3e5af19 45 */
aberk 0:6e12a3e5af19 46
aberk 0:6e12a3e5af19 47 /**
aberk 0:6e12a3e5af19 48 * Includes
aberk 0:6e12a3e5af19 49 */
aberk 0:6e12a3e5af19 50 #include "mbed.h"
aberk 0:6e12a3e5af19 51
Amber77 1:32c9f196607d 52 class PID
Amber77 1:32c9f196607d 53 {
aberk 0:6e12a3e5af19 54
aberk 0:6e12a3e5af19 55
Amber77 1:32c9f196607d 56 public:
aberk 0:6e12a3e5af19 57
Amber77 1:32c9f196607d 58 //Constants used in some of the functions below
Amber77 1:32c9f196607d 59 #define AUTOMATIC 1
Amber77 1:32c9f196607d 60 #define MANUAL 0
Amber77 1:32c9f196607d 61 #define DIRECT 0
Amber77 1:32c9f196607d 62 #define REVERSE 1
aberk 0:6e12a3e5af19 63
aberk 0:6e12a3e5af19 64
Amber77 1:32c9f196607d 65 //commonly used functions **************************************************************************
Amber77 1:32c9f196607d 66 PID(double*, double*, double*, // * constructor. links the PID to the Input, Output, and
Amber77 1:32c9f196607d 67 double, double, double, int, float*, float*); // Setpoint. Initial tuning parameters are also set here
Amber77 1:32c9f196607d 68
Amber77 1:32c9f196607d 69 void SetMode(int Mode); // * sets PID to either Manual (0) or Auto (non-0)
aberk 0:6e12a3e5af19 70
Amber77 1:32c9f196607d 71 double Compute(float*); // * performs the PID calculation. it should be
Amber77 1:32c9f196607d 72 // called every time loop() cycles. ON/OFF and
Amber77 1:32c9f196607d 73 // calculation frequency can be set using SetMode
Amber77 1:32c9f196607d 74 // SetSampleTime respectively
aberk 0:6e12a3e5af19 75
Amber77 1:32c9f196607d 76 void SetOutputLimits(double, double); //clamps the output to a specific range. 0-255 by default, but
Amber77 1:32c9f196607d 77 //it's likely the user will want to change this depending on
Amber77 1:32c9f196607d 78 //the application
Amber77 1:32c9f196607d 79
aberk 0:6e12a3e5af19 80
aberk 0:6e12a3e5af19 81
Amber77 1:32c9f196607d 82 //available but not commonly used functions ********************************************************
Amber77 1:32c9f196607d 83 void SetTunings(double, double, // * While most users will set the tunings once in the
Amber77 1:32c9f196607d 84 double); // constructor, this function gives the user the option
Amber77 1:32c9f196607d 85 // of changing tunings during runtime for Adaptive control
Amber77 1:32c9f196607d 86 void SetControllerDirection(int); // * Sets the Direction, or "Action" of the controller. DIRECT
Amber77 1:32c9f196607d 87 // means the output will increase when error is positive. REVERSE
Amber77 1:32c9f196607d 88 // means the opposite. it's very unlikely that this will be needed
Amber77 1:32c9f196607d 89 // once it is set in the constructor.
Amber77 1:32c9f196607d 90 void SetSampleTime(int); // * sets the frequency, in Milliseconds, with which
Amber77 1:32c9f196607d 91 // the PID calculation is performed. default is 100
Amber77 1:32c9f196607d 92
Amber77 1:32c9f196607d 93
Amber77 1:32c9f196607d 94
Amber77 1:32c9f196607d 95 //Display functions ****************************************************************
Amber77 1:32c9f196607d 96 double GetKp(); // These functions query the pid for interal values.
Amber77 1:32c9f196607d 97 double GetKi(); // they were created mainly for the pid front-end,
Amber77 1:32c9f196607d 98 double GetKd(); // where it's important to know what is actually
Amber77 1:32c9f196607d 99 int GetMode(); // inside the PID.
Amber77 1:32c9f196607d 100 int GetDirection(); //
aberk 0:6e12a3e5af19 101
Amber77 1:32c9f196607d 102 private:
Amber77 1:32c9f196607d 103 void Initialize();
Amber77 1:32c9f196607d 104
Amber77 1:32c9f196607d 105 double dispKp; // * we'll hold on to the tuning parameters in user-entered
Amber77 1:32c9f196607d 106 double dispKi; // format for display purposes
Amber77 1:32c9f196607d 107 double dispKd; //
Amber77 1:32c9f196607d 108
Amber77 1:32c9f196607d 109 double kp; // * (P)roportional Tuning Parameter
Amber77 1:32c9f196607d 110 double ki; // * (I)ntegral Tuning Parameter
Amber77 1:32c9f196607d 111 double kd; // * (D)erivative Tuning Parameter
Amber77 1:32c9f196607d 112
Amber77 1:32c9f196607d 113 int controllerDirection;
aberk 0:6e12a3e5af19 114
Amber77 1:32c9f196607d 115 double *myInput; // * Pointers to the Input, Output, and Setpoint variables
Amber77 1:32c9f196607d 116 double *myOutput; // This creates a hard link between the variables and the
Amber77 1:32c9f196607d 117 double *mySetpoint; // PID, freeing the user from having to constantly tell us
Amber77 1:32c9f196607d 118 // what these values are. with pointers we'll just know.
Amber77 1:32c9f196607d 119
Amber77 1:32c9f196607d 120 float lastTime;
Amber77 1:32c9f196607d 121 double ITerm, lastInput;
aberk 0:6e12a3e5af19 122
Amber77 1:32c9f196607d 123 float SampleTime;
Amber77 1:32c9f196607d 124 double outMin, outMax;
Amber77 1:32c9f196607d 125 bool inAuto;
Amber77 1:32c9f196607d 126 };