Skip to content

Commit

Permalink
Fix floating point issue in costmap_2d boundaries (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
renan028 authored Oct 28, 2024
1 parent 48b6977 commit bd2acbc
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions costmap_2d/plugins/obstacle_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,6 @@ void ObstacleLayer::raytraceFreespace(const Observation& clearing_observation, d
// to isn't off the costmap and scale if necessary
double a = wx - ox;
double b = wy - oy;

// the minimum value to raytrace from is the origin
if (wx < origin_x)
{
Expand Down Expand Up @@ -599,7 +598,7 @@ bool ObstacleLayer::adjustSensorOrigin(const Observation& clearing_observation,
// copy original sensor origin
const double original_ox = ox;
const double original_oy = oy;

// Define the sensor ray as a linestring (from the sensor origin to the endpoint)
Linestring sensor_ray;
bg::append(sensor_ray, Point(ox, oy));
Expand All @@ -610,6 +609,17 @@ bool ObstacleLayer::adjustSensorOrigin(const Observation& clearing_observation,
// find the intersection between the map and the line defined by the sensor ray
bg::intersection(sensor_ray, map_boundary_, intersection_points);

// function to slightly shift the origin inwards to avoid floating point errors
const auto shift_center_inwards = [&ox, &oy, &wx, &wy]()
{
static constexpr double eps = 0.001;
const double magnitude = std::hypot(wx - ox, wy - oy);
const double vx = (wx - ox) / magnitude;
const double vy = (wy - oy) / magnitude;
ox += eps * vx;
oy += eps * vy;
};

// the map is a rectangle, so there should be at least one intersection point
// and at maximum 2 intersection points
if (intersection_points.size() > 2 || intersection_points.size() == 0)
Expand All @@ -629,6 +639,8 @@ bool ObstacleLayer::adjustSensorOrigin(const Observation& clearing_observation,
{
return false;
}

shift_center_inwards();
return true;
}

Expand Down Expand Up @@ -663,6 +675,8 @@ bool ObstacleLayer::adjustSensorOrigin(const Observation& clearing_observation,
{
return false;
}

shift_center_inwards();
return true;
}

Expand Down

0 comments on commit bd2acbc

Please sign in to comment.