Main Content

遗产功能中的固定点参数

此示例向您展示了如何使用遗产C函数集成旧版C函数,该遗产C函数使用固定点数据类型的参数传递其输入和输出。

The Legacy Code Tool allows you to:

  • 提供旧功能规范,

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

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

提供旧功能规范

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

myFixpt timesS16(const myFixpt in1, const myFixpt in2, const uint8_T fracLength)

where myFixpt is logically a fixed point data type which is physically a typedef to a 16-bit integer:

myFixpt = Simulink.NumericType; myFixpt.DataTypeMode =“定点:二进制点缩放”;myfixpt.signed = true;myfixpt.wordlength = 16;myFixpt.FractionLength = 10;myfixpt.isalias = true;myfixpt.headerfile ='timesfixpt.h';

legacy source code is found in the filestimesFixpt.h, andtimesS16.c

% sldemo_sfun_gain_fixptdef = legacy_code('initialize');def.sfunctionName ='sldemo_sfun_gain_fixpt';def.OutputFcnSpec ='myfixpt y1 = timess16(myfixpt u1,myfixpt p1,uint8 p2)';def.headerfiles = {'timesfixpt.h'};def.sourcefiles = {'timess16.c'};def.IncPaths = {'sldemo_lct_src'};def.SrcPaths = {'sldemo_lct_src'};

Generating and Compiling an S-Function for Use During Simulation

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

legacy_code('generate_for_sim',def);
### Start Compiling sldemo_sfun_gain_fixpt mex('-I/tmp/Bdoc22a_1891349_108461/tp5e3db00b/ex52665162/sldemo_lct_src', '-I/tmp/Bdoc22a_1891349_108461/tp5e3db00b/ex52665162', '-c', '-outdir', '/tmp/BDOC22A_1891349_108461/TPF76B4E50_853C_4D4A_B89F_B89F_2DB5CC4D928A','/tmp/BDOC222A_18991349_10846118461/TP5E3DBCRCTCCRCTCRCTCRCTCRCTCRCTCRCTCRCTCRCTCRCTCRTCRCTCRTCRCTCRTCRCTRCTCRCTIMNENDENSLENSLENTIMENMEX成功完成。mex('sldemo_sfun_gain_fixpt.c', '-I/tmp/Bdoc22a_1891349_108461/tp5e3db00b/ex52665162/sldemo_lct_src', '-I/tmp/Bdoc22a_1891349_108461/tp5e3db00b/ex52665162', '/tmp/Bdoc22a_1891349_108461/tpf76b4e50_853c_4d4a_b89f_2db5cc4d928a/timesS16.o') Building与“ GCC”。MEX成功完成。###完成编译SLDEMO_SFUN_GAIN_FIXPT ###退出

生成一个用于代码生成的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_fixpt_paramsshows integration with the legacy code. The subsystem TestFixpt serves as a harness for the call to the legacy C function via the generated S-function.

open_system('sldemo_lct_fixpt_params')open_system('sldemo_lct_fixpt_params/TestFixpt')SIM('sldemo_lct_fixpt_params'
ans = 金宝appsimulink.simulationOutput:yout:[101x4 double] simulationMetadata:[1x1 simulink.simulationmetadata] errormessage:[0x0 char]

也可以看看