创建工作空间和功能包
ROS中的工作空间是存放工程开发相关文件的文件夹。主要包括四个文件夹:
- src 代码空间(功能包的代码、配置文件和其他的一些文件)
- build 编译空间 (编译过程中产生的文件)
- devel 开发空间 (编译生成的可执行文件、库)
- install 安装空间 (使用install指令安装成功的指定位置)
创建工作空间
$ mkdir -p ~/catkin_ws/src
$ cd ~/catkin_ws/src/
$ catkin_init_workspace
使用以上的命令来创建工作空间。可以看到~/catkin_ws/src 文件夹下变成了以下的情况:
编译
$ catkin_make
Base path: /home/devil/catkin_ws
Source space: /home/devil/catkin_ws/src
Build space: /home/devil/catkin_ws/build
Devel space: /home/devil/catkin_ws/devel
Install space: /home/devil/catkin_ws/install
####
#### Running command: "cmake /home/devil/catkin_ws/src -DCATKIN_DEVEL_PREFIX=/home/devil/catkin_ws/devel -DCMAKE_INSTALL_PREFIX=/home/devil/catkin_ws/install -G Unix Makefiles" in "/home/devil/catkin_ws/build"
####
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Using CATKIN_DEVEL_PREFIX: /home/devil/catkin_ws/devel
-- Using CMAKE_PREFIX_PATH: /home/devil/catkin_ws/devel;/opt/ros/melodic
-- This workspace overlays: /home/devil/catkin_ws/devel;/opt/ros/melodic
-- Found PythonInterp: /usr/bin/python2 (found suitable version "2.7.17", minimum required is "2")
-- Using PYTHON_EXECUTABLE: /usr/bin/python2
-- Using Debian Python package layout
-- Using empy: /usr/bin/empy
-- Using CATKIN_ENABLE_TESTING: ON
-- Call enable_testing()
-- Using CATKIN_TEST_RESULTS_DIR: /home/devil/catkin_ws/build/test_results
-- Found gtest sources under '/usr/src/googletest': gtests will be built
-- Found gmock sources under '/usr/src/googletest': gmock will be built
-- Found PythonInterp: /usr/bin/python2 (found version "2.7.17")
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE
-- Using Python nosetests: /usr/bin/nosetests-2.7
-- catkin 0.7.29
-- BUILD_SHARED_LIBS is on
-- BUILD_SHARED_LIBS is on
-- Configuring done
-- Generating done
-- Build files have been written to: /home/devil/catkin_ws/build
####
#### Running command: "make -j12 -l12" in "/home/devil/catkin_ws/build"
####
编译使用ros指定的编译工具来编译工程文件,但是注意一定要在工作空间的根目录下面来执行这个命令。
我们来看一下现在工作空间的情况:
$ ls
build devel src
可以看到里面包含三个文件夹,对比上面的标准不难发现,少了一个install文件夹。这时候需要使用以下命令
$ catkin_make install
Base path: /home/devil/catkin_ws
Source space: /home/devil/catkin_ws/src
Build space: /home/devil/catkin_ws/build
Devel space: /home/devil/catkin_ws/devel
Install space: /home/devil/catkin_ws/install
####
#### Running command: "make cmake_check_build_system" in "/home/devil/catkin_ws/build"
####
####
#### Running command: "make install -j12 -l12" in "/home/devil/catkin_ws/build"
####
Install the project...
-- Install configuration: ""
-- Installing: /home/devil/catkin_ws/install/_setup_util.py
-- Installing: /home/devil/catkin_ws/install/env.sh
-- Installing: /home/devil/catkin_ws/install/setup.bash
-- Installing: /home/devil/catkin_ws/install/local_setup.bash
-- Installing: /home/devil/catkin_ws/install/setup.sh
-- Installing: /home/devil/catkin_ws/install/local_setup.sh
-- Installing: /home/devil/catkin_ws/install/setup.zsh
-- Installing: /home/devil/catkin_ws/install/local_setup.zsh
-- Installing: /home/devil/catkin_ws/install/.rosinstall
在查看就会发现出现了install文件夹。
创建功能包
不能直接把代码放进文件夹里面进行编译,要想创建功能包需要使用以下的命令:
$ catkin_create_pkg test_pkg std_msgs rospy roscpp
Created file test_pkg/package.xml
Created file test_pkg/CMakeLists.txt
Created folder test_pkg/include/test_pkg
Created folder test_pkg/src
Successfully created files in /home/devil/catkin_ws/src/test_pkg. Please adjust the values in package.xml.
catkin_create_pkg是创建功能包的命令,test_pkg是我们创建功能包的名字,后面的三个是创建功能包的依赖。其中std_msgs是标准的消息文件。然后查看src文件夹:
$ tree
.
├── CMakeLists.txt
├── include
│ └── test_pkg
├── package.xml
└── src
3 directories, 2 files
可以看到test_pkg下面有include src CMakeLists.txt package.xml 四个文件(夹)。
当你创建完之后就可以按照上面的步骤字再次编译一次,当然你编译的肯定是一个空功能包,但是无所谓嘛。
$ catkin_make
Base path: /home/devil/catkin_ws
Source space: /home/devil/catkin_ws/src
Build space: /home/devil/catkin_ws/build
Devel space: /home/devil/catkin_ws/devel
Install space: /home/devil/catkin_ws/install
####
#### Running command: "cmake /home/devil/catkin_ws/src -DCATKIN_DEVEL_PREFIX=/home/devil/catkin_ws/devel -DCMAKE_INSTALL_PREFIX=/home/devil/catkin_ws/install -G Unix Makefiles" in "/home/devil/catkin_ws/build"
####
-- Using CATKIN_DEVEL_PREFIX: /home/devil/catkin_ws/devel
-- Using CMAKE_PREFIX_PATH: /home/devil/catkin_ws/devel;/opt/ros/melodic
-- This workspace overlays: /home/devil/catkin_ws/devel;/opt/ros/melodic
-- Found PythonInterp: /usr/bin/python2 (found suitable version "2.7.17", minimum required is "2")
-- Using PYTHON_EXECUTABLE: /usr/bin/python2
-- Using Debian Python package layout
-- Using empy: /usr/bin/empy
-- Using CATKIN_ENABLE_TESTING: ON
-- Call enable_testing()
-- Using CATKIN_TEST_RESULTS_DIR: /home/devil/catkin_ws/build/test_results
-- Found gtest sources under '/usr/src/googletest': gtests will be built
-- Found gmock sources under '/usr/src/googletest': gmock will be built
-- Found PythonInterp: /usr/bin/python2 (found version "2.7.17")
-- Using Python nosetests: /usr/bin/nosetests-2.7
-- catkin 0.7.29
-- BUILD_SHARED_LIBS is on
-- BUILD_SHARED_LIBS is on
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- ~~ traversing 1 packages in topological order:
-- ~~ - test_pkg
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- +++ processing catkin package: 'test_pkg'
-- ==> add_subdirectory(test_pkg)
-- Configuring done
-- Generating done
-- Build files have been written to: /home/devil/catkin_ws/build
####
#### Running command: "make -j12 -l12" in "/home/devil/catkin_ws/build"
####
可以看到中间有一部分出现了我们创建的功能包。
package.xml简介
<?xml version="1.0"?>
<package format="2">
<name>test_pkg</name> //功能包的名称
<version>0.0.0</version> //功能包的版本号
<description>The test_pkg package</description> //叙述
<!-- One maintainer tag required, multiple allowed, one person per tag -->
<!-- Example: -->
<!-- <maintainer email="jane.doe@example.com">Jane Doe</maintainer> -->
<maintainer email="devil@todo.todo">devil</maintainer>
<!-- One license tag required, multiple allowed, one license per tag -->
<!-- Commonly used license strings: -->
<!-- BSD, MIT, Boost Software License, GPLv2, GPLv3, LGPLv2.1, LGPLv3 -->
<license>TODO</license> //开源许可证
<!-- Url tags are optional, but multiple are allowed, one per tag -->
<!-- Optional attribute type can be: website, bugtracker, or repository -->
<!-- Example: -->
<!-- <url type="website">http://wiki.ros.org/test_pkg</url> -->
<!-- Author tags are optional, multiple are allowed, one per tag -->
<!-- Authors do not have to be maintainers, but could be -->
<!-- Example: -->
<!-- <author email="jane.doe@example.com">Jane Doe</author> -->
<!-- The *depend tags are used to specify dependencies -->
<!-- Dependencies can be catkin packages or system dependencies -->
<!-- Examples: -->
<!-- Use depend as a shortcut for packages that are both build and exec dependencies -->
<!-- <depend>roscpp</depend> -->
<!-- Note that this is equivalent to the following: -->
<!-- <build_depend>roscpp</build_depend> -->
<!-- <exec_depend>roscpp</exec_depend> -->
<!-- Use build_depend for packages you need at compile time: -->
<!-- <build_depend>message_generation</build_depend> -->
<!-- Use build_export_depend for packages you need in order to build against this package: -->
<!-- <build_export_depend>message_generation</build_export_depend> -->
<!-- Use buildtool_depend for build tool packages: -->
<!-- <buildtool_depend>catkin</buildtool_depend> -->
<!-- Use exec_depend for packages you need at runtime: -->
<!-- <exec_depend>message_runtime</exec_depend> -->
<!-- Use test_depend for packages you need only for testing: -->
<!-- <test_depend>gtest</test_depend> -->
<!-- Use doc_depend for packages you need only for building documentation: -->
<!-- <doc_depend>doxygen</doc_depend> -->
<buildtool_depend>catkin</buildtool_depend> ///以下是功能包的依赖信息
<build_depend>roscpp</build_depend>
<build_depend>rospy</build_depend>
<build_depend>std_msgs</build_depend>
<build_export_depend>roscpp</build_export_depend>
<build_export_depend>rospy</build_export_depend>
<build_export_depend>std_msgs</build_export_depend>
<exec_depend>roscpp</exec_depend>
<exec_depend>rospy</exec_depend>
<exec_depend>std_msgs</exec_depend>
<!-- The export tag contains other, unspecified, tags -->
<export>
<!-- Other tools can request additional information be placed here -->
</export>
</package>