Saturday, July 6, 2013

PID













Feedback (or closed loop) control is where control theory really starts to get interesting. ? What do we mean by feedback? Feedback is information fed back into the control system providing information about the current status of what it is trying to control. Let's clarify with an example.
You are driving a car along a straight and level road. You press the accelerator until the speed reaches 30mph. You then ease off on the accelerator a touch to maintain that constant speed. You are now participating in a closed loop control system. The objective, or target, of control here is the desired speed of the car. The input to the control loop is the reading of speed seen by you on the speedometer and the output is the control of the accelerator pedal. The intelligence at work here (I use the phrase in the most general sense !) is you, the driver. As you drive along the road there is a dynamic interaction between the speedometer reading and the position of the pedal. As you detect an error in the target speed of 30mph you apply a correcting action using the pedal. In terms of feedback, you are feeding back the detected error in the target speed and applying it to the pedal. (For the moment we will ignore the mathematics that would dictate constants of proportionality etc… and focus on the fundamental characteristics).











So, all is well. We have the car under control, with our personal feedback loop and are wondering what all the fuss is about… feedback control is easy. Before we get too confident let's make some changes to this control system, and see where it takes us. Some of the changes may seem a little strange but stay with me, because they will lead to a better understanding further down the line.
Let's replace the accelerator pedal with a switch. "On" gives maximum acceleration and "off" gives none at all. Crazy?, of course it is but imagine your attempts at trying to maintain a steady speed. You would probably find that your speed would oscillate above and below your target of 30mph, probably quite significantly. You could perhaps achieve a better control with higher speed shorter pulses of the switch but you would then be facing the problem of speed of response of the engine. The engine will not deliver power instantly (well at least not the cars I drive). Without a significant duration of switch-on time you may not see any change in speed.
This scenario is called "Bang-Bang" control and the oscillations about the target speed are called "Hunting". Bang-bang control is the most basic form of feedback control. In its favour we have to say that it is normally the least expensive to implement but against it is the quality and accuracy of achieving the target. Generally we can say that it is more suitable for control systems with slow changing targets. Choosing the switching points more carefully can make some improvements. In the case of the car, the accelerator could be switched on when the speed drops to 28 mph and switched off when 32mph. This allows time for the engine to respond and reduces the amount of switching but sacrifices the accuracy of control. i.e. the range of hunting is, at best, 28 - 32 mph. Another common example of bang-bang control is the central heating thermostat. If you set 20 degrees on the dial, it will normally switch on when the temperature falls to 18 and off when at 22. This gap between on and off is called "Hysterisis".
   Now ask yourself this question; why was it easier to control the speed using the accelerator pedal rather than the switch ? (read on).
Proportional Control
   Without realizing it you were applying proportional control to the car speed. In other words the corrective action you took in response to an incorrect speed was in proportion to the amount of error.  A large drop in speed caused you to floor the accelerator and, as your speed came up, you gradually eased off the pedal until achieving the target of 30mph. So much easier than the switch, but more complex control. With the switch control, your input (reading the speedometer), only had to decide above or below the target. Now you have to know how much above or below.  Your output was a simple on-off, now its a pedal position.  Is it worth it for controlling the car? Of course it is. Would you use it for central heating control? Probably not. Knowing where it is appropriate to apply specific control techniques is as important as knowing how to apply them.

Let’s adjust our car control environment a little more to examine a limitation in proportional control. We are driving along a flat road with our proportional control of the pedal responding to changes in the speedometer reading when suddenly we start to climb a steep hill.  The speed drops very quickly and we match the drop by a carefully proportioned depression of the accelerator. The net result is that eventually we get back up to 30mph but only after we have dropped right down to about 10.    We could see the speed dropping quickly but we constrained ourselves to only apply a fixed proportion of accelerator based on the difference between actual speed and target speed. What we feel we needed to do is produce a lot more accelerator power than we actually needed for a short duration to counteract the sudden decrease in speed followed by a more proportioned response.  How do we relate this “gut” feeling to control theory? The answer is derivative control.

Derivative control quantifies this “need to apply more” correction by linking the amount of accelerator pedal to the “rate of change” of speed. In other words the faster the speed is dropping the more acceleration we apply. A sudden drop in speed requires a large and equally quick depression of the accelerator pedal.  Do not confuse this with the amount of speed drop. It is quite independent. It is also important to realize that on its own derivative control is not sufficient to restore the speed to 30mph. Consider if the change in speed is very slow. For example the speed may be dropping at a rate of 1mph per minute. This would produce an insignificant amount of accelerator pedal depression and even if (after 25 minutes) the speed dropped to 5mph the amount of pedal depression would still be insignificant.  We conclude that we need proportions of both elements to properly control the speed; derivative control to cope with sudden fluctuations and proportional to bring it back from large errors.

We have a very reasonable control system now which can maintain the target speed 30mph within certain limits regardless of flat or hilly roads. What we now need to examine is how close to the target are we capable of controlling the speed. Using the car example in this case is probably a little unfair in that the accuracy of the speedometer and a requirement to travel at an almost exact speed of 30mph are just not sensible. However, lets assume that is exactly what we are trying to achieve.  So, what is wrong with our current accuracy? If I were to estimate what were possible within the current control system I would say that we could hold the speed within the limits of  28 – 32 mph.  So how can we improve that.. Before I answer that lets examine the nature of the speed error.
   If we have a large difference in target and actual speed our proportional control applies a correction. If we have a sudden change in speed the derivative control helps out. However, if we only have a small fixed error the proportional element is so small that it is ineffective and because there is no change in speed the derivative contribution is zero. So the small error persists indefinitely.  What we need here is something that increases in its contribution the longer the error , however small, exists. This is called “Integral Control”.

 Consider integral control as a constant summation function. That is, it is constantly adding up the error from the target speed and providing feedback proportional to the total rather than the error. So in our example, the constant error of even a fraction of a mph will accumulate until at some point the total will be enough to cause effective corrective action. Once again I have to emphasize that integral control on its own will not be enough to control speed within the example already described. Proportional and derivative are still essential ingredients in the mix.


















































    Having seen the benefits and limitations of each technique, which do you choose for your own control system ? Do you use just one, or perhaps two, or perhaps all ?? While it is true that there are a great many control systems that use one or two of these techniques and function perfectly well within their scope of requirements, the most useful way to use these techniques is within the scope of a three term controller i.e. proportional, integral and derivative control. This is more commonly known as PID control. We will now examine how a single control system can make use of all three techniques to achieve the required control of the target (eg speed). This topic is notoriously mathematical in most of the rigorous explanations of the subject which can exclude a great many practical engineers who were not “born again” mathematicians. We feel that this can be avoided, especially initially, in favour of a “feel” for what is going on. If maths is your forte then perhaps later sections will be of more interest. For the purposes of simplicity we will stay with the car speed control system example where the accelerator pedal is the control input and the car speed is the target output.
    When we say PID controller we are in fact saying that there is a single controlled quantity (eg speed) which can be adjusted in such a way as to minimise the observed error between required and measured values, using “feedback” proportional to each of the three PID elements. 
    The above statement contains a lot of concepts !!. By “feedback” we are saying that some function of the output (speed) error is used to correct the input (accelerator) control. i.e. a part of the output is fed back to the input. Because this feedback is intended to reduce the error in the output we call it negative feedback. This should be obvious when you consider what would happed if positive feedback was used. i.e. to correct a slight increase in speed, you pressed the accelerator further…..in no time you would be going rather fast. In fact we will see later that the accidental occurrence of positive feedback constitutes a loss of control, or instability, of the system. In the above statement we also note that there are proportional elements of each of the P,I and D elements. Put simply there are three constants that determine how much of each type are added together to give a resulting correction to the accelerator pedal. The bigger the constant , the more the control pedal reacts to a change of that nature in the speed. For example a large number multiplying the derivative part of the feedback will make the car speed very responsive to sudden changes in speed. This may help control the speed but you might find the ride a bit jumpy. A small number for this will mean that other elements, like proportional feedback, may become more needed to correct speed fluctuations. Sudden drops in speed may take a bit longer to correct in this case, but the ride should be a bit smoother ! What we need is an optimum choice for these constants in order for the speed to be controlled accurately enough for our purposes without other aspects of the system being adversely affected. This process of choosing the numbers is referred to as tuning the PID control system.
    Within this process of tuning you can see that the three term control we have chosen to focus on, can be reduced to two ,or even one, term simply by making the appropriate constants zero (or just very small). So, what happens if we get these constants wrong. At best you will have poor control of speed. The difference between target and actual speed may be quite significant for long periods of time and there may be large delays between recognising a speed difference and it being corrected. At worst you may suffer instability. Instability can create wildly varying accelerator positions and speeds, quite capable of wrecking any car. Let’s focus on the more dangerous case of instability. What is it and how is it caused ?
    In an ideal control system, any observed changes in the output will result in an instantaneous adjustment to the control input using the calculated proportions of P,I and D. In the real world there are always finite delays between observation and corrective action. If these delays become significant then they alone can cause instability. Consider a garden swing. When it comes towards you, you give it a push at the top of its travel in order to make it swing higher. To slow down you apply your force somewhere nearer the lower point of the travel. In other words, timing is crucial. In a control system applying the corrective response at the wrong time can turn negative feedback into positive This in turn would reinforce the observed variations making them bigger and bigger. Although timing is the most obvious and easy to comprehend way in which a control system can end up having positive feedback and instability ,it is by no means the only way. In general, it is possible for any feedback control system to exhibit instability due to a particular combination of control constants, feedback timing and input stimuli (changes in accelerator pedal). 
    So, where do you start in deciding the constants to apply ?. One empirical approach is to start with just proportional control (i.e. I and D constants zero) and increase the P constant until the system just starts to oscillate (i.e. continuously overshooting and undershooting the target speed) then turn up the Integral constant until the oscillations stop. This should provide smooth but relatively slow control of speed. Now turn up the derivative control until the response is just fast enough to be acceptable for the given application. Using this technique your control system will be “fairly well” tuned. About 90% of control systems in operation around the world are “fairly well” tuned. The rigorous way accurate tuning is tackled is to delve into the mathematics of transfer functions that describe the relationship of output to input and find the “poles” of instability in these functions. For the moment this is beyond our current scope. We can leave the topic of instability here with the following warnings..
   1. Be aware that your control system can suddenly become unstable due to a particular choice of PID constants
   2. A stable control system with one set of input stimuli may be unstable with others. (i.e. input range and fluctuations)



  In the previous sections we have seen how something like the speed of a car can be controlled by making adjustments to the control (accelerator) based on an observed error in the required speed. We have considered simple “bang bang” control where the accelerator is used like a switch which is either fully applied or fully off. We have seen that applying the accelerator in proportion to the speed difference helps increase accuracy of achieving the required speed (proportional control) and that adding up all of the small errors over time to adjust the accelerator helps reduce gradual drift in the speed. It was also noted how effective applying a quick jolt of power during sharp slowdown periods helped smooth out fluctuations (derivative control). Each of these techniques are important in their own way in a control system of error analysis and adjustment for achieving the objective of accurate control of the target, which, in our example case was the speed of a car.