本文主要內容翻譯自官方文檔: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流程和其中關鍵函數的解析。