Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(avoidance): improve avoidance safety check logic #6974

Merged
merged 2 commits into from
May 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -97,16 +97,18 @@ class AvoidanceHelper
return std::max(getEgoSpeed(), values.at(idx));
}

double getMinimumPrepareDistance() const
double getNominalPrepareDistance(const double velocity) const
{
const auto & p = parameters_;
return std::max(getEgoSpeed() * p->min_prepare_time, p->min_prepare_distance);
const auto & values = p->velocity_map;
const auto idx = getConstraintsMapIndex(velocity, values);
return std::max(values.at(idx) * p->max_prepare_time, p->min_prepare_distance);
}

double getNominalPrepareDistance() const
{
const auto & p = parameters_;
return std::max(getEgoSpeed() * p->max_prepare_time, p->min_prepare_distance);
return std::max(getAvoidanceEgoSpeed() * p->max_prepare_time, p->min_prepare_distance);
}

double getNominalAvoidanceDistance(const double shift_length) const
@@ -298,6 +300,13 @@ class AvoidanceHelper
return std::numeric_limits<double>::max();
}

bool isEnoughPrepareDistance(const double prepare_distance) const
{
const auto & p = parameters_;
return prepare_distance >
std::max(getEgoSpeed() * p->min_prepare_distance, p->min_prepare_distance);
}

bool isComfortable(const AvoidLineArray & shift_lines) const
{
const auto JERK_BUFFER = 0.1; // [m/sss]
@@ -328,7 +337,7 @@ class AvoidanceHelper
const auto desire_shift_length =
getShiftLength(object, is_object_on_right, object.avoid_margin.value());

const auto prepare_distance = getMinimumPrepareDistance();
const auto prepare_distance = getNominalPrepareDistance(0.0);
const auto constant_distance = getFrontConstantDistance(object);
const auto avoidance_distance = getMinAvoidanceDistance(desire_shift_length);

9 changes: 5 additions & 4 deletions planning/behavior_path_avoidance_module/src/scene.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 TIER IV, Inc.

Check notice on line 1 in planning/behavior_path_avoidance_module/src/scene.cpp

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

ℹ Getting worse: Lines of Code in a Single File

The lines of code increases from 1241 to 1242, improve code health by reducing it to 1000. The number of Lines of Code in a single file. More Lines of Code lowers the code health.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -409,9 +409,9 @@
const auto registered_lines = path_shifter_.getShiftLines();
if (!registered_lines.empty()) {
const size_t idx = planner_data_->findEgoIndex(path_shifter_.getReferencePath().points);
const auto to_shift_start_point = motion_utils::calcSignedArcLength(
const auto prepare_distance = motion_utils::calcSignedArcLength(
path_shifter_.getReferencePath().points, idx, registered_lines.front().start_idx);
if (to_shift_start_point < helper_->getMinimumPrepareDistance()) {
if (!helper_->isEnoughPrepareDistance(prepare_distance)) {
RCLCPP_DEBUG(
getLogger(),
"Distance to shift start point is less than minimum prepare distance. The distance is not "
@@ -1418,10 +1418,11 @@
const auto avoidance_distance = helper_->getMinAvoidanceDistance(
helper_->getShiftLength(object, utils::avoidance::isOnRight(object), avoid_margin));
const auto constant_distance = helper_->getFrontConstantDistance(object);
const auto prepare_distance = helper_->getNominalPrepareDistance(0.0);

return object.longitudinal -
std::min(
avoidance_distance + constant_distance + p->min_prepare_distance + p->stop_buffer,
avoidance_distance + constant_distance + prepare_distance + p->stop_buffer,
p->stop_max_distance);
}

@@ -1450,7 +1451,7 @@
const auto buffer = std::max(0.0, to_shifted_path_end - to_reference_path_end);

const auto min_return_distance =
helper_->getMinAvoidanceDistance(shift_length) + helper_->getMinimumPrepareDistance();
helper_->getMinAvoidanceDistance(shift_length) + helper_->getNominalPrepareDistance(0.0);
const auto to_stop_line = data.to_return_point - min_return_distance - buffer;

// If we don't need to consider deceleration constraints, insert a deceleration point
Original file line number Diff line number Diff line change
@@ -929,7 +929,7 @@ void ShiftLineGenerator::applySmallShiftFilter(
continue;
}

if (s.start_longitudinal + 1e-3 < helper_->getMinimumPrepareDistance()) {
if (!helper_->isEnoughPrepareDistance(s.start_longitudinal)) {
continue;
}

@@ -1173,13 +1173,13 @@ AvoidLineArray ShiftLineGenerator::addReturnShiftLine(
std::max(nominal_prepare_distance - last_sl_distance, 0.0);

double prepare_distance_scaled = std::max(
helper_->getMinimumPrepareDistance(), std::max(nominal_prepare_distance, last_sl_distance));
helper_->getNominalPrepareDistance(), std::max(nominal_prepare_distance, last_sl_distance));
double avoid_distance_scaled = nominal_avoid_distance;
if (remaining_distance < prepare_distance_scaled + avoid_distance_scaled) {
const auto scale = (remaining_distance - last_sl_distance) /
std::max(nominal_avoid_distance + variable_prepare_distance, 0.1);
prepare_distance_scaled = std::max(
helper_->getMinimumPrepareDistance(), last_sl_distance + scale * nominal_prepare_distance);
helper_->getNominalPrepareDistance(), last_sl_distance + scale * nominal_prepare_distance);
avoid_distance_scaled *= scale;
}

@@ -1292,7 +1292,7 @@ AvoidLineArray ShiftLineGenerator::findNewShiftLine(
const auto & candidate = shift_lines.at(i);

// prevent sudden steering.
if (candidate.start_longitudinal + 1e-3 < helper_->getMinimumPrepareDistance()) {
if (!helper_->isEnoughPrepareDistance(candidate.start_longitudinal)) {
break;
}

2 changes: 1 addition & 1 deletion planning/behavior_path_avoidance_module/src/utils.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 TIER IV, Inc.

Check notice on line 1 in planning/behavior_path_avoidance_module/src/utils.cpp

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

ℹ Getting worse: Overall Code Complexity

The mean cyclomatic complexity increases from 5.07 to 5.08, threshold = 4. This file has many conditional statements (e.g. if, for, while) across its implementation, leading to lower code health. Avoid adding more conditionals.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -1910,7 +1910,7 @@
std::for_each(objects.begin(), objects.end(), [&p, &ret, &parameters](const auto & object) {
if (filtering_utils::isSafetyCheckTargetObjectType(object.object, parameters)) {
// check only moving objects
if (filtering_utils::isMovingObject(object, parameters)) {
if (filtering_utils::isMovingObject(object, parameters) || !object.is_parked) {

Check warning on line 1913 in planning/behavior_path_avoidance_module/src/utils.cpp

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

❌ Getting worse: Complex Method

getSafetyCheckTargetObjects increases in cyclomatic complexity from 20 to 21, 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.
ret.objects.push_back(object.object);
}
}