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