《PID 算法的 嵌入式 C语言应用》由会员分享,可在线阅读,更多相关《PID 算法的 嵌入式 C语言应用(4页珍藏版)》请在金锄头文库上搜索。
1、PID算法的嵌入式C语言应用(原创的哦)PID控制算法这东东知道好久了,原来它对我来说好像只是纸上写 写的数学东东,具体如何用程序实现似懂非懂,这几天没事干,研究 了下把心得写下PID控制算法经过离散化后 就变成了公式如下= Kp勒或Q十蓬艺:沖十屈夙們成上一(如何得到?嘻嘻,我们不管了哦,反正拿来用的哦!书上有详细解 释的 这里解释下几个字母:U (K)表示为控制器的输出(控制器输出干嘛用?当然就是給执行器的啦,控制执行器 大小啦)Kp,Ki,Kd就是我们的参数PID三个参数了哦 e (K)表示系统偏差(系统偏差是啥?当然是我们设定 的值和取进来值之差啦,越小越好啦,为0最好了说明我们设定1
2、0 度反馈也是10度,误差0哦呵呵)e (K-1)表示上次采样周期的系统偏差(上次采样周期?计算机采样不是需要时间么)知道了这些用C语言来实现这个公式应该是比较简单的!首先我们定义一个结构体变量,这个结构体变量包括Kp (用pgain 表示),Ki (用 integral 表示),Kd (用 dgain 表示),e (K1)(用 last_error 表示)以及设定值(sv),测量值(pv),死区值(deadband) struct _pid int pv; /*integer that contains the process value*/int sp; /*integer that co
3、ntains the set point*/float integral;float pgain;float igain;float dgain;int deadband;int last_error;定义了这个结构体后,我们可以用一个函数来实现PID的那个公式 公式的主体部分我用红线表示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 i
4、nteger to float conversion only once*/ pterm = pid-pgain * ferror;if (pterm 100 II pterm integral = 0.0;elsepid-integral += pid-igain * ferror;if (pid-integral 100.0)pid-integral = 100.0;else if (pid-integral integral = 0.0;dterm = (float)(err - pid-last_error) * pid-dgain;result = pterm + pid-integral + dterm;else result = pid-integral;pid-last_error = err;return (result);