Apollo 10.0
自动驾驶开放平台
utils.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2020 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 *****************************************************************************/
22
23#include <sstream>
24
26
27namespace apollo {
28namespace v2x {
29
31 remaining_time_ = new int32_t[kBufferSize];
32 msg_timestamp_ = new double[kBufferSize];
33 CHECK_NOTNULL(remaining_time_);
34 CHECK_NOTNULL(msg_timestamp_);
35 obu_light_ = std::make_shared<ObuLight>();
36 this->reset();
37}
38
40 delete[] remaining_time_;
41 delete[] msg_timestamp_;
42}
43
45 oslight_ = nullptr;
46 intersection_id_ = -1;
47 change_color_timestamp_ = 0.0;
48}
49
51 const std::shared_ptr<::apollo::hdmap::HDMap> &hdmap, double distance,
53 if (nullptr == msg) {
54 return false;
55 }
56 if (!msg->has_gps_x_m() || !msg->has_gps_y_m()) {
57 AERROR << "Error::v2x traffic_light ignore, gps point is null";
58 return false;
59 }
61 point.set_x(msg->gps_x_m());
62 point.set_y(msg->gps_y_m());
63 if (0 == msg->single_traffic_light_size()) {
64 AERROR << "Error::v2x traffic_light ignore, size of single light is 0.";
65 return false;
66 }
67 if (0 == msg->single_traffic_light(0).traffic_light_type_size()) {
68 return false;
69 }
71 double dummy_s = 0, dummy_l = 0;
72 if (0 != hdmap->GetNearestLane(point, &laneinfo, &dummy_s, &dummy_l)) {
73 return false;
74 }
75 std::vector<::apollo::hdmap::SignalInfoConstPtr> signals;
76 if (0 != hdmap->GetForwardNearestSignalsOnLane(point, distance, &signals)) {
77 AERROR << "Error::v2x traffic_light ignore, hdmap get no signals."
78 << "traffic light size : " << signals.size() << " "
79 << std::setiosflags(std::ios::fixed) << std::setprecision(11)
80 << "Point:x=" << point.x() << ",y=" << point.y();
81 return false;
82 }
83 if (signals.empty()) {
84 AERROR << "Get traffic light size : " << signals.size() << " "
85 << std::setiosflags(std::ios::fixed) << std::setprecision(11)
86 << "Point:x=" << point.x() << ",y=" << point.y();
87 return false;
88 }
89 AINFO << "the size of traffic light from HDMap is: " << signals.size();
90 auto tl_type = msg->single_traffic_light(0).traffic_light_type(0);
91 auto color = msg->single_traffic_light(0).color();
92 auto remaining_time = msg->single_traffic_light(0).color_remaining_time_s();
93 auto next_color = msg->single_traffic_light(0).next_color();
94 auto next_remaining_time =
96 msg->clear_single_traffic_light();
97 for (size_t i = 0; i < signals.size(); i++) {
98 auto signal_info = signals[i];
99 auto single = msg->add_single_traffic_light();
100 single->set_id(signal_info->id().id());
101 single->add_traffic_light_type(tl_type);
102 single->set_color(color);
103 single->set_color_remaining_time_s(remaining_time);
104 single->set_next_color(next_color);
105 single->set_next_remaining_time_s(next_remaining_time);
106 }
107 return true;
108}
109
111 std::time_t local = std::time(nullptr);
112 std::tm now = {};
113 localtime_r(&local, &now);
114 return now.tm_hour > 16;
115}
116
118 const std::shared_ptr<::apollo::hdmap::HDMap> &hdmap,
119 const ObuLight *x2v_traffic_light, const std::string &junction_id,
120 bool flag_u_turn, double distance, double check_time,
121 std::shared_ptr<OSLight> *os_light) {
122 if (nullptr == os_light) {
123 return false;
124 }
125 if (nullptr == *os_light) {
126 return false;
127 }
128 if (!x2v_traffic_light) {
129 if (junction_id == kUnknownJunctionId) {
130 return false;
131 }
132 return true;
133 }
134 if (junction_id != x2v_traffic_light->hdmap_junction_id()) {
135 AWARN << "current junction id " << junction_id
136 << ", received the junction id "
137 << x2v_traffic_light->hdmap_junction_id();
138 return false;
139 }
140 std::shared_ptr<OSLight> os_traffic_light = nullptr;
141 if (!ProtoAdapter::LightObu2Sys(*x2v_traffic_light, &os_traffic_light)) {
142 return false;
143 }
144 int num_os_traffic_light = os_traffic_light->road_traffic_light_size();
145 if (0 == num_os_traffic_light) {
146 AERROR << "Ignored no traffic light contained after conventor.";
147 return false;
148 }
149 std::shared_ptr<OSLight> sim_traffic_light_data = nullptr;
150 // enter the new intersection if the sim message is not null, clear
151 auto cur_junction_id = x2v_traffic_light->intersection_id();
152 auto tmp_os_traffic_light = std::make_shared<OSLight>();
153 tmp_os_traffic_light->CopyFrom(*os_traffic_light);
154 // clear road traffic light
155 tmp_os_traffic_light->clear_road_traffic_light();
156 for (int i = 0; i < num_os_traffic_light; i++) {
157 auto os_current_light = os_traffic_light->mutable_road_traffic_light(i);
158 if (!TrafficLightProc(hdmap, distance, os_current_light)) {
159 AERROR << "Traffic light proc failed";
160 continue;
161 }
162 if (os_current_light->single_traffic_light_size() > 0) {
163 auto tmp_os_current_light =
164 tmp_os_traffic_light->add_road_traffic_light();
165 tmp_os_current_light->CopyFrom(*(os_current_light));
166 }
167 }
168 tmp_os_traffic_light->set_confidence(IsRushHour() ? 0.5 : 1.0);
169 AINFO << "all traffic light send to os BEFORE is: "
170 << os_traffic_light->DebugString();
171 if (0 == tmp_os_traffic_light->road_traffic_light_size()) {
172 return false;
173 }
174 cur_junction_id = x2v_traffic_light->intersection_id();
175 tmp_os_traffic_light->set_intersection_id(cur_junction_id);
176 // enter a new junction, need to clear the list
177 if (cur_junction_id != intersection_id_) {
178 AINFO << "Enter New Juncion: " << cur_junction_id;
179 oslight_ = nullptr;
180 intersection_id_ = cur_junction_id;
181 int num_traffic_light = tmp_os_traffic_light->road_traffic_light_size();
182 for (int i = 0; i < num_traffic_light; i++) {
183 auto remaining_time = tmp_os_traffic_light->road_traffic_light(i)
184 .single_traffic_light(0)
185 .color_remaining_time_s();
186 remaining_time_[i] = remaining_time;
187 msg_timestamp_[i] = x2v_traffic_light->header().timestamp_sec();
188 }
189 } else {
190 ADEBUG << "Same Juncion: " << cur_junction_id;
191 if (flag_u_turn) {
192 for (unsigned int i = 0; i < kBufferSize; i++) {
193 msg_timestamp_[i] = 0.0;
194 remaining_time_[i] = -1;
195 }
196 oslight_ = nullptr;
197 }
198 int num_traffic_light = tmp_os_traffic_light->road_traffic_light_size();
199 for (int i = 0; i < num_traffic_light; i++) {
200 auto remaining_time = tmp_os_traffic_light->road_traffic_light(i)
201 .single_traffic_light(0)
202 .color_remaining_time_s();
203 if ((remaining_time_[i] != remaining_time)) {
204 remaining_time_[i] = remaining_time;
205 msg_timestamp_[i] = x2v_traffic_light->header().timestamp_sec();
206 }
207 }
208 if (!!oslight_) {
209 int road_valid_size =
210 std::min(oslight_->road_traffic_light_size(),
211 tmp_os_traffic_light->road_traffic_light_size());
212 for (int i = 0; i < road_valid_size; i++) {
213 const auto &last_msg_road = oslight_->road_traffic_light(i);
214 auto current_msg_road =
215 tmp_os_traffic_light->mutable_road_traffic_light(i);
216 int single_valid_size =
217 std::min(last_msg_road.single_traffic_light_size(),
218 current_msg_road->single_traffic_light_size());
219 for (int j = 0; j < single_valid_size; j++) {
220 const auto &last_msg_single_traffic_light =
221 last_msg_road.single_traffic_light(j);
222 auto current_msg_single_traffic_light =
223 current_msg_road->mutable_single_traffic_light(j);
224 if (last_msg_single_traffic_light.color() ==
225 current_msg_single_traffic_light->color()) {
226 if (current_msg_single_traffic_light->color_remaining_time_s() >
227 last_msg_single_traffic_light.color_remaining_time_s()) {
228 AINFO << "correct the remaining time";
229 current_msg_single_traffic_light->set_color_remaining_time_s(
230 last_msg_single_traffic_light.color_remaining_time_s());
231 }
232 }
233 }
234 }
235 }
236 }
237 oslight_ = std::make_shared<::apollo::v2x::IntersectionTrafficLightData>();
238 oslight_->CopyFrom(*tmp_os_traffic_light);
239 (*os_light)->CopyFrom(*tmp_os_traffic_light);
240 return true;
241}
242
244 const ::apollo::planning::ADCTrajectory *planning_msg,
245 const OSLight *last_os_light,
246 std::shared_ptr<::apollo::perception::TrafficLightDetection> *res_light) {
247 if (!planning_msg || !res_light || !(*res_light)) {
248 return false;
249 }
250 // Keep this blank header for protect other module against coredump.
251 (*res_light)->mutable_header();
252 if (!planning_msg->has_debug() ||
253 !planning_msg->debug().has_planning_data()) {
254 return false;
255 }
256 const auto &planning_debug = planning_msg->debug().planning_data();
257 if (!planning_debug.has_signal_light() ||
258 0 == planning_debug.signal_light().signal_size()) {
259 return false;
260 }
261 const std::string light_id =
262 planning_debug.signal_light().signal(0).light_id();
263 if (!last_os_light || light_id.empty()) {
264 return true; // output traffic light without v2x;
265 }
267 bool found = false;
268 for (int idx = 0; idx < last_os_light->road_traffic_light_size(); idx++) {
269 const auto &road_tl = last_os_light->road_traffic_light(idx);
270 if (0 == road_tl.single_traffic_light_size()) {
271 continue;
272 }
273 if (road_tl.single_traffic_light(0).id() == light_id) {
274 attr = road_tl.road_attribute();
275 found = true;
276 break;
277 }
278 }
279 if (!found) {
280 AWARN << "Failed to find light_id from os_light: " << light_id
281 << " , Ignored";
282 return true; // output traffic light without v2x;
283 }
284 (*res_light)->clear_traffic_light();
285 auto res_frame_id = std::to_string(last_os_light->has_intersection_id()
286 ? last_os_light->intersection_id()
287 : -1);
288 (*res_light)->mutable_header()->set_frame_id(res_frame_id);
289 AINFO << "Selected road attr: " << ::apollo::common::Direction_Name(attr);
290 std::set<std::string> idset;
291 for (int idx = 0; idx < last_os_light->road_traffic_light_size(); idx++) {
292 const auto &road_tl = last_os_light->road_traffic_light(idx);
293 if (0 == road_tl.single_traffic_light_size()) {
294 continue;
295 }
296 if (road_tl.road_attribute() != attr) {
297 continue;
298 }
299 const auto &single_tl = road_tl.single_traffic_light(0);
300 if (single_tl.traffic_light_type_size() < 1) {
301 continue;
302 }
303 auto *light1 = (*res_light)->add_traffic_light();
304 // SET ID
305 // light1->set_id(single_tl.id());
306 light1->set_id(SingleTrafficLight_Type_Name( //
307 single_tl.traffic_light_type(0)));
308 idset.emplace(single_tl.id());
309
310 if (single_tl.has_color()) {
311 switch (single_tl.color()) {
312 case OSLightColor::SingleTrafficLight_Color_RED:
313 light1->set_color(
314 ::apollo::perception::TrafficLight_Color::TrafficLight_Color_RED);
315 break;
316 case OSLightColor::SingleTrafficLight_Color_YELLOW:
317 light1->set_color(::apollo::perception::TrafficLight_Color::
318 TrafficLight_Color_YELLOW);
319 break;
320 case OSLightColor::SingleTrafficLight_Color_GREEN:
321 case OSLightColor::SingleTrafficLight_Color_FLASH_GREEN:
322 light1->set_color(::apollo::perception::TrafficLight_Color::
323 TrafficLight_Color_GREEN);
324 break;
325 case OSLightColor::SingleTrafficLight_Color_BLACK:
326 light1->set_color(::apollo::perception::TrafficLight_Color::
327 TrafficLight_Color_BLACK);
328 break;
329 default:
330 light1->set_color(::apollo::perception::TrafficLight_Color::
331 TrafficLight_Color_UNKNOWN);
332 break;
333 }
334 }
335 // REMAINING_TIME
336 if (single_tl.has_color_remaining_time_s()) {
337 light1->set_remaining_time(single_tl.color_remaining_time_s());
338 }
339 }
340 return true;
341}
342namespace utils {
343bool FindAllRoadId(const std::shared_ptr<::apollo::hdmap::HDMap> &hdmap,
344 const ::apollo::hdmap::LaneInfoConstPtr &start_laneinfo,
345 const ::apollo::hdmap::LaneInfoConstPtr &end_laneinfo,
346 size_t max_road_count,
347 std::unordered_set<std::string> *result_id_set) {
348 if (!result_id_set) {
349 return false;
350 }
351 size_t road_counter = 0;
353 result_id_set->clear();
354 result_id_set->insert(start_laneinfo->road_id().id());
355 ::apollo::hdmap::LaneInfoConstPtr start_laneinfo_tmp = start_laneinfo;
356 while (true) {
357 if (0 == start_laneinfo_tmp->lane().successor_id_size()) {
358 AINFO << "The lane has no successor";
359 return false;
360 }
361 id = start_laneinfo_tmp->lane().successor_id(0);
362 AINFO << "Lane id " << id.id();
363 start_laneinfo_tmp = hdmap->GetLaneById(id);
364 if (start_laneinfo_tmp == nullptr) {
365 AINFO << "Get lane by id is null.";
366 return false;
367 }
368 result_id_set->insert(start_laneinfo_tmp->road_id().id());
369 if (start_laneinfo_tmp->road_id().id() == end_laneinfo->road_id().id()) {
370 std::stringstream ss;
371 ss << "find all the road id: ";
372 for (const auto &item : *result_id_set) {
373 ss << item << " ";
374 }
375 AINFO << ss.str();
376 return true;
377 }
378 road_counter++;
379 if (road_counter > max_road_count) {
380 AINFO << "not find the end road id after try " << road_counter;
381 return false;
382 }
383 }
384}
385
386bool CheckCarInSet(const std::shared_ptr<::apollo::hdmap::HDMap> &hdmap,
387 const std::unordered_set<std::string> &id_set,
388 const ::apollo::hdmap::LaneInfoConstPtr &car_laneinfo,
389 size_t max_lane_count) {
390 size_t lane_counter = 0;
392 auto car_laneinfo_tmp = car_laneinfo;
393 while (true) {
394 if (id_set.count(car_laneinfo_tmp->road_id().id()) == 1) {
395 AINFO << "find the car is in the speed limit region";
396 return true;
397 }
398 if (car_laneinfo_tmp->lane().successor_id_size() == 0) {
399 AWARN << "The lane of the card no successor";
400 return false;
401 }
402 id = car_laneinfo_tmp->lane().successor_id(0);
403 AINFO << "Lane id " << id.id();
404 car_laneinfo_tmp = hdmap->GetLaneById(id);
405 if (car_laneinfo_tmp == nullptr) {
406 AWARN << "Get lane by id is null.";
407 return false;
408 } else {
409 if (++lane_counter > max_lane_count) {
410 AWARN << "not find the road in after try to " << lane_counter;
411 return false;
412 }
413 }
414 }
415}
416
417bool GetRsuInfo(const std::shared_ptr<::apollo::hdmap::HDMap> &hdmap,
418 const OSLocation &os_location,
419 const std::set<std::string> &rsu_whitelist, double distance,
420 double max_heading_difference,
421 std::shared_ptr<::apollo::v2x::CarStatus> *v2x_car_status,
422 std::string *out_junction_id, double *out_heading) {
423 if (!v2x_car_status) {
424 return false;
425 }
426 if (!out_junction_id) {
427 return false;
428 }
429 if (!out_heading) {
430 return false;
431 }
432 *out_junction_id = kUnknownJunctionId;
433 auto res = std::make_shared<::apollo::v2x::CarStatus>();
434 if (nullptr == res) {
435 return false;
436 }
437 if (!os_location.has_header()) {
438 return false;
439 }
440 if (!os_location.has_pose()) {
441 return false;
442 }
443 res->mutable_localization()->CopyFrom(os_location);
445 point.set_x(os_location.pose().position().x());
446 point.set_y(os_location.pose().position().y());
448 os_location.pose().orientation().qw(),
449 os_location.pose().orientation().qx(),
450 os_location.pose().orientation().qy(),
451 os_location.pose().orientation().qz());
452 *out_heading = heading;
453 std::vector<::apollo::hdmap::RSUInfoConstPtr> rsus;
454 if (0 != hdmap->GetForwardNearestRSUs(point, distance, heading,
455 max_heading_difference, &rsus) ||
456 rsus.empty()) {
457 AINFO << "no rsu is found";
458 return false;
459 }
460 AINFO << "Get " << rsus.size() << " rsu(s)";
461 if (rsu_whitelist.find(rsus[0]->id().id() + "_tl") == rsu_whitelist.cend()) {
462 AINFO << "This rsu id '" << rsus[0]->id().id()
463 << "' is not in the white list";
464 return false;
465 }
466 AINFO << "This RSU is in the white list";
467 AINFO << "Junction id " << rsus[0]->rsu().junction_id().id();
468 auto junction_info = hdmap->GetJunctionById(rsus[0]->rsu().junction_id());
469 if (nullptr == junction_info) {
470 return false;
471 }
472 std::shared_ptr<::apollo::v2x::ObuJunction> obu_junction = nullptr;
473 if (!ProtoAdapter::JunctionHd2obu(junction_info, &obu_junction)) {
474 return false;
475 }
476 res->mutable_junction()->CopyFrom(*obu_junction);
477 *v2x_car_status = res;
478 *out_junction_id = junction_info->id().id();
479 return true;
480}
481
483 switch (color) {
484 case OSLightColor::SingleTrafficLight_Color_GREEN:
485 return OSLightColor::SingleTrafficLight_Color_YELLOW;
486 case OSLightColor::SingleTrafficLight_Color_YELLOW:
487 return OSLightColor::SingleTrafficLight_Color_RED;
488 case OSLightColor::SingleTrafficLight_Color_RED:
489 return OSLightColor::SingleTrafficLight_Color_GREEN;
490 default:
491 return color;
492 }
493}
494
495void UniqueOslight(OSLight *os_light) {
496 if (nullptr == os_light) {
497 return;
498 }
499 auto tmp_os_light = std::make_shared<OSLight>();
500 tmp_os_light->CopyFrom(*os_light);
501 os_light->clear_road_traffic_light();
502 std::set<std::string> idset;
503 for (int idx_tl = 0; idx_tl < tmp_os_light->road_traffic_light_size();
504 idx_tl++) {
505 const auto &tmp_road_tl = tmp_os_light->road_traffic_light(idx_tl);
506 for (int idx_single = 0;
507 idx_single < tmp_road_tl.single_traffic_light_size(); idx_single++) {
508 const auto &single = tmp_road_tl.single_traffic_light(idx_single);
509 if (0 == single.traffic_light_type_size()) {
510 continue;
511 }
512 std::string tmpid =
513 single.id() +
514 std::to_string(static_cast<int>(single.traffic_light_type(0)));
515 // Has ID
516 if (idset.find(tmpid) != idset.cend()) {
517 continue;
518 }
519 idset.insert(tmpid);
520 // UNIQUE ID
521 auto res_tl = os_light->add_road_traffic_light();
522 res_tl->set_gps_x_m(tmp_road_tl.gps_x_m());
523 res_tl->set_gps_y_m(tmp_road_tl.gps_y_m());
524 res_tl->set_road_attribute(tmp_road_tl.road_attribute());
525 auto res_single = res_tl->add_single_traffic_light();
526 res_single->CopyFrom(single);
527 }
528 }
529}
530} // namespace utils
531} // namespace v2x
532} // namespace apollo
virtual ~InternalData()
Definition utils.cc:39
bool ProcPlanningMessage(const ::apollo::planning::ADCTrajectory *planning_msg, const OSLight *last_os_light, std::shared_ptr<::apollo::perception::TrafficLightDetection > *res_light)
Definition utils.cc:243
bool TrafficLightProc(const std::shared_ptr<::apollo::hdmap::HDMap > &hdmap, double distance, ::apollo::v2x::RoadTrafficLight *msg)
Definition utils.cc:50
bool ProcTrafficlight(const std::shared_ptr<::apollo::hdmap::HDMap > &hdmap, const ObuLight *x2v_traffic_light, const std::string &junction_id, bool flag_u_turn, double distance, double check_time, std::shared_ptr< OSLight > *os_light)
Definition utils.cc:117
static bool LightObu2Sys(const ObuLight &obu_light, std::shared_ptr< OSLight > *os_light)
static bool JunctionHd2obu(const HDJunction &hd_junction, std::shared_ptr< ObuJunction > *obu_junction)
#define ADEBUG
Definition log.h:41
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
#define AWARN
Definition log.h:43
utils for v2x_proxy
double QuaternionToHeading(const double qw, const double qx, const double qy, const double qz)
Definition quaternion.h:56
std::shared_ptr< const LaneInfo > LaneInfoConstPtr
bool GetRsuInfo(const std::shared_ptr<::apollo::hdmap::HDMap > &hdmap, const OSLocation &os_location, const std::set< std::string > &rsu_whitelist, double distance, double max_heading_difference, std::shared_ptr<::apollo::v2x::CarStatus > *v2x_car_status, std::string *out_junction_id, double *out_heading)
Definition utils.cc:417
bool CheckCarInSet(const std::shared_ptr<::apollo::hdmap::HDMap > &hdmap, const std::unordered_set< std::string > &id_set, const ::apollo::hdmap::LaneInfoConstPtr &car_laneinfo, size_t max_lane_count)
Definition utils.cc:386
void UniqueOslight(OSLight *os_light)
Definition utils.cc:495
bool FindAllRoadId(const std::shared_ptr<::apollo::hdmap::HDMap > &hdmap, const ::apollo::hdmap::LaneInfoConstPtr &start_laneinfo, const ::apollo::hdmap::LaneInfoConstPtr &end_laneinfo, size_t max_road_count, std::unordered_set< std::string > *result_id_set)
Definition utils.cc:343
OSLightColor GetNextColor(OSLightColor color)
Definition utils.cc:482
::apollo::v2x::SingleTrafficLight_Color OSLightColor
bool IsRushHour()
Definition utils.cc:110
const char *const kUnknownJunctionId
Definition utils.h:40
class register implement
Definition arena_queue.h:37
Contains a number of helper functions related to quaternions.
optional double timestamp_sec
Definition header.proto:9
optional string id
Definition map_id.proto:7
optional apollo::localization::Pose pose
optional apollo::common::Quaternion orientation
Definition pose.proto:31
optional apollo::common::PointENU position
Definition pose.proto:26
optional apollo::common::Direction road_attribute
repeated SingleTrafficLight single_traffic_light
optional apollo::common::Header header