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