How can I create a DLL target for RTW so I can create a DLL from my Simulink model that can be called from other C/Fortran programs?

19 views (last 30 days)
I would like Real-Time Workshop to generate a DLL for my model that I can include as part of another project. I would like to be able to have a library generated.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 31 Oct 2016
Edited: MathWorks Support Team on 31 Oct 2016
This enhancement has been incorporated in Release 2007a (R2007a). You can use the ert_shrlib.tlc target to generate a host-based shared library from your Simulink model from R2007a onwards. For previous product releases, read below for any possible workarounds:
INTRODUCTION:
These instructions show how a .dll can be built for interfacing to another system, for example a test automation engine. To do this we need a new .dll target which is implemented in ert_default_tmf_dll.m and ert_dll_vc.tmf.
IMPLEMENTATION:
1. Copy the following files to the current working directory (referred to as <root> in this document):
ert_default_tmf_dll.m
ert_dll_vc.tmf
set_up_files.m
2. To capture signals in the console application, two things must be done in the model:
a. The signal must be named and set to ExportedGlobal. This can be done in two ways. The first may be preferable, for a reason explained in the Major/Minor Time Steps section below.
i. Data Store Memory and Data Store Write blocks can be used. Inside the Data Store Memory block, set the RTW storage class to ExportedGlobal. In the Data Store Write block, name the signal in the "Data store name" field.
ii. Alternatively, right-click on a signal line and choose Signal Properties. In the dialog, name the signal, and on the Real-Time Workshop tab, change "RTW storage class" to ExportedGlobal.
b. Custom functions must be added to allow for signal retrieval. This can be done using a Custom Code block (the Model Source Custom Code block in the Real-Time Workshop library). Here is some example code that can be put in the Top of Model Source section of the Custom Code block:
double DumpData(void);
double DumpData(void);
{
/* Note signals here need to be exported globals */
return(Sig2a);
}
Any function declared in a Custom Code block must be defined in the <root>\<model>_ert_rtw\SimExp.def file (see Step 5). This will ensure that the function is exported to be used in the console application.
3. In the Real-Time Workshop Configuration Parameters pane, set the System target file to ert.tlc and change the Template makefile to ert_default_tmf_dll.
4. Run set_up_files.m to set up the directories expected by the target (.dll destination directory, directory for project, and directory for generated code). It will create:
<root>\dll /* the .dll destination directory */
<root>\<model>_ert_rtw /* the model source code directory */
<root>\prj /* the project directory */
5. Edit the <root>\<model>_ert_rtw\SimExp.def file to include all of the functions declared in the Custom Code block.
6. Build the model. This will create:
<root>\dll
<model>.dll
<model>.exp
<model>.lib
<model>.pdb
<root>\<model>_ert_rtw
<model>.c
<model>.h
...
<root>\slprj
7. Open MSVC and start a new console application. These instructions are written using MSVC 7.1. The project can be created in the \prj folder. It is recommended to create an empty project (this can be done on the Application Settings dialog of the Win32 Application Wizard).
a. Add the console C-file (e.g. test_prj.c) as a source file. This will be the file that defines the entry point for the console application (see Examples below for more information on the creation of this file). The console C-file can be located in the <root> directory.
b. Add <root>\dll\<model>.lib to the Resource Files.
c. Add the following Include directories:
$MATLABROOT\rtw\c\libsrc
$MATLABROOT\extern\include
$MATLABROOT\rtw\c\ert
$MATLABROOT\rtw\c\src\ext_mode\common
$MATLABROOT\rtw\c\src
$MATLABROOT\simulink\include
<root>\<model>_ert_rtw
(where $MATLABROOT is the MATLAB root directory on your machine, as returned by typing
matlabroot
at the MATLAB command prompt and <root> is the working directory as mentioned in earlier steps).
8. Copy <model>.dll from <root>\dll to <root>\prj.
9. The console application (<prj>.exe) should be located in <root>\prj\Debug.
EXAMPLES:
Enclosed are three examples using the .dll target. They include project files for MSVC. Note, in all projects, you will have to change:
H:\matlabfiles\dll_target\dll_target_V2.2\<example>\<model>_ert_rtw
in the Additional Includes section to <root>\<model>_ert_rtw (wherever you have installed the example files).
1. simple_example.mdl
This demonstrates the basic use of the .dll target.
2. minor_step_out.mdl
This example demonstrates how there can be issues with minor time steps being output to the console application.
Open minor_step_issue. One thing to note is the signals "timeOut" and "memoryOut" are set to ExportedGlobal RTW storage class. This allows them to be called from the functions declared in the DumpData Custom Code block.
3. major_step_out.mdl
This example demonstrates how to work around the minor time step issue shown in minor_step_out.mdl.
Open major_step_issue. Note the signals "timeOut" and "memoryOut" now have a RTW storage class of Auto. The Data Store Memory and Data Store Write blocks have been added to ensure data is only output at the major time steps. This is done via the sample time setting in the Data Store Write blocks. Note it is [0 1], which forces the values stored to be fixed in minor time steps. For more information on how to set the sample time in Simulink, see the following documentation:
Modeling and Simulating Discrete Systems
web([docroot,'/toolbox/simulink/ug/f7-9753.html'])
You can copy and paste the above command into the MATLAB command window to display the documentation.
Also, in the Data Store Memory block, the RTW storage class should be set to ExportedGlobal.
After running through the example, try changing the sample times in the Data Store Write blocks to -1. Going through the steps again should yield results similar to those in minor_step_issue.mdl (i.e. the outputs will be at minor time steps).
4. write_buffered_example.mdl
This is just another example of the minor time step issue demonstrated in examples 2 and 3.
MAJOR/MINOR TIME STEPS:
Here is a bit more explanation of the major versus minor time step issue that may arise when using continuous Integrator blocks in the model. Sometimes during a simulation, a solver will take a step that is smaller than the given step size if the dynamics of the model require quick iterations when solving. For example, if your step time is 0.01, the solver may first take a step of 0.0092, then, if it determines the step size of 0.01 is sufficient, it will take that for the rest of the simulation.
The step of 0.0092 is a minor step. Normally, Simulink does not output these during simulation, it only outputs major time steps (0, 0.01, 0.02, etc.). Also, in a Real-Time Workshop executable (the .exe generated by using the GRT Target, for example), this is handled internally so only the major time steps are output.
For each signal to be accessed by the console application, a Data Store Write block (along with the corresponding Data Store Memory block) can be used. There are two settings that make it function as desired.
First, in the Data Store write block, a sample time of [0 1] needs to be used. This tells Simulink to inherit only major time steps. Second, in the Data Store Memory block, on the Main tab of the block parameters, the RTW Storage Class needs to be set to ExportedGlobal. This will ensure the variables are available for the code in the custom code to return to the external application.

More Answers (0)

Categories

Find more on Simulink Coder in Help Center and File Exchange

Products


Release

R14SP1

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!