S-Function
모델에 S-Function 포함
라이브러리:
Simulink /
User-Defined Functions
설명
S-Function 블록을 사용하면 C, C++ 또는 Fortran®으로 작성된 S-Function 또는 시스템 함수 알고리즘(C MEX S-Function이라고 함)을 Simulink 모델에 통합할 수 있습니다. 이 블록을 사용하여 Level-1 C MEX S-Function 또는 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이 있는 경우 S-function은 mex 유틸리티를 사용하여 MEX 파일로 컴파일되어야 합니다(C MEX 함수 빌드하기 참조). S-Function은 초기화, 업데이트, 도함수, 출력, 종료와 같은 시뮬레이션의 다양한 부분에서 블록이 작동하는 방식을 정의합니다. S-Function은 Simulink 엔진과 상호 작용할 수 있도록 하는 S-Function API라고 하는 특수 호출 구문을 사용합니다. 이 상호 작용은 엔진과 내장 Simulink 블록 사이에서 발생하는 상호 작용과 매우 비슷합니다. 시뮬레이션의 모든 스텝에서 특정 작업을 처리하기 위해 시뮬레이션 엔진이 메서드를 호출합니다.
예를 들어, 아래는 이름이 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 */
#endifS-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 이전에 개발됨

