Skip to content

Commit

Permalink
feat(utils): add function to calculate shift start/end point
Browse files Browse the repository at this point in the history
Signed-off-by: satoshi-ota <[email protected]>
  • Loading branch information
satoshi-ota committed Oct 28, 2023
1 parent 1bf632d commit 3713bb7
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,16 @@ std::pair<PredictedObjects, PredictedObjects> separateObjectsByPath(
DrivableLanes generateExpandDrivableLanes(
const lanelet::ConstLanelet & lanelet, const std::shared_ptr<const PlannerData> & planner_data,
const std::shared_ptr<AvoidanceParameters> & parameters);

double calcDistanceToReturnDeadLine(
const lanelet::ConstLanelets & lanelets, const PathWithLaneId & path,
const std::shared_ptr<const PlannerData> & planner_data,
const std::shared_ptr<AvoidanceParameters> & parameters);

double calcDistanceToAvoidStartLine(
const lanelet::ConstLanelets & lanelets, const PathWithLaneId & path,
const std::shared_ptr<const PlannerData> & planner_data,
const std::shared_ptr<AvoidanceParameters> & parameters);
} // namespace behavior_path_planner::utils::avoidance

#endif // BEHAVIOR_PATH_PLANNER__UTILS__AVOIDANCE__UTILS_HPP_
140 changes: 140 additions & 0 deletions planning/behavior_path_planner/src/utils/avoidance/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
namespace behavior_path_planner::utils::avoidance
{

using autoware_perception_msgs::msg::TrafficSignalElement;
using motion_utils::calcLongitudinalOffsetPoint;
using motion_utils::calcSignedArcLength;
using motion_utils::findNearestIndex;
Expand Down Expand Up @@ -225,6 +226,86 @@ void pushUniqueVector(T & base_vector, const T & additional_vector)
{
base_vector.insert(base_vector.end(), additional_vector.begin(), additional_vector.end());
}

bool hasTrafficLightCircleColor(const TrafficSignal & tl_state, const uint8_t & lamp_color)
{
const auto it_lamp =
std::find_if(tl_state.elements.begin(), tl_state.elements.end(), [&lamp_color](const auto & x) {
return x.shape == TrafficSignalElement::CIRCLE && x.color == lamp_color;
});

return it_lamp != tl_state.elements.end();
}

bool hasTrafficLightShape(const TrafficSignal & tl_state, const uint8_t & lamp_shape)
{
const auto it_lamp = std::find_if(
tl_state.elements.begin(), tl_state.elements.end(),
[&lamp_shape](const auto & x) { return x.shape == lamp_shape; });

return it_lamp != tl_state.elements.end();
}

bool isTrafficSignalStop(const lanelet::ConstLanelet & lanelet, const TrafficSignal & tl_state)
{
if (hasTrafficLightCircleColor(tl_state, TrafficSignalElement::GREEN)) {
return false;
}

if (hasTrafficLightCircleColor(tl_state, TrafficSignalElement::UNKNOWN)) {
return false;
}

const std::string turn_direction = lanelet.attributeOr("turn_direction", "else");

if (turn_direction == "else") {
return true;
}
if (
turn_direction == "right" &&
hasTrafficLightShape(tl_state, TrafficSignalElement::RIGHT_ARROW)) {
return false;
}
if (
turn_direction == "left" && hasTrafficLightShape(tl_state, TrafficSignalElement::LEFT_ARROW)) {
return false;
}
if (
turn_direction == "straight" &&
hasTrafficLightShape(tl_state, TrafficSignalElement::UP_ARROW)) {
return false;
}

return true;
}

std::optional<double> calcDistanceToRedTrafficLight(
const lanelet::ConstLanelets & lanelets, const PathWithLaneId & path,
const std::shared_ptr<const PlannerData> & planner_data)
{
for (const auto & lanelet : lanelets) {
for (const auto & element : lanelet.regulatoryElementsAs<TrafficLight>()) {
const auto traffic_signal_stamped = planner_data->getTrafficSignal(element->id());
if (!traffic_signal_stamped.has_value()) {
continue;
}

if (!isTrafficSignalStop(lanelet, traffic_signal_stamped.value().signal)) {
continue;
}

const auto & ego_pos = planner_data->self_odometry->pose.pose.position;
lanelet::ConstLineString3d stop_line = *(element->stopLine());
const auto x = 0.5 * (stop_line.front().x() + stop_line.back().x());
const auto y = 0.5 * (stop_line.front().y() + stop_line.back().y());
const auto z = 0.5 * (stop_line.front().z() + stop_line.back().z());

return calcSignedArcLength(path.points, ego_pos, tier4_autoware_utils::createPoint(x, y, z));
}
}

return std::nullopt;
}
} // namespace

bool isOnRight(const ObjectData & obj)
Expand Down Expand Up @@ -1840,4 +1921,63 @@ DrivableLanes generateExpandDrivableLanes(

return current_drivable_lanes;
}

double calcDistanceToAvoidStartLine(
const lanelet::ConstLanelets & lanelets, const PathWithLaneId & path,
const std::shared_ptr<const PlannerData> & planner_data,
const std::shared_ptr<AvoidanceParameters> & parameters)
{
if (lanelets.empty()) {
return std::numeric_limits<double>::lowest();
}

double distance_to_return_dead_line = std::numeric_limits<double>::lowest();

// dead line stop factor(traffic light)
if (parameters->enable_dead_line_for_traffic_light) {
const auto to_traffic_light = calcDistanceToRedTrafficLight(lanelets, path, planner_data);
if (to_traffic_light.has_value()) {
distance_to_return_dead_line = std::max(
distance_to_return_dead_line,
to_traffic_light.value() + parameters->dead_line_buffer_for_traffic_light);
}
}

return distance_to_return_dead_line;
}

double calcDistanceToReturnDeadLine(
const lanelet::ConstLanelets & lanelets, const PathWithLaneId & path,
const std::shared_ptr<const PlannerData> & planner_data,
const std::shared_ptr<AvoidanceParameters> & parameters)
{
if (lanelets.empty()) {
return std::numeric_limits<double>::max();
}

double distance_to_return_dead_line = std::numeric_limits<double>::max();

// dead line stop factor(traffic light)
if (parameters->enable_dead_line_for_traffic_light) {
const auto to_traffic_light = calcDistanceToRedTrafficLight(lanelets, path, planner_data);
if (to_traffic_light.has_value()) {
distance_to_return_dead_line = std::min(
distance_to_return_dead_line,
to_traffic_light.value() - parameters->dead_line_buffer_for_traffic_light);
}
}

// dead line for goal
if (parameters->enable_dead_line_for_goal) {
if (planner_data->route_handler->isInGoalRouteSection(lanelets.back())) {
const auto & ego_pos = planner_data->self_odometry->pose.pose.position;
const auto to_goal_distance =
calcSignedArcLength(path.points, ego_pos, path.points.size() - 1);
distance_to_return_dead_line = std::min(
distance_to_return_dead_line, to_goal_distance - parameters->dead_line_buffer_for_goal);
}
}

return distance_to_return_dead_line;
}
} // namespace behavior_path_planner::utils::avoidance

0 comments on commit 3713bb7

Please sign in to comment.