Apollo 11.0
自动驾驶开放平台
lane_follow_map.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2017 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
21
22#include <algorithm>
23#include <limits>
24
25#include "absl/strings/str_cat.h"
26#include "google/protobuf/text_format.h"
27
28#include "modules/common_msgs/map_msgs/map_id.pb.h"
29
30#include "cyber/common/log.h"
37
38namespace apollo {
39namespace planning {
40
45
46namespace {
47
48// Maximum lateral error used in trajectory approximation.
49const double kTrajectoryApproximationMaxError = 2.0;
50
51} // namespace
52
53LaneFollowMap::LaneFollowMap() : hdmap_(hdmap::HDMapUtil::BaseMapPtr()) {}
54
56 return command.has_lane_follow_command();
57}
58
59hdmap::LaneWaypoint LaneFollowMap::ToLaneWaypoint(
60 const routing::LaneWaypoint &waypoint) const {
61 auto lane = hdmap_->GetLaneById(hdmap::MakeMapId(waypoint.id()));
62 ACHECK(lane) << "Invalid lane id: " << waypoint.id();
63 return hdmap::LaneWaypoint(lane, waypoint.s());
64}
65
66hdmap::LaneSegment LaneFollowMap::ToLaneSegment(
67 const routing::LaneSegment &segment) const {
68 auto lane = hdmap_->GetLaneById(hdmap::MakeMapId(segment.id()));
69 ACHECK(lane) << "Invalid lane id: " << segment.id();
70 return hdmap::LaneSegment(lane, segment.start_s(), segment.end_s());
71}
72
73void LaneFollowMap::UpdateNextRoutingWaypointIndex(int cur_index) {
74 if (cur_index < 0) {
75 next_routing_waypoint_index_ = 0;
76 return;
77 }
78 if (cur_index >= static_cast<int>(route_indices_.size())) {
79 next_routing_waypoint_index_ = routing_waypoint_index_.size() - 1;
80 return;
81 }
82 // Search backwards when the car is driven backward on the route.
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 >
86 cur_index) {
87 --next_routing_waypoint_index_;
88 }
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 ==
92 cur_index &&
94 routing_waypoint_index_[next_routing_waypoint_index_].waypoint.s) {
95 --next_routing_waypoint_index_;
96 }
97 // Search forwards
98 while (next_routing_waypoint_index_ < routing_waypoint_index_.size() &&
99 routing_waypoint_index_[next_routing_waypoint_index_].index <
100 cur_index) {
101 ++next_routing_waypoint_index_;
102 }
103 while (next_routing_waypoint_index_ < routing_waypoint_index_.size() &&
104 cur_index ==
105 routing_waypoint_index_[next_routing_waypoint_index_].index &&
107 routing_waypoint_index_[next_routing_waypoint_index_].waypoint.s) {
108 ++next_routing_waypoint_index_;
109 }
110 if (next_routing_waypoint_index_ >= routing_waypoint_index_.size()) {
111 next_routing_waypoint_index_ = routing_waypoint_index_.size() - 1;
112 }
113}
114
115std::vector<routing::LaneWaypoint> LaneFollowMap::FutureRouteWaypoints() const {
116 const auto &waypoints =
118 return std::vector<routing::LaneWaypoint>(
119 waypoints.begin() + next_routing_waypoint_index_, waypoints.end());
120}
121
123 std::shared_ptr<routing::LaneWaypoint> &end_point) const {
124 if (!last_command_.has_lane_follow_command() ||
125 !last_command_.lane_follow_command().has_routing_request()) {
126 end_point = nullptr;
127 return;
128 }
129 const auto &routing_request =
131 if (routing_request.waypoint().size() < 1) {
132 end_point = nullptr;
133 return;
134 }
135 end_point = std::make_shared<routing::LaneWaypoint>();
136 end_point->CopyFrom(*(routing_request.waypoint().rbegin()));
137}
138
140 if (nullptr == hdmap_) {
141 return nullptr;
142 }
143 return hdmap_->GetLaneById(id);
144}
145
146bool LaneFollowMap::IsValid(const planning::PlanningCommand &command) const {
147 if (!CanProcess(command)) {
148 return false;
149 }
150 const auto &routing = command.lane_follow_command();
151 const int num_road = routing.road_size();
152 if (num_road == 0) {
153 return false;
154 }
155 if (!routing.has_routing_request() ||
156 routing.routing_request().waypoint_size() < 2) {
157 AERROR << "Routing does not have request.";
158 return false;
159 }
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.";
163 return false;
164 }
165 }
166 return true;
167}
168
169void LaneFollowMap::UpdateRoutingRange(int adc_index) {
170 // Track routing range.
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) {
177 break;
178 }
179 range_lane_ids_.insert(lane_id);
180 ++range_end_;
181 }
182}
183
184bool LaneFollowMap::UpdateVehicleState(const VehicleState &vehicle_state) {
185 if (!IsValid(last_command_)) {
186 AERROR << "The routing is invalid when updating vehicle state.";
187 route_segments_lane_ids_.clear();
188 return false;
189 }
190 if (!adc_state_.has_x() ||
191 (common::util::DistanceXY(adc_state_, vehicle_state) >
192 FLAGS_replan_lateral_distance_threshold +
193 FLAGS_replan_longitudinal_distance_threshold)) {
194 // Position is reset, but not replan.
195 next_routing_waypoint_index_ = 0;
196 adc_route_index_ = -1;
197 stop_for_destination_ = false;
198 }
199
200 adc_state_ = vehicle_state;
201 if (!GetNearestPointFromRouting(vehicle_state, &adc_waypoint_)) {
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();
206 return false;
207 }
208 int route_index = GetWaypointIndex(adc_waypoint_);
209 if (route_index < 0 ||
210 route_index >= static_cast<int>(route_indices_.size())) {
211 AERROR << "Cannot find waypoint: " << adc_waypoint_.DebugString();
212 return false;
213 }
214 ADEBUG << "adc_waypoint_" << adc_waypoint_.DebugString() << "route_index"
215 << route_index;
216 // Track how many routing request waypoints the adc have passed.
217 UpdateNextRoutingWaypointIndex(route_index);
218 adc_route_index_ = route_index;
219 UpdateRoutingRange(adc_route_index_);
220
221 if (routing_waypoint_index_.empty()) {
222 AERROR << "No routing waypoint index.";
223 return false;
224 }
225
226 if (next_routing_waypoint_index_ == routing_waypoint_index_.size() - 1) {
227 stop_for_destination_ = true;
228 }
229 return true;
230}
231
233 const planning::PlanningCommand &command) {
234 if (!CanProcess(command)) {
235 AERROR << "Command cannot be processed by LaneFollowMap!";
236 return false;
237 }
238 if (!PncMapBase::UpdatePlanningCommand(command)) {
239 return false;
240 }
241 const auto &routing = command.lane_follow_command();
242 range_lane_ids_.clear();
243 route_indices_.clear();
244 all_lane_ids_.clear();
245 route_segments_lane_ids_.clear();
246 for (int road_index = 0; road_index < routing.road_size(); ++road_index) {
247 const auto &road_segment = routing.road(road_index);
248 for (int passage_index = 0; passage_index < road_segment.passage_size();
249 ++passage_index) {
250 const auto &passage = road_segment.passage(passage_index);
251 for (int lane_index = 0; lane_index < passage.segment_size();
252 ++lane_index) {
253 all_lane_ids_.insert(passage.segment(lane_index).id());
254 route_indices_.emplace_back();
255 route_indices_.back().segment =
256 ToLaneSegment(passage.segment(lane_index));
257 if (route_indices_.back().segment.lane == nullptr) {
258 AERROR << "Failed to get lane segment from passage.";
259 return false;
260 }
261 route_indices_.back().index = {road_index, passage_index, lane_index};
262
263 if (road_index == routing.road_size() - 1 &&
264 lane_index == passage.segment_size() - 1 && passage.can_exit()) {
265 dest_lane_segment_ = passage.segment(lane_index);
266 }
267 }
268 }
269 }
270
271 range_start_ = 0;
272 range_end_ = 0;
273 adc_route_index_ = -1;
274 next_routing_waypoint_index_ = 0;
275 UpdateRoutingRange(adc_route_index_);
276
277 routing_waypoint_index_.clear();
278 const auto &request_waypoints = routing.routing_request().waypoint();
279 if (request_waypoints.empty()) {
280 AERROR << "Invalid routing: no request waypoints.";
281 return false;
282 }
283 int i = 0;
284 for (size_t j = 0; j < route_indices_.size(); ++j) {
285 while (i < request_waypoints.size() &&
286 hdmap::RouteSegments::WithinLaneSegment(route_indices_[j].segment,
287 request_waypoints.Get(i))) {
288 routing_waypoint_index_.emplace_back(
289 hdmap::LaneWaypoint(route_indices_[j].segment.lane,
290 request_waypoints.Get(i).s()),
291 j);
292 ++i;
293 }
294 }
296 stop_for_destination_ = false;
297 return true;
298}
299
300int LaneFollowMap::SearchForwardWaypointIndex(
301 int start, const hdmap::LaneWaypoint &waypoint) const {
302 int i = std::max(start, 0);
303 while (i < static_cast<int>(route_indices_.size()) &&
304 !hdmap::RouteSegments::WithinLaneSegment(route_indices_[i].segment,
305 waypoint)) {
306 ++i;
307 }
308 return i;
309}
310
311int LaneFollowMap::SearchBackwardWaypointIndex(
312 int start, const hdmap::LaneWaypoint &waypoint) const {
313 int i = std::min(static_cast<int>(route_indices_.size() - 1), start);
315 route_indices_[i].segment, waypoint)) {
316 --i;
317 }
318 return i;
319}
320
321int LaneFollowMap::NextWaypointIndex(int index) const {
322 if (index >= static_cast<int>(route_indices_.size() - 1)) {
323 return static_cast<int>(route_indices_.size()) - 1;
324 } else if (index < 0) {
325 return 0;
326 } else {
327 return index + 1;
328 }
329}
330
331int LaneFollowMap::GetWaypointIndex(const hdmap::LaneWaypoint &waypoint) const {
332 int forward_index = SearchForwardWaypointIndex(adc_route_index_, waypoint);
333 if (forward_index >= static_cast<int>(route_indices_.size())) {
334 return SearchBackwardWaypointIndex(adc_route_index_, waypoint);
335 }
336 if (forward_index == adc_route_index_ ||
337 forward_index == adc_route_index_ + 1) {
338 return forward_index;
339 }
340 auto backward_index = SearchBackwardWaypointIndex(adc_route_index_, waypoint);
341 if (backward_index < 0) {
342 return forward_index;
343 }
344
345 return (backward_index + 1 == adc_route_index_) ? backward_index
346 : forward_index;
347}
348
349bool LaneFollowMap::PassageToSegments(routing::Passage passage,
350 hdmap::RouteSegments *segments) const {
351 CHECK_NOTNULL(segments);
352 segments->clear();
353 for (const auto &lane : passage.segment()) {
354 auto lane_ptr = hdmap_->GetLaneById(hdmap::MakeMapId(lane.id()));
355 if (!lane_ptr) {
356 AERROR << "Failed to find lane: " << lane.id();
357 return false;
358 }
359 segments->emplace_back(lane_ptr, std::max(0.0, lane.start_s()),
360 std::min(lane_ptr->total_length(), lane.end_s()));
361 }
362 return !segments->empty();
363}
364
365std::vector<int> LaneFollowMap::GetNeighborPassages(
366 const routing::RoadSegment &road, int start_passage) const {
367 CHECK_GE(start_passage, 0);
368 CHECK_LE(start_passage, road.passage_size());
369 std::vector<int> result;
370 const auto &source_passage = road.passage(start_passage);
371 result.emplace_back(start_passage);
372 if (source_passage.change_lane_type() == routing::FORWARD) {
373 return result;
374 }
375 if (source_passage.can_exit()) { // No need to change lane
376 return result;
377 }
378 hdmap::RouteSegments source_segments;
379 if (!PassageToSegments(source_passage, &source_segments)) {
380 AERROR << "Failed to convert passage to segments";
381 return result;
382 }
383 if (next_routing_waypoint_index_ < routing_waypoint_index_.size() &&
384 source_segments.IsWaypointOnSegment(
385 routing_waypoint_index_[next_routing_waypoint_index_].waypoint)) {
386 ADEBUG << "Need to pass next waypoint[" << next_routing_waypoint_index_
387 << "] before change lane";
388 return result;
389 }
390 std::unordered_set<std::string> neighbor_lanes;
391 if (source_passage.change_lane_type() == routing::LEFT) {
392 for (const auto &segment : source_segments) {
393 for (const auto &left_id :
394 segment.lane->lane().left_neighbor_forward_lane_id()) {
395 neighbor_lanes.insert(left_id.id());
396 }
397 }
398 } else if (source_passage.change_lane_type() == routing::RIGHT) {
399 for (const auto &segment : source_segments) {
400 for (const auto &right_id :
401 segment.lane->lane().right_neighbor_forward_lane_id()) {
402 neighbor_lanes.insert(right_id.id());
403 }
404 }
405 }
406
407 for (int i = 0; i < road.passage_size(); ++i) {
408 if (i == start_passage) {
409 continue;
410 }
411 const auto &target_passage = road.passage(i);
412 for (const auto &segment : target_passage.segment()) {
413 if (neighbor_lanes.count(segment.id())) {
414 result.emplace_back(i);
415 break;
416 }
417 }
418 }
419 return result;
420}
422 const VehicleState &vehicle_state,
423 std::list<hdmap::RouteSegments> *const route_segments) {
424 double look_forward_distance =
425 LookForwardDistance(vehicle_state.linear_velocity());
426 double look_backward_distance = FLAGS_look_backward_distance;
427 return GetRouteSegments(vehicle_state, look_backward_distance,
428 look_forward_distance, route_segments);
429}
430
432 const VehicleState &vehicle_state, const double backward_length,
433 const double forward_length,
434 std::list<hdmap::RouteSegments> *const route_segments) {
435 if (!UpdateVehicleState(vehicle_state)) {
436 AERROR << "Failed to update vehicle state in pnc_map.";
437 return false;
438 }
439 // Vehicle has to be this close to lane center before considering change
440 // lane
441 if (!adc_waypoint_.lane || adc_route_index_ < 0 ||
442 adc_route_index_ >= static_cast<int>(route_indices_.size())) {
443 AERROR << "Invalid vehicle state in pnc_map, update vehicle state first.";
444 return false;
445 }
446 const auto &route_index = route_indices_[adc_route_index_].index;
447 const int road_index = route_index[0];
448 const int passage_index = route_index[1];
449 const auto &road = last_command_.lane_follow_command().road(road_index);
450 // Raw filter to find all neighboring passages
451 auto drive_passages = GetNeighborPassages(road, passage_index);
452 for (const int index : drive_passages) {
453 const auto &passage = road.passage(index);
454 hdmap::RouteSegments segments;
455 if (!PassageToSegments(passage, &segments)) {
456 ADEBUG << "Failed to convert passage to lane segments.";
457 continue;
458 }
459 const PointENU nearest_point =
460 index == passage_index
461 ? adc_waypoint_.lane->GetSmoothPoint(adc_waypoint_.s)
462 : PointFactory::ToPointENU(adc_state_);
463 common::SLPoint sl;
464 hdmap::LaneWaypoint segment_waypoint;
465 if (!segments.GetProjection(nearest_point, &sl, &segment_waypoint)) {
466 ADEBUG << "Failed to get projection from point: "
467 << nearest_point.ShortDebugString();
468 continue;
469 }
470 if (index != passage_index) {
471 if (!segments.CanDriveFrom(adc_waypoint_)) {
472 ADEBUG << "You cannot drive from current waypoint to passage: "
473 << index;
474 continue;
475 }
476 }
477 route_segments->emplace_back();
478 const auto last_waypoint = segments.LastWaypoint();
479 if (!ExtendSegments(segments, sl.s() - backward_length,
480 sl.s() + forward_length, &route_segments->back())) {
481 AERROR << "Failed to extend segments with s=" << sl.s()
482 << ", backward: " << backward_length
483 << ", forward: " << forward_length;
484 return false;
485 }
486 if (route_segments->back().IsWaypointOnSegment(last_waypoint)) {
487 route_segments->back().SetRouteEndWaypoint(last_waypoint);
488 }
489 route_segments->back().SetCanExit(passage.can_exit());
490 route_segments->back().SetNextAction(passage.change_lane_type());
491 const std::string route_segment_id = absl::StrCat(road_index, "_", index);
492 route_segments->back().SetId(route_segment_id);
493 route_segments->back().SetStopForDestination(stop_for_destination_);
494 if (index == passage_index) {
495 route_segments->back().SetIsOnSegment(true);
496 route_segments->back().SetPreviousAction(routing::FORWARD);
497 } else if (sl.l() > 0) {
498 route_segments->back().SetPreviousAction(routing::RIGHT);
499 } else {
500 route_segments->back().SetPreviousAction(routing::LEFT);
501 }
502 }
503 UpdateRouteSegmentsLaneIds(route_segments);
504 return !route_segments->empty();
505}
506
508 const common::VehicleState &state, hdmap::LaneWaypoint *waypoint) const {
509 waypoint->lane = nullptr;
510 std::vector<hdmap::LaneInfoConstPtr> lanes;
511 const auto point = PointFactory::ToPointENU(state);
512 std::vector<hdmap::LaneInfoConstPtr> valid_lanes;
513 for (auto lane_id : all_lane_ids_) {
514 hdmap::Id id = hdmap::MakeMapId(lane_id);
515 auto lane = hdmap_->GetLaneById(id);
516 if (nullptr != lane) {
517 valid_lanes.emplace_back(lane);
518 }
519 }
520
521 // Get nearest_waypoints for current position
522 std::vector<hdmap::LaneWaypoint> valid_way_points;
523 for (const auto &lane : valid_lanes) {
524 if (range_lane_ids_.count(lane->id().id()) == 0) {
525 ADEBUG << "not in range" << lane->id().id();
526 continue;
527 }
528 if (route_segments_lane_ids_.size() > 0 &&
529 route_segments_lane_ids_.count(lane->id().id()) == 0) {
530 ADEBUG << "not in last frame route_segments: " << lane->id().id();
531 continue;
532 }
533 double s = 0.0;
534 double l = 0.0;
535 {
536 if (!lane->GetProjection({point.x(), point.y()}, &s, &l)) {
537 continue;
538 }
539 ADEBUG << lane->id().id() << "," << s << "," << l;
540 // Use large epsilon to allow projection diff
541 static constexpr double kEpsilon = 0.5;
542 if (s > (lane->total_length() + kEpsilon) || (s + kEpsilon) < 0.0) {
543 continue;
544 }
545 double lane_heading = lane->Heading(s);
546 if (std::fabs(common::math::AngleDiff(lane_heading, state.heading())) >
547 M_PI_2 * 1.5) {
548 continue;
549 }
550 }
551
552 valid_way_points.emplace_back();
553 auto &last = valid_way_points.back();
554 last.lane = lane;
555 last.s = s;
556 last.l = l;
557 ADEBUG << "distance:" << std::fabs(l);
558 }
559 if (valid_way_points.empty()) {
560 AERROR << "Failed to find nearest point: " << point.ShortDebugString();
561 return false;
562 }
563
564 // find closest lane that satisfy vehicle heading
565 int closest_index = -1;
566 double distance = std::numeric_limits<double>::max();
567 double lane_heading = 0.0;
568 double vehicle_heading = state.heading();
569 for (size_t i = 0; i < valid_way_points.size(); i++) {
570 lane_heading = valid_way_points[i].lane->Heading(valid_way_points[i].s);
571 if (std::abs(common::math::AngleDiff(lane_heading, vehicle_heading)) >
572 M_PI_2 * 1.5) {
573 continue;
574 }
575 if (std::fabs(valid_way_points[i].l) < distance) {
576 distance = std::fabs(valid_way_points[i].l);
577 closest_index = i;
578 }
579 }
580 if (closest_index == -1) {
581 AERROR << "Can not find nearest waypoint. vehicle heading:"
582 << vehicle_heading << "lane heading:" << lane_heading;
583 return false;
584 }
585 waypoint->lane = valid_way_points[closest_index].lane;
586 waypoint->s = valid_way_points[closest_index].s;
587 waypoint->l = valid_way_points[closest_index].l;
588 return true;
589}
590
591hdmap::LaneInfoConstPtr LaneFollowMap::GetRouteSuccessor(
592 hdmap::LaneInfoConstPtr lane) const {
593 if (lane->lane().successor_id().empty()) {
594 return nullptr;
595 }
596 hdmap::Id preferred_id = lane->lane().successor_id(0);
597 for (const auto &lane_id : lane->lane().successor_id()) {
598 if (range_lane_ids_.count(lane_id.id()) != 0) {
599 preferred_id = lane_id;
600 break;
601 }
602 }
603 return hdmap_->GetLaneById(preferred_id);
604}
605
606hdmap::LaneInfoConstPtr LaneFollowMap::GetRoutePredecessor(
607 hdmap::LaneInfoConstPtr lane) const {
608 if (lane->lane().predecessor_id().empty()) {
609 return nullptr;
610 }
611
612 std::unordered_set<std::string> predecessor_ids;
613 for (const auto &lane_id : lane->lane().predecessor_id()) {
614 predecessor_ids.insert(lane_id.id());
615 }
616
617 hdmap::Id preferred_id = lane->lane().predecessor_id(0);
618 for (const auto &route_index : route_indices_) {
619 auto &lane = route_index.segment.lane->id();
620 if (predecessor_ids.count(lane.id()) != 0) {
621 preferred_id = lane;
622 break;
623 }
624 }
625 return hdmap_->GetLaneById(preferred_id);
626}
627
628bool LaneFollowMap::ExtendSegments(const hdmap::RouteSegments &segments,
629 const common::PointENU &point,
630 double look_backward, double look_forward,
631 hdmap::RouteSegments *extended_segments) {
632 common::SLPoint sl;
633 hdmap::LaneWaypoint waypoint;
634 if (!segments.GetProjection(point, &sl, &waypoint)) {
635 AERROR << "point: " << point.ShortDebugString() << " is not on segment";
636 return false;
637 }
638 return ExtendSegments(segments, sl.s() - look_backward, sl.s() + look_forward,
639 extended_segments);
640}
641
643 const hdmap::RouteSegments &segments, double start_s, double end_s,
644 hdmap::RouteSegments *const truncated_segments) const {
645 if (segments.empty()) {
646 AERROR << "The input segments is empty";
647 return false;
648 }
649 CHECK_NOTNULL(truncated_segments);
650 truncated_segments->SetProperties(segments);
651
652 if (start_s >= end_s) {
653 AERROR << "start_s(" << start_s << " >= end_s(" << end_s << ")";
654 return false;
655 }
656 std::unordered_set<std::string> unique_lanes;
657 static constexpr double kRouteEpsilon = 1e-3;
658 // Extend the trajectory towards the start of the trajectory.
659 if (start_s < 0) {
660 const auto &first_segment = *segments.begin();
661 auto lane = first_segment.lane;
662 double s = first_segment.start_s;
663 double extend_s = -start_s;
664 std::vector<hdmap::LaneSegment> extended_lane_segments;
665 while (extend_s > kRouteEpsilon) {
666 if (s <= kRouteEpsilon) {
667 lane = GetRoutePredecessor(lane);
668 if (lane == nullptr ||
669 unique_lanes.find(lane->id().id()) != unique_lanes.end()) {
670 break;
671 }
672 s = lane->total_length();
673 } else {
674 const double length = std::min(s, extend_s);
675 extended_lane_segments.emplace_back(lane, s - length, s);
676 extend_s -= length;
677 s -= length;
678 unique_lanes.insert(lane->id().id());
679 }
680 }
681 truncated_segments->insert(truncated_segments->begin(),
682 extended_lane_segments.rbegin(),
683 extended_lane_segments.rend());
684 }
685 bool found_loop = false;
686 double router_s = 0;
687 for (const auto &lane_segment : segments) {
688 const double adjusted_start_s = std::max(
689 start_s - router_s + lane_segment.start_s, lane_segment.start_s);
690 const double adjusted_end_s =
691 std::min(end_s - router_s + lane_segment.start_s, lane_segment.end_s);
692 if (adjusted_start_s < adjusted_end_s) {
693 if (!truncated_segments->empty() &&
694 truncated_segments->back().lane->id().id() ==
695 lane_segment.lane->id().id()) {
696 truncated_segments->back().end_s = adjusted_end_s;
697 } else if (unique_lanes.find(lane_segment.lane->id().id()) ==
698 unique_lanes.end()) {
699 truncated_segments->emplace_back(lane_segment.lane, adjusted_start_s,
700 adjusted_end_s);
701 unique_lanes.insert(lane_segment.lane->id().id());
702 } else {
703 found_loop = true;
704 break;
705 }
706 }
707 router_s += (lane_segment.end_s - lane_segment.start_s);
708 if (router_s > end_s) {
709 break;
710 }
711 }
712 if (found_loop) {
713 return true;
714 }
715
716 // adjust end_s to match reference_line_endpoint_extend_length
717 double last_lane_segment_length =
718 segments.back().end_s - segments.back().start_s;
719 auto last_lane = segments.back().lane;
720 bool last_lane_can_exit =
721 last_lane->id().id() == dest_lane_segment_.id() &&
722 (adc_waypoint_.lane->id().id() != dest_lane_segment_.id() ||
723 (adc_waypoint_.lane->id().id() == dest_lane_segment_.id() &&
724 adc_waypoint_.s < dest_lane_segment_.end_s() + 1.0));
725 if (last_lane_can_exit) {
726 end_s =
727 std::max(router_s - kRouteEpsilon,
728 router_s - last_lane_segment_length +
729 dest_lane_segment_.end_s() - dest_lane_segment_.start_s() +
730 FLAGS_reference_line_endpoint_extend_length);
731 }
732
733 // Extend the trajectory towards the end of the trajectory.
734 if (router_s < end_s && !truncated_segments->empty()) {
735 auto &back = truncated_segments->back();
736 if (back.lane->total_length() > back.end_s) {
737 double origin_end_s = back.end_s;
738 back.end_s =
739 std::min(back.end_s + end_s - router_s, back.lane->total_length());
740 router_s += back.end_s - origin_end_s;
741 }
742 }
743
744 while (router_s < end_s - kRouteEpsilon) {
745 last_lane = GetRouteSuccessor(last_lane);
746 if (last_lane == nullptr ||
747 unique_lanes.find(last_lane->id().id()) != unique_lanes.end()) {
748 break;
749 }
750
751 bool last_lane_can_exit =
752 last_lane->id().id() == dest_lane_segment_.id() &&
753 (adc_waypoint_.lane->id().id() != dest_lane_segment_.id() ||
754 (adc_waypoint_.lane->id().id() == dest_lane_segment_.id() &&
755 adc_waypoint_.s < dest_lane_segment_.end_s() + 1.0));
756
757 // adjust end_s to match reference_line_endpoint_extend_length
758 if (last_lane_can_exit) {
759 end_s = std::max(router_s + 1.0,
760 router_s + dest_lane_segment_.end_s() -
761 dest_lane_segment_.start_s() +
762 FLAGS_reference_line_endpoint_extend_length);
763 }
764
765 const double length = std::min(end_s - router_s, last_lane->total_length());
766 truncated_segments->emplace_back(last_lane, 0, length);
767 unique_lanes.insert(last_lane->id().id());
768 router_s += length;
769 AINFO << last_lane->id().id() << ", add length: " << length << ", router_s: " << router_s << ", end_s: " << end_s;
770 }
771 return true;
772}
773
774void LaneFollowMap::AppendLaneToPoints(
775 hdmap::LaneInfoConstPtr lane, const double start_s, const double end_s,
776 std::vector<hdmap::MapPathPoint> *const points) {
777 if (points == nullptr || start_s >= end_s) {
778 return;
779 }
780 double accumulate_s = 0.0;
781 for (size_t i = 0; i < lane->points().size(); ++i) {
782 if (accumulate_s >= start_s && accumulate_s <= end_s) {
783 points->emplace_back(lane->points()[i], lane->headings()[i],
784 hdmap::LaneWaypoint(lane, accumulate_s));
785 }
786 if (i < lane->segments().size()) {
787 const auto &segment = lane->segments()[i];
788 const double next_accumulate_s = accumulate_s + segment.length();
789 if (start_s > accumulate_s && start_s < next_accumulate_s) {
790 points->emplace_back(segment.start() + segment.unit_direction() *
791 (start_s - accumulate_s),
792 lane->headings()[i],
793 hdmap::LaneWaypoint(lane, start_s));
794 }
795 if (end_s > accumulate_s && end_s < next_accumulate_s) {
796 points->emplace_back(
797 segment.start() + segment.unit_direction() * (end_s - accumulate_s),
798 lane->headings()[i], hdmap::LaneWaypoint(lane, end_s));
799 }
800 accumulate_s = next_accumulate_s;
801 }
802 if (accumulate_s > end_s) {
803 break;
804 }
805 }
806}
807
808void LaneFollowMap::UpdateRouteSegmentsLaneIds(
809 const std::list<hdmap::RouteSegments> *route_segments) {
810 route_segments_lane_ids_.clear();
811 for (auto &route_seg : *route_segments) {
812 for (auto &lane_seg : route_seg) {
813 if (nullptr == lane_seg.lane) {
814 continue;
815 }
816 route_segments_lane_ids_.insert(lane_seg.lane->id().id());
817 }
818 }
819}
820
824
826 if (adc_route_index_ < 0 || adc_route_index_ >= route_indices_.size()) {
827 AERROR << "adc_route_index error, can not get distance to destination, "
828 "return 0.";
829 return 0.0;
830 }
831 const auto &routing = last_command_.lane_follow_command();
832 int adc_road_index = route_indices_[adc_route_index_].index[0];
833 int adc_passage_index = route_indices_[adc_route_index_].index[1];
834 int adc_lane_index = route_indices_[adc_route_index_].index[2];
835
836 bool get_adc_exit_waypoint =
837 routing.road(adc_road_index).passage(adc_passage_index).can_exit();
838 int start_passage_index = adc_passage_index;
839 int start_lane_index = adc_lane_index;
840 double start_lane_s = adc_waypoint_.s;
841
842 double dis_to_destination = 0.0;
843
844 for (int road_index = adc_road_index; road_index < routing.road_size();
845 ++road_index) {
846 const auto &road_segment = routing.road(road_index);
847 for (int passage_index = 0; passage_index < road_segment.passage_size();
848 ++passage_index) {
849 const auto &passage = road_segment.passage(passage_index);
850 if (!passage.can_exit()) {
851 continue;
852 }
853 // check this passsage is can_exit
854 if (!get_adc_exit_waypoint) {
855 get_adc_exit_waypoint = true;
856 // find adc waypoint in passage which can exit
857 for (int index = 0; index < passage.segment_size(); ++index) {
858 auto lane = hdmap_->GetLaneById(
859 hdmap::MakeMapId(passage.segment(index).id()));
860 double s = 0.0;
861 double l = 0.0;
862 if (!lane->GetProjection({adc_state_.x(), adc_state_.y()}, &s, &l)) {
863 continue;
864 }
865 static constexpr double kEpsilon = 0.5;
866 if (s > (lane->total_length() + kEpsilon) || (s + kEpsilon) < 0.0) {
867 continue;
868 }
869 start_passage_index = passage_index;
870 start_lane_index = index;
871 start_lane_s = s;
872 }
873 }
874
875 // add distance by lane in can_exit passage
876 if (get_adc_exit_waypoint) {
877 if (road_index == adc_road_index &&
878 passage_index == start_passage_index) {
879 for (int lane_index = start_lane_index;
880 lane_index < passage.segment_size(); ++lane_index) {
881 if (lane_index == start_lane_index) {
882 dis_to_destination +=
883 passage.segment(lane_index).end_s() - start_lane_s;
884 } else {
885 dis_to_destination += passage.segment(lane_index).end_s() -
886 passage.segment(lane_index).start_s();
887 }
888 }
889 } else {
890 for (int lane_index = 0; lane_index < passage.segment_size();
891 ++lane_index) {
892 dis_to_destination += passage.segment(lane_index).end_s() -
893 passage.segment(lane_index).start_s();
894 }
895 }
896 break;
897 }
898 }
899 }
900 return dis_to_destination;
901}
902
903} // namespace planning
904} // namespace apollo
static PointENU ToPointENU(const double x, const double y, const double z=0)
LaneInfoConstPtr GetLaneById(const Id &id) const
Definition hdmap.cc:34
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.
#define ACHECK(cond)
Definition log.h:80
#define ADEBUG
Definition log.h:41
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
Some util functions.
double AngleDiff(const double from, const double to)
Calculate the difference between angle from and to
Definition math_utils.cc:61
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...
Definition util.h:97
apollo::hdmap::Id MakeMapId(const std::string &id)
create a Map ID given a string.
Definition hdmap_util.h:85
std::shared_ptr< const LaneInfo > LaneInfoConstPtr
class register implement
Definition arena_queue.h:37
Some string util functions.
optional string id
Definition map_id.proto:7
std::string DebugString() const
Definition path.cc:68
LaneInfoConstPtr lane
Definition path.h:44
optional apollo::routing::RoutingResponse lane_follow_command
repeated apollo::routing::LaneWaypoint waypoint
Definition routing.proto:14
optional RoutingRequest routing_request
Definition routing.proto:27
repeated apollo::routing::RoadSegment road
Definition routing.proto:25