This is a copy of the Reference Standard PID controller ala controlguru.com

Dependents:   PIDHeater Printer PIDHeater82 UltiSaverController

Fork of PID by Arnaud Suire

Committer:
unix_guru
Date:
Sun Feb 07 19:06:37 2016 +0000
Revision:
3:316f974b7f98
Parent:
2:55bf0f813bb4
Moved from Double to Floating point PID library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
unix_guru 2:55bf0f813bb4 1 /**
unix_guru 3:316f974b7f98 2 * @author Brett Beauregard
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 3:316f974b7f98 33 * https://github.com/br3ttb/Arduino-PID-Library
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
unix_guru 2:55bf0f813bb4 47
unix_guru 2:55bf0f813bb4 48 /**
unix_guru 2:55bf0f813bb4 49 * Includes
unix_guru 2:55bf0f813bb4 50 */
unix_guru 2:55bf0f813bb4 51 #include "mbed.h"
unix_guru 2:55bf0f813bb4 52
unix_guru 2:55bf0f813bb4 53 /**
unix_guru 2:55bf0f813bb4 54 * Defines
unix_guru 2:55bf0f813bb4 55 */
unix_guru 2:55bf0f813bb4 56
unix_guru 3:316f974b7f98 57 #ifndef PID_H
unix_guru 3:316f974b7f98 58 #define PID_H
unix_guru 3:316f974b7f98 59
unix_guru 3:316f974b7f98 60 #define LIBRARY_VERSION 1.1.1
unix_guru 3:316f974b7f98 61
unix_guru 3:316f974b7f98 62 class PID
unix_guru 3:316f974b7f98 63 {
arnaudsuire 0:d58c1b8d63d9 64
arnaudsuire 0:d58c1b8d63d9 65
unix_guru 3:316f974b7f98 66 public:
unix_guru 3:316f974b7f98 67
unix_guru 3:316f974b7f98 68 //Constants used in some of the functions below
unix_guru 3:316f974b7f98 69 #define AUTOMATIC 1
unix_guru 3:316f974b7f98 70 #define MANUAL 0
unix_guru 3:316f974b7f98 71 #define DIRECT 0
unix_guru 3:316f974b7f98 72 #define REVERSE 1
unix_guru 2:55bf0f813bb4 73
unix_guru 3:316f974b7f98 74 //commonly used functions **************************************************************************
unix_guru 3:316f974b7f98 75 PID(float*, float*, float*, // * constructor. links the PID to the Input, Output, and
unix_guru 3:316f974b7f98 76 float, float, float, int); // Setpoint. Initial tuning parameters are also set here
unix_guru 3:316f974b7f98 77
unix_guru 3:316f974b7f98 78 void SetMode(int Mode); // * sets PID to either Manual (0) or Auto (non-0)
unix_guru 2:55bf0f813bb4 79
unix_guru 3:316f974b7f98 80 bool Compute(); // * performs the PID calculation. it should be
unix_guru 3:316f974b7f98 81 // called every time loop() cycles. ON/OFF and
unix_guru 3:316f974b7f98 82 // calculation frequency can be set using SetMode
unix_guru 3:316f974b7f98 83 // SetSampleTime respectively
unix_guru 3:316f974b7f98 84
unix_guru 3:316f974b7f98 85 void SetOutputLimits(float, float); //clamps the output to a specific range. 0-1.0 by default, but
unix_guru 3:316f974b7f98 86 //it's likely the user will want to change this depending on
unix_guru 3:316f974b7f98 87 //the application
unix_guru 2:55bf0f813bb4 88
arnaudsuire 0:d58c1b8d63d9 89
unix_guru 2:55bf0f813bb4 90
unix_guru 3:316f974b7f98 91 //available but not commonly used functions ********************************************************
unix_guru 3:316f974b7f98 92 void SetTunings(float, float, // * While most users will set the tunings once in the
unix_guru 3:316f974b7f98 93 float); // constructor, this function gives the user the option
unix_guru 3:316f974b7f98 94 // of changing tunings during runtime for Adaptive control
unix_guru 3:316f974b7f98 95 void SetControllerDirection(int); // * Sets the Direction, or "Action" of the controller. DIRECT
unix_guru 3:316f974b7f98 96 // means the output will increase when error is positive. REVERSE
unix_guru 3:316f974b7f98 97 // means the opposite. it's very unlikely that this will be needed
unix_guru 3:316f974b7f98 98 // once it is set in the constructor.
unix_guru 3:316f974b7f98 99 void SetSampleTime(int); // * sets the frequency, in Milliseconds, with which
unix_guru 3:316f974b7f98 100 // the PID calculation is performed. default is 100
unix_guru 3:316f974b7f98 101
unix_guru 3:316f974b7f98 102
unix_guru 3:316f974b7f98 103
unix_guru 3:316f974b7f98 104 //Display functions ****************************************************************
unix_guru 3:316f974b7f98 105 float GetKp(); // These functions query the pid for interal values.
unix_guru 3:316f974b7f98 106 float GetKi(); // they were created mainly for the pid front-end,
unix_guru 3:316f974b7f98 107 float GetKd(); // where it's important to know what is actually
unix_guru 3:316f974b7f98 108 int GetMode(); // inside the PID.
unix_guru 3:316f974b7f98 109 int GetDirection(); //
unix_guru 2:55bf0f813bb4 110
unix_guru 3:316f974b7f98 111 private:
unix_guru 3:316f974b7f98 112 void Initialize();
unix_guru 3:316f974b7f98 113
unix_guru 3:316f974b7f98 114 float dispKp; // * we'll hold on to the tuning parameters in user-entered
unix_guru 3:316f974b7f98 115 float dispKi; // format for display purposes
unix_guru 3:316f974b7f98 116 float dispKd; //
unix_guru 3:316f974b7f98 117
unix_guru 3:316f974b7f98 118 float kp; // * (P)roportional Tuning Parameter
unix_guru 3:316f974b7f98 119 float ki; // * (I)ntegral Tuning Parameter
unix_guru 3:316f974b7f98 120 float kd; // * (D)erivative Tuning Parameter
unix_guru 2:55bf0f813bb4 121
unix_guru 3:316f974b7f98 122 int controllerDirection;
unix_guru 3:316f974b7f98 123
unix_guru 3:316f974b7f98 124 float *myInput; // * Pointers to the Input, Output, and Setpoint variables
unix_guru 3:316f974b7f98 125 float *myOutput; // This creates a hard link between the variables and the
unix_guru 3:316f974b7f98 126 float *mySetpoint; // PID, freeing the user from having to constantly tell us
unix_guru 3:316f974b7f98 127 // what these values are. with pointers we'll just know.
unix_guru 3:316f974b7f98 128
unix_guru 3:316f974b7f98 129 unsigned long lastTime;
unix_guru 3:316f974b7f98 130 float ITerm, lastInput;
unix_guru 3:316f974b7f98 131
unix_guru 3:316f974b7f98 132 unsigned long SampleTime;
unix_guru 3:316f974b7f98 133 float outMin, outMax;
unix_guru 3:316f974b7f98 134 bool inAuto;
unix_guru 3:316f974b7f98 135 };
unix_guru 3:316f974b7f98 136 #endif
unix_guru 3:316f974b7f98 137