Skip to content

ROS2 and Gazebo

This page explains how to get started using ROS2 to fly the quadrotor and perform simulations

Learn ROS2

There are lots of available resources for learning ROS2

Learn Gazebo

As long as Gazebo is installed properly, it should work pretty well and easily with ROS2

Multi-Quadcopter Control with PX4 and ROS2

The following set-up information explains how to use the code in the src directory of this repository. It explains how to set up the proper environment and utilize the provided ROS2 package. The src directory is designed to be the source directory for a standard ROS2 workspace. Each subdirectory is a seperate package with different purposes.

An overview of each package is provided as well as instructions for setting up the environment.

px4_offboard

Multi-agent quadrotor simulation using ROS2, PX4, Gazebo, QGroundControl, MicroXRCEAgent, and RViz.

Much of the intial code is based off work from:

I've used their examples to expand functionalities such as multi-agent control. It may be helpful to start with their examples first.

Explanation of launch files

Launch File Description
circle.launch Launch a quadrotor to fly in a circle
square.launch Launch a quadrotor to fly in a square
waypoint.launch Launch a quadrotor to fly to a set of user-supplied waypoints
HITL_offboard_velocity_control.launch Run on a real quadrotor for keyboard control
multi_waypoint_swarm.launch Launch a swarm of quadrotors to follow supplied waypoints
offboard_velocity_control.launch Launch a quadrotor controlled by keyboard inputs
swarm_control.launch Launch a swarm of quadrotors to be controlled by keyboard inputs

Run a launch file

Make sure you have built the ROS2 workspace and sourced the environment

colcon build
source install/setup.bash

Then you can start the launch file of your choice (assuming have already completed the set-up for PX4, Gazebo, MicroXRCEAgent, and the ROS2 workspace)

ros2 launch px4_offboard <package_name>

Several launch files support multiple vehicles with a launch configuration parameter called 'num_vehicles'

ros2 launch px4_offboard multi_waypoint_swarm.launch.py num_vehicles:=3

Notes on circle.launch.py and square.launch.py launch files

These launch files do not call on the processes.py node. Therefore you need to start the different processes manually in different terminals.

Terminal 1

ros2 launch px4_offboard circle.launch.py
Terminal 2
MicroXRCEAgent udp4 -p 8888
Terminal 3
cd ~/PX4-Autopilot && make px4_sitl gz_x500
Terminal 4
cd ~/QGroundControl && ./QGroundControl.AppImage

After everything has started, arm the drone in QGroundControl and switch it into offboard mode.

Set-up

Install PX4 Autopilot

To Install PX4 run this code

git clone https://github.com/PX4/PX4-Autopilot.git --recursive -b release/1.15

Run this script in a bash shell to install everything

bash ./PX4-Autopilot/Tools/setup/ubuntu.sh

You will now need to restart your computer before continuing

Install ROS2 Jazzy

To install ROS2 Jazzy follow the steps here

Install Dependencies

Install Python dependencies as mentioned in the PX4 Docs with this code

pip3 install --user -U empy pyros-genmsg setuptools

Build Micro DDS

As mentioned in the PX4 Docs run this code in order to build MicroDDS on your machine

git clone https://github.com/eProsima/Micro-XRCE-DDS-Agent.git
cd Micro-XRCE-DDS-Agent
mkdir build
cd build
cmake ..
make
sudo make install
sudo ldconfig /usr/local/lib/

Clone in Repo and Packages

Run this code to clone the repo

git clone https://github.com/austin006/3d_printed_quad.git

We need the px4_msgs package. Our ROS2 nodes rely on the message definitions in this package in order to communicate with PX4. Read here for more information.

Be sure you're in the src directory of the workspace and run this code to clone in the px4_msgs repo

cd src
git clone https://github.com/PX4/px4_msgs.git -b release/1.15

For some reason I couldn't get this to work in ROS2 Jazzy without also including px4_msgs_old and the translation_node packages. To copy these packages, make sure you are in your workspace directory and have installed PX4-Autopilot in your root directory. Run the following code

~/PX4-Autopilot/Tools/copy_to_ros_ws.sh .

Clone in all required packages before building the workspace

Build the Workspace

Source the ROS2 installation, build the workspace within the workspace directory (not in src), and source the workspace

source /opt/ros/jazzy/setup.bash
colcon build
source install/setup.bash

Your workspace directory should now look similar to the following

.
├── .github
├── build
├── docs
├── install
├── log
├── src
│   ├── other_ros_packages
│   ├── px4_offboard
│   ├── px4_msgs
│   ├── px4_msgs_old
│   ├── translation_node
├── .gitignore
├── mkdocs.yml
├── README.md

Running the code

Follow the instructions for each package to run the code