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