Apollo 10.0
自动驾驶开放平台
path_data.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
22
23#include <algorithm>
24
25#include "absl/strings/str_cat.h"
26#include "absl/strings/str_join.h"
27
28#include "cyber/common/log.h"
33
34namespace apollo {
35namespace planning {
36
42
44 if (reference_line_ == nullptr) {
45 AERROR << "Should NOT set discretized path when reference line is nullptr. "
46 "Please set reference line first.";
47 return false;
48 }
49 discretized_path_ = std::move(path);
50 if (!XYToSL(discretized_path_, &frenet_path_)) {
51 AERROR << "Fail to transfer discretized path to frenet path.";
52 return false;
53 }
54 DCHECK_EQ(discretized_path_.size(), frenet_path_.size());
55 return true;
56}
57
59 if (reference_line_ == nullptr) {
60 AERROR << "Should NOT set frenet path when reference line is nullptr. "
61 "Please set reference line first.";
62 return false;
63 }
64 frenet_path_ = std::move(frenet_path);
65 if (!SLToXY(frenet_path_, &discretized_path_)) {
66 AERROR << "Fail to transfer frenet path to discretized path.";
67 return false;
68 }
69 DCHECK_EQ(discretized_path_.size(), frenet_path_.size());
70 return true;
71}
72
74 std::vector<std::tuple<double, PathPointType, double>>
75 path_point_decision_guide) {
76 if (reference_line_ == nullptr) {
77 AERROR << "Should NOT set path_point_decision_guide when reference line is "
78 "nullptr. ";
79 return false;
80 }
81 if (frenet_path_.empty() || discretized_path_.empty()) {
82 AERROR << "Should NOT set path_point_decision_guide when frenet_path or "
83 "world frame trajectory is empty. ";
84 return false;
85 }
86 path_point_decision_guide_ = std::move(path_point_decision_guide);
87 return true;
88}
89
91 return discretized_path_;
92}
93
95 return frenet_path_;
96}
97
98const std::vector<std::tuple<double, PathData::PathPointType, double>>
100 return path_point_decision_guide_;
101}
102
103bool PathData::Empty() const {
104 return discretized_path_.empty() && frenet_path_.empty();
105}
106
107void PathData::SetReferenceLine(const ReferenceLine *reference_line) {
108 Clear();
109 reference_line_ = reference_line;
110}
111
113 return discretized_path_.Evaluate(s);
114}
115
116bool PathData::GetPathPointWithRefS(const double ref_s,
117 common::PathPoint *const path_point) const {
118 ACHECK(reference_line_);
119 DCHECK_EQ(discretized_path_.size(), frenet_path_.size());
120 if (ref_s < 0) {
121 AERROR << "ref_s[" << ref_s << "] should be > 0";
122 return false;
123 }
124 if (ref_s > frenet_path_.back().s()) {
125 AERROR << "ref_s is larger than the length of frenet_path_ length ["
126 << frenet_path_.back().s() << "].";
127 return false;
128 }
129
130 uint32_t index = 0;
131 const double kDistanceEpsilon = 1e-3;
132 for (uint32_t i = 0; i + 1 < frenet_path_.size(); ++i) {
133 if (fabs(ref_s - frenet_path_.at(i).s()) < kDistanceEpsilon) {
134 path_point->CopyFrom(discretized_path_.at(i));
135 return true;
136 }
137 if (frenet_path_.at(i).s() < ref_s && ref_s <= frenet_path_.at(i + 1).s()) {
138 index = i;
139 break;
140 }
141 }
142 double r = (ref_s - frenet_path_.at(index).s()) /
143 (frenet_path_.at(index + 1).s() - frenet_path_.at(index).s());
144
145 const double discretized_path_s = discretized_path_.at(index).s() +
146 r * (discretized_path_.at(index + 1).s() -
147 discretized_path_.at(index).s());
148 path_point->CopyFrom(discretized_path_.Evaluate(discretized_path_s));
149
150 return true;
151}
152
154 discretized_path_.clear();
155 frenet_path_.clear();
156 path_point_decision_guide_.clear();
157 path_reference_.clear();
158 reference_line_ = nullptr;
159}
160
161std::string PathData::DebugString() const {
162 const auto limit =
163 std::min(discretized_path_.size(),
164 static_cast<size_t>(FLAGS_trajectory_point_num_for_debug));
165
166 return absl::StrCat(
167 "[\n",
168 absl::StrJoin(discretized_path_.begin(),
169 discretized_path_.begin() + limit, ",\n",
171 "]\n");
172}
173
174bool PathData::SLToXY(const FrenetFramePath &frenet_path,
175 DiscretizedPath *const discretized_path) {
176 std::vector<common::PathPoint> path_points;
177 for (const common::FrenetFramePoint &frenet_point : frenet_path) {
178 const common::SLPoint sl_point =
179 PointFactory::ToSLPoint(frenet_point.s(), frenet_point.l());
180 common::math::Vec2d cartesian_point;
181 if (!reference_line_->SLToXY(sl_point, &cartesian_point)) {
182 AERROR << "Fail to convert sl point to xy point";
183 return false;
184 }
185 const ReferencePoint ref_point =
186 reference_line_->GetReferencePoint(frenet_point.s());
187 const double theta = CartesianFrenetConverter::CalculateTheta(
188 ref_point.heading(), ref_point.kappa(), frenet_point.l(),
189 frenet_point.dl());
190 ADEBUG << "frenet_point: " << frenet_point.ShortDebugString();
191 const double kappa = CartesianFrenetConverter::CalculateKappa(
192 ref_point.kappa(), ref_point.dkappa(), frenet_point.l(),
193 frenet_point.dl(), frenet_point.ddl());
194
195 double s = 0.0;
196 double dkappa = 0.0;
197 if (!path_points.empty()) {
198 common::math::Vec2d last = PointFactory::ToVec2d(path_points.back());
199 const double distance = (last - cartesian_point).Length();
200 s = path_points.back().s() + distance;
201 dkappa = (kappa - path_points.back().kappa()) / distance;
202 }
203 path_points.push_back(PointFactory::ToPathPoint(cartesian_point.x(),
204 cartesian_point.y(), 0.0, s,
205 theta, kappa, dkappa));
206 }
207 *discretized_path = DiscretizedPath(std::move(path_points));
208
209 return true;
210}
211
212bool PathData::XYToSL(const DiscretizedPath &discretized_path,
213 FrenetFramePath *const frenet_path) {
214 ACHECK(reference_line_);
215 std::vector<common::FrenetFramePoint> frenet_frame_points;
216 const double max_len = reference_line_->Length();
217 for (const auto &path_point : discretized_path) {
218 common::FrenetFramePoint frenet_point =
219 reference_line_->GetFrenetPoint(path_point);
220 if (!frenet_point.has_s()) {
221 SLPoint sl_point;
222 if (!reference_line_->XYToSL(path_point, &sl_point)) {
223 AERROR << "Fail to transfer cartesian point to frenet point.";
224 return false;
225 }
226 common::FrenetFramePoint frenet_point;
227 // NOTICE: does not set dl and ddl here. Add if needed.
228 frenet_point.set_s(std::max(0.0, std::min(sl_point.s(), max_len)));
229 frenet_point.set_l(sl_point.l());
230 frenet_frame_points.push_back(std::move(frenet_point));
231 continue;
232 }
233 frenet_point.set_s(std::max(0.0, std::min(frenet_point.s(), max_len)));
234 frenet_frame_points.push_back(std::move(frenet_point));
235 }
236 *frenet_path = FrenetFramePath(std::move(frenet_frame_points));
237 return true;
238}
239
241 ACHECK(reference_line_);
242 std::vector<common::FrenetFramePoint> frenet_frame_points;
243 frenet_frame_points.emplace_back(frenet_point);
244
245 for (const common::FrenetFramePoint fp : frenet_path_) {
246 if (std::fabs(fp.s() - frenet_point.s()) < 1e-6) {
247 continue;
248 }
249 if (fp.s() > frenet_point.s()) {
250 frenet_frame_points.push_back(fp);
251 }
252 }
253 SetFrenetPath(FrenetFramePath(std::move(frenet_frame_points)));
254 return true;
255}
256
258 reference_line_ = reference_line;
259 return SetDiscretizedPath(discretized_path_);
260}
261
262void PathData::set_path_label(const std::string &label) { path_label_ = label; }
263
264const std::string &PathData::path_label() const { return path_label_; }
265
266const std::vector<PathPoint> &PathData::path_reference() const {
267 return path_reference_;
268}
269
271 const std::vector<PathPoint> &path_reference) {
272 path_reference_ = std::move(path_reference);
273}
274
275} // namespace planning
276} // namespace apollo
static double CalculateTheta(const double rtheta, const double rkappa, const double l, const double dl)
static double CalculateKappa(const double rkappa, const double rdkappa, const double l, const double dl, const double ddl)
Implements a class of 2-dimensional vectors.
Definition vec2d.h:42
double y() const
Getter for y component
Definition vec2d.h:57
double x() const
Getter for x component
Definition vec2d.h:54
static math::Vec2d ToVec2d(const XY &xy)
static SLPoint ToSLPoint(const double s, const double l)
static PathPoint ToPathPoint(const double x, const double y, const double z=0, const double s=0, const double theta=0, const double kappa=0, const double dkappa=0, const double ddkappa=0)
common::PathPoint Evaluate(const double path_s) const
const std::vector< common::PathPoint > & path_reference() const
Definition path_data.cc:266
const std::string & path_label() const
Definition path_data.cc:264
bool LeftTrimWithRefS(const common::FrenetFramePoint &frenet_point)
Definition path_data.cc:240
void SetReferenceLine(const ReferenceLine *reference_line)
Definition path_data.cc:107
common::PathPoint GetPathPointWithPathS(const double s) const
Definition path_data.cc:112
const FrenetFramePath & frenet_frame_path() const
Definition path_data.cc:94
bool SetPathPointDecisionGuide(std::vector< std::tuple< double, PathPointType, double > > path_point_decision_guide)
Definition path_data.cc:73
bool GetPathPointWithRefS(const double ref_s, common::PathPoint *const path_point) const
Definition path_data.cc:116
bool SetDiscretizedPath(DiscretizedPath path)
Definition path_data.cc:43
bool UpdateFrenetFramePath(const ReferenceLine *reference_line)
Definition path_data.cc:257
std::string DebugString() const
Definition path_data.cc:161
const DiscretizedPath & discretized_path() const
Definition path_data.cc:90
const std::vector< std::tuple< double, PathPointType, double > > & path_point_decision_guide() const
Definition path_data.cc:99
void set_path_label(const std::string &label)
Definition path_data.cc:262
bool SetFrenetPath(FrenetFramePath frenet_path)
Definition path_data.cc:58
void set_path_reference(const std::vector< common::PathPoint > &path_reference)
Definition path_data.cc:270
ReferencePoint GetReferencePoint(const double s) const
bool SLToXY(const common::SLPoint &sl_point, common::math::Vec2d *const xy_point) const
bool XYToSL(const common::math::Vec2d &xy_point, common::SLPoint *const sl_point, double warm_start_s=-1.0) const
Transvert Cartesian coordinates to Frenet.
common::FrenetFramePoint GetFrenetPoint(const common::PathPoint &path_point) const
Planning module main class.
#define ACHECK(cond)
Definition log.h:80
#define ADEBUG
Definition log.h:41
#define AERROR
Definition log.h:44
class register implement
Definition arena_queue.h:37
Some string util functions.