Motion PID

Fork of PID by Aaron Berk

Committer:
b0ssiz
Date:
Sat Jan 16 12:05:54 2016 +0000
Revision:
1:807a17d0a172
Parent:
0:6e12a3e5af19
Commit for Publish BEAR_Reciever;

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