hello

Dependencies:   mbed-rtos mbed

Fork of BX-car by kao yi

Committer:
TonyLin
Date:
Sat Jun 28 07:32:39 2014 +0000
Revision:
14:303b22b76d7a
Parent:
13:a33a7705fe2b
Dynamic kp!!!!

Who changed what in which revision?

UserRevisionLine numberNew 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 */
backman 7:fd976e1ced33 74 float compute(float pv, float sp);
TonyLin 13:a33a7705fe2b 75
backman 7:fd976e1ced33 76 //Getters.
backman 7:fd976e1ced33 77 float getInMin();
backman 7:fd976e1ced33 78 float getInMax();
backman 7:fd976e1ced33 79 float getOutMin();
backman 7:fd976e1ced33 80 float getOutMax();
backman 7:fd976e1ced33 81 float getInterval();
backman 7:fd976e1ced33 82 float getPParam();
backman 7:fd976e1ced33 83 float getIParam();
backman 7:fd976e1ced33 84 float getDParam();
TonyLin 13:a33a7705fe2b 85
backman 7:fd976e1ced33 86 private:
TonyLin 13:a33a7705fe2b 87
TonyLin 13:a33a7705fe2b 88 // bool usingFeedForward;
TonyLin 13:a33a7705fe2b 89
backman 7:fd976e1ced33 90 //Actual tuning parameters used in PID calculation.
backman 7:fd976e1ced33 91 float Kc_;
backman 11:03d5aa2511c4 92 float tauI_;
backman 7:fd976e1ced33 93 float tauD_;
TonyLin 13:a33a7705fe2b 94
TonyLin 13:a33a7705fe2b 95
TonyLin 13:a33a7705fe2b 96
TonyLin 13:a33a7705fe2b 97 float pParam_;
backman 11:03d5aa2511c4 98 float iParam_;
backman 11:03d5aa2511c4 99 float dParam_;
TonyLin 13:a33a7705fe2b 100
backman 11:03d5aa2511c4 101 float accError_;
TonyLin 13:a33a7705fe2b 102
backman 7:fd976e1ced33 103 //Raw tuning parameters.
TonyLin 13:a33a7705fe2b 104
backman 7:fd976e1ced33 105 //The point we want to reach.
backman 7:fd976e1ced33 106 float setPoint_;
backman 7:fd976e1ced33 107 //The thing we measure.
backman 7:fd976e1ced33 108 float processVariable_;
backman 7:fd976e1ced33 109 float prevProcessVariable_;
backman 7:fd976e1ced33 110 //The output that affects the process variable.
backman 7:fd976e1ced33 111 float controllerOutput_;
backman 7:fd976e1ced33 112 float prevControllerOutput_;
TonyLin 13:a33a7705fe2b 113
backman 7:fd976e1ced33 114 //We work in % for calculations so these will scale from
backman 7:fd976e1ced33 115 //real world values to 0-100% and back again.
backman 7:fd976e1ced33 116 float inMin_;
backman 7:fd976e1ced33 117 float inMax_;
backman 7:fd976e1ced33 118 float inSpan_;
TonyLin 13:a33a7705fe2b 119
backman 7:fd976e1ced33 120 float outMin_;
backman 11:03d5aa2511c4 121 float outMid_;
backman 7:fd976e1ced33 122 float outMax_;
backman 7:fd976e1ced33 123 float outSpan_;
TonyLin 13:a33a7705fe2b 124
backman 11:03d5aa2511c4 125 float ctrl_Lbound;
backman 11:03d5aa2511c4 126 float ctrl_Ubound;
TonyLin 13:a33a7705fe2b 127
TonyLin 13:a33a7705fe2b 128
TonyLin 13:a33a7705fe2b 129
TonyLin 13:a33a7705fe2b 130
backman 7:fd976e1ced33 131 //The accumulated error, i.e. integral.
backman 11:03d5aa2511c4 132 //float accError_;
backman 7:fd976e1ced33 133 //The controller output bias.
TonyLin 13:a33a7705fe2b 134 // float bias_;
TonyLin 13:a33a7705fe2b 135
backman 7:fd976e1ced33 136 //The interval between samples.
backman 7:fd976e1ced33 137 float tSample_;
TonyLin 13:a33a7705fe2b 138
backman 7:fd976e1ced33 139 //Controller output as a real world value.
TonyLin 13:a33a7705fe2b 140 // volatile float realOutput_;
TonyLin 13:a33a7705fe2b 141
backman 7:fd976e1ced33 142 };