Skip to content

Commit b87a98f

Browse files
authored
Merge pull request #19 from HuaYuXiao/dev-pcl
[Update] Transfer PointCloud with `pcl_ros::transformPointCloud`
2 parents acdc2d1 + 41266c8 commit b87a98f

File tree

3 files changed

+28
-24
lines changed

3 files changed

+28
-24
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ find_package(catkin REQUIRED COMPONENTS
1919
visualization_msgs
2020
prometheus_msgs
2121
message_filters
22+
tf
2223
)
2324

2425
catkin_package(

package.xml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?xml version="1.0"?>
22
<package format="2">
33
<name>uav_octomapping</name>
4-
<version>2.0.0</version>
4+
<version>2.1.0</version>
55
<description>
6-
A ROS package to build 3D octomap with 2D lidar
6+
A ROS package to build octomap with LiDAR and D435i
77
</description>
88
<author email="[email protected]">Eason Yi</author>
99
<maintainer email="[email protected]">Eason Hua</maintainer>
@@ -22,6 +22,7 @@
2222
<build_depend>pcl_ros</build_depend>
2323
<build_depend>octomap</build_depend>
2424
<build_depend>message_filters</build_depend>
25+
<build_depend>tf</build_depend>
2526

2627
<build_export_depend>roscpp</build_export_depend>
2728
<build_export_depend>rospy</build_export_depend>
@@ -33,6 +34,7 @@
3334
<build_export_depend>pcl_ros</build_export_depend>
3435
<build_export_depend>octomap</build_export_depend>
3536
<build_export_depend>message_filters</build_export_depend>
37+
<build_export_depend>tf</build_export_depend>
3638

3739
<exec_depend>roscpp</exec_depend>
3840
<exec_depend>rospy</exec_depend>
@@ -44,6 +46,7 @@
4446
<exec_depend>prometheus_msgs</exec_depend>
4547
<exec_depend>pcl_ros</exec_depend>
4648
<exec_depend>message_filters</exec_depend>
49+
<exec_depend>tf</exec_depend>
4750

4851
<export>
4952
<!-- Other tools can request additional information be placed here -->

src/merge_pcl/pcl_merge.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
#include <pcl/point_types.h>
66
#include <pcl_conversions/pcl_conversions.h>
77
#include <exception>
8+
#include <tf/transform_listener.h>
9+
#include <pcl_ros/transforms.h>
10+
#include <pcl/point_types.h>
11+
#include <pcl/kdtree/kdtree_flann.h>
12+
#include <geometry_msgs/TransformStamped.h>
813

914
using namespace std;
1015

@@ -23,71 +28,66 @@ class PointCloudMerger {
2328
private:
2429
ros::NodeHandle nh_;
2530
ros::Subscriber LiDAR_sub, D435i_sub;
31+
tf::TransformListener listener_LiDAR, listener_D435i;
2632
ros::Publisher merged_pub_;
2733
pcl::PointCloud<pcl::PointXYZ>::Ptr LiDAR_pcl, D435i_pcl;
34+
tf::StampedTransform transform_LiDAR, transform_D435i;
2835
};
2936

30-
PointCloudMerger::PointCloudMerger() : nh_("~") {
37+
PointCloudMerger::PointCloudMerger() : nh_("~"){
3138
LiDAR_sub = nh_.subscribe("/prometheus/sensors/3Dlidar_scan", 1, &PointCloudMerger::LiDAR_cb, this);
3239
D435i_sub = nh_.subscribe("/camera/depth/color/points", 1, &PointCloudMerger::D435i_cb, this);
3340
merged_pub_ = nh_.advertise<sensor_msgs::PointCloud2>("/prometheus/merged_pcl", 1);
3441

3542
LiDAR_pcl.reset(new pcl::PointCloud<pcl::PointXYZ>());
3643
D435i_pcl.reset(new pcl::PointCloud<pcl::PointXYZ>());
3744

45+
listener_LiDAR.waitForTransform("base_link", "3Dlidar_link", ros::Time(0), ros::Duration(0.05));
46+
listener_D435i.waitForTransform("base_link", "D435i::camera_depth_frame", ros::Time(0), ros::Duration(0.05));
47+
3848
// 设置cout的精度为小数点后两位
3949
std::cout << std::fixed << std::setprecision(2);
4050

4151
std::cout << "[mapping] merge_pcl initialized!" << std::endl;
4252
}
4353

4454
void PointCloudMerger::LiDAR_cb(const sensor_msgs::PointCloud2ConstPtr& msg) {
45-
try {
46-
cout << "LiDAR received!" << endl;
55+
cout << "LiDAR received!" << endl;
4756

4857
pcl::fromROSMsg(*msg, *LiDAR_pcl);
58+
// 转换LiDAR点云到base_link坐标系
59+
pcl_ros::transformPointCloud("base_link", *LiDAR_pcl, *LiDAR_pcl, listener_LiDAR);
60+
4961
mergePointClouds();
50-
} catch (const std::exception& e) {
51-
ROS_ERROR_STREAM("Exception in LiDAR_cb: " << e.what());
52-
}
5362
}
5463

5564
void PointCloudMerger::D435i_cb(const sensor_msgs::PointCloud2ConstPtr& msg) {
56-
try {
5765
cout << "D435i received!" << endl;
5866

5967
pcl::fromROSMsg(*msg, *D435i_pcl);
68+
// 转换D435i点云到base_link坐标系
69+
pcl_ros::transformPointCloud("base_link", *D435i_pcl, *D435i_pcl, listener_D435i);
70+
6071
mergePointClouds();
61-
} catch (const std::exception& e) {
62-
ROS_ERROR_STREAM("Exception in D435i_cb: " << e.what());
63-
}
6472
}
6573

6674
void PointCloudMerger::mergePointClouds() {
67-
try {
68-
cout << "Merging point clouds!" << endl;
69-
7075
pcl::PointCloud<pcl::PointXYZ>::Ptr merged_pcl;
7176
merged_pcl.reset(new pcl::PointCloud<pcl::PointXYZ>());
7277

73-
// merged_pcl->points.reserve(LiDAR_pcl->size() + D435i_pcl->size());
74-
merged_pcl->header = LiDAR_pcl->header;
75-
76-
cout << merged_pcl->header << endl;
78+
merged_pcl->header.seq = LiDAR_pcl->header.seq;
79+
merged_pcl->header.stamp = LiDAR_pcl->header.stamp;
80+
merged_pcl->header.frame_id = "base_link";
7781

7882
*merged_pcl += *LiDAR_pcl;
7983
*merged_pcl += *D435i_pcl;
8084

8185
sensor_msgs::PointCloud2 merged_cloud_msg;
8286
pcl::toROSMsg(*merged_pcl, merged_cloud_msg);
8387

84-
merged_cloud_msg.header.frame_id = "map";
8588
merged_pub_.publish(merged_cloud_msg);
8689

8790
cout << "Merged pcl published!" << endl;
88-
} catch (const std::exception& e) {
89-
ROS_ERROR_STREAM("Exception in mergePointClouds: " << e.what());
90-
}
9191
}
9292

9393
int main(int argc, char** argv) {
@@ -96,4 +96,4 @@ int main(int argc, char** argv) {
9696

9797
ros::spin();
9898
return 0;
99-
}
99+
}

0 commit comments

Comments
 (0)