Page 879 - Mechatronics with Experiments
P. 879
®
®
MATLAB , SIMULINK , STATEFLOW, AND AUTO-CODE GENERATION 865
®
Step 6: Simulate. A SF chart is normally simulated as part of the Simulink model.
However it can also be simulated with its own simplified input and output for debugging
purposes.
Stateflow > Simulation > Configuration Parameters
Step 7: Debug. Graphical animation tools can be enabled to display the active or
inactive states as debugging tools. Breakpoints can be set at various points in the SF, such
as at the wake-up of the chart, the entry of a state, event occurrences. To set breakpoints,
enable animation, and set the speed of animation, select
Stateflow Editor > Tools > Debug
This brings up a new window with options to set breakpoints, enable/disable animation
and the speed of the animation. Most common breakpoints are set at the “Chart Entry”
and “State Entry” by checking those option boxes. Then the program execution can be
controlled with “Start,” “Stop” “Step” “Break” buttons.
A.3.1 Accessing Data and Functions from
a Stateflow Chart
®
®
MATLAB , Simulink , and Stateflow work together to model and simulate controlled
dynamic systems. Therefore, these three components must be able to access data in each
®
others’ workspace in order to apply logic to it. MATLAB maintains all its data variables in
®
its “Workspace.” Data sharing between Simulink and Stateflow is generally accomplished
by the input and the output ports in the block diagram. This is the recommended method
®
®
of exchanging data between MATLAB , Simulink , and Stateflow. It shows clearly in
®
graphical form what data is passed between Simulink and Stateflow. It is that graphical
®
display of information and logic that makes Simulink and Stateflow attractive program-
®
ming tools. Below are also other methods of accessing data in MATLAB workspace from
a Stateflow chart.
Accessing MATLAB ® Data and Built-in Functions from the SF Chart
One way to access MATLAB ® data and functions from Stateflow is to use the “ml”
namespace operator and “ml” function. Using the “ml” namespace operator we can access
®
data in the MATLAB workspace. The “ml” acts like an object name, followed by a dot
®
operator and variable name in the MATLAB workspace. For instance, let us assume there
is a variable
x
®
in the MATLAB workspace and we want to assign it to variable in Stateflow,
a
a = ml.x ; % Stateflow variable ’a’ is assigned
% to the value of MATLAB® workspace variable ’x’.
®
Likewise, we can assign a Stateflow variable to a MATLAB workspace variable. If
®
the MATLAB workspace variable was not created before, it will be automatically created.
The following accomplishes that,
ml.x = a ; % MATLAB® workspace variable ’x’
% is assigned to the value of Stateflow variable ’a’.