REMODE+ORBSLAM运行配置(1) 把ORB编译成ROS工程
原版的ORBSLAM是一个cmake工程,由于项目的需要,我们要在其他ROS工程(简称A)和ORB工程进行通讯,即需要将这两个工程联编,这就有两个办法:
方法一:把A工程修改成一个cmake工程,然后和ORB联编,利用多线程完成通信。
方法二:把ORB修改成ROS工程,编程成一个节点,和A工程利用ros节点进行通信。
权衡了一下修改的复杂程度以及考虑到ROS的优点,选择了方案二。
下面开始记录修改步骤:
1:找一块地方,新建一个工程,简称ORB_ROS
1.1创建了如上的文件结构,并进入最内层src目录。所有的ros包都要放入到这里面进行编译。
baohua@baohua-ThinkPad-S5:~$ mkdir -p ORB_ROS/catkin_ws/src&&cd ORB_ROS/catkin_ws/src
1.2创建出ORB_SLAM包
baohua@baohua-ThinkPad-S5:~/ORB_ROS/catkin_ws/src$ catkin_create_pkg ORB_SLAM
进入到ORB_SLAM包里,你会看到有两个文件:
一个是ROS版本的CMakeLists.txt,另一个是package.xml文件。这两个文件由catkin_create_pkg自动生产。但是,我们在执行catkin_creat_pkg ORB_SLAM时,里面什么都没有,所以我们要根据原来ORBSLAM工程的CMakeLists.txt修改包内的CMakeLists.txt,
另外,catkin_creat_pkg的指令格式其实是:catkin_creat_pkg <package name> [depend1]...[dependn] ,我们刚才创建ORB_SLAM时并没有添加任何ROS依赖depend,因为在生成package.xml后手动添加ROS依赖depend更方便。所以,我们还要根据新编译的工程将会对ROS的依赖修改package.xml文件。关于package.xml文件是什么,请参考ROSwiki。
2 将原来的ORB工程中除了其自身的CMakeLists后的其他文件拷贝进入第一步创建的ORB_SLAM包内。然后,修改包内的package.xml和CMakeLists.txt。
2.1 修改package.xml
package.xml文件有分为几个部分:分别是工程名版本信息,作者维护者信息,license,以及编译依赖(build_depend) 和运行依赖(run_depend)。
我们只关心编译依赖(build_depend) 和运行依赖(run_depend),其他信息对编译代码没有什么影响。
<!-- Dependencies which this package needs to build itself. --> <buildtool_depend>catkin</buildtool_depend> <!-- Dependencies needed to compile this package. --> <build_depend>roscpp</build_depend> <build_depend>cmake_modules</build_depend> <!-- for FindEigen.cmake module --> <build_depend>nav_msgs</build_depend> <build_depend>std_msgs</build_depend> <build_depend>visualization_msgs</build_depend> <build_depend>sensor_msgs</build_depend> <build_depend>image_transport</build_depend> <build_depend>cv_bridge</build_depend> <build_depend>tf</build_depend> <build_depend>svo</build_depend> <build_depend>svo_msgs</build_depend> <build_depend>vikit_common</build_depend> <build_depend>vikit_ros</build_depend> <!-- Dependencies needed after this package is compiled. --> <run_depend>roscpp</run_depend> <run_depend>nav_msgs</run_depend> <run_depend>std_msgs</run_depend> <run_depend>visualization_msgs</run_depend> <run_depend>sensor_msgs</run_depend> <run_depend>image_transport</run_depend> <run_depend>cv_bridge</run_depend> <run_depend>tf</run_depend> <run_depend>svo</run_depend> <run_depend>svo_msgs</run_depend> <run_depend>vikit_common</run_depend> <run_depend>vikit_ros</run_depend> <!-- The export tag contains other, unspecified, tags -->
注:vikit_common和vikit_ros 需要从下载,这两个依赖的作用是从文件中读取参数。
git clone https://github.com/uzh-rpg/rpg_vikit.git
下载后放到与ORB_SLAM包同目录下。
2.2 修改ROS包ORB_SLAM内的CMakeLists.txt
空的CMakeLists.txt是这样的:
cmake_minimum_required(VERSION 2.8.3) project(ORB_SLAM) ## Find catkin macros and libraries ## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz) ## is used, also find other catkin packages find_package(catkin REQUIRED) ## System dependencies are found with CMake's conventions # find_package(Boost REQUIRED COMPONENTS system) ## Uncomment this if the package has a setup.py. This macro ensures ## modules and global scripts declared therein get installed ## See http://ros.org/doc/api/catkin/html/user_guide/setup_dot_py.html # catkin_python_setup() ################################################ ## Declare ROS messages, services and actions ## ################################################ ## To declare and build messages, services or actions from within this ## package, follow these steps: ## * Let MSG_DEP_SET be the set of packages whose message types you use in ## your messages/services/actions (e.g. std_msgs, actionlib_msgs, ...). ## * In the file package.xml: ## * add a build_depend tag for "message_generation" ## * add a build_depend and a run_depend tag for each package in MSG_DEP_SET ## * If MSG_DEP_SET isn't empty the following dependency has been pulled in ## but can be declared for certainty nonetheless: ## * add a run_depend tag for "message_runtime" ## * In this file (CMakeLists.txt): ## * add "message_generation" and every package in MSG_DEP_SET to ## find_package(catkin REQUIRED COMPONENTS ...) ## * add "message_runtime" and every package in MSG_DEP_SET to ## catkin_package(CATKIN_DEPENDS ...) ## * uncomment the add_*_files sections below as needed ## and list every .msg/.srv/.action file to be processed ## * uncomment the generate_messages entry below ## * add every package in MSG_DEP_SET to generate_messages(DEPENDENCIES ...) ## Generate messages in the 'msg' folder # add_message_files( # FILES # Message1.msg # Message2.msg # ) ## Generate services in the 'srv' folder # add_service_files( # FILES # Service1.srv # Service2.srv # ) ## Generate actions in the 'action' folder # add_action_files( # FILES # Action1.action # Action2.action # ) ## Generate added messages and services with any dependencies listed here # generate_messages( # DEPENDENCIES # std_msgs # Or other packages containing msgs # ) ################################################ ## Declare ROS dynamic reconfigure parameters ## ################################################ ## To declare and build dynamic reconfigure parameters within this ## package, follow these steps: ## * In the file package.xml: ## * add a build_depend and a run_depend tag for "dynamic_reconfigure" ## * In this file (CMakeLists.txt): ## * add "dynamic_reconfigure" to ## find_package(catkin REQUIRED COMPONENTS ...) ## * uncomment the "generate_dynamic_reconfigure_options" section below ## and list every .cfg file to be processed ## Generate dynamic reconfigure parameters in the 'cfg' folder # generate_dynamic_reconfigure_options( # cfg/DynReconf1.cfg # cfg/DynReconf2.cfg # ) ################################### ## catkin specific configuration ## ################################### ## The catkin_package macro generates cmake config files for your package ## Declare things to be passed to dependent projects ## INCLUDE_DIRS: uncomment this if you package contains header files ## LIBRARIES: libraries you create in this project that dependent projects also need ## CATKIN_DEPENDS: catkin_packages dependent projects also need ## DEPENDS: system dependencies of this project that dependent projects also need catkin_package( # INCLUDE_DIRS include # LIBRARIES ORB_SLAM # CATKIN_DEPENDS other_catkin_pkg # DEPENDS system_lib ) ########### ## Build ## ########### ## Specify additional locations of header files ## Your package locations should be listed before other locations # include_directories(include) ## Declare a C++ library # add_library(ORB_SLAM # src/${PROJECT_NAME}/ORB_SLAM.cpp # ) ## Add cmake target dependencies of the library ## as an example, code may need to be generated before libraries ## either from message generation or dynamic reconfigure # add_dependencies(ORB_SLAM ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS}) ## Declare a C++ executable # add_executable(ORB_SLAM_node src/ORB_SLAM_node.cpp) ## Add cmake target dependencies of the executable ## same as for the library above # add_dependencies(ORB_SLAM_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS}) ## Specify libraries to link a library or executable target against # target_link_libraries(ORB_SLAM_node # ${catkin_LIBRARIES} # ) ############# ## Install ## ############# # all install targets should use catkin DESTINATION variables # See http://ros.org/doc/api/catkin/html/adv_user_guide/variables.html ## Mark executable scripts (Python etc.) for installation ## in contrast to setup.py, you can choose the destination # install(PROGRAMS # scripts/my_python_script # DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION} # ) ## Mark executables and/or libraries for installation # install(TARGETS ORB_SLAM ORB_SLAM_node # ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION} # LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION} # RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION} # ) ## Mark cpp header files for installation # install(DIRECTORY include/${PROJECT_NAME}/ # DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION} # FILES_MATCHING PATTERN "*.h" # PATTERN ".svn" EXCLUDE # ) ## Mark other files for installation (e.g. launch and bag files, etc.) # install(FILES # # myfile1 # # myfile2 # DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION} # ) ############# ## Testing ## ############# ## Add gtest based cpp test target and link libraries # catkin_add_gtest(${PROJECT_NAME}-test test/test_ORB_SLAM.cpp) # if(TARGET ${PROJECT_NAME}-test) # target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME}) # endif() ## Add folders to be run by python nosetests # catkin_add_nosetests(test)
修改部分:catkin依赖部分和原cmake依赖部分
2.2.1 catkin依赖部分
#catkin工作空间的配置 FIND_PACKAGE(catkin REQUIRED COMPONENTS roscpp cmake_modules nav_msgs std_msgs visualization_msgs sensor_msgs image_transport cv_bridge tf vikit_common vikit_ros )
# Describe catkin Project catkin_package( DEPENDS Eigen OpenCV Sophus Boost CATKIN_DEPENDS roscpp nav_msgs std_msgs visualization_msgs sensor_msgs image_transport cv_bridge tf svo svo_msgs vikit_common vikit_ros INCLUDE_DIRS include LIBRARIES #svo_visualizer )
2.2.2 ORB_SLAMcmake依赖部分
## System dependencies are found with CMake's conventions # find_package(Boost REQUIRED COMPONENTS system) FIND_PACKAGE(OpenCV REQUIRED) FIND_PACKAGE(Eigen REQUIRED) FIND_PACKAGE(Sophus REQUIRED) FIND_PACKAGE(Boost REQUIRED COMPONENTS thread) FIND_PACKAGE(Pangolin REQUIRED)
#ORB的依赖头文件 INCLUDE_DIRECTORIES(${catkin_INCLUDE_DIRS})#catkin工作空间的路径,这里是baohua@baohua-ThinkPad-S5:~/ORB_ROS/catkin_ws INCLUDE_DIRECTORIES( include #include是此CMakeLists.txt同目录下的include文件夹路径 ${PROJECT_SOURCE_DIR} #${PROJECT_SOURCE_DIR} 是此CMakeLists.txt上层目录路径 ${Eigen_INCLUDE_DIRS} ${OpenCV_INCLUDE_DIRS} ${Sophus_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS} ${catkin_INCLUDE_DIRS} )
#ORB的源文件 LIST(APPEND SOURCEFILES imu/eigen_utils.cpp imu/IMU.cpp src/System.cc src/Tracking.cc src/LocalMapping.cc src/LoopClosing.cc src/ORBextractor.cc src/ORBmatcher.cc src/FrameDrawer.cc src/Converter.cc src/MapPoint.cc src/KeyFrame.cc src/Map.cc src/MapDrawer.cc src/Optimizer.cc src/PnPsolver.cc src/Frame.cc src/KeyFrameDatabase.cc src/Sim3Solver.cc src/Initializer.cc src/Viewer.cc MoveSense/Camera.cpp MoveSense/CameraCtrl.cpp MoveSense/CtrlInf.cpp MoveSense/MoveSenseCamera.cpp MoveSense/runCamera2.cpp MoveSense/VL4IF.cpp )
注:我们对源文件修改过,你可以根据自己的文件结构添加
#ORB的依赖库文件 LIST(APPEND LINK_LIBS ${catkin_LIBRARIES}) LIST(APPEND LINK_LIBS ${OpenCV_LIBS} ${Sophus_LIBRARIES} ${Boost_LIBRARIES} ${Pangolin_LIBRARIES} ${catkin_LIBRARIES} ${PROJECT_SOURCE_DIR}/Thirdparty/DBoW2/lib/libDBoW2.so ${PROJECT_SOURCE_DIR}/Thirdparty/g2o/lib/libg2o.so ${PROJECT_SOURCE_DIR}/Thirdparty/Sophus/build/libSophus.so /usr/lib/x86_64-linux-gnu/libboost_system.so /usr/lib/x86_64-linux-gnu/libboost_filesystem.so /usr/lib/x86_64-linux-gnu/libusb-1.0.so /usr/lib/x86_64-linux-gnu/libudev.so /usr/lib/x86_64-linux-gnu/libusb.so /usr/lib/x86_64-linux-gnu/libv4l1.so )
将除主函数的其他文件生成一个库
# Create ORB library ADD_LIBRARY(ORB SHARED ${SOURCEFILES}) TARGET_LINK_LIBRARIES(ORB ${LINK_LIBS})
生成可执行文件
## Declare a C++ executable ADD_EXECUTABLE(EuRoC Examples/RGB-D/EuRoC.cc) TARGET_LINK_LIBRARIES(EuRoC ORB)
经过第2部分的步骤,我们修改完成了工程的依赖。
3 编译ROS工程ORB_SLAM
首先进入catkin_ws目录
baohua@baohua-ThinkPad-S5:~$ cd '/home/baohua/ORB_ROS/catkin_ws'
找到main函数
加入头文件:
#include<vikit/file_reader.h> #include<vikit/params_helper.h> #include<vikit/camera_loader.h> #include<ros/ros.h> #include<ros/package.h>
在main里开启节点 节点名字随意定义
ros::init(argc,argv,"ORB_SLAM");//一定要开启ros节点,vk::getParam才能找到launch文件的参数 ros::NodeHandle n;
读launch文件的参数(launch文件后面会说到)
const string CamPara_Path(vk::getParam<std::string>("ORB_SLAM/CamPara_Path")); const string BOW_Path(vk::getParam<std::string>("ORB_SLAM/BOW_Path")); string dataset_path(vk::getParam<std::string>("ORB_SLAM/Img_Path"));//imu数据路径
然后编译代码
baohua@baohua-ThinkPad-S5:~/ORB_ROS/catkin_ws$ catkin_make
编译成功后,在catkin_ws目录下,你会看到新生成的build和devel文件,这是catkin_make生成的。其中可执行文件在
~/ORB_ROS/catkin_ws/devel/lib/ORB_SLAM目录下。
4运行代码
4.1 设置环境变量
source '/home/baohua/ORB_ROS/catkin_ws/devel/setup.sh'
4.2 运行可执行程序 EuRoC
ORB代码是带参运行的,为的是在修改一些参数时可以不用重新编译代码就可以运行。因为带参,所以我们选择roslaunch方法运行可执行文件。关于roslaunch,参见另一篇博客http://blog.csdn.net/zhubaohua_bupt/article/details/53858388。
在ORB_SLAM包里新建launch文件,这里直接把文件内容粘上
<launch> <node pkg="ORB_SLAM" type="EuRoC" name="ORB_SLAM"> <!-- CamPara_Path --> <param name="CamPara_Path" value="/home/baohua/project/SLAM/REMODE_ORB/catkin_ws/src/ORB_SLAM/Camera_configs/EuRoC_MH01.yaml" type="str" /> <!-- BOW_Path --> <param name="BOW_Path" value="/home/baohua/project/SLAM/REMODE_ORB/catkin_ws/src/ORB_SLAM/Vocabulary/ORBvoc.bin" type="str" /> <!-- Img_Path --> <param name="Img_Path" value="/media/baohua/media/SLAM_DATA/mav0_MH01" type="str" /> </node> </launch>
最后一步:
运行launch文件:
baohua@baohua-ThinkPad-S5:~/ORB_ROS/catkin_ws$ roslaunch ORB_SLAM EuRoC.launch