Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update padFootprint() function #4832

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions nav2_costmap_2d/include/nav2_costmap_2d/footprint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ void transformFootprint(
const std::vector<geometry_msgs::msg::Point> & footprint_spec,
std::vector<geometry_msgs::msg::Point> & oriented_footprint);

/**
* @brief Calculate the centroid of a vector of 2D points.
* @param footprint A vector of points (type geometry_msgs::Point) representing a 2D polygon (footprint).
* @return The centroid of the footprint as a geometry_msgs::Point.
*/
geometry_msgs::msg::Point calculateCentroid(const std::vector<geometry_msgs::msg::Point>& footprint);

/**
* @brief Given a pose and base footprint, build the oriented footprint of the robot (PolygonStamped)
* @param x The x position of the robot
Expand Down
37 changes: 32 additions & 5 deletions nav2_costmap_2d/src/footprint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,40 @@ void transformFootprint(
}
}

geometry_msgs::msg::Point calculateCentroid(const std::vector<geometry_msgs::msg::Point>& footprint)
{
geometry_msgs::Point center;
double sum_x = 0.0, sum_y = 0.0;

for (const auto& point : footprint)
{
sum_x += point.x;
sum_y += point.y;
}

if (!footprint.empty()) {
center.x = sum_x / footprint.size();
center.y = sum_y / footprint.size();
}

return center;
}

void padFootprint(std::vector<geometry_msgs::msg::Point> & footprint, double padding)
{
// pad footprint in place
for (unsigned int i = 0; i < footprint.size(); i++) {
geometry_msgs::msg::Point & pt = footprint[i];
pt.x += sign0(pt.x) * padding;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please point out to me the error in the code here - I don't see any immediately.

Is the issue the case where you have a coordinate exactly at 0.0? 0.0 for the footprint would be the center of your robot, since that is the 0.0, 0.0 w.r.t. your base_link or base_footprint frame -- which is almost always the center of your robot or aligned with some axle that wouldn't then be a corner of a robot. That would point to me more an issue in your configuration

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Nisarg236 should this be closed?

pt.y += sign0(pt.y) * padding;
geometry_msgs::msg::Point footprint_center = calculateCentroid(footprint);

for (auto& point : footprint)
{
double dx = point.x - footprint_center.x;
double dy = point.y - footprint_center.y;
double distance = std::hypot(dx, dy);

if (distance > 0.0)
{
point.x += padding * (dx / distance);
point.y += padding * (dy / distance);
}
}
}

Expand Down
12 changes: 6 additions & 6 deletions nav2_costmap_2d/test/integration/footprint_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,16 +153,16 @@ TEST_F(TestNode, padded_footprint_from_string_param)
std::vector<geometry_msgs::msg::Point> footprint = footprint_tester_->getRobotFootprint();
EXPECT_EQ(3u, footprint.size());

EXPECT_EQ(1.5f, footprint[0].x);
EXPECT_EQ(1.5f, footprint[0].y);
EXPECT_NEAR(1.44721f, footprint[0].x, 0.0001f);
EXPECT_NEAR(1.22361f, footprint[0].y, 0.0001f);
EXPECT_EQ(0.0f, footprint[0].z);

EXPECT_EQ(-1.5f, footprint[1].x);
EXPECT_EQ(1.5f, footprint[1].y);
EXPECT_NEAR(-1.35355f, footprint[1].x, 0.0001f);
EXPECT_NEAR(1.35355f, footprint[1].y, 0.0001f);
EXPECT_EQ(0.0f, footprint[1].z);

EXPECT_EQ(-1.5f, footprint[2].x);
EXPECT_EQ(-1.5f, footprint[2].y);
EXPECT_NEAR(-1.22361f, footprint[2].x, 0.0001f);
EXPECT_NEAR(-1.44721f, footprint[2].y, 0.0001f);
EXPECT_EQ(0.0f, footprint[2].z);
}

Expand Down
Loading