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

ROS的包编译有两种方法(我知道的),一种是用rosmake,这种方法简单;另一种是用catkin_make,这种方法更方便包的管理和开发。这两种方法都是先建立工作空间workspace(类似于vs下的解决方案,用来管理很多的项目),然后建立包package(类似于vs下的项目),最后利用rosmake或者catkin_make进行编译和运行。学会第一种方式,再去学习第二种就很简单了。源码附在每个小节的后面。

1、rosmake编译包package

 

1.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作为工作空间。

首先执行命令:

$ cd ~
$ mkdir -p dev/rosbook

然后将创建的路径加入到环境变量中,执行如下命令:

$ echo "export ROS_PACKAGE_PATH=~/dev/rosbook:${ROS_PACKAGE_PATH}" >> ~/.bashrc
$ . ~/.bashrc

这样,我们就完成了工作空间的配置,注意:ROS安装的时候,一定要把ROS的环境变量也加到~/.bashrc中。这里还需要把ROS。接下来就是在这个空间下创建包了。

 

1.2、创建包

 可以手动创建包,但是非常的繁琐,为了方便,最好使用roscreate-pkg命令行工具,该命令行的格式如下:

roscreate-pkg [package_name] [depend1] [depend2] [depend3] ...

命令行包含了要创建包的名字,依赖包。

我们的例子中,创建一个叫mypacakge1的 新包,命令如下:

$ cd ~/dev/rosbook
$ roscreate-pkg mypackage1 std_msgs roscpp rospy

过一会弹出如下的信息,表示创建成功:

Created package directory /home/horsetail/dev/rosbook/mypackage1
Created include directory /home/horsetail/dev/rosbook/mypackage1/include/mypackage1
Created cpp source directory /home/horsetail/dev/rosbook/mypackage1/src
Created package file /home/horsetail/dev/rosbook/mypackage1/Makefile
Created package file /home/horsetail/dev/rosbook/mypackage1/manifest.xml
Created package file /home/horsetail/dev/rosbook/mypackage1/CMakeLists.txt
Created package file /home/horsetail/dev/rosbook/mypackage1/mainpage.dox

Please edit mypackage1/manifest.xml and mainpage.dox to finish creating your package

好了这样就完成了包的创建,我们发现在mypackage1的目录下有一个src文件夹,我们接下来就是网这里添加源程序了。

 

1.3、代码编辑和编译

 参考ROS官方网站的教程,我们编写一个编写简单的消息发布器和订阅器 (C++),即编写俩个源文件talker.cpp和listener.cpp,并将他们保存到~/dev/rosbook/mypackage1/src目录中。

代码理解可以参考ROS_wiki:http://wiki.ros.org/cn/ROS/Tutorials/WritingPublisherSubscriber%28c%2B%2B%29

发布器就是一个说话者talker,/mypackage1/srctalker.cpp代码如下:

#include "ros/ros.h"
#include "std_msgs/String.h"

#include 

int main(int argc, char **argv)
{
  ros::init(argc, argv, "talker");

  ros::NodeHandle n;

  ros::Publisher chatter_pub = n.advertise("chatter", 1000);

  ros::Rate loop_rate(10);

  int count = 0;
  while (ros::ok())
  {
    std_msgs::String msg;

    std::stringstream ss;
    ss << "hello world " << count;
    msg.data = ss.str();

    ROS_INFO("%s", msg.data.c_str());

chatter_pub.publish(msg); ros::spinOnce(); loop_rate.sleep(); ++count; } return 0; }

订阅者就是一个听话人listener,他不停地接受talker广播出来的消息,并显示到屏幕上,/mypackage1/srclistener.cpp的代码为:

include "ros/ros.h"
#include "std_msgs/String.h"
void chatterCallback(const std_msgs::String::ConstPtr& msg) { ROS_INFO("I heard: [%s]", msg->data.c_str()); } int main(int argc, char **argv) { ros::init(argc, argv, "listener"); ros::NodeHandle n; ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback); ros::spin(); return 0; }

好了,把上面的源文件放到mypackage1/src下就可以了。

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

rosbuild_add_executable(talker src/talker.cpp)
rosbuild_add_executable(listener src/listener.cpp)

 添加后的文件结构是这样的:

cmake_minimum_required(VERSION 2.4.6)
include($ENV{ROS_ROOT}/core/rosbuild/rosbuild.cmake)

# Set the build type.  Options are:
#  Coverage       : w/ debug symbols, w/o optimization, w/ code-coverage
#  Debug          : w/ debug symbols, w/o optimization
#  Release        : w/o debug symbols, w/ optimization
#  RelWithDebInfo : w/ debug symbols, w/ optimization
#  MinSizeRel     : w/o debug symbols, w/ optimization, stripped binaries
#set(ROS_BUILD_TYPE RelWithDebInfo)

rosbuild_init()

#set the default path for built executables to the "bin" directory
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
#set the default path for built libraries to the "lib" directory
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)

#uncomment if you have defined messages
rosbuild_genmsg()
#uncomment if you have defined services
rosbuild_gensrv()

#common commands for building c++ executables and libraries
#rosbuild_add_library(${PROJECT_NAME} src/example.cpp)
#target_link_libraries(${PROJECT_NAME} another_library)
#rosbuild_add_boost_directories()
#rosbuild_link_boost(${PROJECT_NAME} thread)
#rosbuild_add_executable(example examples/example.cpp)
#target_link_libraries(example ${PROJECT_NAME})
rosbuild_add_executable(talker src/talker.cpp)
rosbuild_add_executable(listener src/listener.cpp)

 万事具备,这样我们就可用rosmake命令来编译这个mypackage1包了。执行下面的命令:

$ rosmake mypackage1

输出下面的信息:

horsetail@horsetail-book:~$ roscore
... logging to /home/horsetail/.ros/log/6eae5b9c-628d-11e5-8bd7-3859f9722953/roslaunch-horsetail-book-6447.log
Checking log directory for disk usage. This may take awhile.
Press Ctrl-C to interrupt
Done checking log file disk usage. Usage is <1GB.

started roslaunch server http://horsetail-book:44362/
ros_comm version 1.11.13


SUMMARY
========

PARAMETERS
 * /rosdistro: jade
 * /rosversion: 1.11.13

NODES

auto-starting new master
process[master]: started with pid [6459]
ROS_MASTER_URI=http://horsetail-book:11311/

...(内容太长了,省去)
[ rosmake ] Results: [ rosmake ] Built 26 packages with 0 failures. [ rosmake ] Summary output to directory [ rosmake ] /home/horsetail/.ros/rosmake/rosmake_output-20150924-164014

哇,编译通过,大家注意到实际上也是用catkin进行编译的,额。我们来运行一下吧。

 

1.4、运行

首先打开一个新的终端,启动初始化ROS,执行命令:

$ roscore

然后再打开一个新的终端,启动talker节点,执行命令:

$ rosrun mypackage1 talker

然后再打开一个新的终端,启动listener节点,执行命令:

$ rosrun mypackage1 listener

这个时候,可以看到listener的窗口不断的接收到数据,如下图所示:

[ INFO] [1443085572.809193925]: I heard:[I am the talker node]
[ INFO] [1443085572.909233411]: I heard:[I am the talker node]
[ INFO] [1443085573.009267370]: I heard:[I am the talker node]
[ INFO] [1443085573.109118292]: I heard:[I am the talker node]
[ INFO] [1443085573.209204095]: I heard:[I am the talker node]
[ INFO] [1443085573.309314244]: I heard:[I am the talker node]
[ INFO] [1443085573.409269818]: I heard:[I am the talker node]
[ INFO] [1443085573.509309147]: I heard:[I am the talker node]

这说明talker广播的消息是能够被listener接收到的,因此程序可以正常运行了。好了,至此基于rosmake的编译和运行完成了。

 

1.5、源码

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

 

2、catkin_make编译包package

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(VERSION 2.8.3)
project(mypackage1)

## Find catkin and any catkin packages
find_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 package
catkin_package()

## Build talker and listener
include_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
-- catkin 0.6.14
-- BUILD_SHARED_LIBS is on
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- ~~  traversing 1 packages in topological 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标准工作流程中依次调用了cmakemake

使用方法:

# 在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
-- catkin 0.6.14
-- BUILD_SHARED_LIBS is on
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- ~~  traversing 1 packages in topological order:
-- ~~  - mypackage1
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- +++ processing catkin package: 'mypackage1'
-- ==> add_subdirectory(mypackage1)
-- Configuring done
-- Generating done
-- 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 CXX object mypackage1/CMakeFiles/listener.dir/src/listener.cpp.o
[100%] Building CXX object mypackage1/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

 

参考资料

[1]. Aaron Martinez Enrique Fern andez, ROS机器人程序设计[B], P14-42, 2014.

转载于:https://www.cnblogs.com/cv-pr/p/4835635.html

你可能感兴趣的:(ROS知识(3)----功能包package编译的两种方式)