Skip to content

Commit

Permalink
fix(avoidance): turn off blinker when the ego return original lane
Browse files Browse the repository at this point in the history
Signed-off-by: satoshi-ota <[email protected]>
  • Loading branch information
satoshi-ota committed Feb 5, 2024
1 parent 882494d commit 4f6e857
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,8 @@ class AvoidanceModule : public SceneModuleInterface

bool safe_{true};

std::optional<UUID> ignore_signal_{std::nullopt};

std::shared_ptr<AvoidanceHelper> helper_;

std::shared_ptr<AvoidanceParameters> parameters_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ double calcDistanceToAvoidStartLine(
const std::shared_ptr<const PlannerData> & planner_data,
const std::shared_ptr<AvoidanceParameters> & parameters);

TurnSignalInfo calcTurnSignalInfo(
std::pair<TurnSignalInfo, bool> calcTurnSignalInfo(
const ShiftedPath & path, const ShiftLine & shift_line, const double current_shift_length,
const AvoidancePlanningData & data, const std::shared_ptr<const PlannerData> & planner_data);
} // namespace behavior_path_planner::utils::avoidance
Expand Down
17 changes: 16 additions & 1 deletion planning/behavior_path_avoidance_module/src/scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -881,19 +881,34 @@ BehaviorModuleOutput AvoidanceModule::plan()

BehaviorModuleOutput output;

const auto is_ignore_signal = [this](const UUID & uuid) {
if (!ignore_signal_.has_value()) {
return false;
}

return ignore_signal_.value() == uuid;
};

const auto update_ignore_signal = [this](const UUID & uuid, const bool is_ignore) {
ignore_signal_ = is_ignore ? std::make_optional(uuid) : std::nullopt;
};

// turn signal info
if (path_shifter_.getShiftLines().empty()) {
output.turn_signal_info = getPreviousModuleOutput().turn_signal_info;
} else if (is_ignore_signal(path_shifter_.getShiftLines().front().id)) {
output.turn_signal_info = getPreviousModuleOutput().turn_signal_info;
} else {
const auto original_signal = getPreviousModuleOutput().turn_signal_info;
const auto new_signal = utils::avoidance::calcTurnSignalInfo(
const auto [new_signal, is_ignore] = utils::avoidance::calcTurnSignalInfo(
linear_shift_path, path_shifter_.getShiftLines().front(), helper_->getEgoShift(), avoid_data_,
planner_data_);
const auto current_seg_idx = planner_data_->findEgoSegmentIndex(spline_shift_path.path.points);
output.turn_signal_info = planner_data_->turn_signal_decider.use_prior_turn_signal(
spline_shift_path.path, getEgoPose(), current_seg_idx, original_signal, new_signal,
planner_data_->parameters.ego_nearest_dist_threshold,
planner_data_->parameters.ego_nearest_yaw_threshold);
update_ignore_signal(path_shifter_.getShiftLines().front().id, is_ignore);

Check warning on line 911 in planning/behavior_path_avoidance_module/src/scene.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

❌ New issue: Complex Method

AvoidanceModule::plan has a cyclomatic complexity of 10, 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.
}

// sparse resampling for computational cost
Expand Down
100 changes: 74 additions & 26 deletions planning/behavior_path_avoidance_module/src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,16 +248,37 @@ void pushUniqueVector(T & base_vector, const T & additional_vector)
base_vector.insert(base_vector.end(), additional_vector.begin(), additional_vector.end());
}

bool isAvoidShift(const double start_shift_length, const double end_shift_length)
{
constexpr double THRESHOLD = 0.1;
return std::abs(start_shift_length) < THRESHOLD && std::abs(end_shift_length) > THRESHOLD;
}

bool isReturnShift(const double start_shift_length, const double end_shift_length)
{
constexpr double THRESHOLD = 0.1;
return std::abs(start_shift_length) > THRESHOLD && std::abs(end_shift_length) < THRESHOLD;
}

bool isLeftMiddleShift(const double start_shift_length, const double end_shift_length)
{
constexpr double THRESHOLD = 0.1;
return start_shift_length > THRESHOLD && end_shift_length > THRESHOLD;
}

bool isRightMiddleShift(const double start_shift_length, const double end_shift_length)
{
constexpr double THRESHOLD = 0.1;
return start_shift_length < THRESHOLD && end_shift_length < THRESHOLD;
}

bool existShiftSideLane(
const double start_shift_length, const double end_shift_length, const bool no_left_lanes,
const bool no_right_lanes)
{
constexpr double THRESHOLD = 0.1;
const auto relative_shift_length = end_shift_length - start_shift_length;

const auto avoid_shift =
std::abs(start_shift_length) < THRESHOLD && std::abs(end_shift_length) > THRESHOLD;
if (avoid_shift) {
if (isAvoidShift(start_shift_length, end_shift_length)) {
// Left avoid. But there is no adjacent lane. No need blinker.
if (relative_shift_length > 0.0 && no_left_lanes) {
return false;
Expand All @@ -269,9 +290,7 @@ bool existShiftSideLane(
}
}

const auto return_shift =
std::abs(start_shift_length) > THRESHOLD && std::abs(end_shift_length) < THRESHOLD;
if (return_shift) {
if (isReturnShift(start_shift_length, end_shift_length)) {
// Right return. But there is no adjacent lane. No need blinker.
if (relative_shift_length > 0.0 && no_right_lanes) {
return false;
Expand All @@ -283,8 +302,7 @@ bool existShiftSideLane(
}
}

const auto left_middle_shift = start_shift_length > THRESHOLD && end_shift_length > THRESHOLD;
if (left_middle_shift) {
if (isLeftMiddleShift(start_shift_length, end_shift_length)) {
// Left avoid. But there is no adjacent lane. No need blinker.
if (relative_shift_length > 0.0 && no_left_lanes) {
return false;
Expand All @@ -296,8 +314,7 @@ bool existShiftSideLane(
}
}

const auto right_middle_shift = start_shift_length < THRESHOLD && end_shift_length < THRESHOLD;
if (right_middle_shift) {
if (isRightMiddleShift(start_shift_length, end_shift_length)) {

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

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

✅ Getting better: Complex Method

existShiftSideLane decreases in cyclomatic complexity from 25 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.
// Right avoid. But there is no adjacent lane. No need blinker.
if (relative_shift_length < 0.0 && no_right_lanes) {
return false;
Expand All @@ -312,6 +329,27 @@ bool existShiftSideLane(
return true;
}

bool isNearEndOfShift(
const double start_shift_length, const double end_shift_length, const Point & ego_pos,
const lanelet::ConstLanelets & original_lanes)
{
using boost::geometry::within;
using lanelet::utils::to2D;
using lanelet::utils::conversion::toLaneletPoint;

if (!isReturnShift(start_shift_length, end_shift_length)) {
return false;
}

for (const auto & lane : original_lanes) {
if (within(to2D(toLaneletPoint(ego_pos)), lane.polygon2d().basicPolygon())) {
return true;
}
}

return false;
}

bool straddleRoadBound(
const ShiftedPath & path, const ShiftLine & shift_line, const lanelet::ConstLanelets & lanes,
const vehicle_info_util::VehicleInfo & vehicle_info)
Expand Down Expand Up @@ -1260,11 +1298,13 @@ lanelet::ConstLanelets getCurrentLanesFromPath(
throw std::logic_error("empty path.");
}

if (path.points.front().lane_ids.empty()) {
const auto idx = planner_data->findEgoIndex(path.points);

if (path.points.at(idx).lane_ids.empty()) {
throw std::logic_error("empty lane ids.");
}

const auto start_id = path.points.front().lane_ids.front();
const auto start_id = path.points.at(idx).lane_ids.front();
const auto start_lane = planner_data->route_handler->getLaneletsFromId(start_id);
const auto & p = planner_data->parameters;

Expand Down Expand Up @@ -2224,7 +2264,7 @@ double calcDistanceToReturnDeadLine(
return distance_to_return_dead_line;
}

TurnSignalInfo calcTurnSignalInfo(
std::pair<TurnSignalInfo, bool> calcTurnSignalInfo(
const ShiftedPath & path, const ShiftLine & shift_line, const double current_shift_length,
const AvoidancePlanningData & data, const std::shared_ptr<const PlannerData> & planner_data)
{
Expand All @@ -2236,22 +2276,22 @@ TurnSignalInfo calcTurnSignalInfo(

if (shift_line.start_idx + 1 > path.shift_length.size()) {
RCLCPP_WARN(rclcpp::get_logger(__func__), "index inconsistency.");
return {};
return std::make_pair(TurnSignalInfo{}, true);
}

if (shift_line.start_idx + 1 > path.path.points.size()) {
RCLCPP_WARN(rclcpp::get_logger(__func__), "index inconsistency.");
return {};
return std::make_pair(TurnSignalInfo{}, true);
}

if (shift_line.end_idx + 1 > path.shift_length.size()) {
RCLCPP_WARN(rclcpp::get_logger(__func__), "index inconsistency.");
return {};
return std::make_pair(TurnSignalInfo{}, true);
}

if (shift_line.end_idx + 1 > path.path.points.size()) {
RCLCPP_WARN(rclcpp::get_logger(__func__), "index inconsistency.");
return {};
return std::make_pair(TurnSignalInfo{}, true);
}

const auto start_shift_length = path.shift_length.at(shift_line.start_idx);
Expand All @@ -2260,12 +2300,12 @@ TurnSignalInfo calcTurnSignalInfo(

// If shift length is shorter than the threshold, it does not need to turn on blinkers
if (std::fabs(relative_shift_length) < p.turn_signal_shift_length_threshold) {
return {};
return std::make_pair(TurnSignalInfo{}, true);
}

// If the vehicle does not shift anymore, we turn off the blinker
if (std::fabs(path.shift_length.at(shift_line.end_idx) - current_shift_length) < THRESHOLD) {
return {};
return std::make_pair(TurnSignalInfo{}, true);
}

const auto get_command = [](const auto & shift_length) {
Expand All @@ -2280,7 +2320,7 @@ TurnSignalInfo calcTurnSignalInfo(
p.vehicle_info.max_longitudinal_offset_m;

if (signal_prepare_distance < ego_front_to_shift_start) {
return {};
return std::make_pair(TurnSignalInfo{}, false);
}

const auto blinker_start_pose = path.path.points.at(shift_line.start_idx).point.pose;
Expand All @@ -2297,12 +2337,12 @@ TurnSignalInfo calcTurnSignalInfo(
turn_signal_info.turn_signal.command = get_command(relative_shift_length);

if (!p.turn_signal_on_swerving) {
return turn_signal_info;
return std::make_pair(turn_signal_info, false);
}

lanelet::ConstLanelet lanelet;
if (!rh->getClosestLaneletWithinRoute(shift_line.end, &lanelet)) {
return {};
return std::make_pair(TurnSignalInfo{}, true);
}

const auto left_same_direction_lane = rh->getLeftLanelet(lanelet, true, true);
Expand All @@ -2314,13 +2354,21 @@ TurnSignalInfo calcTurnSignalInfo(
right_same_direction_lane.has_value() || !right_opposite_lanes.empty();

if (!existShiftSideLane(start_shift_length, end_shift_length, !has_left_lane, !has_right_lane)) {
return {};
return std::make_pair(TurnSignalInfo{}, true);
}

if (!straddleRoadBound(path, shift_line, data.current_lanelets, p.vehicle_info)) {
return {};
return std::make_pair(TurnSignalInfo{}, true);
}

constexpr double STOPPED_THRESHOLD = 0.1; // [m/s]
if (ego_speed < STOPPED_THRESHOLD) {
if (isNearEndOfShift(
start_shift_length, end_shift_length, ego_pose.position, data.current_lanelets)) {
return std::make_pair(TurnSignalInfo{}, true);
}
}

return turn_signal_info;
return std::make_pair(turn_signal_info, false);

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

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

ℹ Getting worse: Complex Method

calcTurnSignalInfo increases in cyclomatic complexity from 16 to 18, 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.
}
} // namespace behavior_path_planner::utils::avoidance

0 comments on commit 4f6e857

Please sign in to comment.