Eurobot2012_Primary

Dependencies:   mbed Eurobot_2012_Primary

Committer:
narshu
Date:
Wed Oct 17 22:22:47 2012 +0000
Revision:
26:0995f61cb7b8
Parent:
25:143b19c1fb05
Eurobot 2012 Primary;

Who changed what in which revision?

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