欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ros学习之topic实例4

程序员文章站 2022-03-05 16:20:30
...

之前都是发布点云,这次的示例是发布之后并处理一下,将点云的颜色改变之后再重新发布出云,实际在slam中使用时这种方式非常多,比如lio-sam里面就有.

代码如下


>>#include <pcl_conversions/pcl_conversions.h>
>>#include <sensor_msgs/PointCloud2.h>
   
>>ros::Subscriber subCloud;   //用于接收点云
>>ros::Publisher pubCloud;    //用于发布新的点云
   
>>void getcloud(const sensor_msgs::PointCloud2ConstPtr& laserScanMsg){
>>    pcl::PointCloud<pcl::PointXYZRGB>::Ptr process_pcl_ptr(new pcl::PointCloud<pcl::PointXYZRGB>);   //因为每次都需要重新初始化
>>    sensor_msgs::PointCloud2 process_msg;  //等待发送的点云消息
>>    pcl::fromROSMsg(*laserScanMsg, *process_pcl_ptr);  //把msg消息转化为点云
>>    pcl::PointCloud<pcl::PointXYZRGB> process_pcl;   //建立了一个pcl的点云,作为中间过程
>>    process_pcl = *process_pcl_ptr;  
>>    for (int i = 0; i < process_pcl.points.size(); i++)
      {
>>      process_pcl.points[i].r = 0;
>>      process_pcl.points[i].g = 255;
>>      process_pcl.points[i].b = 0;
   
      }
>>    pcl::toROSMsg(process_pcl, process_msg);  //将点云转化为消息
      pubCloud.publish(process_msg); //发布处理之后的点云数据,主题为/processed_cloud
  }
    
   
  int main(int argc, char** argv){
      ros::init(argc, argv, "colored");  //初始化了一个节点,名字为colored
      ros::NodeHandle nh;
      //subCloud = nh.subscribe<sensor_msgs::PointCloud2>("/color_cloud", 1, &getcloud); //接收点云数据,进入回调函数getcloud()
      subCloud = nh.subscribe<sensor_msgs::PointCloud2>("/color_cloud", 1, getcloud); //接收点云数据,进入回调函数getcloud()
      pubCloud = nh.advertise<sensor_msgs::PointCloud2>("/processed_cloud_green", 1000);  //建立了一个发布器,主题是/adjusted_cloud,方便之后发布调整后的点云
   
      ros::spin();
      return 0;
  }

这里之所以会在最上面声明pubCloud和subCloud,主要是因为要在callback函数中进行调用。
还有一种方式就是直接用类来实现。会在下个blog中进行介绍。

这次的cmake如下

cmake_minimum_required(VERSION 2.6 FATAL_ERROR)
project(pcl_process)
 
set(PACKAGE_DEPENDENCIES
      roscpp
        sensor_msgs
          pcl_ros
            pcl_conversions
              std_srvs
                message_generation 
                  std_msgs
                  )   
 
find_package(PCL 1.3 REQUIRED COMPONENTS common io roscpp rospy std_msgs)
find_package(catkin REQUIRED COMPONENTS ${PACKAGE_DEPENDENCIES})
 
catkin_package(
#  INCLUDE_DIRS include
#  LIBRARIES publish_subscribe_demo
  CATKIN_DEPENDS  roscpp std_msgs
#  DEPENDS system_lib
)
 
include_directories(include ${catkin_INCLUDE_DIRS} ${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
add_executable(pcl_process src/pcl_process.cpp)
target_link_libraries(pcl_process ${PCL_LIBRARIES}  ${catkin_LIBRARIES} )                                         

在catkin_ws中的编译命令如下

catkin_make -DCATKIN_WHITELIST_PACKAGES="pcl_process"