Skip to content

Commit 8a17324

Browse files
committed
fix(voxel_based_compare_map): temporary fix pointcloud transform lookup_time
Signed-off-by: badai-nguyen <dai.nguyen@tier4.jp>
1 parent 660ae1a commit 8a17324

File tree

3 files changed

+78
-5
lines changed
  • perception/autoware_compare_map_segmentation/src/voxel_based_compare_map_filter
  • sensing/autoware_pointcloud_preprocessor/include/autoware/pointcloud_preprocessor

3 files changed

+78
-5
lines changed

perception/autoware_compare_map_segmentation/src/voxel_based_compare_map_filter/node.cpp

+64-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@
2020
#include <pcl/kdtree/kdtree_flann.h>
2121
#include <pcl/search/kdtree.h>
2222
#include <pcl/segmentation/segment_differences.h>
23-
23+
#ifdef ROS_DISTRO_GALACTIC
24+
#include <tf2_geometry_msgs/tf2_geometry_msgs.h>
25+
#include <tf2_sensor_msgs/tf2_sensor_msgs.h>
26+
#else
27+
#include <tf2_geometry_msgs/tf2_geometry_msgs.hpp>
28+
#include <tf2_sensor_msgs/tf2_sensor_msgs.hpp>
29+
#endif
2430
#include <memory>
2531
#include <string>
2632

@@ -29,7 +35,9 @@ namespace autoware::compare_map_segmentation
2935

3036
VoxelBasedCompareMapFilterComponent::VoxelBasedCompareMapFilterComponent(
3137
const rclcpp::NodeOptions & options)
32-
: Filter("VoxelBasedCompareMapFilter", options)
38+
: Filter("VoxelBasedCompareMapFilter", options),
39+
tf_buffer_(this->get_clock()),
40+
tf_listener_(tf_buffer_)
3341
{
3442
// initialize debug tool
3543
{
@@ -62,6 +70,60 @@ VoxelBasedCompareMapFilterComponent::VoxelBasedCompareMapFilterComponent(
6270
RCLCPP_INFO(this->get_logger(), "tf_map_input_frame: %s", tf_input_frame_.c_str());
6371
}
6472

73+
// TODO(badai-nguyen): Temporary Implementation: Delete this override function when autoware_utils
74+
// refactor (https://github.com/autowarefoundation/autoware_utils/pull/50) or new
75+
// ManagedTransformBuffer lib is deployed for autoware
76+
void VoxelBasedCompareMapFilterComponent::input_indices_callback(
77+
const sensor_msgs::msg::PointCloud2::ConstSharedPtr cloud, const PointIndicesConstPtr indices)
78+
{
79+
// If cloud is given, check if it's valid
80+
if (!isValid(cloud)) {
81+
RCLCPP_ERROR(this->get_logger(), "[input_indices_callback] Invalid input!");
82+
return;
83+
}
84+
// If indices are given, check if they are valid
85+
if (indices && !isValid(indices)) {
86+
RCLCPP_ERROR(this->get_logger(), "[input_indices_callback] Invalid indices!");
87+
return;
88+
}
89+
90+
// Check whether the user has given a different input TF frame
91+
tf_input_orig_frame_ = cloud->header.frame_id;
92+
PointCloud2ConstPtr cloud_tf;
93+
if (cloud->header.frame_id != tf_input_frame_) {
94+
RCLCPP_DEBUG(
95+
this->get_logger(), "[input_indices_callback] Transforming input dataset from %s to %s.",
96+
cloud->header.frame_id.c_str(), tf_input_frame_.c_str());
97+
// Save the original frame ID
98+
// Convert the cloud into the different frame
99+
PointCloud2 cloud_transformed;
100+
101+
try {
102+
// Lookup the transform from input frame to "map"
103+
geometry_msgs::msg::TransformStamped transform_stamped = tf_buffer_.lookupTransform(
104+
tf_input_frame_, tf_input_orig_frame_, rclcpp::Time(cloud->header.stamp),
105+
rclcpp::Duration::from_seconds(1.0));
106+
107+
// Transform the point cloud
108+
tf2::doTransform(*cloud, cloud_transformed, transform_stamped);
109+
cloud_transformed.header.frame_id = tf_input_frame_; // Update frame ID to "map"
110+
cloud_tf = std::make_shared<PointCloud2>(cloud_transformed);
111+
} catch (tf2::TransformException & ex) {
112+
RCLCPP_WARN(this->get_logger(), "Could not transform point cloud: %s", ex.what());
113+
cloud_tf = cloud; // Fallback to original data
114+
}
115+
} else {
116+
cloud_tf = cloud;
117+
}
118+
// Need setInputCloud () here because we have to extract x/y/z
119+
IndicesPtr vindices;
120+
if (indices) {
121+
vindices.reset(new std::vector<int>(indices->indices));
122+
}
123+
124+
computePublish(cloud_tf, vindices);
125+
}
126+
65127
void VoxelBasedCompareMapFilterComponent::filter(
66128
const PointCloud2ConstPtr & input, [[maybe_unused]] const IndicesPtr & indices,
67129
PointCloud2 & output)

perception/autoware_compare_map_segmentation/src/voxel_based_compare_map_filter/node.hpp

+11
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,27 @@ namespace autoware::compare_map_segmentation
2727
{
2828
class VoxelBasedCompareMapFilterComponent : public autoware::pointcloud_preprocessor::Filter
2929
{
30+
using PointCloud2 = sensor_msgs::msg::PointCloud2;
31+
using PointCloud2ConstPtr = sensor_msgs::msg::PointCloud2::ConstSharedPtr;
32+
33+
using PointIndices = pcl_msgs::msg::PointIndices;
34+
using PointIndicesPtr = PointIndices::SharedPtr;
35+
using PointIndicesConstPtr = PointIndices::ConstSharedPtr;
36+
3037
protected:
3138
void filter(
3239
const PointCloud2ConstPtr & input, const IndicesPtr & indices, PointCloud2 & output) override;
40+
void input_indices_callback(
41+
const PointCloud2ConstPtr cloud, const PointIndicesConstPtr indices) override;
3342

3443
private:
3544
// pcl::SegmentDifferences<pcl::PointXYZ> impl_;
3645
std::unique_ptr<VoxelGridMapLoader> voxel_grid_map_loader_;
3746
rclcpp::Subscription<PointCloud2>::SharedPtr sub_map_;
3847
double distance_threshold_;
3948
bool set_map_in_voxel_grid_;
49+
tf2_ros::Buffer tf_buffer_;
50+
tf2_ros::TransformListener tf_listener_;
4051

4152
public:
4253
PCL_MAKE_ALIGNED_OPERATOR_NEW

sensing/autoware_pointcloud_preprocessor/include/autoware/pointcloud_preprocessor/filter.hpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ class Filter : public rclcpp::Node
207207
* \param indices a pointer to the vector of point indices to use.
208208
*/
209209
void computePublish(const PointCloud2ConstPtr & input, const IndicesPtr & indices);
210+
/** \brief PointCloud2 + Indices data callback. */
211+
virtual void input_indices_callback(
212+
const PointCloud2ConstPtr cloud, const PointIndicesConstPtr indices);
210213

211214
//////////////////////
212215
// from PCLNodelet //
@@ -279,9 +282,6 @@ class Filter : public rclcpp::Node
279282
std::shared_ptr<ExactTimeSyncPolicy> sync_input_indices_e_;
280283
std::shared_ptr<ApproximateTimeSyncPolicy> sync_input_indices_a_;
281284

282-
/** \brief PointCloud2 + Indices data callback. */
283-
void input_indices_callback(const PointCloud2ConstPtr cloud, const PointIndicesConstPtr indices);
284-
285285
/** \brief Get a matrix for conversion from the original frame to the target frame */
286286
bool calculate_transform_matrix(
287287
const std::string & target_frame, const sensor_msgs::msg::PointCloud2 & from,

0 commit comments

Comments
 (0)