このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。
時間カウンターのメモリ使用量の最適化
この例は、コード ジェネレーターが時間カウンターに割り当てるメモリ量を最適化する方法を示しています。この例では、経過時間、つまり 2 つのイベントの時間間隔を保存するメモリを最適化します。
コード ジェネレーターは、時間カウンターを符号なし整数で表します。時間カウンターのワード サイズは、モデル コンフィギュレーション パラメーター [アプリケーションのライフスパン] の設定に基づいています。このパラメーターは、アプリケーションが実行される予測最大時間を指定します。このパラメーターを使用して時間カウンターのオーバーフローを防ぐことができます。既定のサイズは 64 ビットです。
時間カウンターが使用するビット数は、[アプリケーションのライフスパン (日)] パラメーターの設定に応じて変わります。たとえば、時間カウンターがオーバーフローを回避するために 1 kHz のレートでインクリメントする場合、カウンターのビット数は次のようになります。
ライフスパン < 0.25 秒: 8 ビット
ライフスパン < 1 分: 16 ビット
ライフスパン < 49 日: 32 ビット
ライフスパン > 50 日: 64 ビット
64 ビット時間カウンターは、5 億 9,000 万年オーバーフローしません。
モデル例を開く
モデル例 TimerMemoryOptimization
を開きます。
open_system('TimerMemoryOptimization');
モデルは、SS1
、SS2
、SS3
の 3 つのサブシステムで構成されます。[モデル コンフィギュレーション パラメーター] ダイアログ ボックスを開きます。[数学とデータ型] ペインで、[アプリケーションのライフスパン (日)] パラメーターは既定値の inf
に設定されています。
3 つのサブシステムには、出力値を計算するための入力として経過時間を必要とする Discrete-time Integrator が含まれます。サブシステムは、次のように変わります。
SS1 - 1 kHz のクロックで動作します。時間カウンターは必要ありません。トリガー端子の [サンプル時間タイプ] パラメーターは、[
periodic
] に設定されます。経過時間は 0.001 としてインライン化されます。SS2 - 100 Hz のクロックで動作します。時間カウンターが必要です。1 日のライフスパンに基づいて、32 ビット カウンターに経過時間が保存されます。
SS3 - 0.5 Hz のクロックで動作します。時間カウンターが必要です。1 日のライフスパンに基づいて、16 ビット カウンターに経過時間が保存されます。
モデルのシミュレーション
モデルのシミュレーションを実行します。既定の設定では、モデルはサンプル時間を異なる色で表示します。3 つのサブシステムの離散サンプル時間は、赤、緑、青で表示されます。トリガーされたサブシステムは青緑で表示されます。
コードとレポートの生成
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_ */
4 つの 32 ビット符号なし整数の clockTick1
、clockTickH1
、clockTick2
および clockTickH2
が、サブシステム SS2
と SS3
の経過時間を格納するカウンターになります。
最適化の有効化とコードの再生成
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
のタスクの経過時間を保存します。