+ All Categories
Home > Documents > Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12]...

Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12]...

Date post: 04-Jun-2018
Category:
Upload: buitu
View: 232 times
Download: 6 times
Share this document with a friend
134
Discrete Control Real-Time Systems, Lecture 14 Karl-Erik ˚ Arz´ en 2 March 2017 Lund University, Department of Automatic Control
Transcript
Page 1: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Discrete Control

Real-Time Systems, Lecture 14

Karl-Erik Arzen

2 March 2017

Lund University, Department of Automatic Control

Page 2: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Content

[Real-Time Control System: Chapter 12]

1. Discrete Event Systems

2. State Machine Formalisms

3. Statecharts

4. Grafcet

5. Petri Nets

6. Implementation

1

Page 3: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Discrete Event Systems

Page 4: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Discrete Event Systems

A Discrete Event System (DES) is a discrete-state, event-driven system,

that is its state evolution depends entirely on the occurrence of

asynchronous discrete events over time.

Sometimes Discrete Event Dynamic Systems (DEDS) is used to

emphasize the dynamic nature of a DES.

2

Page 5: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Discrete Event Systems

Discrete Event Systems:

• The state space is a discrete set.

• The state transition mechanism is event-driven.

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

Continuous Systems:

• Continuous-state systems (real-valued variables)

• The state-transition mechanism is time-driven.

Continuous discrete-time systems x(k + 1) = Ax(k) + Bu(k) can be

viewed as an event-driven system synchronized by clock events.

3

Page 6: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Continuous Systems

State trajectory is the solution of a differential equation

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

x(t)

t

X = R

4

Page 7: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Discrete Event System

State trajectory (sample path) is piecewise constant function that 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 e7 5

Page 8: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Discrete Control Systems

All processes contain discrete elements:

• continuous processes with discrete sensors and/or actuators;

• discrete processes:

• manufacturing lines, elevators, traffic systems;

• mode changes:

• manual/auto, startup/shutdown,

• production (grade) changes;

• alarm and event handling.

6

Page 9: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

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 10: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Basic Elements

• Boolean (binary) signals – 0, 1,

false, true, a, a

• Expressionsa

a or b (a + b)

a and b (a b)

Boolean algebra

Truth valuesTruth value tables

• Events

a

^a

a

8

Page 11: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Logic Nets

• Combinatorial nets

• outputs = f(inputs)

• interlocks, ”forreglingar”

• 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

Delay

9

Page 12: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

State Machine Formalisms

Page 13: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

State Machine

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 in

programming courses.

10

Page 14: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

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 states

11

Page 15: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

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

12

Page 16: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

State Machine Extensions

Ordinary state machines lack structure so extensions are needed to make

them practically useful:

• hierarchy;

• concurrency;

• history (memory).

13

Page 17: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Statecharts

Page 18: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Statecharts

D. Harel, 1987: Statecharts are state machines with hierarchy,

concurrency and history.

XOR superstates

D

A

C

B

c (P)

a / e

b

d

XOR Superstate

Input event

Output event

State

Condition"guard"

14

Page 19: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Statecharts

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 transfers

simultaneously to (C,G).

15

Page 20: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Statecharts

History states

Alarm

Off

On

d d

H

a

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

16

Page 21: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Statecharts

Interfaces for AND and superstates

A D

B

C

E

G

F

JH

K L

H

ν δ η(in B)

ǫβθαω

17

Page 22: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Statecharts

• δ 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

18

Page 23: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Statecharts tools

Statecharts popular for modeling, simulation, and code generation. Used

to represent state-transition diagrams in UML tools (Rational/Rose,

Rhapsody). Stateflow for Matlab/Simulink.

19

Page 24: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Statecharts semantics

Unfortunately, Harel only gave an informal definition of the semantics. As

a results a number of competing semantics were defined.

In 1996, Harel presented his semantics (the Statemate semantics) of

Statechart and compared with 11 other semantics.

The lack of a single semantics is still the major problem with Statecharts.

Each tool vendor defines his own.

20

Page 25: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Grafcet

Page 26: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Grafcet

Extended state machine formalism for implementation of sequence

control.

Industrial name: Sequential Function Charts (SFC).

Defined in France in 1977 as a formal specification and realization

method for logical controllers.

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

21

Page 27: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

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 (”overgang”)

condition true and/or event occurred +previous step active

22

Page 28: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Basic Elements

• Alternative paths: branch

a a

mutually exclusive

• Alternative paths: repetition

23

Page 29: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Basic Elements

• Parallel paths:

join (synchronization)

split

24

Page 30: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Basic Elements

Legal GrafcetIllegal Grafcet

25

Page 31: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Semantics

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

2. A transition is fireable if:

• all steps preceding the the transition are active (enabled);

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

true.

A fireable transition must be fired.

3. All the steps preceding the transition are deactivated and all the

steps following the transition are activated when a transition is fired.

4. All fireable transitions are fired simultaneously.

5. When a step must be both deactivated and activated it remains

activated without interrupt.

26

Page 32: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Semantics

a = 1 or 0 a = 0

a = 1

a) Not enabled b) Enabled but not firable

c) Firable d) After the change from c)

27

Page 33: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Semantics

Unreachable grafcet

28

Page 34: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Semantics

Unreachable grafcet

29

Page 35: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Semantics

Unreachable grafcet

30

Page 36: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Semantics

Unreachable grafcet

31

Page 37: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Semantics

Unsafe grafcet

32

Page 38: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Semantics

Unsafe grafcet

33

Page 39: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Semantics

Unsafe grafcet

34

Page 40: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Semantics

Unsafe grafcet

35

Page 41: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Semantics

Unsafe grafcet

?

36

Page 42: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Actions

Action block

Action types:

• Standard Action (Level Action)

S1V

S1

V

S1V

V

V

S1

S2S2

37

Page 43: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Actions

Action types:

• Stored Action (Impulse Action, local assignment)

S1

S2

1 Unstable situation (stored actions performed)

S V = 1

S V = 0

38

Page 44: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Summary

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

39

Page 45: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Summary

S1

t

S1

t

V

S1

t

S1

t

V

Time−limitedaction L V 8 s.

8

Time−delayedaction

D V 5s.

5

40

Page 46: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Grafcet: macro steps

Hierarchy and macro steps

S1

S2

S3

S21

S22

S23

41

Page 47: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Grafcet: editors

A large number of graphical IEC 1131-3 editors are available. Generates

PLC code or C-code.

42

Page 48: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Laboratory 2

Sequential Control: bead sorter process

JGrafchart - Lund University

• Grafcet/SFC graphical editor

• Grafcet/SFC run-time system

43

Page 49: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Jgraphchart

44

Page 50: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Process

Solenoid 1 Solenoid 2

Solenoid 3

Solenoid 4

ColourSensor

BeadSensor

SORTER

Black bead compartment Yellow bead compartment

45

Page 51: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets

Page 52: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

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.

46

Page 53: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets

Can be used at all stages of system development:

• modeling,

• analysis,

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

• synthesis,

• implementation (Grafcet).

47

Page 54: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets

Application areas:

• communication protocols,

• distributed systems,

• distributed database systems,

• flexible manufacturing systems,

• logical controller design,

• multiprocessor memory systems,

• dataflow computing systems,

• fault tolerant systems.

48

Page 55: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets

A Petri net is a directed bipartite graph consisting of places P and

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 or positive

integer) number of tokens of marks.

49

Page 56: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets

P1

P2

P3 P4

P5 P6

P7

T1

T2

T3 T4

T5

T6

50

Page 57: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Firing Rules

1. A transition t is enabled if each input place contains at least one

token.

2. An enabled transition may or may not fire.

3. Firing an enabled transition t means removing one token from each

input place of t and adding one token to each output place of t.

The firing of a transition has zero duration.

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

The firing of a source transition (only output places) only produces

tokens.

51

Page 58: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets

P1

P2

P3 P4

P5 P6

P7

T1

T2

T3 T4

T5

T6

52

Page 59: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets

P1

P2

P3 P4

P5 P6

P7

T1

T2

T3 T4

T5

T6

53

Page 60: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets

P1

P2

P3 P4

P5 P6

P7

T1

T2

T3 T4

T5

T6

54

Page 61: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets

P1

P2

P3 P4

P5 P6

P7

T1

T2

T3 T4

T5

T6

55

Page 62: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets

P1

P2

P3 P4

P5 P6

P7

T1

T2

T3 T4

T5

T6

56

Page 63: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets

P1

P2

P3 P4

P5 P6

P7

T1

T2

T3 T4

T5

T6

57

Page 64: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets

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

58

Page 65: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Generalized Petri Nets

2

2

P1 P2

P3

T1

Firing rules:

1. A transition t is enabled if each input place p of t contains at least

w(p,t) tokens

2. Firing a transition t means removing w(p,t) tokens from each input

place p and adding w(t,q) tokens to each output place q.

59

Page 66: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Generalized Petri Nets

2

2

P1 P2

P3

T1

Firing rules:

1. A transition t is enabled if each input place p of t contains at least

w(p,t) tokens

2. Firing a transition t means removing w(p,t) tokens from each input

place p and adding w(t,q) tokens to each output place q.

60

Page 67: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

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.

61

Page 68: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Analysis

• Live: No transitions can become unfireable.

• Deadlock-free: Transitions can always be fired.

• Bounded: Finite number of tokens.

62

Page 69: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Analysis methods

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 simpler net while

preserving the properties of interest.

63

Page 70: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: 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.

64

Page 71: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Mutual Exclusion

Waiting forcritical section

Waiting forcritical section

Executinginsidecriticalsection

Executinginsidecriticalsection

Executingoutsidecriticalsection

Executingoutsidecriticalsection

Process A Process B

Mutexsemaphore

65

Page 72: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Mutual Exclusion

Waiting forcritical section

Waiting forcritical section

Executinginsidecriticalsection

Executinginsidecriticalsection

Executingoutsidecriticalsection

Executingoutsidecriticalsection

Process A Process B

Mutexsemaphore

66

Page 73: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Mutual Exclusion

Waiting forcritical section

Waiting forcritical section

Executinginsidecriticalsection

Executinginsidecriticalsection

Executingoutsidecriticalsection

Executingoutsidecriticalsection

Process A Process B

Mutexsemaphore

67

Page 74: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Mutual Exclusion

Waiting forcritical section

Waiting forcritical section

Executinginsidecriticalsection

Executinginsidecriticalsection

Executingoutsidecriticalsection

Executingoutsidecriticalsection

Process A Process B

Mutexsemaphore

68

Page 75: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Mutual Exclusion

Waiting forcritical section

Waiting forcritical section

Executinginsidecriticalsection

Executinginsidecriticalsection

Executingoutsidecriticalsection

Executingoutsidecriticalsection

Process A Process B

Mutexsemaphore

69

Page 76: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Mutual Exclusion

Waiting forcritical section

Waiting forcritical section

Executinginsidecriticalsection

Executinginsidecriticalsection

Executingoutsidecriticalsection

Executingoutsidecriticalsection

Process A Process B

Mutexsemaphore

70

Page 77: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Mutual Exclusion

Waiting forcritical section

Waiting forcritical section

Executinginsidecriticalsection

Executinginsidecriticalsection

Executingoutsidecriticalsection

Executingoutsidecriticalsection

Process A Process B

Mutexsemaphore

71

Page 78: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Producer Consumer

Producer processes

Consumerprocesses

BufferRead

Write

72

Page 79: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Producer Consumer

Producer processes

Consumerprocesses

BufferRead

Write

73

Page 80: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Producer Consumer

Producer processes

Consumerprocesses

BufferRead

Write

74

Page 81: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Producer Consumer

Producer processes

Consumerprocesses

BufferRead

Write

75

Page 82: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Producer Consumer

Producer processes

Consumerprocesses

BufferRead

Write

76

Page 83: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Producer Consumer

Producer processes

Consumerprocesses

BufferRead

Write

77

Page 84: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Producer Consumer

Producer processes

Consumerprocesses

BufferRead

Write

78

Page 85: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Producer Consumer with Bounded Buffer

Producer processes

Consumerprocesses

Buffer

Read

Write

Freeplaces

Fullplaces

79

Page 86: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Producer Consumer with Bounded Buffer

Producer processes

Consumerprocesses

Buffer

Read

Write

Freeplaces

Fullplaces

80

Page 87: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Producer Consumer with Bounded Buffer

Producer processes

Consumerprocesses

Buffer

Read

Write

Freeplaces

Fullplaces

81

Page 88: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Producer Consumer with Bounded Buffer

Producer processes

Consumerprocesses

Buffer

Read

Write

Freeplaces

Fullplaces

82

Page 89: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Producer Consumer with Bounded Buffer

Producer processes

Consumerprocesses

Buffer

Read

Write

Freeplaces

Fullplaces

83

Page 90: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Producer Consumer with Bounded Buffer

Producer processes

Consumerprocesses

Buffer

Read

Write

Freeplaces

Fullplaces

84

Page 91: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Producer Consumer with Bounded Buffer

Producer processes

Consumerprocesses

Buffer

Read

Write

Freeplaces

Fullplaces

85

Page 92: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Producer Consumer with Bounded Buffer

Producer processes

Consumerprocesses

Buffer

Read

Write

Freeplaces

Fullplaces

86

Page 93: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Producer Consumer with Bounded Buffer

Producer processes

Consumerprocesses

Buffer

Read

Write

Freeplaces

Fullplaces

87

Page 94: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Producer Consumer with Bounded Buffer

Producer processes

Consumerprocesses

Buffer

Read

Write

Freeplaces

Fullplaces

88

Page 95: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Producer Consumer with Bounded Buffer

Producer processes

Consumerprocesses

Buffer

Read

Write

Freeplaces

Fullplaces

89

Page 96: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Producer Consumer with Bounded Buffer

Producer processes

Consumerprocesses

Buffer

Read

Write

Freeplaces

Fullplaces

90

Page 97: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Reader and Writer

AccessControl

Ready to write

WritingReading

Readyto read

3

3

Writers processes Readers processes

91

Page 98: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Reader and Writer

AccessControl

Ready to write

WritingReading

Readyto read

3

3

Writers processes Readers processes

92

Page 99: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Reader and Writer

AccessControl

Ready to write

WritingReading

Readyto read

3

3

Writers processes Readers processes

93

Page 100: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Reader and Writer

AccessControl

Ready to write

WritingReading

Readyto read

3

3

Writers processes Readers processes

94

Page 101: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Reader and Writer

AccessControl

Ready to write

WritingReading

Readyto read

3

3

Writers processes Readers processes

95

Page 102: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Reader and Writer

AccessControl

Ready to write

WritingReading

Readyto read

3

3

Writers processes Readers processes

96

Page 103: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Reader and Writer

AccessControl

Ready to write

WritingReading

Readyto read

3

3

Writers processes Readers processes

97

Page 104: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Reader and Writer

AccessControl

Ready to write

WritingReading

Readyto read

3

3

Writers processes Readers processes

98

Page 105: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Reader and Writer

AccessControl

Ready to write

WritingReading

Readyto read

3

3

Writers processes Readers processes

99

Page 106: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Reader and Writer

AccessControl

Ready to write

WritingReading

Readyto read

3

3

Writers processes Readers processes

100

Page 107: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Reader and Writer

AccessControl

Ready to write

WritingReading

Readyto read

3

3

Writers processes Readers processes

101

Page 108: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Reader and Writer

AccessControl

Ready to write

WritingReading

Readyto read

3

3

Writers processes Readers processes

102

Page 109: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Reader and Writer

AccessControl

Ready to write

WritingReading

Readyto read

3

3

Writers processes Readers processes

103

Page 110: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Reader and Writer

AccessControl

Ready to write

WritingReading

Readyto read

3

3

Writers processes Readers processes

104

Page 111: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Dining Philosopers

105

Page 112: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Dining Philosopers

Thinks

Picksleft fork

Picks right fork

Eats

Dropsleft fork

Dropsright fork

Fork

Fork

Philosopher 106

Page 113: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Dining Philosopers

Thinks

Picksleft fork

Picks right fork

Eats

Dropsleft fork

Dropsright fork

Fork

Fork

Philosopher 107

Page 114: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Dining Philosopers

Thinks

Picksleft fork

Picks right fork

Eats

Dropsleft fork

Dropsright fork

Fork

Fork

Philosopher 108

Page 115: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Dining Philosopers

Thinks

Picksleft fork

Picks right fork

Eats

Dropsleft fork

Dropsright fork

Fork

Fork

Philosopher 109

Page 116: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Dining Philosopers

Thinks

Picksleft fork

Picks right fork

Eats

Dropsleft fork

Dropsright fork

Fork

Fork

Philosopher 110

Page 117: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Dining Philosopers

Thinks

Picksleft fork

Picks right fork

Eats

Dropsleft fork

Dropsright fork

Fork

Fork

Philosopher 111

Page 118: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Dining Philosopers

Thinks

Picksleft fork

Picks right fork

Eats

Dropsleft fork

Dropsright fork

Fork

Fork

Philosopher 112

Page 119: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Dining Philosopers

Thinks

Picksleft fork

Picks right fork

Eats

Dropsleft fork

Dropsright fork

Fork

Fork

Philosopher 113

Page 120: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Dining Philosopers

Thinks

Picksleft fork

Picks right fork

Eats

Dropsleft fork

Dropsright fork

Fork

Fork

Philosopher 114

Page 121: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Dining Philosopers

Thinks

Picksleft fork

Picks right fork

Eats

Dropsleft fork

Dropsright fork

Fork

Fork

Philosopher 115

Page 122: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Dining Philosopers

Thinks

Picksleft fork

Picks right fork

Eats

Dropsleft fork

Dropsright fork

Fork

Fork

Philosopher 116

Page 123: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Petri Nets: Dining Philosopers

Thinks

Eats

Leftfork

Right fork

117

Page 124: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Implementation

Page 125: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Implementation (Not covered in the lecture – Homework)

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

Systematic ways to write automata code often not taught in

programming 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 periodic processes with

a period that is determined by the shortest time required when making a

state transition.

118

Page 126: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Example: Passive State Machine

The state machine is implemented as a synchronized object.

1 public class PassiveMealyMachine {

2 private static final int STATE0 = 0;

3 private static final int STATE1 = 1;

4 private static final int STATE2 = 2;

5 private static final int INA = 0;

6 private static final int INB = 1;

7 private static final int INC = 2;

8 private static final int OUTA = 0;

9 private static final int OUTB = 1;

10 private static final int OUTC = 2;

11 private int state;

12 PassiveMealyMachine() {

13 state = STATE0;

14 }

15 private void generateEvent(int outEvent) {

16 // Do something

17 }

119

Page 127: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Example: Passive State Machine

1 public synchronized void inputEvent(int event) {

2 switch (state) {

3 case STATE0 : switch (event) {

4 case INA : generateEvent(OUTA); state = STATE1; break;

5 case INB : generateEvent(OUTB); break;

6 default : break;

7 }; break;

8 case STATE1 : switch (event) {

9 case INC : generateEvent(OUTC); state = STATE2; break;

10 default : break;

11 }; break;

12 case STATE2 : switch (event) {

13 case INA : generateEvent(OUTB); state = STATE0; break;

14 case INC : generateEvent(OUTC); break;

15 default : break;

16 }; break;

17 }

18 }

19 }

120

Page 128: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Example: Active State Machine

The state machine could also be implemented as an active object (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 for an

incoming event (RTEvent) and switches state depending on the event.

121

Page 129: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Example: Active State Machine

An activity is an action that is executed periodically while a state is

active.

More natural to implement the state machine as a thread.

122

Page 130: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Example: Active State Machine 1

1 public class ActiveMachine1 extends Thread {

2 private static final int STATE0 = 0;

3 private static final int STATE1 = 1;

4 private static final int STATE2 = 2;

5 private int state;

6

7 ActiveMachine1() { state = STATE0; }

8

9 private boolean cond0() {

10 // Returns true if condition 0 is true

11 }

12 private boolean cond1() { }

13 private boolean cond2() { }

14

15 private void action0() {

16 // Executes action 0

17 }

18 private void action1() { }

19 private void action2() { }

123

Page 131: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Example: Active State Machine 1

1 public void run() {

2 long t = System.currentTimeMillis();

3 long duration;

4

5 while (true) {

6 switch (state) {

7 case STATE0 : {

8 action0(); t = t + 20;

9 duration = t - System.currentTimeMillis();

10 if (duration > 0) {

11 try { sleep(duration);

12 } catch (InterruptedException e) {}

13 }

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

15 } break;

16 case STATE1 : {

17 // Similar as for STATE0. Executes action1,

18 // waits for 50 ms, checks

19 // cond1 and then changes to STATE2

20 }; break;

124

Page 132: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Example: Active State Machine 1

1 case STATE2 : {

2 // Similar as for STATE0. Executes action2,

3 // waits for 10 ms, checks

4 // cond2 and then changes to STATE0

5 }; break;

6 } } } }

• Conditions tested at a frequency determined by the activity

frequencies of the different states.

• sleep() spread out in the code.

125

Page 133: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

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.

1 public void run() {

2 long t = System.currentTimeMillis();

3 long duration;

4 int counter = 0;

5 while (true) {

6 counter++;

7 switch (state) {

8 case STATE0 : {

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

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

11 }; break;

12 case STATE1 : {

13 // Similar as for STATE0. Executes action1

14 // if counter == 10. Changes to STATE2 if

15 // cond1() is true

16 }; break;

126

Page 134: Discrete Control - Real-Time Systems, Lecture 14 · Discrete Control Real ... System: Chapter 12] 1. Discrete Event Systems 2. ... mechanism is time-driven. Continuous discrete-time

Example: Active State Machine 2

1 case STATE2 : {

2 // Similar as for STATE0. Executes action2

3 // if counter == 12. Changes to STATE0 if

4 // cond2() is true

5 }; break;

6 }

7 t = t + 5; // Base sampling time

8 duration = t - System.currentTimeMillis();

9 if (duration > 0) {

10 try { sleep(duration);

11 } catch (InterruptedException e) {}

12 } } }

• Polled time handling.

• Complicated handling of counter.

• Conditions tested at a high rate.

127


Recommended