Skip to content

Commit 9175e7c

Browse files
authored
Merge pull request #4 from cyberbotics/fix-start-ros2supervisor-node
Fix missing supervisor node creation and explanation
2 parents 1ea7946 + c8549cb commit 9175e7c

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

source/Tutorials/Advanced/Simulators/Code/robot_launch.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from launch_ros.actions import Node
55
from launch import LaunchDescription
66
from ament_index_python.packages import get_package_share_directory
7-
from webots_ros2_driver.webots_launcher import WebotsLauncher
7+
from webots_ros2_driver.webots_launcher import WebotsLauncher, Ros2SupervisorLauncher
88

99

1010
def generate_launch_description():
@@ -15,10 +15,13 @@ def generate_launch_description():
1515
world=os.path.join(package_dir, 'worlds', 'my_world.wbt')
1616
)
1717

18+
ros2_supervisor = Ros2SupervisorLauncher()
19+
1820
my_robot_driver = Node(
1921
package='webots_ros2_driver',
2022
executable='driver',
2123
output='screen',
24+
additional_env={'WEBOTS_CONTROLLER_URL': 'my_robot'},
2225
parameters=[
2326
{'robot_description': robot_description},
2427
]
@@ -27,6 +30,7 @@ def generate_launch_description():
2730
return LaunchDescription([
2831
webots,
2932
my_robot_driver,
33+
ros2_supervisor,
3034
launch.actions.RegisterEventHandler(
3135
event_handler=launch.event_handlers.OnProcessExit(
3236
target_action=webots,

source/Tutorials/Advanced/Simulators/Code/robot_launch_sensor.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from launch_ros.actions import Node
55
from launch import LaunchDescription
66
from ament_index_python.packages import get_package_share_directory
7-
from webots_ros2_driver.webots_launcher import WebotsLauncher
7+
from webots_ros2_driver.webots_launcher import WebotsLauncher, Ros2SupervisorLauncher
88

99

1010
def generate_launch_description():
@@ -15,10 +15,13 @@ def generate_launch_description():
1515
world=os.path.join(package_dir, 'worlds', 'my_world.wbt')
1616
)
1717

18+
ros2_supervisor = Ros2SupervisorLauncher()
19+
1820
my_robot_driver = Node(
1921
package='webots_ros2_driver',
2022
executable='driver',
2123
output='screen',
24+
additional_env={'WEBOTS_CONTROLLER_URL': 'my_robot'},
2225
parameters=[
2326
{'robot_description': robot_description},
2427
]
@@ -32,6 +35,7 @@ def generate_launch_description():
3235
return LaunchDescription([
3336
webots,
3437
my_robot_driver,
38+
ros2_supervisor,
3539
obstacle_avoider,
3640
launch.actions.RegisterEventHandler(
3741
event_handler=launch.event_handlers.OnProcessExit(

source/Tutorials/Advanced/Simulators/Webots.rst

+15-5
Original file line numberDiff line numberDiff line change
@@ -204,31 +204,41 @@ You have to specify in the constructor which world file the simulator will open.
204204
:dedent: 4
205205
:lines: 14-16
206206

207+
A supervisor Robot is always automatically added to the world file by ``WebotsLauncher``.
208+
This robot is controlled by the custom node ``Ros2Supervisor``, which must also be started using the ``Ros2SupervisorLauncher``.
209+
This node allows to spawn URDF robots directly into the world, and it also publishes useful topics like ``/clock``.
210+
211+
.. literalinclude:: Code/robot_launch.py
212+
:language: python
213+
:dedent: 4
214+
:lines: 18
215+
207216
Then, the ROS node interacting with the simulated robot is created.
208217
This node, named ``driver``, is located in the ``webots_ros2_driver`` package.
209218
The node will be able to communicate with the simulated robot by using a custom protocol based on IPC and shared memory.
210219
In your case, you need to run a single instance of this node, because you have a single robot in the simulation.
211220
But if you had more robots in the simulation, you would have to run one instance of this node per robot.
221+
``WEBOTS_CONTROLLER_URL`` is used to define the name of the robot the driver should connect to.
212222
The ``robot_description`` parameter holds the contents of the URDF file which refers to the ``my_robot_driver.py`` Python plugin.
213223

214224
.. literalinclude:: Code/robot_launch.py
215225
:language: python
216226
:dedent: 4
217-
:lines: 18-25
227+
:lines: 20-28
218228

219-
After that, the two nodes are set to be launched in the ``LaunchDescription`` constructor:
229+
After that, the three nodes are set to be launched in the ``LaunchDescription`` constructor:
220230

221231
.. literalinclude:: Code/robot_launch.py
222232
:language: python
223233
:dedent: 4
224-
:lines: 27-29
234+
:lines: 30-33
225235

226236
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).
227237

228238
.. literalinclude:: Code/robot_launch.py
229239
:language: python
230240
:dedent: 8
231-
:lines: 30-35
241+
:lines: 34-39
232242

233243
6 Modify the setup.py file
234244
^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -351,7 +361,7 @@ Go to the file ``robot_launch.py`` and replace ``def generate_launch_description
351361

352362
.. literalinclude:: Code/robot_launch_sensor.py
353363
:language: python
354-
:lines: 10-42
364+
:lines: 10-46
355365

356366
This will create an ``obstacle_avoider`` node that will be included in the ``LaunchDescription``.
357367

0 commit comments

Comments
 (0)