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