This setup assumes Ubuntu 22.04 LTS with ROS 2 Humble and Gazebo Harmonic.
Step 1: Install ROS 2 Humble & Prerequisites
Install ROS 2 Humble Desktop and development tools if not already present:
Bash
sudo apt update && sudo apt install -y curl gnupg lsb-release
sudo curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key -o /usr/share/keyrings/ros-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] http://packages.ros.org/ros2/ubuntu $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/ros2.list > /dev/null
sudo apt update
sudo apt install -y ros-humble-desktop python3-colcon-common-extensions python3-rosdep python3-argcomplete ros-dev-tools
Step 2: Install Gazebo Harmonic & Integration Bridge
Install Gazebo Harmonic along with the ROS 2 to Gazebo bridge:
Bash
# Add 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-stable.list $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/gazebo-stable.list > /dev/null
sudo apt update
sudo apt install -y gz-harmonic ros-humble-ros-gzharmonic
Step 3: Clone & Setup PX4-Autopilot
Clone PX4 with recursive submodules and run the environment setup script:
Bash
cd ~
git clone https://github.com/PX4/PX4-Autopilot.git --recursive
cd PX4-Autopilot
# Run setup script for Ubuntu (installs toolchains and dependencies)
bash ./Tools/setup/ubuntu.sh
# Reboot system to finalize permissions/group additions
sudo reboot
After rebooting, verify PX4 SITL builds and Gazebo launches:
Bash
cd ~/PX4-Autopilot
make px4_sitl gz_x500
(Press Ctrl+C to close the simulation once verified).
Step 4: Build Micro-XRCE-DDS Agent
The Micro-XRCE-DDS Agent bridges PX4 uORB messages directly to ROS 2 topics.
Bash
cd ~
git clone https://github.com/eProsima/Micro-XRCE-DDS-Agent.git
cd Micro-XRCE-DDS-Agent
mkdir build && cd build
cmake ..
make -j$(nproc)
sudo make install
sudo ldconfig
Step 5: Build ROS 2 Workspace (px4_msgs)
Create a ROS 2 workspace containing the px4_msgs definitions to decode PX4 topics in ROS 2.
Bash
mkdir -p ~/px4_ros_ws/src
cd ~/px4_ros_ws/src
# Clone the custom PX4 ROS 2 message definitions
git clone https://github.com/PX4/px4_msgs.git
# Build the workspace
cd ~/px4_ros_ws
source /opt/ros/humble/setup.bash
colcon build
# Add workspace overlay to bashrc
echo "source ~/px4_ros_ws/install/setup.bash" >> ~/.bashrc
source ~/.bashrc
Step 6: Verify Full Simulation Pipeline
Open 3 separate terminals to run the complete environment:
Terminal 1: Start Micro-XRCE-DDS Agent
Bash
MicroXRCEAgent udp4 -p 8888
Terminal 2: Launch PX4 SITL + Gazebo Harmonic
Bash
cd ~/PX4-Autopilot
make px4_sitl gz_x500
Terminal 3: Inspect Active ROS 2 Topics
Bash
source ~/px4_ros_ws/install/setup.bash
ros2 topic list
You should see topics such as /fmu/out/vehicle_status, /fmu/out/vehicle_odometry, and /fmu/in/trajectory_setpoint actively publishing.
This step-by-step setup guide is tailored for Ubuntu 22.04 LTS running ROS 2 Humble, Gazebo Harmonic, and PX4 Autopilot.
Step 1: Install ROS 2 Humble & Build Essentials
- Set up locale and add the ROS 2 repository: Bash
sudo apt update && sudo apt install -y curl gnupg lsb-release sudo curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key -o /usr/share/keyrings/ros-archive-keyring.gpg echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] http://packages.ros.org/ros2/ubuntu $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/ros2.list > /dev/null - Install ROS 2 Humble Desktop and development tooling: Bash
sudo apt update sudo apt install -y ros-humble-desktop python3-colcon-common-extensions python3-rosdep python3-argcomplete ros-dev-tools - Source ROS 2 in your shell: Bash
echo "source /opt/ros/humble/setup.bash" >> ~/.bashrc source ~/.bashrc
Step 2: Install Gazebo Harmonic & ros_gz Integration Bridge
- Add official OSRF Gazebo repositories: Bash
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-stable.list $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/gazebo-stable.list > /dev/null - Remove any legacy Ignition Fortress packages to prevent version conflicts, then install Gazebo Harmonic and its ROS 2 bridge: Bash
sudo apt update sudo apt remove -y ros-humble-ros-gz* sudo apt install -y gz-harmonic ros-humble-ros-gzharmonic - Export the target Gazebo version for building PX4 and ROS nodes: Bash
echo "export GZ_VERSION=harmonic" >> ~/.bashrc source ~/.bashrc
Step 3: Clone & Configure PX4-Autopilot
- Clone PX4 with required submodules: Bash
cd ~ git clone https://github.com/PX4/PX4-Autopilot.git --recursive cd PX4-Autopilot - Run PX4’s automated dependency installer for Ubuntu: Bash
bash ./Tools/setup/ubuntu.sh --no-nuttx - Reboot to apply permissions and user group changes: Bash
sudo reboot - Verify PX4 SITL and Gazebo Harmonic launch correctly: Bash
cd ~/PX4-Autopilot make px4_sitl gz_x500(Wait for Gazebo to load the quadcopter, then close it withCtrl+C).
Step 4: Build Micro-XRCE-DDS Agent
PX4 communicates with ROS 2 via Micro-XRCE-DDS. Build the agent executable from source:
Bash
cd ~
git clone https://github.com/eProsima/Micro-XRCE-DDS-Agent.git
cd Micro-XRCE-DDS-Agent
mkdir build && cd build
cmake ..
make -j$(nproc)
sudo make install
sudo ldconfig
Step 5: Setup ROS 2 Simulation Workspace (px4_msgs)
- Create a ROS 2 workspace and clone matching PX4 ROS messages: Bash
mkdir -p ~/px4_ros_ws/src cd ~/px4_ros_ws/src git clone https://github.com/PX4/px4_msgs.git - Build the workspace using
colcon: Bashcd ~/px4_ros_ws source /opt/ros/humble/setup.bash colcon build - Add the workspace overlay to your environment: Bash
echo "source ~/px4_ros_ws/install/setup.bash" >> ~/.bashrc source ~/.bashrc
Step 6: Running the Complete Simulation Environment
To test the complete stack, open 3 separate terminal windows:
Terminal 1 — Run the Middleware Agent:
Bash
MicroXRCEAgent udp4 -p 8888
Terminal 2 — Launch PX4 & Gazebo Simulation:
Bash
cd ~/PX4-Autopilot
make px4_sitl gz_x500
Terminal 3 — Verify ROS 2 Communication:
Bash
source ~/px4_ros_ws/install/setup.bash
ros2 topic list
You should see incoming PX4 uORB topics mapped to ROS 2 (such as /fmu/out/vehicle_status and /fmu/out/vehicle_odometry).