Apollo 11.0
自动驾驶开放平台
rtk_replay_planner.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 <memory>
20#include <utility>
21
22#include "absl/strings/str_split.h"
23
24#include "cyber/common/log.h"
27
28namespace apollo {
29namespace planning {
30
34
36 const std::shared_ptr<DependencyInjector>& injector,
37 const std::string& config_path) {
38 Planner::Init(injector, config_path);
39 ReadTrajectoryFile(FLAGS_rtk_trajectory_filename);
40 return Status::OK();
41}
42
43Status RTKReplayPlanner::Plan(const TrajectoryPoint& planning_start_point,
44 Frame* frame,
45 ADCTrajectory* ptr_computed_trajectory) {
46 auto status = Status::OK();
47 bool has_plan = false;
48 auto it = std::find_if(
49 frame->mutable_reference_line_info()->begin(),
50 frame->mutable_reference_line_info()->end(),
51 [](const ReferenceLineInfo& ref) { return ref.IsChangeLanePath(); });
52 if (it != frame->mutable_reference_line_info()->end()) {
53 status = PlanOnReferenceLine(planning_start_point, frame, &(*it));
54 has_plan =
55 (it->IsDrivable() && it->IsChangeLanePath() &&
56 it->trajectory().GetSpatialLength() > FLAGS_change_lane_min_length);
57 if (!has_plan) {
58 AERROR << "Fail to plan for lane change.";
59 }
60 }
61
62 if (!has_plan || !FLAGS_prioritize_change_lane) {
63 for (auto& reference_line_info : *frame->mutable_reference_line_info()) {
64 if (reference_line_info.IsChangeLanePath()) {
65 continue;
66 }
67 status = PlanOnReferenceLine(planning_start_point, frame,
68 &reference_line_info);
69 if (status != Status::OK()) {
70 AERROR << "planner failed to make a driving plan for: "
71 << reference_line_info.Lanes().Id();
72 }
73 }
74 }
75 return status;
76}
77
79 const TrajectoryPoint& planning_init_point, Frame*,
80 ReferenceLineInfo* reference_line_info) {
81 if (complete_rtk_trajectory_.empty() || complete_rtk_trajectory_.size() < 2) {
82 const std::string msg =
83 "RTKReplayPlanner doesn't have a recorded trajectory or "
84 "the recorded trajectory doesn't have enough valid trajectory "
85 "points.";
86 AERROR << msg;
87 return Status(ErrorCode::PLANNING_ERROR, msg);
88 }
89
90 std::uint32_t matched_index =
91 QueryPositionMatchedPoint(planning_init_point, complete_rtk_trajectory_);
92
93 std::uint32_t forward_buffer =
94 static_cast<std::uint32_t>(FLAGS_rtk_trajectory_forward);
95 // end_index is excluded.
96 std::uint32_t end_index = std::min<std::uint32_t>(
97 static_cast<std::uint32_t>(complete_rtk_trajectory_.size()),
98 matched_index + forward_buffer);
99
100 // auto* trajectory_points = trajectory_pb->mutable_trajectory_point();
101 std::vector<TrajectoryPoint> trajectory_points(
102 complete_rtk_trajectory_.begin() + matched_index,
103 complete_rtk_trajectory_.begin() + end_index);
104
105 // reset relative time
106 double zero_time = complete_rtk_trajectory_[matched_index].relative_time();
107 for (auto& trajectory_point : trajectory_points) {
108 trajectory_point.set_relative_time(trajectory_point.relative_time() -
109 zero_time);
110 }
111
112 // check if the trajectory has enough points;
113 // if not, append the last points multiple times and
114 // adjust their corresponding time stamps.
115 while (trajectory_points.size() <
116 static_cast<size_t>(FLAGS_rtk_trajectory_forward)) {
117 const auto& last_point = trajectory_points.rbegin();
118 auto new_point = last_point;
119 new_point->set_relative_time(new_point->relative_time() +
120 FLAGS_rtk_trajectory_resolution);
121 trajectory_points.push_back(*new_point);
122 }
123 reference_line_info->SetTrajectory(DiscretizedTrajectory(trajectory_points));
124 return Status::OK();
125}
126
127void RTKReplayPlanner::ReadTrajectoryFile(const std::string& filename) {
128 if (!complete_rtk_trajectory_.empty()) {
129 complete_rtk_trajectory_.clear();
130 }
131
132 std::ifstream file_in(filename.c_str());
133 if (!file_in.is_open()) {
134 AERROR << "RTKReplayPlanner cannot open trajectory file: " << filename;
135 return;
136 }
137
138 std::string line;
139 // skip the header line.
140 getline(file_in, line);
141
142 while (true) {
143 getline(file_in, line);
144 if (line == "") {
145 break;
146 }
147
148 const std::vector<std::string> tokens =
149 absl::StrSplit(line, absl::ByAnyChar("\t "));
150 if (tokens.size() < 11) {
151 AERROR << "RTKReplayPlanner parse line failed; the data dimension does "
152 "not match.";
153 AERROR << line;
154 continue;
155 }
156
157 TrajectoryPoint point;
158 point.mutable_path_point()->set_x(std::stod(tokens[0]));
159 point.mutable_path_point()->set_y(std::stod(tokens[1]));
160 point.mutable_path_point()->set_z(std::stod(tokens[2]));
161
162 point.set_v(std::stod(tokens[3]));
163 point.set_a(std::stod(tokens[4]));
164
165 point.mutable_path_point()->set_kappa(std::stod(tokens[5]));
166 point.mutable_path_point()->set_dkappa(std::stod(tokens[6]));
167
168 point.set_relative_time(std::stod(tokens[7]));
169
170 point.mutable_path_point()->set_theta(std::stod(tokens[8]));
171
172 point.mutable_path_point()->set_s(std::stod(tokens[10]));
173 complete_rtk_trajectory_.push_back(std::move(point));
174 }
175
176 file_in.close();
177}
178
179std::uint32_t RTKReplayPlanner::QueryPositionMatchedPoint(
180 const TrajectoryPoint& start_point,
181 const std::vector<TrajectoryPoint>& trajectory) const {
182 auto func_distance_square = [](const TrajectoryPoint& point, const double x,
183 const double y) {
184 double dx = point.path_point().x() - x;
185 double dy = point.path_point().y() - y;
186 return dx * dx + dy * dy;
187 };
188 double d_min =
189 func_distance_square(trajectory.front(), start_point.path_point().x(),
190 start_point.path_point().y());
191 std::uint32_t index_min = 0;
192 for (std::uint32_t i = 1; i < trajectory.size(); ++i) {
193 double d_temp =
194 func_distance_square(trajectory[i], start_point.path_point().x(),
195 start_point.path_point().y());
196 if (d_temp < d_min) {
197 d_min = d_temp;
198 index_min = i;
199 }
200 }
201 return index_min;
202}
203
204} // namespace planning
205} // 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
Frame holds all data for one planning cycle.
Definition frame.h:62
std::list< ReferenceLineInfo > * mutable_reference_line_info()
Definition frame.cc:127
virtual apollo::common::Status Init(const std::shared_ptr< DependencyInjector > &injector, const std::string &config_path="")
Definition planner.h:54
apollo::common::Status PlanOnReferenceLine(const common::TrajectoryPoint &planning_init_point, Frame *frame, ReferenceLineInfo *reference_line_info) override
Override function Plan in parent class Planner.
apollo::common::Status Init(const std::shared_ptr< DependencyInjector > &injector, const std::string &config_path="") override
apollo::common::Status Plan(const common::TrajectoryPoint &planning_init_point, Frame *frame, ADCTrajectory *ptr_computed_trajectory) override
Override function Plan in parent class Planner.
void ReadTrajectoryFile(const std::string &filename)
Read the recorded trajectory file.
ReferenceLineInfo holds all data for one reference line.
void SetTrajectory(const DiscretizedTrajectory &trajectory)
Planning module main class.
#define AERROR
Definition log.h:44
class register implement
Definition arena_queue.h:37