Apollo 10.0
自动驾驶开放平台
manual_traffic_light.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
17#include <poll.h>
18
19#include "absl/strings/match.h"
20#include "absl/strings/str_join.h"
22#include "cyber/cyber.h"
28#include "modules/common_msgs/perception_msgs/traffic_light_detection.pb.h"
29
38
39DEFINE_bool(all_lights, false, "set all lights on the map");
40
41DEFINE_double(traffic_light_distance, 1000.0,
42 "only retrieves traffic lights within this distance");
43
45 public:
46 bool Init() {
47 localization_reader_ = node_->CreateReader<LocalizationEstimate>(
48 FLAGS_localization_topic,
49 [this](const std::shared_ptr<LocalizationEstimate> &localization) {
50 ADEBUG << "Received chassis data: run chassis callback.";
51 OnLocalization(localization);
52 });
53
54 traffic_light_detection_writer_ =
55 node_->CreateWriter<TrafficLightDetection>(
56 FLAGS_traffic_light_detection_topic);
57
58 return true;
59 }
60
61 bool Proc() {
62 std::vector<SignalInfoConstPtr> signals;
63 bool result = false;
64 if (FLAGS_all_lights) {
65 result = GetAllTrafficLights(&signals);
66 } else {
67 result = GetTrafficLightsWithinDistance(&signals);
68 }
69 if (!result) {
70 ADEBUG << "Failed to get traffic signals from current location on map";
71 } else {
72 ADEBUG << "Received " << signals.size() << " traffic lights";
73 }
74 std::vector<std::string> signal_ids;
75 for (const auto &ptr : signals) {
76 signal_ids.emplace_back(ptr->id().id());
77 }
78 if (IsDifferent(prev_traffic_lights_, signal_ids)) {
79 prev_traffic_lights_ =
80 std::unordered_set<std::string>(signal_ids.begin(), signal_ids.end());
81 updated_ = true;
82 }
83 TrafficLightDetection traffic_light_detection;
84 TrafficLight::Color color = GetKeyBoardColorInput();
85 ADEBUG << "Color: " << TrafficLight::Color_Name(color);
86 if (updated_) {
87 updated_ = false;
88 const char *print_color = is_green_ ? ANSI_GREEN : ANSI_RED;
89 std::cout << print_color
90 << "Current Light: " << (is_green_ ? "GREEN" : "RED");
91 if (signal_ids.empty()) {
92 if (FLAGS_all_lights) {
93 std::cout << " No lights in the map";
94 } else {
95 std::cout << " No lights in the next " << FLAGS_traffic_light_distance
96 << " meters";
97 }
98 } else {
99 if (signal_ids.size() < 5) {
100 std::cout << " IDs: " << absl::StrJoin(signal_ids, " ");
101 } else {
102 std::cout << " IDs: "
103 << absl::StrJoin(signal_ids.begin(), signal_ids.begin() + 4,
104 " ")
105 << " ...";
106 }
107 }
108 std::cout << std::endl
109 << ANSI_RESET << "Press 'c' to change" << std::endl
110 << std::endl;
111 }
112 CreateTrafficLightDetection(signals, color, &traffic_light_detection);
113 traffic_light_detection_writer_->Write(traffic_light_detection);
114 return true;
115 }
116
117 private:
118 bool GetAllTrafficLights(std::vector<SignalInfoConstPtr> *traffic_lights) {
119 static auto map_filename = apollo::hdmap::BaseMapFile();
120 static apollo::hdmap::Map map_proto;
121 static std::vector<SignalInfoConstPtr> map_traffic_lights;
122 if (map_proto.lane().empty() && map_traffic_lights.empty()) {
123 AERROR << "signal size: " << map_proto.signal_size();
124 if (absl::EndsWith(map_filename, ".xml")) {
126 &map_proto)) {
127 return false;
128 }
129 } else if (!apollo::cyber::common::GetProtoFromFile(map_filename,
130 &map_proto)) {
131 return false;
132 }
133 for (const auto &signal : map_proto.signal()) {
134 const auto *hdmap = HDMapUtil::BaseMapPtr();
135 if (!hdmap) {
136 AERROR << "Invalid HD Map.";
137 return false;
138 }
139 map_traffic_lights.push_back(hdmap->GetSignalById(signal.id()));
140 }
141 }
142 *traffic_lights = map_traffic_lights;
143 return true;
144 }
145
146 bool GetTrafficLightsWithinDistance(
147 std::vector<SignalInfoConstPtr> *traffic_lights) {
148 CHECK_NOTNULL(traffic_lights);
149 if (!has_localization_) {
150 AERROR << "No localization received";
151 return false;
152 }
153 const auto *hdmap = HDMapUtil::BaseMapPtr();
154 if (!hdmap) {
155 AERROR << "Invalid HD Map.";
156 return false;
157 }
158 auto position = localization_.pose().position();
159 int ret = hdmap->GetForwardNearestSignalsOnLane(
160 position, FLAGS_traffic_light_distance, traffic_lights);
161 if (ret != 0) {
162 AERROR << "failed to get signals from position: "
163 << position.ShortDebugString()
164 << " with distance: " << FLAGS_traffic_light_distance << " on map";
165 return false;
166 }
167 return true;
168 }
169
170 bool CreateTrafficLightDetection(
171 const std::vector<SignalInfoConstPtr> &signals, TrafficLight::Color color,
172 TrafficLightDetection *detection) {
173 CHECK_NOTNULL(detection);
174 for (const auto &iter : signals) {
175 auto *light = detection->add_traffic_light();
176 light->set_color(color);
177 light->set_confidence(1.0);
178 light->set_tracking_time(1.0);
179 light->set_id(iter->id().id());
180 }
181 apollo::common::util::FillHeader("manual_traffic_light", detection);
182 return true;
183 }
184
185 TrafficLight::Color GetKeyBoardColorInput() {
186 int8_t revent = 0; // short
187 struct pollfd fd = {STDIN_FILENO, POLLIN, revent};
188 switch (poll(&fd, 1, 100)) {
189 case -1:
190 AERROR << "Failed to read keybapoard";
191 break;
192 case 0:
193 break;
194 default:
195 char ch = 'x';
196 std::cin >> ch;
197 if (ch == 'c') {
198 is_green_ = !is_green_;
199 updated_ = true;
200 }
201 break;
202 }
203 if (is_green_) {
204 return TrafficLight::GREEN;
205 } else {
206 return TrafficLight::RED;
207 }
208 }
209
210 bool IsDifferent(const std::unordered_set<std::string> &str_set,
211 const std::vector<std::string> &str_vec) {
212 if (str_vec.size() != str_set.size()) {
213 return true;
214 }
215 for (const auto &ss : str_vec) {
216 if (str_set.count(ss) == 0) {
217 return true;
218 }
219 }
220 return false;
221 }
222
223 void OnLocalization(
224 const std::shared_ptr<LocalizationEstimate> &localization) {
225 localization_ = *localization;
226 has_localization_ = true;
227 }
228
229 private:
230 bool is_green_ = false;
231 bool updated_ = true;
232 bool has_localization_ = false;
233 LocalizationEstimate localization_;
234 std::unordered_set<std::string> prev_traffic_lights_;
235 std::shared_ptr<apollo::cyber::Writer<TrafficLightDetection>>
236 traffic_light_detection_writer_ = nullptr;
237 std::shared_ptr<apollo::cyber::Reader<LocalizationEstimate>>
238 localization_reader_ = nullptr;
239};
240
bool Proc()
The Proc logic of the component, which called by the CyberRT frame.
std::shared_ptr< Node > node_
static bool LoadData(const std::string &filename, apollo::hdmap::Map *pb_map)
Console color definitions
#define CYBER_REGISTER_COMPONENT(name)
Definition component.h:656
#define ADEBUG
Definition log.h:41
#define AERROR
Definition log.h:44
DEFINE_bool(all_lights, false, "set all lights on the map")
DEFINE_double(traffic_light_distance, 1000.0, "only retrieves traffic lights within this distance")
Some string util functions.
constexpr char ANSI_RESET[]
Definition color.h:36
constexpr char ANSI_RED[]
Definition color.h:30
constexpr char ANSI_GREEN[]
Definition color.h:31
bool GetProtoFromFile(const std::string &file_name, google::protobuf::Message *message)
Parses the content of the file specified by the file_name as a representation of protobufs,...
Definition file.cc:132
std::shared_ptr< const SignalInfo > SignalInfoConstPtr
std::string BaseMapFile()
get base map file path from flags.
Definition hdmap_util.cc:47
repeated Lane lane
Definition map.proto:49
optional apollo::localization::Pose pose
optional apollo::common::PointENU position
Definition pose.proto:26