Why does my S-function run faster in SLDRT normal mode than external mode?
MATLAB: S-function performance in SLDRT external mode
desktopexternalmodenormalreals-functionsimulinksldrttime
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:
The observed is the expected behavior of Real-Time Workshop 7.4 (R2009b):
1) Rapid Accelerator: The TLC is being used 2) Accelerator without SS_OPTION_USE_TLC_WITH_ACCELERATOR: No TLC is used 3) Accelerator with SS_OPTION_USE_TLC_WITH_ACCELERATOR: Parts of the TLC are being used * mdlOutputs() * mdlUpdate() * mdlDerivatives()
Read below for a more detailed description:
Look at the documentation for a "Process View" of "How the Simulink Engine Interacts with C S-Functions", at
<http://www.mathworks.com/access/helpdesk/help/toolbox/simulink/sfg/f8-37326.html#f8-83424>
This section of the documentation is about S-Functions, but nearly everything in it applies equally well to all blocks, and to entire models. In Normal mode, running a Model method runs the corresponding Block method for each block in the model. For a subsystem, running a Block method runs the corresponding method for each block in the subsystem.
The Simulink Accelerator generates code for a C-MEX S-Function containing code for the simulation loop of the model. In Accelerator mode, methods in diagram "Model Initialization" run just as they do in Normal mode, but some methods in diagram "Simulation Loop" run differently. Model methods mdlOutputs(), mdlUpdate(), and mdlDerivatives() call the corresponding methods in the Accelerator S-Function, instead of calling the methods for each individual block.
As observed, the Accelerator S-Function does not include the optional mdlStart() and mdlInitializeConditions(). It does contain the required mdlInitializeSampleTimes() and mdlTerminate(), but they are empty functions that are never called. It contains the required mdlInitializeSize(), but it operates differently than in a typical S-Function, checking to see if the S-Function needs to be recreated, and verifying that DWork and BlockIO are the correct size.
If SS_OPTION_USE_TLC_WITH_ACCELERATOR is set in an S-Function, the Accelerator S-Function methods mdlOutputs(), mdlUpdate(), and mdlDerivatives() will contain code generated by the corresponding .tlc file, if it exists. Otherwise the Accelerator S-Function will have code to call the corresponding methods in
the .mex file for the S-Function.
Note that mdlDerivatives() exists only if the model has continuous states.
For both Accelerator and Normal mode, put the desired code for mdlStart() and mdlInitializeConditions() in the S-Function source file and build it into the .mex file
To use mdlStart() and mdlInitializeConditions() code generated by the .tlc file that inlines an S-Function, use Rapid Accelerator or a Real-Time Workshop Target.
Best Answer