PX4模块设计之三:自定义uORB消息

2023-05-16

PX4模块设计之三:自定义uORB消息

  • 1. 新增自定义uORB消息步骤
  • 2. 应用ext_hello_world消息示例
  • 3. 编译执行结果
  • 4. 参考资料

基于PX4开源软件框架简明简介和PX4模块设计之二:uORB消息代理, 了解了更多关于中间件uORB消息代理作为PX4系统内部消息传递的重要性。

关于新增消息或者消息主题,可以分为PX4代码库内部新增(通常是PX4开源组织在进行维护)和外部新增两种方式。

这里自定义uORB消息主要侧重的是外部新增主题消息,后续将会结合一个示例进行相关应用的Demo。

1. 新增自定义uORB消息步骤

  1. 新增与PX4-Autopilot平行目录PX4-ExternalModule;
  2. 建立PX4-ExternalModule下相应的msg、src目录;
  3. 在PX4-ExternalModule的msg目录下新建.msg文件,参考PX4-Autopilot下msg目录下的相关文件;
1 uint64 timestamp	# time since system start (microseconds)
2 int8 hello          # index of hello variable
3 
4 # TOPICS ext_hello_world
  1. 在PX4-ExternalModule的msg目录下新建CMakeLists.txt文件;
  2. 在CMakeList.txt文件中添加.msg文件;
34 set(config_msg_list_external
35    ext_hello_world.msg
36    PARENT_SCOPE
37    )
  1. 在make的时候,框架代码会自动根据上述源文件生成对应的两个C/C++文件;
PX4-Autopilot\build\px4_sitl_default\uORB\topics\ext_hello_world.h
PX4-Autopilot\build\px4_sitl_default\msg\topics_sources\ext_hello_world.cpp

注:自此已经完成新增自定义uORB消息ext_hello_world。

2. 应用ext_hello_world消息示例

定义消息的目的主要是为了不同的任务之间进行通信,这里写了一个Demo:两个任务,一个任务publish消息,一个任务subscribe消息;

  1. 在Demo代码里面包含头文件;
#include <uORB/topics/ext_hello_world.h>
  1. 使用ORB_ID(ext_hello_world)唯一定位消息主题;
  2. 使用px4_task_spawn_cmd函数建立两个任务;
  3. 使用uORB消息处理接口函数Demo应用;
/****************************************************************************
 *
 *   Copyright (c) 2012-2019 PX4 Development Team. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name PX4 nor the names of its contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 ****************************************************************************/

/**
 * @file px4_custom_uorb.c
 * extern custom uorb example for PX4 autopilot
 *
 * @author Example User <lida-mail@163.com>
 */

#include <px4_platform_common/px4_config.h>
#include <px4_platform_common/tasks.h>
#include <px4_platform_common/posix.h>
#include <unistd.h>
#include <stdio.h>
#include <poll.h>
#include <string.h>
#include <math.h>
#include <sys/types.h>
#include <stdlib.h>
#include <sched.h>
#include <errno.h>


#include <uORB/uORB.h>
#include <uORB/topics/ext_hello_world.h>

#define TASK_NAME_CUSTOM_UORB_SUBSCRIBE  "orb subscribe"
#define TASK_NAME_CUSTOM_UORB_PUBLISH    "orb publish"

#define TASK_SUBSCRIBE_NUM                8
#define TASK_PUBLISH_NUM                  5

__EXPORT int px4_custom_uorb_main(int argc, char *argv[]);

int px4_custom_uorb_subscribe(int argc, char *argv[])
{
	/* listen */
	int hello_fd = orb_subscribe(ORB_ID(ext_hello_world));
	/* limit the update rate to 5 Hz */
	orb_set_interval(hello_fd, 200);
	/* one could wait for multiple topics with this technique, just using one here */
	px4_pollfd_struct_t fds[] = {
		{ .fd = hello_fd,   .events = POLLIN },
		/* there could be more file descriptors here, in the form like:
		 * { .fd = other_sub_fd,   .events = POLLIN },
		 */
	};

	int error_counter = 0;

	for (int i = 0; i < TASK_SUBSCRIBE_NUM; i++) {
		/* wait for sensor update of 1 file descriptor for 1000 ms (1 second) */
		int poll_ret = px4_poll(fds, 1, 5000);

		/* handle the poll result */
		if (poll_ret == 0) {
			/* this means none of our providers is giving us data */
			PX4_ERR("Got no data within a second");

		} else if (poll_ret < 0) {
			/* this is seriously bad - should be an emergency */
			if (error_counter < 10 || error_counter % 50 == 0) {
				/* use a counter to prevent flooding (and slowing us down) */
				PX4_ERR("ERROR return value from poll(): %d", poll_ret);
			}

			error_counter++;

		} else {

			if (fds[0].revents & POLLIN) {
				/* obtained data for the first file descriptor */
				struct ext_hello_world_s tmp;
				/* copy sensors raw data into local buffer */
				orb_copy(ORB_ID(ext_hello_world), hello_fd, &tmp);
				PX4_INFO("Hello world(subscribe):\t%d", tmp.hello);
			}
			
			/* there could be more file descriptors here, in the form like:
			 * if (fds[1..n].revents & POLLIN) {}
			 */
		}
	}
	
	PX4_INFO("px4_custom_uorb_subscribe exiting");
	
	return 0;
}

int px4_custom_uorb_publish(int argc, char *argv[])
{
	/* advertise */
	struct ext_hello_world_s hello_world;
	memset(&hello_world, 0, sizeof(hello_world));
	orb_advert_t hello_world_pub = orb_advertise(ORB_ID(ext_hello_world), &hello_world);
	
	for (int i = 0; i < TASK_PUBLISH_NUM; i++) {
		
		/* Wait for subscriber */
		sleep(5);

		/* set att and publish this information for other apps
		 the following does not have any meaning, it's just an example
		*/
		hello_world.hello = i + 1;

		PX4_INFO("Hello world(puslish):\t%d", hello_world.hello);
		orb_publish(ORB_ID(ext_hello_world), hello_world_pub, &hello_world);
	}
	
	PX4_INFO("px4_custom_uorb_publish exiting");
	
	return 0;
}

int px4_custom_uorb_main(int argc, char *argv[])
{
	int task_subscribe, task_publish;

	task_publish = px4_task_spawn_cmd(TASK_NAME_CUSTOM_UORB_PUBLISH, SCHED_DEFAULT, SCHED_PRIORITY_DEFAULT, 2000, px4_custom_uorb_publish, argv);
	PX4_INFO("creating %s task_publish = %d", TASK_NAME_CUSTOM_UORB_PUBLISH, task_publish);
	
	sleep(1);
		
	task_subscribe = px4_task_spawn_cmd(TASK_NAME_CUSTOM_UORB_SUBSCRIBE, SCHED_DEFAULT, SCHED_PRIORITY_DEFAULT, 2000, px4_custom_uorb_subscribe, argv);
	PX4_INFO("creating %s task_subscribe = %d", TASK_NAME_CUSTOM_UORB_SUBSCRIBE, task_subscribe);
	
	while(px4_task_is_running(TASK_NAME_CUSTOM_UORB_SUBSCRIBE) || px4_task_is_running(TASK_NAME_CUSTOM_UORB_PUBLISH)){
		sleep(1);
	}

	PX4_INFO("px4_custom_uorb_main exiting");

	return 0;
}

整个DEMO结构如下所示:

目录结构
 ├── PX4-Autopilot
 │      └── build/px4_sitl_default
 │          ├── external_modules
 │          │    └── examples
 │          │         └── px4_custom_uorb/libexamples__px4_custom_uorb.a
 │          ├── uORB
 │          │    └── topics/ext_hello_world.h
 │          └── msg
 │               └── ttopics_sources/ext_hello_world.cpp
 └── PX4-ExternalModule
        ├── msg
        │   ├── CMakeLists.txt
        │   └── ext_hello_world.msg
        └── src
            ├── CMakeLists.txt
            └── examples
                └── px4_custom_uorb
                    ├── CMakeLists.txt
                    ├── Kconfig
                    └── px4_custom_uorb.c

完整的PX4-ExternalModule代码,请链接下载。

3. 编译执行结果

基于SITL仿真环境,在PX4-Autopilot目录下,进行编译;

$ make px4_sitl EXTERNAL_MODULES_LOCATION=../PX4-ExternalModule/

然后,执行仿真环境;

$ make px4_sitl jmavsim

最后,测试下效果。

pxh> px4_custom_uorb 
INFO  [px4_custom_uorb] creating orb publish task_publish = 13
INFO  [px4_custom_uorb] creating orb subscribe task_subscribe = 14
INFO  [px4_custom_uorb] Hello world(subscribe):	0
INFO  [px4_custom_uorb] Hello world(puslish):	1
INFO  [px4_custom_uorb] Hello world(subscribe):	1
INFO  [px4_custom_uorb] Hello world(puslish):	2
INFO  [px4_custom_uorb] Hello world(subscribe):	2
INFO  [px4_custom_uorb] Hello world(puslish):	3
INFO  [px4_custom_uorb] Hello world(subscribe):	3
INFO  [px4_custom_uorb] Hello world(puslish):	4
INFO  [px4_custom_uorb] Hello world(subscribe):	4
INFO  [px4_custom_uorb] Hello world(puslish):	5
INFO  [px4_custom_uorb] px4_custom_uorb_publish exiting
INFO  [px4_custom_uorb] Hello world(subscribe):	5
ERROR [px4_custom_uorb] Got no data within a second
ERROR [px4_custom_uorb] Got no data within a second
INFO  [px4_custom_uorb] px4_custom_uorb_subscribe exiting
INFO  [px4_custom_uorb] px4_custom_uorb_main exiting

4. 参考资料

【1】uorb messaging
【2】out of tree uorb message definitions

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

PX4模块设计之三:自定义uORB消息 的相关文章

  • PX4 Offboard Control with MAVROS--Takeoff(一键起飞)

    警告 xff1a 请先在仿真环境下进行测试 xff0c 能达到预期效果后在进行实际飞行测试 xff0c 以免发生意外 本篇文章只是用作学习交流 xff0c 实际飞行时如出现意外情况作者不予以负责 所需材料 1 PIXhawk或者Pixrac
  • px4自定义mavlink收不到消息的问题

    px4版本1 12稳定版 最近在做px4二次开发相关工作 按照网上的一些教程自定义了一个mavlink消息用来控制无人机 按照教程里面的单独开了一个xml来定义消息 最后生成的消息在px4端通过流传输的方式自己写的客户端可以收到消息 但是客
  • 【2020-8-9】APM,PX4,GAZEBO,MAVLINK,MAVROS,ROS之间的关系以及科研设备选型

    0 概述 无人机自主飞行平台可以分为四个部分 xff1a 动力平台 xff0c 飞行控制器 xff0c 机载电脑和模拟平台 动力平台 xff1a 负责执行飞行任务 xff0c 包括螺旋桨 电机 机架等 xff0c 用于科研的一般都是F380
  • PX4/Pixhawk---uORB深入理解和应用

    The Instructions of uORB PX4 Pixhawk 软件体系结构 uORB 主题发布 主题订阅 1 简介 1 1 PX4 Pixhawk的软件体系结构 PX4 Pixhawk的软件体系结构主要被分为四个层次 xff0c
  • PX4 GAZEBO无人机添加相机并进行图像识别

    PX4 GAZEBO无人机添加摄像头并进行图像识别 在之前完成了ROS的安装和PX4的安装 xff0c 并可以通过roslaunch启动软件仿真 接下来为无人及添加相机 xff0c 并将图像用python函数读取 xff0c 用于后续操作
  • PX4+Offboard模式+代码控制无人机起飞(Gazebo)

    参考PX4自动驾驶用户指南 https docs px4 io main zh ros mavros offboard cpp html 我的另一篇博客写了 键盘控制PX4无人机飞行 PX4无人机 键盘控制飞行代码 可以先借鉴本篇博客 xf
  • PX4无人机 - 键盘控制飞行代码

    PX4无人机 键盘控制飞行代码 仿真效果 实机效果 由于图片限制5M以内 xff0c 只能上传一小段了 xff0c 整段视频请点击链接 Pixhawk 6c 无人机 键盘控制无人机 Offboard模式 核心 xff1a 发布 mavros
  • PX4代码学习系列博客(6)——offboard模式位置控制代码分析

    分析offboard模式的代码需要用到以下几个模块 local position estimator mavlink mc pos control mc att control mixer 程序数据走向 mavlink 一般的offboar
  • PX4 ---- Mixer

    文章目录 Mixer 混合控制 作用输入输出装载混控文件MAVROS代码解析总结示例MAINAUX Mixer 混合控制 作用 经过位置控制和姿态控制后 xff0c 控制量通过 actuator controls发布 xff0c 其中 co
  • PX4模块设计之五:自定义MAVLink消息

    PX4模块设计之五 xff1a 自定义MAVLink消息 1 MAVLink Dialects1 1 PX4 Dialects1 2 Paprazzi Dialects1 3 MAVLink XML File Format 2 添加自定义M
  • PX4模块设计之十一:Built-In框架

    PX4模块设计之十一 xff1a Built In框架 1 Nuttx Built In框架2 PX4 Built In框架2 1 NSH Built In关联文件2 2 NSH Built In关联文件生成2 3 NSH Built In
  • PX4模块设计之十三:WorkQueue设计

    PX4模块设计之十三 xff1a WorkQueue设计 1 WorkQueue启动2 WorkQueue接口2 1 基本接口2 2 辅助接口2 3 WorkQueue任务函数2 3 1 Flat Build2 3 2 Protected
  • PX4模块设计之三十六:MulticopterPositionControl模块

    PX4模块设计之三十六 xff1a MulticopterPositionControl模块 1 MulticopterPositionControl模块简介2 模块入口函数2 1 主入口mc pos control main2 2 自定义
  • px4_simple_example和uorb机制

    px4 simple app PX4 Autopilot src exampes px4 simple app xff0c 这个程序是用c语言调用orb API和poll机制订阅和发布通讯数据 xff0c 但是这个例子并不是既有接收又有发送
  • uORB和MAVLink通讯例程

    uORB uORB 是一种异步 publish subscribe 的消息传递 API xff0c 用于进程或者线程间通信 IPC 添加新的Topic xff08 主题 xff09 在msg 目录下创建一个新的 msg文件 xff0c 并将
  • PX4-4-任务调度

    PX4所有的功能都封装在独立的模块中 xff0c uORB是任务间数据交互和同步的工具 xff0c 而管理和调度每个任务 xff0c PX4也提供了一套很好的机制 xff0c 这一篇我们分享PX4的任务调度机制 我们以PX4 1 11 3版
  • pixhawk px4 commander.cpp

    对于复杂的函数 xff0c 要做的就是看函数的输入是什么 来自哪里 xff0c 经过处理后得到什么 给谁用 xff0c 这样就可以把程序逻辑理清 中间的分析就是看函数如何处理的 span class hljs keyword extern
  • PX4飞控之自主返航(RTL)控制逻辑

    本文基于PX4飞控1 5 5版本 xff0c 分析导航模块中自护返航模式的控制逻辑和算法 自主返航模式和导航中的其他模式一样 xff0c 在Navigator main函数中一旦触发case vehicle status s NAVIGAT
  • PX4 OffBoard Control

    终于还是走上了这一步 xff0c 对飞控下手 xff0c 可以说是一张白纸了 记录一下学习的过程方便以后的查阅 目录 一 ubuntu18 04配置px4编译环境及mavros环境 二 PX4的OffBoard控制 1 搭建功能包 2 编写
  • 四、无人机知识笔记(初级:基本运动原理)

    笔记来源于 沈阳无距科技 工业级无人机的中国名片 编程外星人 目录 一 多旋翼直升机 二 基本飞行姿态 三 多旋翼飞行原理 四 反扭力与偏航运动 五 螺旋桨 六 有刷电机和无刷电机 七 电调与PWM信号 八 动力电池 九 遥控器 十 机架设

随机推荐

  • ESP32+Blufi+Tcp+UART

    ESP32 43 Blufi 43 TCP 43 UART 在上上一篇文章的基础上添加了蓝牙配网的功能 xff0c 第一次连接WiFi时 xff0c 通过手机微信小程序蓝牙配网 xff0c 第二次通过记录的WiFi内容就能够直接连接 参考的
  • ESP32+UART+TCP_Client+OTA

    在上一篇博客的基础上添加了OTA空中升级功能 实现的功能是一个WiFi DTU模块 xff0c ESP32板子通过串口与下位机连接 xff0c 我目前使用的下位机为51 xff0c ESP32第一次启动时 xff0c 通过微信小程序连接wi
  • ubuntu 网络编程

    ubuntu的网络编程 网络编程 一般有两种 TCP 和 UDP TCP 是面向连接层的传输协议 是无误的 数据没有丢失的失序 和 数据不会重复到达的 UDP是用户数据协议 是不可靠的无连接的协议 xff0c 但是可以进行高效率的传输 说起
  • 头文件intrins.h的用法

    KEIL 中 头文件 INTRINS H的作用 在 C51 单片机编程中 xff0c 头文件 INTRINS H 的函数使用起来 xff0c 就会让你像在用汇编时一样简便 内部函数 描述 crol 字符循环左移 cror 字符循环右移 ir
  • 4G DTU 透传模块简单使用方法

    不是打广告 xff0c 纯记录用途 最近由于项目需要 xff0c 买了一批4g 透传模块 众所周知 xff0c 两个4g模块一般不能直连 xff0c 需要中间通过搭建服务器来搭桥 卖家把桥搭好了 xff0c 自己简单配置下 xff0c 就可
  • ucos多任务与linux进程、多线程的比较分析

    从最初使用51片机 xff0c 再到avr msp430 xff0c arm7 arm9裸机 xff0c 单片机的处理速度越来越快 xff0c 而产品需求的日新月异 xff0c 在硬件成本 功耗 体积以及开发周期等的限制下 xff0c 开发
  • 【记录】Ubuntu下简单的软件安装-aptitude安装,samba安装,terminator软件

    Ubuntu下安装简单的软件 一 安装aptitude管理软件 sudo apt get install aptitude 这样可以使用aptitude来管理软件 如下命令 xff1a sudo aptitude update 更新软件源
  • freeRTOS开源项目crazyflie

    不小心接触到了开源飞控 xff0c 有一个小四轴的项目 xff1a crazyflie xff0c 有兴趣百度搜 使用的就是freeRTOS内核 xff0c 可以作为学习freeRTOS应用的一个参考 另外freeRTOS官方源码包里面就有
  • ROS serial串口通讯

    目录 ROS serial 串口通讯安装基本使用代码示例 Reference ROS serial 串口通讯 安装 sudo apt get install ros kinetic serial 基本使用 代码示例 include lt r
  • 四轴飞控DIY简明步骤介绍

    新手四轴飞控DIY组装简明步骤介绍 1 什么叫做新手 xff1f 2 新手如何思考 xff1f 3 上手步骤Step1 xff1a 四轴飞控介绍定义运动原理组成 Step2 xff1a 四轴飞控组装视频Step3 xff1a 四轴飞控新手规
  • BetaFlight开源工程结构简明介绍

    BetaFlight开源工程结构简明介绍 Step1 获取开源代码开源代码版本克隆开源代码 Step2 了解工程情况支持模型类型 xff1a 多旋翼 amp 固定翼支持特性 amp 功能安装 amp 文档链接配置工具下载其他介绍 xff08
  • 四轴FPV无人机手动操作简明介绍

    四轴FPV无人机手动操作简明介绍 通常航拍机都是有自稳算法 43 GPS导航 43 辅助功能 避障 的支持 xff0c 从而保证飞手能够相对容易且稳定的操作模型飞机 xff0c 通常通过阅读说明书都能很快上手 xff0c 这里就不在赘述 本
  • BetaFlight模块设计之三十:Cli模块分析

    BetaFlight模块设计之三十 xff1a Cli模块分析 Cli模块Cli接口Cli框架Cli命令结构主要函数分析cliProcess函数processCharacterInteractive函数processCharacter函数
  • PX4开发环境搭建--模拟器编译及QGroundControl & RC遥控模拟配置

    PX4开发环境搭建 模拟器编译 1 PX4开发环境介绍2 PX4开发环境搭建2 1代码下载2 2 国内环境调整2 3 建立ubuntu开发环境2 4 构建jMAVSim仿真2 5 补充版本信息 3 jmavsim仿真环境3 1 仿真命令集3
  • Android中的枚举

    在ARouter源码中发现使用到了枚举 xff0c 说明枚举并不是不常见的 xff0c 刚好枚举在我的视野中处于盲区 xff0c 于是打算周末加班给拿下 xff0c 扩展视野 了解枚举之前首先说一下什么是常量和变量 常量 声明后无法改变的量
  • PX4开源工程结构简明介绍

    PX4开源工程结构简明介绍 Step1 获取开源代码1 1 开源代码版本1 2 克隆开源代码 Step2 了解工程情况2 1 支持模型类型2 2 支持特性 amp 功能2 3 安装 amp 文档链接2 4 配置工具下载2 5 其他介绍 xf
  • PX4开源软件框架简明简介

    PX4开源软件框架简明简介 1 PX4系统构架1 1 飞控 43 地面站 RC控制1 2 飞控 43 伴飞电脑 43 地面站 集成RC控制 2 PX4软件构架2 1 设计概念2 2 软件构架2 1 中间件2 2 飞控代码 3 PX4运行环境
  • PX4模块设计之一:SITL & HITL模拟框架

    PX4模块设计之一 xff1a SITL amp HITL模拟框架 1 模拟框架1 1 SITL模拟框架1 2 HITL模拟框架 2 模拟器类型3 MAVLink API4 总结 基于PX4开源软件框架简明简介的框架设计 xff0c 逐步分
  • PX4模块设计之二:uORB消息代理

    PX4模块设计之二 xff1a uORB消息代理 1 uORB模块接口1 1 uORB服务接口1 2 uORB消息注册 去注册接口1 3 uORB消息发布接口1 4 uORB消息订阅 去订阅接口1 5 uORB辅助功能接口2 Hello W
  • PX4模块设计之三:自定义uORB消息

    PX4模块设计之三 xff1a 自定义uORB消息 1 新增自定义uORB消息步骤2 应用ext hello world消息示例3 编译执行结果4 参考资料 基于PX4开源软件框架简明简介和PX4模块设计之二 xff1a uORB消息代理