Sie sind auf Seite 1von 6

2.

SIMULINK: User Defined Functions


2.1 Fcn

Æ Anweisungszeile: Ausgangsberechnung mit C-artiger Syntax:


• Eingangsvektor u – Element-weiser Zugriff u(1)...
• arithmetische, logische, relationale Operatoren, math. Funktionen, Variablen

Beispiel: gewichtete Summe y=a*u1+b*u2 ; a=2, b=3

Institut für Prozessinformatik


und Leittechnik
Prof. Krabbes Hochschule
für Technik, Wirtschaft
Sim-II_V2/ 1
und Kultur Leipzig (FH)

2.2 MATLAB Function

Æ Aufruf einer MATLAB-Funktion (System oder Anwender)


• Eingangsvektor u – Element-weiser Zugriff u(1)...
• Übergabe der deklarierten Parameter

Beispiel: gewichtete Summe y=a*u1+b*u2 ; a=2, b=3

wsum.m:
function summe = wsum(x, a, b)
summe = a*x(1)+b*x(2);

Institut für Prozessinformatik


und Leittechnik
Prof. Krabbes Hochschule
für Technik, Wirtschaft
Sim-II_V2/ 2
und Kultur Leipzig (FH)
2.3 S-Function

Æ Aufruf einer sogenannten S-Function


• zyklischer Aufruf mit Auflösung
Init. / diskrete Zustände bzw. kont. Zustände + Ableitung / Termin.
• Eingangsvektor u – Element-weiser Zugriff u(1)...
• Übergabe der deklarierten Parameter
= komplettes Modell kann spezifizert werden.

Beispiel: gewichtete Summe y=a*u1+b*u2 ; a=2, b=3

Programmierung
• als m.File oder
• in C (kompilieren mit mex)
• gleicher Baustein

Institut für Prozessinformatik


und Leittechnik
Prof. Krabbes Hochschule
für Technik, Wirtschaft
Sim-II_V2/ 3
und Kultur Leipzig (FH)

M S-Function
interner Aufruf
t Current time
x State vector
u Input vector
flag Integer value that indicates the task to be performed by the S-function

Festlegung der Eigenschaften:

Institut für Prozessinformatik


und Leittechnik
Prof. Krabbes Hochschule
für Technik, Wirtschaft
Sim-II_V2/ 4
und Kultur Leipzig (FH)
M S-Function

Parameterübergabe:
Argumente nach t,x,u,flag

Eigenschaften des Modells:

Institut für Prozessinformatik


und Leittechnik
Prof. Krabbes Hochschule
für Technik, Wirtschaft
Sim-II_V2/ 5
und Kultur Leipzig (FH)

M S-Function Template siehe mtemplate.m

Anpassungen für Beispiel:


function [sys,x0,str,ts] = mwsum(t,x,u,flag,a,b)
...
case 3,
sys=mdlOutputs(t,x,u,a,b);
...
sizes.NumContStates = 0;
sizes.NumDiscStates = 0;
sizes.NumOutputs = 1;
sizes.NumInputs = 2;
sizes.DirFeedthrough = 1;
sizes.NumSampleTimes = 1; % at least one sample time is needed
...
function sys=mdlOutputs(t,x,u,a,b)
%pause(0.1);
sys=a*u(1)+b*u(2);
% end mdlOutputs

Institut für Prozessinformatik


und Leittechnik
Prof. Krabbes Hochschule
für Technik, Wirtschaft
Sim-II_V2/ 6
und Kultur Leipzig (FH)
C S-Function (Level 1)

• vordefinierte Struktur eines .c-Files mit Funktionsaufrufen ähnlich M S-


Function
• Funktionen werden direkt aufgerufen (ohne flag)

Institut für Prozessinformatik


und Leittechnik
Prof. Krabbes Hochschule
für Technik, Wirtschaft
Sim-II_V2/ 7
und Kultur Leipzig (FH)

C S-Function siehe sfuntmpl_basic.c

Minimalversion für Beispiel: cwsum.c

Teil 1: Defines und Includes

#define S_FUNCTION_NAME cwsum


#define S_FUNCTION_LEVEL 2

#include "simstruc.h"

Institut für Prozessinformatik


und Leittechnik
Prof. Krabbes Hochschule
für Technik, Wirtschaft
Sim-II_V2/ 8
und Kultur Leipzig (FH)
C S-Function

Teil 2: Callback Implementierung


static real_T a,b;

static void mdlInitializeSizes(SimStruct *S)


{ssSetNumSFcnParams(S, 2); /* Number of expected parameters */
if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {return;}
a=mxGetPr(ssGetSFcnParam(S, 0))[0];
b=mxGetPr(ssGetSFcnParam(S, 1))[0];

ssSetNumContStates(S, 0);
ssSetNumDiscStates(S, 0);
if (!ssSetNumInputPorts(S, 1)) return;
ssSetInputPortWidth(S, 0, 2);
ssSetInputPortDirectFeedThrough(S, 0, 1);

if (!ssSetNumOutputPorts(S, 1)) return;


ssSetOutputPortWidth(S, 0, 1);

Institut für Prozessinformatik


und Leittechnik
Prof. Krabbes Hochschule
für Technik, Wirtschaft
Sim-II_V2/ 9
und Kultur Leipzig (FH)

C S-Function

Teil 2: Callback Implementierung - Fortsetzung


...
ssSetNumSampleTimes(S, 1);
ssSetNumRWork(S, 0);
ssSetNumIWork(S, 0);
ssSetNumPWork(S, 0);
ssSetNumModes(S, 0);
ssSetNumNonsampledZCs(S, 0);

ssSetOptions(S, 0);
}
...
static void mdlOutputs(SimStruct *S, int_T tid)
{
const real_T *u = (const real_T*) ssGetInputPortSignal(S,0);
real_T *y = ssGetOutputPortSignal(S,0);
y[0] = a*u[0]+b*u[1];
}

Institut für Prozessinformatik


und Leittechnik
Prof. Krabbes Hochschule
für Technik, Wirtschaft
Sim-II_V2/ 10
und Kultur Leipzig (FH)
C S-Function

Teil 3: Simulink/Real-Time Workshop Interface

#ifdef MATLAB_MEX_FILE
#include "simulink.c"
#else
#include "cg_sfun.h"
#endif

Teil 4: Building
>> mex cwsum.c

Institut für Prozessinformatik


und Leittechnik
Prof. Krabbes Hochschule
für Technik, Wirtschaft
Sim-II_V2/ 11
und Kultur Leipzig (FH)

C S-Function (Level 2)

• seit Matlab Version 7


• objektorientierter Ansatz Æ strukturierte Datentypen
• stark abweichendes Konzept mit neuem Template
• (Anwendung noch kein Lehrinhalt)

Institut für Prozessinformatik


und Leittechnik
Prof. Krabbes Hochschule
für Technik, Wirtschaft
Sim-II_V2/ 12
und Kultur Leipzig (FH)

Das könnte Ihnen auch gefallen