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@6:99403113343f, 2018-05-07 (annotated)
- Committer:
- batchee7
- Date:
- Mon May 07 05:15:19 2018 +0000
- Revision:
- 6:99403113343f
- Parent:
- 5:1206105e20bd
InitialRelease
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 | |
batchee7 | 2:dd64c2cf9066 | 33 | #ifndef PidController_H |
batchee7 | 2:dd64c2cf9066 | 34 | #define PidController_H |
batchee7 | 4:b590bd8fec6f | 35 | #include "mbed.h" |
batchee7 | 2:dd64c2cf9066 | 36 | |
batchee7 | 2:dd64c2cf9066 | 37 | //-- Constants used in system |
batchee7 | 6:99403113343f | 38 | const int RATE = 100; //--(ms) Time that Calculate mehtod is being called |
batchee7 | 2:dd64c2cf9066 | 39 | const int AUTOMATIC = 0; //-- In automatic then PID Controls Output |
batchee7 | 2:dd64c2cf9066 | 40 | const int MANUAL = 1; //-- In Manual then User Controls Output directly |
batchee7 | 2:dd64c2cf9066 | 41 | |
batchee7 | 2:dd64c2cf9066 | 42 | //-- Constructor |
batchee7 | 2:dd64c2cf9066 | 43 | |
batchee7 | 2:dd64c2cf9066 | 44 | class PidController{ |
batchee7 | 2:dd64c2cf9066 | 45 | public: |
batchee7 | 2:dd64c2cf9066 | 46 | // Public Methods |
batchee7 | 2:dd64c2cf9066 | 47 | |
batchee7 | 6:99403113343f | 48 | // Constructor |
batchee7 | 6:99403113343f | 49 | PidController(); |
batchee7 | 2:dd64c2cf9066 | 50 | |
batchee7 | 2:dd64c2cf9066 | 51 | /** Performs the PID Calculation |
batchee7 | 2:dd64c2cf9066 | 52 | * @param SP - Setpoint (target value) units depends on what PID is controlling |
batchee7 | 2:dd64c2cf9066 | 53 | * @param PV - Process Variable (feedback/ measure value) units depends on what PID is controlling |
batchee7 | 6:99403113343f | 54 | * @return Returns If Controller is in Automatic then returns PID controlled signal. In manual returns the user SP. |
batchee7 | 2:dd64c2cf9066 | 55 | */ |
batchee7 | 6:99403113343f | 56 | float Calculate(float SP, float PV); |
batchee7 | 2:dd64c2cf9066 | 57 | |
batchee7 | 4:b590bd8fec6f | 58 | /** Update Internal Settings |
batchee7 | 2:dd64c2cf9066 | 59 | * @param Bias - Added to the PID Calculation |
batchee7 | 2:dd64c2cf9066 | 60 | * @param PropGain - Proportional Gain |
batchee7 | 2:dd64c2cf9066 | 61 | * @param IntGain - Integral Gain |
batchee7 | 2:dd64c2cf9066 | 62 | * @param DiffGain - Differential Gain |
batchee7 | 2:dd64c2cf9066 | 63 | * @param OutputMin - Minimum Limit for the Output (units are same as setpoint) |
batchee7 | 2:dd64c2cf9066 | 64 | * @param OutputMax - Maximum Limit for the Output (units are same as setpoint) |
batchee7 | 2:dd64c2cf9066 | 65 | * @param OutputScale - Multiplier at End of PID loop to convert from engineering units to signal (eg PWM duty cycle) |
batchee7 | 2:dd64c2cf9066 | 66 | */ |
batchee7 | 5:1206105e20bd | 67 | void UpdateSettings(float Bias, float PropGain, float IntGain, float DiffGain, float OutputMin, float OutputMax); |
batchee7 | 2:dd64c2cf9066 | 68 | |
batchee7 | 4:b590bd8fec6f | 69 | /** Update Internal Settings |
batchee7 | 4:b590bd8fec6f | 70 | * @param OutputMin - Minimum Limit for the Output (units are same as setpoint) |
batchee7 | 4:b590bd8fec6f | 71 | * @param OutputMax - Maximum Limit for the Output (units are same as setpoint) |
batchee7 | 4:b590bd8fec6f | 72 | */ |
batchee7 | 4:b590bd8fec6f | 73 | void UpdateSettings(float OutputMin, float OutputMax); |
batchee7 | 4:b590bd8fec6f | 74 | |
batchee7 | 2:dd64c2cf9066 | 75 | /** Get the controller mode |
batchee7 | 2:dd64c2cf9066 | 76 | * @return 1 = AUTOMATIC, 0 = MANUAL(User has direct contol on output). |
batchee7 | 2:dd64c2cf9066 | 77 | */ |
batchee7 | 2:dd64c2cf9066 | 78 | int GetMode(void){return (int)mode;} |
batchee7 | 2:dd64c2cf9066 | 79 | |
batchee7 | 2:dd64c2cf9066 | 80 | /** Set the controller in Manual Mode (PID turned off SP is writted directly to output) |
batchee7 | 2:dd64c2cf9066 | 81 | */ |
batchee7 | 2:dd64c2cf9066 | 82 | void SetManual(void){mode = MANUAL;} |
batchee7 | 2:dd64c2cf9066 | 83 | |
batchee7 | 2:dd64c2cf9066 | 84 | /** Set the controller in Automatic Mode (PID turned on) |
batchee7 | 2:dd64c2cf9066 | 85 | */ |
batchee7 | 2:dd64c2cf9066 | 86 | void SetAutomatic(void){mode = AUTOMATIC;} |
batchee7 | 2:dd64c2cf9066 | 87 | |
batchee7 | 2:dd64c2cf9066 | 88 | /** Enable Diagnostics for HMI |
batchee7 | 2:dd64c2cf9066 | 89 | */ |
batchee7 | 2:dd64c2cf9066 | 90 | void StartDiag(void); |
batchee7 | 2:dd64c2cf9066 | 91 | |
batchee7 | 2:dd64c2cf9066 | 92 | /** Disable Diagnostics for HMI |
batchee7 | 2:dd64c2cf9066 | 93 | */ |
batchee7 | 3:c169d08a9d0b | 94 | void EndDiag(void); |
batchee7 | 2:dd64c2cf9066 | 95 | |
batchee7 | 2:dd64c2cf9066 | 96 | /** Check to see if the controller is collecting diagnostics data |
batchee7 | 2:dd64c2cf9066 | 97 | * @return true then a diagnostics message is been created. |
batchee7 | 2:dd64c2cf9066 | 98 | */ |
batchee7 | 2:dd64c2cf9066 | 99 | bool DiagnosticsEnabled(void){return collectDiagnostics;} |
batchee7 | 2:dd64c2cf9066 | 100 | |
batchee7 | 6:99403113343f | 101 | /** Check to see if the controller is collecting diagnostics data |
batchee7 | 6:99403113343f | 102 | * @param Data - Starting address of target recieve buffer (must be at least 25char long) |
batchee7 | 6:99403113343f | 103 | */ |
batchee7 | 6:99403113343f | 104 | void GetDiagnosticsMessage(char *data); |
batchee7 | 2:dd64c2cf9066 | 105 | |
batchee7 | 2:dd64c2cf9066 | 106 | private: |
batchee7 | 2:dd64c2cf9066 | 107 | bool mode; |
batchee7 | 2:dd64c2cf9066 | 108 | //-- For Diagnostics to GUI |
batchee7 | 2:dd64c2cf9066 | 109 | bool collectDiagnostics; |
batchee7 | 6:99403113343f | 110 | uint16_t elapsedTime; |
batchee7 | 6:99403113343f | 111 | char diagMsg[28]; |
batchee7 | 6:99403113343f | 112 | |
batchee7 | 6:99403113343f | 113 | //-- Settings |
batchee7 | 6:99403113343f | 114 | float bias; |
batchee7 | 6:99403113343f | 115 | float minLimit, maxLimit; |
batchee7 | 6:99403113343f | 116 | float K_p,K_i,K_d; |
batchee7 | 2:dd64c2cf9066 | 117 | |
batchee7 | 2:dd64c2cf9066 | 118 | //-- For PID Calculations |
batchee7 | 2:dd64c2cf9066 | 119 | float error; |
batchee7 | 2:dd64c2cf9066 | 120 | float accumError; |
batchee7 | 5:1206105e20bd | 121 | float prevError; |
batchee7 | 6:99403113343f | 122 | |
batchee7 | 6:99403113343f | 123 | //-- Private Helper functions |
batchee7 | 6:99403113343f | 124 | void float_to_byte(char *data, float *val); |
batchee7 | 6:99403113343f | 125 | void int_to_byte(char *data, uint16_t val); |
batchee7 | 6:99403113343f | 126 | }; |
batchee7 | 2:dd64c2cf9066 | 127 | |
batchee7 | 2:dd64c2cf9066 | 128 | #endif |
batchee7 | 2:dd64c2cf9066 | 129 | |
batchee7 | 2:dd64c2cf9066 | 130 | |
batchee7 | 2:dd64c2cf9066 | 131 | |
batchee7 | 2:dd64c2cf9066 | 132 | |
batchee7 | 2:dd64c2cf9066 | 133 | |
batchee7 | 2:dd64c2cf9066 | 134 |