Nuttx下移植uorb笔记
之前接触过ros下的消息机制(生产者/消费者)模型,第一感觉是灵活好用,但是在资源有限的嵌入式环境里面,邮箱、消息…显得就有点不那么灵活,后来开发飞控逐渐了解到了nuttx以及uorb发现了这与ros下的消息机制异曲同工,但是px4是一个高度定制化(解决飞行控制问题)的系统,但是如果仅仅想使用nuttx作为os做开发的话,就不那么友好了,所以开始移植吧。下面的一些关键词是移植的重点,需要提前了解;
- c++编译器编译c代码
- px4下的CDev类
- nuttx驱动模型(上半部分、下半部分)
- makefile
uORB
关于uORB的使用方法网上有很多介绍了,就不在重复了,这里主要是梳理下uORB的软件框架:
namespace uORB
{
class DeviceNode;
class DeviceMaster;
class Manager;
}
这三个类是实现整个uORB的主要元素:
class Manager:服务应用,提供消息的操作接口(订阅/发布/取消/检查等等),面向的是使用uORB的应用;
class DeviceNode:消息的最小实体单元(对应nuttx下的字符驱动),每个消息都会是以/obj/xxx下的字符驱动形式存在;
class DeviceMaster:管理DeviceNode,所有消息的创建、参数检查、记录都是是有它来负责完成;
所以到这里为止,其实我们差不多能窥见uORB的框架了;用别人总结的一句话就是“多个任务打开同一个设备”;
class CDev
这个是px4上面做的一个字符驱动的基类,实现了在nuttx下一个字符驱动的上/下部分,通俗一点的话就是,把nuttx定义的字符驱动框架都填满了;后面需要实现的驱动直接公共继承它就OK了。这样做的目的是减少了开发者的重复性工作,也直接规范了驱动编码(高明的做法)
说这个的意义是在于我们上面介绍的关于uorb的DeviceNode、DeviceMaster都是公共继承了CDev类的,所以我们移植也需要去移植这个框架;
这里多说一句,github上也有移植uorb的开源代码,不过是基于ringbuffer来实现,不依赖一固定的某一个OS,这个和我们这次移植的有点不一样(基于Nuttx字符驱动来实现)
具体请参考作者开源的代码地址Github
移植CDev
这里只是梳理整个移植的思路和关键点,具体的移植需要开发者自己实现。
第一阶段的目标是我们需要把CDev的框架在nuttx上搭建起来
移植的顺序:CDev===>Manager===>DeviceNode===>DeviceMaster
1、首先在app目录下建立uORB文件夹并在路径下继续建立如下三个文件夹:device、topic、uorb;
2、然后将下列文件拷贝在device文件夹下
CDev.cxx cdev_platform.cxx device.h drv_device.h
CDev.hpp cdev_platform.hpp Device.hpp drv_orb_dev.h
这些文件都可以在px4的源代码里找到,后面在说各自的作用;
3、接下来就是makefile
./uORB/Makefile 【把uORB文件加入到app的编译路径】
############################################################################
# apps/uORB/Makefile
#
# Copyright (C) 2011-2015 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <gnutt@nuttx.org>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name NuttX nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################
MENUDESC = "uORB"
include $(APPDIR)/Directory.mk
./uORB/Make.defs 【包含uORB下一级文件的makefile】
############################################################################
# apps/uORB/Make.defs
# Adds selected applications to apps/ build
#
# Copyright (C) 2012, 2015 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <gnutt@nuttx.org>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name NuttX nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################
include $(wildcard uORB/*/Make.defs)
通过上面的makefile我们就可以实现把我们新建的文件路径加入编译系统
4、再说回CDev的编译先来看看makefile
############################################################################
# apps/uORB/uorb/Makefile
#
# Copyright (C) 2009-2012 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <gnutt@nuttx.org>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name NuttX nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWN