PID控制器(比例-积分-微分控制器)- I

形象解释PID算法小明接到这样一个任务:有一个水缸点漏水(而且漏水的速度还不一定固定不变),要求水面高度维持在某个位置,一旦发现水面高度低于要求位置,就要往水缸里加水。 小明接到任务后就一直守在水缸旁边,时间长就觉得无聊,就跑到房里看小说了,每30分钟来检查一次水面高度。水漏得太快,每次小明来检查时,水都快漏完了,离要求的高度相差很远,小明改为每3分钟来检查一次,结果每次来水都没怎么漏,不...
摘要由CSDN通过智能技术生成

形象解释PID算法

小明接到这样一个任务:

有一个水缸点漏水(而且漏水的速度还不一定固定不变),要求水面高度维持在某个位置,一旦发现水面高度低于要求位置,就要往水缸里加水。 

小明接到任务后就一直守在水缸旁边,时间长就觉得无聊,就跑到房里看小说了,每30分钟来检查一次水面高度。水漏得太快,
每次小明来检查时,水都快漏完了,离要求的高度相差很远,小明改为每3分钟来检查一次,结果每次来水都没怎么漏,不需要加水,来得太频繁做的是无用功。
几次试验后,确定每10分钟来检查一次。这个检查时间就称为采样周期。 

开始小明用瓢加水,水龙头离水缸有十几米的距离,经常要跑好几趟才加够水,于是小明又改为用桶加,一加就是一桶,跑的次数少了,加水的速度也快了,
但好几次将缸给加溢出了,不小心弄湿了几次鞋,小明又动脑筋,我不用瓢也不用桶,老子用盆,几次下来,
发现刚刚好,不用跑太多次,也不会让水溢出。这个加水工具的大小就称为比例系数。 

小明又发现水虽然不会加过量溢出了,有时会高过要求位置比较多,还是有打湿鞋的危险。他又想了个办法,在水缸上装一个漏斗,
每次加水不直接倒进水缸,而是倒进漏斗让它慢慢加。这样溢出的问题解决了,但加水的速度又慢了,有时还赶不上漏水的速度。
于是他试着变换不同大小口径的漏斗来控制加水的速度,最后终于找到了满意的漏斗。漏斗的时间就称为积分时间

小明终于喘了一口,但任务的要求突然严了,水位控制的及时性要求大大提高,一旦水位过低,必须立即将水加到要求位置,而且不能高出太多,否则不给工钱。
小明又为难了!于是他又开努脑筋,终于让它想到一个办法,常放一盆备用水在旁边,一发现水位低了,不经过漏斗就是一盆水下去,这样及时性是保证了,但水位有时会高多了。
他又在要求水面位置上面一点将水凿一孔,再接一根管子到下面的备用桶里这样多出的水会从上面的孔里漏出来。这个水漏出的快慢就称为微分时间

拿一个水池水位来说,我们 可以制定一个规则,
把水位分为超高、高、较高、中、较低、低、超低几个区段;
再把水位波动的趋势分为甚快、快、较快、慢、停几个区段,并区分趋势的正负;
把输出分为超大幅 度、大幅度、较大幅度、微小几个区段。

当水位处于中值、趋势处于停顿的时候,不调节;
当水位处于中值、趋势缓慢变化的时候,也可以暂不调节;
当水位处于较高、趋势缓慢变化 的时候,输出一个微小调节两就够了;
当水位处于中值、趋势较快变化的时候,输出进行叫 大幅度调节……。

如上所述,我们需要制定一个控制规则表,然后制定参数判断水位区段的界值、波动趋 势的界值、输出幅度的界值。

比例控制(P)是一种最简单的控制方式。其控制器的输出与输入误差信号成比例关系。
根据设备有所不同,比例带一般为2~10%(温度控制)。
但是,仅仅是P控制的话,会产生下面将提到的offset (稳态误差),所以一般加上积分控制(I),以消除稳态误差。
 
比例带与比例控制(P)输出的关系如图所示。用MVp运算式的设定举例:
稳态误差(Off set)

比例控制中,经过一定时间后误差稳定在一定值时,此时的误差叫做稳态误差(off set)。
仅用比例控制的时候,根据负载的变动及设备的固有特性不同,会出现不同的稳态误差。
负载特性与控制特性曲线的交点和设定值不一致是产生稳态误差的原因。
比例带小时不会产生。为消除稳态误差,我们设定手动复位值--manual reset值(MR),以消除控制误差。
手动复位(Manual reset)
 
如前所述,仅用比例控制不能消除稳态误差。
为此,将MR(manual reset值)设为可变,则可自由整定(即调整)调节器的输出。
只要手动操作输出相当于offset的量,就能与目标值一致。
这就叫做手动复位(manual reset),通常比例调节器上配有此功能。
在实际的自动控制中,每次发生off set时以手动进行reset的话,这样并不实用。
在后面将叙述的积分控制功能,能自动消除稳态误差。
 

所谓积分控制(I),就是在出现稳态误差时自动的改变输出量,使其与手动复位动作的输出量相同,达到消除稳态误差的目的。
当系统存在误差时,进行积分控制,根据积分时间的大小调节器的输出会以一定的速度变化,只要误差还存在,就会不断的进行输出。

积分时间的定义: 
积分项比例项对于控制器的输出的贡献相同,即积分作用重复了一次比例作用时所花费的时间,就是积分时间。

微分控制(D)的功能是通过误差的变化率预报误差信号的未来变化趋势
通过提供超前控制作用,微分控制能使被控过程趋于稳定。
因此,它经常用来抵消积分控制产生的不稳定趋势。

微分时间的定义: 
当输入量持续的以一定速率变化时,微分项和比例项对于控制器的输出的贡献相同,
微分作用重复了一次比例作用时所花费的时间,就是微分时间。

PID Control

PID stands for ProportionalIntegralDerivative.

PID control provides a continuous variation of output within a control loop feedback mechanism

 to accurately control the process, removing oscillation and increasing efficiency.

How Do PID Controllers Work? 

Where proportional control is used to minimize the oscillation characteristic of on/off control,

PID control goes that bit further to reduce errors and provide accuracy and stability in a process.

It does this by using the integral action and derivative actions to eliminate control deviation errors 

and to manage rapid process movements.

All three PID terms need to tuned appropriately for the application requirements to achieve the best control.

For a high level of control, digital PID controllers are often used.

These typically come in the form of PID Temperature Controllers or

PID Process Controllers and can be single, dual or multi-loop instruments.

PID control is used for a variety of process variables such as; Temperature, Flow and Pressure.

Typically, challenging applications such as industrial heat treatment processes,

ovens and furnaces use PID controllers as well as in the scientific and

lab sector where precision and reliability is essential to the quality of a control application.

For a detailed insight into how PID Controllers work please refer to the PID Controller Wiki page. 

To achieve the best levels of control it is necessary to tune PID controllers, this can be done in a number of ways.

Manual PID Tuning

Controllers will enable manual PID tuning meaning the P, I and D variables must

be manually calculated and set using the controller menu.

This requires a reasonable level of knowledge and understanding from the user to be able to carry out the calculation.

Often this may still require some trial and error testing.

Manual tuning can be extremely time consuming compared to the alternatives.

PID Auto-Tuning

These days, most products will support auto-tuning (also known as self-tuning) of PID settings.

Typically the way this works will depend on which manufacturer’s product you are using,

but commonly they use a rule based calculation in the same way that an experienced engineer would.

Auto-tuning can either take place at the set-point or with some controllers it occurs as the load is being heated up from the ambient temperature.

More recently, controllers have introduced a number of options for auto-tuning PID settings.

These allow the settings to be more closely aligned to a specific application’s requirements

for example prioritising the minimisation of overshoot over the time it takes to reach the set-point.

PID控制系统

连续一时间PID控制系统如图所示。
D(s)为控制器。在PID控制系统中,D(s)完成PID控制规律,称为PID控制器。
PID控制器是一种线性控制器,用输出量y(t)和给定量r(t)之间的误差的时间函数e(t)=r(t)-y(t)。

 

图中的比例,积分,微分的线性组合,构成控制量u(t)称为
比例(Proportional),积分(Integrating),微分(Differentiation)控制,简称PID控制。

增量算法
当执行机构需要的不是控制量的绝对值,而是控制量的增量(例如去驱动步进电动机)时,需要用PID的“增量算法”。

 

比例,积分,微分

我们先来了解下正式的定义,P,I,D分别代表控制算法中的三个组成部分:比例,积分,微分。 

比例

阀门驱动的准则之一是比例偏差,指的是系统读数与设定值间的差异,该偏差乘以P值后的数值被赋予求和地址位。
由此可见,如果实际读数与设定值之间的差异很大,控制器就会快速驱动阀门工作以试图到达设定值,可以把它看作踩油门。 

积分

由微积分曲线图可知,积分是某两点区间内曲线下方包围的面积,通常即起始时间点与结束时间点。
说得更实际点,就是从零点开始的读数总和或误差总和。
调节P值和D值仅着力于当下或前一刻的测量状态,I值则利用更多较早的数据将当前系统读数修正至设定值。
艾里卡特大多数将质量流量控制器、压力控制器的I值设为零,P值与D值被计入以每秒1000次的速度更新的求和地址位,
免除用户输入I值的必要。求和地址位的数值即用来按比例地控制阀门开度。 

微分

根据微积分函数表达式y=T(dx/dt),微分是变量(x)对时间(t)的求导。
就质量流量控制器而言,流量值就是这个变量(x)。
PID控制算法会将求和地址位数值,减去dx/dt乘以D值后的值,作为阻尼项,因而可以将D值理解为刹车。

PID控制器是一种线性调节器,这种调节器是将系统的给定r与实际输出值y构成的控制偏差

e =y- r的比例、积分、微分,通过线性组合构成控制量输入给控制对象。 

PID控制器各校正环节的作用

比例环节:快速减小偏差,加快响应速度,但是过大会使系统超调量增大,影响系统稳定性。

积分环节:主要用于减小稳态误差,但会使系统响应速度减慢。

微分环节:在系统中偏差信号变得太大之前引入一个修正信号,从而加快系统的动作速度,减小了调节时间,减少了系统的超调量。

PID控制器 

PID Theory

PID控制器(比例-积分-微分控制器),由比例单元P、积分单元I和微分单元D组成。

通过Kp,Ki和Kd三个参数的设定。PID控制器主要适用于基本上线性,且动态特性不随时间变化的系统。

PID控制器是一个在工业控制应用中常见的反馈回路部件。

这个控制器把收集到的数据和一个参考值进行比较,然后把这个差别用于计算新的输入值,

这个新的输入值的目的是可以让系统的数据达到或者保持在参考值。

PID控制器可以根据历史数据和差别的出现率来调整输入值,使系统更加准确而稳定。

PID控制器的比例单元P、积分单元I和微分单元D分别对应目前误差、过去累计误差及未来误差。

若是不知道受控系统的特性,一般认为PID控制器是最适用的控制器。

借由调整PID控制器的三个参数,可以调整控制系统,设法满足设计需求。

控制器的响应可以用控制器对误差的反应快慢、控制器过冲的程度及系统震荡的程度来表示。

不过使用PID控制器不一定保证可达到系统的最佳控制,也不保证系统稳定性。

有些应用只需要PID控制器的部分单元,可以将不需要单元的参数设为零即可。

因此PID控制器可以变成PI控制器、PD控制器、P控制器或I控制器。

其中又以PI控制器比较常用,因为D控制器对回授噪声十分敏感,
而若没有I控制器的话,系统不会回到参考值,会存在一个误差量。

 

PID是以它的三种纠正算法而命名。

受控变数是三种算法(比例、积分、微分)相加后的结果,即为其输出,

其输入为误差值(设定值减去测量值后的结果)或是由误差值衍生的信号。

若定义 u(t) 为控制输出,PID算法可以用下式表示:

比例控件

比例控制考虑当前误差,误差值和一个正值的常数Kp(表示比例)相乘。
Kp只是在控制器的输出和系统的误差成比例的时候成立。
比如说,一个电热器的控制器的比例尺范围是10°C,它的预定值是20°C。
那么它在10°C的时候会输出100%,在15°C的时候会输出50%,在19°C的时候输出10%,
注意在误差是0的时候,控制器的输出也是0。

比例控制的输出如下:

若比例增益大,在相同误差量下,会有较大的输出,
但若比例增益太大,会使系统不稳定。相反的,
若比例增益小,若在相同误差量下,其输出较小,因此控制器会较不敏感的。
若比例增益太小,当有干扰出现时,其控制信号可能不够大,无法修正干扰的影响。

The proportional term changes the output according to the error.

A simple proportional controller has only one control parameter, the Kp.

By changing this parameter, the controller becomes more or less reactive in changes.

The higher the Kp parameter, the faster the system will try to reach the set-point,
but also high proportional gain turns the system unstable
and it oscillates around the set-point.

On the other hand, if the Kp is low, then the the system will stabilize with a constant error bellow (or above) the set-point,
because the output will not provide sufficient power for the system to reach the final position.

Here is a graph to better understand this parameter:

The red line shows the response of the system with a high Kp.
The system will try to reach the set-point faster, but the overshoot is high as well as the error.
This drives the system into an unstable state in which it oscillates around the set-point.

The blue line shows the response with a low Kp.
The system will reach the set-point slower and it will cause a much smaller overshoot.
But after a few oscillations, the controller output does not provide enough power to change the system,
thus it will remain stable a little bellow the set-point.

稳态误差

比例控制在误差为0时,其输出也会为0。
若要让受控输出为非零的数值,就需要有一个稳态误差或偏移量
稳态误差和比例增益成正比,和受控系统本身的增益成反比。
若加入一偏置,或是加入积分控制,可以消除稳态误差。

不同比例增益Kp下,受控变数对时间的变化(Ki和Kd维持定值)

积分控件

不同积分增益K i下,受控变数对时间的变化(K p和K d维持定值)
 
 

积分控制考虑过去误差,将误差值过去一段时间和(误差和)乘以一个正值的常数Ki
Ki从过去的平均误差值来找到系统的输出结果和预定值的平均误差。
一个简单的比例系统会震荡,会在预定值的附近来回变化,因为系统无法消除多余的纠正。
通过加上负的平均误差值,平均系统误差值就会渐渐减少。
所以,最终这个PID回路系统会在设定值稳定下来。积分控制的输出如下:

积分控制会加速系统趋近设定值的过程,并且消除纯比例控制器会出现的稳态误差。

积分增益越大,趋近设定值的速度越快,不过过因为积分控制会累计过去所有的误差,可能会使回授值出现过冲的情形。

A proportional system is usually not sufficient to eliminate the error.

The system must not only change its output according to the current error,
but it must also be able to watch and change the output according to the past errors.

The integral is the sum of the errors over time.
What this means is that, if the error is big, then the integral builds up as time passes
and the output changes rapidly to eliminate the error.

Now suppose that we have the same example as before, a proportional system that has a low Kp.
First of all, the system will respond much faster at the beginning, because the error will build up a big integral.
Then, when the system is stabilized little bellow the set-point, the integral will take over.
This small error is added over time to increase the integral, and finally it will change the system's output.
Finally, the system will stabilize onto the set-point.

Now, suppose that we have a proportional system with low Kp and an integral term.
Such a system is named PI, and here is how the system reacts as Ki changes:

A high integral gain (Ki) (red line) causes a faster system response, but also causes overshoot.
Unlike the pure proportional system, despite the overshoot, the system is not driven into an unstable state.
After a few oscillations the system will stabilize on the set-point.

As the integral gain gets smaller (blue line), the system responses slower,
but it stabilizes much faster with less oscillations.
The optimal integral gain (purple line) causes the system to respond very slow
but it stabilizes on the set-point with almost no oscillations. 

微分控件

不同微分增益K d下,受控变数对时间的变化(K p和K i维持定值)

微分控制考虑将来误差,计算误差的一阶导,并和一个正值的常数Kd相乘。
这个导数的控制会对系统的改变作出反应。
导数的结果越大,那么控制系统就对输出结果作出更快速的反应。
这个Kd参数也是PID被称为可预测的控制器的原因。
Kd参数对减少控制器短期的改变很有帮助。
一些实际中的速度缓慢的系统可以不需要Kd参数。

微分控制的输出如下:

微分控制可以提升整定时间及系统稳定性。
不过因为纯微分器不是因果系统,因此在PID系统实现时,一般会为微分控制加上一个低通滤波器以限制高频增益及噪声。
实务上较少用到微分控制,估计PID控制器中只有约20%有用到微分控制。

The third and last term is the Derivative.

This term calculates the rate of change of the error and adds to the output accordingly.

If the error changes slow, then the derivative is increased in order to make the PID system respond faster.
On the other hand, if the error is changes rapidly, the derivative is decreased
in order to make the system more stable and avoid oscillations. 

Tuning a PID controller

The process of setting the P, I and D parameters to obtain an ideal response of the system is called tuning.

Over the years, several tuning methods have been described,

but we will discuss only the Ziegler-Nichols method and of course the manual tuning.

The tuning time is relative to the system type and speed of change.

If a system has a rapid response, then the tuning time is very short.

On the other hand, a slow response system has a very long tuning time.

A fast-response system is for example a robot positioning system.

An example of a slow response system could be a large water tank heater.

It is obvious that a parameter change in the robot positioning system will have an immediate response at the robot's behavior,

while a parameter change in the water tank heater system may need several minutes for the systems to react.

Manual PID Tuning

If someone wants to tune a PID system with this method, then he first must be sure
that he understands what each parameter does and how each one affects the system,
otherwise this can be a headache. These are the steps for this method:

  • The I and D terms are set to zero
  • The P term is increased until the system oscillates
  • The P term is increased slowly to increase the system response, but it must not become unstable
  • When the P term is set to obtain a desired fast response, the I term is increased to stop the oscillations.
    This will reduce the steady state error but may increase the overshoot.
    Faster system response may require some amount of overshoot.
  • When I is set to desired amount of overshoot and minimal state error,
    the D term is set until the system achieves an acceptable quick loop to its set-point.
    Increasing the D term will result in decreasing the overshoot and yields higher gain with stability,
    but it may cause the system to be very sensitive to noise.

Tuning a PID system with the Ziegler-Nichols method

The process of setting the optimal gains for P, I and D to get an ideal response from a control system is called tuning.

There are different methods of tuning of which the “guess and check” method and the Ziegler Nichols method will be discussed.

The gains of a PID controller can be obtained by trial and error method.

Once an engineer understands the significance of each gain parameter, this method becomes relatively easy.

In this method, the I and D terms are set to zero first and the proportional gain is increased until the output of the loop oscillates.

As one increases the proportional gain, the system becomes faster, but care must be taken not make the system unstable.

Once P has been set to obtain a desired fast response, the integral term is increased to stop the oscillations.

The integral term reduces the steady state error, but increases overshoot.

Some amount of overshoot is always necessary for a fast system so that it could respond to changes immediately.

The integral term is tweaked to achieve a minimal steady state error.

Once the P and I have been set to get the desired fast control system with minimal steady state error,

the derivative term is increased until the loop is acceptably quick to its set point.

Increasing derivative term decreases overshoot and yields higher gain with stability

but would cause the system to be highly sensitive to noise.

Often times, engineers need to tradeoff one characteristic of a control system for another to better meet their requirements.

The Ziegler-Nichols method is another popular method of tuning a PID controller.

It is very similar to the trial and error method wherein I and D are set to zero and P is increased until the loop starts to oscillate.

Once oscillation starts, the critical gain Kc and the period of oscillations Pc are noted.

The P, I and D are then adjusted as per the tabular column shown below.

The Ziegler-Nichols method was introduced by John G. Ziegler and Nathaniel B. Nichols in the 1940s.

To tune a system with the Ziegler-Nichols method,
the engineer must first define the critical P gain, which we will name Kc.
This is how its done:

  • The I and D terms are set to zero
  • The P term is increased until the system oscillates in a stable rate. This will be the Kc

Once the Kc is defined, then the engineer must measure the oscillation period.
It is the same like measuring the period of an AC signal.
This will be the Pc.
Finally, the user can calculate the values for P,I and D parameters using the following table:

If you're wondering what the Ti and Td terms are, these are common parameters in a PID controller.

A typical PID controller has three parameters, K, Ti and Td, but there are many different kinds of PID controllers.

For example, a PID controller with set-point weighting and derivative filter has six parameters K, Ti, Td, Tf , b and c.

Parameters b and c are called set-point weights.

They have no influence on the response to disturbances but they have a significant influence on the response to set-point changes,
so you do not need to worry about them when tuning a PID system.

Example

Here is an example of a system that we want to tune with the Ziegler-Nichols method.

First, we need to find the critical P gain.
So, we make the I and D parameters zero and we let the controller operate as a simple proportional controller (only P).

Then, we start to increase the parameter P and write down the system's response.
Here are 4 typical characteristics of a system's response in relation to the P change:

Suppose that the setting for P is 20. So, Kc is 20.

Now we can take a closer look at the last chart:

The horizontal axis shows the time in seconds.
As you can see, the oscillation has a period of about 15 seconds (45 to 60 seconds).
So, the second parameter that we need, the Pc, is 15' seconds.
To get the PID parameters for our controller, we need to use these values with the Ziegler-Nichols table:

P = 0.60 x Kc => P = 0.6 x 20 => P = 12 
Ti = 0.5 x Pc => Ti = 0.5 x 15 => Ti = 7.5 
Td = Pc / 8 => Td = 15 / 8 => Td = 1.87 

After 2 pages of theory and mathematical hocus-pocus,
there will be people that still are not able to write software for a PID controller.

For those people, i have 2 good news:

First is that i found the best documentation for making a PID controller with a PIC microcontroller.
It is the Application Note 937 from Microchip (who else):

Second good news: A PID controller example explained in simple words

I understand that not all people feel comfortable with mathematics,
but many would like to make a PID controller for number of reasons.

The integral term is by far the hardest to decipher.

Instead of just explaining the integral term, i will explain a complete (and simple) PID controller with an example.

I bet that after this, everyone will be able to make one (as long as he know how to program a microcontroller of course).

So, it goes like this: We want to control the temperature of a water heater.

The heater resistor is controlled by a TRIAC, and the microcontroller controls the gate of that TRIAC (it could operate as a dimmer for example).

A temperature sensor is placed in the water and gives feedback to the microcontroller in degrees Celsius (oC).

The system has these parameters which you must define as RAM variables,

and also it is good to save them in an EEPROM memory because you do not want to define them every time the system restarts: 

  • SP - The Set-Point of the system, the temperature that we want to achieve

  • P_Gain - The gain for the Proportional term
  • I_Gain - The gain for the Integral term
  • D_Gain - The gain for the Derivative term

  • I_Time - Reset time, this is the Ti parameter that we saw on previous pages
  • D_Time - Derivative time, this is the Td parameter that we saw on previous pages 

You will also need to define these additional RAM variables: 

  • P_Term - The Proportional term to be added to the output.
  • I_Term - The Integral term to be added to the output.
  • D_Term - The Derivative term to be added to the output.

  • PV - The Process Variable, this is the temperature that the sensor reads.
  • Err - This will hold the current error (SP-PV).

  • Acc_Err - This is the accumulated error to help calculate the Integral term
  • D_Err - This is the derivative error to help calculate the Derivative term

  • Ti_Counter - A countdown counter for the Integral accumulation routine
  • Td_Counter - A countdown counter for the Derivative error calculation

  • P_Out - The output power calculated by the PID 

This is the simplest PID controller.
The output of this system is described by this function (do not be scared if you know little about mathematics)

The microcontroller will run an infinite loop, during which it will calculate the PID terms and control the output.

I call this routine "MainLoop". During start-up, the microcontroller will run the "Initialization" routine.

Here are the flow chart of the Initialization and the MainLoop routines:

Initialization

 

MainLoop 

 

Finally, there are 3 more subroutines, 

Calculate_P_TermCalculate_I_Term andCalculate_D_Term.

Here are the flow charts for those:

    

How it works then?

First of all, i want you to agree that, behind the mask of the hard math,

there is only a simple program with additions and subtractions (ok, and some multiplications and divisions maybe).

I assume that you do know how to rea

  • 9
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值