Main Content

旧功能中的多维信号

This example shows you how to use the Legacy Code Tool to integrate legacy C functions with Multi-Dimensional Signals.

The Legacy Code Tool allows you to:

  • Provide the legacy function specification,

  • 生成一个C-MEX si期间使用的功能mulation to call the legacy code, and

  • Compile and build the generated S-function for simulation.

Providing the Legacy Function Specification

遗留代码工具提供的功能将特定的数据结构或一系列结构作为参数。数据结构是通过使用“初始化”为第一个输入来调用函数lacacy_code()来初始化数据结构的。初始化结构后,您必须将其属性分配给对应于要集成的旧代码的值。在此示例中调用的遗产函数的原型是:

void array3d_add(real_T *y1, real_T *u1, real_T *u2, int32_T nbRows, int32_T nbCols, int32_T nbPages);

其中real_t是double的打字,而int32_t是32位整数的Typedef。遗产源代码在文件中找到ndarray_ops.hndarray_ops.c.

% sldemo_sfun_ndarray_adddef = legacy_code('initialize');def.sfunctionName ='sldemo_sfun_ndarray_add'; def.OutputFcnSpec = ['void array3d_add(double y1 [size(u1,1)] [size(u1,2)] [size(u1,3)],',...'double u1[][][], double u2[][][], '...'int32 size(u1,1), int32 size(u1,2), int32 size(u1,3))']; def.HeaderFiles = {'ndarray_ops.h'};def.sourcefiles = {'ndarray_ops.c'};def.IncPaths = {'sldemo_lct_src'};def.SrcPaths = {'sldemo_lct_src'};

其中Y1是与3-D输入信号U1相同的尺寸的3-D输出信号。请注意,最后3个参数传递给了遗留函数,对应于3-D输入信号U1的每个维度中的元素数量。

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 'def'. This S-function is used to call the legacy functions in simulation. The source code for the S-function is found in the filesldemo_sfun_ndarray_add.c.

legacy_code('generate_for_sim',def);
### Start Compiling sldemo_sfun_ndarray_add mex('-I/tmp/Bdoc22a_1891349_110180/tpda6e582c/ex68315702/sldemo_lct_src', '-I/tmp/Bdoc22a_1891349_110180/tpda6e582c/ex68315702', '-c', '-outdir', '/tmp/Bdoc22a_1891349_110180/tp1cc6da6f_06c7_4cda_a891_7bf456b4c3fb', '/tmp/Bdoc22a_1891349_110180/tpda6e582c/ex68315702/sldemo_lct_src/ndarray_ops.c') Building with 'gcc'. MEX completed successfully. mex('sldemo_sfun_ndarray_add.c', '-I/tmp/Bdoc22a_1891349_110180/tpda6e582c/ex68315702/sldemo_lct_src', '-I/tmp/Bdoc22a_1891349_110180/tpda6e582c/ex68315702', '/tmp/Bdoc22a_1891349_110180/tp1cc6da6f_06c7_4cda_a891_7bf456b4c3fb/ndarray_ops.o') Building with 'gcc'. MEX completed successfully. ### Finish Compiling sldemo_sfun_ndarray_add ### Exit

Generating an rtwmakecfg.m File for Code Generation

创建TLC块文件后,可以将第一个输入设置为“ rtwmakecfg_generate”再次调用函数lecacy_code(),以生成rtwmakecfg.m文件以通过Simulink®Coder™支持代码生成。万博1manbetx万博1manbetx如果S-函数的所需源和标头文件与S-函数的目录不同,则生成RTWMAKECFG.M文件,并且您想在代码生成过程中生成的makefile中添加这些依赖项。

Note: Complete this step only if you are going to simulate the model in accelerated mode.

legacy_code('rtwmakecfg_generate',def);

Generating a Masked S-Function Block for Calling the Generated S-Function

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 a masked S-function block that is configured to call that S-function. The block is placed in a new model and can be copied to an existing model.

%lectacy_code('slblock_generate',def);

Integrate the Legacy Code

The modelsldemo_lct_ndarrayshows integration with the legacy code. The subsystem ndarray_add serves as a harness for the call to the legacy C function.

open_system('sldemo_lct_ndarray') open_system('sldemo_lct_ndarray/ndarray_add') sim('sldemo_lct_ndarray');

See Also