Lab 1 Introduction To Stateflow
Lab 1 Introduction To Stateflow
Lab 1 Introduction To Stateflow
Implementation (CSEI)
Lab-1
Introduction to Simulink & Stateflow®
Dr. Imtiaz Hussain
Assistant Professor
email: imtiaz.hussain@faculty.muet.edu.pk
URL :http://imtiazhussainkalwar.weebly.com/
1
Lecture Outline
• Introduction
• Objective
2
Introduction
• Simulink and Stateflow provide:
– A powerful environment for modeling real
processes...
– and are fully integrated with the MATLAB
environment.
– Instrument Control and Data Acquisition enable
verification and validation of models.
3
Introduction to Simulink®
• Block-diagram environment
• Model, simulate, and analyze multidomain
systems
• Accurately design, implement, and test:
– Control systems
–Signal processing systems
– Communications systems
– Other dynamic systems
4
Simulink Product Key Features
• Extensive and expandable libraries of
predefined blocks
• Hierarchical, component-based modeling
• Open Application Program Interface (API)
• Hybrid (mixed-signal), multirate and
multitasking system simulation
• Full MATLAB®integration
5
Types of Models
• Continuous-Time
• Discrete-Time / Multi-Rate Sampled-Data
• Finite State Machines / Mode Logic
• Discrete Event Systems
• Physical Domains
6
Stateflow Overview
• Extend Simulink with a design environment for
developing state machines and flow charts.
7
How does Stateflow work with Simulink?
• Simulink is used to respond to continuous changes in
dynamic systems.
• Stateflow is used to respond to instantaneous changes in
dynamic systems.
• Real-world systems have to respond to both continuous and
instantaneous changes.
• Examples:
– Automatic transmission: First, second, third, and fourth gears
– Actuator in a fault detection, isolation, and recovery system: Active,
standby, off, isolated
– Robot arm: Initialization, normal mode, or shutdown
12
What is a finite state machine?
• A finite state machine can be
represented either by a state
diagram or a state transition table.
13
Table-1
Event Driven System
• An event is an action that can trigger a variety of activities.
15
Stateflow Is a Finite State Machine
• If you double-click the Stateflow block in the preceding
Simulink model, its Stateflow diagram appears in the
Stateflow diagram editor window.
16
Stateflow Is a Finite State Machine
• The preceding example has
two states: Power_on and
Power_off.
17
Stateflow Is a Finite State Machine
• Later, when you change the
manual switch in Simulink
from Off to On, the model
sends an event (on_switch)
that makes the Off state
transition to the On state.
18
Stateflow Adds Flow Diagrams to the State Machine
19
Exercise
20
Stateflow Simulates its State Machine
• Once you finish a Simulink
model with Stateflow charts,
you can simulate it.
22
Creating States
• Double-click the Stateflow block in the Simulink model
window to invoke the Stateflow diagram editor.
23
Construction of Finite−State Machines with Stateflow
24
Procedure for Creating a Stateflow Chart
25
Example#1
• Let us design a Counter that counts 0-3.
Start
Clock Pulse
Clock Pulse
3 1
Clock Pulse
2 Clock Pulse
26
Example#1
• Step 1: We define the Interface to Simulink
– Open new model window in Simulink and drag
Pulse Generator block from sources.
27
Example#1
• Step 1: We define the
Interface to Simulink
– To define the input of the
Stateflow Chart block in we
double−click this block, and
we observe that the
Stateflow Editor window
appears.
– From the Chart drop menu
we select Add Inputs &
Outputs>Event Input from
Simulink, and this opens the
event window.
28
Example#1
• Step 1: We define the
Interface to Simulink
29
Example#1
• Step 2: We define the States for each Mode of
Operation
– From the object palette on the left side of the Stateflow Editor
window we click the State tool
State tool
30
Example#1
• Step 3: We define State Actions and Variables
– We click inside the 0 state after the last letter of its name to cause a blinking
text cursor to appear. We press the Enter key and we type entry: COUNT=0;
31
Example#1
• Step 4: We define
Transitions Between
States
– We need to add a transition
from the 0 to the 1 state.
– To do this, we move the
cursor over the top edge of 0
and we observe that the
cursor shape changes to
crosshairs.
– We hold down the left mouse
button, we drag the cursor to
the bottom edge of the 1
state, we release the mouse,
and a transition pointing from
the 0 to the 1 state is formed.
32
Example#1
• Step 4: We define
Transitions Between
States
– click the Default Transition
tool , we move the cursor
into the drawing area, and
we observe that it changes
to a diagonal arrow.
Default Transition
33
Example#1
• Step 6: Simulation of the Stateflow Chart
• When a Simulink model that contains a Stateflow Chart block is
simulated, we can animate the Stateflow Chart to highlight the
states and the transitions as they occur, and this feature
provides visual verification that our chart behaves as expected.
• Animation is enabled by default but we need to specify the
speed.
34
Example#1
35
Example#1
36
Example#2
• Design a Counter that counts 0-7 with
asynchronous reset input.
37
Example-3
• ABC Company maintains checking and savings accounts with a local
bank.
• The initial deposit in the checking account is $10,000 and the fixed
amount of $10,000 is deposited in the savings account.
• The company has also established an overdraft protection for up to
$50,000, and has made an agreement with the bank that if
payments by the bank exceed the overdraft protection, the bank
will deduct the excess amount from the savings account.
• There will be no fees imposed as long as payments do not exceed
present checking account balance and interest at 0.5% will be
earned.
• A fee of 5% will be imposed for overdrafts, and 1% fee will be
charged if it becomes necessary to draw monies from the savings
account.
38
To download this lecture visit
http://imtiazhussainkalwar.weebly.com/
END OF LAB-1
39