Is it possible to access referenced model arguments from InitFcn callback?

I have a Simulink model with multiple instances of a referenced model which I am passing the argument 'vehicle' to in order to configure each model instance appropriately. One parameter I need to modify in the model is the port number of a TCP Receive block. As this block can not accept variables as arguments (e.g. 8000 + vehicle), I am attempting to use the models InitFcn to set the port parameter with the following code
mws = get_param(bdroot,'ModelWorkspace')
if (~mws.hasVariable('vehicle'))
error('Unkonwn variable vehicle');
end
veh = mws.getVariable('vehicle')
recv_port = num2str(8000 + veh - 1)
set_param([bdroot '/Receive'],'Port',recv_port);
Obviously, if I update this model in isolation I would expect the 'veh' variable to be empty as the argument has not been set. But when I update the master model I would expect the parameter to have its value set, but this is not the case, it always returns zero.
I have attached a cut down example which just uses two constant blocks to illustrate the problem, the first of which is updated by the InitFcn and always ends up as 8000, whereas the second is set correctly.

Answers (1)

Owen,
That was a tough one!
I think I figured out a way to do it using masks instead of the InitFcn callback. What I did was mask the Constant block with the port, and added the set_param code you had to the initialization commands of the Mask Editor. I hope the picture below helps.
I've also attached the models in R2014b format, so you can use them as you need. My output looks as follows, which is hopefully what you expected :)
- Sebastian

2 Comments

Hi Sebastian,
Thanks for the response, I have got your solution to work but only when the models are executed in Normal Mode (select from the Model block parameters). However, When I replace the constant block with a TCP receive block I get an error from Simulink which tells me the block doesn't support multiple instances in Normal Mode, so I choose to run the models in accelerator mode but this seems to break your solution.
I have also tried modifying the 'stcpiprb' S-function called by the TCP block to support multiple instances in normal mode, but this still didn't work. I think perhaps because the base mask for the TCP receive block specifies the Port parameter as non-tuneable. Perhaps this means it can not be modified at any time other than when the model is stopped (if I change the model workspace value manually then re-initialise the mask it works!).
If you can think of any way around this then please let me know. Otherwise I will just have to write my own S-function to do this.
Thanks again, Owen
If you use a masked library block instead of model reference, does this work? It may not be ideal, but at least could prevent you from having to write your own S-Function.
Note: If you really want model reference for the rest of the blocks that process the received message, you can just pull the TCP block out of the model and feed it into the input port of your new referenced model.

Sign in to comment.

Categories

Find more on Modeling in Help Center and File Exchange

Asked:

on 4 Feb 2015

Community Treasure Hunt

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

Start Hunting!