主要内容

Generate Code for Fuzzy System Using Simulink Coder

You can generate code for a Fuzzy Logic Controller block using Simulink® Coder™. For more information on generating code, seeGenerate Code Using Simulink Coder(Simulink Coder)

尽管此示例为1型Sugeno模糊推理系统生成代码,但该工作流程也适用于Mamdani和Type-2模糊系统。

Generate Code for Fuzzy Inference System

默认情况下,模糊逻辑控制器块使用双精度数据进行仿真和代码生成。这fuzzyPIDmodel is configured to use double-precision data. For more information on configuring your fuzzy inference system for code generation, seeFuzzy Logic Controller

mdl =“ fuzzypid”;Open_System(MDL)

It is good practice to validate the performance of the system in Simulink. Run the simulation. The model saves the output responseuto the MATLAB® workspace.

sim(mdl)

要为模型生成代码,请使用slbuild(Simulink)function. For this example, suppress the Command Window output for the build process.

set_param(mdl,'RTWVerbose','离开') slbuild(mdl)
### Starting build procedure for: fuzzyPID ### Successful completion of build procedure for: fuzzyPID Build Summary Top model targets built: Model Action Rebuild Reason ========================================================================================= fuzzyPID Code generated and compiled Code generation information file does not exist. 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 15.191s

By default, Simulink Coder generates C code for a generic real-time target. To select a different target file and language, in the Configuration Parameters dialog box, modify theSystem target fileLanguage参数分别。

这generated code is stored in a newfuzzyPID_grt_rtwfolder in your current working folder. The name of this folder depends on the selected target file.

在Windows®系统上,默认情况下,一个名为的可执行文件fuzzypid.exe还添加到当前的工作文件夹中。要生成无编译的代码,在“配置参数”对话框中,选择Generate code only生成代码之前的参数。

运行executable.

ifispc status = system(mdl);elsedisp('The example only runs the executable on Windows system.');end
该示例仅在Windows系统上运行可执行文件。

可执行文件成功完成后(状态= 0), the software creates afuzzypid.matdata file that contains the simulation results.

您可以比较生成代码的输出响应,rtw_y,随着Simulink模拟的输出,万博1manbetxy,使用以下代码。

loadfuzzypid.matplot(tout,y,'b-',rt_tout,rt_y,“罗”) legend('万博1manbetxsimulink',“可执行”,'地点','Southeast')

这result from the generated code matches the Simulink simulation.

您还可以在此模型中仅为控制器子系统生成代码。为此,请在调用该子系统时指定子系统slbuildfunction.

slbuild([mdl'/fuzzy pid')))
### Starting build procedure for: Fuzzy0 ### Successful completion of build procedure for: Fuzzy0 Build Summary Top model targets built: Model Action Rebuild Reason ======================================================================================= Fuzzy0 Code generated and compiled Code generation information file does not exist. 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 11.657s

您可以根据应用程序需求部署生成的代码。例如,您可以配置可执行文件的属性并创建静态或动态库。有关更多信息,请参阅为实时系统构建过程工作流程(Simulink Coder)

Generate Code for Other Data Types

这Fuzzy Logic Controller block also supports single-precision and fixed-point data for simulation and code generation. In both cases, your resulting fuzzy system has decreased accuracy compared to an equivalent double-precision fuzzy system. Use:

  • 单精度数据以减少系统的内存足迹。

  • 定点数据如果您的目标平台仅支持定点算术。万博1manbetx

To use one of these data types, set the数据类型property of the block, and configure the other components in the model to use the same data type.

fuzzypid_singlemodel is configured for single-precision data. Open the model.

mdl2 ='fuzzypid_single';open_system(mdl2)

In this model, the数据类型parameter of the Fuzzy Logic Controller block is set tosingle。这Fuzzy Logic Controller block automatically converts input signals to the specified data type. Also, the模拟使用参数设置为Code Generation。这模拟使用option does not affect the code generation process. Instead, setting this option simulates your fuzzy system using the same code path used by the generated code.

Generate code for this model.

set_param(mdl2,'RTWVerbose','离开')Slbuild(MDL2)
###启动构建过程:fuzzypid_single ###成功完成构建过程的完成过程:fuzzypid_single build build build ummary摘要构建最高模型目标:模型动作重建原因======================================================================================================================= fuzzypid_single代码生成和编译的代码生成信息文件。1个型号中的1个型号(0个模型已经最新)构建持续时间:0H 0M 15.535S

Setting the数据类型模糊逻辑控制器块的参数可确保所有推理步骤使用指定的数据类型。但是,根据模型中其他块的配置,某些生成的代码仍然可以使用双精度数据。

See Also

相关话题