PX4二次开发-订阅传感器数据
·
文件组织结构
sugar@sugar-virtual-machine:~/PX4/Firmware/src/examples/px4_sub_sensor$ ls
CMakeLists.txt Kconfig px4_sub_sensor.c
CMakeLists.txt
px4_add_module(
MODULE examples__px4_sub_sensor
MAIN px4_sub_sensor
STACK_MAIN 2000
SRCS
px4_sub_sensor.c
DEPENDS
)```
# Kconfig
```bash
menuconfig EXAMPLES_PX4_SUB_SENSOR
bool "px4_sub_sensor"
default n
---help---
Enable support for px4_sub_sensor
px4_sub_sensor.c
#include <px4_platform_common/px4_config.h>
#include <px4_platform_common/posix.h>
#include <px4_platform_common/log.h> // ✅ 加上这个头文件
#include <uORB/uORB.h>
#include <uORB/topics/sensor_combined.h>
#include <poll.h>
#include <stdio.h>
__EXPORT int px4_sub_sensor_main(int argc, char *argv[]);
int px4_sub_sensor_main(int argc, char *argv[])
{
int sensor_sub_fd = orb_subscribe(ORB_ID(sensor_combined));
px4_pollfd_struct_t fds[] = {
{ .fd = sensor_sub_fd, .events = POLLIN },
};
while (true) {
int poll_ret = px4_poll(fds, 1, 1000);
if (poll_ret == 0) {
PX4_WARN("No data within 1 second");
} else if (poll_ret < 0) {
PX4_ERR("poll error"); // ✅ 正确用法
} else {
if (fds[0].revents & POLLIN) {
struct sensor_combined_s raw;
orb_copy(ORB_ID(sensor_combined), sensor_sub_fd, &raw);
PX4_INFO("Accel: [%.3f, %.3f, %.3f]", (double)raw.accelerometer_m_s2[0],
(double)raw.accelerometer_m_s2[1], (double)raw.accelerometer_m_s2[2]);
}
}
}
return 0;
}
编译
添加到相应的编译脚本中

增加到最后

我使用的飞控硬件为CUAV NORA+,编译命令为
make cuav_nora_default
运行方式
飞控固件上传到QGC中,然后
px4_sub_sensor start
效果如下图所示

更多推荐



所有评论(0)