首页资源分类嵌入式处理器瑞萨单片机 > C语言实现PID

C语言实现PID

已有 445025个资源

下载专区

上传者其他资源

    文档信息举报收藏

    标    签:pid

    分    享:

    文档简介

    搜集到的程序,不是我写的。共享一下。

    文档预览

    PID控制算法的C语言实现一 PID算法原理    最近两天在考虑一般控制算法的C语言实现问题,发现网络上尚没有一套完整的比较体系的讲解。于是总结了几天,整理一套思路分享给大家。    在工业应用中PID及其衍生算法是应用最广泛的算法之一,是当之无愧的万能算法,如果能够熟练掌握PID算法的设计与实现过程,对于一般的研发人员来讲,应该是足够应对一般研发问题了,而难能可贵的是,在我所接触的控制算法当中,PID控制算法又是最简单,最能体现反馈思想的控制算法,可谓经典中的经典。经典的未必是复杂的,经典的东西常常是简单的,而且是最简单的,想想牛顿的力学三大定律吧,想想爱因斯坦的质能方程吧,何等的简单!简单的不是原始的,简单的也不是落后的,简单到了美的程度。先看看PID算法的一般形式:    PID的流程简单到了不能再简单的程度,通过误差信号控制被控量,而控制器本身就是比例、积分、微分三个环节的加和。这里我们规定(在t时刻):    1.输入量为rin(t);    2.输出量为rout(t);    3.偏差量为err(t)=rin(t)-rout(t);    pid的控制规律为    理解一下这个公式,主要从下面几个问题着手,为了便于理解,把控制环境具体一下:    1.规定这个流程是用来为直流电机调速的;    2.输入量rin(t)为电机转速预定值;    3.输出量rout(t)为电机转速实际值;    4.执行器为直流电机;    5.传感器为光电码盘,假设码盘为10线;    6.直流电机采用PWM调速 转速用单位 转/min 表示;   不难看出以下结论:    1.输入量rin(t)为电机转速预定值(转/min);    2. 输出量rout(t)为电机转速实际值(转/min);    3.偏差量为预定值和实际值之差(转/min);    那么以下几个问题需要弄清楚:    1.通过PID环节之后的U(t)是什么值呢?    2.控制执行器(直流电机)转动转速应该为电压值(也就是PWM占空比)。    3.那么U(t)与PWM之间存在怎样的联系呢? http://blog.21ic.com/user1/3407/archives/2006/33541.html(见附录1)这篇文章上给出了一种方法,即,每个电压对应一个转速,电压和转速之间呈现线性关系。但是我考虑这种方法的前提是把直流电机的特性理解为线性了,而实际情况下,直流电机的特性绝对不是线性的,或者说在局部上是趋于线性的,这就是为什么说PID调速有个范围的问题。具体看一下http://articles.e-works.net.cn/component/article90249.htm(见附录2)这篇文章就可以了解了。所以在正式进行调速设计之前,需要现有开环系统,测试电机和转速之间的特性曲线(或者查阅电机的资料说明),然后再进行闭环参数整定。这篇先写到这,下一篇说明连续系统的离散化问题。并根据离散化后的特点讲述位置型PID和增量型PID的用法和C语言实现过程。 PID控制算法的C语言实现二 PID算法的离散化    上一节中,我论述了PID算法的基本形式,并对其控制过程的实现有了一个简要的说明,通过上一节的总结,基本已经可以明白PID控制的过程。这一节中先继续上一节内容补充说明一下。    1.说明一下反馈控制的原理,通过上一节的框图不难看出,PID控制其实是对偏差的控制过程;    2.如果偏差为0,则比例环节不起作用,只有存在偏差时,比例环节才起作用。    3.积分环节主要是用来消除静差,所谓静差,就是系统稳定后输出值和设定值之间的差值,积分环节实际上就是偏差累计的过程,把累计的误差加到原有系统上以抵消系统造成的静差。    4.而微分信号则反应了偏差信号的变化规律,或者说是变化趋势,根据偏差信号的变化趋势来进行超前调节,从而增加了系统的快速性。    好了,关于PID的基本说明就补充到这里,下面将对PID连续系统离散化,从而方便在处理器上实现。下面把连续状态的公式再贴一下:    假设采样间隔为T,则在第K T时刻: 偏差err(K)=rin(K)-rout(K); 积分环节用加和的形式表示,即err(K)+err(K+1)+……; 微分环节用斜率的形式表示,即[err(K)-err(K-1)]/T; 从而形成如下PID离散表示形式: 则u(K)可表示成为: 至于说Kp、Ki、Kd三个参数的具体表达式,我想可以轻松的推出了,这里节省时间,不再详细表示了。 其实到这里为止,PID的基本离散表示形式已经出来了。目前的这种表述形式属于位置型PID,另外一种表述方式为增量式PID,由U上述表达式可以轻易得到: 那么: 这就是离散化PID的增量式表示方式,由公式可以看出,增量式的表达结果和最近三次的偏差有关,这样就大大提高了系统的稳定性。需要注意的是最终的输出结果应该为        u(K)+增量调节值; PID的离散化过程基本思路就是这样,下面是将离散化的公式转换成为C语言,从而实现微控制器的控制作用。 PID控制算法的C语言实现三 位置型PID的C语言实现    上一节中已经抽象出了位置性PID和增量型PID的数学表达式,这一节,重点讲解C语言代码的实现过程,算法的C语言实现过程具有一般性,通过PID算法的C语言实现,可以以此类推,设计其它算法的C语言实现。    第一步:定义PID变量结构体,代码如下: struct _pid{     float SetSpeed;            //定义设定值     float ActualSpeed;        //定义实际值     float err;                //定义偏差值     float err_last;            //定义上一个偏差值     float Kp,Ki,Kd;            //定义比例、积分、微分系数     float voltage;          //定义电压值(控制执行器的变量)     float integral;            //定义积分值 }pid; 控制算法中所需要用到的参数在一个结构体中统一定义,方便后面的使用。   第二部:初始化变量,代码如下: void PID_init(){     printf("PID_init begin \n");     pid.SetSpeed=0.0;     pid.ActualSpeed=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 speed){     pid.SetSpeed=speed;     pid.err=pid.SetSpeed-pid.ActualSpeed;     pid.integral+=pid.err;     pid.voltage=pid.Kp*pid.err+pid.Ki*pid.integral+pid.Kd*(pid.err-pid.err_last);     pid.err_last=pid.err;     pid.ActualSpeed=pid.voltage*1.0;     return pid.ActualSpeed; } 注意:这里用了最基本的算法实现形式,没有考虑死区问题,没有设定上下限,只是对公式的一种直接的实现,后面的介绍当中还会逐渐的对此改进。    到此为止,PID的基本实现部分就初步完成了。下面是测试代码: int main(){     printf("System begin \n");     PID_init();     int count=0;     while(count<1000)     {         float speed=PID_realize(200.0);         printf("%f\n",speed);         count++;     } return 0; } 下面是经过1000次的调节后输出的1000个数据(具体的参数整定过程就不说明了,网上这种说明非常多): 83.000001 11.555000 59.559675 28.175408 52.907421 38.944152 51.891699 46.141651 53.339054 51.509998 55.908450 55.944631 58.970680 59.882936 62.225001 63.537254 65.527707 67.011058 68.810646 70.355318 72.042040 73.595658 75.207620 76.745444 78.301526 79.812136 81.321929 82.800304 84.268909 85.713108 87.143455 88.553005 89.946960 91.322078 92.680996 94.022234 95.347186 96.655242 97.947180 99.222808 100.482601 101.726572 102.955049 104.168125 105.366066 106.549019 107.717187 108.870756 110.009898 111.134811 112.245652 113.342615 114.425860 115.495564 116.551897 117.595029 118.625116 119.642331 120.646826 121.638767 122.618307 123.585603 124.540813 125.484079 126.415549 127.335383 128.243715 129.140691 130.026459 130.901149 131.764909 132.617870 133.460162 134.291942 135.113308 135.924419 136.725382 137.516332 138.297401 139.068697 139.830352 140.582499 141.325237 142.058701 142.782985 143.498218 144.204509 144.901969 145.590726 146.270843 146.942486 147.605718 148.260674 148.907425 149.546109 150.176794 150.799612 151.414626 152.021959 152.621696 153.213951 153.798781 154.376315 154.946626 155.509812 156.065958 156.615146 157.157471 157.693012 158.221871 158.744097 159.259826 159.769078 160.271991 160.768588 161.258996 161.743264 162.221494 162.693737 163.160075 163.620593 164.075347 164.524422 164.967877 165.405795 165.838235 166.265257 166.686967 167.103377 167.514610 167.920681 168.321682 168.717670 169.108719 169.494862 169.876198 170.252740 170.624605 170.991799 171.354406 171.712487 172.066080 172.415265 172.760077 173.100594 173.436838 173.768895 174.096796 174.420594 174.740352 175.056096 175.367915 175.675818 175.979886 176.280136 176.576656 176.869444 177.158600 177.444121 177.726087 178.004510 178.279458 178.550967 178.819094 179.083860 179.345315 179.603504 179.858466 180.110241 180.358866 180.604388 180.846849 181.086262 181.322699 181.556172 181.786733 182.014396 182.239222 182.461226 182.680475 182.896971 183.110768 183.321881 183.530369 183.736239 183.939545 184.140301 184.338555 184.534321 184.727651 184.918558 185.107080 185.293243 185.477080 185.658625 185.837886 186.014930 186.189745 186.362382 186.532859 186.701207 186.867437 187.031605 187.193713 187.353802 187.511884 187.667997 187.822151 187.974384 188.124700 188.273148 188.419728 188.564488 188.707429 188.848592 188.987995 189.125644 189.261576 189.395801 189.528364 189.659258 189.788528 189.916170 190.042233 190.166702 190.289633 190.411007 190.530867 190.649236 190.766119 190.881544 190.995531 191.108087 191.219243 191.329005 191.437382 191.544428 191.650111 191.754504 191.857565 191.959350 192.059857 192.159119 192.257135 192.353919 192.449511 192.543890 192.637105 192.729137 192.820032 192.909776 192.998410 193.085920 193.172360 193.257700 193.341993 193.425214 193.507408 193.588568 193.668715 193.747847 193.826004 193.903175 193.979391 194.054643 194.128963 194.202349 194.274828 194.346393 194.417073 194.486854 194.555777 194.623820 194.691027 194.757390 194.822919 194.887626 194.951536 195.014633 195.076965 195.138496 195.199273 195.259270 195.318547 195.377060 195.434856 195.491918 195.548283 195.603919 195.658886 195.713145 195.766734 195.819654 195.871912 195.923517 195.974472 196.024791 196.074478 196.123558 196.172016 196.219859 196.267115 196.313778 196.359851 196.405363 196.450296 196.494672 196.538492 196.581753 196.624494 196.666678 196.708363 196.749493 196.790138 196.830267 196.869889 196.909019 196.947656 196.985803 197.023493 197.060701 197.097449 197.133733 197.169558 197.204940 197.239872 197.274378 197.308436 197.342089 197.375309 197.408125 197.440523 197.472520 197.504114 197.535309 197.566127 197.596546 197.626594 197.656258 197.685546 197.714486 197.743047 197.771265 197.799113 197.826629 197.853799 197.880631 197.907131 197.933284 197.959122 197.984629 198.009823 198.034705 198.059275 198.083520 198.107481 198.131129 198.154493 198.177566 198.200349 198.222843 198.245062 198.267001 198.288662 198.310059 198.331178 198.352049 198.372645 198.392982 198.413066 198.432911 198.452499 198.471846 198.490953 198.509819 198.528439 198.546842 198.565003 198.582945 198.600648 198.618147 198.635415 198.652474 198.669313 198.685955 198.702378 198.718611 198.734625 198.750448 198.766067 198.781497 198.796736 198.811776 198.826628 198.841303 198.855788 198.870087 198.884218 198.898162 198.911943 198.925538 198.938970 198.952229 198.965320 198.978257 198.991033 199.003643 199.016092 199.028390 199.040542 199.052536 199.064373 199.076067 199.087617 199.099019 199.110280 199.121407 199.132381 199.143240 199.153940 199.164511 199.174957 199.185270 199.195457 199.205514 199.215440 199.225262 199.234930 199.244503 199.253928 199.263275 199.272468 199.281571 199.290541 199.299421 199.308165 199.316815 199.325345 199.333789 199.342115 199.350336 199.358462 199.366479 199.374396 199.382228 199.389943 199.397586 199.405110 199.412555 199.419891 199.427152 199.434307 199.441389 199.448363 199.455264 199.462073 199.468802 199.475442 199.481995 199.488475 199.494857 199.501183 199.507404 199.513578 199.519639 199.525656 199.531579 199.537437 199.543230 199.548936 199.554583 199.560149 199.565647 199.571073 199.576436 199.581730 199.586961 199.592118 199.597220 199.602260 199.607218 199.612132 199.616974 199.621764 199.626486 199.631156 199.635757 199.640316 199.644808 199.649249 199.653636 199.657959 199.662246 199.666457 199.670635 199.674752 199.678815 199.682833 199.686798 199.690715 199.694583 199.698409 199.702177 199.705905 199.709582 199.713209 199.716788 199.720339 199.723826 199.727276 199.730690 199.734054 199.737378 199.740657 199.743901 199.747111 199.750260 199.753393 199.756474 199.759526 199.762524 199.765490 199.768422 199.771314 199.774169 199.776992 199.779775 199.782527 199.785247 199.787938 199.790590 199.793204 199.795787 199.798338 199.800860 199.803343 199.805802 199.808225 199.810624 199.812986 199.815326 199.817642 199.819915 199.822175 199.824388 199.826587 199.828755 199.830902 199.833006 199.835097 199.837155 199.839194 199.841210 199.843191 199.845168 199.847096 199.849024 199.850905 199.852784 199.854621 199.856449 199.858238 199.860016 199.861757 199.863486 199.865199 199.866879 199.868549 199.870186 199.871813 199.873419 199.874997 199.876563 199.878109 199.879620 199.881136 199.882613 199.884088 199.885527 199.886971 199.888371 199.889783 199.891142 199.892518 199.893845 199.895180 199.896485 199.897783 199.899057 199.900322 199.901562 199.902797 199.904010 199.905222 199.906392 199.907576 199.908720 199.909875 199.910985 199.912108 199.913193 199.914287 199.915352 199.916423 199.917459 199.918505 199.919527 199.920526 199.921513 199.922496 199.923452 199.924415 199.925348 199.926275 199.927198 199.928108 199.929019 199.929903 199.930788 199.931653 199.932509 199.933353 199.934187 199.935002 199.935816 199.936617 199.937420 199.938195 199.938971 199.939733 199.940477 199.941228 199.941961 199.942685 199.943392 199.944111 199.944804 199.945491 199.946181 199.946854 199.947518 199.948165 199.948824 199.949456 199.950083 199.950714 199.951326 199.951930 199.952532 199.953125 199.953714 199.954290 199.954863 199.955424 199.955979 199.956538 199.957073 199.957623 199.958146 199.958671 199.959189 199.959693 199.960203 199.960689 199.961191 199.961665 199.962156 199.962619 199.963098 199.963543 199.964014 199.964448 199.964907 199.965330 199.965772 199.966201 199.966625 199.967046 199.967458 199.967868 199.968263 199.968664 199.969047 199.969437 199.969817 199.970193 199.970565 199.970943 199.971297 199.971668 199.972011 199.972363 199.972712 199.973047 199.973388 199.973726 199.974049 199.974379 199.974699 199.975014 199.975326 199.975645 199.975939 199.976249 199.976546 199.976832 199.977125 199.977414 199.977688 199.977969 199.978247 199.978525 199.978782 199.979061 199.979312 199.979576 199.979825 199.980077 199.980335 199.980569 199.980812 199.981053 199.981300 199.981522 199.981755 199.981984 199.982213 199.982427 199.982648 199.982860 199.983080 199.983298 199.983501 199.983704 199.983914 199.984114 199.984309 199.984500 199.984698 199.984887 199.985079 199.985262 199.985442 199.985623 199.985803 199.985984 199.986170 199.986327 199.986508 199.986668 199.986846 199.987006 199.987169 199.987321 199.987481 199.987633 199.987800 199.987948 199.988094 199.988237 199.988386 199.988526 199.988675 199.988815 199.988965 199.989090 199.989231 199.989359 199.989491 199.989629 199.989757 199.989889 199.990012 199.990133 199.990253 199.990373 199.990493 199.990614 199.990734 199.990854 199.990960 199.991072 199.991180 199.991289 199.991398 199.991507 199.991616 199.991718 199.991837 199.991922 199.992025 199.992123 199.992214 199.992314 199.992412 199.992503 199.992604 199.992701 199.992792 199.992878 199.992967 199.993047 199.993136 199.993216 199.993305 199.993385 199.993474 199.993554 199.993637 199.993726 199.993806 199.993881 199.993952 199.994024 199.994101 199.994170 199.994241 199.994313 199.994391 199.994459 199.994531 199.994602 199.994680 199.994748 199.994805 199.994868 199.994928 199.994989 199.995049 199.995109 199.995175 199.995226 199.995295 199.995346 199.995416 199.995466 199.995536 199.995593 199.995653 199.995713 199.995759 199.995811 199.995859 199.995902 199.995960 199.995999 199.996051 199.996100 199.996148 199.996191 199.996249 199.996288 199.996340 199.996389 199.996438 199.996480 199.996538 199.996578 199.996629 199.996678 199.996712 199.996746 199.996787 199.996824 199.996855 199.996896 199.996927 199.996967 199.997005 199.997036 199.997076 199.997113 199.997145 199.997185 199.997216 199.997256 199.997294 199.997325 199.997365 199.997403 199.997434 199.997474 199.997512 199.997543 199.997583 199.997614 199.997640 199.997669 199.997689 199.997711 199.997740 199.997760 199.997789 199.997809 199.997838 199.997858 199.997880 199.997909 199.997929 199.997958 199.997978 199.998007 199.998027 199.998049 199.998078 199.998098 199.998127 199.998147 199.998170 199.998199 199.998218 199.998247 199.998267 199.998296 199.998316 199.998339 199.998368 199.998387 199.998416 199.998436 199.998459 199.998488 199.998508 199.998537 199.998556 199.998585 199.998590 199.998605 199.998616 199.998634 199.998642 199.998654 199.998665 199.998676 199.998694 199.998702 199.998714 199.998725 199.998743 199.998745 199.998766 199.998774 199.998785 199.998803 199.998805 199.998826 199.998834 199.998845 199.998863 199.998871 199.998883 199.998894 199.998905 199.998923 199.998931 199.998943 199.998954 199.998972 199.998974 199.998995 199.999003 199.999014 199.999032 199.999034 199.999055 199.999063 199.999074 199.999092 199.999094 199.999115 199.999123 199.999135 199.999152 199.999161 199.999172 199.999183 199.999201 199.999203 199.999224 199.999232 199.999243 199.999261 199.999263 199.999284 199.999292 199.999304 199.999321 199.999323 199.999344 199.999352 199.999364 199.999381 199.999390 199.999401 199.999412 199.999430 199.999432 199.999453 199.999461 199.999473 PID控制算法的C语言实现四 增量型PID的C语言实现    上一节中介绍了最简单的位置型PID的实现手段,这一节主要讲解增量式PID的实现方法,位置型和增量型PID的数学公式请参见我的系列文《PID控制算法的C语言实现二》中的讲解。实现过程仍然是分为定义变量、初始化变量、实现控制算法函数、算法测试四个部分,详细分类请参加《PID控制算法的C语言实现三》中的讲解,这里直接给出代码了。 #include #include struct _pid{     float SetSpeed;            //定义设定值     float ActualSpeed;        //定义实际值     float err;                //定义偏差值     float err_next;            //定义上一个偏差值     float err_last;            //定义最上前的偏差值     float Kp,Ki,Kd;            //定义比例、积分、微分系数 }pid; void PID_init(){     pid.SetSpeed=0.0;     pid.ActualSpeed=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){     pid.SetSpeed=speed;     pid.err=pid.SetSpeed-pid.ActualSpeed;     float incrementSpeed=pid.Kp*(pid.err-pid.err_next)+pid.Ki*pid.err+pid.Kd*(pid.err-2*pid.err_next+pid.err_last);     pid.ActualSpeed+=incrementSpeed;     pid.err_last=pid.err_next;     pid.err_next=pid.err;     return pid.ActualSpeed; } int main(){     PID_init();     int count=0;     while(count<1000)     {         float speed=PID_realize(200.0);         printf("%f\n",speed);         count++;     }     return 0; } 运行后的1000个数据为: 83.000000 11.555000 59.559677 28.175406 52.907425 38.944149 51.891701 46.141655 53.339050 51.510002 55.908447 55.944637 58.970676 59.882942 62.224998 63.537247 65.527702 67.011047 68.810638 70.355309 72.042023 73.595642 75.207603 76.745430 78.301514 79.812126 81.321915 82.800293 84.268898 85.713097 87.143440 88.552994 89.946945 91.322067 92.680977 94.022224 95.347176 96.655235 97.947174 99.222801 100.482597 101.726562 102.955040 104.168114 105.366058 106.549004 107.717178 108.870743 110.009888 111.134796 112.245636 113.342598 114.425842 115.495552 116.551880 117.595009 118.625099 119.642311 120.646812 121.638756 122.618294 123.585594 124.540794 125.484062 126.415535 127.335365 128.243698 129.140671 130.026443 130.901138 131.764893 132.617859 133.460159 134.291931 135.113297 135.924408 136.725372 137.516327 138.297394 139.068695 139.830353 140.582489 141.325226 142.058685 142.782974 143.498199 144.204498 144.901962 145.590714 146.270844 146.942474 147.605713 148.260651 148.907410 149.546082 150.176773 150.799576 151.414597 152.021927 152.621674 153.213913 153.798752 154.376282 154.946594 155.509781 156.065918 156.615112 157.157440 157.692993 158.221848 158.744095 159.259811 159.769073 160.271973 160.768585 161.258987 161.743271 162.221481 162.693726 163.160065 163.620575 164.075333 164.524399 164.967865 165.405777 165.838226 166.265259 166.686951 167.103378 167.514587 167.920670 168.321671 168.717667 169.108704 169.494858 169.876175 170.252731 170.624588 170.991791 171.354401 171.712479 172.066086 172.415268 172.760086 173.100601 173.436844 173.768890 174.096786 174.420578 174.740326 175.056076 175.367889 175.675797 175.979858 176.280121 176.576630 176.869431 177.158569 177.444092 177.726044 178.004471 178.279419 178.550934 178.819046 179.083817 179.345276 179.603470 179.858429 180.110214 180.358841 180.604370 180.846817 181.086243 181.322662 181.556137 181.786682 182.014359 182.239182 182.461197 182.680435 182.896942 183.110733 183.321854 183.530334 183.736206 183.939514 184.140274 184.338531 184.534302 184.727631 184.918533 185.107056 185.293228 185.477066 185.658615 185.837891 186.014923 186.189743 186.362381 186.532852 186.701202 186.867432 187.031601 187.193710 187.353790 187.511871 187.667984 187.822128 187.974365 188.124680 188.273132 188.419724 188.564484 188.707428 188.848587 188.987976 189.125626 189.261566 189.395798 189.528351 189.659256 189.788513 189.916168 190.042221 190.166702 190.289627 190.411011 190.530884 190.649246 190.766144 190.881561 190.995544 191.108109 191.219254 191.329025 191.437408 191.544449 191.650146 191.754517 191.857590 191.959366 192.059875 192.159134 192.257141 192.353928 192.449509 192.543884 192.637085 192.729126 192.820007 192.909760 192.998383 193.085907 193.172333 193.257675 193.341965 193.425186 193.507385 193.588531 193.668686 193.747818 193.825974 193.903152 193.979370 194.054626 194.128952 194.202332 194.274811 194.346375 194.417053 194.486832 194.555756 194.623810 194.691010 194.757370 194.822906 194.887619 194.951523 195.014633 195.076950 195.138489 195.199265 195.259277 195.318542 195.377060 195.434845 195.491913 195.548264 195.603912 195.658859 195.713135 195.766724 195.819641 195.871902 195.923508 195.974472 196.024796 196.074493 196.123566 196.172028 196.219879 196.267136 196.313797 196.359879 196.405380 196.450317 196.494690 196.538513 196.581787 196.624512 196.666702 196.708374 196.749512 196.790146 196.830261 196.869888 196.909012 196.947647 196.985809 197.023483 197.060699 197.097443 197.133728 197.169556 197.204941 197.239883 197.274384 197.308456 197.342102 197.375320 197.408127 197.440521 197.472519 197.504105 197.535309 197.566116 197.596542 197.626587 197.656250 197.685547 197.714478 197.743042 197.771255 197.799118 197.826630 197.853806 197.880630 197.907120 197.933289 197.959122 197.984634 198.009827 198.034698 198.059265 198.083527 198.107483 198.131134 198.154495 198.177567 198.200348 198.222839 198.245056 198.266998 198.288666 198.310059 198.331177 198.352036 198.372635 198.392975 198.413071 198.432907 198.452499 198.471848 198.490952 198.509811 198.528442 198.546829 198.565002 198.582932 198.600647 198.618134 198.635406 198.652466 198.669312 198.685944 198.702377 198.718597 198.734619 198.750443 198.766068 198.781494 198.796722 198.811768 198.826614 198.841278 198.855759 198.870056 198.884186 198.898132 198.911911 198.925507 198.938934 198.952194 198.965286 198.978226 198.990997 199.003616 199.016068 199.028366 199.040512 199.052505 199.064346 199.076050 199.087601 199.099014 199.110275 199.121399 199.132385 199.143234 199.153946 199.164520 199.174973 199.185287 199.195465 199.205521 199.215454 199.225266 199.234955 199.244522 199.253967 199.263290 199.272507 199.281601 199.290588 199.299454 199.308212 199.316864 199.325409 199.333847 199.342178 199.350403 199.358521 199.366547 199.374466 199.382294 199.390015 199.397644 199.405167 199.412613 199.419952 199.427200 199.434357 199.441422 199.448410 199.455307 199.462112 199.468842 199.475479 199.482040 199.488510 199.494904 199.501221 199.507462 199.513611 199.519699 199.525696 199.531631 199.537476 199.543259 199.548965 199.554611 199.560181 199.565674 199.571106 199.576462 199.581757 199.586990 199.592148 199.597244 199.602280 199.607254 199.612167 199.617020 199.621811 199.626541 199.631210 199.635818 199.640366 199.644867 199.649307 199.653687 199.658020 199.662292 199.666519 199.670685 199.674805 199.678864 199.682877 199.686844 199.690750 199.694626 199.698441 199.702209 199.705933 199.709610 199.713242 199.716827 199.720367 199.723862 199.727310 199.730713 199.734085 199.737411 199.740692 199.743942 199.747147 199.750305 199.753433 199.756516 199.759567 199.762573 199.765549 199.768478 199.771378 199.774231 199.777054 199.779846 199.782593 199.785309 199.787994 199.790649 199.793259 199.795853 199.798401 199.800919 199.803406 199.805862 199.808289 199.810684 199.813049 199.815384 199.817688 199.819962 199.822220 199.824432 199.826630 199.828796 199.830933 199.833054 199.835144 199.837204 199.839233 199.841248 199.843231 199.845200 199.847137 199.849045 199.850937 199.852798 199.854645 199.856461 199.858261 199.860031 199.861786 199.863510 199.865219 199.866898 199.868561 199.870209 199.871826 199.873428 199.875015 199.876572 199.878113 199.879639 199.881149 199.882629 199.884094 199.885544 199.886978 199.888397 199.889786 199.891174 199.892532 199.893875 199.895203 199.896515 199.897812 199.899094 199.900360 199.901611 199.902847 199.904068 199.905273 199.906464 199.907639 199.908798 199.909943 199.911072 199.912186 199.913284 199.914368 199.915436 199.916489 199.917526 199.918564 199.919571 199.920578 199.921570 199.922546 199.923523 199.924469 199.925415 199.926346 199.927261 199.928177 199.929077 199.929962 199.930832 199.931702 199.932556 199.933395 199.934235 199.935059 199.935867 199.936676 199.937469 199.938248 199.939026 199.939789 199.940536 199.941284 199.942017 199.942749 199.943466 199.944168 199.944870 199.945557 199.946243 199.946915 199.947586 199.948242 199.948883 199.949524 199.950150 199.950775 199.951385 199.951996 199.952591 199.953186 199.953766 199.954346 199.954910 199.955475 199.956024 199.956573 199.957108 199.957642 199.958176 199.958694 199.959213 199.959717 199.960220 199.960724 199.961212 199.961700 199.962173 199.962646 199.963120 199.963577 199.964035 199.964478 199.964920 199.965363 199.965790 199.966217 199.966644 199.967056 199.967468 199.967880 199.968277 199.968674 199.969070 199.969452 199.969833 199.970215 199.970581 199.970947 199.971313 199.971664 199.972015 199.972366 199.972717 199.973053 199.973389 199.973724 199.974045 199.974380 199.974701 199.975021 199.975327 199.975632 199.975937 199.976242 199.976532 199.976822 199.977112 199.977402 199.977676 199.977966 199.978241 199.978516 199.978790 199.979050 199.979309 199.979568 199.979828 199.980072 199.980331 199.980576 199.980820 199.981064 199.981293 199.981537 199.981766 199.981995 199.982224 199.982437 199.982666 199.982880 199.983093 199.983307 199.983521 199.983719 199.983932 199.984131 199.984329 199.984528 199.984726 199.984909 199.985107 199.985291 199.985474 199.985657 199.985840 199.986023 199.986191 199.986374 199.986542 199.986710 199.986877 199.987045 199.987213 199.987366 199.987534 199.987686 199.987839 199.987991 199.988144 199.988297 199.988449 199.988586 199.988739 199.988876 199.989014 199.989151 199.989288 199.989426 199.989563 199.989685 199.989822 199.989944 199.990067 199.990189 199.990311 199.990433 199.990555 199.990677 199.990799 199.990906 199.991028 199.991135 199.991257 199.991364 199.991470 199.991577 199.991684 199.991791 199.991898 199.992004 199.992096 199.992203 199.992294 199.992401 199.992493 199.992584 199.992676 199.992767 199.992859 199.992950 199.993042 199.993134 199.993225 199.993301 199.993393 199.993469 199.993561 199.993637 199.993713 199.993790 199.993866 199.993942 199.994019 199.994095 199.994171 199.994247 199.994324 199.994400 199.994476 199.994537 199.994614 199.994675 199.994751 199.994812 199.994873 199.994934 199.994995 199.995056 199.995117 199.995178 199.995239 199.995300 199.995361 199.995422 199.995483 199.995544 199.995605 199.995667 199.995712 199.995773 199.995819 199.995880 199.995926 199.995987 199.996033 199.996094 199.996140 199.996185 199.996231 199.996277 199.996323 199.996368 199.996414 199.996460 199.996506 199.996552 199.996597 199.996643 199.996689 199.996735 199.996780 199.996826 199.996872 199.996902 199.996948 199.996979 199.997025 199.997055 199.997101 199.997131 199.997177 199.997208 199.997253 199.997284 199.997314 199.997345 199.997375 199.997406 199.997437 199.997467 199.997498 199.997528 199.997559 199.997589 199.997620 199.997650 199.997681 199.997711 199.997742 199.997772 199.997803 199.997833 199.997864 199.997894 199.997925 199.997955 199.997986 199.998016 199.998047 199.998077 199.998093 199.998123 199.998138 199.998169 199.998184 199.998215 199.998230 199.998260 199.998276 199.998306 199.998322 199.998352 199.998367 199.998398 199.998413 199.998444 199.998459 199.998489 199.998505 199.998520 199.998535 199.998550 199.998566 199.998581 199.998596 199.998611 199.998627 199.998642 199.998657 199.998672 199.998688 199.998703 199.998718 199.998734 199.998749 199.998764 199.998779 199.998795 199.998810 199.998825 199.998840 199.998856 199.998871 199.998886 199.998901 199.998917 199.998932 199.998947 199.998962 199.998978 199.998993 199.999008 199.999023 199.999039 199.999054 199.999069 199.999084 199.999100 199.999115 199.999130 199.999146 199.999161 199.999176 199.999191 199.999207 199.999222 199.999237 199.999252 199.999268 199.999283 199.999298 199.999298 199.999313 199.999313 199.999329 199.999329 199.999344 199.999344 199.999359 199.999359 199.999374 199.999374 199.999390 199.999390 199.999405 199.999405 199.999420 199.999420 199.999435 199.999435 199.999451 199.999451 199.999466 199.999466 199.999481 199.999481 199.999496 199.999496 199.999512 199.999512 PID控制算法的C语言实现五 积分分离的PID控制算法C语言实现     通过三、四两篇文章,基本上已经弄清楚了PID控制算法的最常规的表达方法。在普通PID控制中,引入积分环节的目的,主要是为了消除静差,提高控制精度。但是在启动、结束或大幅度增减设定时,短时间内系统输出有很大的偏差,会造成PID运算的积分积累,导致控制量超过执行机构可能允许的最大动作范围对应极限控制量,从而引起较大的超调,甚至是震荡,这是绝对不允许的。    为了克服这一问题,引入了积分分离的概念,其基本思路是 当被控量与设定值偏差较大时,取消积分作用; 当被控量接近给定值时,引入积分控制,以消除静差,提高精度。其具体实现代码如下:     pid.Kp=0.2;     pid.Ki=0.04;     pid.Kd=0.2;  //初始化过程   if(abs(pid.err)>200)     {     index=0;     }else{     index=1;     pid.integral+=pid.err;     }     pid.voltage=pid.Kp*pid.err+index*pid.Ki*pid.integral+pid.Kd*(pid.err-pid.err_last);    //算法具体实现过程   其它部分的代码参见《PID控制算法的C语言实现三》中的讲解,不再赘述。同样采集1000个量,会发现,系统到199所有的时间是原来时间的1/2,系统的快速性得到了提高。 199.003571 199.036804 199.068924 199.099960 199.129974 199.158981 199.187012 199.214111 199.240311 199.265640 199.290115 199.313797 199.336655 199.358795 199.380157 199.400818 199.420792 199.440109 199.458771 199.476807 199.494263 199.511124 199.527420 199.543182 199.558426 199.573135 199.587372 199.601120 199.614426 199.627289 199.639694 199.651718 199.663315 199.674561 199.685410 199.695908 199.706039 199.715851 199.725311 199.734482 199.743332 199.751907 199.760162 199.768173 199.775894 199.783371 199.790588 199.797577 199.804337 199.810867 199.817154 199.823257 199.829147 199.834839 199.840347 199.845673 199.850815 199.855789 199.860596 199.865234 199.869736 199.874069 199.878281 199.882324 199.886261 199.890045 199.893707 199.897263 199.900665 199.903992 199.907181 199.910278 199.913284 199.916168 199.918976 199.921677 199.924286 199.926804 199.929245 199.931610 199.933884 199.936081 199.938217 199.940277 199.942276 199.944183 199.946045 199.947830 199.949585 199.951248 199.952896 199.954437 199.955963 199.957428 199.958847 199.960205 199.961548 199.962830 199.964066 199.965271 199.966431 199.967545 199.968628 199.969666 199.970673 199.971649 199.972595 199.973511 199.974380 199.975235 199.976074 199.976852 199.977631 199.978378 199.979095 199.979797 199.980453 199.981125 199.981735 199.982361 199.982925 199.983505 199.984055 199.984604 199.985107 199.985611 199.986069 199.986557 199.987000 199.987442 199.987869 199.988281 199.988663 199.989044 199.989395 199.989761 199.990097 199.990417 199.990753 199.991058 199.991348 199.991653 199.991913 199.992203 199.992447 199.992706 199.992950 199.993179 199.993408 199.993607 199.993835 199.994034 199.994232 199.994431 199.994598 199.994797 199.994965 199.995132 199.995285 199.995453 199.995605 199.995743 199.995895 199.996017 199.996155 199.996277 199.996414 199.996521 199.996643 199.996750 199.996872 199.996964 199.997070 199.997162 199.997269 199.997360 199.997437 199.997528 199.997604 199.997681 199.997772 199.997849 199.997910 199.997986 199.998047 199.998108 199.998169 199.998245 199.998306 199.998352 199.998398 199.998459 199.998489 199.998550 199.998596 199.998642 199.998703 199.998734 199.998795 199.998825 199.998856 199.998886 199.998917 199.998962 199.998978 199.999023 199.999054 199.999084 199.999115 199.999146 199.999191 199.999207 199.999252 199.999283 199.999298 199.999313 199.999329 199.999344 199.999359 199.999374 199.999390 199.999405 199.999435 199.999435 199.999466 199.999466 199.999496 199.999512 199.999527 199.999542 199.999557 199.999573 199.999588 199.999603 199.999619 199.999634 199.999649 199.999680 199.999680 199.999710 199.999710 199.999741 199.999756 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 199.999771 PID控制算法的C语言实现六 抗积分饱和的PID控制算法C语言实现    所谓的积分饱和现象是指如果系统存在一个方向的偏差,PID控制器的输出由于积分作用的不断累加而加大,从而导致执行机构达到极限位置,若控制器输出U(k)继续增大,执行器开度不可能再增大,此时计算机输出控制量超出了正常运行范围而进入饱和区。一旦系统出现反向偏差,u(k)逐渐从饱和区退出。进入饱和区越深则退出饱和区时间越长。在这段时间里,执行机构仍然停留在极限位置而不随偏差反向而立即做出相应的改变,这时系统就像失控一样,造成控制性能恶化,这种现象称为积分饱和现象或积分失控现象。     防止积分饱和的方法之一就是抗积分饱和法,该方法的思路是在计算u(k)时,首先判断上一时刻的控制量u(k-1)是否已经超出了极限范围: 如果u(k-1)>umax,则只累加负偏差; 如果u(k-1)pid.umax)  //灰色底色表示抗积分饱和的实现     {        if(abs(pid.err)>200)      //蓝色标注为积分分离过程         {             index=0;         }else{             index=1;             if(pid.err<0)             {              pid.integral+=pid.err;             }         }     }else if(pid.ActualSpeed200)      //积分分离过程         {             index=0;         }else{             index=1;             if(pid.err>0)             {             pid.integral+=pid.err;             }         }     }else{ //未超出极限范围         if(abs(pid.err)>200)                    //积分分离过程         {             index=0;         }else{             index=1;             pid.integral+=pid.err;         }     }     pid.voltage=pid.Kp*pid.err+index*pid.Ki*pid.integral+pid.Kd*(pid.err-pid.err_last);     pid.err_last=pid.err;     pid.ActualSpeed=pid.voltage*1.0;     return pid.ActualSpeed; } 最终的测试程序运算结果如下,可以明显的看出系统的稳定时间相对前几次来讲缩短了不少。 100.000000 30.000000 95.000000 65.500000 103.750000 92.175003 115.237503 112.173752 126.794380 127.653938 137.468842 139.967911 146.934479 149.954224 155.144211 158.157745 162.174561 164.953079 168.149734 170.611786 173.205124 175.339691 177.470551 179.298065 181.063431 182.616440 184.086655 185.400513 186.628952 187.737457 188.766006 189.699692 190.561951 191.347580 192.071030 192.731674 193.338928 193.894257 194.404160 194.870834 195.299072 195.691193 196.050888 196.380341 196.682465 196.959244 197.213043 197.445572 197.658768 197.854111 198.033203 198.197311 198.347763 198.485626 198.612015 198.727829 198.834000 198.931290 199.020477 199.102219 199.177139 199.245804 199.308746 199.366425 199.419296 199.467758 199.512161 199.552872 199.590179 199.624390 199.655716 199.684464 199.710785 199.734924 199.757034 199.777298 199.795883 199.812912 199.828537 199.842834 199.855972 199.867981 199.879013 199.889099 199.898361 199.906845 199.914612 199.921753 199.928268 199.934280 199.939743 199.944794 199.949371 199.953629 199.957474 199.961029 199.964279 199.967270 199.969986 199.972504 199.974792 199.976898 199.978821 199.980591 199.982208 199.983688 199.985062 199.986298 199.987442 199.988495 199.989441 199.990326 199.991135 199.991867 199.992554 199.993179 199.993744 199.994263 199.994751 199.995178 199.995590 199.995941 199.996292 199.996582 199.996887 199.997116 199.997391 199.997574 199.997803 199.997971 199.998154 199.998291 199.998444 199.998581 199.998703 199.998810 199.998917 199.999008 199.999084 199.999176 199.999237 199.999298 199.999359 199.999405 199.999466 199.999496 199.999542 199.999588 199.999619 199.999649 199.999680 199.999710 199.999725 199.999756 199.999771 199.999786 199.999817 199.999817 199.999847 199.999847 199.999863 199.999863 199.999878 199.999893 199.999893 199.999908 199.999924 199.999924 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 199.999939 PID控制算法的C语言实现七 梯形积分的PID控制算法C语言实现    先看一下梯形算法的积分环节公式    作为PID控制律的积分项,其作用是消除余差,为了尽量减小余差,应提高积分项运算精度,为此可以将矩形积分改为梯形积分,具体实现的语句为: pid.voltage=pid.Kp*pid.err+index*pid.Ki*pid.integral/2+pid.Kd*(pid.err-pid.err_last);  //梯形积分 其它函数请参见本系列教程六中的介绍 最后运算的稳定数据为:199.999878,较教程六中的199.9999390而言,精度进一步提高。 PID控制算法的C语言实现八 变积分的PID控制算法C语言实现    变积分PID可以看成是积分分离的PID算法的更一般的形式。在普通的PID控制算法中,由于积分系数ki是常数,所以在整个控制过程中,积分增量是不变的。但是,系统对于积分项的要求是,系统偏差大时,积分作用应该减弱甚至是全无,而在偏差小时,则应该加强。积分系数取大了会产生超调,甚至积分饱和,取小了又不能短时间内消除静差。因此,根据系统的偏差大小改变积分速度是有必要的。    变积分PID的基本思想是设法改变积分项的累加速度,使其与偏差大小相对应:偏差越大,积分越慢; 偏差越小,积分越快。    这里给积分系数前加上一个比例值index:    当abs(err)<180时,index=1;    当180200时,index=0;    最终的比例环节的比例系数值为ki*index;    具体PID实现代码如下:     pid.Kp=0.4;     pid.Ki=0.2;    //增加了积分系数     pid.Kd=0.2;      float PID_realize(float speed){     float index;     pid.SetSpeed=speed;     pid.err=pid.SetSpeed-pid.ActualSpeed;     if(abs(pid.err)>200)           //变积分过程     {     index=0.0;     }else if(abs(pid.err)<180){     index=1.0;     pid.integral+=pid.err;     }else{     index=(200-abs(pid.err))/20;     pid.integral+=pid.err;     }     pid.voltage=pid.Kp*pid.err+index*pid.Ki*pid.integral+pid.Kd*(pid.err-pid.err_last);     pid.err_last=pid.err;     pid.ActualSpeed=pid.voltage*1.0;     return pid.ActualSpeed; } 最终结果可以看出,系统的稳定速度非常快(测试程序参见本系列教程3): 120.000000 64.000000 148.800003 96.959999 165.632004 120.934395 177.300476 139.081223 185.469742 152.898834 191.139313 163.452988 195.022278 171.538986 197.635025 177.753738 199.350967 182.546188 200.439255 186.254608 201.093094 189.134460 201.450439 191.379044 201.609268 193.135010 201.638611 194.513870 201.586670 195.600708 201.486694 196.460571 201.361328 197.143387 201.225632 197.687561 201.089340 198.122787 200.958511 198.472076 200.836655 198.753296 200.725555 198.980423 200.625870 199.164398 200.537506 199.313843 200.459900 199.435547 200.392258 199.534912 200.333679 199.616211 200.283203 199.682877 200.239899 199.737640 200.202866 199.782700 200.171295 199.819855 200.144470 199.850525 200.121704 199.875870 200.102432 199.896851 200.086136 199.914230 200.072372 199.928635 200.060776 199.940582 200.051010 199.950500 200.042801 199.958755 200.035904 199.965622 200.030090 199.971344 200.025223 199.976105 200.021118 199.980057 200.017700 199.983353 200.014832 199.986099 200.012421 199.988403 200.010391 199.990326 200.008698 199.991928 200.007263 199.993256 200.006088 199.994370 200.005081 199.995300 200.004257 199.996063 200.003555 199.996719 200.002975 199.997253 200.002487 199.997711 200.002075 199.998077 200.001740 199.998398 200.001465 199.998657 200.001221 199.998886 200.001007 199.999084 200.000839 199.999237 200.000702 199.999359 200.000580 199.999451 200.000488 199.999542 200.000397 199.999619 200.000336 199.999680 200.000275 199.999725 200.000229 199.999756 200.000198 199.999802 200.000168 199.999832 200.000137 199.999863 200.000107 199.999893 200.000092 199.999908 200.000076 199.999924 200.000061 199.999939 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 199.999954 200.000046 PID控制算法的C语言实现九 专家PID与模糊PID的C语言实现    本节是PID控制算法的C语言实现系列的最后一节,前面8节中,已经分别从PID的实现到深入的过程进行了一个简要的讲解,从前面的讲解中不难看出,PID的控制思想非常简单,其主要问题点和难点在于比例、积分、微分环节上的参数整定过程,对于执行器控制模型确定或者控制模型简单的系统而言,参数的整定可以通过计算获得,对于一般精度要求不是很高的执行器系统,可以采用拼凑的方法进行实验型的整定。    然而,在实际的控制系统中,线性系统毕竟是少数,大部分的系统属于非线性系统,或者说是系统模型不确定的系统,如果控制精度要求较高的话,那么对于参数的整定过程是有难度的。专家PID和模糊PID就是为满足这方面的需求而设计的。专家算法和模糊算法都归属于智能算法的范畴,智能算法最大的优点就是在控制模型未知的情况下,可以对模型进行控制。这里需要注意的是,专家PID也好,模糊PID也罢,绝对不是专家系统或模糊算法与PID控制算法的简单加和,他是专家系统或者模糊算法在PID控制器参数整定上的应用。也就是说,智能算法是辅助PID进行参数整定的手段。    其实在前面几节的讲述中,已经用到了专家PID的一些特例行为了,从第五节到第八节都是专家系统一些特列化的算法,对某些条件进行了局部的判定,比如如果偏差太大的话,就去除积分项,这本身就是含有经验的专家系统。    专家系统、模糊算法,需要参数整定就一定要有整定的依据,也就是说什么情况下整定什么值是要有依据的,这个依据是一些逻辑的组合,只要找出其中的逻辑组合关系来,这些依据就再明显不过了。下面先说一下专家PID的C语言实现。正如前面所说,需要找到一些依据,还得从PID系数本身说起。    1.比例系数Kp的作用是加快系统的响应速度,提高系统的调节精度。Kp越大,系统的响应速度越快,系统的调节精度越高,但是容易产生超调,甚至会使系统不稳定。Kp取值过小,则会降低调节精度,使响应速度缓慢,从而延长调节时间,是系统静态、动态特性变差;    2.积分作用系数Ki的作用是消除系统的稳态误差。Ki越大,系统的静态误差消除的越快,但是Ki过大,在响应过程的初期会产生积分饱和的现象,从而引起响应过程的较大超调。若Ki过小,将使系统静态误差难以消除,影响系统的调节精度;    3.微分系数Kd的作用是改善系统的动态特性,其作用主要是在响应过程中抑制偏差向任何方向的变化,对偏差变化进行提前预报。但是kd过大,会使响应过程提前制动,从而延长调节时间,而且会降低系统的抗干扰性。    反应系统性能的两个参数是系统误差e和误差变化律ec,这点还是好理解的:     首先我们规定一个误差的极限值,假设为Mmax;规定一个误差的比较大的值,假设为Mmid;规定一个误差的较小值,假设为Mmin;    当abs(e)>Mmax时,说明误差的绝对值已经很大了,不论误差变化趋势如何,都应该考虑控制器的输入应按最大(或最小)输出,以达到迅速调整误差的效果,使误差绝对值以最大的速度减小。此时,相当于实施开环控制。    当e*ec>0时,说明误差在朝向误差绝对值增大的方向变化,此时,如果abs(e)>Mmid,说明误差也较大,可考虑由控制器实施较强的控制作用,以达到扭转误差绝对值向减小的方向变化,并迅速减小误差的绝对值。此时如果abs(e)0或者e=0时,说明误差的绝对值向减小的方向变化,或者已经达到平衡状态,此时保持控制器输出不变即可。    当e*err<0且e*err(k-1)<0时,说明误差处于极限状态。如果此时误差的绝对值较大,大于Mmin,可以考虑实施较强控制作用。如果此时误差绝对值较小,可以考虑实施较弱控制作用。    当abs(e)999) { UKTemp = 999; } //这里只所以是999封顶而不是1024是因为我的系统PWM的峰值电压是12V导致。 while(1) //如果输出电压和期望电压相差 Delta,则继续调整占空比,直到在误差以内 { ADChPro(UPWMADCH); //测量输出电压 if( ADPool.Value_Uint16[UPWMADCH] == UKTemp) { return; } if( ADPool.Value_Uint16[UPWMADCH] > UKTemp) //如果当前电压大于输出电压,减小占空比 { if( ( ADPool.Value_Uint16[UPWMADCH] - UKTemp ) > UDELTA ) { temp = ADPool.Value_Uint16[UPWMADCH] - UKTemp; // temp = temp / 2; //下降可以加速下降,所以下降参数加倍 if( Pwm.DutyCycle_Uint8 > temp ) { Pwm.DutyCycle_Uint8 = Pwm.DutyCycle_Uint8 - temp; } else { Pwm.DutyCycle_Uint8 = 0; } } else { return; } } else //如果当前电压小于输出电压 { if( ( UKTemp - ADPool.Value_Uint16[UPWMADCH] ) > UDELTA ) { temp = UKTemp - ADPool.Value_Uint16[UPWMADCH]; temp = temp / 4; //上升处理不要超调,所以每次只+一半 if( (255-Pwm.DutyCycle_Uint8) > temp ) { Pwm.DutyCycle_Uint8 += (temp/2); } else { Pwm.DutyCycle_Uint8 = 255; } } else { return; } } DisPlayVoltage(); PWMChangeDuty(Pwm.DutyCycle_Uint8); //改变占空比 Delay(10,10); } } } /*****************************************************/ 附录2 直流电机PWM调速系统中控制电压非线性研究 引言     由于线性放大驱动方式效率和散热问题严重,目前绝大多数直流电动机采用开关驱动方式。开关驱动方式是半导体功率器件工作在开关状态,通过脉宽调制PWM控制电动机电枢电压,实现调速。目前已有许多文献介绍直流电机调速,宋卫国等用89C51单片机实现了直流电机闭环调速;张立勋等用AVR单片机实现了直流电机PWM调速;郭崇军等用C8051实现了无刷直流电机控制;张红娟等用PIC单片机实现了直流电机PWM调速;王晨阳等用DSP实现了无刷直流电机控制。上述文献对实现调速的硬件电路和软件流程的设计有较详细的描述,但没有说明具体的调压调速方法,也没有提及占空比与电机端电压平均值之间的关系。在李维军等基于单片机用软件实现直流电机PWM调速系统中提到平均速度与占空比并不是严格的线性关系,在一般的应用中,可以将其近似地看作线性关系。但没有做深入的研究。本文通过实验验证,在不带电机情况下,PWM波占空比与控制输出端电压平均值之间呈线性关系;在带电机情况下,占空比与电机端电压平均值满足抛物线方程,能取得精确的控制。本文的电机闭环调速是运用Matlab拟合的关系式通过PID控制算法实现。 1 系统硬件设计     本系统是基于TX-1C实验板上的AT89C52单片机,调速系统的硬件原理图如图1所示,主要由AT89C52单片机、555振荡电路、L298驱动电路、光电隔离、霍尔元件测速电路、MAX 232电平转换电路等组成。   图1 闭环控制系统示意图 2 系统软件设计     系统采用模块化设计,软件由1个主程序,3个中断子程序,即外部中断0、外部中断1,定时器0子程序,PID算法子程序,测速子程序及发送数据到串口显示子程序组成,主程序流程图如图2所示。外部中断0通过比较直流电平与锯齿波信号产生PWM波,外部中断1用于对传感器的脉冲计数。定时器0用于对计数脉冲定时。测得的转速通过串口发送到上位机显示,通过PID模块调整转速到设定值。本实验采用M/T法测速,它是同时测量检测时间和在此检测时间内霍尔传感器所产生的转速脉冲信号的个数来确定转速。由外部中断1对霍尔传感器脉冲计数,同时起动定时器0,当计数个数到预定值2 000后,关定时器0,可得到计2 000个脉冲的计数时间,由式计算出转速: n=60f/K=60N/(KT) (1)     式中:n为直流电机的转速;K为霍尔传感器转盘上磁钢数;f为脉冲频率;N为脉冲个数;T为采样周期。   图2 主程序流程图 3 实验结果及原因分析     3.1 端电压平均值与转速关系     3.1.1 实验结果     实验用的是永磁稳速直流电机,型号是EG-530YD-2BH,额定转速2 000~4 000 r/min,额定电压12 V。电机在空载的情况下,测得的数据用Matlab做一次线性拟合,拟合的端电压平均值与转速关系曲线如图3(a)所示。相关系数R-square:0.952 1。拟合曲线方程为: y=0.001 852x+0.296 3 (2)     由式(2)可知,端电压平均值与转速可近似为线性关系,根椐此关系式,在已测得的转速的情况下可以计算出当前电压。为了比较分析,同样用Matlab做二次线性拟合,拟合的端电压平均值与转速关系曲线如图3(b)所示。相关系数R-square:0.986 7。   图3 端电压平均值与转速关系曲线图 3.1.2 原因分析     比较图3(a)可知,当转速在0~1 500 r/min和4 000~5 000 r/min,端电压平均值与转速间存在的非线性,用二次曲拟合如图3(b)所示,拟合相关系数较高。由图3(a)可见,当电机转速为0时电机两端电压平均值约为1.3 V。这是因为电机处于静止状态时,摩擦力为静摩擦力,静摩擦力是非线性的。随着外力的增加而增加,最大值发生在运动前的瞬间。电磁转矩为负载制动转矩和空载制动转矩之和,由于本系统不带负载,因此电磁转矩为空载制动转矩。空载制动转矩与转速之间此时是非线性的。电磁转矩与电流成正比,电流又与电压成正比,因此此时电压与转速之间是非线性的。     当转速在2 000~4 000 r/min线性关系较好,占空比的微小改变带来的转速改变较大,因此具有较好的调速性能。这是因为随着运动速度的增加,摩擦力成线性的增加,此时的摩擦力为粘性摩擦力。粘性摩擦是线性的,与速度成正比,空载制动转矩与速度成正比,也即电磁转矩与电流成正比,电流又与电压成正比,因此此时电压与转速之间是线性的。当转速大于4 000 r/min。由于超出了额定转速所以线性度较差且调速性能较差。此时用二次曲线拟合结果较好,因为当电机高速旋转时,摩擦阻力小到可以忽略,此时主要受电机风阻型负荷的影响,当运动部件在气体或液体中运动时,其受到的摩擦阻力或摩擦阻力矩被称为风机型负荷。对同一物体,风阻系数一般为固定值。阻力大小与速度的平方成正比。即空载制动转矩与速度的平方成正比,也即电磁转矩与速度的平方成正比,电磁转矩与电流成正比,电流又与电压成正比,因此此时电压与转速之间是非线性的。  3.2 占空比与端电压平均值关系     3.2.1 实验结果     拟合占空比与端电压平均值关系曲线如图4所示。相关系数R-square:0.998 4。拟合曲线方程为:   图4 占空比与端电压平均值关系曲线图     如图4所示,占空比与端电压平均值满足抛物线方程。运用积分分离的PID算法改变电机端电压平均值,可以运用此关系式改变占空比,从而实现了PWM调速。     用示波器分别测出电压的顶端值Utop与底端值Ubase,端电压平均值Uarg满足关系式:     其中:α为占空比。     正是由于所测得的电机端电压底端值Ubase不为0,所以得出的占空比与端电压平均值之间关系曲线为抛物线。若将电机取下,直接测L298的out1与out2输出电压。所测得的电机端电压底端值Ubase约为0,所得的占空比与端电压平均值满足线性关系,即令式(4)中Ubase为0,式(4)变为:       3.2.2 原因分析     将电机取下后,直接测L298的输出端之间的电压,占空比与端电压平均值满足关系式(5),说明整个硬件电路的设计以及软件编程的正确性。从电机反电势角度分析,当直流电机旋转时,电枢导体切割气隙磁场,在电枢绕组中产生感应电动势。由于感应电动势方向与电流的方向相反,感应电动势也即反电势。直流电机的等效模型如图5所示。图5(a)表示电机工作在电动机状态。图5(b)表示电机工作在发电机状态。   图5 直流电机等效电路     如图5(a)所示,电压平衡方程为: 式中:U为外加电压;Ia为电枢电流;Ra为电枢绕组电阻;2△Ub为一对电刷接触压降,一般取2△Ub为0.5~2 V;Ea为电枢绕组内的感应电动势。电机空载时,电枢电流可忽略不计,即电流Ia为0。空载时的磁场由主磁极的励磁磁动势单独作用产生。给电机外加12 V的额定电压,由(6)可得反电势:       以40%的占空比为例,电机端电压Uab是测量中的电压平均值Uarg,其值为8.34 V,测量中的电压底端值Ubase约为7 V。由式(7)可得Ea的值范围应在6.34~7.84 V。由图5(b)可见,此时Uab的值是测得的底端值Ubase即电机的电动势Ea为7 V。     当PWM工作在低电平状态,直流电机不会立刻停止,会继续旋转,电枢绕组切割气隙磁场,电机此时工作在发电机状态,产生感应电动势E。       式中:Ce为电机电动势常数;φ为每级磁通量。由于电机空载,所以图5(b)中无法形成回路。用单片机仿真软件Proteus可直观的看出在PWM为低电平状态,电机处于减速状态。低电平持续时间越长,电机减速量越大。正是由于在低电平期间,电机处于减速状态,由式(8)可知,Ce,φ均为不变量,转速n的变化引起E的改变。此时Uab的值等于E的值。电机在低电平期间不断的减速,由于PWM周期较短,本文中取20 ms,电机在低电平期间转速还未减至0,PWM又变为高电平了。这样,就使测得的Ubase值不为0。以40%的占空比为例,当PWM工作在低电平状态,测得Ubase的值约为7 V。由式(8)可知,当正占空比越大,转速也就越大,同时减速时间越短,感应电势E的值越大。所以Ubase的值也就越大。 4 结语     重点分析了直流电机PWM调速过程中控制电压的非线性,对非线性的影响因素做了详细的分析。由于PWM在低电平期间电压的底端值不为0,导致了占空比与电机端电压平均值之间呈抛物线关系。因此,可用得出的抛物线关系式实现精确调速。本系统的非线性研究可为电机控制中非线性的进一步研究提供依据,在实际运用中,可用于移动机器人、飞行模拟机的精确控制。 附录3 隶属函数(membership function),用于表征模糊集合的数学工具。对于普通集合A,它可以理解为某个论域U上的一个子集。为了描述论域U中任一元素u是否属于集合A,通常可以用0或1标志。用0表示u不属于A,而用1表示属于A ,从而得到了U上的一个二值函数χA(u),它表征了U的元素u对普通集合的从属关系,通常称为A的特征函数,为了描述元素u对U上的一个模糊集合的隶属关系,由于这种关系的不分明性,它将用从区间[0,1]中所取的数值代替0,1这两值来描述,记为(u),数值(u)表示元素隶属于模糊集的程度,论域U上的函数μ即为模糊集的隶属函数,而(u)即为u对A的隶属度。

    Top_arrow
    回到顶部
    EEWORLD下载中心所有资源均来自网友分享,如有侵权,请发送举报邮件到客服邮箱bbs_service@eeworld.com.cn 或通过站内短信息或QQ:273568022联系管理员 高员外,我们会尽快处理。