Skip to content

Commit 0e5df90

Browse files
authored
[Fix]map merge misalignment (SteveMacenski#704)
* fix map merge generate map misalignment * fix rotation issue
1 parent d889151 commit 0e5df90

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

Diff for: include/slam_toolbox/merge_maps_kinematic.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ class MergeMapsKinematic : public rclcpp::Node
101101
std::map<int, Eigen::Vector3d> submap_locations_;
102102
std::vector<karto::LocalizedRangeScanVector> scans_vec_;
103103
std::map<int, tf2::Transform> submap_marker_transform_;
104+
std::map<int, tf2::Transform> prev_submap_marker_transform_;
104105
double resolution_;
105106
int num_submaps_;
106107
};

Diff for: src/merge_maps_kinematic.cpp

+14-10
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ bool MergeMapsKinematic::addSubmapCallback(
136136
submap_marker_transform_[num_submaps_] =
137137
tf2::Transform(tf2::Quaternion(0., 0., 0., 1.0),
138138
tf2::Vector3(0, 0, 0)); // no initial correction -- identity mat
139+
prev_submap_marker_transform_ = submap_marker_transform_;
139140
submap_locations_[num_submaps_] =
140141
Eigen::Vector3d(transform.getOrigin().getX(),
141142
transform.getOrigin().getY(), 0.);
@@ -267,11 +268,12 @@ bool MergeMapsKinematic::mergeMapCallback(
267268
for (LocalizedRangeScansIt iter = it_LRV->begin();
268269
iter != it_LRV->end(); ++iter)
269270
{
270-
tf2::Transform submap_correction = submap_marker_transform_[id];
271+
tf2::Transform submap_correction = submap_marker_transform_[id] * prev_submap_marker_transform_[id].inverse();
271272
transformScan(iter, submap_correction);
272273
transformed_scans.push_back((*iter));
273274
}
274275
}
276+
prev_submap_marker_transform_ = submap_marker_transform_;
275277

276278
// create the map
277279
nav_msgs::srv::GetMap::Response map;
@@ -317,46 +319,47 @@ void MergeMapsKinematic::processInteractiveFeedback(
317319
{
318320
const int id = std::stoi(feedback->marker_name, nullptr, 10);
319321

322+
tf2::Quaternion quat(feedback->pose.orientation.x,
323+
feedback->pose.orientation.y,
324+
feedback->pose.orientation.z,
325+
feedback->pose.orientation.w);
326+
320327
if (feedback->event_type ==
321328
visualization_msgs::msg::InteractiveMarkerFeedback::MOUSE_UP &&
322329
feedback->mouse_point_valid)
323330
{
324331
tf2Scalar yaw = tf2::getYaw(feedback->pose.orientation);
325-
tf2::Quaternion quat(0., 0., 0., 1.0);
326332
tf2::fromMsg(feedback->pose.orientation, quat); // relative
327333

328334
tf2::Transform previous_submap_correction;
329335
previous_submap_correction.setIdentity();
330336
previous_submap_correction.setOrigin(tf2::Vector3(submap_locations_[id](0),
331337
submap_locations_[id](1), 0.));
332-
338+
previous_submap_correction.setRotation(submap_marker_transform_[id].getRotation());
333339
// update internal knowledge of submap locations
334340
submap_locations_[id] = Eigen::Vector3d(feedback->pose.position.x,
335341
feedback->pose.position.y,
336-
submap_locations_[id](2) + yaw);
342+
0.0);
337343

338344
// add the map_N frame there
339345
tf2::Transform new_submap_location;
340-
new_submap_location.setOrigin(tf2::Vector3(submap_locations_[id](0),
341-
submap_locations_[id](1), 0.));
346+
new_submap_location.setOrigin(tf2::Vector3(feedback->pose.position.x,
347+
feedback->pose.position.y, 0.));
342348
new_submap_location.setRotation(quat);
343-
344349
geometry_msgs::msg::TransformStamped msg;
345350
msg.transform = tf2::toMsg(new_submap_location);
346351
msg.child_frame_id = "/map_" + std::to_string(id);
347352
msg.header.frame_id = "/map";
348353
msg.header.stamp = this->now();
349354
tfB_->sendTransform(msg);
350355

351-
submap_marker_transform_[id] = submap_marker_transform_[id] *
352-
previous_submap_correction.inverse() * new_submap_location;
356+
submap_marker_transform_[id] = new_submap_location;
353357
}
354358

355359
if (feedback->event_type ==
356360
visualization_msgs::msg::InteractiveMarkerFeedback::POSE_UPDATE)
357361
{
358362
tf2Scalar yaw = tf2::getYaw(feedback->pose.orientation);
359-
tf2::Quaternion quat(0., 0., 0., 1.0);
360363
tf2::fromMsg(feedback->pose.orientation, quat); // relative
361364

362365
// add the map_N frame there
@@ -372,6 +375,7 @@ void MergeMapsKinematic::processInteractiveFeedback(
372375
msg.header.stamp = this->now();
373376
tfB_->sendTransform(msg);
374377
}
378+
this->mergeMapCallback(nullptr, nullptr, nullptr);
375379
}
376380

377381
/*****************************************************************************/

0 commit comments

Comments
 (0)