Skip to content

Commit

Permalink
update for checkConsistency()
Browse files Browse the repository at this point in the history
Signed-off-by: Yuki Takagi <[email protected]>
  • Loading branch information
yuki-takagi-66 committed May 24, 2024
1 parent fd5827e commit 11628a3
Showing 1 changed file with 40 additions and 43 deletions.
83 changes: 40 additions & 43 deletions planning/obstacle_cruise_planner/src/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -815,12 +815,6 @@ ObstacleCruisePlannerNode::determineEgoBehaviorAgainstObstacles(
}
slow_down_condition_counter_.removeCounterUnlessUpdated();

std::sort(
stop_obstacles.begin(), stop_obstacles.end(),
[](const StopObstacle & o1, const StopObstacle & o2) {
return o1.dist_to_collide_on_decimated_traj < o2.dist_to_collide_on_decimated_traj;
});

// Check target obstacles' consistency
checkConsistency(objects.header.stamp, objects, stop_obstacles);

Expand Down Expand Up @@ -1381,50 +1375,53 @@ void ObstacleCruisePlannerNode::checkConsistency(
const rclcpp::Time & current_time, const PredictedObjects & predicted_objects,
std::vector<StopObstacle> & stop_obstacles)
{
const auto current_closest_stop_obstacle =
obstacle_cruise_utils::getClosestStopObstacle(stop_obstacles);

if (!prev_closest_stop_obstacle_ptr_) {
if (current_closest_stop_obstacle) {
prev_closest_stop_obstacle_ptr_ =
std::make_shared<StopObstacle>(*current_closest_stop_obstacle);
std::vector<StopObstacle> current_closest_stop_obstacles{};
std::sort(
stop_obstacles.begin(), stop_obstacles.end(),
[](const StopObstacle & o1, const StopObstacle & o2) {
return o1.dist_to_collide_on_decimated_traj < o2.dist_to_collide_on_decimated_traj;
});
for (const auto & stop_obstacle : stop_obstacles) {
if (std::none_of(
current_closest_stop_obstacles.begin(), current_closest_stop_obstacles.end(),
[&stop_obstacle](StopObstacle & cco) {
return cco.classification.label == stop_obstacle.classification.label;
})) {
current_closest_stop_obstacles.push_back(stop_obstacle);
}
return;
}

const auto predicted_object_itr = std::find_if(
predicted_objects.objects.begin(), predicted_objects.objects.end(),
[&](PredictedObject predicted_object) {
return tier4_autoware_utils::toHexString(predicted_object.object_id) ==
prev_closest_stop_obstacle_ptr_->uuid;
});
// If previous closest obstacle disappear from the perception result, do nothing anymore.
if (predicted_object_itr == predicted_objects.objects.end()) {
return;
}
static std::vector<StopObstacle> prev_closest_stop_obstacles{};
for (const auto & prev_closest_stop_obstacle : prev_closest_stop_obstacles) {
const auto predicted_object_itr = std::find_if(
predicted_objects.objects.begin(), predicted_objects.objects.end(),
[&](PredictedObject predicted_object) {
return tier4_autoware_utils::toHexString(predicted_object.object_id) ==
prev_closest_stop_obstacle_ptr_->uuid;
});
// If previous closest obstacle disappear from the perception result, do nothing anymore.
if (predicted_object_itr == predicted_objects.objects.end()) {
continue;
}

const auto is_disappeared_from_stop_obstacle = std::none_of(
stop_obstacles.begin(), stop_obstacles.end(),
[&](const auto & obstacle) { return obstacle.uuid == prev_closest_stop_obstacle_ptr_->uuid; });
if (is_disappeared_from_stop_obstacle) {
// re-evaluate as a stop candidate, and overwrite the current decision if "maintain stop"
// condition is satisfied
const double elapsed_time = (current_time - prev_closest_stop_obstacle_ptr_->stamp).seconds();
if (
predicted_object_itr->kinematics.initial_twist_with_covariance.twist.linear.x <
behavior_determination_param_.obstacle_velocity_threshold_from_stop_to_cruise &&
elapsed_time < behavior_determination_param_.stop_obstacle_hold_time_threshold) {
stop_obstacles.push_back(*prev_closest_stop_obstacle_ptr_);
return;
const auto is_disappeared_from_stop_obstacle = std::none_of(
stop_obstacles.begin(), stop_obstacles.end(),
[&](const auto & obstacle) { return obstacle.uuid == prev_closest_stop_obstacle.uuid; });
if (is_disappeared_from_stop_obstacle) {
// re-evaluate as a stop candidate, and overwrite the current decision if "maintain stop"
// condition is satisfied
const double elapsed_time = (current_time - prev_closest_stop_obstacle.stamp).seconds();
if (
predicted_object_itr->kinematics.initial_twist_with_covariance.twist.linear.x <
behavior_determination_param_.obstacle_velocity_threshold_from_stop_to_cruise &&
elapsed_time < behavior_determination_param_.stop_obstacle_hold_time_threshold) {
stop_obstacles.push_back(prev_closest_stop_obstacle);
current_closest_stop_obstacles.push_back(prev_closest_stop_obstacle);
}
}
}

if (current_closest_stop_obstacle) {
prev_closest_stop_obstacle_ptr_ =
std::make_shared<StopObstacle>(*current_closest_stop_obstacle);
} else {
prev_closest_stop_obstacle_ptr_ = nullptr;
}
prev_closest_stop_obstacles = std::move(current_closest_stop_obstacles);

Check warning on line 1424 in planning/obstacle_cruise_planner/src/node.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

❌ New issue: Complex Method

ObstacleCruisePlannerNode::checkConsistency has a cyclomatic complexity of 9, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.
}

bool ObstacleCruisePlannerNode::isObstacleCrossing(
Expand Down

0 comments on commit 11628a3

Please sign in to comment.