Tutorial 5: Systems Modelling With Stateflow/Simulink
Tutorial 5: Systems Modelling With Stateflow/Simulink
Tutorial 5
Systems Modelling with Stateflow/Simulink
1 The Specification
The specification roughly corresponds to the behaviour of an electronically heated car
seat.
The heating of the seat is controlled by a button which has three positions; {neutral, half,
full}, see Figure 1. The button is always in the neutral position if not pushed because of
two springs. The left part of the button is marked with an H. If this part is pushed, this
means that 50% heating should be applied to the seat. The right hand part of the button is
marked with an F. If this part is pushed, the seat should be fully heated (100%).
H F
2 Introduction to Stateflow
What is a Stateflow chart? Figure 2 shows an example which models, as a finite state
machine, the logic required to shift gears in an automatic transmission system of a car.
Notice the following details in this flow chart:
• Each gear and shift position is represented by a state
• Some states are exclusive (only one can be active at a time) while others are
parallel (can be active concurrently).
• Transitions can be triggered by events and conditions.
• States can execute actions while they are active.
You will learn more about these features later in this tutorial as you build your own
Stateflow chart.
Event
State action
Condition
Transition
Parallel
(AND) state
Exclusive
(OR) state
Given that you have also decided to use Stateflow to describe a portion of the system
functionality, you also have to consider when this functionality should execute, e.g. in a
periodic fashion or triggered by some event.
Now, assuming you have an initial idea of the functions to describe using Stateflow (in
this case, the main portion of the specification and behaviour logic described in section
1), you can proceed according to the procedure described in Figure 1, including the
following steps:
1. Defining the interface to Simulink. In this step, you define how the Stateflow
chart is connected to the other parts of the Simulink model. In particular, you
define the inputs required from Simulink, and the outputs that the chart should
produce. You can then connect these inputs and outputs to the appropriate signals
in the Simulink model
2. Defining the states, types if states and their structuring. It is usually possible to
develop solutions with a different number of states. Stateflow allows you to
define hierarchical states – this is a powerful technique in which certain states are
grouped together and where events “common” to all the grouped states only have
to be defined once. In addition, states can be defined to be sequential – so called
OR states, or to run in parallel – AND states.
3. Defining state actions and variables. Actions can be defined to take place when
entering a state, when exiting a state, or while a state is active – “during” a state
(actually they can also be defined for transitions, but we will skip that part here).
Variables can be defined for states, and just as in programming, they can be
temporary or persistent.
4. Defining transitions between states. Transitions create paths for the logic flow of
a system from one state to another. When a transition is taken from state A to
state B, state A becomes inactive and state B becomes active. Transitions have
direction and are represented in Stateflow charts by lines with arrowheads. You
must add a transition for each direction of between two states. Guarding a
transition means specifying a condition, action or event that allows the transition
to be taken from one state to another.
5. Triggering a Stateflow chart. Simulink can wake up a Stateflow chart by sampling
the chart at a specified or inherited rate, by using a signal as a trigger, or by using
one Stateflow chart to drive the activity of another.
6. Simulating the chart. This is not so different from ordinary simulation in
Simulink. The main difference is that you should set up the use of the Stateflow
debugger which is a very useful tool. It is similar to an ordinary debugger in that
it allows you to set breakpoints and to step through the execution of the Stateflow
chart.
7. Debugging the chart. Here you execute the chart with the debugger.
3 Step-by-Step Tutorial
Note! If you have problems – or if you want to read more – then you have the following
help available:
• the Stateflow tool help functionality: Open a Stateflow chart and click on Help,
Stateflow Help
• the Stateflow web documentation:
http://www.mathworks.com/access/helpdesk/help/toolbox/stateflow/ug/index.html?/
Look back at the Simulink model by clicking the up-arrow in the Stateflow Editor
toolbar: You should be able to see that two inputs have been created. Connect these two
to the respective button signals.
Tip: There are several ways to add data objects to Stateflow charts. You have now used
the Stateflow Editor, which allows you to add data elements to the Stateflow chart that is
open and has focus. However, to add data objects not just to a chart, but anywhere in the
design hierarchy, you can use a tool called the Model Explorer. This tool also lets you
view and modify the data objects that you have already added to Stateflow.
You can start the Model Explorer from the Tools menu by pressing Explore.
Figure 7. Simulink model after defining the inputs and outputs to the Stateflow chart.
Next, you will add entry actions to the states that you have defined. The syntax for entry
actions is
entry:{one or more actions};
en:{one or more actions};
To write an entry action for the Idle state, follow these steps:
• Click inside the Idle state after the last letter of its name label to get a blinking
text cursor.
• Press the Enter key and type entry: HeatSignal = 0;
• Add entry actions to the other states as well.
To ensure that you are on the right track, you should now have a Stateflow chart that
looks something like Figure 8:
Figure 10. Setting the sampling rate for a periodically executed Stateflow chart.
Previously you used the condition expressions, e.g. [Hbutton]. Now you will instead use
the event inputs for the transitions. This means that it suffices to label the transitions with
the appropriate names of events – as shown in the previous figure.
o In the Help navigator, select the Contents pane and scroll down to the
Stateflow node.
o Select the Stateflow node and follow the links to documentation resources,
including real-world example models.
• Stateflow demos – Stateflow provides a collection of demonstration models,
which you can access as follows:
o In the Matlab window, select Demos from the Start menu.
o The Help browser opens, displaying information on how to get started
using demos.
o In the Help Navigator, expand the Simulink node to expose the Stateflow
one.
o Expand the Stateflow node to examine demonstration models that
illustrate a variety of applications