You are on page 1of 25

Stateflow

Eric Hamke
March 28, 2015
Getting Ready

– This course is a condensed version of


Matlab Stateflow Basics with Stateflow
as taught by the Mathworks over a
period of 1 day.

– Please ask questions during the class!

– Please contact me with any questions


after the class.
2
Some Preliminaries
(Continous System Example)
• What is a Continuous System?
The physics of a bouncing ball

Equations describing the velocity of


the ball in the x-and y-directions

Equations describing the ball’s


position in the x-and y-directions

Inelastic collisions cause direction of ball to change direction with a slight


loss in velocity
3
Some Preliminaries
(Discrete Time System Example)
• What is a Discrete Time System?
The physics of a bouncing ball

Equations describing the velocity of


the ball in the x-and y-directions

Equations describing the ball’s


position in the x-and y-directions

Inelastic collisions cause direction of ball to change direction with a slight


loss in velocity
4
The Bouncing Ball System

• Can continuous system be a Hybrid


system?

Falling
during:
[ p <= 0 && v < 0 ]
% derivatives
{
p_dot = v;
p = 0;
v_dot = -9.81;
v = -0.8.*v;
% outputs
}
p_out = p_dot*delta_t;
v_out = v_dot*delta_t;;

Inelastic collisions cause direction of ball to change direction with a slight


loss in velocity 5
Creating The Model

2. Select Simulation and then


Model Configuration Parameters

3. Set the simulation to fixed step

4. Set time step to 0.01


1. Select New
and then
Simulink Model
6
Some Preliminaries
(Discrete System States)
• What is a Discrete System?
Suppose a robotic assembly station assembles a product
called a widget. A discrete system has states such as when
it is Idle, Working, and Down. These are its states.

Idle – The robot waits for a part to


Idle arrive from the previous robot assembly
part repair step. (Default initial state)
arrives machine
(p) (r) Working – The robot performs its
completes assembly task.
assembly
(c) Down – every now and then the Robot
fails and cannot assemble parts.
Working Down
fails (f)
7
Some Preliminaries
(Discrete System Transitions)
• What are Guard conditions?
A discrete system moves between states when the rules for
a transition are met Part arrives (p) – When a part arrives at
the robot’s station the robot begins working
on the assembly.
Idle
Completes assembly (c) – When the robot
part repair finishes it assembly task, the robot returns
arrives machine to the Idle state.
(p) (r)
completes
assembly Fails (f) – Every now and then robot breaks
(c) down, a part jams in the fixture, or the robot
needs a replaced manipulator.
Working Down
fails (f) Repair machine (r) – The robots attendant
clears jam or replaces the worn out
manipulator 8
Some Preliminaries
(Transition Conditions)
• How are Guard conditions Described?
Part arrives (p) – Robot checks for a part in
the waiting area (Queue Length > 0)

Idle Completes assembly (c) – The robot takes 30


part repair seconds to complete its assembly task. (Time
arrives machine at the station >= 30 seconds)
(p) (r)
completes Fails (f) – The robot break downs can b e
assembly modeled with a random process with a
(c) distribution.

Working Down Repair machine (r) – The robot’s attendant


fails (f)
has repair times that depend on the type of
repair (120 minutes) to change a manipulator
or a random process to clear the part from the
robots fixture.
9
Random Processes

• Random process has 2 properties


– Is an indexed set of observations. Usually the
observations are ordered by time or the sequence the
values are observed.
– The values of the observations can be described as
being randomly distributed
• Random numbers in MATLAB

y
1

So the number needs to be converted to


the range of values you need.

x
10
a b
Exponentially Distributed Number
• Similar formula exists for converting these numbers into
numbers that are exponentially distributed set

y = rand(100000,1);
mu = 0.6;
[n c] = hist(y,100);
x = -mu*log(1-y);
bar(c, n/sum(n));
xlabel('y')
[n c] = hist(x,100);
ylabel('Relative Frequency')
bar(c, n/sum(n));
title('rand(100000,1) Output')
xlabel(‘\mu*x')
ylabel('Relative Frequency')
print -f1 -dpng
title('Transformed Output, using \mu = 0.6')
11
Create Exponential Values Generator

12
Encapsulate Repair Time System

1. Select the blocks that are in


the subsystem

2. Right click on the grouping


and create a subsystem or
use <ctrl> G

13
Inserting Stateflow Block

Drag and Drop a


Stateflow block from
the Simulink Library
Browser.

Note: The block has


no in or out ports.

14
Creating the Stateflow Model

Double click on the Stateflow block to open the Stateflow


editor screen

15
Creating In/Out Ports in Stateflow

There are 4 scopes of data in a


Stateflow diagram.

- Local scope variables are localized


to the state machine being defined in
state flow.

- Input from Simulink are variables


whose values come from the
Simulink diagram only.

- Output to Simulink are variables


that the Stateflow diagram will make
visible to Simulink.

- Constant is a value that is local to


the diagram but could be initialized
from the workspace.

16
Creating Ports in Stateflow

port label/variable name

Port number allows


you order the ports to
help simplify drawings
with large numbers of
signals

Size and type will be


inherited from the
incoming signal
definition.
17
Editing Ports And Varaibles Definitions
Selecting the View\Explore will
bring up the Model Explorer
window.

This window allows you to edit the


Stateflow variables

18
Creating Out Ports in Stateflow

port label/variable name

Port number allows


you order the ports to
help simplify drawings
with large numbers of
signals
Size needs to be
entered so Stateflow
can create a variable, Sets index base value
vector or matrix as used when input is a
needed. vector or matrix.

Type was changed


from default double to
19
unit8.
Creating Ports in Stateflow
(Concluded)

20
Implementing State Machine

Click and drag state


into diagram

Enter State Name on


first line

Enter any actions


taken while in the
state.

The actions are in the


form of assignments
& function calls.

21
Implementing State Machine
(Continued)

Transitions between states are


created by
1) clicking an edge of a state
and holding.
Note: Cursor becomes a
hollow circle.
2) Drag mouse to receiving
state edge and release button.

Transitions criteria are entered by selecting the


transition and the question mark or the existing
condition.

Conditions are logical expressions that when 22


evaluated TRUE allow the transition to occur.
Implementing State Machine
(Continued)

A default transition on
chart entry is required.
This identifies the initial
state of the state
machine.

23
Running the Model

Click on the run button.

The display shows


• The state machine puts out a pulse
for each part arriving in the robots
queue

24
One Robot Model

Name of variable in the workspace

The data is output to the workspace


is a vector
25

You might also like