Fork of PID by Kamil Foryszewski

Committer:
aberk
Date:
Thu Sep 02 16:48:10 2010 +0000
Revision:
0:6e12a3e5af19
Child:
1:0ffb635770b3
Version 1.0

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 "PID.h"
aberk 0:6e12a3e5af19 51
aberk 0:6e12a3e5af19 52 PID::PID(float Kc, float tauI, float tauD, float interval) {
aberk 0:6e12a3e5af19 53
aberk 0:6e12a3e5af19 54 usingFeedForward = false;
aberk 0:6e12a3e5af19 55 inAuto = false;
aberk 0:6e12a3e5af19 56
aberk 0:6e12a3e5af19 57 //Default the limits to the full range of I/O: 3.3V
aberk 0:6e12a3e5af19 58 //Make sure to set these to more appropriate limits for
aberk 0:6e12a3e5af19 59 //your application.
aberk 0:6e12a3e5af19 60 setInputLimits(0.0, 3.3);
aberk 0:6e12a3e5af19 61 setOutputLimits(0.0, 3.3);
aberk 0:6e12a3e5af19 62
aberk 0:6e12a3e5af19 63 tSample_ = interval;
aberk 0:6e12a3e5af19 64
aberk 0:6e12a3e5af19 65 setTunings(Kc, tauI, tauD);
aberk 0:6e12a3e5af19 66
aberk 0:6e12a3e5af19 67 setPoint_ = 0.0;
aberk 0:6e12a3e5af19 68 processVariable_ = 0.0;
aberk 0:6e12a3e5af19 69 prevProcessVariable_ = 0.0;
aberk 0:6e12a3e5af19 70 controllerOutput_ = 0.0;
aberk 0:6e12a3e5af19 71 prevControllerOutput_ = 0.0;
aberk 0:6e12a3e5af19 72
aberk 0:6e12a3e5af19 73 accError_ = 0.0;
aberk 0:6e12a3e5af19 74 bias_ = 0.0;
aberk 0:6e12a3e5af19 75
aberk 0:6e12a3e5af19 76 realOutput_ = 0.0;
aberk 0:6e12a3e5af19 77
aberk 0:6e12a3e5af19 78 }
aberk 0:6e12a3e5af19 79
aberk 0:6e12a3e5af19 80 void PID::setInputLimits(float inMin, float inMax) {
aberk 0:6e12a3e5af19 81
aberk 0:6e12a3e5af19 82 //Make sure we haven't been given impossible values.
aberk 0:6e12a3e5af19 83 if (inMin >= inMax) {
aberk 0:6e12a3e5af19 84 return;
aberk 0:6e12a3e5af19 85 }
aberk 0:6e12a3e5af19 86
aberk 0:6e12a3e5af19 87 //Rescale the working variables to reflect the changes.
aberk 0:6e12a3e5af19 88 prevProcessVariable_ *= (inMax - inMin) / inSpan_;
aberk 0:6e12a3e5af19 89 accError_ *= (inMax - inMin) / inSpan_;
aberk 0:6e12a3e5af19 90
aberk 0:6e12a3e5af19 91 //Make sure the working variables are within the new limits.
aberk 0:6e12a3e5af19 92 if (prevProcessVariable_ > 1) {
aberk 0:6e12a3e5af19 93 prevProcessVariable_ = 1;
aberk 0:6e12a3e5af19 94 } else if (prevProcessVariable_ < 0) {
aberk 0:6e12a3e5af19 95 prevProcessVariable_ = 0;
aberk 0:6e12a3e5af19 96 }
aberk 0:6e12a3e5af19 97
aberk 0:6e12a3e5af19 98 inMin_ = inMin;
aberk 0:6e12a3e5af19 99 inMax_ = inMax;
aberk 0:6e12a3e5af19 100 inSpan_ = inMax - inMin;
aberk 0:6e12a3e5af19 101
aberk 0:6e12a3e5af19 102 }
aberk 0:6e12a3e5af19 103
aberk 0:6e12a3e5af19 104 void PID::setOutputLimits(float outMin, float outMax) {
aberk 0:6e12a3e5af19 105
aberk 0:6e12a3e5af19 106 //Make sure we haven't been given impossible values.
aberk 0:6e12a3e5af19 107 if (outMin >= outMax) {
aberk 0:6e12a3e5af19 108 return;
aberk 0:6e12a3e5af19 109 }
aberk 0:6e12a3e5af19 110
aberk 0:6e12a3e5af19 111 //Rescale the working variables to reflect the changes.
aberk 0:6e12a3e5af19 112 prevControllerOutput_ *= (outMax - outMin) / outSpan_;
aberk 0:6e12a3e5af19 113
aberk 0:6e12a3e5af19 114 //Make sure the working variables are within the new limits.
aberk 0:6e12a3e5af19 115 if (prevControllerOutput_ > 1) {
aberk 0:6e12a3e5af19 116 prevControllerOutput_ = 1;
aberk 0:6e12a3e5af19 117 } else if (prevControllerOutput_ < 0) {
aberk 0:6e12a3e5af19 118 prevControllerOutput_ = 0;
aberk 0:6e12a3e5af19 119 }
aberk 0:6e12a3e5af19 120
aberk 0:6e12a3e5af19 121 outMin_ = outMin;
aberk 0:6e12a3e5af19 122 outMax_ = outMax;
aberk 0:6e12a3e5af19 123 outSpan_ = outMax - outMin;
aberk 0:6e12a3e5af19 124
aberk 0:6e12a3e5af19 125 }
aberk 0:6e12a3e5af19 126
aberk 0:6e12a3e5af19 127 void PID::setTunings(float Kc, float tauI, float tauD) {
aberk 0:6e12a3e5af19 128
aberk 0:6e12a3e5af19 129 //Verify that the tunings make sense.
aberk 0:6e12a3e5af19 130 if (Kc == 0.0 || tauI < 0.0 || tauD < 0.0) {
aberk 0:6e12a3e5af19 131 return;
aberk 0:6e12a3e5af19 132 }
aberk 0:6e12a3e5af19 133
aberk 0:6e12a3e5af19 134 //Store raw values to hand back to user on request.
aberk 0:6e12a3e5af19 135 pParam_ = Kc;
aberk 0:6e12a3e5af19 136 iParam_ = tauI;
aberk 0:6e12a3e5af19 137 dParam_ = tauD;
aberk 0:6e12a3e5af19 138
aberk 0:6e12a3e5af19 139 float tempTauR;
aberk 0:6e12a3e5af19 140
aberk 0:6e12a3e5af19 141 if (tauI == 0.0) {
aberk 0:6e12a3e5af19 142 tempTauR = 0.0;
aberk 0:6e12a3e5af19 143 } else {
aberk 0:6e12a3e5af19 144 tempTauR = (1.0 / tauI) * tSample_;
aberk 0:6e12a3e5af19 145 }
aberk 0:6e12a3e5af19 146
aberk 0:6e12a3e5af19 147 //For "bumpless transfer" we need to rescale the accumulated error.
aberk 0:6e12a3e5af19 148 if (inAuto) {
aberk 0:6e12a3e5af19 149 if (tempTauR == 0.0) {
aberk 0:6e12a3e5af19 150 accError_ = 0.0;
aberk 0:6e12a3e5af19 151 } else {
aberk 0:6e12a3e5af19 152 accError_ *= (Kc_ * tauR_) / (Kc * tempTauR);
aberk 0:6e12a3e5af19 153 }
aberk 0:6e12a3e5af19 154 }
aberk 0:6e12a3e5af19 155
aberk 0:6e12a3e5af19 156 Kc_ = Kc;
aberk 0:6e12a3e5af19 157 tauR_ = tempTauR;
aberk 0:6e12a3e5af19 158 tauD_ = tauD / tSample_;
aberk 0:6e12a3e5af19 159
aberk 0:6e12a3e5af19 160 }
aberk 0:6e12a3e5af19 161
aberk 0:6e12a3e5af19 162 void PID::reset(void) {
aberk 0:6e12a3e5af19 163
aberk 0:6e12a3e5af19 164 float scaledBias = 0.0;
aberk 0:6e12a3e5af19 165
aberk 0:6e12a3e5af19 166 if (usingFeedForward) {
aberk 0:6e12a3e5af19 167 scaledBias = (bias_ - outMin_) / outSpan_;
aberk 0:6e12a3e5af19 168 } else {
aberk 0:6e12a3e5af19 169 scaledBias = (realOutput_ - outMin_) / outSpan_;
aberk 0:6e12a3e5af19 170 }
aberk 0:6e12a3e5af19 171
aberk 0:6e12a3e5af19 172 prevControllerOutput_ = scaledBias;
aberk 0:6e12a3e5af19 173 prevProcessVariable_ = (processVariable_ - inMin_) / inSpan_;
aberk 0:6e12a3e5af19 174
aberk 0:6e12a3e5af19 175 //Clear any error in the integral.
aberk 0:6e12a3e5af19 176 accError_ = 0;
aberk 0:6e12a3e5af19 177
aberk 0:6e12a3e5af19 178 }
aberk 0:6e12a3e5af19 179
aberk 0:6e12a3e5af19 180 void PID::setMode(int mode) {
aberk 0:6e12a3e5af19 181
aberk 0:6e12a3e5af19 182 //We were in manual, and we just got set to auto.
aberk 0:6e12a3e5af19 183 //Reset the controller internals.
aberk 0:6e12a3e5af19 184 if (mode != 0 && !inAuto) {
aberk 0:6e12a3e5af19 185 reset();
aberk 0:6e12a3e5af19 186 }
aberk 0:6e12a3e5af19 187
aberk 0:6e12a3e5af19 188 inAuto = (mode != 0);
aberk 0:6e12a3e5af19 189
aberk 0:6e12a3e5af19 190 }
aberk 0:6e12a3e5af19 191
aberk 0:6e12a3e5af19 192 void PID::setInterval(float interval) {
aberk 0:6e12a3e5af19 193
aberk 0:6e12a3e5af19 194 if (interval > 0) {
aberk 0:6e12a3e5af19 195 //Convert the time-based tunings to reflect this change.
aberk 0:6e12a3e5af19 196 tauR_ *= (interval / tSample_);
aberk 0:6e12a3e5af19 197 accError_ *= (tSample_ / interval);
aberk 0:6e12a3e5af19 198 tauD_ *= (interval / tSample_);
aberk 0:6e12a3e5af19 199 tSample_ = interval;
aberk 0:6e12a3e5af19 200 }
aberk 0:6e12a3e5af19 201
aberk 0:6e12a3e5af19 202 }
aberk 0:6e12a3e5af19 203
aberk 0:6e12a3e5af19 204 void PID::setSetPoint(float sp) {
aberk 0:6e12a3e5af19 205
aberk 0:6e12a3e5af19 206 setPoint_ = sp;
aberk 0:6e12a3e5af19 207
aberk 0:6e12a3e5af19 208 }
aberk 0:6e12a3e5af19 209
aberk 0:6e12a3e5af19 210 void PID::setProcessValue(float pv) {
aberk 0:6e12a3e5af19 211
aberk 0:6e12a3e5af19 212 processVariable_ = pv;
aberk 0:6e12a3e5af19 213
aberk 0:6e12a3e5af19 214 }
aberk 0:6e12a3e5af19 215
aberk 0:6e12a3e5af19 216 void PID::setBias(float bias){
aberk 0:6e12a3e5af19 217
aberk 0:6e12a3e5af19 218 bias_ = bias;
aberk 0:6e12a3e5af19 219 usingFeedForward = 1;
aberk 0:6e12a3e5af19 220
aberk 0:6e12a3e5af19 221 }
aberk 0:6e12a3e5af19 222
aberk 0:6e12a3e5af19 223 float PID::compute() {
aberk 0:6e12a3e5af19 224
aberk 0:6e12a3e5af19 225 //Pull in the input and setpoint, and scale them into percent span.
aberk 0:6e12a3e5af19 226 float scaledPV = (processVariable_ - inMin_) / inSpan_;
aberk 0:6e12a3e5af19 227
aberk 0:6e12a3e5af19 228 if (scaledPV > 1.0) {
aberk 0:6e12a3e5af19 229 scaledPV = 1.0;
aberk 0:6e12a3e5af19 230 } else if (scaledPV < 0.0) {
aberk 0:6e12a3e5af19 231 scaledPV = 0.0;
aberk 0:6e12a3e5af19 232 }
aberk 0:6e12a3e5af19 233
aberk 0:6e12a3e5af19 234 float scaledSP = (setPoint_ - inMin_) / inSpan_;
aberk 0:6e12a3e5af19 235 if (scaledSP > 1.0) {
aberk 0:6e12a3e5af19 236 scaledSP = 1;
aberk 0:6e12a3e5af19 237 } else if (scaledSP < 0.0) {
aberk 0:6e12a3e5af19 238 scaledSP = 0;
aberk 0:6e12a3e5af19 239 }
aberk 0:6e12a3e5af19 240
aberk 0:6e12a3e5af19 241 float error = scaledSP - scaledPV;
aberk 0:6e12a3e5af19 242
aberk 0:6e12a3e5af19 243 //Check and see if the output is pegged at a limit and only
aberk 0:6e12a3e5af19 244 //integrate if it is not. This is to prevent reset-windup.
aberk 0:6e12a3e5af19 245 if (!(prevControllerOutput_ >= 1 && error > 0) && !(prevControllerOutput_ <= 0 && error < 0)) {
aberk 0:6e12a3e5af19 246 accError_ += error;
aberk 0:6e12a3e5af19 247 }
aberk 0:6e12a3e5af19 248
aberk 0:6e12a3e5af19 249 //Compute the current slope of the input signal.
aberk 0:6e12a3e5af19 250 float dMeas = (scaledPV - prevProcessVariable_) / tSample_;
aberk 0:6e12a3e5af19 251
aberk 0:6e12a3e5af19 252 float scaledBias = 0.0;
aberk 0:6e12a3e5af19 253
aberk 0:6e12a3e5af19 254 if (usingFeedForward) {
aberk 0:6e12a3e5af19 255 scaledBias = (bias_ - outMin_) / outSpan_;
aberk 0:6e12a3e5af19 256 }
aberk 0:6e12a3e5af19 257
aberk 0:6e12a3e5af19 258 //Perform the PID calculation.
aberk 0:6e12a3e5af19 259 controllerOutput_ = scaledBias + Kc_ * (error + (tauR_ * accError_) - (tauD_ * dMeas));
aberk 0:6e12a3e5af19 260
aberk 0:6e12a3e5af19 261 //Make sure the computed output is within output constraints.
aberk 0:6e12a3e5af19 262 if (controllerOutput_ < 0.0) {
aberk 0:6e12a3e5af19 263 controllerOutput_ = 0.0;
aberk 0:6e12a3e5af19 264 } else if (controllerOutput_ > 1.0) {
aberk 0:6e12a3e5af19 265 controllerOutput_ = 1.0;
aberk 0:6e12a3e5af19 266 }
aberk 0:6e12a3e5af19 267
aberk 0:6e12a3e5af19 268 //Remember this output for the windup check next time.
aberk 0:6e12a3e5af19 269 prevControllerOutput_ = controllerOutput_;
aberk 0:6e12a3e5af19 270 //Remember the input for the derivative calculation next time.
aberk 0:6e12a3e5af19 271 prevProcessVariable_ = scaledPV;
aberk 0:6e12a3e5af19 272
aberk 0:6e12a3e5af19 273 //Scale the output from percent span back out to a real world number.
aberk 0:6e12a3e5af19 274 return ((controllerOutput_ * outSpan_) + outMin_);
aberk 0:6e12a3e5af19 275
aberk 0:6e12a3e5af19 276 }
aberk 0:6e12a3e5af19 277
aberk 0:6e12a3e5af19 278 float PID::getInMin() {
aberk 0:6e12a3e5af19 279
aberk 0:6e12a3e5af19 280 return inMin_;
aberk 0:6e12a3e5af19 281
aberk 0:6e12a3e5af19 282 }
aberk 0:6e12a3e5af19 283
aberk 0:6e12a3e5af19 284 float PID::getInMax() {
aberk 0:6e12a3e5af19 285
aberk 0:6e12a3e5af19 286 return inMax_;
aberk 0:6e12a3e5af19 287
aberk 0:6e12a3e5af19 288 }
aberk 0:6e12a3e5af19 289
aberk 0:6e12a3e5af19 290 float PID::getOutMin() {
aberk 0:6e12a3e5af19 291
aberk 0:6e12a3e5af19 292 return outMin_;
aberk 0:6e12a3e5af19 293
aberk 0:6e12a3e5af19 294 }
aberk 0:6e12a3e5af19 295
aberk 0:6e12a3e5af19 296 float PID::getOutMax() {
aberk 0:6e12a3e5af19 297
aberk 0:6e12a3e5af19 298 return outMax_;
aberk 0:6e12a3e5af19 299
aberk 0:6e12a3e5af19 300 }
aberk 0:6e12a3e5af19 301
aberk 0:6e12a3e5af19 302 float PID::getInterval() {
aberk 0:6e12a3e5af19 303
aberk 0:6e12a3e5af19 304 return tSample_;
aberk 0:6e12a3e5af19 305
aberk 0:6e12a3e5af19 306 }
aberk 0:6e12a3e5af19 307
aberk 0:6e12a3e5af19 308 float PID::getPParam() {
aberk 0:6e12a3e5af19 309
aberk 0:6e12a3e5af19 310 return pParam_;
aberk 0:6e12a3e5af19 311
aberk 0:6e12a3e5af19 312 }
aberk 0:6e12a3e5af19 313
aberk 0:6e12a3e5af19 314 float PID::getIParam() {
aberk 0:6e12a3e5af19 315
aberk 0:6e12a3e5af19 316 return iParam_;
aberk 0:6e12a3e5af19 317
aberk 0:6e12a3e5af19 318 }
aberk 0:6e12a3e5af19 319
aberk 0:6e12a3e5af19 320 float PID::getDParam() {
aberk 0:6e12a3e5af19 321
aberk 0:6e12a3e5af19 322 return dParam_;
aberk 0:6e12a3e5af19 323
aberk 0:6e12a3e5af19 324 }