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.
Fork of PidControllerV2 by
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 00033 #ifndef PidController_H 00034 #define PidController_H 00035 #include "mbed.h" 00036 00037 //-- Constants used in system 00038 const int RATE = 100; //--(ms) Time that Calculate mehtod is being called 00039 const int AUTOMATIC = 0; //-- In automatic then PID Controls Output 00040 const int MANUAL = 1; //-- In Manual then User Controls Output directly 00041 00042 //-- Constructor 00043 00044 class PidController{ 00045 public: 00046 // Public Methods 00047 00048 // Constructor 00049 PidController(); 00050 00051 /** Performs the PID Calculation 00052 * @param SP - Setpoint (target value) units depends on what PID is controlling 00053 * @param PV - Process Variable (feedback/ measure value) units depends on what PID is controlling 00054 * @return Returns If Controller is in Automatic then returns PID controlled signal. In manual returns the user SP. 00055 */ 00056 float Calculate(float SP, float PV); 00057 00058 /** Update Internal Settings 00059 * @param Bias - Added to the PID Calculation 00060 * @param PropGain - Proportional Gain 00061 * @param IntGain - Integral Gain 00062 * @param DiffGain - Differential Gain 00063 * @param OutputMin - Minimum Limit for the Output (units are same as setpoint) 00064 * @param OutputMax - Maximum Limit for the Output (units are same as setpoint) 00065 * @param OutputScale - Multiplier at End of PID loop to convert from engineering units to signal (eg PWM duty cycle) 00066 */ 00067 void UpdateSettings(float Bias, float PropGain, float IntGain, float DiffGain, float OutputMin, float OutputMax); 00068 00069 /** Update Internal Settings 00070 * @param OutputMin - Minimum Limit for the Output (units are same as setpoint) 00071 * @param OutputMax - Maximum Limit for the Output (units are same as setpoint) 00072 */ 00073 void UpdateSettings(float OutputMin, float OutputMax); 00074 00075 /** Get the controller mode 00076 * @return 1 = AUTOMATIC, 0 = MANUAL(User has direct contol on output). 00077 */ 00078 int GetMode(void){return (int)mode;} 00079 00080 /** Set the controller in Manual Mode (PID turned off SP is writted directly to output) 00081 */ 00082 void SetManual(void){mode = MANUAL;} 00083 00084 /** Set the controller in Automatic Mode (PID turned on) 00085 */ 00086 void SetAutomatic(void){mode = AUTOMATIC;} 00087 00088 /** Enable Diagnostics for HMI 00089 */ 00090 void StartDiag(void); 00091 00092 /** Disable Diagnostics for HMI 00093 */ 00094 void EndDiag(void); 00095 00096 /** Check to see if the controller is collecting diagnostics data 00097 * @return true then a diagnostics message is been created. 00098 */ 00099 bool DiagnosticsEnabled(void){return collectDiagnostics;} 00100 00101 /** Check to see if the controller is collecting diagnostics data 00102 * @param Data - Starting address of target recieve buffer (must be at least 25char long) 00103 */ 00104 void GetDiagnosticsMessage(char *data); 00105 00106 private: 00107 bool mode; 00108 //-- For Diagnostics to GUI 00109 bool collectDiagnostics; 00110 uint16_t elapsedTime; 00111 char diagMsg[28]; 00112 00113 //-- Settings 00114 float bias; 00115 float minLimit, maxLimit; 00116 float K_p,K_i,K_d; 00117 00118 //-- For PID Calculations 00119 float error; 00120 float accumError; 00121 float prevError; 00122 00123 //-- Private Helper functions 00124 void float_to_byte(char *data, float *val); 00125 void int_to_byte(char *data, uint16_t val); 00126 }; 00127 00128 #endif 00129 00130 00131 00132 00133 00134
Generated on Wed Jul 13 2022 02:40:15 by
1.7.2
