PX4 uORB Messaging: What It Is and How It Works
uORB messaging is a lightweight publish-subscribe communication system used within the PX4 autopilot to exchange data between different modules. It allows components like sensors, controllers, and estimators to send and receive messages asynchronously without direct connections.How It Works
Imagine a busy office where different teams need to share information quickly without calling each other directly. PX4's uORB messaging works like a bulletin board where teams post updates and others read them when needed. This system uses a publish-subscribe model: modules publish messages to topics, and other modules subscribe to those topics to receive updates.
Each message topic represents a specific type of data, such as sensor readings or control commands. When a module publishes a message, uORB stores it and notifies subscribers. This design keeps communication efficient and decoupled, so modules can work independently and only react when new data arrives.
Example
This example shows how a PX4 module publishes and subscribes to a sensor_combined message, which contains sensor data.
#include <uORB/uORB.h> #include <uORB/topics/sensor_combined.h> #include <px4_platform_common/px4_config.h> #include <px4_platform_common/px4_work_queue/ScheduledWorkItem.hpp> #include <px4_platform_common/hrt.h> #include <cstdio> int main() { // Advertise (publish) sensor data orb_advert_t pub = orb_advertise(ORB_ID(sensor_combined), nullptr); sensor_combined_s sensor_data{}; sensor_data.timestamp = hrt_absolute_time(); sensor_data.accelerometer_m_s2[0] = 0.1f; sensor_data.accelerometer_m_s2[1] = 0.2f; sensor_data.accelerometer_m_s2[2] = 9.8f; orb_publish(ORB_ID(sensor_combined), pub, &sensor_data); // Subscribe to sensor data int sub = orb_subscribe(ORB_ID(sensor_combined)); sensor_combined_s received_data{}; // Check for updates bool updated = false; orb_check(sub, &updated); if (updated) { orb_copy(ORB_ID(sensor_combined), sub, &received_data); printf("Accelerometer X: %.2f m/s^2\n", (double)received_data.accelerometer_m_s2[0]); } orb_unsubscribe(sub); orb_unadvertise(pub); return 0; }
When to Use
Use uORB messaging in PX4 when you need modules to share data efficiently without tight coupling. It is ideal for sensor data distribution, control commands, and state estimation updates. For example, the sensor driver publishes raw data, and the estimator subscribes to it to compute the vehicle's position.
This system is essential in drone autopilots to keep components modular, maintainable, and responsive to real-time data changes.
Key Points
- uORB is a publish-subscribe messaging system inside PX4.
- It enables asynchronous communication between modules.
- Messages are organized by topics representing data types.
- Modules publish data and others subscribe to receive updates.
- This design supports modular and efficient drone software architecture.