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