execute and unit test stateflow chart objects
a standalone stateflow® chart is a matlab® class that defines the behavior of a finite state machine. standalone charts implement classic chart semantics with matlab as the action language. you can program the chart by using the full functionality of matlab, including those functions that are restricted for code generation in simulink®. for more information, see create stateflow charts for execution as matlab objects.
example of a standalone stateflow chart
the file sf_chart.sfx
contains the standalone stateflow chart sf_chart
. the chart has local data u
, x
, and y
.
this example shows how to execute this chart from the stateflow editor and in the matlab command window.
execute a standalone chart from the stateflow editor
to unit test and debug a standalone chart, you can execute the chart directly from the stateflow editor. during execution, you enter data values and broadcast events from the user interface.
open the chart in the stateflow editor:
edit sf_chart.sfx
in the symbols pane, enter a value of
u
= 1 and click run . the chart executes its default transition and:initializes
x
to the value of 0.makes state
a
the active state.assigns
y
to the value of 1.increases the value of
x
to 1.
the chart animation highlights the active state
a
. the symbols pane displays the valuesu
= 1,x
= 1, andy
= 1. the chart maintains its current state and local data until the next execution command.click step . because the value of
u
does not satisfy the condition[u<0]
to transition out of statea
, this state remains active and the values ofx
andy
increase to 2. the symbols pane now displays the valuesu
= 1,x
= 2, andy
= 2.in the symbols pane, enter a value of
u
= −1 and click step . the negative data value triggers the transition to stateb
. the symbols pane displays the valuesu
= −1,x
= 1, andy
= 3.you can modify the value of any chart data in the symbols pane. for example, enter a value of
x
= 3. the chart will use this data value in the next time execution step.enter a value of
u
= 2 and click step . the chart transitions back to statea
. the symbols pane displays the valuesu
= 2,x
= 4, andy
= 5.to stop the chart animation, click stop .
to interrupt the execution and step through each action in the chart, add breakpoints before you execute the chart. for more information, see debug a standalone stateflow chart.
execute a standalone chart in matlab
you can execute a standalone chart in matlab without opening the stateflow editor. if the chart is open, then the editor highlights active states and transitions through chart animation.
open the chart in the stateflow editor. in the matlab command window, enter:
edit sf_chart.sfx
create the stateflow chart object by using the name of the
sfx
file for the standalone chart as a function. specify the initial value for the datau
as a name-value pair.s = sf_chart(u=1)
this command creates the chart objectstateflow chart execution function y = step(s) local data u : 1 x : 1 y : 1 active states: {'a'}
s
, executes the default transition, and initializes the values ofx
andy
. the stateflow editor animates the chart and highlights the active statea
.to execute the chart, call the
step
function. for example, suppose that you call thestep
function with a value ofu
= 1:step(s,u=1)
disp(s)
because the value ofstateflow chart execution function y = step(s) local data u : 1 x : 2 y : 2 active states: {'a'}
u
does not satisfy the condition[u<0]
to transition out of statea
, this state remains active and the values ofx
andy
increase to 2.execute the chart again, this time with a value of
u
= −1:step(s,u=-1)
disp(s)
the negative data value triggers the transition to statestateflow chart execution function y = step(s) local data u : -1 x : 1 y : 3 active states: {'b'}
b
. the value ofx
decreases to 1 and the value ofy
increases to 3.to access the value of any chart data, use dot notation. for example, you can assign a value of 3 to the local data
x
by entering:s.x = 3
the standalone chart will use this data value in the next time execution step.stateflow chart execution function y = step(s) local data u : -1 x : 3 y : 3 active states: {'b'}
execute the chart with a value of
u
= 2:step(s,u=2)
disp(s)
the chart transitions back to statestateflow chart execution function y = step(s) local data u : 2 x : 4 y : 5 active states: {'a'}
a
and modifies the values ofx
andy
.to stop the chart animation, delete the stateflow chart object
s
:delete(s)
execute multiple chart objects
you can execute multiple chart objects defined by the same standalone chart. concurrent chart objects maintain their internal state independently, but remain associated to the same chart in the editor. the chart animation reflects the state of the chart object most recently executed. executing multiple chart objects while the stateflow editor is open can produce confusing results and is not recommended.
see also
| |