Skip to main content

Gazebo Physics Simulation

Introduction to Gazebo

Gazebo is a powerful, open-source robotics simulator that provides high-fidelity physics simulation, realistic rendering, and convenient programmatic interfaces. It's widely used in robotics research and development for testing algorithms, robot designs, and control systems before deployment on physical robots.

Key Features of Gazebo

  • Physics Simulation: Accurate simulation of rigid body dynamics, collisions, and environmental forces
  • Sensor Simulation: Realistic simulation of cameras, LiDAR, IMUs, GPS, and other sensors
  • 3D Visualization: High-quality rendering with support for lighting, shadows, and textures
  • Plugin Architecture: Extensible through plugins for custom sensors, controllers, and physics
  • ROS Integration: Seamless integration with ROS and ROS 2 for robotics development

Installing Gazebo

For this textbook, we'll use Gazebo Garden (the latest stable version):

# Update package list
sudo apt update

# Install Gazebo Garden
sudo apt install gazebo libgazebo-dev

# Install ROS 2 Gazebo packages
sudo apt install ros-humble-gazebo-ros-pkgs

Basic Gazebo Concepts

Worlds

A Gazebo world file (.world) defines the environment, including:

  • Physical properties (gravity, atmosphere)
  • Static and dynamic objects
  • Lighting and visual effects
  • Initial robot positions

Models

Gazebo models represent physical objects and include:

  • 3D geometry and visual appearance
  • Physical properties (mass, friction, collision shapes)
  • Joints and actuators
  • Sensors

SDF (Simulation Description Format)

SDF is Gazebo's XML-based format for describing simulation elements:

<?xml version="1.0" ?>
<sdf version="1.7">
<world name="default">
<!-- World properties -->
<physics type="ode">
<gravity>0 0 -9.8</gravity>
</physics>

<!-- Models and objects -->
<model name="ground_plane">
<static>true</static>
<link name="link">
<visual name="visual">
<geometry>
<plane>
<normal>0 0 1</normal>
<size>100 100</size>
</plane>
</geometry>
</visual>
<collision name="collision">
<geometry>
<plane>
<normal>0 0 1</normal>
</plane>
</geometry>
</collision>
</link>
</model>
</world>
</sdf>

Physics Simulation in Gazebo

Gravity and Environmental Forces

Gazebo accurately simulates gravitational forces and other environmental effects:

<world name="physics_world">
<gravity>0 0 -9.8</gravity> <!-- Standard Earth gravity -->
<magnetic_field>6e-6 2.3e-5 -4.2e-5</magnetic_field>
<atmosphere type="adiabatic">
<pressure>101325</pressure>
</atmosphere>
</world>

Collision Detection

Gazebo supports multiple physics engines with different collision detection capabilities:

  • ODE (Open Dynamics Engine): Fast and stable, good for most applications
  • Bullet: More accurate for complex collision scenarios
  • DART: Advanced for articulated bodies and soft contacts

Material Properties and Friction

Objects in Gazebo have material properties that affect their behavior:

<link name="wheel">
<collision name="collision">
<surface>
<friction>
<ode>
<mu>1.0</mu> <!-- Static friction coefficient -->
<mu2>1.0</mu2> <!-- Secondary friction coefficient -->
<slip1>0.0</slip1> <!-- Primary slip coefficient -->
<slip2>0.0</slip2> <!-- Secondary slip coefficient -->
</ode>
</friction>
</surface>
</collision>
</link>

Creating a Simple Gazebo World

Let's create a simple world file that demonstrates basic physics concepts:

<?xml version="1.0" ?>
<sdf version="1.7">
<world name="physics_demo">
<include>
<uri>model://sun</uri>
</include>

<include>
<uri>model://ground_plane</uri>
</include>

<!-- A ball that will fall due to gravity -->
<model name="falling_ball">
<pose>0 0 5 0 0 0</pose>
<link name="link">
<inertial>
<mass>1.0</mass>
<inertia>
<ixx>0.4</ixx>
<iyy>0.4</iyy>
<izz>0.4</izz>
</inertia>
</inertial>
<visual name="visual">
<geometry>
<sphere>
<radius>0.1</radius>
</sphere>
</geometry>
<material>
<ambient>1 0 0 1</ambient>
<diffuse>1 0 0 1</diffuse>
</material>
</visual>
<collision name="collision">
<geometry>
<sphere>
<radius>0.1</radius>
</sphere>
</geometry>
</collision>
</link>
</model>

<!-- A box that will interact with the ball -->
<model name="static_box">
<pose>0.5 0 0.5 0 0 0</pose>
<static>true</static>
<link name="link">
<visual name="visual">
<geometry>
<box>
<size>1 1 1</size>
</box>
</geometry>
<material>
<ambient>0 0 1 1</ambient>
<diffuse>0 0 1 1</diffuse>
</material>
</visual>
<collision name="collision">
<geometry>
<box>
<size>1 1 1</size>
</box>
</geometry>
</collision>
</link>
</model>
</world>
</sdf>

Launching Gazebo with ROS 2

To integrate Gazebo with ROS 2, use the gazebo_ros package:

# Launch Gazebo with a world file
ros2 launch gazebo_ros empty_world.launch.py world:=/path/to/your/world.world

# Or launch with GUI
ros2 launch gazebo_ros gzserver.launch.py &
ros2 run gazebo_ros gzclient

Working with Gazebo Models

Spawning Models

Use the spawn_entity service to add models to the simulation:

import rclpy
from rclpy.node import Node
from gazebo_msgs.srv import SpawnEntity

class ModelSpawner(Node):
def __init__(self):
super().__init__('model_spawner')
self.cli = self.create_client(SpawnEntity, '/spawn_entity')
while not self.cli.wait_for_service(timeout_sec=1.0):
self.get_logger().info('Service not available, waiting again...')

def spawn_model(self, name, xml, pose):
req = SpawnEntity.Request()
req.name = name
req.xml = xml
req.initial_pose = pose
future = self.cli.call_async(req)
rclpy.spin_until_future_complete(self, future)
return future.result()

Controlling Models

Models can be controlled through ROS 2 topics and services:

  • /model/{model_name}/cmd_vel for velocity commands
  • /model/{model_name}/set_pose for position control
  • /gazebo/set_model_state for comprehensive state control

Best Practices for Physics Simulation

  1. Start Simple: Begin with basic shapes and simple physics, then add complexity
  2. Tune Parameters: Adjust physics parameters to match real-world behavior
  3. Validate: Compare simulation results with real-world data when possible
  4. Optimize Performance: Balance accuracy with simulation speed
  5. Use Appropriate Time Steps: Smaller time steps for more accuracy but slower performance

Exercise: Physics Simulation Challenge

Create a simulation with:

  1. A robot with differential drive
  2. Multiple objects with different friction coefficients
  3. A ramp to test how objects behave with different properties
  4. A camera sensor to observe the simulation

Summary

In this section, we've covered the fundamentals of Gazebo physics simulation:

  • Setting up Gazebo environments
  • Understanding physics properties and collision detection
  • Creating and controlling models in simulation
  • Integrating with ROS 2 for robotics applications

The next section will cover Unity for high-fidelity rendering and human-robot interaction.