如何建立和维护ROS catkin-based package

创建ROS Package

每一个ROS package都是~/catkin_ws/src路径下的一个文件夹,所以我们需要在~/catkin_ws/src路径下来创建一个ros package,命令如下:

cd ~/catkin_ws/src
catkin_create_pkg beginner_tutorials std_msgs rospy roscpp

以上命令中,beginner_tutorials是package的名字,std_msgs rospy roscpp代表了该ros package依赖这三个ros package(ROS自带的)。接下来就要build package了,我们需要回到~/catkin_ws路径下,进行build。

cd ~/catkin_ws
catkin build

运行该命令之后,你就发现~/catkin_ws/src路径下多了一个文件夹beginner_tutorials,并且该文件夹下有两个文件package.xml和CMakeLists.txt,并且package.xml中自动有了build_depend的信息,即刚才的std_msgs rospy roscpp三个ros packages。

建议:除了第一次~/catkin_ws/src路径下还一个ros package都没有的时候,用catkin_make(会自动生成devel文件夹和build文件夹),后面每次有任何包括的任何文件有更新,都需要把整个src code进行build,而我们可以用更好的命令catkin build或者catkin_make_isolated,具体为什么可以看链接的解释

具体含义

每一个ROS package都是~/catkin_ws/src路径下的一个文件夹,所以通常~/catkin_ws/src路径下有好多个文件夹,每个都是一个ros package。一个ros package文件夹必须包含以下两个文件

  1. package.xml文件: 定义package的名字,license,以及最重要的是定义本ros package的dependency。
    • buildtool_depend: Use this for buildtool packages,这里我们的build system用的是catkin啦
    • build_depend: Use this for packages you need at compile time
    • exec_depend: Use this for packages you need at runtime
    • build_export_depend: Use this for packages you need in order to build against this package
    • test_depend: Use this for packages you need only for testing
    • doc_depend: Use this for packages you need only for building documentation
    • depend: Use this as a shortcut for packages that are both build and exec dependencies
  2. CMakeLists.txt文件: CMakeLists.txt必须遵循如下的格式和顺序。
    • cmake_minimum_required(): Catkin requires version 2.8.3 or higher,the required version of CMake 
    • project(): 相当于给${PROJECT_NAME}赋值了,可以在本文件使用
    • find_package()用来specify其他的CMake packages(注意不是ros packages)。那么至少有一个dependency,那就是caktin: find_package(catkin REQUIRED)。 最好是将Catkin Packages 能够Specify成Components,例如find_package(catkin REQUIRED COMPONENTS nodelet)
    • catkin_package():必须在声明任何目标文件前做,即在add_library() or add_executable()之前做。

ROS package与ROS node

注意一个ROS package下面可以有多个ROS node,需要指明启动ROS的哪个package里面的哪个node.

  • Each ROS package is built as a unit
  • Pay attention to package dependencies. Avoid combining nodes that pull in mutually unneeded dependencies.
  • Avoid combining nodes that are often used separately (to eliminate unnecessary build overhead).
  • The overhead of an extra ROS package is not very large. Define separate packages wherever they make sense.
  • If a group of nodes with similar dependencies are generally used together, consider combining them in a single package.
  • If some nodes have common dependencies on shared code that you do not wish to export publicly, they can be combined internally within a single package.
  • If the shared code should be exported, consider making it a separate package that the others depend on. (This is reasonable, but not required. A single package can export libraries and headers as well as nodes.)

Leave a Comment