Create Variables to be used in States and Transitions
In this part of the example you create a new model in a new
library, and define the variable i to be used in the state machine. You also
makes the variable declaration visible as a text.
Create States
In this part of the example you create two states in the state
machine above, including the code.
-
From the
Behavior Authoring section of the
action bar,
click
Local State
.
A
Create Local State dialog box appears.
-
Enter
state_UpCount as
Name and
State_UpCount as
Class Name - the former
is displayed as the state name, compare with the first figure above.
-
Activate
Show Text to see the equations in the states,
without opening Modelica Text editor.
-
Click
OK.
The state is created, but with dashed outline since it must
have a transition to be defined as state.
Notes:
- The state can be
dragged and resized like other objects.
- A
context toolbar
is displayed when selecting the state, like any other object.
-
In the
Package Browser, double-click to open the state.
-
Display the
Modelica Editor.
-
In the
Modelica Editor, add code for the state, as being shown in the first figure below.
(An
outer output declaration for a variable means that
the equations containing this variable have access to the corresponding
variable declared
inner .)
The code is displayed in the state, as in the
first figure above. (You must open MySimpleStateMachine to see the complete
state machine.)
-
Create the second state,
State_DownCount, using copy/paste and changing
the state.
Note:
You can also repeat the scenario to create the second state.
Define Initial State
Each state machine must have a single initial state. You can
define an initial state using a specific kind of transition.
-
Click
Transition
.
The
Transition
is highlighted, you can now create a transition.
-
Create an initial state by:
-
Click the state that should be initial state.
-
Move your pointer to a location outside the step (for
example above it).
-
Right-click and select
Set Initial State.
A specific transition indicating that this state is
an initial state appears.
The state is now the initial state in the state machine.
Create Transitions
You can create the transitions between the states in the
example.
Before you begin:
Create states to connect with a transition.
-
Click
Transition
.
The
Transition
is highlighted, you can now create a transition.
-
Click the first state and then the second, to draw your transition.
The
Edit Transition dialog box appears.
-
Specify an integer value to set the priority. Default value is
1, which is the highest priority.
-
Add a comment in the
Comment box.
-
Do the following to create the example above:
-
Enter
i>10 as the transition
Condition (it must be a Boolean expression).
-
Clear
Immediate - the transition fires when
previous(condition)=true (this is called "delayed
transition"). If
Immediate is selected, the transition fires
when
condition=true (this is called "immediate
transition").
By default,
Immediate is selected.
-
Keep
Reset selected; the target state (and
included states, if any) is reinitialized; i. e. included state machines are
restarted in initial state and state variables are reset to their start values.
By default,
Reset is selected.
-
Keep
Synchronize cleared; if it is selected, all
state machines within the from-state must have reached the final states before
the transition condition is tested for.
By default,
Synchronize is not selected.
-
Keep
Priority at 1, it can be set to an integer
corresponding to the priority (1 is the highest priority).
By default,
Priority is set to 1 (highest).
- Optional:
You can add a comment in the
Comment box.
-
Click
OK to validate and create the transition.
The first transition in the first figure above is created, as
an arrow between two states. It indicates the above selections:
- The arrow direction is
from the source state to the destination state.
- The perpendicular bar of
the transition is close to the destination state for an immediate transition,
and close to the source state for a delayed transition.
- The arrow is filled for
a reset transition, otherwise non-filled.
- A synchronize transition
has an "inverted fork" at the source state.
- Priority is shown
preceding the condition if not equal to one (e. g.
2: i>1 .)
- Optional:
You can change the location of the transition label the following
way:
-
Right-click the transition and select
.
-
Move the mouse to select location of label.
-
Click to validate location.
-
Repeat the previous steps to create the transition between the first
and the second state (see figure above).
Note:
You can make "corners" on the transition by clicking when you
draw it.
You
have now created the simple state machine in the first figure above. You might
want to save it before going on to simulate it.
Simulate the State Machine
You can simulate the state machine.
-
Set the execution
Stop time to
20. See
Simulating a Behavior.
-
Execute your behavior. See
Simulating a Behavior.
The model is simulated.
Note:
Since you have not defined a clock in this scenario, a
default clock is used, with a default period of 1 second. This time can be
changed in the
Variable Browser - the signal is
Clocks.BaseClock_0.period.
-
Plot the variable
i . See
Creating Plots.
The resulting plot is:
Note that the states that are not active are dimmed:
-
To follow the execution in slower pace, you can synchronize to
real time. See
Simulating a Behavior Synchronized with Real Time.
Extend to a Hierarchical State Machine
You can use the result above to create a hierarchical state
machine.
-
Select the state machine created above, right-click and select
Cut.
You use the result in the clipboard to create the
enclosed state machine in the hierarchical state machine below.
-
From the
Behavior Authoring section of the
action bar,
click
Local State
.
A
Create Local State dialog box appears.
-
Enter
state1 as
Name and
State1 as
Class Name - the former is displayed as the
state name, compare with the first figure above.
-
Activate
Show Text to see the equations in the states
without opening Modelica Text editor.
-
Click
OK.
The state is created, but with dashed outline since it
must have a transition to be defined as state.
-
Define
state1 as the initial state. See
Define Initial State.
-
Open
state1 and use
Ctrl-V to paste your previous state machine
into it.
-
Declare
i in
state1; the declaration should correspond to
the blue text in
state1 in the figure above.
-
Open
MySimpleStateMachine.
You should now see
state1 with the enclosed state machine like
in the figure above.
Note:
Enclosed states are displayed by default. This is
the result of the annotation
annotation(showDiagram=true) in the code for the
state. To hide the enclosed states, change the code to
annotation(showDiagram=false) . You have to do
this for each state for which you want to hide the enclosed states.
-
Create
state2, including the code.
-
Create the transitions between
state1 and
state2.
-
Save your state machine.
-
Simulate your state machine. Select a simulation stop time of
for example 30 to be able to see the effect of all transitions.
|