Skip to content

Commit deb5a99

Browse files
leander-dsouzastevedanomodolor
authored andcommitted
Clear costmap if reset distance exceeds costmap bounds. (ros-navigation#5010)
* Migrate costmap bound check to clearArea for STVL override Signed-off-by: Leander Stephen D'Souza <leanderdsouza1234@gmail.com> * Added unbounded map to world function. Signed-off-by: Leander Stephen D'Souza <leanderdsouza1234@gmail.com> * Added test for mapToWorldNoBounds Signed-off-by: Leander Stephen D'Souza <leanderdsouza1234@gmail.com> --------- Signed-off-by: Leander Stephen D'Souza <leanderdsouza1234@gmail.com> Signed-off-by: stevedanomodolor <stevedan.o.omodolor@gmail.com>
1 parent 5ecf290 commit deb5a99

File tree

6 files changed

+92
-4
lines changed

6 files changed

+92
-4
lines changed

nav2_costmap_2d/include/nav2_costmap_2d/costmap_2d.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,15 @@ class Costmap2D
173173
*/
174174
void mapToWorld(unsigned int mx, unsigned int my, double & wx, double & wy) const;
175175

176+
/**
177+
* @brief Convert from map coordinates to world coordinates with no bounds checking
178+
* @param wx The x world coordinate
179+
* @param wy The y world coordinate
180+
* @param mx Will be set to the associated map x coordinate
181+
* @param my Will be set to the associated map y coordinate
182+
*/
183+
void mapToWorldNoBounds(int mx, int my, double & wx, double & wy) const;
184+
176185
/**
177186
* @brief Convert from world coordinates to map coordinates
178187
* @param wx The x world coordinate

nav2_costmap_2d/src/clear_costmap_service.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ void ClearCostmapService::clearLayerRegion(
135135
double end_point_y = start_point_y + reset_distance;
136136

137137
int start_x, start_y, end_x, end_y;
138-
costmap->worldToMapEnforceBounds(start_point_x, start_point_y, start_x, start_y);
139-
costmap->worldToMapEnforceBounds(end_point_x, end_point_y, end_x, end_y);
138+
costmap->worldToMapNoBounds(start_point_x, start_point_y, start_x, start_y);
139+
costmap->worldToMapNoBounds(end_point_x, end_point_y, end_x, end_y);
140140

141141
costmap->clearArea(start_x, start_y, end_x, end_y, invert);
142142

nav2_costmap_2d/src/costmap_2d.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,12 @@ void Costmap2D::mapToWorld(unsigned int mx, unsigned int my, double & wx, double
282282
wy = origin_y_ + (my + 0.5) * resolution_;
283283
}
284284

285+
void Costmap2D::mapToWorldNoBounds(int mx, int my, double & wx, double & wy) const
286+
{
287+
wx = origin_x_ + (mx + 0.5) * resolution_;
288+
wy = origin_y_ + (my + 0.5) * resolution_;
289+
}
290+
285291
bool Costmap2D::worldToMap(double wx, double wy, unsigned int & mx, unsigned int & my) const
286292
{
287293
if (wx < origin_x_ || wy < origin_y_) {

nav2_costmap_2d/src/costmap_layer.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,19 @@ void CostmapLayer::clearArea(int start_x, int start_y, int end_x, int end_y, boo
6666
{
6767
current_ = false;
6868
unsigned char * grid = getCharMap();
69-
for (int x = 0; x < static_cast<int>(getSizeInCellsX()); x++) {
69+
70+
int size_x = getSizeInCellsX();
71+
int size_y = getSizeInCellsY();
72+
73+
start_x = std::clamp(start_x, 0, size_x);
74+
start_y = std::clamp(start_y, 0, size_y);
75+
end_x = std::clamp(end_x, 0, size_x);
76+
end_y = std::clamp(end_y, 0, size_y);
77+
78+
for (int x = 0; x < size_x; x++) {
7079
bool xrange = x > start_x && x < end_x;
7180

72-
for (int y = 0; y < static_cast<int>(getSizeInCellsY()); y++) {
81+
for (int y = 0; y < size_y; y++) {
7382
if ((xrange && y > start_y && y < end_y) == invert) {
7483
continue;
7584
}

nav2_costmap_2d/test/unit/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,8 @@ ament_add_gtest(lifecycle_test lifecycle_test.cpp)
6262
target_link_libraries(lifecycle_test
6363
nav2_costmap_2d_core
6464
)
65+
66+
ament_add_gtest(coordinate_transform_test coordinate_transform_test.cpp)
67+
target_link_libraries(coordinate_transform_test
68+
nav2_costmap_2d_core
69+
)
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright (c) 2025 Open Navigation LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License. Reserved.
14+
15+
#include <gtest/gtest.h>
16+
17+
#include "rclcpp/rclcpp.hpp"
18+
#include "nav2_costmap_2d/costmap_2d.hpp"
19+
20+
21+
/**
22+
* Test for mapToWorldNoBounds
23+
*/
24+
25+
TEST(mapToWorldNoBounds, MapToWorldNoBoundsNegativeMapCoords)
26+
{
27+
double wx, wy;
28+
29+
std::unique_ptr<nav2_costmap_2d::Costmap2D> map;
30+
31+
map = std::make_unique<nav2_costmap_2d::Costmap2D>(10, 10, 1.0, 0.0, 0.0);
32+
map->mapToWorldNoBounds(-1, -1, wx, wy);
33+
EXPECT_DOUBLE_EQ(wx, -0.5);
34+
EXPECT_DOUBLE_EQ(wy, -0.5);
35+
36+
map = std::make_unique<nav2_costmap_2d::Costmap2D>(10, 10, 1.0, 1.0, 2.0);
37+
map->mapToWorldNoBounds(-5, -5, wx, wy);
38+
EXPECT_DOUBLE_EQ(wx, -3.5);
39+
EXPECT_DOUBLE_EQ(wy, -2.5);
40+
41+
map = std::make_unique<nav2_costmap_2d::Costmap2D>(10, 10, 2.0, 3.0, 4.0);
42+
map->mapToWorldNoBounds(-10, -10, wx, wy);
43+
EXPECT_DOUBLE_EQ(wx, -16.0);
44+
EXPECT_DOUBLE_EQ(wy, -15.0);
45+
}
46+
47+
48+
int main(int argc, char **argv)
49+
{
50+
::testing::InitGoogleTest(&argc, argv);
51+
52+
rclcpp::init(0, nullptr);
53+
54+
int result = RUN_ALL_TESTS();
55+
56+
rclcpp::shutdown();
57+
58+
return result;
59+
}

0 commit comments

Comments
 (0)