创建ROS-wrapper

创建ROS-wrapper

  • 对高博的ygz-stereo-inertial的开源算法添加ROS node
    • 源文件的编写
    • CMakeLists文件的编写
      • 分目录下的CMakeLists
        • 完整CMakeLists
          • 路径/home/fei/devv/ygz-stereo-inertial-master/backend下的CMakeLists文件
          • 路径/home/fei/devv/ygz-stereo-inertial-master/common下的CMakeLists文件
          • 路径 /home/fei/devv/ygz-stereo-inertial-master/cv下的完整CMakeLists
          • 路径/home/fei/devv/ygz-stereo-inertial-master/system下的CMakeLists
          • 路径/home/fei/devv/ygz-stereo-inertial-master/util下的CMakeLists
        • 小结
      • 在主目录下的CMakeLists
        • 完整的CMakeLists文件
      • ROS目录下的CMakeLists
        • 完整CMakeLists

ros节点是为了让整个工程能够订阅topic消息,那么必须要给这个工程创建一个ros node ,以便能够通过ROS来进行控制,对于部分的开源算法,可能不会提供ROS Wrapper功能包,那么这需要自己写一个才能成功运用于ROS工程并能落地实现。

对高博的ygz-stereo-inertial的开源算法添加ROS node

这里也并不知道为什么源码不提供ROS接口,而只是单纯的跑跑数据集。但是最后发现自己写出来的ROS node照样是跑.bag数据集文件效果并不是很好。

源文件的编写

1、基本格式

  ros::init(argc,argv,"ygz_ros");
  ros::start();
  ros::NodeHandle nh;

这是必不可少的语句,其中ygz_ros是当前节点的名称,并且是当前工程下唯一的。

  • 函数原型:ros::init(argc, argv, “my_node_name”, ros::init_options::AnonymousName);
  • 初始化节点名字必须在最前面,如果ROS系统中出现重名,则之前的节点会被自动关闭
  • 如果想要多个重名节点而不报错,可以在init中添加ros::init_options::AnonymousName参数
  • 该参数会在原有节点名字的后面添加一些随机数来使每个节点独一无二
  • ros::NodeHandle获取节点的句柄,init是初始化节点,ros::start()是Starting the node
  • 如果不想通过对象的生命周期来管理节点的开始和结束,
  • 你可以通过ros::start()和ros::shutdown() 来自己管理节点。

2、订阅图像topic消息

    message_filters::Subscriber left_sub(nh, left_topic, 2);
    message_filters::Subscriber right_sub(nh, right_topic, 2);
    

这两句是分别创建了订阅left_topic、right_topic两个topic消息的对象,最后一个参数是缓冲容器,如果缓冲容器中已满,那么如再有topic消息进来,那么会丢弃前面的topic。具体参考官网

    typedef message_filters::sync_policies::ApproximateTime sync_pol;
    message_filters::Synchronizer sync(sync_pol(10), left_sub,right_sub);
    sync.registerCallback(boost::bind(&ImageGrabber::FetchImageCallback,&igb,_1,_2));

那么需要对订阅的两个topic被一个回调函数进行处理,需要进行以上的编写,其中boost::bind(&ImageGrabber::FetchImageCallback,&igb,_1,_2)是回调函数。可参考

其中这里的boost::bind的调用,目的是使用一个类的成员函数作为回调函数所必须的操作。这里,boost::bind中的第一个参

数是回调函数名,第二个是回调函数所属类的对象,第三个 _1 是一个占位符,因为回调函数image_callback的第一个参数

是msg,要给它留位置。第四个,就是传的一个参数。以此扩展,如果想要传两个参数,和留多一个占用符,那么这

样:boost::bind(image_callback, _1, _2,&pub_vt, &pub_odo),不要把_1/_2当做参数个数哈
3、订阅IMU topic消息

    ros::Subscriber imu_sub = nh.subscribe(imu_topic, 10, &ImageGrabber::FetchImuCallback, &igb);
  • 由于IMU只有一个topic,因此不需要像订阅图像topic一样创建各种对象。这里直接可以用subscribe函数进行订阅且执行回调函数。
  • 同样是使用类中的成员函数作为回调函数,但是这里并没有调用boost::bind。这是区别,应该注意

4、ros::spin()
在最后要加上ros::spin()
在topic接收方,有一个比较重要的概念,就是回调(CallBack),在本例中,回调就是预先给话题传来的消息准备一个回调函数,你事先定义好回调函数的操作,这里分别是对图像和IMU数据进行处理。只有当有消息来时,回调函数才会被触发执行。具体去触发的命令就是 ros::spin() ,它会反复的查看有没有消息来,如果有就会让回调函数去处理。
因此千万不要认为,只要指定了回调函数,系统就回去自动触发,你必须 ros::spin() 或者 ros::spinOnce() 才能真正使回调函数生效。
5、源文件

#include 
#include 

#include 
#include 

#include "ygz/System.h"   //../../../system/include
#include "ygz/EurocReader.h"  //../../../util/include/

#include 
#include 
#include 
#include 
#include  //for DJI.



#include 
#include 
#include 
#include 


#include  //for px4's external pose estimate


using namespace std;
using namespace ygz;
using namespace cv;

class ImageGrabber
{
public:
    ImageGrabber(System* pSLAM):mpSLAM(pSLAM){}


    void FetchImuCallback(const sensor_msgs::Imu& imu);
    void FetchImageCallback(const sensor_msgs::ImageConstPtr& msgLeft,const sensor_msgs::ImageConstPtr& msgRight);
    
    System* mpSLAM;
    VecIMU temp_vimu;
    //bool do_rectify;
    cv::Mat M1l,M2l,M1r,M2r;
};





int main(int argc,char **argv)
{
  //初始化节点名字必须在最前面,如果ROS系统中出现重名,则之前的节点会被自动关闭
//如果想要多个重名节点而不报错,可以在init中添加ros::init_options::AnonymousName参数
//该参数会在原有节点名字的后面添加一些随机数来使每个节点独一无二
//ros::init(argc, argv, "my_node_name", ros::init_options::AnonymousName);
  
  ros::init(argc,argv,"ygz_ros");
  ros::start();
    if(argc != 2)
    {
        cout<<"YOU NEED TO SPECIFY CONFIG PATH!"<<endl;
        return 0;
    }
   	//获取节点的句柄,init是初始化节点,这个是Starting the node
	//如果不想通过对象的生命周期来管理节点的开始和结束,
	//你可以通过ros::start()和ros::shutdown() 来自己管理节点。 
    ros::NodeHandle nh;
    
    string configFile(argv[1]);
    cv::FileStorage fsSettings(configFile, cv::FileStorage::READ);

    if (fsSettings.isOpened() == false) {
        cerr << "Cannot load the config file from " << argv[1] << endl;
    }

    System system(argv[1]);
    //pSystem = &system;
    
    ImageGrabber igb(&system);
    
    string left_topic = string(fsSettings["Left"]);
    string right_topic = string(fsSettings["Right"]);
    string imu_topic=string(fsSettings["IMU"]);
    
    // rectification parameters
    cv::Mat K_l, K_r, P_l, P_r, R_l, R_r, D_l, D_r;
    fsSettings["LEFT.K"] >> K_l;
    fsSettings["RIGHT.K"] >> K_r;

    fsSettings["LEFT.P"] >> P_l;
    fsSettings["RIGHT.P"] >> P_r;

    fsSettings["LEFT.R"] >> R_l;
    fsSettings["RIGHT.R"] >> R_r;

    fsSettings["LEFT.D"] >> D_l;
    fsSettings["RIGHT.D"] >> D_r;

    int rows_l = fsSettings["LEFT.height"];
    int cols_l = fsSettings["LEFT.width"];
    int rows_r = fsSettings["RIGHT.height"];
    int cols_r = fsSettings["RIGHT.width"];

    if (K_l.empty() || K_r.empty() || P_l.empty() || P_r.empty() || R_l.empty() || R_r.empty() || D_l.empty() ||
        D_r.empty() ||
        rows_l == 0 || rows_r == 0 || cols_l == 0 || cols_r == 0) {
        cerr << "ERROR: Calibration parameters to rectify stereo are missing!" << endl;
        return 1;
    }

    
    cv::initUndistortRectifyMap(K_l, D_l, R_l, P_l.rowRange(0, 3).colRange(0, 3), cv::Size(cols_l, rows_l), CV_32F, igb.M1l,
                                igb.M2l);
    cv::initUndistortRectifyMap(K_r, D_r, R_r, P_r.rowRange(0, 3).colRange(0, 3), cv::Size(cols_r, rows_r), CV_32F, igb.M1r,
                                igb.M2r);




	
    VecIMU vimus;
 //   if (LoadImus(imuFolder, vimus) == false)
 //       return 1;

    // read TBC
    cv::Mat Rbc, tbc;
    fsSettings["RBC"] >> Rbc;
    fsSettings["TBC"] >> tbc;
    if (!Rbc.empty() && tbc.empty()) {
        Matrix3d Rbc_;
        Vector3d tbc_;
        Rbc_ <<
             Rbc.at<double>(0, 0), Rbc.at<double>(0, 1), Rbc.at<double>(0, 2),
                Rbc.at<double>(1, 0), Rbc.at<double>(1, 1), Rbc.at<double>(1, 2),
                Rbc.at<double>(2, 0), Rbc.at<double>(2, 1), Rbc.at<double>(2, 2);
        tbc_ <<
             tbc.at<double>(0, 0), tbc.at<double>(1, 0), tbc.at<double>(2, 0);

        setting::TBC = SE3d(Rbc_, tbc_);
    }

    
    
   // ros::init(argc, argv, "ygz_ros");
  //  ros::NodeHandle nh;
        message_filters::Subscriber<sensor_msgs::Image> left_sub(nh, left_topic, 2);
    message_filters::Subscriber<sensor_msgs::Image> right_sub(nh, right_topic, 2);
    
    
    ros::Subscriber imu_sub = nh.subscribe(imu_topic, 10, &ImageGrabber::FetchImuCallback, &igb);
    


    typedef message_filters::sync_policies::ApproximateTime<sensor_msgs::Image, sensor_msgs::Image> sync_pol;
    message_filters::Synchronizer<sync_pol> sync(sync_pol(10), left_sub,right_sub);
    sync.registerCallback(boost::bind(&ImageGrabber::FetchImageCallback,&igb,_1,_2));

    ros::spin();
    return 0;
  
}


 void ImageGrabber::FetchImuCallback(const sensor_msgs::Imu& imu)
{
  //LOG(INFO) << "fetching imu" << endl;
  IMUData t_imu(imu.angular_velocity.x,
		imu.angular_velocity.y,
		imu.angular_velocity.z,
		imu.linear_acceleration.x,
		imu.linear_acceleration.y,
		imu.linear_acceleration.z,
		imu.header.stamp.toSec() );
  cerr<<"IMU time stamp:"<<imu.header.stamp.toNSec()<<endl;
  
  temp_vimu.push_back(t_imu);
  //LOG(INFO) << "fecthing imu2" << endl;
}

void ImageGrabber::FetchImageCallback(const sensor_msgs::ImageConstPtr& msgLeft,const sensor_msgs::ImageConstPtr& msgRight)
{
    // Copy the ros image message to cv::Mat.
    cv_bridge::CvImageConstPtr cv_ptrLeft;
    try
    {
        cv_ptrLeft = cv_bridge::toCvShare(msgLeft);
    }
    catch (cv_bridge::Exception& e)
    {
        ROS_ERROR("cv_bridge exception: %s", e.what());
        return;
    }

    cv_bridge::CvImageConstPtr cv_ptrRight;
    try
    {
        cv_ptrRight = cv_bridge::toCvShare(msgRight);
    }
    catch (cv_bridge::Exception& e)
    {
        ROS_ERROR("cv_bridge exception: %s", e.what());
        return;
    }

        cv::Mat imLeft, imRight;
        cv::remap(cv_ptrLeft->image,imLeft,M1l,M2l,cv::INTER_LINEAR);
        cv::remap(cv_ptrRight->image,imRight,M1r,M2r,cv::INTER_LINEAR);
	//imLeft=cv_ptrLeft->image, imRight=cv_ptrRight->image;
	//toNSec得到的是浮点数toSec得到的是
	cerr<<"image time stamp:"<<cv_ptrLeft->header.stamp.toNSec()<<endl;
        mpSLAM->AddStereoIMU(imLeft,imRight,cv_ptrLeft->header.stamp.toSec(),temp_vimu);

    //清理上一状态的imu数据,方便接收下一批新的imu数据
    temp_vimu.clear();

}

CMakeLists文件的编写

要让上面源文件试用于该工程,那么必须对这个文件进行正确编写,这主要涉及到头文件、外部共享库等相关的调用,要能提供搜索路径
整上我是单独创建了一个ROS工作目录,在example目录下创建了ros文件夹,用于创建工作目录。类似与ORBSLAM2的ROS工作空间。

分目录下的CMakeLists

首先主目录下(/home/fei/devv/ygz-stereo-inertial-master)的CMakeLists文件是不需要进行改动的,然后为了能够让ROS工作空间中的源文件(.cpp)能够顺利使用该工程下定义的各个功能函数和类,需要给这个工程创建共享库。
路径:

  • /home/fei/devv/ygz-stereo-inertial-master/backend
  • /home/fei/devv/ygz-stereo-inertial-master/common
  • /home/fei/devv/ygz-stereo-inertial-master/cv
  • /home/fei/devv/ygz-stereo-inertial-master/system
  • /home/fei/devv/ygz-stereo-inertial-master/util
    这些文件夹下面都放置了当前工程需要用到的头文件和源文件,要单独为这些创建共享库,那么在对应路径下的CMakeLists文件下
    使用下面命令
add_library(ygz-system SHARED
        src/System.cpp
)

SHARED是生成共享库的关键字、ygz-system是该共享库的名称、 src/System.cpp只需要提供源文件。那么其它路径下同样是一样按照这种方式生成共享库

固定生成库的存放路径

set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/lib)

放在主目录下的lib文件夹下
在生成该共享库的时候,可能需要其它共享库(类似需要外部头文件一样),那么需要把一些库连接到当前生成库中

target_link_libraries( ygz-system 
    ygz-common ygz-cv ygz-util ygz-backend
)

最终在lib目录下

创建ROS-wrapper_第1张图片为了方便能更好的连接这些库,那么把他们整合到一起,用set命令,定义一个变量。

set(YGZ_LIBS ygz-common ygz-cv ygz-util ygz-backend ygz-system)
# 同样三方库也进行整合
set(THIRD_PARTY_LIBS
        ${OpenCV_LIBS}
        ${EIGEN3_LIBS}
        ${Pangolin_LIBRARIES}
        ${PROJECT_SOURCE_DIR}/Thirdparty/DBoW2/lib/libDBoW2.so
        ${PROJECT_SOURCE_DIR}/Thirdparty/fast/build/libfast.so
        ${PROJECT_SOURCE_DIR}/Thirdparty/g2o/lib/libg2o.so
        ${BLAS_LIBRARIES}
        ${LAPACK_LIBRARIES}
        ${GLOG_LIBRARY}
)

注意这里的YGZ_LIBS和THIRD_PARTY_LIBS只适合当前工作环境内使用,不如之后的ROS工作空间下不能再使用这个变量,需要重新声明
上面的这个库只是为了能够让该工程创建的可执行文件(非ROS节点)进行共享库的链接,如路径
/home/fei/devv/ygz-stereo-inertial-master/examples下的CMakeLists

add_executable(EurocStereo EurocStereo.cpp)
target_link_libraries(EurocStereo
        ${YGZ_LIBS}
        ${THIRD_PARTY_LIBS}
        )

add_executable(EurocStereoVIO EurocStereoVIO.cpp)
target_link_libraries(EurocStereoVIO
        ${YGZ_LIBS}
        ${THIRD_PARTY_LIBS}
        )

add_executable(Kitti Kitti.cpp)
target_link_libraries(Kitti
        ${YGZ_LIBS}
        ${THIRD_PARTY_LIBS}
        )

消除XXX.c:(.text+0x12): undefined reference to `XXX’ 的未定义声明的错误

完整CMakeLists

整合后的三方库使用体现在 ygz-util 共享库的生成上

target_link_libraries( ygz-util 
        ${THIRD_PARTY_LIBS}
)
路径/home/fei/devv/ygz-stereo-inertial-master/backend下的CMakeLists文件
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/lib)


add_library( ygz-backend SHARED
        src/BackendSlidingWindowG2O.cpp
        )

SET_TARGET_PROPERTIES(ygz-backend PROPERTIES OUTPUT_NAME "ygz-backend")
路径/home/fei/devv/ygz-stereo-inertial-master/common下的CMakeLists文件

set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/lib)
add_library(ygz-common SHARED
        src/IMUData.cpp
        src/Settings.cpp
        src/Frame.cpp
        src/MapPoint.cpp
        src/G2OTypes.cpp
        )

target_link_libraries(ygz-common
        ${THIRD_PARTY_LIBS}
        )

SET_TARGET_PROPERTIES(ygz-common PROPERTIES OUTPUT_NAME "ygz-common")
路径 /home/fei/devv/ygz-stereo-inertial-master/cv下的完整CMakeLists
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/lib)
add_library(ygz-cv SHARED
        src/ORBExtractor.cpp
        src/ORBMatcher.cpp
        src/Tracker.cpp
        src/Align.cpp
        src/LKFlow.cpp
        src/TrackerLK.cpp
        )

target_link_libraries(ygz-cv ygz-common ${THIRD_PARTY_LIBS})

SET_TARGET_PROPERTIES(ygz-cv PROPERTIES OUTPUT_NAME "ygz-cv")
路径/home/fei/devv/ygz-stereo-inertial-master/system下的CMakeLists
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/lib)
add_library(ygz-system SHARED
        src/System.cpp
)

target_link_libraries( ygz-system 
    ygz-common ygz-cv ygz-util ygz-backend
)

SET_TARGET_PROPERTIES(ygz-system  PROPERTIES OUTPUT_NAME "ygz-system")
路径/home/fei/devv/ygz-stereo-inertial-master/util下的CMakeLists
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/lib)
add_library( ygz-util SHARED
        src/EurocReader.cpp
        src/Viewer.cpp
)

target_link_libraries( ygz-util 
        ${THIRD_PARTY_LIBS}
)

SET_TARGET_PROPERTIES(ygz-util  PROPERTIES OUTPUT_NAME "ygz-util")

小结

这原工程是把一些头文件和源文件进行了不同文件夹的分类,那么需要都把这些路径通过CMakeLists告诉编译器,并且在生成共享库的时候我们这里是每一个子目录下进行单独生成,其实也可以在主目录下进行共享库的生成,只不过需要给清除路径
另一种方案是把所有的头文件和源文件都放在一个子目录下(类似于ORBSLAM2工程),

set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/lib)

add_library(${PROJECT_NAME} SHARED
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
src/InitKeyFrame.cc
src/SystemSetting.cc
)

这样生成库的时候不需要这么麻烦,但是为了不大幅度改动原工程结构,还是保留原结构

在主目录下的CMakeLists

该目录是主要提供外部三方库的搜索路径和分布在各个文件夹内的头文件的路径,解决以下的错误提示
XXX.cpp:1:19: error: XXX.h: 没有那个文件或目录

#三方库头文件的搜索路径
include_directories(
${PROJECT_SOURCE_DIR}
${PROJECT_SOURCE_DIR}/Thirdparty/fast/include
${PROJECT_SOURCE_DIR}/Thirdparty/DBoW2
${PROJECT_SOURCE_DIR}/Thirdparty/g2o/
${EIGEN3_INCLUDE_DIR}
${Pangolin_INCLUDE_DIRS}
${GLOG_INCLUDE_DIRS}
)
#内部头文件路径
include_directories(
        util/include
        cv/include
        common/include
        backend/include
        system/include
)

由于工程中还有其它的文件夹,并且存放着不同的源文件和头文件且这些文件夹中也都有属于自己的CMakeLists,那么需要让它们进行相互连接一起编译,需要在主目录下的CMakeLists文件声明文件夹

add_subdirectory(util)
add_subdirectory(common)
add_subdirectory(cv)
add_subdirectory(backend)
add_subdirectory(system)
add_subdirectory( examples )

完整的CMakeLists文件

cmake_minimum_required( VERSION 2.8 )
project(YGZ-Stereo)

# uncommont this to use release
set( CMAKE_BUILD_TYPE "Release" )

# make sure we use Release and warn otherwise
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
  message(STATUS "Setting build type to 'Release' as none was specified.")
  set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE)
  set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release"
    "MinSizeRel" "RelWithDebInfo")
endif()


if (NOT "${CMAKE_BUILD_TYPE}" STREQUAL "Release")
  message(WARNING "CMAKE_BUILD_TYPE not set to 'Release'. Performance may be terrible.")
else()
  message(STATUS "Building with build type '${CMAKE_BUILD_TYPE}', turn on the compiler optimization")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}     -Wall -march=native -O3 -Wno-reorder -pthread -fopenmp")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -march=native -O3 -pthread -fopenmp")
endif()

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/lib)

# 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()

# arm and intel cpu configurations
IF(DEFINED ENV{ARM_ARCHITECTURE})
  SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mfpu=neon -march=armv7-a")
ELSE()
  SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -msse -msse2 -march=native")
ENDIF()

LIST(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake-modules)

# thirdparty libs 
find_package(OpenCV 3.0 QUIET)
if(NOT OpenCV_FOUND)
    message(STATUS "Don't get Opencv 3.0, looking for OpenCV 2.4.")
   find_package(OpenCV 2.4 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)
find_package( Glog REQUIRED )

# to show 'include' in QtCreator
FILE(GLOB_RECURSE INC_ALL "*/*.h")
add_custom_target(headers SOURCES ${INC_ALL})

include_directories(
${PROJECT_SOURCE_DIR}
${PROJECT_SOURCE_DIR}/include
${PROJECT_SOURCE_DIR}/src
${PROJECT_SOURCE_DIR}/Thirdparty/fast/include
${PROJECT_SOURCE_DIR}/Thirdparty/DBoW2
${PROJECT_SOURCE_DIR}/Thirdparty/g2o/
${EIGEN3_INCLUDE_DIR}
${Pangolin_INCLUDE_DIRS}
${GLOG_INCLUDE_DIRS}
)


# thirdparty
set(THIRD_PARTY_LIBS
        ${OpenCV_LIBS}
        ${EIGEN3_LIBS}
        ${Pangolin_LIBRARIES}
        ${PROJECT_SOURCE_DIR}/Thirdparty/DBoW2/lib/libDBoW2.so
        ${PROJECT_SOURCE_DIR}/Thirdparty/fast/build/libfast.so
        ${PROJECT_SOURCE_DIR}/Thirdparty/g2o/lib/libg2o.so
        ${BLAS_LIBRARIES}
        ${LAPACK_LIBRARIES}
        ${GLOG_LIBRARY}
)

# subdirectories
include_directories(
        util/include
        cv/include
        common/include
        backend/include
        system/include
)


add_subdirectory(util)

add_subdirectory(common)

add_subdirectory(cv)

add_subdirectory(backend)

add_subdirectory(system)

set(YGZ_LIBS ygz-common ygz-cv ygz-util ygz-backend ygz-system)

# build examples
add_subdirectory( examples )

ROS目录下的CMakeLists

在ROS目录下需要配置CMakeLists文件,并且是针对ROS工程的

include($ENV{ROS_ROOT}/core/rosbuild/rosbuild.cmake)
rosbuild_init()

要在文件开头加上创建ROS空间的环境

需要为ros节点的.cpp源文件能够访问到当前工程的头文件,然后需要添加头文件搜索路径。这里解决的问题是找不到头文件的错误提示
error: xxx.h: 没有那个文件或目录

include_directories(
${PROJECT_SOURCE_DIR}
${PROJECT_SOURCE_DIR}/../../../
${PROJECT_SOURCE_DIR}/../../../include
${PROJECT_SOURCE_DIR}/../../../cv/include
${PROJECT_SOURCE_DIR}/../../../common/include
${PROJECT_SOURCE_DIR}/../../../util/include
${PROJECT_SOURCE_DIR}/../../../backend/include
${PROJECT_SOURCE_DIR}/../../../system/include
${Pangolin_INCLUDE_DIRS}
)

注意这里的${PROJECT_SOURCE_DIR}是当前CMakeLists所在的目录,是当前ROS工作空间的路径

添加上面的头文件的搜索路径的时候那么上面找不到头文件的提示已经消失了但是还是会出现另一个错误

xxx.cpp:(.text+0x12): undefined reference to `XXXX’

这需要在生成可执行文件的时候需要链接上所有需要的外部库(注意当前工程对于ROS空间来说都属于外部库),为了方便添加,我们先把所有的外部共享库整合到一个变量来表示

set(LIBS 
${OpenCV_LIBS} 
${EIGEN3_LIBS}
${Pangolin_LIBRARIES}
${PROJECT_SOURCE_DIR}/../../../Thirdparty/DBoW2/lib/libDBoW2.so
${PROJECT_SOURCE_DIR}/../../../Thirdparty/g2o/lib/libg2o.so

-lboost_system
)

set(YGZ_LIBS
${PROJECT_SOURCE_DIR}/../../../lib/libygz-backend.so
${PROJECT_SOURCE_DIR}/../../../lib/libygz-common.so
${PROJECT_SOURCE_DIR}/../../../lib/libygz-cv.so
${PROJECT_SOURCE_DIR}/../../../lib/libygz-util.so
${PROJECT_SOURCE_DIR}/../../../lib/libygz-system.so
)

这里的LIBS和YGZ_LIBS就是整合之后的外部共享库、YGZ_LIBS和之前虽然同名但是所代表的值是不一样的
通过

TARGET_LINK_LIBRARIES(target library1
 library2
...)

给可执行文件链接外部共享库,如:

rosbuild_add_executable(YGZ-StereoVIO-ROS
src/eurocSteteoVIO_ros.cpp
)

target_link_libraries(YGZ-StereoVIO-ROS
${LIBS}
${YGZ_LIBS}
)

注意rosbuild_add_executable是生成ROS可执行文件的语法,和add_executable一样的使用方法
这样外部共享库链接上了可执行文件之后就会把 undefined reference to `XXXX’ 这错误给消除

完整CMakeLists

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  REQUIRED)
find_package(Pangolin REQUIRED)

include_directories(
${PROJECT_SOURCE_DIR}
${PROJECT_SOURCE_DIR}/../../../
${PROJECT_SOURCE_DIR}/../../../include
${PROJECT_SOURCE_DIR}/../../../cv/include
${PROJECT_SOURCE_DIR}/../../../common/include
${PROJECT_SOURCE_DIR}/../../../util/include
${PROJECT_SOURCE_DIR}/../../../backend/include
${PROJECT_SOURCE_DIR}/../../../system/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
-lboost_system
)

set(YGZ_LIBS
${PROJECT_SOURCE_DIR}/../../../lib/libygz-backend.so
${PROJECT_SOURCE_DIR}/../../../lib/libygz-common.so
${PROJECT_SOURCE_DIR}/../../../lib/libygz-cv.so
${PROJECT_SOURCE_DIR}/../../../lib/libygz-util.so
${PROJECT_SOURCE_DIR}/../../../lib/libygz-system.so
)
# Node for monocular camera
rosbuild_add_executable(YGZ-StereoVIO-ROS
src/eurocSteteoVIO_ros.cpp
)

target_link_libraries(YGZ-StereoVIO-ROS
${LIBS}
${YGZ_LIBS}
)

rosbuild_add_executable(YGZ-Stereo-ROS
src/eurocSteteoVIO_ros.cpp
)

target_link_libraries(YGZ-Stereo-ROS
${LIBS}
${YGZ_LIBS}
)



你可能感兴趣的:(ROS学习)