Skip to content

Commit 5bd91aa

Browse files
SteveMacenskijplapp
authored andcommitted
Update Smac Planner for rounding to closest bin rather than flooring (ros-navigation#4636)
(cherry picked from commit a63090f)
1 parent 48ef102 commit 5bd91aa

File tree

3 files changed

+9
-11
lines changed

3 files changed

+9
-11
lines changed

nav2_smac_planner/src/node_hybrid.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,8 @@ MotionPoses HybridMotionTable::getProjections(const NodeHybrid * node)
251251

252252
unsigned int HybridMotionTable::getClosestAngularBin(const double & theta)
253253
{
254-
return static_cast<unsigned int>(floor(theta / static_cast<double>(bin_size))) %
255-
num_angle_quantization;
254+
auto bin = static_cast<unsigned int>(round(static_cast<float>(theta) / bin_size));
255+
return bin < num_angle_quantization ? bin : 0u;
256256
}
257257

258258
float HybridMotionTable::getAngleFromBin(const unsigned int & bin_idx)

nav2_smac_planner/src/smac_planner_hybrid.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -337,33 +337,31 @@ nav_msgs::msg::Path SmacPlannerHybrid::createPlan(
337337
std::to_string(start.pose.position.y) + ") was outside bounds");
338338
}
339339

340-
double orientation_bin = tf2::getYaw(start.pose.orientation) / _angle_bin_size;
340+
double orientation_bin = std::round(tf2::getYaw(start.pose.orientation) / _angle_bin_size);
341341
while (orientation_bin < 0.0) {
342342
orientation_bin += static_cast<float>(_angle_quantizations);
343343
}
344344
// This is needed to handle precision issues
345345
if (orientation_bin >= static_cast<float>(_angle_quantizations)) {
346346
orientation_bin -= static_cast<float>(_angle_quantizations);
347347
}
348-
unsigned int orientation_bin_id = static_cast<unsigned int>(floor(orientation_bin));
349-
_a_star->setStart(mx, my, orientation_bin_id);
348+
_a_star->setStart(mx, my, static_cast<unsigned int>(orientation_bin));
350349

351350
// Set goal point, in A* bin search coordinates
352351
if (!costmap->worldToMap(goal.pose.position.x, goal.pose.position.y, mx, my)) {
353352
throw nav2_core::GoalOutsideMapBounds(
354353
"Goal Coordinates of(" + std::to_string(goal.pose.position.x) + ", " +
355354
std::to_string(goal.pose.position.y) + ") was outside bounds");
356355
}
357-
orientation_bin = tf2::getYaw(goal.pose.orientation) / _angle_bin_size;
356+
orientation_bin = std::round(tf2::getYaw(goal.pose.orientation) / _angle_bin_size);
358357
while (orientation_bin < 0.0) {
359358
orientation_bin += static_cast<float>(_angle_quantizations);
360359
}
361360
// This is needed to handle precision issues
362361
if (orientation_bin >= static_cast<float>(_angle_quantizations)) {
363362
orientation_bin -= static_cast<float>(_angle_quantizations);
364363
}
365-
orientation_bin_id = static_cast<unsigned int>(floor(orientation_bin));
366-
_a_star->setGoal(mx, my, orientation_bin_id);
364+
_a_star->setGoal(mx, my, static_cast<unsigned int>(orientation_bin));
367365

368366
// Setup message
369367
nav_msgs::msg::Path plan;

nav2_smac_planner/test/test_nodehybrid.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ TEST(NodeHybridTest, basic_get_closest_angular_bin_test)
344344
{
345345
motion_table.bin_size = M_PI;
346346
motion_table.num_angle_quantization = 2;
347-
double test_theta = M_PI;
347+
double test_theta = M_PI / 2.0 - 0.000001;
348348
unsigned int expected_angular_bin = 0;
349349
unsigned int calculated_angular_bin = motion_table.getClosestAngularBin(test_theta);
350350
EXPECT_EQ(expected_angular_bin, calculated_angular_bin);
@@ -362,8 +362,8 @@ TEST(NodeHybridTest, basic_get_closest_angular_bin_test)
362362
{
363363
motion_table.bin_size = 0.0872664675;
364364
motion_table.num_angle_quantization = 72;
365-
double test_theta = 6.28318526567925;
366-
unsigned int expected_angular_bin = 71;
365+
double test_theta = 6.28317530718; // 0.0001 less than 2 pi
366+
unsigned int expected_angular_bin = 0; // should be closer to wrap around
367367
unsigned int calculated_angular_bin = motion_table.getClosestAngularBin(test_theta);
368368
EXPECT_EQ(expected_angular_bin, calculated_angular_bin);
369369
}

0 commit comments

Comments
 (0)