Stateflow Learning Part 1
Stateflow Learning Part 1
By
- A. MAHENDRAN
TABLE OF CONTENT
S. No Content
1 Introduction to Stateflow
1.1 Defining Chart Behavior by Using State and Transition Actions
1.1.1 State Action Types
1.1.2 Transition Action Types
1.2 Stateflow Working
1.2.1 Creating a Stateflow Chart
1.2.2 Constructing the Stateflow Model
1.2.3 Resolving Undefined Symbols
1.3 Simulating the stateflow Chart as Simulink block - By using constant blocks as inputs.
1 STATEFLOW
➢ Stateflow provides a graphical language that includes state transition diagrams, flow charts, state transition tables, and
truth tables. You can use Stateflow to describe how MATLAB algorithms and Simulink models react to input signals,
events, and time-based conditions.
➢ With Stateflow, you can model combinatorial and sequential decision logic that can be simulated as a block within a
Simulink model or executed as an object in MATLAB. Graphical animation enables you to analyze and debug your logic
while it is executing. Edit-time and run-time checks ensure design consistency and completeness before implementation.
Applications
Stateflow enables you to design and develop supervisory control, task scheduling, fault management / detection,
communication protocols, user interfaces and is used for Control systems logic, Finite state machines.
Simulink is used to respond to continuous changes in Stateflow is used to respond to instantaneous changes
dynamic systems in dynamic systems
1.1 Defining Chart Behavior by Using State and Transition Actions
State actions define what a Stateflow chart During action is performed, when remaining in a
does while a state is active. The most state, during actions occur on a time step when the
common types of state actions state is already active, and the chart does not
are entry, during, and exit actions: transition out of the state.
➢ Condition is a Boolean expression that determines whether the transition occurs. If you do not specify a condition, the
transition occurs one time step after the source state becomes active.
➢ ConditionAction is an instruction that executes when the condition that guards the transition is true. The condition action
takes place after the condition but before any exit or entry state actions.
1.2 Stateflow Working
1.2.1 Creating a Stateflow Chart
➢ The Stateflow Editor is a graphical environment for designing state transition diagrams, flow
charts, state transition tables, and truth tables. Before opening the Stateflow Editor, decide on the
chart execution mode that best meets your needs.
➢ To model conditional, event-based, and time-based logic for periodic or continuous-time Simulink
algorithms, use the sfnew function to create a Stateflow chart that you can simulate as a block in
a Simulink model. At the MATLAB command prompt, enter: >> sfnew motorctrl
Stateflow chart /
editor / canvas
The Stateflow chart / 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.
Step 1 - From the object palette, click the
After opening the state State icon and move the pointer to the
1.2.2 Constructing flow chart, follow these chart window. A state with its default
the stateflow steps to create and transition appears. To place the state, click
Model Simulate the Chart as a the location on the window. At the text
Simulink Block, prompt, enter the state name On and below
state action y = x.
➢ If you are building a chart in a Simulink model, in the Modeling tab, under Design
Data, select Symbols Pane.
➢ If you are building a standalone chart for execution in MATLAB, in the State Chart
tab, select Design Data > Symbols Pane.
➢ We can resolve the undefined symbols by giving the variables of inputs & outputs.
For this example, give x & t0 as input data and y as output
• After resolving, come out of the chart, add 2 constant blocks for inputs t0, x and a display block for output y to view results.
(Note: Based on the input values and transition conditions, the states will execute with respect to each step of stop
time.)
Now we’ll see different set of cases for executing the transition conditions
Case 1: We give both t0, x = 1, which showed in symbols pane. When we simulate, with the transition conditions satisfying
[x>=t0], the On state will execute for the whole stop time and highlights as shown below.
Case 2: If we give t0 as 1 and x as 0, now simulate the model in Step forward to see the states execution at every time
step. The Off state should be executed for the condition, but as we mentioned that On state as default junction, at 1st time
step the state entry will be at On state & will highlight as we seen below.
For further time steps up to the last stop time, the Off will be executed and highlighted.
Note : As we can see the output value in scope blocks, while we executing the transition conditions, parallelly we can view
this data at each time step on symbols pane at right side of window