Skip to content

Commit 7eb827a

Browse files
committed
Last fixes cpp.
Signed-off-by: silanus23 <[email protected]>
1 parent a7927e2 commit 7eb827a

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

nav2_util/src/path_utils.cpp

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@
1313
// limitations under the License.
1414

1515
#include "nav2_util/path_utils.hpp"
16+
1617
#include <limits>
1718
#include <cmath>
19+
#include <stdexcept>
20+
1821
#include "nav2_util/geometry_utils.hpp"
1922
namespace nav2_util
2023
{
@@ -29,26 +32,32 @@ PathSearchResult distance_from_path(
2932
result.closest_segment_index = start_index;
3033
result.distance = std::numeric_limits<double>::max();
3134

32-
if (path.poses.size() < 2) {
33-
if (path.poses.empty()) {
34-
return result;
35-
}
36-
result.distance = nav2_util::geometry_utils::euclidean_distance(
37-
robot_pose.pose, path.poses.front().pose);
38-
result.closest_segment_index = 0;
35+
if (path.poses.empty()) {
3936
return result;
4037
}
4138

42-
if (start_index >= path.poses.size() - 1) {
39+
if (path.poses.size() == 1) {
4340
result.distance = nav2_util::geometry_utils::euclidean_distance(
44-
robot_pose.pose,
45-
path.poses.back().pose);
46-
result.closest_segment_index = path.poses.size() - 1;
41+
robot_pose.pose, path.poses.front().pose);
42+
result.closest_segment_index = 0;
4743
return result;
4844
}
4945

50-
double distance_traversed = 0.0;
46+
if (start_index >= path.poses.size()) {
47+
throw std::invalid_argument(
48+
"Invalid operation: requested start index (" + std::to_string(start_index) +
49+
") is greater than or equal to path size (" + std::to_string(path.poses.size()) +
50+
"). Application is not properly managing state.");
51+
}
5152

53+
if (path.header.frame_id != robot_pose.header.frame_id) {
54+
throw std::invalid_argument(
55+
"Invalid input, path frame (" + (path.header.frame_id) +
56+
") is not the same frame as robot frame (" + (robot_pose.header.frame_id) +
57+
"). Use the same frame for both pose and path.");
58+
}
59+
60+
double distance_traversed = 0.0;
5261
for (size_t i = start_index; i < path.poses.size() - 1; ++i) {
5362
if (distance_traversed > search_window_length) {
5463
break;

nav2_util/test/test_path_utils.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ geometry_msgs::msg::PoseStamped createPose(double x, double y)
2929
pose.pose.position.y = y;
3030
pose.pose.position.z = 0.0;
3131
pose.pose.orientation.w = 1.0;
32-
pose.header.frame_id = "map";
3332
return pose;
3433
}
3534

@@ -303,6 +302,6 @@ TEST(PathUtilsWindowedTest, EdgeCases)
303302
test_path.poses.push_back(createPose(0.0, 0.0));
304303
test_path.poses.push_back(createPose(10.0, 0.0));
305304

306-
auto result = nav2_util::distance_from_path(test_path, robot_pose, 1, 5.0);
307-
EXPECT_NEAR(result.distance, 7.071, 0.01);
305+
auto result = nav2_util::distance_from_path(test_path, robot_pose, 0, 5.0);
306+
EXPECT_NEAR(result.distance, 5.0, 0.01);
308307
}

0 commit comments

Comments
 (0)