2
votes

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
I believe that a MATLAB Function block is the easiest was to do this.Phil Goddard
@PhilGoddard That's what I thought, but i wouldn't really know how to approach the problem. The bus signals are interpreted as a struct in the function block, so a for loop through all elements might be a solution, but I wouldn't know how and I don't think it would be efficient.Bart
It looks like you should be able to use logical indexing to do the assignments, rather than a loop. But either way, note that the code withing the MATLAB Function block is being converted to C and compiled, it is not running in the interpreter, so speed shouldn't be an issue.Phil Goddard

1 Answers

1
votes

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.