Ubuntu 20.04 和 ROS 的安装

Linux 版本:Ubuntu 20.04 LTS

ROS 版本:noetic

Ubuntu 下载地址(重庆大学源):https://mirrors.cqu.edu.cn/ubuntu-releases/20.04.6/ubuntu-20.04.6-desktop-amd64.iso

Ubuntu 安装过程略。。。

更换阿里云源

编辑 /etc/apt/sources.list

deb <http://mirrors.aliyun.com/ubuntu/> focal main restricted universe multiverse
deb-src <http://mirrors.aliyun.com/ubuntu/> focal main restricted universe multiverse

deb <http://mirrors.aliyun.com/ubuntu/> focal-security main restricted universe multiverse
deb-src <http://mirrors.aliyun.com/ubuntu/> focal-security main restricted universe multiverse

deb <http://mirrors.aliyun.com/ubuntu/> focal-updates main restricted universe multiverse
deb-src <http://mirrors.aliyun.com/ubuntu/> focal-updates main restricted universe multiverse

# deb <http://mirrors.aliyun.com/ubuntu/> focal-proposed main restricted universe multiverse
# deb-src <http://mirrors.aliyun.com/ubuntu/> focal-proposed main restricted universe multiverse

deb <http://mirrors.aliyun.com/ubuntu/> focal-backports main restricted universe multiverse
deb-src <http://mirrors.aliyun.com/ubuntu/> focal-backports main restricted universe multiverse

ROS noetic 安装

安装参考:*【ros学习笔记】在ubuntu 20.04中安装ros的详细步骤*

# 添加清华镜像源(ros官方太卡了)
sudo sh -c '. /etc/lsb-release && echo "deb http://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu/ `lsb_release -cs` main" > /etc/apt/sources.list.d/ros-latest.list'
# 添加密钥
sudo apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654
# 安装ros
sudo apt update
sudo apt install ros-noetic-desktop-full
# 配置环境变量
echo "source /opt/ros/noetic/setup.bash" >> ~/.bashrc
source ~/.bashrc
# 安装rosintall
sudo apt install python3-rosinstall python3-rosinstall-generator python3-wstool build-essential
# 测试
roscore
rosrun turtlesim turtlesim_node
rosrun turtlesim turtle_teleop_key

ORB-SLAM3 的编译安装

克隆仓库

ORB-SLAM3 中文代码注释仓库:https://github.com/electech6/ORB_SLAM3_detailed_comments

git clone https://github.com/electech6/ORB_SLAM3_detailed_comments.git

安装 EIGEN 3.3.4

# install tools
sudo apt update
sudo apt install git cmake gcc g++

# install EIGEN3.3.4
sudo apt-get install libeigen3-dev

安装 Pangolin 0.6

# install pangolin0.6
## 安装依赖
sudo apt install libglew-dev libpython2.7-dev
## 获得pangolin的稳定版本安装包
https://github.com/stevenlovegrove/Pangolin/releases/tag/v0.6
## 编译安装
mkdir build
cd build
cmake ..
make
sudo make install       # 安装后pangolin将在/usr/local/include/中找到
## 测试
cd build/examples/HelloPangolin
./HelloPangolin    # 运行此示例,效果显示是一个红绿蓝立方体,表示即为安装成功

安装 OpenCV 4.4.0

## download opencv4.4.0 and opencv_contrib4.4.0
https://github.com/opencv/opencv/releases/tag/4.4.0
https://github.com/opencv/opencv_contrib/releases/tag/4.4.0
## 安装依赖项
sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
  # meybe "E: 无法定位软件包 libjasper-dev"
  sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
  sudo apt update
  sudo apt install libjasper1 libjasper-dev
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev 
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev liblapacke-dev
sudo apt-get install libxvidcore-dev libx264-dev
sudo apt-get install libatlas-base-dev gfortran 
sudo apt-get install ffmpeg
## install cmake-gui
sudo apt-get install cmake-gui
cmake-gui    # open cmake-gui

打开 cmake-gui 后,进行一些配置:

  • 将opencv解压,然后将opencv_contrib解压到opencv文件夹内,并在该文件夹内创建一个build文件夹
  • 在 cmake-gui 中操作:

    • 源码目录选择opencv的解压目录
    • 构建的目标目录选择创建的build文件夹目录
    • 确定路径没问题之后点击configure选择UnixMakefiles,其余默认,点击finish
    • 这里需要对三个地方进行修改:

      • (1)在CMAKE_BUILD_TYPE值处输入RELEASE,其他保持不变(如果已经存在就不必修改)。
      • (2)在OPENCV_EXTRA_MODULES_PATH处,选择输入目录(单击这一行后方空白处即可选中),接着选择opencv_contrib文件夹中的modules文件夹。
      • (3)在BUILD_opencv_world后面打√。
    • 上面的内容设置好以后就可以Configure了。【把代理弄好!】
    • Configure完成之后,进行Generate

完成之后,在build文件夹下重新打开一个终端

make
sudo make install
# 配置环境变量
sudo gedit /etc/ld.so.conf.d/opencv.conf
/usr/local/lib  # 这行添加到最后
sudo ldconfig
sudo gedit /etc/bash.bashrc
# 在文件最后添加下面两行:
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig 
export PKG_CONFIG_PATH

编译 ORB-SLAM3 非 ROS 版本

  • 编译 Thirdparty/DBoW2

修改 CMakeLists.txt(支持OpenCV4):

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

LIST(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake_modules)
find_package(OpenCV 4 REQUIRED)
   if(NOT OpenCV_FOUND)
      message(FATAL_ERROR "OpenCV > 4.4 not found.")
   endif()

然后编译:

cd Thirdparty/DBoW2
mkdir build
cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
make -j4
  • 编译 Thirdparty/Sophus
cd Thirdparty/Sophus
mkdir build
cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
make -j4
  • 解压 /ORB_SLAM3/Vocabulary/ORBvoc.txt.tar.gz
# 在/ORB_SLAM3下开终端
cd Vocabulary
tar -xf ORBvoc.txt.tar.gz
cd ..
  • 编译ORB_SLAM3
# 参考 Thirdparty/DBoW2 修改 CMakeLists.txt,支持OpenCV4
mkdir build
cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
make -j4

ORB_SLAM3 ROS 版本编译

# 添加ROS_PACKAGE_PATH
gedit ~/.bashrc
# 添加下面的内容到.bashrc(PATH改为ROB_SLAM3目录)
export ROS_PACKAGE_PATH=${ROS_PACKAGE_PATH}:PATH/ORB_SLAM3/Examples/ROS
# 开始编译
chmod +x build_ros.sh
./build_ros.sh
  • 报错1 找不到 rosdep
# [rospack] Error: the rosdep view is empty: call 'sudo rosdep init' and 'rosdep update'
# 安装 python3-rosdep
sudo apt install python3-rosdep
# 然后
sudo rosdep init
# sudo rosdep init 报错
ERROR: cannot download default sources list from:
https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/sources.list.d/20-default.list 
Website may be down.
# 自己打开上面的网址,将内容复制到:
sudo gedit /etc/ros/rosdep/sources.list.d/20-default.list
# 然后
rosdep update
  • 报错2 OpenCV > 3.2 not found.
# 修改CMakeLists.txt
gedit ~/ORB_SLAM3/Examples/ROS/ORB_SLAM3/CMakeLists.txt
# 修改:
# find_package(OpenCV 3.2)
#    if(NOT OpenCV_FOUND)
#       message(FATAL_ERROR "OpenCV > 3.2 not found.")
#    endif()
find_package(OpenCV 4 REQUIRED)
   if(NOT OpenCV_FOUND)
      message(FATAL_ERROR "OpenCV > 4.4 not found.")
   endif()
  • Euroc 双目运行报错
# error "Calibration parameters to rectify stereo are missing!"
# 将命令中的EuRoC.yaml改为Examples_old中的
rosrun ORB_SLAM3 Stereo Vocabulary/ORBvoc.txt Examples_old/Stereo-Inertial/EuRoC.yaml true

在 ORB3 已安装的基础上,直接编译 ORB2

  • 报错fatal error: opencv/cv.h: 没有那个文件或目录

打开 ORB_SLAM2/include/ORBextractor.h

把:

#include <opencv/cv.h>

替换为:

#include <opencv2/imgproc/imgproc_c.h>
#include <opencv2/highgui/highgui_c.h>
  • 报错 error: static assertion failed: std::map must have the same value_type as its allocator static_assert(is_same<typename _Alloc::value_type, value_type>::value,

打开 ORB_SLAM2/include/LoopClosing.h 把:

typedef map<KeyFrame*,                  //键
                g2o::Sim3,                  //值
                std::less<KeyFrame*>,       //排序算法
                Eigen::aligned_allocator<std::pair<const KeyFrame*, g2o::Sim3> >

改为

typedef map<KeyFrame*,                  //键
                g2o::Sim3,                  //值
                std::less<KeyFrame*>,       //排序算法
                Eigen::aligned_allocator<std::pair<KeyFrame* const, g2o::Sim3> >
  • **报错 error: ‘CV_LOAD_IMAGE_UNCHANGED’ was not declared in this scope
    160 | imLeft = cv::imread(vstrImageLeft[ni],CV_LOAD_IMAGE_UNCHANGED);**

打开 ORB_SLAM2/Examples/Monocular/mono_euroc.cc

打开 ORB_SLAM2/Examples/Monocular/mono_tum.cc

打开 ORB_SLAM2/Examples/Monocular/mono_kitti.cc

打开 ORB_SLAM2/Examples/Stereo/stereo_euroc.cc

打开 ORB_SLAM2/Examples/Stereo/stereo_kitti.cc

打开 ORB_SLAM2/Examples/RGB-D/rgbd_tum.cc

#include<opencv2/core/core.hpp>

改为

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