经典PID控制算法用C语言实现!

1.经典PID控制算法框图

2.经典PID控制算法公式 

3. 经典PID控制公式用C语言实现

float pidUpdate(PidObject* pid, const float error)
{
    float output;
    pid->error = error; 
    pid->integ += pid->error * pid->dt;

    if (pid->integ > pid->iLimit)
        {
            pid->integ = pid->iLimit;
        }
    else if (pid->integ < pid->iLimitLow)
        {
            pid->integ = pid->iLimitLow;
        }
    pid->deriv = (pid->error - pid->prevError) / pid->dt;
    pid->outP = pid->kp * pid->error;
    pid->outI = pid->ki * pid->integ;
    pid->outD = pid->kd * pid->deriv;
    output = pid->outP + pid->outI + pid->outD;
    pid->prevError = pid->error;
    return output;
}

PidObject 为 PID 对象结构体数据类型:
typedef struct
{
	float desired;		//< set point
	float error;        //< error
	float prevError;    //< previous error
	float integ;        //< integral
	float deriv;        //< derivative
	float kp;           //< proportional gain
	float ki;           //< integral gain
	float kd;           //< derivative gain
	float outP;         //< proportional output (debugging)
	float outI;         //< integral output (debugging)
	float outD;         //< derivative output (debugging)
	float iLimit;       //< integral limit
	float outputLimit;  //< total PID output limit, absolute value. '0' means no limit.
	float dt;           //< delta-time dt
	float out;	        //< out
} PidObject;

第一个参数为将被更新的 PID 结构体对象,第二个参数则是偏差(期望值-测量值),积分项为偏差对时间的积分,微分项则是偏差对时间的微分,然后函数里面有三个参数 pid->kp,pid->ki,pid->kd 分别指的是该 pid 对象的比例项,积分项和微分项系数,每个 pid 对象都有属于自己的 PID 系数,PID 初始化 pid 对象的时候会设定一组默认的系数,同时这组系数是可以调整的,我们常说的 PID 参数整定,其实就是调整这组系数,让它满足你的系统。

(参考ATK公式四旋翼飞行器开源代码)

猜你喜欢

转载自blog.csdn.net/weibo1230123/article/details/86736470