File Exchange

image thumbnail

Dynamically create a bus testing harness in Simulink

version 1.0.0.1 (5.83 KB) by

Dynamically create a test harness in Simulink given a bus object with arbitrary underlying structure

1 Download

Updated

View License

This script will dynamically create a test harness in Simulink for a bus object of arbitrary nesting structure. To use the script just update the first two lines of the Initialization section to use the name of your main bus object.
The script works by starting with the top level bus object and looping through its elements. For any elements that are not bus objects, it will add a Constant block to the model and copy over the data type and signal size. For elements that are nested bus objects, it adds a Bus Creator block, sets it to use the bus object as a nonvirtual bus, and increments a counter to loop back through the new bus's elements. Each new block is connected to the Bus Creator above it in the hierarchy when it is added to the model. The blocks are then repositioned for visual clarity. Finally the model is updated to check if there were any problems with the setup.

A mat file with a sample set of nested bus objects is included. Load the mat file into the workspace then run the script to see it work with the sample bus objects.

Comments and Ratings (4)

Thank you so much for this contribution. I find it very useful.
The solution mentioned in the Mathworks blog requires that "Underspecified initialization detection" be set to "simplified"; which is not feasible in some cases.
http://blogs.mathworks.com/seth/2011/12/05/initializing-buses-using-a-matlab-structure/

I've made small enhancements to have the signal names on lines. I hope you find them useful.
Within the for-loop, in line 83

LineHandle = add_line(modelname,[get_param(Bus(numBC).handle,'name') '/1'],...
                [get_param(Bus(k).handle,'name') '/' num2str(Bus(numBC).TargetPortNum)]); %connect outport to parent bus inport
            set_param(LineHandle, 'Name', currentBus.Elements(j).Name);

and in line 92
LineHandle = add_line(modelname,[get_param(Const(numConst).handle,'name') '/1'],...
                [get_param(Bus(k).handle,'name') '/' num2str(Const(numConst).TargetPortNum)]); %connect Constant block to parent Bus Creator
            set_param(LineHandle, 'Name', currentBus.Elements(j).Name);

Gernot

Gernot (view profile)

Understood.
Code documented well and nice simple example how to distribute the blocks.

The script defines all variables in the main Matlab workspace. Possibly, a function would be an idea.

Adam Filion

Adam Filion (view profile)

Hi Gernot,

Thanks for pointing out that blog post. You're correct that there's a very simple solution pointed to in that post that will take care of checking initialization. However in that example you are initializing the bus through a constant block, so you can't use non-constant signals to check simulation behavior. In the attached file you could easily swap out the constant blocks for something else.

I originally built this such that it used sine blocks instead of constants, and read in the appropriate frequency and amplitudes from another source, but decided to reduce it to just constants to simplify the example. Once you get the hang of how the sorting works and using the Simulink API to construct a model, it's fairly easy to expand this example to more complex situations.

Adam

Gernot

Gernot (view profile)

Great because I've been looking for an example building and connecting simulink blocks.
But is this problem not solved since 2010a?
http://blogs.mathworks.com/seth/2011/12/05/initializing-buses-using-a-matlab-structure/

Updates

1.0.0.1

Updated license

MATLAB Release
MATLAB 7.14 (R2012a)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video