Main Content

Multi-Dimensional Signals in Legacy Functions

此示例向您展示了如何使用旧版代码工具将旧版C函数与多维信号集成在一起。

The Legacy Code Tool allows you to:

  • 提供旧功能规范,

  • 生成一个C-MEX S功能,该功能在模拟过程中用于调用旧代码,并

  • 编译并构建生成的S功能以进行仿真。

提供旧功能规范

遗留代码工具提供的功能将特定的数据结构或一系列结构作为参数。数据结构是通过使用“初始化”为第一个输入来调用函数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.handndarray_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尺寸(U1,1),INT32尺寸(U1,2),INT32尺寸(U1,3))'];def.headerfiles = {'ndarray_ops.h'};def.sourcefiles = {'ndarray_ops.c'};def.incpaths = {'sldemo_lct_src'};def.SrcPaths = {'sldemo_lct_src'};

where y1 is a 3-D output signal of same dimensions as the 3-D input signal u1. Note that the last 3 arguments passed to the legacy function correspond to the number of element in each dimension of the 3-D input signal u1.

在模拟过程中生成和编译S功能以供使用

将第一个输入设置为“ generate_for_sim”再次调用函数lecacy_code(),以根据输入参数“ def”提供的描述自动生成和编译C-Mex s功能。此S功能用于调用模拟中的旧功能。在文件中找到S功能的源代码sldemo_sfun_ndarray_add.c

legacy_code('generate_for_sim',def);
# # #开始编译sldemo_sfun_ndarray_add墨西哥人(' -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

生成一个用于代码生成的RTWMAKECFG.M文件

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

注意:仅当您要以加速模式模拟模型时,才能完成此步骤。

legacy_code('rtwmakecfg_generate',def);

生成蒙版的S功能块,用于调用生成的S功能

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.

% legacy_code('slblock_generate', def);

集成旧代码

该模型sldemo_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');

也可以看看