Skip to main content

Webots: ROSbot 2R + SLAM Toolbox

About

Webots is a popular simulation engine for ROS 2 projects. This guide demonstrates how to run the ROSbot simulation in Webots and create a map of the unknown world using the Slam Toolbox. The final result looks like this:

Quick Start

Prerequisites

Install Docker Engine (+ Compose) by using the official guide. The project was tested on:

$ docker compose version
Docker Compose version v2.12.2
$
$ docker --version
Docker version 20.10.21, build baeda1f

The compose.sim.webots.yaml file uses NVIDIA Container Runtime. Make sure you have NVIDIA GPU and the NVIDIA Container Toolkit installed.

Clone the repository:

git clone https://github.com/husarion/rosbot-mapping
cd rosbot-mapping

Start the containers in a new terminal:

xhost +local:docker && \
docker compose -f compose.sim.webots.yaml up

And in the second terminal start telop-twist-keyboard for manual ROSbot XL control:

docker exec -it rviz bash

And inside the running container shell execute:

ros2 run teleop_twist_keyboard teleop_twist_keyboard

Source code

The full source code can be found here and it allows you to start SLAM Toolbox on ROSbot in the following configurations:

ConfigurationCompose FilesAdditional Requirements
Physical robot and PC in the same LANcompose.rosbot.yaml + compose.pc.yaml-
Physical robot and PC in different networks (VPN)compose.rosbot.yaml + compose.pc.yamlHusarnet VPN configured on ROSbot and PC and DDS_CONFIG environment variable set to HUSARNET_SIMPLE_AUTO
Webots simulationcompose.sim.webots.yaml-

Let's focus on the Webots simulation:

compose.sim.webots.yaml
x-net-config:
&net-config
network_mode: host
ipc: host
env_file: net.env

x-gpu-config:
&gpu-config
runtime: nvidia
environment:
- DISPLAY=${DISPLAY:?err}
- NVIDIA_VISIBLE_DEVICES=all
- NVIDIA_DRIVER_CAPABILITIES=all

services:

rviz:
image: husarion/rviz2:humble
<<: [ *net-config, *gpu-config ]
container_name: rviz
volumes:
- /tmp/.X11-unix:/tmp/.X11-unix:rw
- ./config/rosbot.rviz:/root/.rviz2/default.rviz

rosbot:
image: husarion/webots:humble
<<: [ *net-config, *gpu-config ]
volumes:
- /tmp/.X11-unix:/tmp/.X11-unix:rw
command: ros2 launch webots_ros2_husarion robot_launch.py robot_name:=rosbot

mapping:
image: husarion/slam-toolbox:humble
<<: *net-config
volumes:
- ./config/slam_toolbox_webots.yaml:/slam_params.yaml
command: >
ros2 launch slam_toolbox online_sync_launch.py
slam_params_file:=/slam_params.yaml
use_sim_time:=True

map-saver:
image: husarion/nav2-map-server:humble
<<: *net-config
volumes:
- ./maps:/maps
command: bash -c "while true; do ros2 run nav2_map_server map_saver_cli --free 0.15 --fmt png -f /maps/map; sleep 5; done"

There are 3 Docker services:

  • rviz which starts the RViz2 ROS 2 Visualisation tool. The source code for building that Docker image is here
  • rosbot which starts the ROSbot 2R Webots simulation in the Husarion world. The source code for building that Docker image is here
  • mapping is a Dockerized SLAM Toolbox package.

Additionally, config files are bind mounted to rviz and mapping services that are available in the project repository.

Summary

And that's all! If you would like to run your own ROS 2 code with Webots, all you need to do is to build your own, minimal Docker image running only your own ROS 2 node. You can simply reuse the husarion/webots Docker image that contains launch files for Husarion robots.