二、快速启动ROS小乌龟


启动你的ROS小乌龟

本想在环境配好了之后去买一本书,但是看到一篇文章也想到之前看过类似知乎回答—— 如何学习机器人Ros?

也就放弃了,改看ros官方的指导,这里附上中文的官网指导链接ROS Tutorials,接下来步入正题,如何通过这篇文章快速让你的ROS运行起标志性乌龟。请注意,这里指的是“快速”,如果想细节性理解一些函数或库请使用上边提到的官方链接学习。

安装并配置ROS环境

这些操作方法只适用于 ROS Groovy 及后期版本,对于 ROS Fuerte 及早期版本请选择 rosbuild 。下面我们开始创建一个 catkin 工作空间

$ mkdir -p ~/catkin_ws/src
$ cd ~/catkin_ws/src
$ catkin_make

另外,如果你查看一下当前目录应该能看到 ‘build’ 和 ‘devel’ 这两个文件夹。在 ‘devel’ 文件夹里面你可以看到几个 setup.*sh 文件。source 这些文件中的任何一个都可以将当前工作空间设置在ROS工作环境的最顶层,想了解更多请参考 catkin 文档。接下来首先 source 一下新生成的 setup.*sh 文件:

$ source devel/setup.bash

本教程中我们将会用到ros-tutorials程序包,请先安装,如果安装不上先进行下一步,如果你按照我之前的教程那么已经安装了这个包:

$ sudo apt-get install ros-<distro>-ros-tutorials

将 ‘’ (包括’<>’)替换成你所安装的版本(比如 kinetic、melodic 等)。

创建一个catkin程序包

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

# You should have created this in the Creating a Workspace Tutorial
$ cd ~/catkin_ws/src

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

$ catkin_create_pkg beginner_tutorials std_msgs rospy roscpp

按照之前的创建一个ROS程序包教程,你应该已经创建好了一个catkin 工作空间 和一个名为beginner_tutorials的catkin 程序包。现在切换到catkin workspace 并查看src文件夹:

$ cd ~/catkin_ws/
$ ls src
## it should be like
## beginner_tutorials/  CMakeLists.txt@  

你可以看到一个名为beginner_tutorials的文件夹,这就是你在之前的 catkin_create_pkg教程里创建的。现在我们可以使用catkin_make来编译它了:

$ catkin_make

roscore

roscore 是你在运行所有ROS程序前首先要运行的命令。请运行:

$ roscore

你可以看到很多cmakemake 输出的信息:

Base path: /home/user/catkin_ws
Source space: /home/user/catkin_ws/src
Build space: /home/user/catkin_ws/build
Devel space: /home/user/catkin_ws/devel
Install space: /home/user/catkin_ws/install
####
#### Running command: "cmake /home/user/catkin_ws/src
-DCATKIN_DEVEL_PREFIX=/home/user/catkin_ws/devel
-DCMAKE_INSTALL_PREFIX=/home/user/catkin_ws/install" in "/home/user/catkin_ws/build"
####
-- The C compiler identification is GNU 4.2.1
-- The CXX compiler identification is Clang 4.0.0
-- Checking whether C compiler has -isysroot
-- Checking whether C compiler has -isysroot - yes
-- Checking whether C compiler supports OSX deployment target flag
-- Checking whether C compiler supports OSX deployment target flag - yes
-- Check for working C compiler: /usr/bin/gcc
-- Check for working C compiler: /usr/bin/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Using CATKIN_DEVEL_PREFIX: /tmp/catkin_ws/devel
-- Using CMAKE_PREFIX_PATH: /opt/ros/groovy
-- This workspace overlays: /opt/ros/groovy
-- Found PythonInterp: /usr/bin/python (found version "2.7.1") 
-- Found PY_em: /usr/lib/python2.7/dist-packages/em.pyc
-- Found gtest: gtests will be built
-- catkin 0.5.51
-- BUILD_SHARED_LIBS is on
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- ~~  traversing packages in topological order:
-- ~~  - beginner_tutorials
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- +++ add_subdirectory(beginner_tutorials)
-- Configuring done
-- Generating done
-- Build files have been written to: /home/user/catkin_ws/build
####
#### Running command: "make -j4" in "/home/user/catkin_ws/build"
####

注1:如果 roscore 运行后无法正常初始化,很有可能是存在网络配置问题。参见网络设置——单机设置

注2:如果 roscore 不能初始化并提示缺少权限,这可能是因为~/.ros文件夹归属于root用户(只有root用户才能访问),修改该文件夹的用户归属关系:

$ sudo chown -R <your_username> ~/.ros

catkin_make首先输出它所使用到的每个空间所在的路径。更多关于空间的信息,请参考REP128catkin/workspaces。需要注意的是由于这些空间存在默认配置的原因,有几个文件夹已经在catkin工作空间自动生成了,使用ls查看:

$ ls
## it should be like 
## build
## devel
## src

build 目录是build space的默认所在位置,同时cmakemake也是在这里被调用来配置并编译你的程序。devel 目录是devel space的默认所在位置, 同时也是在你安装程序包之前存放可执行文件和库文件的地方。

使用rosrun

现在我们可以运行turtlesim包中的 turtlesim_node。

然后, 在一个 新的终端:

$ rosrun turtlesim turtlesim_node

你会看到 turtlesim 窗口:
在这里插入图片描述

接下来运行一个键盘控制的node,我们需要通过键盘来控制turtle的运动,请 在一个新的终端中 运行::

$ rosrun turtlesim turtle_teleop_key

依稀可以看出写的抽象英语…
在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章