pixhawk飞控中添加uORB主题

本说明针对 Firmware v1.2.0
参考:
https://pixhawk.org/start?id=zh/dev/shared_object_communication

1. 添加流程说明

在Firmware/msg下新建uORB的成员变量。注:第一个成员为uint64 timestamp,作为话题运行的时间戳,格式为:xxxx.msg 。
1. 在Firmware/src/modules/uORB中的object_common.cpp添加所需要的主题。在编译结束后,能查看到所定义的结构体,并且经过ORB_DECLARE。
2. 在Firmware/cmake中的ros-CMakeLists.txt中添加话题的xxxx.msg ,作为cmake的编译索引。
2. 添加话题示例

在Firmware/msg下新建 li_yuanxing.msg
内容为:

uint64 timestamp            # Microseconds since system boot
uint64 hehe1
uint64 hehe2
uint64 hehe3
uint64 hehe4

1 在Firmware/src/modules/uORB中的object_common.cpp添加:

#include "topics/li_yuanxing.h"
ORB_DEFINE(li_yuanxing, struct li_yuanxing_s);

2.在Firmware/cmake中的ros-CMakeLists.txt中添加:li_yuanxing.msg
注:

## Generate messages in the 'msg' folder
add_message_files(
  FILES
  rc_channels.msg
  vehicle_attitude.msg
  vehicle_attitude_setpoint.msg
  manual_control_setpoint.msg
  actuator_controls.msg
  actuator_controls_0.msg
  actuator_controls_virtual_mc.msg
  vehicle_rates_setpoint.msg
  mc_virtual_rates_setpoint.msg
  vehicle_attitude.msg
  vehicle_control_mode.msg
  actuator_armed.msg
  parameter_update.msg
  vehicle_status.msg
  vehicle_local_position.msg
  position_setpoint.msg
  position_setpoint_triplet.msg
  vehicle_local_position_setpoint.msg
  vehicle_global_velocity_setpoint.msg
  offboard_control_mode.msg
  vehicle_force_setpoint.msg
  distance_sensor.msg
  control_state.msg
 +li_yuanxing.msg
)

3. 原理说明

xxx.msg为成员;
ORB_DEFINE(li_yuanxing, struct li_yuanxing_s);为话题的定义;
在Firmware/msg/templates/px4/uorb/msg.template 有模块化代码的编译设置;
在编译的时候,通过msg.template,生成一定结构的代码,再通过ros-CMakeLists.txt进行编译,所以在编译一遍后,才能具体看到所定义的话题成员。
这里写图片描述
4. 相关函数说明

初始化

orb_advert_t orb_advertise(const struct orb_metadata *meta, const void *data)
orb_advert_t orb_advertise_multi(const struct orb_metadata *meta, const void *data, int *instance, int priority)

发布

int  orb_publish(const struct orb_metadata *meta, orb_advert_t handle, const void *data)
int  orb_publish_auto(const struct orb_metadata *meta, orb_advert_t *handle, const void *data, int *instance, int priority)

订阅

int  orb_subscribe(const struct orb_metadata *meta)
int  orb_subscribe_multi(const struct orb_metadata *meta, unsigned instance)

拷贝

int  orb_copy(const struct orb_metadata *meta, int handle, void *buffer)

参数说明
orb_advert_t :空指针 handle
const struct orb_metadata *meta :话题ID
const void *data :相关数据类型指针
话题之间的发布订阅依赖于handle进行相关性的传递,话题的ID和结构通过ORB_DEFINE(li_yuanxing, struct li_yuanxing_s);来定义;
注:在初始化和发布用的是handle指针,订阅和copy用的是整形。
5. 发布订阅示例

示例在 px4_simple_app.c上进行测试

/*添加头文件*/
#include <uORB/uORB.h>
#include <uORB/topics/li_yuanxing.h>


/*定义话题结构*/
struct li_yuanxing_s test;
/*初始化数据*/
memset(&test, 0, sizeof(test));
/*初始化话题*/
/*test_pub 为handle指针*/
orb_advert_t test_pub = orb_advertise(ORB_ID(li_yuanxing), &test);
/*test数据赋值*/
 test.hehe1 = 1;
 test.hehe2 = 2;
 test.hehe3 = 3;
/*发布测试数据*/
orb_publish(ORB_ID(li_yuanxing), test_pub, &test);
/*订阅数据,在copy之前,必须要订阅*/
/*test_sub_fd为handle*/
int test_sub_fd = orb_subscribe(ORB_ID(li_yuanxing));
struct li_yuanxing_s data_copy;
/*copy数据*/
orb_copy(ORB_ID(li_yuanxing), test_sub_fd, &data_copy);
/*打印*/
PX4_WARN("[px4_simple_app] Accelerometer:\t%8.4f\t%8.4f\t%8.4f",
                     (double)data_copy.hehe1,
                     (double)data_copy.hehe2,
                     (double)data_copy.hehe3);
结果

这里写图片描述

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值