MATLAB: How to configure ROS2 for MATLAB R2020a on Ubuntu 18.04

Robotics System ToolboxROS Toolbox

I want to be able to work with ROS2 and custom messages in ROS Toolbox R2020a running on Ubuntu 18.04, how do I set this up exactly?

Best Answer

Installation
The following instructions are based on an Ubuntu 18.04 standard desktop installation if you have a 'server' or 'minimal' installation you may need to manually install additional dependencies.
First install ROS2 Dashing according to the instructions on the ROS website:
This should install most of the ROS2 tools and dependencies which you need.
Secondly ROS Toolbox R2020a requires Python 3.7 (for custom message support), this will not have been installed by the previous step. To install Python 3.7 and the packages we require execute:
sudo apt install python3.7 python3.7-venv libpython3.7
Further, ROS Toolbox by default assumes the Python 3.7 libraries will have been installed in /usr/lib but the Ubuntu 18.04 packages will install them in /usr/lib/x86_64-linux-gnu. To allow MATLAB to find the correct libraries create the following two symlinks:
sudo ln -s /usr/lib/x86_64-linux-gnu/libpython3.7m.so.1 /usr/lib/libpython3.7m.so.1
sudo ln -s /usr/lib/x86_64-linux-gnu/libpython3.7m.so.1.0 /usr/lib/libpython3.7m.so.1.0
Troubleshooting
1. If you tried using functions like "ros2genmsg" before your environment had been configured entirely you may need to delete the partial virtual environment which has been created, otherwise you may keep running into issues, even after installing the missing dependencies. For example you might keep receiving:
Error using ros.ros2.internal.createOrGetLocalPython (line 128)
chmod: cannot access '/home/user/.matlab/R2020a/ros2/python_venv/bin/activate': No such file or directory
Error in ros2genmsg (line 53)
ros.ros2.internal.createOrGetLocalPython(); %ensure python is available

if you tried using ROS Toolbox functionality before having installed python3.7-venv, even after you then later did install this package. To delete the partial virtual environment use:
rm -rf ~/.matlab/R2020a/ros2
2. If you receive the following error:
Error using ros.ros2.internal.createOrGetLocalPython (line 186)
Unknown error occurred.
Error in ros2genmsg (line 53)
ros.ros2.internal.createOrGetLocalPython(); %ensure python is available
as a first step please double check whether you have correctly created the two symlinks mentioned above. If these have been created and are valid and you still receive this error, please contact support.
Related Question