← Back to Blog

[ROS2, Gazebo, PX4] Ubuntu Setup and Connection Guide

engineering > simulation

2026-07-086 min read

#engineering #simulation #ros2 #gazebo #px4 #ubuntu #drone #sitl

ROS2, Gazebo, PX4를 같이 쓰려면 단순히 세 프로그램을 설치하는 것보다 연결 구조를 먼저 이해하는 것이 중요하다. 각 도구가 맡는 역할이 다르기 때문이다.

Component역할
Ubuntu개발 환경의 OS
ROS2로봇 애플리케이션, topic/service/action 통신
Gazebo물리 시뮬레이터와 3D world
PX4비행 제어기 autopilot
Micro XRCE-DDS AgentPX4 uORB topic과 ROS2 DDS graph 사이의 bridge

PX4 SITL에서 핵심 연결은 ROS2와 Gazebo를 직접 붙이는 것이 아니라, ROS2는 PX4와 DDS로 통신하고 PX4는 Gazebo와 simulator bridge로 통신하는 구조다.

전체 구조는 다음과 같다.

ROS2 node
  <-> Micro XRCE-DDS Agent
  <-> PX4 uXRCE-DDS client
  <-> PX4 SITL
  <-> PX4 Gazebo bridge
  <-> Gazebo

Gazebo의 sensor topic이나 world topic을 ROS2에서 직접 다루고 싶다면 ros_gz_bridge를 추가로 사용한다. 하지만 PX4의 vehicle state, sensor_combined, vehicle_command 같은 비행 제어 topic은 보통 px4_msgs와 Micro XRCE-DDS 경로로 다룬다.

이 글은 전체 구조를 이해하기 위한 1부이다. 명령어를 하나씩 치기보다 자동 설치 스크립트로 구성하고 싶다면 2부: Ubuntu에서 ROS2, Gazebo, PX4 자동 설치 Shell Script를 보면 된다. Gazebo topic을 Python 코드로 읽고 프로젝트로 관리하는 방법은 3부: Gazebo ROS2 Python Package 구조와 코드 관리를 보면 된다.


추천 버전

PX4 공식 ROS2 guide는 PX4와 함께 쓰는 권장 조합으로 Ubuntu 22.04와 ROS2 Humble을 안내한다. 따라서 드론/PX4 SITL 실습 목적이라면 다음 조합이 가장 무난하다.

항목추천
OSUbuntu 22.04 LTS
ROS2Humble Hawksbill
GazeboPX4 setup script가 설치하는 Gazebo 또는 Gazebo Harmonic
PX4PX4-Autopilot main 또는 사용하는 firmware version에 맞는 branch

Ubuntu 24.04에서는 ROS2 Jazzy와 Gazebo Harmonic 조합도 가능하다. 다만 PX4와 ROS2 message compatibility까지 생각하면, 처음 구성할 때는 Ubuntu 22.04 + Humble로 시작하는 편이 시행착오가 적다.

PX4까지 연결할 목적이면 Ubuntu 22.04 + ROS2 Humble을 기준 환경으로 잡는 것이 가장 안정적이다.


1. Ubuntu 기본 패키지 설치

먼저 시스템 패키지를 업데이트한다.

sudo apt update
sudo apt upgrade -y

개발에 필요한 기본 도구를 설치한다.

sudo apt install -y \
  build-essential \
  cmake \
  git \
  curl \
  wget \
  lsb-release \
  gnupg \
  python3-pip \
  python3-venv

locale도 확인한다.

sudo apt install -y locales
sudo locale-gen en_US en_US.UTF-8
sudo update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8
export LANG=en_US.UTF-8

.bashrc에 locale을 넣어두면 새 terminal에서도 유지된다.

echo 'export LANG=en_US.UTF-8' >> ~/.bashrc

2. ROS2 Humble 설치

Ubuntu 22.04 기준으로 ROS2 Humble apt repository를 추가한다.

sudo apt install -y software-properties-common
sudo add-apt-repository universe
sudo apt update
sudo apt install -y curl

ROS2 repository key를 등록한다.

sudo curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key \
  -o /usr/share/keyrings/ros-archive-keyring.gpg

ROS2 apt source를 추가한다.

echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] http://packages.ros.org/ros2/ubuntu $(. /etc/os-release && echo $UBUNTU_CODENAME) main" \
  | sudo tee /etc/apt/sources.list.d/ros2.list > /dev/null

ROS2 desktop과 개발 도구를 설치한다.

sudo apt update
sudo apt upgrade -y
sudo apt install -y ros-humble-desktop ros-dev-tools

매번 ROS2 환경을 source하지 않도록 .bashrc에 추가한다.

echo "source /opt/ros/humble/setup.bash" >> ~/.bashrc
source ~/.bashrc

설치 확인:

ros2 --help
ros2 topic list

3. Gazebo 설치

PX4의 Ubuntu setup script를 실행하면 PX4가 권장하는 simulator dependency가 함께 설치된다. 그래도 Gazebo만 별도로 설치하고 싶다면 Gazebo Harmonic binary package를 설치할 수 있다.

Gazebo repository key를 등록한다.

sudo curl https://packages.osrfoundation.org/gazebo.gpg \
  --output /usr/share/keyrings/pkgs-osrf-archive-keyring.gpg

Gazebo apt source를 추가한다.

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

Gazebo Harmonic을 설치한다.

sudo apt update
sudo apt install -y gz-harmonic

설치 확인:

gz sim --version
gz sim

gz sim을 실행했을 때 Gazebo GUI가 뜨면 Gazebo 자체 설치는 된 것이다.


4. PX4 설치

PX4 SITL을 사용하려면 PX4-Autopilot source와 개발 toolchain이 필요하다.

cd ~
git clone https://github.com/PX4/PX4-Autopilot.git --recursive

PX4 Ubuntu setup script를 실행한다.

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

이 script는 PX4 build toolchain과 simulator dependency를 설치한다. 설치가 끝나면 terminal을 다시 열거나 shell 환경을 다시 읽는다.

source ~/.bashrc

PX4 SITL build 확인:

cd ~/PX4-Autopilot
make px4_sitl

Gazebo와 함께 X500 multicopter를 실행한다.

cd ~/PX4-Autopilot
make px4_sitl gz_x500

Gazebo 창에 X500 drone이 뜨고 PX4 console이 실행되면 PX4와 Gazebo 연결은 된 것이다.

headless 환경에서는 GUI 없이 실행할 수 있다.

HEADLESS=1 make px4_sitl gz_x500

5. Micro XRCE-DDS Agent 설치

ROS2가 PX4 uORB topic을 직접 읽고 쓰려면 Micro XRCE-DDS Agent가 필요하다. PX4 SITL 안에는 uXRCE-DDS client가 들어 있고, simulator build에서는 UDP 8888 port로 agent에 연결된다.

ROS2와 PX4를 연결하려면 PX4 쪽 uXRCE-DDS client와 컴퓨터 쪽 Micro XRCE-DDS Agent가 서로 연결되어야 한다.

Agent를 source에서 빌드한다.

cd ~
git clone -b v2.4.3 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/

Agent 실행:

MicroXRCEAgent udp4 -p 8888

이 terminal은 계속 켜둔다. PX4 SITL을 실행하면 Agent terminal에 publisher, datawriter, topic 생성 로그가 보인다.


6. ROS2 Workspace 만들기

PX4 ROS2 topic을 해석하려면 px4_msgs가 필요하다. 예제 node를 실행하려면 px4_ros_com도 같이 받으면 된다.

mkdir -p ~/px4_ros2_ws/src
cd ~/px4_ros2_ws/src
git clone https://github.com/PX4/px4_msgs.git
git clone https://github.com/PX4/px4_ros_com.git

workspace를 build한다.

cd ~/px4_ros2_ws
source /opt/ros/humble/setup.bash
colcon build

새 terminal에서 workspace를 source한다.

source /opt/ros/humble/setup.bash
source ~/px4_ros2_ws/install/local_setup.bash

자주 쓸 것이므로 .bashrc에 추가해도 된다.

echo "source ~/px4_ros2_ws/install/local_setup.bash" >> ~/.bashrc

주의할 점은 px4_msgs version이다. 기본적으로는 사용 중인 PX4 firmware와 같은 message definition을 쓰는 branch를 맞춰야 한다. PX4 v1.16 이후 message versioning을 사용하는 경우에는 message translation node가 필요할 수 있다.


7. 전체 연결 실행 순서

실제 실행은 terminal을 나눠서 하는 것이 편하다.

Terminal 1: Micro XRCE-DDS Agent

MicroXRCEAgent udp4 -p 8888

Terminal 2: PX4 SITL + Gazebo

cd ~/PX4-Autopilot
make px4_sitl gz_x500

PX4 simulator build는 uXRCE-DDS client를 자동으로 시작하고, local UDP 8888 port의 Agent에 연결한다.

Terminal 3: ROS2 topic 확인

source /opt/ros/humble/setup.bash
source ~/px4_ros2_ws/install/local_setup.bash
ros2 topic list

정상 연결되면 다음과 같은 PX4 topic들이 보인다.

/fmu/out/sensor_combined
/fmu/out/vehicle_odometry
/fmu/out/vehicle_status
/fmu/in/vehicle_command
/fmu/in/offboard_control_mode
/fmu/in/trajectory_setpoint

topic echo로 실제 데이터가 들어오는지 확인한다.

ros2 topic echo /fmu/out/sensor_combined

예제 listener도 실행할 수 있다.

cd ~/px4_ros2_ws
source /opt/ros/humble/setup.bash
source install/local_setup.bash
ros2 launch px4_ros_com sensor_combined_listener.launch.py

센서 값이 계속 출력되면 ROS2와 PX4 사이의 DDS 연결이 정상이다.


8. Gazebo와 ROS2를 직접 연결하고 싶을 때

PX4 SITL에서 비행 제어를 하려면 보통 ROS2는 PX4 topic을 보고, Gazebo는 PX4가 연결한다. 하지만 Gazebo world, model, sensor topic을 ROS2에서 직접 보고 싶을 수 있다. 이때는 ros_gz_bridge를 사용한다.

ROS2 Humble에서 bridge package를 설치한다.

sudo apt install -y ros-humble-ros-gz-bridge

먼저 Gazebo topic을 확인한다.

gz topic -l

ROS2 topic을 확인한다.

ros2 topic list

Gazebo clock을 ROS2로 bridge하는 예시는 다음과 같다.

ros2 run ros_gz_bridge parameter_bridge /clock@rosgraph_msgs/msg/Clock[gz.msgs.Clock

다른 Gazebo topic도 message type을 맞춰서 같은 방식으로 bridge한다.

ros2 run ros_gz_bridge parameter_bridge \
  /world/default/model/x500/link/base_link/sensor/imu_sensor/imu@sensor_msgs/msg/Imu[gz.msgs.IMU

다만 PX4가 이미 sensor simulation과 vehicle dynamics를 처리하고 있으므로, offboard control이나 autopilot 상태를 다룰 때는 /fmu/* topic을 우선 보는 것이 좋다.

PX4 제어 목적의 ROS2 node는 Gazebo topic보다 /fmu/in/*, /fmu/out/* PX4 topic을 기준으로 작성하는 것이 일반적이다.


9. MAVROS2가 필요한 경우

PX4와 ROS2를 연결하는 방법은 크게 두 가지로 나눌 수 있다.

목적권장 경로
PX4 uORB topic 기반 offboard controlpx4_msgs + Micro XRCE-DDS Agent
PX4 parameter 변경, MAVLink command 활용MAVROS2

앞에서 구성한 px4_msgs 경로는 PX4의 ROS2 native workflow에 가깝다. 하지만 PX4 파라미터를 바꾸는 작업은 MAVLink parameter protocol을 쓰는 MAVROS2가 더 현실적이다.

MAVROS2 package를 설치한다.

sudo apt install -y ros-humble-mavros ros-humble-mavros-extras

MAVROS에서 GeographicLib dataset이 필요한 경우 다음 script를 실행한다.

sudo /opt/ros/humble/lib/mavros/install_geographiclib_datasets.sh

PX4 SITL의 MAVLink UDP port에 MAVROS2를 연결하는 예시는 다음과 같다.

ros2 launch mavros px4.launch fcu_url:=udp://:14540@127.0.0.1:14557

실제 port는 PX4 실행 방식과 MAVROS2 launch file에 따라 다를 수 있으므로, 연결이 안 되면 PX4 console과 MAVROS2 log에서 UDP port를 확인한다.

MAVROS2가 정상 연결되면 topic과 service가 보인다.

ros2 topic list | grep mavros
ros2 service list | grep mavros

파라미터 변경 service도 확인한다.

ros2 service list | grep param

예를 들어 PX4 multicopter roll rate P gain을 바꾸려면 다음과 같은 형태를 사용한다.

ros2 service call /mavros/param/set mavros_msgs/srv/ParamSetV2 \
  "{force_set: false, param_id: 'MC_ROLLRATE_P', value: {type: 3, double_value: 0.15}}"

실기체에서는 이 값을 바로 적용하지 말고, 반드시 기존 값을 기록하고 SITL에서 먼저 검증해야 한다.


10. 연결 구조별로 무엇을 확인해야 하는가

문제가 생겼을 때는 연결을 한 번에 보지 말고 구간별로 나눠서 확인한다.

Gazebo만 확인

gz sim
gz topic -l

Gazebo GUI가 뜨고 topic list가 나오면 Gazebo 자체는 동작한다.

PX4와 Gazebo 확인

cd ~/PX4-Autopilot
make px4_sitl gz_x500

PX4 console이 뜨고 Gazebo에 기체가 나타나면 PX4 SITL과 Gazebo 연결은 동작한다.

PX4와 ROS2 확인

Terminal 1:

MicroXRCEAgent udp4 -p 8888

Terminal 2:

cd ~/PX4-Autopilot
make px4_sitl gz_x500

Terminal 3:

source /opt/ros/humble/setup.bash
source ~/px4_ros2_ws/install/local_setup.bash
ros2 topic list | grep /fmu

/fmu/out/* topic이 보이면 PX4에서 ROS2로 나오는 경로가 열린 것이다.

ROS2 workspace 확인

cd ~/px4_ros2_ws
colcon build
source install/local_setup.bash
ros2 pkg list | grep px4

px4_msgs, px4_ros_com이 보이면 workspace source까지 된 것이다.


11. 자주 나는 문제

ros2 topic list/fmu topic이 안 보임

Agent가 실행 중인지 확인한다.

pgrep -af MicroXRCEAgent

Agent를 먼저 켜고 PX4 SITL을 다시 실행한다.

MicroXRCEAgent udp4 -p 8888

make px4_sitl gz_x500에서 target error가 남

PX4 build cache가 꼬였을 수 있다.

cd ~/PX4-Autopilot
make distclean
make px4_sitl gz_x500

ROS2 예제가 message import error를 냄

workspace source가 빠졌을 가능성이 크다.

source /opt/ros/humble/setup.bash
source ~/px4_ros2_ws/install/local_setup.bash

그래도 안 되면 다시 build한다.

cd ~/px4_ros2_ws
colcon build
source install/local_setup.bash

WSL2에서 Gazebo GUI가 느리거나 안 뜸

Windows에서 WSL2를 쓰는 경우 GUI/OpenGL 문제가 생길 수 있다. 처음에는 headless로 PX4 SITL을 확인하는 것이 더 편하다.

HEADLESS=1 make px4_sitl gz_x500

GUI가 꼭 필요하면 Windows GPU driver, WSLg, OpenGL 지원 여부를 확인해야 한다.


정리

ROS2, Gazebo, PX4를 연결할 때는 다음 순서로 구성한다.

  1. Ubuntu 기본 개발 도구 설치
  2. ROS2 Humble 설치
  3. Gazebo 설치 또는 PX4 setup script로 simulator dependency 설치
  4. PX4-Autopilot clone 및 ubuntu.sh 실행
  5. MicroXRCEAgent udp4 -p 8888 실행
  6. make px4_sitl gz_x500으로 PX4 SITL + Gazebo 실행
  7. px4_msgs, px4_ros_com workspace build
  8. ros2 topic list, /fmu/out/sensor_combined echo로 연결 확인
  9. Gazebo topic이 필요하면 ros_gz_bridge 추가
  10. PX4 parameter 변경이 필요하면 MAVROS2 추가

핵심은 연결 방향을 구분하는 것이다.

ROS2 <-> Micro XRCE-DDS Agent <-> PX4 <-> Gazebo

Gazebo topic을 직접 ROS2로 가져와야 할 때만 ros_gz_bridge를 추가한다. PX4 제어, 상태 확인, offboard control을 하려면 /fmu/in/*, /fmu/out/* topic을 기준으로 작업하면 된다. PX4 파라미터 변경처럼 MAVLink 기능이 필요한 작업은 MAVROS2를 별도로 붙이는 것이 실용적이다.


References