You are on page 1of 8

Stateflow

Design and simulate state machines and control logic


Introduction Stateflow extends Simulink with a design environment for developing state machines and flow charts. Stateflow provides the language elements required to describe complex logic in a natural, readable, and understandable form. It is tightly integrated with MATLAB and Simulink, providing an efficient environment for designing embedded systems that contain control, supervisory, and mode logic.

Introduction to Stateflow for Controls Applications

22:01

Stateflow charts enable the graphical representation of hierarchical and parallel states and the transitions between them. Stateflow augments traditional state charts with flow charts, MATLAB Coder functions, graphical functions, truth tables, temporal operators, 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).

Introduction to Stateflow for Communications Applications (Part 1)

8:20

Introduction to Stateflow for Communications Applications (Part 2)

9:13

Key Features Provides language elements, hierarchy, parallelism, and deterministic execution semantics for describing complex logic in a natural and understandable form Defines functions graphically, using flow diagrams; procedurally, using Embedded MATLAB functions; and in tabular form, with truth tables Schedules transitions and events using temporal logic Supports Mealy and Moore finite-state machines Incorporates custom and legacy C code with input and output arguments Supports bus signals and vector, matrix, and fixed-point data 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

Animates Stateflow charts and logs data during simulation to enhance understanding of the system and facilitate debugging Includes an integrated debugger for setting graphical breakpoints, stepping through charts, and browsing data

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.

Working with Stateflow Stateflow is part of an integrated simulation environment in which you can model, simulate, and analyze complex dynamic systems. By using Stateflow with Simulink, you can create models of your embedded software that combine the logical behavior of Stateflow (such as fault management and mode switching) with the algorithmic behavior of Simulink (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. Stateflow uses Model Explorer in Simulink to create data, events, and targets and to navigate, search, and configure the attributes of all Stateflow objects.

Simulink model (bottom left) and Stateflow chart (top right) of a radio receiver and decoder. Stateflow is used in this model to achieve symbol synchronization.

Modeling a Launch Abort System

12:43

Simulink model (bottom left) and Stateflow chart (top right) of the control logic for a power window system. The Simulink model incorporates 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 windows path.

Creating State Charts A Stateflow chart graphically represents a finite-state machine where states and transitions form the building blocks of the system. You can specify your Stateflow chart as one of three machine classes: Mealy, Moore, or a Mealy-Moore hybrid with extended functionality. You create a Stateflow chart by dragging states, junctions, and functions from a graphical palette into the drawing window. You can then create transitions and flow by connecting states and junctions together. Subcharts let you reduce a complex chart to a set of simpler, hierarchically organized diagrams. Stateflow includes the following tools for creating and modifying charts: Stateflow chart editorprovides shortcut menus, navigation and command toolbars, zoom controls, and other productivity enhancers Symbol Autocreation Wizardhelps you detect and add missing data and events to your Stateflow charts Stateflow debuggercontrols animation of your Stateflow charts, and provides extensive capabilities for examining simulation results and locating and diagnosing unexpected behavior in your Stateflow chart Adding Functions With Stateflow you can define and use three types of functions in your chart:

Graphical functionsfor graphically programming an algorithm and tracking its behavior during simulation Embedded MATLAB functions for calling MATLAB functions for data analysis and visualization, and for defining embedded, matrix-oriented algorithms Truth tablesfor specifying functions with combinational logic in a concise, tabular form without drawing flow diagrams You can call Simulink subsystems like functions from Stateflow. You can also incorporate existing hand-written C code functions into your Stateflow chart.

Truth table used to implement the logic to select a valid sensor reading in a fault-detection algorithm.

Specifying Temporal Logic Conditions Stateflow employs an event-scheduling mechanism that generates accurate and efficient 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 temporal logic conditions respond to counts of Stateflow events, freeing you from the error-prone event-scheduling tasks of handling timers and counters.

Stateflow chart (left) 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 (right) and graphical functions (right side of chart) implement utility algorithms called by the heater system (left side of chart).

Creating Chart Libraries By 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 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. 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.

Model Explorer, which lets you navigate, create, configure, and search all data, events, and attributes of your Stateflow chart, as well as create an interface with Simulink.

Logic for a fault-tolerant fuel system, defined using several key Stateflow elements.

Executing State Charts After modeling your logic in Stateflow, you can simulate and view its behavior live to test and validate your design. A graphical debugger helps you resolve simulation run-time errors and diagnose unexpected behavior in your design. Visualizing Results During simulation, you can visualize the Stateflow chart 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 After simulation, you can log states and data for postprocessing.

Floating scope (left) from Simulink for viewing state transitions as the simulation runs. Specific states can be selected using the Signal Selector (right) within Simulink.

Debugging a Simulation The Stateflow debugger lets you set breakpoints, 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 charts 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 violations, potential infinite loops, and ill-specified truth tables.

Stateflow debugger, which can set breakpoints, start and stop simulations, evaluate run-time errors, and perform dynamic error checking.

Validating Your Design You can map requirements directly to Stateflow objects and collect model coverage metrics using Simulink Verification and Validation (available separately). Generating Code Stateflow charts can be deployed to many applications using Simulink Coder (available separately).

Resources
Product Details, Demos, and System Requirements www.mathworks.com/products/stateflow Trial Software www.mathworks.com/trialrequest Sales www.mathworks.com/contactsales Technical Support www.mathworks.com/support Online User Community www.mathworks.com/matlabcentral Training Services www.mathworks.com/training Third-Party Products and Services www.mathworks.com/connections Worldwide Contacts www.mathworks.com/contact

2011 The MathWorks, Inc. MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See www.mathworks.com/trademarks for a list of additional trademarks. Other product or brand names may be trademarks or registered trademarks of their respective holders.