在工程中生成代码并进行模型仿真
R2026b此示例说明如何使用代码生成模板创建新的工程。Simulink 工程的代码生成工程模板包含多个模型。工程模板还提供可帮助您生成控制器代码并运行框架模型仿真的实用工具(.m 脚本)。
创建工程
从代码生成工程模板中创建新的工程。
要从 Simulink® 起始页创建此工程,请在命令行窗口中键入:
simulink
从起始页选择“代码生成”模板,然后创建 exampleCodeGen 工程。
生成代码
为 feedback_control.slx 模型生成控制器代码。
要生成控制器代码,请选择工程快捷方式选项卡,然后选择生成控制器代码快捷方式。
此快捷方式将运行工程文件夹 utilities 中的 generate_controller_code.m 脚本。此脚本将在工程文件夹 controller 中编译 feedback_control.slx 模型。
### Searching for referenced models in model 'feedback_control'. ### Unable to find Simulink cache file "feedback_control.slxc". ### Total of 1 models to build. ### Starting top model code generation target build for: feedback_control ### Build reason: "feedback_control.c" did not exist. ### Build folder for generated code: /tmp/Bdoc26b_3351752_2634458/tp19ba529d/simulinkcoder-ex70644236/projects/templateCodeGen/work/codegen/feedback_control_ert_rtw ### Invoking Target Language Compiler on feedback_control.rtw ### Using System Target File: /mathworks/devel/bat/filer/batfs2566-0/Bdoc26b.3351752/build/runnable/matlab/rtw/c/ert/ert.tlc ### Loading TLC function libraries ....... ### Generating TLC interface API for custom data . ### Initial pass through model to cache user defined code ### Caching model source code ................................. ### Writing header file feedback_control_types.h ### Writing header file feedback_control.h ### Writing header file rtwtypes.h ### Writing source file feedback_control.c . ### Writing header file feedback_control_private.h ### Writing source file feedback_control_data.c ### Writing source file ert_main.c ### TLC code generation complete (took 1.926s). .### Creating HTML report file index.html ### Saving binary information cache. ### Using toolchain: GNU gcc/g++ | gmake (64-bit Linux) ### Creating '/tmp/Bdoc26b_3351752_2634458/tp19ba529d/simulinkcoder-ex70644236/projects/templateCodeGen/work/codegen/feedback_control_ert_rtw/feedback_control.mk' ... ### Building 'feedback_control': "/mathworks/devel/bat/filer/batfs2566-0/Bdoc26b.3351752/build/runnable/matlab/bin/glnxa64/gmake" -j 4 -l 4 -f feedback_control.mk all gcc -c -fwrapv -fPIC -Wno-error=incompatible-pointer-types -Wno-error=stringop-overflow -O0 -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DTERMFCN=1 -DONESTEPFCN=1 -DMAT_FILE=0 -DMULTI_INSTANCE_CODE=0 -DINTEGER_CODE=0 -DMT=0 -DTID01EQ=0 -DMODEL=feedback_control -DNUMST=1 -DNCSTATES=0 -DHAVESTDIO -DMODEL_HAS_DYNAMICALLY_LOADED_SFCNS=0 -I/tmp/Bdoc26b_3351752_2634458/tp19ba529d/simulinkcoder-ex70644236/projects/templateCodeGen/work/codegen -I/tmp/Bdoc26b_3351752_2634458/tp19ba529d/simulinkcoder-ex70644236/projects/templateCodeGen/work/codegen/feedback_control_ert_rtw -I/mathworks/devel/bat/filer/batfs2566-0/Bdoc26b.3351752/build/runnable/matlab/extern/include -I/mathworks/devel/bat/filer/batfs2566-0/Bdoc26b.3351752/build/runnable/matlab/simulink/include -I/mathworks/devel/bat/filer/batfs2566-0/Bdoc26b.3351752/build/runnable/matlab/rtw/c/src -I/mathworks/devel/bat/filer/batfs2566-0/Bdoc26b.3351752/build/runnable/matlab/rtw/c/src/ext_mode/common -I/mathworks/devel/bat/filer/batfs2566-0/Bdoc26b.3351752/build/runnable/matlab/rtw/c/ert -o "feedback_control.o" "/tmp/Bdoc26b_3351752_2634458/tp19ba529d/simulinkcoder-ex70644236/projects/templateCodeGen/work/codegen/feedback_control_ert_rtw/feedback_control.c" gcc -c -fwrapv -fPIC -Wno-error=incompatible-pointer-types -Wno-error=stringop-overflow -O0 -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DTERMFCN=1 -DONESTEPFCN=1 -DMAT_FILE=0 -DMULTI_INSTANCE_CODE=0 -DINTEGER_CODE=0 -DMT=0 -DTID01EQ=0 -DMODEL=feedback_control -DNUMST=1 -DNCSTATES=0 -DHAVESTDIO -DMODEL_HAS_DYNAMICALLY_LOADED_SFCNS=0 -I/tmp/Bdoc26b_3351752_2634458/tp19ba529d/simulinkcoder-ex70644236/projects/templateCodeGen/work/codegen -I/tmp/Bdoc26b_3351752_2634458/tp19ba529d/simulinkcoder-ex70644236/projects/templateCodeGen/work/codegen/feedback_control_ert_rtw -I/mathworks/devel/bat/filer/batfs2566-0/Bdoc26b.3351752/build/runnable/matlab/extern/include -I/mathworks/devel/bat/filer/batfs2566-0/Bdoc26b.3351752/build/runnable/matlab/simulink/include -I/mathworks/devel/bat/filer/batfs2566-0/Bdoc26b.3351752/build/runnable/matlab/rtw/c/src -I/mathworks/devel/bat/filer/batfs2566-0/Bdoc26b.3351752/build/runnable/matlab/rtw/c/src/ext_mode/common -I/mathworks/devel/bat/filer/batfs2566-0/Bdoc26b.3351752/build/runnable/matlab/rtw/c/ert -o "feedback_control_data.o" "/tmp/Bdoc26b_3351752_2634458/tp19ba529d/simulinkcoder-ex70644236/projects/templateCodeGen/work/codegen/feedback_control_ert_rtw/feedback_control_data.c" gcc -c -fwrapv -fPIC -Wno-error=incompatible-pointer-types -Wno-error=stringop-overflow -O0 -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DTERMFCN=1 -DONESTEPFCN=1 -DMAT_FILE=0 -DMULTI_INSTANCE_CODE=0 -DINTEGER_CODE=0 -DMT=0 -DTID01EQ=0 -DMODEL=feedback_control -DNUMST=1 -DNCSTATES=0 -DHAVESTDIO -DMODEL_HAS_DYNAMICALLY_LOADED_SFCNS=0 -I/tmp/Bdoc26b_3351752_2634458/tp19ba529d/simulinkcoder-ex70644236/projects/templateCodeGen/work/codegen -I/tmp/Bdoc26b_3351752_2634458/tp19ba529d/simulinkcoder-ex70644236/projects/templateCodeGen/work/codegen/feedback_control_ert_rtw -I/mathworks/devel/bat/filer/batfs2566-0/Bdoc26b.3351752/build/runnable/matlab/extern/include -I/mathworks/devel/bat/filer/batfs2566-0/Bdoc26b.3351752/build/runnable/matlab/simulink/include -I/mathworks/devel/bat/filer/batfs2566-0/Bdoc26b.3351752/build/runnable/matlab/rtw/c/src -I/mathworks/devel/bat/filer/batfs2566-0/Bdoc26b.3351752/build/runnable/matlab/rtw/c/src/ext_mode/common -I/mathworks/devel/bat/filer/batfs2566-0/Bdoc26b.3351752/build/runnable/matlab/rtw/c/ert -o "ert_main.o" "/tmp/Bdoc26b_3351752_2634458/tp19ba529d/simulinkcoder-ex70644236/projects/templateCodeGen/work/codegen/feedback_control_ert_rtw/ert_main.c" ### Creating standalone executable "../feedback_control" ... g++ -o ../feedback_control feedback_control.o feedback_control_data.o ert_main.o -Wl,--start-group -Wl,--end-group ### Created: "../feedback_control" ### Successfully generated all binary outputs. gmake: Nothing to be done for `all'. ### Successful completion of build procedure for: feedback_control ### Simulink cache artifacts for 'feedback_control' were created in '/tmp/Bdoc26b_3351752_2634458/tp19ba529d/simulinkcoder-ex70644236/projects/templateCodeGen/work/cache/feedback_control.slxc'. Build Summary Top model targets: Model Build Reason Status Build Duration ========================================================================================================== feedback_control Target (feedback_control.c) did not exist. Code generated and compiled. 0h 0m 10.379s 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 10.936s

对模型进行仿真
仿真顶层框架 feedback_harness.slx 模型。
要打开框架模型进行仿真,请选择工程快捷方式选项卡,然后选择反馈框架快捷方式。此快捷方式将打开工程文件夹 harnesses 中的 feedback_harness.slx 模型。
要仿真该模型,请点击运行。


查看仿真输出
打开模型中的 Scope 模块,并查看仿真输出。
要打开示波器并查看仿真,请在 Simulink 窗口中双击 Scope 模块。

使用依赖关系分析器视图
要查看文件依赖关系,请使用工程的依赖关系分析器视图。
从依赖关系分析器视图中,您可以:
查看文件依赖项。
将鼠标悬停在依赖关系箭头上,检查依赖关系类型。
双击一个文件或模型将其打开。
