Create Nonvirtual Buses
A nonvirtual bus is analogous to a struct in C code. Use nonvirtual buses to:
Construct an array of buses.
Have bus data cross MATLAB® Function block or Stateflow® chart boundaries.
Package bus data as structures in generated C code.
Interface with external code through an S-function.
Reduce the number of function arguments passed to a subcomponent in generated code.
A Simulink.Bus
object data type must define the
bus you want to make nonvirtual. A bus becomes nonvirtual when you select a block parameter
such as Output as nonvirtual bus. Making the bus nonvirtual causes
simulation and code generation to apply the structure defined by the Bus
object. When the bus is virtual, the Bus
object only validates the properties
of the bus.
To simulate a model that contains nonvirtual buses, the referenced Bus
objects must be in the base workspace or a data dictionary used by the model. For more
information, see Specify Bus Properties with Simulink.Bus Object Data Types.
Nonvirtual buses also require all elements of the bus to have the same sample time. For more information, see Modify Sample Times for Nonvirtual Buses.
The way to create nonvirtual buses differs based on the location of the bus and the block that creates the bus:
To focus on fundamental steps, these examples are simple. However, buses are most useful when you have many signals to combine.
Create Nonvirtual Buses Within a Component
You can use Bus Creator blocks to create nonvirtual buses within components.
Open and compile the example model, which uses Bus Creator blocks to create a hierarchy of virtual buses. To compile the model, on the Modeling tab of the Simulink Toolstrip, click Update Model or Run. Compiling the model updates the line styles, which you can use to visually identify buses.
To create this model, see Group Signal Lines Within a Component.
The virtual buses in this model are not defined by Simulink.Bus
objects. To change the output of the Bus Creator blocks to nonvirtual buses, you must have Bus
objects that match the bus hierarchy.
To create the Bus
objects that correspond to TopBus
and NestedBus
, use the Simulink.Bus.createObject
function. In the MATLAB Command Window, enter:
Simulink.Bus.createObject('NonvirtualBusCreationModel',... 'NonvirtualBusCreationModel/Bus Creator1');
This function creates the Bus
object that corresponds to the output bus of the specified block. If the output bus contains nested buses, the function also creates Bus
objects that correspond to the nested buses. In this example, the function creates two Bus
objects that are named after the corresponding buses, TopBus
and NestedBus
.
To view the Bus
objects, open the Type Editor. In the MATLAB Command Window, enter:
typeeditor
Now that you have Bus
objects that correspond to the nonvirtual buses you want to create, create the nonvirtual buses. In the Simulink Editor, double-click the Bus Creator block named Bus Creator1. In the Block Parameters dialog box, set Output data type to Bus: TopBus
, select the Output as nonvirtual bus check box, and click OK. TopBus
is now a nonvirtual bus, while NestedBus
remains a virtual bus. To identify the nonvirtual bus by line style, compile the model.
Double-click the block named Bus Creator
. In the Block Parameters dialog box, set Output data type to Bus: NestedBus
, select the Output as nonvirtual bus check box, and click OK. NestedBus
is now a nonvirtual bus. To update its line style, compile the model.
If you do not save the Bus
objects, then you must recreate the Bus
objects when you reopen the model. For information on how to save the Bus
objects, see Specify Bus Properties with Simulink.Bus Object Data Types.
Create Nonvirtual Buses at Interfaces
To create a nonvirtual bus at an interface, connect the bus elements to Out Bus Element blocks and specify that the output bus is nonvirtual.
Open the example model, which uses three Out Bus Element blocks to create a virtual bus at the output port, OutBus
.
To create this model, see Connect Multiple Output Signals to a Port.
Nonvirtual buses require a
object data type. Create the Simulink.Bus
Bus
object that corresponds to the output bus.
In the Simulink Toolstrip, on the Modeling tab, in the Design gallery, select Type Editor.
In the Type Editor, create two
Bus
objects. In the toolstrip, click Bus twice.Name the
Bus
objects. Select one of theBus
objects, and set Name toOutBus
. Select the otherBus
object, and set Name toNestedBus
.For each
Bus
object, create the
objects that correspond to the elements of the buses. SelectSimulink.BusElement
NestedBus
, then, in the toolstrip, click Bus Element twice. Name the elementsSine
andStep
. SelectOutBus
, then click Bus Element twice. Name the elementsChirp
andNestedBus
.Specify a
Bus
object data type for any nested buses. With theNestedBus
object selected, set Data type toBus: NestedBus
.
To output a nonvirtual bus, specify the Bus
object data type and bus virtuality.
Open the dialog box for the port by double-clicking one of the Out Bus Element blocks.
In the tree hierarchy of the dialog box, pause on
OutBus
, and click the pencil icon that appears next toOutBus
.Specify the
Bus
object data type. Set Data type toBus: OutBus
.In the tree hierarchy of the dialog box, click the signal attributes that now appear next to
OutBus
.Specify the bus virtuality. Set Bus virtuality to
nonvirtual
, then close the dialog box.
The output of the model is a nonvirtual bus. To see the nonvirtual bus, reference the model in another model or move the Out Bus Element blocks into a subsystem. The line styles update when you compile the model. To compile the model, on the Modeling tab of the Simulink Toolstrip, click Update Model or Run.
If you do not save the Bus
objects, then you must recreate the Bus
objects when you reopen the model. For information on how to save the Bus
objects, see Specify Bus Properties with Simulink.Bus Object Data Types.
Root (top-level) Inport and Outport blocks can also create nonvirtual buses at interfaces. To specify the bus virtuality:
For a root Inport block, on the Signal Attributes tab of the dialog box, select Output as nonvirtual bus.
For a root Outport block, on the Signal Attributes tab of the dialog box, select Output as nonvirtual bus in parent model.
In Bus Element blocks use the Bus virtuality parameter to determine whether they inherit or define the bus virtuality. If the block defines the bus virtuality and the virtuality of the input bus does not match, compiling the model produces an error.
Convert Virtual Bus to Nonvirtual Bus
You can convert a virtual bus to a nonvirtual bus anywhere within a model by using a Signal Conversion block.
Open and compile the example model, which contains a virtual bus hierarchy created by Bus Creator blocks. To compile the model, on the Modeling tab of the Simulink Toolstrip, click Update Model or Run. Compiling the model updates the line styles, which you can use to visually identify buses.
To define the buses and support conversion to nonvirtual buses, the Bus Creator blocks specify Simulink.Bus
object data types. To create the Bus
objects in the base workspace when the model is loaded, the model uses the PreLoadFcn
callback.
To view the callback, in the Simulink Toolstrip, on the Modeling tab, click Model Settings > Model Properties. In the Model Properties dialog box, open the Callbacks tab and select the PreLoadFcn model callback.
To convert a virtual bus to a nonvirtual bus, use a To Nonvirtual Bus block, which is a preconfigured version of the Signal Conversion block that has Output set to Nonvirtual bus
.
In the example model:
Add a To Nonvirtual Bus block to the model.
Drag the To Nonvirtual Bus block onto the line between the Bus Creator1 and Scope blocks. When you drag the block onto the line, the block connects to the line at both ends.
Suppose the data type of the bus you want to convert is not specified by a Bus
object. You would need to perform these additional actions:
Creating the corresponding
Bus
objects.Specifying the
Bus
object data type for the bus. For example, setting the Data type parameter of the To Nonvirtual Bus block to theBus
object that corresponds to the input bus.
The input to the To Nonvirtual Bus block is a virtual bus, and the output is a nonvirtual bus. To see the nonvirtual bus line style, compile the model.
Create Nonvirtual Bus from MATLAB Structure
You can use a Constant block to compactly represent a nonvirtual bus with constant-valued elements.
On the Constant block, specify:
A MATLAB structure for Constant value.
A
Simulink.Bus
object as the Output data type.
Constant blocks only support MATLAB structures when the output data type is a Bus
object.
For example, define a MATLAB structure named const_param_struct
with elements named Offset
, Gain
, and Threshold
.
const_param_struct.Offset = 197; const_param_struct.Gain = 4.32; const_param_struct.Threshold = 795.68;
Define the corresponding Bus
object data type.
Simulink.Bus.createObject(const_param_struct)
The new Bus
object uses the default name slBus1
.
Open and simulate the example model, which contains a Constant block with Constant value set to const_param_struct
and Output data type set to Bus: slBus1
.
open_system('NonvirtualBusFromStructModel') sim('NonvirtualBusFromStructModel');
The output of the Constant block is a nonvirtual bus.