优化时间计数器的内存使用量
此示例说明如何优化代码生成器为时间计数器分配的内存量。该示例优化存储历时的内存,历时是模块的两次连续执行之间的时间间隔。
代码生成器将绝对时间和历时的计时器表示为无符号整数。在生成的代码中,字长是经过优化的。代码生成器将时间计数器存储为字长为 8、16、32 或 64 位的无符号整数。在生成的代码中,字长自动设置为能够容纳算法使用的最大时钟计时单元数的最小字长。代码生成器使用模型配置参数Application lifespan (days) 和Clock resolution (seconds, -1 for inherited) 的设置来计算最大时钟计时单元数。应用程序生命周期是包含模块或图的应用程序(取决于历时或绝对时间)在计时器溢出发生之前应能够执行的天数。时钟分辨率是时钟值的最小增量(以秒为单位)。有关代码生成器如何使用这些参数来计算算法使用的最大时钟计时单元数的详细信息,请参阅Timer Data Types and Memory Allocation。
打开示例模型
打开示例模型 TimerMemoryOptimization。
open_system('TimerMemoryOptimization');
该模型由子系统 SS1、SS2 和 SS3 组成,并针对代码生成器进行配置,以使用 GRT 系统 taraget 文件和 inf 天的生命周期。
这三个子系统包含一个离散时间积分器,它需要经过的时间作为输入来计算输出值。子系统的变化如下:
SS1 - 时钟频率为 1 kHz。不需要时间计数器。触发端口的采样时间类型参数设置为
periodic。已用时间直接写入代码,数值为0.001。SS2 - 时钟频率为 100 Hz。需要时间计数器。基于 1 天的生命周期,32 位计数器存储经过的时间。
SS3 - 时钟频率为 0.5 Hz。需要时间计数器。基于 1 天的生命周期,16 位计数器存储经过的时间。
对模型进行仿真
对模型进行仿真。默认情况下,模型配置为以不同颜色显示采样时间。三个子系统的离散采样时间显示为红色、绿色和蓝色。触发子系统用浅蓝色框突出显示。
生成代码和报告
生成代码和代码生成报告。
slbuild('TimerMemoryOptimization');### Searching for referenced models in model 'TimerMemoryOptimization'. ### Total of 1 models to build. ### Starting build procedure for: TimerMemoryOptimization ### Successful completion of build procedure for: TimerMemoryOptimization Build Summary Top model targets: Model Build Reason Status Build Duration ========================================================================================================================== TimerMemoryOptimization Information cache folder or artifacts were missing. Code generated and compiled. 0h 0m 6.5754s 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 6.9819s
查看生成的代码
打开生成的源文件 TimerMemoryOptimization.h。
cfile = fullfile('TimerMemoryOptimization_grt_rtw', 'TimerMemoryOptimization.h'); coder.example.extractLines(cfile,'/* Real-time Model Data Structure */',... '/* Block states (auto storage) */', 0, 1);
struct tag_RTM_TimerMemoryOptimizati_T {
const char_T *errorStatus;
/*
* Timing:
* The following substructure contains information regarding
* the timing information for the model.
*/
struct {
uint32_T clockTick1;
uint32_T clockTickH1;
uint32_T clockTick2;
uint32_T clockTickH2;
struct {
uint16_T TID[3];
uint16_T cLimit[3];
} TaskCounters;
} Timing;
};
/* Block states (default storage) */
extern DW_TimerMemoryOptimization_T TimerMemoryOptimization_DW;
/* Zero-crossing (trigger) state */
extern PrevZCX_TimerMemoryOptimizati_T TimerMemoryOptimization_PrevZCX;
/* External inputs (root inport signals with default storage) */
extern ExtU_TimerMemoryOptimization_T TimerMemoryOptimization_U;
/* External outputs (root outports fed by signals with default storage) */
extern ExtY_TimerMemoryOptimization_T TimerMemoryOptimization_Y;
/* Model entry point functions */
extern void TimerMemoryOptimization_initialize(void);
extern void TimerMemoryOptimization_step0(void);/* Sample time: [0.001s, 0.0s] */
extern void TimerMemoryOptimization_step1(void);/* Sample time: [0.01s, 0.0s] */
extern void TimerMemoryOptimization_step2(void);/* Sample time: [2.0s, 0.0s] */
extern void TimerMemoryOptimization_terminate(void);
/* Real-time Model object */
extern RT_MODEL_TimerMemoryOptimizat_T *const TimerMemoryOptimization_M;
/*-
* The generated code includes comments that allow you to trace directly
* back to the appropriate location in the model. The basic format
* is <system>/block_name, where system is the system number (uniquely
* assigned by Simulink) and block_name is the name of the block.
*
* Use the MATLAB hilite_system command to trace the generated code back
* to the model. For example,
*
* hilite_system('<S3>') - opens system 3
* hilite_system('<S3>/Kp') - opens and selects block Kp which resides in S3
*
* Here is the system hierarchy for this model
*
* '<Root>' : 'TimerMemoryOptimization'
* '<S1>' : 'TimerMemoryOptimization/SS1'
* '<S2>' : 'TimerMemoryOptimization/SS2'
* '<S3>' : 'TimerMemoryOptimization/SS3'
*/
#endif /* TimerMemoryOptimization_h_ */
四个 32 位无符号整数 clockTick1、clockTickH1、clockTick2 和 clockTickH2 是用于存储子系统 SS2 和 SS3 的经过的时间的计数器。
启用优化和重新生成代码
1.将模型配置参数应用程序生命周期(天) (LifeSpan) 设置为 1 天。
set_param('TimerMemoryOptimization', 'LifeSpan', '1');
2. 保存模型并重新生成代码。
slbuild('TimerMemoryOptimization');### Searching for referenced models in model 'TimerMemoryOptimization'. ### Total of 1 models to build. ### Starting build procedure for: TimerMemoryOptimization ### Successful completion of build procedure for: TimerMemoryOptimization Build Summary Top model targets: Model Build Reason Status Build Duration ==================================================================================================== TimerMemoryOptimization Incremental checksum changed. Code generated and compiled. 0h 0m 4.9593s 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 5.3472s
查看重新生成的代码
cfile = fullfile('TimerMemoryOptimization_grt_rtw', 'TimerMemoryOptimization.h'); coder.example.extractLines(cfile,'/* Real-time Model Data Structure */',... '/* Block states (auto storage) */', 0, 1);
struct tag_RTM_TimerMemoryOptimizati_T {
const char_T *errorStatus;
/*
* Timing:
* The following substructure contains information regarding
* the timing information for the model.
*/
struct {
uint32_T clockTick1;
uint16_T clockTick2;
struct {
uint16_T TID[3];
uint16_T cLimit[3];
} TaskCounters;
} Timing;
};
/* Block states (default storage) */
extern DW_TimerMemoryOptimization_T TimerMemoryOptimization_DW;
/* Zero-crossing (trigger) state */
extern PrevZCX_TimerMemoryOptimizati_T TimerMemoryOptimization_PrevZCX;
/* External inputs (root inport signals with default storage) */
extern ExtU_TimerMemoryOptimization_T TimerMemoryOptimization_U;
/* External outputs (root outports fed by signals with default storage) */
extern ExtY_TimerMemoryOptimization_T TimerMemoryOptimization_Y;
/* Model entry point functions */
extern void TimerMemoryOptimization_initialize(void);
extern void TimerMemoryOptimization_step0(void);/* Sample time: [0.001s, 0.0s] */
extern void TimerMemoryOptimization_step1(void);/* Sample time: [0.01s, 0.0s] */
extern void TimerMemoryOptimization_step2(void);/* Sample time: [2.0s, 0.0s] */
extern void TimerMemoryOptimization_terminate(void);
/* Real-time Model object */
extern RT_MODEL_TimerMemoryOptimizat_T *const TimerMemoryOptimization_M;
/*-
* The generated code includes comments that allow you to trace directly
* back to the appropriate location in the model. The basic format
* is <system>/block_name, where system is the system number (uniquely
* assigned by Simulink) and block_name is the name of the block.
*
* Use the MATLAB hilite_system command to trace the generated code back
* to the model. For example,
*
* hilite_system('<S3>') - opens system 3
* hilite_system('<S3>/Kp') - opens and selects block Kp which resides in S3
*
* Here is the system hierarchy for this model
*
* '<Root>' : 'TimerMemoryOptimization'
* '<S1>' : 'TimerMemoryOptimization/SS1'
* '<S2>' : 'TimerMemoryOptimization/SS2'
* '<S3>' : 'TimerMemoryOptimization/SS3'
*/
#endif /* TimerMemoryOptimization_h_ */
应用程序生命周期(天) 参数的新设置指示代码生成器为时间计数器留出更少的内存。重新生成的代码包括:
32 位无符号整数
clockTick1,用于计算和存储SS2的任务的经过的时间16 位无符号整数
clockTick2,用于计算和存储SS3的任务的经过的时间