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