「ROSPointCloud」对点云进⾏位姿态变换功能包依赖⽂件
劝学一词多义<?xml version="1.0"?>
<package format="2">
<name>pub_sub_pcl_topic_pkg</name>
<version>0.0.0</version>
<description>The pub_sub_pcl_topic_pkg package</description>
<maintainer email="do">q</maintainer>
<licen>TODO</licen>
<buildtool_depend>catkin</buildtool_depend>
<build_depend>message_generation</build_depend>
<build_depend>pcl_conversions</build_depend>
<build_depend>pcl_ros</build_depend>
<build_depend>roscpp</build_depend>
MXM接口<build_depend>rospy</build_depend>古诗五言
<build_depend>nsor_msgs</build_depend>
<build_depend>std_msgs</build_depend>
芈月传主题曲<build_export_depend>pcl_conversions</build_export_depend>
<build_export_depend>pcl_ros</build_export_depend>
<build_export_depend>roscpp</build_export_depend>
<build_export_depend>rospy</build_export_depend>
<build_export_depend>nsor_msgs</build_export_depend>
<build_export_depend>std_msgs</build_export_depend>
<exec_depend>pcl_conversions</exec_depend>
<exec_depend>pcl_ros</exec_depend>
<exec_depend>roscpp</exec_depend>
<exec_depend>rospy</exec_depend>
<exec_depend>nsor_msgs</exec_depend>
<exec_depend>std_msgs</exec_depend>上海朱家角古镇
<export>
</export>
</package>
可运⾏代码
#include<ros/ros.h>
#include<nsor_msgs/PointCloud2.h>
#include<pcl/point_types.h>
#include<pcl_conversions/pcl_conversions.h>
#include<pcl/common/transforms.h>
#include<pcl/visualization/cloud_viewer.h>
#include<pcl/filters/voxel_grid.h>
class pcl_sub
{
private:
ros::NodeHandle n;
ros::Subscriber subCloud;
ros::Publisher pubCloud;
// 接收到的点云消息
nsor_msgs::PointCloud2 msg;
// 等待发布的点云消息
nsor_msgs::PointCloud2 adjust_msg;
// 建⽴了⼀个pcl的点云,⽤于完成点云的中间处理过程
pcl::PointCloud<pcl::PointXYZRGB> adjust_pcl;
public:
// blog.csdn/u014587147/article/details/75647002 构造函数
pcl_sub():n("~")
{
/
/ 接收点云数据,进⼊回调函数 getcloud() /point_cloud_topic 为订阅的点云话题名
// 1 为待处理信息队列⼤⼩,⼀次只处理⼀个消息
// &pcl_sub::getcloud 调⽤的函数指针,即回调函数。
// this 回调函数所在的类
subCloud = n.subscribe<nsor_msgs::PointCloud2>("/point_cloud_topic",1,&pcl_sub::getcloud,this);
// 发布位姿变换后的点云/adjusted_cloud
pubCloud = n.adverti<nsor_msgs::PointCloud2>("/adjustd_cloud",1);
}
// 回调函数
助残日void getcloud(const nsor_msgs::PointCloud2ConstPtr &larCloudMsg)
{
// 放在这⾥是因为,每次都需要重新初始化
pcl::PointCloud<pcl::PointXYZRGB>::Ptr adjust_pcl_ptr(new pcl::PointCloud<pcl::PointXYZRGB>);
// 把msg消息转化为点云
pcl::fromROSMsg(*larCloudMsg,*adjust_pcl_ptr);
// 定义⼀个旋转矩阵虽然称为3d,实质上是4x4的矩阵(旋转R+平移t)
Eigen::Isometry3d T = Eigen::Isometry3d::Identity();
Eigen::AngleAxisd rotationVector(M_PI/4,Eigen::Vector3d(0,0,1));
Eigen::Matrix3d rotationMatrix=Eigen::Matrix3d::Identity();
RotationMatrix();
//旋转部分赋值
T.linear()= rotationMatrix;
/
/平移部分赋值
// 执⾏变换,并将结果保存在新创建的 transformed_cloud 中
pcl::PointCloud<pcl::PointXYZRGB>::Ptr transformed_cloud(new pcl::PointCloud<pcl::PointXYZRGB>());
pcl::transformPointCloud(*adjust_pcl_ptr,*transformed_cloud, T.matrix());
adjust_pcl =*transformed_cloud;
for(int i =0; i < adjust_pcl.points.size(); i++)
//把接收到的点云位置不变,颜⾊全部变为红⾊
{
adjust_pcl.points[i].r =255;
adjust_pcl.points[i].g =0;
adjust_pcl.points[i].b =0;
}
// 将点云转化为消息才能发布
pcl::toROSMsg(adjust_pcl, adjust_msg);
李白长诗
// 发布调整之后的点云数据主题为/adjustd_cloud
pubCloud.publish(adjust_msg);
}
// 析构函数
~pcl_sub(){}
};
int main(int argc,char**argv)
{
// 初始化了⼀个节点,名字为colored
ros::init(argc, argv,"point_cloud_transform");
// 创建⼀个对象,回调函数放在了构造函数中,所以创建对象的时候会顺序调⽤构造函数,回调函数,然后点云的接收、转换、发布就完成了。 pcl_sub ps;
ros::spin();
return0;
}
编译⽂件
cmake_minimum_required(VERSION 3.0.2)
project(pub_sub_pcl_topic_pkg)
find_package(catkin REQUIRED COMPONENTS
message_generation
pcl_conversions
pcl_ros
roscpp
rospy
nsor_msgs
std_msgs
)
catkin_package()
include_directories(${catkin_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
add_executable(creat_point_cloud_pub src/creat_point_cloud_pub.cpp)
target_link_libraries(creat_point_cloud_pub ${PCL_LIBRARIES} ${catkin_LIBRARIES})
add_executable(point_cloud_transform src/point_cloud_transform.cpp)
target_link_libraries(point_cloud_transform ${PCL_LIBRARIES} ${catkin_LIBRARIES})运⾏效果
catkin_ws$ source devel/tup.bash
披肩发怎么扎好看catkin_ws$ rosrun pub_sub_pcl_topic_pkg creat_point_cloud_pub
catkin_ws$ source devel/tup.bash
catkin_ws$ rosrun pub_sub_pcl_topic_pkg point_cloud_transform