Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7553874ea3080285ec123851ca44c3c2072726ec (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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
h1. Tutorial Blinky

h2. Scope

This tutorial describes how to use the ??TimingService??, combine a generated model with manual code and how to modeling a hierarchical state machine. The idea of the tutorial is, to switch a LED on and off. The behavior of the LED should be: blinking in a one second interval for 5 seconds, stop blinking for 5 seconds, blinking, stop,...  
For this exercise we will use a little GUI class that will be used in more sophisticated tutorials too. The GUI simulates a pedestrian traffic crossing. For now, just a simple LED simulation will be used from the GUI. 

To use the GUI please copy the package ??de.protos.PedLightGUI?? to your ??src?? directory. The package contains four java classes which implements a little window with a 3-light traffic light which simulates the signals for the car traffic and a 2-light traffic light which simulates the pedestrian signals.

The GUI looks like this:

!images/020-Blinky08.PNG!

Within this tutorial we just will switching on and off the yellow light.

You will perform the following steps:

# create a new model from scratch
# define a protocol
# create an actor structure
# create a hierarchical state machine
# use the predefined ??TimingService??
# combine manual code with generated code
# build and run the model
# open the message sequence chart

h2. Create a new model from scratch

Remember exercise ??HelloWorld??.
Create a new eTrice project and name it ??Blinky??
Open the ??Blinky.room?? file and copy the following code into the file or use content assist to create the model.

bc.. 
RoomModel Blinky {

	LogicalSystem System_Blinky {
		SubSystemRef subsystem : SubSystem_Blinky
	}

	SubSystemClass SubSystem_Blinky {
		ActorRef application : BlinkyTop
	}

	ActorClass BlinkyTop {
	}
}
bq. 

h2. Add two additional actor classes

Position the cursor outside any class definition and right click the mouse within the editor window. From the context menu select ??Content Assist??  

!images/020-Blinky02.png!

Select ??ActorClass - actor class skeleton?? and name it ??Blinky??.

!images/020-Blinky01.PNG! 

Repeat the described procedure and name the new actor ??BlinkyController??.

Save the model and visit the outline view.

h2. Create a new protocol

With the help of ??Content Assist?? create a ??ProtocolClass?? and name it ??BlinkyControlProtocol??.
Inside the brackets use the ??Content Assist?? (CTRL+Space) to create two incomming messages called ??start?? and ??stop??.

The resulting code should look like this:

!images/020-Blinky03.PNG! 

With Ctrl-Shift+F or selecting ??Format?? from the context menu you can format the text. Mention that all elements are displayed in the outline view.

h2. Import the Timing Service

Switching on and off the LED is timing controlled. Therefore a timing service is needed. To import the timing service in the outline view right click to ??SubSystem_Blinky??. Select ??Edit Structure??. Drag and Drop an ??ActorRef?? to the ??SubSystem_Blinky?? and name it ??application??. From the actor class drop down list select ??BlinkyTop??. Do the same clicks for the timing service. Name it ??timingService?? and from the drop down list select ??room.basic.service.timing.ATimingService??. Draw a ??LayerConnection?? from ??application?? to each service provision point (SPP) of the ??timingService??. The resulting structure should look like this:

!images/020-Blinky06.PNG! 

The current version of eTrice does not provide a graphical element for a service access point (SAP). Therefore the SAPs to access the timing service must be added in the .room file. Open the ??Blinky.room?? file and navigate to the ??Blinky?? actor. Add the following line to the structure of the actor:

bc. SAP timer: room.basic.service.timing.PTimeout

Do the same thing for ??BlinkyController??.

The resulting code should look like this:

!images/020-Blinky07.PNG!


h2. Finish the model structure

From the outline view right click to ??Blinky?? and select ??Edit Structure??. Drag and Drop an ??Interface Port?? to the boarder of the ??Blinky?? actor. Note that an interface port is not possible inside the the actor. Name the port ??ControlPort?? and select ??BlinkyControlProtocol?? from the drop down list. Uncheck ??Conjugated?? and ??Is Relay Port??. Klick ??ok??. The resulting structure should look like this:


!images/020-Blinky04.PNG!

Repeat the above steps for the ??BlinkyController??. Make the port ??Conjugated??

Keep in mind that the protocol defines ??start?? and ??stop?? as incoming messages. ??Blinky?? receives this messages and therefore ??Blinky??'s ??ControlPort?? must be a base port and ??BlinkyController??'s ??ControlPort?? must be a conjugated port.


From the outline view right click ??BlinkyTop?? and select ??Edit Structure??.

Drag and Drop an ??ActorRef?? inside the ??BlinkyTop?? actor. Name it ??blinky??. From the actor class drop down list select ??Blinky??. Do the same for ??controller??. Connect the ports via the binding tool. The resulting structure should look like this:

!images/020-Blinky05.PNG!

h2. Implement the Behavior

The application should switch on and off the LED for 5 seconds in a 1 second interval, than 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 ??Blinky??. The 5 second interval should be implemented in ??BlinkyController??. First implement the Controller.

Right click to ??BlinkyController?? and select ??Edit Behavior??.
Drag and Drop the ??Initial Point?? and two ??States?? into the top state. Name the states ??on?? and ??off??. 
Use the ??Transition?? tool to draw transitions from ??init?? to ??off?? from ??on?? to ??off?? and from ??off?? to ??on??.

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.

The dialog should look like this:

!{width=500px}images/020-Blinky09.PNG! 

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 ??port.message(p1,p2);??. In this example ??ControlPort.start()?? sends the ??start?? message via the ??ControlPort?? to the outside world. Assuming that ??Blinky?? is connected to this port, the message will start the one second blinking FSM. It is the same thing with the ??timer??. The SAP is also a port and follows the same rules. So it is clear that ??timer.Start(5000);?? will send the ??Start?? message to the timing service. The timing service will send a ??timeoutTick?? message back after 5000ms.

Within each transition the timer will be restarted and the appropriate message will be sent via the ??ControlPort??. 

The resulting state machine should look like this:

!images/020-Blinky10.PNG!

Save the diagram and inspect the ??Blinky.room?? file. The ??BlinkyController?? should look like this:

!images/020-Blinky11.PNG! 
 
Now we will implement ??Blinky??. Due to the fact that ??Blinky?? interacts with the GUI class a view things must to be done in the model file.

Double click ??Blinky?? in the outline view to navigate to ??Blinky?? within the model file.
Add the following code:

!images/020-Blinky12.PNG! 

??usercode1?? will be generated at the beginning of the file, outside the class definition. ??usercode2?? 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 ??destroyUser()?? is a predefined operation that will be called during shutdown of the application. Within this operation, cleanup of manual coded classes can be done.
 
 
 
Now design the FSM of ??Blinky??. Open the behavior diagram of ??Blinky?? by right clicking the ??Blinky?? actor in the outline view. Create two states named ??blinking?? and ??off??. Right click to ??blinking?? and create a subgraph.

!images/020-Blinky13.PNG!

Create the following state machine. The trigger events between ??on?? and ??off?? are the ??timeoutTick?? from the ??timer?? port. 

!images/020-Blinky14.PNG!

Create entry code for both states by right clicking the state and select ??Edit State...??

Entry code of ??on?? is:

bc..  
timer.Start(1000);
carLights.setState(TrafficLight3.YELLOW); 
bq. 

 
Entry code  of ??off?? is:

bc.. 
timer.Start(1000);
carLights.setState(TrafficLight3.OFF);
bq. 

Navigate to the Top level state by double clicking the ??/blinking?? state. Create the following state machine:

!images/020-Blinky15.PNG!

The trigger event from ??off?? to ??blinking?? is the ??start?? event from the ??ControlPort??.The trigger event from ??blinking?? to ??off?? is the ??stop?? event from the ??ControlPort??.

Action code of the init transition is:

bc.. 
carLights = light.getCarLights();
pedLights = light.getPedLights();
carLights.setState(TrafficLight3.OFF);
pedLights.setState(TrafficLight2.OFF);
bq. 

Action code from ??blinking?? to ??off?? is:

bc.. 
timer.Kill();
carLights.setState(TrafficLight3.OFF); 
bq. 

The complete resulting model looks like this:

bc.. 
RoomModel Blinky {

	LogicalSystem System_Blinky {
		SubSystemRef subsystem: SubSystem_Blinky
	}

	SubSystemClass SubSystem_Blinky {
		ActorRef application: BlinkyTop
		ActorRef timingService: room.basic.service.timing.ATimingService
		LayerConnection ref application satisfied_by timingService.timer
		LayerConnection ref application satisfied_by timingService.timeout
	}

	ActorClass BlinkyTop {
		Structure {
			ActorRef blinky: Blinky
			ActorRef controller: BlinkyController
			Binding blinky.ControlPort and controller.ControlPort
		}
		Behavior { }
	}

	ActorClass Blinky {
		Interface {
			Port ControlPort: BlinkyControlProtocoll
		}
		Structure {
			usercode1{
				"import de.protos.PedLightGUI.*;"
			}
			usercode2 {
				"private PedestrianLightWndNoTcp light = new PedestrianLightWndNoTcp();"
				"private TrafficLight3 carLights;"
				"private TrafficLight2 pedLights;"
				
			}
			external Port ControlPort
			SAP timer: room.basic.service.timing.PTimeout
		}
		Behavior {
			Operation destroyUser(){
				"light.closeWindow();"
			}
			StateMachine {
				Transition init: initial -> off {
					action {
						"carLights = light.getCarLights();"
						"pedLights = light.getPedLights();"
						"carLights.setState(TrafficLight3.OFF);"
						"pedLights.setState(TrafficLight2.OFF);"
					}
				}
				Transition tr0: off -> tp0 of blinking {
					triggers {
						<start: ControlPort>
					}
				}
				Transition tr1: blinking -> off {
					triggers {
						<stop: ControlPort>
					}
					action {
						"timer.Kill();"
						"carLights.setState(TrafficLight3.OFF);"
					}
				}
				State off
				State blinking {
					subgraph {
						Transition tr0: my tp0 -> on
						Transition tr1: on -> off {
							triggers {
								<timeoutTick: timer>
							}
						}
						Transition tr2: off -> on {
							triggers {
								<timeoutTick: timer>
							}
						}
						Transition init: initial -> on { }
						EntryPoint tp0
						State on {
							entry {
								"timer.Start(1000);"
								"carLights.setState(TrafficLight3.YELLOW);"
							}
						}
						State off {
							entry {
								"timer.Start(1000);"
								"carLights.setState(TrafficLight3.OFF);"
							}
						}
					}
				}
			}
		}
	}

	ActorClass BlinkyController {
		Interface {
			conjugated Port ControlPort: BlinkyControlProtocoll
		}
		Structure {
			external Port ControlPort
			SAP timer: room.basic.service.timing.PTimeout
		}
		Behavior {
			StateMachine {
				Transition init: initial -> on {
					action {
						"timer.Start(5000);"
						"ControlPort.start();"
					}
				}
				Transition goOff: on -> off {
					triggers {
						<timeoutTick: timer>
					}
					action {
						"ControlPort.stop();"
						"timer.Start(5000);"
					}
				}
				Transition goOn: off -> on {
					triggers {
						<timeoutTick: timer|timeoutTick: timer>
					}
					action {
						"ControlPort.start();"
						"timer.Start(5000);"
					}
				}
				State on
				State off
			}
		}
	}

	ProtocolClass BlinkyControlProtocoll {
		incoming {
			Message start()
			Message stop()
		}
		outgoing { }
	}

}
bq. 

The model is complete now. You can run and debug the model as described in getting started. Have fun.

h2. Summary

Run the model and take look at the generated MSCs. Inspect the generated code to understand the runtime model of eTrice. Within this tutorial you have learned how to create a hierarchical FSM with group transitions and history transitions and you have used entry code. You are now familiar with the basic features of eTrice. The further tutorials will take this knowledge as a precondition.

Back to the top