Simple PID Controller with Integral Windup Supports creating a diagnostics message to send to a GUI Prints to Binary
Fork of PidControllerV2 by
Diff: PidController.h
- Revision:
- 2:dd64c2cf9066
- Child:
- 3:c169d08a9d0b
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PidController.h Tue Sep 05 05:18:52 2017 +0000 @@ -0,0 +1,135 @@ +/** + * @author James Batchelar + * + * @section LICENSE + * + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + * @section DESCRIPTION + * + * PID Controller with Integral Clamping + * + * + * + + * @code +#include "mbed.h" +#include "PID.h" + + * @endcode + */ + +#include "mbed.h" + +#ifndef PidController_H +#define PidController_H + +//-- Constants used in system +const int RATE = 100; //--(ms) Time that Calculate mehtod is being called +const int AUTOMATIC = 0; //-- In automatic then PID Controls Output +const int MANUAL = 1; //-- In Manual then User Controls Output directly + +//-- Constructor + +class PidController{ + public: + // Public Methods + + /** Constructor + * + */ + PidController(); + + /** Performs the PID Calculation + * @param SP - Setpoint (target value) units depends on what PID is controlling + * @param PV - Process Variable (feedback/ measure value) units depends on what PID is controlling + * @return Returns If Controller is in Automatic then returns PID controlled signal. In manual returns the user SP multipled by scalar. + */ + int Calculate(float SP, float PV); + + /** Update Internal Seetings + * @param Bias - Added to the PID Calculation + * @param PropGain - Proportional Gain + * @param IntGain - Integral Gain + * @param DiffGain - Differential Gain + * @param OutputMin - Minimum Limit for the Output (units are same as setpoint) + * @param OutputMax - Maximum Limit for the Output (units are same as setpoint) + * @param OutputScale - Multiplier at End of PID loop to convert from engineering units to signal (eg PWM duty cycle) + */ + void UpdateSettings(float Bias, float PropGain, float IntGain, float DiffGain, float OutputMin, float OutputMax, float OutputScale); + + /** Get the controller mode + * @return 1 = AUTOMATIC, 0 = MANUAL(User has direct contol on output). + */ + int GetMode(void){return (int)mode;} + + /** Set the controller in Manual Mode (PID turned off SP is writted directly to output) + */ + void SetManual(void){mode = MANUAL;} + + /** Set the controller in Automatic Mode (PID turned on) + */ + void SetAutomatic(void){mode = AUTOMATIC;} + + /** Enable Diagnostics for HMI + */ + void StartDiag(void); + + /** Disable Diagnostics for HMI + */ + void EndDiag(void){collectDiagnostics = false;} + + /** Build a string to send back to HMI to Graph PID + */ + void BuildDiagMessage(float SP, float PV, float PWM, float PropAction, float IntAction, float DifAction); + + /** Check to see if the controller is collecting diagnostics data + * @return true then a diagnostics message is been created. + */ + bool DiagnosticsEnabled(void){return collectDiagnostics;} + + int GetElapsedtime(void){return elapsedTime;} + + // Made Public as im lazy + char diagMsg[65]; + + private: + bool mode; + + //-- For Diagnostics to GUI + bool collectDiagnostics; + int elapsedTime; + + //-- For PID Calculations + float bias, scalar; + float error; + float lastError; + float accumError; + float minLimit, maxLimit; + float K_p,K_i,K_d; + }; + +#endif + + + + + +