二、快速啓動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

依稀可以看出寫的抽象英語…
在這裏插入圖片描述

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