[转载]ROS知识(3)----功能包package编译的两种方式

原文链接:http://www.cnblogs.com/cv-pr/p/4835635.html

catkin命令创建工作空间和包相对要复杂些,但是熟悉以后对项目的管理是非常有利的,因此官方也建议使用第二种方式。

2.1、创建工作空间

在开始具体工作之前,首先创建工作空间,并且为工作空间设置环境变量到~/.bashrc中,如果要查看已有的空间路径,可以用查询命令

$echo $ROS_PACKAGE_PATH

你将会看到如下的信息:

/home/horsetail/dev/rosbook:/home/horsetail/catkin_ws/src:/opt/ros/jade/share:/opt/ros/jade/stacks

这里的创建空间实际上就是先建立一个文件夹,然后把文件夹的路径设置到环境变量~/.bashrc中。例如我们这里创建目录~/dev/rosbook作为工作空间,和第一种方法类似。

下面我们开始创建一个catkin 工作空间

$mkdir-p ~/catkin_ws/src

$ cd~/catkin_ws/src

即使这个工作空间是空的(在'src'目录中没有任何软件包,只有一个CMakeLists.txt链接文件),你依然可以“build”它:

$ cd ~/catkin_ws/

$ catkin_make

接下来首先source一下新生成的setup.*sh文件,把它加入环境变量到~/.bashrc文件中:

$ source ~/catkin_ws/devel/setup.bash

到此你的工作空间环境已经搭建完成

2.2、创建包

首先切换到之前创建的catkin工作空间中的src目录下:

$ cd~/catkin_ws/src

现在使用catkin_create_pkg命令来创建一个名为'mypackage1'的新程序包,这个程序包依赖于std_msgs、roscpp和rospy:

$ catkin_create_pkg mypackage1 std_msgs rospy roscpp

这将会创建一个名为beginner_tutorials的文件夹,这个文件夹里面包含一个package.xml文件和一个CMakeLists.txt文件,这两个文件都已经自动包含了部分你在执行catkin_create_pkg命令时提供的信息。

和第一种方法类似类似,把1.3节中的两个talker.cpp和listener.cpp拷贝到mypackage1/src下。

接下来,要告诉编译器如何去找到这两个文件。你需要打开mypackage1/CMakeLists.txt,在文件的末尾添加命令:

include_directories(include ${catkin_INCLUDE_DIRS})

add_executable(talker src/talker.cpp)

target_link_libraries(talker ${catkin_LIBRARIES})

add_executable(listener src/listener.cpp)

target_link_libraries(listener ${catkin_LIBRARIES})

整理后的mypackage/CMakeLists.txt内容结构如下:

cmake_minimum_required(VERSION2.8.3)project(mypackage1)## Find catkin and any catkin packagesfind_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs)## Declare ROS messages and services#add_message_files(FILES Num.msg)#默认是不注释掉的,编译会出错#add_service_files(FILES AddTwoInts.srv)#默认是不注释掉的,编译会出错## Generate added messages and services#generate_messages(DEPENDENCIES std_msgs)#默认是不注释掉的,编译会出错## Declare a catkin packagecatkin_package()## Build talker and listenerinclude_directories(include ${catkin_INCLUDE_DIRS})add_executable(talker src/talker.cpp)

target_link_libraries(talker ${catkin_LIBRARIES})

add_dependencies(talker beginner_tutorials_generate_messages_cpp)

add_executable(listener src/listener.cpp)

target_link_libraries(listener ${catkin_LIBRARIES})

add_dependencies(listener beginner_tutorials_generate_messages_cpp)

注意:CMakeLists.txt中的有三那个命令是保留的:

## Declare ROS messages and services

#add_message_files(FILES Num.msg)

#add_service_files(FILES AddTwoInts.srv)

## Generate added messages and services

#generate_messages(DEPENDENCIES std_msgs)

这是关于服务消息方面的,这个项目中并没有用到,因此必须要注释掉,否则编译会出现错误如下:

Base path: /home/horsetail/catkin_ws

Source space:/home/horsetail/catkin_ws/src

Build space:/home/horsetail/catkin_ws/build

Devel space:/home/horsetail/catkin_ws/devel

Install space:/home/horsetail/catkin_ws/install####

#### Running command:"make cmake_check_build_system"in"/home/horsetail/catkin_ws/build"####-- Using CATKIN_DEVEL_PREFIX: /home/horsetail/catkin_ws/devel-- Using CMAKE_PREFIX_PATH: /home/horsetail/catkin_ws/devel;/opt/ros/jade-- This workspace overlays: /home/horsetail/catkin_ws/devel;/opt/ros/jade-- Using PYTHON_EXECUTABLE: /usr/bin/python--Using Debian Python package layout-- Using empy: /usr/bin/empy--Using CATKIN_ENABLE_TESTING: ON--Call enable_testing()-- Using CATKIN_TEST_RESULTS_DIR: /home/horsetail/catkin_ws/build/test_results-- Found gtest sources under'/usr/src/gtest': gtests will be built-- Using Python nosetests: /usr/bin/nosetests-2.7-- catkin0.6.14--BUILD_SHARED_LIBS is on-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-- ~~  traversing1packagesintopological order:-- ~~  -mypackage1-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-- +++ processing catkin package:'mypackage1'-- ==>add_subdirectory(mypackage1)

CMake Error at mypackage1/CMakeLists.txt:8(add_message_files):

Unknown CMake command"add_message_files".-- Configuring incomplete, errors occurred!See also"/home/horsetail/catkin_ws/build/CMakeFiles/CMakeOutput.log".

See also"/home/horsetail/catkin_ws/build/CMakeFiles/CMakeError.log".make: *** [cmake_check_build_system] 错误1

这个错误也折腾了我一阵子,以此铭记之。

好了,接下来就是编译它了。

2.3、编译

catkin_make是一个命令行工具,它简化了catkin的标准工作流程。你可以认为catkin_make是在CMake标准工作流程中依次调用了cmake和make。

使用方法:

# 在catkin工作空间下

$ catkin_make [make_targets] [-DCMAKE_VARIABLES=...]

记得事先source你的环境配置(setup)文件,在Ubuntu中的操作指令如下:

$ source /opt/ros/indigo/setup.bash

切换到catkin workspace :

$ cd ~/catkin_ws/

执行编译:

$ catkin_make

输出如下编译信息:

Base path: /home/horsetail/catkin_ws

Source space:/home/horsetail/catkin_ws/src

Build space:/home/horsetail/catkin_ws/build

Devel space:/home/horsetail/catkin_ws/devel

Install space:/home/horsetail/catkin_ws/install####

#### Running command:"make cmake_check_build_system"in"/home/horsetail/catkin_ws/build"####-- Using CATKIN_DEVEL_PREFIX: /home/horsetail/catkin_ws/devel-- Using CMAKE_PREFIX_PATH: /home/horsetail/catkin_ws/devel;/opt/ros/jade-- This workspace overlays: /home/horsetail/catkin_ws/devel;/opt/ros/jade-- Using PYTHON_EXECUTABLE: /usr/bin/python--Using Debian Python package layout-- Using empy: /usr/bin/empy--Using CATKIN_ENABLE_TESTING: ON--Call enable_testing()-- Using CATKIN_TEST_RESULTS_DIR: /home/horsetail/catkin_ws/build/test_results-- Found gtest sources under'/usr/src/gtest': gtests will be built-- Using Python nosetests: /usr/bin/nosetests-2.7-- catkin0.6.14--BUILD_SHARED_LIBS is on-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-- ~~  traversing1packagesintopological order:-- ~~  -mypackage1-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-- +++ processing catkin package:'mypackage1'-- ==>add_subdirectory(mypackage1)-- Configuringdone-- Generatingdone-- Build files have been written to: /home/horsetail/catkin_ws/build

####

#### Running command:"make -j4 -l4"in"/home/horsetail/catkin_ws/build"####

Scanning dependencies of target talker

Scanning dependencies of target listener

[50%] Building CXXobjectmypackage1/CMakeFiles/listener.dir/src/listener.cpp.o

[100%] Building CXXobjectmypackage1/CMakeFiles/talker.dir/src/talker.cpp.o

Linking CXX executable/home/horsetail/catkin_ws/devel/lib/mypackage1/talker

[100%] Built target talker

Linking CXX executable/home/horsetail/catkin_ws/devel/lib/mypackage1/listener

[100%] Built target listener

恭喜编译成功了,接下来可以尝下先。

2.4、运行

运行的步骤和1.3是一样的,请参阅1.3的详细描述。因为按照两种方法编译了两个相同的包名和节点,因此在启动时会提示选择哪一个节点进行运行,按照提示选择即可。

2.5、源码

最后,附上源码:catkin_ws.tar.gz

推荐阅读更多精彩内容