Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8847c69b562067d49346fe6ad46c36efb7e827bb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Implement the Behavior</title>
<link href="book.css" rel="stylesheet" type="text/css">
<meta content="DocBook XSL Stylesheets V1.75.1" name="generator">
<link rel="home" href="index.html" title="eTrice User Guide">
<link rel="up" href="TutorialBlinkyJava.html" title="Tutorial Blinky (Java)">
<link rel="prev" href="Finishthemodelstructure.html" title="Finish the model structure">
<link rel="next" href="Summary2.html" title="Summary">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<h1 xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0">Implement the Behavior</h1>
<div class="section" title="Implement the Behavior">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both">
<a name="ImplementtheBehavior"></a>Implement the Behavior</h2>
</div>
</div>
</div>
<p>The application should switch on and off the LED for 5 seconds in a 1 second interval, then stop blinking for 5 seconds and start again. To implement this behavior we will implement two FSMs. One for the 1 second interval and one for the 5 second interval. The 1 second blinking should be implemented in 
				<span class="emphasis"><em>Blinky</em></span>. The 5 second interval should be implemented in 
				<span class="emphasis"><em>BlinkyController</em></span>. First implement the Controller.
			</p>
<p>Right click to 
				<span class="emphasis"><em>BlinkyController</em></span> and select 
				<span class="emphasis"><em>Edit Behavior</em></span>.
				Drag and Drop the 
				<span class="emphasis"><em>Initial Point</em></span> and two 
				<span class="emphasis"><em>States</em></span> into the top state. Name the states 
				<span class="emphasis"><em>on</em></span> and 
				<span class="emphasis"><em>off</em></span>. 
				Use the 
				<span class="emphasis"><em>Transition</em></span> tool to draw transitions from 
				<span class="emphasis"><em>init</em></span> to 
				<span class="emphasis"><em>on</em></span> from 
				<span class="emphasis"><em>on</em></span> to 
				<span class="emphasis"><em>off</em></span> and from 
				<span class="emphasis"><em>off</em></span> to 
				<span class="emphasis"><em>on</em></span>.
			</p>
<p>Open the transition dialog by double click the arrow to specify the trigger event and the action code of each transition. Note that the initial transition does not have a trigger event.</p>
<p>The transition dialog should look like this:</p>
<p>
				
</p>
<div class="mediaobject">
<img src="images/020-Blinky09.png"></div>
<p> 
			
</p>
<p>The defined ports will be generated as a member attribute of the actor class from type of the attached protocol. So, to send e message you must state 
				<span class="emphasis"><em>port.message(param);</em></span>. In this example 
				<span class="emphasis"><em>ControlPort.start()</em></span> sends the 
				<span class="emphasis"><em>start</em></span> message via the 
				<span class="emphasis"><em>ControlPort</em></span> to the outside world. Assuming that 
				<span class="emphasis"><em>Blinky</em></span> is connected to this port, the message will start the one second blinking FSM. It is the same thing with the 
				<span class="emphasis"><em>timer</em></span>. The SAP is also a port and follows the same rules. So it is clear that 
				<span class="emphasis"><em>timer.Start(5000);</em></span> will send the 
				<span class="emphasis"><em>Start</em></span> message to the timing service. The timing service will send a 
				<span class="emphasis"><em>timeoutTick</em></span> message back after 5000ms.
			</p>
<p>Within each transition the timer will be restarted and the appropriate message will be sent via the 
				<span class="emphasis"><em>ControlPort</em></span>. 
			</p>
<p>The resulting state machine should look like this:
				(Note that the arrows peak changes if the transition contains action code.)</p>
<p>
				
</p>
<div class="mediaobject">
<img src="images/020-Blinky10.png"></div>
<p>
			
</p>
<p>Save the diagram and inspect the 
				<span class="emphasis"><em>Blinky.room</em></span> file. The 
				<span class="emphasis"><em>BlinkyController</em></span> should look like this:
			</p>
<p>
				
</p>
<div class="mediaobject">
<img src="images/020-Blinky11.png"></div>
<p> 
			
</p>
<p>Now we will implement 
				<span class="emphasis"><em>Blinky</em></span>. Due to the fact that 
				<span class="emphasis"><em>Blinky</em></span> interacts with the GUI class a view things must to be done in the model file.
			</p>
<p>Double click 
				<span class="emphasis"><em>Blinky</em></span> in the outline view to navigate to 
				<span class="emphasis"><em>Blinky</em></span> within the model file.
				Add the following code:
				(type it or simply copy it from the tutorial project)
			</p>
<p>
				
</p>
<div class="mediaobject">
<img src="images/020-Blinky12.png"></div>
<p> 
			
</p>
<p>
				
<span class="emphasis"><em>usercode1</em></span> will be generated at the beginning of the file, outside the class definition. 
				<span class="emphasis"><em>usercode2</em></span> will be generated within the class definition. The code imports the GUI class and instantiates the window class. Attributes for the carLights and pedLights will be declared to easily access the lights in the state machine.
				The Operation 
				<span class="emphasis"><em>destroyUser()</em></span> is a predefined operation that will be called during shutdown of the application. Within this operation, cleanup of manual coded classes can be done.
			</p>
<p>Now design the FSM of 
				<span class="emphasis"><em>Blinky</em></span>. Remember, as the name suggested 
				<span class="emphasis"><em>blinking</em></span> is a state in which the LED must be switched on and off. We will realize that by an hierarchical FSM in which the 
				<span class="emphasis"><em>blinking</em></span> state has two sub states.
			</p>
<p>Open the behavior diagram of 
				<span class="emphasis"><em>Blinky</em></span> by right clicking the 
				<span class="emphasis"><em>Blinky</em></span> actor in the outline view. Create two states named 
				<span class="emphasis"><em>blinking</em></span> and 
				<span class="emphasis"><em>off</em></span>. Right click to 
				<span class="emphasis"><em>blinking</em></span> and create a subgraph.
			</p>
<p>
				
</p>
<div class="mediaobject">
<img src="images/020-Blinky13.png"></div>
<p>
			
</p>
<p>Create the following state machine. The trigger events between 
				<span class="emphasis"><em>on</em></span> and 
				<span class="emphasis"><em>off</em></span> are the 
				<span class="emphasis"><em>timeoutTick</em></span> from the 
				<span class="emphasis"><em>timer</em></span> port. 
			</p>
<p>
				
</p>
<div class="mediaobject">
<img src="images/020-Blinky14.png"></div>
<p>
			
</p>
<p>Create entry code for both states by right clicking the state and select 
				<span class="emphasis"><em>Edit State...</em></span>
			
</p>
<p>Entry code of 
				<span class="emphasis"><em>on</em></span> is:
			</p>
<div class="literallayout">
<p>
<code class="code">timer.Start(1000);<br>
carLights.setState(TrafficLight3.YELLOW);&nbsp;<br>

</code>
</p>
</div>
<div class="blockquote">
<blockquote class="blockquote">
<p></p>
</blockquote>
</div>
<p>Entry code  of 
				<span class="emphasis"><em>off</em></span> is:
			</p>
<div class="literallayout">
<p>
<code class="code">timer.Start(1000);<br>
carLights.setState(TrafficLight3.OFF);<br>

</code>
</p>
</div>
<div class="blockquote">
<blockquote class="blockquote">
<p></p>
</blockquote>
</div>
<p>Navigate to the Top level state by double clicking the 
				<span class="emphasis"><em>/blinking</em></span> state. Create the following state machine:
			</p>
<p>
				
</p>
<div class="mediaobject">
<img src="images/020-Blinky15.png"></div>
<p>
			
</p>
<p>The trigger event from 
				<span class="emphasis"><em>off</em></span> to 
				<span class="emphasis"><em>blinking</em></span> is the 
				<span class="emphasis"><em>start</em></span> event from the 
				<span class="emphasis"><em>ControlPort</em></span>.The trigger event from 
				<span class="emphasis"><em>blinking</em></span> to 
				<span class="emphasis"><em>off</em></span> is the 
				<span class="emphasis"><em>stop</em></span> event from the 
				<span class="emphasis"><em>ControlPort</em></span>.
				Note: The transition from 
				<span class="emphasis"><em>blinking</em></span> to 
				<span class="emphasis"><em>off</em></span> is a so called group transition. This is a outgoing transition from a super state (state with sub states) without specifying the concrete leave state (state without sub states). An incoming transition to a super state is called history transition.   
			</p>
<p>Action code of the init transition is:</p>
<div class="literallayout">
<p>
<code class="code">carLights&nbsp;=&nbsp;light.getCarLights();<br>
pedLights&nbsp;=&nbsp;light.getPedLights();<br>
carLights.setState(TrafficLight3.OFF);<br>
pedLights.setState(TrafficLight2.OFF);<br>

</code>
</p>
</div>
<div class="blockquote">
<blockquote class="blockquote">
<p></p>
</blockquote>
</div>
<p>Action code from 
				<span class="emphasis"><em>blinking</em></span> to 
				<span class="emphasis"><em>off</em></span> is:
			</p>
<div class="literallayout">
<p>
<code class="code">timer.Kill();<br>
carLights.setState(TrafficLight3.OFF);&nbsp;<br>

</code>
</p>
</div>
<div class="blockquote">
<blockquote class="blockquote">
<p></p>
</blockquote>
</div>
<p>The model is complete now. You can run and debug the model as described in getting started. Have fun.</p>
<p>The complete model can be found in /org.eclipse.etrice.tutorials/model/Blinky.</p>
</div>
</body>
</html>

Back to the top