Lab-1 - Dr. Imtiaz Hussain

advertisement
Control Systems With Embedded
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.
• Design systems containing control, supervisory, and
mode logic.
• Describe logic in a natural and understandable form
with deterministic execution semantics.
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.
Use both Simulink and Stateflow so that you
can use the right tool for the right job.
8
Key Features
•
•
•
•
•
Defines functions
–Procedurally, using Embedded MATLABTM
–Graphically, using flow diagrams
–In tabular form, with truth tables
Provides language elements, hierarchy, and
parallelism
• Animates Stateflow®charts
• Incorporates custom and legacy C code
• Performs static and run-time checks
9
Objective of the Lab
• In this lab we will cover the basic workflow for building
Stateflow charts that are used to model event−driven
systems, and how they work with Simulink blocks.
• We will begin with definitions that are essential in
understanding Stateflow and its relation to Simulink
and MATLAB.
• Later with the help of a demo model provided by The
MathWorks™, we will develop an example model with
step−by−step procedure.
10
What is a finite state machine?
• A finite state machine is a model describing the behavior of a finite
number of states, the transitions between those states, and
actions.
• A state represents an operating mode of a machine.
– For instance, a typical household portable space heater has four
states, off, low, medium, and high.
• An action describes the activity that is to be performed.
• An action can be further classified as an entry action which is
performed when entering the state, an exit action which is
performed when exiting the state, and as a transition action which
is performed during a transition.
11
What is a finite state machine?
• A representation of a reactive system that contains a
finite number of states.
• System changes states based on defined conditions.
• 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.
• Thus, a typical household heater
can be represented as shown in
Figure-1, or as a state transition
table shown as in Table-1.
Figure-1
Table-1
13
Event Driven System
• An event is an action that can trigger a variety of activities.
• For example, in a typical household space heater a switch
allows a transition to occur between medium state and high
state.
• Thus, event driven systems allow the transition from one
operating mode to another in response to events and
conditions.
• Event−driven systems can be implemented as finite−state
machines.
14
What Is Stateflow?
• Stateflow is a graphical design and development tool for
control and supervisory logic used in conjunction with
Simulink.
• Stateflow is a product that is part of Simulink. In Simulink,
Stateflow blocks are referred to as Stateflow Chart blocks.
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.
• When you first turn the state
machine on, this chart is set
to
execute
a
special
transition called a default
transition that points to the
initial state, Power_off.
• This makes the Power_off
state active. Later, when you
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.
• This makes the Off state
inactive and On state
active.
18
Stateflow Adds Flow Diagrams to the State Machine
• Stateflow also adds the
feature of flow diagrams
that
provide
decision
points in transitions.
• Stateflow implements flow
diagrams with junctions,
round objects that provide
an alternative path for
transitions.
19
Exercise
20
Stateflow Simulates its State Machine
• Once you finish a Simulink
model with Stateflow charts,
you can simulate it.
• This allows you to see an
animated Stateflow chart
while it responds to events
and changes states.
• In the animated chart, active
states are highlighted.
21
Build a Stateflow Model
• At the MATLAB prompt enter sfnew.
• Label the Stateflow block in the new untitled model by clicking
in the text area and replacing the text Untitled with the text
On_Off.
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
• Stateflow provides us the necessary graphical objects to
construct finite−state machines.
• Like Simulink, we can drag and drop objects to create
state−transition charts in which a series of transitions
directs a flow of logic from one state to another.
24
Procedure for Creating a Stateflow Chart
• To understand the basic steps for creating a Stateflow
Chart let us start with an example.
• We will follow the procedure recommended by The
MathWorks illustrated in the functional block diagram.
25
Example#1
• Let us design a Counter that counts 0-3.
Start
0
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.
– Drag chart block from stateflow blockset
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
– To define the output of the
Stateflow Chart block From
the Chart drop menu we
select Add Inputs &
Outputs>Data Output to
Simulink, and this opens
the event window.
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
asynchronous reset input.
counts
0-7
with
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
Download