使用笔记本摄像头跑 ORB_SLAM2

1.准备运行环境

1.1 操作系统

Ubuntu 16.04 LTS,最好是干净的系统。

可以到阿里巴巴开源镜像站或其他下载站下载系统镜像。 https://mirrors.aliyun.com/ubuntu-releases/16.04/ubuntu-16.04.6-desktop-amd64.iso

安装git、cmake、vim、wget

1
2
3
4
5
sudo apt-get update
sudo apt-get install git
sudo apt-get install cmake
sudo apt-get install vim
sudo apt-get install wget

1.2 ROS

安装说明:http://wiki.ros.org/cn/kinetic/Installation/Ubuntu

1.2.1 配置 Ubuntu 软件仓库

配置你的 Ubuntu 软件仓库(repositories) 以允许 “restricted”、“universe” 和 “multiverse"这三种安装模式。 你可以按照ubuntu中的配置指南来完成配置。

1.2.2 添加 sources.list

官方源速度有些慢,添加中科大源

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

或清华源

1
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'

1.2.3 添加key

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

添加公钥(官方文档没有这一步,但是不添加公钥会报不信任错误)

1
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys F42ED6FBAB17C654

1.2.4 安装

1
sudo apt-get update

安装桌面完整版

1
sudo apt-get install ros-kinetic-desktop-full

1.2.5 初始化rosdep

1
2
sudo rosdep init
rosdep update

1.2.6 环境配置

1
2
echo "source /opt/ros/kinetic/setup.bash" >> ~/.bashrc
source ~/.bashrc

1.2.7 构建工厂依赖

1
sudo apt-get install python-rosinstall python-rosinstall-generator python-wstool build-essential

1.2.8 配置ROS环境,创建工作空间

参考:http://wiki.ros.org/cn/ROS/Tutorials/InstallingandConfiguringROSEnvironment

1
2
3
4
5
6
mkdir -p ~/catkin_ws/src
cd ~/catkin_ws
catkin_make
source devel/setup.bash
echo "source ~/catkin_ws/devel/setup.bash" >> ~/.bashrc
source ~/.bashrc

2. 安装依赖项

2.1 安装Pangolin

准备依赖

1
sudo apt-get install libglew-dev libpython2.7-dev libboost-dev libboost-thread-dev libboost-filesystem-dev -y

下载Pangolin并配置环境

1
2
3
4
5
6
7
git clone https://github.com/stevenlovegrove/Pangolin Pangolin
cd Pangolin
mkdir build
cd build
cmake ..
make -j2
sudo make install

2.2 安装Eigen3

最低要求为3.1.0,这里安装Eigen 3.2.10。

可以从官网下载源码。

1
2
3
4
cd ~/catkin_ws/src
wget https://bitbucket.org/eigen/eigen/get/3.2.10.tar.bz2
tar -xjf 3.2.10.tar.bz2
mv eigen-eigen-b9cd8366d4e8/ eigen-3.2.10

编译安装

1
2
3
4
5
6
cd eigen-3.2.10
mkdir build
cd build
cmake ..
make
sudo make install

2.3 安装OpenCV 3.2.0

参考:https://docs.opencv.org/master/d7/d9f/tutorial_linux_install.html

  1. 安装依赖(前两个必装,optional可选)
1
2
3
[compiler] sudo apt-get install build-essential
[required] sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
[optional] sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
  1. 下载(也可以从官网下载安装)
1
2
3
4
5
6
cd ~
wget https://github.com/opencv/opencv/archive/3.2.0.zip
unzip 3.2.0.zip
cd opencv-3.2.0
mkdir build
cd build
  1. 配置并编译安装
1
2
3
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
make
sudo make install
  1. 在/etc/ld.so.conf.d/opencv.conf中添加路径
1
sudo vim /etc/ld.so.conf.d/opencv.conf

​ 在其中添加

1
/usr/local/lib

​ 使之生效

1
sudo ldconfig
  1. 在bash.bashrc中添加路径
1
sudo vim /etc/bash.bashrc

​ 在其中添加

1
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig export PKG_CONFIG_PATH

​ 使之生效

1
source /etc/bash.bashrc
  1. 测试opencv
1
2
3
4
5
6
cd ~/opencv-3.2.0/samples/cpp/example_cmake
mkdir muild
cd build
cmake ..
make
./opencv_example

​ 此时应当打开摄像头,并在左上角显示 Hello OpenCV

2.4 安装usb_cam

同样的,要安装在工作区的~/catkin_ws/src文件夹下。

  1. 下载源码
1
2
3
4
5
cd ~/catkin_ws/src
git clone https://github.com/bosch-ros-pkg/usb_cam.git
cd ..
catkin_make
source ~/catkin_ws/devel/setup.bash
  1. 编译安装
1
2
3
4
5
cd ~/catkin_ws/src/usb_cam
mkdir build
cd build
cmake ..
make
  1. 测试摄像头

    1. 打开终端
    1
    
    roscore
    1. 打开新的终端
    1
    2
    3
    
    cd ~/catkin_ws/src/usb_cam/launch
    source ~/catkin_ws/devel/setup.bash
    roslaunch usb_cam usb_cam-test.launch

    能正常打开摄像头,显示图像即可。

3. 安装ORB SLAM2

  1. 下载源码到工作空间 ~/catkin_ws/src
1
2
3
cd ~/catkin_ws/src
git clone https://github.com/raulmur/ORB_SLAM2.git ORB_SLAM2
cd ORB_SLAM2
  1. 分别打开build.sh和build_ros.sh,更改 make -jmake -j2(或j4)。

  2. 编译安装(无ROS依赖):

1
2
chmod +x build.sh
./build.sh
  1. 添加路径

sudo vim ~/.bashrc,在最后添加

1
export ROS_PACKAGE_PATH=${ROS_PACKAGE_PATH}:~/catkin_ws/src/ORB_SLAM2/Examples/ROS
1
source ~/.bashrc
  1. 配置库文件

    1. 打开终端,查找库文件
    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    $ locate boost_system //查找到目录,然后加到cmakelist中。
    /usr/lib/x86_64-linux-gnu/libboost_system.a 
    /usr/lib/x86_64-linux-gnu/libboost_system.so 
    /usr/lib/x86_64-linux-gnu/libboost_system.so.1.58.0 
    
    $ locate boost_filesystem //查找到目录 
    /usr/lib/x86_64-linux-gnu/libboost_filesystem.a 
    /usr/lib/x86_64-linux-gnu/libboost_filesystem.so 
    /usr/lib/x86_64-linux-gnu/libboost_filesystem.so.1.58.0
    1. 拷贝库文件
    1
    2
    
    sudo cp /usr/lib/x86_64-linux-gnu/libboost_system.so ~/catkin_ws/src/ORB_SLAM2/lib
    sudo cp /usr/lib/x86_64-linux-gnu/libboost_filesystem.so ~/catkin_ws/src/ORB_SLAM2/lib
    1. 打开CMakeLists.txt
    1
    2
    3
    
    vim ~/catkin_ws/src/ORB_SLAM2/Examples/ROS/ORB_SLAM2/CMakeLists.txt
    gedit ~/catkin_ws/src/ORB_SLAM2/Examples/ROS/ORB_SLAM2/CMakeLists.txt

    ​ 找到 set(LIBS ... ,添加以下内容:

    1
    2
    3
    
    set(LIBS
    ${PROJECT_SOURCE_DIR}/../../../lib/libboost_filesystem.so
    ${PROJECT_SOURCE_DIR}/../../../lib/libboost_system.so
  2. 编译ORB SLAM2 ROS

    1
    2
    3
    
    cd ~/catkin_ws/src/ORB_SLAM2
    chmod +x build_ros.sh
    ./build_ros.sh

4.运行ROS实例(笔记本摄像头)

  1. 标定相机

    参考https://www.jianshu.com/p/c3e8c88edb64进行相机参数标定。

  2. 订阅usb_cam节点

    1. 在ROS目录下的ros_mono.cc文件中修改,双目,深度以及AR demo同理。

      /camera/image_raw 改为 /usb_cam/image_raw

    2. 修改完成后删除Examples/ROS/ORB_SLAM2/下的build文件夹:

    1
    2
    
    cd ~/catkin_ws/src/ORB_SLAM2/Examples/ROS/ORB_SLAM2/
    sudo rm -r build
    1. 重编ORB SLAM ROS
    1
    2
    
    cd ~/catkin_ws/src/ORB_SLAM2/
    ./build_ros.sh
  3. 启动新的终端,运行usb_cam

    1
    
    roslaunch usb_cam usb_cam-test.launch
  4. 启动新的终端,运行orb_slam

    1
    
    rosrun ORB_SLAM2 Mono ~/catkin_ws/src/ORB_SLAM2/Vocabulary/ORBvoc.txt ~/catkin_ws/src/ORB_SLAM2/Examples/ROS/ORB_SLAM2/Asus.yaml

5.后记

ORB SLAM是我第一个跑的开源项目,从配置到跑完折腾了挺久,期间还重装了三次系统。为了最顺利的跑通项目,最好使用一个新安装的Ubuntu 16.04 LTS系统,按照本文跑下来应该是没有什么问题的。在装完系统之后,提示的升级先都不要升,我有一次执行 sudo apt upgrade之后,ROS就装不上了==虽然不太清楚究竟是什么原因,但还是不要做一些奇怪的操作比较好。

参考资料:

https://blog.csdn.net/qq_39397000/article/details/83745332

https://blog.csdn.net/qq_42037180/article/details/88371971

0%