Skip to main content

Gazebo Environment Setup

This chapter provides a comprehensive guide to setting up and configuring the Gazebo simulation environment for robotics development. Gazebo is a powerful 3D dynamic simulator with accurate physics and rendering capabilities.

Learning Objectives

  • Install and configure Gazebo simulation environment
  • Understand Gazebo's architecture and core components
  • Set up basic simulation worlds and robot models
  • Configure physics engines and rendering options
  • Integrate Gazebo with ROS/ROS 2

Introduction to Gazebo

Gazebo is a 3D dynamic simulator that provides realistic physics simulation and rendering capabilities. It's widely used in robotics research and development for testing algorithms, training AI models, and validating robot behaviors before real-world deployment.

Key Features of Gazebo

  • Realistic Physics: Accurate simulation of rigid body dynamics, collisions, and contacts
  • High-Quality Rendering: Advanced 3D graphics using the OGRE rendering engine
  • Sensor Simulation: Support for cameras, LiDAR, IMUs, GPS, and other sensors
  • Plugin Architecture: Extensible through custom plugins
  • ROS/ROS 2 Integration: Native support for ROS/ROS 2 communication
  • Multi-Robot Simulation: Support for simulating multiple robots simultaneously

Installation

Ubuntu Installation

# Add the OSRF APT repository
sudo sh -c 'echo "deb http://packages.osrfoundation.org/gazebo/ubuntu-stable `lsb_release -cs` main" > /etc/apt/sources.list.d/gazebo-stable.list'

# Setup keys
wget https://packages.osrfoundation.org/gazebo.key -O - | sudo apt-key add -

# Update packages
sudo apt update

# Install Gazebo
sudo apt install gazebo libgazebo-dev

ROS 2 Integration

For ROS 2 integration, install the appropriate bridge packages:

# For Humble Hawksbill
sudo apt install ros-humble-gazebo-ros-pkgs ros-humble-gazebo-plugins ros-humble-gazebo-dev

Gazebo Architecture

Gazebo's architecture consists of several key components:

┌─────────────────┐
│ Gazebo GUI │ ←─┐
└─────────────────┘ │
┌─────────────────┐ │
│ Gazebo Server │ ←─┤ Communication
└─────────────────┘ │
┌─────────────────┐ │
│ Physics Engine│ ←─┤ (ODE, Bullet, DART)
└─────────────────┘ │
┌─────────────────┐ │
│ Rendering │ ←─┘
│ Engine (OGRE) │
└─────────────────┘

Core Components

  1. Gazebo Server (gzserver): Headless simulation engine
  2. Gazebo Client (gzclient): Graphical user interface
  3. Physics Engine: Handles dynamics, collisions, contacts
  4. Rendering Engine: Manages 3D visualization
  5. Sensor System: Simulates various sensor types
  6. Plugin System: Extends functionality

Basic Configuration

World Files

Gazebo uses SDF (Simulation Description Format) files to define simulation worlds:

<?xml version="1.0" ?>
<sdf version="1.7">
<world name="default">
<!-- Include a model from the database -->
<include>
<uri>model://ground_plane</uri>
</include>

<!-- Include lighting -->
<include>
<uri>model://sun</uri>
</include>

<!-- Define a simple robot -->
<model name="simple_robot">
<pose>0 0 0.5 0 0 0</pose>
<link name="chassis">
<inertial>
<mass>1.0</mass>
<inertia>
<ixx>0.01</ixx>
<ixy>0</ixy>
<ixz>0</ixz>
<iyy>0.01</iyy>
<iyz>0</iyz>
<izz>0.01</izz>
</inertia>
</inertial>
<collision name="collision">
<geometry>
<box>
<size>0.5 0.5 0.2</size>
</box>
</geometry>
</collision>
<visual name="visual">
<geometry>
<box>
<size>0.5 0.5 0.2</size>
</box>
</geometry>
<material>
<ambient>0.8 0.8 0.8 1</ambient>
<diffuse>0.8 0.8 0.8 1</diffuse>
</material>
</visual>
</link>
</model>
</world>
</sdf>

Launching Gazebo

# Launch with empty world
gzserver

# Launch with GUI
gzclient

# Launch with specific world file
gzserver /path/to/world.sdf

# Launch with GUI and specific world
gzserver /path/to/world.sdf &
gzclient

Physics Engine Configuration

Gazebo supports multiple physics engines:

ODE (Open Dynamics Engine)

  • Default engine in older versions
  • Good for basic rigid body simulation
  • Configured in world files:
<physics type="ode">
<max_step_size>0.001</max_step_size>
<real_time_factor>1</real_time_factor>
<real_time_update_rate>1000</real_time_update_rate>
</physics>

Bullet Physics

  • Better performance for complex simulations
  • Supports more advanced collision detection

DART (Dynamic Animation and Robotics Toolkit)

  • Advanced dynamics simulation
  • Supports soft body physics

ROS 2 Integration

Gazebo Bridge

The Gazebo-ROS bridge enables communication between Gazebo and ROS 2:

<!-- Example plugin configuration -->
<sdf version="1.7">
<world name="default">
<!-- Gazebo ROS bridge plugin -->
<plugin name="gazebo_ros_init" filename="libgazebo_ros_init.so">
<ros>
<namespace>/gazebo</namespace>
</ros>
<update_rate>1000</update_rate>
</plugin>
</world>
</sdf>

Robot Model Integration

<!-- Robot model with ROS plugins -->
<model name="my_robot">
<!-- Links and joints as defined in URDF -->

<!-- Differential drive plugin -->
<plugin name="differential_drive" filename="libgazebo_ros_diff_drive.so">
<ros>
<namespace>/my_robot</namespace>
<remapping>cmd_vel:=cmd_vel</remapping>
<remapping>odom:=odom</remapping>
</ros>
<left_joint>left_wheel_joint</left_joint>
<right_joint>right_wheel_joint</right_joint>
<wheel_separation>0.3</wheel_separation>
<wheel_diameter>0.15</wheel_diameter>
<odom_publish_frequency>30</odom_publish_frequency>
</plugin>
</model>

Environment Setup Best Practices

Directory Structure

gazebo_ws/
├── worlds/
│ ├── simple_room.sdf
│ └── maze.sdf
├── models/
│ ├── my_robot/
│ │ ├── model.sdf
│ │ └── meshes/
│ └── custom_objects/
├── launch/
│ └── simulation.launch.py
└── config/
└── robot_control.yaml

Model Database

Custom models should be placed in the GAZEBO_MODEL_PATH:

# Add to .bashrc
export GAZEBO_MODEL_PATH=$GAZEBO_MODEL_PATH:~/gazebo_ws/models

Testing and Validation

Basic Test

# Launch Gazebo with empty world
gzserver --verbose worlds/empty.world &

# Check if server is running
gz stats

# Launch GUI client
gzclient --verbose

ROS 2 Integration Test

import rclpy
from rclpy.node import Node
from geometry_msgs.msg import Twist

class GazeboTestNode(Node):
def __init__(self):
super().__init__('gazebo_test_node')

# Publisher for robot velocity commands
self.cmd_vel_pub = self.create_publisher(
Twist,
'/my_robot/cmd_vel',
10
)

# Timer to send commands
self.timer = self.create_timer(0.1, self.send_command)
self.i = 0

def send_command(self):
msg = Twist()
msg.linear.x = 0.5 # Move forward
msg.angular.z = 0.1 # Turn slightly
self.cmd_vel_pub.publish(msg)
self.get_logger().info(f'Publishing velocity command: {self.i}')
self.i += 1

def main(args=None):
rclpy.init(args=args)
node = GazeboTestNode()
rclpy.spin(node)
node.destroy_node()
rclpy.shutdown()

if __name__ == '__main__':
main()

Troubleshooting Common Issues

Performance Issues

  • Reduce physics update rate for better performance
  • Simplify collision geometries
  • Use less complex rendering settings

Physics Issues

  • Adjust time step size for stability
  • Verify inertial properties
  • Check joint limits and constraints

ROS Integration Issues

  • Verify namespace configurations
  • Check topic remappings
  • Ensure proper plugin loading

Advanced Configuration

Custom Plugins

#include <gazebo/gazebo.hh>
#include <gazebo/physics/physics.hh>

namespace gazebo
{
class CustomPlugin : public ModelPlugin
{
public: void Load(physics::ModelPtr _model, sdf::ElementPtr _sdf)
{
this->model = _model;
this->updateConnection = event::Events::ConnectWorldUpdateBegin(
std::bind(&CustomPlugin::OnUpdate, this));
}

public: void OnUpdate()
{
// Custom update logic
}

private: physics::ModelPtr model;
private: event::ConnectionPtr updateConnection;
};

GZ_REGISTER_MODEL_PLUGIN(CustomPlugin)
}

Learning Objectives Review

  • Install and configure Gazebo simulation environment ✓
  • Understand Gazebo's architecture and core components ✓
  • Set up basic simulation worlds and robot models ✓
  • Configure physics engines and rendering options ✓
  • Integrate Gazebo with ROS/ROS 2 ✓

Practical Exercise

  1. Install Gazebo on your system
  2. Launch Gazebo with the empty world
  3. Create a simple world file with a ground plane and a light source
  4. Add a simple box model to your world
  5. Verify that you can interact with the simulation through the GUI

Assessment Questions

  1. What are the main components of Gazebo's architecture?
  2. Explain the difference between gzserver and gzclient.
  3. How do you configure different physics engines in Gazebo?
  4. What is the purpose of the Gazebo-ROS bridge?

Further Reading

Next Steps

Continue to Physics Simulation to learn about the physics principles that govern simulation behavior.