Apollo 11.0
自动驾驶开放平台
old_routing_adapter.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2023 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 <limits>
24
25#include "modules/external_command/old_routing_adapter/proto/old_routing_adapter.pb.h"
26
28
33namespace apollo {
34namespace old_routing_adapter {
35
40
41OldRoutingAdapter::OldRoutingAdapter() : command_id_(0), hdmap_(nullptr) {}
42
45 // Load the config.
47 if (!GetProtoConfig(&config)) {
48 AERROR << "Unable to load OldRoutingAdapter conf file: "
49 << ConfigFilePath();
50 return false;
51 }
52 // Init the readers, writers and service clients.
53 routing_request_reader_ = node_->CreateReader<routing::RoutingRequest>(
54 config.routing_request_topic(),
55 [this](const std::shared_ptr<routing::RoutingRequest>& routing_request) {
56 ADEBUG << "Received routing request: run callback.";
57 OnRoutingRequest(routing_request);
58 });
59 lane_follow_command_client_ =
63 valet_parking_command_client_ =
67 return true;
68}
69
70void OldRoutingAdapter::OnRoutingRequest(
71 const std::shared_ptr<routing::RoutingRequest>& routing_request) {
72 // Delay sending routing request and wait for localization updated.
73 cyber::SleepFor(std::chrono::microseconds(500));
74 AINFO << "Receive routing request " << routing_request->DebugString();
75 if (routing_request->has_parking_info()) {
76 auto valet_parking_command = std::make_shared<ValetParkingCommand>();
77 // Copy the way points from RoutingRequest.
78 // int way_point_size = routing_request->waypoint().size();
79 // if (way_point_size > 0) {
80 // CopyRoutingRequest<ValetParkingCommand>(
81 // routing_request, 0, routing_request->waypoint().size(),
82 // valet_parking_command.get());
83 // }
84 valet_parking_command->set_parking_spot_id(
85 routing_request->parking_info().parking_space_id());
86 auto response =
87 valet_parking_command_client_->SendRequest(valet_parking_command);
88 AINFO << "Send valet parking command "
89 << valet_parking_command->DebugString();
90 if (nullptr == response) {
91 AERROR << "Failed to send request of valet parking!";
92 }
93 } else {
94 auto lane_follow_command = std::make_shared<LaneFollowCommand>();
95 // Copy the way points from RoutingRequest.
96 int way_point_size = routing_request->waypoint().size();
97 AINFO << routing_request->DebugString();
98 CHECK_GT(way_point_size, 0);
99 if (way_point_size > 1) {
100 routing_request->mutable_waypoint()->DeleteSubrange(0, 1);
101 CopyRoutingRequest<LaneFollowCommand>(
102 routing_request, 0, way_point_size - 2, lane_follow_command.get());
103 --way_point_size;
104 }
105 // Copy the end point.
106 lane_follow_command->mutable_header()->CopyFrom(routing_request->header());
107 Convert(routing_request->waypoint().Get(way_point_size - 1),
108 lane_follow_command->mutable_end_pose());
109 AINFO << "Send lane follow command " << lane_follow_command->DebugString();
110 auto response =
111 lane_follow_command_client_->SendRequest(lane_follow_command);
112 if (nullptr == response) {
113 AERROR << "Failed to get response of lane follow!";
114 }
115 }
116}
117
118void OldRoutingAdapter::Convert(
119 const apollo::routing::LaneWaypoint& lane_way_point,
120 apollo::external_command::Pose* pose) const {
121 CHECK(lane_way_point.has_pose());
122 const auto& way_pose = lane_way_point.pose();
123 pose->set_x(way_pose.x());
124 pose->set_y(way_pose.y());
125 if (lane_way_point.has_heading()) {
126 pose->set_heading(lane_way_point.heading());
127 return;
128 }
129 // Get heading according to lane id
130 if (lane_way_point.has_id()) {
131 pose->set_heading(
132 GetNearestHeading(lane_way_point.id(), way_pose.x(), way_pose.y()));
133 }
134}
135
136double OldRoutingAdapter::GetNearestHeading(const std::string& lane_id,
137 double x, double y) const {
138 hdmap::Id id;
139 id.set_id(lane_id);
140 auto lane = hdmap_->GetLaneById(id);
141 AINFO << "hdmap lane id " << lane_id << " " << hdmap::BaseMapFile();
142 CHECK_NOTNULL(lane);
143 common::math::Vec2d proj_pt(0.0, 0.0);
144 common::math::Vec2d point(x, y);
145 double s_offset = 0.0;
146 int s_offset_index = 0;
147 lane->DistanceTo(point, &proj_pt, &s_offset, &s_offset_index);
148 return lane->headings()[s_offset_index];
149}
150
151} // namespace old_routing_adapter
152} // namespace apollo
bool GetProtoConfig(T *config) const
const std::string & ConfigFilePath() const
std::shared_ptr< Node > node_
static const HDMap * BaseMapPtr()
LaneInfoConstPtr GetLaneById(const Id &id) const
Definition hdmap.cc:34
#define ADEBUG
Definition log.h:41
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
std::string BaseMapFile()
get base map file path from flags.
Definition hdmap_util.cc:47
class register implement
Definition arena_queue.h:37
optional apollo::common::PointENU pose