首页 上一页 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



yhsu

  • 精华:1帖
  • 求助:0帖
  • 帖子:5帖 | 98回
  • 年度积分:0
  • 历史总积分:126
  • 注册:2002年7月04日
发表于:2003-11-07 16:01:00
1楼
我调试过的,可行!呵呵,我是抄老外的:) 大家共享。

咯咯达

  • 精华:0帖
  • 求助:1帖
  • 帖子:15帖 | 13回
  • 年度积分:0
  • 历史总积分:108
  • 注册:2003年11月09日
发表于:2003-11-10 10:02:00
2楼
就是没有注释

yhsu

  • 精华:1帖
  • 求助:0帖
  • 帖子:5帖 | 98回
  • 年度积分:0
  • 历史总积分:126
  • 注册:2002年7月04日
发表于:2003-11-12 10:36:00
3楼
伙计,已经很详细了!了解PV SV P I D就可以了。

sunly

  • 精华:0帖
  • 求助:0帖
  • 帖子:4帖 | 23回
  • 年度积分:0
  • 历史总积分:120
  • 注册:2005年5月29日
发表于:2005-05-30 13:04:00
4楼
如果有注解就好了

hunter_wjt

  • 精华:0帖
  • 求助:0帖
  • 帖子:0帖 | 3回
  • 年度积分:0
  • 历史总积分:0
  • 注册:2005年8月25日
发表于:2005-08-21 12:11:00
5楼
我是pid控制的新手,很想学学,能不能把程序的详细注释写一下,谢谢了

weixiao

  • 精华:0帖
  • 求助:0帖
  • 帖子:1帖 | 20回
  • 年度积分:0
  • 历史总积分:84
  • 注册:2003年3月31日
发表于:2006-01-04 10:58:00
6楼
兄弟,能不能交流交流,nicemeetyou@163.com

hooko

  • 精华:1帖
  • 求助:0帖
  • 帖子:3帖 | 90回
  • 年度积分:0
  • 历史总积分:146
  • 注册:2005年3月03日
发表于:2007-06-26 23:06:00
7楼
 谢谢了,学习中

三脚猫

  • 精华:0帖
  • 求助:0帖
  • 帖子:180帖 | 1598回
  • 年度积分:0
  • 历史总积分:1097
  • 注册:2004年7月08日
发表于:2007-07-03 12:44:00
8楼
用不着自已编

雅各宾

  • 精华:23帖
  • 求助:1帖
  • 帖子:178帖 | 6966回
  • 年度积分:0
  • 历史总积分:15881
  • 注册:2002年12月10日
发表于:2007-07-08 15:06:00
9楼
作大量的实例好了,以后的工程师套用就行

songzg

  • 精华:0帖
  • 求助:0帖
  • 帖子:96帖 | 732回
  • 年度积分:0
  • 历史总积分:1884
  • 注册:2006年11月16日
发表于:2007-07-24 15:49:00
10楼
太多看不懂

happyoicq

  • 精华:0帖
  • 求助:0帖
  • 帖子:49帖 | 1382回
  • 年度积分:0
  • 历史总积分:3120
  • 注册:2005年10月16日
发表于:2007-07-25 15:14:00
11楼
真是太好了

无竹

  • 精华:0帖
  • 求助:0帖
  • 帖子:84帖 | 708回
  • 年度积分:0
  • 历史总积分:1737
  • 注册:2007年4月11日
发表于:2007-07-26 11:07:00
12楼
 太多看不懂

bestvvvf

  • 精华:1帖
  • 求助:0帖
  • 帖子:7帖 | 38回
  • 年度积分:0
  • 历史总积分:107
  • 注册:2007年5月24日
发表于:2009-02-17 17:07:30
13楼

阳光月光

  • 精华:1帖
  • 求助:0帖
  • 帖子:16帖 | 421回
  • 年度积分:0
  • 历史总积分:1071
  • 注册:2008年9月27日
发表于:2009-02-18 08:18:39
14楼
看不懂呀     能不能发点中文的

实习岗

  • 精华:0帖
  • 求助:0帖
  • 帖子:4帖 | 142回
  • 年度积分:0
  • 历史总积分:448
  • 注册:2006年3月05日
发表于:2009-02-19 22:58:03
15楼
不懂                                        

tomyi

  • 精华:0帖
  • 求助:1帖
  • 帖子:21帖 | 233回
  • 年度积分:0
  • 历史总积分:598
  • 注册:2002年10月30日
发表于:2009-09-09 14:42:21
16楼

稍微乱了点,整理了一下:

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;
}

tomyi

  • 精华:0帖
  • 求助:1帖
  • 帖子:21帖 | 233回
  • 年度积分:0
  • 历史总积分:598
  • 注册:2002年10月30日
发表于:2009-09-09 14:43:14
17楼
/*------------------------------------------------------------------------
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.
---
设定PID参数 ---- P,I,D,死区
------------------------------------------------------------------------*/
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;
}

tomyi

  • 精华:0帖
  • 求助:1帖
  • 帖子:21帖 | 233回
  • 年度积分:0
  • 历史总积分:598
  • 注册:2002年10月30日
发表于:2009-09-09 14:46:11
18楼
/*------------------------------------------------------------------------
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.
---
pid_bumpless 实现无扰切换
当突然改变设定值时,或重新启动后,将引起扰动输出。这个函数将能实现平顺扰动,在调用该函数之前需要先更新PV值
------------------------------------------------------------------------*/
void pid_bumpless(struct _pid *pid)
{
pid->last_error = (pid->sp)-(pid->pv);
}

tomyi

  • 精华:0帖
  • 求助:1帖
  • 帖子:21帖 | 233回
  • 年度积分:0
  • 历史总积分:598
  • 注册:2002年10月30日
发表于:2009-09-09 14:47:09
19楼
/*------------------------------------------------------------------------
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;

// 输出为0--100%
// 如果计算结果大于100,则等于100
if (pid->integral > 100.0)
{
pid->integral = 100.0;
}
// 如果计算结果小于0.0,则等于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;

// 输出PID值(0-100)
return (result);
}

tomyi

  • 精华:0帖
  • 求助:1帖
  • 帖子:21帖 | 233回
  • 年度积分:0
  • 历史总积分:598
  • 注册:2002年10月30日
发表于:2009-09-09 14:47:48
20楼
void main(void)
{
float display_value;
int count=0;
pid = &warm;

// printf("Enter the values of Process point, Set point, P gain, I gain, D gain \n");
// scanf("%d%d%f%f%f", &process_point, &set_point, &p_gain, &i_gain, &d_gain);

// 初始化参数
process_point = 30;
set_point = 40;
p_gain = (float)(5.2);

i_gain = (float)(0.77);
d_gain = (float)(0.18);
dead_band = 2;

integral_val =(float)(0.01);

printf("The values of Process point, Set point, P gain, I gain, D gain \n");
printf(" %6d %6d %4f %4f %4f\n", process_point, set_point, p_gain, i_gain, d_gain);
printf("Enter the values of Process point\n");
while(count<=20)
{
scanf("%d",&process_point);

// 设定PV,SP值
pid_init(&warm, process_point, set_point);

// 初始化PID参数值
pid_tune(&warm, p_gain,i_gain,d_gain,dead_band);

// 初始化PID输出值
pid_setinteg(&warm,0.0);
//pid_setinteg(&warm,30.0);

//Get input value for process point
pid_bumpless(&warm);

// how to display output
display_value = pid_calc(&warm);

printf("%f\n", display_value);
//printf("\n%f%f%f%f",warm.pv,warm.sp,warm.igain,warm.dgain);

count++;
}
}

热门招聘
相关主题

官方公众号

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