Skip to content

Commit

Permalink
feat(lane_change): cancel lane change on red traffic light
Browse files Browse the repository at this point in the history
Signed-off-by: Muhammad Zulfaqar Azmi <[email protected]>
  • Loading branch information
zulfaqar-azmi-t4 committed Nov 20, 2023
1 parent 40c407a commit d65dfb1
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ struct PlannerData
std::optional<PoseWithUuidStamped> prev_modified_goal{};
std::optional<UUID> prev_route_id{};
std::shared_ptr<RouteHandler> route_handler{std::make_shared<RouteHandler>()};
std::map<int, TrafficSignalStamped> traffic_light_id_map;
std::map<int64_t, TrafficSignalStamped> traffic_light_id_map;
BehaviorPathPlannerParameters parameters{};
drivable_area_expansion::DrivableAreaExpansionParameters drivable_area_expansion_parameters{};

Expand All @@ -174,7 +174,7 @@ struct PlannerData
route_handler, path, turn_signal_info, current_pose, current_vel, parameters, debug_data);
}

std::optional<TrafficSignalStamped> getTrafficSignal(const int id) const
std::optional<TrafficSignalStamped> getTrafficSignal(const int64_t id) const

Check warning on line 177 in planning/behavior_path_planner/include/behavior_path_planner/data_manager.hpp

View check run for this annotation

Codecov / codecov/patch

planning/behavior_path_planner/include/behavior_path_planner/data_manager.hpp#L177

Added line #L177 was not covered by tests
{
if (traffic_light_id_map.count(id) == 0) {
return std::nullopt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ class LaneChangeBase
const lanelet::ConstLanelets & current_lanes, const lanelet::ConstLanelets & target_lanes,
const double threshold) const = 0;

virtual bool isStoppedAtRedTrafficLight() const = 0;

virtual bool calcAbortPath() = 0;

virtual bool specialRequiredCheck() const { return false; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ class NormalLaneChange : public LaneChangeBase

bool isLaneChangeRequired() const override;

bool isStoppedAtRedTrafficLight() const override;

protected:
lanelet::ConstLanelets getCurrentLanes() const override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <lanelet2_core/geometry/Point.h>
#include <lanelet2_core/geometry/Polygon.h>

#include <limits>
#include <memory>

namespace behavior_path_planner::utils::traffic_light
Expand Down Expand Up @@ -107,6 +108,27 @@ double getDistanceToNextTrafficLight(
std::optional<double> calcDistanceToRedTrafficLight(
const lanelet::ConstLanelets & lanelets, const PathWithLaneId & path,
const std::shared_ptr<const PlannerData> & planner_data);

/**
* @brief Checks if the vehicle is stationary within a specified distance from a red traffic light.
*
* This function first checks if the vehicle's velocity is below a minimum threshold, indicating it
* is stopped. It then calculates the distance to the nearest red traffic light using the
* calcDistanceToRedTrafficLight function. If the vehicle is within the specified distance threshold
* from the red traffic light, the function returns true, otherwise false.
*
* @param lanelets The lanelets to search for traffic lights.
* @param path The path along which to measure the distance to the traffic light.
* @param planner_data Shared pointer to the planner data containing vehicle state information.
* @param distance_threshold The maximum allowable distance from a red traffic light to consider the
* vehicle stopped.
* @return True if the vehicle is stopped within the distance threshold from a red traffic light,
* false otherwise.
*/
bool isStoppedAtRedTrafficLightWithinDistance(
const lanelet::ConstLanelets & lanelets, const PathWithLaneId & path,
const std::shared_ptr<const PlannerData> & planner_data,
const double distance_threshold = std::numeric_limits<double>::infinity());
} // namespace behavior_path_planner::utils::traffic_light

#endif // BEHAVIOR_PATH_PLANNER__UTILS__TRAFFIC_LIGHT_UTILS_HPP_
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ ModuleStatus LaneChangeInterface::updateState()
return ModuleStatus::SUCCESS;
}

if (module_type_->isEgoOnPreparePhase() && module_type_->isStoppedAtRedTrafficLight()) {
RCLCPP_WARN_STREAM_THROTTLE(

Check warning on line 112 in planning/behavior_path_planner/src/scene_module/lane_change/interface.cpp

View check run for this annotation

Codecov / codecov/patch

planning/behavior_path_planner/src/scene_module/lane_change/interface.cpp#L112

Added line #L112 was not covered by tests
getLogger().get_child(module_type_->getModuleTypeStr()), *clock_, 5000,
"Ego stopped at traffic light. Canceling lane change");
module_type_->toCancelState();
return isWaitingApproval() ? ModuleStatus::RUNNING : ModuleStatus::SUCCESS;
}

Check warning on line 118 in planning/behavior_path_planner/src/scene_module/lane_change/interface.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

❌ Getting worse: Complex Method

LaneChangeInterface::updateState increases in cyclomatic complexity from 19 to 22, 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.
const auto [is_safe, is_object_coming_from_rear] = module_type_->isApprovedPathSafe();

setObjectDebugVisualization();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ bool NormalLaneChange::isLaneChangeRequired() const
return !target_lanes.empty();
}

bool NormalLaneChange::isStoppedAtRedTrafficLight() const

Check warning on line 130 in planning/behavior_path_planner/src/scene_module/lane_change/normal.cpp

View check run for this annotation

Codecov / codecov/patch

planning/behavior_path_planner/src/scene_module/lane_change/normal.cpp#L130

Added line #L130 was not covered by tests
{
return utils::traffic_light::isStoppedAtRedTrafficLightWithinDistance(
status_.current_lanes, status_.lane_change_path.path, planner_data_,
status_.lane_change_path.info.length.sum());

Check warning on line 134 in planning/behavior_path_planner/src/scene_module/lane_change/normal.cpp

View check run for this annotation

Codecov / codecov/patch

planning/behavior_path_planner/src/scene_module/lane_change/normal.cpp#L132-L134

Added lines #L132 - L134 were not covered by tests
}

LaneChangePath NormalLaneChange::getLaneChangePath() const
{
return status_.lane_change_path;
Expand Down Expand Up @@ -1363,9 +1370,16 @@ bool NormalLaneChange::getLaneChangePaths(
if (

Check warning on line 1370 in planning/behavior_path_planner/src/scene_module/lane_change/normal.cpp

View check run for this annotation

Codecov / codecov/patch

planning/behavior_path_planner/src/scene_module/lane_change/normal.cpp#L1370

Added line #L1370 was not covered by tests
lane_change_parameters_->regulate_on_traffic_light &&
!hasEnoughLengthToTrafficLight(*candidate_path, current_lanes)) {
debug_print("Reject: regulate on traffic light!!");
continue;

Check warning on line 1374 in planning/behavior_path_planner/src/scene_module/lane_change/normal.cpp

View check run for this annotation

Codecov / codecov/patch

planning/behavior_path_planner/src/scene_module/lane_change/normal.cpp#L1374

Added line #L1374 was not covered by tests
}

if (utils::traffic_light::isStoppedAtRedTrafficLightWithinDistance(
lane_change_info.current_lanes, candidate_path.value().path, planner_data_,
lane_change_info.length.sum())) {
debug_print("Ego is stopping near traffic light. Do not allow lane change");
continue;

Check warning on line 1381 in planning/behavior_path_planner/src/scene_module/lane_change/normal.cpp

View check run for this annotation

Codecov / codecov/patch

planning/behavior_path_planner/src/scene_module/lane_change/normal.cpp#L1381

Added line #L1381 was not covered by tests
}

Check warning on line 1382 in planning/behavior_path_planner/src/scene_module/lane_change/normal.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

❌ Getting worse: Complex Method

NormalLaneChange::getLaneChangePaths increases in cyclomatic complexity from 44 to 46, 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.
candidate_paths->push_back(*candidate_path);

std::vector<ExtendedPredictedObject> filtered_objects =
Expand Down
23 changes: 23 additions & 0 deletions planning/behavior_path_planner/src/utils/traffic_light_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,27 @@ std::optional<double> calcDistanceToRedTrafficLight(

return std::nullopt;
}

bool isStoppedAtRedTrafficLightWithinDistance(

Check warning on line 158 in planning/behavior_path_planner/src/utils/traffic_light_utils.cpp

View check run for this annotation

Codecov / codecov/patch

planning/behavior_path_planner/src/utils/traffic_light_utils.cpp#L158

Added line #L158 was not covered by tests
const lanelet::ConstLanelets & lanelets, const PathWithLaneId & path,
const std::shared_ptr<const PlannerData> & planner_data, const double distance_threshold)
{
const auto ego_velocity = std::hypot(
planner_data->self_odometry->twist.twist.linear.x,
planner_data->self_odometry->twist.twist.linear.y);

Check warning on line 164 in planning/behavior_path_planner/src/utils/traffic_light_utils.cpp

View check run for this annotation

Codecov / codecov/patch

planning/behavior_path_planner/src/utils/traffic_light_utils.cpp#L162-L164

Added lines #L162 - L164 were not covered by tests
constexpr double minimum_speed = 0.1;
if (ego_velocity > minimum_speed) {
return false;
}

const auto distance_to_red_traffic_light =
calcDistanceToRedTrafficLight(lanelets, path, planner_data);

if (!distance_to_red_traffic_light) {
return false;
}

return (distance_to_red_traffic_light < distance_threshold);
}

} // namespace behavior_path_planner::utils::traffic_light

0 comments on commit d65dfb1

Please sign in to comment.