本文仅作为个人学习笔记,记录学习过程。
sudo apt-get install g++
sudo apt-get install cmake
测试使用了VScode
mkdir include
mkdir scr
mkdir build
touch CMakeLists.txt
#include
using namespace std;
int main(int argc,char argv)
{
cout << "Hello SLAM!" << endl;
return 0;
}
cmake_minimum_required (VERSION 2.8)
# project()指令语法:
# PROJECT(projectname [CXX] [C] [Java])
# 1.projectname:工程名
# 可以指定工程支持的语言:可省略,默认支持所有语言
project (SLAM)
message(STATUS "This is BINARY dir" ${PROJECT_BINARY_DIR})
message(STATUS "This is SOURCE dir" ${PROJECT_SOURCE_DIR})
# cmake也预定义了两个系统变量:
# PROJECT_BINARY_DIR:cmake执行的路径
# PROJECT_SOURCE_DIR:CMakeLists.txt所在的路径
# ***建议使用这个!!!!这样修改工程名不会影响其他代码
# MESSAGE([SEND_ERROR | STATUS | FATAL_ERROR] "message to display" ...)
# message()指令语法:作用用于向终端输出用户定义的信息;变量SEND_ERROR:产生错误,生成过程被跳过;变量STATUS:输出前缀为-信息;变量FATAL_ERROR:立即终止所有cmake过程
# 执行cmake . 输出:
# -- This is BINARY dir/home/ubuntu/Test/build
# -- This is SOURCE dir/home/ubuntu/Test
# add_executable()语法指令:
# add_executable(hello ${SRC_LIST})
# 会生成一个文件名为hello的可执行文件,相关的源文件是SRC_LIST中定义的源文件列表
ADD_executable(slam scr/main.cpp)
cd build
cmake ..
编译生成可执行文件slam,实际上调用了g++来编译程序
make
./slam
Hello SLAM!
初学者如果不知道该./
什么,其实很简单,如下make
成功后会出现Built target XXX
XXX
是什么就./
什么就可以了
(base) ubuntu@ubuntu:~/Test/build$ make
[100%] Built target slam
——————以上就是基于VScode的Cmake编译g++程序简单介绍——————
把ros软件源添加到系统软件源中,因为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 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654
【注意!!!】
选择ros安装版本,这里18.04对应的是melodic-desktop版本的ros
ros对应不同的ubuntu版本有不同的版本名字:
ubuntu16.04对应ros-kinetic;
ubuntu18.04对应ros-melodic;
ubuntu20.04对应ros-noetic。
这里的full只是把常用的包安装好了,其实并不是真正的完全版
sudo apt-get update
sudo apt-get install ros-melodic-desktop-full
sudo rosdep init
rosdep update
这个环节可能会出现问题
bupo@bupo-vpc:~$ sudo rosdep init
[sudo] bupo 的密码:
sudo: rosdep:找不到命令
【解决方法】
sudo apt install python-rosdep
bupo@bupo-vpc:~$ 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 apt-get install python3-pip
sudo pip3 install rosdepc
sudo rosdepc init
rosdepc update
参考链接
如果每次启动新 shell 时 ROS 环境变量都自动添加到 bash 会话中,这将非常方便
echo "source /opt/ros/melodic/setup.bash" >> ~/.bashrc
source ~/.bashrc
sudo apt install python-rosdep python-rosinstall python-rosinstall-generator python-wstool build-essential
roscore
如果在roscore
命令后,出现了这样的错误:
Command 'roscore' not found, but can be installed with:
sudo apt install python-roslaunch
参考链接
解决方法是sudo apt-get -f install ros-melodic-desktop-full
执行完毕之后,再运行roscore,就可以了
执行以下命令:
roscore
rosrun turtlesim turtlesim_node
rosrun turtlesim turtle_teleop_key
rqt_graph
可以通过上下左右键控制小海龟的移动,如果看见下面这张图片那就成功了
wget -O opencv-3.4.1.zip https://github.com/Itseez/opencv/archive/3.4.1.zip官网
opencv官网
sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg.dev libtiff5.dev libswscale-dev libjasper-dev
cd
opencv-4.3.1文件夹的目录下mkdir
build文件夹,cd
build,执行cmake
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=Release –D CMAKE_INSTALL_PREFIX=/usr/local ..
参考链接
这里的cmake可能会报错,方法是将–D CMAKE_INSTALL_PREFIX=/usr/local ..
替换为-DWITH_CUDA:BOOL="0"
执行make
sudo make
sudo make install
配置OpenCV的编译环境,目的是让系统找到OpenCV的库的路径
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
source /etc/bash.bashrc
OpenCV配置完成!!
先去安装Eigen3.2.1回来再安装Pangolin!
Pangolin建议安装0.5版,如果装了0.6后面会出问题
sudo apt install libgl1-mesa-dev
sudo apt install libglew-dev
sudo apt install cmake
sudo apt install libpython2.7-dev
sudo apt install python-pip
sudo python -mpip install numpy pyopengl Pillow pybind11
sudo apt install pkg-config
sudo apt install libegl1-mesa-dev libwayland-dev libxkbcommon-dev wayland-protocols
sudo apt install ffmpeg libavcodec-dev libavutil-dev libavformat-dev libswscale-dev libavdevice-dev
sudo apt install libdc1394-22-dev libraw1394-dev
sudo apt install libjpeg-dev libpng-dev libtiff5-dev libopenexr-dev
cd Pangolin
mkdir build
cd build
cmake ..
cmake --build .
如果遇到 :error: ISO C++1z does not allow dynamic exception specifications 类似报错
使用:cmake -DBUILD_PANGOLIN_LIBOPENEXR=OFF .. #目的是禁用OpenEXR
sudo make install
在执行cmake --build .
时可能会遇到问题
In file included from /home/micoarm/Pangolin/src/video/video_output.cpp:33:0:
/home/micoarm/Pangolin/include/pangolin/video/drivers/ffmpeg.h:94:5: error: ‘PixelFormat’ does not name a type
PixelFormat fmtout;
^
/home/micoarm/Pangolin/include/pangolin/video/drivers/ffmpeg.h:142:5: error: ‘PixelFormat’ does not name a type
PixelFormat fmtsrc;
^
/home/micoarm/Pangolin/include/pangolin/video/drivers/ffmpeg.h:143:5: error: ‘PixelFormat’ does not name a type
PixelFormat fmtdst;
^
make[2]: *** [src/CMakeFiles/pangolin.dir/video/video_output.cpp.o] Error 1
make[2]: Leaving directory /home/micoarm/Pangolin/build' make[1]: *** [src/CMakeFiles/pangolin.dir/all] Error 2 make[1]: Leaving directory/home/micoarm/Pangolin/build'
make: *** [all] Error 2
具体解决办法(之一,亲测可解决)如下:
参考
到Pagolin/src文件夹下,在CMakeList.txt中删去下面这段代码(屏蔽掉ffmpeg的影响):
find_package(FFMPEG QUIET)
if(BUILD_PANGOLIN_VIDEO AND FFMPEG_FOUND)
set(HAVE_FFMPEG 1)
list(APPEND INTERNAL_INC ${FFMPEG_INCLUDE_DIRS} )
list(APPEND LINK_LIBS ${FFMPEG_LIBRARIES} )
list(APPEND HEADERS ${INCDIR}/video/drivers/ffmpeg.h)
list(APPEND SOURCES video/drivers/ffmpeg.cpp)
list(APPEND VIDEO_FACTORY_REG RegisterFfmpegVideoFactory )
list(APPEND VIDEO_FACTORY_REG RegisterFfmpegVideoOutputFactory )
if(_GCC_)
# FFMPEG is a real pain for deprecating the API.
set_source_files_properties(video/drivers/ffmpeg.cpp PROPERTIES COMPILE_FLAGS "-Wno-deprecated-declarations")
endif()
message(STATUS "ffmpeg Found and Enabled")
endif()
然后回到Pagolin/build文件夹下,运行cmake …和make即可通过编译。
编译完,可以运行以下命令测试,如果显示出一个彩色立方体,并且可以通过鼠标左、右键和滚轮按住拖拽,就表示 Pangolin 安装成功了。
cd ~/Pangolin/build/examples/HelloPangolin
./HelloPangolin
参考
cd Pangolin/build
make clean
sudo make uninstall
执行之后就会开始卸载所有的.so文件和.h文件。
cd ../..
sudo rm -r Pangolin
执行之后删除源代码文件夹。
sudo updatedb
locate pangolin
更新下索引,搜索pangolin的位置
可以看到在/usr/local/include/pangolin目录还未删除,因为第一步是卸载了该目录下的所有.h文件。
/usr/local/include/pangolin
/usr/local/include/pangolin/compat
/usr/local/include/pangolin/console
/usr/local/include/pangolin/display
/usr/local/include/pangolin/factory
/usr/local/include/pangolin/geometry
/usr/local/include/pangolin/gl
/usr/local/include/pangolin/handler
/usr/local/include/pangolin/image
/usr/local/include/pangolin/log
所以,我们将该目录删除即可。
sudo rm -r /usr/local/include/pangolin
locate pangolin
这里强烈建议安装Eigen3.2.1 版本,不然安装ORB_SLAM2时会报错,具体错误在介绍ORB_SLAM2安装的时候会说
这里分别给出3.2.1版和3.3.4版的安装方法
参考
pkg-config --modversion eigen3
gedit /usr/include/eigen3/Eigen/src/Core/util/Macros.h
#define EIGEN_WORLD_VERSION 3
#define EIGEN_MAJOR_VERSION 3
#define EIGEN_MINOR_VERSION 4
目前版本是3.3.4
查看eigen3位置
sudo updatedb
locate eigen3
删除eigen3相关文件
sudo rm -rf /usr/include/eigen3
sudo rm -rf /usr/lib/cmake/eigen3
sudo rm -rf /usr/local/include/eigen3
sudo rm -rf /usr/share/doc/libeigen3-dev
sudo rm -rf /usr/local/share/pkgconfig/eigen3.pc /usr/share/pkgconfig/eigen3.pc /var/lib/dpkg/info/libeigen3-dev.list /var/lib/dpkg/info/libeigen3-dev.md5sums
sudo rm -rf /usr/local/lib/pkgconfig/eigen3.pc
sudo rm -rf /usr/local/share/eigen3
刷新查看是否删除彻底
sudo updatedb
locate eigen3
pkg-config --modversion eigen3
进入Eigen官网。如下图,点击other downloads
找到Eigen3.2.1
点击 Source code (zip) 下载得到 eigen-3.2.1.zip 文件,解压zip后执行下列命令:
cd eigen-3.2.1
mkdir build
cd build
cmake..
sudo make install
sudo cp -r /usr/local/include/eigen3 /usr/include
pkg-config --modversion eigen3
Eigen官网
sudo apt-get install libeigen3-dev
gedit /usr/include/eigen3/Eigen/src/Core/util/Macros.h
#define EIGEN_WORLD_VERSION 3
#define EIGEN_MAJOR_VERSION 3
#define EIGEN_MINOR_VERSION 4
目前版本是3.3.4
mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/src
使用 git clone 得到ORB_SLAM2
git clone https://github.com/raulmur/ORB_SLAM2.git ORB_SLAM2
进入ORB_SLAM2目录,给build.sh文件可执行权限,编译ORB-SLAM2,运行如下命令
cd ORB_SLAM2
chmod +x build.sh
./build.sh
sudo gedit ~/.bashrc
将包含Examples/ROS/ORB_SLAM2的路径添加到ROS_PACKAGE_PATH环境变量中。打开.bashrc文件并在最后添加的内容如图所示
export ROS_PACKAGE_PATH=${ROS_PACKAGE_PATH}:~/catkin_ws/src/ORB_SLAM2/Examples/ROS
安装 ORB_SLAM2时可能会出现一些错误
错误关键词:usleep
这个错误是因为.cc文件缺少#include
引起的
/home/ubuntu/catkin_ws/src/ORB_SLAM2/src/Viewer.cc:160:17: note: suggested alternative: ‘fseek’
usleep(3000);
^~~~~~
fseek
/home/ubuntu/catkin_ws/src/ORB_SLAM2/src/System.cc: In member function ‘cv::Mat ORB_SLAM2::System::TrackStereo(const cv::Mat&, const cv::Mat&, const double&)’:
/home/ubuntu/catkin_ws/src/ORB_SLAM2/src/System.cc:134:17: error: ‘usleep’ was not declared in this scope
usleep(1000);
^~~~~~
/home/ubuntu/catkin_ws/src/ORB_SLAM2/src/System.cc:134:17: note: suggested alternative: ‘fseek’
usleep(1000);
^~~~~~
fseek
/home/ubuntu/catkin_ws/src/ORB_SLAM2/src/System.cc: In member function ‘cv::Mat ORB_SLAM2::System::TrackRGBD(const cv::Mat&, const cv::Mat&, const double&)’:
/home/ubuntu/catkin_ws/src/ORB_SLAM2/src/System.cc:185:17: error: ‘usleep’ was not declared in this scope
usleep(1000);
^~~~~~
/home/ubuntu/catkin_ws/src/ORB_SLAM2/src/System.cc:185:17: note: suggested alternative: ‘fseek’
usleep(1000);
^~~~~~
fseek
/home/ubuntu/catkin_ws/src/ORB_SLAM2/src/Tracking.cc:1512:13: note: suggested alternative: ‘fseek’
usleep(3000);
^~~~~~
fseek
/home/ubuntu/catkin_ws/src/ORB_SLAM2/src/LocalMapping.cc:108:9: error: ‘usleep’ was not declared in this scope
usleep(3000);
^~~~~~
/home/ubuntu/catkin_ws/src/ORB_SLAM2/src/LoopClosing.cc: In member function ‘void ORB_SLAM2::LoopClosing::Run()’:
我的报错主要有Viewer.cc
、System.cc
、Tracking.cc
、LocalMapping.cc
、LoopClosing.cc
几个文件
参考1
参考2
需要增加#include
的文件可能还会有:
Examples/Monocular/mono_euroc.cc
Examples/Monocular/mono_kitti.cc
Examples/Monocular/mono_tum.cc
Examples/RGB-D/rgbd_tum.cc
Examples/Stereo/stereo_euroc.cc
Examples/Stereo/stereo_kitti.cc
src/LocalMapping.cc
src/LoopClosing.cc
src/System.cc
src/Tracking.cc
src/Viewer.cc
根据实际情况,提示哪个文件usleep有问题,就去加这个头文件
出现这个问题是因为安装的Eigen版本太高,需要降低到3.2.1版本。
In file included from /home/hri/rosbuild_ws/package_dir/ORB_SLAM2_Fusion/Thirdparty/g2o/g2o/types/types_six_dof_expmap.h:37:0,
from /home/hri/rosbuild_ws/package_dir/ORB_SLAM2_Fusion/include/Converter.h:29,
from /home/hri/rosbuild_ws/package_dir/ORB_SLAM2_Fusion/src/Converter.cc:22:
/home/hri/rosbuild_ws/package_dir/ORB_SLAM2_Fusion/Thirdparty/g2o/g2o/types/../core/base_vertex.h:62:74: warning: ‘Eigen::AlignedBit’ is deprecated [-Wdeprecated-declarations]
typedef Eigen::Map<Matrix<double, D, D>, Matrix<double,D,D>::Flags & AlignedBit ? Aligned : Unaligned > HessianBlockType;
^~~~~~~~~~
In file included from /usr/local/include/eigen3/Eigen/Core:363:0,
from /usr/local/include/eigen3/Eigen/Dense:1,
from /home/hri/rosbuild_ws/package_dir/ORB_SLAM2_Fusion/include/Converter.h:26,
from /home/hri/rosbuild_ws/package_dir/ORB_SLAM2_Fusion/src/Converter.cc:22:
/usr/local/include/eigen3/Eigen/src/Core/util/Constants.h:162:37: note: declared here
EIGEN_DEPRECATED const unsigned int AlignedBit = 0x80;
^~~~~~~~~~
In file included from /home/hri/rosbuild_ws/package_dir/ORB_SLAM2_Fusion/Thirdparty/g2o/g2o/types/types_six_dof_expmap.h:38:0,
from /home/hri/rosbuild_ws/package_dir/ORB_SLAM2_Fusion/include/Converter.h:29,
from /home/hri/rosbuild_ws/package_dir/ORB_SLAM2_Fusion/src/Converter.cc:22:
/home/hri/rosbuild_ws/package_dir/ORB_SLAM2_Fusion/Thirdparty/g2o/g2o/types/../core/base_binary_edge.h:59:82: warning: ‘Eigen::AlignedBit’ is deprecated [-Wdeprecated-declarations]
typedef Eigen::Map<Matrix<double, Di, Dj>, Matrix<double, Di, Dj>::Flags & AlignedBit ? Aligned : Unaligned > HessianBlockType;
卸载原版本Eigen,重装后,重新编译Pangolin, DBoW2, g2o, ORB_SLAM2
有TUM、KITTI、EuRoC三种数据集,本实验使用TUM数据集,从下载序列并解压缩。
数据及存放位置
(base) ubuntu@ubuntu:~/catkin_ws/src$ pwd
/home/ubuntu/catkin_ws/src
(base) ubuntu@ubuntu:~/catkin_ws/src$ ls
ORB_SLAM2 rgbd_dataset_freiburg1_xyz
(base) ubuntu@ubuntu:~/catkin_ws/src$
PATH_TO_SEQUENCE_FOLDER为数据集的存储路径,并将tumx.yaml与下载的数据集对应,比如TUM1.yaml,TUM2.yaml 和TUM3.yaml 分别对应 freiburg1, freiburg2 和 freiburg3
数据集目录/home/ubuntu/catkin_ws/src
./Examples/Monocular/mono_tum Vocabulary/ORBvoc.txt Examples/Monocular/TUM1.yaml /home/ubuntu/catkin_ws/src/rgbd_dataset_freiburg1_xyz