#include <stdio.h>
#include<math.h>
struct _pid
{
int pv; //integer that contains the process value 过程量
int sp; //*integer that contains the set point 设定值
float integral; // 积分值 -- 偏差累计值
float pgain;
float igain;
float dgain;
int deadband; //死区
int last_error;
};
struct _pid warm,*pid;
int process_point, set_point,dead_band; float p_gain, i_gain, d_gain,integral_val,new_integ;
void pid_init(struct _pid *warm, int process_point, int set_point)
{
struct _pid *pid;
pid = warm;
pid->pv = process_point;
pid->sp = set_point;
}
void pid_tune(struct _pid *pid, float p_gain, float i_gain, float d_gain, int dead_band)
{
pid->pgain = p_gain;
pid->igain = i_gain;
pid->dgain = d_gain;
pid->deadband = dead_band;
pid->integral= integral_val;
pid->last_error=0;
}
void pid_setinteg(struct _pid *pid,float new_integ)
{
pid->integral = new_integ;
pid->last_error = 0;
}
void pid_bumpless(struct _pid *pid)
{
pid->last_error = (pid->sp)-(pid->pv); //设定值与反馈值偏差
}
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;
pterm = pid->pgain * ferror;
if (pterm > 100 || pterm < -100)