Documentation

Persistent Memory Within Legacy Functions

此示例向您展示了如何使用旧版代码工具将旧版C函数与实例特定持久内存集成在一起。

旧版代码工具允许您:

  • 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. For detailed help on the properties, calllegacy_code('help'). The prototype of the legacy functions being called in this example are:

void memory_bus_init(COUNTERBUS *mem, int32_T upper_sat, int32_T lower_sat);

void memory_bus_step(COUNTERBUS *input, COUNTERBUS *mem, COUNTERBUS *output);

where mem is an instance specific persistent memory for applying a one integration step delay. COUNTERBUS is a struct typedef defined incounterbus.hand implemented with a Simulink.Bus object in the base workspace. The legacy source code is found in the filesmemory_bus.h, 和memory_bus.c.

evalin('根据','load sldemo_lct_data.mat')% sldemo_sfun_workdef = legacy_code('初始化'); def.SFunctionName ='sldemo_sfun_work';def.InitializeConditionsFcnSpec ='void memory_bus_init(Counterbus Work1 [1],INT32 P1,INT32 P2)';def.outputfcnspec ='void memory_bus_step(conterbus u1 [1],conterbus work1 [1],counterbus y1 [1])';def.HeaderFiles = {'memory_bus.h'};def.SourceFiles = {'memory_bus.c'};def.IncPaths = {'sldemo_lct_src'};def.srcpaths = {'sldemo_lct_src'};

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_work.c.

legacy_code('generate_for_sim', def);
###开始编译SLDEMO_SFUN_WORK MEX(' -  I/MATHWORKS/DEVEL/BAT/BR2018AD/BUILD/MATLAB/MATLAB/simulink/si万博1manbetxmdemos/simdemos/simfeatures/sldemo_lct_src'c', '-outdir', '/tmp/BR2018ad_802882_51908/publish_examples4/tp2760349d_43e5_48c9_b005_085a42f4a23d', '/mathworks/devel/bat/BR2018ad/build/matlab/toolbox/万博1manbetxsimulink/simdemos/simfeatures/sldemo_lct_src/memory_bus.c') Building with'GCC'。MEX成功完成。mex('sldemo_sfun_work.c','-i/Mathworks/devel/devel/bat/br2018ad/build/matlab/sim万博1manbetxulink/simulink/simdemos/simfeatures/sldemo_lct_src'TMP/BR2018AD_802882_51908/PUSHRASS_EXAMPLES4/TP2760349D_43E5_48C9_B005_085A42F4A23D/MONEME_BUS.BUS.O')带有'gcc'的建筑物。MEX成功完成。###完成编译SLDEMO_SFUN_WORK ###退出

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.

不te: 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

在编译了C-MEX S功能源后,可以将第一个输入设置为“ slblock_generate”再次调用函数legacy_code(),以生成一个配置为调用S函数的屏蔽s功能块。该块放置在新模型中,可以复制到现有模型。

%lectacy_code('slblock_generate',def);

将生成的集成与旧代码进行演示

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

open_system('sldemo_lct_work') open_system('sldemo_lct_work/memory_bus') sim('sldemo_lct_work')
ans = Simulink.SimulationOutput: ScopeDataA: [11x3 double] tout: [11x1 double] SimulationMetadata: [1x1 Simulink.SimulationMetadata] ErrorMessage: [0x0 char]

这个话题有帮助吗?