このページの翻訳は最新ではありません。ここをクリックして、英語の最新版を参照してください。
この例は、コード ジェネレーターが時間カウンターに割り当てるメモリ量を最適化する方法を示しています。この例では、経過時間、つまり 2 つのイベントの時間間隔を保存するメモリを最適化します。
コード ジェネレーターは、時間カウンターを符号なし整数で表します。時間カウンターのワード サイズは、モデル コンフィギュレーション パラメーター[アプリケーションのライフスパン]の設定に基づいています。このパラメーターは、アプリケーションが実行される予測最大時間を指定します。このパラメーターを使用して時間カウンターのオーバーフローを防ぐことができます。既定のサイズは 64 ビットです。
時間カウンターが使用するビット数は、[アプリケーションのライフスパン (日)]パラメーターの設定に応じて変わります。たとえば、時間カウンターがオーバーフローを回避するために 1 kHz のレートでインクリメントする場合、カウンターのビット数は次のようになります。
ライフスパン < 0.25 秒: 8 ビット
ライフスパン < 1 分: 16 ビット
ライフスパン < 49 日: 32 ビット
ライフスパン > 50 日: 64 ビット
64 ビット時間カウンターは、5 億 9,000 万年オーバーフローしません。
モデル例rtwdemo_abstime
を開きます。
モデルは、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. ビルドと検査プロセス用に一時フォルダーを作成します。
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 ビット符号なし整数のclockTick1
、clockTickH1
、clockTick2
およびclockTickH2
が,サブシステムSS2
とSS3
の経過時間を格納するカウンターになります。
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 ビット符号なし整数clockTick1
。SS2
のタスクの経過時間を保存します。
16 ビット符号なし整数clockTick2
。SS3
のタスクの経過時間を保存します。