Palestine Polytechnic University
Workshop
Matlab/Simulink in Drives
and Power electronics
Lecture 5 : Introduction to Stateflow/Statechart
Ghislain REMY Jean DEPREZ
18-21 May 2009 Workshop Matlab/Simulink in Drives and Power electronics – Lecture 5 1 / 24
Palestine Polytechnic University
Workshop Programe
• 8 lectures will be presented based on Matlab/Simulink :
– 1 Introduction to Matlab
– 2 Introduction to Simulink
– 3 DC-Motor Control design
– 4 DC-Motor Chopper design SimPowerSystems
– 5 Introduction to Stateflow/Statechart
– 6 Induction Motor Inverter Control
– 7 Synchronous Motor Modeling
– 8 Synchronous Motor Control
• Two system applications (four quadrants electric drives of
mechanical systems) will be used as “guidelines" during the
workshop.
18-21 May 2009 Workshop Matlab/Simulink in Drives and Power electronics – Lecture 5 2 / 24
Palestine Polytechnic University
Stateflow/Statechart
• States can be added using blocks
• To copy a State:
– Hold the Right Click
and move to Copy
• To create Transitions:
– Close to a state frame,
– Left Click and Hold
– Move over an other state
– Release the Left click
18-21 May 2009 Workshop Matlab/Simulink in Drives and Power electronics – Lecture 5 3 / 24
Palestine Polytechnic University
Stateflow/Statechart Interface
In the Stateflow Editor, you can: draw, zoom, modify, print, and
save a chart shown in the window.
• Title bar: The full chart name appears here in model name/chart name* format.
The * appears for an unsaved chart only
• Menu bar: to access to editor commands
• Toolbar: Buttons of the editor commands as:
cut, copy, paste… by placing your pointer
over it, an identifying tool tip appears.
• Object palette: Displays tools for drawing
states, transitions, and state chart objects.
• Drawing area: Zone to edit the chart.
• Zoom control
• Shortcut menus: Pop up display commands
when you right-click an object.
• Status bar: Displays tool tips
and status information
18-21 May 2009 Workshop Matlab/Simulink in Drives and Power electronics – Lecture 5 4 / 24
Palestine Polytechnic University
Differentiating Syntax Elements in the Stateflow
You can the option of using color highlighting to differentiate the
syntax elements in the Stateflow action language:
Keyword, Comment, Event, Graphical function, String, Number
18-21 May 2009 Workshop Matlab/Simulink in Drives and Power electronics – Lecture 5 5 / 24
Palestine Polytechnic University
Selecting and Deselecting Graphical Objects
• Select objects in the Stateflow Editor as follows:
– To select an object, click anywhere inside of the object.
– To select multiple adjacent objects, click and drag a selection box so
that the box encompasses or touches the objects you want to select,
and then release the mouse button.
– To select multiple separate objects, simultaneously press the Shift key
and click an object or box a group of objects.
– To select all objects in the Stateflow chart, from the Edit menu select
Select All.
– To deselect all selected objects, press the Esc key. Pressing the Esc key
again displays the parent of the current chart.
18-21 May 2009 Workshop Matlab/Simulink in Drives and Power electronics – Lecture 5 6 / 24
Palestine Polytechnic University
Zooming with Shortcut Keys
• This table is a summary of the shortcut keys you can use to
perform some of the zooming operations described above:
• Moving in Zoomed Charts with Shortcut Keys: You can also
use number keys to move in zoomed charts according to their
layout in the number keypad:
18-21 May 2009 Workshop Matlab/Simulink in Drives and Power electronics – Lecture 5 7 / 24
Palestine Polytechnic University
Keyboard Shortcuts for Stateflow Charts
18-21 May 2009 Workshop Matlab/Simulink in Drives and Power electronics – Lecture 5 8 / 24
Palestine Polytechnic University
Model Browser
18-21 May 2009 Workshop Matlab/Simulink in Drives and Power electronics – Lecture 5 9 / 24
Palestine Polytechnic University
Data Properties
18-21 May 2009 Workshop Matlab/Simulink in Drives and Power electronics – Lecture 5 10 / 24
Palestine Polytechnic University
Creating an Event input
• Goal: Define a Transition based on an Event Input
– Click Add menu,
– Select Event, then
Input from Simulink
– Change name to SWITCH
– Select Either as Trigger
– So SWITCH change from
blue to yellow Color
– An Event Port is now added
to the Chart (a vector Port)
18-21 May 2009 Workshop Matlab/Simulink in Drives and Power electronics – Lecture 5 11 / 24
Palestine Polytechnic University
Types of Events
• An explicit event is an event defined as:
• Use events when you want to do:
– Activate a Simulink triggered subsystem
– Activate a Simulink function-call subsystem
– Trigger actions in parallel states of a Stateflow chart
18-21 May 2009 Workshop Matlab/Simulink in Drives and Power electronics – Lecture 5 12 / 24
Palestine Polytechnic University
Defining Simultaneous Action
• Goal: Define two Simultaneous Sub-States
– Select the PowerOn Action
– Right Click on a the frame
– Select Decomposition
– And finally Parallel (AND)
– Both Fan1 and Fan2 frames
change from continuous
to dotted line style
– Now Both States can be
Turn On or Off
at the same time
18-21 May 2009 Workshop Matlab/Simulink in Drives and Power electronics – Lecture 5 13 / 24
Palestine Polytechnic University
Creating a Subchart
• You create a subchart by converting an existing state, box, or
graphical function into the subchart. To convert a new or
existing state, box, or graphical function to a subchart:
– Select the object and right-click a state to display the menu
– Click Make Contents, and then Subcharted
18-21 May 2009 Workshop Matlab/Simulink in Drives and Power electronics – Lecture 5 14 / 24
Palestine Polytechnic University
Use Implicit Events
• Implicit events are built-in events that occur when a chart
executes:
– Chart waking up Examples:
– Entry into a state enter(switch_on)
– Exit from a state en(switch_on)
change(engine.rpm)
– Value assigned to an internal data object
18-21 May 2009 Workshop Matlab/Simulink in Drives and Power electronics – Lecture 5 15 / 24
Palestine Polytechnic University
Examples of Implicit Events
• Fan and Heater are parallel (AND) superstates.
– The first time that an event awakens the Stateflow chart, the states
Fan.Off and Heater.Off become active.
• Assuming that it is a discrete-time simulation,
– Each time that the chart awakens, a tick event broadcast occurs.
– After four broadcasts, the transition from Fan.Off to Fan.On occurs.
– After three broadcasts,
the transition from
Heater.Off to
Heater.On occurs.
18-21 May 2009 Workshop Matlab/Simulink in Drives and Power electronics – Lecture 5 16 / 24
Palestine Polytechnic University
Defining Action Types
• Different action types are possible:
– entry, during, exit, bind, and on event_name actions.
• The actions for states are assigned using label notation:
– name/
– entry:entry actions
– during:during actions
– exit:exit actions
bind:data_name, event_name
– on event_name:on event_name actions
18-21 May 2009 Workshop Matlab/Simulink in Drives and Power electronics – Lecture 5 17 / 24
Palestine Polytechnic University
Examples of Action Types
18-21 May 2009 Workshop Matlab/Simulink in Drives and Power electronics – Lecture 5 18 / 24
Palestine Polytechnic University
Transition Action Types
• Transitions have different action types, which include
– event triggers, conditions, condition actions, and transition actions.
• The actions for transitions are assigned using label notation :
event_trigger[condition]{condition_action}/transition_action
If the Stateflow chart wakes up:
1. The default transition to state A occurs.
2. The entry action id = x+y executes.
3. The event time_out binds to state A.
4. State A is active.
If state A is active and the Stateflow chart
receives the event event2:
1. The exit action broadcast of the event
time_out executes.
2. State A becomes inactive.
3. The transition action data1 = 5 executes.
4. State B becomes active.
18-21 May 2009 Workshop Matlab/Simulink in Drives and Power electronics – Lecture 5 19 / 24
Palestine Polytechnic University
Modeling Logic Patterns and Iterative Loops
Using Flow Graphs
• What Is a Flow Graph?
– A flow graph is a graphical construct that models logic patterns by
using connective junctions and transitions. The junctions provide
decision branches between alternate transition paths. You can use
flow graphs to represent decision and iterative loop logic.
– Examples of a if-else logic:
18-21 May 2009 Workshop Matlab/Simulink in Drives and Power electronics – Lecture 5 20 / 24
Palestine Polytechnic University
Best Practices for Creating Flow Graphs
• Follow these best practices to create efficient, accurate flow
graphs:
– Only one default transition: Flows graphs
should have a single entry point.
– Provide an unconditional transition from
every junction except the terminating junction:
• This guideline ensures that unintended backtracking
behavior does not occur in a flow graph.
Otherwise, a warning message appears.
– Only one terminating junction for readability
– Converge all transition paths to the terminating junction
– Use condition actions to process updates, not transition actions
• Flow graphs test transitions, but do not execute them
(and, therefore, never execute transition actions).
18-21 May 2009 Workshop Matlab/Simulink in Drives and Power electronics – Lecture 5 21 / 24
Palestine Polytechnic University
About Flow graphs
• A flow graph cannot maintain its active state between
updates. As a result, a flow graph always begins executing
from a default transition and ends at a terminating junction
(a junction that has no valid outgoing transitions).
• Use flow graphs to represent flow logic in graphical functions
or between states in a chart. A best practice is to encapsulate
flow graphs in graphical functions to create modular, reusable
decision and loop logic that you can call anywhere in a
Stateflow chart
18-21 May 2009 Workshop Matlab/Simulink in Drives and Power electronics – Lecture 5 22 / 24
Palestine Polytechnic University
Decision Logic Patterns in Flow Graphs
if
if-elseif if-elseif-else
if-else
18-21 May 2009 Workshop Matlab/Simulink in Drives and Power electronics – Lecture 5 23 / 24
Palestine Polytechnic University
Iterative Loop Patterns in Flow Graphs
for while do-while
18-21 May 2009 Workshop Matlab/Simulink in Drives and Power electronics – Lecture 5 24 / 24