Skip to main content
Version: 2025

3.3 Sensor-Driven Autonomy

Gazebo

Gazebo is a set of open source tools for simulating robots in an environment. It is commonly used in robotics research and education.

We will be using Gazebo to simulate the BlueROV2 robot in a virtual pool environment.

note

We will be using Gazebo Harmonic, the latest LTS version of Gazebo.

Installation

The documentation for installing Gazebo can be found here. See "Binary Installation" for instructions on installing Gazebo on your operating system. Below is a summary of the steps.

note

Only Ubuntu is fully supported by Gazebo, and macOS is partially supported. On Windows the simulation will run in the WSL environment.

First we need to install ruby from Homebrew:

brew install ruby

Then we have to add the Homebrew Ruby to our path:

echo 'export PATH="$(brew --prefix)/opt/ruby/bin:$PATH"' >> ~/.zshrc

Now, we add the osrf/simulation tap:

brew tap osrf/simulation
Apple Silicon-based Macs

For Apple Silicon-based Macs, we need to modify the Formulae before installing Gazebo.

cd /opt/homebrew/Library/Taps/osrf/homebrew-simulation/Formula
sed -i '' 's|cmake_args << "-DCMAKE_INSTALL_RPATH=#{rpath}"|cmake_args << "-DCMAKE_INSTALL_RPATH=#{rpath};/opt/homebrew/lib"|' *.rb

Next we install Gazebo:

brew install gz-harmonic

Testing

To test your installation, run the following command:

In one terminal, run:

gz sim -v4 -s shapes.sdf

In another terminal, run:

gz sim -v4 -g

BlueROV2 Simulation

Installation

In this course, you will use a custom simulation for the BlueROV2. To install the simulation, clone the repository onto your macOs, Windows WSL, or Ubuntu machine:

git clone https://github.com/blksail-edu/gazebo_bluerov2

Dr. Saad it is your time to shine !

Testing

After the build is complete, you can start the simulation with:

chmod +x ./launch_1x_bluerov2.sh
./launch_1x_bluerov2.sh
info

The ./ in the command is the relative path to the build.sh and launch.sh files. As is, the above commands only run if your working directory is the gaebo_bluerov2 directory.

This command MUST be run from inside the /gazebo_bluerov2 directory.

Environment Setup

Now that you can run the BlueROV2 Gazebo simulation, the Gazebo simulation must be bridged with ROS 2.

Raspberry Pi Configuration

In a terminal on the backseat, install the ros_gz_bridge package. First, add the Gazebo package repository:

sudo wget https://packages.osrfoundation.org/gazebo.gpg -O /usr/share/keyrings/pkgs-osrf-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/pkgs-osrf-archive-keyring.gpg] http://packages.osrfoundation.org/gazebo/ubuntu-stable $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/gazebo-stable.list > /dev/null

Then install the bridge and some dependencies:

sudo apt update
sudo apt install ros-jazzy-ros-gz ros-jazzy-ros-gz-bridge libgz-transport13-dev libgz-msgs10-dev libgz-common5-dev libgz-tools2-dev
sudo apt upgrade -y

Then, with your favorite text editor, open the .zshrc file and add the following lines:

# Gazebo
export GZ_PARTITION=my_sim
export GZ_TRANSPORT_IP=192.168.3.2
export GZ_TRANSPORT_PUB_PORT=11345
export GZ_TRANSPORT_SUB_PORT=11346
export GZ_DISCOVERY_SERVER=192.168.3.1:11345

These environment variables will help point the Raspberry Pi to the Gazebo simulation.

Source the .zshrc file:

source ~/.zshrc

Laptop Configuration

warning

If you are using Windows and WSL, then for this section only, follow the Windows tab rather than the Ubuntu tab.

danger

Dr. Saad part two, electric boogaloo !

ROS 2 Gazebo Bridge

To communicate with the BlueROV2 simulation, you will use the ros_gz_bridge package. The rosmav package contains a launch file that is already configured for both our single-rov and double-rov simulations.

warning

The bridge node should be started after the Gazebo simulations to ensure it functions properly.

This node will convert Gazebo messages to ROS 2 messages for crucial onboard components like the camera, IMU, magnetometer/compass. It is also configured for thruster manual inputs; however, we WILL NOT be using these to control the ROV(s).

info

You will start the bridge in the simulation.launch.py ROS 2 launch file in the following section.

ROS 2 Simulation Interface

To control the BlueROV2 vehicle(s) in the Gazebo simulations, you will use the simulation interface in the rosmav package. The simulation interface publishes topics identical to those from the hardware interface using a custom node paired with the ros_gz_bridge package.

First, start the simulation on your macOs, Windows WSL, or Ubuntu machine:

info

If you want to start a simulation with 1x BlueROV2, run:

./launch_1x_bluerov2.sh

If you want to start a simulation with 2x BlueROV2, run:

./launch_2x_bluerov2.sh

Then, on the backseat, launch the simulation interface and bridge nodes:

ros2 launch rosmav simulation.launch.py

If all things are working correctly, several topics will be advertised with the rov1 and rov2 namespaces that correspond to the same topics from the bluerov2_hardware_interface node.

note

Topics will be advertised for both ROVs in the simulation interface regardless of whether you are simulating 1x or 2x vehicles.

If you are only simulating 1x vehicle, ignore the rov2 topics.

info

For the simulation, you do not need to arm the ROVs to publish ManualControl messages.

Problem Set

Create a new package that will contain nodes to run with the Gazebo simulations.

Problem One

Create a new node that deploys your line-following logic with rov1 in the ./launch_1x_bluerov2 simulation. Publish control messages to the /rov1/manual_control topic.

info

If you do not want to publish all the x, y, z, and r in a single ManualControl msg, you can replace any of the fields with nan. In this case, the software interface will use the previous value for that field.

Check-off

Review with a TA or instructor to check-off this section.

Problem Two

Create a launch file that deploys two instances of your control logic for both rov1 and rov2. Please specify the ROV names as namespaces in your launch file.

Then, test your code using the ./launch_2x_bluerov2 simulation and ros2 launch rosmav simulation.launch.py.

note

This simulation can be used to test your strategy for the final challenge.

Check-off

Review with a TA or instructor to check-off this section.