I have two identical bus structures in Simulink, with different values. One is the (CAN) bus from a real vehicle, the other is an identical simulated (CAN) bus in Simulink. I want to compose a mixed, identical output bus with these, where some signals are taken from the first bus, and others from the second bus as is shown.
I want to switch with a third identical bus, containing a value for each signal determining to switch the signal to the output from either bus 1 or bus 2. Schematically, it looks like the grey section:
pic As you can see, the first signal (Rpm, 3) is taken from the vehicle bus, the second signal (Spd, 6) is taken from the simulated bus. Regarding structure, the output bus is identical to the two input buses. The principle in the schematic works, but I have over 500 signals, so this method is not really applicable anymore.
How can I do this without having to manually route 500 signals?
I'm thinking about a MATLAB function block, but suggestions are welcome.
1 Answer 1
I have found a way to solve this. It's not nearly the most elegant way, but it works.
Since buses are handled as structures in MATLAB function blocks, I for-looped through all fields and elements to select the desired source for each element in the output bus like this:
subbus = fieldnames(SLCT);
for i=1:+1:11
signal = fieldnames(SLCT.(subbus{i}))
for j=1:+1:5
switch SLCT.(subbus{i}).(signal{j})
case 0
TRGT.(subbus{i}).(signal{j}) = SRCA.(subbus{i}).(signal{j});
case 1
TRGT.(subbus{i}).(signal{j}) = SRCB.(subbus{i}).(signal{j});
otherwise
TRGT.(subbus{i}).(signal{j}) = SRCC.(subbus{i}).(signal{j});
end
end
end
I'm sure there must be much better, much faster ways, but this works and it's fast enough for my application.
Comments
Explore related questions
See similar questions with these tags.
MATLAB Functionblock is the easiest was to do this.logical indexingto do the assignments, rather than a loop. But either way, note that the code withing theMATLAB Functionblock is being converted to C and compiled, it is not running in the interpreter, so speed shouldn't be an issue.