このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。
S-Function
モデルに S-Function を追加
ライブラリ:
Simulink /
User-Defined Functions
説明
S-Function ブロックでは、C、C++、または Fortran® で記述された C MEX S-Function と呼ばれる S-Function (システム関数) のアルゴリズムを Simulink モデルに統合できます。このブロックは Level-1 または Level-2 の C MEX S-Function の実装に使用できます。S-Function をモデルに組み込むには、C MEX S-Function をコンパイルしてから以下を行います。
S-function ブロックを [Simulink ライブラリ ブラウザー] からモデルにドラッグします。
[ブロック パラメーター] ダイアログ ボックスを開き、[S-Function 名] フィールドで S-Function の名前を指定して S-function ブロックの機能を提供します。たとえば、「
timestwo
」と入力して [適用] を選択します。
"S-Function" は MATLAB®、C、C++ または Fortran で記述された Simulink® ブロックをコンピューター言語で記述したものです。C、C++、または Fortran の S-Function を用意したら、mex
ユーティリティを使用して、それらを MEX ファイルとしてコンパイルする必要があります (C MEX 関数のビルドを参照)。S-Function では、初期化、更新、微分、出力、終了など、シミュレーションのさまざまな部分におけるブロックの動作を定義します。S-Function では、S-Function API と呼ばれる特殊な呼び出し構文を使用して Simulink エンジンと対話できます。この対話は、エンジンと組み込みの Simulink ブロックの間で行われる対話と非常によく似ています。シミュレーションの各ステップで、シミュレーション エンジンにより、特定のタスクを実行するメソッドが呼び出されます。
たとえば、以下は、入力を 2 倍にして出力する timestwo.c
という名前の簡単な C MEX S-Function の例です。
#define S_FUNCTION_NAME timestwo /* Specifies the name of the S-function (timestwo) */
#define S_FUNCTION_LEVEL 2 /* Specifies that the S-function is in the Level 2 format */
#include "simstruc.h" /* Defines the data structure */
static void mdlInitializeSizes(SimStruct *S) /* Initialize the input and output ports and their size */
{
ssSetNumSFcnParams(S, 0);
if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
return; /* Parameter mismatch reported by the Simulink engine*/
}
if (!ssSetNumInputPorts(S, 1)) return;
ssSetInputPortWidth(S, 0, DYNAMICALLY_SIZED);
ssSetInputPortDirectFeedThrough(S, 0, 1);
if (!ssSetNumOutputPorts(S,1)) return;
ssSetOutputPortWidth(S, 0, DYNAMICALLY_SIZED);
ssSetNumSampleTimes(S, 1);
/* Take care when specifying exception free code - see sfuntmpl.doc */
ssSetOptions(S, SS_OPTION_EXCEPTION_FREE_CODE);
}
static void mdlInitializeSampleTimes(SimStruct *S) /* Set the sample time of the S-function as inherited */
{
ssSetSampleTime(S, 0, INHERITED_SAMPLE_TIME);
ssSetOffsetTime(S, 0, 0.0);
}
static void mdlOutputs(SimStruct *S, int_T tid) /* Calculate the block output for each time step */
{
int_T i;
InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S,0);
real_T *y = ssGetOutputPortRealSignal(S,0);
int_T width = ssGetOutputPortWidth(S,0);
for (i=0; i<width; i++) {
*y++ = 5.0 *(*uPtrs[i]);
}
}
static void mdlTerminate(SimStruct *S){} /* Perform tasks at the end of the simulation */
#ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */
#include "simulink.c" /* MEX-file interface mechanism */
#else
#include "cg_sfun.h" /* Code generation registration function */
#endif
S-Function は一般的な形式に従っており、連続システム、離散システム、およびハイブリッド システムに対応できます。S-Function でアルゴリズムを実装し、それを S-Function ブロックを使用して Simulink モデルに追加できます。S-Function を記述して、その名前を S-Function ブロック (User-Defined Functions ブロック ライブラリで利用可能) に配置した後、マスクを使用してユーザー インターフェイスをカスタマイズできます。この例では、timestwo.c
関数が mex timestwo.c
を使用してコンパイルされ、S-Function ブロックを使用して実装されています。
S-Function ブロックは、指定された S-Function の名前と S-Function により指定される入力端子および出力端子の数を表示します。入力に接続される信号は、入力に対し、S-Function により指定される次元をもつ必要があります。
メモ
Level-2 MATLAB S-Function ブロックを使用して、ブロック線図に Level-2 MATLAB S-Function をインクルードします。
例
端子
入力
出力
パラメーター
ブロックの特性
拡張機能
バージョン履歴
R2006a より前に導入