This example shows how to generate code for a Simulink® model that contains an S-Function that relies on a static library. Static libraries can encapsulate proprietary algorithms and provide an interface to be used in generated code and Simulink. Using a static library is one technique to protect your model intellectual property (IP). Alternatively, you may use S-Function objects. Shared libraries, or a Simulink® Protected model for IP protection, etc.
Given that the model code must run on the SimWB real-time host, which uses a Linux platform, this example focuses on the Linux workflow. You must work through this example on an instance of MATLAB® running on a MATLAB and SimWB-supported Linux platform.
Suppose that your proprietary library calculates the hypotenuse of a right-angled triangle,
given the two sides.
The following are the contents of <MLTOOLKITROOT>/examples/use_lib/pythagoras.c.
#include <stdio.h>
#include <math.h>
double hypotenuse(double a, double b){
return sqrt(pow(a,2)+pow(b,2));
}
pythagoras.h is a companion header file for pythagoras.c , provided as an interface to the
library. Its contents are as follows:
#ifndef HYPOTENUSE_H_
#define HYPOTENUSE_H_
double hypotenuse(double a, double b);
#endif
Using the GCC compiler and the ar tool,
you can create the static library for pythagoras.c by
executing the following at the MATLAB command prompt:
% using the bang operator (!) as a shell escape
!gcc -c -o pythagoras.o pythagoras.c -fPIC
!ar rsv libpythagoras.a pythagoras.o
For more information regarding these commands,
type man gcc or man ar at a shell terminal.
Write an S-Function
that uses the hypotenuse method of the library. For an example, see <MLTOOLKITROOT>/examples/use_lib/use_lib.c.
Create a MEX-file for simulation using the following command at the MATLAB command prompt:
mex use_lib.c libpythagoras.a
The model <MLTOOLKITROOT>/examples/use_lib/ccur_calc_hyp.mdl (developed in MATLAB version 7.11
(R2010b)) contains an S-Function block that uses the use_lib S-Function. Constant blocks
(SW_a, SW_b) supply the required inputs and the output can be viewed via the Display block
(SW_out). You can run this model in Simulink® for different values of SW_a and
SW_b to verify the algorithm behavior.
To distribute this S-Function to a third party, you must provide your user with the following files.
Notice that the list excludes the pythagoras.c file, which contains the IP to be protected.
use_lib.c — S-Function source filepythagoras.h — Library interface header filelibpythagoras.a — Proprietary libraryuse_lib.mexa64 — S-Function MEX file to facilitate simulation in Simulinkccur_calc_hyp.mdl (Optional) — S-Function exampleThe process to create the RTDB and generate SimWB-compliant code for this model is described in this example.
Note:
To ensure that the static library is ported to the real-time host along with the model source
files, you must modify the Simulink model's
Configuration Parameters > Code Generation > Custom Code > Include
list of additional > Libraries list to include libpythagoras.a.
|
Simulink®, developed by MathWorks, is a graphical modeling environment.