您的位置:首页 > 移动开发

PX4源码初学习(px4_simple_app.c)

2017-11-02 20:57 253 查看
注:我也不知道源码的学习应该从哪里开始,按照导师的计划,从这个开始,那就从这个开始吧。

/***关于头文件没什么好说的,注意观察头文件,里面有个头文件是zx.h,很明显这是我们自己写的头文件。这个头文件是用来添加自己的topic用的,关于如何添加自己的topic在后面在详细介绍***/
#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/sensor_combined.h>
#include <uORB/topics/vehicle_attitude.h>
#include <uORB/topics/zx.h>

__EXPORT int px4_simple_app_main(int argc, char *argv[]);//这是主函数的声明,必须要有

int px4_simple_app_main(int argc, char *argv[])
{
PX4_INFO("Hello zouxu!\n");// PX4_INFO为打印函数,用printf也可以

/*定义话题结构*/
struct zx_s test;//结构体zx_s虽然我们没有去写,但是系统已经生成了。在后面会有所介绍。这里定义了                                一个结构体变量test,test里有什么呢?后面会介绍。

/*初始化数据*/
memset(&test, 0, sizeof(test));//memset() 函数常用于内存空间初始化,可以方便的清空一个结构类型的变量或数组。第一参数为一个指针或数组,第二个为初始化的值,第三个为长度。

/*公告主题*/
/*test_pub 为handle指针*,他是随取的,就是句柄的名字。因为后面的orb_advertise函数会返回一个类型为orb_advert_t的句柄,所以前面定义了一个类型为orb_advert_t的变量test_pub,返回的句柄要给后面的publish使用*/
orb_advert_t test_pub = orb_advertise(ORB_ID(zx), &test);

/*test数据赋值*/
test.zx1 = 200;

/*发布测试数据,因为前面一句话给test.zx1赋予了值*/
orb_publish(ORB_ID(zx), test_pub, &test);

/*订阅数据,在copy之前,必须要订阅*/
/*test_sub_fd为handle*,他也是随便取的,。因为后面orb_subscribe返回一个int类型的可以读取数据、更新话题的句柄,所以前面定义了int类型的test_sub_fd,而这个句柄在后面的copy数据中要用到*/
int test_sub_fd = orb_subscribe(ORB_ID(zx));

struct zx_s data_copy;//这里又声明了一个结构体变量,用于存储copy来的数据

/*copy数据*/
orb_copy(ORB_ID(zx), test_sub_fd, &data_copy);

/*打印*/
PX4_WARN("[px4_simple_app] GanTA:\t%8.4f",
(double)data_copy.zx1//这里进行了类型转换
);

return 0;
}


关于添加自己的topic

( 1 ) 这里,在Firmware/msg下新建zx.msg。内容为:

uint8 zx1
# TOPICS zx zx_x zx_y    //# TOPICS之间有个空格。这一步生成了3个主题ID。


( 2 ) Firmware/msg中的CMakeLists.txt中添加:zx.msg



(3).编译文件

在Firmware/build_px4fmu-v2_default/src/modules/uORB/topics中会出现zx.h文件,打开

#include <stdint.h>
#ifdef __cplusplus
#include <cstring>
#else
#include <string.h>
#endif

#include <uORB/uORB.h>

#ifndef __cplusplus

#endif

struct microCDR;

#ifdef __cplusplus
struct __EXPORT zx_s {
#else
struct zx_s {
#endif
uint64_t timestamp; // required for logger
uint8_t zx1;
uint8_t _padding0[7]; // required for logger

#ifdef __cplusplus

#endif
};

void serialize_zx(const struct zx_s *input, char *output, uint32_t *length, struct microCDR *microCDRWriter);
void deserialize_zx(struct zx_s *output, char *input, struct microCDR *microCDRReader);

/* register this as object request broker structure */
ORB_DECLARE(zx);
ORB_DECLARE(zx_x);
ORB_DECLARE(zx_y);


会发现里面定义了一个结构体:

struct zx_s {
#endif
uint64_t timestamp; // required for logger
uint8_t zx1;
uint8_t _padding0[7]; // required for logger

#ifdef __cplusplus

#endif
};


其中就有我们后面会用到的zx1,而这个成员x1也是我们最开始在写zx.msg时写在里面的。

(4).到这里就基本结束了,以后只用在需要的时候在代码文件中加上头文件

#include <uORB/topics/zx.h>


即可。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: