首页 上一页 1 2 3 下一页 尾页

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



东京末班车

  • 精华:0帖
  • 求助:0帖
  • 帖子:2帖 | 8回
  • 年度积分:0
  • 历史总积分:93
  • 注册:2016年10月10日
发表于:2017-06-20 16:47:09
41楼

谢谢,谢谢,正在找这方面的资料,打算开发一个控制器

weishaojun125

  • 精华:0帖
  • 求助:0帖
  • 帖子:2帖 | 761回
  • 年度积分:0
  • 历史总积分:491
  • 注册:2008年2月15日
发表于:2017-08-16 09:50:44
42楼

厉害了,都是高手,谢谢分享!!!!

brillian0315

  • 精华:0帖
  • 求助:0帖
  • 帖子:0帖 | 479回
  • 年度积分:0
  • 历史总积分:276
  • 注册:2016年1月27日
发表于:2018-06-05 15:26:15
43楼

XIE XIE FEN XIANG

miao520

  • 精华:0帖
  • 求助:0帖
  • 帖子:2帖 | 176回
  • 年度积分:0
  • 历史总积分:107
  • 注册:2015年9月14日
发表于:2018-10-12 08:29:41
44楼

谢谢分享,新手过来学习学习


tantaode123

  • 精华:0帖
  • 求助:0帖
  • 帖子:5帖 | 30回
  • 年度积分:0
  • 历史总积分:174
  • 注册:2016年11月11日
发表于:2019-02-20 16:38:13
45楼

回复内容:

对: tomyi void main(void) { float d... 内容的回复!

-------------------------


acdc_1

  • 精华:0帖
  • 求助:0帖
  • 帖子:0帖 | 76回
  • 年度积分:0
  • 历史总积分:111
  • 注册:2008年12月03日
发表于:2019-02-28 19:44:49
46楼

谢谢分享,新手过来学习学习


热门招聘
相关主题

官方公众号

智造工程师
    首页 上一页 1 2 3 下一页 尾页