主要内容

使用布尔数据优化逻辑信号的生成代码

R2026b

通过将逻辑信号存储为布尔数据来优化生成代码。当您选择模型配置参数将逻辑信号实现为布尔数据(而不是双精度数据) 时,生成逻辑信号的模块会输出布尔信号。

优化可以:

  • 减少 ROM 和 RAM 消耗量。

  • 提高执行速度。

示例模型

以模型 LogicalAsBoolean 为例。Relational Operator、Logical Operator 和 HitCrossing 模块的输出是 double,即使它们表示逻辑数据也是如此。

model = 'LogicalAsBoolean';
open_system(model);

生成代码

编译模型。

slbuild(model)
### Searching for referenced models in model 'LogicalAsBoolean'.
### Total of 1 models to build.
### Starting top model code generation target build for: LogicalAsBoolean
### Successful completion of build procedure for: LogicalAsBoolean

Build Summary

Top model targets:

Model             Build Reason                                Status                        Build Duration
==========================================================================================================
LogicalAsBoolean  Target (LogicalAsBoolean.c) did not exist.  Code generated and compiled.  0h 0m 12.973s

1 of 1 models built (0 models already up to date)
Build duration: 0h 0m 15.208s

查看生成的代码而不进行优化。这些代码行位于 LogicalAsBoolean.h 中。

hfile = fullfile('LogicalAsBoolean_grt_rtw',...
    'LogicalAsBoolean.h');
coder.example.extractLines(hfile,'/* External outputs','/* Parameters (default storage) */',1,0);
/* External outputs (root outports fed by signals with default storage) */
typedef struct {
  real_T Out1;                         /* '<Root>/Out1' */
  real_T Out2;                         /* '<Root>/Out2' */
  real_T Out3;                         /* '<Root>/Out3' */
} ExtY_LogicalAsBoolean_T;

启用优化

  1. 打开“配置参数”对话框。

  2. 选择将逻辑信号实现为布尔数据(而不是双精度数据) 参数。

您也可以使用命令行 API 启用优化:

set_param(model,'BooleanDataType','on');

生成代码并进行优化

生成代码将逻辑信号输出存储为布尔数据。

编译模型。

slbuild(model)
### Searching for referenced models in model 'LogicalAsBoolean'.
### Total of 1 models to build.
### Starting top model code generation target build for: LogicalAsBoolean
### Successful completion of build procedure for: LogicalAsBoolean

Build Summary

Top model targets:

Model             Build Reason                                            Status                        Build Duration
======================================================================================================================
LogicalAsBoolean  Model or library LogicalAsBoolean has unsaved changes.  Code generated and compiled.  0h 0m 4.9087s

1 of 1 models built (0 models already up to date)
Build duration: 0h 0m 5.6493s

查看优化后的生成代码。这些代码行位于 LogicalAsBoolean.h 中。

coder.example.extractLines(hfile,'/* External outputs','/* Parameters (default storage) */',1,0);
/* External outputs (root outports fed by signals with default storage) */
typedef struct {
  boolean_T Out1;                      /* '<Root>/Out1' */
  boolean_T Out2;                      /* '<Root>/Out2' */
  boolean_T Out3;                      /* '<Root>/Out3' */
} ExtY_LogicalAsBoolean_T;

关闭模型和代码生成报告。

bdclose(model)

另请参阅

主题