ORB_SLAM2--源码编译

前言

学习ORB_SLAM2,从编译源码开始

ORB_SLAM2的github地址: https://github.com/raulmur/ORB_SLAM2

一、准备工作:

 1.安装依赖库:

sudo apt install cmake
sudo apt install libgl1-mesa-dev
sudo apt install libeigen3-dev
sudo apt install libglew-dev freeglut3-dev

2.安装Pangolin:

git clone https://github.com/stevenlovegrove/Pangolin.git
cd Pangolin/
mkdir build
cmake ..
make -j4
sudo make install
# 默认安装在/usr/local/bin/Plotter

3.编译ORB_SLAM2:

git clone https://github.com/raulmur/ORB_SLAM2.git
cd ORB_SLAM2
chmod +x build.sh
./build.sh
# 如果编译过程中死机,将build.sh中的make -j改为make -j4

如果编译的过程中, 发现有usleep()函数的报错, 可以把报错的文件中添加头文件: #include

# 一个小技巧, 搜索所有包含"usleep"字符串的文件
grep -r -e "usleep" ./Examples/
grep -r -e "usleep" ./src/

# 然后手动为这些文件添加: #include 

我编译过程中遇到的其他问题:

error: static assertion failed: std::map must have the same value_type as its allocator

解决方法:https://blog.csdn.net/lixujie666/article/details/90023059

打开LoopClosing.h,将

typedef map,
        Eigen::aligned_allocator > > KeyFrameAndPose;

改为

typedef map,
        Eigen::aligned_allocator > > KeyFrameAndPose;

二、运行例程:

下载测试数据: https://vision.in.tum.de/rgbd/dataset/freiburg1/rgbd_dataset_freiburg1_desk.tgz

然后执行:

cd ORB_SLAM2/
mkdir sequence
# 然后把文件解压到sequence文件夹下

# 运行例程
./Examples/Monocular/mono_tum Vocabulary/ORBvoc.txt Examples/Monocular/TUM1.yaml sequence/rgbd_dataset_freiburg1_desk

可以看到演示:

ORB_SLAM2--源码编译_第1张图片

三、有关ros的安装:

1.先安装ros

sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys F42ED6FBAB17C654
sudo apt update

# Ubuntu16的安装指令:
sudo apt-get install ros-melodic-desktop
echo "source /opt/ros/melodic/setup.bash" >> ~/.bashrc
source ~/.bashrc
sudo ln -s /home/jeff/study/ORB_SLAM2/Examples/ROS/ORB_SLAM2 /opt/ros/melodic/share/ORB_SLAM2 # 第一个路径换成自己的

# Ubuntu20的安装指令:
sudo apt install ros-noetic-desktop
echo "source /opt/ros/noetic/setup.bash" >> ~/.bashrc
source ~/.bashrc
sudo ln -s /home/jeff/study/ORB_SLAM2/Examples/ROS/ORB_SLAM2 /opt/ros/noetic/share/ORB_SLAM2

sudo apt install python3-rosdep python3-rosinstall python3-rosinstall-generator python3-wstool
sudo rosdep init # 网络问题可能需要更改网络多次尝试
rosdep update

2.运行build_ros.sh

./build_ros.sh
source Examples/ROS/ORB_SLAM2/build/devel/setup.bash

如果报错:

[100%] Linking CXX executable ../MonoAR
/usr/bin/ld: warning: libopencv_imgproc.so.4.2, needed by /opt/ros/noetic/lib/libcv_bridge.so, may conflict with libopencv_imgproc.so.3.4
/usr/bin/ld: CMakeFiles/MonoAR.dir/src/AR/ViewerAR.cc.o: undefined reference to symbol '_ZN2cv7putTextERKNS_17_InputOutputArrayERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEENS_6Point_IiEEidNS_7Scalar_IdEEiib'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libopencv_imgproc.so.4.2.0: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/MonoAR.dir/build.make:248:../MonoAR] 错误 1
make[1]: *** [CMakeFiles/Makefile2:487:CMakeFiles/MonoAR.dir/all] 错误 2
make: *** [Makefile:130:all] 错误 2

需要安装opencv4.2,之后再重新运行./build.sh

下载解压opencv4.2:https://github.com/opencv/opencv/archive/4.2.0.zip

cd opencv-4.2.0
mkdir build
cd build
cmake ..
make -j8
sudo make install

3.编译调用笔记本自带摄像头的代码:

参考:https://www.cnblogs.com/feifanrensheng/p/8995836.html

mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/src
catkin_init_workspace
cd ~/catkin_ws/
catkin_make
source devel/setup.bash
cd ~/catkin_ws/src
git clone https://github.com/bosch-ros-pkg/usb_cam.git
cd ..
catkin_make
source ~/catkin_ws/devel/setup.bash
roscd usb_cam # 没报错算成功

4.ros调用AR:

用笔记本自带的摄像头,需要把ros_mono_ar.cc中的:ros::Subscriber sub = nodeHandler.subscribe("/camera/image_raw", 1, &ImageGrabber::GrabImage,&igb);

改为:ros::Subscriber sub = nodeHandler.subscribe("/usb_cam/image_raw", 1, &ImageGrabber::GrabImage,&igb);,再重新编译。

开启一个终端,运行:

roscore

再开启一个终端,运行:

source ~/catkin_ws/devel/setup.bash
cd ~/catkin_ws/src/usb_cam/launch/
roslaunch usb_cam usb_cam-test.launch # 可以看到笔记本摄像头的画面

在ORB_SLAM2目录下开启第三个终端,运行:

rosrun ORB_SLAM2 Mono Vocabulary/ORBvoc.txt Examples/Monocular/TUM1.yaml 

 

你可能感兴趣的:(SLAM)