ros2 点云话题订阅 c++

foxy系统

工作空间站的创建

mkdir -p ros2_ws/src

功能包创建

cd src 

ros2 pkg create --build-type ament_cmake ros2pcl --dependencies rclcpp 

subscription_pcl.cpp 创建 移动到功能包src下

touch subscription_pcl.cpp

将以下内容复制进去,修改为自己所需

#include "rclcpp/rclcpp.hpp"
#include "sensor_msgs/msg/point_cloud2.hpp"
#include <iostream>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>

typedef pcl::PointXYZRGB PointT;
typedef pcl::PointCloud<PointT> PointCloud;

class PclSub : public rclcpp::Node
{

public:
    PclSub(std::string name)
        : Node(name)
    {
        using std::placeholders::_1;
        sub_novel = this->create_subscription<sensor_msgs::msg::PointCloud2>("//此处为所需订阅的点云话题", 1, std::bind(&PclSub::topic_callback, this, std::placeholders::_1));
    }

private:
    rclcpp::Subscription<sensor_msgs::msg::PointCloud2>::SharedPtr sub_novel;
    void topic_callback(const sensor_msgs::msg::PointCloud2::SharedPtr msg)
    {
       //此处为需要对点云进行处理的操作
      }
};

int main(int argc, char **argv)
{
    rclcpp::init(argc, argv);

    auto node = std::make_shared<PclSub>("pclsub");

    rclcpp::spin(node);
 
    //rclcpp::spin(node);
    rclcpp::shutdown();
    return 0;
   
}

 

Cmakelists.txt

cmake_minimum_required(VERSION 3.5)
project(ros2pcl_test)

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

find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(sensor_msgs REQUIRED)
find_package(PCL REQUIRED)

if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)

  ament_lint_auto_find_test_dependencies()
endif()

add_executable(ros2pcl_test_sub 
  src/subscription_pcl.cpp 
)
ament_target_dependencies(ros2pcl_test_sub 
  rclcpp 
  sensor_msgs
  PCL
)
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
target_link_libraries(ros2pcl_test_sub ${PCL_LIBRARIES})
install(TARGETS
  ros2pcl_test_sub 
  DESTINATION lib/${PROJECT_NAME})

ament_package()

然后返回到ros2_ws目录下编译

colcon build

更新环境

. install/setup.bash

ok,生成文件 

ros2 run 运行 

补充一个定时器

      rclcpp::Rate rate(0.1);
      while (rclcpp::ok()) 
      {     
       rate.sleep();
      }

转载请表明出处

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值