On this series we’ll show, in a practical and intuitive way, how to model one PID ( Proportional – Integral – Derivative) controller in a way that your final equation can be easy implemented in a general purpose microcontroller (general purpose here, meaning a processor with at least hardware multiplication instructions).
When we speak about system control, is hard when a PID compensator type is not mentioned. This kind of controller is largely implemented to control the output of different systems. Utilized in closed loop systems, one controller of this type can be set to offer the desired response and maintain the system’s output with a minimum amount of error only using three setup parameters.
The Desired State signal is the desired value for which the system output should remain until it is changed again. At the system output (Actual Robot State sample signal) we have the signal of the current state of the system which may or may not be the same as desired. To do this, its current value is sent to the input and the difference between the desired state and the current state is computed, forming a third commonly called error variable, ie the difference between the desired state and the current state. The Robot Kinematics block represents the characteristic of the simulated system, usually described by differential equations, that is, the system to be controlled (an electric motor having its speed controlled, for example). Finally, we have the Controller block, which as its name suggests is a block to compensate for the (often undesirable) characteristics of the system response to be controlled. This is where the PID controller is “docked”. Now that we remember (or know) the aspect of a closed loop system, let us better understand what this PID compensator is.
The compensator PID
Knowing that a closed loop system is usually added by a compensator block to get the response and control over a variable, the reader should ask what these compensators are. In fact, there are several implementations of compensators ranging from a simple first order filter to complex arrangements with equations greater than or equal to 3. Among them we highlight the PID controller, which has up to three forms of actuation (up to three, since the others actions can be deactivated just by zeroing one of the coefficients) each with a different effect to the system, we can generalize these effects as follows:
- The proportional action (P) will provide a faster response of the system under a variation in the input signal (desired state);
- The integral action (I) aims to cancel a phenomenon known as steady state error, so that when reaching a steady state this value is what is desired in the input signal;
- The derivative action (D) has an effect of anticipating the correction of the output value of the system so that it also improves the speed of response of the system and reduces the value of the envelope (value that refers to the quantity in which the signal output is higher than desired).
The combination of the quantity of each of these three actions will cause the controller together with the plant (a common way of referring to the system in control) to provide a suitable response to a given input variation. In this way we can write the output state of the PID controller as a function of its input by the equation:
- Kp: Coefficient of proportional action;
- Ki: Coefficient of the integral action;
- Kd: Derivative action ratio;
- t: Instant of the state to be processed;
- u (t): System output signal at time t;
- e (t): Error signal at controller input at time t.
The major problem for implementing such a compensator lies in the fact that it consists of an equation involving an integral and
Let us return to equation 1:
“L” by transforming the equation above gives:
is a complex variable.
In the frequency domain (called for this case of Laplace domain) it is interesting to analyze the transfer function, ie the quotient between the output signal by the input signal, then we have:
It is done, with equation 1.1 we have what is necessary to integrate with the equations of the system to be controlled (previously also “L” – transformed) just to evaluate the characteristics of the plant and to determine Kp, Ki and Kd. After this process a circuit developed from operational amplifiers may provide a simple but efficient analogue PID controller as shown in figure 2:
If we take the inverse path and obtain the transfer function of the circuit of figure 2, we arrive at an expression if not equal, very similar and of the same order as the equation obtained in 1.1. Thus, we have the analogue system ready to solve the problem (or complete) of the control system.
Okay, but how do you implement this in a microcontroller? Incidentally, in a general purpose microcontroller is it possible to implement? The answer is partly yes. Partially because a PID algorithm to have an acceptable performance the processor needs at least the hardware multiplication instruction in the next, we will derive from equation 1.1 an optimal quality approximation of a PID controller.
Implementing a numeric PID
We now know what a closed loop system is and how to perform a PID controller in the form of an electronic circuit based on operational amplifiers. Now we will understand how to turn all this theory into numbers so that our processor can execute a sequence of commands compatible with the same operation of the electronic circuit shown in figure 2.
We will observe Figure 3, which illustrates a control system where a processor (or microcontroller) takes the form of the control block responsible for performing the compensation and acting on the plant to be controlled:
Disregarding the other signals that appeared in comparison with figure 1, and looking at the controller, we saw that it takes the form of a general purpose processor, where the error signal arrives at it through an A / D conversion. The numerical value is then processed by generating another that is passed to a D / A (it can be a modulation method like PWM) where it returns to its analog form and controls the plant. Thus we know that to model a digital PID we will need to work with numerical processing.
Let’s start this session by deriving a very popular PID algorithm and using even 8-bit microcontrollers as demonstrated in ATMEL (2006). The basic idea as absurd as it may seem is to take the inverse Laplace transform and return to equation 1:
Yes, for this first demonstration to work in the time domain and with differential equations will facilitate the accounts well, observing the equation we have an integral and a derivative operation, remembering (or presenting) some classes of numerical calculation it is known that it is possible to approach both one operation over another using algebraic operations that behave similarly.
ATTENTION! From this session we will use a little more mathematics and will remember some concepts of differential and integral calculus. So we ask for a little patience from the reader (especially from the beginner) until we get to the first algorithm. It is worth the effort as a gift will come a great understanding of what are derived and integral in practice.
Starting with the proportional term, this is undoubtedly the most intuitive. Note that this can be computed only by taking the multiplication of the proportional constant by the error variable that arrives at an instant t:
Where n denotes the instant of sampling of the error variable received by the A / D converter. Note that we are still in the time domain, but now we are in a discrete domain where incoming samples are equally spaced by a difference of 1 new value. That is, we already have the hint from where to go to find the other terms of the equation. We now turn to the term derivative.
In order to approximate a derivative to a digital computer, we need to recall the concept of differentiation learned in the first calculation cells, so the derivative of a function with respect to t is defined as:
Forgetting the calculus classes now, let’s take a closer look at what this expression says, that is, a derivative is nothing more than the subtraction of a value from a function for a given value, and a previous value of this same t, where the difference between them is very close to zero or even zero. Mathematically this is not possible, but if we make this t small enough, we can get a very similar approximation to that derived by the definition, so if we want a small spacing, why not choose the sampling rate value of the A / D converter? So:
Thus, we now have an acceptable numerical value which allows us to develop the limit presented in 1.3:
We can now move to the discrete-time domain because we know that each A / D conversion result will already be spaced from one sample to another. Thus we have:
Adding the Kd gain to the equation, we finally arrive at:
And so we have just unraveled the great meaning of a derivative, in the domain of continuous or discrete time, this operation is reduced to a subtraction of the current value of the function by the previous one. What differentiates it from one to the other is that this subtraction is done when the spacing between instants is symbolically zero, and in the case of the discrete time is a value close to zero but known in time, the term t contained in the denominator has been suppressed, because in addition to the samples already being properly spaced with this time base, this value is computed at the moment we adjust the parameter Kd of our controller.
Equation 1.5 is easily implemented in any microcontroller, and actually calculates the term derivative or any other derivative. Now we have the integral term, that follows the same numerical principle, but inversely, if before we had a subtraction, in the case of the integrator we will have a summation. Let us now go to the definition of an integral, as done for the derivative, let us begin with the domain of continuous time, it is worth remembering that for this demonstration, the integral of interest only if it is defined, ie there are intervals whose values are finite:
Although it seems more frightening at first glance, we can disaggregate the sum for only two samples, since for a control system the present and previous states of an interest concern, this leads to:
In the same
Again with the term being suppressed because it is contained in Ki, so we have the integral term of the equation as:
Thus we have the integral term of the PID equation, and we simply add equations 1.2, 1.5 and 1.7 to obtain the state value that will be sent to the D / A converter as a function of the input e (t):
Comparing this equation obtained with equation 1, we now conclude that it is possible to compute a PID compensator digitally, right? A simple C program or even assembly if you want better performance could be easily implemented in a microcontroller.
and that: ti = integration time, td = Time of differentiation.
We could already implement this code in C which is used even in some PID libraries used in Arduino, and thus we have a simple and efficient implementation of a digital PID capable of attending to the control of simple systems. We can call this digital PID approximation as a rectangular approximation given the approximation method used to calculate the integral term.
In this first part, we will stop here. In Github we have put a simple example of PID using the rectangular approximation, but in the next part we will discuss more about the limitations of this technique, as well as we will approach the design of a new digital PID using the dreaded (so far!) Transformed Z, as well as tips how to approach other analogue compensators for computational implementation.
Stay tuned for the second part of this article.
ATMEL – AVR221: Discrete PID Controller-2006, avaiable in: <http://www.atmel.com/images/doc2558.pdf>.
CRENSHAW, Jack W. – Math Toolkit for Real – Time Programming – 2000.
MEYRATH, Todd – Multipurpose Analog PID Controller – 2005.
NATIONAL INSTRUMENTS – Controls Law, (link deprecated by NI):
STARR, Gregory P. – Introduction to Applied Digital Control – 2006.