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

Fork of PidController by James Batchelar

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PidController.h Source File

PidController.h

00001 /**
00002  * @author James Batchelar
00003  *
00004  * @section LICENSE
00005  *
00006  *
00007  * Permission is hereby granted, free of charge, to any person obtaining a copy
00008  * of this software and associated documentation files (the "Software"), to deal
00009  * in the Software without restriction, including without limitation the rights
00010  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00011  * copies of the Software, and to permit persons to whom the Software is
00012  * furnished to do so, subject to the following conditions:
00013  *
00014  * The above copyright notice and this permission notice shall be included in
00015  * all copies or substantial portions of the Software.
00016  *
00017  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00018  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00019  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00020  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00021  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00022  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00023  * THE SOFTWARE.
00024  *
00025  * @section DESCRIPTION
00026  *
00027  * PID Controller with Integral Clamping
00028  * 
00029  * 
00030  *     
00031  
00032  * @code
00033 #include "mbed.h"
00034 #include "PID.h"
00035 
00036  * @endcode
00037  */
00038 
00039 
00040 
00041 #ifndef PidController_H
00042 #define PidController_H
00043 #include "mbed.h"
00044 
00045 //-- Constants used in system
00046 const int RATE = 20;       //--(ms) Time that Calculate mehtod is being called       
00047 const int AUTOMATIC = 0;    //-- In automatic then PID Controls Output
00048 const int MANUAL = 1;       //-- In Manual then User Controls Output directly
00049 
00050 //-- Constructor 
00051 
00052 class PidController{
00053     public:
00054         // Public Methods
00055     
00056         /** Constructor
00057         *
00058         */
00059         PidController(char);
00060         
00061         /** Performs the PID Calculation
00062         * @param SP - Setpoint (target value) units depends on what PID is controlling
00063         * @param PV - Process Variable (feedback/ measure value) units depends on what PID is controlling
00064         * @return Returns If Controller is in Automatic then returns PID controlled signal. In manual returns the user SP multipled by scalar.
00065         */
00066         float Calculate(float SP, float PV, float ManualMV);
00067         
00068         /** Update Internal Settings
00069         * @param Bias - Added to the PID Calculation
00070         * @param PropGain - Proportional Gain
00071         * @param IntGain - Integral Gain
00072         * @param DiffGain - Differential Gain
00073         * @param OutputMin - Minimum Limit for the Output (units are same as setpoint)
00074         * @param OutputMax - Maximum Limit for the Output (units are same as setpoint)
00075         * @param OutputScale - Multiplier at End of PID loop to convert from engineering units to signal (eg PWM duty cycle)   
00076         */    
00077         void UpdateSettings(float Bias, float PropGain, float IntGain, float DiffGain, float OutputMin, float OutputMax);
00078         
00079         /** Update Internal Settings
00080         * @param OutputMin - Minimum Limit for the Output (units are same as setpoint)
00081         * @param OutputMax - Maximum Limit for the Output (units are same as setpoint)
00082         */    
00083         void UpdateSettings(float OutputMin, float OutputMax);
00084         
00085         /** Get the controller mode
00086         * @return 1 = AUTOMATIC, 0 = MANUAL(User has direct contol on output).
00087         */
00088         int GetMode(void){return (int)mode;}
00089         
00090         /** Set the controller in Manual Mode (PID turned off SP is writted directly to output)
00091         */
00092         void SetManual(void){mode = MANUAL;}
00093         
00094         /** Set the controller in Automatic Mode (PID turned on)
00095         */
00096         void SetAutomatic(void){mode = AUTOMATIC;}
00097         
00098         /** Enable Diagnostics for HMI
00099         */
00100         void StartDiag(void);
00101         
00102         /** Disable Diagnostics for HMI
00103         */
00104         void EndDiag(void);
00105         
00106         /** Build a string to send back to HMI to Graph PID
00107         */
00108         void BuildDiagMessage(float SP, float PV, float PWM, float PropAction, float IntAction, float DifAction);
00109         
00110         /** Check to see if the controller is collecting diagnostics data
00111         * @return true then a diagnostics message is been created.
00112         */
00113         bool DiagnosticsEnabled(void){return collectDiagnostics;}
00114         
00115         int GetElapsedtime(void){return elapsedTime;}
00116         
00117         // Made Public as im lazy
00118         char diagMsg[65];
00119         
00120     private:
00121         bool mode;
00122         
00123         //-- For Diagnostics to GUI
00124         bool collectDiagnostics;
00125         int elapsedTime;
00126         
00127         //-- For PID Calculations
00128         char diagChar;
00129         float bias;
00130         float error;
00131         float lastInput;
00132         float accumError;
00133         float minLimit, maxLimit;
00134         float K_p,K_i,K_d;
00135         float prevError;
00136     };
00137     
00138 #endif
00139     
00140     
00141     
00142 
00143 
00144