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
  • 2
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Simulink代码生成配置方法有多种。根据引用和引用,Simulink模型生成代码的配置Simulink仿真和实时仿真机(SpeedGoat\dsPACE)的配置方式不同。 一种配置方法是通过模型进行配置。具体配置步骤如下: 1. 选择合适的步长,以确保模型的仿真结果精确且稳定。步长选择可以根据模型的需求和要求来进行调整。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Simulink代码生成(二)——代码生成时模型的配置方法及操作流程](https://blog.csdn.net/Rlover_star/article/details/127261444)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [Simulink模型代码生成配置转C代码](https://download.csdn.net/download/ee21xsxj/10272093)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Simulink模型生成代码配置配置教程)](https://blog.csdn.net/RNG_uzi_/article/details/129158902)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SissonLi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值