This is a copy of the Reference Standard PID controller ala controlguru.com
Dependents: PIDHeater Printer PIDHeater82 UltiSaverController
Fork of PID by
PID.h@2:55bf0f813bb4, 2016-01-27 (annotated)
- Committer:
- unix_guru
- Date:
- Wed Jan 27 21:32:11 2016 +0000
- Revision:
- 2:55bf0f813bb4
- Parent:
- 1:117e0c36eb22
- Child:
- 3:316f974b7f98
minor updates to comply with Brett Beauregard's Arduino PID library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
unix_guru | 2:55bf0f813bb4 | 1 | /** |
unix_guru | 2:55bf0f813bb4 | 2 | * @author Aaron Berk |
unix_guru | 2:55bf0f813bb4 | 3 | * |
unix_guru | 2:55bf0f813bb4 | 4 | * @section LICENSE |
unix_guru | 2:55bf0f813bb4 | 5 | * |
unix_guru | 2:55bf0f813bb4 | 6 | * Copyright (c) 2010 ARM Limited |
unix_guru | 2:55bf0f813bb4 | 7 | * |
unix_guru | 2:55bf0f813bb4 | 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
unix_guru | 2:55bf0f813bb4 | 9 | * of this software and associated documentation files (the "Software"), to deal |
unix_guru | 2:55bf0f813bb4 | 10 | * in the Software without restriction, including without limitation the rights |
unix_guru | 2:55bf0f813bb4 | 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
unix_guru | 2:55bf0f813bb4 | 12 | * copies of the Software, and to permit persons to whom the Software is |
unix_guru | 2:55bf0f813bb4 | 13 | * furnished to do so, subject to the following conditions: |
unix_guru | 2:55bf0f813bb4 | 14 | * |
unix_guru | 2:55bf0f813bb4 | 15 | * The above copyright notice and this permission notice shall be included in |
unix_guru | 2:55bf0f813bb4 | 16 | * all copies or substantial portions of the Software. |
unix_guru | 2:55bf0f813bb4 | 17 | * |
unix_guru | 2:55bf0f813bb4 | 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
unix_guru | 2:55bf0f813bb4 | 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
unix_guru | 2:55bf0f813bb4 | 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
unix_guru | 2:55bf0f813bb4 | 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
unix_guru | 2:55bf0f813bb4 | 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
unix_guru | 2:55bf0f813bb4 | 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
unix_guru | 2:55bf0f813bb4 | 24 | * THE SOFTWARE. |
unix_guru | 2:55bf0f813bb4 | 25 | * |
unix_guru | 2:55bf0f813bb4 | 26 | * @section DESCRIPTION |
unix_guru | 2:55bf0f813bb4 | 27 | * |
unix_guru | 2:55bf0f813bb4 | 28 | * A PID controller is a widely used feedback controller commonly found in |
unix_guru | 2:55bf0f813bb4 | 29 | * industry. |
unix_guru | 2:55bf0f813bb4 | 30 | * |
unix_guru | 2:55bf0f813bb4 | 31 | * This library is a port of Brett Beauregard's Arduino PID library: |
unix_guru | 2:55bf0f813bb4 | 32 | * |
unix_guru | 2:55bf0f813bb4 | 33 | * http://www.arduino.cc/playground/Code/PIDLibrary |
unix_guru | 2:55bf0f813bb4 | 34 | * |
unix_guru | 2:55bf0f813bb4 | 35 | * The wikipedia article on PID controllers is a good place to start on |
unix_guru | 2:55bf0f813bb4 | 36 | * understanding how they work: |
unix_guru | 2:55bf0f813bb4 | 37 | * |
unix_guru | 2:55bf0f813bb4 | 38 | * http://en.wikipedia.org/wiki/PID_controller |
unix_guru | 2:55bf0f813bb4 | 39 | * |
unix_guru | 2:55bf0f813bb4 | 40 | * For a clear and elegant explanation of how to implement and tune a |
unix_guru | 2:55bf0f813bb4 | 41 | * controller, the controlguru website by Douglas J. Cooper (who also happened |
unix_guru | 2:55bf0f813bb4 | 42 | * to be Brett's controls professor) is an excellent reference: |
unix_guru | 2:55bf0f813bb4 | 43 | * |
unix_guru | 2:55bf0f813bb4 | 44 | * http://www.controlguru.com/ |
unix_guru | 2:55bf0f813bb4 | 45 | */ |
unix_guru | 2:55bf0f813bb4 | 46 | |
arnaudsuire | 0:d58c1b8d63d9 | 47 | #ifndef PID_H |
arnaudsuire | 0:d58c1b8d63d9 | 48 | #define PID_H |
unix_guru | 2:55bf0f813bb4 | 49 | |
unix_guru | 2:55bf0f813bb4 | 50 | /** |
unix_guru | 2:55bf0f813bb4 | 51 | * Includes |
unix_guru | 2:55bf0f813bb4 | 52 | */ |
unix_guru | 2:55bf0f813bb4 | 53 | #include "mbed.h" |
unix_guru | 2:55bf0f813bb4 | 54 | |
unix_guru | 2:55bf0f813bb4 | 55 | /** |
unix_guru | 2:55bf0f813bb4 | 56 | * Defines |
unix_guru | 2:55bf0f813bb4 | 57 | */ |
unix_guru | 1:117e0c36eb22 | 58 | #define MANUAL_MODE 0 |
unix_guru | 1:117e0c36eb22 | 59 | #define AUTO_MODE 1 |
unix_guru | 2:55bf0f813bb4 | 60 | |
unix_guru | 2:55bf0f813bb4 | 61 | /** |
unix_guru | 2:55bf0f813bb4 | 62 | * Proportional-integral-derivative controller. |
unix_guru | 2:55bf0f813bb4 | 63 | */ |
unix_guru | 2:55bf0f813bb4 | 64 | class PID { |
unix_guru | 2:55bf0f813bb4 | 65 | |
arnaudsuire | 0:d58c1b8d63d9 | 66 | public: |
unix_guru | 2:55bf0f813bb4 | 67 | |
unix_guru | 2:55bf0f813bb4 | 68 | /** |
unix_guru | 2:55bf0f813bb4 | 69 | * Constructor. |
unix_guru | 2:55bf0f813bb4 | 70 | * |
unix_guru | 2:55bf0f813bb4 | 71 | * Sets default limits [0-3.3V], calculates tuning parameters, and sets |
unix_guru | 2:55bf0f813bb4 | 72 | * manual mode with no bias. |
unix_guru | 2:55bf0f813bb4 | 73 | * |
unix_guru | 2:55bf0f813bb4 | 74 | * @param Kc - Tuning parameter |
unix_guru | 2:55bf0f813bb4 | 75 | * @param tauI - Tuning parameter |
unix_guru | 2:55bf0f813bb4 | 76 | * @param tauD - Tuning parameter |
unix_guru | 2:55bf0f813bb4 | 77 | * @param interval PID calculation performed every interval seconds. |
unix_guru | 2:55bf0f813bb4 | 78 | */ |
arnaudsuire | 0:d58c1b8d63d9 | 79 | PID(float Kc, float tauI, float tauD, float interval); |
unix_guru | 2:55bf0f813bb4 | 80 | |
unix_guru | 2:55bf0f813bb4 | 81 | /** |
unix_guru | 2:55bf0f813bb4 | 82 | * Scale from inputs to 0-100%. |
unix_guru | 2:55bf0f813bb4 | 83 | * |
unix_guru | 2:55bf0f813bb4 | 84 | * @param InMin The real world value corresponding to 0%. |
unix_guru | 2:55bf0f813bb4 | 85 | * @param InMax The real world value corresponding to 100%. |
unix_guru | 2:55bf0f813bb4 | 86 | */ |
unix_guru | 2:55bf0f813bb4 | 87 | void setInputLimits(float inMin , float inMax); |
unix_guru | 2:55bf0f813bb4 | 88 | |
unix_guru | 2:55bf0f813bb4 | 89 | /** |
unix_guru | 2:55bf0f813bb4 | 90 | * Scale from outputs to 0-100%. |
unix_guru | 2:55bf0f813bb4 | 91 | * |
unix_guru | 2:55bf0f813bb4 | 92 | * @param outMin The real world value corresponding to 0%. |
unix_guru | 2:55bf0f813bb4 | 93 | * @param outMax The real world value corresponding to 100%. |
unix_guru | 2:55bf0f813bb4 | 94 | */ |
arnaudsuire | 0:d58c1b8d63d9 | 95 | void setOutputLimits(float outMin, float outMax); |
unix_guru | 2:55bf0f813bb4 | 96 | |
unix_guru | 2:55bf0f813bb4 | 97 | /** |
unix_guru | 2:55bf0f813bb4 | 98 | * Calculate PID constants. |
unix_guru | 2:55bf0f813bb4 | 99 | * |
unix_guru | 2:55bf0f813bb4 | 100 | * Allows parameters to be changed on the fly without ruining calculations. |
unix_guru | 2:55bf0f813bb4 | 101 | * |
unix_guru | 2:55bf0f813bb4 | 102 | * @param Kc - Tuning parameter |
unix_guru | 2:55bf0f813bb4 | 103 | * @param tauI - Tuning parameter |
unix_guru | 2:55bf0f813bb4 | 104 | * @param tauD - Tuning parameter |
unix_guru | 2:55bf0f813bb4 | 105 | */ |
arnaudsuire | 0:d58c1b8d63d9 | 106 | void setTunings(float Kc, float tauI, float tauD); |
unix_guru | 2:55bf0f813bb4 | 107 | |
unix_guru | 2:55bf0f813bb4 | 108 | /** |
unix_guru | 2:55bf0f813bb4 | 109 | * Reinitializes controller internals. Automatically |
unix_guru | 2:55bf0f813bb4 | 110 | * called on a manual to auto transition. |
unix_guru | 2:55bf0f813bb4 | 111 | */ |
arnaudsuire | 0:d58c1b8d63d9 | 112 | void reset(void); |
unix_guru | 2:55bf0f813bb4 | 113 | |
unix_guru | 1:117e0c36eb22 | 114 | /** |
unix_guru | 1:117e0c36eb22 | 115 | * Set PID to manual or auto mode. |
unix_guru | 1:117e0c36eb22 | 116 | * |
unix_guru | 1:117e0c36eb22 | 117 | * @param mode 0 -> Manual |
unix_guru | 1:117e0c36eb22 | 118 | * Non-zero -> Auto |
unix_guru | 1:117e0c36eb22 | 119 | */ |
unix_guru | 1:117e0c36eb22 | 120 | void setMode(int mode); |
unix_guru | 2:55bf0f813bb4 | 121 | |
unix_guru | 2:55bf0f813bb4 | 122 | /** |
unix_guru | 2:55bf0f813bb4 | 123 | * Set how fast the PID loop is run. |
unix_guru | 2:55bf0f813bb4 | 124 | * |
unix_guru | 2:55bf0f813bb4 | 125 | * @param interval PID calculation peformed every interval seconds. |
unix_guru | 2:55bf0f813bb4 | 126 | */ |
unix_guru | 2:55bf0f813bb4 | 127 | void setInterval(float interval); |
unix_guru | 2:55bf0f813bb4 | 128 | |
unix_guru | 2:55bf0f813bb4 | 129 | /** |
unix_guru | 2:55bf0f813bb4 | 130 | * Set the set point. |
unix_guru | 2:55bf0f813bb4 | 131 | * |
unix_guru | 2:55bf0f813bb4 | 132 | * @param sp The set point as a real world value. |
unix_guru | 2:55bf0f813bb4 | 133 | */ |
unix_guru | 2:55bf0f813bb4 | 134 | void setSetPoint(float sp); |
unix_guru | 2:55bf0f813bb4 | 135 | |
unix_guru | 2:55bf0f813bb4 | 136 | /** |
unix_guru | 2:55bf0f813bb4 | 137 | * Set the process value. |
unix_guru | 2:55bf0f813bb4 | 138 | * |
unix_guru | 2:55bf0f813bb4 | 139 | * @param pv The process value as a real world value. |
unix_guru | 2:55bf0f813bb4 | 140 | */ |
unix_guru | 2:55bf0f813bb4 | 141 | void setProcessValue(float pv); |
unix_guru | 2:55bf0f813bb4 | 142 | |
unix_guru | 2:55bf0f813bb4 | 143 | /** |
unix_guru | 2:55bf0f813bb4 | 144 | * Set the bias. |
unix_guru | 2:55bf0f813bb4 | 145 | * |
unix_guru | 2:55bf0f813bb4 | 146 | * @param bias The bias for the controller output. |
unix_guru | 2:55bf0f813bb4 | 147 | */ |
arnaudsuire | 0:d58c1b8d63d9 | 148 | void setBias(float bias); |
unix_guru | 2:55bf0f813bb4 | 149 | |
unix_guru | 2:55bf0f813bb4 | 150 | /** |
unix_guru | 2:55bf0f813bb4 | 151 | * PID calculation. |
unix_guru | 2:55bf0f813bb4 | 152 | * |
unix_guru | 2:55bf0f813bb4 | 153 | * @return The controller output as a float between outMin and outMax. |
unix_guru | 2:55bf0f813bb4 | 154 | */ |
unix_guru | 2:55bf0f813bb4 | 155 | float compute(void); |
unix_guru | 2:55bf0f813bb4 | 156 | |
arnaudsuire | 0:d58c1b8d63d9 | 157 | //Getters. |
arnaudsuire | 0:d58c1b8d63d9 | 158 | float getInMin(); |
arnaudsuire | 0:d58c1b8d63d9 | 159 | float getInMax(); |
arnaudsuire | 0:d58c1b8d63d9 | 160 | float getOutMin(); |
arnaudsuire | 0:d58c1b8d63d9 | 161 | float getOutMax(); |
arnaudsuire | 0:d58c1b8d63d9 | 162 | float getInterval(); |
arnaudsuire | 0:d58c1b8d63d9 | 163 | float getPParam(); |
arnaudsuire | 0:d58c1b8d63d9 | 164 | float getIParam(); |
arnaudsuire | 0:d58c1b8d63d9 | 165 | float getDParam(); |
unix_guru | 2:55bf0f813bb4 | 166 | |
arnaudsuire | 0:d58c1b8d63d9 | 167 | private: |
unix_guru | 2:55bf0f813bb4 | 168 | |
arnaudsuire | 0:d58c1b8d63d9 | 169 | bool usingFeedForward; |
unix_guru | 1:117e0c36eb22 | 170 | bool inAuto; |
unix_guru | 2:55bf0f813bb4 | 171 | |
arnaudsuire | 0:d58c1b8d63d9 | 172 | //Actual tuning parameters used in PID calculation. |
arnaudsuire | 0:d58c1b8d63d9 | 173 | float Kc_; |
arnaudsuire | 0:d58c1b8d63d9 | 174 | float tauR_; |
arnaudsuire | 0:d58c1b8d63d9 | 175 | float tauD_; |
unix_guru | 2:55bf0f813bb4 | 176 | |
arnaudsuire | 0:d58c1b8d63d9 | 177 | //Raw tuning parameters. |
arnaudsuire | 0:d58c1b8d63d9 | 178 | float pParam_; |
arnaudsuire | 0:d58c1b8d63d9 | 179 | float iParam_; |
arnaudsuire | 0:d58c1b8d63d9 | 180 | float dParam_; |
unix_guru | 2:55bf0f813bb4 | 181 | |
arnaudsuire | 0:d58c1b8d63d9 | 182 | //The point we want to reach. |
unix_guru | 2:55bf0f813bb4 | 183 | float setPoint_; |
arnaudsuire | 0:d58c1b8d63d9 | 184 | //The thing we measure. |
unix_guru | 2:55bf0f813bb4 | 185 | float processVariable_; |
arnaudsuire | 0:d58c1b8d63d9 | 186 | float prevProcessVariable_; |
arnaudsuire | 0:d58c1b8d63d9 | 187 | //The output that affects the process variable. |
unix_guru | 2:55bf0f813bb4 | 188 | float controllerOutput_; |
arnaudsuire | 0:d58c1b8d63d9 | 189 | float prevControllerOutput_; |
unix_guru | 2:55bf0f813bb4 | 190 | |
arnaudsuire | 0:d58c1b8d63d9 | 191 | //We work in % for calculations so these will scale from |
arnaudsuire | 0:d58c1b8d63d9 | 192 | //real world values to 0-100% and back again. |
arnaudsuire | 0:d58c1b8d63d9 | 193 | float inMin_; |
arnaudsuire | 0:d58c1b8d63d9 | 194 | float inMax_; |
arnaudsuire | 0:d58c1b8d63d9 | 195 | float inSpan_; |
arnaudsuire | 0:d58c1b8d63d9 | 196 | float outMin_; |
arnaudsuire | 0:d58c1b8d63d9 | 197 | float outMax_; |
arnaudsuire | 0:d58c1b8d63d9 | 198 | float outSpan_; |
unix_guru | 2:55bf0f813bb4 | 199 | |
arnaudsuire | 0:d58c1b8d63d9 | 200 | //The accumulated error, i.e. integral. |
arnaudsuire | 0:d58c1b8d63d9 | 201 | float accError_; |
arnaudsuire | 0:d58c1b8d63d9 | 202 | //The controller output bias. |
arnaudsuire | 0:d58c1b8d63d9 | 203 | float bias_; |
unix_guru | 2:55bf0f813bb4 | 204 | |
arnaudsuire | 0:d58c1b8d63d9 | 205 | //The interval between samples. |
unix_guru | 2:55bf0f813bb4 | 206 | float tSample_; |
unix_guru | 2:55bf0f813bb4 | 207 | |
arnaudsuire | 0:d58c1b8d63d9 | 208 | //Controller output as a real world value. |
arnaudsuire | 0:d58c1b8d63d9 | 209 | volatile float realOutput_; |
unix_guru | 2:55bf0f813bb4 | 210 | |
unix_guru | 2:55bf0f813bb4 | 211 | }; |
unix_guru | 2:55bf0f813bb4 | 212 | |
unix_guru | 2:55bf0f813bb4 | 213 | #endif /* PID_H */ |
unix_guru | 2:55bf0f813bb4 | 214 | |
unix_guru | 2:55bf0f813bb4 | 215 | |
arnaudsuire | 0:d58c1b8d63d9 | 216 | |
arnaudsuire | 0:d58c1b8d63d9 | 217 | |
unix_guru | 2:55bf0f813bb4 | 218 | |
unix_guru | 2:55bf0f813bb4 | 219 | |
unix_guru | 2:55bf0f813bb4 | 220 | |
unix_guru | 2:55bf0f813bb4 | 221 | |
arnaudsuire | 0:d58c1b8d63d9 | 222 | |
unix_guru | 2:55bf0f813bb4 | 223 | |
unix_guru | 2:55bf0f813bb4 | 224 | |
unix_guru | 2:55bf0f813bb4 | 225 | |
unix_guru | 2:55bf0f813bb4 | 226 |