Ubuntu16.04 LTS安装ROS Kinetic 参考:Ubuntu install of ROS Kinetic
1.创建工作区
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.创建功能包
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
这里生成了两个配置文件CMakeLists.txt和package.xml:
CMakeLists.txt是一个Cmake脚本文件;包含了一系列的编译指令,包括应该生成哪种可执行文件,需要哪些源文件,以及在哪里可以找到所需的头文件和链接库。当然,这个文件表明 catkin 在内部使用了 Cmake。
package.xml是清单文件;关于包的一些细节,包括其名称、版本、维护者和依赖关系。
3.创建源码文件
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++代码,先修改两个配置文件完成hello程序的编译;
第一步,修改CMakeLists.txt文件,添加依赖库,修改如下:
find_package(catkin REQUIRED)
改为
find_package(catkin REQUIRED COMPONENTS roscpp) #添加roscpp依赖库,它提供了 ROS 的 C++客户端库
第二步,修改package.xml文件,列出依赖库:
<build_depend>roscpp</build_depend>
<run_depend>roscpp</run_depend>
第三步,CMakeLists.txt添加生命可执行文件:
add_executable(hello src/hello.cpp)
target_link_libraries(hello
${catkin_LIBRARIES}
)
5.编译
工作区根目录执行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.运行
pim_server@pim-Precision-T1700:~/ROS/Project$ source devel/setup.bash #设置环境变量
pim_server@pim-Precision-T1700:~/ROS/Project$ rosrun hello hello_node #运行
[ INFO] [1476870000.446207343]: Hello, ROS!
注意:首先要在终端启动rosrun
7.附加
创建功能包时,还可以通过命令行参数添加依赖库,如下
pim_server@pim-Precision-T1700:~/ROS/Project/src$ catkin_create_pkg hello roscpp
小记:对Cmake不熟悉,导致hello world程序编译、运行起来花了一点时间,最后总算跑起来,任重道远啊~