Main Content

このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。

SIL シミュレーションと PIL シミュレーションを使用した生成コードのテスト

ソフトウェアインザループ (SIL) シミュレーションとプロセッサインザループ (PIL) シミュレーションを使用して、モデル コンポーネントと、そのコンポーネントから生成する量産コードの数値的等価性をテストします。

SIL シミュレーションでは、開発用コンピューターでソース コードをテストします。PIL シミュレーションでは、製品に展開する予定のコンパイルされたオブジェクト コードを、実際のターゲット ハードウェアまたは命令セット シミュレーター上で実行してテストします。モデル コンポーネントと生成コードが数値的に等価であるかどうかを判断するには、SIL と PIL の結果をノーマル モードの結果と比較します。

SIL シミュレーションと PIL シミュレーションには 3 つの実行方法があります。最上位モデル、Model ブロック、またはサブシステムから作成した SIL ブロックと PIL ブロックを使用できます。SIL アプローチまたは PIL アプローチの選択を参照してください。

PIL のターゲット接続構成

PIL シミュレーションを実行する前に、ターゲット接続性を設定しなければなりません。ターゲット接続構成によって、PIL シミュレーションで次が可能になります。

  • ターゲット アプリケーションの作成。

  • ターゲット上のアプリケーションのダウンロード、開始および停止。

  • Simulink とターゲットの間の通信のサポート。

ターゲット接続構成を行うには、与えられたターゲット接続 API を使用できます。詳細については、Create PIL Target Connectivity Configuration for Simulinkを参照してください。

サポート対象のハードウェアについては、ターゲット サポート パッケージを使用できます。詳細については、Embedded Coder でサポートされているハードウェアを参照してください。

最上位モデルを使用した SIL または PIL シミュレーション

最上位モデルの SIL または PIL シミュレーションを実行することで、生成されたモデル コードをテストします。このアプローチを使用すると、次のことが可能になります。

  • 最上位モデルから生成され、スタンドアロンのコード インターフェイスを使用するコードをテストします。

  • MATLAB ワークスペースからテスト ベクトルまたはスティミュラスの入力を読み込むようにモデルを構成します。

  • 最上位モデルをノーマル、SIL および PIL シミュレーション モードの間で簡単に切り替えることができます。

単純なカウンターの最上位モデルを開きます。

model='SILTopModel';
close_system(model,0)
open_system(model)

数値的等価性のテストに焦点を当てるには、次を無効にします。

  • モデル カバレッジ

  • コード カバレッジ

  • 実行時間プロファイリング

set_param(gcs, 'RecordCoverage','off');
coverageSettings = get_param(model, 'CodeCoverageSettings');
coverageSettings.CoverageTool='None';
set_param(model, 'CodeCoverageSettings',coverageSettings);
set_param(model, 'CodeExecutionProfiling','off');

入力のスティミュラス データを構成します。

[ticks_to_count, reset, counter_mode, count_enable] = ...
    SILTopModelData(T);

モデルのログ オプションを構成します。

set_param(model, 'LoadExternalInput','on');
set_param(model, 'ExternalInput','ticks_to_count, reset, counter_mode, count_enable');
set_param(model, 'SignalLogging', 'on');
set_param(model, 'SignalLoggingName', 'logsOut');
set_param(model, 'SaveOutput','on')

ノーマル モードのシミュレーションを実行します。

set_param(model,'SimulationMode','normal')
sim_output = sim(model,10);
yout_normal = [sim_output.yout.signals(1).values sim_output.yout.signals(2).values];

最上位モデルの SIL シミュレーションを実行します。

set_param(model,'SimulationMode','Software-in-the-Loop (SIL)')
sim_output = sim(model,10);
yout_sil = [sim_output.yout.signals(1).values sim_output.yout.signals(2).values];
### Starting build procedure for: SILTopModel
### Successful completion of build procedure for: SILTopModel

Build Summary

Top model targets built:

Model        Action                        Rebuild Reason                                    
=============================================================================================
SILTopModel  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 40.741s
### Preparing to start SIL simulation ...
Building with 'gcc'.
MEX completed successfully.
### Updating code generation report with SIL files ...
### Starting SIL simulation for component: SILTopModel
### Application stopped
### Stopping SIL simulation for component: SILTopModel

このモデルの最新のコードが存在する場合を除いて、新しいコードが生成され、コンパイルされます。生成されたコードはコンピューター上で別のプロセスとして実行されます。

ノーマル シミュレーションと SIL シミュレーションの結果をプロットして比較します。結果が一致することを確認します。

fig1 = figure;
subplot(3,1,1), plot(yout_normal), title('Counter Output for Normal Simulation')
subplot(3,1,2), plot(yout_sil), title('Counter Output for SIL Simulation')
subplot(3,1,3), plot(yout_normal-yout_sil), ...
    title('Difference Between Normal and SIL');

クリーンアップします。

close_system(model,0);
if ishandle(fig1), close(fig1), end
clear fig1
simResults = {'yout_sil','yout_normal','model','T',...
              'ticks_to_count','reset'};
save([model '_results'],simResults{:});
clear(simResults{:},'simResults')

Model ブロックを使用した SIL または PIL シミュレーション

Model ブロックを SIL モードで実行するテスト ハーネス モデルを使用して、生成されたモデル コードをテストします。このアプローチを使用すると、次のことが可能になります。

  • 最上位モデルまたは参照モデルから生成されたコードをテストできます。最上位モデルから生成されたコードはスタンドアロンのコード インターフェイスを使用します。参照モデルから生成されたコードはモデル参照コード インターフェイスを使用します。詳細については、SIL および PIL のコード インターフェイスを参照してください。

  • テスト ハーネス モデルまたはシステム モデルを使用して、テスト ベクトルまたはスティミュラスの入力を提供します。

  • Model ブロックをノーマル、SIL および PIL シミュレーション モードの間で簡単に切り替えることができます。

同じモデルを参照する 2 つの Model ブロックを含むモデル例を開きます。シミュレーションでは、1 つの Model ブロックを SIL モードで実行し、別の Model ブロックをノーマル モードで実行します。

model='SILModelBlock';
open_system(model);

以下を無効にします。

  • コード カバレッジ

  • 実行時間プロファイリング

coverageSettings = get_param(model, 'CodeCoverageSettings');
coverageSettings.CoverageTool='None';
set_param(model, 'CodeCoverageSettings',coverageSettings);
open_system('SILModelBlock')
set_param('SILModelBlock', 'CodeExecutionProfiling','off');
open_system('SILCounter')
set_param('SILCounter', 'CodeExecutionProfiling','off');
currentFolder=pwd;
save_system('SILCounter', fullfile(currentFolder,'SILCounter.slx'))

モデルの状態ログを構成します。

set_param('SILCounter', 'SaveFormat','Dataset');
save_system('SILCounter', fullfile(currentFolder,'SILCounter.slx'))
set_param(model, 'SaveFormat','Dataset');
set_param(model, 'SaveState','on');
set_param(model, 'StateSaveName', 'xout');

最上位モデルのコードのテスト

SIL モードの Model ブロックについて、スタンドアロン コード インターフェイスを使用する、最上位モデル コードの生成を指定します。

set_param([model '/CounterA'], 'CodeInterface', 'Top model');

テスト ハーネス モデルのシミュレーションを実行します。

out = sim(model,20);
### Starting build procedure for: SILCounter
### Successful completion of build procedure for: SILCounter

Build Summary

Top model targets built:

Model       Action                        Rebuild Reason                                    
============================================================================================
SILCounter  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 14.68s
### Preparing to start SIL simulation ...
Building with 'gcc'.
MEX completed successfully.
### Updating code generation report with SIL files ...
### Starting SIL simulation for component: SILCounter
### Application stopped
### Stopping SIL simulation for component: SILCounter

SIL モードの Model ブロックは、コンピューター上で別のプロセスとして実行されます。以前のビルドで生成されたコードが存在する場合を除いて、作業フォルダー内でスタンドアロン コードが参照モデル用に生成されているのを確認できます。

ノーマル モードと SIL モードの Model ブロックの動作を比較します。結果が一致します。

yout = out.logsOut;
yout_sil = yout.get('counterA').Values.Data;
yout_normal = yout.get('counterB').Values.Data;
fig1 = figure;
subplot(3,1,1), plot(yout_normal), title('Counter Output for Normal Simulation')
subplot(3,1,2), ...
    plot(yout_sil), title('Counter Output for Model Block SIL (Top-Model) Simulation')
subplot(3,1,3), plot(yout_normal-yout_sil), ...
    title('Difference Between Normal and SIL');

ノーマル モードでのシミュレーションと SIL モードでのシミュレーションから Model ブロックのログが作成された状態を比較します。

xout = out.xout;
xout_sil = xout{1}.Values.Data;
xout_normal = xout{2}.Values.Data;
fig1 = figure;
subplot(3,1,1), plot(xout_sil), title('State Logging for Normal Simulation')
subplot(3,1,2), ...
    plot(xout_normal), title('State Logging for Model Block SIL (Top-Model) Simulation')
subplot(3,1,3), plot(xout_normal-xout_sil), ...
    title('Difference Between Normal and SIL');

モデル参照コードのテスト

SIL モードの Model ブロックについて、モデル参照コード インターフェイスを使用する、参照モデル コードの生成を指定します。

set_param([model '/CounterA'], 'CodeInterface', 'Model reference');

テスト ハーネス モデルのシミュレーションを実行します。

out2 = sim(model,20);
### Starting serial model reference code generation build.
### Starting build procedure for: SILCounter
### Successful completion of build procedure for: SILCounter

Build Summary

Code generation targets built:

Model       Action                        Rebuild Reason                
========================================================================
SILCounter  Code generated and compiled.  SILCounter.c does not exist.  

1 of 1 models built (0 models already up to date)
Build duration: 0h 0m 12.055s
### Preparing to start SIL simulation ...
Building with 'gcc'.
MEX completed successfully.
### Updating code generation report with SIL files ...
### Starting SIL simulation for component: SILCounter
### Application stopped
### Stopping SIL simulation for component: SILCounter

SIL モードの Model ブロックは、コンピューター上で別のプロセスとして実行されます。以前のビルドで生成されたコードが存在する場合を除いて、作業フォルダー内でモデル参照コードが生成されているのを確認できます。

ノーマル モードと SIL モードの Model ブロックの動作を比較します。結果が一致します。

yout2 = out2.logsOut;
yout2_sil = yout2.get('counterA').Values.Data;
yout2_normal = yout2.get('counterB').Values.Data;
fig1 = figure;
subplot(3,1,1), plot(yout2_normal), title('Counter Output for Normal Simulation')
subplot(3,1,2), ...
    plot(yout2_sil), title('Counter Output for Model Block SIL (Model Reference) Simulation')
subplot(3,1,3), plot(yout2_normal-yout2_sil), ...
   title('Difference Between Normal and SIL');

ノーマル モードでのシミュレーションと SIL モードでのシミュレーションから Model ブロックのログが作成された状態を比較します。

xout2 = out.xout;
xout2_sil = xout2{1}.Values.Data;
xout2_normal = xout2{2}.Values.Data;
fig1 = figure;
subplot(3,1,1), plot(xout2_sil), title('State Logging for Normal Simulation')
subplot(3,1,2), ...
    plot(xout2_normal), title('State Logging for Model Block SIL (Model Reference) Simulation')
subplot(3,1,3), plot(xout2_normal-xout2_sil), ...
    title('Difference Between Normal and SIL');

クリーンアップします。

close_system(model,0);
if ishandle(fig1), close(fig1), end, clear fig1
simResults={'out','yout','yout_sil','yout_normal', ...
            'out2','yout2','yout2_sil','yout2_normal', ...
            'SilCounterBus','T','reset','ticks_to_count','Increment'};
save([model '_results'],simResults{:});
clear(simResults{:},'simResults')

SIL または PIL ブロックのシミュレーション

シミュレーションで SIL または PIL ブロックを使用することで、生成されたサブシステムのコードをテストします。このアプローチを使用すると、次のことが可能になります。

  • サブシステムから生成された、スタンドアロン コード インターフェイスを使用するコードをテストします。

  • テスト ベクトルまたはスティミュラスの入力を提供するために、テスト ハーネスまたはシステム モデルを指定します。

  • 元のサブシステムを生成された SIL または PIL ブロックで置き換えます。

制御アルゴリズムとプラント モデルが閉ループで構成されている、単純なモデルを開きます。制御アルゴリズムは、プラントからの出力を調整します。

model='SILBlock';
close_system(model,0)
open_system(model)

ノーマル モードのシミュレーションを実行します。

out = sim(model,10);
yout_normal = out.yout;
clear out

テスト用の SIL ブロックを作成するようにビルド プロセスを構成します。

set_param(model,'CreateSILPILBlock','SIL');

製品ハードウェア上の動作をテストするには、PIL ブロックを指定します。

SIL ブロックを作成するために、制御アルゴリズム サブシステムのコードを生成します。ビルド プロセスの最後に SIL ブロックが表示されます。SIL ブロックの入力端子と出力端子は、制御アルゴリズム サブシステムのものと一致します。

close_system('untitled',0);
slbuild([model '/Controller'])
### Starting build procedure for: Controller
### Successful completion of build procedure for: Controller
### Creating SIL block ...
Building with 'gcc'.
MEX completed successfully.

Build Summary

Top model targets built:

Model       Action                        Rebuild Reason                                    
============================================================================================
Controller  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 24.916s

または、サブシステムを右クリックして [C/C++ コード]、[このサブシステムをビルド] を選択することもできます。開いたダイアログ ボックスで [ビルド] をクリックします。

閉ループのコントローラーとプラント モデルの SIL シミュレーションを実施するには、元の制御アルゴリズムを新しい SIL ブロックで置換します。元のサブシステムが失われることを避けるために、この状態でモデルを保存しないでください。

controllerBlock = [model '/Controller'];
blockPosition = get_param(controllerBlock,'Position');
delete_block(controllerBlock);
add_block('untitled/Controller',[controllerBlock '(SIL)'],...
          'Position', blockPosition);
close_system('untitled',0);
clear controllerBlock blockPosition

SIL シミュレーションを実行します。

out = sim(model,10);
### Preparing to start SIL block simulation: <a href="matlab: targets_hyperlink_manager('run',1);">SILBlock/Controller(SIL)</a> ...
### Starting SIL simulation for component: SILBlock
### Application stopped
### Stopping SIL simulation for component: SILBlock

この制御アルゴリズムは単精度浮動小数点演算を使用します。SIL シミュレーションとノーマル シミュレーションの差の桁数は、単精度データのマシンの精度に近くなると予想できます。

単精度のノーマル シミュレーションの結果に関するマシンの精度に基づいて、SIL シミュレーションの結果の許容誤差を定義します。

machine_precision = eps(single(yout_normal));
tolerance = 4 * machine_precision;

ノーマル シミュレーションの結果と SIL シミュレーションの結果を比較します。3 番目のプロットにおいて、シミュレーション間の相違は、定義されたエラー許容誤差内に十分に収まっています。

yout_sil = out.yout;
tout = out.tout;
fig1 = figure;
subplot(3,1,1), plot(yout_normal), title('Controller Output for Normal Simulation')
subplot(3,1,2), plot(yout_sil), title('Controller Output for SIL Simulation')
subplot(3,1,3), plot(tout,abs(yout_normal-yout_sil),'g-', tout,tolerance,'r-'), ...
    title('Normal and SIL Difference and Error Tolerance');

クリーンアップします。

close_system(model,0);
if ishandle(fig1), close(fig1), end
clear fig1
simResults={'out','yout_sil','yout_normal','tout','machine_precision'};
save([model '_results'],simResults{:});
clear(simResults{:},'simResults')

ハードウェア実行設定

SIL シミュレーションを実行するときは、ハードウェア実行設定 (ネイティブなワード サイズなどの特性) を行って開発用コンピューターでのコンパイルを可能にしなければなりません。これらの設定は、実際の製品ハードウェア用にモデルをビルドするときに使用するハードウェア実行設定とは異なる場合があります。SIL シミュレーションと PIL シミュレーションの間でハードウェア実行設定の変更の必要性をなくすには、移植可能なワード サイズを有効にします。詳細については、Configure Hardware Implementation Settingsを参照してください。

関連するトピック