Design and Implementation Issues for Explorative Location-based Applications: The NexusRallye
State-Based Design Implementation issues
Transcript of State-Based Design Implementation issues
![Page 1: State-Based Design Implementation issues](https://reader030.fdocuments.net/reader030/viewer/2022032902/623f991bf648f164ef242a80/html5/thumbnails/1.jpg)
State-Based Design Implementation issues
School of Electrical Engineering
Department of Electrical Engineering and Automation
ELEC 8102 Distributed and Intelligent Automation Systems
Valeriy Vyatkin, Igor Buzhinsky
![Page 2: State-Based Design Implementation issues](https://reader030.fdocuments.net/reader030/viewer/2022032902/623f991bf648f164ef242a80/html5/thumbnails/2.jpg)
Lecture plan
ā¢ Implementation of state-based design in PLCsā SFCā STā UML SCā LD
ā¢ Use of counters and timers in the state-based design
ā¢ Initial stateā¢ Limitations
2
![Page 3: State-Based Design Implementation issues](https://reader030.fdocuments.net/reader030/viewer/2022032902/623f991bf648f164ef242a80/html5/thumbnails/3.jpg)
Recap: finite-state machines (FSMs)
3
ā¢ Initial state (shown with double circle)
ā¢ The next state depends on the current state and current input signals
ā¢ The outputs either depend on the current state and input signals (Mealy machines) or on the current state only (Moore machines)
ā¢ Which kind of state machine is shown on the left?
![Page 4: State-Based Design Implementation issues](https://reader030.fdocuments.net/reader030/viewer/2022032902/623f991bf648f164ef242a80/html5/thumbnails/4.jpg)
Recap: why state-based design
4
![Page 5: State-Based Design Implementation issues](https://reader030.fdocuments.net/reader030/viewer/2022032902/623f991bf648f164ef242a80/html5/thumbnails/5.jpg)
Recap: why state-based design
5
ā¢ Decomposing the program into meaningful parts whose correctness can be checked independently
ā¢ Also with formal verification methods!
ā¢ Graphical program representationā¢ And, possibly, code generation from state diagrams
ā¢ State machines can also be used to represent specifications and plant/environment models
ā¢ Thus, consistency of notation
![Page 6: State-Based Design Implementation issues](https://reader030.fdocuments.net/reader030/viewer/2022032902/623f991bf648f164ef242a80/html5/thumbnails/6.jpg)
Tl
STATE
...
...
STATEi1
STATEiM
Ti1
TiM
To1
ToN
STATEoN
STATEo1
6
ā¢ Tl ā condition defining when the FSM remains in the state STATE
ā¢ It can be defined explicitly or implicitly (else)
successor states
predecessor states
Implementation of FSM with Boolean variables
š! =#"#$
%
š"&
![Page 7: State-Based Design Implementation issues](https://reader030.fdocuments.net/reader030/viewer/2022032902/623f991bf648f164ef242a80/html5/thumbnails/7.jpg)
Implementation algorithm
7
1. Mark every state with a Boolean state variable, e.g., ššš“ššø!.
2. Define the state transition function (see the next slides).3. Assign output signals depending on the state (the one after
the transition). In Boolean logic: if š¼ is the tuple of input signals and š ! is the š-th state, then š" āā!#$% ššš“ššø! ā šŗ š¼, š ! ā the logical sum on all states where the signal is to be TRUE.
4. Add initialization section defining which state is true initially and what are values of variables then.
Moore:š! = šŗ(š!),š!"# = š¹(š¼! , š!).
where š” and š” + 1 are two consecutive moments of discrete time.
Mealy:š! = šŗ(š¼!, š!),š!"# = š¹(š¼! , š!).
![Page 8: State-Based Design Implementation issues](https://reader030.fdocuments.net/reader030/viewer/2022032902/623f991bf648f164ef242a80/html5/thumbnails/8.jpg)
Definition of the state transition function F
Tl
STATE
...
...
STATEi1
STATEiM
Ti1
TiM
To1
ToN
STATEoN
STATEo1
The state transition function is the logical sum of conditions on all incoming arcs to the state including Tl.
where: + is logical OR, dot is logical AND, ššš“ššø!" - Boolean variables indicating activity of āpredecessorā states.STATEā ā state variable in the next scan,M ā the number of transitions to STATE. N - the number of transitions from STATE. š!" - transition condition from a predecessor state ššš“ššø!" to STATE. š#$ - transition condition from STATE to the successor state ššš“ššø#$
8
ššš“ššøā² = ššš“ššø ' š! +)"#$
%
ššš“ššø"& ' š"&
š!=*'#(
)
š'*
![Page 9: State-Based Design Implementation issues](https://reader030.fdocuments.net/reader030/viewer/2022032902/623f991bf648f164ef242a80/html5/thumbnails/9.jpg)
Structured Text representation
9
// InitialisationIF FirstScan THENS1 := 1; S2 := 0; S3 := 0;S1x := 1; S2x := 0; S3x := 0;MOVEF := 0; LED := 1;FirstScan := 0;
END_IF;// State transition function S2x := S2 AND NOT END
OR S1 AND START;S3x := S3 AND NOT HOME
OR S2 AND END; S1x := S1 AND NOT START
OR S3 AND HOME;// OutputsLED := S1x;MOVEF := S2x;// Next state variablesS1 := S1x; S2 := S2x; S3 := S3x;
RetractMove
END
S1
S3
LeftLED:=1
START HOME
MOVEF:=1LED:=0
MOVEF:=0
ššš“ššøā² = ššš“ššø - š! +/"#$
%
ššš“ššø"& - š"&
S2
![Page 10: State-Based Design Implementation issues](https://reader030.fdocuments.net/reader030/viewer/2022032902/623f991bf648f164ef242a80/html5/thumbnails/10.jpg)
Ladder logic representation
10
The same in Structured text:
S2x := S2 AND NOT ENDOR S1 AND START;
S3x := S3 AND NOT HOMEOR S2 AND END;
S1x := S1 AND NOT STARTOR S3 AND HOME;
LED := S1x;MOVEF := S2x;
S1 := S1x;S2 := S2x;S3 := S3x;
![Page 11: State-Based Design Implementation issues](https://reader030.fdocuments.net/reader030/viewer/2022032902/623f991bf648f164ef242a80/html5/thumbnails/11.jpg)
Table implementation of FSM
Inputs StatesSTART HOME END S1 S2 S3
1 0 0 0 X ... ...
2 0 0 1 X
3 0 1 0 S1
4 0 1 1 X
5 1 0 0 S2
6 1 0 1 S2
7 1 1 0 S2
8 1 1 1 x
Output LED=1 MOVEF=1
11
State transition function F for the Cylinder2n
for n
bin
ary
inpu
ts
Fill the table and then update states and outputs according to it
![Page 12: State-Based Design Implementation issues](https://reader030.fdocuments.net/reader030/viewer/2022032902/623f991bf648f164ef242a80/html5/thumbnails/12.jpg)
Counters
There are three counter instructions in IEC 61131-3:
CTU - Count Up CounterCTD - Count Down CounterCTUD - Count Up/Down Counter
CTU
200
Load_Cnt
PV CV 178
QCU
R
12
![Page 13: State-Based Design Implementation issues](https://reader030.fdocuments.net/reader030/viewer/2022032902/623f991bf648f164ef242a80/html5/thumbnails/13.jpg)
Counter Operation
Parameters:- IN/CU/CD = Count up/Down- Q/QU/QD = Comparison Output- R = Reset to Zero- LD = Load CV with PV- PV = Preset Value- CV = Count Value
...
...
CVPV|0
CUQUCDQD
LDR
Count Up/Down (CTUD) Counter
...INQ
CVPV|0
LD
...Count Down (CTD) Counter
...INQ
CVPV|0
R
Count Up (CTU) Counter...
13
![Page 14: State-Based Design Implementation issues](https://reader030.fdocuments.net/reader030/viewer/2022032902/623f991bf648f164ef242a80/html5/thumbnails/14.jpg)
ExampleRequirement: Once the START button is pressed, the cylinder will need to shuttle back and forth 3 times.
To count the number of passes we need an integer variable which will be incremented every time cylinder reaches the āendā position. The variable will be compared with the desired number of passes (3) to decide whether to repeat or stop.
We use a counter object called CNT. CTU
3
CNT
PV CV
QCU
RESET
14
Notation:ā¢ CNT.Q ā is true if CNT.CV = 3 ā¢ CNT.CV is an integer variableā¢ CNT.RESET ā CNT.CV:=0
(and CNV.Q:=false)ā¢ CNT.CU ā increment CV if it is
less than PV
![Page 15: State-Based Design Implementation issues](https://reader030.fdocuments.net/reader030/viewer/2022032902/623f991bf648f164ef242a80/html5/thumbnails/15.jpg)
Example
Notation for state diagrams: Ā®Op : do the operation Op once on entry to the state.
Rule for converting to Boolean equations: 1. For each counter add one invocation line before the
state transition section.
CNT(CU:= <C1>, RESET:= <C2 >; PV:= preset value); where: ā¢ C1 ā logical sum of all states in which there is
Ā®CNT.CUā¢ C2 - logical sum of all states in which there is
Ā®CNT.RESET
In ladder logic diagrams use the function block instead.
2. Use CNT.Q in state equations transition conditions 15
CTU
3
CNT
PV CV
QCU
RESET
![Page 16: State-Based Design Implementation issues](https://reader030.fdocuments.net/reader030/viewer/2022032902/623f991bf648f164ef242a80/html5/thumbnails/16.jpg)
Simplified Structured TextVARCNT: CTU; // Up counter with
// detection of// input rising edges
FirstScan: BOOL := 1;END_VAR
// InitialisationIF FirstScan THENS1 := 1; S2 := 0; S3 := 0;S1x := 1; S2x := 0; S3x := 0;MOVEF := 0; LED := 1FirstScan := 0;
END_IF;
// State transitions function S2x := S1 AND START OR S2 AND NOT ENDOR S3 AND HOME AND NOT CNT.Q;
S3x := S2 AND END OR S3 AND NOT HOME; S1x := S3 AND HOME AND CNT.QOR S1 AND NOT START;
// Invoking the counter object;CNT(CU:=S3, RESET:=S1; PV:=3);
// OutputsLED := S1;MOVEF := S2;S1 := S1x; S2 := S2x; S3 := S3x;
16
![Page 17: State-Based Design Implementation issues](https://reader030.fdocuments.net/reader030/viewer/2022032902/623f991bf648f164ef242a80/html5/thumbnails/17.jpg)
Ladder Diagram
17
// State transitions function S2x := S1Ā·START + S2Ā·Ā¬ END+ S3Ā·HOMEĀ·Ā¬CNT.Q;
S3x := S2Ā·END + S3Ā·Ā¬HOME;
S1x := S3Ā·HOMEĀ·CNT.Q+ S1Ā·Ā¬ START;
// Invoking the counter// objectCNT(CU:=S3x,
RESET:=S1x;PV:=3);
![Page 18: State-Based Design Implementation issues](https://reader030.fdocuments.net/reader030/viewer/2022032902/623f991bf648f164ef242a80/html5/thumbnails/18.jpg)
Timer
PLC program
Configure and activate
Timer instance
Activation condition fulfilled, starts ticking
... ...
t
Time expired, stopped ticking, program can take action
Timer is an object, an instance of a function block
18
![Page 19: State-Based Design Implementation issues](https://reader030.fdocuments.net/reader030/viewer/2022032902/623f991bf648f164ef242a80/html5/thumbnails/19.jpg)
Pulse Timer TP
As can be seen from Figure timers of type TP are not "retriggerable". If the intervals between the input pulses at IN are shorter than the pre-set time period, the pulse duration still remains constant (see period [t2; t2+PT]). Timing therefore does not begin again with every rising edge at IN.
TP
19
![Page 20: State-Based Design Implementation issues](https://reader030.fdocuments.net/reader030/viewer/2022032902/623f991bf648f164ef242a80/html5/thumbnails/20.jpg)
TON/ TOFThe on-delay timer TON supplies the input value IN at Q with a time delay when a rising edge is detected at IN. If input IN is "1" only for a short pulse (shorter than PT), the timer is not started for this edge.The elapsed time can be read off at output ET.
The off-delay timer TOF performs the inverse function to TON i.e. it delays a falling edge in the same way as TON delays a rising one.The behaviour of the timer TOF if PT is modified during timer operation is implementation-dependent.
TON TOF
20
![Page 21: State-Based Design Implementation issues](https://reader030.fdocuments.net/reader030/viewer/2022032902/623f991bf648f164ef242a80/html5/thumbnails/21.jpg)
Cylinder delay in the extended position
21
// State transitions
S2ā :=S1Ā·START + S2Ā· Ā¬ END;S4ā :=S2Ā·END + S4 Ā· Ā¬ T1.Q; S1ā :=S3Ā·HOME + S1 Ā· Ā¬ START;S3ā :=S4Ā·T1.Q + S3 Ā·Ā¬HOME;
// Timer activation in S4
T1(IN: S4ā, PT: #5s);
Rule for converting to Boolean equations: 1. Declare TON timer instance for each delay. 2. For each timer TIMER add one invocation line
before or after the state transition section.
TIMER(IN:= <C1>; PT:= preset time); where: C1 ā logical sum of all states in which there is Ā®TIMER.IN(preset time)
In ladder logic diagram use the function block instead with the rung connected to input IN implementing the C1 condition.
3. Use TIMER.Q in state equations transition conditions as a condition of timer expiration.
![Page 22: State-Based Design Implementation issues](https://reader030.fdocuments.net/reader030/viewer/2022032902/623f991bf648f164ef242a80/html5/thumbnails/22.jpg)
LD implementation
22
// State transitions
S2ā :=S1Ā·START + S2Ā· Ā¬END;S4ā :=S2Ā·END + S4Ā· Ā¬T1.Q; S1ā :=S3Ā·HOME + S1Ā· Ā¬START;S3ā :=S4Ā·T1.Q + S3Ā· Ā¬HOME;
// Timer activation in S4
T1(IN: S4ā, PT: #5s);
![Page 23: State-Based Design Implementation issues](https://reader030.fdocuments.net/reader030/viewer/2022032902/623f991bf648f164ef242a80/html5/thumbnails/23.jpg)
Example: blinking lampRequirement: Make the LED of the START button blinking while the cylinder is in the leftmost state. Implement controller in LLD.To implement the blinking, we need to introduce a āsisterā state to S1, where the LED will be reset for some time, say 200 ms.
23
RetractMove
END
S1
S3
Left
START HOME
MOVEF:=1LED:=0 MOVEF:=0
OffS2 S4T1.Q
Ā®T2.IN(200ms)LED:=0
T2.Q
Ā®T1.IN(200ms)LED:=1
![Page 24: State-Based Design Implementation issues](https://reader030.fdocuments.net/reader030/viewer/2022032902/623f991bf648f164ef242a80/html5/thumbnails/24.jpg)
Example: heating ovenVerbal specification: The oven is started with a Start button that seals in the Auto mode. This can be stopped if the Stop button is pushed. (Remember: Stopbuttons are normally closed.)When the Auto goes on, the horn is used to sound for the first 10 seconds to warn that the oven will start, and after that the horn stops and the heating coils start. When the oven is turned off the fancontinues to blow for 300s, or 5 minutes, after.
START STOP
HORN
FANCOIL
TON
PT ET
QIN
TOF
T2
PT ET
QIN
T1
24
![Page 25: State-Based Design Implementation issues](https://reader030.fdocuments.net/reader030/viewer/2022032902/623f991bf648f164ef242a80/html5/thumbnails/25.jpg)
Timing Diagram
START STOP
HORN
FANCOIL
25
![Page 26: State-Based Design Implementation issues](https://reader030.fdocuments.net/reader030/viewer/2022032902/623f991bf648f164ef242a80/html5/thumbnails/26.jpg)
Timing diagram vs. Specification
The oven is started with a Start buttonthat seals in the Auto mode. This can be stopped if the Stop button is pushed.(Remember: Stop buttons are normally closed.)When the Auto goes on, the horn is used to sound for the first 10 seconds to warn that the oven will start, and after that the horn stops and the heating coils start. When the oven is turned off the fan continues to blow for 300s or 5 minutes after.
Note: 1) What will happen if Start is pressed here?
26
![Page 27: State-Based Design Implementation issues](https://reader030.fdocuments.net/reader030/viewer/2022032902/623f991bf648f164ef242a80/html5/thumbnails/27.jpg)
Let us follow the diagram ā¦ (naive engineering)PROGRAM OvenSTVAR
HeatTimer: TP;CoolTimer: TOF;RE: R_TRIG;FE: F_TRIG;Edge: BOOL;
END_VARRE(CLK:=Start); // rising edge of Start is detectedIF RE.Q THEN
HeatTimer(IN:=Start, PT:=T#10S); Horn := 1;
END_IF;IF HeatTimer.Q THEN
Horn := 0; HeatingCoils := 1; Fan := 1;
END_IF;FE(CLK := Stop); // falling edge of Stop is detectedIF FE.Q THEN
HeatingCoils := 0; CoolTimer(IN:=NOT Stop, PT:=T#5M);
END_IF;IF NOT CoolTimer.Q THEN
Fan := 0; END_IF;
Notes1. Why do we use TP timer?2. Detecting rising/falling edge
27
![Page 28: State-Based Design Implementation issues](https://reader030.fdocuments.net/reader030/viewer/2022032902/623f991bf648f164ef242a80/html5/thumbnails/28.jpg)
State Machine
Timer T1 starts on the rising edge of the state activity flag
Transition occurswhen timer expires
28
![Page 29: State-Based Design Implementation issues](https://reader030.fdocuments.net/reader030/viewer/2022032902/623f991bf648f164ef242a80/html5/thumbnails/29.jpg)
From State Machine to Ladder Diagram
29
Complete the Boolean functions implementation:
![Page 30: State-Based Design Implementation issues](https://reader030.fdocuments.net/reader030/viewer/2022032902/623f991bf648f164ef242a80/html5/thumbnails/30.jpg)
Limitations of State-based Design Approach
ā¢ Concurrent processes ā need to decompose to smaller subsystemsā how to handle their interaction and
coordination?ā¢ Hard to define stable states
ā e.g., the Oven example
30
![Page 31: State-Based Design Implementation issues](https://reader030.fdocuments.net/reader030/viewer/2022032902/623f991bf648f164ef242a80/html5/thumbnails/31.jpg)
Initialization ā First scan
VARFirstScan: BOOL := 1;
END_VAR
// InitializationIF FirstScan THEN
// Put here setting of the first // state in state machine, etc.
FirstScan:=0;END_IF;
31
1) Declare FirstScan Boolean variable and initialise it to TRUE.
2) Add POU INIT and put its call first in the MainTask.
3) Add initialisation code to INIT as follows:
![Page 32: State-Based Design Implementation issues](https://reader030.fdocuments.net/reader030/viewer/2022032902/623f991bf648f164ef242a80/html5/thumbnails/32.jpg)
Initialization in Application Design
32
A new initial state Startup is introduced.
Problem: The machine may require positioning to the intial state before starting the operation
// InitialisationIF FirstScan THENS0 := 1; S1 := 0;S2 := 0; S3 := 0;S0x := 1; S1x := 0;S2x := 0; S3x := 0;MOVEF:=0; LED:=1;FirstScan :=0;
END_IF;// State transition function S2x := S2 AND NOT END OR S1 AND START;S3x := S3 AND NOT HOME OR S2 AND END; S1x := S1 AND NOT START OR S3 AND HOME
OR S0 AND HOME; S0x := S0 AND not HOME;// OutputsLED := S1x;MOVEF := S2x;// Next state variablesS0 := s0x; S1 := S1x;S2 := S2x; S3 := S3x;
OTHER SOLUTIONS?
![Page 33: State-Based Design Implementation issues](https://reader030.fdocuments.net/reader030/viewer/2022032902/623f991bf648f164ef242a80/html5/thumbnails/33.jpg)
Warning: do not overcomplicate things!
ā¢ State-based design is used to simplify things, not to complicate!
ā¢ It is reasonable to use Boolean variables to encode states only if this is really needed
ā¢ Otherwise, using integers/enumerations to encode states is more comprehensible and less error-proneā IF-ELSIF-ELSE (āswitchā in C-like languages) pattern
from the previous lectureā In general purpose languages, maybe youād like to
use the State design pattern in similar situations
33
![Page 34: State-Based Design Implementation issues](https://reader030.fdocuments.net/reader030/viewer/2022032902/623f991bf648f164ef242a80/html5/thumbnails/34.jpg)
Summary
ā¢ State-based design implementation via Boolean logicā Reasonsā Methods
ā¢ Use of standard functional elements in state-based design (implemented via Boolean logic)
ā¢ Limitations of state-based design and some workarounds
34