c++ Cmake工程(4) opevslam ros工程模式

MKT-porter發表於2024-09-01

關鍵點

1 openvslam先編譯安裝到 usr/local/include/ 然後再是編譯ros節點。ros節點檔案直接從系統目錄找的"openvslam/system.h"。 當然也可以手動指定

#include <ros/ros.h>
//#include "openvslam/system.h" // 優先從 usr/local/include/下查詢 然後才是自己指定的目錄

  

2 相對路徑

# 自己寫的依賴庫檔案目錄
message(STATUS "當前CMakeLists路徑: ${PROJECT_SOURCE_DIR}")
set(VSLAM_ROOT "${PROJECT_SOURCE_DIR}/../../../src" )
message(STATUS "自己寫的依賴庫檔案目錄: ${VSLAM_ROOT}")

include_directories( 
  ${catkin_INCLUDE_DIRS}
  ${OpenCV_INCLUDE_DIRS}
  ${Boost_INCLUDE_DIRS}
  "/usr/local/include/eigen3"
  ${VSLAM_ROOT} #自己寫的依賴庫檔案目錄
)

從而可以直接使用

#include "vslam/system.h" // 等同於 ${VSLAM_ROOT}/vslam/system.h

  

CMakeLists.txt

cmake_minimum_required(VERSION 3.0.2)
project(test_node)

find_package(catkin REQUIRED COMPONENTS
  roscpp
  geometry_msgs
  sensor_msgs
  cv_bridge
  message_filters # 訊息同步
  image_transport
)

find_package(OpenCV REQUIRED)
find_package(Boost REQUIRED COMPONENTS filesystem)
find_package(Eigen3 REQUIRED)

catkin_package(
  CATKIN_DEPENDS roscpp geometry_msgs sensor_msgs cv_bridge
  DEPENDS Boost
)


# 自己寫的依賴庫檔案目錄
message(STATUS "當前CMakeLists路徑: ${PROJECT_SOURCE_DIR}")
set(VSLAM_ROOT "${PROJECT_SOURCE_DIR}/../../../src" )
message(STATUS "自己寫的依賴庫檔案目錄: ${VSLAM_ROOT}")

include_directories( 
  ${catkin_INCLUDE_DIRS}
  ${OpenCV_INCLUDE_DIRS}
  ${Boost_INCLUDE_DIRS}
  "/usr/local/include/eigen3"
  ${VSLAM_ROOT} #自己寫的依賴庫檔案目錄
)

# 編譯自己寫的依賴庫
add_library(openvslam_system
   ${VSLAM_ROOT}/vslam/system.cc
)

# 編譯釋出節點
add_executable(run_slam src/run_slam.cc)
target_link_libraries(run_slam 
  ${catkin_LIBRARIES}
  ${OpenCV_LIBRARIES}
  ${Boost_LIBRARIES}
  openvslam_system)

  

package.xml

<?xml version="1.0"?>
<package format="2">
  <name>test_node</name>
  <version>0.0.1</version>
  <description>
    A package to publish and subscribe to images and GPS data using ROS.
  </description>

  <!-- Maintainer of the package -->
  <maintainer email="your_email@example.com">Your Name</maintainer>

  <!-- License of the package -->
  <license>MIT</license>

  <!-- Build tool required to build this package -->
  <buildtool_depend>catkin</buildtool_depend>

  <!-- Dependencies of the package during build and runtime -->
  <build_depend>roscpp</build_depend>
  <build_depend>sensor_msgs</build_depend>
  <build_depend>cv_bridge</build_depend>
  <build_depend>eigen</build_depend>
  <build_depend>geometry_msgs</build_depend>
  <build_depend>message_filters</build_depend>
  <build_depend>image_transport</build_depend>

  
  
  <exec_depend>roscpp</exec_depend>
  <exec_depend>sensor_msgs</exec_depend>
  <exec_depend>cv_bridge</exec_depend>
  <exec_depend>eigen</exec_depend>
  <exec_depend>geometry_msgs</exec_depend>
  <exec_depend>message_filters</exec_depend>
  <exec_depend>image_transport</exec_depend>

  <!-- Declare additional dependencies required for building this package -->
  <build_export_depend>roscpp</build_export_depend>
  <build_export_depend>sensor_msgs</build_export_depend>
  <build_export_depend>cv_bridge</build_export_depend>
  <build_export_depend>eigen</build_export_depend>
  <build_export_depend>geometry_msgs</build_export_depend>
  <build_export_depend>message_filters</build_export_depend>
  <build_export_depend>image_transport</build_export_depend>

  <!-- Export information, can be used by other packages -->
  <export>
    <!-- Export any specific information here -->
  </export>
</package>

  

run_slam.cc

#include <ros/ros.h>
//#include "openvslam/system.h" // 優先從 usr/local/include/下查詢 然後才是自己指定的目錄
#include "vslam/system.h" // 等同於 ${VSLAM_ROOT}/vslam/system.h


int main(int argc, char** argv) {
    ros::init(argc, argv, "run_slam_node");
    ros::NodeHandle nh;

    // Create an instance of SLAMSystem
    SLAMSystem slam_system;
    slam_system.initialize();

    ros::spin();  // Enter a loop, waiting for callbacks
    return 0;
}

  

依賴庫

system.h

#ifndef OPENVSLAM_SYSTEM_H
#define OPENVSLAM_SYSTEM_H

#include <iostream>


class SLAMSystem {
public:
    SLAMSystem();
    void initialize();
};

#endif // OPENVSLAM_SYSTEM_H

  

system.cc

#include "system.h"

SLAMSystem::SLAMSystem() {
    // Constructor implementation
}

void SLAMSystem::initialize() {
    std::cout << "SLAMSystem initialized." << std::endl;
}

  

相關文章