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