Matlab 代码生成问题,参数替换为常量

Matlab Codegeneration Issue, parameters replaced by constants

提问人:Weltgeist 提问时间:10/25/2023 更新时间:10/26/2023 访问量:28

问:

我们一直在尝试将 matlab2013a 中的一些自动生成的代码更新到 matlab2022b 版本,因为我们更改了工具链和依赖项。

为此,我们从旧的 matlab 2013a 项目中打开了配置参数窗口代码生成窗格,记下了检查的所有内容,并尝试将其添加到 matlab2022b 添加的默认 ert 代码生成项目中。

function [] = setCodeGenSettings2022(cs)
%%%% Mainly to set all configs to match 2013 codegen project setting,
%%%% starting from normal template.
% Data Type Replacement
set_param(cs,'EnableUserReplacementTypes','on'); 
struc = get_param(cs,'ReplacementTypes');
struc.double = '***_real_T';
struc.single = '***_real32_T';
struc.int32 = '***_int32_T';
struc.int16 = '***_int16_T';
struc.int8 = '***_int8_T';
struc.uint32 = '***_uint32_T';
struc.uint16 = '***_uint16_T';
struc.uint8 = '***_uint8_T';
struc.boolean = '***_bolean_T';
set_param(cs,'ReplacementTypes',struc);
% Code Placement 
set_param(cs,'ERTFilePackagingFormat','Modular'); 
% Templates
%%No Tomcat_code_template.cgt available, we use the default
%%ert_code_template.cgt.2013-2022 Diff
% Verification
%%Exactly the same
% Code Style
%%2013-2022 Diff
set_param(cs,'SuppressUnreachableDefaultCases','off')
set_param(cs,'ConvertIfToSwitch','off')
%%2022 New
%%set_param(cs,'PreserveStaticInFcnDecls','true') %% Doesn't exist in 2013,
%%but seems useful.
% Interface
%%2013-2022 Diff
set_param(cs,'SupportNonFinite','off')
set_param(cs,'SupportComplex','off')
set_param(cs, 'CodeInterfacePackaging', 'Reusable function') 
set_param(cs, 'RootIOFormat', 'Structure reference')
% Custom Code
%%Exactly the same
% Identifier
%%Exactly the same
% Comments
%%2013-2022 Diff
set_param(cs, 'StateflowObjectComments', 'on') ;
set_param(cs, 'ShowEliminatedStatement', 'off') ; 
set_param(cs, 'ForceParamTrailComments', 'off') ; 
set_param(cs, 'OperatorAnnotations', 'off') ; 
%%2022 New
%%Kept Simulink block Description,Simulink Data Object and State flow
%%Object Description. Seems Relevant.
% set_param(cs, 'InsertBlockDesc', 'off') ;
% set_param(cs, 'SimulinkDataObjDesc', 'off') ;
% set_param(cs, 'SFDataObjDesc', 'off') ;
% Report
%%Exactly the same
% Optimization
%%2013-2022 Diff
set_param(cs,'DefaultParameterBehavior','Inline');
set_param(cs,'OptimizationCustomize','on');
set_param(cs,'DifferentSizesBufferReuse','off');
set_param(cs,'GlobalVariableUsage','None');
set_param(cs,'OptimizeBlockOrder','off');
% Code Generation - Main
%%2013-2022 Diff
set_param(cs,'PackageGeneratedCodeAndArtifacts','on');
set_param(cs,'BuildConfiguration','Faster Runs')
set_param(cs,'ObjectivePriorities', 'MISRA C:2012 guidelines')
end

结果如下所示。我们遇到的唯一问题是一些以前在 C 代码中保存为变量参数的 simulink 命名常量模块(参数)现在保存为硬编码常量参数。我们想知道,为什么会这样?配置参数中未选中哪个选项?还是别的什么?请指教?

enter image description here

左侧的当前代码。右边是 2013 年代码。

enter image description here

请注意,我也向 Mathworks 社区提出了同样的问题,相当于 stackoverflow。

C MATLAB 代码生成 Simulink

评论


答:

0赞 Weltgeist 10/26/2023 #1

从Mathworks得到了答案! 非常感谢Jason Chen。

若要将参数存储在生成代码的变量中,请将默认参数行为从“内联”更改为“可调”。从您的 MATLAB Answers 帖子中,我看到了这一行

set_param(cs,'DefaultParameterBehavior','Inline');

此选项将导致 Embedded Coder 生成块参数作为常量,以减少 RAM 使用量。您可以将此行更改为

set_param(cs,'DefaultParameterBehavior','Tunable');

以再次将参数存储为变量。有关更多信息,请参阅此文档页面:在生成的代码中表示数值模块参数 - MATLAB (mathworks.com)。