Apollo 10.0
自动驾驶开放平台
routing.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
18
19#include <limits>
20#include <unordered_map>
21
25
26namespace apollo {
27namespace routing {
28
31
32std::string Routing::Name() const { return FLAGS_routing_node_name; }
33
35 : monitor_logger_buffer_(common::monitor::MonitorMessageItem::ROUTING) {}
36
38 const auto routing_map_file = apollo::hdmap::RoutingMapFile();
39 AINFO << "Use routing topology graph path: " << routing_map_file;
40 navigator_ptr_.reset(new Navigator(routing_map_file));
41
43 ACHECK(hdmap_) << "Failed to load map file:" << apollo::hdmap::BaseMapFile();
44
46}
47
49 if (!navigator_ptr_->IsReady()) {
50 AERROR << "Navigator is not ready!";
51 return apollo::common::Status(ErrorCode::ROUTING_ERROR,
52 "Navigator not ready");
53 }
54 AINFO << "Routing service is ready.";
55 monitor_logger_buffer_.INFO("Routing started");
57}
58
59std::vector<routing::RoutingRequest> Routing::FillLaneInfoIfMissing(
60 const routing::RoutingRequest& routing_request) {
61 std::vector<routing::RoutingRequest> fixed_requests;
62 std::unordered_map<int, std::vector<LaneWaypoint>>
63 additional_lane_waypoint_map;
64 routing::RoutingRequest fixed_request(routing_request);
65 for (int i = 0; i < routing_request.waypoint_size(); ++i) {
66 LaneWaypoint lane_waypoint(routing_request.waypoint(i));
67 if (lane_waypoint.has_id()) {
68 continue;
69 }
70
71 // fill lane info when missing
72 const auto point =
73 common::util::PointFactory::ToPointENU(lane_waypoint.pose());
74 std::vector<std::shared_ptr<const hdmap::LaneInfo>> lanes;
75 // look for lanes with bigger radius if not found
76 constexpr double kRadius = 0.3;
77 for (int i = 0; i < 20; ++i) {
78 hdmap_->GetLanes(point, kRadius + i * kRadius, &lanes);
79 if (lanes.size() > 0) {
80 break;
81 }
82 }
83 if (lanes.empty()) {
84 AERROR << "Failed to find nearest lane from map at position: "
85 << point.DebugString();
86 return fixed_requests; // return empty vector
87 }
88 for (size_t j = 0; j < lanes.size(); ++j) {
89 double s = 0.0;
90 double l = 0.0;
91 lanes[j]->GetProjection({point.x(), point.y()}, &s, &l);
92 if (j == 0) {
93 auto waypoint_info = fixed_request.mutable_waypoint(i);
94 waypoint_info->set_id(lanes[j]->id().id());
95 waypoint_info->set_s(s);
96 } else {
97 // additional candidate lanes
98 LaneWaypoint new_lane_waypoint(lane_waypoint);
99 new_lane_waypoint.set_id(lanes[j]->id().id());
100 new_lane_waypoint.set_s(s);
101 additional_lane_waypoint_map[i].push_back(new_lane_waypoint);
102 }
103 }
104 }
105 // first routing_request
106 fixed_requests.push_back(fixed_request);
107
108 // additional routing_requests because of lane overlaps
109 for (const auto& m : additional_lane_waypoint_map) {
110 size_t cur_size = fixed_requests.size();
111 for (size_t i = 0; i < cur_size; ++i) {
112 // use index to iterate while keeping push_back
113 for (const auto& lane_waypoint : m.second) {
114 routing::RoutingRequest new_request(fixed_requests[i]);
115 auto waypoint_info = new_request.mutable_waypoint(m.first);
116 waypoint_info->set_id(lane_waypoint.id());
117 waypoint_info->set_s(lane_waypoint.s());
118 fixed_requests.push_back(new_request);
119 }
120 }
121 }
122
123 for (const auto& fixed_request : fixed_requests) {
124 ADEBUG << "Fixed routing request:" << fixed_request.DebugString();
125 }
126 return fixed_requests;
127}
128
130 const std::shared_ptr<routing::RoutingRequest>& routing_request,
131 routing::RoutingResponse* const routing_response) {
132 if (nullptr == routing_request) {
133 AWARN << "Routing request is empty!";
134 return true;
135 }
136 CHECK_NOTNULL(routing_response);
137 AINFO << "Get new routing request:" << routing_request->DebugString();
138
139 const auto& fixed_requests = FillLaneInfoIfMissing(*routing_request);
140 double min_routing_length = std::numeric_limits<double>::max();
141 for (const auto& fixed_request : fixed_requests) {
142 routing::RoutingResponse routing_response_temp;
143 if (navigator_ptr_->SearchRoute(fixed_request, &routing_response_temp)) {
144 const double routing_length =
145 routing_response_temp.measurement().distance();
146 if (routing_length < min_routing_length) {
147 routing_response->CopyFrom(routing_response_temp);
148 min_routing_length = routing_length;
149 }
150 }
151 }
152 if (min_routing_length < std::numeric_limits<double>::max()) {
153 monitor_logger_buffer_.INFO("Routing success!");
154 return true;
155 }
156
157 AERROR << "Failed to search route with navigator.";
158 monitor_logger_buffer_.WARN("Routing failed! " +
159 routing_response->status().msg());
160 return false;
161}
162
163} // namespace routing
164} // namespace apollo
A general class to denote the return status of an API call.
Definition status.h:43
static Status OK()
generate a success status.
Definition status.h:60
static PointENU ToPointENU(const double x, const double y, const double z=0)
static const HDMap * BaseMapPtr()
int GetLanes(const apollo::common::PointENU &point, double distance, std::vector< LaneInfoConstPtr > *lanes) const
get all lanes in certain range
Definition hdmap.cc:90
bool Process(const std::shared_ptr< routing::RoutingRequest > &routing_request, routing::RoutingResponse *const routing_response)
Definition routing.cc:129
std::string Name() const
module name
Definition routing.cc:32
apollo::common::Status Init()
module initialization function
Definition routing.cc:37
apollo::common::Status Start()
module start function
Definition routing.cc:48
#define ACHECK(cond)
Definition log.h:80
#define ADEBUG
Definition log.h:41
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
#define AWARN
Definition log.h:43
std::string RoutingMapFile()
get routing map file path from flags.
Definition hdmap_util.cc:63
std::string BaseMapFile()
get base map file path from flags.
Definition hdmap_util.cc:47
class register implement
Definition arena_queue.h:37
repeated apollo::routing::LaneWaypoint waypoint
Definition routing.proto:14
optional apollo::routing::Measurement measurement
Definition routing.proto:26
optional apollo::common::StatusPb status
Definition routing.proto:31