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

ros学习之topic实例5

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

接上次的实例4,这次就用类来实现,接收,处理,并重新发布

先创建包

 catkin_create_pkg pcl_process_class roscpp std_msgs

在对应的src下面写入 pcl_process_class.cpp 代码如下


>>#include <ros/ros.h>
>>#include <pcl/point_types.h>
>>#include <pcl_conversions/pcl_conversions.h>
>>#include <sensor_msgs/PointCloud2.h>
   
  class pcl_sub_process_pub{
      private:
>>        ros::NodeHandle nh;
>>        ros::Subscriber subCloud;
>>        ros::Publisher pubCloud;
>>        sensor_msgs::PointCloud2 process_msg;
>>        pcl::PointCloud<pcl::PointXYZRGB> process_pcl;
   
      public:
          pcl_sub_process_pub():
              nh("~"){
>>            subCloud = nh.subscribe<sensor_msgs::PointCloud2>("/color_cloud", 1, &pcl_sub_process_pub::getcloud, this);
>>            pubCloud = nh.advertise<sensor_msgs::PointCloud2>("/processed_cloud_red", 1000);
   
          }
   
          //callback
>>        void getcloud(const sensor_msgs::PointCloud2ConstPtr& laserScanMsg){
  
>>            pcl::PointCloud<pcl::PointXYZRGB>::Ptr process_pcl_ptr(new pcl::PointCloud<pcl::PointXYZRGB>);
   
              //把msg给到pcl
>>            pcl::fromROSMsg(*laserScanMsg, *process_pcl_ptr);
>>            process_pcl = *process_pcl_ptr;
   
              //处理点云再发布出去.
              for (int i=0; i<process_pcl.points.size(); i++){
                  process_pcl.points[i].r = 255;
                  process_pcl.points[i].g = 0;
                  process_pcl.points[i].b = 0;
              }
              //重新转成msg发布出去
>>            pcl::toROSMsg(process_pcl, process_msg);
              pubCloud.publish(process_msg);
          }
                                                                                                                                                                                                                                                              
          ~pcl_sub_process_pub(){}
  };
   
  int main(int argc, char** argv){
>>    ros::init(argc, argv, "change_color");
      pcl_sub_process_pub ps;
>>    ros::spin();
      return 0;
  }
                                                        

CMake代如下

cmake_minimum_required(VERSION 2.6 FATAL_ERROR)
project(pcl_process_class)
 
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_class src/pcl_process_class.cpp)
target_link_libraries(pcl_process_class ${PCL_LIBRARIES}  ${catkin_LIBRARIES} )


编译如下


catkin_make -DCATKIN_WHITELIST_PACKAGES="pcl_process_class"

开多个tmux窗口分别运行

roscore
rosrun pub_pcl pub_pcl
rosrun pcl_process_class pcl_process_class
rosrun pcl_rpocess pcl_process
rosrun rviz rviz

就可以看到原始点云,以及分别改成绿色和红色的点云.