Code generation from level-2 s-function without tlc-file

4 views (last 30 days)
Hello,
I`m using R2012b. I have a problem generating code for a xpc-target system. I have written a level 2 Matlab S-function in which I have called a c-code (which I have mex-ed before). I can call this C-Code inside the Simulink Matlab S-Function and it runs fine on the normal mode.
But now I have tried to compile this model to be run in a xpc-target system. I have added the corresponding c-file by parameters-> code generation -> custom code -> include list of additional -> source file = the c-file and -> libraries = the dll-file and the mexw64-file. But when building the model, I get the error: The corresponding "xxxx.tlc" file for the Matlab S-function "xxx" in block "xxx" must be located in the current working directory, the Matlab S-function directory, or the directory "xxx\tlc_c".
It is not possible to automatically create a tlc-file, when using a Matlab S-function. Is it really necessary to write the tlc-file manually? If yes, for what did I have to write the S-function? Could I call a C-code (legacy code) in an tlc-file?
Thanks in advance for any answers!
Bettina
  1 Comment
Or Hirshfeld
Or Hirshfeld on 19 Feb 2015
did you found any solution to by pass full coding in TLC because i have similar problem where i have level 2 s-function and i want to run the model in External mode in Windows Real-Time target

Sign in to comment.

Accepted Answer

Rajiv Ghosh-Roy
Rajiv Ghosh-Roy on 10 Dec 2013
In order to use this with code generation, everything you call must be in C (i.e. no M). The only exception would be if you were using a MATLAB function block.
In your case, the myfunc and myconstraint MATLAB files must also have a C equivalent. Without this, it would be hard to proceed.
  1 Comment
Bettina
Bettina on 17 Dec 2013
Thanks, I am then working to translate the MatLab M-S-Function into C-Code for the C-S-Function. Happy X-Mas!

Sign in to comment.

More Answers (1)

Rajiv Ghosh-Roy
Rajiv Ghosh-Roy on 9 Dec 2013
You can either write a C S-function, or add a simple TLC file for your MATLAB S-function. The TLC file could be as simple as calling an external function from your external C file.
Note that you will have to call LibAddToModelSources in order to inform the code generator of your external C-file, and also the external function call should be a C-style call, not a mex-file.
  2 Comments
Bettina
Bettina on 10 Dec 2013
As I am not so firm with C S-Functions (or C-programming in general), I had used the M S-Function. the write manually a TLC-file is not that easy, as I have to integrate somehow 2 subfunction, which I have until now called as matlab m-files.
What kind of language have the TLC-files? Is this C or can I also use MatLab commands?
I need to translate the following M S-function, maybe someone can help me with the corresponding TLC translation:
function msfuntmpl_NLopt_V7(block)
setup(block);
%endfunction
function setup(block)
block.NumDialogPrms = 1;
block.DialogPrmsTunable = {'Nontunable'};
block.NumInputPorts = 1;
block.InputPort(1).Dimensions = 2;
block.InputPort(1).DatatypeID = 0;
block.InputPort(1).Complexity = 'Real';
block.InputPort(1).DirectFeedthrough = false;
block.InputPort(1).SamplingMode = 'Sample';
block.NumOutputPorts = 2;
block.OutputPort(1).Dimensions = 2;
block.OutputPort(1).DatatypeID = 0;
block.OutputPort(1).Complexity = 'Real';
block.OutputPort(1).SamplingMode = 'Sample';
block.OutputPort(2).Dimensions = 1;
block.OutputPort(2).DatatypeID = 0;
block.OutputPort(2).Complexity = 'Real';
block.OutputPort(2).SamplingMode = 'Sample';
block.SampleTimes = [0 0];
block.SetAccelRunOnTLC(false);
block.RegBlockMethod('PostPropagationSetup', @DoPostPropSetup);
block.RegBlockMethod('InitializeConditions', @InitializeConditions);
block.RegBlockMethod('Outputs', @Outputs); % Required
% end setup
function DoPostPropSetup(block)
block.NumDworks = 1;
block.Dwork(1).Name = 'x0';
block.Dwork(1).Dimensions = 2;
block.Dwork(1).DatatypeID = 0;
block.Dwork(1).Complexity = 'Real';
block.Dwork(1).UsedAsDiscState = true;
block.NumDworks = 2;
block.Dwork(2).Name = 'xopt';
block.Dwork(2).Dimensions = 2;
block.Dwork(2).DatatypeID = 0;
block.Dwork(2).Complexity = 'Real';
block.Dwork(2).UsedAsDiscState = true;
block.NumDworks = 3;
block.Dwork(3).Name = 'fmin';
block.Dwork(3).Dimensions = 1;
block.Dwork(3).DatatypeID = 0;
block.Dwork(3).Complexity = 'Real';
block.Dwork(3).UsedAsDiscState = true;
% end DoPostPropSetup
function InitializeConditions(block)
global opt
opt.algorithm = block.DialogPrm(1).Data;
opt.lower_bounds = [-inf, 0];
opt.min_objective =@(x) myfunc(x);
opt.fc = {(@(x) myconstraint(x,2,0)), (@(x) myconstraint(x,-1,1))};
opt.fc_tol=[1e-8, 1e-8];
opt.xtol_rel = 1e-4;
block.Dwork(1).Data = block.InputPort(1).Data;
%end InitializeConditions
function Outputs(block)
global opt
[block.Dwork(2).Data, block.Dwork(3).Data] = n_optimize(opt, block.InputPort(1).Data); % Aufruf der Optimierungsroutine
block.OutputPort(1).Data = block.Dwork(2).Data;
block.OutputPort(2).Data = block.Dwork(3).Data;
% end Outputs
"myfunc" and "myconstraint" are M-Files, while "n_optimize" calls the C-Code (mexed before).
liangjunfu
liangjunfu on 26 May 2023
How did you solve this problem? I recently encountered the same problem.

Sign in to comment.

Categories

Find more on Block and Blockset Authoring in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!