Skip to content

Commit

Permalink
fix(avoidance): consider avoidance prepare distance
Browse files Browse the repository at this point in the history
Signed-off-by: satoshi-ota <[email protected]>
  • Loading branch information
satoshi-ota committed Nov 12, 2023
1 parent 55d04fc commit fb4fe8b
Showing 1 changed file with 35 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -907,8 +907,8 @@ AvoidOutlines AvoidanceModule::generateAvoidOutline(
const auto & base_link2rear = planner_data_->parameters.base_link2rear;

// Calculate feasible shift length
const auto get_shift_length =
[&](auto & object, const auto & desire_shift_length) -> boost::optional<double> {
const auto get_shift_profile = [&](auto & object, const auto & desire_shift_length)

Check warning on line 910 in planning/behavior_path_planner/src/scene_module/avoidance/avoidance_module.cpp

View check run for this annotation

Codecov / codecov/patch

planning/behavior_path_planner/src/scene_module/avoidance/avoidance_module.cpp#L910

Added line #L910 was not covered by tests
-> boost::optional<std::pair<double, double>> {
// use each object param
const auto object_type = utils::getHighestProbLabel(object.object.classification);
const auto object_parameter = parameters_->object_parameters.at(object_type);
Expand All @@ -918,44 +918,51 @@ AvoidOutlines AvoidanceModule::generateAvoidOutline(
const auto avoiding_shift = desire_shift_length - current_ego_shift;
const auto nominal_avoid_distance = helper_.getMaxAvoidanceDistance(avoiding_shift);

// calculate remaining distance.
const auto prepare_distance = helper_.getNominalPrepareDistance();
const auto & additional_buffer_longitudinal =
object_parameter.use_conservative_buffer_longitudinal

Check warning on line 924 in planning/behavior_path_planner/src/scene_module/avoidance/avoidance_module.cpp

View check run for this annotation

Codecov / codecov/patch

planning/behavior_path_planner/src/scene_module/avoidance/avoidance_module.cpp#L924

Added line #L924 was not covered by tests
? planner_data_->parameters.base_link2front
: 0.0;
const auto constant = object_parameter.safety_buffer_longitudinal +

Check warning on line 927 in planning/behavior_path_planner/src/scene_module/avoidance/avoidance_module.cpp

View check run for this annotation

Codecov / codecov/patch

planning/behavior_path_planner/src/scene_module/avoidance/avoidance_module.cpp#L927

Added line #L927 was not covered by tests
additional_buffer_longitudinal + prepare_distance;
const auto has_enough_distance = object.longitudinal > constant + nominal_avoid_distance;
const auto remaining_distance = object.longitudinal - constant;

Check warning on line 930 in planning/behavior_path_planner/src/scene_module/avoidance/avoidance_module.cpp

View check run for this annotation

Codecov / codecov/patch

planning/behavior_path_planner/src/scene_module/avoidance/avoidance_module.cpp#L929-L930

Added lines #L929 - L930 were not covered by tests
const auto avoidance_distance =
has_enough_distance ? nominal_avoid_distance : remaining_distance;

// nominal case. avoidable.
if (has_enough_distance) {
return std::make_pair(desire_shift_length, avoidance_distance);
}

if (!isBestEffort(parameters_->policy_lateral_margin)) {
return desire_shift_length;
return std::make_pair(desire_shift_length, avoidance_distance);
}

// ego already has enough positive shift.
const auto has_enough_positive_shift = avoiding_shift < -1e-3 && desire_shift_length > 1e-3;
if (is_object_on_right && has_enough_positive_shift) {
return desire_shift_length;
return std::make_pair(desire_shift_length, avoidance_distance);
}

// ego already has enough negative shift.
const auto has_enough_negative_shift = avoiding_shift > 1e-3 && desire_shift_length < -1e-3;
if (!is_object_on_right && has_enough_negative_shift) {
return desire_shift_length;
return std::make_pair(desire_shift_length, avoidance_distance);
}

// don't relax shift length since it can stop in front of the object.
if (object.is_stoppable && !parameters_->use_shorten_margin_immediately) {
return desire_shift_length;
return std::make_pair(desire_shift_length, avoidance_distance);
}

// calculate remaining distance.
const auto prepare_distance = helper_.getNominalPrepareDistance();
const auto & additional_buffer_longitudinal =
object_parameter.use_conservative_buffer_longitudinal
? planner_data_->parameters.base_link2front
: 0.0;
const auto constant = object_parameter.safety_buffer_longitudinal +
additional_buffer_longitudinal + prepare_distance;
const auto has_enough_distance = object.longitudinal > constant + nominal_avoid_distance;
const auto remaining_distance = object.longitudinal - constant;

// the avoidance path is already approved
const auto & object_pos = object.object.kinematics.initial_pose_with_covariance.pose.position;
const auto is_approved = (helper_.getShift(object_pos) > 0.0 && is_object_on_right) ||
(helper_.getShift(object_pos) < 0.0 && !is_object_on_right);
if (is_approved) {
return desire_shift_length;
return std::make_pair(desire_shift_length, avoidance_distance);
}

// prepare distance is not enough. unavoidable.
Expand All @@ -964,18 +971,13 @@ AvoidOutlines AvoidanceModule::generateAvoidOutline(
return boost::none;
}

// nominal case. avoidable.
if (has_enough_distance) {
return desire_shift_length;
}

// calculate lateral jerk.
const auto required_jerk = PathShifter::calcJerkFromLatLonDistance(
avoiding_shift, remaining_distance, helper_.getAvoidanceEgoSpeed());

// relax lateral jerk limit. avoidable.
if (required_jerk < helper_.getLateralMaxJerkLimit()) {
return desire_shift_length;
return std::make_pair(desire_shift_length, avoidance_distance);
}

// avoidance distance is not enough. unavoidable.
Expand Down Expand Up @@ -1013,7 +1015,7 @@ AvoidOutlines AvoidanceModule::generateAvoidOutline(
std::abs(avoiding_shift), feasible_relative_shift_length);
}

return feasible_shift_length;
return std::make_pair(feasible_shift_length, avoidance_distance);
};

const auto is_forward_object = [](const auto & object) { return object.longitudinal > 0.0; };
Expand Down Expand Up @@ -1048,9 +1050,9 @@ AvoidOutlines AvoidanceModule::generateAvoidOutline(
// use each object param
const auto object_type = utils::getHighestProbLabel(o.object.classification);
const auto object_parameter = parameters_->object_parameters.at(object_type);
const auto feasible_shift_length = get_shift_length(o, desire_shift_length);
const auto feasible_shift_profile = get_shift_profile(o, desire_shift_length);

if (!feasible_shift_length) {
if (!feasible_shift_profile) {
if (o.avoid_required && is_forward_object(o)) {
break;
} else {
Expand All @@ -1059,10 +1061,8 @@ AvoidOutlines AvoidanceModule::generateAvoidOutline(
}

// use absolute dist for return-to-center, relative dist from current for avoiding.
const auto feasible_avoid_distance =
helper_.getMaxAvoidanceDistance(feasible_shift_length.get() - current_ego_shift);
const auto feasible_return_distance =
helper_.getMaxAvoidanceDistance(feasible_shift_length.get());
helper_.getMaxAvoidanceDistance(feasible_shift_profile.get().first);

AvoidLine al_avoid;
{
Expand All @@ -1078,14 +1078,15 @@ AvoidOutlines AvoidanceModule::generateAvoidOutline(

// start point (use previous linear shift length as start shift length.)
al_avoid.start_longitudinal = [&]() {
const auto nearest_avoid_distance = std::max(to_shift_end - feasible_avoid_distance, 1e-3);
const auto nearest_avoid_distance =
std::max(to_shift_end - feasible_shift_profile.get().second, 1e-3);

if (data.to_start_point > to_shift_end) {
return nearest_avoid_distance;
}

const auto minimum_avoid_distance =
helper_.getMinAvoidanceDistance(feasible_shift_length.get() - current_ego_shift);
helper_.getMinAvoidanceDistance(feasible_shift_profile.get().first - current_ego_shift);

Check warning on line 1089 in planning/behavior_path_planner/src/scene_module/avoidance/avoidance_module.cpp

View check run for this annotation

Codecov / codecov/patch

planning/behavior_path_planner/src/scene_module/avoidance/avoidance_module.cpp#L1089

Added line #L1089 was not covered by tests
const auto furthest_avoid_distance = std::max(to_shift_end - minimum_avoid_distance, 1e-3);

return std::clamp(data.to_start_point, nearest_avoid_distance, furthest_avoid_distance);
Expand All @@ -1097,7 +1098,7 @@ AvoidOutlines AvoidanceModule::generateAvoidOutline(
al_avoid.start_shift_length = helper_.getLinearShift(al_avoid.start.position);

// end point
al_avoid.end_shift_length = feasible_shift_length.get();
al_avoid.end_shift_length = feasible_shift_profile.get().first;

Check warning on line 1101 in planning/behavior_path_planner/src/scene_module/avoidance/avoidance_module.cpp

View check run for this annotation

Codecov / codecov/patch

planning/behavior_path_planner/src/scene_module/avoidance/avoidance_module.cpp#L1101

Added line #L1101 was not covered by tests
al_avoid.end_longitudinal = to_shift_end;

// misc
Expand All @@ -1112,7 +1113,7 @@ AvoidOutlines AvoidanceModule::generateAvoidOutline(
const auto to_shift_start = o.longitudinal + offset;

// start point
al_return.start_shift_length = feasible_shift_length.get();
al_return.start_shift_length = feasible_shift_profile.get().first;

Check warning on line 1116 in planning/behavior_path_planner/src/scene_module/avoidance/avoidance_module.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

❌ Getting worse: Complex Method

AvoidanceModule::generateAvoidOutline increases in cyclomatic complexity from 43 to 44, 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.

Check warning on line 1116 in planning/behavior_path_planner/src/scene_module/avoidance/avoidance_module.cpp

View check run for this annotation

Codecov / codecov/patch

planning/behavior_path_planner/src/scene_module/avoidance/avoidance_module.cpp#L1116

Added line #L1116 was not covered by tests
al_return.start_longitudinal = to_shift_start;

// end point
Expand Down

0 comments on commit fb4fe8b

Please sign in to comment.