Access Bus Signals
A Stateflow® bus is a data type that you define from a Simulink.Bus
(Simulink) object. Using Stateflow buses, you can bundle data of different size and type to create:
Inputs and outputs that access Simulink® bus signals from Stateflow charts, Truth Table blocks, and MATLAB Function blocks.
Local data in Stateflow charts, truth tables, graphical functions, MATLAB® functions, and boxes.
Temporary data in Stateflow graphical functions, truth tables, and MATLAB functions.
For more information, see Create Simulink Bus Objects (Simulink).
Example of Stateflow Buses
In this example, a Stateflow chart receives a bus input signal from Simulink by using the input inbus
and outputs a bus signal from outbus
. The input signal comes from the Simulink Bus Creator block COUNTERBUSCreator
, which bundles signals from two other Bus Creator blocks. The output outbus
connects to a Simulink Bus Selector block. Both inbus
and outbus
derive their type from the Simulink.Bus
object COUNTERBUS
. For more information about this example, see Integrate Custom Structures in Stateflow Charts.
The elements of a Stateflow bus data type are called fields. Fields can be any combination of individual signals, muxed signals, vectors, and other buses, also called subbuses. Each field has its own data type. The data type does not have to match the type of any other field in the bus. For example, in this model, each of the buses, inbus
and outbus
, has two fields:
inputsignal
is a subbus with one field,input
.limits
is a subbus with two fields,upper_saturation_limit
andlower_saturation_limit
.
Define Stateflow Buses
To define the bus data type, create a Simulink bus object in the base workspace, as described in Create Simulink Bus Objects (Simulink).
Add a data object to the chart, as described in Add Stateflow Data.
To define temporary buses in truth tables, graphical functions, and MATLAB functions, add a data object to your function. For more information, see Add Data Through the Model Explorer.
Set the Scope property for the bus. Your choices are:
Input
Output
Local
Parameter
Data Store Memory
Temporary
(Only in charts that use C as the action language)
Set the Type property for the bus. Depending on its scope, a Stateflow bus can have one of these data types.
Type Description Inherit: Same as Simulink
This option is available for input buses only. The input bus inherits its data type from the Simulink bus signal in your model that connects to it. The Simulink bus signal must be a nonvirtual bus. For more information, see Virtual and Nonvirtual Buses.
In the base workspace, specify a
Simulink.Bus
object with the same properties as the bus signal that connects to the Stateflow input bus. These properties must match:Number, name, and type of inputs
Dimension
Sample Time
Complexity
Sampling Mode
If the input signal comes from a Bus Creator block, in the Bus Creator dialog box, specify an appropriate bus object for Output data type field. When you specify the bus object, Simulink verifies that the properties of the
Simulink.Bus
object in the base workspace match the properties of the Simulink bus signal.Bus: <object name>
In the Type field, replace
<object name>
with the name of theSimulink.Bus
object that defines the Stateflow bus.For input or output buses, you are not required to specify the bus signal in your Simulink model that connects to the Stateflow bus. If you do specify a bus signal, its properties must match the
Simulink.Bus
object that defines the Stateflow bus.<data type expression>
For buses with scopes other than
Output
, use an expression that calls thetype
operator. This operator sets the type of one bus to the type of another bus in the Stateflow chart. For example, in thesf_bus_demo
model, atype
operator expression specifies the type of the local buscounterbus_struct
in terms of the input businbus
. Both buses are defined from theSimulink.Bus
objectCOUNTERBUS
. For more information, see Derive Data Types from Other Data Objects.
Initialize Stateflow Buses
After you define a Stateflow bus as a data object, you can assign values to the data object in a state or transition. Alternatively, you can initialize the data object through the Property Inspector in one of these ways:
Directly assign an initial value using the
struct
function.Assign a variable in the MATLAB workspace as a parameter.
Directly Assign an Initial Value
In the Symbols pane, right-click the bus and select Inspect.
In the Initial value field, create a bus using the
struct
function.In the Advanced section, in the Initialize method drop-down, select
Parameter
.
Assign a Variable in the MATLAB Workspace as a Parameter
In the MATLAB Command Window, use the
Simulink.Bus.createMATLABStruct
(Simulink) function to assign a MATLAB structure to a variable.In the Symbols pane, right-click the bus and select Inspect.
In the Initial value field, enter the name of the variable.
If the variable and the bus share a name, entering an initial value is optional. The bus infers the variable name.
In the Advanced section, in the Initialize method drop-down, select
Parameter
.
Limitations
The Property Inspector does not support initializing:
Buses in models that use HDL code generation
Buses with an Initial value field set to either
0
or a structure with no fieldsBuses with a scope of
Input
,Constant
,Parameter
, orData Store Memory
Unbounded buses or buses that contain strings
Virtual and Nonvirtual Buses
Simulink models support virtual and nonvirtual buses. Nonvirtual buses read their inputs from data structures stored in contiguous memory. Virtual buses read their inputs from noncontiguous memory. For more information, see Composite Interface Guidelines (Simulink).
Stateflow charts support only nonvirtual buses. Stateflow input buses can accept virtual bus signals and convert them to nonvirtual bus
signals. Stateflow input buses cannot inherit properties from virtual bus signals. If the input
to a chart is a virtual bus, set the Type property of the input bus
through a type specification of the form Bus:
.<object
name>
Debug Buses
To debug a Stateflow bus, open the Stateflow Breakpoints and Watch window and examine the values of bus fields during simulation. To view the values of bus fields at the command line, use dot notation to index into the bus. For more information, see Inspect and Modify Data and Messages While Debugging.
See Also
Simulink.Bus
(Simulink)