For demos, application examples, tutorials, user stories, and pricing:
• Visit www.mathworks.com
• Contact The MathWorks directly
US & Canada 508-647-7000
Benelux +31 (0)182 53 76 44France +33 (0)1 41 14 67 14Germany +49 (0)241 470 750Italy +39 (011) 2274 700Korea +82 (0)2 6006 5114Spain +34 93 362 13 00Sweden +46 (8)505 317 00Switzerland +41 (0)31 950 60 20UK +44 (0)1223 423 200
Visit www.mathworks.com to obtain contact information for authorized MathWorks representatives in countries throughout Asia Pacific, Latin America, the Middle East, Africa, and the rest of Europe.
Stateflow® is an interactive design and simulation
tool for event-driven systems. Stateflow pro-
vides the language elements required to describe
complex logic in a natural, readable, and under-
standable form. It is tightly integrated with
MATLAB® and Simulink®, providing an efficient
environment for designing embedded systems that
contain control, supervisory, and mode logic.
Stateflow charts enable the graphical representa-
tion of hierarchical and parallel states and the
event-driven transitions between them. Stateflow
augments traditional statecharts with the innova-
tive capabilities of control flow, MATLAB and
graphical functions, truth tables, temporal opera-
tors, directed-event broadcasting, and support for
integrating hand-written C code.
You can automatically generate C code from
Stateflow charts using Stateflow® Coder (available
separately).
Stateflow® 6Design and simulate event-driven systems
KEY FEATURES■ Provides language elements, hierarchy, parallelism, and deter-
ministic execution semantics for describing complex logic in a
natural and understandable form
■ Defines functions graphically, using flow diagrams; procedurally,
using the MATLAB language; and in tabular form, with truth tables
■ Schedules transitions and events using temporal operators
■ Incorporates custom and legacy C code with input and output
arguments
■ Supports vector, matrix, and fixed-point data types
■ Provides viewing and logging of states and data
■ Animates Stateflow charts to enhance understanding of the
system and facilitate debugging
■ Performs static checks, including checks for ill-specified truth tables
■ Performs run-time checks for transition conflicts, cyclic problems,
state inconsistencies, data-range violations, and overflow conditions
■ Includes an integrated debugger for setting graphical break-
points, stepping through models, and browsing data
The fault detection, isolation, and recovery system of an aircraft elevator. You can use Simulink and Stateflow to simulate and study the effects of switching modes on the control system and aircraft dynamics.
elev_pos pos_bus
Signal Conditioning
and
Fault Detection
modes
left positions
right positions
control points
Pilot Commands and Control
low_press
R_pos_fail
L_pos_fail
LDL_mode
Mode Logic
modes
control points
hydraulic pressures
elevator positions
Hyraulic Acutators
and Elevators
3
<low_press>
44
42
<R_pos_fail>
2
<L_pos_fail>
2
<Left positions>
{14}
{14}
{14}
{14}
2
<Right positions>
2
3
3
<hydraulic pressure>
2{2
2{2}elev_pos pos_bus
low_press
R_pos_fail
L_pos_fail
LDL_mode
3
<low_press>
42
<R_pos_fail><R_pos_fail>
2
<L_pos_fail>
{14} {14} {14} {14} {14}
{14} {14}
Working with StateflowStateflow is part of an integrated simulation
environment in which you can model, simu-
late, and analyze complex dynamic systems.
By using Stateflow with Simulink, you can
create models of your embedded software
that combine the logical behavior (such as
fault detection and mode switching) with
the algorithmic behavior (such as feedback
control and signal conditioning). You can
also create a model of the system and its
environment in Simulink and run hybrid
simulations to study the interactions
between the two.
Simulink model of the same system, incorporating the electric motor (modeled in SimPowerSystems) and the window mechanism (modeled in SimMechanics) to determine the load on the motor and enable the logic to detect obstacles in the window’s path.
Stateflow chart of the control logic for a power window system.
Window_Switches
Window_Mechanics
Visulization
Driver Up
Driver Down
Passenger Up
Passenger Down
Obstacle
Test Cases
DOC
Requirements
obstacle
position
velocity
force
Obstacle_Effects
Motor_and_Electronics
Convert(SI) IaADC
driver_up
driver_down
passenger_up
passenger_down
moveUp
moveDown
Control
Logicon tick: switches();
Remain in Stop state until a NEUTRALevent triggers Stop.Mode to be Ready. go()
function switches()
truthtable
Fail safe time out after continuousmovement of more than five secondsStop /
go.neutral();
EmergencyDown /go.down();
Moveentry: obstacles.init();on tick: obstacles.detect();
EndStopAtTop /go.down();
Position 2 Direction 1
Error
Mode 1
AtBottom /go.up();
obstacles() function
Mode 2
ErrorNearBottom Up /go.up();
NearTop
Down /go.down();
NotSureYet
Ready
Auto
Waiting
NotSureWhere
Manual
NEUTRALafter(FAILSAFE_TIMEOUT,tick)
UP | DOWN DOWN[~in(Down)]
DOWN[in(AtTop)]
after(_70ms,tick)
UP [in(Ready)] UP[~in(Up)]
NEUTRAL[in(Manual)]
[after(_1s,tick)] en(Direction.Up) | ...en(Direction.Down)
NEUTRALOBSTACLE NEUTRAL
[after(_200ms,tick) ...& before(_1s,tick)]
after(_1s,tick)[in(Direction.Down)] ENDSTOP
4x4 Switch
Packet Contents:1) 92) Total length (>=5)3) Destination4) Source5) Data (Integer):Length-1) Data (Integer)Length) 0
1/z
0
0
0
Dummy Event
Dummy Event
Dummy Event
Dummy Event
Clock
Fifos
Selector
Fifo
Start_Popping
SchedulerPacket Generator
Packet Generator
Packet Generator
Packet Generator Vert Cat
[Fifo_Select]
[Start_Popping_1]
[Fifo_Out_2]
[Fifo_Out_1]
[Input_3]
[Dest1]
[Start_Popping_4]
[Input_2]
[Dest4]
[Tick_Out4]
[Tick_In4]
[Input_4] [Status_4]
[Fifo_Out_4]
[Tick_Out3][Tick_Out2][Tick_Out1]
[Tick][Tick_In1]
[Start_Popping_3]
[Dest3]
[Dest2]
[Tick_In3][Tick_In2]
[Status_3]
[Status_2]
[Start_Popping_2]
[Status_1]
[Fifo_Out_3]
[Input_1]
GlobalCloc
Fifo_Select]
rt_Popping_
[Tick_In2]
Fifo_Select]
[Input_2]
[Dest1]
[Dest4]
[Input_4]
[Tick_Out4][Dest4]
Fifo_Out_4]
Fifo_Select]
[Tick_In1]
[Tick_In4]
rt_Popping_
[Tick_Out3[Dest3]
[Tick_Out2[Dest2]
[Tick_Out1][Dest1]
[Dest3][Dest2]
Fifo_Out_3]
Fifo_Out_2]
Fifo_Out_1]
art_Popping_2
Fifo_Select]
[Tick_In3]
[Input_3]
rt_Popping_
[Input_1]
Clock
In
Pop
Fifo
Out
Status
FIFOManager
#4
Fifo
Out
Status
FIFOManager
#3
Out
Status
FIFOManager
#2
ClockOut
Status
FIFOManager
#1
Clock
In
Pop
Fifo
Out
Status
FIFOManager
#4
Clock
In
Pop
Fifo
Out
Status
FIFOManager
#3
Clock
In
Pop
Fifo
Out
Status
FIFOManager
#2
Clock
In
Pop
Fifo
Out
Status
FIFOManager
#1
emu
emu
U
E1
U(E)
Crossbar
Packet_In
[4x1]
[5x1]
4
4
Input 1Input 2Input 3
Output 3
Output 1Output 2
[3x4]
3
3
3
4
4
3{3}
3{3}
3{3}
3 3{3}
Output 4
Input 4
1/z
0
0
0
Dummy Event
Dummy Event
Dummy Event
Dummy Event
Clock
Fifos
Selector
Fifo
Start_Popping
Scheduler
Vert Cat
[Fifo_Select[Fifo_Select]
[Start_Popping_1][Start_Popping_1]
[Dest1]
[Start_Popping_4][Start_Popping_4]
[Dest4]
[Tick_Out4[Tick_Out4]
[Tick_In4]
[Tick_Out3[Tick_Out3][Tick_Out2[Tick_Out2][Tick_Out1[Tick_Out1]
[Tick][Tick_In1]
[Start_Popping_3][Start_Popping_3]
[Dest3]
[Dest2]
[Tick_In3][Tick_In2]
[Start_Popping_2][Start_Popping_2]
GlobalCloc
[Tick_Out4[Tick_Out4]][Dest4][Dest4]
Fifo_Out_4]Fifo_Out_4]Fifo_Out_4]
[Tick_Out3[Tick_Out3[Dest3][Dest3]
[Tick_Out2[Tick_Out2[Dest2][Dest2]
[Tick_Out1[Tick_Out1][Dest1][Dest1]
Fifo_Out_3]Fifo_Out_3]Fifo_Out_3]
Fifo_Out_2]Fifo_Out_2]Fifo_Out_2]
Fifo_Out_1]Fifo_Out_1]Fifo_Out_1]
Clock
In
Pop
Fifo
Out
Status
FIFOManager
#4
Fifo
Out
Status
FIFOManager
#3
Out
Status
FIFOManager
#2
ClockOut
Status
FIFOManager
#1
emememu
emememu
U
E1
U(E)
Crossbar
[4x1]
[5x1]
4
4
[3x4] 4
4
3{3}
3{3}
3{3}
3{3}
AutoPush 1 Queue 2
PacketState 4
AutoPop 3
Idle
DeQueue function
NonEmpty
PacketInen:Queue.Push;
EnQueue function
Tempen:Queue.Push;
Full
Idle
Empty
Nees gap of 2 between packets
Empty
PacketReadyLenOut=Fifo[LenLoc];Dst=Fifo[DstLoc];
PacketNotReadyPacketInen:Queue.Pop;
[strt==End]PacketStart
Pop[!in(Queue.Empty)]{DeQueue();}
after(LenIn41,Tick)...{PacketState.PacketPushed;}
Tick{LenIn=DataIn;}
Tick{Queue.Push;} Push[!in(Queue.Full)]{EnQueue();}
[strt==End]
after(LenOut,Tick){PacketState.PacketPopped;}
StartPopping[WhichFifo==My_Number]{UsingSwitch=1;PacketState.PacketNotReady;} PacketPushed
{NumPackets++;} [NumPackets==0]
PacketPopped{NumPackets44;UsingSwitch=0;}
PacketPushed{NumPackets++;}
PacketNotReadyPacketPushed{NumPackets++;}
Tick/Queue.Pop;
Simulink model of a packet switch used in data network-ing applications. Stateflow defines the queue and control logic for the FIFO Manager.
Creating Stateflow ChartsA Stateflow chart is a graphical representation
of a finite state machine where states and tran-
sitions form the building blocks of the system.
You create a Stateflow chart by dragging states,
junctions, and functions from the toolbar into
the drawing window. You can then create
transitions and flow by connecting states and
junctions together. You can reduce a complex
chart to a set of simpler, hierarchically
organized diagrams by using subcharts.
Stateflow features several interactive tools for
creating and modifying charts. These include:
A Stateflow diagram editor that provides
shortcut menus, navigation and command
toolbars, zoom controls, and other produc-
tivity enhancers
A Symbol Autocreation Wizard that lets you
define nongraphical objects
A debugger that controls animation of your
Stateflow charts and provides extensive capa-
bilities for examining simulation results and
locating and diagnosing unexpected behavior
in your Stateflow chart
Stateflow uses the Model Explorer in
Simulink to create data, events, and targets
and to navigate, search, and configure the
attributes of all Stateflow objects.
Adding FunctionsStateflow lets you include functions within
your statechart. You can define and use
three types of functions:
Graphical functions let you graphi-
cally program an algorithm and track its
behavior during simulation.
Embedded MATLAB functions let you call
MATLAB functions for data analysis and
visualization. You can also use an embedded
subset of the MATLAB language to define
embedded, matrix-oriented algorithms.
Truth tables let you specify functions with
combinational logic in a concise, tabular
form without drawing flow diagrams.
You can control the execution of Simulink
function-call subsystems from Stateflow,
letting you call Simulink subsystems like a
function. You can also incorporate existing
hand-written C code functions into your
Stateflow chart.
A truth table is used to implement the logic to select a valid sensor reading in a fault-detection algorithm.
Stateflow chart defining the logic for a boiler temperature control system that uses temporal logic to ensure that the boiler does not overheat. An embedded MATLAB function (above) and graphical functions (right side of chart) implement utility algorithms called by the heater system (left side of chart).
b = cold() function
turn_boiler(mode) eM
flash_LED() function
Heater
On
Offentry: turn_boiler(OFF)on every(5,sec): flash_LED()
after(20,sec) after(40,sec)[cold()]
{LED=OFF}
[Heater.On.warm()]
{LED=color}
[LED==OFF]
{ b = temp<=reference}
Specifying Temporal Logic ConditionsStateflow employs an event-scheduling
mechanism that generates accurate and effi-
cient code for event-driven systems. You can
use temporal operators (“before”, “after”, “at”,
and “every”) to specify scheduling logic for
transition and state activities. These tempo-
ral logic conditions operate on occurrence
counts of Stateflow events, freeing you from
the error-prone event-scheduling tasks of
handling timers and counters.
Creating Chart LibrariesBy creating a chart library in Stateflow, you
facilitate chart reuse. For example, when you
update a chart in a library, all instances of the
chart are automatically updated.
Interfacing with Simulink You can exchange events and data with
Simulink by simply:
• Adding and defining input and output
events and data for your Stateflow chart in
the Model Explorer
• Connecting the Stateflow input and output
ports to Simulink blocks
• Defining simulation parameters in Simulink
You can also define an interface between a
Stateflow block and code sources external to
the Stateflow block and Simulink model.
The Model Explorer lets you navigate, create, configure, and search all data, events, and attributes of your Stateflow chart, as well as create an interface with Simulink.
The logic for a fault-tolerant fuel system, defined using several key Stateflow elements.
[in(O2_normal)]
enter(MultiFail)
[in(FL1)]
[in(FL1)]
[in(FL0)]
DEC
[ speed > max_speed ]
INCINC INCINC
DECDECDEC
[!in(MultiFail)]
exit(MultiFail)
Warmup
Rich_Mixtureentry: fuel_mode = RICH
Single_Failure
Low_Emissionsentry: fuel_mode = LOW
Normal
Running
FL0
Fuel_Disabled
Fueling_Mode 2
FL1 FL2
Sens_Failure_Counter 1MultiFail
FL3 FL4
H
H
ParallelismState
Transition
History
Action
Junction
Hierarchy
Condition
Subchart
Supertransition
Using the floating scope from Simulink, you can view state transitions as the simulation runs.
Executing Stateflow ChartsAfter modeling your logic in Stateflow, you
can simulate and view its behavior live to test
and validate your design. A graphical debug-
ger helps you resolve simulation run-time
errors and diagnose unexpected behavior in
your design.
Visualizing ResultsYou can visualize the Stateflow chart during
simulation by:
• Highlighting the active states and the
transition paths taken
• Viewing states and data with the displays
and scopes provided in Simulink
• Building your own custom displays using
MATLAB visualization and graphical user
interface development tools
You can also log states and data for post-
processing.
Debugging a SimulationThe Stateflow debugger lets you set break-
points, control the simulation execution,
and display model information. It can be
run from a graphical user interface (GUI) or
from the MATLAB command line. The GUI
provides clear information about the chart’s
execution status. You can display information
on breakpoints, current data values, active
states, and the call stack.
Run-time errors can also be detected with the
debugger. These include state inconsistencies,
transition conflicts, data range, potential infi-
nite loops, and ill-specified truth tables.
For demos, application examples, tutorials, user stories, and pricing:
• Visit www.mathworks.com
• Contact The MathWorks directly
US & Canada 508-647-7000
Benelux +31 (0)182 53 76 44France +33 (0)1 41 14 67 14Germany +49 (0)241 470 750Italy +39 (011) 2274 700Korea +82 (0)2 6006 5114Spain +34 93 362 13 00Sweden +46 (8)505 317 00Switzerland +41 (0)31 950 60 20UK +44 (0)1223 423 200
Visit www.mathworks.com to obtain contact information for authorized MathWorks representatives in countries throughout Asia Pacific, Latin America, the Middle East, Africa, and the rest of Europe.
Validating your DesignYou can map requirements directly to
Stateflow objects and collect model cover-
age metrics using Simulink Verification and
Validation (available separately).
Generating CodeStateflow charts can be deployed to many
applications using Stateflow Coder (available
separately).
Required ProductsMATLABSimulink
Related ProductsReal-Time Workshop. Generate optimized,
portable, and customizable C code from
Simulink models
Simulink® Verification and Validation. Develop designs and test cases mapped to
requirements and measure test coverage
Stateflow Coder. Generate C code for
Stateflow charts
For more information on related products,
visit www.mathworks.com/products/stateflow
Platform and System RequirementsFor platform and system requirements, visit
www.mathworks.com/products/stateflow ■
Tel: 508.647.7000 [email protected] www.mathworks.com 9397v05 05/04
© 2004 by The MathWorks, Inc. MATLAB, Simulink, Stateflow, Handle Graphics, and Real-Time Workshop are registered trademarks, and TargetBox is a trademark of The MathWorks, Inc. Other product or brand names are trademarks or registered trademarks of their respective holders.
The Stateflow debugger can set breakpoints, start and stop simulations, evaluate run-time errors, and perform dynamic error checking.
Top Related