1
Fork of PID by
PID.h@1:28542afc96d0, 2017-02-15 (annotated)
- Committer:
- d15321854
- Date:
- Wed Feb 15 12:33:10 2017 +0000
- Revision:
- 1:28542afc96d0
- Parent:
- 0:6e12a3e5af19
123
Who changed what in which revision?
User | Revision | Line number | New 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 | |
d15321854 | 1:28542afc96d0 | 52 | class PID |
d15321854 | 1:28542afc96d0 | 53 | { |
aberk | 0:6e12a3e5af19 | 54 | |
aberk | 0:6e12a3e5af19 | 55 | |
d15321854 | 1:28542afc96d0 | 56 | public: |
aberk | 0:6e12a3e5af19 | 57 | |
d15321854 | 1:28542afc96d0 | 58 | //Constants used in some of the functions below |
d15321854 | 1:28542afc96d0 | 59 | #define AUTOMATIC 1 |
d15321854 | 1:28542afc96d0 | 60 | #define MANUAL 0 |
d15321854 | 1:28542afc96d0 | 61 | #define DIRECT 0 |
d15321854 | 1:28542afc96d0 | 62 | #define REVERSE 1 |
aberk | 0:6e12a3e5af19 | 63 | |
aberk | 0:6e12a3e5af19 | 64 | |
d15321854 | 1:28542afc96d0 | 65 | //commonly used functions ************************************************************************** |
d15321854 | 1:28542afc96d0 | 66 | PID(double*, double*, double*, // * constructor. links the PID to the Input, Output, and |
d15321854 | 1:28542afc96d0 | 67 | double, double, double, int, float*, float*); // Setpoint. Initial tuning parameters are also set here |
d15321854 | 1:28542afc96d0 | 68 | |
d15321854 | 1:28542afc96d0 | 69 | void SetMode(int Mode); // * sets PID to either Manual (0) or Auto (non-0) |
aberk | 0:6e12a3e5af19 | 70 | |
d15321854 | 1:28542afc96d0 | 71 | double Compute(float*); // * performs the PID calculation. it should be |
d15321854 | 1:28542afc96d0 | 72 | // called every time loop() cycles. ON/OFF and |
d15321854 | 1:28542afc96d0 | 73 | // calculation frequency can be set using SetMode |
d15321854 | 1:28542afc96d0 | 74 | // SetSampleTime respectively |
aberk | 0:6e12a3e5af19 | 75 | |
d15321854 | 1:28542afc96d0 | 76 | void SetOutputLimits(double, double); //clamps the output to a specific range. 0-255 by default, but |
d15321854 | 1:28542afc96d0 | 77 | //it's likely the user will want to change this depending on |
d15321854 | 1:28542afc96d0 | 78 | //the application |
d15321854 | 1:28542afc96d0 | 79 | |
aberk | 0:6e12a3e5af19 | 80 | |
aberk | 0:6e12a3e5af19 | 81 | |
d15321854 | 1:28542afc96d0 | 82 | //available but not commonly used functions ******************************************************** |
d15321854 | 1:28542afc96d0 | 83 | void SetTunings(double, double, // * While most users will set the tunings once in the |
d15321854 | 1:28542afc96d0 | 84 | double); // constructor, this function gives the user the option |
d15321854 | 1:28542afc96d0 | 85 | // of changing tunings during runtime for Adaptive control |
d15321854 | 1:28542afc96d0 | 86 | void SetControllerDirection(int); // * Sets the Direction, or "Action" of the controller. DIRECT |
d15321854 | 1:28542afc96d0 | 87 | // means the output will increase when error is positive. REVERSE |
d15321854 | 1:28542afc96d0 | 88 | // means the opposite. it's very unlikely that this will be needed |
d15321854 | 1:28542afc96d0 | 89 | // once it is set in the constructor. |
d15321854 | 1:28542afc96d0 | 90 | void SetSampleTime(int); // * sets the frequency, in Milliseconds, with which |
d15321854 | 1:28542afc96d0 | 91 | // the PID calculation is performed. default is 100 |
d15321854 | 1:28542afc96d0 | 92 | |
d15321854 | 1:28542afc96d0 | 93 | |
d15321854 | 1:28542afc96d0 | 94 | |
d15321854 | 1:28542afc96d0 | 95 | //Display functions **************************************************************** |
d15321854 | 1:28542afc96d0 | 96 | double GetKp(); // These functions query the pid for interal values. |
d15321854 | 1:28542afc96d0 | 97 | double GetKi(); // they were created mainly for the pid front-end, |
d15321854 | 1:28542afc96d0 | 98 | double GetKd(); // where it's important to know what is actually |
d15321854 | 1:28542afc96d0 | 99 | int GetMode(); // inside the PID. |
d15321854 | 1:28542afc96d0 | 100 | int GetDirection(); // |
aberk | 0:6e12a3e5af19 | 101 | |
d15321854 | 1:28542afc96d0 | 102 | private: |
d15321854 | 1:28542afc96d0 | 103 | void Initialize(); |
d15321854 | 1:28542afc96d0 | 104 | |
d15321854 | 1:28542afc96d0 | 105 | double dispKp; // * we'll hold on to the tuning parameters in user-entered |
d15321854 | 1:28542afc96d0 | 106 | double dispKi; // format for display purposes |
d15321854 | 1:28542afc96d0 | 107 | double dispKd; // |
d15321854 | 1:28542afc96d0 | 108 | |
d15321854 | 1:28542afc96d0 | 109 | double kp; // * (P)roportional Tuning Parameter |
d15321854 | 1:28542afc96d0 | 110 | double ki; // * (I)ntegral Tuning Parameter |
d15321854 | 1:28542afc96d0 | 111 | double kd; // * (D)erivative Tuning Parameter |
d15321854 | 1:28542afc96d0 | 112 | |
d15321854 | 1:28542afc96d0 | 113 | int controllerDirection; |
aberk | 0:6e12a3e5af19 | 114 | |
d15321854 | 1:28542afc96d0 | 115 | double *myInput; // * Pointers to the Input, Output, and Setpoint variables |
d15321854 | 1:28542afc96d0 | 116 | double *myOutput; // This creates a hard link between the variables and the |
d15321854 | 1:28542afc96d0 | 117 | double *mySetpoint; // PID, freeing the user from having to constantly tell us |
d15321854 | 1:28542afc96d0 | 118 | // what these values are. with pointers we'll just know. |
d15321854 | 1:28542afc96d0 | 119 | |
d15321854 | 1:28542afc96d0 | 120 | float lastTime; |
d15321854 | 1:28542afc96d0 | 121 | double ITerm, lastInput; |
aberk | 0:6e12a3e5af19 | 122 | |
d15321854 | 1:28542afc96d0 | 123 | float SampleTime; |
d15321854 | 1:28542afc96d0 | 124 | double outMin, outMax; |
d15321854 | 1:28542afc96d0 | 125 | bool inAuto; |
d15321854 | 1:28542afc96d0 | 126 | }; |