Hi, we started looking for the PID library for our robots. Did not adopted it yet, but we worked a little bit on it. We used the v06, not the FixedPoint version, which we saw later, :(.
I don't know if this may be useful, but here is the code with the small changes. Basically, added the "const" when we think it's needed, converted the getters to inlines, and with a default param in the constructor, eliminated the second constructor and the ConstructorCommon(). The only change in the interface is that the Bias is now the last param in the constructor, due to the C++ requirement that the default arguments must be the last ones).
So we compiled the three examples, and them are a bit reduced now, and the inline members must be a little faster:
- PIDSample: 5790 bytes -> 5708 bytes
- PIDSample2: 5730 bytes -> 5648 bytes
- PIDSample3: 5948 bytes -> 5826 bytes
I know it's not a lot of less bytes, but it's not only smaller, but also a little more robust (due to the const).
Here is the .h, and the cpp will be in the next post, to make this shorter:
class PID
{
public:
#define AUTO 1
#define MANUAL 0
#define LIBRARY_VERSION 0.6
//commonly used functions **************************************************************************
PID(double*, double*, double*, // * constructor. links the PID to the Input, Output, and
const double, const double, const double,
//This is truly NULL in C++ (see http://www2.research.att.com/~bs/bs_faq2.html#null):
double *FFBias = 0); // Setpoint. Initial tuning parameters are also set here
void SetMode(const int Mode); // * sets PID to either Manual (0) or Auto (non-0)
void Compute(); // * performs the PID calculation. it should be
// called every time loop() cycles. ON/OFF and
// calculation frequency can be set using SetMode
// SetSampleTime respectively
void SetInputLimits(const double, const double); //Tells the PID what 0-100% are for the Input
void SetOutputLimits(const double, const double); //Tells the PID what 0-100% are for the Output
//available but not commonly used functions ********************************************************
void SetTunings(const double, const double, // * While most users will set the tunings once in the
const double); // constructor, this function gives the user the option
// of changing tunings during runtime for Adaptive control
void SetSampleTime(const int); // * sets the frequency, in Milliseconds, with which
// the PID calculation is performed. default is 1000
void Reset(); // * reinitializes controller internals. automatically
// called on a manual to auto transition
/*****************************************************************************
* STATUS SECTION
* These functions allow the outside world to query the status of the PID
*****************************************************************************/
bool JustCalculated()
{
return justCalced;
}
int GetMode() const
{
if(inAuto)
return 1;
else
return 0;
}
inline double GetINMin() const
{
return inMin;
}
inline double GetINMax() const
{
return inMin + inSpan;
}
inline double GetOUTMin() const
{
return outMin;
}
inline double GetOUTMax() const
{
return outMin+outSpan;
}
inline int GetSampleTime() const
{
return tSample;
}
inline double GetP_Param() const
{
return P_Param;
}
inline double GetI_Param() const
{
return I_Param;
}
inline double GetD_Param() const
{
return D_Param;
}
private:
//scaled, tweaked parameters we'll actually be using
double kc; // * (P)roportional Tuning Parameter
double taur; // * (I)ntegral Tuning Parameter
double taud; // * (D)erivative Tuning Parameter
//nice, pretty parameters we'll give back to the user if they ask what the tunings are
double P_Param;
double I_Param;
double D_Param;
double *myInput; // * Pointers to the Input, Output, and Setpoint variables
double *myOutput; // This creates a hard link between the variables and the
double *mySetpoint; // PID, freeing the user from having to constantly tell us
// what these values are. with pointers we'll just know.
double *myBias; // * Pointer to the External FeedForward bias, only used
// if the advanced constructor is used
unsigned long nextCompTime; // * Helps us figure out when the PID Calculation needs to
// be performed next
// to determine when to compute next
unsigned long tSample; // * the frequency, in milliseconds, with which we want the
// the PID calculation to occur.
bool inAuto; // * Flag letting us know if we are in Automatic or not
double lastOutput; // * remembering the last output is used to prevent
// reset windup.
double lastInput; // * we need to remember the last Input Value so we can compute
// the derivative required for the D term
double accError; // * the (I)ntegral term is based on the sum of error over
// time. this variable keeps track of that
double bias; // * the base output from which the PID operates
double inMin, inSpan; // * input and output limits, and spans. used convert
double outMin, outSpan; // real world numbers into percent span, with which
// the PID algorithm is more comfortable.
bool justCalced; // * flag gets set for one cycle after the pid calculates
};
Regards,
Julián