文件组织结构

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

效果如下图所示
在这里插入图片描述

Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐