Wescott Design Services Systems - Embedded Software - Circuits |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

Home | Capabilities | Contact Us | Articles | Books | Seminars | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

The z transform is an essential part of a structured control system design. This paper describes the basics of using the z transform to develop control systems, using the sort of math that is familiar to the accomplished embedded system designer. ## 1 IntroductionThis paper talks about closed-loop (or feedback) control systems. This is the case when you are implementing a system like the one in Figure 1. The control software combines the feedback with the command to create the drive signal. The drive signal is amplified and applied to the plant, which changes its behavior. The plant behavior is measured and read by the control software as feedback. Since this all goes in one big circle it is called "closed loop". The z transform is useful, but it only works on certain types of problems. If you can describe your plant and your controller using linear difference equations, and if the coefficients of the equations don't change from sample to sample, then your controller and plant are linear and shift-invariant, and you can use the z transform. If the problem at hand won't allow you to model the plant and controller as linear shift invariant systems you won't be able to use the z transform. While all real-world plants are nonlinear in some sense, and have parameters that vary with time to some degree, the majority of plants can be usefully modeled with linear time invariant differential equations. Once this is done the effects of sampling the input and updating the output at discrete intervals can be taken into account and a difference equation that describes the plant behavior from the software's point of view can be produced. Let's say that you've reduced the behavior of the plant to a difference equation that looks like
where n, u is the command to the DAC at sample time
_{n}n, and the various a and b are constants set by the design
of the plant, driver and ADC circuits.You can solve (1) for u using techniques very similar
to those used to solve differential equations, but the process is just as
tedious and obscure as solving differential equations, and just as error prone
and frustrating. Furthermore if you want to investigate what happens when you
add feedback to the system you would need to solve many equations like (1) over
and over again. The z transform allows you to do both of these things._{n}## 2 The z TransformJust as the Laplace transform is used to solve linear time-invariant differential equations and to deal with many common feedback control problems using continuous-time control, the z transform is used in sampled-time control to deal with linear shift-invariant difference equations. Officially, the z transform takes a sequence of numbers
X(z)
that depends on the variable z but not n. That's the transform
part: the problem is transformed from one in the sampled time domain (n),
and put it into the z domain. The z transform of x is denoted as
Z(x) and defined as
For example, if you have a signal
a you can plug it into (2)
and get^{n}
There are some complex rules about the values of The most commonly used and most important property of the z
transformation is that the z transform of a signal that is delayed by one sample
time is the z transform of the original signal times
This seems pretty simplistic, but we'll get a lot of mileage out of it. Table 1 lists some other properties of the z transform (see [1]). If you feel inclined, it's good practice to use (2) or (4) to verify each one of these properties.
## 2.1 An ExampleFigure 2 shows a motor and geartrain that we might use in a
servo system. We're driving it with a voltage, and reading the position of the
output gear with a potentiometer. Let's say that we're going to use it as our
plant. If we ignore all the nonlinearities in the system, and any difficulties
with the motor performance changing over time, and if we set
u = v, then
the difference equation that describes it might look like _{i}
You can take the u are by applying (2) as
appropriate (remember that _{n}x transforms to _{n}X(z)
and u transforms to _{n}U(z)). If you do this you
get
Notice a cool thing: We've turned the difference equation into
an algebraic equation! This one of the many things that makes the z transform so
useful. It is perfectly fair to treat
With (14) we have a general solution for (11), which applies
for If this was all that we could do with the z transform it would be a useful tool to have around, but probably not something you'd want in your toolkit for everyday use. Fortunately, the z transform goes beyond allowing easy solutions to specific difference equations. ## 2.2 Transfer FunctionsWe can take the idea of plugging in signals one step further
and express the relationship between
The function One easy and obvious thing that we can do with the transfer
function in (15) is to predict the behavior of the motor. Let's say I want to
see what the motor will do if
and
There's two ways I can go with (17). First, I can solve it by polynomial division. This is a good way to go if I want to graph the response vs. time, and it is simple to do. The result looks like this
The system response is now expressed as a polynomial in
(19) gives the system response for that particular input. This
is very useful, but you would like to know if the system will ever settle out,
or what it will settle out to. The second method that you can use is partial
fraction expansion to break things down into easy bits, followed by doing the
reverse transform by inspection. The partial fraction expansion of
You can use (20) with the transforms in Table 2 to get
When you do this it becomes immediately clear that the motor position and velocity start at zero, then the motor velocity increases with its velocity increasing asymptotically to one count per step. This is exactly what you'd expect from a motor with a constant voltage on it. In fact, for all but a few special cases there will always be
certain elements of the system response (in this case the 1 and the
0.8
## 2.3 Characteristic PolynomialThe derivation leading up to (21) points up a very important and useful property of the z transform: All of the elements in the response of the system to an input come from the system transfer function or from the input signal. Physically this happens because the system being modeled is linear. Mathematically this happens because the fractions that appear in a partial fraction expansion such as the one in (20) are the roots of the denominator polynomial of the output signal, which are in turn the roots of the denominator of the transfer function plus the roots of the denominator of the input signal. What all this means is that the roots of a transfer function's denominator polynomial will nearly always make their presence known in the output of the system. This means that to get a general idea about how the system will behave you don't have to do all the work of putting a test signal into your system to see what comes out, you only need to factor the denominator and look at the value of its roots. The usefulness of the transfer function denominator in
predicting system behavior is The Fundamental Theorem of Algebra states that any polynomial
can be factored into as many complex roots as the order of the polynomial When you raise a complex number by an integer, the magnitude of
the result is the magnitude of the original number raised by the integer and the
angle of the result is the angle of the original multiplied by the integer. This
means that if I start with 0.99 10 and raise it to the 10 ## 3 Z Transforms and SoftwareOne of the very nice things about the z transform is the ease with which you can write software from it. Since you'll be implementing controllers in software this is very important. You can do a theoretically correct job of coding from a transfer function almost by inspection. If you have a transfer function
it turns into the z domain equation
which can be converted by inspection into the difference equation
In turn this difference equation can be turned into software very quickly, as shown in Listing 1: ## 3.1 A Better FilterThe filter implemented in Listing 1 really isn't very good for
a number of reasons. First, it uses floating point, which is quite slow on many
embedded processors Finally, and most important by far, is that Listing 1 implements a transfer function of unlimited order in one step. When a transfer function is implemented by the algorithm in Listing 1 the required precision rises rapidly with the order of the denominator polynomial. A guideline to use is that the precision of your math needs to be better than the precision of your input plus the precision required to accurately express each of the roots of the denominator. As an example of this, if you have input from a 12-bit ADC that you want to run through a 2-pole low-pass filter with a double pole of 0.9 you would have a transfer function that would look like
To maintain accuracy with the code in Listing 1 you would need
at least 12 bits for the incoming data, plus 3.3 bits for each pole (to
distinguish 0.9 from 1.0). Rounding up you need a bare minimum of
12 + 7 = 19 bits � and 24 would be even nicer. Keep in mind
your pole position, however! If you need to filter down to a really low
frequency, with a pole at 0.999, say, you'll need 10 bits per pole, for
12 + 20 = 32 bits. If you wanted to use a third-order
filter, you'd need The way around this is to use the fact that any transfer
function can be broken down into a cascade of 1 If you stick to filters of no more than 2 For some simple control problems you can often get by with
1 When you factor a transfer function you should factor it into
1
If you work through the math you'll find that the resulting transfer function is
The code is in Listing 2. This code gets rid of the need to store a separate state vector for the input, and unrolls the computation. You could rearrange this even more to get rid of the temporary state should you wish to give up some understandability for execution speed.
## 3.2 The PID FilterWhat about the ubiquitous PID controller The difference equation looks like
The z transform of this is
Eliminating the state variables gives you and finally,
In (39) you can see the effect of the three gains; in (40) you can see the transfer function of the filter. ## 4 The z Transform and FeedbackSo how about using this in a control problem? Lets say that you want to use the controller described by (40) to control the plant described by (14). Control engineers like to reduce control systems to block diagrams like the one shown in Figure 3. This is an extremely simple system, which is reflected in the extremely simple diagram. What Figure 3 shows is that the only input to the controller,
I'll start by writing the expression for the plant output, x:
We know that we can get z) from
E(z):
Now substitute (42) into (41) to get: But
and solve for the transfer function of the system: (45) is a classic equation in closed-loop control theory. Now
we can go way back to (15) (for
This "simplifies" to
If the system is sampled at 250Hz and the mechanical time
constant of the motor with its load is 100 milliseconds then
We can use (49) to predict what will happen to our system as well as to design systems. With k = 0.1 and _{i}k = 0.7.
Then (49) becomes_{d}
This system has poles at ## 5 The Frequency Domain and the Z TransformSo far in my presentation I've stuck to math, and we've found some results that are mathematically valid, but not really useful enough to write home about. The last section showed us how to take a system and describe it, but it didn't show how to use a system description to arrive at a result. (50) tells us that a hypothetical system is stable, but only if the real system matches the system that we've modeled - and we all know that our models don't always match reality! We also don't know anything about how well our system will fare as the parts age, or as multiple units are manufactured with differing component tolerances. Furthermore there isn't a really good way to go from real measurements of a system to a z domain model. It is often very informative to excite a system with a unit
sine wave input and look at its response. In the
If you do all the multiplying and the fractional expansions and
whatnot you'll find that after all the transients due to the structure of
H(e)| and phase
shift equal to ^{jq}H(e)^{jq}^{10}. These numbers are called the
gain and phase of the system response (or the system gain and
phase).You can define the whole system's behavior using only its gain
and phase response. In fact, you can do a whole closed-loop system design
entirely with gain and phase responses, without ever needing a z-domain model of
the plant. This works because there are some very useful things that you can do
with gain and phase responses, not least of which is designing safe, stable
control systems with well defined behavior. This means that if you don't have a
good model of the system (or you suspect the model you have) you can take its
Look back at Figure 3, and at (45). In a single-loop feedback
control system, the overall transfer function will have a denominator that takes
the form 1 + G Now assume that you've whomped up a closed loop system that is known to be stable. Further assume that you know the plant gain and phase response. You know that since the system is stable, the poles of the system must lie within the unit circle. Your job is to design a control system that keeps those poles in the unit circle while achieving your other design goals. If the system parameters change in a continuous manner the
system poles will move in a continuous manner. This means that any condition
that will move a system pole ## 5.1 Gain and Phase MarginsTraditionally the way that you express the robustness of a
control system is by talking about its So how does this work? For the PID example above, (14) and (40) give us the loop gain:
You can scan MathCad, MatLab,
Maple, etc. In a pinch you can whip up a display program in C or C++, but
the graphing becomes a chore.Figure 4 shows the gain and phase vs. angle b,
k = 0.1/_{i}b and
k = 0.7/_{d}b. The gain magnitude crosses 1 at
= 1, where the phase is
�150^{o}, indicating a phase margin of 30^{o}, and the gain
phase crosses 180^{o} at a magnitude of about 9 and 0.3 for gain margins
of 19 and 10dB, respectively^{13}.
The first thing to notice about this result is that the phase margin is a bit too small -- as I said previously, 30� is pushing it. Notice that this system appeared to be perfectly safe when we designed it using the traditional PID methods, and its poles were comfortably inside the unit circle, yet we've already found a potential problem! This indicates that if you just used the PID tuning method you could end up with a system that goes unstable when the temperature changes, or that was stable in the lab but doesn't always build to a stable system in production. So how do we proceed? Let's look at our available tools for some clues. Figure 5 gives the Bode plot of the plant alone. Notice that the plant's phase approaches 180� pretty quickly. This is why we can't just wrap the motor with position feedback, unless we don't mind a low performance system. Figure 6 shows the Bode plot of the controller. See how the controller phase is positive for angular frequencies between about 0.4 radian/cycle and 3 radians/cycle? This extra phase makes the plant more stable.
Figure 7 shows the Bode plots of an integrator. Here you can
see why an integrator decreases stability -- it's phase is essentially 90�
for most useful frequencies. Since most systems need
Figure 8 shows the Bode plot of the differentiator. The differentiator is just about the opposite of the integrator: while the integrator drives the phase negative and does good things at DC, the differentiator drives the phase more positive, which increases stability, but it doesn't do anything at DC, and it amplifies noise (which is bad).
As you can see from these plots, if we can increase the effect
of the differentiator (or decrease the effect of the integrator and proportional
term) we should see an increase in the phase margin of the system. I used this
to tune the motor, Figure 9 shows the resulting open-loop bode plot with
b,
k = 0.004/_{i}b and
k = 0.8/_{d}b. The gain margins are now 40dB and
7dB, and the phase margin is a nice comfortable 61^{o}. What's more, the unity-gain frequency,
which gives a rough measure of the system response speed, hasn't changed all
that much, indicating that we've increased the stability of the system without
losing too much performance.
## 5.2 Time-Domain ImplicationsBode plots tell you all about system The settling time of the closed-loop system is roughly equal to the inverse of the bandwidth of the closed-loop response (usually measured from the frequency is 3dB or 6dB down from the DC value). Since the bandwidth of the loop response is strongly related to the unity gain frequency of the open loop response, this number can also be used. If the closed-loop amplitude response rises from its DC value, the time domain response to a step input will likely overshoot. If the closed-loop amplitude response is "peaky", i.e. if it has a narrow rise of more than 3dB or so the step response will "ring". In fact, if the closed-loop amplitude response falls off sharply from its maximum gain the step response will ring. All of these conditions are caused by mild to extreme lack of phase or gain margin. ## 6 FinallyThis paper has presented the very basics of control system design using the z transform. Using the z transform you can intelligently design stable, well behaved systems with performance that can be made predictable and reliable over variations in manufacturing tolerances and environmental conditions.
1: Constantine H. Houpis, Gary B. Lamont, Digital Control Systems, Theory, Hardware, Software, 1985 2: Gene H. Hostetter, Clement J. Savant, Jr., Raymond T. Stefani, Design of Feedback Control Systems, 1982 3: Tim Wescott, PID Without a PhD, , 2000 1 2 3 Because the magnitude of the transfer function as
4 Multiple roots must be counted individually, so
5 "Close to one" and "slowly" vary with the problem at hand, of course� 6Although some DSP chips can do floating point nearly as quickly as they can do integer arithmetic. This property is not common. 7 This is not the usual biquad filter, but it does essentially the same thing and I think it makes for tighter code on a general purpose processor. 8 See [3] 9 This is an example of linearization, where you're taking a non-linear system and modeling it as a linear one. 10 11 In decibels (dB). A gain in dB is equal to 20 times
the logarithm to the base 10 of the linear gain:
gain 12 The angle , which is a sinusoid with a frequency
of ^{jn}/T radians per second, or /(2T)
Hz.13 Yes, you can have more than one gain or phase
margin, because there's more than one gain or phase crossing or you can
have Copyright 2004, Tim Wescott, All Rights Reserved. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

Copyright © 2009, Wescott Design Services, Inc. All Rights Reserved. |