PX4源码初学习(px4_simple_app.c)
2017-11-02 20:57
253 查看
注:我也不知道源码的学习应该从哪里开始,按照导师的计划,从这个开始,那就从这个开始吧。
( 2 ) Firmware/msg中的CMakeLists.txt中添加:zx.msg
(3).编译文件
在Firmware/build_px4fmu-v2_default/src/modules/uORB/topics中会出现zx.h文件,打开
会发现里面定义了一个结构体:
其中就有我们后面会用到的zx1,而这个成员x1也是我们最开始在写zx.msg时写在里面的。
(4).到这里就基本结束了,以后只用在需要的时候在代码文件中加上头文件
即可。
/***关于头文件没什么好说的,注意观察头文件,里面有个头文件是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>
即可。
相关文章推荐
- PX4应用实例3:px4_simple_app
- px4原生源码学习-(4)--Nuttx 实时操作系统编程
- PX4源码学习二--PX4环境搭建
- Api demo源码学习(9)--App/Activity/Receive Result --Activity间传递数据
- Java日志学习四:Simple Logging Facade for Java (SLF4J)源码浅析
- px4原生源码学习一
- px4原生源码学习-(4)--Nuttx 实时操作系统编程
- px4原生源码学习二--实时操作系统篇
- Spark2.X源码学习--从SparkContext创建分析AppClient注册
- px4原生源码学习三--Nuttx实时操作系统的使用
- Redis源码学习——简单动态字符串SDS(Simple Dynamic String)
- Api demo源码学习(17)--App/Device Admin
- px4原生源码学习四--Nuttx 实时操作系统编程
- Api demo源码学习(11)--App/Activity/Reorder Activity
- px4原生源码学习-(3)--Nuttx实时操作系统的使用
- px4原生源码学习-(1)
- px4原生源码学习-(1)
- Api demo源码学习(2)--App/Activity/Custom Dialog --自定义Activity样式
- Api demo源码学习(3)--App/Activity/Custom Title --自定义标题栏
- 设计比较好,有助于学习的Github上的iOS App源码 (中文)