PID算法c语言来自老外.docx
- 文档编号:3884396
- 上传时间:2023-05-06
- 格式:DOCX
- 页数:8
- 大小:17.06KB
PID算法c语言来自老外.docx
《PID算法c语言来自老外.docx》由会员分享,可在线阅读,更多相关《PID算法c语言来自老外.docx(8页珍藏版)》请在冰点文库上搜索。
PID算法c语言来自老外
PID算法(c语言)(来自老外)
(2010-02-1700:
18:
24)
转载
#include
#include
//定义PID的结构体
struct_pid
{
intpv;//integerthatcontainstheprocessvalue过程量
intsp;//*integerthatcontainsthesetpoint设定值
floatintegral;//积分值--偏差累计值
floatpgain;
floatigain;
floatdgain;
intdeadband;//死区
intlast_error;
};
struct_pidwarm,*pid;
intprocess_point,set_point,dead_band;
floatp_gain,i_gain,d_gain,integral_val,new_integ;;
//----------------------------------------------
pid_initDESCRIPTIONThisfunctioninitializesthepointersinthe_pidstructuretotheprocessvariableandthesetpoint.*pvand*spareintegerpointers.
//----------------------------------------------
voidpid_init(struct_pid*warm,intprocess_point,intset_point)
{
struct_pid*pid;
pid=warm;
pid->pv=process_point;
pid->sp=set_point;
}
//----------------------------------------------pid_tuneDESCRIPTIONSetstheproportionalgain(p_gain),integralgain(i_gain),
derivitivegain(d_gain),andthedeadband(dead_band)ofapidcontrolstructure_pid.
设定PID参数----P,I,D,死区
//----------------------------------------------
voidpid_tune(struct_pid*pid,floatp_gain,floati_gain,floatd_gain,intdead_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_setintegDESCRIPTIONSetanewvaluefortheintegraltermofthepidequation.
Thisisusefulforsettingtheinitialoutputofthepidcontrolleratstartup.
设定输出初始值
//----------------------------------------------
voidpid_setinteg(struct_pid*pid,floatnew_integ)
{
pid->integral=new_integ;
pid->last_error=0;
}
//----------------------------------------------
pid_bumplessDESCRIPTIONBumplesstransferalgorithim.
Whensuddenlychangingsetpoints,orwhenrestartingthePIDequationafteranextendedpause,
thederivativeoftheequationcancauseabumpinthecontrolleroutput.Thisfunctionwillhelpsmoothoutthatbump.
Theprocessvaluein*pvshouldbetheupdatedjustbeforethisfunctionisused.
pid_bumpless实现无扰切换
当突然改变设定值时,或重新启动后,将引起扰动输出。
这个函数将能实现平顺扰动,在调用该函数之前需要先更新PV值
//----------------------------------------------
voidpid_bumpless(struct_pid*pid)
{
pid->last_error=(pid->sp)-(pid->pv);//设定值与反馈值偏差
}
//----------------------------------------------
pid_calcDESCRIPTIONPerformsPIDcalculationsforthe_pidstructure*a.
Thisfunctionusesthepositionalformofthepidequation,andincorporatesanintegralwinduppreventionalgorithim.
Rectangularintegrationisused,sothisfunctionmustberepeatedonaconsistenttimebasisforaccuratecontrol.
RETURNVALUEThenewoutputvalueforthepidloop.USAGE#include"control.h"
本函数使用位置式PID计算方式,并且采取了积分饱和限制运算
PID计算
//----------------------------------------------
floatpid_calc(struct_pid*pid)
{·
interr;
floatpterm,dterm,result,ferror;
//计算偏差
err=(pid->sp)-(pid->pv);
//判断是否大于死区
if(abs(err)>pid->deadband)
{
ferror=(float)err;//dointegertofloatconversiononlyonce数据类型转换
//比例项
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
elseif(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);
}
//----------------------------------------------
voidmain(void)
{
floatdisplay_value;
intcount=0;
pid=&warm;
//printf("EnterthevaluesofProcesspoint,Setpoint,Pgain,Igain,Dgain\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("ThevaluesofProcesspoint,Setpoint,Pgain,Igain,Dgain\n");
printf("%6d%6d%4f%4f%4f\n",process_point,set_point,p_gain,i_gain,d_gain);
printf("EnterthevaluesofProcesspoint\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);
//Getinputvalueforprocesspoint
pid_bumpless(&warm);
//howtodisplayoutput
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++;
}
}
增量式PID算法
#include
#include
struct_pid
{doubleset;
doubleactual;
doubleerr;
doubleerr_one;
doubleerr_two;
doubleKp,Ki,Kd;
}pid;
voidPID_init()
{pid.set=0.0;
pid.actual=0.0;
pid.err=0.0;
pid.err_one=0.0;
pid.err_two=0.0;
pid.Kp=0.2;
pid.Ki=0.02;
pid.Kd=0.2;
}
doublePID_real(doublespeed)
{doubleincrement;
pid.set=speed;
pid.err=pid.set-pid.actual;
increment=pid.Kp*(pid.err-pid.err_one)+pid.Ki*pid.err+pid.Kd*(pid.err-2*pid.err_one+pid.err_two);pid.actual+=increment;
pid.err_two=pid.err_one;
pid.err_one=pid.err;
returnpid.actual;
}
main()
{inti;
PID_init();
for(i=0;i<1200;i++)
{doublespeed=PID_real(100.0);
printf("%f\n",speed);
}system("pause");
}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- PID 算法 语言 来自 老外
![提示](https://static.bingdoc.com/images/bang_tan.gif)