Ubuntu16.04 LTS安裝ROS Kinetic 參考:Ubuntu install of ROS Kinetic
1.創(chuàng)建工作區(qū)
pim_server@pim-Precision-T1700:~/ROS$ mkdir Project
pim_server@pim-Precision-T1700:~/ROS$ cd Project/
pim_server@pim-Precision-T1700:~/ROS/Project$ mkdir src
pim_server@pim-Precision-T1700:~/ROS/Project$ cd src/
pim_server@pim-Precision-T1700:~/ROS/Project/src$ pwd
/home/pim_server/ROS/Project/src
2.創(chuàng)建功能包
pim_server@pim-Precision-T1700:~/ROS/Project/src$ catkin_create_pkg hello
Created file hello/package.xml
Created file hello/CMakeLists.txt
Successfully created files in /home/pim_server/ROS/Project/src/hello. Please adjust the values in package.xml.
pim_server@pim-Precision-T1700:~/ROS/Project/src$ ls
hello
pim_server@pim-Precision-T1700:~/ROS/Project/src$ cd hello/
pim_server@pim-Precision-T1700:~/ROS/Project/src/hello$ ls
CMakeLists.txt ?package.xml
這里生成了兩個(gè)配置文件CMakeLists.txt和package.xml:
CMakeLists.txt是一個(gè)Cmake腳本文件葛虐;包含了一系列的編譯指令,包括應(yīng)該生成哪種可執(zhí)行文件,需要哪些源文件,以及在哪里可以找到所需的頭文件和鏈接庫(kù)涎拉。當(dāng)然,這個(gè)文件表明 catkin 在內(nèi)部使用了 Cmake。
package.xml是清單文件缺前;關(guān)于包的一些細(xì)節(jié),包括其名稱、版本们衙、維護(hù)者和依賴關(guān)系剃斧。
3.創(chuàng)建源碼文件
pim_server@pim-Precision-T1700:~/ROS/Project/src/hello$ mkdir src
pim_server@pim-Precision-T1700:~/ROS/Project/src/hello$ cd src/
pim_server@pim-Precision-T1700:~/ROS/Project/src/hello/src$ vim hello.cpp
pim_server@pim-Precision-T1700:~/ROS/Project/src/hello/src$ ls
hello.cpp
hello.cpp源碼文件如下:
#include <ros/ros.h>
int main(int argc, char** argv){
? ?ros::init(argc, argv, "hello ros");
? ?ros::NodeHandle nh;
? ?ROS_INFO_STREAM("Hello, ROS!");
}
4.編譯前配置
忽略上面C++代碼瞬逊,先修改兩個(gè)配置文件完成hello程序的編譯;
第一步仪或,修改CMakeLists.txt文件确镊,添加依賴庫(kù),修改如下:
find_package(catkin REQUIRED)
改為
find_package(catkin REQUIRED COMPONENTS roscpp) ? ?#添加roscpp依賴庫(kù),它提供了 ROS 的 C++客戶端庫(kù)
第二步范删,修改package.xml文件蕾域,列出依賴庫(kù):
<build_depend>roscpp</build_depend>
<run_depend>roscpp</run_depend>
第三步,CMakeLists.txt添加生命可執(zhí)行文件:
add_executable(hello src/hello.cpp)
target_link_libraries(hello
? ?${catkin_LIBRARIES}
)
5.編譯
工作區(qū)根目錄執(zhí)行catkin_make:
pim_server@pim-Precision-T1700:~/ROS/Project$ catkin_make
Base path: /home/pim_server/ROS/Project
Source space: /home/pim_server/ROS/Project/src
Build space: /home/pim_server/ROS/Project/build
Devel space: /home/pim_server/ROS/Project/devel
Install space: /home/pim_server/ROS/Project/install
####
#### Running command: "make cmake_check_build_system" in "/home/pim_server/ROS/Project/build"
####
####
#### Running command: "make -j8 -l8" in "/home/pim_server/ROS/Project/build"
####
Scanning dependencies of target hello_node
[ 50%] Building CXX object hello/CMakeFiles/hello_node.dir/src/hello.cpp.o
[100%] Linking CXX executable /home/pim_server/ROS/Project/devel/lib/hello/hello_node
[100%] Built target hello_node
6.運(yùn)行
pim_server@pim-Precision-T1700:~/ROS/Project$ source devel/setup.bash ? ?#設(shè)置環(huán)境變量
pim_server@pim-Precision-T1700:~/ROS/Project$ rosrun hello hello_node ? #運(yùn)行
[ INFO] [1476870000.446207343]: Hello, ROS!
注意:首先要在終端啟動(dòng)rosrun
7.附加
創(chuàng)建功能包時(shí)到旦,還可以通過命令行參數(shù)添加依賴庫(kù)旨巷,如下
pim_server@pim-Precision-T1700:~/ROS/Project/src$ catkin_create_pkg hello roscpp
小記:對(duì)Cmake不熟悉,導(dǎo)致hello world程序編譯添忘、運(yùn)行起來(lái)花了一點(diǎn)時(shí)間采呐,最后總算跑起來(lái),任重道遠(yuǎn)啊~