主要内容

Fixed Point Parameters in Legacy Functions

This example shows you how to use the Legacy Code Tool to integrate legacy C functions that pass their inputs and outputs using parameters of fixed point data type.

旧版代码工具允许您:

  • 提供旧功能规范,

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

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

提供旧功能规范

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

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

在逻辑上,myfixpt是一种固定点数据类型,它是16位整数的物理上的类型:

myfixpt = 金宝appsimulink.numerictype;myfixpt.datatypemode =“定点:二进制点缩放”;myFixpt。签署= true;myFixpt。字= 16;myFixpt。FractionLength = 10; myFixpt.IsAlias = true; myFixpt.HeaderFile ='timesFixpt.h';

遗产源代码在文件中找到timesFixpt.h, 和timesS16.c

% sldemo_sfun_gain_fixptdef = legacy_code('初始化');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'};

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

将第一个输入设置为“ 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/bdoc22a_18991349_108461_108461/tp5e3dbcct/ex52/ctcrctrct''/tmp/bdoc2a_1891349_1084616.1652/ctcrctrct'''';MEX成功完成。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文件

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.

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

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);

Integration with Legacy Code

该模型sldemo_lct_fixpt_params显示与旧版代码的集成。子系统TestFixpt是通过生成的S函数来调用传统C函数的安全带。

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]

也可以看看