I'm trying to generate code for a simple model which has a Level-2 M-File S-Function in it. My S-Function takes in one parameter from the dialog box which is used for some computation. I understand that I have to write a corresponding .tlc file to tell RTW how to generate code for this S-Function. My question is this:
In the Ouputs() method in the .m file, I need access to the dialog param and this is done using block.DialogPrm(1).Data. This is all good, but when I need to access this same dialog param while writing the TLC file, I face errors.
Pasted below is my .m code and the corresponding .tlc file. Any help is greatly appreciated.
Thanks,
Tanuj
_______________________add.m______________________________________________
function add(block)
setup(block);
%endfunction
function setup(block)
%% Register number of input and output ports
block.NumInputPorts = 2;
block.NumOutputPorts = 1;
%% Setup functional port properties to dynamically
%% inherited.
block.SetPreCompInpPortInfoToDynamic;
block.SetPreCompOutPortInfoToDynamic;
% Register parameters
block.NumDialogPrms = 1; % Power
block.DialogPrmsTunable = {'Nontunable'};
block.InputPort(1).DirectFeedthrough = true;
block.InputPort(2).DirectFeedthrough = true;
%% Set block sample time to inherited
block.SampleTimes = [-1 0];
%% Run accelerator on TLC
block.SetAccelRunOnTLC(true);
%% Register methods
block.RegBlockMethod('CheckParameters', @CheckPrms);
block.RegBlockMethod('ProcessParameters', @ProcessPrms);
block.RegBlockMethod('PostPropagationSetup', @DoPostPropSetup);
block.RegBlockMethod('Outputs', @Output);
%endfunction
function CheckPrms(block)
%% nothing to check here
%endfunction
function ProcessPrms(block)
%% Update run time parameters
block.AutoUpdateRuntimePrms;
%endfunction
function DoPostPropSetup(block)
%% Register all tunable parameters as runtime parameters.
block.AutoRegRuntimePrms;
%endfunction
function Output(block)
Power = block.DialogPrm(1).Data;
block.OutputPort(1).Data = (block.InputPort(1).Data + block.InputPort(2).Data)^Power;
%endfunction
___________________________add.tlc_______________________________________
%implements "add" "C"
%% Function: Outputs=======================================================
%% Abstract:
%% Y = (U1 + U2)^DialogParam1
%function Outputs(block,system) Output
%assign a = LibBlockParameter(Power,"","",0) %%This is where I face the problem, I am not sure how to access the S-Func dialog parameter here.
/*%<Type> Block: %<Name>*/
%assign u1 = LibBlockInputSignal(0,"","",0)
%assign u2 = LibBlockInputSignal(1,"","",1)
%assign y = LibBlockOutputSignal(0,"","",0)
%<y> = pow((%<u1> + %<u2>),%<a>)
%endfunction
/*End %<Type> Block: %<Name>*/
Basically, in the S-Function you need to register and use a WriteRTW method, which will write the parameters to the .rtw file during code gen.
i.e.
You need to register the method
block.RegBlockMethod('WriteRTW', @WriteRTW);
And include/write the function:
function WriteRTW(block)
% Save the parameters to an RTW file for code generation.
p1 = sprintf('%f', block.DialogPrm(1).Data);
block.WriteRTWParam('string', 'Pow', p1);
%endfunction
Then the tlc file uses the method ParamSettings to read that info from the .rtw file.
i.e.
%implements "testAdd_sfun" "C"
%% Function: Outputs=======================================================
%% Abstract:
%% Y = (U1 + U2)^DialogParam1
%function Outputs(block,system) Output
%%This is where I face the problem, I am not sure how to access the S-Func dialog parameter here.
/*%<Type> Block: %<Name>*/
%assign u1 = LibBlockInputSignal(0,"","",0)
%assign u2 = LibBlockInputSignal(1,"","",0)
%% Get the power coefficient from the RTW file.
%assign a = ParamSettings.Pow
%assign y = LibBlockOutputSignal(0,"","",0)
%<y> = pow((%<u1> + %<u2>),%<a>)
%endfunction
Phil.
Thanks a bunch for the timely response. What you explained worked perfectly, thanks!
Tanuj