转载请注明原创地址:https://blog.csdn.net/qq_30356613/article/details/71075244
搞完毕设之后与本科毕业还有两个月的时间,这段时间除了每天到处无聊闲逛之外,还是决定做点什么,于是就拿来网上一个现成的开源SLAM代码,看看别人是怎么将SLAM写成一个完备的工程库的所以就写了一起学ORBSLAM2这个系列,本系列结合现有的关于ORBSLAM的相关资料,包括国内的论文博客等,目的在于让自己加深学习的印象,并且刚接触SLAM这个领域,因此可能有许多不足之处还请各位见谅,并欢迎批评指正。
五一放假闲的无聊,就跑了跑ORBSLAM的相关程序,谁知道遇到的问题还不少,搞了好长时间,下边是一份具体的流程供大家参考
关于ORBSLAM2的运行网上的相关博客的确不少,但是完整的详尽的运行方法很少介绍,这里我就ORBSLAM在线RGBD的运行方法介绍一下
这段时间也是一直在看在线RGBDSLAM的一些东西,想把自己原先写的一个离线SLAM做成在线的,因此就去看网上一些开源SLAM代码,目前ORBSLAM做的算是做的比较完善的一个SLAM,其中包括了单目,双目,RGBD的离线和在线SLAM实现方法,实现效果的话算还可以吧,下面系统介绍其运行步骤
1.首先,下载开源ORBSLAM代码:http://webdiis.unizar.es/~raulmur/orbslam/
2.下载opencv2.4.9并编译安装
方法:官网下载源代码: http://opencv.org/releases.html
2.1将下载包移动到你想编译的地方 我是在/home/lmt下建立了User目录专门放各种库的源代码进行编译
2.2用终端进入opencv2.4.9目录下,在终端里运行:
mkdir build
cd build
cmake cmake -D WITH_GTK_2_X=ON -D CMAKE_INSTALL_PREFIX=/usr/local .. (CMAKE_INSTALL_PREFIX这个参数是指定你opencv的安装路径,你可以自己选择路径,默认是安装在/usr/local)
make -j2
sudo make install
3.在Ubuntu中安装ROS Indigo(参考:http://wiki.ros.org/cn/indigo/Installation/Ubuntu)
3.1 配置 Ubuntu 软件仓库,查看ubuntu软件中心,选择编辑-软件源 可以看到从互联网下载中main,universe,restricted,multiverse四个都可以就完事
3.2 添加 sources.list
sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'
3.3添加keys
sudo apt-key adv --keyserver hkp://pool.sks-keyservers.net --recv-key 421C365BD9FF1F717815A3895523BAEEB01FA116
3.4 安装 ROS indigo
sudo apt-get update
sudo apt-get install ros-indigo-desktop-full
3.5 初始化 rosdep
sudo rosdep init
rosdep update
3.6 环境设置
echo "source /opt/ros/indigo/setup.bash" >> ~/.bashrc
source ~/.bashrc
3.7 安装 rosinstall
sudo apt-get install python-rosinstall
3.8 配置自己的工作环境(参考:http://wiki.ros.org/cn/ROS/Tutorials/InstallingandConfiguringROSEnvironment)
mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/src
cd ~/catkin_ws/
catkin_make
source devel/setup.bash
echo $ROS_PACKAGE_PATH(查看你当前工作目录)
4.下载并安装libfreenect2(参考:http://www.cnblogs.com/hitcm/p/5118196.html)
4.1 终端中输入:git clone https://github.com/OpenKinect/libfreenect2.git
4.2 安装依赖项:sudo apt-get install build-essential cmake pkg-config libturbojpeg libjpeg-turbo8-dev mesa-common-dev freeglut3-dev libxrandr-dev libxi-dev
4.3 安装libusb:
sudo apt-add-repository ppa:floe/libusb
sudo apt-get update
sudo apt-get install libusb-1.0-0-dev
4.4安装GLFW3
sudo apt-get install libglfw3-dev
如果没成功用下面代替上面语句
cd libfreenect2/depends
sh install_ubuntu.sh
sudo dpkg -i libglfw3*_3.0.4-1_*.deb
4.5 然后安装OpenCL的支持库(不打算使用GPU,这一步直接跳过没做)
4.6 编译库
cd ..
mkdir build && cd build
cmake ..
make
sudo make install
4.7 测试
最后可以运行程序.
在build下面有个bin文件夹,放置生成的输出文件,插上kinect,然后运行。此时黄灯变成白色的,表示有驱动。注意:只能用于USB3的接口,好在台式机和笔记本都有3.0的口。
./bin/Protonect
但是提示权限不够,failed to open Kinect V2 Access denied
此时需要把libfreenect2文件夹下面的rules里面的一个90开头的文件复制到/etc/udev/rules.d/下面就可以了。
然后重新运行上面的命令就可以了。
5.检验以上是否安装成功并检查kinect链接是否正确
打开终端输入
cd catkin_ws
source devel/setup.bash
roslaunch kinect2_bridge kinect2_bridge.launch
打开新的终端:
source devel/setup.bash
rosrun kinect2_viewer kinect2_viewer
会出现点云图则证明上述安装基本没问题
打开新的终端输入:rostopic list -v
查看最后几行中有depth图片和rgb图片的话题
我的计算机中显示:
Published topics:
- /kinect2/qhd/image_depth_rect/compressed [sensor_msgs/CompressedImage] 1 publisher
- /kinect2/hd/image_color/compressed [sensor_msgs/CompressedImage] 1 publisher
- /kinect2/hd/image_color_rect/compressed [sensor_msgs/CompressedImage] 1 publisher
- /kinect2/sd/points [sensor_msgs/PointCloud2] 1 publisher
- /kinect2/hd/image_mono/compressed [sensor_msgs/CompressedImage] 1 publisher
- /kinect2/bond [bond/Status] 5 publishers
- /kinect2/qhd/image_depth_rect [sensor_msgs/Image] 1 publisher
- /kinect2/hd/points [sensor_msgs/PointCloud2] 1 publisher
- /kinect2/hd/image_depth_rect [sensor_msgs/Image] 1 publisher
- /kinect2/sd/image_depth_rect [sensor_msgs/Image] 1 publisher
- /kinect2/hd/image_mono [sensor_msgs/Image] 1 publisher
- /kinect2/sd/image_color_rect [sensor_msgs/Image] 1 publisher
- /kinect2/hd/image_color_rect [sensor_msgs/Image] 1 publisher
- /kinect2/sd/image_ir/compressed [sensor_msgs/CompressedImage] 1 publisher
- /kinect2/qhd/image_mono [sensor_msgs/Image] 1 publisher
- /kinect2/sd/image_depth [sensor_msgs/Image] 1 publisher
- /kinect2/qhd/image_color [sensor_msgs/Image] 1 publisher
- /kinect2/qhd/image_color_rect [sensor_msgs/Image] 1 publisher
- /kinect2/sd/image_ir [sensor_msgs/Image] 1 publisher
- /kinect2/hd/image_color [sensor_msgs/Image] 1 publisher
- /kinect2/sd/image_depth_rect/compressed [sensor_msgs/CompressedImage] 1 publisher
- /rosout [rosgraph_msgs/Log] 6 publishers
- /kinect2/hd/image_depth_rect/compressed [sensor_msgs/CompressedImage] 1 publisher
- /kinect2/qhd/points [sensor_msgs/PointCloud2] 1 publisher
- /kinect2/sd/camera_info [sensor_msgs/CameraInfo] 1 publisher
- /rosout_agg [rosgraph_msgs/Log] 1 publisher
- /kinect2/qhd/image_mono_rect [sensor_msgs/Image] 1 publisher
- /kinect2/hd/image_mono_rect [sensor_msgs/Image] 1 publisher
- /kinect2/qhd/image_mono_rect/compressed [sensor_msgs/CompressedImage] 1 publisher
- /kinect2/sd/image_ir_rect/compressed [sensor_msgs/CompressedImage] 1 publisher
- /kinect2/qhd/camera_info [sensor_msgs/CameraInfo] 1 publisher
- /kinect2/hd/image_mono_rect/compressed [sensor_msgs/CompressedImage] 1 publisher
- /kinect2/sd/image_color_rect/compressed [sensor_msgs/CompressedImage] 1 publisher
- /kinect2/qhd/image_color_rect/compressed [sensor_msgs/CompressedImage] 1 publisher
- /kinect2/sd/image_ir_rect [sensor_msgs/Image] 1 publisher
- /kinect2/qhd/image_mono/compressed [sensor_msgs/CompressedImage] 1 publisher
- /kinect2/hd/camera_info [sensor_msgs/CameraInfo] 1 publisher
- /kinect2/qhd/image_color/compressed [sensor_msgs/CompressedImage] 1 publisher
- /kinect2/sd/image_depth/compressed [sensor_msgs/CompressedImage] 1 publisher
Subscribed topics:
- /kinect2/qhd/camera_info [sensor_msgs/CameraInfo] 1 subscriber
- /rosout [rosgraph_msgs/Log] 1 subscriber
- /kinect2/qhd/image_color_rect [sensor_msgs/Image] 1 subscriber
- /kinect2/bond [bond/Status] 5 subscribers
- /kinect2/qhd/image_depth_rect [sensor_msgs/Image] 1 subscriber
记下此处的/kinect2/qhd/image_color_rect和 /kinect2/qhd/image_depth_rect 下一步中会用到(重要!!!)
6.相机标定:
参考链接:https://github.com/code-iai/iai_kinect2/tree/master/kinect2_calibration
标定之后得到四个文件中查看calib_color.yaml文件中的相关相机参数并改正ORBSLAM~/ORB_SLAM2-master/Examples/ROS/ORB_SLAM2 文件夹中的Asus.yaml文件中的相关相关相机参数
我的修改部分为:
#--------------------------------------------------------------------------------------------
# Camera Parameters. Adjust them!
#--------------------------------------------------------------------------------------------
# Camera calibration and distortion parameters (OpenCV)
Camera.fx: 1050.6
Camera.fy: 1051.3
Camera.cx: 938.0
Camera.cy: 554.4
Camera.k1: 0.0
Camera.k2: 0.0
Camera.p1: 0.0
Camera.p2: 0.0
Camera. 1920
Camera.height: 1080
并将其重命名为Asus1.yaml
7.进入第一步中下载的ORBSLAM文件夹
并打开~/ORB_SLAM2-master/Examples/ROS/ORB_SLAM2/src 中的ros_rgbd.cc文件修改里边的一个地方:
main函数中
message_filters::Subscriber<sensor_msgs::Image> rgb_sub(nh, "/camera/rgb/image_raw", 1);
message_filters::Subscriber<sensor_msgs::Image> depth_sub(nh, "camera/depth_registered/image_raw", 1);
改成:
message_filters::Subscriber<sensor_msgs::Image> rgb_sub(nh, "yourtopic", 1);
message_filters::Subscriber<sensor_msgs::Image> depth_sub(nh, "yourtopic", 1);
我的这里的yourtopic是第五部中查到的/kinect2/qhd/image_color_rect和 /kinect2/qhd/image_depth_rect
修改保存
8.下载其他以来库:
8.1 Pangolin:https://github.com/stevenlovegrove/Pangolin
mkdir build
cd build
cmake ..
make -j2
sudo make install
8.2 Eigen3:http://eigen.tuxfamily.org 需要3.1.0以上版本,只需编译无需安装
mkdir build
cd build
cmake ..
make -j2
8.3 解压Vocabulary文件中的压缩文件
9.编译ORBSLAM
用终端直接运行ORBSLAM文件夹下的build.sh (按说应该是没任何问题)
和 build_ros.sh文件
可能会出现错误:
这一句出现:
CMake Error at CMakeLists.txt:2 (include):
include could not find load file:
/core/rosbuild/rosbuild.cmake
CMake Error at CMakeLists.txt:4 (rosbuild_init):
Unknown CMake command "rosbuild_init".
-- Configuring incomplete, errors occurred!
See also "/home/wishchin/WorkFrameSrc/ORB_SLAM-master/build/CMakeFiles/CMakeO
解决方法:原因是ROS path 没有自动添加,需要进行手动添加。
shell语句:
source /opt/ros/indigo/setup.bash source /opt/ros/kinetic/setup.bash
切换到主目录下。。。。
source .bashrc #使能
出现:
[rosbuild] Building package ORB_SLAM-master
[rosbuild] Error from directory check: /opt/ros/indigo/share/ros/core/rosbuild/bin/check_same_directories.py /home/wishchin/WorkFrameSrc/ORB_SLAM-master
Traceback (most recent call last):
File "/opt/ros/indigo/share/ros/core/rosbuild/bin/check_same_directories.py", line 46, in <module>
raise Exception
Exception
CMake Error at /opt/ros/indigo/share/ros/core/rosbuild/private.cmake:102 (message):
[rosbuild] rospack found package "ORB_SLAM-master" at "", but the current
directory is "/home/wishchin/WorkFrameSrc/ORB_SLAM-master". 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/indigo/share/ros/core/rosbuild/public.cmake:177 (_rosbuild_check_package_location)
CMakeLists.txt:4 (rosbuild_init)
-- Configuring incomplete, errors occurred!
See also "/home/wishchin/WorkFrameSrc/ORB_SLAM-master/build/CMakeFiles/CMakeOutput.log".
解决方法:
在.bashrc 末尾添加:export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:/home/wishchin/WorkFrameSrc/ORB_SLAM
ORB路径添加到ROS
10以上方法完成之后就可以进行测试ORBSLAM了
开启终端:cd catkin_ws
source devel/setup.bash
roslaunch kinect2_bridge kinect2_bridge.launch
开启新终端:rosrun ORB_SLAM2 RGBD /home/lmt/ORB_SLAM2-master/Vocabulary/ORBvoc.txt /home/lmt/ORB_SLAM2-master/Examples/ROS/ORB_SLAM2/Asus1.yaml