48 FLAGS_localization_topic,
49 [
this](
const std::shared_ptr<LocalizationEstimate> &localization) {
50 ADEBUG <<
"Received chassis data: run chassis callback.";
51 OnLocalization(localization);
54 traffic_light_detection_writer_ =
56 FLAGS_traffic_light_detection_topic);
62 std::vector<SignalInfoConstPtr> signals;
64 if (FLAGS_all_lights) {
65 result = GetAllTrafficLights(&signals);
67 result = GetTrafficLightsWithinDistance(&signals);
70 ADEBUG <<
"Failed to get traffic signals from current location on map";
72 ADEBUG <<
"Received " << signals.size() <<
" traffic lights";
74 std::vector<std::string> signal_ids;
75 for (
const auto &ptr : signals) {
76 signal_ids.emplace_back(ptr->id().id());
78 if (IsDifferent(prev_traffic_lights_, signal_ids)) {
79 prev_traffic_lights_ =
80 std::unordered_set<std::string>(signal_ids.begin(), signal_ids.end());
84 TrafficLight::Color color = GetKeyBoardColorInput();
85 ADEBUG <<
"Color: " << TrafficLight::Color_Name(color);
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";
95 std::cout <<
" No lights in the next " << FLAGS_traffic_light_distance
99 if (signal_ids.size() < 5) {
100 std::cout <<
" IDs: " << absl::StrJoin(signal_ids,
" ");
102 std::cout <<
" IDs: "
103 << absl::StrJoin(signal_ids.begin(), signal_ids.begin() + 4,
108 std::cout << std::endl
109 << ANSI_RESET <<
"Press 'c' to change" << std::endl
112 CreateTrafficLightDetection(signals, color, &traffic_light_detection);
113 traffic_light_detection_writer_->Write(traffic_light_detection);
118 bool GetAllTrafficLights(std::vector<SignalInfoConstPtr> *traffic_lights) {
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")) {
133 for (
const auto &signal : map_proto.signal()) {
134 const auto *hdmap = HDMapUtil::BaseMapPtr();
136 AERROR <<
"Invalid HD Map.";
139 map_traffic_lights.push_back(hdmap->GetSignalById(signal.id()));
142 *traffic_lights = map_traffic_lights;
146 bool GetTrafficLightsWithinDistance(
147 std::vector<SignalInfoConstPtr> *traffic_lights) {
148 CHECK_NOTNULL(traffic_lights);
149 if (!has_localization_) {
150 AERROR <<
"No localization received";
153 const auto *hdmap = HDMapUtil::BaseMapPtr();
155 AERROR <<
"Invalid HD Map.";
159 int ret = hdmap->GetForwardNearestSignalsOnLane(
160 position, FLAGS_traffic_light_distance, traffic_lights);
162 AERROR <<
"failed to get signals from position: "
163 << position.ShortDebugString()
164 <<
" with distance: " << FLAGS_traffic_light_distance <<
" on map";
170 bool CreateTrafficLightDetection(
171 const std::vector<SignalInfoConstPtr> &signals, TrafficLight::Color color,
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());
181 apollo::common::util::FillHeader(
"manual_traffic_light", detection);
185 TrafficLight::Color GetKeyBoardColorInput() {
187 struct pollfd fd = {STDIN_FILENO, POLLIN, revent};
188 switch (poll(&fd, 1, 100)) {
190 AERROR <<
"Failed to read keybapoard";
198 is_green_ = !is_green_;
204 return TrafficLight::GREEN;
206 return TrafficLight::RED;
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()) {
215 for (
const auto &ss : str_vec) {
216 if (str_set.count(ss) == 0) {
224 const std::shared_ptr<LocalizationEstimate> &localization) {
225 localization_ = *localization;
226 has_localization_ =
true;
230 bool is_green_ =
false;
231 bool updated_ =
true;
232 bool has_localization_ =
false;
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;