Main Content

本页翻译不是最新的。点击此处可查看最新英文版本。

优化时间计数器的内存使用量

此示例说明如何优化代码生成器为时间计数器分配的内存量。该示例优化存储经过时间(两个事件之间的时间间隔)的内存。

代码生成器将时间计数器表示为无符号整数。时间计数器的字长基于模型配置参数应用程序生命周期(天) 的设置,该参数指定应用程序运行的预期最长持续时间。您可以使用此参数来防止时间计数器溢出。默认大小为 64 位。

时间计数器使用的位数取决于应用程序生命周期(天) 参数的设置。例如,如果时间计数器以 1 kHz 的速率递增,为了避免溢出,计数器的位数如下:

  • 生命周期 < 0.25 秒:8 位

  • 生命周期 < 1 分钟:16 位

  • 生命周期 < 49 天:32 位

  • 生命周期 > 50 天:64 位

64 位时间计数器在寿命超过 5.9 亿年后才会溢出。

打开示例模型

打开示例模型 TimerMemoryOptimization

open_system('TimerMemoryOptimization');

该模型由三个子系统 SS1SS2SS3 组成。打开“模型配置参数”对话框。在数学和数据类型窗格中,应用程序生命周期(天) 参数的设置为默认值,即 inf

这三个子系统包含一个离散时间积分器,它需要经过的时间作为输入来计算其输出值。子系统的变化如下:

  • SS1 - 时钟频率为 1 kHz。不需要时间计数器。触发端口的采样时间类型参数设置为 periodic。历时为 0.001。

  • SS2 - 时钟频率为 100 Hz。需要时间计数器。基于 1 天的生命周期,32 位计数器存储经过的时间。

  • SS3 - 时钟频率为 0.5 Hz。需要时间计数器。基于 1 天的生命周期,16 位计数器存储经过的时间。

对模型进行仿真

对模型进行仿真。默认情况下,模型配置为以不同颜色显示采样时间。三个子系统的离散采样时间显示为红色、绿色和蓝色。触发子系统是蓝绿色。

生成代码和报告

1.将代码生成器的模型配置为使用 GRT 系统目标文件,生命周期为 inf 天。

2.编译模型。

slbuild('TimerMemoryOptimization');
### Starting build procedure for: TimerMemoryOptimization
### Successful completion of build procedure for: TimerMemoryOptimization

Build Summary

Top model targets built:

Model                    Action                        Rebuild Reason                                    
=========================================================================================================
TimerMemoryOptimization  Code generated and compiled.  Code generation information file does not exist.  

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

查看生成的代码

打开生成的源文件 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);
extern void TimerMemoryOptimization_step1(void);
extern void TimerMemoryOptimization_step2(void);
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                               /* RTW_HEADER_TimerMemoryOptimization_h_ */

四个 32 位无符号整数 clockTick1clockTickH1clockTick2clockTickH2 是用于存储子系统 SS2SS3 的经过的时间的计数器。

启用优化和重新生成代码

1.重新配置模型以将生命周期设置为 1 天。

set_param('TimerMemoryOptimization', 'LifeSpan', '1');

2.编译模型。

slbuild('TimerMemoryOptimization');
### Starting build procedure for: TimerMemoryOptimization
### Successful completion of build procedure for: TimerMemoryOptimization

Build Summary

Top model targets built:

Model                    Action                        Rebuild Reason                 
======================================================================================
TimerMemoryOptimization  Code generated and compiled.  Incremental checksum changed.  

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

查看重新生成的代码。

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);
extern void TimerMemoryOptimization_step1(void);
extern void TimerMemoryOptimization_step2(void);
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                               /* RTW_HEADER_TimerMemoryOptimization_h_ */

应用程序生命周期(天) 参数的新设置指示代码生成器为时间计数器留出更少的内存。重新生成的代码包括:

  • 32 位无符号整数 clockTick1,用于存储 SS2 的任务的经过的时间

  • 16 位无符号整数 clockTick2,用于存储 SS3 的任务的经过的时间

相关信息

相关主题