Home      Projects     Experiments     Circuits     Theory     BLOG     PIC Tutorials     Time for Science

13 August 2011
Author: Giorgos Lazaridis
PID Theory

If you have work with some kind of industrial or marine automation, then most probably you've heard before the term PID. PID controllers are very common in closed-loop systems today. Here is how this system can calculate and minimize the error with great precision.

History

The whole story began as a marine application, when people were trying to find ways to make reliable and accurate ship steering systems. But the problem was that, if the automation turns the rudder let's say left, the ship will not turn instantaneously, instead it needs a long course, for ships do not steer like like cars, instead they have a big hysteresis. Another problem is also that when the ship finally turns to the right direction and the automation turns the rudder straight, the ship keeps turning left due to inertia and many other parameters like waves, wind, speed etc.

At first, proportional systems were developed to do this. A proportional systems reads the feedback (electronic compass) and turns the rudder according to the angle that the ship needs to turn. If for example the ship had to turn 45 degrees left, the rudder would turn 20 degrees, and as the ship slowly turns to this direction, the rudder decreases its angle proportionally. But this system has a great disadvantage: Either the rudder will oscillate left and right because the ship will never stay on course precisely due to external disturbances, or the system will stabilize with a small constant error in angle.

Elmer Sperry was the first to introduce an early PID-type controller back in 1911, and it took another 11 years until engineer Nicolas Minorsky published the first theoretical analysis of a PID controller (Minorsky 1922). In order to design an automatic steering system for the US navy, Minorsky spent several hours observing helmsmen while on duty. He noticed that the helmsman turned the rudder not only according to the current error, but also according to the past errors and the rate of change. During first trials with a PI controller on the USS New Mexico, the system achieved an error of 2 degrees. When the parameter D was added (PID) the system managed to achieve error of 1/6 of a degree, better than most helmsmen could achieve.

What is P, what is I, what is D?

The word PID is composed by the first letters from the terms Proportional, Integral and Derivative. The Proportional, Integral, and Derivative terms are summed to calculate the output of the PID. Here is the diagram to get a first (just a first) idea how this system works:

As you see, the setpoint is compared with the feedback. With the error (setpoint-feedback) the controller calculates the three terms (P-I-D), and finally these terms are added to calculate the new output.

Before i go into details, i need to define some terms in advance:

• Error [e]: The error is the difference between the target position and the real position. Suppose for example that we have a system that controls the temperature. If the set-point is 100oC and the actual temperature is 60oC, then the error is 40oC. The error is represented by the letter "e".
• Proportional gain parameter [Kp]: This is a number that controls the proportional term. In a real life controller, this number is usually a controller parameter which the end-user can change
• Integral gain parameter [Ki]: This is a number that controls the integral term. In a real life controller, this number is usually a controller parameter which the end-user can change
• Derivative gain parameter [Kd]: This is a number that controls the derivative term. In a real life controller, this number is usually a controller parameter which the end-user can change

• The term P: Proportional

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 proportional term is given by this form:

P = Kp x e

And the output of the controller:

Pout = P = Kp x e

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.

The term I: Integral

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.

Like before, the integral term is multiplied by the integral gain parameter to increase or decrease the effect and it is given by this form:

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.

A PI system output is mathematically represented as:

In other words, the output is the sum of the Proportional term plus the Integral term.

The term D: Derivative

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. The formula to describe the derivative is:

The output of a full PID controller is given my this mathematical form:

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 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:

 Control P Ti Td P 0.5Kc - - PI 0.45Kc Pc/1.2 - PID 0.60Kc 0.5Pc Pc/8

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:

 P is too low and must be increased P is still low P is too high and made the system unstable This is the value for P that we are looking for. The system has a stable oscillation.

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''. 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