Apollo 11.0
自动驾驶开放平台
traffic_light_protected_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/planning/planning_base/proto/planning_config.pb.h"
24
25#include "cyber/common/log.h"
30
31namespace apollo {
32namespace planning {
33
35
37 std::shared_ptr<DependencyInjector> injector, const std::string& name) {
38 if (init_) {
39 return true;
40 }
41
42 if (!Scenario::Init(injector, name)) {
43 AERROR << "failed to init scenario" << Name();
44 return false;
45 }
46
47 if (!Scenario::LoadConfig<ScenarioTrafficLightProtectedConfig>(
48 &context_.scenario_config)) {
49 AERROR << "fail to get specific config of scenario " << Name();
50 return false;
51 }
52 init_ = true;
53 return true;
54}
55
57 const Scenario* const other_scenario, const Frame& frame) {
58 if (!frame.local_view().planning_command->has_lane_follow_command()) {
59 return false;
60 }
61 if (other_scenario == nullptr || frame.reference_line_info().empty()) {
62 return false;
63 }
64 const auto& reference_line_info = frame.reference_line_info().front();
65 const auto& first_encountered_overlaps =
66 reference_line_info.FirstEncounteredOverlaps();
67 hdmap::PathOverlap* traffic_sign_overlap = nullptr;
68 for (const auto& overlap : first_encountered_overlaps) {
69 if (overlap.first == ReferenceLineInfo::STOP_SIGN ||
70 overlap.first == ReferenceLineInfo::YIELD_SIGN) {
71 return false;
72 } else if (overlap.first == ReferenceLineInfo::SIGNAL) {
73 traffic_sign_overlap = const_cast<hdmap::PathOverlap*>(&overlap.second);
74 break;
75 }
76 }
77 if (traffic_sign_overlap == nullptr) {
78 return false;
79 }
80 const std::vector<hdmap::PathOverlap>& traffic_light_overlaps =
81 reference_line_info.reference_line().map_path().signal_overlaps();
82 const double start_check_distance =
84 const double adc_front_edge_s = reference_line_info.AdcSlBoundary().end_s();
85 // find all the traffic light belong to
86 // the same group as first encountered traffic light
87 std::vector<hdmap::PathOverlap> next_traffic_lights;
88 static constexpr double kTrafficLightGroupingMaxDist = 2.0; // unit: m
89 for (const auto& overlap : traffic_light_overlaps) {
90 const double dist = overlap.start_s - traffic_sign_overlap->start_s;
91 if (fabs(dist) <= kTrafficLightGroupingMaxDist) {
92 next_traffic_lights.push_back(overlap);
93 }
94 }
95 bool traffic_light_scenario = false;
96 // note: need iterate all lights to check no RED/YELLOW/UNKNOWN
97 for (const auto& overlap : next_traffic_lights) {
98 const double adc_distance_to_traffic_light =
99 overlap.start_s - adc_front_edge_s;
100 ADEBUG << "traffic_light[" << overlap.object_id << "] start_s["
101 << overlap.start_s << "] adc_distance_to_traffic_light["
102 << adc_distance_to_traffic_light << "]";
103
104 // enter traffic-light scenarios: based on distance only
105 if (adc_distance_to_traffic_light <= 0.0 ||
106 adc_distance_to_traffic_light > start_check_distance) {
107 continue;
108 }
109
110 const auto& signal_color = frame.GetSignal(overlap.object_id).color();
111 ADEBUG << "traffic_light_id[" << overlap.object_id << "] start_s["
112 << overlap.start_s << "] color[" << signal_color << "]";
113
114 if (signal_color != perception::TrafficLight::GREEN &&
115 signal_color != perception::TrafficLight::BLACK) {
116 traffic_light_scenario = true;
117 break;
118 }
119 }
120 if (!traffic_light_scenario) {
121 return false;
122 }
123 context_.current_traffic_light_overlap_ids.clear();
124 for (const auto& overlap : next_traffic_lights) {
125 context_.current_traffic_light_overlap_ids.push_back(overlap.object_id);
126 }
127 return true;
128}
129
131 injector_->planning_context()
132 ->mutable_planning_status()
133 ->mutable_traffic_light()
134 ->Clear();
135 return true;
136}
137
139 injector_->planning_context()
140 ->mutable_planning_status()
141 ->mutable_traffic_light()
142 ->Clear();
143 if (context_.current_traffic_light_overlap_ids.empty()) {
144 AERROR << "Can not find traffic light overlap in reference line!";
145 return false;
146 }
147 for (const auto& overlap_id : context_.current_traffic_light_overlap_ids) {
148 injector_->planning_context()
149 ->mutable_planning_status()
150 ->mutable_traffic_light()
151 ->add_current_traffic_light_overlap_id(overlap_id);
152 }
153 return true;
154}
155
156} // namespace planning
157} // 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 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...
bool Init(std::shared_ptr< DependencyInjector > injector, const std::string &name) override
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
std::vector< std::string > current_traffic_light_overlap_ids
Definition context.h:32
ScenarioTrafficLightProtectedConfig scenario_config
Definition context.h:31