如何使用PLC实现PID控制

如何使用PLC实现PID控制

 

 

1 引言
工业生产中,常需要用闭环控制方式来实现温度、压力、流量等连续变化的模拟量控制。无论使用模拟控制器的模拟控制系统,还是使用计算机(包括PLC)的数字控制系统,PID控制都得到了广泛的应用。
PID控制器是比例-积分-微分控制的简称,具有
(1) 不需要精确的控制系统数学模型;
(2) 有较强的灵活性和适应性;
(3) 结构典型、程序设计简单,工程上易于实现,参数调整方便等优点。积分控制可以消除系统的静差,微分控制可以改善系统的动态相应速度,比例、积分、微分三者有效地结合可以满足不同的控制要求。

2 PLC实现PID的控制方式
2.1 PID过程控制模块
这种模块的PID控制程序是PLC生产厂家设计的,并存放在模块中,用户使用时序要设置一些参数,使用起来非常方便,一个模块可以控制几路甚至几十路闭环回路。
2.2 PID功能指令
现在很多PLC都有供PID控制用的功能指令,如S7-200的PID指令。它们实际上是用于PID控制的子程序,与模拟量输入/输出模块一起使用,可以得到类似于使用PID过程控制模块的效果。
2.3 用自编的程序实现PID闭环控制
有的PLC没有PID过程控制模块和PID控制用的功能指令,有时虽然可以使用PID控制指令,但是希望采用某种改进的PID控制算法。在上述情况下都需要用户自己编制PID控制程序。

3 PLC-PID控制器的实现
本文以西门子S7-200PLC为例,说明PID控制的原理及PLC的PID功能指令的使用及控制功能的实现。
3.1 PID控制器的数字化
PLC的PID控制器的设计是以连续系统的PID控制规律为基础,将其数字化写成离散形式的PID控制方程,再跟据离散方程进行控制程序设计。
在连续系统中,典型的PID闭环控制系统如图1所示。图1中sp(t)是给定值,pv(t)是反馈量,c(t)是系统的输出量,PID控制的输入输出关系式为:

式中:
M(t)—控制器的输出量,M0为输出的初始值;
e(t)=sp(t)-pv(t)-误差信号;
KC比例系数;
TI-积分时间常数;
TD-微分时间常数。

 

图1 连续闭环控制系统方框图


式(1)的右边前3项分别是比例、积分、微分部分,它们分别与误差,误差的积分和微分成正比。如果取其中的一项或两项,可以组成P、PD或PI控制器。
假设采样周期为TS,系统开始运行的时刻为t=0,用矩形积分来近似精确积分,用差分近似精确微分,将公式1离散化,第n次采样时控制器的输出为: (2)

式中:
en-1-第n-1次采样时的误差值;
KI-积分系数;
KD-微分系数。
基于PLC的闭环控制系统如图2所示。图中的虚线部分在PLC内。其中spn、pvn、en、Mn分别为模拟量在sp(t)、pv(t)、e(t)、M(t)在第n次采样时的数字量。

 

图2 PLC闭环控制系统方框图

在许多控制系统内,可能只需要P、I、D中的一种或两种控制类型。如可能只要求比例控制或比例与积分控制,通过设置参数可对回路进行控制类型进行选择。
3.2 输入输出变量的转换
PID控制有两个输入量:给定值(sp)和过程变量(pv)。多数工艺要求给定值是固定的值,如加热炉温度的给定值。过程变量是经A/D转换和计算后得到的被控量的实测值,如加热炉温度的测量值。给定值与过程变量都是与被控对象有关的值,对于不同的系统,它们的大小、范围与工程单位有很大的区别。应用PLC的PID指令对这些量进行运算之前,必须将其转换成标准化的浮点数(实数)。
同样,对于PID指令的输出,在将其送给D/A转化器之前,也需进行转换。

3.3 回路输入的转换
转换的第一步是将给定值或A/D转换后得到的整数值由16位整数转换成浮点数,可用下面的程序实现这种转换:
XORD AC0, ACO
//清除累加器
MOVW AIWO, AC0
//将待转化的模拟量存入累加器
LDW>= AC0, 0
//如果模拟量数值为正
JMP 0
//直接转换成实数
ORD 16#FFFF0000, ACO
//将AC0内的数值进行符号扩展,扩展为32位负数
LBL 0
DTR AC0, AC0
//将32位整数转换成实数
转换的下一步是将实数进一步转换成0.0~1.0之间的标准化实数,可用下面的式(3)对给定值及过程变量进行标准化:
RNorm=(RRaw/Span)+Offset (3)
式中:
RNorm-标准化实数值;
RRaw-标准化前的值;
Offset-偏移量,对单极性变量为0.0,对双极性变量为0.5;
Span-取值范围,等于变量的最大值减去最小值,单极性变量的典型值为32000,双极性变量的典型值为64000。
下面的程序将上述转换后得到的AC0中的双极性实数(其Span=64000)转换成0.0~1.0之间的实数:
/R 64000.0, AC0
//累加器中的实数标准化
+R 0.5, AC0
//加上偏移值,使其在0.0~1.0之间
MOVR ACO, VD100
//加标准化后的值存入回路表内
3.4 回路输出的转换
回路输出即PID控制器输出,它是标准化的0.0~1.0之间的实数。将回路输出送给D/A转换器之前,必须转换成16位二进制整数。这一过程是将pv与sp转换成标准化数值的逆过程。用下面的公式将回路输出转换成实数:
RScal=(Mn-Offset)×Span (4)
式中,RScal是回路输出对应的实数值,Mn是回路输出标准化的实数值。
下面的程序用来将回路输出转换为对应的实数:
MOVR VD108, AC0
//将回路输出送入累加器
-R 0.5, AC0
//仅双极性数才有此语句
*R 64000.0, AC0
//单极性变量乘以32000.0
用下面的指令将代表回路输出的实数转换成16位整数:
ROUND AC0, AC0
//将实数转换为32位整数
MOVW AC0, AQW0
//将16位整数写入模拟输出(D/A)寄存器
3.5 PID指令及回路表
S7-200的PID指令如图3所示:

 

图3 PID指令

指令中TBL是回路表的起始地址,LOOP是回路的编号。编译时如果指令指定的回路表起始地址或回路号超出范围,CPU将生成编译错误(范围错误)仪器编译失败。PID指令对回路表中的某些输入值不进行范围检查,应保证过程变量、给定值等不超限。回路表参见附表。

附表 PID指令的回路表

 


如果PID指令中的算术运算发生错误,特殊存储器SMI.1(溢出或非法数值)被置1,并将终止PID指令的执行。要想消除错误,在下次执行PID运算之前,应改变引起运算错误的输入值,而不是更新输出值。

4 PID指令编程举例
某一水箱里的水以变化速度流出,一台变频器驱动的水泵给水箱打水,以保持水箱的水位维持在满水位的75%。过程变量由浮在水面上的水位测量仪提供,PID控制器的输出值作为变频器的速度给定值。过程变量与回路输出均为单极性模拟量,取值范围为0.0~1.0。
本例采用PI控制器,给定值为0.75,选取控制器参数的初始值为:KC=0.25,TS=0.1s,TI=30min。编程如下:
//主程序(OBI)
LD SM0.1 //首次扫描时
CALL 0 //调用初始化子程序
//子程序
LD SM0.0
MOVR 0.75, VD104 //装入给定值75%
MOVR 0.25, VD112 //装入回路增益0.25
MOVR 0.10, VD116 //装入采样时间0.1s
MOVR 30.0 VD120 //装入积分时间30min
MOVR 0.0, VD124 //关闭微分作用
MOVB 100, SMB34
//设置定时中断0的时间间隔为100ms
ATCH 0, 10
//设定定时中断以执行PID指令
ENI
//允许中断,子程序0结束
//中断程序0
LD SM0.0
LTD AIW0, AC0
//单极性模拟量经A/D转换后存入累加器
DTR AC0, AC0
//32位整数转换为实数
/R 32000.0, AC0
//标准化累加器中的实数
MOVR AC0, VD100 //存入回路表
LD 10.0
//在自动方式下,执行PID指令
PID VB100, 0
//回路表的起始地址为VB100,回路号为0
LD SM0.0
MOVB VD108, AC0
//PID控制器的输出值送入累加器
*R 32000.0 AC0
//将累加器中的数值标准化
ROUND AC0, AC0
//实数转换为32位整数
DTI AC0, AQW0
//将16位整数写入到模拟量输出(D/A)寄存器

5 结束语
PLC实现PID控制的方法多种,直接应用PID指令来实现基于PLC的PID控制,是一种易于实现且经济实用的方法。

Write a comment

Comments: 18
  • #1

    Lindsey Napoli (Saturday, 04 February 2017 09:12)


    Hi, I do believe this is a great website. I stumbledupon it ;) I'm going to come back yet again since i have book-marked it. Money and freedom is the best way to change, may you be rich and continue to guide others.

  • #2

    Darlene Ogilvie (Saturday, 04 February 2017 13:44)


    I have to thank you for the efforts you've put in writing this website. I am hoping to check out the same high-grade content from you in the future as well. In fact, your creative writing abilities has inspired me to get my own site now ;)

  • #3

    Damon Trueblood (Sunday, 05 February 2017 02:16)


    Hi to all, how is everything, I think every one is getting more from this web site, and your views are fastidious in favor of new users.

  • #4

    Jesus Trask (Sunday, 05 February 2017 04:30)


    Awesome post.

  • #5

    Christina Caskey (Sunday, 05 February 2017 08:12)


    This is a topic which is near to my heart... Thank you! Exactly where are your contact details though?

  • #6

    Iona Cavalier (Sunday, 05 February 2017 11:56)


    Nice blog here! Additionally your site a lot up fast! What host are you the usage of? Can I am getting your associate link to your host? I want my website loaded up as quickly as yours lol

  • #7

    Anna Groover (Sunday, 05 February 2017 14:53)


    It's perfect time to make some plans for the future and it's time to be happy. I've read this publish and if I may just I want to counsel you few attention-grabbing things or advice. Maybe you can write next articles regarding this article. I desire to learn even more things about it!

  • #8

    Micha Leisinger (Monday, 06 February 2017 01:29)


    Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically tweet my newest twitter updates. I've been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with something like this. Please let me know if you run into anything. I truly enjoy reading your blog and I look forward to your new updates.

  • #9

    Susie Tenaglia (Monday, 06 February 2017 06:31)


    Thanks for the marvelous posting! I definitely enjoyed reading it, you happen to be a great author.I will remember to bookmark your blog and will often come back in the future. I want to encourage you continue your great job, have a nice weekend!

  • #10

    Cheryl Hatchell (Monday, 06 February 2017 12:08)


    Pretty! This was a really wonderful article. Thanks for supplying these details.

  • #11

    Zenobia Mullins (Monday, 06 February 2017 23:21)


    Hello there! Do you know if they make any plugins to assist with SEO? I'm trying to get my blog to rank for some targeted keywords but I'm not seeing very good results. If you know of any please share. Cheers!

  • #12

    Heide Chestnut (Tuesday, 07 February 2017 16:54)


    I like the helpful information you provide in your articles. I'll bookmark your blog and check again here frequently. I am quite sure I will learn lots of new stuff right here! Good luck for the next!

  • #13

    Wonda Iversen (Tuesday, 07 February 2017 22:45)


    Awesome! Its in fact remarkable post, I have got much clear idea about from this piece of writing.

  • #14

    Marcel Niccum (Wednesday, 08 February 2017 01:13)


    Greetings from Ohio! I'm bored at work so I decided to browse your blog on my iphone during lunch break. I really like the knowledge you present here and can't wait to take a look when I get home. I'm amazed at how fast your blog loaded on my mobile .. I'm not even using WIFI, just 3G .. Anyways, good site!

  • #15

    Chae Baird (Wednesday, 08 February 2017 13:22)


    An impressive share! I've just forwarded this onto a colleague who has been conducting a little research on this. And he in fact ordered me lunch due to the fact that I found it for him... lol. So let me reword this.... Thank YOU for the meal!! But yeah, thanks for spending some time to talk about this topic here on your web page.

  • #16

    Flo Griffie (Wednesday, 08 February 2017 18:42)


    Just desire to say your article is as amazing. The clearness in your post is just excellent and i could assume you're an expert on this subject. Well with your permission allow me to grab your RSS feed to keep up to date with forthcoming post. Thanks a million and please keep up the gratifying work.

  • #17

    Brendon Dameron (Thursday, 09 February 2017 05:01)


    I really like what you guys tend to be up too. This type of clever work and reporting! Keep up the fantastic works guys I've incorporated you guys to blogroll.

  • #18

    Latosha Rebuck (Thursday, 09 February 2017 07:24)


    Hiya! Quick question that's totally off topic. Do you know how to make your site mobile friendly? My site looks weird when browsing from my iphone. I'm trying to find a template or plugin that might be able to fix this problem. If you have any recommendations, please share. Appreciate it!