Simulation in ROS

Simulating robotic systems is crucial before doing real experiments. It exsits several simulators:

In this tutorial, we will use two of them: Stage (2d) and Gazebo (3d).

Stage Simulator

Installing Stage

Here the commands:

sudo apt install -y git cmake g++ libjpeg8-dev libpng-dev libglu1-mesa-dev libltdl-dev libfltk1.1-dev
mkdir pkg-stage
cd pkg-stage
git clone --branch ros2 https://github.com/tuw-robotics/Stage.git
git clone --branch humble https://github.com/tuw-robotics/stage_ros2.git
cd ..
colcon build --cmake-args -DOpenGL_GL_PREFERENCE=LEGACY
colcon build --packages-select stage_ros2

First Launch & Exercices

ros2 launch stage_ros2 stage.launch.py world:=cave

Note: by pressing 'r' you can see a 3d view of the scene in stage.

Question: What are the topics published by stage?

Exercise: Launch rviz2 and display the robot position and its laser scan data

Teleoperate the robot using the keyboard by running:

ros2 run teleop_twist_keyboard teleop_twist_keyboard

Ensure that the robot is correctly moving in stage and that the laser data are correctly displayed in rviz2.

Question: Why the robot is moving in the simulator?

Gazebo Simulator

Gazebo is a 3D simulator. It makes it possible to rapidly test algorithms, design robots, perform regression testing, and train AI systems using realistic scenarios. Gazebo is integrated with ROS and supports various robots out of the box.

Gazebo is heavily used by the DARPA challenges (cf. Wikipedia). You can see videos online (example) and even load the maps and robot model that are available.

Gazebo Installation

Verify that Gazebo is installed using:

dpkg -l | grep gazebo

You should have at least the following packages:

ii  gazebo                                          11.10.2+dfsg-1                          amd64        Open Source Robotics Simulator - Binaries
ii  gazebo-common                                   11.10.2+dfsg-1                          all          Open Source Robotics Simulator - Shared files
ii  gazebo-plugin-base                              11.10.2+dfsg-1                          amd64        Open Source Robotics Simulator - base plug-ins
ii  libgazebo-dev                                   11.10.2+dfsg-1                          amd64        Open Source Robotics Simulator - Development Files
ii  libgazebo11:amd64                               11.10.2+dfsg-1                          amd64        Open Source Robotics Simulator - shared library
ii  ros-iron-gazebo-dev                             3.7.0-3jammy.20230622.191804            amd64        Provides a cmake config for the default version of Gazebo for the ROS distribution.
ii  ros-iron-gazebo-msgs                            3.7.0-3jammy.20231117.090251            amd64        Message and service data structures for interacting with Gazebo from ROS2.
ii  ros-iron-gazebo-plugins                         3.7.0-3jammy.20231117.111548            amd64        Robot-independent Gazebo plugins for sensors, motors and dynamic reconfigurable components.
ii  ros-iron-gazebo-ros                             3.7.0-3jammy.20231117.104944            amd64        Utilities to interface with Gazebo through ROS.
ii  ros-iron-gazebo-ros-pkgs                        3.7.0-3jammy.20231117.114324            amd64        Interface for using ROS with the Gazebo simulator.
ii  ros-iron-turtlebot3-gazebo                      2.2.5-4jammy.20231117.114359            amd64        Gazebo simulation package for the TurtleBot3

Install missing packages using:

sudo apt install <pakage_name>

Launch your first Gazebo Simulation

We propose some configuration into a pkg-tsim project, including a tbot_sim ROS2 package.

cd ~/ros_space
git clone https://github.com/imt-mobisyst/pkg-tsim
colcon build
source ./install/setup.bash

Then, you can launch a preconfigured simulation:

ros2 launch tbot_sim challenge-1.launch.py

Look at the content of this launch file here. We can see that Gazebo/ROS supports loading a world file describing the simulation environment and spawn elements such as robots. This simulation spawns a robot configured like a tbot i.e. it is equipped with a laser range finder and a camera (kinect). The interaction with the simulation will operate through ROS topics as it would be with a real robot with real equipment.

Quiz on challenge 1

While the challenge 1 simulation is running:

Question: which topics are available i.e published by Gazebo?

Hint: an infinite loop safely` is a versatile and powerful tool to display data published in topics. Launch it:

rviz2

Question: How to configure rviz2 to visualize the laser scans?

Be careful, ensure that Global Option / Fixed frame is correctly set to base_link.

Question: why is this important? (hint: check your tf using ros2 run tf2_tools view_frames)

You can also display the tf in rviz2 directly.

Question: How to visualize camera images using rqt?

rviz2

Controlling the Simulated Robot

Launch a simple node to control the simulated robot using keyboard:

ros2 run teleop_twist_keyboard teleop_twist_keyboard

tuto_sim

Create a launch file that starts the appropriate configuration: the challenge-1, a configured rviz2 displaying laser scans and the teleop. We will prefer YAML format for launch file.

All the information you need are in the tutorials : - Launch file documentation - Launch examples

Create $ROS_WORKSPACE/pkg-tsim/tbot_sim/launch/tutosim_launch.yaml Add this code into this file to:

launch:

- include:
    file: "$(find-pkg-share tbot_sim)/launch/challenge-1.launch.py"

- node:
    pkg: "rviz2"
    exec: "rviz2"
    name: "rviz2"

- executable:
    cmd: gnome-terminal --tab -e 'ros2 run  teleop_twist_keyboard teleop_twist_keyboard'

Exercise: modify this launch file so that rviz loads a saved configuration file when it starts. This configuration file should add the laser data, ...