December 24, 2024

Install Gazebo in ROS2 (Iron Irwini) and Run Test Example: Differential Drive Robot

In this ROS2 and Gazebo tutorial we explain how to perform three important tasks:

  1. Install Gazebo in ROS2 (Iron Irwini).
  2. Test the installation by running a test example. The test example is a differential drive robot.
  3. Control the robot by sending velocity commands. The velocity commands are sent by using the Linux command line. Also, we explain how to listen to the robot’s odometry.

The YouTube tutorial accompanying this post is given below.

STEP1: Check the Linux and ROS2 distributions

In order to run the example from this tutorial, you need to have the correct Linux and ROS2 distributions. We are using Linux Ubuntu 22.04 and ROS2 Iron Irwini distributions. We strongly suggest you to install this combination of Linux-ROS2 distributions. In the tutorial given here, we explained how to install the ROS2 Iron Irvini distribution on a Linux Ubuntu machine. However, with small modifications, the example from this tutorial can be executed in other ROS2 distributions.

To check the Linux version (distributions), open a new terminal, and type this:

cat /etc/os-release

The output should look like this:

PRETTY_NAME="Ubuntu 22.04.3 LTS"
NAME="Ubuntu"
VERSION_ID="22.04"
VERSION="22.04.3 LTS (Jammy Jellyfish)"
VERSION_CODENAME=jammy
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=jammy

Next, let us check the ROS2 version

source /opt/ros/iron/setup.bash
printenv ROS_DISTRO

As the output, we get

iron

This means that our current robot version is ROS2 Iron Irwini.

The tutorial presented in the sequel should work also for other ROS2 distributions. However, instead of “iron”, you should type the name of your distribution in the packages that explicitly require the ROS2 distribution name.

STEP2: Install Gazebo

Before you run any ROS2 node or a package, you need to source the environment. We do that by sourcing this setup ROS2 file:

source /opt/ros/iron/setup.bash

To install Gazebo, we run

sudo apt install ros-iron-gazebo-ros-pkgs

Note that if you are using some other version of ROS2, “iron” in the above command should be replaced by the version you have.

Next, let us install Gazebo ROS2 core tools

sudo apt install ros-iron-ros-core ros-iron-geometry2

Let us now double-check if we can run Gazebo. In the terminal, type

gazebo

After this command, the Gazebo main window will open.

STEP 3: Run Gazebo Demo Example: Differential Drive Robot

Let us now run a Gazebo demo. We will run a Gazebo world file in order to test everything. The Gazebo world files contain ROS2 plugins. Gazebo world files are given in the folder “worlds” with this absolute path:

/opt/ros/iron/share/gazebo_plugins/worlds/

Let us investigate all the Gazebo world files

cd /opt/ros/iron/share/gazebo_plugins/worlds/
ls -l

You should see all the world files. In our case, the files are

total 196
-rw-r--r-- 1 root root 17250 Apr 20  2023 gazebo_ros_ackermann_drive_demo.world
-rw-r--r-- 1 root root  3266 Apr 20  2023 gazebo_ros_bumper_demo.world
-rw-r--r-- 1 root root  4706 Apr 20  2023 gazebo_ros_camera_demo.world
-rw-r--r-- 1 root root  3981 Apr 20  2023 gazebo_ros_camera_distortion_barrel_demo.world
-rw-r--r-- 1 root root  4004 Apr 20  2023 gazebo_ros_camera_distortion_pincushion_demo.world
-rw-r--r-- 1 root root  4343 Apr 20  2023 gazebo_ros_camera_triggered_demo.world
-rw-r--r-- 1 root root  5086 Apr 20  2023 gazebo_ros_depth_camera_demo.world
-rw-r--r-- 1 root root  6440 Apr 20  2023 gazebo_ros_diff_drive_demo.world
-rw-r--r-- 1 root root  7625 Apr 20  2023 gazebo_ros_elevator_demo.world
-rw-r--r-- 1 root root  2165 Apr 20  2023 gazebo_ros_force_demo.world
-rw-r--r-- 1 root root  6157 Apr 20  2023 gazebo_ros_ft_sensor_demo.world
-rw-r--r-- 1 root root  1975 Apr 20  2023 gazebo_ros_gps_sensor_demo.world
-rw-r--r-- 1 root root  1097 Apr 20  2023 gazebo_ros_hand_of_god_demo.world
-rw-r--r-- 1 root root  3019 Apr 20  2023 gazebo_ros_harness_demo.world
-rw-r--r-- 1 root root   852 Apr 20  2023 gazebo_ros_joint_pose_trajectory_demo.world
-rw-r--r-- 1 root root   802 Apr 20  2023 gazebo_ros_joint_state_publisher_demo.world
-rw-r--r-- 1 root root  5753 Apr 20  2023 gazebo_ros_multi_camera_demo.world
-rw-r--r-- 1 root root  5682 Apr 20  2023 gazebo_ros_multicamera_demo.world
-rw-r--r-- 1 root root  1637 Apr 20  2023 gazebo_ros_p3d_demo.world
-rw-r--r-- 1 root root  1720 Apr 20  2023 gazebo_ros_planar_move_demo.world
-rw-r--r-- 1 root root  1674 Apr 20  2023 gazebo_ros_projector_demo.world
-rw-r--r-- 1 root root  7095 Apr 20  2023 gazebo_ros_ray_sensor_demo.world
-rw-r--r-- 1 root root 10085 Apr 20  2023 gazebo_ros_skid_steer_drive_demo.world
-rw-r--r-- 1 root root   362 Apr 20  2023 gazebo_ros_template_demo.world
-rw-r--r-- 1 root root 13749 Apr 20  2023 gazebo_ros_tricycle_drive_demo.world
-rw-r--r-- 1 root root  4269 Apr 20  2023 gazebo_ros_vacuum_gripper_demo.world
-rw-r--r-- 1 root root  4422 Apr 20  2023 gazebo_ros_video_demo.world
-rw-r--r-- 1 root root  4722 Apr 20  2023 gazebo_ros_wheel_slip_demo.world

Let us load one of these files in Gazebo. We load the Gazebo world file implementing the differential drive robot:

gazebo --verbose /opt/ros/iron/share/gazebo_plugins/worlds/gazebo_ros_diff_drive_demo.world

Keep the Gazebo window open. Next, open another Linux terminal, and let us use this terminal to inspect the source file of the differential drive robot. In the new terminal type:


gedit /opt/ros/iron/share/gazebo_plugins/worlds/gazebo_ros_diff_drive_demo.world

You can close this file after you inspect it. Next, let us move the robot. For that purpose, open a new terminal by keeping the original terminal that is running Gazebo open. First, let us source the environment

source /opt/ros/iron/setup.bash

Then let us list the topics

ros2 topic list

We get the following output

/clock
/demo/cmd_demo
/demo/odom_demo
/parameter_events
/rosout
/tf

These are all active topics. For us, the most important topics are “/demo/cmd_demo” and “/demo/odom_demo”. The topic “/demo/cmd_demo” is used to send velocity commands to Gazebo. On the other hand, the topic “/demo/odom_demo” is used to communicate the odometry information. The odometry information consists of the position, orientation, linear velocity, and angular velocity. Let us send the velocity command message to the topic “/demo/cmd_demo”. To move the robot, we type the following command

ros2 topic pub /demo/cmd_demo geometry_msgs/Twist '{linear: {x: 1.0}}' -1

We sent the message of the type “geometry_msgs/Twist” to the topic “/demo/cmd_demo “. The message that we send is

'{linear: {x: 1.0}}' -1

The message consists of the linear velocity in the x direction with the magnitude of 1.0. To stop the robot, we type

ros2 topic pub /demo/cmd_demo geometry_msgs/Twist '{linear: {x: 0}}' -1

To reverse the robot direction, we type

ros2 topic pub /demo/cmd_demo geometry_msgs/Twist '{linear: {x: -1}}' -1

Next, let us learn how to listen to the robot odometry. Open a new terminal while keeping all the other terminals open, and type

source /opt/ros/iron/setup.bash
ros2 topic echo /demo/odom_demo

This will print (echo) the odometry information in the terminal.