summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Jung2011-08-02 15:56:55 (EDT)
committerThomas Jung2011-08-02 15:56:55 (EDT)
commitbcfc79a7a8d049ff9f173af976d9dbe99f7923c4 (patch)
tree892a0e4aebf2f2aa71bc6028532c518cca2dc4b0
parentcb58fb5d6e87f197b15647a68bfb4fffd860df2d (diff)
downloadorg.eclipse.etrice-bcfc79a7a8d049ff9f173af976d9dbe99f7923c4.zip
org.eclipse.etrice-bcfc79a7a8d049ff9f173af976d9dbe99f7923c4.tar.gz
org.eclipse.etrice-bcfc79a7a8d049ff9f173af976d9dbe99f7923c4.tar.bz2
[doc] PedLight tutorial added
-rw-r--r--plugins/org.eclipse.etrice.doc/doc/030-tutorial-ped-lights.textile237
1 files changed, 237 insertions, 0 deletions
diff --git a/plugins/org.eclipse.etrice.doc/doc/030-tutorial-ped-lights.textile b/plugins/org.eclipse.etrice.doc/doc/030-tutorial-ped-lights.textile
new file mode 100644
index 0000000..c6a22d2
--- /dev/null
+++ b/plugins/org.eclipse.etrice.doc/doc/030-tutorial-ped-lights.textile
@@ -0,0 +1,237 @@
+h1. Tutorial Pedestrian Lights
+
+h2. Scope
+
+The scope of this tutorial is to demonstrate how to receive model messages from outside the model. Calling methods which are not part of the model is simple and you have already done this within the blinky tutorial (this is the other way round model => external code). Receiving events from outside the model is a very common problem and a very frequently asked question. Therefore this tutorial shows how an external event (outside the model) can be received from the model.
+
+This tutorial is not like hello world or blinky. Be familiar with the basic tool features is a precondition for this tutorial. The goal is to understand the mechanism not to learn the tool features.
+
+The idea behind the exercise is, to control a Pedestrian crossing light. We will use the same GUI as for the blinky tutorial but now we will use the ??REQUEST?? button to start a FSM, which controls the traffic lights.
+
+!images/020-Blinky08.PNG!
+
+The ??REQUEST?? must lead to a model message which starts the activity of the lights.
+
+There are a view possibilities to receive external events (e.g. TCP/UDP Socket, using OS messaging mechanism), but the easiest way is, to make a port usable from outside the model. To do that a view steps are necessary:
+# specify the messages (within a protocol) which should be sent into the model
+# model an actor with a port (which uses the specified protocol) and connect the port to the receiver
+# the external code should know the port (import of the port class)
+# the external code should provide a registration method, so that the actor is able to allow access to this port
+# the port can be used from the external code
+
+h2. Setup the model
+
+* Use the ??New Model Wizzard?? to create a new eTrice project and name it ??PedLightsController??.
+* Copy the package ??de.protos.PedLightGUI?? to your ??src?? directory (see blinky tutorial).
+* Uncoment line 15 (import), 36, 122 (usage) and 132-134 (registration).
+* Copy the following model to your model file:
+
+bc..
+RoomModel PedLightsController {
+
+ LogicalSystem LogSys_PedLights {
+ SubSystemRef application: SubSys_PedLights
+ }
+
+ SubSystemClass SubSys_PedLights {
+ ActorRef PedLightsTopRef: PedLightsTop
+ ActorRef timingService: room.basic.service.timing.ATimingService
+ LayerConnection ref PedLightsTopRef satisfied_by timingService.timer
+ LayerConnection ref PedLightsTopRef satisfied_by timingService.timeout
+ }
+
+ ActorClass PedLightsTop {
+ Structure {
+ ActorRef adapter: GuiAdapter
+ ActorRef controller: Controller
+ Binding adapter.ControlPort and controller.ControlPort
+ }
+ Behavior { }
+ }
+
+ ActorClass GuiAdapter {
+ Interface {
+ conjugated Port ControlPort: PedControlProtocol
+ }
+ Structure {
+ usercode1 {
+ "import PedLightGUI.*;"
+ }
+ usercode2 {
+ "private PedestrianLightWndNoTcp lights = new PedestrianLightWndNoTcp(\"Pedestrian Lights\",\" external port connection \");"
+ "private TrafficLight3 carLights;"
+ "private TrafficLight2 pedLights;"
+ }
+ external Port ControlPort
+ }
+ Behavior {
+ Operation destroyUser() {
+ "lights.closeWindow();"
+ }
+ StateMachine {
+ Transition init: initial -> running {
+ action {
+ "carLights=lights.getCarLights();"
+ "pedLights=lights.getPedLights();"
+ "carLights.setState(TrafficLight3.OFF);"
+ "pedLights.setState(TrafficLight2.OFF);"
+ "lights.setPort(ControlPort);"
+ }
+ }
+ Transition tr0: running -> running {
+ triggers {
+ <setCarLights: ControlPort>
+ }
+ action {
+ "carLights.setState(state);"
+ }
+ }
+ Transition tr1: running -> running {
+ triggers {
+ <setPedLights: ControlPort>
+ }
+ action {
+ "pedLights.setState(state);"
+ }
+ }
+ State running
+ }
+ }
+ }
+
+ ActorClass Controller {
+ Interface {
+ Port ControlPort: PedControlProtocol
+ }
+ Structure {
+ usercode1 {
+ "import PedLightGUI.*;"
+ }
+ external Port ControlPort
+ SAP timer: room.basic.service.timing.PTimeout
+ }
+ Behavior {
+ StateMachine {
+ Transition init: initial -> off { }
+ Transition tr0: off -> carsGreen {
+ triggers {
+ <start: ControlPort>
+ }
+ action {
+ "timer.Start(700);"
+ "ControlPort.setCarLights(TrafficLight3.GREEN);"
+ "ControlPort.setPedLights(TrafficLight2.RED);"
+ }
+ }
+ Transition tr1: carsGreen -> carsYellow {
+ triggers {
+ <timeoutTick: timer>
+ }
+ action {
+ "timer.Start(700);"
+ "ControlPort.setCarLights(TrafficLight3.YELLOW);"
+ "ControlPort.setPedLights(TrafficLight2.RED);"
+ }
+ }
+ Transition tr2: carsYellow -> carsRed {
+ triggers {
+ <timeoutTick: timer>
+ }
+ action {
+ "timer.Start(1500);"
+ "ControlPort.setCarLights(TrafficLight3.RED);"
+ "ControlPort.setPedLights(TrafficLight2.GREEN);"
+ }
+ }
+ Transition tr3: carsRed -> carsYellowRed {
+ triggers {
+ <timeoutTick: timer>
+ }
+ action {
+ "timer.Start(700);"
+ "ControlPort.setCarLights(TrafficLight3.YELLOW_RED);"
+ "ControlPort.setPedLights(TrafficLight2.RED);"
+ }
+ }
+ Transition tr4: carsYellowRed -> carsGreen2 {
+ triggers {
+ <timeoutTick: timer>
+ }
+ action {
+ "timer.Start(700);"
+ "ControlPort.setCarLights(TrafficLight3.GREEN);"
+ "ControlPort.setPedLights(TrafficLight2.RED);"
+ }
+ }
+ Transition tr5: carsGreen2 -> off {
+ triggers {
+ <timeoutTick: timer>
+ }
+ action {
+ "ControlPort.setCarLights(TrafficLight3.OFF);"
+ "ControlPort.setPedLights(TrafficLight2.OFF);"
+ }
+ }
+ State off
+ State carsGreen
+ State carsYellow
+ State carsRed
+ State carsYellowRed
+ State carsGreen2
+ }
+ }
+ }
+
+ ProtocolClass PedControlProtocol {
+ incoming {
+ Message start()
+ }
+ outgoing {
+ Message setCarLights(state: int32)
+ Message setPedLights(state: int32)
+ }
+ }
+}
+bq.
+
+* Arrange the Structure and the Statemachines to understand the model
+
+!images/030-PedLights01.PNG!
+The ??GuiAdapter?? represents the interface to the external code. It registers its ??ControlPort?? by the external code.
+
+!images/030-PedLights02.PNG!
+Visit the initial transition to understand the registration. The actor handles the incoming messages as usual and controls the traffic lights as known from blinky.
+
+!images/030-PedLights03.PNG!
+The ??Controller?? receives the ??start?? message and controls the timing of the lights. Note that the ??start?? message will be sent from the external code whenever the ??REQUEST?? button is pressed.
+
+* Visit the model and take a closer look to the following elements:
+# PedControlProtocol => notice that the start message is defined as usual
+# Initial transition of the ??GuiAdapter?? => see the registration
+# The ??Controller?? => notice that the ??Controller?? receives the external message (not the ??GuiAdapter??). The ??GuiAdapter?? just provides its port and handles the incoming messages.
+# Visit the hand written code => see the import statement of the protocol class and the usage of the port.
+
+* Generate and test the model
+* Take a look at the generated MSC => notice that the start message will shown as if the ??GuiAdapter?? had sent it.
+
+!images/030-PedLights04.PNG!
+
+h2. Why does it work and why is it save?
+
+The tutorial shows that it is generally possible to use every port from outside the model as long as the port knows its peer. This is guaranteed by describing protocol and the complete structure (especially the bindings) within the model.
+The only remaining question is: Why is it save and does not hurt the "run to completion" semantic. To answer this question, take a look at the ??MessageService.java?? from the runtime environment. There you will find the receive method which puts each message into the queue.
+
+bc..
+ @Override
+ public synchronized void receive(Message msg) {
+ if (msg!=null) {
+ messageQueue.push(msg);
+ notifyAll(); // wake up thread to compute message
+ }
+ }
+bq.
+
+This method is synchronized. That means, regardless who sends the message, the queue is secured. If we later on (e.g. for performance reasons in C/C++) distinguish between internal and external senders (same thread or not), care must be taken to use the external (secure) queue.
+
+
+