Skip to main content

Simulation

An additional benefit 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 movement 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 the MAVROSPY Docker image via the following commands:

$ cd ~
$ git clone -b ros2 https://github.com/bandofpv/mavrospy.git
$ 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.

Install Isaac Sim

Gazebo is already installed in the Docker image. However, if you want to use Isaac Sim, you will need to install it manually.

First, start by downloading Isaac Sim 4.2 which is the latest version supported by the Pegasus Simulator extension which is required to integrate a PX4 backend and thus MAVROSPY. Currently, the traditional method of installing Isaac Sim via the Omniverse Launcher is not supported and must be installed manually:

$ mkdir ~/isaacsim
$ cd ~/Downloads
$ wget https://download.isaacsim.omniverse.nvidia.com/isaac-sim-standalone%404.2.0-rc.18%2Brelease.16044.3b2ed111.gl.linux-x86_64.release.zip
$ unzip "isaac-sim-standalone@4.2.0-rc.18+release.16044.3b2ed111.gl.linux-x86_64.release.zip" -d ~/isaacsim
$ rm isaac-sim-standalone@4.2.0-rc.18+release.16044.3b2ed111.gl.linux-x86_64.release.zip

Next, install Isaac Sim by running the following commands one after another:

$ cd ~/isaacsim
$ ./omni.isaac.sim.post.install.sh
$ ./isaac-sim.selector.sh
note

These commands will take a while to run. Please don't close the terminal or interrupt the process.

Now, you can install Pegasus Simulator by following the installation instructions.

Due to the manual installation of Isaac Sim, the Pegasus Simulator installation instructions must be modified slightly. The ISAACSIM_PATH variable must be set to the directory where Isaac Sim was installed. For example, if you installed Isaac Sim in ~/isaacsim, as per these instructions, then the following commands should be added to the end of your ~/.bashrc file:

# Isaac Sim root directory
export ISAACSIM_PATH="${HOME}/isaacsim"
# Isaac Sim python executable
alias ISAACSIM_PYTHON="${ISAACSIM_PATH}/python.sh"
# Isaac Sim app
alias ISAACSIM="${ISAACSIM_PATH}/isaac-sim.sh"

Additionally, the following commands should be added to your ~/.bashrc file after the previous step to set the correct environment variables for ROS 2 Humble integration:

export RMW_IMPLEMENTATION=rmw_fastrtps_cpp
export ROS_DISTRO=humble
export ROS_DOMAIN_ID=1

# Can only be set once per terminal.
# Setting this command multiple times will append the internal library path again potentially leading to conflicts
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ISAACSIM_PATH/exts/omni.isaac.ros2_bridge/humble/lib

You can press CTRL+C after the make px4_sitl_default none command when you see:

[100%] Built target px4
tip

The rest of the installation instructions should work as is. However, in my experience, I found it necessary to install empy v3.3.4 during the PX4-Autopilot installation step. This can be done by running the following command prior to compiling the PX4-Autopilot source code:

$ pip install empy==3.3.4

Launch Isaac Simulator

Open QGroundControl.

Inside the docker container, launch a simulation:

$ ros2 launch mavrospy isaac_sim.py
info

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

$ ros2 launch mavrospy isaac_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.

Next, open a new terminal and run Isaac Sim:

ISAACSIM_PYTHON ~/PegasusSimulator/examples/1_px4_single_vehicle.py

Isaac Sim will open up and display a quadcopter model. The simulator will automatically connect to your QGroundControl application. Look for Received first heartbeat at the bottom of the window.

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

Go back to the Isaac Sim window and watch drone fly!

You can close the Isaac Sim window using the x at the top right and press CTRL+C in the MAVROSPY container to close the simulation.

Visualization with Rviz

We can also visualize our simulation poses with Rviz.

After launching Gazebo or Isaac Sim, 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.