lecture 14: discrete control€¦ · lecture 14: discrete control ... continuous discrete-time...

135
Lecture 14: Discrete Control [Chapter: Sequential Control + These Slides] Discrete Event Systems State Machine-Based Formalisms Statecharts Grafcet Laboratory 2 Petri Nets Implementation Not covered in the lecture. Homework. 1

Upload: dangque

Post on 21-May-2018

224 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Lecture 14: Discrete Control

[Chapter: Sequential Control + These Slides]

• Discrete Event Systems

• State Machine-Based Formalisms

• Statecharts

• Grafcet

• Laboratory 2

• Petri Nets

• Implementation

– Not covered in the lecture. Homework.1

Page 2: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Discrete Event Systems

Definition:

A Discrete Event System (DES) is a discrete-state, event-drivensystem, that is its state evolution depends entirely on the occurrence

of asynchronous discrete events over time.

Sometimes the name Discrete Event Dynamic System (DEDS)

is used to emphasize the dynamic nature of DES.

2

Page 3: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

DES:

1. The state space is a discrete set.

2. The state transition mechanism is event-driven.

3. The events need not to be synchronized by, e.g., a clock.

Continuous systems:

1. Continuous-state systems (real-valued variables)

2. The state-transition mechanism is time-driven.

Continuous discrete-time systems x(k+1) = Ax(k)+Bu(k) canbe viewed as an event-driven system synchronized by clockevents.

3

Page 4: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Continuous System

State trajectory is the solution of a differential equation

x(t) = f (x(t),u(t), t)

x(t)

t

X = R

4

Page 5: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Discrete Event System

State trajectory (sample path) is piecewise constant functionthat jumps from one value to another when an event occurs.

x(t)

t

X = (s1, s2, s3, s4, s5, s6)

s1

s2

s3

s4

s5

s6

t1 t2 t3 t4 t5 t6 t7

e1 e2 e3 e4e5 e6 e75

Page 6: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Discrete Control Systems

All processes contain discrete elements:

• continuous processes with discrete sensors and/or actua-tors

• discrete processes

– manufacturing lines, elevators, traffic systems, ...

• mode changes

– manual/auto, startup/shutdown

– production (grade) changes

• alarm and event handling

6

Page 7: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Discrete Logic

• Larger in volume than continuous control

• Very little theoretical support

– verification, synthesis

– formal methods beginning to emerge

– still not widespread in industry

7

Page 8: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Basic Elements

• Boolean (binary) signals – 0, 1,f alse, true, a, a

• expressionsa

a or b (a + b)

a and b (a b)

Boolean algebra

Truth valuesTruth value tables

• events

a

^a

a8

Page 9: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Logic Nets

• Combinatorial nets

– outputs = f(inputs)

– interlocks, "förreglingar"

• Sequence nets

– newstate = f(state,inputs)

– outputs = g(state,inputs)

– state machines

– automata

Asynchronous nets or synchronous (clocked) nets

Logic net

Inputs Outputs

New stateState

Delay9

Page 10: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

• Discrete Event Systems

• State Machine-Based Formalisms

• Statecharts

• Grafcet

• Laboratory 2

• Petri Nets

• Implementation

– Not covered in the lecture. Homework.

10

Page 11: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

State Machines

Formal properties [ analysis possible in certain cases

Using state machines is often a good way to structure code.

Systematic ways to write automata code often not taught inprogramming courses.

11

Page 12: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Moore Machine

State−0 State−1

State−2

Out−a

Out−c

In−a

In−a

In−b

In−c

In−b

In−a

Out−b

State transitions in response to input events

Output events (actions) associated with states12

Page 13: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Mealy Machine

State−0 State−1

State−2

Out−a

In−b

In−a

Out−b

In−c

Out−c

In−b

Out−a

In−a

Out−b

In−c

Out−c

Output events (actions) associated with input events 13

Page 14: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

State Machine Extensions

Ordinary state machines lack structure

Extensions needed to make them practically useful

• hierarchy

• concurrency

• history (memory)

14

Page 15: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

• Discrete Event Systems

• State Machine-Based Formalisms

• Statecharts

• Grafcet

• Laboratory 2

• Petri Nets

• Implementation

– Not covered in the lecture. Homework.

15

Page 16: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Statecharts

D. Harel, 1987

Statecharts =

• state machine

• hierarchy

• concurrency

• history

16

Page 17: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Statechart Syntax

D

A

C

B

c (P)

a / e

b

d

XOR Superstate

Input event

Output event

State

Condition"guard"

17

Page 18: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Statecharts Concurrency

AND Superstates:

Y

A D

B

C

E

G

Fa b (in G)

c

d

a

g

Y is the orthogonal product of A and D

When in state (B,F) and event a occurs, the system transferssimultaneously to (C,G).

18

Page 19: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

History Arrows

Alarm

Off

On

d d

H

a

On event ‘a’ the last visited state within D becomes active.

19

Page 20: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Syntax

Interfaces for AND Superstates:

A D

B

C

E

G

F

JH

K L

H

ν δ η(in B)

ǫβθαω

20

Page 21: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

• δ exit from J [ (B, E)

• α exit from K [ (C, F)

• ν exit from J [ (B, F)

• β exit from L[ (C,most recently visited state in D)

• ω exit from (B,G) [ K

• η exit from (B, F) [ H

• θ exit from (C,D) [ K

• ǫ exit from (A,D) [ L

21

Page 22: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Statechart Tools

Statecharts popular for modeling, simulation, and code generation

Used to represent state-transition diagrams in UML tools (Ratio-nal/Rose, Rhapsody, ...)

Stateflow for Matlab/Simulink

22

Page 23: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Statechart Semantics

Unfortunately, Harel only gave an informal definition of thesemantics

As a results a number of competing semantics were defined.

In 1996, Harel presented his semantics (the Statemate seman-tics) of Statechart and compared with 11 other semantics.

The lack of a single semantics is still the major problem withStatecharts

Each tool vendor defines his own.

23

Page 24: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

• Discrete Event Systems

• State Machine-Based Formalisms

• Statecharts

• Grafcet

• Laboratory 2

• Petri Nets

• Implementation

– Not covered in the lecture. Homework.

24

Page 25: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Grafcet

Extended state machine formalism for implementation ofsequence control

Industrial name: Sequential Function Charts (SFC)

Defined in France in 1977 as a formal specification andrealization method for logical controllers

Part of IEC 61131-3 (industry standard for PLC controllers)

25

Page 26: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Basic elements

Steps:

• active or inactive

S1

S1.x = 1 when active

S1.T = number of time units since the step last became active

Initial step

Transitions ("övergång"):

condition true and/or event occurred +previous step active

26

Page 27: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Control structures

Alternative paths:

• branches

a a

mutually exclusive

• repetition

27

Page 28: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Parallel paths:

join (synchronization)

split

28

Page 29: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Legal GrafcetIllegal Grafcet

29

Page 30: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Semantics

1. The initial step(s) is active when the function chart isinitiated.

2. A transition is fireable if:

• all steps preceding the the transition are active (en-abled).

• the receptivity (transition condition and/or event) of thetransition is true

A fireable transition must be fired.

3. All the steps preceding the transition are deactivated andall the steps following the transition are activated when atransition is fired

4. All fireable transitions are fired simultaneously

5. When a step must be both deactivated and activated itremains activated without interrupt 30

Page 31: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

a = 1 or 0 a = 0

a = 1

a) Not enabled b) Enabled but not firable

c) Firable d) After the change from c)

31

Page 32: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Unreachable grafcets

32

Page 33: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Unreachable grafcets

33

Page 34: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Unreachable grafcets

34

Page 35: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Unreachable grafcets

35

Page 36: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Unsafe grafcets

36

Page 37: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Unsafe grafcets

37

Page 38: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Unsafe grafcets

38

Page 39: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Unsafe grafcets

39

Page 40: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Unsafe grafcets

?

40

Page 41: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

ActionsAction block

Action types:

• standard action (level action)

S1V

S1

V

S1V

V

V

S1

S2S2

41

Page 42: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

• stored action (impulse action)logical assignment

S1

S2

1 Unstable situation (stored actions performed)

S V = 1

S V = 0

42

Page 43: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

S1

t

V Standard

action

S1

t

V

Conditional

actionS1

t

condition

S1

cond.

t

V

S1

S2

t1

t2

S1

t1

V

S2

t2

Stored

action

C V

S V = 1

S V = 0

43

Page 44: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

S1

t

S1

t

V

S1

t

S1

t

V

Time−limitedaction L V 8 s.

8

Time−delayedaction

D V 5s.

5

44

Page 45: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Hierarchy

Macro Steps:

S1

S2

S3

S21

S22

S23

45

Page 46: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Grafcet/SFC and IEC-1131 Editors

A large number of graphical IEC 1131-3 editors are available.Generates PLC code or C-code.

46

Page 47: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

• Discrete Event Systems

• State Machine-Based Formalisms

• Statecharts

• Grafcet

• Laboratory 2

• Petri Nets

• Implementation

– Not covered in the lecture. Homework.

47

Page 48: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Laboratory 2

Sequential Control

• bead sorter process

JGrafchart - Lund University

• Grafcet/SFC graphical editor

• Grafcet/SFC run-time system

48

Page 49: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

JGrafchart

49

Page 50: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Process

Bead Sorter process

Solenoid 1 Solenoid 2

Solenoid 3

Solenoid 4

ColourSensor

BeadSensor

SORTER

Black bead compartment Yellow bead compartment

50

Page 51: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

• Discrete Event Systems

• State Machine-Based Formalisms

• Statecharts

• Grafcet

• Laboratory 2

• Petri Nets

• Implementation

– Not covered in the lecture. Homework.

51

Page 52: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Petri Nets

C.A Petri, TU Darmstadt, 1962

A mathematical and graphical modeling method.

Describe systems that are:

• concurrent

• asynchronous or synchronous

• distributed

• nondeterministic or deterministic

52

Page 53: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Petri Nets

Can be used at all stages of system development:

• modeling

• analysis

• simulation/visualization (“playing the token game”)

• synthesis

• implementation (Grafcet)

53

Page 54: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Application areas

• communication protocols

• distributed systems

• distributed database systems

• flexible manufacturing systems

• logical controller design

• multiprocessor memory systems

• dataflow computing systems

• fault tolerant systems

• ...

54

Page 55: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Introduction

A Petri net is a directed bipartite graph consisting of places Pand transitions T .

Places are represented by circles.

Transitions are represented by bars (or rectangles)

Places and transitions are connected by arcs.

In a marked Petri net each place contains a cardinal (zero orpositive integer) number of tokens of marks.

55

Page 56: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

P1

P2

P3 P4

P5 P6

P7

T1

T2

T3 T4

T5

T6

56

Page 57: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Firing rules

1. A transition t is enabled if each input place contains atleast one token.

2. An enabled transition may or may not fire.

3. Firing an enabled transition t means removing one tokenfrom each input place of t and adding one token to eachoutput place of t.

The firing of a transition has zero duration.

The firing of a sink transition (only input places) only consumestokens.

The firing of a source transition (only output places) onlyproduces tokens.

57

Page 58: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

P1

P2

P3 P4

P5 P6

P7

T1

T2

T3 T4

T5

T6

58

Page 59: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

P1

P2

P3 P4

P5 P6

P7

T1

T2

T3 T4

T5

T6

59

Page 60: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

P1

P2

P3 P4

P5 P6

P7

T1

T2

T3 T4

T5

T6

60

Page 61: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

P1

P2

P3 P4

P5 P6

P7

T1

T2

T3 T4

T5

T6

61

Page 62: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

P1

P2

P3 P4

P5 P6

P7

T1

T2

T3 T4

T5

T6

62

Page 63: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

P1

P2

P3 P4

P5 P6

P7

T1

T2

T3 T4

T5

T6

63

Page 64: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Typical interpretations of places and transitions:

Input Places Transition

Preconditions

Input data

Input signals

Resources needed

Conditions

Buffers

Event

Computation step

Signal processor

Task or job

Clause in logic

Processor

Output Places

Postconditions

Output data

Output signals

Resources needed

Conclusions

Buffers

64

Page 65: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Generalized Petri Nets

2

2

P1 P2

P3

T1

Firing rules:

1. A transition t is enabled if each input place p of t containsat least w(p,t) tokens

2. Firing a transition t means removing w(p,t) tokens fromeach input place p and adding w(t,q) tokens to eachoutput place q.

65

Page 66: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Generalized Petri Nets

2

2

P1 P2

P3

T1

Firing rules:

1. A transition t is enabled if each input place p of t containsat least w(p,t) tokens

2. Firing a transition t means removing w(p,t) tokens fromeach input place p and adding w(t,q) tokens to eachoutput place q.

66

Page 67: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Petri Net Variants

Timed Petri Nets:

Times associated with transitions or places

High-Level Petri Nets:

Tokens are structured data types (objects)

Continuous & Hybrid Petri Nets:

The markings are real numbers instead of integers

Mixed continuous/discrete systems

67

Page 68: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Analysis

Properties:

• Live: No transitions can become unfireable.

• Deadlock-free: Transitions can always be fired

• Bounded: Finite number of tokens

• ...

68

Page 69: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Analysis

Analysis methods:

• Reachability methods

– exhaustive enumeration of all possible markings

• Linear algebra methods

– describe the dynamic behaviour as matrix equations

• Reduction methods

– transformation rules that reduce the net to a simplernet while preserving the properties of interest

69

Page 70: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

The classical real-time problems

Dijkstra’s classical problems

• mutual exclusion problem

• producer-consumer problem

• readers-writers problem

• dining philosophers problem

All can be modeled by Petri Nets.

70

Page 71: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Mutual Exclusion

Waiting forcritical section

Waiting forcritical section

Executinginsidecriticalsection

Executinginsidecriticalsection

Executingoutsidecriticalsection

Executingoutsidecriticalsection

Process A Process B

Mutexsemaphore

71

Page 72: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Waiting forcritical section

Waiting forcritical section

Executinginsidecriticalsection

Executinginsidecriticalsection

Executingoutsidecriticalsection

Executingoutsidecriticalsection

Process A Process B

Mutexsemaphore

72

Page 73: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Waiting forcritical section

Waiting forcritical section

Executinginsidecriticalsection

Executinginsidecriticalsection

Executingoutsidecriticalsection

Executingoutsidecriticalsection

Process A Process B

Mutexsemaphore

73

Page 74: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Waiting forcritical section

Waiting forcritical section

Executinginsidecriticalsection

Executinginsidecriticalsection

Executingoutsidecriticalsection

Executingoutsidecriticalsection

Process A Process B

Mutexsemaphore

74

Page 75: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Waiting forcritical section

Waiting forcritical section

Executinginsidecriticalsection

Executinginsidecriticalsection

Executingoutsidecriticalsection

Executingoutsidecriticalsection

Process A Process B

Mutexsemaphore

75

Page 76: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Waiting forcritical section

Waiting forcritical section

Executinginsidecriticalsection

Executinginsidecriticalsection

Executingoutsidecriticalsection

Executingoutsidecriticalsection

Process A Process B

Mutexsemaphore

76

Page 77: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Waiting forcritical section

Waiting forcritical section

Executinginsidecriticalsection

Executinginsidecriticalsection

Executingoutsidecriticalsection

Executingoutsidecriticalsection

Process A Process B

Mutexsemaphore

77

Page 78: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Producer-Consumer

Unbounded buffer:Producer processes

Consumerprocesses

BufferRead

Write

78

Page 79: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Producer processes

Consumerprocesses

BufferRead

Write

79

Page 80: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Producer processes

Consumerprocesses

BufferRead

Write

80

Page 81: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Producer processes

Consumerprocesses

BufferRead

Write

81

Page 82: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Producer processes

Consumerprocesses

BufferRead

Write

82

Page 83: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Producer processes

Consumerprocesses

BufferRead

Write

83

Page 84: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Producer processes

Consumerprocesses

BufferRead

Write

84

Page 85: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Bounded buffer:Producer processes

Consumerprocesses

Buffer

Read

Write

Freeplaces

Fullplaces

85

Page 86: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Producer processes

Consumerprocesses

Buffer

Read

Write

Freeplaces

Fullplaces

86

Page 87: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Producer processes

Consumerprocesses

Buffer

Read

Write

Freeplaces

Fullplaces

87

Page 88: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Producer processes

Consumerprocesses

Buffer

Read

Write

Freeplaces

Fullplaces

88

Page 89: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Producer processes

Consumerprocesses

Buffer

Read

Write

Freeplaces

Fullplaces

89

Page 90: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Producer processes

Consumerprocesses

Buffer

Read

Write

Freeplaces

Fullplaces

90

Page 91: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Producer processes

Consumerprocesses

Buffer

Read

Write

Freeplaces

Fullplaces

91

Page 92: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Producer processes

Consumerprocesses

Buffer

Read

Write

Freeplaces

Fullplaces

92

Page 93: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Producer processes

Consumerprocesses

Buffer

Read

Write

Freeplaces

Fullplaces

93

Page 94: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Producer processes

Consumerprocesses

Buffer

Read

Write

Freeplaces

Fullplaces

94

Page 95: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Producer processes

Consumerprocesses

Buffer

Read

Write

Freeplaces

Fullplaces

95

Page 96: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Producer processes

Consumerprocesses

Buffer

Read

Write

Freeplaces

Fullplaces

96

Page 97: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Readers-Writers

AccessControl

Ready to write

WritingReading

Readyto read

3

3

Writers processes Readers processes

97

Page 98: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

AccessControl

Ready to write

WritingReading

Readyto read

3

3

Writers processes Readers processes

98

Page 99: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

AccessControl

Ready to write

WritingReading

Readyto read

3

3

Writers processes Readers processes

99

Page 100: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

AccessControl

Ready to write

WritingReading

Readyto read

3

3

Writers processes Readers processes

100

Page 101: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

AccessControl

Ready to write

WritingReading

Readyto read

3

3

Writers processes Readers processes

101

Page 102: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

AccessControl

Ready to write

WritingReading

Readyto read

3

3

Writers processes Readers processes

102

Page 103: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

AccessControl

Ready to write

WritingReading

Readyto read

3

3

Writers processes Readers processes

103

Page 104: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

AccessControl

Ready to write

WritingReading

Readyto read

3

3

Writers processes Readers processes

104

Page 105: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

AccessControl

Ready to write

WritingReading

Readyto read

3

3

Writers processes Readers processes

105

Page 106: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

AccessControl

Ready to write

WritingReading

Readyto read

3

3

Writers processes Readers processes

106

Page 107: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

AccessControl

Ready to write

WritingReading

Readyto read

3

3

Writers processes Readers processes

107

Page 108: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

AccessControl

Ready to write

WritingReading

Readyto read

3

3

Writers processes Readers processes

108

Page 109: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

AccessControl

Ready to write

WritingReading

Readyto read

3

3

Writers processes Readers processes

109

Page 110: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

AccessControl

Ready to write

WritingReading

Readyto read

3

3

Writers processes Readers processes

110

Page 111: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Dining Philosophers

111

Page 112: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Thinks

Picksleft fork

Picks right fork

Eats

Dropsleft fork

Dropsright fork

Fork

Fork

Philosopher

112

Page 113: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Thinks

Picksleft fork

Picks right fork

Eats

Dropsleft fork

Dropsright fork

Fork

Fork

Philosopher

113

Page 114: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Thinks

Picksleft fork

Picks right fork

Eats

Dropsleft fork

Dropsright fork

Fork

Fork

Philosopher

114

Page 115: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Thinks

Picksleft fork

Picks right fork

Eats

Dropsleft fork

Dropsright fork

Fork

Fork

Philosopher

115

Page 116: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Thinks

Picksleft fork

Picks right fork

Eats

Dropsleft fork

Dropsright fork

Fork

Fork

Philosopher

116

Page 117: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Thinks

Picksleft fork

Picks right fork

Eats

Dropsleft fork

Dropsright fork

Fork

Fork

Philosopher

117

Page 118: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Thinks

Picksleft fork

Picks right fork

Eats

Dropsleft fork

Dropsright fork

Fork

Fork

Philosopher

118

Page 119: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Thinks

Picksleft fork

Picks right fork

Eats

Dropsleft fork

Dropsright fork

Fork

Fork

Philosopher

119

Page 120: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Thinks

Picksleft fork

Picks right fork

Eats

Dropsleft fork

Dropsright fork

Fork

Fork

Philosopher

120

Page 121: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Thinks

Picksleft fork

Picks right fork

Eats

Dropsleft fork

Dropsright fork

Fork

Fork

Philosopher

121

Page 122: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Thinks

Picksleft fork

Picks right fork

Eats

Dropsleft fork

Dropsright fork

Fork

Fork

Philosopher

122

Page 123: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Thinks

Eats

Leftfork

Right fork

123

Page 124: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

• Discrete Event Systems

• State Machine-Based Formalisms

• Statecharts

• Grafcet

• Laboratory 2

• Petri Nets

• Implementation

– Not covered in the lecture. Homework.

124

Page 125: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Coding State Machines

Using state machines is often a good way to structure code.

Systematic ways to write automata code often not taught inprogramming courses.

Issues:

• active or passive object

• Mealy vs Moore machines

• states with timeout events

• states with periodic activities

Often convenient to implement state machines as periodicprocesses with a period that is determined by the shortest timerequired when making a state transition.

125

Page 126: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Example: Passive state machine

The state machine is implemented as a synchronized object

public class PassiveMealyMachine {

private static final int STATE0 = 0;

private static final int STATE1 = 1;

private static final int STATE2 = 2;

private static final int INA = 0;

private static final int INB = 1;

private static final int INC = 2;

private static final int OUTA = 0;

private static final int OUTB = 1;

private static final int OUTC = 2;

private int state;

PassiveMealyMachine() {

state = STATE0;

}

private void generateEvent(int outEvent) {

// Do something

} 126

Page 127: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

public synchronized void inputEvent(int event) {

switch (state) {

case STATE0 : switch (event) {

case INA : generateEvent(OUTA);

state = STATE1;

break;

case INB : generateEvent(OUTB);

break;

default : break;

}; break;

case STATE1 : switch (event) {

case INC : generateEvent(OUTC);

state = STATE2;

break;

default : break;

}; break;

case STATE2 : switch (event) {

case INA : generateEvent(OUTB);

state = STATE0;

break;

case INC : generateEvent(OUTC);

break;

default : break;

}; break;

}

}

}

127

Page 128: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Active State Machines

The state machine could also be implemented as an activeobject (thread)

The thread object would typically contain an event-buffer (e.g.,an RTEventBuffer).

The run method would consist of an infinite loop that waits foran incoming event (RTEvent) and switches state depending onthe event.

128

Page 129: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Example: Active state machine 1

An activity is an action that is executed periodically while astate is active.

More natural to implement the state machine as a thread.

129

Page 130: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

public class ActiveMachine1 extends Thread {

private static final int STATE0 = 0;

private static final int STATE1 = 1;

private static final int STATE2 = 2;

private int state;

ActiveMachine1() {

state = STATE0;

}

private boolean cond0() {

// Returns true if condition 0 is true

}

private boolean cond1() {

}

private boolean cond2() {

}

private void action0() {

// Executes action 0

}

private void action1() {

}

private void action2() {

}130

Page 131: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

public void run() {

long t = System.currentTimeMillis();

long duration;

while (true) {

switch (state) {

case STATE0 : {

action0();

t = t + 20;

duration = t - System.currentTimeMillis();

if (duration > 0) {

try {

sleep(duration);

} catch (InterruptedException e) {}

}

if (cond0()) {state = STATE1;}

} break;

case STATE1 : {

// Similar as for STATE0. Executes action1, waits for 50 ms, checks

// cond1 and then changes to STATE2

}; break;

case STATE2 : {

// Similar as for STATE0. Executes action2, waits for 10 ms, checks

// cond2 and then changes to STATE0

}; break;

}

}

}

131

Page 132: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Comments

• Conditions tested at a frequency determined by the activityfrequencies of the different states.

• sleep() spread out in the code

132

Page 133: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Example: Active state machine 2

The thread runs at a constant (high) base frequency.

Activity frequencies multiples of the base frequency.

Conditions tested at the base frequency.

133

Page 134: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

public void run() {

long t = System.currentTimeMillis();

long duration;

int counter = 0;

while (true) {

counter++;

switch (state) {

case STATE0 : {

if (counter == 4) { counter = 0; action0();

}

if (cond0()) { counter = 0; state = STATE1;

}

}; break;

case STATE1 : {

// Similar as for STATE0. Executes action1 if counter == 10. Changes to STATE2 if cond1()

}; break;

case STATE2 : {

// Similar as for STATE0. Executes action2 if counter == 12. Changes to STATE0 if cond2()

}; break;

}

t = t + 5; // Base sampling time

duration = t - System.currentTimeMillis();

if (duration > 0) {

try {

sleep(duration);

} catch (InterruptedException e) {}

}}}

134

Page 135: Lecture 14: Discrete Control€¦ · Lecture 14: Discrete Control ... Continuous discrete-time systems x(k+1)=Ax(k)+Bu(k)can ... • Larger in volume than continuous control

Comments

• Polled time handling

• Complicated handling of counter

• Conditions tested at a high rate

135