25#include "absl/strings/str_cat.h"
26#include "google/protobuf/text_format.h"
28#include "modules/common_msgs/map_msgs/map_id.pb.h"
49const double kTrajectoryApproximationMaxError = 2.0;
56 return command.has_lane_follow_command();
62 ACHECK(lane) <<
"Invalid lane id: " << waypoint.
id();
69 ACHECK(lane) <<
"Invalid lane id: " << segment.
id();
73void LaneFollowMap::UpdateNextRoutingWaypointIndex(
int cur_index) {
75 next_routing_waypoint_index_ = 0;
78 if (cur_index >=
static_cast<int>(route_indices_.size())) {
79 next_routing_waypoint_index_ = routing_waypoint_index_.size() - 1;
83 while (next_routing_waypoint_index_ != 0 &&
84 next_routing_waypoint_index_ < routing_waypoint_index_.size() &&
85 routing_waypoint_index_[next_routing_waypoint_index_].index >
87 --next_routing_waypoint_index_;
89 while (next_routing_waypoint_index_ != 0 &&
90 next_routing_waypoint_index_ < routing_waypoint_index_.size() &&
91 routing_waypoint_index_[next_routing_waypoint_index_].index ==
94 routing_waypoint_index_[next_routing_waypoint_index_].waypoint.s) {
95 --next_routing_waypoint_index_;
98 while (next_routing_waypoint_index_ < routing_waypoint_index_.size() &&
99 routing_waypoint_index_[next_routing_waypoint_index_].index <
101 ++next_routing_waypoint_index_;
103 while (next_routing_waypoint_index_ < routing_waypoint_index_.size() &&
105 routing_waypoint_index_[next_routing_waypoint_index_].index &&
107 routing_waypoint_index_[next_routing_waypoint_index_].waypoint.s) {
108 ++next_routing_waypoint_index_;
110 if (next_routing_waypoint_index_ >= routing_waypoint_index_.size()) {
111 next_routing_waypoint_index_ = routing_waypoint_index_.size() - 1;
116 const auto &waypoints =
118 return std::vector<routing::LaneWaypoint>(
119 waypoints.begin() + next_routing_waypoint_index_, waypoints.end());
123 std::shared_ptr<routing::LaneWaypoint> &end_point)
const {
129 const auto &routing_request =
131 if (routing_request.waypoint().size() < 1) {
135 end_point = std::make_shared<routing::LaneWaypoint>();
136 end_point->CopyFrom(*(routing_request.waypoint().rbegin()));
140 if (
nullptr == hdmap_) {
151 const int num_road = routing.road_size();
155 if (!routing.has_routing_request() ||
156 routing.routing_request().waypoint_size() < 2) {
157 AERROR <<
"Routing does not have request.";
160 for (
const auto &waypoint : routing.routing_request().waypoint()) {
161 if (!waypoint.has_id() || !waypoint.has_s()) {
162 AERROR <<
"Routing waypoint has no lane_id or s.";
169void LaneFollowMap::UpdateRoutingRange(
int adc_index) {
171 range_lane_ids_.clear();
172 range_start_ = std::max(0, adc_index - 1);
173 range_end_ = range_start_;
174 while (range_end_ <
static_cast<int>(route_indices_.size())) {
175 const auto &lane_id = route_indices_[range_end_].segment.lane->id().id();
176 if (range_lane_ids_.count(lane_id) != 0) {
179 range_lane_ids_.insert(lane_id);
184bool LaneFollowMap::UpdateVehicleState(
const VehicleState &vehicle_state) {
186 AERROR <<
"The routing is invalid when updating vehicle state.";
187 route_segments_lane_ids_.clear();
190 if (!adc_state_.has_x() ||
192 FLAGS_replan_lateral_distance_threshold +
193 FLAGS_replan_longitudinal_distance_threshold)) {
195 next_routing_waypoint_index_ = 0;
196 adc_route_index_ = -1;
197 stop_for_destination_ =
false;
200 adc_state_ = vehicle_state;
202 AERROR <<
"Failed to get waypoint from routing with point: " <<
"("
203 << vehicle_state.
x() <<
", " << vehicle_state.y() <<
", "
204 << vehicle_state.z() <<
").";
205 route_segments_lane_ids_.clear();
209 if (route_index < 0 ||
210 route_index >=
static_cast<int>(route_indices_.size())) {
217 UpdateNextRoutingWaypointIndex(route_index);
218 adc_route_index_ = route_index;
219 UpdateRoutingRange(adc_route_index_);
221 if (routing_waypoint_index_.empty()) {
222 AERROR <<
"No routing waypoint index.";
226 if (next_routing_waypoint_index_ == routing_waypoint_index_.size() - 1) {
227 stop_for_destination_ =
true;
235 AERROR <<
"Command cannot be processed by LaneFollowMap!";
242 range_lane_ids_.clear();
243 route_indices_.clear();
244 all_lane_ids_.clear();
245 for (
int road_index = 0; road_index < routing.road_size(); ++road_index) {
246 const auto &road_segment = routing.road(road_index);
247 for (
int passage_index = 0; passage_index < road_segment.passage_size();
249 const auto &passage = road_segment.passage(passage_index);
250 for (
int lane_index = 0; lane_index < passage.segment_size();
252 all_lane_ids_.insert(passage.segment(lane_index).id());
253 route_indices_.emplace_back();
254 route_indices_.back().segment =
255 ToLaneSegment(passage.segment(lane_index));
256 if (route_indices_.back().segment.lane ==
nullptr) {
257 AERROR <<
"Failed to get lane segment from passage.";
260 route_indices_.back().index = {road_index, passage_index, lane_index};
267 adc_route_index_ = -1;
268 next_routing_waypoint_index_ = 0;
269 UpdateRoutingRange(adc_route_index_);
271 routing_waypoint_index_.clear();
272 const auto &request_waypoints = routing.routing_request().waypoint();
273 if (request_waypoints.empty()) {
274 AERROR <<
"Invalid routing: no request waypoints.";
278 for (
size_t j = 0; j < route_indices_.size(); ++j) {
279 while (i < request_waypoints.size() &&
281 request_waypoints.Get(i))) {
282 routing_waypoint_index_.emplace_back(
284 request_waypoints.Get(i).s()),
290 stop_for_destination_ =
false;
294int LaneFollowMap::SearchForwardWaypointIndex(
296 int i = std::max(start, 0);
297 while (i <
static_cast<int>(route_indices_.size()) &&
305int LaneFollowMap::SearchBackwardWaypointIndex(
306 int start,
const hdmap::LaneWaypoint &waypoint)
const {
307 int i = std::min(
static_cast<int>(route_indices_.size() - 1), start);
309 route_indices_[i].segment, waypoint)) {
315int LaneFollowMap::NextWaypointIndex(
int index)
const {
316 if (index >=
static_cast<int>(route_indices_.size() - 1)) {
317 return static_cast<int>(route_indices_.size()) - 1;
318 }
else if (index < 0) {
325int LaneFollowMap::GetWaypointIndex(
const hdmap::LaneWaypoint &waypoint)
const {
326 int forward_index = SearchForwardWaypointIndex(adc_route_index_, waypoint);
327 if (forward_index >=
static_cast<int>(route_indices_.size())) {
328 return SearchBackwardWaypointIndex(adc_route_index_, waypoint);
330 if (forward_index == adc_route_index_ ||
331 forward_index == adc_route_index_ + 1) {
332 return forward_index;
334 auto backward_index = SearchBackwardWaypointIndex(adc_route_index_, waypoint);
335 if (backward_index < 0) {
336 return forward_index;
339 return (backward_index + 1 == adc_route_index_) ? backward_index
343bool LaneFollowMap::PassageToSegments(routing::Passage passage,
344 hdmap::RouteSegments *segments)
const {
345 CHECK_NOTNULL(segments);
347 for (
const auto &lane : passage.segment()) {
350 AERROR <<
"Failed to find lane: " << lane.id();
353 segments->emplace_back(lane_ptr, std::max(0.0, lane.start_s()),
354 std::min(lane_ptr->total_length(), lane.end_s()));
356 return !segments->empty();
359std::vector<int> LaneFollowMap::GetNeighborPassages(
360 const routing::RoadSegment &road,
int start_passage)
const {
361 CHECK_GE(start_passage, 0);
362 CHECK_LE(start_passage, road.passage_size());
363 std::vector<int> result;
364 const auto &source_passage = road.passage(start_passage);
365 result.emplace_back(start_passage);
369 if (source_passage.can_exit()) {
372 hdmap::RouteSegments source_segments;
373 if (!PassageToSegments(source_passage, &source_segments)) {
374 AERROR <<
"Failed to convert passage to segments";
377 if (next_routing_waypoint_index_ < routing_waypoint_index_.size() &&
378 source_segments.IsWaypointOnSegment(
379 routing_waypoint_index_[next_routing_waypoint_index_].waypoint)) {
380 ADEBUG <<
"Need to pass next waypoint[" << next_routing_waypoint_index_
381 <<
"] before change lane";
384 std::unordered_set<std::string> neighbor_lanes;
386 for (
const auto &segment : source_segments) {
387 for (
const auto &left_id :
388 segment.lane->lane().left_neighbor_forward_lane_id()) {
389 neighbor_lanes.insert(left_id.id());
393 for (
const auto &segment : source_segments) {
394 for (
const auto &right_id :
395 segment.lane->lane().right_neighbor_forward_lane_id()) {
396 neighbor_lanes.insert(right_id.id());
401 for (
int i = 0; i < road.passage_size(); ++i) {
402 if (i == start_passage) {
405 const auto &target_passage = road.passage(i);
406 for (
const auto &segment : target_passage.segment()) {
407 if (neighbor_lanes.count(segment.
id())) {
408 result.emplace_back(i);
416 const VehicleState &vehicle_state,
417 std::list<hdmap::RouteSegments> *
const route_segments) {
418 double look_forward_distance =
420 double look_backward_distance = FLAGS_look_backward_distance;
422 look_forward_distance, route_segments);
426 const VehicleState &vehicle_state,
const double backward_length,
427 const double forward_length,
428 std::list<hdmap::RouteSegments> *
const route_segments) {
429 if (!UpdateVehicleState(vehicle_state)) {
430 AERROR <<
"Failed to update vehicle state in pnc_map.";
436 adc_route_index_ >=
static_cast<int>(route_indices_.size())) {
437 AERROR <<
"Invalid vehicle state in pnc_map, update vehicle state first.";
440 const auto &route_index = route_indices_[adc_route_index_].index;
441 const int road_index = route_index[0];
442 const int passage_index = route_index[1];
445 auto drive_passages = GetNeighborPassages(road, passage_index);
446 for (
const int index : drive_passages) {
447 const auto &passage = road.passage(index);
448 hdmap::RouteSegments segments;
449 if (!PassageToSegments(passage, &segments)) {
450 ADEBUG <<
"Failed to convert passage to lane segments.";
453 const PointENU nearest_point =
454 index == passage_index
458 hdmap::LaneWaypoint segment_waypoint;
459 if (!segments.GetProjection(nearest_point, &sl, &segment_waypoint)) {
460 ADEBUG <<
"Failed to get projection from point: "
461 << nearest_point.ShortDebugString();
464 if (index != passage_index) {
466 ADEBUG <<
"You cannot drive from current waypoint to passage: "
471 route_segments->emplace_back();
472 const auto last_waypoint = segments.LastWaypoint();
474 sl.s() + forward_length, &route_segments->back())) {
475 AERROR <<
"Failed to extend segments with s=" << sl.s()
476 <<
", backward: " << backward_length
477 <<
", forward: " << forward_length;
480 if (route_segments->back().IsWaypointOnSegment(last_waypoint)) {
481 route_segments->back().SetRouteEndWaypoint(last_waypoint);
483 route_segments->back().SetCanExit(passage.can_exit());
484 route_segments->back().SetNextAction(passage.change_lane_type());
485 const std::string route_segment_id = absl::StrCat(road_index,
"_", index);
486 route_segments->back().SetId(route_segment_id);
487 route_segments->back().SetStopForDestination(stop_for_destination_);
488 if (index == passage_index) {
489 route_segments->back().SetIsOnSegment(
true);
491 }
else if (sl.l() > 0) {
497 UpdateRouteSegmentsLaneIds(route_segments);
498 return !route_segments->empty();
503 waypoint->
lane =
nullptr;
504 std::vector<hdmap::LaneInfoConstPtr> lanes;
506 std::vector<hdmap::LaneInfoConstPtr> valid_lanes;
507 for (
auto lane_id : all_lane_ids_) {
510 if (
nullptr != lane) {
511 valid_lanes.emplace_back(lane);
516 std::vector<hdmap::LaneWaypoint> valid_way_points;
517 for (
const auto &lane : valid_lanes) {
518 if (range_lane_ids_.count(lane->id().id()) == 0) {
519 ADEBUG <<
"not in range" << lane->id().id();
522 if (route_segments_lane_ids_.size() > 0 &&
523 route_segments_lane_ids_.count(lane->id().id()) == 0) {
524 ADEBUG <<
"not in last frame route_segments: " << lane->id().id();
530 if (!lane->GetProjection({point.x(), point.y()}, &s, &l)) {
533 ADEBUG << lane->id().id() <<
"," << s <<
"," << l;
535 static constexpr double kEpsilon = 0.5;
536 if (s > (lane->total_length() + kEpsilon) || (s + kEpsilon) < 0.0) {
539 double lane_heading = lane->Heading(s);
546 valid_way_points.emplace_back();
547 auto &last = valid_way_points.back();
551 ADEBUG <<
"distance:" << std::fabs(l);
553 if (valid_way_points.empty()) {
554 AERROR <<
"Failed to find nearest point: " << point.ShortDebugString();
559 int closest_index = -1;
560 double distance = std::numeric_limits<double>::max();
561 double lane_heading = 0.0;
562 double vehicle_heading = state.
heading();
563 for (
size_t i = 0; i < valid_way_points.size(); i++) {
564 lane_heading = valid_way_points[i].lane->Heading(valid_way_points[i].s);
569 if (std::fabs(valid_way_points[i].l) < distance) {
570 distance = std::fabs(valid_way_points[i].l);
574 if (closest_index == -1) {
575 AERROR <<
"Can not find nearest waypoint. vehicle heading:"
576 << vehicle_heading <<
"lane heading:" << lane_heading;
579 waypoint->
lane = valid_way_points[closest_index].lane;
580 waypoint->
s = valid_way_points[closest_index].s;
581 waypoint->
l = valid_way_points[closest_index].l;
587 if (lane->lane().successor_id().empty()) {
590 hdmap::Id preferred_id = lane->lane().successor_id(0);
591 for (
const auto &lane_id : lane->lane().successor_id()) {
592 if (range_lane_ids_.count(lane_id.id()) != 0) {
593 preferred_id = lane_id;
602 if (lane->lane().predecessor_id().empty()) {
606 std::unordered_set<std::string> predecessor_ids;
607 for (
const auto &lane_id : lane->lane().predecessor_id()) {
608 predecessor_ids.insert(lane_id.id());
611 hdmap::Id preferred_id = lane->lane().predecessor_id(0);
612 for (
const auto &route_index : route_indices_) {
613 auto &lane = route_index.segment.lane->
id();
614 if (predecessor_ids.count(lane.id()) != 0) {
623 const common::PointENU &point,
624 double look_backward,
double look_forward,
625 hdmap::RouteSegments *extended_segments) {
627 hdmap::LaneWaypoint waypoint;
628 if (!segments.GetProjection(point, &sl, &waypoint)) {
629 AERROR <<
"point: " << point.ShortDebugString() <<
" is not on segment";
632 return ExtendSegments(segments, sl.s() - look_backward, sl.s() + look_forward,
639 if (segments.empty()) {
640 AERROR <<
"The input segments is empty";
643 CHECK_NOTNULL(truncated_segments);
646 if (start_s >= end_s) {
647 AERROR <<
"start_s(" << start_s <<
" >= end_s(" << end_s <<
")";
650 std::unordered_set<std::string> unique_lanes;
651 static constexpr double kRouteEpsilon = 1e-3;
654 const auto &first_segment = *segments.begin();
655 auto lane = first_segment.lane;
656 double s = first_segment.start_s;
657 double extend_s = -start_s;
658 std::vector<hdmap::LaneSegment> extended_lane_segments;
659 while (extend_s > kRouteEpsilon) {
660 if (s <= kRouteEpsilon) {
661 lane = GetRoutePredecessor(lane);
662 if (lane ==
nullptr ||
663 unique_lanes.find(lane->id().id()) != unique_lanes.end()) {
666 s = lane->total_length();
668 const double length = std::min(s, extend_s);
669 extended_lane_segments.emplace_back(lane, s - length, s);
672 unique_lanes.insert(lane->id().id());
675 truncated_segments->insert(truncated_segments->begin(),
676 extended_lane_segments.rbegin(),
677 extended_lane_segments.rend());
679 bool found_loop =
false;
681 for (
const auto &lane_segment : segments) {
682 const double adjusted_start_s = std::max(
683 start_s - router_s + lane_segment.start_s, lane_segment.start_s);
684 const double adjusted_end_s =
685 std::min(end_s - router_s + lane_segment.start_s, lane_segment.end_s);
686 if (adjusted_start_s < adjusted_end_s) {
687 if (!truncated_segments->empty() &&
688 truncated_segments->back().lane->id().id() ==
689 lane_segment.lane->id().id()) {
690 truncated_segments->back().end_s = adjusted_end_s;
691 }
else if (unique_lanes.find(lane_segment.lane->id().id()) ==
692 unique_lanes.end()) {
693 truncated_segments->emplace_back(lane_segment.lane, adjusted_start_s,
695 unique_lanes.insert(lane_segment.lane->id().id());
701 router_s += (lane_segment.end_s - lane_segment.start_s);
702 if (router_s > end_s) {
710 if (router_s < end_s && !truncated_segments->empty()) {
711 auto &back = truncated_segments->back();
712 if (back.lane->total_length() > back.end_s) {
713 double origin_end_s = back.end_s;
715 std::min(back.end_s + end_s - router_s, back.lane->total_length());
716 router_s += back.end_s - origin_end_s;
719 auto last_lane = segments.back().lane;
720 while (router_s < end_s - kRouteEpsilon) {
721 last_lane = GetRouteSuccessor(last_lane);
722 if (last_lane ==
nullptr ||
723 unique_lanes.find(last_lane->id().id()) != unique_lanes.end()) {
726 const double length = std::min(end_s - router_s, last_lane->total_length());
727 truncated_segments->emplace_back(last_lane, 0, length);
728 unique_lanes.insert(last_lane->id().id());
734void LaneFollowMap::AppendLaneToPoints(
736 std::vector<hdmap::MapPathPoint> *
const points) {
737 if (points ==
nullptr || start_s >= end_s) {
740 double accumulate_s = 0.0;
741 for (
size_t i = 0; i < lane->points().size(); ++i) {
742 if (accumulate_s >= start_s && accumulate_s <= end_s) {
743 points->emplace_back(lane->points()[i], lane->headings()[i],
746 if (i < lane->segments().size()) {
747 const auto &segment = lane->segments()[i];
748 const double next_accumulate_s = accumulate_s + segment.length();
749 if (start_s > accumulate_s && start_s < next_accumulate_s) {
750 points->emplace_back(segment.start() + segment.unit_direction() *
751 (start_s - accumulate_s),
753 hdmap::LaneWaypoint(lane, start_s));
755 if (end_s > accumulate_s && end_s < next_accumulate_s) {
756 points->emplace_back(
757 segment.start() + segment.unit_direction() * (end_s - accumulate_s),
758 lane->headings()[i], hdmap::LaneWaypoint(lane, end_s));
760 accumulate_s = next_accumulate_s;
762 if (accumulate_s > end_s) {
768void LaneFollowMap::UpdateRouteSegmentsLaneIds(
769 const std::list<hdmap::RouteSegments> *route_segments) {
770 route_segments_lane_ids_.clear();
771 for (
auto &route_seg : *route_segments) {
772 for (
auto &lane_seg : route_seg) {
773 if (
nullptr == lane_seg.lane) {
776 route_segments_lane_ids_.insert(lane_seg.lane->id().id());
786 if (adc_route_index_ < 0 || adc_route_index_ >= route_indices_.size()) {
787 AERROR <<
"adc_route_index error, can not get distance to destination, "
792 int adc_road_index = route_indices_[adc_route_index_].index[0];
793 int adc_passage_index = route_indices_[adc_route_index_].index[1];
794 int adc_lane_index = route_indices_[adc_route_index_].index[2];
796 bool get_adc_exit_waypoint =
797 routing.road(adc_road_index).passage(adc_passage_index).can_exit();
798 int start_passage_index = adc_passage_index;
799 int start_lane_index = adc_lane_index;
802 double dis_to_destination = 0.0;
804 for (
int road_index = adc_road_index; road_index < routing.road_size();
806 const auto &road_segment = routing.road(road_index);
807 for (
int passage_index = 0; passage_index < road_segment.passage_size();
809 const auto &passage = road_segment.passage(passage_index);
810 if (!passage.can_exit()) {
814 if (!get_adc_exit_waypoint) {
815 get_adc_exit_waypoint =
true;
817 for (
int index = 0; index < passage.segment_size(); ++index) {
822 if (!lane->GetProjection({adc_state_.x(), adc_state_.y()}, &s, &l)) {
825 static constexpr double kEpsilon = 0.5;
826 if (s > (lane->total_length() + kEpsilon) || (s + kEpsilon) < 0.0) {
829 start_passage_index = passage_index;
830 start_lane_index = index;
836 if (get_adc_exit_waypoint) {
837 if (road_index == adc_road_index &&
838 passage_index == start_passage_index) {
839 for (
int lane_index = start_lane_index;
840 lane_index < passage.segment_size(); ++lane_index) {
841 if (lane_index == start_lane_index) {
842 dis_to_destination +=
843 passage.segment(lane_index).end_s() - start_lane_s;
845 dis_to_destination += passage.segment(lane_index).end_s() -
846 passage.segment(lane_index).start_s();
850 for (
int lane_index = 0; lane_index < passage.segment_size();
852 dis_to_destination += passage.segment(lane_index).end_s() -
853 passage.segment(lane_index).start_s();
860 return dis_to_destination;
static PointENU ToPointENU(const double x, const double y, const double z=0)
LaneInfoConstPtr GetLaneById(const Id &id) const
void SetProperties(const RouteSegments &other)
Copy the properties of other segments to current one
static bool WithinLaneSegment(const LaneSegment &lane_segment, const LaneWaypoint &waypoint)
std::vector< routing::LaneWaypoint > FutureRouteWaypoints() const override
apollo::hdmap::LaneWaypoint GetAdcWaypoint() const override
bool GetRouteSegments(const common::VehicleState &vehicle_state, std::list< apollo::hdmap::RouteSegments > *const route_segments) override
use heuristic forward length and backward length
hdmap::LaneInfoConstPtr GetLaneById(const hdmap::Id &id) const override
Get the Lane with the given id.
bool CanProcess(const planning::PlanningCommand &command) const override
bool UpdatePlanningCommand(const planning::PlanningCommand &command) override
double GetDistanceToDestination() const override
void GetEndLaneWayPoint(std::shared_ptr< routing::LaneWaypoint > &end_point) const override
Get the end point of PlanningCommand.
bool ExtendSegments(const apollo::hdmap::RouteSegments &segments, double start_s, double end_s, apollo::hdmap::RouteSegments *const truncated_segments) const override
bool GetNearestPointFromRouting(const common::VehicleState &state, apollo::hdmap::LaneWaypoint *waypoint) const override
static double LookForwardDistance(const double velocity)
virtual bool UpdatePlanningCommand(const planning::PlanningCommand &command)
apollo::hdmap::LaneWaypoint adc_waypoint_
The waypoint of the autonomous driving car
planning::PlanningCommand last_command_
Planning module main class.
double AngleDiff(const double from, const double to)
Calculate the difference between angle from and to
double DistanceXY(const U &u, const V &v)
calculate the distance beteween Point u and Point v, which are all have member function x() and y() i...
apollo::hdmap::Id MakeMapId(const std::string &id)
create a Map ID given a string.
std::shared_ptr< const LaneInfo > LaneInfoConstPtr
Some string util functions.
std::string DebugString() const
optional apollo::routing::RoutingResponse lane_follow_command
repeated apollo::routing::LaneWaypoint waypoint
optional RoutingRequest routing_request
repeated apollo::routing::RoadSegment road