Stateflow

Designing Logic

Stateflow provides graphical and tabular interfaces for modeling system logic using state machines. In a state machine, you model the system's modes of operation as states and represent the logic for switching between modes using transitions and junctions. You can model the different components in your system as states that execute exclusively or in parallel. Stateflow lets you manage the complexity of your design by organizing state diagram objects, functions, and components hierarchically.

In Stateflow you can represent combinatorial logic graphically with flow charts and in tabular format with truth tables.

Designing logic involves defining conditions to be checked and subsequent actions to be performed. Stateflow enables you to define conditions and actions in C or in MATLAB®. You can manage the data used in conditions and actions from the Simulink® Model Explorer. Before executing your design, Stateflow notifies you of possible state inconsistencies, unused data and events, and invalid transitions.

Stateflow diagram defining the logic for a boiler temperature control system.
Stateflow diagram defining the logic for a boiler temperature control system. The diagram uses graphical functions (right side) to implement utility algorithms called by the heater system (left side).

Designing Logic Graphically

Stateflow provides an editor and graphical objects for drawing state machines and flow charts. You build a state machine by selecting states, transitions, and junctions from a graphical palette and dragging them into the Stateflow Editor. You can also create functions using flow chart notation, Simulink subsystems, MATLAB, and truth tables. Your Stateflow diagram can be specified as a hybrid of Mealy and Moore machines with extended functionality, a Mealy machine, or a Moore machine.

You can create flow charts by drawing transitions that are connected at junctions and execute based on conditional logic. The Pattern Wizard lets you create commonly used logic flow patterns. Flow charts can be placed in graphical functions for use in many locations in the state diagram and in other state diagrams in the same model. You can use flow charts to design the logic for transitioning between states.

Stateflow diagram that uses a graphical function generated by the pattern wizard.
Stateflow diagram (left) that uses a graphical function (right) generated by the Pattern Wizard (center).

The Stateflow Editor provides edit-time checks to identify illegal object placement and invalid transitions.

Stateflow Editor with a model of a cooling system.
Stateflow Editor with a model of a cooling system. States with overlapping boundaries are highlighted in red.

Designing Logic with Tables

State transition tables in Stateflow provide a structured environment for modeling state machines. You build a state machine by adding rows for states, and columns for the transitions between states. State transition tables help you create state machines by providing dropdown menus for state names, automatic completion of finite state machine syntax, and other edit-time checks. Before executing your model, you can run static diagnostic tests to detect syntax errors, incomplete transitions, and unreachable states.

The state transition matrix view generated from state transition tables lets you quickly identify conditions and possible destination states from a particular state.

State Transition Tables 4:53
Model state machines using the tabular interface in Stateflow®.

 

Truth tables in Stateflow let you model logic that does not require the maintenance of a state over successive runs. You build a truth table by entering conditions to be checked followed by combinations of outcomes for those conditions. Actions can then be entered for different outcome combinations. After building the truth table, you can run static diagnostic checks to identify over-specified and under-specified conditions.

Truth table implementing the logic for selecting a valid sensor reading in a fault-detection algorithm, and an automatically generated graphical representation of the truth table.
Left: Truth table implementing the logic for selecting a valid sensor reading in a fault-detection algorithm. Right: Automatically generated graphical representation of the truth table.
Next: Integrating Components and Scheduling Algorithms

Try Stateflow

Get trial software

Mobile Robot Simulation for Collision Avoidance with Simulink

View webinar

Learn to Create and Debug State Logic for Simulink Models

Get more info