Main Content

このページの翻訳は最新ではありません。ここをクリックして、英語の最新版を参照してください。

時間カウンターのメモリ使用量の最適化

この例は、コード ジェネレーターが時間カウンターに割り当てるメモリ量を最適化する方法を示しています。この例では、経過時間、つまり 2 つのイベントの時間間隔を保存するメモリを最適化します。

コード ジェネレーターは、時間カウンターを符号なし整数で表します。時間カウンターのワード サイズは、モデル コンフィギュレーション パラメーター[アプリケーションのライフスパン]の設定に基づいています。このパラメーターは、アプリケーションが実行される予測最大時間を指定します。このパラメーターを使用して時間カウンターのオーバーフローを防ぐことができます。既定のサイズは 64 ビットです。

時間カウンターが使用するビット数は、[アプリケーションのライフスパン (日)]パラメーターの設定に応じて変わります。たとえば、時間カウンターがオーバーフローを回避するために 1 kHz のレートでインクリメントする場合、カウンターのビット数は次のようになります。

  • ライフスパン < 0.25 秒: 8 ビット

  • ライフスパン < 1 分: 16 ビット

  • ライフスパン < 49 日: 32 ビット

  • ライフスパン > 50 日: 64 ビット

64 ビット時間カウンターは、5 億 9,000 万年オーバーフローしません。

モデル例を開く

モデル例rtwdemo_abstimeを開きます。

モデルは、SS1SS2SS3の 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. ビルドと検査プロセス用に一時フォルダーを作成します。

2. GRT システム ターゲット ファイルとinf日間のライフスパンを使用するようにコード ジェネレーターのモデルを設定します。

3. モデルを作成します。

### Starting build procedure for: rtwdemo_abstime ### Successful completion of build procedure for: rtwdemo_abstime Build Summary Top model targets built: Model Action Rebuild Reason ================================================================================================ rtwdemo_abstime 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 16.349s

生成コードのレビュー

生成されたソース ファイルrtwdemo_abstime.cを開きます。

struct tag_RTM_rtwdemo_abstime_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_rtwdemo_abstime_T rtwdemo_abstime_DW; /* Zero-crossing (trigger) state */ extern PrevZCX_rtwdemo_abstime_T rtwdemo_abstime_PrevZCX; /* External inputs (root inport signals with default storage) */ extern ExtU_rtwdemo_abstime_T rtwdemo_abstime_U; /* External outputs (root outports fed by signals with default storage) */ extern ExtY_rtwdemo_abstime_T rtwdemo_abstime_Y; /* Model entry point functions */ extern void rtwdemo_abstime_initialize(void); extern void rtwdemo_abstime_step0(void); extern void rtwdemo_abstime_step1(void); extern void rtwdemo_abstime_step2(void); extern void rtwdemo_abstime_step(int_T tid); extern void rtwdemo_abstime_terminate(void); /* Real-time Model object */ extern RT_MODEL_rtwdemo_abstime_T *const rtwdemo_abstime_M; /*- * The generated code includes comments that allow you to trace directly * back to the appropriate location in the model. The basic format * is /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('') - opens system 3 * hilite_system('/Kp') - opens and selects block Kp which resides in S3 * * Here is the system hierarchy for this model * * '' : 'rtwdemo_abstime' * '' : 'rtwdemo_abstime/SS1' * '' : 'rtwdemo_abstime/SS2' * '' : 'rtwdemo_abstime/SS3' */ #endif /* RTW_HEADER_rtwdemo_abstime_h_ */

4 つの 32 ビット符号なし整数のclockTick1clockTickH1clockTick2およびclockTickH2が,サブシステムSS2SS3の経過時間を格納するカウンターになります。

最適化の有効化とコードの再生成

1. モデルを再設定してライフスパンを 1 日に設定します。

2. モデルを作成します。

### Starting build procedure for: rtwdemo_abstime ### Successful completion of build procedure for: rtwdemo_abstime Build Summary Top model targets built: Model Action Rebuild Reason ================================================================================= rtwdemo_abstime Code generated and compiled Incremental checksum has changed. 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 10.445s

再生成コードのレビュー

struct tag_RTM_rtwdemo_abstime_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_rtwdemo_abstime_T rtwdemo_abstime_DW; /* Zero-crossing (trigger) state */ extern PrevZCX_rtwdemo_abstime_T rtwdemo_abstime_PrevZCX; /* External inputs (root inport signals with default storage) */ extern ExtU_rtwdemo_abstime_T rtwdemo_abstime_U; /* External outputs (root outports fed by signals with default storage) */ extern ExtY_rtwdemo_abstime_T rtwdemo_abstime_Y; /* Model entry point functions */ extern void rtwdemo_abstime_initialize(void); extern void rtwdemo_abstime_step0(void); extern void rtwdemo_abstime_step1(void); extern void rtwdemo_abstime_step2(void); extern void rtwdemo_abstime_step(int_T tid); extern void rtwdemo_abstime_terminate(void); /* Real-time Model object */ extern RT_MODEL_rtwdemo_abstime_T *const rtwdemo_abstime_M; /*- * The generated code includes comments that allow you to trace directly * back to the appropriate location in the model. The basic format * is /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('') - opens system 3 * hilite_system('/Kp') - opens and selects block Kp which resides in S3 * * Here is the system hierarchy for this model * * '' : 'rtwdemo_abstime' * '' : 'rtwdemo_abstime/SS1' * '' : 'rtwdemo_abstime/SS2' * '' : 'rtwdemo_abstime/SS3' */ #endif /* RTW_HEADER_rtwdemo_abstime_h_ */

[アプリケーションのライフスパン (日)]パラメーターの新しい設定では、コード ジェネレーターに、時間カウンター用に確保するメモリを少なくするよう指示します。生成コードには次が含まれます。

  • 32 ビット符号なし整数clockTick1SS2のタスクの経過時間を保存します。

  • 16 ビット符号なし整数clockTick2SS3のタスクの経過時間を保存します。

関連情報

関連するトピック