Implementing Compensators in Control Programs
Background
The Euler Integration Algorithm
The Trapezoidal Integration Algorithm


Background

        There are many times when the best way to control a system is to use a lead or lag compensator.  In this lesson we will examine how you can implement a lead or lag compensator in a program written in whatever language you write the control program in.  Here is the process.

We will assume that you have a transfer function, Gc(s), and that it is given by:

Gc(s) = (s + a)/(s + b)

We assume that you have determined values for a and b in the transfer function and that you want to implement the compensator digitally.

        We want to devise an equivalent z-function that performs the same mathematical operations on a signal - or at least get as close as possible to that.  One way to find an equivalent is to start with an equivalent in the z-domain for the "s" in the s-domain - that appears in the transfer function.  Here's one way to think of that.  Consider this "algorithm" for implementing a digital compensator.  (And note that there might be other ways to implement a compensator.)

        The critical step in the algorithm is the choice of z-form to substitute for s in the expression you get for Gc(s).  That z-form is often obtained by considering a way to integrate sampled signals.  The reason for that is simple.  Integrating a continuous signal means that you are dealing with a transfer function of 1/s, and that means that you can get to a direct relationship that involves s.

        Let's start by considering how you might integrate a sampled signal.  There are two methods that we will consider, the Backward Euler and the Trapezoidal Integration Algorithms.


The Backward Euler Algorithm

        We will start with the Backward Euler.

       If we are trying to integrate an error signal, for example, we might use code like the following:

// I = Integral
I = I + T*Error;
ControlSignal = Ki*I;
This code sums up theareas of the little rectangles in the figure above when the code is embedded in a loop.  This code really implements a difference equation:

Ik = Ik-1 + T*Errork-1

Then, we can take the Z-transform of this difference equation for the integral computation.  (We will assume zero initial conditions since we are trying to determine a transfer function.)

I[z] = z-1I[z] + z-1T*Error[z]

So,

I[z](1 - z-1) = z-1T*Error[z]
and:
I[z] = z-1T*Error[z]/(1 - z-1)
and:
I[z] = Error[z]*{T/(z - 1)}

        The transfer function of this digital integrator is then given by:

Gint[z] = T/(z - 1)

Now, reflect on this result.  An analog integrator has a transfer function of 1/s.  We can develop an argument that 1/s corresponds in some way with T/(z - 1) - the transfer function of the digital integrator.  That argument is not entirely true because there are other integration algorithms that could have been used, and we will examine one of them in a moment.  However, drawing that correspondence lets us develop an algorithm for converting an analog transfer function into an equivalent sampled transfer function.

        That problem of getting an equivalent is related to another similar problem.  If you are designing a digital filter you may need to generate a digital filter.  In a phone system, for example, you might digitize a voice signal using an A/D, then run the signal through a filter with a transfer function, G[z], and finally convert the signal back into a continuous signal.  That situation is shown below.

Situations like this occur frequently.  Music CDs produce digital signals that can be filtered (as you adjust the bass or treble controls, for example) then converted to continuous signals that you can hear.  Conversion between analog and digital forms is something that is done in many different situations, and knowing how to do that can be useful.

        Here's an algorithm that can be used to do the conversion.

An example will help in understanding the process. We would have to put the computer code into a loop that runs continuously, and we would have to perform variable declarations and initializations to ensure that the program ran.  We would also have to write code to measure and/or compute the input, In

        That's the complete algorithm.  However, remember that there is no unique digital equivalent, and that the equivalent you find depends upon the integration algorithm you choose in the process.  There are other integration algorithms and we will look at one more next.


The Trapezoidal Integration Algorithm

        Consider integrating a signal digitally using the trapezoidal algorithm.  You would calculate an integral interatively using this algorithm.  We represent the signal with Signalk.  In a control system, the signal will probably be the error signal in the system, but we will do a general system here.  Here is a graph of the area computed by the expression below.

Now, if we let:
Signalk = Signal(t)

Signalk+1= Signal(t+T)

then:
Ik = Ik-1 + (T/2)*(Signalk + Signalk-1)

Finally, we can take the Z-transform of this small difference equation, and we get:

I[z] = z-1I[z] + (T/2)(Signal[z] + z-1Signal[z])

or:
I[z](1 - z-1) =  (T/2)Signal[z](1 + z-1)

so, the transfer function of the integration is:

I[z]/Signal[z] =  (T/2)(1 + z-1)/(1 - z-1)

or:
I[z]/Signal[z] =  (T/2)(z + 1)/(z - 1)

The transfer function of the integrator is a bilinear function of z.



Getting Digital Versions of Analog Compensators

        Now, assume that you have an analog compensator.  We will look at a general form:

Gc(s) = (s + a)/(s + b)

This compensator transfer function is a function of s, and we need to get a function of z that performs equivalent functions.  However, we know that an integrator has a transfer function of 1/s, and that the digital integration algorithm we examined above has a transfer function of (T/2)(z + 1)/(z - 1).  Since both of these factors perform the same function - so the argument goes - we can replace s - wherever we find an s - with the equivalent z-form.  Let's consider that carefully.  We have an argument that says the two expressions below are equivalent.

We can invert these two equivalent expressions, so that we have:

Our strategy is to replace s by the equivalent expression above.  We will do that everywhere s appears in the compensator transfer function.  If we do that, we have the following.

Simplify this to get a ratio of first degree polynomials.

Finally, combine terms.

From this last form, we can compute the difference equation.  We'll do that with an example.

Example

        Consider a compensator transfer function.  We'll assume you have designed this compensator somehow, using continuous sysem (s-domain) techniques.  Here is the transfer function we will use.

We will also need a sampling period.  Since we have a pole at s = - 17.3, we have a time constant on the order of .05 sec.  That proably means that we need a sampling period, T, of .02 sec. or less.  We will assume that T = 0.02 sec.  If we do that, then we will have:

Combining terms, this leads to the result:

Now, we have to remember that this transfer function is the ratio of the transform of the output to the transform of the input.  That means we have:

And, from this we can write a different equation:

CompOutk = [2.114*CompInk + .886*CompInk-1 - .654*CompOutk-1]/2.346

Then, the pseudo-code you would use is:

CompInLast = CompIn;
CompIn = some expression from the system  (Error?);
CompOut = [2.114*CompIn + .886*CompInLast - .654*CompOut]/2.346;


Problems