Skip to content

Commit 31c87c5

Browse files
committed
fixing static layer update bounds with different fram
Signed-off-by: 2linkthefire <byepanda@163.com>
1 parent 7c5d0de commit 31c87c5

File tree

1 file changed

+45
-7
lines changed

1 file changed

+45
-7
lines changed

nav2_costmap_2d/plugins/static_layer.cpp

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -363,15 +363,53 @@ StaticLayer::updateBounds(
363363

364364
useExtraBounds(min_x, min_y, max_x, max_y);
365365

366-
double wx, wy;
366+
// Might even be in a different frame
367+
if (map_frame_ == global_frame_) {
368+
double wx, wy;
369+
370+
mapToWorld(x_, y_, wx, wy);
371+
*min_x = std::min(wx, *min_x);
372+
*min_y = std::min(wy, *min_y);
373+
374+
mapToWorld(x_ + width_, y_ + height_, wx, wy);
375+
*max_x = std::max(wx, *max_x);
376+
*max_y = std::max(wy, *max_y);
377+
} else {
378+
geometry_msgs::msg::TransformStamped transform;
379+
try {
380+
transform = tf_->lookupTransform(
381+
global_frame_, map_frame_, tf2::TimePointZero,
382+
transform_tolerance_);
383+
} catch (tf2::TransformException & ex) {
384+
RCLCPP_ERROR(logger_, "StaticLayer: %s", ex.what());
385+
return;
386+
}
387+
tf2::Transform tf2_transform;
388+
tf2::fromMsg(transform.transform, tf2_transform);
389+
390+
double wx, wy;
391+
mapToWorld(x_, y_, wx, wy);
392+
tf2::Vector3 down_left(wx, wy, 0);
393+
394+
mapToWorld(x_ + width_, y_, wx, wy);
395+
tf2::Vector3 down_right(wx, wy, 0);
396+
397+
mapToWorld(x_, y_ + height_, wx, wy);
398+
tf2::Vector3 up_left(wx, wy, 0);
367399

368-
mapToWorld(x_, y_, wx, wy);
369-
*min_x = std::min(wx, *min_x);
370-
*min_y = std::min(wy, *min_y);
400+
mapToWorld(x_ + width_, y_ + height_, wx, wy);
401+
tf2::Vector3 up_right(wx, wy, 0);
371402

372-
mapToWorld(x_ + width_, y_ + height_, wx, wy);
373-
*max_x = std::max(wx, *max_x);
374-
*max_y = std::max(wy, *max_y);
403+
down_left = tf2_transform * down_left;
404+
down_right = tf2_transform * down_right;
405+
up_left = tf2_transform * up_left;
406+
up_right = tf2_transform * up_right;
407+
408+
*min_x = std::min({*min_x, down_left.x(), down_right.x(), up_left.x(), up_right.x()});
409+
*min_y = std::min({*min_y, down_left.y(), down_right.y(), up_left.y(), up_right.y()});
410+
*max_x = std::max({*max_x, down_left.x(), down_right.x(), up_left.x(), up_right.x()});
411+
*max_y = std::max({*max_y, down_left.y(), down_right.y(), up_left.y(), up_right.y()});
412+
}
375413

376414
has_updated_data_ = false;
377415

0 commit comments

Comments
 (0)