Skip to main content

ROS2 Gazebo Robot Simulation

TST, Hongkong

Excerpt from ROS 2 Documentation (IRON): The Robot Operating System (ROS) is a set of software libraries and tools for building robot applications. From drivers and state-of-the-art algorithms to powerful developer tools, ROS has the open source tools you need for your next robotics project.

Launch the Gazebo Simulation​

Use one of the worlds defined in the Gazebo examples called visualize_lidar.sdf to simulate a simple diff drive robot:

ign gazebo -v 4 -r visualize_lidar.sdf

This command will automatically download all dependencies and start the Gazebo UI:

ROS2 Gazebo Robot Simulation

When the simulation is running you can check the topics provided by Gazebo with the ign command line tool:

ign list -l

/clock
/gazebo/resource_paths
/gui/camera/pose
/gui/record_video/stats
/model/vehicle_blue/odometry
/model/vehicle_blue/tf
/stats
/world/visualize_lidar_world/clock
/world/visualize_lidar_world/dynamic_pose/info
/world/visualize_lidar_world/pose/info
/world/visualize_lidar_world/scene/deletion
/world/visualize_lidar_world/scene/info
/world/visualize_lidar_world/state
/world/visualize_lidar_world/stats

Configure ROS2​

To be able to communicate our simulation with ROS 2 you need to use a package called ros_gz_bridge. This package provides a network bridge which enables the exchange of messages between ROS 2 and Gazebo Transport:

sudo apt install ros-iron-ros-ign-bridge

At this point you are ready to launch a bridge from ROS to Gazebo. In particular you are going to create a bridge for the topic /model/vehicle_blue/cmd_vel:

ros2 run ros_gz_bridge parameter_bridge /model/vehicle_blue/cmd_vel@geometry_msgs/msg/Twist]ignition.msgs.Twist

Once the bridge is running the robot is able to follow your motor commands. There are two options:

Publish an Topic Update​

Send a command to the topic using ros2 topic pub:

ros2 topic pub /model/vehicle_blue/cmd_vel geometry_msgs/Twist "linear: { x: 0.1 }"

This will make the blue robot start to translate forward:

ROS2 Gazebo Robot Simulation

Use the following command to drive the robot back to the starting position:

ros2 topic pub /model/vehicle_blue/cmd_vel geometry_msgs/Twist "linear: { x: -0.1 }"

Use the teleop_twist_keyboard Package​

The teleop_twist_keyboard node takes keypresses from the keyboard and publishes them as Twist messages. You can install it typing:

sudo apt install ros-iron-teleop-twist-keyboard

The default topic where teleop_twist_keyboard is publishing Twist messages is /cmd_vel but you can remap this topic to make use of the topic used in the bridge:

ros2 run teleop_twist_keyboard teleop_twist_keyboard --ros-args -r /cmd_vel:=/model/vehicle_blue/cmd_vel


This node takes keypresses from the keyboard and publishes them
as Twist messages. It works best with a US keyboard layout.
---------------------------
Moving around:
u i o
j k l
m , .

For Holonomic mode (strafing), hold down the shift key:
---------------------------
U I O
J K L
M < >

t : up (+z)
b : down (-z)

anything else : stop

q/z : increase/decrease max speeds by 10%
w/x : increase/decrease only linear speed by 10%
e/c : increase/decrease only angular speed by 10%

CTRL-C to quit

currently: speed 0.5 turn 1.0

Visualizing Lidar Data in ROS2​

The diff drive robot has a lidar. To send the data generated by Gazebo to ROS2, you need to launch another bridge. In the case the data from the lidar is provided in the Gazebo Transport topic /lidar2, which you are going to remap in the bridge. This topic will be available under the topic /lidar_scan:

ros2 run ros_gz_bridge parameter_bridge /lidar2@sensor_msgs/msg/LaserScan[ignition.msgs.LaserScan --ros-args -r /lidar2:=/laser_scan

To visualize the data from the lidar in ROS2 you can use Rviz2:

rviz2

Here you need to Set the fixed frame to vehicle_blue/lidar_link/gpu_lidar and click the Add button to add the LaserScan to visualize the lidar:

ROS2 Gazebo Robot Simulation

ROS2 Gazebo Robot Simulation