This is a copy of the Reference Standard PID controller ala controlguru.com
Fork of PID by
PID.h@1:117e0c36eb22, 2016-01-25 (annotated)
- Committer:
- unix_guru
- Date:
- Mon Jan 25 22:29:38 2016 +0000
- Revision:
- 1:117e0c36eb22
- Parent:
- 0:d58c1b8d63d9
- Child:
- 2:55bf0f813bb4
Old Standard PID library from ControlGuru.com
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
unix_guru | 1:117e0c36eb22 | 1 | //#pragma once |
arnaudsuire | 0:d58c1b8d63d9 | 2 | |
arnaudsuire | 0:d58c1b8d63d9 | 3 | #ifndef PID_H |
arnaudsuire | 0:d58c1b8d63d9 | 4 | #define PID_H |
arnaudsuire | 0:d58c1b8d63d9 | 5 | |
unix_guru | 1:117e0c36eb22 | 6 | #define MANUAL_MODE 0 |
unix_guru | 1:117e0c36eb22 | 7 | #define AUTO_MODE 1 |
unix_guru | 1:117e0c36eb22 | 8 | |
arnaudsuire | 0:d58c1b8d63d9 | 9 | class PID |
arnaudsuire | 0:d58c1b8d63d9 | 10 | { |
arnaudsuire | 0:d58c1b8d63d9 | 11 | public: |
arnaudsuire | 0:d58c1b8d63d9 | 12 | |
arnaudsuire | 0:d58c1b8d63d9 | 13 | /* |
unix_guru | 1:117e0c36eb22 | 14 | * Constructor |
arnaudsuire | 0:d58c1b8d63d9 | 15 | * Sets default limits, calculates tuning parameters, and sets manual mode with no bias. |
arnaudsuire | 0:d58c1b8d63d9 | 16 | * @param Kc - Tuning parameter |
arnaudsuire | 0:d58c1b8d63d9 | 17 | * @param tauI - Tuning parameter |
arnaudsuire | 0:d58c1b8d63d9 | 18 | * @param tauD - Tuning parameter |
arnaudsuire | 0:d58c1b8d63d9 | 19 | * @param interval PID calculation performed every interval seconds. |
arnaudsuire | 0:d58c1b8d63d9 | 20 | */ |
arnaudsuire | 0:d58c1b8d63d9 | 21 | PID(float Kc, float tauI, float tauD, float interval); |
arnaudsuire | 0:d58c1b8d63d9 | 22 | |
arnaudsuire | 0:d58c1b8d63d9 | 23 | /* |
arnaudsuire | 0:d58c1b8d63d9 | 24 | * Scale from inputs to 0-100%. |
arnaudsuire | 0:d58c1b8d63d9 | 25 | * @param InMin The real world value corresponding to 0%. |
arnaudsuire | 0:d58c1b8d63d9 | 26 | * @param InMax The real world value corresponding to 100%. |
arnaudsuire | 0:d58c1b8d63d9 | 27 | */ |
arnaudsuire | 0:d58c1b8d63d9 | 28 | void setInputLimits(float inMin, float inMax); |
arnaudsuire | 0:d58c1b8d63d9 | 29 | |
arnaudsuire | 0:d58c1b8d63d9 | 30 | /* |
arnaudsuire | 0:d58c1b8d63d9 | 31 | * Scale from outputs to 0-100%. |
arnaudsuire | 0:d58c1b8d63d9 | 32 | * @param outMin The real world value corresponding to 0%. |
arnaudsuire | 0:d58c1b8d63d9 | 33 | * @param outMax The real world value corresponding to 100%. |
arnaudsuire | 0:d58c1b8d63d9 | 34 | */ |
arnaudsuire | 0:d58c1b8d63d9 | 35 | void setOutputLimits(float outMin, float outMax); |
arnaudsuire | 0:d58c1b8d63d9 | 36 | |
arnaudsuire | 0:d58c1b8d63d9 | 37 | /* |
arnaudsuire | 0:d58c1b8d63d9 | 38 | * Calculate PID constants. |
arnaudsuire | 0:d58c1b8d63d9 | 39 | * Allows parameters to be changed on the fly without ruining calculations. |
arnaudsuire | 0:d58c1b8d63d9 | 40 | * @param Kc - Tuning parameter |
arnaudsuire | 0:d58c1b8d63d9 | 41 | * @param tauI - Tuning parameter |
arnaudsuire | 0:d58c1b8d63d9 | 42 | * @param tauD - Tuning parameter |
arnaudsuire | 0:d58c1b8d63d9 | 43 | */ |
arnaudsuire | 0:d58c1b8d63d9 | 44 | void setTunings(float Kc, float tauI, float tauD); |
arnaudsuire | 0:d58c1b8d63d9 | 45 | |
arnaudsuire | 0:d58c1b8d63d9 | 46 | /* |
arnaudsuire | 0:d58c1b8d63d9 | 47 | * Reinitializes controller internals. Automatically |
arnaudsuire | 0:d58c1b8d63d9 | 48 | * called on a manual to auto transition. |
arnaudsuire | 0:d58c1b8d63d9 | 49 | */ |
arnaudsuire | 0:d58c1b8d63d9 | 50 | void reset(void); |
arnaudsuire | 0:d58c1b8d63d9 | 51 | |
arnaudsuire | 0:d58c1b8d63d9 | 52 | /* |
arnaudsuire | 0:d58c1b8d63d9 | 53 | * Set how fast the PID loop is run. |
arnaudsuire | 0:d58c1b8d63d9 | 54 | * @param interval PID calculation peformed every interval seconds. |
arnaudsuire | 0:d58c1b8d63d9 | 55 | */ |
arnaudsuire | 0:d58c1b8d63d9 | 56 | void setInterval(float interval); |
arnaudsuire | 0:d58c1b8d63d9 | 57 | |
arnaudsuire | 0:d58c1b8d63d9 | 58 | /* |
unix_guru | 1:117e0c36eb22 | 59 | * Set the target value for the PID loop to maintain. |
unix_guru | 1:117e0c36eb22 | 60 | * @param sp The target value to maintain. |
unix_guru | 1:117e0c36eb22 | 61 | */ |
unix_guru | 1:117e0c36eb22 | 62 | void setSetPoint(float sp); |
unix_guru | 1:117e0c36eb22 | 63 | |
unix_guru | 1:117e0c36eb22 | 64 | /* |
unix_guru | 1:117e0c36eb22 | 65 | * Set the target value for the PID loop to maintain. |
unix_guru | 1:117e0c36eb22 | 66 | * @param pv The target value to maintain. |
unix_guru | 1:117e0c36eb22 | 67 | */ |
unix_guru | 1:117e0c36eb22 | 68 | void setProcessValue(float pv); |
unix_guru | 1:117e0c36eb22 | 69 | |
unix_guru | 1:117e0c36eb22 | 70 | /** |
unix_guru | 1:117e0c36eb22 | 71 | * Set PID to manual or auto mode. |
unix_guru | 1:117e0c36eb22 | 72 | * |
unix_guru | 1:117e0c36eb22 | 73 | * @param mode 0 -> Manual |
unix_guru | 1:117e0c36eb22 | 74 | * Non-zero -> Auto |
unix_guru | 1:117e0c36eb22 | 75 | */ |
unix_guru | 1:117e0c36eb22 | 76 | void setMode(int mode); |
unix_guru | 1:117e0c36eb22 | 77 | |
unix_guru | 1:117e0c36eb22 | 78 | /* |
arnaudsuire | 0:d58c1b8d63d9 | 79 | * Set the bias. |
arnaudsuire | 0:d58c1b8d63d9 | 80 | * @param bias The bias for the controller output. |
arnaudsuire | 0:d58c1b8d63d9 | 81 | */ |
arnaudsuire | 0:d58c1b8d63d9 | 82 | void setBias(float bias); |
arnaudsuire | 0:d58c1b8d63d9 | 83 | |
arnaudsuire | 0:d58c1b8d63d9 | 84 | /* |
arnaudsuire | 0:d58c1b8d63d9 | 85 | * PID calculation. |
arnaudsuire | 0:d58c1b8d63d9 | 86 | * @return The controller output as a float between outMin and outMax. |
arnaudsuire | 0:d58c1b8d63d9 | 87 | */ |
arnaudsuire | 0:d58c1b8d63d9 | 88 | float compute(float pv, float sp); |
arnaudsuire | 0:d58c1b8d63d9 | 89 | |
arnaudsuire | 0:d58c1b8d63d9 | 90 | //Getters. |
arnaudsuire | 0:d58c1b8d63d9 | 91 | float getInMin(); |
arnaudsuire | 0:d58c1b8d63d9 | 92 | float getInMax(); |
arnaudsuire | 0:d58c1b8d63d9 | 93 | float getOutMin(); |
arnaudsuire | 0:d58c1b8d63d9 | 94 | float getOutMax(); |
arnaudsuire | 0:d58c1b8d63d9 | 95 | float getInterval(); |
arnaudsuire | 0:d58c1b8d63d9 | 96 | float getPParam(); |
arnaudsuire | 0:d58c1b8d63d9 | 97 | float getIParam(); |
arnaudsuire | 0:d58c1b8d63d9 | 98 | float getDParam(); |
arnaudsuire | 0:d58c1b8d63d9 | 99 | |
arnaudsuire | 0:d58c1b8d63d9 | 100 | private: |
arnaudsuire | 0:d58c1b8d63d9 | 101 | |
arnaudsuire | 0:d58c1b8d63d9 | 102 | bool usingFeedForward; |
unix_guru | 1:117e0c36eb22 | 103 | bool inAuto; |
unix_guru | 1:117e0c36eb22 | 104 | |
arnaudsuire | 0:d58c1b8d63d9 | 105 | //Actual tuning parameters used in PID calculation. |
arnaudsuire | 0:d58c1b8d63d9 | 106 | float Kc_; |
arnaudsuire | 0:d58c1b8d63d9 | 107 | float tauR_; |
arnaudsuire | 0:d58c1b8d63d9 | 108 | float tauD_; |
arnaudsuire | 0:d58c1b8d63d9 | 109 | |
arnaudsuire | 0:d58c1b8d63d9 | 110 | //Raw tuning parameters. |
arnaudsuire | 0:d58c1b8d63d9 | 111 | float pParam_; |
arnaudsuire | 0:d58c1b8d63d9 | 112 | float iParam_; |
arnaudsuire | 0:d58c1b8d63d9 | 113 | float dParam_; |
arnaudsuire | 0:d58c1b8d63d9 | 114 | |
arnaudsuire | 0:d58c1b8d63d9 | 115 | //The point we want to reach. |
arnaudsuire | 0:d58c1b8d63d9 | 116 | float setPoint_; |
arnaudsuire | 0:d58c1b8d63d9 | 117 | //The thing we measure. |
arnaudsuire | 0:d58c1b8d63d9 | 118 | float processVariable_; |
arnaudsuire | 0:d58c1b8d63d9 | 119 | float prevProcessVariable_; |
arnaudsuire | 0:d58c1b8d63d9 | 120 | //The output that affects the process variable. |
arnaudsuire | 0:d58c1b8d63d9 | 121 | float controllerOutput_; |
arnaudsuire | 0:d58c1b8d63d9 | 122 | float prevControllerOutput_; |
arnaudsuire | 0:d58c1b8d63d9 | 123 | |
arnaudsuire | 0:d58c1b8d63d9 | 124 | //We work in % for calculations so these will scale from |
arnaudsuire | 0:d58c1b8d63d9 | 125 | //real world values to 0-100% and back again. |
arnaudsuire | 0:d58c1b8d63d9 | 126 | float inMin_; |
arnaudsuire | 0:d58c1b8d63d9 | 127 | float inMax_; |
arnaudsuire | 0:d58c1b8d63d9 | 128 | float inSpan_; |
arnaudsuire | 0:d58c1b8d63d9 | 129 | float outMin_; |
arnaudsuire | 0:d58c1b8d63d9 | 130 | float outMax_; |
arnaudsuire | 0:d58c1b8d63d9 | 131 | float outSpan_; |
arnaudsuire | 0:d58c1b8d63d9 | 132 | |
arnaudsuire | 0:d58c1b8d63d9 | 133 | //The accumulated error, i.e. integral. |
arnaudsuire | 0:d58c1b8d63d9 | 134 | float accError_; |
arnaudsuire | 0:d58c1b8d63d9 | 135 | //The controller output bias. |
arnaudsuire | 0:d58c1b8d63d9 | 136 | float bias_; |
arnaudsuire | 0:d58c1b8d63d9 | 137 | |
arnaudsuire | 0:d58c1b8d63d9 | 138 | //The interval between samples. |
arnaudsuire | 0:d58c1b8d63d9 | 139 | float tSample_; |
arnaudsuire | 0:d58c1b8d63d9 | 140 | |
arnaudsuire | 0:d58c1b8d63d9 | 141 | //Controller output as a real world value. |
arnaudsuire | 0:d58c1b8d63d9 | 142 | volatile float realOutput_; |
arnaudsuire | 0:d58c1b8d63d9 | 143 | |
arnaudsuire | 0:d58c1b8d63d9 | 144 | }; |
arnaudsuire | 0:d58c1b8d63d9 | 145 | |
arnaudsuire | 0:d58c1b8d63d9 | 146 | #endif |
arnaudsuire | 0:d58c1b8d63d9 | 147 |