Skip to content

Commit 8754e15

Browse files
authored
Merge pull request #2 from cyberbotics/enhancement-replace-windows-compatibility-with-wsl
Update Windows compatibility to WSL in tutorials
2 parents 9175e7c + 6b5beab commit 8754e15

File tree

5 files changed

+141
-61
lines changed

5 files changed

+141
-61
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import os
2+
import pathlib
3+
import launch
4+
from launch_ros.actions import Node
5+
from launch import LaunchDescription
6+
from ament_index_python.packages import get_package_share_directory
7+
from webots_ros2_driver.webots_launcher import WebotsLauncher, Ros2SupervisorLauncher
8+
from webots_ros2_driver.utils import get_wsl_ip_address
9+
10+
11+
def generate_launch_description():
12+
package_dir = get_package_share_directory('my_package')
13+
robot_description = pathlib.Path(os.path.join(package_dir, 'resource', 'my_robot.urdf')).read_text()
14+
15+
webots = WebotsLauncher(
16+
world=os.path.join(package_dir, 'worlds', 'my_world.wbt')
17+
)
18+
19+
ros2_supervisor = Ros2SupervisorLauncher()
20+
21+
my_robot_driver = Node(
22+
package='webots_ros2_driver',
23+
executable='driver',
24+
output='screen',
25+
additional_env={'WEBOTS_CONTROLLER_URL': 'tcp://' + get_wsl_ip_address() + ':1234/my_robot'},
26+
parameters=[
27+
{'robot_description': robot_description},
28+
]
29+
)
30+
31+
obstacle_avoider = Node(
32+
package='my_package',
33+
executable='obstacle_avoider',
34+
)
35+
36+
return LaunchDescription([
37+
webots,
38+
my_robot_driver,
39+
ros2_supervisor,
40+
obstacle_avoider,
41+
launch.actions.RegisterEventHandler(
42+
event_handler=launch.event_handlers.OnProcessExit(
43+
target_action=webots,
44+
on_exit=[launch.actions.EmitEvent(event=launch.events.Shutdown())],
45+
)
46+
)
47+
])
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import os
2+
import pathlib
3+
import launch
4+
from launch_ros.actions import Node
5+
from launch import LaunchDescription
6+
from ament_index_python.packages import get_package_share_directory
7+
from webots_ros2_driver.webots_launcher import WebotsLauncher, Ros2SupervisorLauncher
8+
from webots_ros2_driver.utils import get_wsl_ip_address
9+
10+
11+
def generate_launch_description():
12+
package_dir = get_package_share_directory('my_package')
13+
robot_description = pathlib.Path(os.path.join(package_dir, 'resource', 'my_robot.urdf')).read_text()
14+
15+
webots = WebotsLauncher(
16+
world=os.path.join(package_dir, 'worlds', 'my_world.wbt')
17+
)
18+
19+
ros2_supervisor = Ros2SupervisorLauncher()
20+
21+
my_robot_driver = Node(
22+
package='webots_ros2_driver',
23+
executable='driver',
24+
output='screen',
25+
additional_env={'WEBOTS_CONTROLLER_URL': 'tcp://' + get_wsl_ip_address() + ':1234/my_robot'},
26+
parameters=[
27+
{'robot_description': robot_description},
28+
]
29+
)
30+
31+
return LaunchDescription([
32+
webots,
33+
my_robot_driver,
34+
ros2_supervisor,
35+
launch.actions.RegisterEventHandler(
36+
event_handler=launch.event_handlers.OnProcessExit(
37+
target_action=webots,
38+
on_exit=[launch.actions.EmitEvent(event=launch.events.Shutdown())],
39+
)
40+
)
41+
])

source/Tutorials/Advanced/Simulators/Webots.rst

+53-61
Original file line numberDiff line numberDiff line change
@@ -35,32 +35,15 @@ Prerequisites
3535

3636
It is recommended to understand basic ROS principles covered in the beginner :doc:`../../../Tutorials`.
3737
In particular, :doc:`../../Beginner-CLI-Tools/Introducing-Turtlesim/Introducing-Turtlesim`, :doc:`../../Beginner-CLI-Tools/Understanding-ROS2-Topics/Understanding-ROS2-Topics`, :doc:`../../Beginner-Client-Libraries/Creating-A-Workspace/Creating-A-Workspace`, :doc:`../../Beginner-Client-Libraries/Creating-Your-First-ROS2-Package` and :doc:`../../Intermediate/Launch/Creating-Launch-Files` are useful prerequisites.
38-
Finally, you will need to install ``webots_ros2_driver`` from a terminal with this command:
38+
Finally, you will need to install ``webots_ros2_driver`` from a terminal with the following commands.
39+
On Windows, a WSL (Windows Subsystem for Linux) environment must be configured and used to run all the Linux and ROS commands contained in this tutorial.
40+
This `page <https://github.com/cyberbotics/webots_ros2/wiki/Build-and-Install#windows>`_ explains how to setup such installation.
3941

40-
.. tabs::
41-
42-
.. group-tab:: Linux
43-
44-
.. code-block:: console
42+
.. code-block:: console
4543
4644
sudo apt update
4745
sudo apt install ros-{DISTRO}-webots-ros2-driver
48-
49-
.. group-tab:: Windows
50-
51-
.. code-block:: console
52-
53-
# Install webots_ros2_driver and dependencies
54-
cd \ros2_ws
55-
pip install rosinstall_generator
56-
rosinstall_generator webots_ros2_driver --deps --exclude-path=C:\dev\ros2_{DISTRO} > deps.repos
57-
vcs import src < deps.repos
58-
59-
# Build the packages
60-
colcon build
61-
62-
# Source this workspace
63-
call install\local_setup.bat
46+
source /opt/ros/{DISTRO}/setup.bash
6447
6548
.. note::
6649

@@ -193,13 +176,22 @@ In the ``my_package/resource`` folder create a text file named ``my_robot.urdf``
193176
Let's create now the launch file to easily launch the simulation and the ROS controller with a single command.
194177
In the ``my_package/launch`` folder create a new text file named ``robot_launch.py`` with this code:
195178

196-
.. literalinclude:: Code/robot_launch.py
197-
:language: python
179+
.. tabs::
180+
181+
.. group-tab:: Linux
182+
183+
.. literalinclude:: Code/robot_launch_linux.py
184+
:language: python
185+
186+
.. group-tab:: Windows
187+
188+
.. literalinclude:: Code/robot_launch_windows.py
189+
:language: python
198190

199191
The ``WebotsLauncher`` object is a custom action that allows you to start a Webots simulation instance.
200192
You have to specify in the constructor which world file the simulator will open.
201193

202-
.. literalinclude:: Code/robot_launch.py
194+
.. literalinclude:: Code/robot_launch_linux.py
203195
:language: python
204196
:dedent: 4
205197
:lines: 14-16
@@ -208,7 +200,7 @@ A supervisor Robot is always automatically added to the world file by ``WebotsLa
208200
This robot is controlled by the custom node ``Ros2Supervisor``, which must also be started using the ``Ros2SupervisorLauncher``.
209201
This node allows to spawn URDF robots directly into the world, and it also publishes useful topics like ``/clock``.
210202

211-
.. literalinclude:: Code/robot_launch.py
203+
.. literalinclude:: Code/robot_launch_linux.py
212204
:language: python
213205
:dedent: 4
214206
:lines: 18
@@ -219,23 +211,37 @@ The node will be able to communicate with the simulated robot by using a custom
219211
In your case, you need to run a single instance of this node, because you have a single robot in the simulation.
220212
But if you had more robots in the simulation, you would have to run one instance of this node per robot.
221213
``WEBOTS_CONTROLLER_URL`` is used to define the name of the robot the driver should connect to.
214+
On Windows, the communication between Webots (running in Windows) and the controllers (running in WSL) goes through a TCP connection.
215+
The IP address is automatically retrieved by ``webots_ros2_driver``.
216+
The default Webots port number is 1234.
222217
The ``robot_description`` parameter holds the contents of the URDF file which refers to the ``my_robot_driver.py`` Python plugin.
223218

224-
.. literalinclude:: Code/robot_launch.py
225-
:language: python
226-
:dedent: 4
227-
:lines: 20-28
219+
.. tabs::
220+
221+
.. group-tab:: Linux
222+
223+
.. literalinclude:: Code/robot_launch_linux.py
224+
:language: python
225+
:dedent: 4
226+
:lines: 20-28
227+
228+
.. group-tab:: Windows
229+
230+
.. literalinclude:: Code/robot_launch_windows.py
231+
:language: python
232+
:dedent: 4
233+
:lines: 21-29
228234

229235
After that, the three nodes are set to be launched in the ``LaunchDescription`` constructor:
230236

231-
.. literalinclude:: Code/robot_launch.py
237+
.. literalinclude:: Code/robot_launch_linux.py
232238
:language: python
233239
:dedent: 4
234240
:lines: 30-33
235241

236242
Finally, an optional part is added in order to shutdown all the nodes once Webots terminates (e.g., when it gets closed from the graphical user interface).
237243

238-
.. literalinclude:: Code/robot_launch.py
244+
.. literalinclude:: Code/robot_launch_linux.py
239245
:language: python
240246
:dedent: 8
241247
:lines: 34-39
@@ -256,24 +262,12 @@ This sets-up the package and adds in the ``data_files`` variable the newly added
256262

257263
From a terminal in your ROS2 workspace run:
258264

259-
.. tabs::
260-
261-
.. group-tab:: Linux
262-
263-
.. code-block:: console
265+
.. code-block:: console
264266
265267
colcon build
266268
source install/local_setup.bash
267269
ros2 launch my_package robot_launch.py
268270
269-
.. group-tab:: Windows
270-
271-
.. code-block:: console
272-
273-
colcon build
274-
call install\local_setup.bat
275-
ros2 launch my_package robot_launch.py
276-
277271
This will launch the simulation.
278272
Webots will be automatically installed on the first run in case it was not already installed.
279273

@@ -359,9 +353,19 @@ This will add an entry point for the ``obstacle_avoider`` node.
359353

360354
Go to the file ``robot_launch.py`` and replace ``def generate_launch_description():`` with:
361355

362-
.. literalinclude:: Code/robot_launch_sensor.py
363-
:language: python
364-
:lines: 10-46
356+
.. tabs::
357+
358+
.. group-tab:: Linux
359+
360+
.. literalinclude:: Code/robot_launch_sensor_linux.py
361+
:language: python
362+
:lines: 10-46
363+
364+
.. group-tab:: Windows
365+
366+
.. literalinclude:: Code/robot_launch_sensor_windows.py
367+
:language: python
368+
:lines: 11-47
365369

366370
This will create an ``obstacle_avoider`` node that will be included in the ``LaunchDescription``.
367371

@@ -370,24 +374,12 @@ This will create an ``obstacle_avoider`` node that will be included in the ``Lau
370374

371375
As in task ``7``, launch the simulation from a terminal in your ROS 2 workspace:
372376

373-
.. tabs::
374-
375-
.. group-tab:: Linux
376-
377-
.. code-block:: console
377+
.. code-block:: console
378378
379379
colcon build
380380
source install/local_setup.bash
381381
ros2 launch my_package robot_launch.py
382382
383-
.. group-tab:: Windows
384-
385-
.. code-block:: console
386-
387-
colcon build
388-
call install\local_setup.bat
389-
ros2 launch my_package robot_launch.py
390-
391383
Your robot should go forward and before hitting the wall it should turn clockwise.
392384
You can press ``Ctrl+F10`` in Webots or go to the ``View`` menu, ``Optional Rendering`` and ``Show DistanceSensor Rays`` to display the range of the distance sensors of the robot.
393385

0 commit comments

Comments
 (0)