ros2工程编译运行调试经验总结

以项目preprocess为例
1、CMakeLists.txt

cmake_minimum_required(VERSION 3.5)
project(preprocess)
set(CUDA_USE_STATIC_CUDA_RUNTIME ON)

if(NOT CMAKE_BUILD_TYPE OR CMAKE_BUILD_TYPE STREQUAL "")
   set(CMAKE_BUILD_TYPE Release)
endif()
if(NOT CMAKE_C_STANDARD)
    set(CMAKE_C_STANDARD 99)
endif()
if(NOT CMAKE_CXX_STANDARD)
    set(CMAKE_CXX_STANDARD 14)
endif()
if (CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
    add_compile_options(-Wall -Wextra -Wpedantic -Wnon-virtual-dtor -Woverloaded-virtual)
endif ()

find_package(ament_cmake REQUIRED)

find_package(rclcpp REQUIRED)
find_package(rosidl_default_generators REQUIRED)
find_package(tf2 REQUIRED)
find_package(tf2_ros REQUIRED)

find_package(sensor_msgs REQUIRED)
find_package(std_msgs REQUIRED)

find_package(common REQUIRED)
find_package(PCL REQUIRED)
find_package(OpenCV REQUIRED)
find_package(Boost REQUIRED COMPONENTS thread system)

file(GLOB_RECURSE ALL_COMMON_SRCS "src/*.cc" "src/preprocess/*.cc")
add_executable(preprocess_node ${ALL_COMMON_SRCS} )

ament_target_dependencies(preprocess_node
    rclcpp
    rosidl_default_generators
    tf2
    tf2_ros
    sensor_msgs
    std_msgs
    common
    PCL
    OpenCV
    Boost
)

target_include_directories(preprocess_node  PUBLIC  ${EIGEN3_INCLUDE_DIR} ${Boost_INCLUDE_DIR}  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
        $<INSTALL_INTERFACE:include>)

install(DIRECTORY rviz_cfg DESTINATION share/${PROJECT_NAME})
# 会将config文件夹安装到install/${PROJECT_NAME}/share/${PROJECT_NAME}路径下
install(DIRECTORY config DESTINATION share/${PROJECT_NAME})

# 注册 导出库文件
# 会将preprocess_node安装到install/${PROJECT_NAME}/lib/${PROJECT_NAME}路径下
# 为了使运行时可以被找到
install(
  TARGETS preprocess_node
  EXPORT preprocess_node
  ARCHIVE DESTINATION lib
  LIBRARY DESTINATION lib
  RUNTIME DESTINATION bin
  DESTINATION lib/${PROJECT_NAME}
)

# 注册 导出头文件
# 会将include下的头文件安装到install/${PROJECT_NAME}/include路径下
install(DIRECTORY include/
        DESTINATION include
)
# 必不可少
ament_package()

2、package.xml

<?xml version="1.0"?>
<package  format="2">
  <name>preprocess</name>
  <version>3.3.20190304</version>

  <description>
    This is a ROS implementation of objects detect and track base lidar by WAYTOUS
  </description>

  <maintainer email="[email protected]">dejiang meng</maintainer>

  <license>1.0</license>

  <author email="[email protected]">dejiang meng</author>
  
  <buildtool_depend>ament_cmake</buildtool_depend>

  <depend>common</depend>
  <depend>geometry_msgs</depend>
  <depend>message_runtime</depend>
  <depend>rclcpp</depend>
  <depend>sensor_msgs</depend>
  <depend>std_msgs</depend>

  <export>
     #为了使运行时可以被找到
     <build_type>ament_cmake</build_type>
  </export>
</package>

3、启动文件run.launch.py

import os
import launch
from ament_index_python.packages import get_package_share_directory
from launch.substitutions import EnvironmentVariable
from launch.substitutions import LaunchConfiguration
from launch import LaunchDescription
from launch_ros.actions import Node
from launch.actions import DeclareLaunchArgument

def generate_launch_description():
	  #获取参数文件绝对路径
    preprocess_para_dir = os.path.join(get_package_share_directory('preprocess'), 'config/shendongL4', 'system.yaml')

    return LaunchDescription([
        Node(
        	   #包名
            package='preprocess',
            #节点名
            executable='preprocess_node',
            #给包起的名称
            name='preprocess',
            #可以打印到终端
            output='screen',
            #从yaml中读取参数
            parameters=[preprocess_para_dir],
        )
    ])

4、参数配置文件system.yaml

#给节点起的名字,对应run.launch.py中的name
preprocess:
    ros__parameters:
      rawpointcloudnum: 4
      rawpointcloud0: "/livox_left/points"
      rawpointcloud1: "/livox_right/points"
      rawpointcloud2: "/rs_back/points"
      rawpointcloud3: "/rslidar_points"
      rawpointcloud4: "/no_points"

      rawpointcloudframeid0: "lidar0"
      rawpointcloudframeid1: "lidar1"
      rawpointcloudframeid2: "lidar2"
      rawpointcloudframeid3: "lidar3"
      rawpointcloudframeid4: "lidar4"

      parameterfoldername: "shendongL4"

      calibpointcloudall: "/calib_pointcloud_all"
      calibpointcloudsingle0: "/calib_pointcloud_single0"
      calibpointcloudsingle1: "/calib_pointcloud_single1"
      calibpointcloudsingle2: "/calib_pointcloud_single2"
      calibpointcloudsingle3: "/calib_pointcloud_single3"
      calibpointcloudsingle4: "/calib_pointcloud_single4"

      calibpointcloudframeid: "Lidar"

你可能感兴趣的:(ROS,c++)