Apollo 11.0
自动驾驶开放平台
traffic_light.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2019 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#include <vector>
26
27#include "modules/common_msgs/planning_msgs/planning_internal.pb.h"
35
36namespace apollo {
37namespace planning {
38
41
42bool TrafficLight::Init(const std::string& name,
43 const std::shared_ptr<DependencyInjector>& injector) {
44 if (!TrafficRule::Init(name, injector)) {
45 return false;
46 }
47 // Load the config this task.
48 return TrafficRule::LoadConfig<TrafficLightConfig>(&config_);
49}
50
51Status TrafficLight::ApplyRule(Frame* const frame,
52 ReferenceLineInfo* const reference_line_info) {
53 MakeDecisions(frame, reference_line_info);
54
55 return Status::OK();
56}
57
58void TrafficLight::MakeDecisions(Frame* const frame,
59 ReferenceLineInfo* const reference_line_info) {
60 CHECK_NOTNULL(frame);
61 CHECK_NOTNULL(reference_line_info);
62
63 if (!config_.enabled()) {
64 return;
65 }
66
67 const auto& traffic_light_status =
68 injector_->planning_context()->planning_status().traffic_light();
69
70 const double adc_front_edge_s = reference_line_info->AdcSlBoundary().end_s();
71 const double adc_back_edge_s = reference_line_info->AdcSlBoundary().start_s();
72
73 // debug info
74 planning_internal::SignalLightDebug* signal_light_debug =
75 reference_line_info->mutable_debug()
76 ->mutable_planning_data()
77 ->mutable_signal_light();
78 signal_light_debug->set_adc_front_s(adc_front_edge_s);
79 signal_light_debug->set_adc_speed(
80 injector_->vehicle_state()->linear_velocity());
81
82 const std::vector<PathOverlap>& traffic_light_overlaps =
83 reference_line_info->reference_line().map_path().signal_overlaps();
84 for (const auto& traffic_light_overlap : traffic_light_overlaps) {
85 if (traffic_light_overlap.end_s <= adc_back_edge_s) {
86 continue;
87 }
88
89 // check if traffic-light-stop already finished, set by scenario/stage
90 bool traffic_light_done = false;
91 for (const auto& done_traffic_light_overlap_id :
92 traffic_light_status.done_traffic_light_overlap_id()) {
93 if (traffic_light_overlap.object_id == done_traffic_light_overlap_id) {
94 traffic_light_done = true;
95 break;
96 }
97 }
98 if (traffic_light_done) {
99 continue;
100 }
101
102 // work around incorrect s-projection along round routing
103 static constexpr double kSDiscrepanceTolerance = 10.0;
104 const auto& reference_line = reference_line_info->reference_line();
105 common::SLPoint traffic_light_sl;
106 traffic_light_sl.set_s(traffic_light_overlap.start_s);
107 traffic_light_sl.set_l(0);
108 common::math::Vec2d traffic_light_point;
109 reference_line.SLToXY(traffic_light_sl, &traffic_light_point);
110 common::math::Vec2d adc_position = {injector_->vehicle_state()->x(),
111 injector_->vehicle_state()->y()};
112 const double distance =
113 common::util::DistanceXY(traffic_light_point, adc_position);
114 const double s_distance = traffic_light_overlap.start_s - adc_front_edge_s;
115 ADEBUG << "traffic_light[" << traffic_light_overlap.object_id
116 << "] start_s[" << traffic_light_overlap.start_s << "] s_distance["
117 << s_distance << "] actual_distance[" << distance << "]";
118 if (s_distance >= 0 &&
119 fabs(s_distance - distance) > kSDiscrepanceTolerance) {
120 ADEBUG << "SKIP traffic_light[" << traffic_light_overlap.object_id
121 << "] close in position, but far away along reference line";
122 continue;
123 }
124
125 auto signal_color =
126 frame->GetSignal(traffic_light_overlap.object_id).color();
127 const double stop_deceleration = util::GetADCStopDeceleration(
128 injector_->vehicle_state(), adc_front_edge_s,
129 traffic_light_overlap.start_s);
130 ADEBUG << "traffic_light_id[" << traffic_light_overlap.object_id
131 << "] start_s[" << traffic_light_overlap.start_s << "] color["
132 << signal_color << "] stop_deceleration[" << stop_deceleration
133 << "]";
134
135 // debug info
136 planning_internal::SignalLightDebug::SignalDebug* signal_debug =
137 signal_light_debug->add_signal();
138 signal_debug->set_adc_stop_deceleration(stop_deceleration);
139 signal_debug->set_color(signal_color);
140 signal_debug->set_light_id(traffic_light_overlap.object_id);
141 signal_debug->set_light_stop_s(traffic_light_overlap.start_s);
142
143 // mayaochang add
144 if (signal_color == perception::TrafficLight::GREEN ||
145 signal_color == perception::TrafficLight::BLACK) {
146 continue;
147 }
148
149 // Red/Yellow/Unknown: check deceleration
150 if (stop_deceleration > config_.max_stop_deceleration()) {
151 AWARN << "stop_deceleration too big to achieve. SKIP red light";
152 continue;
153 }
154
155 // build stop decision
156 ADEBUG << "BuildStopDecision: traffic_light["
157 << traffic_light_overlap.object_id << "] start_s["
158 << traffic_light_overlap.start_s << "]"
159 << "stop_distance: " << config_.stop_distance();
160 std::string virtual_obstacle_id =
161 TRAFFIC_LIGHT_VO_ID_PREFIX + traffic_light_overlap.object_id;
162 const std::vector<std::string> wait_for_obstacles;
163 util::BuildStopDecision(
164 virtual_obstacle_id, traffic_light_overlap.start_s,
165 config_.stop_distance(), StopReasonCode::STOP_REASON_SIGNAL,
166 wait_for_obstacles, Getname(), frame, reference_line_info);
167 }
168}
169
170} // namespace planning
171} // namespace apollo
A general class to denote the return status of an API call.
Definition status.h:43
const std::vector< PathOverlap > & signal_overlaps() const
Definition path.h:301
Frame holds all data for one planning cycle.
Definition frame.h:62
perception::TrafficLight GetSignal(const std::string &traffic_light_id) const
Definition frame.cc:511
ReferenceLineInfo holds all data for one reference line.
const ReferenceLine & reference_line() const
planning_internal::Debug * mutable_debug()
const SLBoundary & AdcSlBoundary() const
const hdmap::Path & map_path() const
bool SLToXY(const common::SLPoint &sl_point, common::math::Vec2d *const xy_point) const
bool Init(const std::string &name, const std::shared_ptr< DependencyInjector > &injector) override
Planning module main class.
#define ADEBUG
Definition log.h:41
#define AWARN
Definition log.h:43
Some util functions.
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
class register implement
Definition arena_queue.h:37