S-Functions and Code Generation
您使用s函数扩展simulink万博1manbetx®support for simulation and code generation. For example, you can use them to:
Represent custom algorithms
Interface existing external code with Simulink and the code generator
代表用于与硬件接口的设备驱动程序
Generate highly optimized code for embedded systems
Verify code generated for a subsystem as part of a Simulink simulation
The application program interface (API) for writing S-functions allows you to implement generic algorithms in the Simulink environment with a great deal of flexibility. If you intend to use S-functions in a model for code generation, the level of flexibility can vary. For example, it is not possible to access the MATLAB®workspace from an S-function that you use with the code generator. This topic explains conditions to be aware of for using S-functions However, using the techniques presented in this topic, you can create S-functions for most applications that work with the generated code.
尽管S函数为在模型中实现复杂算法提供了一种通用和灵活的解决方案,但基础API在内存和计算资源方面产生了开销。对于实时快速原型制度,通常可以接受其他资源。但是,在许多情况下,在实时嵌入式应用程序中不可用其他资源。您可以使用代码生成器提供的目标语言编译器技术来最大程度地减少内存和计算需求,从而嵌入您的s功能。如果您正在为现有外部代码生产S功能,请考虑使用旧版代码工具生成S函数和相关的TLC文件。
This content assumes that you understand the following concepts:
2级S函数
目标语言编译器(TLC) scripting
How the code generator produces and builds C/C++ code
笔记s
此信息适用于代码生成器用户。即使您当前不使用代码生成器,也要在编写S功能时遵循这些实践,尤其是在创建通用S-pountlions时。
Types of S-Functions
您可能会选择实现S函数进行仿真和代码生成的示例包括:
“I'm not concerned with efficiency. I just want to write one version of my algorithm and have it work in the Simulink and code generator products automatically.”
“I want to implement a highly optimized algorithm in the Simulink and code generator products that looks like a built-in block and generates very efficient code.”
“我有很多我需要的手写代码interface. I want to call my function from the Simulink and code generator products in an efficient manner.”
Respectively, the preceding situations map to the following MathWorks®terminology:
Noninlined S-function
Inlined S-function
Autogenerated S-function for external code
非线s功能
一诺丽果汁nlined S-function is a C or C++ MEX S-function that is treated identically by the Simulink engine and generated code. In general, you implement your algorithm once according to the S-function API. The Simulink engine and generated code call the S-function routines (for example,mdlOutputs
)在模型执行期间。
Additional memory and computation resources are required for each instance of a noninlined S-Function block. However, this routine of incorporating algorithms into models and code generation applications is typical during the prototyping phase of a project where efficiency is not important. The advantage gained by forgoing efficiency is the ability to change model parameters and structures rapidly.
编写非线值s功能不涉及TLC编码。非线值s函数是构建过程的默认情况,从某种意义上说,一旦您在模型中构建MEX s功能,则在按下之前就没有其他准备Ctrl+B建立您的模型。
Some restrictions exist concerning the names and locations of noninlined S-function files when generating makefiles. See编写非线值的S函数和TLC文件。
Inlined S-Functions
为了使S函数在Simulink环境中工作,生成了一些高架代码。万博1manbetx当代码生成器从包含s功能的模型中生成代码时(没有
文件),它在生成的代码中嵌入了一些此类开销代码。如果要优化实时代码并消除了一些间接费用,则必须inline(or embed) your S-functions. This involves writing a TLC (sfunction
。tlc
)文件,从生成的代码中消除了开销代码。目标语言编译器流程sfunction
。tlc
files to define how to inline your S-function algorithm in the generated code.sfunction
。tlc
笔记
The terminline不应该与C ++混淆inlinekeyword. Inline means to specify text in place of the call to the general S-function API routines (for example,mdlOutputs
). For example, when a TLC file is used to inline an S-function, the generated code contains the C/ C++ code that would normally appear within the S-function routines and the S-function itself has been removed from the build process.
完全插入的s功能以与内置块无法区分的方式将您的算法(块)构建到生成的代码中。通常,完全插入的S函数要求您两次实现算法:一次用于Simulink模型(C/C ++ MEX S功能),一次用于代码生成(TLC文件)。万博1manbetxTLC文件的复杂性取决于算法的复杂性以及您在生成的代码中要实现的效率水平。TLC文件在结构上从简单到复杂不等。看Inlining S-Functions
Autogenerated S-Functions for Legacy or Custom Code
如果您需要在模型中调用手写的C/C ++代码,请考虑使用Simulink Legacy代码工具。万博1manbetx旧版代码工具可以根据您在旧版代码工具数据结构中注册的信息来自动化完全插入的S函数和相应的TLC文件。
For more information, seeIntegrate C Functions Using Legacy Code Tool(Simulink) and seeImport Calls to External Code into Generated Code with Legacy Code Tool。
Files Required for Implementing Noninlined and Inlined S-Functions
This topic briefly describes what files and functions you need to create noninlined and inlined S-functions.
Noninlined S-functions require the C or C++ MEX S-function source code (
orsfunction
。c
).sfunction
。cppFully inlined S-functions require an
文件。完全插入的s功能为参数化的S功能产生最佳代码。这是一个s功能,以特定模式运行,取决于固定的S函数参数,这些参数在模型执行过程中不会更改。对于给定的操作模式,sfunction
。tlc
file specifies the exact code that is generated to implement the algorithm for that mode. For example, the direct-index lookup table S-function inWrite Fully Inlined S-Functions with mdlRTW Routinecontains two operating modes — one for evenly spacedsfunction
。tlcx-data
and one for unevenly spacedx-data
。笔记
Fully-inlined S-functions that are generated to invoke legacy or custom C/C++ code also require an
file, which is generated by Legacy Code Tool.sfunction
。tlc
Fully inlined S-functions might require the placement of themdlRTW
routine in your S-function MEX-file
orsfunction
。c
。Thesfunction
。cppmdlRTW
例行程序使您可以将信息放入
,指定模型的记录文件以及代码生成器在执行之前调用目标语言编译器进行处理model
。rtw
when generating code.sfunction
。tlc
包括mdlRTW
当您想在TLC文件中引入非戒断参数时,例程很有用。此类参数通常用于确定在s功能的给定实例中哪种操作模式处于活动状态。基于此信息,S功能的TLC文件可以为该操作模式生成高效,最佳代码。
编写支持代码生成的S功能的指南万博1manbetx
您可以使用代码生成的C/C ++ MEX,MATLAB语言和Fortran Mex S功能。
您可以通过提供内部tlc文件来插入代码生成的s函数。请参阅s函数在目标语言编译器。MATLAB和FORTRAN MEX S-runctions必须夹住。C/C ++ MEX S函数可以为代码效率而夹住,也可以非线。
To automatically generate a fully inlined C MEX S-function for invoking legacy or custom code, use the Legacy Code Tool. For more information, seeIntegrate C Functions Using Legacy Code Tool(Simulink) and seeImport Calls to External Code into Generated Code with Legacy Code Tool。
If code efficiency is not an overriding consideration, for example, if you are rapid prototyping, you can choose not to inline a C/C++ MEX S-function. For more information, see编写非线值的S函数和TLC文件。