PX4应用实例2:创建uORB消息

1.在Firmware/msg下新建uORB成员Mytest.msg

uint64 data1
uint64 data2
uint64 data3
uint64 data4

# TOPICS Mytest offboard_Mytest onboard_Mytest
# TOPICS第一个参数要与成员名称相同
编译后,会在Firmware\build_px4fmu-v2_default\src\modules\uORB\topics文件夹下生成Mytest.h头文件,

头文件中会生成Mytest_s结构体和声明#TOPICS后的三个变量,如下所示:

#ifdef __cplusplus
struct __EXPORT Mytest_s {
#else
struct Mytest_s {
#endif
	uint64_t timestamp; // required for logger
	uint64_t data1;
	uint64_t data2;
	uint64_t data3;
	uint64_t data4;

#ifdef __cplusplus

#endif
};

/* register this as object request broker structure */
ORB_DECLARE(Mytest);
ORB_DECLARE(offboard_Mytest);
ORB_DECLARE(onboard_Mytest);

2.在Firmware/msg/CMakeLists.txt中添加Mytest.msg作为索引

3. 在Firmware/src/examples下新建Mytest文件夹

在文件夹下新建Mytest.c和CMakeLists.txt两个文件

4. 编写Mytest.c

#include <px4_config.h>
#include <px4_tasks.h>
#include <px4_posix.h>
#include <unistd.h>
#include <stdio.h>
#include <poll.h>
#include <string.h>
#include <math.h>

#include <uORB/uORB.h>
#include <uORB/topics/Mytest.h> //第一次编译会生成Mytest.h,需要添加到头文件中

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

int Mytest_main(int argc, char *argv[])
{
    /*在Mytest.h中会生成Mytest_s结构体*/
    struct Mytest_s orbtest;

    memset(&orbtest,0,sizeof(orbtest));

    /*第一步:公告主题*/
    orb_advert_t pub_fd=orb_advertise(ORB_ID(Mytest),&orbtest);

    orbtest.data1=1;
    orbtest.data2=2;
    orbtest.data3=3;
    orbtest.data4=4;

    /*第二步:发布主题*/
    orb_publish(ORB_ID(Mytest),pub_fd,&orbtest);

    /*第三步:订阅主题*/
    int sub_fd=orb_subscribe(ORB_ID(Mytest));

    /*第四步:复制主题*/
    struct Mytest_s data_copy;
    orb_copy(ORB_ID(Mytest),sub_fd,&data_copy);

    PX4_INFO("DATA:\t%4.2f\t%4.2f\t%4.2f\t%4.2f",
            (double)data_copy.data1,
            (double)data_copy.data2,
            (double)data_copy.data3,
            (double)data_copy.data4);
    PX4_INFO("exiting");

    return 0;
}

5. 编写CMakeLists.txt

px4_add_module(
    MODULE examples__Mytest
    MAIN Mytest
    STACK_MAIN 2000
    SRCS
        Mytest.c
    DEPENDS
        platforms__common
    )
# vim: set noet ft=cmake fenc=utf-8 ff=unix : 

6. 修改D:\pixhawk\Firmware\cmake\configs\nuttx_px4fmu-v2_default.cmake文件

在示例程序后添加一行

examples/Mytest

7. 编译并上传固件

编译命令:make px4fmu-v2_default

命令比较难记,可在Firmware/Makefile文件中添加,以px4代替px4fmu-v2_default,同时px4 upload也可以代替px4fmu-v2_default upload

#  explicity set default build target
px4: px4fmu-v2_default

8. 拔出SD卡,连接teraterm

敲回车出现nsh>

输入?出现可用命令

输入Mytest






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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值