Specified or Inherited Sample Time with Legacy Functions
This example shows you how to use the Legacy Code Tool to integrate legacy C functions with the sample time specified, inherited and parameterized.
The Legacy Code Tool allows you to:
Provide the legacy function specification,
Generate a C-MEX S-function that is used during simulation to call the legacy code, and
Compile and build the generated S-function for simulation.
Providing the Legacy Function Specification
Functions provided with the Legacy Code Tool take a specific data structure or array of structures as the argument. The data structure is initialized by calling the function legacy_code()
using initialize
as the first input. After initializing the structure, you have to assign its properties to values corresponding to the legacy code being integrated. The prototypes of the legacy functions being called in this example are:
FLT gainScalar(const FLT in, const FLT gain)
where FLT
is a typedef to float. The legacy source code is found in the files your_types.h
, gain.h
, and gainScalar.c
.
defs = []; % sldemo_sfun_st_inherited def = legacy_code('initialize'); def.SFunctionName = 'sldemo_sfun_st_inherited'; def.OutputFcnSpec = 'single y1 = gainScalar(single u1, single p1)'; def.HeaderFiles = {'gain.h'}; def.SourceFiles = {'gainScalar.c'}; def.IncPaths = {'sldemo_lct_src'}; def.SrcPaths = {'sldemo_lct_src'}; defs = [defs; def]; % sldemo_sfun_st_fixed def = legacy_code('initialize'); def.SFunctionName = 'sldemo_sfun_st_fixed'; def.OutputFcnSpec = 'single y1 = gainScalar(single u1, single p1)'; def.HeaderFiles = {'gain.h'}; def.SourceFiles = {'gainScalar.c'}; def.IncPaths = {'sldemo_lct_src'}; def.SrcPaths = {'sldemo_lct_src'}; def.SampleTime = [2 1]; defs = [defs; def]; % sldemo_sfun_st_parameterized def = legacy_code('initialize'); def.SFunctionName = 'sldemo_sfun_st_parameterized'; def.OutputFcnSpec = 'single y1 = gainScalar(single u1, single p1)'; def.HeaderFiles = {'gain.h'}; def.SourceFiles = {'gainScalar.c'}; def.IncPaths = {'sldemo_lct_src'}; def.SrcPaths = {'sldemo_lct_src'}; def.SampleTime = 'parameterized'; defs = [defs; def];
Generating and Compiling an S-Function for Use During Simulation
The function legacy_code()
is called again with the first input set to generate_for_sim
in order to automatically generate and compile the C-MEX S-function according to the description provided by the input argument defs
. This S-function is used to call the legacy functions in simulation. The source code for the S-function is found in the files sldemo_sfun_st_inherited.c
, sldemo_sfun_st_fixed.c
, and sldemo_sfun_st_parameterized.c
.
legacy_code('generate_for_sim', defs);
### Start Compiling sldemo_sfun_st_inherited mex('-I/tmp/Bdoc24b_2725827_2542211/tp61fd10c4/simulink_features-ex32622275/sldemo_lct_src', '-I/tmp/Bdoc24b_2725827_2542211/tp61fd10c4/simulink_features-ex32622275', '-c', '-outdir', '/tmp/Bdoc24b_2725827_2542211/tp45161f6f_ba34_4676_b1af_0985ce3e6499', '/tmp/Bdoc24b_2725827_2542211/tp61fd10c4/simulink_features-ex32622275/sldemo_lct_src/gainScalar.c') Building with 'gcc'. MEX completed successfully. mex('sldemo_sfun_st_inherited.c', '-I/tmp/Bdoc24b_2725827_2542211/tp61fd10c4/simulink_features-ex32622275/sldemo_lct_src', '-I/tmp/Bdoc24b_2725827_2542211/tp61fd10c4/simulink_features-ex32622275', '/tmp/Bdoc24b_2725827_2542211/tp45161f6f_ba34_4676_b1af_0985ce3e6499/gainScalar.o') Building with 'gcc'. MEX completed successfully. ### Finish Compiling sldemo_sfun_st_inherited ### Exit ### Start Compiling sldemo_sfun_st_fixed mex('-I/tmp/Bdoc24b_2725827_2542211/tp61fd10c4/simulink_features-ex32622275/sldemo_lct_src', '-I/tmp/Bdoc24b_2725827_2542211/tp61fd10c4/simulink_features-ex32622275', '-c', '-outdir', '/tmp/Bdoc24b_2725827_2542211/tp6b2cb0c0_4ac8_4b98_b3bd_f9517ff8f796', '/tmp/Bdoc24b_2725827_2542211/tp61fd10c4/simulink_features-ex32622275/sldemo_lct_src/gainScalar.c') Building with 'gcc'. MEX completed successfully. mex('sldemo_sfun_st_fixed.c', '-I/tmp/Bdoc24b_2725827_2542211/tp61fd10c4/simulink_features-ex32622275/sldemo_lct_src', '-I/tmp/Bdoc24b_2725827_2542211/tp61fd10c4/simulink_features-ex32622275', '/tmp/Bdoc24b_2725827_2542211/tp6b2cb0c0_4ac8_4b98_b3bd_f9517ff8f796/gainScalar.o') Building with 'gcc'. MEX completed successfully. ### Finish Compiling sldemo_sfun_st_fixed ### Exit ### Start Compiling sldemo_sfun_st_parameterized mex('-I/tmp/Bdoc24b_2725827_2542211/tp61fd10c4/simulink_features-ex32622275/sldemo_lct_src', '-I/tmp/Bdoc24b_2725827_2542211/tp61fd10c4/simulink_features-ex32622275', '-c', '-outdir', '/tmp/Bdoc24b_2725827_2542211/tp4394c4c4_ad8d_41a6_9b7d_511b63847285', '/tmp/Bdoc24b_2725827_2542211/tp61fd10c4/simulink_features-ex32622275/sldemo_lct_src/gainScalar.c') Building with 'gcc'. MEX completed successfully. mex('sldemo_sfun_st_parameterized.c', '-I/tmp/Bdoc24b_2725827_2542211/tp61fd10c4/simulink_features-ex32622275/sldemo_lct_src', '-I/tmp/Bdoc24b_2725827_2542211/tp61fd10c4/simulink_features-ex32622275', '/tmp/Bdoc24b_2725827_2542211/tp4394c4c4_ad8d_41a6_9b7d_511b63847285/gainScalar.o') Building with 'gcc'. MEX completed successfully. ### Finish Compiling sldemo_sfun_st_parameterized ### Exit
Generating an rtwmakecfg.m File for Code Generation
After the TLC block file is created, the function legacy_code()
can be called again with the first input set to rtwmakecfg_generate
in order to generate an rtwmakecfg.m
file to support code generation through Simulink® Coder™. Generate the rtwmakecfg.m
file if the required source and header files for the S-functions are not in the same directory as the S-functions, and you want to add these dependencies in the makefile
produced during code generation.
Note: Complete this step only if you are going to simulate the model in accelerated mode.
legacy_code('rtwmakecfg_generate', def);
Generating Masked S-Function Blocks for Calling the Generated S-Functions
After the C-MEX S-function source is compiled, the function legacy_code()
can be called again with the first input set to slblock_generate
in order to generate masked S-function blocks which are configured to call those S-functions. The blocks are placed in a new model and can be copied to an existing model.
% legacy_code('slblock_generate', defs);
Integration the Legacy Code
The model sldemo_lct_sampletime
shows integration with the legacy code. The subsystem sample_time serves as a harness for the calls to the legacy C functions, with unit delays serving to store the previous output values.
open_system('sldemo_lct_sampletime') open_system('sldemo_lct_sampletime/sample_time') sim('sldemo_lct_sampletime');