Apollo 11.0
自动驾驶开放平台
traffic_light_unprotected_right_turn_scenario.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 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 "modules/common_msgs/perception_msgs/perception_obstacle.pb.h"
24#include "modules/common_msgs/perception_msgs/traffic_light_detection.pb.h"
25#include "cyber/common/log.h"
26#include "cyber/time/clock.h"
33
34namespace apollo {
35namespace planning {
36
38
40 std::shared_ptr<DependencyInjector> injector, const std::string& name) {
41 if (init_) {
42 return true;
43 }
44
45 if (!Scenario::Init(injector, name)) {
46 AERROR << "failed to init scenario" << Name();
47 return false;
48 }
49
50 if (!Scenario::LoadConfig<ScenarioTrafficLightUnprotectedRightTurnConfig>(
51 &context_.scenario_config)) {
52 AERROR << "fail to get specific config of scenario " << Name();
53 return false;
54 }
55 init_ = true;
56 return true;
57}
58
60 const Scenario* const other_scenario, const Frame& frame) {
61 if (!frame.local_view().planning_command->has_lane_follow_command()) {
62 return false;
63 }
64 if (other_scenario == nullptr || frame.reference_line_info().empty()) {
65 return false;
66 }
67 const auto& reference_line_info = frame.reference_line_info().front();
68 const auto& first_encountered_overlaps =
69 reference_line_info.FirstEncounteredOverlaps();
70 hdmap::PathOverlap* traffic_sign_overlap = nullptr;
71 for (const auto& overlap : first_encountered_overlaps) {
72 if (overlap.first == ReferenceLineInfo::STOP_SIGN ||
73 overlap.first == ReferenceLineInfo::YIELD_SIGN) {
74 return false;
75 } else if (overlap.first == ReferenceLineInfo::SIGNAL) {
76 traffic_sign_overlap = const_cast<hdmap::PathOverlap*>(&overlap.second);
77 break;
78 }
79 }
80 if (traffic_sign_overlap == nullptr) {
81 return false;
82 }
83 const std::vector<hdmap::PathOverlap>& traffic_light_overlaps =
84 reference_line_info.reference_line().map_path().signal_overlaps();
85 const double start_check_distance =
87 const double adc_front_edge_s = reference_line_info.AdcSlBoundary().end_s();
88 // find all the traffic light belong to
89 // the same group as first encountered traffic light
90 std::vector<hdmap::PathOverlap> next_traffic_lights;
91 static constexpr double kTrafficLightGroupingMaxDist = 2.0; // unit: m
92 for (const auto& overlap : traffic_light_overlaps) {
93 const double dist = overlap.start_s - traffic_sign_overlap->start_s;
94 if (fabs(dist) <= kTrafficLightGroupingMaxDist) {
95 next_traffic_lights.push_back(overlap);
96 }
97 }
98 bool traffic_light_scenario = false;
99 // note: need iterate all lights to check no RED/YELLOW/UNKNOWN
100 for (const auto& overlap : next_traffic_lights) {
101 const double adc_distance_to_traffic_light =
102 overlap.start_s - adc_front_edge_s;
103 ADEBUG << "traffic_light[" << overlap.object_id << "] start_s["
104 << overlap.start_s << "] adc_distance_to_traffic_light["
105 << adc_distance_to_traffic_light << "]";
106
107 // enter traffic-light scenarios: based on distance only
108 if (adc_distance_to_traffic_light <= 0.0 ||
109 adc_distance_to_traffic_light > start_check_distance) {
110 continue;
111 }
112
113 const auto& signal_color = frame.GetSignal(overlap.object_id).color();
114 ADEBUG << "traffic_light_id[" << overlap.object_id << "] start_s["
115 << overlap.start_s << "] color[" << signal_color << "]";
116
117 if (signal_color != perception::TrafficLight::GREEN &&
118 signal_color != perception::TrafficLight::BLACK) {
119 traffic_light_scenario = true;
120 break;
121 }
122 }
123 if (!traffic_light_scenario) {
124 return false;
125 }
126 const auto& turn_type =
127 reference_line_info.GetPathTurnType(traffic_sign_overlap->start_s);
128 if (turn_type != hdmap::Lane::RIGHT_TURN) {
129 return false;
130 }
131 context_.current_traffic_light_overlap_ids.clear();
132 for (const auto& overlap : next_traffic_lights) {
133 context_.current_traffic_light_overlap_ids.push_back(overlap.object_id);
134 }
135 return true;
136}
137
139 injector_->planning_context()
140 ->mutable_planning_status()
141 ->mutable_traffic_light()
142 ->Clear();
143 return true;
144}
145
147 const auto& reference_line_info = frame->reference_line_info().front();
148 std::string current_traffic_light_overlap_id;
149 const auto& overlaps = reference_line_info.FirstEncounteredOverlaps();
150 for (auto overlap : overlaps) {
151 if (overlap.first == ReferenceLineInfo::SIGNAL) {
152 current_traffic_light_overlap_id = overlap.second.object_id;
153 break;
154 }
155 }
156
157 if (current_traffic_light_overlap_id.empty()) {
158 injector_->planning_context()
159 ->mutable_planning_status()
160 ->mutable_traffic_light()
161 ->Clear();
162 AERROR << "Can not find traffic light overlap in reference line!";
163 return false;
164 }
165
166 // find all the traffic light at/within the same location/group
167 const std::vector<apollo::hdmap::PathOverlap>& traffic_light_overlaps =
168 reference_line_info.reference_line().map_path().signal_overlaps();
169 auto traffic_light_overlap_itr = std::find_if(
170 traffic_light_overlaps.begin(), traffic_light_overlaps.end(),
171 [&current_traffic_light_overlap_id](const hdmap::PathOverlap& overlap) {
172 return overlap.object_id == current_traffic_light_overlap_id;
173 });
174 if (traffic_light_overlap_itr == traffic_light_overlaps.end()) {
175 injector_->planning_context()
176 ->mutable_planning_status()
177 ->mutable_traffic_light()
178 ->Clear();
179 return true;
180 }
181
182 static constexpr double kTrafficLightGroupingMaxDist = 2.0; // unit: m
183 const double current_traffic_light_overlap_start_s =
184 traffic_light_overlap_itr->start_s;
185 for (const auto& traffic_light_overlap : traffic_light_overlaps) {
186 const double dist =
187 traffic_light_overlap.start_s - current_traffic_light_overlap_start_s;
188 if (fabs(dist) <= kTrafficLightGroupingMaxDist) {
189 injector_->planning_context()
190 ->mutable_planning_status()
191 ->mutable_traffic_light()
192 ->add_current_traffic_light_overlap_id(
193 traffic_light_overlap.object_id);
194 ADEBUG << "Update PlanningContext with first_encountered traffic_light["
195 << traffic_light_overlap.object_id << "] start_s["
196 << traffic_light_overlap.start_s << "]";
197 }
198 }
199 return true;
200}
201
202} // namespace planning
203} // namespace apollo
Frame holds all data for one planning cycle.
Definition frame.h:62
const LocalView & local_view() const
Definition frame.h:163
perception::TrafficLight GetSignal(const std::string &traffic_light_id) const
Definition frame.cc:511
const std::list< ReferenceLineInfo > & reference_line_info() const
Definition frame.cc:123
virtual bool Init(std::shared_ptr< DependencyInjector > injector, const std::string &name)
Definition scenario.cc:43
std::shared_ptr< DependencyInjector > injector_
Definition scenario.h:113
const std::string & Name() const
Definition scenario.h:98
bool Init(std::shared_ptr< DependencyInjector > injector, const std::string &name) override
bool IsTransferable(const Scenario *const other_scenario, const Frame &frame) override
Each scenario should define its own transfer condition, i.e., when it should allow to transfer from o...
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::shared_ptr< PlanningCommand > planning_command
Definition local_view.h:48
ScenarioTrafficLightUnprotectedRightTurnConfig scenario_config
Definition context.h:32
std::vector< std::string > current_traffic_light_overlap_ids
Definition context.h:33