Skip to main content

Simulation

An additional benifit of using ROS is that it allows users to easily develop simulators such as Gazebo with their robotic projects. This is crucial for rapid development as it allows you to test your scripts without having to risk crashing your physical drone.

Docker

Setting up Gazebo ROS on Linux is made easy via Docker.

This tutorial will assume you are running on a Linux OS, but Docker can be used on both Mac and Windows via Docker Desktop.

First, make sure you installed Docker Engine. It is also recommend you follow the Linux Post-Installation Steps as well to manage Docker as a non-root user.

We can verify our Docker installation via:

$ docker run hello-world

NVIDIA GPU Acceleration

We can use Gazebo to simulate our MAVROSPY movment commands. Using an NVIDIA graphics card will improve simulation performance and is highly recommended.

To use our NVIDIA GPU, verify you have installed its driver:

$ nvidia-smi

If it's not installed, follow your desktops OS driver installation instructions.

To utilize our NVIDIA GPU inside a Docker container, we must install the NVIDIA Container Toolkit.

This is a summary of the official installation instructions.

Configure the production repository:

$ curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg \
&& curl -s -L https://nvidia.github.io/libnvidia-container/stable/deb/nvidia-container-toolkit.list | \
sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' | \
sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list

Update the packages list from the repository:

$ sudo apt update

Install the NVIDIA Container Toolkit packages:

$ sudo apt install nvidia-container-toolkit

Configure the Docker container runtime by using the nvidia-ctk command:

$ sudo nvidia-ctk runtime configure --runtime=docker

Restart the Docker daemon:

$ sudo systemctl restart docker

Verify the installation by running a sample CUDA container:

$ sudo docker run --rm --runtime=nvidia --gpus all ubuntu nvidia-smi

Then, reboot.

Build Docker Image

We can now build our Docker image via the following commands:

$ cd ~/mavrospy/docker/sim
$ bash run_docker.sh

This script will automatically build the Docker image and open it in a Docker container.

Launch Gazebo Simulator

Open QGroundControl.

Inside the docker container, launch a simulation:

$ ros2 launch mavrospy gazebo_sim.py
info

You can specify the flight pattern via the pattern argument. If unspecified, it will default to square.

$ ros2 launch mavrospy gazebo_sim.py pattern:=spiral

Available Patterns: square, square_head, circle, circle_head, figure8, figure8_head, spiral, and spiral_head. Where _head will specify the drone to face in the direction of motion.

Gazebo will open up and display a quadcopter model. The simulator will automatically connect to your QGroundControl application.

In the container, wait for the messages to stop updating until you see the following line:

INFO  [commander] Ready for takeoff!

Navigate to your QGroundControl application and click on the text that says Hold to change the mode to Offboard.

Go back to the Gazebo window and watch drone fly!

You can press CTRL-C in the container running Gazebo to close the simulation.

Visualization with Rviz

We can also visualize our simulation poses with Rviz.

After launching the Gazebo Simulator, we can launch Rviz.

Open a new termial and attach to the MAVROSPY sim container:

$ bash ~/mavrospy/docker/sim/run_docker.sh

Launch Rviz:

$ ros2 launch mavrospy rviz.py

Rviz will automatically begin plotting the drone's path.

CTRL+C to exit.