Construct and Run a Stateflow Chart
A Stateflow® chart is a graphical representation of a finite state machine consisting of states, transitions, and data. You can create a Stateflow chart to define how a system reacts to external input signals, events, and time-based conditions.
For instance, this Stateflow chart presents the logic underlying a half-wave rectifier. The chart
contains two states labeled On
and Off
. In the
On
state, the chart output signal y
is
equal to the input x
. In the Off
state, the
output signal is set to zero. When the input signal crosses some threshold
t0
, the chart transitions between these states. The actions
in each state update the value of y
at each time step of the
simulation.
This example shows how to create and simulate this Stateflow chart.
Construct the Stateflow Chart
Open the Stateflow Editor
The Stateflow Editor is a graphical environment for designing state transition diagrams, flow charts, state transition tables, and truth tables.
The main components of the Stateflow Editor are the chart canvas, the object palette, and the Symbols pane.
The chart canvas is a drawing area where you create a chart by combining states, transitions, and other graphical elements.
On the left side of the canvas, the object palette displays a set of tools for adding graphical elements to your chart.
On the right side of the canvas, in the Symbols pane, you add new data, events, and messages to the chart and resolve any undefined or unused symbols.
Add States and Transitions
From the object palette, click the State icon and move the pointer to the chart canvas. A state with its default transition appears. To place the state, click a location on the canvas. At the text prompt, enter the state name
On
and the state actiony = x
.Add another state. Right-click and drag the
On
state. Blue graphical cues help you to align your states horizontally or vertically. The name of the new state changes toOff
. Double-click the state and modify the state action toy = 0
.Realign the two states and pause on the space between the two states. Blue transition cues indicate several ways in which you can connect the states. To add transitions, click the appropriate cue.
Alternatively, to draw a transition, click and drag from the edge of one state to the edge of the other state.
Double-click each transition and type the appropriate transition condition
x<t0
orx>=t0
. The conditions appear inside square brackets.Clean up the chart:
To improve clarity, move each transition label to a convenient location above or below its corresponding transition.
To align and resize the graphical elements of your chart, in the Format tab, click Auto Arrange or press Ctrl+Shift+A.
To resize the chart to fit the canvas, press the space bar or click the Fit To View icon .
Resolve Undefined Symbols
Before you can execute your chart, you must define each symbol that you use in the chart and specify its scope (for example, input data, output data, or local data).
In the Modeling tab, under Design Data, select Symbols Pane.
In the Symbols pane, undefined symbols are marked with a red error badge .
In the Symbols pane, click Resolve Undefined Symbols . The Type column displays the suggested scope for each undefined symbol based on its usage in the chart. The Stateflow Editor resolves the symbols
x
andt0
as input data andy
as output data .Because the threshold
t0
does not change during simulation, change its scope to constant data. In the Type column, click the data type icon next tot0
and selectConstant Data
.Set the value for the threshold
t0
. In the Value column, click the blank entry next tot0
and enter a value of 0.Save your Stateflow chart.
Simulate the Model
To simulate the Simulink model, connect the chart block to other blocks in the model through input and output ports.
To return to the Simulink Editor, on the explorer bar at the top of the canvas, click the name of the Simulink model:
rectify
. If the explorer bar is not visible, click the Hide/Show Explorer Bar icon at the top of the object palette.Add a source to the model:
From the Simulink Sources library, add a Sine Wave (Simulink) block.
Double-click the Sine Wave block and set the Sample time to 0.2.
Connect the output of the Sine Wave block to the input of the Stateflow chart.
Label the signal as
x
.
Add a sink to the model:
From the Simulink Sinks library, add a Scope (Simulink) block with two input ports.
Connect the output of the Sine Wave block to the first input of the Scope block.
Connect the output of the Stateflow chart to the second input of the Scope block.
Label the signal as
y
.
Save the Simulink model.
To simulate the model, click Run . During the simulation, the Stateflow Editor highlights active states and transitions through chart animation.
After you simulate the model, double-click the Scope block. The scope displays the graphs of the input and output signals to the charts.
The simulation results show that the rectifier filters out negative input values.