栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

ros2 点云话题订阅 c++

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 
#include 
#include 
#include 

typedef pcl::PointXYZRGB PointT;
typedef pcl::PointCloud PointCloud;

class PclSub : public rclcpp::Node
{

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

private:
    rclcpp::Subscription::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");

    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();
      }

转载请表明出处

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/928684.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号