Skip to main content

Gazebo Simulation

Spawning our Robot in Gazebo

Updated 4 min readMarkdown
Part of seriesSimulating and Building a Mobile Robot
Gazebo Simulation

Introduction

The new Gazebo simulator builds on the legacy of Gazebo Classic but introduces key changes that affect how ROS 2 projects interact with it.

  • ros_gz vs. gazebo_ros_pkgs: ROS 2 now uses the ros_gz package instead of gazebo_ros_pkgs for launch files and utilities.

  • Bridging vs. plugins: In Gazebo Classic, gazebo_ros_pkgs provided plugins that ran directly inside the simulator to connect ROS and Gazebo. In the new system, ros_gz mainly acts as a bridge between ROS topics and gz-transport topics.

Understanding these differences will help you transition smoothly to the updated workflow.

Creating a Launch File

To avoid manually starting and stopping multiple programs each time we make a change, we’ll create a single launch file to run everything at once.

  1. Inside your launch/ directory, create a file called robot.launch.py.

  2. Paste in the provided code in the repository and update the package name to match your own.

This file does three things:

  • Includes your own rsp.launch.py and forces use_sim_time to true.

  • Includes the Gazebo launch file from the ros_gz package.

  • Runs the entity-spawn node from ros_gz.

Rebuild your workspace, close any running instances, and launch this file. Your robot should appear in Gazebo as before - only now the process is much simpler.

cd ros2_ws
colcon build --packages-select diffbot_tut --symlink-install
source install/setup.bash
ros2 launch diffbot_tut robot.launch.py

Tip: When you close Gazebo, stop the launch script manually with Ctrl + C.

Adding Gazebo Tags

To refine our simulation, we can embed <gazebo> tags directly in the URDF. For example, to reduce friction on a caster wheel so it can spin freely:

<gazebo reference="castor_wheel">
    <mu1>0.000001</mu1>
    <mu2>0.000001</mu2>
</gazebo>

Make sure your wheels already have the necessary <gazebo> tags from earlier steps.

Controlling the Robot in Gazebo

Eventually, we’ll use the ros2_control library to manage our robot’s motors. The advantage of ros2_control is that the same code works for both simulation and real hardware.

For now, we’ll keep things simple by using Gazebo’s built-in differential-drive control plugin to illustrate key concepts.

How Control Works

A physical robot’s control system converts a desired velocity into motor commands, measures the motors’ actual speed, and estimates the robot’s position.

In ROS, velocity commands are published on the /cmd_vel topic as a Twist message:

  • Linear velocity: x, y, z

  • Angular velocity: roll, pitch, yaw

For a differential-drive robot, only linear x (forward/back) and angular z (turning) matter; the other four values remain zero.

The robot’s position is calculated by integrating velocity over time, a process known as dead reckoning, which produces an odometry estimate.

Within Gazebo, control is provided by a plugin (for example, gz-sim-diff-drive-system). This plugin publishes joint_states and broadcasts a transform from a frame called odom (the world origin) to base_link, giving other nodes a live position estimate.

Instead of faking the joint states (with joint_state_publisher_gui), the Gazebo robot is spawned from /robot_description, and the joint_states are published by the control plugin(gz-sim-joint-state-publisher-system).

Adding the Control Plugin

To drive the robot, create a new Xacro file named robot.gazebo and include it in your root URDF (robot.urdf).

<?xml version="1.0"?>
<robot xmlns:xacro="http://www.ros.org/wiki/xacro">
    <gazebo>

        <!-- Plugins will go here! -->

    </gazebo>
</robot>

So then inside those <gazebo> tags, we will create a <plugin> tag, using the gz-sim-diff-drive-system and the others. Find the full robot.gazebo file with all the needed plugins here.

Bridge ROS Topics

In Gazebo Classic, ROS communication relied on plugins inside gazebo_ros_pkgs.
In the new Gazebo, bridging is handled by a generic bridge node from ros_gz, which connects gz-transport topics to ROS 2 topics.

Create a file called gz_bridge.yaml inside a config/ directory and reference it in your launch file. If you started from a template, this may already be in place.

Testing Control

With everything set up, relaunch Gazebo. The robot is now ready to receive velocity commands on the /cmd_vel topic. The easiest way to send them is with teleop_twist_keyboard:

ros2 run teleop_twist_keyboard teleop_twist_keyboard

Follow the on-screen instructions - press i to move forward, for example - and watch the robot respond in Gazebo.

Visualizing the Result

Because the control plugin broadcasts a transform from odom to base_link, you can visualize the robot’s motion in RViz:

  1. Start RViz and add the TF and RobotModel displays.

  2. Set the Fixed Frame to odom.

As you drive the robot in Gazebo, its movement should match in RViz in real time.

Let’s Make an Obstacle World

Gazebo also lets you build custom environments. Create a world file - for example, obstacles.world with construction cones - and update the world_path in your launch file to load it. Relaunch to see your new obstacle course.

What’s Next?

Next, we’ll add sensors such as an IMU and LiDAR, then visualize their data in both Gazebo and RViz.

You can follow the progress or grab the full example code from the project’s GitHub repository.

About the author

No comments yet. Be the first to comment.