ROS入门:ROS下使用电脑相机运行ORB_Slam2

介绍:

最近在学习slam,想将其应用在ros平台上,故跑了orb-slam2的代码。这里粗略总结一下"ROS下使用电脑相机运行ORB_Slam2"的过程。本人菜鸟一枚,如有问题欢迎交流。

创建ros工作空间

参考博文:https://blog.csdn.net/qinqinxiansheng/article/details/106768491

编译usb摄像头驱动

网上有很多博文:
https://www.jianshu.com/p/ec0ebf7fef22
配置好后,在终端运行

source devel/setup.bash
roslaunch usb_cam usb_cam-test.launch 

即可打开摄像头:其中/usb_can/image_raw即为ros节点发布的话题消息
ROS入门:ROS下使用电脑相机运行ORB_Slam2_第1张图片

下载 orb_slam源码

(github链接https://github.com/raulmur/ORB_SLAM2)
下载orb_slam2的源码,并按照提示配置相关环境,包括Pangolin,opencv,eigen,g2o
(这里因为想用ros机制,所以安装了ros).这里只需要配置好环境就行,不需要编译,后续会统一编译。

编译orbslam

1.将下载好的orb-slam源码RB_SLAM2-master放在工作空间的src文件夹下(/home/qin/catkin_ws/src

2.在ORB_SLAM2-master/Examples/ROS/ORB_SLAM2/src文件夹找到ros_mono.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);

3.添加相机参数文件
在ORB_SLAM2-master文件夹下编写一个myslam.yaml文件用于存放相机参数,只为运行程序的话也可以用其他相机的参数:

%YAML:1.0
 
# Camera calibration and distortion parameters (OpenCV) 
Camera.fx: 500.0
Camera.fy: 500.0
Camera.cx: 320.0
Camera.cy: 240.0
 
Camera.k1: 0
Camera.k2: 0
Camera.p1: 0
Camera.p2: 0
Camera.k3: 0
 
# Camera frames per second 
Camera.fps: 30.0
 
# Color order of the images (0: BGR, 1: RGB. It is ignored if images are grayscale)
Camera.RGB: 0
 
# ORB Extractor: Number of features per image
ORBextractor.nFeatures: 1000
 
# ORB Extractor: Scale factor between levels in the scale pyramid     
ORBextractor.scaleFactor: 1.2
 
# ORB Extractor: Number of levels in the scale pyramid    
ORBextractor.nLevels: 8
 
        
ORBextractor.iniThFAST: 10
ORBextractor.minThFAST: 5
 
Viewer.KeyFrameSize: 0.05
Viewer.KeyFrameLineWidth: 1
Viewer.GraphLineWidth: 0.9
Viewer.PointSize: 2
Viewer.CameraSize: 0.08
Viewer.CameraLineWidth: 3
Viewer.ViewpointX: 0
Viewer.ViewpointY: -0.7
Viewer.ViewpointZ: -1.8
Viewer.ViewpointF: 500

4.在主文件夹下,Ctrl+h,可以看到隐藏的文件,在.bashrc文档末尾添加两行:(根据自己路径更改)

export ROS_PACKAGE_PATH=${ROS_PACKAGE_PATH}:/home/qin/catkin_ws/src/ORB_SLAM2-master/Examples/ROS
source ~/catkin_ws/devel/setup.bash

然后在终端运行

source ~/.bashrc

5.修改Examples/ROS/ORB_SLAM2/文件夹下的CMakeLists.txt文件
在相应位置添加:一行 -lboost_system
ROS入门:ROS下使用电脑相机运行ORB_Slam2_第2张图片
6.编译ORB_Slam2
打开终端,运行

cd catkin_ws/src/ORB_SLAM2-master
mkdir build 
cd build 
cmake ..
make 

7.编译ROS的example

cd ~/catkin_ws/src/ORB_SLAM2-master/Example/ROS/ORB_SLAM2-master
mkdir build
cd build
cmake ..
make 

运行测试

1.打开rosmaster
打开终端,输入

roscore

2.打开相机
打开新的终端,输入

roslaunch usb_cam usb_cam-test.launch 

3.运行orbslam
新打开终端,运行(路径需要根据实际进行修改)

rosrun ORB_SLAM2 Mono /home/qin/catkin_ws/src/ORB_SLAM2-master/Vocabulary/ORBvoc.txt /home/qin/catkin_ws/src/ORB_SLAM2-master/myslam.yaml 
/home/qin/catkin_ws/src/ORB_SLAM2-master

这里献上博主的建模结果:

ROS入门:ROS下使用电脑相机运行ORB_Slam2_第3张图片

你可能感兴趣的:(ROS)