/************  PID算法(C语言)  ************/#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 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);pid_init(&warm, process_point, set_point);pid_tune(&warm, p_gain,i_gain,d_gain,dead_band);pid_setinteg(&warm,0.0); //pid_setinteg(&warm,30.0);//Get input value for process pointpid_bumpless(&warm);// how to display outputdisplay_value = pid_calc(&warm); printf("%f\n", display_value); //printf("\n%f%f%f%f",warm.pv,warm.sp,warm.igain,warm.dgain); count++; } }

转载于:https://www.cnblogs.com/gaohongchen01/p/3700018.html

PID算法(C语言)相关推荐

  1. 三菱FX3U PLC 位置式PID算法(ST语言)

    三菱PLC自带的PID不必多说,大家可以自行查看指令说明.关于FX3U 增量式PID可以参看专栏的另一篇博客 三菱PLC增量式PID算法FB(带死区设置和外部复位控制)_RXXW_Dor的博客-CSD ...

  2. c语言写pid算法,用c语言实现的pid算法

    pid算法应该算是所以算法中最稳定最可靠最简单的算法,在库函数中添加这种算法对实际控制的时延有非常大的帮助. 全部资料51hei下载地址:   C语言PID算法.doc PID算法(c语言)(来自老外 ...

  3. C语言实现PID算法:位置式PID和增量式PID

    原创者微信公众号 PID算法可以说是在自动控制原理中比较经典的一套算法,在现实生活中应用的比较广泛. 大学参加过电子竞赛的朋友都应该玩过电机(或者说循迹小车),我们要控制电机按照设定的速度运转,PID ...

  4. C语言实现pid算法(附完整源码)

    实现pid算法 pid结构体定义 C语言实现pid算法完整源码(定义,实现,main函数测试) pid结构体定义 struct pid {// Controller gainsfloat kP;flo ...

  5. c语言程序位置式pid算法,位置式PID算法的C语言代码

    描述 位置式PID的C语言写法详解 PID调节口诀: 参数整定找最佳,从小到大顺序查 先是比例后积分,最后再把微分加 曲线振荡很频繁,比例度盘要放大 曲线漂浮绕大湾,比例度盘往小扳 曲线偏离回复慢,积 ...

  6. 小型温控系统c语言程序,pid算法温度控制c语言程序 - 全文

    温度控制PID自整定原理介绍 整定PID(三模式)控制器 整定温度控制器涉及设置比例.积分和微分值,以得到对特定过 程的可能的最佳控制.如果控制器不包含自动整定算法,或者自 动整定算法未提供适合特定应 ...

  7. pid温度控制c语言程序及仿真,温度控制PID算法的C语言程序实例代码

    //PID算法温控C语言 #include #include #include #include struct PID { unsigned int SetPoint; // 设定目标 Desired ...

  8. PID算法入门与C语言代码实现

    PID算法的入门理解以及C代码实现 在结束了自控原理的学习后,了解到PID算法是一种万能算法,在课设中也是经常使用到的一种算法,所以想具体的来进行以下总结与学习,如果有错漏的地方,欢迎大家共同来探讨与 ...

  9. matlab控制算法C语言,PID算法Matlab仿真程序和C程序

    <PID算法Matlab仿真程序和C程序>由会员分享,可在线阅读,更多相关<PID算法Matlab仿真程序和C程序(6页珍藏版)>请在人人文库网上搜索. 1.增量式PID控制算 ...

  10. 温度闭环控制C语言,计算机基于PID算法的模拟温度闭环控制系统.docx

    PAGE PAGE # 目录 目录 摘耍 温度闭环控制系统仿真 摘要 控制系统主要由控制器和控制对象两部分组成,通过一定的控制方法使系统 达到所要求的控制性能.控制模式有开环控制.闭环控制和复合控制三 ...

最新文章

  1. HTML转PDF(C#---itextsharp)(转自别人的文章)
  2. dw自动滚动图片_3分钟搞定图片懒加载
  3. publiccms中将推荐页的内容显示在页面片段中
  4. ora-24247:网络访问被访问控制列表(ACL)拒绝
  5. 记录——《C Primer Plus (第五版)》第九章编程练习第四题
  6. c++ windows forms 使用_从头开始了解和使用Hypervisor(第1部分)
  7. 3 docker容器
  8. Unity Understanding Lifetime Managers
  9. 2017年全国大学生电子设计竞赛报告(F题)调幅信号处理实验电路
  10. 虚拟现实的起源、趋势及应用
  11. ZYNQ-定时器中断使用
  12. Albumentations 中的空间级图像变换
  13. iOS的键盘种类(不同的键盘布局)
  14. 编程技巧│这些好用的网站,你千万别错过
  15. 如何解决chrome一打开就是360搜索页面(亲测有效)
  16. 【BUUCTF】MISC 秘密文件
  17. 第07章 图形操作 · 7.2 绘制图形(1)
  18. 记一次页面区块点击无反应的问题排查
  19. 《Unix编程艺术》 八九章读书笔记
  20. AutoCAD_创建直线,圆弧,圆

热门文章

  1. PHP html 转换成PDF wkhtmltopdf HTML 转换成 PDF (JAVA C#都适用)
  2. WINDOWS 服务端 SVN自动部署/一键批处理 SVN 更新项目
  3. 计算机一级选择题电子档,计算机一级考试选择题
  4. 阶段5 3.微服务项目【学成在线】_day03 CMS页面管理开发_17-异常处理-可预知异常处理-异常处理测试...
  5. python2.7.11 for iOS 苹果上的python27环境
  6. [RMQ] [线段树] POJ 3368 Frequent Values
  7. .NET Framework中的配置文件(config)
  8. win7 64位Apache http server+PHP配置
  9. mysql+centos7+主从复制
  10. logging日志模块 , 序列化json pickle , 随机数random