Skip to content

Commit da8a4e1

Browse files
committed
Fix missing cost consideration in first expansion of SmacPlannerHybrid (#5626)
Signed-off-by: wang-fire <[email protected]>
1 parent d536d33 commit da8a4e1

22 files changed

+84
-72
lines changed

nav2_smac_planner/include/nav2_smac_planner/goal_manager.hpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ class GoalManager
4848
typedef typename NodeT::Coordinates Coordinates;
4949
typedef typename NodeT::CoordinateVector CoordinateVector;
5050

51-
/**
52-
* @brief Constructor: Initializes empty goal state. sets and coordinate lists.
53-
*/
51+
/**
52+
* @brief Constructor: Initializes empty goal state. sets and coordinate lists.
53+
*/
5454
GoalManager()
5555
: _goals_set(NodeSet()),
5656
_goals_state(GoalStateVector()),
@@ -134,7 +134,7 @@ class GoalManager
134134
const auto size_x = collision_checker->getCostmap()->getSizeInCellsX();
135135
const auto size_y = collision_checker->getCostmap()->getSizeInCellsY();
136136

137-
auto getIndexFromPoint = [&size_x] (const Coordinates & point) {
137+
auto getIndexFromPoint = [&size_x](const Coordinates & point) {
138138
unsigned int index = 0;
139139

140140
const auto mx = static_cast<unsigned int>(point.x);
@@ -197,8 +197,9 @@ class GoalManager
197197
{
198198
// Make sure that there was a goal clear before this was run
199199
if (!_goals_set.empty() || !_goals_coordinate.empty()) {
200-
throw std::runtime_error("Goal set should be cleared before calling "
201-
"removeinvalidgoals");
200+
throw std::runtime_error(
201+
"Goal set should be cleared before calling "
202+
"removeinvalidgoals");
202203
}
203204
for (unsigned int i = 0; i < _goals_state.size(); i++) {
204205
if (_goals_state[i].goal->isNodeValid(traverse_unknown, collision_checker) ||

nav2_smac_planner/src/a_star.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,8 @@ bool AStarAlgorithm<NodeT>::createPath(
350350
}
351351

352352
NodeVector coarse_check_goals, fine_check_goals;
353-
_goal_manager.prepareGoalsForAnalyticExpansion(coarse_check_goals, fine_check_goals,
353+
_goal_manager.prepareGoalsForAnalyticExpansion(
354+
coarse_check_goals, fine_check_goals,
354355
_coarse_search_resolution);
355356

356357
// 0) Add starting point to the open set

nav2_smac_planner/src/analytic_expansion.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,10 +349,10 @@ float AnalyticExpansion<NodeT>::refineAnalyticPath(
349349

350350
float score = 0.0;
351351
float normalized_cost = 0.0;
352-
// Analytic expansions are consistently spaced
352+
// Analytic expansions are consistently spaced
353353
const float distance = hypotf(
354-
expansion.nodes[1].proposed_coords.x - expansion.nodes[0].proposed_coords.x,
355-
expansion.nodes[1].proposed_coords.y - expansion.nodes[0].proposed_coords.y);
354+
expansion.nodes[1].proposed_coords.x - expansion.nodes[0].proposed_coords.x,
355+
expansion.nodes[1].proposed_coords.y - expansion.nodes[0].proposed_coords.y);
356356
const float & weight = expansion.nodes[0].node->motion_table.cost_penalty;
357357
for (auto iter = expansion.nodes.begin(); iter != expansion.nodes.end(); ++iter) {
358358
normalized_cost = iter->node->getCost() / 252.0f;

nav2_smac_planner/src/collision_checker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ bool GridCollisionChecker::inCollision(
116116

117117
// Assumes setFootprint already set
118118
center_cost_ = static_cast<float>(costmap_->getCost(
119-
static_cast<unsigned int>(x + 0.5f), static_cast<unsigned int>(y + 0.5f)));
119+
static_cast<unsigned int>(x + 0.5f), static_cast<unsigned int>(y + 0.5f)));
120120

121121
if (!footprint_is_radius_) {
122122
// if footprint, then we check for the footprint's points, but first see

nav2_smac_planner/src/node_hybrid.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -397,11 +397,6 @@ float NodeHybrid::getTraversalCost(const NodePtr & child)
397397
"cost without a known SE2 collision cost!");
398398
}
399399

400-
// this is the first node
401-
if (getMotionPrimitiveIndex() == std::numeric_limits<unsigned int>::max()) {
402-
return NodeHybrid::travel_distance_cost;
403-
}
404-
405400
const TurnDirection & child_turn_dir = child->getTurnDirection();
406401
float travel_cost_raw = motion_table.travel_costs[child->getMotionPrimitiveIndex()];
407402
float travel_cost = 0.0;
@@ -419,7 +414,9 @@ float NodeHybrid::getTraversalCost(const NodePtr & child)
419414
// New motion is a straight motion, no additional costs to be applied
420415
travel_cost = travel_cost_raw;
421416
} else {
422-
if (getTurnDirection() == child_turn_dir) {
417+
if (getTurnDirection() == child_turn_dir ||
418+
getMotionPrimitiveIndex() == std::numeric_limits<unsigned int>::max())
419+
{
423420
// Turning motion but keeps in same direction: encourages to commit to turning if starting it
424421
travel_cost = travel_cost_raw * motion_table.non_straight_penalty;
425422
} else {

nav2_smac_planner/src/node_lattice.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,9 @@ bool NodeLattice::isNodeValid(
230230
// Check primitive end pose
231231
// Convert grid quantization of primitives to radians, then collision checker quantization
232232
static const double bin_size = 2.0 * M_PI / collision_checker->getPrecomputedAngles().size();
233-
const double angle = std::fmod(motion_table.getAngleFromBin(this->pose.theta),
234-
2.0 * M_PI) / bin_size;
233+
const double angle = std::fmod(
234+
motion_table.getAngleFromBin(this->pose.theta),
235+
2.0 * M_PI) / bin_size;
235236
if (collision_checker->inCollision(
236237
this->pose.x, this->pose.y, angle /*bin in collision checker*/, traverse_unknown))
237238
{

nav2_smac_planner/src/smac_planner_2d.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ SmacPlanner2D::dynamicParametersCallback(std::vector<rclcpp::Parameter> paramete
362362
for (auto parameter : parameters) {
363363
const auto & param_type = parameter.get_type();
364364
const auto & param_name = parameter.get_name();
365-
if(param_name.find(_name + ".") != 0) {
365+
if (param_name.find(_name + ".") != 0) {
366366
continue;
367367
}
368368
if (param_type == ParameterType::PARAMETER_DOUBLE) {

nav2_smac_planner/src/smac_planner_hybrid.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -404,10 +404,10 @@ nav_msgs::msg::Path SmacPlannerHybrid::createPlan(
404404
// Set starting point, in A* bin search coordinates
405405
float mx_start, my_start, mx_goal, my_goal;
406406
if (!costmap->worldToMapContinuous(
407-
start.pose.position.x,
408-
start.pose.position.y,
409-
mx_start,
410-
my_start))
407+
start.pose.position.x,
408+
start.pose.position.y,
409+
mx_start,
410+
my_start))
411411
{
412412
throw nav2_core::StartOutsideMapBounds(
413413
"Start Coordinates of(" + std::to_string(start.pose.position.x) + ", " +
@@ -428,10 +428,10 @@ nav_msgs::msg::Path SmacPlannerHybrid::createPlan(
428428

429429
// Set goal point, in A* bin search coordinates
430430
if (!costmap->worldToMapContinuous(
431-
goal.pose.position.x,
432-
goal.pose.position.y,
433-
mx_goal,
434-
my_goal))
431+
goal.pose.position.x,
432+
goal.pose.position.y,
433+
mx_goal,
434+
my_goal))
435435
{
436436
throw nav2_core::GoalOutsideMapBounds(
437437
"Goal Coordinates of(" + std::to_string(goal.pose.position.x) + ", " +
@@ -447,7 +447,8 @@ nav_msgs::msg::Path SmacPlannerHybrid::createPlan(
447447
}
448448
unsigned int goal_orientation_bin_int =
449449
static_cast<unsigned int>(goal_orientation_bin);
450-
_a_star->setGoal(mx_goal, my_goal, static_cast<unsigned int>(goal_orientation_bin_int),
450+
_a_star->setGoal(
451+
mx_goal, my_goal, static_cast<unsigned int>(goal_orientation_bin_int),
451452
_goal_heading_mode, _coarse_search_resolution);
452453

453454
// Setup message
@@ -625,7 +626,7 @@ SmacPlannerHybrid::dynamicParametersCallback(std::vector<rclcpp::Parameter> para
625626
for (auto parameter : parameters) {
626627
const auto & param_type = parameter.get_type();
627628
const auto & param_name = parameter.get_name();
628-
if(param_name.find(_name + ".") != 0 && param_name != "resolution") {
629+
if (param_name.find(_name + ".") != 0 && param_name != "resolution") {
629630
continue;
630631
}
631632
if (param_type == ParameterType::PARAMETER_DOUBLE) {
@@ -756,8 +757,8 @@ SmacPlannerHybrid::dynamicParametersCallback(std::vector<rclcpp::Parameter> para
756757
if (_angle_quantizations % _coarse_search_resolution != 0) {
757758
RCLCPP_WARN(
758759
_logger,
759-
"coarse iteration should be an increment of the "
760-
"number of angular bins configured. Disabling course research!"
760+
"coarse iteration should be an increment of the "
761+
"number of angular bins configured. Disabling course research!"
761762
);
762763
_coarse_search_resolution = 1;
763764
}

nav2_smac_planner/src/smac_planner_lattice.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -329,10 +329,10 @@ nav_msgs::msg::Path SmacPlannerLattice::createPlan(
329329
// Set starting point, in A* bin search coordinates
330330
float mx_start, my_start, mx_goal, my_goal;
331331
if (!_costmap->worldToMapContinuous(
332-
start.pose.position.x,
333-
start.pose.position.y,
334-
mx_start,
335-
my_start))
332+
start.pose.position.x,
333+
start.pose.position.y,
334+
mx_start,
335+
my_start))
336336
{
337337
throw nav2_core::StartOutsideMapBounds(
338338
"Start Coordinates of(" + std::to_string(start.pose.position.x) + ", " +
@@ -344,10 +344,10 @@ nav_msgs::msg::Path SmacPlannerLattice::createPlan(
344344

345345
// Set goal point, in A* bin search coordinates
346346
if (!_costmap->worldToMapContinuous(
347-
goal.pose.position.x,
348-
goal.pose.position.y,
349-
mx_goal,
350-
my_goal))
347+
goal.pose.position.x,
348+
goal.pose.position.y,
349+
mx_goal,
350+
my_goal))
351351
{
352352
throw nav2_core::GoalOutsideMapBounds(
353353
"Goal Coordinates of(" + std::to_string(goal.pose.position.x) + ", " +
@@ -357,7 +357,7 @@ nav_msgs::msg::Path SmacPlannerLattice::createPlan(
357357
NodeLattice::motion_table.getClosestAngularBin(tf2::getYaw(goal.pose.orientation));
358358
_a_star->setGoal(
359359
mx_goal, my_goal, goal_bin,
360-
_goal_heading_mode, _coarse_search_resolution);
360+
_goal_heading_mode, _coarse_search_resolution);
361361

362362
// Setup message
363363
nav_msgs::msg::Path plan;
@@ -546,7 +546,7 @@ SmacPlannerLattice::dynamicParametersCallback(std::vector<rclcpp::Parameter> par
546546
for (auto parameter : parameters) {
547547
const auto & param_type = parameter.get_type();
548548
const auto & param_name = parameter.get_name();
549-
if(param_name.find(_name + ".") != 0) {
549+
if (param_name.find(_name + ".") != 0) {
550550
continue;
551551
}
552552
if (param_type == ParameterType::PARAMETER_DOUBLE) {
@@ -638,8 +638,8 @@ SmacPlannerLattice::dynamicParametersCallback(std::vector<rclcpp::Parameter> par
638638
if (_metadata.number_of_headings % _coarse_search_resolution != 0) {
639639
RCLCPP_WARN(
640640
_logger,
641-
"coarse iteration should be an increment of the number<"
642-
" of angular bins configured. Disabling course research!"
641+
"coarse iteration should be an increment of the number<"
642+
" of angular bins configured. Disabling course research!"
643643
);
644644
_coarse_search_resolution = 1;
645645
}

nav2_smac_planner/src/smoother.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ bool Smoother::smooth(
6464
bool success = true, reversing_segment;
6565
nav_msgs::msg::Path curr_path_segment;
6666
curr_path_segment.header = path.header;
67-
std::vector<PathSegment> path_segments = nav2_util::findDirectionalPathSegments(path,
68-
is_holonomic_);
67+
std::vector<PathSegment> path_segments = nav2_util::findDirectionalPathSegments(
68+
path,
69+
is_holonomic_);
6970

7071
for (unsigned int i = 0; i != path_segments.size(); i++) {
7172
if (path_segments[i].end - path_segments[i].start > 10) {

0 commit comments

Comments
 (0)