Skip to content

Commit 2926d3b

Browse files
committed
Frame check fix
Signed-off-by: silanus23 <[email protected]>
1 parent ca0d306 commit 2926d3b

File tree

3 files changed

+64
-48
lines changed

3 files changed

+64
-48
lines changed

nav2_util/include/nav2_util/path_utils.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ struct PathSearchResult
4444
* It returns the minimum distance found and the index of the closest segment.
4545
*
4646
* @param path The path to search (sequence of poses).
47-
* @param robot_pose The robot's current pose.
47+
* @param robot_pose The robot's current pose in pose form.
4848
* @param start_index The index in the path to start searching from.
4949
* @param search_window_length The maximum length (in meters) to search along the path.
5050
* @return PathSearchResult Struct containing the minimum distance and the index of the closest segment.
5151
*/
5252
PathSearchResult distance_from_path(
5353
const nav_msgs::msg::Path & path,
54-
const geometry_msgs::msg::PoseStamped & robot_pose,
54+
const geometry_msgs::msg::Pose & robot_pose,
5555
const size_t start_index = 0,
5656
const double search_window_length = std::numeric_limits<double>::max());
5757

nav2_util/src/path_utils.cpp

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace nav2_util
2424

2525
PathSearchResult distance_from_path(
2626
const nav_msgs::msg::Path & path,
27-
const geometry_msgs::msg::PoseStamped & robot_pose,
27+
const geometry_msgs::msg::Pose & robot_pose,
2828
const size_t start_index,
2929
const double search_window_length)
3030
{
@@ -38,7 +38,7 @@ PathSearchResult distance_from_path(
3838

3939
if (path.poses.size() == 1) {
4040
result.distance = nav2_util::geometry_utils::euclidean_distance(
41-
robot_pose.pose, path.poses.front().pose);
41+
robot_pose, path.poses.front().pose);
4242
result.closest_segment_index = 0;
4343
return result;
4444
}
@@ -50,21 +50,14 @@ PathSearchResult distance_from_path(
5050
"). Application is not properly managing state.");
5151
}
5252

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-
6053
double distance_traversed = 0.0;
6154
for (size_t i = start_index; i < path.poses.size() - 1; ++i) {
6255
if (distance_traversed > search_window_length) {
6356
break;
6457
}
6558

6659
const double current_distance = geometry_utils::distance_to_segment(
67-
robot_pose.pose.position,
60+
robot_pose.position,
6861
path.poses[i].pose,
6962
path.poses[i + 1].pose);
7063

@@ -74,8 +67,8 @@ PathSearchResult distance_from_path(
7467
}
7568

7669
distance_traversed += geometry_utils::euclidean_distance(
77-
path.poses[i].pose,
78-
path.poses[i + 1].pose);
70+
path.poses[i],
71+
path.poses[i + 1]);
7972
}
8073

8174
return result;

nav2_util/test/test_path_utils.cpp

Lines changed: 57 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include "nav_msgs/msg/path.hpp"
2323
#include "nav2_util/path_utils.hpp"
2424

25-
geometry_msgs::msg::PoseStamped createPose(double x, double y)
25+
geometry_msgs::msg::PoseStamped createPoseStamped(double x, double y)
2626
{
2727
geometry_msgs::msg::PoseStamped pose;
2828
pose.pose.position.x = x;
@@ -32,6 +32,16 @@ geometry_msgs::msg::PoseStamped createPose(double x, double y)
3232
return pose;
3333
}
3434

35+
geometry_msgs::msg::Pose createPose(double x, double y)
36+
{
37+
geometry_msgs::msg::Pose pose;
38+
pose.position.x = x;
39+
pose.position.y = y;
40+
pose.position.z = 0.0;
41+
pose.orientation.w = 1.0;
42+
return pose;
43+
}
44+
3545
void generateCirclePath(
3646
nav_msgs::msg::Path & path,
3747
double center_x, double center_y, double radius,
@@ -40,12 +50,25 @@ void generateCirclePath(
4050
const double angle_step = (end_angle - start_angle) / (num_points - 1);
4151
for (int i = 0; i < num_points; ++i) {
4252
const double angle = start_angle + i * angle_step;
43-
path.poses.push_back(createPose(
53+
path.poses.push_back(createPoseStamped(
4454
center_x + radius * std::cos(angle),
4555
center_y + radius * std::sin(angle)));
4656
}
4757
}
4858

59+
void generateCircleTrajectory(
60+
std::vector<geometry_msgs::msg::Pose> & path,
61+
double center_x, double center_y, double radius,
62+
int num_points, double start_angle = 0.0, double end_angle = 2.0 * M_PI)
63+
{
64+
const double angle_step = (end_angle - start_angle) / (num_points - 1);
65+
for (int i = 0; i < num_points; ++i) {
66+
const double angle = start_angle + i * angle_step;
67+
path.push_back(createPose(
68+
center_x + radius * std::cos(angle),
69+
center_y + radius * std::sin(angle)));
70+
}
71+
}
4972
class CloverleafPathTest : public ::testing::Test
5073
{
5174
protected:
@@ -57,14 +80,14 @@ class CloverleafPathTest : public ::testing::Test
5780
generateCirclePath(target_path, 0.0, 5.0, 5.0, 50);
5881

5982
// Robot trajectory now also travels all three leaves, but with a radius of 4.8
60-
nav_msgs::msg::Path robot_path;
61-
generateCirclePath(robot_path, 5.0, 0.0, 4.8, 50);
62-
generateCirclePath(robot_path, -5.0, 0.0, 4.8, 50);
63-
generateCirclePath(robot_path, 0.0, 5.0, 4.8, 50);
64-
robot_trajectory = robot_path.poses;
83+
std::vector<geometry_msgs::msg::Pose> robot_path;
84+
generateCircleTrajectory(robot_path, 5.0, 0.0, 4.8, 50);
85+
generateCircleTrajectory(robot_path, -5.0, 0.0, 4.8, 50);
86+
generateCircleTrajectory(robot_path, 0.0, 5.0, 4.8, 50);
87+
robot_trajectory = robot_path;
6588
}
6689
nav_msgs::msg::Path target_path;
67-
std::vector<geometry_msgs::msg::PoseStamped> robot_trajectory;
90+
std::vector<geometry_msgs::msg::Pose> robot_trajectory;
6891
};
6992

7093
class RetracingCircleTest : public ::testing::Test
@@ -74,25 +97,25 @@ class RetracingCircleTest : public ::testing::Test
7497
{
7598
generateCirclePath(target_path, 0.0, 0.0, 5.0, 50, 0.0, 2.0 * M_PI);
7699
generateCirclePath(target_path, 0.0, 0.0, 5.0, 50, 2.0 * M_PI, 0.0);
77-
nav_msgs::msg::Path robot_path;
78-
generateCirclePath(robot_path, 0.0, 0.0, 5.2, 50, 0.0, 2.0 * M_PI);
79-
generateCirclePath(robot_path, 0.0, 0.0, 5.2, 50, 2.0 * M_PI, 0.0);
80-
robot_trajectory = robot_path.poses;
100+
std::vector<geometry_msgs::msg::Pose> robot_path;
101+
generateCircleTrajectory(robot_path, 0.0, 0.0, 5.2, 50, 0.0, 2.0 * M_PI);
102+
generateCircleTrajectory(robot_path, 0.0, 0.0, 5.2, 50, 2.0 * M_PI, 0.0);
103+
robot_trajectory = robot_path;
81104
}
82105
nav_msgs::msg::Path target_path;
83-
std::vector<geometry_msgs::msg::PoseStamped> robot_trajectory;
106+
std::vector<geometry_msgs::msg::Pose> robot_trajectory;
84107
};
85108

86109
class ZigZagPathTest : public ::testing::Test
87110
{
88111
protected:
89112
void SetUp() override
90113
{
91-
target_path.poses.push_back(createPose(0.0, 0.0));
92-
target_path.poses.push_back(createPose(10.0, 0.0));
93-
target_path.poses.push_back(createPose(10.0, 5.0));
94-
target_path.poses.push_back(createPose(0.0, 5.0));
95-
target_path.poses.push_back(createPose(0.0, 10.0));
114+
target_path.poses.push_back(createPoseStamped(0.0, 0.0));
115+
target_path.poses.push_back(createPoseStamped(10.0, 0.0));
116+
target_path.poses.push_back(createPoseStamped(10.0, 5.0));
117+
target_path.poses.push_back(createPoseStamped(0.0, 5.0));
118+
target_path.poses.push_back(createPoseStamped(0.0, 10.0));
96119

97120
robot_trajectory = {
98121
createPose(1.0, 0.2), createPose(5.0, 0.2), createPose(9.0, 0.2),
@@ -102,18 +125,18 @@ class ZigZagPathTest : public ::testing::Test
102125
};
103126
}
104127
nav_msgs::msg::Path target_path;
105-
std::vector<geometry_msgs::msg::PoseStamped> robot_trajectory;
128+
std::vector<geometry_msgs::msg::Pose> robot_trajectory;
106129
};
107130

108131
class HairpinTurnTest : public ::testing::Test
109132
{
110133
protected:
111134
void SetUp() override
112135
{
113-
target_path.poses.push_back(createPose(0.0, 1.0));
114-
target_path.poses.push_back(createPose(10.0, 1.0));
115-
target_path.poses.push_back(createPose(10.0, -1.0));
116-
target_path.poses.push_back(createPose(0.0, -1.0));
136+
target_path.poses.push_back(createPoseStamped(0.0, 1.0));
137+
target_path.poses.push_back(createPoseStamped(10.0, 1.0));
138+
target_path.poses.push_back(createPoseStamped(10.0, -1.0));
139+
target_path.poses.push_back(createPoseStamped(0.0, -1.0));
117140

118141
robot_trajectory = {
119142
createPose(1.0, 1.2), createPose(3.0, 1.2), createPose(5.0, 1.2),
@@ -124,17 +147,17 @@ class HairpinTurnTest : public ::testing::Test
124147
};
125148
}
126149
nav_msgs::msg::Path target_path;
127-
std::vector<geometry_msgs::msg::PoseStamped> robot_trajectory;
150+
std::vector<geometry_msgs::msg::Pose> robot_trajectory;
128151
};
129152

130153
class CuttingCornerTest : public ::testing::Test
131154
{
132155
protected:
133156
void SetUp() override
134157
{
135-
target_path.poses.push_back(createPose(0.0, 0.0));
136-
target_path.poses.push_back(createPose(10.0, 0.0));
137-
target_path.poses.push_back(createPose(10.0, 10.0));
158+
target_path.poses.push_back(createPoseStamped(0.0, 0.0));
159+
target_path.poses.push_back(createPoseStamped(10.0, 0.0));
160+
target_path.poses.push_back(createPoseStamped(10.0, 10.0));
138161

139162
robot_trajectory = {
140163
createPose(0.0, 0.2), createPose(2.0, 0.2), createPose(4.0, 0.2),
@@ -145,7 +168,7 @@ class CuttingCornerTest : public ::testing::Test
145168
expected_distances = {0.2, 0.2, 0.2, 0.2, 0.2, 1.0, 0.2, 0.2, 0.2, 0.2, 0.2};
146169
}
147170
nav_msgs::msg::Path target_path;
148-
std::vector<geometry_msgs::msg::PoseStamped> robot_trajectory;
171+
std::vector<geometry_msgs::msg::Pose> robot_trajectory;
149172
std::vector<double> expected_distances;
150173
};
151174

@@ -154,8 +177,8 @@ class RetracingPathTest : public ::testing::Test
154177
protected:
155178
void SetUp() override
156179
{
157-
for (int i = 0; i <= 10; ++i) {target_path.poses.push_back(createPose(i, 0.0));}
158-
for (int i = 9; i >= 0; --i) {target_path.poses.push_back(createPose(i, 0.0));}
180+
for (int i = 0; i <= 10; ++i) {target_path.poses.push_back(createPoseStamped(i, 0.0));}
181+
for (int i = 9; i >= 0; --i) {target_path.poses.push_back(createPoseStamped(i, 0.0));}
159182
for (int i = 0; i <= 10; ++i) {
160183
robot_trajectory.push_back(createPose(i, 0.5));
161184
}
@@ -164,7 +187,7 @@ class RetracingPathTest : public ::testing::Test
164187
}
165188
}
166189
nav_msgs::msg::Path target_path;
167-
std::vector<geometry_msgs::msg::PoseStamped> robot_trajectory;
190+
std::vector<geometry_msgs::msg::Pose> robot_trajectory;
168191
};
169192

170193
TEST(PathUtilsTest, EmptyAndSinglePointPaths)
@@ -176,7 +199,7 @@ TEST(PathUtilsTest, EmptyAndSinglePointPaths)
176199
EXPECT_EQ(result.distance, std::numeric_limits<double>::max());
177200

178201
nav_msgs::msg::Path single_point_path;
179-
single_point_path.poses.push_back(createPose(0.0, 0.0));
202+
single_point_path.poses.push_back(createPoseStamped(0.0, 0.0));
180203
result = nav2_util::distance_from_path(single_point_path, robot_pose);
181204
EXPECT_NEAR(result.distance, 7.071, 0.01);
182205
}
@@ -303,8 +326,8 @@ TEST(PathUtilsWindowedTest, EdgeCases)
303326
{
304327
auto robot_pose = createPose(5.0, 5.0);
305328
nav_msgs::msg::Path test_path;
306-
test_path.poses.push_back(createPose(0.0, 0.0));
307-
test_path.poses.push_back(createPose(10.0, 0.0));
329+
test_path.poses.push_back(createPoseStamped(0.0, 0.0));
330+
test_path.poses.push_back(createPoseStamped(10.0, 0.0));
308331

309332
auto result = nav2_util::distance_from_path(test_path, robot_pose, 0, 5.0);
310333
EXPECT_NEAR(result.distance, 5.0, 0.01);

0 commit comments

Comments
 (0)