I have model containing an S-functions that calls into external binary code (DLL, LIB, EXE). I would like to run this simulation in real-time on my Windows PC using Simulink Desktop Real-Time (SLDRT) in External Mode. Is this possible?
MATLAB: Can I call external binary code (DLL, LIB, EXE) during a Simulink Desktop Real-Time simulation
dllexelibrtwtsldrt
Related Solutions
Simulink Desktop Real-Time (SLDRT) provides a real-time kernel for executing Simulink models on a laptop or desktop running Windows or MacOS. It includes a library of I/O driver blocks that provide connections between physical I/O devices and your real-time model. You can run your model in Normal, Accelerator, or External Mode, depending on the sample rate that you require:
Normal Mode real-time simulation:
If you require a moderate sample rate up to 1 kHz, use Normal mode. In these modes, the model is run "normally" within Simulink. Only the I/O module drivers run in the real-time kernel.
This means that in Normal mode, SLDRT does not guarantee real-time performance. However, it is capable of reporting to the user when real-time performance cannot be achieved by using the Missed Ticks feature. On each sample hit, I/O is performed, while the rest of the algorithm runs in Simulink, and Simulink tries to synchronize to the real-time I/O. If Simulink makes it in time, there is no missed tick. If Simulink is late, it is indicated by the number of missed ticks Simulink lags behind. If your model contains no I/O blocks, you can add the Real-Time Sync block for the same purpose.
Setting "Maximum Missed Ticks" to some low number or even zero would error out when real-time cannot be met. Some higher number specifies how much tolerance you permit before erroring out, or you may use the missed ticks output port to do any custom action you want.
The advantages of SLDRT Normal Mode are:
- No additional toolboxes required.
- The possibility to use blocks that do not support code generation.
- The possibility to use any S-Function and call external binaries.
- The possibility to use variable-step solvers.
The limitations of SLDRT Normal Mode are:
- No guarantee of real-time (only a detection if real-time constraints are met or not)
- Limited performance of up to 1 kHz.
See this page for a detailed description of SLDRT Normal Mode:
Accelerator Mode real-time simulation:
If you see too many missed ticks while running your model in Normal Mode, this can mean that your model is too big or complex to run in the desired sample time. This is a situation where running in Accelerator mode may help, which is an extension to the Normal Mode and has the same advantages and limitations.
See this page for a detailed description of SLDRT Accelerator Mode:
External Mode real-time simulation:
f you require a higher sample rate, use External Mode. In External Mode, the model, solver and drivers are converted to C code, built into a real-time executable, and run in the real-time kernel. This means that the entire model is running in the real-time kernel and the executable is fully synchronized with the real-time clock. Depending on model complexity and number and type of I/O, it is usable to up to 20 kHz of sample rate.
The advantages of SLDRT External Mode are:
- Guarantee of real-time.
- Good performance of up to 20 kHz.
- Recommend for operation with external real-time I/O hardware.
The limitations of SLDRT External Mode are:
- Simulink Coder toolbox is required.
- Models must only use blocks that support code generation
- Limited support for S-Functions and no support for external binaries
- Fixed-step solver is required.
See this page for a detailed description of SLDRT External Mode:
S-functions can be used in Simulink Desktop Real-Time (SLDRT) External Mode only if they are self-contained, i.e. they do not call anything but a limited subset of the standard C library, like math and string functions, but not file or console I/O, or Windows API functions (manifested by the need to #include <windows.h>).
Note that being able to run an S-function with other targets (such as grt.tlc, ert.tlc or slrt.tlc) is not a proof that it will run with sldrt.tlc. For example, S-functions that are linked with dynamically linked libraries (DLLs) on Windows generally do not run with SLDRT because DLLs are not available to the real-time kernel.
In particular, the following functions are not supported in the C source code:
- File I/O (fopen etc.)
- Process management (spawn, exit, etc.)
- Signals and exceptions (signal, longjmp, __try etc.)
- Time functions (clock etc.)
- anything from the Windows API
The following C library functions are supported for use with Simulink Desktop Real-Time:
- Data conversion functions: abs atof atoi atol itoa labs ltoa strtod strtol strtoul ultoa
- Memory allocation functions: calloc free malloc
- Memory manipulation functions: _memccpy memcpy memchr memcmp _memicmp memmove memset
- String manipulation functions: strcat strchr strcmp strcpy strcspn _strdup _stricmp strlen _strlwr strncat strncmp strncpy _strnset strpbrk strrchr _strrev _strset strspn strstr strtok _strupr
- Mathematical functions: acos asin atan atan2 ceil cos cosh div exp fabs floor fmod frexp ldexp ldiv log log10 max min modf pow rand sin sinh sqrt srand tan tanh uldiv
- Character class tests and conversion: isalnum isalpha _isascii iscntrl isdigit isgraph islower isprint ispunct isspace isupper isxdigit isxupper isxlower _toascii tolower toupper
- Searching and sorting: bsearch qsort
- Dummy functions - can be there but do nothing: exit
- Console I/O: fprintf printf
This information is currently not part of the Simulink Desktop Real-Time documentation. However, we submitted an internal request to add this list of functions to a future release of the documentation.
Note that the above limitation do not apply for Normal or Accelerator Mode real-time simulations. To learn more about the different SLDRT simulation modes, see the following MATLAB Answers article:
Related Question
- Is External Mode Real Time Windows Target not supported for 64-bit machines and why does Simulink Coder 8.2PR (R2012a) crash when I use the External Mode Real Time Windows Target
- Am I unable to use CAN blocks from Vehicle Network Toolbox in External Mode of Simulink Desktop Real-Time
- What is causing missed ticks in the Simulink Desktop Real-Time simulation, and how can I reduce them
- S-function performance in SLDRT external mode
Best Answer