MATLAB: Autocode fails without setting S-function parameters as runtime params

autocodemexs-functionsimulinksimulink codertlc

I'm trying to integrate a MEX S-function into a model with accompanying TLC so that it 1) runs the MEX during simulation, but 2) autocodes the right C wrapper during autocoding. Everything was working fine (model simulating fine, autocoding fine) up until we had some configuration changes, and now when I attempt to autocode the model, I get an error like this:
S-function 'string_sequencer' in '[my S-function block]' does not explicitly set the number of run-time parameters using ssSetNumRunTimeParams. This compatibility diagnostic on the need for S-function upgrades can be set to none, warning, or error using the Configuration Parameters dialog
Except, in that code, I was setting the number of runtime params using ssSetNumRuntimeParams in the mdlSetWorkWidths function: I was setting it to 0. However, there are 7 tunable S-function parameters for this block. When I simply set the number of runtime parameters to 7, I get this error:
In S-function 'X/X/X_SEQUENCER/StringBasedSequencer', dialog parameter 1 has not been utilized to create any run-time parameter even though it was declared to be tunable. Each tunable dialog parameter must appear in the 'dlgParamIndices' field of at least one run-time parameter
…which begins to make it a little clearer what the issue is. The final thing I tried was to put this piece in my mdlSetWorkWidths:
char **names = ...set all of the variable names...
ssRegAllTunableParamsAsRunTimeParams(S, names);
This gets past all of the previous errors I was getting about not setting the number of runtime params, but then my TLC breaks. Specifically, I have a block that looks like this in my TLC
%function Start(block, system) Output
%assign nelements1 = LibBlockParameterSize(P1)
%assign param_width1 = nelements1[0] * nelements1[1]
%if (param_width1) > 1
%assign pp1 = LibBlockMatrixParameterBaseAddr(P1)
%else
%assign pp1 = LibBlockParameterAddr(P1, "", "", 0)
%endif
functionThatUsesP1(%<pp1>, %<param_width1>)
%endfunction
…and now that my parameters are runtime parameters, they must no longer be visible to the Start function, because I now get the error "Undefined identifier P1" when my TLC is being used for the autocode.
It seems to me that there could be a few ways out of this issue. First, if I rewind the configuration parameters such that if I set "Diagnostics -> Compatibility -> S-function upgrades needed" to "warning" or "none" instead of "error", everything autocodes and simulates just fine. The problem here is that I don't necessarily control the configuration, and changes to it will affect other systems. So, my first series of questions: how important is the first error that pops up, about not explicitly setting the number of runtime params? Is it acceptable to ignore the error via this configuration parameter, and if so, what am I trading by doing this? Why does Simulink/Coder want those tunable S-function parameters declared as runtime parameters if it works just fine without them being so? Is there some other way to set up those parameters so I won't get the error about them not being runtime params?
If I do need my S-function parameters to be runtime parameters, then what's the best way to access those parameters via TLC? I.e. since P1 is no longer visible if I set all of my tunable parameters as runtime parameters, are those parameters named something else? Thanks in advance for any insight,
Dan

Best Answer

I think ssRegAllTunableParamsAsRunTimeParams(S, names); is the right command to use in your case. To access the corresponding parameters in the TLC, use the same names that you specified in the names array to the second argument of ssRegAllTunableParamsAsRunTimeParams. For example, as several S-function demos show:
const char_T *rtParamNames[] = {"Gain"};
ssRegAllTunableParamsAsRunTimeParams(S, rtParamNames);
Corresponds to this in TLC:
%assign k = LibBlockParameter(Gain, "", "", 0)