DESCRIPTION Bumpless transfer algorithim. When suddenly changing
setpoints, or when restarting the PID equation after an
extended pause, the derivative of the equation can cause
a bump in the controller output. This function will help
smooth out that bump. The process value in *pv should
be the updated just before this function is used.
void pid_bumpless(struct _pid *pid)
{
pid->last_error = (pid->sp)-(pid->pv);
}
pid_calc
DESCRIPTION Performs PID calculations for the _pid structure *a. This function uses the positional form of the pid equation, and incorporates an integral windup prevention algorithim. Rectangular integration is used, so this function must be repeated on a consistent time basis for accurate control.
RETURN VALUE The new output value for the pid loop.
USAGE #include "control.h"*/
float pid_calc(struct _pid *pid) {
int err;
float pterm, dterm, result, ferror;
err = (pid->sp) - (pid->pv);
if (abs(err) > pid->deadband) {
ferror = (float) err; /*do integer to float conversion only once*/
pterm = pid->pgain * ferror;
if (pterm > 100 || pterm < -100) {
pid->integral = 0.0; }
else {
pid->integral += pid->igain * ferror;
if (pid->integral > 100.0)
{
pid->integral = 100.0; }
else if (pid->integral < 0.0) pid->integral = 0.0; }