2

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.

asked Dec 13, 2018 at 16:58
3
  • 1
    I believe that a MATLAB Function block is the easiest was to do this. Commented Dec 13, 2018 at 19:56
  • @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. Commented Dec 14, 2018 at 9:10
  • 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. Commented Dec 14, 2018 at 15:41

1 Answer 1

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.

answered May 7, 2019 at 7:58
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.