PID 算法(c语言) 点击:7221 | 回复:47



yhsu

    
  • 精华:1帖
  • 求助:0帖
  • 帖子:5帖 | 98回
  • 年度积分:0
  • 历史总积分:126
  • 注册:2002年7月04日
发表于:2003-11-12 10:36:00
楼主
BC31 TC30 编译过,可运行。 #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;; /*------------------------------------------------------------------------ pid_init DESCRIPTION This function initializes the pointers in the _pid structure to the process variable and the setpoint. *pv and *sp are integer pointers. ------------------------------------------------------------------------*/ 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; } /*------------------------------------------------------------------------ pid_tune DESCRIPTION Sets the proportional gain (p_gain), integral gain (i_gain), derivitive gain (d_gain), and the dead band (dead_band) of a pid control structure _pid. ------------------------------------------------------------------------*/ 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; } /*------------------------------------------------------------------------ pid_setinteg DESCRIPTION Set a new value for the integral term of the pid equation. This is useful for setting the initial output of the pid controller at start up. ------------------------------------------------------------------------*/ void pid_setinteg(struct _pid *pid,float new_integ) { pid->integral = new_integ; pid->last_error = 0; } /*------------------------------------------------------------------------ pid_bumpless 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; } dterm = ((float)(err - pid->last_error)) * pid->dgain; result = pterm + pid->integral + dterm; } else result = pid->integral; pid->last_error = err; return (result); } void main(void) { float display_value; int count=0; pid = &warm; // printf("Enter the values of Proce



工控学徒

  • 精华:5帖
  • 求助:0帖
  • 帖子:218帖 | 1663回
  • 年度积分:0
  • 历史总积分:9769
  • 注册:2002年12月13日
发表于:2009-09-09 14:54:29
21楼
辛苦了!谢谢!                   

daniel

  • 精华:0帖
  • 求助:0帖
  • 帖子:3帖 | 69回
  • 年度积分:0
  • 历史总积分:385
  • 注册:2007年11月23日
发表于:2009-09-10 15:16:52
22楼
先学习了。。谢谢楼上的辛苦整理。

笑子

  • 精华:0帖
  • 求助:0帖
  • 帖子:23帖 | 106回
  • 年度积分:0
  • 历史总积分:200
  • 注册:2004年4月12日
发表于:2009-12-09 09:54:59
23楼

学习一下!!!!!!!!

小爷们

  • 精华:0帖
  • 求助:0帖
  • 帖子:19帖 | 129回
  • 年度积分:0
  • 历史总积分:182
  • 注册:2004年12月10日
发表于:2009-12-10 13:31:13
24楼
很感谢,非常好的资料

小爷们

  • 精华:0帖
  • 求助:0帖
  • 帖子:19帖 | 129回
  • 年度积分:0
  • 历史总积分:182
  • 注册:2004年12月10日
发表于:2009-12-10 13:31:32
25楼
                                      

相信明天

  • 精华:0帖
  • 求助:0帖
  • 帖子:0帖 | 8回
  • 年度积分:0
  • 历史总积分:13
  • 注册:2009年10月24日
发表于:2010-04-26 00:06:04
26楼
好,学习一下。看来得提高英语水平了。

xixia

  • 精华:0帖
  • 求助:0帖
  • 帖子:0帖 | 26回
  • 年度积分:0
  • 历史总积分:31
  • 注册:2002年3月01日
发表于:2010-04-26 02:27:01
27楼
学习一下。看来得提高英语水平了

kyle251

  • 精华:0帖
  • 求助:0帖
  • 帖子:0帖 | 1回
  • 年度积分:0
  • 历史总积分:111
  • 注册:2011年4月09日
发表于:2011-04-09 14:08:01
28楼
感谢提供资料,好好学习,天天向上

leafegk

  • 精华:0帖
  • 求助:0帖
  • 帖子:2帖 | 85回
  • 年度积分:0
  • 历史总积分:154
  • 注册:2008年5月19日
发表于:2011-04-13 14:25:05
29楼
楼主和16楼的tomyi真是两个好人!

事非经过不知难

  • 精华:1帖
  • 求助:1帖
  • 帖子:13帖 | 1814回
  • 年度积分:0
  • 历史总积分:3231
  • 注册:2006年12月07日
发表于:2011-04-13 21:28:44
30楼

引用leafegk 的回复内容:楼主和16楼的tomyi真是两个好人!



谢谢分享!保存,以后一定有用。

汪孔乙

  • 精华:0帖
  • 求助:0帖
  • 帖子:1帖 | 81回
  • 年度积分:0
  • 历史总积分:136
  • 注册:2009年8月24日
发表于:2011-04-15 14:32:04
31楼
谢谢分享!保存,以后学习!

gk000

  • 精华:0帖
  • 求助:1帖
  • 帖子:1帖 | 92回
  • 年度积分:0
  • 历史总积分:493
  • 注册:2010年9月08日
发表于:2011-05-23 17:27:29
32楼
呵呵 很好 很实用呢 感谢ing

测电笔

  • 精华:0帖
  • 求助:0帖
  • 帖子:0帖 | 75回
  • 年度积分:0
  • 历史总积分:214
  • 注册:2009年2月05日
发表于:2011-05-23 18:24:50
33楼
好,学习一下。看来得提高英语水平了

kkh680

  • 精华:0帖
  • 求助:0帖
  • 帖子:0帖 | 10回
  • 年度积分:0
  • 历史总积分:0
  • 注册:1900年1月01日
发表于:2012-11-13 13:50:29
34楼

谢谢了,学习了………………

润浪

  • 精华:0帖
  • 求助:1帖
  • 帖子:2帖 | 172回
  • 年度积分:81
  • 历史总积分:209
  • 注册:2007年1月18日
发表于:2016-03-22 23:03:05
35楼

如果有注解就好了,学习学习

liuczdyx

  • 精华:0帖
  • 求助:0帖
  • 帖子:2帖 | 18回
  • 年度积分:0
  • 历史总积分:2
  • 注册:2013年12月26日
发表于:2016-04-18 09:27:59
36楼

的确很强大,无数个赞

小抠脚大汉

  • 精华:0帖
  • 求助:3帖
  • 帖子:10帖 | 221回
  • 年度积分:4
  • 历史总积分:286
  • 注册:2015年11月02日
发表于:2016-04-18 17:05:21
37楼

这一步提示语法错误是怎么回事

小抠脚大汉

  • 精华:0帖
  • 求助:3帖
  • 帖子:10帖 | 221回
  • 年度积分:4
  • 历史总积分:286
  • 注册:2015年11月02日
发表于:2016-04-18 17:07:03
38楼

红圈内的XXXXXXXXXX

eighteenthird

  • 精华:0帖
  • 求助:0帖
  • 帖子:0帖 | 19回
  • 年度积分:0
  • 历史总积分:7
  • 注册:2012年12月30日
发表于:2016-06-02 17:15:25
39楼

你所框的红圈内的都是注释,你是不是把注释的格式符给删了?

回复内容:

对: 小抠脚大汉 红圈内的XXXXXXXXXX 内容的回复!

 

南城逸雪

  • 精华:0帖
  • 求助:0帖
  • 帖子:0帖 | 274回
  • 年度积分:0
  • 历史总积分:189
  • 注册:2015年9月23日
发表于:2017-06-13 14:39:50
40楼

先学习了。。谢谢楼上的辛苦整理。


热门招聘
相关主题

官方公众号

智造工程师