首先,进入到自己的功能包里,具体路径如下:
/home/..../orb_slam2_ws/src/ORB_SLAM2
(根据自己放的位置调整)
打开终端并使用 ./build_ros.sh进行编译,出现以下错误:
[rosbuild] rospack found package “ORB_SLAM2” at “”, but the current directory is
“/home/zhangjian/catkin_ws/src/ORB_SLAM2/Examples/ROS/ORB_SLAM2”. You should
double-check your ROS_PACKAGE_PATH to ensure that packages are found in the correct
precedence order.
这个问题是ROS路径设置的问题,由于ORBSLAM2中设置了rosbuild_init(),而我们编译文档之前需要先关联工作区间。ORBSLAM2这里提供的这个文档中,并没有我们熟悉的ROS里的package.xml和devel这些文件,直接编译会显示ORBSLAM2路径冲突之类的问题,这个错误提示的是显示的错误表示ROS环境并没有设置正确,解决方法如下:
首先是需要添加路径,在~/.bashrc上设置了路径(注意这个path是我的路径,读者需要自己修改):
sudo gedit ~/.bashrc
打开文件后千万不要乱写,在最后一行添加:()
export ROS_PACKAGE_PATH=${ROS_PACKAGE_PATH}:/home/.../..../src/ORB_SLAM2/Examples/ROS
这里冒号后面的路径,就是是自己放置ORB-SLAM2功能包的位置
添加完后,打开个新的终端,输入:
source ~/.bashrc
来更新路径。
输入下面一行命令来查看路径是否创建成功:
echo $ROS_PACKAGE_PATH
输出结果如果是刚刚添加的路径:
/home/.../..../src/ORB_SLAM2/Examples/ROS,说明路径创建成功了!
使用下述命令再次编译:
./build_ros.sh
/usr/bin/ld: warning: libopencv_imgcodecs.so.4.2, needed by /opt/ros/noetic/lib/libcv_bridge.so, may conflict with libopencv_imgcodecs.so.3.4
/usr/bin/ld: warning: libopencv_core.so.4.2, needed by /opt/ros/noetic/lib/libcv_bridge.so, may conflict with libopencv_core.so.3.4
/usr/bin/ld: warning: libopencv_imgcodecs.so.4.2, needed by /opt/ros/noetic/lib/libcv_bridge.so, may conflict with libopencv_imgcodecs.so.3.4
/usr/bin/ld: warning: libopencv_core.so.4.2, needed by /opt/ros/[ 55%] Built target Mono
noetic/lib/libcv_bridge.so, may conflict with libopencv_core.so.3.4
[ 66%] Built target RGBD
make: *** [Makefile:130:all] 错误 2
网上可能会有一些教程说,需要另外安装OPENCV4.2,
但是如果你是Ubuntu20.04,且使用命令默认安装了ROS的话,opencv4.2是自带的。
解决报错方法如下:
第一步,定位出现问题的位置:
locate libopencv_imgproc.so
locate libopencv_core.so
如果出现提示,说没有找到命令,就去安装一下locate功能包。使用命令:
sudo apt install mlocate
安装完成后,定位到这两个文件,如下图:
我的解决方案是:将/usr/lib/x86_64-linux-gnu/libopencv_imgproc.4.2.0和/usr/lib/x86_64-linux-gnu/libopencv_core.4.2.0复制到ORB_SLAM2的lib文件夹当中。
(这一步要小心,不要复制错了!!一旦不是那个文件,就会出问题!)
复制过后,在 ORB_SLAM2/Examples/ROS/ORB_SLAM2路径下的 CMakeList.txt 文件中添加
${PROJECT_SOURCE_DIR}/../../../lib/libopencv_imgproc.so.4.2.0
${PROJECT_SOURCE_DIR}/../../../lib/libopencv_core.so.4.2.0
(有几处注释#,不用写进去,那都是踩出来的坑!
比如:
# /usr/local/lib/libopencv_core.so.3.4
# /usr/local/lib/libopencv_imgproc.so.3.4
# /usr/lib/aarch64-linux-gnu/libopencv_core.so.4.2
# /usr/lib/aarch64-linux-gnu/libopencv_imgproc.so.4.2 这些复制到lib中,都是不行的,而且最后CMakeList.txt中后缀必须加上4.2.0!否则报错。
)
我的CMakeList.txt最后是这个样子:
cmake_minimum_required(VERSION 2.4.6)
include($ENV{ROS_ROOT}/core/rosbuild/rosbuild.cmake)
rosbuild_init()
IF(NOT ROS_BUILD_TYPE)
SET(ROS_BUILD_TYPE Release)
ENDIF()
MESSAGE("Build type: " ${ROS_BUILD_TYPE})
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -O3 -march=native ")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -O3 -march=native")
# Check C++11 or C++0x support
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
add_definitions(-DCOMPILEDWITHC11)
message(STATUS "Using flag -std=c++11.")
elseif(COMPILER_SUPPORTS_CXX0X)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
add_definitions(-DCOMPILEDWITHC0X)
message(STATUS "Using flag -std=c++0x.")
else()
message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()
LIST(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/../../../cmake_modules)
find_package(OpenCV 3.0 QUIET)
if(NOT OpenCV_FOUND)
find_package(OpenCV 2.4.3 QUIET)
if(NOT OpenCV_FOUND)
message(FATAL_ERROR "OpenCV > 2.4.3 not found.")
endif()
endif()
find_package(Eigen3 3.1.0 REQUIRED)
find_package(Pangolin REQUIRED)
include_directories(
${PROJECT_SOURCE_DIR}
${PROJECT_SOURCE_DIR}/../../../
${PROJECT_SOURCE_DIR}/../../../include
${Pangolin_INCLUDE_DIRS}
)
set(LIBS
${OpenCV_LIBS}
${EIGEN3_LIBS}
${Pangolin_LIBRARIES}
${PROJECT_SOURCE_DIR}/../../../Thirdparty/DBoW2/lib/libDBoW2.so
${PROJECT_SOURCE_DIR}/../../../Thirdparty/g2o/lib/libg2o.so
${PROJECT_SOURCE_DIR}/../../../lib/libORB_SLAM2.so
# /usr/local/lib/libopencv_core.so.3.4
# /usr/local/lib/libopencv_imgproc.so.3.4
# /usr/lib/aarch64-linux-gnu/libopencv_core.so.4.2
# /usr/lib/aarch64-linux-gnu/libopencv_imgproc.so.4.2
# -lboost_system
${PROJECT_SOURCE_DIR}/../../../lib/libopencv_imgproc.so.4.2.0
${PROJECT_SOURCE_DIR}/../../../lib/libopencv_core.so.4.2.0
)
# Node for monocular camera
rosbuild_add_executable(Mono
src/ros_mono.cc
)
target_link_libraries(Mono
${LIBS}
)
# Node for monocular camera (Augmented Reality Demo)
rosbuild_add_executable(MonoAR
src/AR/ros_mono_ar.cc
src/AR/ViewerAR.h
src/AR/ViewerAR.cc
)
target_link_libraries(MonoAR
${LIBS}
)
# Node for stereo camera
rosbuild_add_executable(Stereo
src/ros_stereo.cc
)
target_link_libraries(Stereo
${LIBS}
)
# Node for RGB-D camera
rosbuild_add_executable(RGBD
src/ros_rgbd.cc
)
target_link_libraries(RGBD
${LIBS}
)
# rosbuild_add_executable(RGBD_WITH_MY_BAG
# src/ros_rgbd_with_my_bag.cc
# )
# target_link_libraries(RGBD_WITH_MY_BAG
# ${LIBS}
# )
到这里可以喝口水休息一下了,头很大。
再执行./build_ros.sh,发现编译通过(但是有warning,影响不大)
[ 55%] Built target Mono
[ 66%] Built target Stereo
[ 88%] Built target MonoAR
[100%] Built target RGBD
完成ORB—SLAM2内的ros功能包编译。
参考文章如下:
ROS采坑日记(3)----在ROS下 编译ORB_SLAM2时遇到问题:[rosbuild] rospack found package "ORB_SLAM2" at ""........ - 灰信网(软件开发博客聚合)
【已解决】ORB_SLAM2 编译 ./build_ros.h错误_缔宇diyu的博客-CSDN博客