您好,欢迎光临本网站![请登录][注册会员]  
文件名称: PID控制算法的C语言实现(完整版).pdf
  所属分类: C
  开发工具:
  文件大小: 397kb
  下载次数: 0
  上传时间: 2019-10-31
  提 供 者: u0100*****
 详细说明:最近两天在考虑一般控制算法的C语言实现问题,发现网络上尚没有一套完整的比较体系的讲解。于是总结了几天,整理一套思路分享给大家。在工业应用中PID及其衍生算法是应用最广泛的算法之一,是当之无愧的万能算法,如果能够熟练掌握PID算法的设计与实现过程,对于一般的研发人员来讲,应该是足够应对一般研发问题了,而难能可贵的是,在我所接触的控制算法当中,PID控制算法又是最简单,最能体现反馈思想的控制算法,可谓经典中的经典。经典的未必是复杂的,经典的东西常常是简单的,而且是最简单的,想想牛顿的力学三大定律吧,想想爱因斯坦的质能方程吧,何等的简单!简单的不是原始的,简单的也不是落后的,简单到了美的程度。先看看PID控制算法的C语言实现二PID算法的离散化 上一节中,我论述了PID算法的基木形式,并对其控制过程的实现有了 个简要的说明,通过上一节的总结,基本已经可以明白PID控制的过程。这一节 中先继续上一节内容补充说明一下。 1.说明一下反馈控制的原理,通过上一节的框图不难看出,PID控制其实 是对偏差的控制过程 2.如果偏差为0,则比例环节不起作用,只有存在偏差时,比例环节才起作 用 3.积分环节主要是用来消除静差,所谓静差,就是系统稳定后输出值和设 定值之间的差值,积分环节实际上就是偏差累计的过程,把累计的误差加到原有 系统上以抵消系统造成的静差。 4.而微分信号则反应了偏差信号的变化规律,或者说是变化趋势,根据偏 差信号的变化趋势来进行超前调节,从而增加了系统的快速性 好了,关于PID的基本说明就补充到这里,下面将对PID连续系统离散化 从而方便在处理器上实现。下面把连续状态的公式再贴 u(x=kp(err(tiTerr(t)dt+-Dderr(t dt 伎设采样间隔为T,则在第KT时刻: 偏差err(K)=rin(K)-rout(K) 积分环节用加和的形式表示,即err(K)err(K+1)+……; 微分环节用斜率的形式表示,即[err(K)-err(K-1)] 从而形成如下PID离散表示形式: u(k)=Kp(err( K)++ err()+T(err(k)-err(k-1) 则u(K)可表示成为 u(k)=k,(err(k)+Ki err(j)Derr(k)-err(k-1) 仝于说Kp、Ki、Kd三个参数的具体表达式,我想可以轻松的推出」,这里节省 时间,不再详细表示了。 其实到这里为止,PID的基本离散表示形式已经出来了。目前的这种表述形式属 于位置型PID,另外一种表述方式为增量式PID,由U上述表达式可以轻易得到: u(k-1)=k(err(k-1)+K>err(j)+Ka(err(k-1)-err(k-2) 那么: Auk)=k(error k werrork-1)+k;erronk)+k(error)-2errork-1+errork-2) 这就是离散化PID的增量式表示方式,由公式可以看出,增量式的表达结果和最 近三次的偏差有关,这样就大大提高了系统的稳定性。需要注意的是最终的输出 结果应该为 u(K)+增量调节值; PID的离散化过程基本思路就是这样,下面是将离散化的公式转换成为C语言, 从而实现微控制器的控制作用 PID控制算法的C语言实现三位置型PID的C语言实 现 上一节中已经抽象出了位置性PID和增量型PID的数学表达式,这一节, 重点讲解C语言代码的实现过程,算法的C语言实现过程具有一般性,通过PID 算法的C语言实现,可以以此类推,设计其它算法的C语言实现。 第一步:定义PID变量结构体,代码如下: struct pi float jetspeed //定义设定值 float Actualspeed /定义实际值 float err //定义偏差值 float crr last //定义上一个偏差值 float Kp, Ki, Kd; //定义比例、积分、微分系数 float voltage /定义电压值(控制执行器的变量) float integral /定义积分值 p 控制算法中所需要用到的参数在一个结构体中统一定义,方使后面的使用。 第二部:初始化变量,代码如下: void pid inito i printf( Pld init begin \n") pid Set speed=0. 0 pid. Actual Speed=0. 0 pid. err=0. 0 pid. err last=0. 0 pid. voltage=0. 0 pid integral=0. 0 pid. kp=0. 2 pid.Ki-0.015; pid Kd=0. 2 printf( pid init end \n") 统一初始化变量,尤其是Kp,Ki,Kd三个参数,调试过程当中,对于要求的控制 效果,可以通过调节这三个量直接进行调节。 第三步:编写控制算法,代码如下: float PID realize(float d) pid Set Speed=speed pid err=pid. SetSpeed-pid ActualSpeed pid integral+=pid pid voltage=pid. Kp*pid err+pid Ki*pid integral+pid Kdsk(pid.err -pid. err last) pid. err last-pI d. err pid. Actual Speed=pid voltage*k1.0 return pid. ActualSpeed 注意:这里用了最基本的算法实现形式,没有考虑死区问题,没有设定上下限, 只是对公式的一种直接的实现,后面的介绍当中还会逐渐的对此改进。 到此为止上,PID的基本实现部分就初步完成了。下面是测试代码: nt maino( printf( system begin \n PID inito int count=0 while(count<1000) float speed-PID realize(200. 0) printf( %f\n, speed) count+t return 0 下面是经过1000次的调节后输出的1000个数据(具体的参数整定过程就不说明 了,网上这种说明非常多) 83.000001 81.321929 99.222808 114.425860 127.335383 138.297401 11.555000 82.800301 100.48260l 115.19556 128.243715 139.068697 59.559675 63.537254 84.268909 101.726572 116.551897 129.140691 139.330352 8.175408 65.52770 85.713108 955049 117.595029 130.026459 140.582499 52.907421 64.011058 87.143455 104.168125 118.625l16 130.901149 141.325237 38.944152 8.810646 88.5300 105.366066 119.642331 131.764909 42.0:870 51.891699 70.355318 89.946960 106.549019 120.646826 132.617870 142.782985 46.141651 72.042040 107.717187 121.638767 133.460162 143,498218 53.339054 108.870756 122.618307 134.29194 144.204509 51.509998 75,207620 91.022231 110.009898 135.113308 144.901969 5.908450 76.745444 95.347186 111.134811 124.540813 135.924419 145.590726 55.944631 78.301526 96.655242 112.245652 125.484079 136.725382 146.270843 58.970680 79.812136 97.947180 113.3426l 126.415549 146.342 47.605718 169.876198 182.680475 190.042233 194.274828 196.708363 198.107481 143.260674 170.252740 182.896971 190.166702 191.346393 196.749493 198.131129 148.907425 170.624605 183.110768 194.417073 196.790138 198.154493 149.516109 170.991799 183.321881 190.411007 190806719176 150.176794 171.354406 183.530369 90.530867 94.55577 196.869889 198.200349 150.799612 171.712487 183.736239 190.649236 194.623820196.909019 198.222843 151.414625 172.066080 183.939545 190.766119 191.691027 196.947656 l98.245062 152.021959 172.415265 184.140301 190.881544 194.757390 196.985803 198.267001 152.621696 172.760077 184.338555 190.995531 94.822919 197.023493 l98.288662 153.213951 173.100594 184.534321 19L.10808 194.887626 197.06070l 198.310059 153.798781 17343688116119.21923195156 197.097449 198.3311 154.3763l5 173.768895 184.918558 19L.329005 195.01463 l97.133 198.352019 154.946626 174.09(796 185.107080 191.437382 195.076955 197.169.558 98.37245 155.509812 174.420594 185.293243 191.544428 195.138496 197.204940 198.392982 174.740352 185.477080 191.650111 195.199273 197.239872 198.413066 156.615146 175.056096 185.68625 191.754:04 195.2:9270 197.274378 l98.432911 57.157471 175.367915 185.837886 191.857565 195.318547 197.308136 198.452499 157.693012 175.6i75818 186.014930 191.959350 195.377050 197.342089 198.471846 158.221871 175.979886 192.059857 195.434856 197.375309 l98.490953 158.744097 176.280136 192.159119 195.491918 197.403 8.509319 159.259826 176.576656 186.5328,59 192.257135 195.548233 197.440523 198.528439 159.769078 176.869444 186.70120 195.603919 197.472520 198.546812 l60.271991 171586000386.868437 192.449511 19750414198.56500 160.768588 177.444121 187.031605 192.543890 195.713145 197.535309 198.58294 161.258996 177.726087 187.193713 192.637105 197.566127 198.500548 161.743264 178.004510 187.3.802 192.729137 195.819654 197.5954f 198.618147 162.221491 178.279458 187.511884 192.820032 195.871912 197.626594 198.635415 162.693737 17855967187.657997 192.909776 195.92351 197.655258 198.552474 163.160075 178.819094 187.822151 192.998410 195.97447 197.685546 l98.669313 163.620593 187.974384 193.085920 196.024791 197.714486 l98.585955 164.075347 179.34531 188.124700 196.07447 197.743047 198.702378 61.524422 179.603504 188.273148 193.257700 196.123558 197.771265 198.718611 164.967877 179.858466 188.419728 193.341993 196.172016 197.799113 198.734525 165.405795 180.110241 193.425214 96.219859 197.826629 198.750448 165.838235 180.358866 193.507408 96.267115 197.853799 l98.766067 1661.265257 180.601388 188.848592 193.588568 196.313778 197.880631 198.781497 166.686967 180.846849 188.987995 193.66 196.359851 197.907131 l98.796736 167.103377 181.086262 193.747847 196.40536 19932819.1776 167.514610 181.322699 189.261576 193.826004 196.450296 197.959122 l98.826628 167.920681 181.556172 189.395801 193.903175 196.494672 197.984629 198.841303 163.321682 181.786733 189.528364 193.979391 196.538492 198.00982 198.355788 168.717670 182.014396 194.054643 198.034705 198.870087 169.108719 182.239222 194.128963 196.624434 198.059275 198.884218 169.494862 182.461226 189.916170 194.202349 196.666678 198.083520 198.898162 198.9119 199.374396 199.640316 199.793204 199.881136 199.931653 l99.960689 193.925533 199.382228 199.64488 199.795787 199.88261 199.932509 l99.961191 198.938970 99.389943 199.649249 199.88403 199.933353 199.9616f65 198.952229 199.397586 199.653636 199.800860 199.88552 199.934187 199.962156 l93.96:320 199.405110 199.803343 9.886971 199.935002 199.962619 198.978257 199.412555 199.662246 199.805802 199.888371 199.935816 198.991033 199.419891 199.65645 199.808225 199.889783 l99.936617 l99.963343 199.003643 199.427152 199.6706.3 199.810624 199.891142 199.937420 199.964014 199.0l6092 199.431307 199.674752 199.812986 99.892518 199.93819 199.964448 199.028390 199.441389 199.678815 199.815326 199.893845 199.938971 199.964907 199.04054 19948633968319:872 199.89:5180 199.939733 l99.965330 199.052535 199.455264 199.686798 199.819915 199.896485 199.940477 l99.965772 199.064373 199.462073 199.690715 199.822175 199.897783 199.941228 99.966201 199.076067 199.468802 199.824388 199.899057 199.941961 199.966625 199.087617 199.475442 199.698409 199.900322 199.942685 199.967046 199.099019 199.481995 199.702177 199.82875;5 199.901562 199.943392 l99.967458 199.110280 199.488175 199.705905 199.830902 199.902797 199.914111 199.967868 199.121407 199.494857 907958:2198306019.90 199.944804 199.968263 199.132381 199.501183 199.713209 199.835097 199.905222 199.945491 199.968664 199.143240 199.507404 199.71678 199.837155 199.906392 199.946l81 99.969047 199.153940 199.513578 99.720339 199.839194 199.907576 199.946854 199.969437 199.164511 199.519639 199.723826 199.841210 199.908720 l99.947518 l99.969817 199.174957 199.525656 19.727276 199.843191 199.909875 199.948165 199.970193 199.185270 199.531579 199.730690 199.845168 199.910985 199.948824 l99.970565 199.195457 199.537437 199.734054 199.847096 99.912l08 l99.949456 199.970943 199.205514 199.5432:30 199.737378 199.849024 199.91319 199.950083 199.215440 199.548936 199.850905 199.91428 199.950714 199.971668 199.225262 199.554583 199.743901 199.852784 199.915352 199.951326 199.972011 199.234930 199.560149 199.747111 199.854621 199.916423 199.951930 199.972363 199.214503 199.7502 199.856449 199.917459 199.952532 199.972712 199.571073 199.753393 199.918505 199.95312 199.973047 199.576436 199.756474 199.860016 199.919527 199.953714 199.973388 199.272463 199.759526 199.861757 199.920526 199.954290 199.973726 199.281571 199.586961 199.863486 99.921513 199.954863 199.974049 199.290541 199.592118 199.765490 198699119594 l99.974379 19.299421 199.597220 199.76842 199.866879 199.9234 199.9979 199.974699 199.308165 199.602260 199.771314 199.868549 199.924415 199.955538 199.975014 199.3l6815 199.607218 199.774169 199.870186 199.925348 199.957073 199.975326 199.325345 199.776992 199.871813 199.926275 199.957623 199.975645 199.333789 199.616974 199.779775 199.873119 199.927198 l99.958146 199.975939 199.342115 199.621764 199.782527 199.874997 199.928108 199.958671 199.976249 199.350336 199.626486 199.785247 199.876563 199.929019 199.959189 199.358462 199.63l156 199.787938 199.878109 19.929903 l99.959693 199.976832 199.366479 199.635757 199.790590 199.879620 199.930788 199.960203 199.977125 199.977414 199.986668 199.992123 199.995346 199.997256 199.998387 l99.998995 199.977688 199.986846 199.992214 199.995416 199.997294 l99.993416 l99.999003 199.992314 199.997325 199.998436 199.99014 199.978217 199.987169 199.992412 199.995536 199.997365 199.998459 l99.999032 199.978:25 199.987321 199.992503 199.995593 99.997403 199.993488 199.999034 199.978782 199.987481 199.992604 199.995653 199.97434 199.998508 l99.999055 l99.979061 199.987633 199.992701 199.99571319.997474199.998537199.999063 199.979312 199.987800 19.992792 199.997512 199.998556 199.99074 199.979576 199.987948 199.992878 199.99581l 99.997543 199.998585 l99.999092 199.979825 199.988094 199.992967 199.995859 199.99753 199.998590 199.999094 199.980077 199.993047 199.995902 199.997614 199.998605 l99.999115 199.98033 199.993136 199.995960 199.997640 l99.998 199.999123 199.980:63 199.988526 99.99216 199.995999 199.997659 199.998634 99.999135 199.980812 199.988675 199.993305 199.99605l 199.997689 199.998612 199.999152 199.988815 199.9933 199.996100 199.997711 199.993654 199.9161 199.981300 199.988965 199.993474 199.996148 199.997740 199.998665 199.999172 199.9815 199.989090 199.993554 199.99619l 199.997760 199.998676 199.999183 199.981755 199.989231 199.997789 199.998694 199.999201 199.981984 199.989359 199.993726 199.996288 199.997809 199.998702 199.999203 199.982213 199.989491 199.993806 l99.996340 199.997838 199.998714 99.999224 199.982427 199.989629 199.993881 199.996389 199.997858 199.998725 199.9992:32 199.982618 199.989757 199.993952 199.996438 199.997880 199.998713 199.999213 19.98860199899199041196989119.999 199.998745 199.999261 199.983080 199.990012 199.994101 199.996538 199.997929 199.998766 l99.999263 199.983293 199.990133 199.994170 199.996578 199.997958 199.993774 199.99284 199.983501 199.990253 199.994241 199.997978 199.99878 99.999292 199.983704 199.990373 199.994313 199.996678 199.998007 199.998803 199.999304 199.983914 199.990493 199.994391 199.996712 199.998027 199.998805 199.999321 199.984114 199.990614 199.99459 199.996746 199.998049 199.998826 199.999323 199.984309 199.990734 199.994531 199.998078 199.999344 199.984:500 199.990854 199.994602 199.996824 199.998098 199.99884 199.999352 199.984698 199.990960 199.994680 199.996855 199.99812 199.998863 199.999364 199.984887 199.991072 199.994748 199.996896 199.998147 199.993871 199.999381 199.985079 199.991180 199.994805 99.998170 199.99883 199.999390 199.98526 199.991289 199.994868 199.996967 99.998199 199.998894 l99.999401 199.985442 199.991398 199.994928 199.997005 199.998218 199.99390 199.999412 199.985623 199.991507 199.994989 199.997036 199.998247 199.998923 199.999430 199.985803 199.991616 199.995045 l99.997076 199.998267 l99.993931 199.999432 199.985984 199.991718 199.995109 199973198 199.998943 199.999453 199.986170 199.991837 199.995175 199.997145 199.998316 199.998954 l99.999461 199.986327 199.995226 199.997185 199.998339 199.998972 199.999473 199.986508 199902519.959 199.998368 199.998974 PID控制算法的C语言实现四增量型PID的C语言实 现 上一节中介绍了最简单的位置型PID的实现手段,这一节主要讲解增量式 PID的实现方法,位冒型和增量型PID的数学公式请参见我的系列文《PID控制 算法的C语言实现二》中的讲解。实现过程仍然是分为定义变量、初始化变量、 实现控制算法函数、算法测试四个部分,详细分类请参加《PⅠD控制算法的C语 言实现三》中的讲解,这里直接给出代码了 #includestdio, h> #includestdlib. h> struct pid float Jetspeed /定义设定值 float Actualspeed /定义实际值 float err /定义偏差值 float err next /定义上一个偏差值 float err last /定义最上前的偏差值 float Kp, Ki, Kd /定义比例、积分、微分系数 pid; void pid inito i pid. Setspeed=0. 0 pid. Actual Speed=0. 0 pid. err=0. 0: pid err last=0. 0 pid. err next=0. 0 pid. kp=0. 2 pid.Ki=0.015; pid Kd=0. 2 float PID realize(float speed)t pid. Setspeed=speed pid err- pid. SetSpeed-pid Actual Speed float increment Speed-pid Kp*(pid. err-pid err next)+pid Ki*pid. errtpid Kd*(p d.err-2*pid err next+pid err last pid. Actual Speed+=increment Speed pid err last- pid err next: pid err next=pid. err return pid. ActualSpeed
(系统自动生成,下载前可以参看下载内容)

下载文件列表

相关说明

  • 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
  • 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度
  • 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
  • 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
  • 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
  • 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.
 输入关键字,在本站1000多万海量源码库中尽情搜索: