492 Example PID Algorithm Listings Appendix G

PIDA Source Listing
/********************************************************************************************/
/* I/O Channels */
/* Must be defined by the user */
/* */
/* inchan - Input channel name */
/* outchan - Output channel name */
/* */
/********************************************************************************************/
/* */
/********************************************************************************************/
/* PID algorithm for VT1422A controller module. This algorithm is called */
/* once per scan trigger by main(). It performs Proportional, Integral */
/* and Derivative control. */
/* */
/* */
/* The output is derived from the following equations: */
/* */
/* PID_out = P_out + I_out + D_out */
/* P_out = Error * P_factor */
/* I_out = I_out + (Error * I_factor) */
/* D_out = ((Error - Error_old) * D_factor) */
/* Error = Setpoint - PV */
/* */
/* where: */
/* Setpoint is the desired value of the process variable (user supplied) */
/* PV is process variable measured on the input channel */
/* PID_out is the algorithm result sent to the output channel */
/* P_factor, I_factor and D_factor are the PID constants(user supplied) */
/* */
/* */
/* At startup the output will abruptly change to P_factor*Error */
/* */
/* */
/********************************************************************************************/
/* */
/* User determined control parameters */
static float Setpoint = 0; /* The setpoint */
static float P_factor = 1; /* Proportional control constant */
static float I_factor = 0; /* Integral control constant */
static float D_factor = 0; /* Derivative control constant */
/* */
/* Other Variables */
static float I_out; /* Integral term */
static float Error; /* Error term */
static float Error_old; /* Last Error - for derivative */
/* */
/*PID algorithm code: */
/* Begin PID calculations */
/* First, find the Process Variable "error" */
/* This calculation has gain of minus one (-1) */
Error = Setpoint - inchan;
/* On the first trigger after INIT, initialize the I and D terms */
if (First_loop)
{
/* Zero the I term and start integrating */
I_out = Error * I_factor;
/* Zero the derivative term */
Error_old = Error;
}
/* On subsequent triggers, continue integrating */
else /* not First trigger */
{ I_out = Error * I_factor + I_out;
}
/* Sum PID terms */
outchan = Error * P_factor + I_out + D_factor * (Error
- Error_old);
/* Save values for next pass */
Error_old = Error;