Skip to content

Commit a6aa499

Browse files
doisygGuillaume Doisy
authored andcommitted
[static_layer] limit comparison precision (#5405)
* [DEX] limit comparison precision Signed-off-by: Guillaume Doisy <[email protected]> * EPSILON 1e-5 Signed-off-by: Guillaume Doisy <[email protected]> --------- Signed-off-by: Guillaume Doisy <[email protected]> Co-authored-by: Guillaume Doisy <[email protected]>
1 parent dae54da commit a6aa499

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

nav2_costmap_2d/include/nav2_costmap_2d/static_layer.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,15 @@ class StaticLayer : public CostmapLayer
154154
*/
155155
unsigned char interpretValue(unsigned char value);
156156

157+
/**
158+
* @brief Check if two double values are equal within a given epsilon
159+
* @param a First double value
160+
* @param b Second double value
161+
* @param epsilon The tolerance for equality check
162+
* @return True if the values are equal within the tolerance, false otherwise
163+
*/
164+
bool isEqual(double a, double b, double epsilon);
165+
157166
/**
158167
* @brief Callback executed when a parameter change is detected
159168
* @param event ParameterEvent message

nav2_costmap_2d/plugins/static_layer.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
#include "tf2_geometry_msgs/tf2_geometry_msgs.hpp"
4848
#include "nav2_util/validate_messages.hpp"
4949

50+
#define EPSILON 1e-5
51+
5052
PLUGINLIB_EXPORT_CLASS(nav2_costmap_2d::StaticLayer, nav2_costmap_2d::Layer)
5153

5254
using nav2_costmap_2d::NO_INFORMATION;
@@ -196,9 +198,9 @@ StaticLayer::processMap(const nav_msgs::msg::OccupancyGrid & new_map)
196198
Costmap2D * master = layered_costmap_->getCostmap();
197199
if (!layered_costmap_->isRolling() && (master->getSizeInCellsX() != size_x ||
198200
master->getSizeInCellsY() != size_y ||
199-
master->getResolution() != new_map.info.resolution ||
200-
master->getOriginX() != new_map.info.origin.position.x ||
201-
master->getOriginY() != new_map.info.origin.position.y ||
201+
!isEqual(master->getResolution(), new_map.info.resolution, EPSILON) ||
202+
!isEqual(master->getOriginX(), new_map.info.origin.position.x, EPSILON) ||
203+
!isEqual(master->getOriginY(), new_map.info.origin.position.y, EPSILON) ||
202204
!layered_costmap_->isSizeLocked()))
203205
{
204206
// Update the size of the layered costmap (and all layers, including this one)
@@ -212,9 +214,9 @@ StaticLayer::processMap(const nav_msgs::msg::OccupancyGrid & new_map)
212214
new_map.info.origin.position.y,
213215
true);
214216
} else if (size_x_ != size_x || size_y_ != size_y || // NOLINT
215-
resolution_ != new_map.info.resolution ||
216-
origin_x_ != new_map.info.origin.position.x ||
217-
origin_y_ != new_map.info.origin.position.y)
217+
!isEqual(resolution_, new_map.info.resolution, EPSILON) ||
218+
!isEqual(origin_x_, new_map.info.origin.position.x, EPSILON) ||
219+
!isEqual(origin_y_, new_map.info.origin.position.y, EPSILON))
218220
{
219221
// only update the size of the costmap stored locally in this layer
220222
RCLCPP_INFO(
@@ -467,6 +469,18 @@ StaticLayer::updateCosts(
467469
current_ = true;
468470
}
469471

472+
/**
473+
* @brief Check if two floating point numbers are equal within a given epsilon
474+
* @param a First number
475+
* @param b Second number
476+
* @param epsilon Tolerance for equality check
477+
* @return True if numbers are equal within the tolerance, false otherwise
478+
*/
479+
bool StaticLayer::isEqual(double a, double b, double epsilon)
480+
{
481+
return std::abs(a - b) < epsilon;
482+
}
483+
470484
/**
471485
* @brief Callback executed when a parameter change is detected
472486
* @param event ParameterEvent message

0 commit comments

Comments
 (0)