Proportional, integral, derivative controller library. Ported from the Arduino PID library by Brett Beauregard.

Fork of PID by Aaron Berk

Committer:
nicovv44
Date:
Wed Oct 17 05:35:43 2018 +0000
Revision:
1:214eb4d0c5bd
Parent:
0:6e12a3e5af19
not working code

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 #ifndef PID_H
aberk 0:6e12a3e5af19 48 #define PID_H
aberk 0:6e12a3e5af19 49
aberk 0:6e12a3e5af19 50 /**
aberk 0:6e12a3e5af19 51 * Includes
aberk 0:6e12a3e5af19 52 */
aberk 0:6e12a3e5af19 53 #include "mbed.h"
aberk 0:6e12a3e5af19 54
aberk 0:6e12a3e5af19 55 /**
aberk 0:6e12a3e5af19 56 * Defines
aberk 0:6e12a3e5af19 57 */
aberk 0:6e12a3e5af19 58 #define MANUAL_MODE 0
aberk 0:6e12a3e5af19 59 #define AUTO_MODE 1
aberk 0:6e12a3e5af19 60
aberk 0:6e12a3e5af19 61 /**
aberk 0:6e12a3e5af19 62 * Proportional-integral-derivative controller.
aberk 0:6e12a3e5af19 63 */
aberk 0:6e12a3e5af19 64 class PID {
aberk 0:6e12a3e5af19 65
aberk 0:6e12a3e5af19 66 public:
aberk 0:6e12a3e5af19 67
aberk 0:6e12a3e5af19 68 /**
aberk 0:6e12a3e5af19 69 * Constructor.
aberk 0:6e12a3e5af19 70 *
aberk 0:6e12a3e5af19 71 * Sets default limits [0-3.3V], calculates tuning parameters, and sets
aberk 0:6e12a3e5af19 72 * manual mode with no bias.
aberk 0:6e12a3e5af19 73 *
aberk 0:6e12a3e5af19 74 * @param Kc - Tuning parameter
aberk 0:6e12a3e5af19 75 * @param tauI - Tuning parameter
aberk 0:6e12a3e5af19 76 * @param tauD - Tuning parameter
aberk 0:6e12a3e5af19 77 * @param interval PID calculation performed every interval seconds.
aberk 0:6e12a3e5af19 78 */
aberk 0:6e12a3e5af19 79 PID(float Kc, float tauI, float tauD, float interval);
aberk 0:6e12a3e5af19 80
aberk 0:6e12a3e5af19 81 /**
aberk 0:6e12a3e5af19 82 * Scale from inputs to 0-100%.
aberk 0:6e12a3e5af19 83 *
aberk 0:6e12a3e5af19 84 * @param InMin The real world value corresponding to 0%.
aberk 0:6e12a3e5af19 85 * @param InMax The real world value corresponding to 100%.
aberk 0:6e12a3e5af19 86 */
aberk 0:6e12a3e5af19 87 void setInputLimits(float inMin , float inMax);
aberk 0:6e12a3e5af19 88
aberk 0:6e12a3e5af19 89 /**
aberk 0:6e12a3e5af19 90 * Scale from outputs to 0-100%.
aberk 0:6e12a3e5af19 91 *
aberk 0:6e12a3e5af19 92 * @param outMin The real world value corresponding to 0%.
aberk 0:6e12a3e5af19 93 * @param outMax The real world value corresponding to 100%.
aberk 0:6e12a3e5af19 94 */
aberk 0:6e12a3e5af19 95 void setOutputLimits(float outMin, float outMax);
aberk 0:6e12a3e5af19 96
aberk 0:6e12a3e5af19 97 /**
aberk 0:6e12a3e5af19 98 * Calculate PID constants.
aberk 0:6e12a3e5af19 99 *
aberk 0:6e12a3e5af19 100 * Allows parameters to be changed on the fly without ruining calculations.
aberk 0:6e12a3e5af19 101 *
aberk 0:6e12a3e5af19 102 * @param Kc - Tuning parameter
aberk 0:6e12a3e5af19 103 * @param tauI - Tuning parameter
aberk 0:6e12a3e5af19 104 * @param tauD - Tuning parameter
aberk 0:6e12a3e5af19 105 */
aberk 0:6e12a3e5af19 106 void setTunings(float Kc, float tauI, float tauD);
aberk 0:6e12a3e5af19 107
aberk 0:6e12a3e5af19 108 /**
aberk 0:6e12a3e5af19 109 * Reinitializes controller internals. Automatically
aberk 0:6e12a3e5af19 110 * called on a manual to auto transition.
aberk 0:6e12a3e5af19 111 */
aberk 0:6e12a3e5af19 112 void reset(void);
aberk 0:6e12a3e5af19 113
aberk 0:6e12a3e5af19 114 /**
aberk 0:6e12a3e5af19 115 * Set PID to manual or auto mode.
aberk 0:6e12a3e5af19 116 *
aberk 0:6e12a3e5af19 117 * @param mode 0 -> Manual
aberk 0:6e12a3e5af19 118 * Non-zero -> Auto
aberk 0:6e12a3e5af19 119 */
aberk 0:6e12a3e5af19 120 void setMode(int mode);
aberk 0:6e12a3e5af19 121
aberk 0:6e12a3e5af19 122 /**
aberk 0:6e12a3e5af19 123 * Set how fast the PID loop is run.
aberk 0:6e12a3e5af19 124 *
aberk 0:6e12a3e5af19 125 * @param interval PID calculation peformed every interval seconds.
aberk 0:6e12a3e5af19 126 */
aberk 0:6e12a3e5af19 127 void setInterval(float interval);
aberk 0:6e12a3e5af19 128
aberk 0:6e12a3e5af19 129 /**
aberk 0:6e12a3e5af19 130 * Set the set point.
aberk 0:6e12a3e5af19 131 *
aberk 0:6e12a3e5af19 132 * @param sp The set point as a real world value.
aberk 0:6e12a3e5af19 133 */
aberk 0:6e12a3e5af19 134 void setSetPoint(float sp);
aberk 0:6e12a3e5af19 135
aberk 0:6e12a3e5af19 136 /**
aberk 0:6e12a3e5af19 137 * Set the process value.
aberk 0:6e12a3e5af19 138 *
aberk 0:6e12a3e5af19 139 * @param pv The process value as a real world value.
aberk 0:6e12a3e5af19 140 */
aberk 0:6e12a3e5af19 141 void setProcessValue(float pv);
aberk 0:6e12a3e5af19 142
aberk 0:6e12a3e5af19 143 /**
aberk 0:6e12a3e5af19 144 * Set the bias.
aberk 0:6e12a3e5af19 145 *
aberk 0:6e12a3e5af19 146 * @param bias The bias for the controller output.
aberk 0:6e12a3e5af19 147 */
aberk 0:6e12a3e5af19 148 void setBias(float bias);
aberk 0:6e12a3e5af19 149
aberk 0:6e12a3e5af19 150 /**
aberk 0:6e12a3e5af19 151 * PID calculation.
aberk 0:6e12a3e5af19 152 *
aberk 0:6e12a3e5af19 153 * @return The controller output as a float between outMin and outMax.
aberk 0:6e12a3e5af19 154 */
aberk 0:6e12a3e5af19 155 float compute(void);
aberk 0:6e12a3e5af19 156
aberk 0:6e12a3e5af19 157 //Getters.
aberk 0:6e12a3e5af19 158 float getInMin();
aberk 0:6e12a3e5af19 159 float getInMax();
aberk 0:6e12a3e5af19 160 float getOutMin();
aberk 0:6e12a3e5af19 161 float getOutMax();
aberk 0:6e12a3e5af19 162 float getInterval();
aberk 0:6e12a3e5af19 163 float getPParam();
aberk 0:6e12a3e5af19 164 float getIParam();
aberk 0:6e12a3e5af19 165 float getDParam();
aberk 0:6e12a3e5af19 166
aberk 0:6e12a3e5af19 167 private:
aberk 0:6e12a3e5af19 168
aberk 0:6e12a3e5af19 169 bool usingFeedForward;
aberk 0:6e12a3e5af19 170 bool inAuto;
aberk 0:6e12a3e5af19 171
aberk 0:6e12a3e5af19 172 //Actual tuning parameters used in PID calculation.
aberk 0:6e12a3e5af19 173 float Kc_;
aberk 0:6e12a3e5af19 174 float tauR_;
aberk 0:6e12a3e5af19 175 float tauD_;
aberk 0:6e12a3e5af19 176
aberk 0:6e12a3e5af19 177 //Raw tuning parameters.
aberk 0:6e12a3e5af19 178 float pParam_;
aberk 0:6e12a3e5af19 179 float iParam_;
aberk 0:6e12a3e5af19 180 float dParam_;
aberk 0:6e12a3e5af19 181
aberk 0:6e12a3e5af19 182 //The point we want to reach.
aberk 0:6e12a3e5af19 183 float setPoint_;
aberk 0:6e12a3e5af19 184 //The thing we measure.
aberk 0:6e12a3e5af19 185 float processVariable_;
aberk 0:6e12a3e5af19 186 float prevProcessVariable_;
aberk 0:6e12a3e5af19 187 //The output that affects the process variable.
aberk 0:6e12a3e5af19 188 float controllerOutput_;
aberk 0:6e12a3e5af19 189 float prevControllerOutput_;
aberk 0:6e12a3e5af19 190
aberk 0:6e12a3e5af19 191 //We work in % for calculations so these will scale from
aberk 0:6e12a3e5af19 192 //real world values to 0-100% and back again.
aberk 0:6e12a3e5af19 193 float inMin_;
aberk 0:6e12a3e5af19 194 float inMax_;
aberk 0:6e12a3e5af19 195 float inSpan_;
aberk 0:6e12a3e5af19 196 float outMin_;
aberk 0:6e12a3e5af19 197 float outMax_;
aberk 0:6e12a3e5af19 198 float outSpan_;
aberk 0:6e12a3e5af19 199
aberk 0:6e12a3e5af19 200 //The accumulated error, i.e. integral.
aberk 0:6e12a3e5af19 201 float accError_;
aberk 0:6e12a3e5af19 202 //The controller output bias.
aberk 0:6e12a3e5af19 203 float bias_;
aberk 0:6e12a3e5af19 204
aberk 0:6e12a3e5af19 205 //The interval between samples.
aberk 0:6e12a3e5af19 206 float tSample_;
aberk 0:6e12a3e5af19 207
aberk 0:6e12a3e5af19 208 //Controller output as a real world value.
aberk 0:6e12a3e5af19 209 volatile float realOutput_;
aberk 0:6e12a3e5af19 210
aberk 0:6e12a3e5af19 211 };
aberk 0:6e12a3e5af19 212
aberk 0:6e12a3e5af19 213 #endif /* PID_H */