Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
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 #include "mbed.h" 00040 00041 #ifndef PidController_H 00042 #define PidController_H 00043 00044 //-- Constants used in system 00045 const int RATE = 100; //--(ms) Time that Calculate mehtod is being called 00046 const int AUTOMATIC = 0; //-- In automatic then PID Controls Output 00047 const int MANUAL = 1; //-- In Manual then User Controls Output directly 00048 00049 //-- Constructor 00050 00051 class PidController{ 00052 public: 00053 // Public Methods 00054 00055 /** Constructor 00056 * 00057 */ 00058 PidController(); 00059 00060 /** Performs the PID Calculation 00061 * @param SP - Setpoint (target value) units depends on what PID is controlling 00062 * @param PV - Process Variable (feedback/ measure value) units depends on what PID is controlling 00063 * @return Returns If Controller is in Automatic then returns PID controlled signal. In manual returns the user SP multipled by scalar. 00064 */ 00065 int Calculate(float SP, float PV); 00066 00067 /** Update Internal Seetings 00068 * @param Bias - Added to the PID Calculation 00069 * @param PropGain - Proportional Gain 00070 * @param IntGain - Integral Gain 00071 * @param DiffGain - Differential Gain 00072 * @param OutputMin - Minimum Limit for the Output (units are same as setpoint) 00073 * @param OutputMax - Maximum Limit for the Output (units are same as setpoint) 00074 * @param OutputScale - Multiplier at End of PID loop to convert from engineering units to signal (eg PWM duty cycle) 00075 */ 00076 void UpdateSettings(float Bias, float PropGain, float IntGain, float DiffGain, float OutputMin, float OutputMax, float OutputScale); 00077 00078 /** Get the controller mode 00079 * @return 1 = AUTOMATIC, 0 = MANUAL(User has direct contol on output). 00080 */ 00081 int GetMode(void){return (int)mode;} 00082 00083 /** Set the controller in Manual Mode (PID turned off SP is writted directly to output) 00084 */ 00085 void SetManual(void){mode = MANUAL;} 00086 00087 /** Set the controller in Automatic Mode (PID turned on) 00088 */ 00089 void SetAutomatic(void){mode = AUTOMATIC;} 00090 00091 /** Enable Diagnostics for HMI 00092 */ 00093 void StartDiag(void); 00094 00095 /** Disable Diagnostics for HMI 00096 */ 00097 void EndDiag(void); 00098 00099 /** Build a string to send back to HMI to Graph PID 00100 */ 00101 void BuildDiagMessage(float SP, float PV, float PWM, float PropAction, float IntAction, float DifAction); 00102 00103 /** Check to see if the controller is collecting diagnostics data 00104 * @return true then a diagnostics message is been created. 00105 */ 00106 bool DiagnosticsEnabled(void){return collectDiagnostics;} 00107 00108 int GetElapsedtime(void){return elapsedTime;} 00109 00110 // Made Public as im lazy 00111 char diagMsg[65]; 00112 00113 private: 00114 bool mode; 00115 00116 //-- For Diagnostics to GUI 00117 bool collectDiagnostics; 00118 int elapsedTime; 00119 00120 //-- For PID Calculations 00121 float bias, scalar; 00122 float error; 00123 float lastError; 00124 float accumError; 00125 float minLimit, maxLimit; 00126 float K_p,K_i,K_d; 00127 }; 00128 00129 #endif 00130 00131 00132 00133 00134 00135
Generated on Wed Jul 13 2022 11:29:52 by
1.7.2