Apollo 11.0
自动驾驶开放平台
trajectory_evaluator.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 *****************************************************************************/
16
18
19#include <algorithm>
20#include <limits>
21
22#include "absl/strings/str_cat.h"
23
24#include "cyber/common/file.h"
28
29namespace apollo {
30namespace planning {
31
32void TrajectoryEvaluator::WriteLog(const std::string& msg) {
33 AERROR << msg;
34 if (FLAGS_planning_offline_learning) {
35 EvaluatorLogger::GetStream() << msg << std::endl;
36 }
37}
38
39void TrajectoryEvaluator::EvaluateTrajectoryByTime(
40 const int frame_num, const std::string& obstacle_id,
41 const std::vector<std::pair<double, CommonTrajectoryPointFeature>>&
42 trajectory,
43 const double start_point_timestamp_sec, const double delta_time,
44 std::vector<TrajectoryPointFeature>* evaluated_trajectory) {
45 if (trajectory.empty() || (fabs(trajectory.front().first -
46 start_point_timestamp_sec) < delta_time &&
47 fabs(trajectory.back().first -
48 start_point_timestamp_sec) < delta_time)) {
49 return;
50 }
51
52 std::vector<apollo::common::TrajectoryPoint> updated_trajectory;
53 for (const auto& tp : trajectory) {
54 // CommonTrajectoryPointFeature => common::TrajectoryPoint
55 double relative_time = tp.first - start_point_timestamp_sec;
56 apollo::common::TrajectoryPoint trajectory_point;
57 Convert(tp.second, relative_time, &trajectory_point);
58 updated_trajectory.push_back(trajectory_point);
59 }
60
61 if (trajectory.front().first > trajectory.back().first) {
62 std::reverse(updated_trajectory.begin(), updated_trajectory.end());
63 }
64
65 DiscretizedTrajectory discretized_trajectory;
66 double last_relative_time = std::numeric_limits<double>::lowest();
67 for (const auto& tp : updated_trajectory) {
68 // filter out abnormal perception data
69 if (tp.relative_time() > last_relative_time) {
70 discretized_trajectory.AppendTrajectoryPoint(tp);
71 last_relative_time = tp.relative_time();
72 } else {
73 const std::string msg = absl::StrCat(
74 "DISCARD trajectory point: frame_num[", frame_num, "] obstacle_id[",
75 obstacle_id, "] last_relative_time[", last_relative_time,
76 "] relatice_time[", tp.relative_time(), "] relative_time_diff[",
77 tp.relative_time() - last_relative_time, "]");
78 WriteLog(msg);
79 }
80 }
81
82 const int low_bound = std::max(
83 -150.0, ceil(updated_trajectory.front().relative_time() / delta_time));
84 const int high_bound = std::min(
85 150.0, floor(updated_trajectory.back().relative_time() / delta_time));
86 ADEBUG << "frame_num[" << frame_num << "] obstacle_id[" << obstacle_id
87 << "] low[" << low_bound << "] high[" << high_bound << "]";
88 for (int i = low_bound; i <= high_bound; ++i) {
89 double timestamp_sec = start_point_timestamp_sec + i * delta_time;
90 double relative_time = i * delta_time;
91 auto tp = discretized_trajectory.Evaluate(relative_time);
92
93 // common::TrajectoryPoint => TrajectoryPointFeature
94 TrajectoryPointFeature trajectory_point;
95 Convert(tp, timestamp_sec, &trajectory_point);
96
97 evaluated_trajectory->push_back(trajectory_point);
98 }
99}
100
102 const double start_point_timestamp_sec, const double delta_time,
103 LearningDataFrame* learning_data_frame) {
104 std::vector<std::pair<double, CommonTrajectoryPointFeature>> trajectory;
105 for (const auto& adc_tp : learning_data_frame->adc_trajectory_point()) {
106 trajectory.push_back(
107 std::make_pair(adc_tp.timestamp_sec(), adc_tp.trajectory_point()));
108 }
109
110 if (trajectory.size() <= 1) {
111 const std::string msg = absl::StrCat(
112 "too short adc_trajectory_point. frame_num[",
113 learning_data_frame->frame_num(), "] size[", trajectory.size(), "]");
114 WriteLog(msg);
115 return;
116 }
117
118 learning_data_frame->clear_adc_trajectory_point();
119
120 if (fabs(trajectory.front().first - start_point_timestamp_sec) <=
121 delta_time) {
122 auto adc_trajectory_point = learning_data_frame->add_adc_trajectory_point();
123 adc_trajectory_point->set_timestamp_sec(trajectory.back().first);
124 adc_trajectory_point->mutable_trajectory_point()->CopyFrom(
125 trajectory.back().second);
126
127 const std::string msg =
128 absl::StrCat("too short adc_trajectory. frame_num[",
129 learning_data_frame->frame_num(), "] size[",
130 trajectory.size(), "] timestamp_diff[",
131 start_point_timestamp_sec - trajectory.front().first, "]");
132 WriteLog(msg);
133 return;
134 }
135
136 std::vector<TrajectoryPointFeature> evaluated_trajectory;
137 EvaluateTrajectoryByTime(learning_data_frame->frame_num(), "adc_trajectory",
138 trajectory, start_point_timestamp_sec, delta_time,
139 &evaluated_trajectory);
140 ADEBUG << "frame_num[" << learning_data_frame->frame_num()
141 << "] orig adc_trajectory[" << trajectory.size()
142 << "] evaluated_trajectory_size[" << evaluated_trajectory.size()
143 << "]";
144
145 for (const auto& tp : evaluated_trajectory) {
146 if (tp.trajectory_point().relative_time() <= 0.0 &&
147 tp.trajectory_point().relative_time() >=
148 -FLAGS_trajectory_time_length) {
149 auto adc_trajectory_point =
150 learning_data_frame->add_adc_trajectory_point();
151 adc_trajectory_point->set_timestamp_sec(tp.timestamp_sec());
152 adc_trajectory_point->mutable_trajectory_point()->CopyFrom(
153 tp.trajectory_point());
154 } else {
155 const std::string msg =
156 absl::StrCat("DISCARD adc_trajectory_point. frame_num[",
157 learning_data_frame->frame_num(), "] size[",
158 evaluated_trajectory.size(), "] relative_time[",
159 tp.trajectory_point().relative_time(), "]");
160 WriteLog(msg);
161 }
162 }
163}
164
166 const int frame_num,
167 const std::vector<TrajectoryPointFeature>& adc_future_trajectory,
168 const double start_point_timestamp_sec, const double delta_time,
169 std::vector<TrajectoryPointFeature>* evaluated_adc_future_trajectory) {
170 evaluated_adc_future_trajectory->clear();
171
172 std::vector<std::pair<double, CommonTrajectoryPointFeature>> trajectory;
173 for (const auto& adc_future_trajectory_point : adc_future_trajectory) {
174 trajectory.push_back(
175 std::make_pair(adc_future_trajectory_point.timestamp_sec(),
176 adc_future_trajectory_point.trajectory_point()));
177 }
178
179 if (trajectory.size() <= 1) {
180 const std::string msg =
181 absl::StrCat("too short adc_future_trajectory. frame_num[", frame_num,
182 "] size[", trajectory.size(), "]");
183 WriteLog(msg);
184 return;
185 }
186
187 if (fabs(trajectory.back().first - start_point_timestamp_sec) <= delta_time) {
188 const std::string msg =
189 absl::StrCat("too short adc_future_trajectory. frame_num[", frame_num,
190 "] size[", trajectory.size(), "] time_range[",
191 trajectory.back().first - start_point_timestamp_sec, "]");
192 WriteLog(msg);
193 return;
194 }
195
196 std::vector<TrajectoryPointFeature> evaluated_trajectory;
197 EvaluateTrajectoryByTime(frame_num, "adc_future_trajectory", trajectory,
198 start_point_timestamp_sec, delta_time,
199 &evaluated_trajectory);
200
201 ADEBUG << "frame_num[" << frame_num << "] orig adc_future_trajectory["
202 << trajectory.size() << "] evaluated_trajectory_size["
203 << evaluated_trajectory.size() << "]";
204
205 if (evaluated_trajectory.empty()) {
206 const std::string msg = absl::StrCat(
207 "WARNING: adc_future_trajectory not long enough. ", "frame_num[",
208 frame_num, "] size[", evaluated_trajectory.size(), "]");
209 WriteLog(msg);
210 } else {
211 const double time_range =
212 evaluated_trajectory.back().timestamp_sec() - start_point_timestamp_sec;
213 if (time_range < FLAGS_trajectory_time_length) {
214 const std::string msg = absl::StrCat(
215 "WARNING: adc_future_trajectory not long enough. ", "frame_num[",
216 frame_num, "] size[", evaluated_trajectory.size(), "] time_range[",
217 time_range, "]");
218 WriteLog(msg);
219 }
220 }
221
222 for (const auto& tp : evaluated_trajectory) {
223 if (tp.trajectory_point().relative_time() > 0.0 &&
224 tp.trajectory_point().relative_time() <= FLAGS_trajectory_time_length) {
225 evaluated_adc_future_trajectory->push_back(tp);
226 } else {
227 const std::string msg = absl::StrCat(
228 "DISCARD adc_future_trajectory_point. frame_num[", frame_num,
229 "] size[", evaluated_trajectory.size(), "] relative_time[",
230 tp.trajectory_point().relative_time(), "]");
231 WriteLog(msg);
232 }
233 }
234}
235
237 const double start_point_timestamp_sec, const double delta_time,
238 LearningDataFrame* learning_data_frame) {
239 for (int i = 0; i < learning_data_frame->obstacle_size(); ++i) {
240 const int obstacle_id = learning_data_frame->obstacle(i).id();
241 const auto obstacle_trajectory =
242 learning_data_frame->obstacle(i).obstacle_trajectory();
243 std::vector<std::pair<double, CommonTrajectoryPointFeature>> trajectory;
244 for (const auto& perception_obstacle :
245 obstacle_trajectory.perception_obstacle_history()) {
246 CommonTrajectoryPointFeature trajectory_point;
247 trajectory_point.mutable_path_point()->set_x(
248 perception_obstacle.position().x());
249 trajectory_point.mutable_path_point()->set_y(
250 perception_obstacle.position().y());
251 trajectory_point.mutable_path_point()->set_z(
252 perception_obstacle.position().z());
253 trajectory_point.mutable_path_point()->set_theta(
254 perception_obstacle.theta());
255
256 const double v = std::sqrt(perception_obstacle.velocity().x() *
257 perception_obstacle.velocity().x() +
258 perception_obstacle.velocity().y() *
259 perception_obstacle.velocity().y());
260 trajectory_point.set_v(v);
261
262 const double a = std::sqrt(perception_obstacle.acceleration().x() *
263 perception_obstacle.acceleration().x() +
264 perception_obstacle.acceleration().y() *
265 perception_obstacle.acceleration().y());
266 trajectory_point.set_a(a);
267
268 trajectory.push_back(std::make_pair(perception_obstacle.timestamp_sec(),
269 trajectory_point));
270 }
271
272 std::vector<TrajectoryPointFeature> evaluated_trajectory;
273 if (trajectory.size() == 1 ||
274 fabs(trajectory.front().first - start_point_timestamp_sec) <=
275 delta_time ||
276 fabs(trajectory.front().first - trajectory.back().first) <=
277 delta_time) {
278 ADEBUG << "too short obstacle_trajectory. frame_num["
279 << learning_data_frame->frame_num() << "] obstacle_id["
280 << obstacle_id << "] size[" << trajectory.size()
281 << "] timestamp_diff["
282 << start_point_timestamp_sec - trajectory.front().first
283 << "] time_range["
284 << fabs(trajectory.front().first - trajectory.back().first) << "]";
285
286 // pick at lease one point regardless of short timestamp,
287 // to avoid model failure
288 TrajectoryPointFeature trajectory_point;
289 trajectory_point.set_timestamp_sec(trajectory.front().first);
290 trajectory_point.mutable_trajectory_point()->CopyFrom(
291 trajectory.front().second);
292 evaluated_trajectory.push_back(trajectory_point);
293 } else {
294 EvaluateTrajectoryByTime(learning_data_frame->frame_num(),
295 std::to_string(obstacle_id), trajectory,
296 start_point_timestamp_sec, delta_time,
297 &evaluated_trajectory);
298
299 ADEBUG << "frame_num[" << learning_data_frame->frame_num()
300 << "] obstacle_id[" << obstacle_id << "] orig obstacle_trajectory["
301 << trajectory.size() << "] evaluated_trajectory_size["
302 << evaluated_trajectory.size() << "]";
303 }
304
305 // update learning_data
306 learning_data_frame->mutable_obstacle(i)
307 ->mutable_obstacle_trajectory()
308 ->clear_evaluated_trajectory_point();
309 for (const auto& tp : evaluated_trajectory) {
310 auto evaluated_trajectory_point = learning_data_frame->mutable_obstacle(i)
311 ->mutable_obstacle_trajectory()
312 ->add_evaluated_trajectory_point();
313 evaluated_trajectory_point->set_timestamp_sec(tp.timestamp_sec());
314 evaluated_trajectory_point->mutable_trajectory_point()->CopyFrom(
315 tp.trajectory_point());
316 }
317 }
318}
319
321 const double start_point_timestamp_sec, const double delta_time,
322 LearningDataFrame* learning_data_frame) {
323 for (int i = 0; i < learning_data_frame->obstacle_size(); ++i) {
324 const int obstacle_id = learning_data_frame->obstacle(i).id();
325 const auto obstacle_prediction =
326 learning_data_frame->obstacle(i).obstacle_prediction();
327 for (int j = 0; j < obstacle_prediction.trajectory_size(); ++j) {
328 const auto obstacle_prediction_trajectory =
329 obstacle_prediction.trajectory(j);
330
331 std::vector<std::pair<double, CommonTrajectoryPointFeature>> trajectory;
332 for (const auto& trajectory_point :
333 obstacle_prediction_trajectory.trajectory_point()) {
334 const double timestamp_sec =
335 obstacle_prediction.timestamp_sec() +
336 trajectory_point.trajectory_point().relative_time();
337 trajectory.push_back(
338 std::make_pair(timestamp_sec, trajectory_point.trajectory_point()));
339 }
340 if (fabs(trajectory.back().first - start_point_timestamp_sec) <=
341 delta_time) {
342 ADEBUG << "too short obstacle_prediction_trajectory. frame_num["
343 << learning_data_frame->frame_num() << "] obstacle_id["
344 << obstacle_id << "] size[" << trajectory.size()
345 << "] timestamp_diff["
346 << trajectory.back().first - start_point_timestamp_sec << "]";
347 continue;
348 }
349
350 std::vector<TrajectoryPointFeature> evaluated_trajectory;
351 EvaluateTrajectoryByTime(learning_data_frame->frame_num(),
352 std::to_string(obstacle_id), trajectory,
353 start_point_timestamp_sec, delta_time,
354 &evaluated_trajectory);
355
356 ADEBUG << "frame_num[" << learning_data_frame->frame_num()
357 << "] obstacle_id[" << obstacle_id
358 << "orig obstacle_prediction_trajectory[" << trajectory.size()
359 << "] evaluated_trajectory_size[" << evaluated_trajectory.size()
360 << "]";
361
362 // update learning_data
363 learning_data_frame->mutable_obstacle(i)
364 ->mutable_obstacle_prediction()
365 ->mutable_trajectory(j)
366 ->clear_trajectory_point();
367 for (const auto& tp : evaluated_trajectory) {
368 auto obstacle_prediction_trajectory_point =
369 learning_data_frame->mutable_obstacle(i)
370 ->mutable_obstacle_prediction()
371 ->mutable_trajectory(j)
372 ->add_trajectory_point();
373 obstacle_prediction_trajectory_point->set_timestamp_sec(
374 tp.timestamp_sec());
375 obstacle_prediction_trajectory_point->mutable_trajectory_point()
376 ->CopyFrom(tp.trajectory_point());
377 }
378 }
379 }
380}
381
382void TrajectoryEvaluator::Convert(const CommonTrajectoryPointFeature& tp,
383 const double relative_time,
384 common::TrajectoryPoint* trajectory_point) {
385 auto path_point = trajectory_point->mutable_path_point();
386 path_point->set_x(tp.path_point().x());
387 path_point->set_y(tp.path_point().y());
388 path_point->set_z(tp.path_point().z());
389 path_point->set_theta(tp.path_point().theta());
390 path_point->set_s(tp.path_point().s());
391 path_point->set_lane_id(tp.path_point().lane_id());
392 trajectory_point->set_v(tp.v());
393 trajectory_point->set_a(tp.a());
394 trajectory_point->set_relative_time(relative_time);
395 trajectory_point->mutable_gaussian_info()->CopyFrom(tp.gaussian_info());
396}
397
398void TrajectoryEvaluator::Convert(const common::TrajectoryPoint& tp,
399 const double timestamp_sec,
400 TrajectoryPointFeature* trajectory_point) {
401 trajectory_point->set_timestamp_sec(timestamp_sec);
402 auto path_point =
403 trajectory_point->mutable_trajectory_point()->mutable_path_point();
404 path_point->set_x(tp.path_point().x());
405 path_point->set_y(tp.path_point().y());
406 path_point->set_z(tp.path_point().z());
407 path_point->set_theta(tp.path_point().theta());
408 path_point->set_s(tp.path_point().s());
409 path_point->set_lane_id(tp.path_point().lane_id());
410 trajectory_point->mutable_trajectory_point()->set_v(tp.v());
411 trajectory_point->mutable_trajectory_point()->set_a(tp.a());
412 trajectory_point->mutable_trajectory_point()->set_relative_time(
413 tp.relative_time());
414 trajectory_point->mutable_trajectory_point()
415 ->mutable_gaussian_info()
416 ->CopyFrom(tp.gaussian_info());
417}
418
419} // namespace planning
420} // namespace apollo
static std::ofstream & GetStream()
void EvaluateADCFutureTrajectory(const int frame_num, const std::vector< TrajectoryPointFeature > &adc_future_trajectory, const double start_point_timestamp_sec, const double delta_time, std::vector< TrajectoryPointFeature > *evaluated_adc_future_trajectory)
void EvaluateADCTrajectory(const double start_point_timestamp_sec, const double delta_time, LearningDataFrame *learning_data_frame)
void EvaluateObstacleTrajectory(const double start_point_timestamp_sec, const double delta_time, LearningDataFrame *learning_data_frame)
void EvaluateObstaclePredictionTrajectory(const double start_point_timestamp_sec, const double delta_time, LearningDataFrame *learning_data_frame)
Planning module main class.
#define ADEBUG
Definition log.h:41
#define AERROR
Definition log.h:44
class register implement
Definition arena_queue.h:37
optional GaussianInfo gaussian_info
optional apollo::common::GaussianInfo gaussian_info
repeated ADCTrajectoryPoint adc_trajectory_point
optional PredictionObstacleFeature obstacle_prediction
optional ObstacleTrajectoryFeature obstacle_trajectory
repeated PredictionTrajectoryFeature trajectory