Apollo 11.0
自动驾驶开放平台
keep_clear.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 <vector>
25
26#include "modules/common_msgs/basic_msgs/pnc_point.pb.h"
27#include "modules/planning/planning_base/proto/planning_config.pb.h"
28#include "modules/planning/planning_base/proto/planning_status.pb.h"
31
32namespace apollo {
33namespace planning {
34
37
38bool KeepClear::Init(const std::string& name,
39 const std::shared_ptr<DependencyInjector>& injector) {
40 if (!TrafficRule::Init(name, injector)) {
41 return false;
42 }
43 // Load the config this task.
44 return TrafficRule::LoadConfig<KeepClearConfig>(&config_);
45}
46
48 ReferenceLineInfo* const reference_line_info) {
49 CHECK_NOTNULL(frame);
50 CHECK_NOTNULL(reference_line_info);
51
52 // keep_clear zone
53 if (config_.enable_keep_clear_zone()) {
54 const std::vector<PathOverlap>& keep_clear_overlaps =
55 reference_line_info->reference_line().map_path().clear_area_overlaps();
56 for (const auto& keep_clear_overlap : keep_clear_overlaps) {
57 const auto obstacle_id =
58 KEEP_CLEAR_VO_ID_PREFIX + keep_clear_overlap.object_id;
59
60 if (BuildKeepClearObstacle(frame, reference_line_info, obstacle_id,
61 keep_clear_overlap.start_s,
62 keep_clear_overlap.end_s)) {
63 ADEBUG << "KEEP_CLAER for keep_clear_zone["
64 << keep_clear_overlap.object_id << "] s["
65 << keep_clear_overlap.start_s << ", " << keep_clear_overlap.end_s
66 << "] BUILD";
67 }
68 }
69 }
70
71 // junction
72 if (config_.enable_junction()) {
73 hdmap::PathOverlap* crosswalk_overlap = nullptr;
74 hdmap::PathOverlap* stop_sign_overlap = nullptr;
75 hdmap::PathOverlap* traffic_light_overlap = nullptr;
76 hdmap::PathOverlap* pnc_junction_overlap = nullptr;
77
78 const auto& first_encountered_overlaps =
79 reference_line_info->FirstEncounteredOverlaps();
80 for (const auto& overlap : first_encountered_overlaps) {
81 ADEBUG << overlap.first << ", " << overlap.second.DebugString();
82 switch (overlap.first) {
84 ADEBUG << "CROSSWALK[" << overlap.second.object_id << "] s["
85 << overlap.second.start_s << ", " << overlap.second.end_s
86 << "]";
87 crosswalk_overlap = const_cast<PathOverlap*>(&overlap.second);
88 break;
90 ADEBUG << "STOP_SIGN[" << overlap.second.object_id << "] s["
91 << overlap.second.start_s << ", " << overlap.second.end_s
92 << "]";
93 stop_sign_overlap = const_cast<PathOverlap*>(&overlap.second);
94 break;
96 ADEBUG << "SIGNAL[" << overlap.second.object_id << "] s["
97 << overlap.second.start_s << ", " << overlap.second.end_s
98 << "]";
99 traffic_light_overlap = const_cast<PathOverlap*>(&overlap.second);
100 break;
102 ADEBUG << "PNC_JUNCTION[" << overlap.second.object_id << "] s["
103 << overlap.second.start_s << ", " << overlap.second.end_s
104 << "]";
105 pnc_junction_overlap = const_cast<PathOverlap*>(&overlap.second);
106 break;
107 default:
108 break;
109 }
110 }
111
112 if (pnc_junction_overlap != nullptr) {
113 const double adc_front_edge_s =
114 reference_line_info->AdcSlBoundary().end_s();
115 if (!IsCreeping(pnc_junction_overlap->start_s, adc_front_edge_s)) {
116 // adjust pnc_junction start_s to align with
117 // the start_s of other "stop type" overlaps
118 double pnc_junction_start_s = pnc_junction_overlap->start_s;
119 // traffic_light, stop_sign, and then crosswalk if neither
120 if (traffic_light_overlap != nullptr &&
121 std::fabs(pnc_junction_start_s - traffic_light_overlap->start_s) <=
123 ADEBUG << "adjust pnc_junction_start_s[" << pnc_junction_start_s
124 << "] to traffic_light_start_s"
125 << traffic_light_overlap->start_s << "]";
126 pnc_junction_start_s = traffic_light_overlap->start_s;
127 } else if (stop_sign_overlap != nullptr &&
128 std::fabs(pnc_junction_start_s -
129 stop_sign_overlap->start_s) <=
131 ADEBUG << "adjust pnc_junction_start_s[" << pnc_junction_start_s
132 << "] to stop_sign_start_s" << stop_sign_overlap->start_s
133 << "]";
134 pnc_junction_start_s = stop_sign_overlap->start_s;
135 } else if (crosswalk_overlap != nullptr &&
136 std::fabs(pnc_junction_start_s -
137 crosswalk_overlap->start_s) <=
139 ADEBUG << "adjust pnc_junction_start_s[" << pnc_junction_start_s
140 << "] to cross_walk_start_s" << crosswalk_overlap->start_s
141 << "]";
142 pnc_junction_start_s = crosswalk_overlap->start_s;
143 }
144
145 const auto obstacle_id =
146 KEEP_CLEAR_JUNCTION_VO_ID_PREFIX + pnc_junction_overlap->object_id;
147
148 if (BuildKeepClearObstacle(frame, reference_line_info, obstacle_id,
149 pnc_junction_start_s,
150 pnc_junction_overlap->end_s)) {
151 ADEBUG << "KEEP_CLAER for junction["
152 << pnc_junction_overlap->object_id << "] s["
153 << pnc_junction_start_s << ", " << pnc_junction_overlap->end_s
154 << "] BUILD";
155 }
156 }
157 }
158 }
159
160 return Status::OK();
161}
162
163bool KeepClear::IsCreeping(const double pnc_junction_start_s,
164 const double adc_front_edge_s) const {
165 // check if in scenario creep stage
166 // while creeping, no need create keep clear obstacle
167 const auto& stage_type =
168 injector_->planning_context()->planning_status().scenario().stage_type();
169 if (stage_type != "STOP_SIGN_UNPROTECTED_CREEP" &&
170 stage_type != "TRAFFIC_LIGHT_UNPROTECTED_RIGHT_TURN_CREEP" &&
171 stage_type != "TRAFFIC_LIGHT_UNPROTECTED_LEFT_TURN_CREEP") {
172 return false;
173 }
174
175 // check distance
176 static constexpr double kDistance = 5.0;
177 return (fabs(adc_front_edge_s - pnc_junction_start_s) <= kDistance);
178}
179
180bool KeepClear::BuildKeepClearObstacle(
181 Frame* const frame, ReferenceLineInfo* const reference_line_info,
182 const std::string& virtual_obstacle_id, const double keep_clear_start_s,
183 const double keep_clear_end_s) {
184 CHECK_NOTNULL(frame);
185 CHECK_NOTNULL(reference_line_info);
186
187 // check
188 const double adc_front_edge_s = reference_line_info->AdcSlBoundary().end_s();
189 if (adc_front_edge_s - keep_clear_start_s > config_.min_pass_s_distance()) {
190 ADEBUG << "adc inside keep_clear zone[" << virtual_obstacle_id << "] s["
191 << keep_clear_start_s << ", " << keep_clear_end_s
192 << "] adc_front_edge_s[" << adc_front_edge_s
193 << "]. skip this keep clear zone";
194 return false;
195 }
196
197 ADEBUG << "keep clear obstacle: [" << keep_clear_start_s << ", "
198 << keep_clear_end_s << "]";
199 // create virtual static obstacle
200 auto* obstacle =
201 frame->CreateStaticObstacle(reference_line_info, virtual_obstacle_id,
202 keep_clear_start_s, keep_clear_end_s);
203 if (!obstacle) {
204 AERROR << "Failed to create obstacle [" << virtual_obstacle_id << "]";
205 return false;
206 }
207 auto* path_obstacle = reference_line_info->AddObstacle(obstacle);
208 if (!path_obstacle) {
209 AERROR << "Failed to create path_obstacle: " << virtual_obstacle_id;
210 return false;
211 }
212 path_obstacle->SetReferenceLineStBoundaryType(
214
215 return true;
216}
217
218} // namespace planning
219} // 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
const std::vector< PathOverlap > & clear_area_overlaps() const
Definition path.h:319
Frame holds all data for one planning cycle.
Definition frame.h:62
common::Status ApplyRule(Frame *const frame, ReferenceLineInfo *const reference_line_info)
Definition keep_clear.cc:47
bool Init(const std::string &name, const std::shared_ptr< DependencyInjector > &injector) override
Definition keep_clear.cc:38
ReferenceLineInfo holds all data for one reference line.
const ReferenceLine & reference_line() const
const std::vector< std::pair< OverlapType, hdmap::PathOverlap > > & FirstEncounteredOverlaps() const
const SLBoundary & AdcSlBoundary() const
const hdmap::Path & map_path() const
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
std::string object_id
Definition path.h:95
optional double align_with_traffic_sign_tolerance