Skip to content

Commit 9209372

Browse files
refactor
Signed-off-by: Daniel Sanchez <danielsanchezaran@gmail.com>
1 parent c33eb34 commit 9209372

File tree

1 file changed

+25
-33
lines changed
  • planning/obstacle_cruise_planner/src

1 file changed

+25
-33
lines changed

planning/obstacle_cruise_planner/src/node.cpp

+25-33
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,6 @@ void ObstacleCruisePlannerNode::onTrajectory(const Trajectory::ConstSharedPtr ms
499499
}
500500

501501
stop_watch_.tic(__func__);
502-
const auto & p = behavior_determination_param_;
503502
*debug_data_ptr_ = DebugData();
504503

505504
const auto is_driving_forward = motion_utils::isDrivingForwardWithTwist(traj_points);
@@ -509,17 +508,7 @@ void ObstacleCruisePlannerNode::onTrajectory(const Trajectory::ConstSharedPtr ms
509508
// (1) with a proper label
510509
// (2) in front of ego
511510
// (3) not too far from trajectory
512-
const auto target_obstacles = [&]() {
513-
auto target_obstacles = convertToObstacles(traj_points);
514-
// Sort from closest to farthest obstacle to speed up yield
515-
if (p.enable_yield) {
516-
std::sort(
517-
target_obstacles.begin(), target_obstacles.end(), [](const auto & o1, const auto & o2) {
518-
return o1.ego_to_obstacle_distance < o2.ego_to_obstacle_distance;
519-
});
520-
}
521-
return target_obstacles;
522-
}();
511+
const auto target_obstacles = convertToObstacles(traj_points);
523512

524513
// 2. Determine ego's behavior against each obstacle from stop, cruise and slow down.
525514
const auto & [stop_obstacles, cruise_obstacles, slow_down_obstacles] =
@@ -940,63 +929,66 @@ std::optional<std::vector<CruiseObstacle>> ObstacleCruisePlannerNode::findYieldC
940929
if (obstacles.empty() || traj_points.empty()) return std::nullopt;
941930
const auto & p = behavior_determination_param_;
942931

943-
// obstacles are sorted by closest to farthest, we want to preserve the order
944-
std::vector<std::pair<size_t, Obstacle>> indexed_obstacles;
945-
for (std::size_t i = 0; i < obstacles.size(); ++i) {
946-
indexed_obstacles.emplace_back(i, obstacles[i]);
947-
}
948-
949-
std::vector<std::pair<size_t, Obstacle>> stopped_obstacles;
950-
std::vector<std::pair<size_t, Obstacle>> moving_obstacles;
932+
std::vector<Obstacle> stopped_obstacles;
933+
std::vector<Obstacle> moving_obstacles;
951934

952935
std::for_each(
953-
indexed_obstacles.begin(), indexed_obstacles.end(),
936+
obstacles.begin(), obstacles.end(),
954937
[&stopped_obstacles, &moving_obstacles, &p](const auto & o) {
955-
const bool is_moving = std::hypot(o.second.twist.linear.x, o.second.twist.linear.y) >
956-
p.stopped_obstacle_velocity_threshold;
938+
const bool is_moving =
939+
std::hypot(o.twist.linear.x, o.twist.linear.y) > p.stopped_obstacle_velocity_threshold;
957940
if (is_moving) {
958941
const bool is_within_lat_dist_threshold =
959-
o.second.lat_dist_from_obstacle_to_traj < p.yield_lat_distance_threshold;
942+
o.lat_dist_from_obstacle_to_traj < p.yield_lat_distance_threshold;
960943
if (is_within_lat_dist_threshold) moving_obstacles.push_back(o);
961944
return;
962945
}
963946
// lat threshold is larger for stopped obstacles
964947
const bool is_within_lat_dist_threshold =
965-
o.second.lat_dist_from_obstacle_to_traj <
948+
o.lat_dist_from_obstacle_to_traj <
966949
p.yield_lat_distance_threshold + p.max_lat_dist_between_obstacles;
967950
if (is_within_lat_dist_threshold) stopped_obstacles.push_back(o);
968951
return;
969952
});
970953

971954
if (stopped_obstacles.empty() || moving_obstacles.empty()) return std::nullopt;
955+
956+
std::sort(
957+
stopped_obstacles.begin(), stopped_obstacles.end(), [](const auto & o1, const auto & o2) {
958+
return o1.ego_to_obstacle_distance < o2.ego_to_obstacle_distance;
959+
});
960+
961+
std::sort(moving_obstacles.begin(), moving_obstacles.end(), [](const auto & o1, const auto & o2) {
962+
return o1.ego_to_obstacle_distance < o2.ego_to_obstacle_distance;
963+
});
964+
972965
std::vector<CruiseObstacle> yield_obstacles;
973966
for (const auto & moving_obstacle : moving_obstacles) {
974967
for (const auto & stopped_obstacle : stopped_obstacles) {
975968
const bool is_moving_obs_behind_of_stopped_obs =
976-
moving_obstacle.first < stopped_obstacle.first;
969+
moving_obstacle.ego_to_obstacle_distance < stopped_obstacle.ego_to_obstacle_distance;
977970
const bool is_moving_obs_ahead_of_ego_front =
978-
moving_obstacle.second.ego_to_obstacle_distance > vehicle_info_.vehicle_length_m;
971+
moving_obstacle.ego_to_obstacle_distance > vehicle_info_.vehicle_length_m;
979972

980973
if (!is_moving_obs_ahead_of_ego_front || !is_moving_obs_behind_of_stopped_obs) continue;
981974

982975
const double lateral_distance_between_obstacles = std::abs(
983-
moving_obstacle.second.lat_dist_from_obstacle_to_traj -
984-
stopped_obstacle.second.lat_dist_from_obstacle_to_traj);
976+
moving_obstacle.lat_dist_from_obstacle_to_traj -
977+
stopped_obstacle.lat_dist_from_obstacle_to_traj);
985978

986979
const double longitudinal_distance_between_obstacles = std::abs(
987-
moving_obstacle.second.ego_to_obstacle_distance -
988-
stopped_obstacle.second.ego_to_obstacle_distance);
980+
moving_obstacle.ego_to_obstacle_distance - stopped_obstacle.ego_to_obstacle_distance);
989981

990982
const double moving_obstacle_speed =
991-
std::hypot(moving_obstacle.second.twist.linear.x, moving_obstacle.second.twist.linear.y);
983+
std::hypot(moving_obstacle.twist.linear.x, moving_obstacle.twist.linear.y);
992984

993985
const bool are_obstacles_aligned =
994986
lateral_distance_between_obstacles < p.max_lat_dist_between_obstacles;
995987
const bool obstacles_collide_within_threshold_time =
996988
longitudinal_distance_between_obstacles / moving_obstacle_speed <
997989
p.max_obstacles_collision_time;
998990
if (are_obstacles_aligned && obstacles_collide_within_threshold_time) {
999-
const auto yield_obstacle = createYieldCruiseObstacle(moving_obstacle.second, traj_points);
991+
const auto yield_obstacle = createYieldCruiseObstacle(moving_obstacle, traj_points);
1000992
if (yield_obstacle) {
1001993
yield_obstacles.push_back(*yield_obstacle);
1002994
}

0 commit comments

Comments
 (0)