Device Drivers
PX4 device drivers are based on the Device framework.
Creating a Driver
PX4 almost exclusively consumes data from uORB. Drivers for common peripheral types must publish the correct uORB messages (for example: gyro, accelerometer, pressure sensors, etc.).
The best approach for creating a new driver is to start with a similar driver as a template (see src/drivers).
:::note More detailed information about working with specific I/O buses and sensors may be available in Sensor and Actuator Buses section. :::
:::note Publishing the correct uORB topics is the only pattern that drivers must follow. :::
Core Architecture
PX4 is a reactive system and uses uORB publish/subscribe to transport messages. File handles are not required or used for the core operation of the system. Two main APIs are used:
The publish / subscribe system which has a file, network or shared memory backend depending on the system PX4 runs on.
The global device registry, which can be used to enumerate devices and get/set their configuration. This can be as simple as a linked list or map to the file system.
Device IDs
PX4 uses device IDs to identify individual sensors consistently across the system. These IDs are stored in the configuration parameters and used to match sensor calibration values, as well as to determine which sensor is logged to which logfile entry.
The order of sensors (e.g. if there is a /dev/mag0
and an alternate /dev/mag1
) does not determine priority - the priority is instead stored as part of the published uORB topic.
Decoding example
For the example of three magnetometers on a system, use the flight log (.px4log) to dump the parameters. The three parameters encode the sensor IDs and MAG_PRIME
identifies which magnetometer is selected as the primary sensor. Each MAGx_ID is a 24bit number and should be padded left with zeros for manual decoding.
This is the external HMC5983 connected via I2C, bus 1 at address 0x1E
: It will show up in the log file as IMU.MagX
.
This is the internal HMC5983 connected via SPI, bus 1, slave select slot 5. It will show up in the log file as IMU1.MagX
.
And this is the internal MPU9250 magnetometer connected via SPI, bus 1, slave select slot 4. It will show up in the log file as IMU2.MagX
.
Device ID Encoding
The device ID is a 24bit number according to this format. Note that the first fields are the least significant bits in the decoding example above.
The bus_type
is decoded according to:
and devtype
is decoded according to:
Debugging
For general debugging topics see: Debugging/Logging.
Verbose Logging
Drivers (and other modules) output minimally verbose logs strings by default (e.g. for PX4_DEBUG
, PX4_WARN
, PX4_ERR
, etc.).
Log verbosity is defined at build time using the RELEASE_BUILD
(default), DEBUG_BUILD
(verbose) or TRACE_BUILD
(extremely verbose) macros.
Change the logging level using COMPILE_FLAGS
in the driver px4_add_module
function (CMakeLists.txt). The code fragment below shows the required change to enable DEBUG_BUILD level debugging for a single module or driver.
:::tip Verbose logging can also be enabled on a per-file basis, by adding #define DEBUG_BUILD
at the very top of a .cpp file (before any includes). :::
Last updated