1. 程式人生 > >Ubuntu14.04下配置ORB-SLAM

Ubuntu14.04下配置ORB-SLAM

本文記錄了ORB-SLAM在Ubuntu14.04下的安裝過程。
安裝環境:

  1. Trusty (Ubuntu 14.04) amd64
  2. ROS Indigo Igloo
  3. ORB-SLAM

文章目錄

1 準備工作

首先,安裝若干必備工具:

#sudo apt-get install vim git cmake -y

接下來將安裝 ROS Indigo Igloo

2 ROS Indigo Igloo 的安裝和配置

下面我將給出 ROS Indigo Igloo 在 Trusty (Ubuntu 14.04) 上的安裝和測試過程。
點選[這裡](http://wiki.ros.org/indigo/Installation/Ubuntu/“optional title here”),檢視 ROS 官網的 ROS Indigo Igloo 安裝手冊。
###2.1 配置Ubuntu軟體中心###
配置Ubuntu要求允許接受"restricted," “universe,” and "multiverse."的軟體源,可以根據下面的連結配置:

https://help.ubuntu.com/community/Repositories/Ubuntu

一般地,這是系統預設設定,如下圖所示:

ROS Indigo ONLY supports Saucy (Ubuntu13.10) and Trusty (Ubuntu14.04) for debian packages.這裡寫圖片描述

2.2 設定sources.list(軟體源)

設定你的計算機從packages.ros.org接收軟體包。

新增ROS官方軟體源:

sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'

一般地,ROS官方源速度很慢或者連線不上,推薦使用清華 TUNA Mirror 。
設定清華大學TUNA源:

sudo sh -c '. /etc/lsb-release && echo "deb http://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu/ $DISTRIB_CODENAME main" > /etc/apt/sources.list.d/ros-latest.list'

所有 [Mirrors](http://wiki.ros.org/ROS/Installation/UbuntuMirrors/“optional title here”)

2.3 設定的金鑰

sudo apt-key adv --keyserver hkp://ha.pool.sks-keyservers.net:80 --recv-key 421C365BD9FF1F717815A3895523BAEEB01FA116

如果你在連線祕鑰伺服器時遇到問題,你可以在上面的命令中替換連結為hkp://pgp.mit.edu:80或者hkp://keyserver.ubuntu.com:80

2.4 安裝

首先,更新Debian包索引

sudo apt-get update

If you are using Ubuntu Trusty 14.04.2 and experience dependency issues during the ROS installation, you may have to install some additional system dependencies.

Do not install these packages if you are using 14.04, it will destroy your X server:

sudo apt-get install xserver-xorg-dev-lts-trusty mesa-common-dev-lts-trusty libxatracker-dev-lts-trusty libopenvg1-mesa-dev-lts-trusty libgles2-mesa-dev-lts-trusty libgles1-mesa-dev-lts-trusty libgl1-mesa-dev-lts-trusty libgbm-dev-lts-trusty libegl1-mesa-dev-lts-trusty

Do not install the above packages if you are using 14.04, it will ***destroy*** your X server

Alternatively, try installing just this to fix dependency issues:

sudo apt-get install libgl1-mesa-dev-lts-trusty

For more information on this issue see this [answers.ros.org thread](http://answers.ros.org/question/203610/ubuntu-14042-unmet-dependencies/“optional title here”) or this [launchpad issue](https://bugs.launchpad.net/ubuntu/trusty/+source/mesa/+bug/1424059/“optional title here”)


X server is an application that manages one or more graphics displays and one or more input devices (keyboard, mouse, etc.) connected to the computer.

It works as a server and can run on the local computer or on another computer on the network. Services can communicate with the X server to display graphical interfaces and receive input from the user.

It’s worth noting, a common component used with an X server is the Window Manager, an application that manages the resizing and moving of windows and decorative elements of windows such as title bars, minimize, and close buttons.

The X server can be started with the ‘startx’ command, or more commonly, from a display manager such as gdm.

  • ~/.xinitrc is a shell script used by xinit, that starts the X server when not using a display manager, to define some application to start automatically in the X server.
  • /etc/X11/xorg.conf is a configuration file used to give the X server information about the hardware components used, but now the X server can avoid using it, because it is capable of autoconfiguring itself.

-在安裝之前,請認真閱讀上述引用。安裝ROS完整版後可能會導致 X server 損壞,在重新啟動Ubuntu後出現The system is running in low-graphics mode的錯誤,導致無法進入Ubuntu桌面。請嘗試點選 [answers.ros.org thread](http://answers.ros.org/question/203610/ubuntu-14042-unmet-dependencies/“optional title here”) 或者 [launchpad issue](https://bugs.launchpad.net/ubuntu/trusty/+source/mesa/+bug/1424059/“optional title here”) 解決問題。

ROS含有多種庫和工具。官網提供了4種預設配置的安裝供你選擇。你也可以依據自己需求進行個性化安裝。


  • (1) 桌面完整版
    Desktop-Full Install: (Recommended) : ROS, rqt, rviz, robot-generic libraries, 2D/3D simulators and 2D/3D perception

Indigo uses Gazebo 2 which is the default version of Gazebo on Trusty and is recommended. If you would like to instead use a newer version of Gazebo (5, 6 or 7), refer to these instructions on the Gazebo site. Note that installing a newer version of Gazebo will require you to build dependent packages (such as turtlebot_gazebo) to be built from source. See also Using a specific Gazebo version with ROS.

sudo apt-get install ros-indigo-desktop-full

  • (2) 桌面版
    Desktop Install: ROS, rqt, rviz, and robot-generic libraries
sudo apt-get install ros-indigo-desktop

  • (3) 基礎版
    ROS-Base: (Bare Bones) ROS package, build, and communication libraries. No GUI tools.
sudo apt-get install ros-indigo-ros-base

  • (4) 個性化安裝
    Individual Package: You can also install a specific ROS package (replace underscores with dashes of the package name):
sudo apt-get install ros-indigo-PACKAGE

PACKAGE指包的名稱

e.g.

sudo apt-get install ros-indigo-slam-gmapping

搜尋可用包:

apt-cache search ros-indigo

我們使用 Desktop-Full Install

sudo apt-get install ros-indigo-desktop-full

2.5 初始化 rosdep

在使用ROS之前,我們需要首先初始化rosdeprosdep幫助你輕鬆地進行系統依賴項原始碼的編譯和安裝,並且它是ROS核心元件執行的必要成分。

sudo rosdep init

rosdep初始化後如下圖,寫入/etc/ros/rosdep/sources.list.d/20-default.list
這裡寫圖片描述

更新ROS軟體包

rosdep update

2.6 設定環境

使用apt-get方式在 Ubuntu上安裝 ROS,在使用前需要啟用/opt/ros/indigo/目錄下的setup.bash檔案來新增 ROS 環境變數。

我們安裝的是ROS Indigo Igloo,命令如下:

source /opt/ros/indigo/setup.bash

在每次開啟一個新的 shell 時,我們都需要使用上述命令啟用setup.bash檔案。這樣很繁瑣。
因此,可以新增 ROS 的環境變數,這樣,當我們開啟新的shell時,我們的bash會話中會自動新增環境變數。

echo "source /opt/ros/indigo/setup.bash" >> ~/.bashrc

source ~/.bashrc #(使環境變數設定立即生效)

Tips:
使用下面命令可以在不同的 ROS distribution (ROS 發行版)間切換

$ source /opt/ros/<distro>/setup.bash

使用 ROS distribution 的短名稱替換 <distro>

如果我們想切換到 ROS Kinetic Kame :

$ source /opt/ros/kinetic/setup.bash

2.7 安裝 rosinstall

rosinstall是 ROS 包中常用的命令列工具。它可以幫助我們輕鬆地下載許多 ROS 包原始碼並建立依賴關係,並且只需要一行命令。

sudo apt-get install python-rosinstall

其他的常用項

sudo apt-get install python-rosinstall-generator python-wstool -y

2.8 測試 ROS

執行 roscore 是使用 ROS 的首要步驟。

roscore

我們可以看到

... logging to ~/.ros/log/9cf88ce4-b14d-11df-8a75-00251148e8cf/roslaunch-machine_name-13039.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://machine_name:33919/
ros_comm version 1.4.7

SUMMARY
======

PARAMETERS
 * /rosversion
 * /rosdistro

NODES

auto-starting new master
process[master]: started with pid [13054]
ROS_MASTER_URI=http://machine_name:11311/

setting /run_id to 9cf88ce4-b14d-11df-8a75-00251148e8cf
process[rosout-1]: started with pid [13067]
started core service [/rosout]

如圖所示
這裡寫圖片描述

說明 ROS 已經成功安裝並配置

2.9 建立 ROS 工作空間

有兩種方式建立 ROS 工作空間:catkinrosbuild

  • ROS Groovy 以及更新的版本請使用 catkin
  • ROS Fuerte 以及以前的版本請使用 rosbuild

我們開始建立 catkin workspaces

mkdir -p ~/catkin_ws/src

cd ~/catkin_ws/

catkin_make

The catkin_make command is a convenience tool for working with catkin workspaces. Running it the first time in your workspace, it will create a CMakeLists.txt link in your ‘src’ folder. Additionally, if you look in your current directory you should now have a ‘build’ and ‘devel’ folder. Inside the ‘devel’ folder you can see that there are now several setup.*sh files. Sourcing any of these files will overlay this workspace on top of your environment. To understand more about this see the general catkin documentation: catkin. Before continuing source your new setup.*sh file:

source devel/setup.bash

To make sure your workspace is properly overlayed by the setup script, make sure ROS_PACKAGE_PATH environment variable includes the directory you’re in.

[email protected]:~/catkin_ws$ echo $ROS_PACKAGE_PATH

/home/slam1a/catkin_ws/src:/opt/ros/indigo/share:/opt/ros/indigo/stacks

出現上述結果,說明我們的 catkin workspaces 已經建立好了,並且 ROS_PACKAGE_PATH也已經配置好了。

接下來的所有工作都在 ~/catkin_ws/src 目錄下完成,即在 catkin workspaces

=======================================================

3 ORB-SLAM依賴項的安裝

除了 ROS 外,ORB-SLAM 還依賴於其他若干第三方庫:BoostOpenCVEigen3g2oDBoW2g2oDBoW2 已經包含在 ORB-SLAM 專案下的 Thirdparty/ 目錄下。
下面我們將依次安裝其它各第三方庫。

3.1 Boost

使用 Boost 管理 ORB-SLAM 多執行緒。

sudo apt-get install libboost-all-dev

3.2 OpenCV

OpenCV 版本最低要求為2.4.3,建議採用 OpenCV 2.4.13 或者 OpenCV 3.2.0。
OpenCV 官網 或者 OpenCV GitHub 下載 OpenCV2.4.13。
也可以直接點選這裡下載

然後安裝依賴項:
安裝編譯工具

sudo apt-get install build-essential -y

安裝依賴包

sudo apt-get install libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev -y

安裝可選包

sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev -y

sudo apt-get install libgtk2.0-dev -y

sudo apt-get install pkg-config -y

進入 ~/catkin_ws/src

從GitHub下載opencv2.4.13,這不是一個 git repository,使用wget。
將下載的OpenCV解壓~/catkin_ws/src目錄下。

cd ~/catkin_ws/src

wget https://github.com/Itseez/opencv/archive/2.4.13.zip

unzip 2.4.13.zip

進入OpenCV的目錄下。
編譯安裝OpenCV 2.4.13 原始碼。

cd opencv-2.4.13/

mkdir build

cd build

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local .. 

make

sudo make install

安裝完畢。

檢視OpenCV版本。

pkg-config --modversion opencv

下面進行測試。
測試例項

//檔名字lena.cpp
#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;

int main( )
{
    Mat image;

    //按照自己的目錄,或者將
    image = imread("lena.png", 1 );
    if ( !image.data )
    {
        printf("No image data \n");
        return -1;
    }
    namedWindow("Display Image", WINDOW_AUTOSIZE );
    imshow("Display Image", image);
    waitKey(0);
    return 0;
}

lena.png

將上述 lena.cpplena.png 檔案放入同一目錄下,編譯

g++ lena.cpp -o lena.o  `pkg-config --cflags --libs opencv`

執行

./lena.o

效果如下
這裡寫圖片描述

OpenCVC 已經成功安裝。

3.3 Eigen3

最低要求版本為3.1.0。在http://eigen.tuxfamily.org 下載 Eigen3.2.10 壓縮檔案,並解壓。

進入~/catkin_ws/src/

cd ~/catkin_ws/src/
wget https://bitbucket.org/eigen/eigen/get/3.2.10.tar.bz2
tar -xjf 3.2.10.tar.bz2

得到eigen-eigen-b9cd8366d4e8目錄,重新命名eigen-3.2.10

mv eigen-eigen-b9cd8366d4e8/ eigen-3.2.10

編譯安裝eigen3.2.10

cd eigen-3.2.10/

mkdir build

cd build

cmake ..

make

sudo make install

Eigen3.2.10 安裝完畢

##4 編譯安裝 ORB-SLAM##

###4.1 下載 ORB-SLAM 原始碼###
進入 ROS 工作空間 ~/catkin_ws/src

cd ~/catkin_ws/src
git clone https://github.com/raulmur/ORB_SLAM.git ORB_SLAM

4.2 設定環境變數

ORB-SLAM專案的絕對路徑PATH_TO_PARENT_OF_ORB_SLAM~/catkin_ws/src/ORB-SLAM。設定環境變數ROS_PACKAGE_PATH為~/catkin_ws/src/ORB_SLAM

  • 開啟 ~/.bashrc,將下列命令新增在檔案最下面,儲存退出。
export ROS_PACKAGE_PATH=${ROS_PACKAGE_PATH}:~/catkin_ws/src/ORB_SLAM
  • 啟用 ~/.bashrc 檔案
source ~/.bashrc

這樣,開啟每個新的shell後就自動添加了環境變數。

4.3 編譯g2o

進入目錄 ORB_SLAM/Thirdparty/g2o/,並在命令列中執行命令,

mkdir build
cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
make

g2o 編譯完畢

###4.4 編譯DBoW2
進入目錄 ORB_SLAM/Thirdparty/DBoW2/,並在命令列中執行命令,

mkdir build
cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
make

DBoW2 編譯完畢

4.5 編譯ORB-SLAM專案

特別需要注意的是,我們安裝的 ROS 版本是 ROS Indigo,需要將根目錄下的 manifest.xml 檔案中的opencv2依賴一行的程式碼去掉!

# 用 <!--  ...  --!> 進行註釋 
#
<!--  <depend package="opencv2"/> -->

進入目錄ORB_SLAM專案根目錄,並在命令列中執行命令,

mkdir build
cd build
cmake .. -DROS_BUILD_TYPE=Release
make

ORB-SLAM 編譯完成

5 執行 ORB-SLAM##

5.1 準備測試資料

  • 下載資料集
    從http://webdiis.unizar.es/~raulmur/orbslam/downloads/Example.bag.tar.gz下載解壓得到
    Example.bag 進行測試。
cd ~/Downloads/

http://webdiis.unizar.es/~raulmur/orbslam/downloads/Example.bag.tar.gz

tar -xzf Example.bag.tar.gz
  • 進入~/catkin_ws/src/ORB_SLAM/Data目錄,解壓 ORBvoc.txt.tar.gz 得到 ORBvoc.txt
cd ~/catkin_ws/src/ORB_SLAM/Data

tar -xzvf ORBvoc.txt.tar.gz

測試資料集準備就緒

5.2 執行 ORB-SLAM

使用根目錄下的launch檔案啟動程式,ORB_SLAM專案目錄下包含兩個launch檔案:ExampleFuerte.launchExampleGroovyOrNewer.launch,對應不同的ROS版本,根據ROS的版本選擇不同的launch檔案。launch檔案是一個指令碼檔案,包含啟動程式的各個步驟。接下來,按下列步驟啟動 ORB-SLAM。

  • 開啟終端(快捷鍵ctrl+alt+t),執行命令啟動ROS服務
roscore

這裡寫圖片描述

  • 開啟新的終端,進入launch所在目錄,執行命令
roslaunch ExampleGroovyOrNewer.launch

這裡寫圖片描述

  • 開啟新的終端,進入測試用例Example.bag所在的目錄,執行下面的命令,啟動後,按s鍵可以單步檢視執行結果
rosbag play --pause Example.bag

執行下面命令,完整執行

rosbag play Example.bag

這裡寫圖片描述

ORB-SLAM 成功執行