Main Content

Create Nonvirtual Buses

A nonvirtual bus is analogous to a structure 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 Bus Objects.

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, the examples are simple. However, buses are most useful when you have many signals to combine.

Create Nonvirtual Buses Within 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 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 this command.

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 Simulink® Toolstrip, on the Modeling tab, in the Design gallery, click Type Editor.

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 Bus Objects.

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.

To create this model, see Connect Multiple Output Signals to a Port.

Nonvirtual buses require a Simulink.Bus object data type. Create the bus object that corresponds to the output bus.

  1. In the Simulink Toolstrip, on the Modeling tab, in the Design gallery, select Type Editor.

  2. In the Type Editor, create bus objects named Interface and Sinusoidal.

  3. For each bus object, create the Simulink.BusElement objects that correspond to the elements of the buses. In Sinusoidal, create elements named Chirp and Sine. In Interface, create elements named NestedBus and Step.

  4. Specify a bus object data type for any nested buses. For NestedBus, set Data type to Bus: Sinusoidal.

To output a nonvirtual bus, specify the bus object data type and bus virtuality.

  1. Open the dialog box for the port by double-clicking one of the Out Bus Element blocks.

  2. In the tree hierarchy of the dialog box, pause on OutBus. Then, click the pencil icon that appears next to OutBus.

  3. Specify the bus object data type. Set Data type to Bus: Interface.

  4. In the tree hierarchy of the dialog box, click the signal attributes that now appear next to OutBus.

  5. Specify the bus virtuality. Set Bus virtuality to nonvirtual.

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, 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 Bus Objects.

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.

Root 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.

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 view the bus objects, open the Type Editor.

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:

  1. Add a To Nonvirtual Bus block to the model.

  2. Drag the To Nonvirtual Bus block onto the line between the Bus Creator block named Bus Creator1 and the Scope block. 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:

  1. Create the corresponding bus objects.

  2. Specify the bus object data type for the bus. For example, set the Data type parameter of the To Nonvirtual Bus block to the bus 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.

See Also

Blocks

Objects

Related Topics