Communicate with ROS in WSL2

88 次查看(过去 30 天)
I am using MATLAB 2020a on Windows 10, v2004 with WSL2 Ubuntu 20.04.
I am having trouble communicating between Ubuntu using WSL and Matlab on Windows. I build ROS nodes on both Ubuntu and Matlab.
TRYING TO GO FROM WSL2 UBUNTU TO WINDOWS:
I ran the basic publisher (talker.py) in Ubuntu. Within WSL2 Ubuntu, I have no issues seeing /talker.
In Matlab, I run
rosinit % run once only
rostopic list
% OUTPUT:
% /chatter
% /rosout
% /rosout_agg
python_talker = rossubscriber('/chatter','std_msgs/String');
python_talker.LatestMessage.Data
but the /chatter message is empty.
TRYING TO GO FROM WINDOWS TO WSL2 UBUNTU:
Similarly, if I start in Matlab with
matlab_talker = rospublisher('/matlab_chatter','std_msgs/Float64MultiArray');
matlab_message = rosmessage( matlab_talker );
tic
while toc < 10
matlab_message.Data = [toc , 1 , 2 , 3 , 4 ] ;
pause(0.1)
send( matlab_talker , matlab_message )
end
I can see that the /matlab_chatter node exists in Ubuntu, but it's also empty:
$ rostopic list
% OUTPUT:
% /chatter
% /matlab_chatter
% /rosout
% /rosout_agg
$ rostopic echo /matlab_chatter
% no output
This all works if I'm running pure Ubuntu. Any ideas on troubleshooting WSL2 inter-connectivity?
  2 个评论
Cam Salzberger
Cam Salzberger 2020-11-11
编辑:Cam Salzberger 2020-11-11
The most common cause I've seen for introspection being able to see topics, but messages not going through, is not having ROS_IP set correctly in all terminals on the non-MATLAB side of communication. See here for more details.
That said, I've never tested MATLAB with (or even used) WSL or WSL2, and I'm not sure that functionality can be guaranteed to work in that environment.
Another semi-common cause of lack of communication is a firewall on Windows. This can sometimes be resolved by whitelisting MATLAB for that firewall, or by whitelisting the range of ports used for ROS communication. Again, though, I'm not sure how WSL2 handles firewalls or if it even has one.
There have been other people with issues communicating through WSL2 with native ROS. The suggested possible cause there is that having the Windows environment share a hostname and/or IP with the Ubuntu environment could cause issues in communication. That sounds similar to using a VM in "NAT" mode, where the VM and the host machine share an IP. If you can configure your WSL2 environment like you can with a VM, you could try the equivalent of a "Bridged" connection, where the VM has its own address on the network.
-Cam
Steven Crews
Steven Crews 2020-11-18
Thanks, Cam. I'm looking into these options. I hope it is an easy setting somewhere.

请先登录,再进行评论。

采纳的回答

MathWorks Robotics and Autonomous Systems Team
Hello Steven,
To communicate between WSL2 ROS and MATLAB/Simulink, you would need to make sure the environment variable "ROS_IP" being set correctly on both MATLAB and WSL2 sides. Otherwise, it might ended up getting only part of working functionalities. Below are steps to get this work. Feel free to skip directly to Step 3 if you already have WSL2 enabled and ROS Noetic installed on WSL2.
Step 1: Enable WSL2 on Windows
You may follow this offical link to install WSL2 on windows, I would recommand using Ubuntu20.04 and ROS Noetic if you would like to use Gazebo in your project later on.
Step 2: Install ROS/ROS 2 on WSL2
You may follow this ROS tutorial to install ROS Noetic on WSL2. You may install ROS 2 following this tutorial instead.
Step 3: Configure Environment Variables
To make sure specific URI is known for both side of ROS (ROS NetworkSetup wiki), you can either use the automatic configuration function or set it up by yourself.
Using function:
(Note that you need to enable ssh on WSL2 for running this script. Refer to this article on enabling ssh on WSL2.)
1. Type in "$ifconfig" on WSL2 Terminal, look for "inet" under "eth0". This will be your WSL2 IP Address.
2. Download configureWSL2.m for attachment and launch MATLAB.
3. Call "configureWSL2" with IP Address, Username, Password of your WSL2 as input. This function will setup all required environment variables for you. Note that it assumes you have installed ROS Noetic on Ubuntu20.04. If you installed different version of ROS or installed it in another directory other than the default directory, you may need to modify the "ROSPath" variable in the function.
Manually set it up:
1. Type in "$ifconfig" on WSL2 Terminal, look for "inet" under "eth0". This will be your WSL2 IP Address. Set this on WSL2 side (e.g. $ export ROS_IP=xxx.xxx.xxx.xxx)
2. Type in ">ipconfig" on Windows Command Prompt and find IPv4 Address under "Ethernet adapter vEthernet (WSL)". Set this on MATLAB side (e.g. >> setenv('ROS_IP','xxx.xxx.xxx.xxx'))
Step 4: Visualization with Gazebo and Rviz
To run GUI applications such as Gazebo and Rviz and bring them to your windows desktop, you need to install X server on Windows, such as VcXsrc.
If you already run "configureWSL2.m" in Step 3, all you need to do is launch "Xlaunch" (VcXsrv) and type in "$gazebo" on WSL2 terminal.
As an alternative, you may also set environment variables manually referring to this video.
Hope this helps,
MathWorks Robotics and Autonomous Systems Team
  1 个评论
Haasith Pasala
Haasith Pasala 2022-3-5
After running "configureWSL2.m", when I launch gazebo it's not responding. Please find the attachment. As you see here, the real timee factor, sim time all are blank.
In Ubuntu 20.04, Ros Noetic on WSL2 in windows 11. Matlab version R2021b

请先登录,再进行评论。

更多回答(1 个)

Nupur
Nupur 2023-11-28
I am trying to communicate the ROS2 node which is in container and the Matlab which is in Host machine. However, Matlab has not been able to see the topics from the container.
I have ROS2 also installed in Host machine and the communication works well between ROS2 and Matlab.
I dont understand the reason behind it.
  • Matlab : R2022b
  • ROS2 : Humble
  • Host Machine: Ubuntu 22.04
Both the container and the Matlab running in the same Host machine.
I have tried: Maintained the ROS_DOMAIN_ID, ROS_MASTER_URI same throughtout ROS2 node and Matlab.
Installed Matlab in container and tried to communicate but didn't work.
  2 个评论
Edward Young
Edward Young 2024-2-17
Did you figure out how to do it?
Nupur
Nupur 2024-2-19
编辑:Nupur 2024-2-19
@Edward Young I have solved the task by creating a subscriber/publisher node for the Container's ROS2 topic in my Host Machine and subscribing again from Matlab.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 ROS Network Access in Simulink 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by