Writing S-Functions    

Example of an Ada S-Function

This section presents an example of a basic Ada S-function that you can use as a model when creating your own Ada S-functions. The example is the timestwo S-function example that comes with Simulink (see matlabroot/simulink/ada/examples/timestwo.ads and matlabroot/simulink/ada/examples/timestwo.adb). This S-function outputs twice its input.

The following model uses the timestwo S-function to double the amplitude of a sine wave and plot it in a scope.

The block dialog for the S-function specifies timestwo as the S-function name; the parameters field is empty.

The timestwo S-function contains the S-function callback methods shown in this figure.

The source code for the timestwo S-function comprises two parts:

The following sections explain each of these parts.

Timestwo Package Specification

The timestwo package specification, timestwo.ads, contains the following code.

The package specification begins by specifying that the S-function uses the Simulink package.

The Simulink package defines Ada procedures for accessing the internal data structure (SimStruct) that Simulink maintains for each S-function (see SimStruct Functions).

Next the specification specifies the name of the S-function.

The name ada_times_two serves to distinguish the MEX-file generated from Ada source from those generated from the timestwo source coded in other languages.

Finally the specification specifies the callback methods implemented by the timestwo S-function.

The specification specifies that the Ada compiler should compile each method as a C-callable function. This is because the Simulink engine assumes that callback methods are C functions.

Timestwo Package Body

The timestwo package body, timestwo.adb, contains

The package body contains implementations of the callback methods needed to implement the timestwo example.

mdlInitializeSizes

Simulink calls mdlInitializeSizes to inquire about the number of input and output ports, the sizes of the ports, and any other objects (such as the number of states) needed by the S-function.

The timestwo implementation of mdlInitializeSizes uses SimStruct functions defined in the Simulink package to specify the following size information:

Finally the method provides an exception handler to handle any errors that occur in invoking the SimStruct functions.

mdlOutputs

Simulink calls mdlOutputs at each time step to calculate a block's outputs. The timestwo implementation of mdlOutputs takes the input, multiplies it by 2, and writes the answer to the output.

The timestwo implementation of the mdlOutputs method uses the SimStruct functions ssGetInputPortWidth and ssGetInputPortSignalAddress to access the input signal.

Similarly, the mdlOutputs method uses the functions ssGetOutputPortWidth and ssGetOutputPortSignalAddress to access the output signal.

Finally the method loops over the inputs to compute the outputs.

Building the Timestwo Example

To build this S-function into Simulink, enter

at the command line.


  Building an Ada S-Function Creating C++ S-Functions