zoukankan      html  css  js  c++  java
  • [pixhawk笔记]5-uORB消息传递

    本文主要内容翻译自官方文档: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流程和其中关键函数的解析。

  • 相关阅读:
    Solr4.8.0源码分析(12)之Lucene的索引文件(5)
    JAVA基础(1)之hashCode()
    Solr4.8.0源码分析(11)之Lucene的索引文件(4)
    检查数据不一致脚本
    索引的新理解
    数据库放到容器里
    动态规划
    每天晚上一个动态规划
    postgresql parallel join example
    名不正,则言不顺
  • 原文地址:https://www.cnblogs.com/spyplus/p/7401555.html
Copyright © 2011-2022 走看看