eProsima for Matlab
Although Matlab includes a DDS Support Package starting with version 2021a, it does not really fulfill our needs - its setup does not seem to be easy to automate, it only runs using Simulink and would require some work.
Instead, C++-based MEX-files are used to include DDS in our Matlab projects. These files are part of the software repository that also includes the cpm library.
All mentioned files except for matlab_setup.sh are located in
How to compile the MEX Files
In the following, we will assume that you already have installed FastDDS and FastCDR globally at /usr/local/lib. The
install.sh script provided with our software repository takes care of that for you.
If you use our provided scripts as explained in The Easy Way, everything should work as expected. In The Hard Way, we try to explain how to do everything manually instead.
The Easy Way
Everything you need to compile the MEX files should already be provided, given that you have Matlab installed on your system and added it to your path.
- Just run the install script of the software repository. If Matlab is in your path, it should setup the right GCC version and replace Matlab's own GCC version. If that fails, you should see a warning regarding Matlab somewhere when running
install.sh. You can also directly call
matlab_setup.sh. Make sure to call it using sudo. It needs to set up update-alternatives for GCC and also replace a file in the Matlab installation (as explained below in 'The Hard Way', which you may read if you're interested in why we have to do that).
- Now your system and Matlab should be set up correctly. In
cpm_lib/matlab_MEX_bindingsyou can find a script called
build_MEX.bash. This script can be used to compile all relevant MEX files. Make sure to call it using sudo. Switching the C++ compiler to a supported version, as set in the matlab_setup.sh script, requires sudo privileges. The script switches back to your automatic / default compiler after compiling the MEX files. MEX files are also built using build_all.bash or the cpm lib's build.bash, unless you do not enter a sudo password when prompted to do so, or if Matlab is not installed.
YOU ARE NOT DONE YET!
The Hard Way
You need to compile these files using MEX. This should already be taken care of by the build_MEX.bash here. BUT: You need to make some preparations beforehand.
Before you do so, you need to make sure that, besides Matlab, you have a supported GCC version installed on your system (see https://de.mathworks.com/support/requirements/supported-compilers.html). Also, please note that version 2021a of Matlab was used when developing these files, and that older versions might not be supported.
Install the newest recommended gcc version (and g++ version). Then use update-alternatives to set the current system compiler to these gcc and g++ versions.
Now you should be able to build the readers and writers (..._reader.cpp, ..._writer.cpp). You need to link your cpm lib file as well as the installed fastdds lib files to make the compilation work - just use the commands in build_MEX.bash as an orientation. It is expected that Matlab is installed on your system and that it has been added to your path.
For the readers to work properly, they are started in a new Matlab thread. This thread seems to ignore the set C++ version in LD_PRELOAD (see How to Execute the MEX Files). The execution fails. As a result, you need to implement a workaround:
- (Not working) Set LD_PRELOAD globally as environment variable -> Other programs may crash & this does not work
- (Untested) Use the same "outdated" compiler for MEX and to create your library files (libcpm, libfastdds)
- (Tested, works) Replace Matlabs internal C++ environment with a link to yours (inspired by Stackoverflow):
YOU ARE NOT DONE YET!
Some things need to be done before any execution of Matlab files. You can find out more about this here.