Skip to content

Instantly share code, notes, and snippets.

@vibhoraggarwal
Last active January 5, 2022 02:36
Show Gist options
  • Save vibhoraggarwal/f206f60a0a0cd4aa2f2f26b3d1c47151 to your computer and use it in GitHub Desktop.
Save vibhoraggarwal/f206f60a0a0cd4aa2f2f26b3d1c47151 to your computer and use it in GitHub Desktop.
Implementation of S-function
  • The Legacy toolbox can generate, compile and link, and create a masked block for the specified S-function.
  • Legacy Code Tool helps users create S-functions automatically from supplied specifications expressed with MATLAB code

Essential commands

General commands

legacy_code('sfcn_cmex_generate', specs) generates an S-function source file as specified by the Legacy Code Tool data structure, specs.

legacy_code('compile', specs, compilerOptions) compiles and links the S-function generated by the Legacy Code Tool based on the data structure, specs, and compiler options that you might specify. The compiler options must be supported by the mex (MATLAB) function.

More important commands

legacy_code('generate_for_sim', specs, modelname) generates, compiles, and links the S-function in a single step. If the Options.useTlcWithAccel field of the Legacy Code Tool data structure is set to logical 1 (true), the function also generates a TLC file for accelerated simulations.

legacy_code('slblock_generate', specs, modelname) generates a masked S-Function block for the S-function generated by the Legacy Code Tool based on the data structure, specs. The block appears in the Simulink® model specified by modelname. If you omit modelname, the block appears in an empty model editor window.

Declaring Legacy Code Tool Function Specifications

The InitializeConditionsFcnSpec, OutputFcnSpec, StartFcnSpec, and TerminateFcnSpec fields defined in the Legacy Code Tool data structure require character vector values that adhere to a specific syntax format. The required syntax format enables the Legacy Code Tool to map the return value and arguments of an existing C function to the return value, inputs, outputs, parameters, and work vectors of the S-function that the tool generates.

General syntax

return-spec = function-name(argument-spec) For example, the following character vector specifies a function named doubleIt with return specification double y1 and input argument specification double u1.

def.OutputFcnSpec = 'double y1 = doubleIt(double u1)';

Compiling the libraries

Shared libraries (*.so (linux), or *.dll (Windows) files)

Using command: specs.HostLibFiles = {['exlib',strrep(system_dependent('GetSharedLibExt'),'.dll','.lib')]}; mentioned here

Points to be noted

  1. There should be two implementations of the function:[1]
  • One for simulating your model on the host computer
  • other for the target hardware
  1. The Legacy Code Tool can interface with C++ functions, but not C++ objects

References

  1. https://in.mathworks.com/matlabcentral/answers/404764-how-to-properly-generate-s-functions-for-functions-from-a-static-library
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment