Creating Instances of Bus Objects defined in the Bus Editor

39 views (last 30 days)
As I am Working with Multi Object Tracking I use many Simulink Bus objects and have to create many custom buses, often containing arrays of buses. The only way to do this in Simulink right now is using the Bus Editor. But if you want to create a Bus Object of this type, you defined in the Bus Editor, in a Matlab block, you have to define it all again using structs and then hand the struct to the output Bus. This is sometimes twice the work if you want to use arrays and have to use the repmat function to define an array of structures. There really should be an option to just create an instance of the Object defined in the Bus Editor, so you dont have to define all the fields again when using it in a Matlab Block. Maybe someone can tell me a way how to do this. Elseway this is a message for the MATLAB developers to think about adding a feature like this. Because right now everytime I want to use a new Sensor with the Multi Object Tracker Blocks i have to do way to much work, figuring out all the datatypes of the messages send by the sensor and creating an object for them in the Bus editor and also having to create a struct in the Matlab Block.
Also a feature to click on a bus and copy its definition into the Bus Editor would be great. But this would be just a little extra.

Answers (1)

Elad Kivelevitch
Elad Kivelevitch on 16 Apr 2021
Hi Niklas,
Thank you for your question and comments. I can definitely appreciate the difficulty in creating buses that you describe.
There are a few more options to create buses, which I don't know if you have considered.
For me, what usually works best is to define a separate function that knows how to convert the data you want to pass on the bus to a struct. For example:
function thisStruct = convertToStruct(thisData)
end
I then use this function in two places:
  1. To create the bus object before the model compiles using Simulink.Bus.createObject(thisStruct).
  2. In the MATLAB function block that in the model to create the struct output from the block. (the MATLAB function block must use the same bus name as the one you defined for the bus above.
I hope this helps.
  1 Comment
Niklas Dörr
Niklas Dörr on 16 Apr 2021
Hi Elad,
Thank you for your fast reply!
I'm aware of all those options and I am allready using those options.
But if you want to create an array of buses and change its content there seems to be no other way then first creating the bus in the bus editor to use it as output in the matlab function block (because you cant inherit the type because it is a bus) and then defining it again in the matlab function block using repmat for an array of structs. This work is redundant and I would appreciate it so much if there was an option to create a bus object in a matlab function block with the data type defined in the bus editor.
For example:
I want to use the Multi Object Tracker Block. This block needs a Detection Object List as an Input. I can easily copy this bus structure in to the bus editor by using the Radar Data Creator. But if i want to create a totally new list of this type i cant just create a new object of this type and put the values i want into it by looping over it with a for loop. I first have to create the list as an empty struct of this type and write down every variable this bus contains to do this. Also if i just want to touch a few values and leave the rest as it is.
It would look like this:
detectionObjectList = repmat(struct('Time',0,'Measurement',[0; 0; 0; 0; 0; 0],'MeasurementNoise', ...
[0 0 0 0 0 0; 0 0 0 0 0 0; 0 0 0 0 0 0; 0 0 0 0 0 0; 0 0 0 0 0 0; 0 0 0 0 0 0],'SensorIndex',radarData.Objects(1,1).SensorIndex, ...
'ObjectClassID',0,'MeasurementParameters',struct( ...
'Frame',drivingCoordinateFrameType.Invalid,'OriginPosition',[0; 0; 0],'Orientation',[0 0 0; ...
0 0 0; 0 0 0],'HasVelocity',false),'ObjectAttributes',struct('TargetIndex',0)),80,1);
This seems way to much work for something that could be done in c++ by doing this:
detectionObjectList* arrayOfDetections = new detectionObjectList[80]
And this is still a small datastructure.
Also if you dont know the datatype which can often be the case if you just use incoming datastructures coming from a carmaker model or a ros custom message, you have to find them out by splitting every bus with bus selectors just to create a bus that is exactly the same as the one you allready have in the bus editor to use it as an input to the Matlab function block.

Sign in to comment.

Categories

Find more on Simulink Environment Customization in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!