Apollo 11.0
自动驾驶开放平台
rerouting.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 *****************************************************************************/
16
22
23#include <memory>
24#include <string>
25
26#include "modules/common_msgs/basic_msgs/pnc_point.pb.h"
27#include "cyber/time/clock.h"
30
31namespace apollo {
32namespace planning {
33
36
37bool Rerouting::Init(const std::string& name,
38 const std::shared_ptr<DependencyInjector>& injector) {
39 if (!TrafficRule::Init(name, injector)) {
40 return false;
41 }
42 // Load the config this task.
43 return TrafficRule::LoadConfig<ReroutingConfig>(&config_);
44}
45
46bool Rerouting::ChangeLaneFailRerouting() {
47 static constexpr double kRerouteThresholdToEnd = 20.0;
48 for (const auto& ref_line_info : frame_->reference_line_info()) {
49 if (ref_line_info.ReachedDestination() ||
50 ref_line_info.SDistanceToDestination() < kRerouteThresholdToEnd) {
51 return true;
52 }
53 }
54 const auto& segments = reference_line_info_->Lanes();
55 // 1. If current reference line is drive forward, no rerouting.
56 if (segments.NextAction() == routing::FORWARD) {
57 // if not current lane, do not check for rerouting
58 return true;
59 }
60
61 // 2. If vehicle is not on current reference line yet, no rerouting
62 if (!segments.IsOnSegment()) {
63 return true;
64 }
65
66 // 3. If current reference line can connect to next passage, no rerouting
67 if (segments.CanExit()) {
68 return true;
69 }
70
71 // 4. If the end of current passage region not appeared, no rerouting
72 const auto& route_end_waypoint = segments.RouteEndWaypoint();
73 if (!route_end_waypoint.lane) {
74 return true;
75 }
76 auto point = route_end_waypoint.lane->GetSmoothPoint(route_end_waypoint.s);
77 const auto& reference_line = reference_line_info_->reference_line();
78 common::SLPoint sl_point;
79 if (!reference_line.XYToSL(point, &sl_point)) {
80 AERROR << "Failed to project point: " << point.ShortDebugString();
81 return false;
82 }
83 if (!reference_line.IsOnLane(sl_point)) {
84 return true;
85 }
86 // 5. If the end of current passage region is further than kPrepareRoutingTime
87 // * speed, no rerouting
88 double adc_s = reference_line_info_->AdcSlBoundary().end_s();
89 const auto vehicle_state = injector_->vehicle_state();
90 double speed = vehicle_state->linear_velocity();
91 const double prepare_rerouting_time = config_.prepare_rerouting_time();
92 const double prepare_distance = speed * prepare_rerouting_time;
93 if (sl_point.s() > adc_s + prepare_distance) {
94 ADEBUG << "No need rerouting now because still can drive for time: "
95 << prepare_rerouting_time << " seconds";
96 return true;
97 }
98 // 6. Check if we have done rerouting before
99 auto* rerouting = injector_->planning_context()
100 ->mutable_planning_status()
101 ->mutable_rerouting();
102 if (rerouting == nullptr) {
103 AERROR << "rerouting is nullptr.";
104 return false;
105 }
106 double current_time = Clock::NowInSeconds();
107 if (rerouting->has_last_rerouting_time() &&
108 (current_time - rerouting->last_rerouting_time() <
109 config_.cooldown_time())) {
110 ADEBUG << "Skip rerouting and wait for previous rerouting result";
111 return true;
112 }
113 if (!frame_->Rerouting(injector_->planning_context())) {
114 AERROR << "Failed to send rerouting request";
115 return false;
116 }
117
118 // store last rerouting time.
119 rerouting->set_last_rerouting_time(current_time);
120 return true;
121}
122
124 ReferenceLineInfo* const reference_line_info) {
125 frame_ = frame;
126 reference_line_info_ = reference_line_info;
127 if (!ChangeLaneFailRerouting()) {
129 "In un-successful lane change case, rerouting failed");
130 }
131 return Status::OK();
132}
133
134} // namespace planning
135} // namespace apollo
A general class to denote the return status of an API call.
Definition status.h:43
static Status OK()
generate a success status.
Definition status.h:60
a singleton clock that can be used to get the current timestamp.
Definition clock.h:39
static double NowInSeconds()
gets the current time in second.
Definition clock.cc:56
const LaneWaypoint & RouteEndWaypoint() const
Frame holds all data for one planning cycle.
Definition frame.h:62
bool Rerouting(PlanningContext *planning_context)
Definition frame.cc:87
ReferenceLineInfo holds all data for one reference line.
const hdmap::RouteSegments & Lanes() const
const ReferenceLine & reference_line() const
const SLBoundary & AdcSlBoundary() const
common::Status ApplyRule(Frame *const frame, ReferenceLineInfo *const reference_line_info)
Definition rerouting.cc:123
bool Init(const std::string &name, const std::shared_ptr< DependencyInjector > &injector) override
Definition rerouting.cc:37
virtual bool Init(const std::string &name, const std::shared_ptr< DependencyInjector > &injector)
std::shared_ptr< DependencyInjector > injector_
Planning module main class.
#define ADEBUG
Definition log.h:41
#define AERROR
Definition log.h:44
class register implement
Definition arena_queue.h:37
LaneInfoConstPtr lane
Definition path.h:44