Implement the Behavior

Implement the Behavior

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 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 on 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 transition dialog should look like this:

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(param);. 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: (Note that the arrows peak changes if the transition contains action code.)

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

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: (type it or simply copy it from the tutorial project)

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. Remember, as the name suggested blinking is a state in which the LED must be switched on and off. We will realize that by an hierarchical FSM in which the blinking state has two sub states.

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.

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

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

Entry code of on is:

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

Entry code of off is:

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

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

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. Note: The transition from blinking to off 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.

Action code of the init transition is:

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

Action code from blinking to off is:

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

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

The complete model can be found in /org.eclipse.etrice.tutorials/model/Blinky.