[pixhawk笔记]5-uORB消息传递

2023-05-16

本文主要内容翻译自官方文档:https://dev.px4.io/en/middleware/uorb.html

在前一篇笔记中使用uORB完成消息传递,实现了一个简单示例程序,本文将对uORB进行系统学习。

uORB是一种异步发布(publish)/订阅(subscribe)机制的消息API,该机制用于在线程/进程之间通信。uORB在其他程序启动之前自动启动,因为其他很多程序依赖于他。

使用uorb start命令启动它,可以使用uorb_tests开始单元测试。

  • 加入一个新主题
    可以在msg/文件夹里新建一个.msg文件来创建一个新主题,并在msg/CMakeLists.txt中加入,则在编译时会自动生成对应的C/C++代码。
    例如下面是vehicle_attitude.msg中的内容:
    
    # This is similar to the mavlink message ATTITUDE_QUATERNION, but for onboard use
    float32 rollspeed    # Angular velocity about body north axis (x) in rad/s
    float32 pitchspeed    # Angular velocity about body east axis (y) in rad/s
    float32 yawspeed    # Angular velocity about body down axis (z) in rad/s
    float32[4] q        # Quaternion (NED)
    
    # TOPICS vehicle_attitude vehicle_attitude_groundtruth vehicle_vision_attitude  

    在编译后,将在编译目录的src/modules/uORB/topics文件夹下生成vehicle_attitude.h头文件,内容如下:

    
    /****************************************************************************
     *
     *   Copyright (C) 2013-2016 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.
     *
     ****************************************************************************/
    
    /* Auto-generated by genmsg_cpp from file /home/spy/src/Firmware/msg/vehicle_attitude.msg */
    
    
    #pragma once
    
    #include <stdint.h>
    #ifdef __cplusplus
    #include <cstring>
    #else
    #include <string.h>
    #endif
    
    #include <uORB/uORB.h>
    
    
    #ifndef __cplusplus
    
    #endif
    
    
    #ifdef __cplusplus
    struct __EXPORT vehicle_attitude_s {
    #else
    struct vehicle_attitude_s {
    #endif
        uint64_t timestamp; // required for logger
        float roll;
        float pitch;
        float yaw;
        float q[4];
        uint8_t _padding0[4]; // required for logger
    
    #ifdef __cplusplus
    
    #endif
    };
    
    /* register this as object request broker structure */
    ORB_DECLARE(vehicle_attitude);
    ORB_DECLARE(vehicle_attitude_groundtruth);
    ORB_DECLARE(vehicle_vision_attitude);  

    对于生成的每个C/C++结构体,将会自动添加一个uint64_t timestamp成员,该成员用于logger模块记录数据,所以确保在发布消息时给该成员赋值。
    为了在代码里使用该主题,需要包含如下头文件:

    #include <uORB/topics/topic_name.h>
    

    在.msg的代码中加入如下行,则一条msg定义可以被多个独立的主题实例所使用:

    # TOPICS mission offboard_mission onboard_mission
    

    之后可以在代码中使用 ORB_ID(offboard_mission)来获取主题ID。  

  • 发布主题
    可以在系统中的任何位置发布主题,包括中断内容中(hrt_call API调用的函数)。但是,只能在中断内容之外广播主题。必须同一进程中广播主题,以便随后发布他(不太理解这句话的意思)。

  • 列出和监听主题
    要列出所有的主题,使用
    ls /obj
    

    笔者在飞控刚开机时使用qgroundcontrol的MAVLink控制台,得到所有主题列表如下图:

    
    nsh> ls /obj
    /obj:
     _obj_
     actuator_armed0
     actuator_controls_00
     actuator_controls_10
     actuator_controls_20
     actuator_controls_30
     actuator_outputs0
     actuator_outputs1
     adc_report0
     airspeed0
     att_pos_mocap0
     battery_status0
     camera_capture0
     camera_trigger0
     commander_state0
     control_state0
     cpuload0
     differential_pressure0
     distance_sensor0
     ekf2_innovations0
     ekf2_timestamps0
     esc_status0
     estimator_status0
     fw_pos_ctrl_status0
     geofence_result0
     gps_dump0
     gps_inject_data0
     home_position0
     input_rc0
     led_control0
     log_message0
     manual_control_setpoint0
     mavlink_log0
     mc_att_ctrl_status0
     mission_result0
     multirotor_motor_limits0
     multirotor_motor_limits1
     offboard_control_mode0
     offboard_mission0
     onboard_mission0
     optical_flow0
     parameter_update0
     position_setpoint_triplet0
     power_button_state0
     rc_channels0
     rc_parameter_map0
     safety0
     satellite_info0
     sensor_accel0
     sensor_accel1
     sensor_accel2
     sensor_baro0
     sensor_baro1
     sensor_combined0
     sensor_correction0
     sensor_gyro0
     sensor_gyro1
     sensor_gyro2
     sensor_mag0
     sensor_mag1
     sensor_mag2
     sensor_mag3
     sensor_preflight0
     sensor_selection0
     servorail_status0
     subsystem_info0
     system_power0
     task_stack_info0
     tecs_status0
     telemetry_status0
     uavcan_parameter_request0
     uavcan_parameter_value0
     vehicle_attitude0
     vehicle_attitude_setpoint0
     vehicle_command0
     vehicle_command_ack0
     vehicle_control_mode0
     vehicle_global_position0
     vehicle_gps_position0
     vehicle_land_detected0
     vehicle_local_position0
     vehicle_local_position_setpoint0
     vehicle_rates_setpoint0
     vehicle_status0
     vehicle_status_flags0
     vehicle_vision_attitude0
     vehicle_vision_position0
     vtol_vehicle_status0
     wind_estimate0  

    使用 listener 可以监听主题:

    
    listener sensor_accel 5  

    文档中给出listener只能在Pixracer中使用,不过笔者使用pixhawk2.1运行pixhawk固件,也可以运行listener命令
    结果如下:

    
    nsh> listener sensor_accel 5
    
    TOPIC: sensor_accel instance 0 #1
    timestamp: 45504766
    integral_dt: 4000
    error_count: 0
    x:   0.0126
    y:   0.4502
    z:  -9.7865
    x_integral:   0.0001
    y_integral:   0.0018
    z_integral:  -0.0392
    temperature:  44.7562
    range_m_s2: 156.9064
    scaling:   0.0048
    x_raw: 41
    y_raw: -123
    z_raw: -2067
    temperature_raw: 3522
    device_id: 1442082
    
    TOPIC: sensor_accel instance 0 #2
    timestamp: 45564766
    integral_dt: 3991
    error_count: 0
    x:   0.0208
    y:   0.4487
    z:  -9.7956
    x_integral:   0.0001
    y_integral:   0.0018
    z_integral:  -0.0390
    temperature:  44.7424
    range_m_s2: 156.9064
    scaling:   0.0048
    x_raw: 45
    y_raw: -121
    z_raw: -2067
    temperature_raw: 3517
    device_id: 1442082
    
    TOPIC: sensor_accel instance 0 #3
    timestamp: 45620766
    integral_dt: 4000
    error_count: 0
    x:   0.0133
    y:   0.4447
    z:  -9.7794
    x_integral:   0.0000
    y_integral:   0.0018
    z_integral:  -0.0391
    temperature:  44.7368
    range_m_s2: 156.9064
    scaling:   0.0048
    x_raw: 44
    y_raw: -122
    z_raw: -2064
    temperature_raw: 3515
    device_id: 1442082
    
    TOPIC: sensor_accel instance 0 #4
    timestamp: 45670381
    integral_dt: 4013
    error_count: 0
    x:   0.0179
    y:   0.4408
    z:  -9.7816
    x_integral:   0.0000
    y_integral:   0.0017
    z_integral:  -0.0393
    temperature:  44.7673
    range_m_s2: 156.9064
    scaling:   0.0048
    x_raw: 47
    y_raw: -121
    z_raw: -2065
    temperature_raw: 3526
    device_id: 1442082
    
    TOPIC: sensor_accel instance 0 #5
    timestamp: 45730379
    integral_dt: 4000
    error_count: 0
    x:   0.0164
    y:   0.4363
    z:  -9.7636
    x_integral:   0.0000
    y_integral:   0.0018
    z_integral:  -0.0390
    temperature:  44.7645
    range_m_s2: 156.9064
    scaling:   0.0048
    x_raw: 49
    y_raw: -123
    z_raw: -2062
    temperature_raw: 3525
    device_id: 1442082  



  • uorb top 指令
    uorb top指令用来实时显示每个主题的发布频率,输出如下:
    
    nsh> uorb top
    
    update: 1s, num topics: 86
    TOPIC NAME                    INST #SUB #MSG #LOST #QSIZE
    sensor_baro                      0    2   66    22 1
    sensor_baro                      1    1   67     0 1
    sensor_mag                       0    1   42     0 1
    vehicle_gps_position             0    6    4    11 1
    sensor_mag                       1    1  108     8 1
    sensor_accel                     0    1  248     0 1
    sensor_gyro                      0    2  248     0 1
    sensor_gyro                      1    2  238     7 1
    sensor_mag                       2    1   99     0 1
    sensor_accel                     1    1  223     0 1
    sensor_mag                       3    1   97     2 1
    sensor_accel                     2    1  250     2 1
    sensor_gyro                      2    2  250     2 1
    adc_report                       0    1   99     0 1
    system_power                     0    2   99    16 1
    vehicle_control_mode             0    7    4     0 1
    actuator_controls_0              0    7  248  1022 1
    sensor_combined                  0    6  248   927 1
    sensor_preflight                 0    1  248     0 1
    battery_status                   0    6   82   218 1
    vehicle_status                   0    8    4     4 1
    actuator_armed                   0    7    4     2 1
    safety                           0    1   41     0 1
    vehicle_local_position           0    7  248   712 1
    vehicle_attitude                 0    5  248   836 1
    vehicle_status_flags             0    0   83     0 1  
    其中,每列分别是:主题名称,多实例索引,订阅者数量,发布频率,丢包数(所有订阅者合起来的)和队列大小。

  • 多实例
    uORB提供了一种通过orb_advertise_multi函数来发布同一个主题的多个独立实例的机制。该函数将会返回一个实例索引给发布者。订阅者必须使用orb_subscribe_multi函数并提供实例索引来指定订阅哪个实例(使用orb_subscribe可以订阅第一个实例)。拥有多个实例是十分有用的,比如系统存在多个同类型的传感器时。
    注意对于同一主题,不要混淆orb_advertise_multi和orb_advertise。
    完整的API在src/modules/uORB/uORBManager.hpp中给出。
  • 问题和陷阱
    下面是几种常见的问题和陷阱:
    • 主题没有被发布:确保每个ORB_ID()对应好。有一点比较重要的是必须在orb_publish函数所在的任务中调用orb_subscribe和orb_unsubscribe。
    • 必须使用orb_unsubscribe和orb_unadvertise来清理订阅和广播。
    • 成功调用orb_check或px4_poll后必须使用orb_copy()函数,否则下一个阻塞将会立即返回。
    • 在主题被广播之前就订阅该主题是没有任何问题的。
    • orb_check()和px4_poll()只对订阅后的信息发布返回true。这对不是规律性发布的主题是很重要的。如果一个订阅者需要使用前一组数据,则在orb_subscribe()之后调用orb_copy()即可(在主题没有被发布时,orb_copy()函数会执行失败)。

上文中的内容主要是官方文档里的,给出了uORB的概述,但是没有给出关键函数的解析,由于这部分比较重要,下一篇中笔者将结合代码和文档给出uORB流程和其中关键函数的解析。

转载于:https://www.cnblogs.com/spyplus/p/7401555.html

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

[pixhawk笔记]5-uORB消息传递 的相关文章

  • RGB-D相机建图——3、使用kalibr进行相机标定

    Kalibr 视觉惯性校准工具箱 官方网站 xff1a https github com ethz asl kalibr Kalibr是一个工具箱 xff0c 可以解决以下校准问题 xff1a 多摄像机校准 xff1a 具有非全局共享重叠视
  • 02.构建项目流程梳理及总结

    02 构建项目流程梳理及总结
  • Nuttx学习入门

    Nuttx学习 NuttX 是一个实时操作系统 RTOS xff0c 强调标准合规性和占用空间小 可从 8 位扩展到 64 位微控制器环境 xff0c NuttX 中的主要管理标准是 POSIX 和 ANSI 标准 NuttX 的主要环境依
  • 软件测试之如何介绍自己的项目

    测试人员在找工作的过程中 xff0c 通常有一个问题是很难绕开的 就是要如何向别人介绍自己之前做过的项目 要解决这个问题 xff0c 大致可以分为如下几个步骤 xff1a 1 对项目进行基本介绍 2 说明自己负责测试的模块 3 针对部分模块
  • FreeRTOS多任务管理

    文章目录 1 任务1 1 任务简介1 2 任务调度1 3 任务的状态 就绪态 运行态 阻塞态 挂起态 1 4 空闲任务 2 动态创建两个任务2 1 定义动态内存空间的堆2 2 定义任务函数2 3 定义 任务控制块 指针2 4 动态创建任务
  • 计算机类期刊投稿心得 [ 添加中...现35种 ]

    1 杂志名称 计算机应用研究 杂志文章包含专业 建模 xff0c 仿真 xff0c 网络 xff0c 人工智能 xff0c 比较杂 投稿联系方式 http www arocmag com 注册在线投稿审稿 投稿费用 250元 页 杂志级别
  • Minix下的汇编

    Minix下的汇编 大多数的编译器 xff0c 如Turbo C C 43 43 xff0c Borland C C 43 43 xff0c M C C 43 43 xff0c GCC xff0c VC 43 xff0c 编译过程都是 xf
  • 解决Xshell 7 报错 “要继续使用此程序,您必须应用最新的更新或使用新版本”

    1 先创建一个文本文档 xff0c 同时把该文档名称和后缀改为xshell7 bat xff1b 2 打开编辑这个xshell7 bat文件 xff0c 并且把以下文字复制进去 xff0c 注意set XSHELL 61 这一项需要改成你自
  • 多任务操作系统的任务切换

    在学习OS时 xff0c 对于多任务操作系统的任务切换 xff0c 一直不能理解 xff1a 控制权是怎么么回到调度程序上的 xff1f 记得在描述任务切换时 xff0c 一般都是这么描述的 xff1a 在每一个时钟滴答 xff0c 都将检
  • Minix下的汇编2

    似乎minix平台并没有带一个真正的汇编编译器 xff0c 看看makefile xff0c 几乎都是清一色的用cc来编译汇编代码的 而且 xff0c 即使是一个最简单功能的汇编程序 xff0c 也少不了一个 main 标签 在minix的
  • 原来在/var/spool/mail中

    fetchmail会把从mail server收到的邮件投递到 var spool mail 中去 而mutt也会自动地到 var spool mail里取信 xff0c 解码 xff0c 并显示 但 xff0c fetchmail的速度不
  • 汉字编码标准与识别(一)代码页(Code Page)初识

    BBS水木清华站 精华区 发信人 yanglc 魂归燕园 别理我 xff0c 烦着呢 信区 Linux 标 题 汉字编码标准与识别 一 发信站 BBS 水木清华站 Sat Apr 29 17 19 05 2000 http www linu
  • 让xpdf支持中文(C++primer中文版)

    首先到http www linuxfans org nuke modules php name 61 Site Downloads amp op 61 geninfo amp did 61 2385下载一个打了补丁的xpdf 安装 xff0
  • Xpdf-3 for MDK

    http www linuxfans org nuke modules php name 61 Site Downloads amp op 61 geninfo amp did 61 2385 Xpdf 3 for MDK 类别 其它软件
  • 不同公司的牛

    本文转自 C 43 43 Builder 研究 http www ccrun com other go asp i 61 264 amp d 61 sgz5id 传统公司 xff1a 你有两头母牛 你卖掉一头 xff0c 买了一头公牛 你的
  • 从词法分析开始

    刚开始时 xff0c 用lex的确是很方便 xff0c 但是这样却不能将词法分析的思想实践出来 最好的方法还是自己写一个lex 当然龙书上写得很详细了 xff0c 但是写得再详细 xff0c 把它实现出来还是很难的 我的计划是 xff1a
  • Python 获取当前路径几种方法

    Python 获取当前路径的几种方法 绝对路径 1 os path 方法 span class token comment coding utf 8 span span class token comment usr bin python
  • [pixhawk笔记]2-飞行模式

    本文翻译自px4官方开发文档 xff1a https dev px4 io en concept flight modes html xff0c 有不对之处 xff0c 敬请指正 pixhawk的飞行模式如下 xff1a MANUAL xf
  • 扩展卡尔曼滤波详解

    Extened Kalman Filter 简单介绍 卡尔曼滤波详解讲解的基本的卡尔曼滤波算法是通过一个线性随机差分方程来估计当前状态 xff0c 但是如果状态估计关系以及测量关系使非线性的怎么办 xff0c 而且在实际使用中大部分的问题都
  • 关于PX4中的高度若干问题

    飞行的高度是如何测量的 xff1f 地面的高度和海平面的高度差别很大 xff0c 飞控又是如何有效判别进行降落的 xff1f 这是我脑子里的疑问 搜索的一圈发现很少有人讨论这方面的问题 xff0c 于是本次我就直接看一下源代码 xff0c

随机推荐