Ubuntu18.04安装灭霸SLAM:ORBSLAM3


1.写在前面

终于有时间好好整理一下最近跑通ORB-SLAM3的一些笔记了,在xavier上安装了一下谷歌拼音输入法,具体可以参考:AGX Xavier安装中文输入法
先说结论:
1)不建议安装最新版,安装时有许多坑需要填,即使填好了,运行时依然会有很多坑,比如依赖于最新版的opencv4.4等等,最新版链接为:UZ-SLAMLab/ORB_SLAM3,安装参考链接为:ORB-SLAM3 安装运行。

2)建议使用opencv3版本作为依赖,如果是请跳过第二步;如果不是,请看第二步,继续填坑。


2.安装依赖

2.1Pangolin

下载地址:Pangolin
安装方式:
Pangolin的依赖安装

# Required
# OpenGL (Desktop / ES / ES2)
sudo apt install libgl1-mesa-dev
# Glew
sudo apt install libglew-dev
# CMake
sudo apt install cmake

# Recommended
# Python2 / Python3, for drop-down interactive console
sudo apt install libpython2.7-dev
# Wayland
sudo apt install pkg-config
sudo apt install libegl1-mesa-dev libwayland-dev libxkbcommon-dev wayland-protocols

# Optional
# FFMPEG (For video decoding and image rescaling)
sudo apt install ffmpeg libavcodec-dev libavutil-dev libavformat-dev libswscale-dev libavdevice-dev
# DC1394 (For firewire input)
sudo apt install libdc1394-22-dev libraw1394-dev
# libuvc (For cross-platform webcam video input via libusb)
git://github.com/ktossell/libuvc.git
# libjpeg, libpng, libtiff, libopenexr (For reading still-image sequences)
sudo apt install libjpeg-dev libpng-dev libtiff5-dev libopenexr-dev

下载&编译:

git clone https://github.com/stevenlovegrove/Pangolin.git
cd Pangolin
mkdir build
cd build
cmake ..
make
sudo make install

2.2 Opencv

Opencv比较有意思,由于我下载的版本是ORBSLAMv3的版本,它是依赖的Opencv>=2.4.11,但是又要<4,但是此时我的电脑里是opencv4.1.5,因而我修改了源代码:ORBSLAMv3依赖opencv4可编译。
OpenCV安装方式
去官网下载OpenCV,如果从零开始的话建议安装OpenCV3的版本,这样会省去很多麻烦。
Ubuntu18.04安装灭霸SLAM:ORBSLAM3_第1张图片
下载后解压:

unzip opencv-3.4.15.zip

更新一下,准备开始安装OpenCV依赖库(不更新也行)

sudo apt-get update

一条指令完成所有的依赖库:

sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg.dev libtiff4.dev libswscale-dev libjasper-dev

如果报错

error: unable to locate libjasper-dev    无法定位这个包libjasper-dev

解决办法是:执行如下指令

sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt update
sudo apt install libjasper1 libjasper-dev

编译OpenCV

cd opencv-3.4.15
mkdir build
cd build
make -j8##jx代表的是x核编译
sudo make install

添加库路径

sudo /bin/bash -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'

更新系统库

sudo ldconfig

配置bash

sudo gedit /etc/bash.bashrc

在末尾粘贴如下两行代码

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

保存,执行如下命令使得配置生效

source /etc/bash.bashrc 

更新

sudo updatedb  

检查版本

pkg-config --modversion opencv

如果没问题的话,就说明安装结束了。

2.3 Eigen3

Eigen的安装方式比较简单,一条命令即可:

#github 有个mirror,版本3.3.4 from 2017
git clone https://github.com/eigenteam/eigen-git-mirror
#安装
cd eigen-git-mirror
mkdir build
cd build
cmake ..
sudo make install
#安装后,头文件安装在/usr/local/include/eigen3/

2.4 boost

boost库应该在安装ros的时候已经安装过了
,但是如果报错误的话,需要重装一次。
下载:boost,选择一个版本。

下载之后解压:

tar -xzvf boost_1_7x_0.tar.gz

解压之后进入解压出来的文件夹

然后执行这个脚本

sudo ./bootstrap.sh

执行完毕之后,你会发现又多了些文件

执行如下脚本

sudo ./b2 install

2.5 libssl-dev

一条命令即可

sudo apt-get install libssl-dev

3 ORB_SLAM3的编译和安装

3.1 源码编译

源码编译:
进入源码文件夹

cd ORB_SLAM3
chmod +x build.sh
./build.sh

注意查看脚本build.sh:

echo "Configuring and building Thirdparty/DBoW2 ..."
 
cd Thirdparty/DBoW2
mkdir build
cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
make -j
 
cd ../../g2o
 
echo "Configuring and building Thirdparty/g2o ..."
 
mkdir build
cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
make -j
 
cd ../../../
 
echo "Uncompress vocabulary ..."
 
cd Vocabulary
tar -xf ORBvoc.txt.tar.gz
 
 
echo "Configuring and building ORB_SLAM3 ..."
 
mkdir build
cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
make -j

-j后面没有数字,说明是满线程在跑,如果卡顿的话,注意改一下少一点线程。

3.2 ros下编译

3.2.1 将源码中的 Examples/ROS/ORB_SLAM3 路径添加到ROS_PACKAGE_PATH环境变量中

# 打开 .bashrc file:gedit ~/.bashrc# 添加路径 , 
gedit ~/.bashrc
# 其中PATH是本机的具体路径(如/home/xxx)
export ROS_PACKAGE_PATH=${ROS_PACKAGE_PATH}:“PATH”/ORB_SLAM3/Examples/ROS
# 然后保存一下,一定要再source一下
source ~/.bashrc

3.2.2 执行build_ros.sh文件

chmod +x build_ros.sh
./build_ros.s

4.编译过程中遇到的问题

4.1 opencv4版本与ORB-SLAM3的各种冲突

4.1.1 Thirdparty/DBoW2和主函数的CMakeLists.txt里面的opencv版本路径要修改,改成自己的。

运行build.sh报错
Ubuntu18.04安装灭霸SLAM:ORBSLAM3_第2张图片
修改CMakeList.txt文件中的opencv:

find_package(OpenCV 3.0)
if(NOT OpenCV_FOUND)
   find_package(OpenCV 2.4.3 QUIET)
   if(NOT OpenCV_FOUND)
      message(FATAL_ERROR "OpenCV > 2.4.3 not found.")
   endif()
endif()

改正后:

find_package(OpenCV 4.0)
if(NOT OpenCV_FOUND)
   find_package(OpenCV 2.4.3 QUIET)
   if(NOT OpenCV_FOUND)
      message(FATAL_ERROR "OpenCV > 2.4.3 not found.")
   endif()
endif()

make时报错:
Ubuntu18.04安装灭霸SLAM:ORBSLAM3_第3张图片
解决方法就是把 所有文件里面的

#include 

换成

#include 

4.2 错误 ‘CV_LOAD_IMAGE_UNCHANGED’ was not declared in this scope119 | imLeft = cv::imread(vstrImageLeft[ni],CV_LOAD_IMAGE_UNCHANGED);

解决办法:打开vscode,查找所有存在CV_LOAD_IMAGE_UNCHANGED的文件,添加头文件:

#include "opencv2/imgcodecs/legacy/constants_c.h"

4.3 报错:std::map must have the same value_type as its allocator

解决方法:
在LoopClosing.h中修改

typedef map<KeyFrame*,g2o::Sim3,std::less<KeyFrame*>,
        Eigen::aligned_allocator<std::pair<const KeyFrame*, g2o::Sim3> > > KeyFrameAndPose;

typedef map<KeyFrame*,g2o::Sim3,std::less<KeyFrame*>,
        Eigen::aligned_allocator<std::pair<KeyFrame* const, g2o::Sim3> > > KeyFrameAndPose;

4.4 错误:‘CvMat’ has not been declared/‘CV_GRAY2BGR’ was not declared in this scope error: ‘CV_RGB2GRAY’ was not declared in this scope

解决办法:
同样打开vscode,查找所有存在CV_RGB2GRAY的文件,添加以下头文件即可:

#include 

4.5 错误CMake Deprecation Warning at /opt/ros/melodic/share/ros/core/rosbuild/rosbuild.cmake:7 (cmake_policy):

Building ROS nodes
mkdir: cannot create directory ‘build’: File exists
CMake Deprecation Warning at /opt/ros/melodic/share/ros/core/rosbuild/rosbuild.cmake:7 (cmake_policy):
  The OLD behavior for policy CMP0011 will be removed from a future version
  of CMake.

  The cmake-policies(7) manual explains that the OLD behaviors of all
  policies are deprecated and that a policy should be set to OLD only under
  specific short-term circumstances.  Projects should be ported to the NEW
  behavior and not rely on setting a policy to OLD.
Call Stack (most recent call first):
  CMakeLists.txt:2 (include)


CMake Deprecation Warning at /opt/ros/melodic/share/ros/core/rosbuild/rosbuild.cmake:16 (cmake_policy):
  The OLD behavior for policy CMP0002 will be removed from a future version
  of CMake.

  The cmake-policies(7) manual explains that the OLD behaviors of all
  policies are deprecated and that a policy should be set to OLD only under
  specific short-term circumstances.  Projects should be ported to the NEW
  behavior and not rely on setting a policy to OLD.
Call Stack (most recent call first):
  CMakeLists.txt:2 (include)


CMake Deprecation Warning at /opt/ros/melodic/share/ros/core/rosbuild/rosbuild.cmake:18 (cmake_policy):
  The OLD behavior for policy CMP0003 will be removed from a future version
  of CMake.

  The cmake-policies(7) manual explains that the OLD behaviors of all
  policies are deprecated and that a policy should be set to OLD only under
  specific short-term circumstances.  Projects should be ported to the NEW
  behavior and not rely on setting a policy to OLD.
Call Stack (most recent call first):
  CMakeLists.txt:2 (include)


CMake Deprecation Warning at /opt/ros/melodic/share/ros/core/rosbuild/rosbuild.cmake:20 (cmake_policy):
  The OLD behavior for policy CMP0005 will be removed from a future version
  of CMake.

  The cmake-policies(7) manual explains that the OLD behaviors of all
  policies are deprecated and that a policy should be set to OLD only under
  specific short-term circumstances.  Projects should be ported to the NEW
  behavior and not rely on setting a policy to OLD.
Call Stack (most recent call first):
  CMakeLists.txt:2 (include)


CMake Deprecation Warning at /opt/ros/melodic/share/ros/core/rosbuild/rosbuild.cmake:23 (cmake_policy):
  The OLD behavior for policy CMP0011 will be removed from a future version
  of CMake.

  The cmake-policies(7) manual explains that the OLD behaviors of all
  policies are deprecated and that a policy should be set to OLD only under
  specific short-term circumstances.  Projects should be ported to the NEW
  behavior and not rely on setting a policy to OLD.
Call Stack (most recent call first):
  CMakeLists.txt:2 (include)


[rosbuild] Building package ORB_SLAM3
Failed to invoke /opt/ros/melodic/bin/rospack deps-manifests ORB_SLAM3
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/rosdep2/rospack.py", line 60, in init_rospack_interface
    lookup = _get_default_RosdepLookup(Options())
  File "/usr/lib/python2.7/dist-packages/rosdep2/main.py", line 136, in _get_default_RosdepLookup
    verbose=options.verbose)
  File "/usr/lib/python2.7/dist-packages/rosdep2/sources_list.py", line 609, in create_default
    sources = load_cached_sources_list(sources_cache_dir=sources_cache_dir, verbose=verbose)
  File "/usr/lib/python2.7/dist-packages/rosdep2/sources_list.py", line 566, in load_cached_sources_list
    raise CachePermissionError('Failed to write cache file: ' + str(e))
rosdep2.core.CachePermissionError: Failed to write cache file: [Errno 13] Permission denied: '/home/nvidia/.ros/rosdep/sources.cache/index'
[rospack] Error: could not call python function 'rosdep2.rospack.init_rospack_interface'


CMake Error at /opt/ros/melodic/share/ros/core/rosbuild/public.cmake:129 (message):
  

  Failed to invoke rospack to get compile flags for package 'ORB_SLAM3'.
  Look above for errors from rospack itself.  Aborting.  Please fix the
  broken dependency!

Call Stack (most recent call first):
  /opt/ros/melodic/share/ros/core/rosbuild/public.cmake:207 (rosbuild_invoke_rospack)
  CMakeLists.txt:4 (rosbuild_init)


-- Configuring incomplete, errors occurred!
See also "/home/nvidia/ORB_SLAM3/catkin_ws/src/ORB_SLAM3-v0.3-beta/Examples/ROS/ORB_SLAM3/build/CMakeFiles/CMakeOutput.log".
make: *** No targets specified and no makefile found.  Stop.

这个错误的原因时因为,没有写入环境变量里面,但是写入后同时也报这个错误,那么需要rosdep一下,推荐使用鱼香ros的rosdep方法。
最简单的ROS安装方法和rosdepc安装(一行代码搞定)
具体方法是:

curl http://fishros.com/tools/install/rosdepc | bash 
sudo rosdep fix-permissions
rosdep update

4.6.如果是卸载重装报错[rosbuild] Building package ORB_SLAM3

[rosbuild] Error from directory check: /opt/ros/melodic/share/ros/core/rosbuild/bin/check_same_directories.py /opt/ros/melodic/share/ORB_SLAM3 /home/nvidia/ORB_SLAM3-v0.3-beta/Examples/ROS/ORB_SLAM3
1
Traceback (most recent call last):
File “/opt/ros/melodic/share/ros/core/rosbuild/bin/check_same_directories.py”, line 48, in
raise Exception
Exception
CMake Error at /opt/ros/melodic/share/ros/core/rosbuild/private.cmake:99 (message):
[rosbuild] rospack found package “ORB_SLAM3” at
“/opt/ros/melodic/share/ORB_SLAM3”, but the current directory is
“/home/nvidia/ORB_SLAM3-v0.3-beta/Examples/ROS/ORB_SLAM3”. You should
double-check your ROS_PACKAGE_PATH to ensure that packages are found in the
correct precedence order.
Call Stack (most recent call first):
/opt/ros/melodic/share/ros/core/rosbuild/public.cmake:177 (_rosbuild_check_package_location)
CMakeLists.txt:4 (rosbuild_init)

– Configuring incomplete, errors occurred!
See also “/home/nvidia/ORB_SLAM3-v0.3-beta/Examples/ROS/ORB_SLAM3/build/CMakeFiles/CMakeOutput.log”.
make: *** No targets specified and no makefile found. Stop.

解决办法,找到所有关于ORB_SLAM3的安装文件并且删除:

cd /opt/ros/melodic/share/ORB_SLAM3
nvidia@MiWiFi-R1CM-srv:/opt/ros/melodic/share/ORB_SLAM3$ ls
Asus.yaml       lib           MonoAR         RGBD    Stereo_Inertial
build           manifest.xml  Mono_Inertial  src
CMakeLists.txt  Mono          ORB_SLAM3      Stereo

cd ..
sudo rm -rf ORB_SLAM3

5 写在最后

运行命令:

纯单目

  rosrun ORB_SLAM3 Mono PATH_TO_VOCABULARY PATH_TO_SETTINGS_FILE

单目+IMU

rosrun ORB_SLAM3 Mono PATH_TO_VOCABULARY PATH_TO_SETTINGS_FILE [EQUALIZATION]	

纯双目

rosrun ORB_SLAM3 Stereo PATH_TO_VOCABULARY PATH_TO_SETTINGS_FILE ONLINE_RECTIFICATION

其中ONLINE_RECTIFICATION为在线的意思

双目+IMU

rosrun ORB_SLAM3 Stereo_Inertial PATH_TO_VOCABULARY PATH_TO_SETTINGS_FILE ONLINE_RECTIFICATION [EQUALIZATION]	

纯RGB-D相机

rosrun ORB_SLAM3 RGBD PATH_TO_VOCABULARY PATH_TO_SETTINGS_FILE

ROS下的例子如

roscore
rosrun ORB_SLAM3 Stereo_Inertial Vocabulary/ORBvoc.txt Examples/Stereo-Inertial/EuRoC.yaml true
 rosbag play --pause V1_02_medium.bag /cam0/image_raw:=/camera/left/image_raw /cam1/image_raw:=/camera/right/image_raw /imu0:=/imu

参考链接:
1.ubuntu20.04装ORB-slam2 和ORB-slam2-ros
2.ORB-SLAM3 安装运行
3.初学尝试 Ubuntu 18.04 运行 ORB-SLAM3 demo
4.ORBSLAM2 安装与运行(Ubuntu 18.04下测试)

你可能感兴趣的:(SLAM,视觉SLAM,git,github,slam,ubuntu,c++)