Simulink代码生成常用配置项及脚本修改
- Simulink代码生成配置
- Solver 解算器
- Code Generation 代码生成
- Hardware Impelementation 硬件匹配
- Code Feneration->Optimization 优化
- Code Feneration->Report 报告
- Code Feneration->Comments 注释
- Code Feneration->Identifiers 标识符
- Code Feneration->Custom Code 代码定制
- Code Feneration->Interface 接口
- Code Feneration->Code Style 代码风格
- Code Feneration->Templates 模板
- 使用脚本进行Simulink配置修改
- 源码:
本文讲述使用Simulink进行APP软件编程时常用的代码生成配置项,及使用matlab脚本对已打开模型进行Simulink配置修改。
Simulink代码生成配置
Solver 解算器
Solver ->Simulation time 仿真时间
Solver ->Simulation time ->Start time:仿真开始时间
Solver ->Simulation time ->Stop time:仿真结束时间
Solver ->Solver selection
Solver ->Solver selection->Type 选择要用于模拟模型的求解器类型
Variable-step:变步长,当模型状态快速变化时,减少步长,以保持准确性。当模型状态变化缓慢时增加步长,以避免不必要的步长。
Fixed-step:固定步长,固定步在整个模拟过程中步长保持不变。(单片机晶振为固定步长,所以选择Fixed-step)
Solver ->Solver selection->Solver 选择求解器
discrete(no continuous states):离散(无连续状态),通过在当前时间上添加固定步长来计算下一个时间步的时间。依赖于模型的块来更新离散状态。
Solver ->Solver details
Solver ->Solver details ->Fixed-step size (fundamental sample time): 指定所选固定步长求解器使用的步长。(根据单片机调度时间进行设置。例如生成的代码10ms执行一次,设置为0.01(秒),即10ms执行一次)
Code Generation 代码生成
Code Generation->Target selection->System target file 选择系统目标文件
选择与嵌入式相关的系统目标文件,ert.tlc。(Embedded Coder)
Code Generation->Build process->Generate code only
只生成代码不进行编译
Code Generation->Build process->Package code and artifacts
生成为压缩包
Code Generation-> Toolchain settings-> Toolchain
指定一组第三方软件工具,用来编译生成的代码。Toolchain 的默认值为 “Automatically locate an installed toolchain”。
Hardware Impelementation 硬件匹配
Hardware Impelementation->Device vendor:Freescale
Hardware Impelementation->Device type:32-bit PowerPC
选择Freescale 和 32-bit PowerPC主要是由于目前项目所使用的单片机为32位单片机,具体配置主要根据下方Device details内的参数选择,APP部分只要求数据类型匹配即可。
Code Feneration->Optimization 优化
Optimization->Data initialization
两个选项为是否移除初始化为0的I/O初始化和数据初始化,勾选为不生成。
Code Feneration->Report 报告
Report->Create code generation report
创建代码生成报告。
Report->Open report automatically
在Create code generation report选中后,该项才能选中,选中效果为代码生成后自动打开报告。
Report->Genetate model Web view
生成网页版报告。
Report->Metrics-> Generate static code metrics
生成静态代码参数指标。
Code Feneration->Comments 注释
选择是否生成注释及注释的内容。
Code Feneration->Identifiers 标识符
Identifiers ->Identifier format control
选择生成的变量、函数、形参等标识符的格式。
Identifiers ->Maximum identifier length
标识符的最大长度。
Code Feneration->Custom Code 代码定制
生成代码中的定制部分,例如引用接口头文件。
Code Feneration->Interface 接口
Interface->Code interface->Remove error status field in real-time model data structure
移除错误标志函数,该函数目前没有发现有明确的作用。
Code Feneration->Code Style 代码风格
Code indentation->Indent style
K&R为大括号不换行,Allman为大括号换行。
Code indentation->Indent size
代码缩进长度,单位为空格。
Code Feneration->Templates 模板
Custom templates->Generate an example main program
生成一个示例main函数
使用脚本进行Simulink配置修改
分享一个我一直使用的脚本,用来配置当前打开模型的配置信息。
注意:
1.该配置会在生成代码中“#include "signal_api.h”。
2.脚本仅修改了新建模型后非默认值的部分,如果已经修改过配置,无法还原为默认配置。
源码:
%% 设置Simulink配置
% 2023.3.20
% Author: LL
function cfg_Simulink(~)
cs = getActiveConfigSet(bdroot);
% MATLAB version: 9.9.0.1444674 (R2020b)
% Original configuration set version: 20.1.0
disp(cs.versionCompare('20.1.0'));
if cs.versionCompare('20.1.0') < 0
error('Simulink:MFileVersionViolation', 'The version of the target configuration set is older than the original configuration set.');
end
% Original environment character encoding: GBK
if ~strcmpi(get_param(0, 'CharacterEncoding'), 'GBK')
warning('Simulink:EncodingUnMatched', 'The target character encoding (%s) is different from the original (%s).', get_param(0, 'CharacterEncoding'), 'GBK');
end
% Do not change the order of the following commands. There are dependencies between the parameters.
cs.set_param('Name', 'Configuration'); % Name
cs.set_param('Description', ''); % Description
% Original configuration set target is ert.tlc
cs.switchTarget('ert.tlc','');
% Solver
cs.set_param('SolverType', 'Fixed-step'); % Type
cs.set_param('Solver', 'FixedStepDiscrete'); % Solver
cs.set_param('FixedStep', '0.01'); % Fixed-step size (fundamental sample time)
% Hardware Implementation
cs.set_param('HardwareBoard', 'None'); % Hardware board
cs.set_param('ProdHWDeviceType', 'Freescale->32-bit PowerPC'); % Production device vendor and type
% Code Generation
cs.set_param('GenCodeOnly', 'on'); % Generate code only
cs.set_param('CustomSourceCode', '#include "signal_api.h"'); % Source file
cs.set_param('GenerateReport', 'on'); % Create code generation report
cs.set_param('LaunchReport', 'on'); % Open report automatically
cs.set_param('GenerateCodeMetricsReport', 'on'); % Static code metrics
cs.set_param('SuppressErrorStatus', 'on'); % Remove error status field in real-time model data structure
cs.set_param('IndentStyle', 'Allman'); % Indent style
cs.set_param('IndentSize', '4'); % Indent size
cs.set_param('GenerateSampleERTMain', 'off'); % Generate an example main program
end