欢迎来到淘文阁 - 分享文档赚钱的网站! | 帮助中心 好文档才是您的得力助手!
淘文阁 - 分享文档赚钱的网站
全部分类
  • 研究报告>
  • 管理文献>
  • 标准材料>
  • 技术资料>
  • 教育专区>
  • 应用文书>
  • 生活休闲>
  • 考试试题>
  • pptx模板>
  • 工商注册>
  • 期刊短文>
  • 图片设计>
  • ImageVerifierCode 换一换

    PID控制算法的C语言实现(完整版)(38页).doc

    • 资源ID:36362255       资源大小:1.08MB        全文页数:38页
    • 资源格式: DOC        下载积分:15金币
    快捷下载 游客一键下载
    会员登录下载
    微信登录下载
    三方登录下载: 微信开放平台登录   QQ登录  
    二维码
    微信扫一扫登录
    下载资源需要15金币
    邮箱/手机:
    温馨提示:
    快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如填写123,账号就是123,密码也是123。
    支付方式: 支付宝    微信支付   
    验证码:   换一换

     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    PID控制算法的C语言实现(完整版)(38页).doc

    -PID控制算法的C语言实现(完整版)-第 38 页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之间存在怎样的联系呢?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("%fn",speed);        count+;    return 0;下面是经过1000次的调节后输出的1000个数据(具体的参数整定过程就不说明了,网上这种说明非常多):83.00000111.55500059.55967528.17540852.90742138.94415251.89169946.14165153.33905451.50999855.90845055.94463158.97068059.88293662.22500163.53725465.52770767.01105868.81064670.35531872.04204073.59565875.20762076.74544478.30152679.81213681.32192982.80030484.26890985.71310887.14345588.55300589.94696091.32207892.68099694.02223495.34718696.65524297.94718099.222808100.482601101.726572102.955049104.168125105.366066106.549019107.717187108.870756110.009898111.134811112.245652113.342615114.425860115.495564116.551897117.595029118.625116119.642331120.646826121.638767122.618307123.585603124.540813125.484079126.415549127.335383128.243715129.140691130.026459130.901149131.764909132.617870133.460162134.291942135.113308135.924419136.725382137.516332138.297401139.068697139.830352140.582499141.325237142.058701142.782985143.498218144.204509144.901969145.590726146.270843146.942486147.605718148.260674148.907425149.546109150.176794150.799612151.414626152.021959152.621696153.213951153.798781154.376315154.946626155.509812156.065958156.615146157.157471157.693012158.221871158.744097159.259826159.769078160.271991160.768588161.258996161.743264162.221494162.693737163.160075163.620593164.075347164.524422164.967877165.405795165.838235166.265257166.686967167.103377167.514610167.920681168.321682168.717670169.108719169.494862169.876198170.252740170.624605170.991799171.354406171.712487172.066080172.415265172.760077173.100594173.436838173.768895174.096796174.420594174.740352175.056096175.367915175.675818175.979886176.280136176.576656176.869444177.158600177.444121177.726087178.004510178.279458178.550967178.819094179.083860179.345315179.603504179.858466180.110241180.358866180.604388180.846849181.086262181.322699181.556172181.786733182.014396182.239222182.461226182.680475182.896971183.110768183.321881183.530369183.736239183.939545184.140301184.338555184.534321184.727651184.918558185.107080185.293243185.477080185.658625185.837886186.014930186.189745186.362382186.532859186.701207186.867437187.031605187.193713187.353802187.511884187.667997187.822151187.974384188.124700188.273148188.419728188.564488188.707429188.848592188.987995189.125644189.261576189.395801189.528364189.659258189.788528189.916170190.042233190.166702190.289633190.411007190.530867190.649236190.766119190.881544190.995531191.108087191.219243191.329005191.437382191.544428191.650111191.754504191.857565191.959350192.059857192.159119192.257135192.353919192.449511192.543890192.637105192.729137192.820032192.909776192.998410193.085920193.172360193.257700193.341993193.425214193.507408193.588568193.668715193.747847193.826004193.903175193.979391194.054643194.128963194.202349194.274828194.346393194.417073194.486854194.555777194.623820194.691027194.757390194.822919194.887626194.951536195.014633195.076965195.138496195.199273195.259270195.318547195.377060195.434856195.491918195.548283195.603919195.658886195.713145195.766734195.819654195.871912195.923517195.974472196.024791196.074478196.123558196.172016196.219859196.267115196.313778196.359851196.405363196.450296196.494672196.538492196.581753196.624494196.666678196.708363196.749493196.790138196.830267196.869889196.909019196.947656196.985803197.023493197.060701197.097449197.133733197.169558197.204940197.239872197.274378197.308436197.342089197.375309197.408125197.440523197.472520197.504114197.535309197.566127197.596546197.626594197.656258197.685546197.714486197.743047197.771265197.799113197.826629197.853799197.880631197.907131197.933284197.959122197.984629198.009823198.034705198.059275198.083520198.107481198.131129198.154493198.177566198.200349198.222843198.245062198.267001198.288662198.310059198.331178198.352049198.372645198.392982198.413066198.432911198.452499198.471846198.490953198.509819198.528439198.546842198.565003198.582945198.600648198.618147198.635415198.652474198.669313198.685955198.702378198.718611198.734625198.750448198.766067198.781497198.796736198.811776198.826628198.841303198.855788198.870087198.884218198.898162198.911943198.925538198.938970198.952229198.965320198.978257198.991033199.003643199.016092199.028390199.040542199.052536199.064373199.076067199.087617199.099019199.110280199.121407199.132381199.143240199.153940199.164511199.174957199.185270199.195457199.205514199.215440199.225262199.234930199.244503199.253928199.263275199.272468199.281571199.290541199.299421199.308165199.316815199.325345199.333789199.342115199.350336199.358462199.366479199.374396199.382228199.389943199.397586199.405110199.412555199.419891199.427152199.434307199.441389199.448363199.455264199.462073199.468802199.475442199.481995199.488475199.494857199.501183199.507404199.513578199.519639199.525656199.531579199.537437199.543230199.548936199.554583199.560149199.565647199.571073199.576436199.581730199.586961199.592118199.597220199.602260199.607218199.612132199.616974199.621764199.626486199.631156199.635757199.640316199.644808199.649249199.653636199.657959199.662246199.666457199.670635199.674752199.678815199.682833199.686798199.690715199.694583199.698409199.702177199.705905199.709582199.713209199.716788199.720339199.723826199.727276199.730690199.734054199.737378199.740657199.743901199.747111199.750260199.753393199.756474199.759526199.762524199.765490199.768422199.771314199.774169199.776992199.779775199.782527199.785247199.787938199.790590199.793204199.795787199.798338199.800860199.803343199.805802199.808225199.810624199.812986199.815326199.817642199.819915199.822175199.824388199.826587199.828755199.830902199.833006199.835097199.837155199.839194199.841210199.843191199.845168199.847096199.849024199.850905199.852784199.854621199.856449199.858238199.860016199.861757199.863486199.865199199.866879199.868549199.870186199.871813199.873419199.874997199.876563199.878109199.879620199.881136199.882613199.884088199.885527199.886971199.888371199.889783199.891142199.892518199.893845199.895180199.896485199.897783199.899057199.900322199.901562199.902797199.904010199.905222199.906392199.907576199.908720199.909875199.910985199.912108199.913193199.914287199.915352199.916423199.917459199.918505199.919527199.920526199.921513199.922496199.923452199.924415199.925348199.926275199.927198199.928108199.929019199.929903199.930788199.931653199.932509199.933353199.934187199.935002199.935816199.936617199.937420199.938195199.938971199.939733199.940477199.941228199.941961199.942685199.943392199.944111199.944804199.945491199.946181199.946854199.947518199.948165199.948824199.949456199.950083199.950714199.951326199.951930199.952532199.953125199.953714199.954290199.954863199.955424199.955979199.956538199.957073199.957623199.958146199.958671199.959189199.959693199.960203199.960689199.961191199.961665199.962156199.962619199.963098199.963543199.964014199.964448199.964907199.965330199.965772199.966201199.966625199.967046199.967458199.967868199.968263199.968664199.969047199.969437199.969817199.970193199.970565199.970943199.971297199.971668199.972011199.972363199.972712199.973047199.973388199.973726199.974049199.974379199.974699199.975014199.975326199.975645199.975939199.976249199.976546199.976832199.977125199.977414199.977688199.977969199.978247199.978525199.978782199.979061199.979312199.979576199.979825199.980077199.980335199.980569199.980812199.981053199.981300199.981522199.981755199.981984199.982213199.982427199.982648199.982860199.983080199.983298199.983501199.983704199.983914199.984114199.984309199.984500199.984698199.984887199.985079199.985262199.985442199.985623199.985803199.985984199.986170199.986327199.986508199.986668199.986846199.987006199.987169199.987321199.987481199.987633199.987800199.987948199.988094199.988237199.988386199.988526199.988675199.988815199.988965199.989090199.989231199.989359199.989491199.989629199.989757199.989889199.990012199.990133199.990253199.990373199.990493199.990614199.990734199.990854199.990960199.991072199.991180199.991289199.991398199.991507199.991616199.991718199.991837199.991922199.992025199.992123199.992214199.992314199.992412199.992503199.992604199.992701199.992792199.992878199.992967199.993047199.993136199.993216199.993305199.993385199.993474199.993554199.993637199.993726199.993806199.993881199.993952199.994024199.994101199.994170199.994241199.994313199.994391199.994459199.994531199.994602199.994680199.994748199.994805199.994868199.994928199.994989199.995049199.995109199.995175199.995226199.995295199.995346199.995416199.995466199.995536199.995593199.995653199.995713199.995759199.995811199.995859199.995902199.995960199.995999199.996051199.996100199.996148199.996191199.996249199.996288199.996340199.996389199.996438199.996480199.996538199.996578199.996629199.996678199.996712199.996746199.996787199.996824199.996855199.996896199.996927199.996967199.997005199.997036199.997076199.997113199.997145199.997185199.997216199.997256199.997294199.997325199.997365199.997403199.997434199.997474199.997512199.997543199.997583199.997614199.997640199.997669199.997689199.997711199.997740199.997760199.997789199.997809199.997838199.997858199.997880199.997909199.997929199.997958199.997978199.998007199.998027199.998049199.998078199.998098199.998127199.998147199.998170199.998199199.998218199.998247199.998267199.998296199.998316199.998339199.998368199.998387199.998416199.998436199.998459199.998488199.998508199.998537199.998556199.998585199.998590199.998605199.998616199.998634199.998642199.998654199.998665199.

    注意事项

    本文(PID控制算法的C语言实现(完整版)(38页).doc)为本站会员(1595****071)主动上传,淘文阁 - 分享文档赚钱的网站仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知淘文阁 - 分享文档赚钱的网站(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    关于淘文阁 - 版权申诉 - 用户使用规则 - 积分规则 - 联系我们

    本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

    工信部备案号:黑ICP备15003705号 © 2020-2023 www.taowenge.com 淘文阁 

    收起
    展开