Simple PID Controller with Integral Windup Supports creating a diagnostics message to send to a GUI

Committer:
batchee7
Date:
Wed Sep 06 23:45:40 2017 +0000
Revision:
3:c169d08a9d0b
Parent:
2:dd64c2cf9066
Corrections to output generation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
batchee7 2:dd64c2cf9066 1 /**
batchee7 2:dd64c2cf9066 2 * @author James Batchelar
batchee7 2:dd64c2cf9066 3 *
batchee7 2:dd64c2cf9066 4 * @section LICENSE
batchee7 2:dd64c2cf9066 5 *
batchee7 2:dd64c2cf9066 6 *
batchee7 2:dd64c2cf9066 7 * Permission is hereby granted, free of charge, to any person obtaining a copy
batchee7 2:dd64c2cf9066 8 * of this software and associated documentation files (the "Software"), to deal
batchee7 2:dd64c2cf9066 9 * in the Software without restriction, including without limitation the rights
batchee7 2:dd64c2cf9066 10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
batchee7 2:dd64c2cf9066 11 * copies of the Software, and to permit persons to whom the Software is
batchee7 2:dd64c2cf9066 12 * furnished to do so, subject to the following conditions:
batchee7 2:dd64c2cf9066 13 *
batchee7 2:dd64c2cf9066 14 * The above copyright notice and this permission notice shall be included in
batchee7 2:dd64c2cf9066 15 * all copies or substantial portions of the Software.
batchee7 2:dd64c2cf9066 16 *
batchee7 2:dd64c2cf9066 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
batchee7 2:dd64c2cf9066 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
batchee7 2:dd64c2cf9066 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
batchee7 2:dd64c2cf9066 20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
batchee7 2:dd64c2cf9066 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
batchee7 2:dd64c2cf9066 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
batchee7 2:dd64c2cf9066 23 * THE SOFTWARE.
batchee7 2:dd64c2cf9066 24 *
batchee7 2:dd64c2cf9066 25 * @section DESCRIPTION
batchee7 2:dd64c2cf9066 26 *
batchee7 2:dd64c2cf9066 27 * PID Controller with Integral Clamping
batchee7 2:dd64c2cf9066 28 *
batchee7 2:dd64c2cf9066 29 *
batchee7 2:dd64c2cf9066 30 *
batchee7 2:dd64c2cf9066 31
batchee7 2:dd64c2cf9066 32 * @code
batchee7 2:dd64c2cf9066 33 #include "mbed.h"
batchee7 2:dd64c2cf9066 34 #include "PID.h"
batchee7 2:dd64c2cf9066 35
batchee7 2:dd64c2cf9066 36 * @endcode
batchee7 2:dd64c2cf9066 37 */
batchee7 2:dd64c2cf9066 38
batchee7 2:dd64c2cf9066 39 #include "mbed.h"
batchee7 2:dd64c2cf9066 40
batchee7 2:dd64c2cf9066 41 #ifndef PidController_H
batchee7 2:dd64c2cf9066 42 #define PidController_H
batchee7 2:dd64c2cf9066 43
batchee7 2:dd64c2cf9066 44 //-- Constants used in system
batchee7 2:dd64c2cf9066 45 const int RATE = 100; //--(ms) Time that Calculate mehtod is being called
batchee7 2:dd64c2cf9066 46 const int AUTOMATIC = 0; //-- In automatic then PID Controls Output
batchee7 2:dd64c2cf9066 47 const int MANUAL = 1; //-- In Manual then User Controls Output directly
batchee7 2:dd64c2cf9066 48
batchee7 2:dd64c2cf9066 49 //-- Constructor
batchee7 2:dd64c2cf9066 50
batchee7 2:dd64c2cf9066 51 class PidController{
batchee7 2:dd64c2cf9066 52 public:
batchee7 2:dd64c2cf9066 53 // Public Methods
batchee7 2:dd64c2cf9066 54
batchee7 2:dd64c2cf9066 55 /** Constructor
batchee7 2:dd64c2cf9066 56 *
batchee7 2:dd64c2cf9066 57 */
batchee7 2:dd64c2cf9066 58 PidController();
batchee7 2:dd64c2cf9066 59
batchee7 2:dd64c2cf9066 60 /** Performs the PID Calculation
batchee7 2:dd64c2cf9066 61 * @param SP - Setpoint (target value) units depends on what PID is controlling
batchee7 2:dd64c2cf9066 62 * @param PV - Process Variable (feedback/ measure value) units depends on what PID is controlling
batchee7 2:dd64c2cf9066 63 * @return Returns If Controller is in Automatic then returns PID controlled signal. In manual returns the user SP multipled by scalar.
batchee7 2:dd64c2cf9066 64 */
batchee7 2:dd64c2cf9066 65 int Calculate(float SP, float PV);
batchee7 2:dd64c2cf9066 66
batchee7 2:dd64c2cf9066 67 /** Update Internal Seetings
batchee7 2:dd64c2cf9066 68 * @param Bias - Added to the PID Calculation
batchee7 2:dd64c2cf9066 69 * @param PropGain - Proportional Gain
batchee7 2:dd64c2cf9066 70 * @param IntGain - Integral Gain
batchee7 2:dd64c2cf9066 71 * @param DiffGain - Differential Gain
batchee7 2:dd64c2cf9066 72 * @param OutputMin - Minimum Limit for the Output (units are same as setpoint)
batchee7 2:dd64c2cf9066 73 * @param OutputMax - Maximum Limit for the Output (units are same as setpoint)
batchee7 2:dd64c2cf9066 74 * @param OutputScale - Multiplier at End of PID loop to convert from engineering units to signal (eg PWM duty cycle)
batchee7 2:dd64c2cf9066 75 */
batchee7 2:dd64c2cf9066 76 void UpdateSettings(float Bias, float PropGain, float IntGain, float DiffGain, float OutputMin, float OutputMax, float OutputScale);
batchee7 2:dd64c2cf9066 77
batchee7 2:dd64c2cf9066 78 /** Get the controller mode
batchee7 2:dd64c2cf9066 79 * @return 1 = AUTOMATIC, 0 = MANUAL(User has direct contol on output).
batchee7 2:dd64c2cf9066 80 */
batchee7 2:dd64c2cf9066 81 int GetMode(void){return (int)mode;}
batchee7 2:dd64c2cf9066 82
batchee7 2:dd64c2cf9066 83 /** Set the controller in Manual Mode (PID turned off SP is writted directly to output)
batchee7 2:dd64c2cf9066 84 */
batchee7 2:dd64c2cf9066 85 void SetManual(void){mode = MANUAL;}
batchee7 2:dd64c2cf9066 86
batchee7 2:dd64c2cf9066 87 /** Set the controller in Automatic Mode (PID turned on)
batchee7 2:dd64c2cf9066 88 */
batchee7 2:dd64c2cf9066 89 void SetAutomatic(void){mode = AUTOMATIC;}
batchee7 2:dd64c2cf9066 90
batchee7 2:dd64c2cf9066 91 /** Enable Diagnostics for HMI
batchee7 2:dd64c2cf9066 92 */
batchee7 2:dd64c2cf9066 93 void StartDiag(void);
batchee7 2:dd64c2cf9066 94
batchee7 2:dd64c2cf9066 95 /** Disable Diagnostics for HMI
batchee7 2:dd64c2cf9066 96 */
batchee7 3:c169d08a9d0b 97 void EndDiag(void);
batchee7 2:dd64c2cf9066 98
batchee7 2:dd64c2cf9066 99 /** Build a string to send back to HMI to Graph PID
batchee7 2:dd64c2cf9066 100 */
batchee7 2:dd64c2cf9066 101 void BuildDiagMessage(float SP, float PV, float PWM, float PropAction, float IntAction, float DifAction);
batchee7 2:dd64c2cf9066 102
batchee7 2:dd64c2cf9066 103 /** Check to see if the controller is collecting diagnostics data
batchee7 2:dd64c2cf9066 104 * @return true then a diagnostics message is been created.
batchee7 2:dd64c2cf9066 105 */
batchee7 2:dd64c2cf9066 106 bool DiagnosticsEnabled(void){return collectDiagnostics;}
batchee7 2:dd64c2cf9066 107
batchee7 2:dd64c2cf9066 108 int GetElapsedtime(void){return elapsedTime;}
batchee7 2:dd64c2cf9066 109
batchee7 2:dd64c2cf9066 110 // Made Public as im lazy
batchee7 2:dd64c2cf9066 111 char diagMsg[65];
batchee7 2:dd64c2cf9066 112
batchee7 2:dd64c2cf9066 113 private:
batchee7 2:dd64c2cf9066 114 bool mode;
batchee7 2:dd64c2cf9066 115
batchee7 2:dd64c2cf9066 116 //-- For Diagnostics to GUI
batchee7 2:dd64c2cf9066 117 bool collectDiagnostics;
batchee7 2:dd64c2cf9066 118 int elapsedTime;
batchee7 2:dd64c2cf9066 119
batchee7 2:dd64c2cf9066 120 //-- For PID Calculations
batchee7 2:dd64c2cf9066 121 float bias, scalar;
batchee7 2:dd64c2cf9066 122 float error;
batchee7 2:dd64c2cf9066 123 float lastError;
batchee7 2:dd64c2cf9066 124 float accumError;
batchee7 2:dd64c2cf9066 125 float minLimit, maxLimit;
batchee7 2:dd64c2cf9066 126 float K_p,K_i,K_d;
batchee7 2:dd64c2cf9066 127 };
batchee7 2:dd64c2cf9066 128
batchee7 2:dd64c2cf9066 129 #endif
batchee7 2:dd64c2cf9066 130
batchee7 2:dd64c2cf9066 131
batchee7 2:dd64c2cf9066 132
batchee7 2:dd64c2cf9066 133
batchee7 2:dd64c2cf9066 134
batchee7 2:dd64c2cf9066 135