Simple PID Controller with Integral Windup Supports creating a diagnostics message to send to a GUI Prints to Binary
Fork of PidControllerV2 by
PidController.h@4:b590bd8fec6f, 2017-10-04 (annotated)
- Committer:
- batchee7
- Date:
- Wed Oct 04 01:13:49 2017 +0000
- Revision:
- 4:b590bd8fec6f
- Parent:
- 3:c169d08a9d0b
- Child:
- 5:1206105e20bd
Working upload before planning to make more generic
Who changed what in which revision?
User | Revision | Line number | New 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 | 4:b590bd8fec6f | 39 | |
batchee7 | 2:dd64c2cf9066 | 40 | |
batchee7 | 2:dd64c2cf9066 | 41 | #ifndef PidController_H |
batchee7 | 2:dd64c2cf9066 | 42 | #define PidController_H |
batchee7 | 4:b590bd8fec6f | 43 | #include "mbed.h" |
batchee7 | 4:b590bd8fec6f | 44 | #include "math.h" |
batchee7 | 2:dd64c2cf9066 | 45 | |
batchee7 | 2:dd64c2cf9066 | 46 | //-- Constants used in system |
batchee7 | 2:dd64c2cf9066 | 47 | const int RATE = 100; //--(ms) Time that Calculate mehtod is being called |
batchee7 | 2:dd64c2cf9066 | 48 | const int AUTOMATIC = 0; //-- In automatic then PID Controls Output |
batchee7 | 2:dd64c2cf9066 | 49 | const int MANUAL = 1; //-- In Manual then User Controls Output directly |
batchee7 | 2:dd64c2cf9066 | 50 | |
batchee7 | 2:dd64c2cf9066 | 51 | //-- Constructor |
batchee7 | 2:dd64c2cf9066 | 52 | |
batchee7 | 2:dd64c2cf9066 | 53 | class PidController{ |
batchee7 | 2:dd64c2cf9066 | 54 | public: |
batchee7 | 2:dd64c2cf9066 | 55 | // Public Methods |
batchee7 | 2:dd64c2cf9066 | 56 | |
batchee7 | 2:dd64c2cf9066 | 57 | /** Constructor |
batchee7 | 2:dd64c2cf9066 | 58 | * |
batchee7 | 2:dd64c2cf9066 | 59 | */ |
batchee7 | 4:b590bd8fec6f | 60 | PidController(bool); |
batchee7 | 2:dd64c2cf9066 | 61 | |
batchee7 | 2:dd64c2cf9066 | 62 | /** Performs the PID Calculation |
batchee7 | 2:dd64c2cf9066 | 63 | * @param SP - Setpoint (target value) units depends on what PID is controlling |
batchee7 | 2:dd64c2cf9066 | 64 | * @param PV - Process Variable (feedback/ measure value) units depends on what PID is controlling |
batchee7 | 2:dd64c2cf9066 | 65 | * @return Returns If Controller is in Automatic then returns PID controlled signal. In manual returns the user SP multipled by scalar. |
batchee7 | 2:dd64c2cf9066 | 66 | */ |
batchee7 | 4:b590bd8fec6f | 67 | int Calculate(float SP, float PV, float ManualMV); |
batchee7 | 2:dd64c2cf9066 | 68 | |
batchee7 | 4:b590bd8fec6f | 69 | /** Update Internal Settings |
batchee7 | 2:dd64c2cf9066 | 70 | * @param Bias - Added to the PID Calculation |
batchee7 | 2:dd64c2cf9066 | 71 | * @param PropGain - Proportional Gain |
batchee7 | 2:dd64c2cf9066 | 72 | * @param IntGain - Integral Gain |
batchee7 | 2:dd64c2cf9066 | 73 | * @param DiffGain - Differential Gain |
batchee7 | 2:dd64c2cf9066 | 74 | * @param OutputMin - Minimum Limit for the Output (units are same as setpoint) |
batchee7 | 2:dd64c2cf9066 | 75 | * @param OutputMax - Maximum Limit for the Output (units are same as setpoint) |
batchee7 | 2:dd64c2cf9066 | 76 | * @param OutputScale - Multiplier at End of PID loop to convert from engineering units to signal (eg PWM duty cycle) |
batchee7 | 2:dd64c2cf9066 | 77 | */ |
batchee7 | 2:dd64c2cf9066 | 78 | void UpdateSettings(float Bias, float PropGain, float IntGain, float DiffGain, float OutputMin, float OutputMax, float OutputScale); |
batchee7 | 2:dd64c2cf9066 | 79 | |
batchee7 | 4:b590bd8fec6f | 80 | /** Update Internal Settings |
batchee7 | 4:b590bd8fec6f | 81 | * @param OutputMin - Minimum Limit for the Output (units are same as setpoint) |
batchee7 | 4:b590bd8fec6f | 82 | * @param OutputMax - Maximum Limit for the Output (units are same as setpoint) |
batchee7 | 4:b590bd8fec6f | 83 | */ |
batchee7 | 4:b590bd8fec6f | 84 | void UpdateSettings(float OutputMin, float OutputMax); |
batchee7 | 4:b590bd8fec6f | 85 | |
batchee7 | 2:dd64c2cf9066 | 86 | /** Get the controller mode |
batchee7 | 2:dd64c2cf9066 | 87 | * @return 1 = AUTOMATIC, 0 = MANUAL(User has direct contol on output). |
batchee7 | 2:dd64c2cf9066 | 88 | */ |
batchee7 | 2:dd64c2cf9066 | 89 | int GetMode(void){return (int)mode;} |
batchee7 | 2:dd64c2cf9066 | 90 | |
batchee7 | 2:dd64c2cf9066 | 91 | /** Set the controller in Manual Mode (PID turned off SP is writted directly to output) |
batchee7 | 2:dd64c2cf9066 | 92 | */ |
batchee7 | 2:dd64c2cf9066 | 93 | void SetManual(void){mode = MANUAL;} |
batchee7 | 2:dd64c2cf9066 | 94 | |
batchee7 | 2:dd64c2cf9066 | 95 | /** Set the controller in Automatic Mode (PID turned on) |
batchee7 | 2:dd64c2cf9066 | 96 | */ |
batchee7 | 2:dd64c2cf9066 | 97 | void SetAutomatic(void){mode = AUTOMATIC;} |
batchee7 | 2:dd64c2cf9066 | 98 | |
batchee7 | 2:dd64c2cf9066 | 99 | /** Enable Diagnostics for HMI |
batchee7 | 2:dd64c2cf9066 | 100 | */ |
batchee7 | 2:dd64c2cf9066 | 101 | void StartDiag(void); |
batchee7 | 2:dd64c2cf9066 | 102 | |
batchee7 | 2:dd64c2cf9066 | 103 | /** Disable Diagnostics for HMI |
batchee7 | 2:dd64c2cf9066 | 104 | */ |
batchee7 | 3:c169d08a9d0b | 105 | void EndDiag(void); |
batchee7 | 2:dd64c2cf9066 | 106 | |
batchee7 | 2:dd64c2cf9066 | 107 | /** Build a string to send back to HMI to Graph PID |
batchee7 | 2:dd64c2cf9066 | 108 | */ |
batchee7 | 2:dd64c2cf9066 | 109 | void BuildDiagMessage(float SP, float PV, float PWM, float PropAction, float IntAction, float DifAction); |
batchee7 | 2:dd64c2cf9066 | 110 | |
batchee7 | 2:dd64c2cf9066 | 111 | /** Check to see if the controller is collecting diagnostics data |
batchee7 | 2:dd64c2cf9066 | 112 | * @return true then a diagnostics message is been created. |
batchee7 | 2:dd64c2cf9066 | 113 | */ |
batchee7 | 2:dd64c2cf9066 | 114 | bool DiagnosticsEnabled(void){return collectDiagnostics;} |
batchee7 | 2:dd64c2cf9066 | 115 | |
batchee7 | 2:dd64c2cf9066 | 116 | int GetElapsedtime(void){return elapsedTime;} |
batchee7 | 2:dd64c2cf9066 | 117 | |
batchee7 | 2:dd64c2cf9066 | 118 | // Made Public as im lazy |
batchee7 | 2:dd64c2cf9066 | 119 | char diagMsg[65]; |
batchee7 | 2:dd64c2cf9066 | 120 | |
batchee7 | 2:dd64c2cf9066 | 121 | private: |
batchee7 | 2:dd64c2cf9066 | 122 | bool mode; |
batchee7 | 2:dd64c2cf9066 | 123 | |
batchee7 | 2:dd64c2cf9066 | 124 | //-- For Diagnostics to GUI |
batchee7 | 2:dd64c2cf9066 | 125 | bool collectDiagnostics; |
batchee7 | 2:dd64c2cf9066 | 126 | int elapsedTime; |
batchee7 | 2:dd64c2cf9066 | 127 | |
batchee7 | 2:dd64c2cf9066 | 128 | //-- For PID Calculations |
batchee7 | 4:b590bd8fec6f | 129 | bool squareRootOuptut; |
batchee7 | 2:dd64c2cf9066 | 130 | float bias, scalar; |
batchee7 | 2:dd64c2cf9066 | 131 | float error; |
batchee7 | 4:b590bd8fec6f | 132 | float lastInput; |
batchee7 | 2:dd64c2cf9066 | 133 | float accumError; |
batchee7 | 2:dd64c2cf9066 | 134 | float minLimit, maxLimit; |
batchee7 | 2:dd64c2cf9066 | 135 | float K_p,K_i,K_d; |
batchee7 | 2:dd64c2cf9066 | 136 | }; |
batchee7 | 2:dd64c2cf9066 | 137 | |
batchee7 | 2:dd64c2cf9066 | 138 | #endif |
batchee7 | 2:dd64c2cf9066 | 139 | |
batchee7 | 2:dd64c2cf9066 | 140 | |
batchee7 | 2:dd64c2cf9066 | 141 | |
batchee7 | 2:dd64c2cf9066 | 142 | |
batchee7 | 2:dd64c2cf9066 | 143 | |
batchee7 | 2:dd64c2cf9066 | 144 |