ROS学习指南3-创建ROS程序包
ROS学习指南3-创建ROS程序包
创建ROS程序包
Description: 本教程介绍如何使用roscreate-pkg或catkin创建一个新程序包,并使用rospack查看程序包的依赖关系。
Tutorial Level: BEGINNER
Next Tutorial: 编译ROS程序包
针对rosbuild方式
目录
1.Using roscreate
2.Creating a New ROS Package
3.First-order package dependencies
4.Indirect package dependencies
5.ROS Client Libraries
6.Review
Using roscreate
Before we create a package, let's see how the roscreate-pkg command-line tool works. This creates a new ROS package. All ROS packages consist of the many similar files : manifests, CMakeLists.txt, mainpage.dox, and Makefiles. roscreate-pkg eliminates many tedious tasks of creating a new package by hand, and eliminates common errors caused by hand-typing build files and manifests.
To create a new package in the current directory:
# roscreate-pkg [package_name]
You can also specify dependencies of that package:
# roscreate-pkg [package_name] [depend1] [depend2] [depend3]
Creating a New ROS Package
Now we're going to go into your home or project directory and create our beginner_tutorials package. We are going to make it depend on std_msgs, roscpp, and rospy, which are common ROS packages.
Now go into the ~/fuerte_workspace/sandbox directory:
$ cd ~/fuerte_workspace/sandbox
Alternatively, if you use Fuerte or later release, you can simply do:
$ roscd $ cd sandbox
Then create your package:
$ roscreate-pkg beginner_tutorials std_msgs rospy roscpp
You will see something similar to:
Creating package directory ~/fuerte_workspace/sandbox/beginner_tutorials Creating include directory ~/fuerte_workspace/sandbox/beginner_tutorials/include/beginner_tutorials Creating cpp source directory ~/ros/ros_tutorials/beginner_tutorials/src Creating python source directory ~/fuerte_workspace/sandbox/beginner_tutorials/src/beginner_tutorials Creating package file ~/fuerte_workspace/sandbox/beginner_tutorials/Makefile Creating package file ~/fuerte_workspace/sandbox/beginner_tutorials/manifest.xml Creating package file ~/fuerte_workspace/sandbox/beginner_tutorials/CMakeLists.txt Creating package file ~/fuerte_workspace/sandbox/beginner_tutorials/mainpage.doxPlease edit beginner_tutorials/manifest.xml and mainpage.dox to finish creating your package
You're going to want to spend some time looking at beginner_tutorials/manifest.xml. manifests play an important role in ROS as they define how Packages are built, run, and documented.
Now lets make sure that ROS can find your new package. It is often useful to call rospack profile after making changes to your path so that new directories will be found:
$ rospack profile $ rospack find beginner_tutorials
YOUR_PACKAGE_PATH/beginner_tutorials
If this fails, it means ROS can't find your new package, which may be an issue with your ROS_PACKAGE_PATH. Please consult the installation instructions for setup from SVN or from binaries, depending how you installed ROS. If you've created or added a package that's outside of the existing package paths, you will need to amend your ROS_PACKAGE_PATH environment variable to include that new location.
Try re-sourcing your setup.sh in your fuerte_workspace. Try moving to the directory for the package.
$ roscd beginner_tutorials $ pwd
YOUR_PACKAGE_PATH/beginner_tutorials
First-order package dependencies
When using roscreate-pkg earlier, a few package dependencies were provided. These first-order dependencies can now be reviewed with the rospack tool.
(Jan 9, 2013) There is a bug reported and already fixed in rospack in groovy; it may take some time to be reflected in the packages. If you see an issue similar to this with the next command, you can skip to the following command.
$ rospack depends1 beginner_tutorials
std_msgs rospy roscpp
As you can see, rospack lists the same dependencies that were used as arguments when running roscreate-pkg. These dependencies for a package are stored in the manifest file. Take a look at the manifest file.
$ roscd beginner_tutorials $ cat manifest.xml
<package>...
<depend package="std_msgs"/>
<depend package="rospy"/>
<depend package="roscpp"/></package>
Indirect package dependencies
In many cases, a dependency will also have its own dependencies.
For instance, rospy has other dependencies.
(Jan 9, 2013) There is a bug reported and already fixed in rospack in groovy; it may take some time to be reflected in the packages. If you see an issue similar to this with the next command, you can skip to the following command.
$ rospack depends1 rospy
roslib roslang
A package can have quite a few indirect dependencies. Luckily rospack can recursively determine all nested dependencies.
$ rospack depends beginner_tutorials
rospack roslib std_msgs rosgraph_msgs rosbuild roslang rospy cpp_common roscpp_traits rostime roscpp_serialization xmlrpcpp rosconsole roscpp
Note: in Fuerte, the list is much shorter:
std_msgs roslang rospy roscpp
ROS Client Libraries
You may be wondering what rospy and roscpp dependencies are from the previous examples. rospy and roscpp are Client Libraries. The client libraries allow different programming languages to communicate through ROS. rospy is the client library for Python. roscpp is the client library for C++.
Review
Lets just list some of the commands we've used so far:
1.roscreate-pkg = ros+create-pkg : generates all the files needed to create a ROS package
2.rospack = ros+pack(age) : provides information related to ROS packages
3.rosstack = ros+stack : provides information related to ROS stacks
针对CATKIN方式
目录
1.一个catkin程序包由什么组成?
2.在catkin工作空间中的程序包
3.创建一个catkin程序包
4.程序包依赖关系
5.一级依赖
6.间接依赖
7.自定义你的程序包
8.自定义 package.xml
9.描述标签
10.维护者标签
11.许可标签
12.依赖项标签
13.最后完成的 package.xml
14.自定义 CMakeLists.txt
一个catkin程序包由什么组成?
一个程序包要想称为catkin程序包必须符合以下要求:
1.该程序包必须包含catkin compliant package.xml文件
这个package.xml文件提供有关程序包的元信息。
2.程序包必须包含一个catkin 版本的CMakeLists.txt文件,而Catkin metapackages中必须包含一个对CMakeList.txt文件的引用。
3.每个目录下只能有一个程序包。
这意味着在同一个目录下不能有嵌套的或者多个程序包存在。
最简单的程序包也许看起来就像这样:
my_package/ CMakeLists.txt package.xml
在catkin工作空间中的程序包
开发catkin程序包的一个推荐方法是使用catkin工作空间,但是你也可以单独开发(standalone)catkin 软件包。一个简单的工作空间也许看起来像这样:
workspace_folder/ -- WORKSPACE src/ -- SOURCE SPACE CMakeLists.txt -- 'Toplevel' CMake file, provided by catkin package_1/ CMakeLists.txt -- CMakeLists.txt file for package_1 package.xml -- Package manifest for package_1 ... package_n/ CMakeLists.txt -- CMakeLists.txt file for package_n package.xml -- Package manifest for package_n
在继续本教程之前请先按照创建catkin工作空间教程创建一个空白的catkin工作空间。
创建一个catkin程序包
本部分教程将演示如何使用catkin_create_pkg命令来创建一个新的catkin程序包以及创建之后都能做些什么。 首先切换到之前通过创建catkin工作空间教程创建的catkin工作空间中的src目录下:
# You should have created this in the Creating a Workspace Tutorial $ cd ~/catkin_ws/src
现在使用catkin_create_pkg命令来创建一个名为'beginner_tutorials'的新程序包,这个程序包依赖于std_msgs、roscpp和rospy:
$ catkin_create_pkg beginner_tutorials std_msgs rospy roscpp
这将会创建一个名为beginner_tutorials的文件夹,这个文件夹里面包含一个package.xml文件和一个CMakeLists.txt文件,这两个文件都已经自动包含了部分你在执行catkin_create_pkg命令时提供的信息。
catkin_create_pkg命令会要求你输入package_name,如果有需要你还可以在后面添加一些需要依赖的其它程序包:
# This is an example, do not try to run this # catkin_create_pkg <package_name> [depend1] [depend2] [depend3]
catkin_create_pkg命令也有更多的高级功能,这些功能在catkin/commands/catkin_create_pkg中有描述。
程序包依赖关系
一级依赖
之前在使用catkin_create_pkg命令时提供了几个程序包作为依赖包,现在我们可以使用rospack命令工具来查看一级依赖包。
(Jan 9, 2013) There is a bug reported and already fixed in rospack in groovy, which takes sometime until the change gets reflected on your computer. If you see a similar issue like this with the next command, you can skip to the next command.
$ rospack depends1 beginner_tutorials
std_msgs rospy roscpp
就像你看到的,rospack列出了在运行catkin_create_pkg命令时作为参数的依赖包,这些依赖包随后保存在package.xml文件中。
$ roscd beginner_tutorials $ cat package.xml
<package> ... <buildtool_depend>catkin</buildtool_depend> <build_depend>roscpp</build_depend> <build_depend>rospy</build_depend> <build_depend>std_msgs</build_depend> ... </package>
间接依赖
在很多情况中,一个依赖包还会有它自己的依赖包,比如,rospy还有其它依赖包。
(Jan 9, 2013) There is a bug reported and already fixed in rospack in groovy, which takes sometime until the change gets reflected on your computer. If you see a similar issue like this with the next command, you can skip to the next command.
$ rospack depends1 rospy
genpy rosgraph rosgraph_msgs roslib std_msgs
一个程序包还可以有好几个间接的依赖包,幸运的是使用rospack可以递归检测出所有的依赖包。
$ rospack depends beginner_tutorials cpp_common rostime roscpp_traits roscpp_serialization genmsg genpy message_runtime rosconsole std_msgs rosgraph_msgs xmlrpcpp roscpp rosgraph catkin rospack roslib rospy
自定义你的程序包
本部分教程将剖析catkin_create_pkg命令生成的每个文件并详细描述这些文件的组成部分以及如何自定义这些文件。
自定义 package.xml
自动生成的package.xml文件应该在你的新程序包中。现在让我们一起来看看新生成的package.xml文件以及每一个需要你注意的标签元素。
描述标签
首先更新描述标签:
5 <description>The beginner_tutorials package</description>
将描述信息修改为任何你喜欢的内容,但是按照约定第一句话应该简短一些,因为它覆盖了程序包的范围。如果用一句话难以描述完全那就需要换行了。
维护者标签
接下来是维护者标签:
7 <!-- One maintainer tag required, multiple allowed, one person per tag --> 8 <!-- Example: --> 9 <!-- <maintainer email="jane.doe@example.com">Jane Doe</maintainer> --> 10 <maintainer email="user@todo.todo">user</maintainer>
这是package.xml中要求填写的一个重要标签,因为它能够让其他人联系到程序包的相关人员。至少需要填写一个维护者名称,但如果有需要的话你可以添加多个。除了在标签里面填写维护者的名称外,还应该在标签的email属性中填写邮箱地址:
7 <maintainer email="you@yourdomain.tld">Your Name</maintainer>
许可标签
再接下来是许可标签,同样的也需要:
12 <!-- One license tag required, multiple allowed, one license per tag --> 13 <!-- Commonly used license strings: --> 14 <!-- BSD, MIT, Boost Software License, GPLv2, GPLv3, LGPLv2.1, LGPLv3 --> 15 <license>TODO</license>
你应该选择一种许可协议并将它填写到这里。一些常见的开源许可协议有BSD、MIT、Boost Software License、GPLv2、GPLv3、LGPLv2.1和LGPLv3。你可以在Open Source Initiative中阅读其中的若干个许可协议的相关信息。对于本教程我们将使用BSD协议,因为ROS核心组件的剩余部分已经使用了该协议:
8 <license>BSD</license>
依赖项标签
接下来的标签用来描述程序包的各种依赖项,这些依赖项分为build_depend、buildtool_depend、run_depend、test_depend。
关于这些标签的更详细介绍请参考Catkin Dependencies相关的文档。在之前的操作中,因为我们将 std_msgs、 roscpp、 和 rospy作为catkin_create_pkg命令的参数,所以生成的依赖项看起来如下:
27 <!-- The *_depend tags are used to specify dependencies --> 28 <!-- Dependencies can be catkin packages or system dependencies --> 29 <!-- Examples: --> 30 <!-- Use build_depend for packages you need at compile time: --> 31 <!-- <build_depend>genmsg</build_depend> --> 32 <!-- Use buildtool_depend for build tool packages: --> 33 <!-- <buildtool_depend>catkin</buildtool_depend> --> 34 <!-- Use run_depend for packages you need at runtime: --> 35 <!-- <run_depend>python-yaml</run_depend> --> 36 <!-- Use test_depend for packages you need only for testing: --> 37 <!-- <test_depend>gtest</test_depend> --> 38 <buildtool_depend>catkin</buildtool_depend> 39 <build_depend>roscpp</build_depend> 40 <build_depend>rospy</build_depend> 41 <build_depend>std_msgs</build_depend>
除了catkin中默认提供的buildtool_depend,所有我们列出的依赖包都已经被添加到build_depend标签中。在本例中,因为在编译和运行时我们需要用到所有指定的依赖包,因此还需要将每一个依赖包分别添加到run_depend标签中:
12 <buildtool_depend>catkin</buildtool_depend> 13 14 <build_depend>roscpp</build_depend> 15 <build_depend>rospy</build_depend> 16 <build_depend>std_msgs</build_depend> 17 18 <run_depend>roscpp</run_depend> 19 <run_depend>rospy</run_depend> 20 <run_depend>std_msgs</run_depend>
最后完成的 package.xml
现在看下面最后去掉了注释和未使用标签后的package.xml文件就显得更加简洁了:
1 <?xml version="1.0"?> 2 <package> 3 <name>beginner_tutorials</name> 4 <version>0.1.0</version> 5 <description>The beginner_tutorials package</description> 6 7 <maintainer email="you@yourdomain.tld">Your Name</maintainer> 8 <license>BSD</license> 9 <url type="website">http://wiki.ros.org/beginner_tutorials</url> 10 <author email="you@yourdomain.tld">Jane Doe</author> 11 12 <buildtool_depend>catkin</buildtool_depend> 13 14 <build_depend>roscpp</build_depend> 15 <build_depend>rospy</build_depend> 16 <build_depend>std_msgs</build_depend> 17 18 <run_depend>roscpp</run_depend> 19 <run_depend>rospy</run_depend> 20 <run_depend>std_msgs</run_depend> 21 22 </package>
自定义 CMakeLists.txt
到此,这个包含程序包元信息的package.xml文件已经按照需要完成了裁剪整理,现在你可以继续下面的教程了。catkin_create_pkg命令生成的CMakeLists.txt文件将在后续关于编译ROS程序代码的教程中讲述。
现在你已经创建了一个新的ROS程序包,接下来我们开始编译这个程序包