Using Simulink Previous page   Next Page

Comparing Stateflow and Control Flow Statements

Stateflow already possesses the logical capabilities of the Simulink control flow statements. It can call Function-Call subsystems (see Function-Call Subsystems) on condition or iteratively. However, since Stateflow provides a great deal more in logical sophistication, if your requirements are simpler, you might find the capabilities of the Simulink control flow blocks sufficient for your needs. In addition, the control flow statements offer a few advantages, which are listed in the following topics.

Sample Times

The Function-Call subsystems that Stateflow can call are triggered subsystems. Triggered subsystems inherit their sample times from the calling block. However, the Action subsystems used in if-else and switch control flow statements and the While and For subsystems that make up while and for control flow statements are enabled subsystems. Enabled subsystems can have their own sample times independent of the calling block. This also allows you to use more categories of blocks in your iterated subsystem than in a Function-Call subsystem.

Resetting of States When Reenabled

Simulink control flow statement blocks allow you to retain or reset (to their initial values) the values of states for Action, For, and While subsystems when they are reenabled. For detailed information, see the references for the While Iterator and For Iterator blocks regarding the parameter States when starting and the reference for the Action Port block regarding the parameter States when execution is resumed.

Using Stateflow with the Control Flow Blocks

You might want to consider the possibility of using Stateflow and the Simulink control flow blocks together. The following sections contain some examples that give you a few suggestions on how to combine the two.

Using Stateflow with If-Else or Switch Subsystems.   In the following model, Stateflow places one of a variety of values in a Stateflow data object. Upon chart termination, a Simulink if control flow statement uses that data to make a conditional decision.

.

In this case, control is given to a Switch Case block, which uses the value to choose one of several case subsystems to execute.

Using Stateflow with While Subsystems.   In the following diagram, Stateflow computes the value of a data object that is available to a condition input of a While Iterator block in do-while mode.

The While Iterator block has iterative control over its host subsystem, which includes the Stateflow Chart block. In do-while mode, the While block is guaranteed to operate for its first iteration value ( = 1 ). During that time, the Stateflow chart is awakened and sets a data value used by the While Iterator block, which is evaluated as a condition for the next while iteration.

In the following diagram, the While block is now set in while mode. In this mode, the While Iterator block must have input to its initial condition port in order to execute its first iteration value. This value must come from outside the While subsystem.

If the initial condition is true, the While Iterator block wakes up the Stateflow chart and executes it to termination. During that time the Stateflow chart sets data, which the While Iterator condition port uses as a condition for the next iteration.


Previous page  Modeling with Control Flow Blocks Using Callback Functions Next page

© 1994-2005 The MathWorks, Inc.