Specify Bus Properties with Simulink.Bus Object Data Types
A bus can be associated with aSimulink.Bus
object, which specifies properties that Simulink®uses to validate the bus.Bus
objects are optional for virtual buses, but required for nonvirtual buses.
ABus
object specifies only the architectural properties of a bus, as distinct from the values of the signals it contains. For example, aBus
object can specify the number of elements in a bus, the order of those elements, whether and how elements are nested, and the data types of constituent signals; but not the signal values.
ABus
object is analogous to a structure definition in C: it defines the members of the bus, but does not create the bus. ABus
object is also similar to a cable connector. The connector defines all the pins and their configuration and controls what types of wires can be connected to it. Similarly, aBus
object defines the configuration and properties of the signals that the associated bus must have.
Simulink.Bus Object Workflow
UsingBus
objects in a model involves performing these tasks, in many cases iteratively.
Determine Whether to Use Simulink.Bus Objects
You must useBus
objects for these modeling configurations:
Nonvirtual buses
Stateflow®charts with bus input or output
S-function or Legacy Code Tool interface with external code
You can associate aBus
object with multiple blocks. Some blocks require that you specify aBus
object if the block has a bus input or output. For more information, seeBus-Capable Blocks.
When aBus
object governs a signal input or output for a block, the signal must be a bus that has the properties specified by the object. Any variance causes an error.
If you useBus CreatororIn Bus Elementblock parameters to specify bus properties, all blocks downstream from the bus inherit the same properties.
You can useBus Creatorblock parameters to define virtual buses and perform limited error checking. To perform thorough error checking on a bus, associate aBus
object with that bus. UsingBus
objects to check buses for errors is important when you want to create reusable and shareable model components.
To make tracing the correspondence between the model and the generated code for a bus easier, use a nonvirtual bus. The generated code for a nonvirtual bus produces a structure. Nonvirtual buses can result in multiple copies of some buses.
Determine How to Manage Simulink.Bus Objects
You can saveBus
objects to these locations:
Data dictionary
Function
MAT-file
Database or other external files
If you do not saveBus
对象,当你重新打开一个模型,使用theBus
objects, you need to recreate theBus
对象。
DifferentBus
object storage locations provide different advantages.
Location | Usage Considerations |
---|---|
Data dictionary |
Use for large model componentization. When you save to a data dictionary from the base workspace, you get all the variables used by the model, not just the Before you save to a data dictionary, readConsiderations before Migrating to Data Dictionary. |
Function |
Use for when you want to use MATLAB®for traceability and model differencing. |
MAT-file |
Use for faster |
Database or other external files |
Use for comparing bus interface information with design documents stored in an external data source. |
Create and Specify Simulink.Bus Objects
To create or editBus
objects interactively, use theBus EditororModel Explorer.Bus
objects created with these tools are initially stored in the base workspace or data dictionary. To visualize bus hierarchy and access capabilities such as import and export, use theBus Editor. When you have multiple types of objects to update, use theModel Explorer. TheModel Explorerlets you update additional types of objects, such asSimulink.Parameter
对象。
To create and editBus
objects programmatically, seeCreate Bus Objects Programmatically.Bus
objects are initially stored in either the base workspace, a data dictionary, or a function.
After you create aBus
object and specify its attributes, you can associate it with any block that needs to use the bus definition that the object provides. To associate a block with a bus, in the Block Parameters dialog box, setOutput data typeorData typetoBus:
and replacewith the
Bus
object name.
You can specify theBus
object as the data type of a block either before or after defining theBus
object. However, before you simulate the model, theBus
object and the corresponding bus must have the same number of bus elements, in the same order. Also, each element in theBus
object and in the corresponding bus must have the same data type and dimensions.
During model development, you can modify buses to matchBus
objects or modifyBus
objects to match buses.
If you do not want to change theBus
object, you can:
Create a
Bus
object that matches the changes to the bus and use the newBus
object for the blocks that the changed bus connects to.Revert the bus changes so that the bus continues to match the associated
Bus
object.
Save Simulink.Bus Objects
To saveBus
objects stored in the base workspace, you can use any MATLAB technique that saves the contents of the base workspace. However, the resulting file contains everything in the base workspace, not justBus
对象。
You can saveBus
objects in a variety of locations.
Location | File Creation Method | File Contents |
---|---|---|
Data dictionary |
SeeMigrate Models to Use Simulink Data Dictionary. | Bus objects and other base workspace variables used by a model |
Function |
Use theBus EditororSimulink.Bus.save function. |
Bus objects |
MAT file |
Use theBus Editor. | Bus objects |
Database or other external files |
Use the |
Bus objects |
When you modify savedBus
objects, you must resave them to keep the changes.
Map Simulink.Bus Objects to Models
你模拟一个模型之前,所有的Bus
objects it uses must be loaded into the base workspace or a data dictionary used by the model. For automation and consistency across models, mappingBus
objects to models is important.
By identifying all of the
Bus
objects that a model requires, you can ensure that those objects are loaded before model execution.By identifying all models that use a
Bus
object, you can ensure that changes to aBus
object do not cause unexpected changes in any of the models that use theBus
object.
To ensure the necessaryBus
objects load before model execution, consider:
Projects — Automatically load or run files that define
Bus
objects by configuring the files to run when you open a project. For details, seeProject Management.Data dictionaries — Store
Bus
objects with variables and other objects for one or more models.To share a
Bus
object among models, you can link each model to a dictionary and create a common referenced dictionary to store the object. For an example, seePartition Dictionary Data Using Referenced Dictionaries.Databases — Capture mapping information in an external data source, such as a database.
Model callbacks — Load or run files that define
Bus
objects by using a model callback, such asPreLoadFcn
. For more information, seeModel Callbacks.If a model uses only a few
Bus
objects, consider copying theBus
目标代码直接进入回调,而不是loading a file. For an example, open modelex_bus_tutorial_nonvirtual
and examine the callback.
To find where aBus
object is used in an open model, seeFinding Blocks That Use a Specific Variable.
Tip
Using a rigorous and standard naming convention is very helpful for mappingBus
object usage. For example, consider the model and data required for an actuator control function. Naming the modelActuator
and the input and output portsActuator_bus_in
andActuator_bus_out
, respectively, makes the connection between theBus
objects and the model clear.
Note that this approach can cause issues if the output from one model is fed directly to another model. In this case, the naming mismatch results in an error.