Download What Is an S
Transcript
3
Writing S-Functions in C
correct way to access input elements and write them to the output elements
(assuming the input and output ports have equal widths) is to use this code.
int_T element;
int_T portWidth = ssGetInputPortWidth(S,inputPortIndex);
InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S,inputPortIndex);
real_T *y = ssGetOutputPortSignal(S,outputPortIdx);
for (element=0; element<portWidth; element++) {
y[element] = *uPtrs[element];
}
A common mistake is to try to access the input signals via pointer arithmetic.
For example, if you were to place
real_T *u = *uPtrs; /* Incorrect */
just below the initialization of uPtrs and replace the inner part of the above
loop with
*y++ = *u++; /* Incorrect */
the code compiles, but the MEX-file might crash Simulink. This is because it
is possible to access invalid memory (which depends on how you build your
model). When accessing the input signals incorrectly, a crash occurs when the
signals entering your S-function block are not contiguous. Noncontiguous
signal data occurs when signals pass through virtual connection blocks such
as the Mux or Selector blocks.
To verify that you are correctly accessing wide input signals, pass a replicated
signal to each input port of your S-function. You do this by creating a Mux
block with the number of input ports equal to the width of the desired signal
entering your S-function. Then the driving source should be connected to each
input port as shown in this figure.
3-66
Related documents
Writing S-Functions
MATLAB SIMULINK 7 - DEVELOPING S-FUNCTIONS User`s guide
Developement Of An Instrument Landing Simulation Using MATLAB
An Integrated, Modular Simulation System for Education and
MATLAB REAL-TIME WORKSHOP 7 - TARGET LANGUAGE COMPILER User`s guide
Simulink User's Guide
Motion Control of Industrial Printers
7.0 User`s Manual - Opal-RT