ROS 2 และการเชื่อมต่อกับ flight stack
Nodes, topics, services, actions, uXRCE-DDS/MAVROS and simulation
Why it matters
Advanced autonomous drones usually carry two layers of computing. The lower layer is the flight controller, running a flight stack such as PX4 or ArduPilot, which stabilises the aircraft and follows commands in real time. The upper layer is the companion computer, which processes images, builds maps and makes decisions. Upper-layer software is commonly built on ROS 2 (Robot Operating System 2). It is not an operating system but a set of libraries and tools that let many programs talk to each other in a standard way.
The benefit is access to a large body of community software, such as camera drivers, time synchronisers and visualisation tools, and easy reuse of code between ground robots and drones. This page summarises ROS 2 concepts and how to connect them to a flight stack.
Core ROS 2 concepts
- Node: one program with one job, such as reading a camera, detecting objects or planning a path.
- Topic: a publish/subscribe channel. A publisher sends messages continuously without knowing who listens. Suited to streams such as 30 Hz images or 50 Hz positions. Messages have defined types, for example
sensor_msgs/msg/Image. - Service: one request, one reply. Suited to short jobs such as switching camera mode.
- Action: a long-running goal with progress feedback and the option to cancel, such as flying to a destination.
- Parameters are node settings that can change at runtime, and a launch file starts several nodes with their settings in one command.
Underneath is DDS (Data Distribution Service), a publish/subscribe standard in which nodes discover each other on the network without a central server. Nodes with different ROS_DOMAIN_ID values cannot see each other, which is one way to separate several drones on the same network.
Quality of service (QoS)
Each topic has QoS settings. The important ones are:
- Reliability: reliable retransmits until delivery and suits commands. Best effort may drop messages but never waits, and suits sensor data where new values quickly replace old ones.
- History and depth: how many recent messages to keep. A deep queue feeds a slow subscriber stale data. The oldest message in a full queue is roughly depth divided by publish rate.
- Durability: transient local gives late-joining subscribers the last message already sent. Suits data that rarely changes, such as a map.
If publisher and subscriber QoS are incompatible, for example a best-effort publisher and a reliable subscriber, the subscriber receives nothing and no obvious error appears. This is the most common problem when first connecting to PX4.
Connecting ROS 2 to the flight stack
PX4 via uXRCE-DDS. A client runs on the flight controller and exposes internal uORB messages as ROS 2 topics. An agent runs on the companion computer and acts as the client's proxy in the DDS network. They connect over serial or a network link. Topics published by PX4 sit under /fmu/out/, for example /fmu/out/vehicle_odometry, and commands into PX4 sit under /fmu/in/, for example /fmu/in/trajectory_setpoint. The PX4 documentation says to subscribe with the ROS 2 sensor-data QoS profile rather than the default.
Different frames. PX4 uses NED for the world and FRD for the body. ROS 2 uses ENU and FLU, following REP 103. Fields of TrajectorySetpoint must be converted from ENU to NED before sending.
Offboard mode lets the companion computer supply targets. PX4 requires a continuous "proof of life" stream of at least 2 Hz (OffboardControlMode or setpoint messages) before entering the mode and throughout the flight. If it stops for longer than the COM_OF_LOSS_T parameter, PX4 triggers the failsafe set in COM_OBL_RC_ACT.
Alternatives. ArduPilot has its own DDS interface (AP_DDS). MAVROS bridges MAVLink and ROS and works with either flight stack over MAVLink, which suits existing systems already built on MAVLink.
Simulation. Test everything first in software-in-the-loop (SITL) simulation, for example PX4 with Gazebo, which runs the real flight stack on a computer and moves data along the same paths as on the real aircraft.
Example: a data budget and a frame conversion
Assume an onboard camera publishes 640 × 480 RGB images at 8 bits per channel (3 bytes per pixel) at 30 Hz. The data rate is 640 × 480 × 3 × 30 ≈ 27.6 MB/s, about 221 Mbit/s. Sending raw images over a wireless link to a ground computer will not fit. Process onboard and send results, or send compressed images.
If the image topic uses depth 10 at 30 Hz and the processing node falls behind, the oldest queued image can be about 10 / 30 ≈ 0.33 s old. For control, use depth 1 or 2 so the node always gets the latest frame.
A ROS 2 planner wants a point 5 m east, 10 m north and 20 m above the take-off point (ENU = 5, 10, 20). The value sent to PX4 in NED is (N, E, D) = (10, 5, −20). Without conversion PX4 would try to fly 5 m north, 10 m east and 20 m below the ground.
If total latency from camera to command is 0.12 s while flying at 5 m/s, the drone moves 0.6 m before the command takes effect. Obstacle avoidance must allow for this. All values are assumed.
Common pitfalls
- Incompatible QoS. The topic looks fine but no data arrives. Check with
ros2 topic info -v. - Forgetting ENU/NED and yaw conversion. Always test in simulation first.
- Irregular setpoints because a node stalls or is overloaded. PX4 will leave offboard mode.
- Putting hard real-time tasks in ROS 2. Stabilisation and failsafes belong in the flight stack. The companion computer only supplies targets.
- Version mismatch. PX4 message definitions in ROS 2 must match the firmware version, and all machines should run the same ROS 2 distribution.
Continue in the lessons
- UAT 308 Module 3: ROS 2 QoS and time synchronisation (four-year programme)
- UAT 314 Module 5: Robots with drones, ROS 2 and system integration (continuing programme)
- UAT 322 Module 1: Automation system integration (continuing programme)
Key references
- Open Robotics. ROS 2 documentation. link
- PX4 Autopilot. ROS 2 user guide (uXRCE-DDS bridge and frame conventions). PX4 user guide (main). link
- PX4 Autopilot. uXRCE-DDS (PX4-ROS 2/DDS bridge). PX4 user guide (main). link
- Macenski, S., Foote, T., Gerkey, B., Lalancette, C., & Woodall, W. (2022). Robot Operating System 2: Design, architecture, and uses in the wild. Science Robotics, 7(66), eabm6074. link
- MAVLink Development Team. MAVROS: MAVLink to ROS gateway (ROS 2). link
- Foote, T., & Purvis, M. (2010). REP 103: Standard units of measure and coordinate conventions. ROS Enhancement Proposals. link