ROS的包編譯有兩種方法(我知道的),一種是用rosmake,這種方法簡單;另外一種是用catkin_make,這種方法更方便包的管理和開發。這兩種方法都是先創建工做空間workspace(相似於vs下的解決方案,用來管理不少的項目),而後創建包package(相似於vs下的項目),最後利用rosmake或者catkin_make進行編譯和運行。學會第一種方式,再去學習第二種就很簡單了。源碼附在每一個小節的後面。node
一、rosmake編譯包packagepython
1.一、建立工做空間c++
在開始具體工做以前,首先建立工做空間,而且爲工做空間設置環境變量到~/.bashrc中,若是要查看已有的空間路徑,能夠用查詢命令bash
$ echo $ROS_PACKAGE_PATH
你將會看到以下的信息:工具
/home/horsetail/dev/rosbook:/home/horsetail/catkin_ws/src:/opt/ros/jade/share:/opt/ros/jade/stacks
這裏的建立空間實際上就是先創建一個文件夾,而後把文件夾的路徑設置到環境變量~/.bashrc中。例如咱們這裏建立目錄~/dev/rosbook做爲工做空間。oop
首先執行命令:學習
$ cd ~ $ mkdir -p dev/rosbook
而後將建立的路徑加入到環境變量中,執行以下命令:網站
$ echo "export ROS_PACKAGE_PATH=~/dev/rosbook:${ROS_PACKAGE_PATH}" >> ~/.bashrc $ . ~/.bashrc
這樣,咱們就完成了工做空間的配置,注意:ROS安裝的時候,必定要把ROS的環境變量也加到~/.bashrc中。這裏還須要把ROS。接下來就是在這個空間下建立包了。ui
1.二、建立包spa
能夠手動建立包,可是很是的繁瑣,爲了方便,最好使用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.三、代碼編輯和編譯
參考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 <sstream> int main(int argc, char **argv) { ros::init(argc, argv, "talker"); ros::NodeHandle n; ros::Publisher chatter_pub = n.advertise<std_msgs::String>("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.四、運行
首先打開一個新的終端,啓動初始化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.五、源碼
最後,附上源碼:dev.tar.gz
二、catkin_make編譯包package
catkin命令建立工做空間和包相對要複雜些,可是熟悉之後對項目的管理是很是有利的,所以官方也建議使用第二種方式。
2.一、建立工做空間
在開始具體工做以前,首先建立工做空間,而且爲工做空間設置環境變量到~/.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.二、建立包
首先切換到以前建立的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.三、編譯
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 -- 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.四、運行
運行的步驟和1.3是同樣的,請參閱1.3的詳細描述。由於按照兩種方法編譯了兩個相同的包名和節點,所以在啓動時會提示選擇哪個節點進行運行,按照提示選擇便可。
2.五、源碼
最後,附上源碼:catkin_ws.tar.gz
參考資料
[1]. Aaron Martinez Enrique Fern andez, ROS機器人程序設計[B], P14-42, 2014.