Clear Filters
Clear Filters

Simulink and ROS2 - How to dynamically adjust the number of simulated systems to the number of robots

1 view (last 30 days)
In ROS2, I have a dynamic number of robots which I would like to connect to the controllers that are running in Simulink. While it is possible to create a static Simulink model with the needed number of systems, I would like to have a MATLAB script that dynamically adjusts the number of systems in the model.
Another option would be to run the same system with parameters in parallel in the following way:
model = 'model_name';
simIn(1:num_robots-1) = Simulink.SimulationInput(model);
for idx = 1:num_robots-1
simIn(idx) = simIn(idx).setVariable('var',var);
... % do for every variable needed
simIn(idx) = simIn(idx).setBlockParameter([model '/subscriber/Subscribe'],'Topic',['/robot' num2str(idx) '/topicName']);
...% do for every publisher/subscriber block
simOut = parsim(simIn);
However, if there are errors in one of the models, every other model keeps running and it is more difficult to get everything working right now. This could also caused because of some compatibility issues between the current ROS2 version of MATLAB and ROS2-Foxy, which I am using right now. Also, if there are errors, the "matlabprefs.mat" file gets corrupted and needs to be deleted before running the script again.
So, another idea would be to create a temporary model and use the 'Reference Model' or 'Model' block to add them, after which you would have to set the parameters for the variables and subscribers/publishers like in the previous method. However, I don't think it is actually possible to change the subscriber and publisher blocks this way, as the Topic parameter does not show up in the Parameters list when inspecting the subsystem.
My question would be: Is there some way to change the subsriber/publisher topic value in a "Model" block or do I have to use the parallel computing method to dynamically adjust the number of simulated systems?

Answers (1)

Alvaro on 28 Nov 2022
If you have already set up your system using parsim, then you could set StopOnError to 'on' which should stop the simulations if an error is encountered.
Alternatively, you could set a mask on a reference subsystem to promote certain block parameters. See the example here:

Community Treasure Hunt

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

Start Hunting!