Вы находитесь на странице: 1из 58

Real-Time Systems

Problem-Solving Exercises
Fall 2008

Department of Automatic Control


Lund University
Sweden
Exercise 0. Control in Matlab

This exercise is intended to give a basic introduction to Matlab. The main focus will
be on the use of Control Systems Toolbox for control system analysis and design.
A short Matlab reference guide and a guide to the most commonly used commands
from Control Systems Toolbox can be found at the end of this exercise.

Getting started
Matlab is started by issuing the command

> matlab

which will bring up a Java-based interface with three different frames showing the
current directory, your defined variables, and the Matlab command window. This
gives a good overview, but may be slow on some computers.
An alternative way to start Matlab is by the command

> matlab -nodesktop

which will only open up the command window. You can then use the commands ls
and whos to examine the current directory and your defined Matlab variables.
All Matlab commands have a help text, which is displayed by typing

>> help <command>

Try for example

>> help help

Use the help command frequently in the following exercises.

Matrices and system representations


Matrices in Matlab are created with the following syntax

>> A = [1 2;3 4]

A =

1 2
3 4

with semi-colons being used to separate the lines of the matrix. The (complex con-
jugate) transpose of a matrix is written as

>> A’

ans =

1 3
2 4

3
Exercise 0. Control in Matlab

0.1 Consider the following state-space model describing the dynamics of an


inverted pendulum
   
dx  0 1 1
=
  x(t) + 
  
  u(t)
dt 1 0 0 (0.1)
 
y(t) = 0 1 x(t)
 

Enter the system matrices A, B, and C in Matlab and use the command
eig to determine the poles of the system.

In Control System Toolbox, the basic data structure is the linear time-invariant
(LTI) model. There are a number of ways to create, manipulate and analyze models
(type, e.g., help ltimodels). Some operations are best done on the LTI system, and
others directly on the matrices of the model.

0.2 Define an LTI model of the pendulum system with the command ss. Then
use the command tf to determine the transfer function of the system.

0.3 The “zero-pole-gain” (zpk) format is a numerically more robust variant of


the transfer function format. Factor the transfer function of the pendulum
into zeros and poles using the command zpk.

The command tf is used to create an LTI model from a transfer function. This is
done by specifying the coefficients of the numerator and denominator polynomials,
e.g.
>> %% Continuous transfer function G(s) = 1/(2s+1)
>> G = tf(1,[2 1])

Another convenient way to create transfer function models is by the following com-
mands
>> s = tf(’s’); % or: s = zpk(’s’);
>> G = 1/(2*s+1)

To specify a time delay, set the property InputDelay of the LTI model:
>> G.InputDelay = 0.5

To display all properties of an LTI model and their respective values, type
>> get(G)

0.4 a. Define an LTI model of the continuous-time transfer function


1
G (s) = e−1.5s (0.2)
s2 + 0.6s + 1
and use the command step to plot the step response of the system. Is the
system stable?
b. Use the commands bode and nyquist to plot the frequency response of the
system. Will the closed-loop system be stable if unit negative feedback is
applied?

4
Exercise 0. Control in Matlab

State space design example


0.5 Now return to the model of the inverted pendulum (0.1). We want to design
a state feedback controller

u(t) = − Lx(t)

such that the closed loop system gets the characteristic equation

s2 + 1.4s + 1 = 0

Use the command place to determine the state-feedback vector L.

0.6 Next, we want to design an observer

d x̂(t)
= A x̂(t) + Bu(t) + K ( y(t) − C x̂(t))
dt

such that the observer error decays according to the characteristic equation

s2 + 2.8s + 4 = 0

Use the command place to determine the observer gain K .


Hint: The observer error x̃ = x − x̂ is governed by

d x̃(t)
= ( A − K C) x̃(t)
dt

Further, note that eig( A − K C) = eig( AT − C T K T )

Connecting systems
LTI systems can be interconnected in a number of ways. For example, you may
add and multiply systems (or constants) to achieve parallel and series connections,
respectively. Assume that the system (0.2) without time delay is controlled by a PID
controller  
1
Gc (s) = K 1 + + sTd (0.3)
sTi
with K = 2, Ti = 1.5 and Td = 0.5, according to the standard block diagram to the
left in Figure 0.1:

l
uc u y +
Σ Gc Σ Gp Σ SYS1

−1 SYS2

Figure 0.1 A closed loop control system.

5
Exercise 0. Control in Matlab

0.7 Define an LTI model of the PID controller (0.3) and plot its Bode diagram.
Then find the amplitude margin and phase margin for the loop transfer
function G p (s) Gc (s) using the command margin.

There is a function feedback for constructing feedback systems. The block diagram
to the right in Figure 0.1 is obtained by the call feedback(SYS1,SYS2). Note the
sign conventions. To find the transfer function from the setpoint uc to the output y,
we identify that SYS1 is G p (s) Gc (s) and SYS2 is 1.

0.8 Compute the transfer function for the closed-loop system, both using the
feedback command and by direct computation (use minreal to simplify)
according to the formula

G p (s) Gc (s)
Gcl (s) =
1 + G p (s) Gc (s)

0.9 Plot the step response of the closed-loop system. What is the stationary gain
from uc to y (use dcgain)?

Discrete-time systems
Discrete-time systems are handled in much the same way as continuous-time sys-
tems. The main difference is that a sample time Ts must be specified when a
discrete-time LTI system is created. (The special case Ts = −1 denotes an un-
specified sample time.)

0.10 A stock inventory is described by the discrete-time system


    
1 1 0 −1   u( k) 
x( k + 1) =   x( k) + 
  
 

0 0 1 0 v( k)
 
y( k) =  1 0  x( k)

where the measured variable y( k) is the inventory at time k before any


transaction is started, the controlled variable u( k) is amount of goods or-
dered at time k, and the disturbance v( k) represents the outflow of goods.
Assume that the sample time is 1 and enter the system as an LTI model
using the command ss. Plot the step response of the inventory. Compute
the poles of the system. Can you give an interpretation?
Warning: Control System Toolbox plots the response of discrete-time sys-
tems using stairsteps rather than discrete points. This can be a bit confus-
ing at first. Proper discrete-time plots can be created using stem.

Extra problems
0.11 Using Matlab, calculate the poles of the following systems

1
G1 (s) =
s3
+ 3s2 + 3s + 1
1
G2 (s) =
(s + 1)3

6
Exercise 0. Control in Matlab

During calculations, numerical round-off errors can arise, which can lead
to changed dynamics of the system. Calculate the poles of the following
systems where one coefficient has been slightly modified.

1
G3 (s) =
s3
+ 2.99s2 + 3s + 1
1
G4 (s) =
(s + 0.99)3

(This is supposed to illustrate why the zpk-format is numerically better


than the tf-format in Matlab.)

7
Exercise 0. Control in Matlab

Quick Matlab Reference

Some Basic Commands


Note: the command syntax is case-sensitive!
help <command> display the Matlab help for <command>.
who lists all of the variables in your matlab workspace.
whos list the variables and describes their matrix size.
clear deletes all matrices from active workspace.
clear x deletes the matrix x from active workspace.
save saves all the matrices defined in the current session into
the file matlab.mat.
load loads contents of matlab.mat into current workspace.
save filename saves the contents of workspace into filename.mat
save filename x y z saves the matrices x, y and z into the file titled file-
name.mat.
load filename loads the contents of filename into current workspace; the
file can be a binary (.mat) file or an ASCII file.
diary filename saves all typed and printed text in a file
! the ! preceding any unix command causes the unix com-
mand to be executed from matlab.

Matrix commands  
 1 2
[ 1 2; 3 4] create the matrix   .

3 4
zeros(n) creates an nxn matrix whose elements are zero.
zeros(m,n) creates a m-row, n-column matrix of zeros.
ones(n) creates a n x n square matrix whose elements are 1’s
ones(m,n) creates a mxn matrix whose elements are 1’s.
ones(A) creates an m x n matrix of 1’s, where m and n are based on the
size of an existing matrix, A.
zeros(A) creates an mxn matrix of 0’s, where m and n are based on the
size of the existing matrix, A.
eye(n) creates the nxn identity matrix with 1’s on the diagonal.
A’ (complex conjugate) transpose of A
diag(V) creates a matrix with the elements of V on the diagonal.
blkdiag(A,B,C) creates block matrix with the matrices A, B, and C on the di-
agonal

8
Exercise 0. Control in Matlab

Plotting commands
plot(x,y) creates a plot of the vectors x & y.
stairs(x,y) creates a stairstep plot of the vectors x & y.
stem(x,y) creates a discrete sequence plot of vectors x & y.
semilogx(x,y) plots log(x) vs y.
semilogy(x,y) plots x vs log(y)
loglog(x,y) plots log(x) vs log(y).
grid creates a grid on the graphics plot.
title(’text’) places a title at top of graphics plot.
xlabel(’text’) writes ’text’ beneath the x-axis of a plot.
ylabel(’text’) writes ’text’ beside the y-axis of a plot.
gtext(’text’) writes text according to placement of mouse
hold on maintains the current plot in the graphics window while
executing subsequent plotting commands.
hold off turns off the ’hold on’ option.
print filename -dps writes the contents of current graphics to ’filename’ in
postscript format.

Misc. commands
length(x) returns the number elements in a vector.
size(x) returns the size m(rows) and n(columns) of matrix x.
rand returns a random number between 0 and 1.
randn returns a random number selected from a normal
distribution with a mean of 0 and variance of 1.
rand(A) returns a matrix of size A of random numbers.
fliplr(x) reverses the order of a vector. If x is a matrix,
this reverse the order of the columns in the matrix.
flipud(x) reverses the order of a matrix in the sense of
exchanging or reversing the order of the matrix
rows. This will not reverse a row vector!
reshape(A,m,n) reshapes the matrix A into an mxn matrix
from element (1,1) working column-wise.
squeeze(A) remove empty dimensions from A
A.x access element x in the struct A

9
Exercise 0. Control in Matlab

Some useful functions from Control Systems Toolbox


Do help <function> to find possible input and output arguments.
Creation and conversion of continuous or discrete time LTI models.
ltimodels - Detailed help on the various types of LTI models.
ltiprops - Detailed help on available LTI model properties.
ss - Create/convert to a state-space model.
tf - Create/convert to a transfer function model.
zpk - Create/convert to a zero/pole/gain model.
ssdata etc. - Extract data from a LTI model.
set - Set/modify properties of LTI models.
get - Access values of LTI model properties.
minreal - Minimal realization and pole/zero cancellation.

Sampling/discretization of systems.
c2d - Continuous to discrete conversion.
d2c - Discrete to continuous conversion.

Model dynamics.
pole, eig - System poles.
zero - System zeros.
pzmap - Pole-zero map.

State-space models.
ctrb, obsv - Controllability and observability matrices.

Time response.
step - Step response.
impulse - Impulse response.
initial - Response of state-space system to given initial state.
lsim - Response to arbitrary inputs (and initial state).

Frequency response.
bode - Bode plot of the frequency response.
margin - Bode plot with phase and gain margins.
allmargin - All crossover freqs. and related gain/phase/delay margins.
nyquist - Nyquist plot.

System interconnections.
+ and - - Add and subtract systems (parallel connection).
* - Multiplication of systems (series connection).
/ and \ - Division of systems (right and left, respectively).
inv - Inverse of a system.
[ ] - Horizontal/vertical concatenation of systems.
feedback - Feedback connection of two systems.

Classical design tools.


place, acker - Pole placement (state feedback or estimator).
estim - Form estimator given estimator gain.
reg - Form regulator given state-feedback and estimator gains.

LQG design tools.


lqr,dlqr - Linear-quadratic (LQ) state-feedback regulator.
lqrd - Discrete LQ regulator for continuous plant.
kalman, kalmd - Kalman estimator.
lqgreg - Form LQG regulator given LQ gain and Kalman estimator.

10
Exercise 1. Sampling of Systems

1.1 Consider the scalar system

dx
= −ax + bu, a>0
dt
y = cx

Sample the system assuming zero-order hold and the sampling interval h.
Discuss how the pole of the discrete-time system varies with the sampling
interval.

1.2 A simple model of a DC motor is given by


   
dx  0 1  0
= x+
   u
dt 0 −8 5
 
y = 1 0 x

Sample the system assuming zero-order hold and the sampling interval
h = 0.01. Check your results using the Matlab command c2d.
Hint: Use the Laplace transform method to compute eAh .

1.3 Determine the pulse-transfer function H ( z) of the system


   
 0.5 −0.2  2
x( kh + h) =   x( kh) + 
    u( kh)
0 0 1
 
y( kh) =  1 0  x( kh)

Do the calculations both by hand and on the computer.

1.4 Derive the discrete-time system corresponding to the following continuous-


time systems when a zero-order-hold circuit is used. Give the answer in the
form of a pulse transfer function. Then use the Matlab command c2d to
derive the discrete-time system numerically for h = 0.1 and h = 1.

a.
d2 y dy
2
+3 + 2y = u
dt dt
Hint: Do a partial fraction decomposition into two first-order systems: G (s) =
G1 (s) + G2 (s). Sample each system and add the results.

b.
d3 y
=u
dt3
Hint: Transfer the system into state-space form (e.g. using Matlab) before
sampling. Then use the Laplace transform method.

11
Exercise 1. Sampling of Systems

1.5 The following difference equations are assumed to describe continuous-time


systems sampled using a zero-order-hold circuit and the sampling period h.
Determine, if possible, the corresponding first-order continuous-time sys-
tems.

a.
x( kh + h) = 0.5x( kh) + 6u( kh)

b.
x( kh + h) = −0.5x( kh) + 6u( kh)

Extra problems
1.6 Consider a linearized model of a harmonic oscillator
   
dx  0 1 0
= x+
   u
dt −1 0 1
 
y = 1 0 x

Use Matlab to compute the discrete-time step response when the sampling
period is (a) h = π /2, (b) h = π /4. Compare with the continuous-time step
response.

1.7 Sample the system


1
G (s) =
s(s + 1)(s + 2)
using a zero-order-hold circuit and h = 1.

12
Exercise 2. Input-Output Models

2.1 A state-space model of an electrical DC motor is given by


   
dx  −1 0  1
= x+
   u
dt 1 0 0
  (2.1)
y = 0 1 x

Sampling the system using a zero-order hold gives the discrete-time model
   
 e−h 0  1 − e−h 
x( kh + h) = 
  x( kh) + 
   u( kh)

1 − e−h 1 h − 1 + e−h
  (2.2)
y( kh) =  0 1  x( kh)

Determine the following for the sampled system:

a. The pulse-transfer function H ( z).

b. A difference equation relating the input and the output.

c. The variation of the poles and zeros of the pulse-transfer function with the
sampling period.

2.2 A continuous-time system

dx(t)
= u(t − τ )
dt
y(t) = x(t)

is sampled using zero-order hold. Solve problems (a)–(c) by hand and then
verify your results using Matlab.

a. Sample the system assuming h = 1 and τ = 0.5 and give a state-space


model of the sampled system. What is the order of the sampled model?

b. Determine the pulse-transfer function of the sampled system.

c. Determine the poles and zeros of the sampled system.

d. Plot the pulse response of the sampled system using the Matlab command
impulse.

2.3 Consider the difference equation

y( k + 2) − 1.5y( k + 1) + 0.5y( k) = u( k + 1)

a. Write the system as a discrete-time transfer function and then convert the
system into state-space form using Matlab.

13
Exercise 2. Input-Output Models

Table 2.1 Some discrete-time functions and corresponding z-transforms.



f ( k) Z f ( k)
δ ( k) (pulse) 1
z
1 (step)
z−1
z
k (ramp)
( z − 1)2
z
ak
z− a

b. Use the state-space form from (a) to determine the output sequence when
u( k) is a unit step function, y(0) = 0.5 and y(−1) = 1. Use the fact that
 
Y ( z) = C( zI − Φ)−1 zx(0) + C( zI − Φ)−1 Γ + D U ( z)

Do the calculations by hand, using the z-transforms in Table 2.1.

Extra problems
2.4 Use the Matlab command lsim to verify the results from Problem 2.3 (b).

2.5 If β < α , then


s+β
G (s) =
s+α
is called a lead filter (i.e., it gives a phase advance). Consider the discrete-
time system
z+ b
H ( z) =
z+ a
a. Determine when it is a lead filter.

b. Simulate the step response for different pole and zero locations.

2.6 Consider the system


z+b
(1 + b)( z2 − 1.1z + 0.4)
The pole location corresponds to a continuous-time system with damping
ζ = 0.7. Simulate the system and determine the overshoot for different
values of b in the interval (−1, 1).

14
Exercise 3. State Feedback and Observers

3.1 Recall the equations for the DC-motor from Problem 2.1:
   
 e−h 0  1 − e−h 
x( k + 1) =   x( k) + 
   u( k)

1 − e−h 1 h − 1 + e−h
 
y( k) =  0 1  x( k)

a. Determine a state-feedback controller,

u( k) = − Lx( k),

such that the poles are placed in the origin (“deadbeat control”).

b. Assume that x(0) = [1 1]T . Determine the sampling interval such that the
control signal is less than one in magnitude. It can be assumed that the
largest magnitude occurs for k = 0.
Hint: Plot the control signal u(0) as a function of the sampling period h.

3.2 The system


   
 0.78 0   0.22 
x( k + 1) =   x( k) + 
   u( k)

0.22 1 0.03
 
y( k) =  0 1  x( k)

represents the normalized motor for the sampling interval h = 0.25.

a. Determine an observer in the form



x̂( k + 1) = Φ x̂( k) + Γ u( k) + K y( k) − C x̂( k)

such that the observer poles are placed in the origin (“deadbeat observer”).
Do the calculations by hand and then verify the results using the Matlab
command acker (place does not work for poles with multiplicity > 1).
Hint: For this type of observer, the dynamics of the observer error x̃ = x − x̂
is given by x̃( k + 1) = (Φ − K C) x̃( k).

b. The observer in (a) has a delay of one sample, because x̂( k) depends only
on measurements up to time k − 1. The following observer can be used to
avoid the delay:

x̂( k) = Φ x̂( k − 1) + Γ u( k − 1) + K y( k) − C(Φ x̂ ( k − 1) + Γ u( k − 1)

= ( I − K C) Φ x̂( k − 1) + Γ u( k − 1) + K y( k)

Determine an observer in this form such that the observer poles are placed
in the origin. Do the calculations by hand and then verify the results using
Matlab.
Hint: For this type of observer, the dynamics of the observer error is given
by x̃( k + 1) = (Φ − K C Φ) x̃ ( k).

15
Exercise 3. State Feedback and Observers

3.3 Consider the discrete-time system


   
 0.5 1   0.2   
x( k + 1) =   x( k) + 
   u( k) + v( k)

0.5 0.7 0.1
 
y( k) =  1 0  x( k)

where v is a constant input disturbance. Our goal is to design a controller


that has integral action.

a. Introduce the extended state vector


 
x
z=
  
v

Show that the system can be written in the form


   
 Φ Γ Γ
z( k + 1) = 
  z( k) + 
    u( k)
0 1 0
| {z } | {z }
Φe Γe
 
y( k) =  C 0  z( k)
| {z }
Ce

Then, use the Matlab command place to determine an observer



ẑ( k + 1) = Φ e ẑ( k) + Γ eu( k) + K e y( k) − Ce ẑ( k)

for the extended system. Place the observer poles in 0.4, 0.5, and 0.6.

b. The system should be controlled by an output feedback controller

u( k) = − L e ẑ( k) = − L x̂( k) − Lvv̂( k)

where x̂( k) and v̂( k) are given by the observer in (a). Use Matlab to de-
termine L such that the controller poles are placed in 0.3 ± 0.3i. Choose
Lv (think!) such that the influence of the disturbance is eliminated when
v̂ = v.

c. Show that the complete controller can be written as

ẑ( k + 1) = (Φ e − K e Ce − Γ e L e) ẑ( k) + K e y( k)
u( k) = − L e ẑ( k)

Enter the controller and the process into Matlab using the command ss.
Assume the sampling period h = 1. Simulate the response of the closed-
loop system to a step input disturbance. Is the influence of the disturbance
eliminated in steady state?
Plot the Bode diagram of the loop transfer function using margin. How
robust is the control system?

16
Exercise 3. State Feedback and Observers

Extra problems
3.4 Consider the discrete-time process
   
 0.9 0  1
x( k + 1) = 
  x( k) + 
    u( k)
1 0.7 0
 
y( k) = 0 1  x( k)

Determine a state-feedback deadbeat controller that gives unit static gain,


that is, determine L c and L in the control law

u( k) = L c uc ( k) − Lx( k)

3.5 Consider the deadbeat controller that was derived in Problem 3.1. Assume
that h = 3 and x(0) = [1 1]T . Formulate the closed-loop system and
plot the resulting discrete-time control signal. Use the Matlab command
initial. Does the system behave as expected?

3.6 Given the discrete-time system


     
 0.5 1   0.2  1
x( k + 1) =   x( k) + 
   u( k) + 
    v( k)
0.5 0.7 0.1 0
 
y( k) =  1 0  x( k)

where v is a constant disturbance. Determine controllers such that the in-


fluence of v can be eliminated in steady state in each case.

a. The state and v can be measured.

b. The state can be measured.

c. Only the output can be measured.

17
Exercise 4. Approximation of
Continuous-Time Controllers. PID Control

4.1 Assuming the sampling interval h, use the various methods below to deter-
mine discrete-time approximations of the stable transfer function
a
G (s) = , a>0
s+a

For what values of h is the discrete-time system stable? For what values of
h is the pole on the positive real axis? (What is the qualitative behavior of
the discrete-time system if the pole is on the negative real axis?)

a. Forward difference (Euler’s method)

b. Backward difference

c. Tustin’s method

d. Tustin’s method with prewarping and warping frequency ω 1 = a rad/s

4.2 A continuous-time controller is given by

1+s
G (s) =
(1 + s/10)2

Use Matlab to discretize the controller assuming h = 0.1 using the various
methods below. Plot the Bode diagram of the continuous-time controller and
the discrete-time approximations. Which approximation is the best?
Hint: help c2d. For cases that c2d cannot handle, use e.g.
>> h = 0.1;
>> z = tf(’z’,h);
>> sp = (z-1)/h; % Euler’s method
>> H = (1+sp)/(1+sp/10)^2; % discrete approximation

a. Forward difference

b. Backward difference

c. Tustin’s method

d. Ramp invariance (first-order hold)

4.3 In theory, a PID-controller is given by


 
1
U (s) = K 1+ + Td s E(s)
Ti s

a. Discretize the controller using the forward difference approximation. Can


you see any problems with the obtained difference equation?

18
Exercise 4. Approximation of Continuous-Time Controllers. PID Control

b. In practice, the derivative term is often approximated by

Td s
Td s (
1 + Td s/ N

Redo the discretization for the new transfer function. Are there any prob-
lems with the obtained difference equation?

4.4 Write pseudo-code for a PID-controller on standard, parallel form. The


controller should include anti-reset windup based on tracking with inter-
nal control signal limitation, support for manual control mode with incre-
ment/decrement action, and bumpless mode changes. The derivative action
should act on the measured signal only.

Extra problems
4.5 Given the continuous-time system
   
dx  −3 1  0
= x+
   u
dt 0 −2 1
 
y = 1 0x

a. Use Matlab to determine a continuous-time state-feedback controller u(t) =


− Lx(t) such that the characteristic polynomial of the closed-loop system is
s2 + 8s + 32.

b. Assuming h = 0.1, modify the controller using “state-feedback redesign”


(see p. 71–72 in the IFAC PB ). The suggested translation from continuous-
time L to discrete-time L̃ is
 
L̃ = L I + ( A − B L)h/2 .

We can now form three controllers:


(1) Use the continuous control u(t) = − Lx(t). The closed-loop system is
described by ẋ = ( A − B L) x.
(2) Use the discrete control u( kh) = − Lx( kh). The closed-loop system is
described x(n + 1) = (Φ − Γ L) x(n).
(3) Use the discrete control u( kh) = − L̃x( kh). The closed-loop system is
described by x(n + 1) = (Φ − Γ L̃) x(n).
Simulate the three systems for the initial state x(0) = [1 0]. Use the Matlab
command initial. Which discrete controller approximates the continuous
controller the best?

19
Exercise 5. Implementation

5.1 Consider the following citation from p. 44 in the IFAC PB :


“The choice of sampling period depends on many factors. One way to deter-
mine the sampling period is to use continuous-time arguments. The sampled
system can be approximated by the hold circuit, followed by the continuous-
time system [see Figure 5.1]. For small sampling periods, the transfer func-
tion of the hold circuit can be approximated as

1 − e−sh 1 − 1 + sh − (sh)2 /2 + ⋅ ⋅ ⋅ sh
= =1− + ⋅⋅⋅
sh sh 2

The first two terms correspond to the Taylor series expansion of exp(−sh/2).
That is, for small h, the hold can be approximated by a time delay of half a
sampling interval.”
Assume that the phase margin can be decreased by 5○ to 15○ . Show that
this implies the following rule of thumb for the sampling period h:

hω c ( 0.15 to 0.5

Here, ω c is the crossover frequency (in radians per second) of the continuous-
time loop transfer function.
(We ignore the antialiasing filter in this problem.)

Two steps

Zero−order hold
1
s
+ =

1− e−sh
s
− e−sh
s

Figure 5.1 Writing the zero-order hold as a sum of two steps.

5.2 Assume that you have designed a PD-controller with the sampling interval
h = 0.05. The controller, written in a numerically well-conditioned state-
space form, is given by
   
 0.6 0.5   0 
x( kh + h) = 
  x( kh) + 
   y( kh)

0 0.6 2.34
 
u( kh) =  0.788 −0.875  x( kh) − 1.64 y( kh)

The controller should now be implemented using fixed-point arithmetic.


The A/D and D/A converters have 10-bit resolution, such that the input
and output signals are represented by integers in the interval −512 to 511.

20
Exercise 5. Implementation

a. Assume that you use n = 3 fractional bits for the encoding of the coefficients.
Convert the controller coefficients to integers using this representation.

b. Using three fractional bits, what real values will the controller coefficients
effectively be rounded off to? Enter the original and the “rounded” state-
space models in Matlab and plot their step responses and Bode diagrams.
How similar are they?

c. Assume that 16-bit signed integers are used throughout in the control cal-
culations (even for intermediate results). Is there any risk of overflow?

5.3 A signal that is going to be sampled has the spectrum shown in Figure 5.2.
Of interest are the frequencies in the range from 0 to f1 Hz. A disturbance
has a fixed known frequency with f2 = 5 f1 .

a. In what choice of sampling intervals will the disturbance not be aliased into
the useful spectrum?

b. If using a prefilter that removes f2 , what choice of sampling interval will


give no aliasing of the useful signal?

0 f1 f2

Figure 5.2 Spectrum of the signal being sampled in Problem 5.3.

5.4 Figure 5.3 shows the Bode diagram for an inverted pendulum being con-
trolled by a regulator with the sampling interval h = 0.2 s. The phase
margin is ϕ m = 49○ and the cross-over frequency is ω c = 1.7 rad/s. Ap-
proximately how large extra delay can be introduced by the computer im-
plementation before the system becomes unstable?

Extra problems
5.5 Sketch the implementation of a Java class Servo that simulates, in real-
time, a DC servo process. The class should have the following signature:
public class Servo {
/** Initialize and start the servo simulator. The internal
"sampling" interval (in milliseconds) is T. */
public Servo(long T)

/** Set the control signal to u */


public synchronized void setU(double u)

/** Get the measurement signal y */


public synchronized double getY()
}

21
Exercise 5. Implementation

Bode Diagrams
Gm=−6.6345 dB (at 0 rad/sec), Pm=49.463 deg. (at 1.7001 rad/sec)
10

Phase (deg); Magnitude (dB)


−10

−20
−120

−140

−160

−180 0 1
10 10
Frequency (rad/sec)

Figure 5.3 Bode diagram for the system in Problem 5.4.

The process is described by the continuous-time equations

dx1 (t)
= −1368x1 (t) − 17.5x2 (t) + 175u(t)
dt
dx2 (t)
= 5000x1 (t) − 0.5x2 (t)
dt
y(t) = x2 (t)

The process should be discretized using the forward difference approxima-


tion.

5.6 A discrete-time LQG-controller with direct term is given by



x̂( k + 1 p k) = Φ x̂( k p k − 1) + Γ u( k) + K y( k) − C x̂( k p k − 1)

x̂( k p k) = x̂( k p k − 1) + K f y( k) − C x̂( k p k − 1)
u( k) = − L x̂( k p k)

where Φ , Γ , C, L, K , and K f are matrices of appropriate sizes.

a. To minimize the computational delay, the controller can be written in gen-


eral state-space form:

x̂( k + 1 p k) = Φ c x̂( k p k − 1) + Γ c y( k)
u( k) = Cc x̂( k p k − 1) + Dc y( k)

What are the expressions for the matrices Φ c , Γ c , Cc , and Dc ? (Notice that
if the process has one input and one output, Dc will be a scalar.)

b. Sketch the implementation of the Java class LQGController, which is used


in the following example.

22
Exercise 5. Implementation

public class Regul extends Thread {


public void run {
...
LQGController lqg = new LQGController();
lqg.setParameters(Phi,Gamma,C,L,K,Kf);
while (true) {
...
y = analogIn.get();
u = lqg.calculateOutput(y);
analogOut.set(u);
lqg.updateState();
...
}
}
}

You may assume that Java supports matrix data types (class Matrix) and
matrix operations (e.g., you are allowed to write y = A*x + B*u).

23
Exercise 6. Scheduling

6.1 Consider the task set below.


Task name Ti Di Ci
A 10 2 1
B 5 4 2
C 20 10 4

a. Assign priorities to the tasks according to the rate-monotonic principle.


Draw the schedule assuming worst-case conditions, i.e., the tasks are re-
leased simultaneously and the actual execution times are equal to the worst-
case execution times. Do the tasks meet their deadlines?

b. Assign priorities to the tasks according to the deadline-monotonic principle.


Draw the schedule assuming worst-case conditions. Do the tasks meet their
deadlines?

6.2 Three tasks, A, B and C should be scheduled on a single processor. The


tasks have the following characteristics:

Task name Ti Di Ci
A 3 3 0.6
B 4 4 1.2
C 5 5 1.5

The tasks do not communicate with each other.

a. What will the CPU utilization of the system be?

b. Will the tasks meet their deadlines according to the approximate analysis
in rate-monotonic scheduling theory?

c. Will the tasks meet their deadlines according to the exact analysis in rate-
monotonic scheduling theory?

6.3 Consider the following task set.

Task name Ti Di Ci
A 5 5 2
B 2 2 1

a. Assume rate-monotonic priority assignments. Is the task set schedulable?

b. Assume non-rate-monotonic priority assignments and that the priority val-


ues are unique. What is the maximum response time for task B?

c. Assume again rate-monotonic priority assignments. Also, assume that the


two tasks are controller tasks. Each task starts by sampling a measurement
signal (e.g., perform A/D conversion) and ends by actuating a control signal
(e.g., perform D/A conversion). The time for sampling and actuation can be
assumed to be negligible compared to the execution times. What are the

24
Exercise 6. Scheduling

worst-case input-output latencies (control/computational delay) for the two


tasks? What are the best-case latencies for the two tasks, assuming that
the actual execution times equal the given worst-case values?

6.4 Consider the following task set:

Task name Ti Di Ci
A 5 5 2
B 7 7 4

a. Check the schedulability of the task set using the sufficient rate-monotonic
schedulability condition.

b. Check the schedulability of the task set using the necessary and sufficient
rate-monotonic schedulability condition.

c. Show the execution trace (schedule) for the worst case (both tasks arrive
(i.e., want to start their execution) at the same time). Assume that the
actual execution times for the tasks are the same as the worst case execution
times.

d. Verify the schedulability of the task set using the EDF schedulability con-
dition.

e. Show the execution trace (schedule) for the worst case (both tasks arrive
at the same time) in the EDF case. Assume that the actual execution times
for the tasks are the same as the worst-case execution times.

Extra problems
6.5 Three tasks, A, B and C should be scheduled on a single processor using
the STORK Modula 2 kernel. The tasks have the following characteristics
(a low numeric priority value means a high priority for the task):

Task name T (ms) D (ms) C (ms) P


A 3 1 0.8 12
B 3 0.9 0.8 10
C 3 1.2 0.8 14

a. Assume that each task is implemented by a thread in STORK. Assume that


the clock tick is 1 msec, and that the time it takes to execute CreateProcess
and SetPriority is negligible compared to the execution time for the pro-
cesses.
(cont.)

25
Exercise 6. Scheduling

The processes all have the standard structure:


Procedure TaskprocessX()
VAR
t: Time;
BEGIN
SetPriority(..);
CurrentTime(t);
LOOP
...
IncTime(t,Period);
WaitUntil(t);
END;
END;

The main program looks as follows:


BEGIN
...
...
CreateProcess(TaskprocessA,1000,"A");
CreateProcess(TaskprocessB,1000,"B");
CreateProcess(TaskprocessC,1000,"C");
Wait(Terminate);
END Main.

Will the tasks meet their deadlines? Motivate your answer. (Hints: In STORK,
the main program runs as a thread with the highest priority. A thread cre-
ated with CreateProcess runs at one priority level lower that the main
program until it does a SetPriority. The time returned by CurrentTime is
given in multiples of 1 msec. Assume that the kernel is ideal and that no
other threads are running.)

b. Modify the main program so that the tasks all will meet their deadlines.
(Hint: The primitive WaitTime(1) is equivalent to sleep(1) in Java.)

6.6 In many situations, it is valuable to be able to judge the computational


requirements for implementing different control schemes. Consider the fol-
lowing situation:
A control engineer has implemented a real-time control of a flight control
system. The controller is able to stabilize the system for a broad range of op-
erating conditions, although the controller is very slow at tracking reference
signals. In an effort to improve performance, the engineer decides to include
a nonlinear feedforward term to compensate for the main nonlinearity, see
Figure 6.1.
The engineer chooses a (neural network-like) compensation on the form

M
X “
y− ȳi
”2
σi
f ( y) = wi ⋅ e (6.1)
i=1

It is straightforward to verify that evaluating the function (6.1) requires

26
Exercise 6. Scheduling

Nonlinear
Compensation
f(y)

.
u x = f(x,u) y
Initial Control Σ
Algorithm y = h(x)

Physical System

Figure 6.1 Initial control algorithm augmented with a nonlinear compensation term.

M Additions
M Subtractions
M Divisions
2M Multiplications
M Evaluations of exp( z).
The controller hardware is based on a Motorola 68040 processor working at
fc = 25 MHz. On this platform, the required operations take the following
times
Additions 4 fc−1
Subtractions 4 fc−1
Divisions 40 fc−1
Multiplications 10 fc−1
Evaluations of exp( z) 400 fc−1

a. The controller can guarantee stability provided that the extra time delay
introduced by the compensation algorithm is less than 0.1 ms. Would you
advise the engineer to implement his compensation scheme with M = 10
in the compensation (6.1)?

b. Without the compensation for the nonlinearity, the sampling time is set to
1 ms and the CPU utilization for all the tasks in the system is estimated
to be 60%. Assuming only periodic tasks and rate-monotonic scheduling,
what is the largest M for which the engineer can still guarantee that all
deadlines will be met when the compensation is included?

27
Solutions to Exercise 0. Control in Matlab

0.1 >> A = [0 1; 1 0];


>> B = [1 0]’;
>> C = [0 1];
>> D = 0;
>> eig(A)

ans =

-1
1

0.2 >> G = ss(A,B,C,D);


>> tf(G)

Transfer function:
1
–––-
s^2 - 1

0.3 >> zpk(G)

Zero/pole/gain:
1
–––––-
(s+1) (s-1)

0.4 a. >> s = tf(’s’);


>> G = 1/(s^2+0.6*s+1);
>> G.InputDelay = 1.5

Transfer function:
1
exp(-1.5*s) * –––––––-
s^2 + 0.6 s + 1

>> step(G)

As seen in the step response the system is stable.

b. >> bode(G)
>> nyquist(G)

The Nyquist curve encircles the critical point −1, which means that the
closed-loop system will be unstable.

0.5 >> p = [1 1.4 1]; % coefficients in characteristic polynomial


>> L = place(A,B,roots(p))

L =

1.4000 2.0000

28
Solutions to Exercise 0. Control in Matlab

0.6 >> p = [1 2.8 4]; % coefficients in characteristic polynomial


>> K = place(A’,C’,roots(p))’

K =

5.0000
2.8000

0.7 >> Gp = 1/(s^2+0.6*s+1);


>> Gc = 2*(1+1/(s*1.5)+0.5*s);
>> bode(Gc) % Bode diagram of controller
>> margin(Gp*Gc) % Bode diagram of loop transfer function

0.8 >> Gcl = feedback(Gp*Gc,1)

Transfer function:
1.5 s^2 + 3 s + 2
––––––––––––––-
1.5 s^3 + 2.4 s^2 + 4.5 s + 2

>> Gcl = Gp*Gc/(1+Gp*Gc)

Transfer function:
2.25 s^5 + 5.85 s^4 + 7.95 s^3 + 6.3 s^2 + 3 s
––––––––––––––––––––––––––––––
2.25 s^6 + 4.95 s^5 + 11.16 s^4 + 10.65 s^3 + 8.55 s^2 + 3 s

>> Gcl = minreal(Gp*Gc/(1+Gp*Gc))

Transfer function:
s^2 + 2 s + 1.333
–––––––––––––-
s^3 + 1.6 s^2 + 3 s + 1.333

0.9 >> step(Gcl)


>> dcgain(Gcl)

ans =
1.0000

0.10 >> H = ss([1 1;0 0],[0 -1;1 0],[1 0],0,1);


>> step(H)
>> pole(H)

ans =

1
0

For discrete-time systems, a pole in the origin represents a time delay of


one step, while a pole in 1 represents an integrator.

0.11

29
Solutions to Exercise 0. Control in Matlab

>> G1 = 1/((s+1)^3)

Transfer function:
1
––––––––––-
s^3 + 3 s^2 + 3 s + 1

>> pole(G1)

ans =

-1.0000
-1.0000 + 0.0000i
-1.0000 - 0.0000i

>> G2 = zpk(1/((s+1)^3))

Zero/pole/gain:
1
–––-
(s+1)^3

>> pole(G2)

ans =

-1.0000
-1.0000 + 0.0000i
-1.0000 - 0.0000i

>> G3 = 1/(s^3+2.99*s^2+3*s+1);
>> pole(G3)

ans =

-1.0888 + 0.2131i
-1.0888 - 0.2131i
-0.8124

>> G4 = zpk(1/((s+0.99)^3));
>> pole(G4)

ans =

-0.9900 + 0.0000i
-0.9900 - 0.0000i
-0.9900

We see that the same modification in a parameter, causes larger changes


in the dynamics in the tf-format than the zpk-format. In general, the zpk-
format is better numerically compared to the tf-format.

30
Solutions to Exercise 1. Sampling of Systems

1.1 The system is described by

ẋ = −ax + bu
y = cx

Sampling the system using Eqs. (8) from the IFAC PB gives

 b 
x( kh + h) = e−ah x( kh) + 1 − e−ah u( kh)
a
y( kh) = cx( kh)

The pole of the sampled system is located in e−ah , i.e., the pole is located
on the positive real axis and within the unit circle. For small values of h
the pole is close to 1, i.e., the discrete system behaves like an integrator.
For large values of h the pole is close to the origin, i.e., the discrete system
behaves like a one-sample time delay.

1.2 The sampled system is given by

x( kh + h) = Φ x( kh) + Γ( kh)
y( kh) = Cx( kh)

Using the Laplace transform method, we have


1 1 
 − 1     
s −1  1 s+8 1  s

s(s + 8) 
(sI − A)−1
 
=
 
 = 
  =
   

0 s+8 s(s + 8) 0 s 

0 1 


s+8

Hence
 1   
Ah −1 −1 1 8 (1 − e−8h )   1 0.00961 
Φ=e = L (sI − A) =
 =
  

0 e−8h 0 0.9231

and
Z h Z h
5 −8s )   5 1 −8h   
As  8 (1 − e   8 ( 8 (e − 1) + h)   0.0002435 
Γ= e Bds = 
  ds = 
 
5
=
  

0 0 5e−8s 8 (1 − e−8h ) 0.04805

1.3 The pulse transfer function is given by


 
1 0   
z − 0.2  2 2( z − 0.1)
H ( z) = C( zI − Φ)−1 Γ =


 
 =
z( z − 0.5) 0 z − 0.5 1 z( z − 0.5)

Matlab solution:

31
Solutions to Exercise 1. Sampling of Systems

>> Phi = [0.5 -0.2; 0 0]; % Define system matrices


>> Gamma = [2; 1];
>> C = [1 0];
>> D = 0;
>> H = ss(Phi,Gamma,C,D,1); % Define discrete-time model
>> zpk(H) % Calculate pulse transfer function

1.4 a. The transfer function of the continuous-time system is

1 1 1
G (s) = = −
(s + 1)(s + 2) s+1 s+2

Using Table 2, row 4 in the IFAC PB we get the ZOH equivalent

1 − e−h 1 − e−2h
H ( z) = −
z − e−h 2( z − e−2h )

b. The system can be transfered into state-space form by the following Matlab
commands:
>> s = zpk(’s’);
>> G = 1/s^3;
>> G = ss(G)

Hand calculations and conversion back into transfer function form give

h3 ( z2 + 4z + 1)
H ( z) =
6( z − 1)3

Using Matlab’s c2d command, we can directly convert a continuous-time


transfer function into a discrete-time transfer function:
>> s = zpk(’s’);
>> G = 1/s^3
>> H01 = c2d(G,0.1) % For h = 0.1
>> H1 = c2d(G,1) % For h = 1

0.00016667( z + 3.732)( z + 0.2679)


[ H01 ( z) =
( z − 1)3
0.16667( z + 3.732)( z + 0.2679)
H1 ( z) =
( z − 1)3

1.5 a. Assume that the continuous-time system is

dx(t)
= Ax(t) + Bu(t)
dt
We are looking for scalars A and B that fulfill


 Φ = eAh = 0.5


Zh
Γ=

 eAs B ds = 6

0

32
Solutions to Exercise 1. Sampling of Systems

From the first equation we get

ln 2
Ah = ln 0.5 [ A=−
h

The second equation then gives


Z h
B  Ah 
eAs B ds = e −1 =6
0 A

6A 12 ln 2
[ B= =
eAh −1 h
b. In this case we get the equations

Ah
 Φ = e = −0.5

Z h
Γ=
 eAs B ds = 6
0

The first equation has no (real) solution, which implies that no equivalent
first-order continuous system exists. In fact, the discrete system has a pole
on the negative real axis and will hence oscillatate, which no continuous
first-order system can do.

1.6 The sampled model becomes

x( k + 1) = Φ x( k) + Γ u( k)
y( k) = Cx( k)

where
 
 cos h sin h 
Φ(h) = 
 

− sin h cos h
 
 1 − cos h 
Γ(h) = 
 

sin h

In Matlab:
>> A = [0 1 ; -1 0]; % Define system matrices
>> B = [0 ; 1];
>> C = [1 0];
>> D = 0;
>> G = ss(A,B,C,D) % Define continuous-time system
>> H1 = c2d(G,pi/2); % Sample the oscillator
>> H2 = c2d(G,pi/4);
>> step(G,H1,H2,20) % Plot the step response of all systems until 20 s

33
Solutions to Exercise 1. Sampling of Systems

Step Response
From: U(1)
2.5

Amplitude 1.5

To: Y(1)

0.5

−0.5
2 4 6 8 10 12 14

Time (sec.)

1.7 A continuous-time pole in s = −a maps to the discrete pole z = e−ah . The


discrete system corresponding to a continuous first order system

1
G (s) = kc
s+a

is then given as
1
H ( z) = kd
z − e−ah
The gain kd of the discrete system is given by matching stationary gains,
i.e., by putting G (0) = H (1). This gives

1 − e−ah
kd = kc ⋅
a

and
kc 1 − e−ah
⋅ H ( z) =
a z − e−ah
So, for this problem we can do a partial fraction decomposition and then
sample each term. This gives with h = 1:

1 1 1 1 1 1
G (s) = = ⋅ − + ⋅
s(s + 1)(s + 2) 2 s s+1 2 s+2

1 1 1 − e−1 1 1 − e−2
H ( z) = − +
2 z−1 z − e−1 4 z − e−2

Matlab solution:
>> G = tf([1],[1 3 2 0]);
>> G = ss(G);
>> H = c2d(G,1);
>> tf(H)

34
Solutions to Exercise 2. Input-Output Models

2.1 a. The pulse transfer function is

H ( z) = C( zI − Φ)−1 Γ
 − 1  
z − e−h 0 1 − e−h
= (0 1)
−1 + e−h z − 1 h + e−h − 1
  
(0 1) z−1 0 1 − e−h
=
( z − e−h )( z − 1) 1 − e−h z − e−h h + e−h − 1

(h + e−h − 1) z + (1 − e−h − he−h )


=
( z − e−h )( z − 1)

(h + e−h − 1) z + (1 − e−h − he−h )


=
z2 − (1 + e−h ) z + e−h

b. A difference equation is obtained from

y( kh) = H ( q)u( kh) [

q2 y( kh) − (1 + e−h ) qy( kh) + e−h y( kh) =


= (h + e−h − 1) qu( kh) + (1 − e−h − he−h )u( kh)

y( kh + 2h) − (1 + e−h ) y( kh + h) + e−h y( kh) =


= (h + e−h − 1)u( kh + h) + (1 − e−h − he−h )u( kh)

c. The poles are located in z = 1 (integrator) and z = e−h . The second pole
will move from 1 to the origin when h goes from zero to infinity (see the
solution to Problem 1.1).
The zero is located in
1 − e−h − he−h
z=−
h + e−h − 1

2.2 a. We first note that τ < h. The discrete-time system in this case is given by
(see the IFAC PB Eqs. (12) and (13))

x( k + 1) = Φ x( k) + Γ 0 u( k) + Γ 1 u( k − 1)

Φ = eAh = e0 = 1
Z h−τ Z 0.5
As
Γ0 = e ds B = 1 ds 1 = 0.5
0 0
Z τ Z 0.5
A(h−τ ) As
Γ1 = e e ds B = ds = 0.5
0 0

[ x( k + 1) = x( k) + 0.5u( k) + 0.5u( k − 1)

35
Solutions to Exercise 2. Input-Output Models

The state-space model becomes


      
 x( k + 1)   1 0.5   x( k)   0.5 

 =
  
 +
   u( k) = Φ̄ x̄( k) + Γ̄ u( k)

u( k) 0 0 u( k − 1) 1
   x( k) 
 
y( k) = 1 0  
  

u( k − 1)

The sampled model is of second order.


Matlab solution:
>> G = ss(0,1,1,0)
>> G.InputDelay = 0.5
>> H = c2d(G,1)

b. The pulse-transfer function is

−0.5 −1 0.5
   
−1 z−1
H ( z) = C( zI − Φ̄) Γ̄ = ( 1 0 )
0 z 1
  
1 z 0.5 0.5
= (1 0)
z( z − 1) 0 z − 1 1
 
1 0.5 0.5( z + 1)
= ( z 0.5 ) =
z( z − 1) 1 z( z − 1)

Matlab solution:
>> zpk(H)

c. The poles are located in z = 0 (time delay) and z = 1 (integrator). The zero
is located in z = −1.
Matlab solution:
>> pole(H)
>> zero(H)

d. The pulse response is plotted using


>> impulse(H)

The result, shown to the left in Figure 2.1, is a bit confusing because of the
stairstep plot. A better plot, shown to the right in Figure 2.1, uses plot and
stem to display the continuous-time pulse response (dashed) together with
the sampled response (stem plot).

2.3 a.
z
H ( z) =
z2 − 1.5z + 0.5
>> z = tf(’z’);
>> H = z/(z^2-1.5*z+0.5);
>> H = ss(H)

36
Solutions to Exercise 2. Input-Output Models

Impulse Response Impulse Response

1 1

0.8 0.8

Amplitude
Amplitude
0.6 0.6

0.4 0.4

0.2 0.2

0 0
0 1 2 3 4 5 0 1 2 3 4 5
Time (sec) Time (sec)

Figure 2.1 The pulse response in Problem 2.2.

This yields the state-space description


   
 1.5 −0.5  1
x( k + 1) =   x( k) + 
    u( k)
1 0 0
 
y( k) =  1 0  x( k)

We note that x1 ( k) = y( k) and x2 ( k) = y( k − 1).


b. The z-transform of the input is
z
U ( z) =
z−1
The z-transform of the output sequence, taking into account the initial con-
ditions, is (see the IFAC PB p. 26)
h i
Y ( z) = C( zI − Φ)−1 zx(0) + C( zI − Φ)−1 Γ + D U ( z)

where
C( zI − Φ)−1 Γ + D = H ( z)
is the pulse transfer function. The initial state is
   
 y(0)   0.5 
x(0) =  =
  

y(−1) 1
This gives

0.5z z2 0.5z 1 2
Y ( z) = + 2
= + +
z − 0.5 ( z − 1) ( z − 0.5) z − 0.5 z − 0.5 ( z − 1)2
Inverse transformation using the table gives
 
−1 z
Z 0.5 = 0.5 ⋅ 0.5k = 0.5k+1
z − 0.5
 
−1 −1 z
Z z = 0.5k−1
z − 0.5
 
−1 −1 z
Z 2z = 2( k − 1)
( z − 1)2
Summing up, we have

y( k) = 0.5 k+1 + 0.5 k−1 + 2( k − 1)

37
Solutions to Exercise 2. Input-Output Models

2.4 Matlab code:


>> t = 0:10; % time steps
>> x0 = [0.5; 1]; % initial state
>> u = ones(1,length(t)); % input signal
>> y = lsim(H,u,t,x0) % compute response
>> stairs(t,y) % plot discrete output

2.5 a.
s+β
is lead if β <α
s+α
Consider the discrete time system

z+b
z+ a
   
eiω h + b cos ω h + b + i sin ω h
arg iω h = arg =
e +a cos ω h + a + i sin ω h
   
sin ω h sin ω h
= arctan − arctan
b + cos ω h a + cos ω h
Phase lead if
 
sin ω h sin ω h
arctan > arctan 0 < ωh < π
b + cos ω h a + cos ω h

sin ω h sin ω h
>
b + cos ω h a + cos ω h
We thus get phase lead if b < a.

b. Example of Matlab code to plot a step response:


>> a = 0.5;
>> b = 0.4;
>> H = tf([1 b],[1 a],-1); % Sample time not specified
>> figure(4)
>> step(H,10) % Plot 10 samples of the step response

2.6 A state space representation of the system is


   
1.1 −0.4 1
x( k + 1) = x+ u
1 0 0
1
y( k) = (1 b ) x( k)
b+1

Simulation of the system for b = −0.9, −0.75, −0.5, 0, 0.5 and 1 is shown in
Figure 2.2.

38
Solutions to Exercise 2. Input-Output Models

10
Output

0
0 10 20
Time

Figure 2.2 Simulation of the step response of the system in Problem E-5.6 for b = −0.9
(upper solid), −0.75 (upper dashed), −0.50 (dash-dotted), 0 (dotted), 0.5 (lower solid), and 1
(lower dashed).

39
Solutions to Exercise 3. State Feedback and
Observers

3.1 a. The characteristic polynomial of the closed-loop system is given by

det( zI − Φ + Γ L)
 
 z − e−h + l1 (1 − e−h ) l2 (1 − e−h ) 
= det  

−(1 − e−h ) + l1 (h − 1 + e−h ) z − 1 + l2 (h − 1 + e−h )
= z2 + (−1 − e−h + (1 − e−h )l1 + (h − 1 + e−h )l2 ) z

+ e−h + ( e−h − 1)l1 + (1 − e−h − he−h )l2 .

The desired characteristic polynomial is z2 . Equating the coefficients, we


get (
−1 − e−h + (1 − e−h )l1 + (h − 1 + e−h )l2 = 0
e−h + ( e−h − 1)l1 + (1 − e−h − he−h )l2 = 0
 −h −2h

[ L =  1− e −−heh 2
h(1− e )
1
−h
.
h(1− e )

b. If x(0) = [ 1 1 ]T , then

−2 + 2e−h + he−2h
u(0) = −l1 − l2 = .
h(1 − e−h )2

Figure 3.1 shows u(0) as a function of h. From the plot we see that the
control signal is less than one in magnitude if h > 2.2.

0
Output u(0)

−2

0 1 2 3 4 5
Sampling period h

Figure 3.1 Control signal u(0) as a function of h in Problem 3.1.

3.2 a. The characteristic polynomial of the observer is given by


 
 z − 0.78 k1 
det( zI − Φ + K C) = det 
 

−0.22 z − 1 + k2
= z2 + (−1.78 + k2 ) z + 0.78 − 0.78k2 + 0.22k1

40
Solutions to Exercise 3. State Feedback and Observers

The desired characteristic polynomial is z2 . Equating the coefficients, we


get (
−1.78 + k2 = 0
0.78 + 0.22k1 − 0.78k2 = 0
 T
[ K =  2.77 1.78 

Matlab solution:

>> Phi = [0.78 0; 0.22 1];


>> C = [0 1];
>> K = acker(Phi’,C’,[0 0])’

b. The characteristic polynomial of the observer is given by


 
 z − 0.78 + 0.22k1 k1 
det( zI − Φ + K C Φ) = det 
 

−0.22 + 0.22k2 z − 1 + k2
2
= z + (−1.78 + 0.22k1 + k2 ) z + 0.78 − 0.78k2

The desired characteristic polynomial is z2 . Equating the coefficients, we


get (
−1.78 + 0.22k1 + k2 = 0
0.78 − 0.78k2 = 0
 T
[ K =  3.55 1 

Matlab solution:

>> Phi = [0.78 0; 0.22 1];


>> C = [0 1];
>> K = acker(Phi’,(C*Phi)’,[0 0])’

3.3 The observer and feedback gains become


 
 0.700   

 

Ke = 

 0.730 

 , L =  −0.214 6.43  .
 
0.750

To eliminate the disturbance in steady state, choose Lv = 1.


Matlab solution:

>> Phi = [0.5 1; 0.5 0.7];


>> Gamma = [0.2; 0.1];
>> C = [1 0];

>> Phie = [Phi Gamma; zeros(1,2) 1];


>> Gammae = [Gamma; 0];
>> Ce = [C 0];

>> % (a) Observer design


>> Ke = place(Phie’, Ce’, [0.4 0.5 0.6])’;

>> % (b) State feedback design

41
Solutions to Exercise 3. State Feedback and Observers

>> L = place(Phi, Gamma, [0.3+0.3*i 0.3-0.3*i]);


>> Lv = 1;
>> Le = [L Lv];

>> % (c) Form complete controller


>> Gc = ss(Phie-Ke*Ce-Gammae*Le, Ke, Le, 0, 1); % define controller
>> Gp = ss(Phi, Gamma, C, 0, 1); % define process
>> figure(1)
>> step(Gp/(1+Gp*Gc),20) % plot input disturbance response
>> figure(2)
>> margin(Gp*Gc) % plot loop transfer function

The response to an input step disturbance and the Bode diagram of the
loop gain are shown in Figure 3.2. The disturbance is eliminated in steady
state. The system is not very robust, since the phase margin is only 19○ .

Bode Diagram
Step Response Gm = 3.8236 dB (at 1.1034 rad/sec), Pm = 19.463 deg (at 0.55708 rad/sec)
0.5 30
Magnitude (dB)

20
0.4
10

0
0.3
Amplitude

−10
225
0.2
180
Phase (deg)

135
0.1 90
45
0 0
−2 −1 0
0 5 10 15 20 10 10 10
Time (sec) Frequency (rad/sec)

Figure 3.2 The response of the closed-loop system to an input step disturbance (left) and
the Bode diagram of the loop gain (right).

3.4 We have to determine the feedback vector L such that


 
 0.9 − {1 −{2 
Φ − ΓL =  

1 0.7

has all eigenvalues in the origin. This gives the condition

det(λ I − Φ + Γ L) = λ 2 + (−1.6 + {1 )λ + 0.63 − 0.7{1 + {2 = λ 2

I.e.,
−1.6 + {1 = 0 0.63 − 0.7{1 + {2 = 0
or  
L =  1.6 0.49 

The stationary gain of the closed loop system is given by stationary gain of
the
L c ⋅ C( I − Φ + Γ L)−1 Γ = 1
To get unit steady state gain we choose L c = 1

42
Solutions to Exercise 3. State Feedback and Observers

3.5 The closed-loop system, assuming u( k) as output, is given by

x( k + 1) = (Φ − Γ L) x( k)
u( k) = − Lx( k)

>> h = 3;
>> Phi = [exp(-h) 0; 1-exp(-h) 1];
>> Gamma = [1-exp(-h); h-1+exp(-h)];
>> L = acker(Phi,Gamma,[0 0]);
>> Hcl = ss(Phi-Gamma*L,[0;0],-L,0,h);
>> x0 = [1;1];
>> [u,t] = initial(Hcl,x0,15);
>> stairs(t,u)

The control signal is non-zero only in the first two steps.

3.6 The constant disturbance v( k) can be described by the dynamical system

w( k + 1) = w( k)
v( k) = w( k)

The process can thus be described in the form given in IFAC PB p. 65, with
 
Φ w = 1, Φ xw 1

= 

0

a. If the state and v can be measured then we can use the controller

u( k) = − Lx( k) − Lww( k).

This gives the closed loop system

x( k + 1) = Φ x( k) + Φ xw w( k) − Γ Lx( k) − Γ Lww( k)
= (Φ − Γ L) x( k) + (Φ xw − Γ Lw)w( k)
y( k) = Cx( k)

In general it is not possible to totally eliminate the influence of w( k). This


is only possible if Φ xw − Γ Lw is the zero matrix. We will therefore only
consider the situation at the output in steady state

y(∞) = C[ I − (Φ − Γ L)]−1 (Φ xw − Γ Lw)w(∞) = Hw(1)w(∞)

The influence of w (or v) can be zero in steady state if

Hw(1) = 0

This will be the case if


1 − ϕ c22
Lw =
γ 1 (1 − ϕ c22 ) + γ 2ϕ c12

43
Solutions to Exercise 3. State Feedback and Observers

where ϕ ci j is the (i, j ) element of Φ − Γ L and γ i is the i:th element of Γ .


Assume that L is determined to give a deadbeat regulator then
 
L =  3.21 5.57 

and  
 −0.142 −0.114 
Φ − ΓL = 
 

0.179 0.142
and
Lw = 5.356
b. In this case is the state but not the disturbance measurable. The distur-
bance can now be calculated from the state equation
Φ xww( k − 1) = x( k) − Φ x( k − 1) − Γ u( k − 1).
The first element in this vector equation gives
w( k − 1) = [1 0]( x( k) − Φ x( k − 1) − Γ u( k − 1))
Since w( k) is constant and x( k) is measurable it is possible to calculate
ŵ( k) = w( k − 1). The following control law can now be used
u( k) = − Lx( k) − Lwŵ( k)
where Lw is the same as in (a). Compared with the controller in (a) there
is a delay in the detection of the disturbance.
c. If only the output is measurable then the state and the disturbance can be
estimated using an observer of the form
        
 x̂( k + 1)   Φ Φ xw   x̂( k)   Γ   K 

 =
  
  +
    u( k) + 
  ε ( k)ε ( k) = y( k) − C x̂( k)

ŵ( k + 1) 0 1 ŵ( k) 0 Kw
The gain vector can now be determined such that the error goes to zero
provided the augmented system is observable. The error equation is
    
 x̃( k + 1)   Φ − K C Φ xw   x̃( k) 

 =
  
 

w̃( k + 1) − Kw C 1 w̃( k)
The characteristic equation of the system matrix for the error is
z3 + ( k1 − 2.2) z2 + (1.05 − 1.7k1 + k2 + kw) z + 0.7k1 + 0.15 − 0.7kw − k2 = 0.
The eigenvalues are in the origin if
k1 = 2.2
k2 = −0.6433
kw = 3.3333.
The controller now has to be
u( k) = − L x̂( k) − Lwŵ( k)
where L and Lv are the same as in (a). The solutions above have the draw-
back that there may be an error in the output due to the disturbance if
there are small errors in the model. Figure 3.3 show that the output when
the controllers in (a), (b) and (c) are used.

44
Solutions to Exercise 3. State Feedback and Observers

0.2 2
Output

Output
0 0

−0.2 −2
0 5 10 15 0 5 10 15
2
Estimate ve
Output

0 2

−2 0
0 5 10 15 0 5 10 15
Time Time

Figure 3.3 The output of the system in Problem 3.6 for the regulators in a) (upper left), b)
(upper right) and c) (lower left and right). The estimate of v is also shown for case c). Notice
the difference in scale in the upper left curve.

45
Solutions to Exercise 4. Approximation of
Continuous-Time Controllers. PID Control

4.1 a. Euler’s method implies a translation


z−1
s′ =
h
We get
a ah
H ( z) = G (s′ ) = z−1
=
h +a z + (ah − 1)
The discrete-time pole is located in z = 1 − ah. The system is stable if the
pole is inside the unit circle. This is the case for h < a2 .
For h < 1a the pole ends up on the positive real axis, and the discrete-
time system will have a somewhat similar behavior to the continuous-time
system. For h > 1a , the pole will be on the negative real axis, and the system
will oscillate – completely unlike the continuous system.

b. The backward difference implies a translation


z−1
s′ =
zh
We get
a zah
H ( z) = G (s′ ) = z−1
=
zh +a z(1 + ah) − 1
The discrete-time pole is located in z = 1+1ah . The system is stable and the
pole is on the positive real axis for all values of h.
2 z−1
c. We use s′ = h z+1 and obtain

a ah( z + 1) ah( z + 1)
H ( z) = G (s′ ) = 2 z−1
= =
h z+1 +a 2( z − 1) + ah( z + 1) (2 + ah) z + (ah − 2)

The discrete-time pole is located in z = 22− ah


+ah . The system is stable for all
values of h, since
2−ah
2+ah < 1 \ −2 < 2 < 2 + 2ah

which holds for all positive values of a and h.


The pole ends up on the positive real axis for h < 2a .
a z−1
d. We use s′ = tan(ah/2) z+1
and obtain

a tan(ah/2)( z + 1)
H ( z) = G (s′ ) = a z−1
= =
tan(ah/2) z+1
+a ( z − 1) + tan(ah/2)( z + 1)
tan(ah/2)( z + 1)
(1 + tan(ah/2)) z + (tan(ah/2) − 1)
1−tan(ah/2)
The discrete-time pole is located in z = 1+tan(ah/2) . The system stable for all
values of h.
π
The pole ends up on the positive real axis for h < 2a .

46
Solutions to Exercise 4. Approximation of Continuous-Time Controllers. PID Control

4.2 >> s = tf(’s’);


>> G = (1+s)/(1+s/10)^2;
>> bode(G)
>> hold on
>> h = 0.1;
>> z = tf(’z’,h);

a. >> sp = (z-1)/h; % Euler’s method


>> H1 = (1+sp)/(1+sp/10)^2; % discrete approximation
>> bode(H1)

b. >> sp = (z-1)/(z*h); % backward approximation


>> H2 = (1+sp)/(1+sp/10)^2; % discrete approximation
>> bode(H2)

c. >> H3 = c2d(G,h,’tustin’);
>> bode(H3)

d. >> H4 = c2d(G,h,’foh’);
>> bode(H4)

The Tustin and ramp invariance methods give the best approximations for
this example.

4.3 a. The forward difference approximation is obtained by the transformation

z−1
s′ =
h
This gives
 
h Td ( z − 1)
U ( z) = K 1 + + E( z)
( z − 1)Ti h
 
h Td 2
( z − 1) U ( z) = K z − 1 + + ( z − 1) E( z)
Ti h

Notice that, in the time domain, on the left hand side we have terms involv-
ing u( k) and u( k + 1), while on the right hand side we have terms involving
e( k), e( k + 1) and e( k + 2). The difference equation is non-causal and thus
impossible to implement.

b. If we use the same approximation as in (a) we get:


!
Td
h h ( z − 1)
U ( z) = K 1 + + Td
E( z)
( z − 1)Ti 1 + Nh ( z − 1)
    
Td Td
( z − 1) 1 + Nh ( z − 1) U ( z) = K ( z − 1) 1 + Nh ( z − 1)
  
Td Td 2
+ Thi 1+ Nh ( z − 1) + h (z − 1) E( z)

This represents a causal difference equation. The poles are located in z = 1


(the integrator) and in z = 1 − Nh Td (the filter pole). The second pole is
unstable (i.e., outside the unit circle) if h is large or if Td is small.

47
Solutions to Exercise 4. Approximation of Continuous-Time Controllers. PID Control

4.4 y:= ADIn(ychan)


e := yref - y
D := ad * D - bd * (y - yold)
v := K*(beta*yref - y) + I + D
if mode = auto then u := sat(v,umax,umin)
else u := sat(uman,umax,umin)
DAOut(u,uchan)
I := I + (K*h/Ti)*e + (h/Tr)*(u - v)
if increment then uinc := 1
elsif decrement then
uinc := -1
else
uinc := 0
uman := uman + (h/Tm) * uinc + (h/Tr) * (u - uman)
yold := y

ad and bd are pre-calculated parameters given by the backward difference


approximation of the D-term, i.e., as
ad := Td / (Td + N*h);
bd := K*Td*N / (Td + N*h);

4.5 a. >> A = [-3 1; 0 -2];


>> B = [0; 1];
>> C = [1 0];
>> L = place(A,B,roots([1 8 32]))

L =

17.0000 3.0000

b. >> h = 0.1;
>> Ltilde = L*(eye(2)+(A-B*L)*h/2)

Ltilde =

11.9000 3.1000

>> H1 = ss(A-B*L,[0;0],C,0);
>> [Phi,Gamma]=c2d(A,B,h);
>> H2 = ss(Phi-Gamma*L,[0;0],C,0,h);
>> H3 = ss(Phi-Gamma*Ltilde,[0;0],C,0,h);
>> [y1,t1] = initial(H1,[1;0],2.5);
>> [y2,t2] = initial(H2,[1;0],2.5);
>> [y3,t3] = initial(H3,[1;0],2.5);
>> plot(t1,y1)
>> hold on
>> stem(t2,y2,’r’)
>> stem(t3,y3,’g’)

It is seen that the discrete-time controller using L̃ approximates the continuous-


time controller much better than the discrete-time controller using L.

48
Solutions to Exercise 5. Implementation

5.1
1 − e−sh 1 − 1 + sh − (sh)2 /2 + ⋅ ⋅ ⋅ sh
= =1− +⋅⋅⋅
sh sh 2
The argument of
sh
1−
2
at the cross-over frequncy, wc , is given by
wc h
arctan(− )
2
Decreasing the phase margin with 15○ (0.26 rad) gives
wc h
arctan(− ) = −0.26 [ wc h = 0.53
2
Decreasing the phase margin with 5○ (0.087 rad) gives
wc h
arctan(− ) = −0.087 [ wc h = 0.17
2

5.2 a. In Matlab, the integer coefficients can be found by, e.g.


>> A = [0.6 0.5; 0 0.6];
>> B = [0; 2.34];
>> C = [0.788 -0.875];
>> D = -1.64;
>> n = 3; % number of fractional bits
>> round(A*2^n)

ans =

5 4
0 5

>> round(B*2^n)

ans =

0
19

>> round(C*2^3)

ans =

6 -7

>> round(D*2^3)

ans =

-13

49
Solutions to Exercise 5. Implementation

b. Three fractional bits means that the coefficients will effectively be rounded
off to the nearest multiple of 1/23 = 0.125. In Matlab, the rounded coffi-
cients can be found by e.g.
>> round(A*2^3)/2^3

ans =

0.6250 0.5000
0 0.6250

The “rounded” state-space model becomes


   
 0.625 0.5   0 
x( kh + h) = 
  x( kh) + 
   y( kh)

0 0.625 2.375
 
u( kh) =  0.75 −0.875  x( kh) − 1.625 y( kh)

The step responses and Bode diagrams can be plotted by


>> h = 0.05;
>> H = ss(A,B,C,D,h);
>> A3 = round(A*2^n)/2^n;
>> B3 = round(B*2^n)/2^n;
>> C3 = round(C*2^n)/2^n;
>> D3 = round(D*2^n)/2^n;
>> H3 = ss(A3,B3,C3,D3,h)
>> step(H,H3)
>> bode(H,H3)

The systems behave quite similarly, although the fixed-point version has
too low static gain.

c. 16-bit signed integers can take values from −32768 to 32767. The input is
in the range −512 to 511. Simulated step responses (or gain computations
using the command norm) show that the gain from the input to the states
may be as large as 8.5. Since 512 ⋅ 8.5 ⋅ 23 = 34816 > 32768, there is a small
risk of overflow. A more detailed analysis would have to consider the exact
order in which the various terms in the control law are added.
Matlab code for simulating input-to-state gain:
>> Hx1 = ss(A3,B3,[1 0],0,h); % model with x1 as output
>> step(Hx1)
>> Hx2 = ss(A3,B3,[0 1],0,h); % model with x2 as output
>> step(Hx2)

5.3 Let fs = h1 be the sample frequency. The sampling theorem states that if
the continuous-time signal has a spectrum G ( f ), the sampled signal gets
the spectrum
X∞
H( f ) = G ( f + k fs ).
k=−∞

Our G ( f ) consists of useful information in − f1 < ω < f1 and a disturbance


at f = ±5 f1 .

50
Solutions to Exercise 5. Implementation

a. Without presampling filter

• If we let fs ≥ 2 ⋅ 5 ⋅ f1 , no part of the signal will be aliased. The


disturbance can be removed using a discrete-time filter on the sampled
signal.
• With 6 f1 < fs < 10 f1 , the disturbance will be aliased to f1 < f < 5 f1
according to the sample theorem sum (above) with k = 1. This means
that the disturbance will not be aliased into the useful spectrum of the
signal, and can thus be removed using a filter.
• With 4 f1 < fs < 6 f1 , the disturbance is aliased inside the useful spec-
trum and cannot be removed.
• With 3 f1 < fs < 4 f1 , the disturbance is again aliased outside the
useful spectrum and can be removed.
• With 2 f1 < fs < 3 f1 , the disturbance is aliased inside the useful spec-
trum and cannot be removed.

b. With presampling filter


The presampling filter can remove the disturbance at 5 f1 before the sam-
pling, and therefore only the useful signal at f < f1 limits the sampling
frequency. From the sampling theorem we see that fs > 2 f1 is needed.

π
5.4 The delay margin is approximately ϕ m /ω c = 49○ 180○ /1.7 = 0.50 s.

(The analysis is only approximate since we have a sampled system. An


exact analysis would require sampling the process assuming a delay τ , for-
mulating the sampled closed-loop system, and checking whether all poles
are inside the unit circle.)

5.5 Using the forward difference approximation we get

x1 (t + h) ( x1 (t) + h(−1368x1 (t) − 17.5x2 (t) + 175u(t))


x2 (t + h) ( x2 (t) + h(5000x1 (t) − 0.5x2 (t))

public class Servo {


// monitor variables
private double u = 0.0, y = 0.0;

// monitor methods
public synchronized void setU(double u) {
this.u = u;
}
public synchronized double getY() {
return y;
}
private synchronized double getU() {
return u;
}
private synchronized void setY(double y) {
this.y = y;
}

// constructor

51
Solutions to Exercise 5. Implementation

public Servo(long T) {
ServoThread st = new ServoThread(T);
st.start();
}

// internal thread
private class ServoThread extends Thread {
private long duration, T, t;
private double h, u, x1 = 0.0, x2 = 0.0, newx1, newx2;

ServoThread(long T) {
this.T = T;
h = 0.001*(double)T;
}

public void run() {


t = System.currentTimeMillis();
while(true) {
// get input
u = getU();
// compute new states
newx1 = x1 + h * (-1368.0 * x1 - 17.5 * x2 + 175.0 * u);
newx2 = x2 + h * (5000.0 * x1 - 0.5 * x2);
setY(newx2);
x1 = newx1;
x2 = newx2;

t += T;
duration = t - System.currentTimeMillis();
if (duration < 1) duration = 1;
try {
sleep(duration);
} catch (Exception e) {}
}
}
}
}

5.6 a. Eliminating the equation for x̂( k p k), we get



x̂( k + 1 p k) = Φ x̂( k p k − 1) + Γ u( k) + K y( k) − C x̂( k p k − 1)

u( k) = − L x̂( k p k − 1) + K f y( k) − C x̂( k p k − 1)
Inserting the expression for u( k) into the first equation and simplifying, we
get
x̂( k + 1 p k) = (Φ − K C − Γ L + Γ L K f C) x̂( k p k − 1) + ( K − Γ L K f ) y( k)
u( k) = (− L + L K f C) x̂ ( k p k − 1) − L K f y( k)
We thus have
Φc = Φ − K C − Γ L + Γ LK f C
Γc = K − Γ LK f
Cc = −L + LK f C
Dc = −LK f

52
Solutions to Exercise 5. Implementation

b. public class LQGController {


private Matrix Phic, Gammac, Cc, Dc;
private Matrix xhat;
private double y, preu = 0.0;

LQGController() {
// create matrices, etc.
...
}

public void setParameters(Matrix Phi, Matrix Gamma, Matrix C,


Matrix L, Matrix K, Matrix Kf) {
Phic = Phi-K*C-Gamma*L+Gamma*L*Kf*C;
Gammac = K-Gamma*L*Kf;
Cc = -L+L*Kf*C;
Dc = -L*Kf;
}

public double calculateOutput(double y) {


this.y = y;
return preu + Dc*y;
}

public void updateState() {


xhat = Phic*xhat + Gammac*y;
preu = Cc*xhat;
}
}

53
Solutions to Exercise 6. Scheduling

6.1 a. The tasks are given the following priorities:

Task name Ti Priority


A 10 Medium
B 5 High
C 20 Low

The schedule is shown in Figure 6.1. The worst-case response times of the
tasks are R A = 3, R B = 2, R C = 9, i.e., task A will not meet its deadlines.

A
0 2 4 6 8 10 12 14 16 18 20

Arrival
B
0 2 4 6 8 10 12 14 16 18 20
Completion

C
0 2 4 6 8 10 12 14 16 18 20

Figure 6.1 Schedule with rate-monotonic priority assignments.

b. The tasks are given the following priorities:

Task name Di Priority


A 2 High
B 4 Medium
C 10 Low

The schedule is shown in Figure 6.2. The worst-case response times of the
tasks are R A = 1, R B = 3, R C = 9, i.e. all tasks will meet their deadlines.

A
0 2 4 6 8 10 12 14 16 18 20

Arrival
B
0 2 4 6 8 10 12 14 16 18 20
Completion

C
0 2 4 6 8 10 12 14 16 18 20

Figure 6.2 Schedule with deadline-monotonic priority assignments.

6.2 a.
3
X Ci 0.6 1.2 1.5
U= = + + = 0.8
Ti 3 4 5
i=1

54
Solutions to Exercise 6. Scheduling

b. A sufficient condition for all the tasks to meet their deadlines is


n
X Ci
≤ n(21/n − 1).
Ti
i=1

In this case n(21/n − 1) = 0.7798 < 0.8, and we cannot conclude from this
criterion whether the tasks are schedulable or not.

c. Here A has high priority, B has medium priority, and C has low priority.
The worst-case response time R i of task i satisfies
 
X Ri
R i = Ci + Cj
Tj
∀ j ∈hp(i)

where hp(i) is the set of tasks of higher priority than i, and ⌈⋅⌉ is the ceiling
function. The equation can be solved by fix-point iterations.
Start with calculating the worst-case response time for A ( R A ): as A has
the highest priority it will not be interrupted, thus

R A = CA = 0.6.

Task B can be interrupted by task A. Start the fix-point iterations with the
initial value R1B = CB = 1.2:
 
R1B
R2B = CB + CA = CB + CA = 1.8.
TA
 
R2B
R3B = CB + CA = CB + CA = 1.8.
TA
Thus R B = 1.8.
Task C can be interrupted both by A and B. Start with the initial value
R1C = 1.5.
& ' & '
R1C R1C
R2C = CC + CA + CB = CC + CA + CB = 3.3.
TA TB
& ' & '
R2C R2C
R3C = CC + CA + CB = CC + 2CA + CB = 3.9.
TA TB
& ' & '
R3C R3C
R4C = CC + CA + CB = CC + 2CA + CB = 3.9.
TA TB

Thus R C = 3.9.
As R i ≤ Di for all i, all the tasks will meet their deadlines with rate mono-
tonic scheduling.

55
Solutions to Exercise 6. Scheduling

6.3 a. The utilization is 90%. This is above the limit (n(21/n − 1) = [ n=2 ] =
0.828) for the sufficient condition and the exact analysis must be applied.
The tasks will meet their deadlines if and only if
R i ≤ Di , ∀i
where  
X Ri
R i = Ci + Cj
Tj
∀ j ∈hp(i)

where hp(i) is the set with all tasks of higher priority than task i and ⌈ x⌉
is the ceiling function that returns the smallest integer ≥ x.
The calculation of R i is a recurrence equation that can be solved by the
iteration
X  Rn 
n+1
Ri = Ci + i
Cj , R0i = 0
Tj
∀ j ∈hp(i)
For task B the response time becomes equal to the execution time, i.e, = 1.
For task A the following results are obtained.
R0A = 0, R1A = CA = 2
 1
2 RA
R A = CA + CB = CA + CB = 3
TB
 2
RA
R3A = CA + CB = CA + 2CB = 4
TB
& '
3
R
R4A = CA + A
CB = CA + 2CB = 4
TB

Hence, the response times are smaller than the deadlines for both tasks,
and the task set is schedulable.
b. Task A has higher priority than task B. We draw the schedule for the worst-
case when both tasks are released simultaneously, at time 0. The maximum

0 1 2 3 4 5 6 7 8 9 10

Figure 6.3 Schedule

response time is obtained for the first invocation that is released at time
0 and does not become finished until time 3, i.e., the maximum response
time is 3, well above the deadline that is 2.
c. For task B with the highest priority the worst-case and the best-case input-
output latencies are both equal to the execution time, i.e. = 1. Consider
now task A. The worst case occurs when task A is preempted by task B
immediately after it has performed the sampling. Then first task B executes
for 1 time unit, then task A is resumed and continues for another time unit
until it again is preempted by task B. After yet another time unit task A
may continue and finish. The worst case latency for task A is thus equal
to 4. Using similar reasoning one can easily see that the best-case input
latency for task A is equal to 3.

56
Solutions to Exercise 6. Scheduling

6.4 a.
U = 2/5 + 4/7 = 0.9714 > 2(21/2 − 1) = 0.8284
It is not possible to prove schedulability using the sufficient RM schedula-
bility test.

b. Since task A has highest priority one immediately gets R A = CA = 2. When


calculating R B one gets the following:
4
R0B = 0, R1B = CB = 4, R2B = CB + ⌈ ⌉2 = 6
5
6
R3B = CB + ⌈ ⌉2 = 8
5
3
Since R B is larger than the deadline for task B, the task set is not schedu-
lable using rate-monotonic scheduling.

c.

A
0 5 10 15 20 25 30 35

Arrival
B
0 7 14 21 28 35
Completion

d. Since U < 1 the task set is schedulable with EDF scheduling.

e.
A
B

0 5 10 15 20 25 30 35

Here, we assume that when two processes are equally close to their dead-
lines, the task that was added to the ready queue latest has priority. This
occurs at time=30 when both tasks have 5 time units to their deadlines.
Here, task A interrupts task B. The task set would, however, be schedulable
also with the opposite policy of letting the currently executing task finish.

6.5 a. The tasks will not meet their deadlines. When all the threads have been
created and executed their SetPriority, process B will execute, calculate
the next execution time and do a waituntil. The worst case execution time
is 0.8 msec. At that time thread A will start executing. The first thing it
does is that it measures the current time which will return the same tick
as for thread B. This means that thread B and A both will try to run in the
same clock time tick. The situation is shown in Fig. 6.4.
Thread A and B will both be scheduled for execution at time t = 0, 3, 6, 9, . . ..
Thread C will be scheduled for execution at time t = 1, 4, 7, 10, . . .. The
response time of B will be 0.8 msec. The response time of A will be 1.6
msec, i.e., larger than the deadline. The response time of C will be 1.4
msec, also larger than the deadline.

57
Solutions to Exercise 6. Scheduling

C C
A A
B B

1 ms 2 ms 3 ms 4 ms 5 ms 6 ms

Figure 6.4 Clock interrupts

b. The load on the CPU is balanced if the execution of the three threads are
spread out evenly in time. A simple way of ensuring this for this simple
case is to do a simple WaitTime between the calls to CreateProcess.
BEGIN
...
...
CreateProcess(TaskprocessA,1000,"A");
WaitTime(1);
CreateProcess(TaskprocessB,1000,"B");
WaitTime(1);
CreateProcess(TaskprocessC,1000,"C");
Wait(Terminate);
END Main.

Process A will now be scheduled for execution at t = 0, 3, 6, 9, . . . process B


will be scheduled for execution at t = 1, 4, 7, 10, . . . and process C will be
scheduled for execution at t = 2, 5, 8, 11, . . . This means that all processes
will meet their deadlines.
(In the scheduling theory, this corresponds to introducing offsets to the
tasks. Introducing offsets can increase the schedulability of a task set.)

6.6 a. Evaluating M terms in the sum takes

(4M + 4M + 20M + 40M + 400M ) fc−1 = 468M fc−1

seconds.
For fc−1 = 4 ⋅ 10−8 s and M = 10, this evaluates to

0.187 ms

and stability can therefore not be guaranteed.

b. For rate monotonic scheduling, it holds that all deadlines will be met if the
CPU utilization is less than 69%.
Since the CPU utilization with the original control algorithm is 60 %, the
additional compensation may use at maximum 9% of the CPU time. We
thus have
C 468M fc−1
Ucomp = = ≤ 0.09
T 0.001
from which we obtain

M ≤4.8

Conclusion: No more than 4 terms can be included in the compensation.

58

Вам также может понравиться