22#include "absl/strings/str_cat.h"
32void TrajectoryEvaluator::WriteLog(
const std::string& msg) {
34 if (FLAGS_planning_offline_learning) {
39void TrajectoryEvaluator::EvaluateTrajectoryByTime(
40 const int frame_num,
const std::string& obstacle_id,
41 const std::vector<std::pair<double, CommonTrajectoryPointFeature>>&
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)) {
52 std::vector<apollo::common::TrajectoryPoint> updated_trajectory;
53 for (
const auto& tp : trajectory) {
55 double relative_time = tp.first - start_point_timestamp_sec;
57 Convert(tp.second, relative_time, &trajectory_point);
58 updated_trajectory.push_back(trajectory_point);
61 if (trajectory.front().first > trajectory.back().first) {
62 std::reverse(updated_trajectory.begin(), updated_trajectory.end());
65 DiscretizedTrajectory discretized_trajectory;
66 double last_relative_time = std::numeric_limits<double>::lowest();
67 for (
const auto& tp : updated_trajectory) {
69 if (tp.relative_time() > last_relative_time) {
70 discretized_trajectory.AppendTrajectoryPoint(tp);
71 last_relative_time = tp.relative_time();
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,
"]");
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);
94 TrajectoryPointFeature trajectory_point;
95 Convert(tp, timestamp_sec, &trajectory_point);
97 evaluated_trajectory->push_back(trajectory_point);
102 const double start_point_timestamp_sec,
const double delta_time,
104 std::vector<std::pair<double, CommonTrajectoryPointFeature>> trajectory;
106 trajectory.push_back(
107 std::make_pair(adc_tp.timestamp_sec(), adc_tp.trajectory_point()));
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(),
"]");
118 learning_data_frame->clear_adc_trajectory_point();
120 if (fabs(trajectory.front().first - start_point_timestamp_sec) <=
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);
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,
"]");
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);
141 <<
"] orig adc_trajectory[" << trajectory.size()
142 <<
"] evaluated_trajectory_size[" << evaluated_trajectory.size()
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());
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(),
"]");
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();
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()));
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(),
"]");
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,
"]");
196 std::vector<TrajectoryPointFeature> evaluated_trajectory;
197 EvaluateTrajectoryByTime(frame_num,
"adc_future_trajectory", trajectory,
198 start_point_timestamp_sec, delta_time,
199 &evaluated_trajectory);
201 ADEBUG <<
"frame_num[" << frame_num <<
"] orig adc_future_trajectory["
202 << trajectory.size() <<
"] evaluated_trajectory_size["
203 << evaluated_trajectory.size() <<
"]";
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(),
"]");
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[",
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);
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(),
"]");
237 const double start_point_timestamp_sec,
const double delta_time,
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 =
243 std::vector<std::pair<double, CommonTrajectoryPointFeature>> trajectory;
244 for (
const auto& perception_obstacle :
245 obstacle_trajectory.perception_obstacle_history()) {
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());
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);
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);
268 trajectory.push_back(std::make_pair(perception_obstacle.timestamp_sec(),
272 std::vector<TrajectoryPointFeature> evaluated_trajectory;
273 if (trajectory.size() == 1 ||
274 fabs(trajectory.front().first - start_point_timestamp_sec) <=
276 fabs(trajectory.front().first - trajectory.back().first) <=
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
284 << fabs(trajectory.front().first - trajectory.back().first) <<
"]";
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);
294 EvaluateTrajectoryByTime(learning_data_frame->
frame_num(),
295 std::to_string(obstacle_id), trajectory,
296 start_point_timestamp_sec, delta_time,
297 &evaluated_trajectory);
300 <<
"] obstacle_id[" << obstacle_id <<
"] orig obstacle_trajectory["
301 << trajectory.size() <<
"] evaluated_trajectory_size["
302 << evaluated_trajectory.size() <<
"]";
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());
321 const double start_point_timestamp_sec,
const double delta_time,
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 =
327 for (
int j = 0; j < obstacle_prediction.trajectory_size(); ++j) {
328 const auto obstacle_prediction_trajectory =
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() +
337 trajectory.push_back(
338 std::make_pair(timestamp_sec, trajectory_point.trajectory_point()));
340 if (fabs(trajectory.back().first - start_point_timestamp_sec) <=
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 <<
"]";
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);
357 <<
"] obstacle_id[" << obstacle_id
358 <<
"orig obstacle_prediction_trajectory[" << trajectory.size()
359 <<
"] evaluated_trajectory_size[" << evaluated_trajectory.size()
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(
375 obstacle_prediction_trajectory_point->mutable_trajectory_point()
376 ->CopyFrom(tp.trajectory_point());
383 const double relative_time,
385 auto path_point = trajectory_point->mutable_path_point();
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());
399 const double timestamp_sec,
400 TrajectoryPointFeature* trajectory_point) {
401 trajectory_point->set_timestamp_sec(timestamp_sec);
403 trajectory_point->mutable_trajectory_point()->mutable_path_point();
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(
414 trajectory_point->mutable_trajectory_point()
415 ->mutable_gaussian_info()
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.
optional double relative_time
optional PathPoint path_point
optional GaussianInfo gaussian_info
optional CommonPathPointFeature path_point
optional apollo::common::GaussianInfo gaussian_info
repeated ADCTrajectoryPoint adc_trajectory_point
repeated ObstacleFeature obstacle
optional uint32 frame_num
optional PredictionObstacleFeature obstacle_prediction
optional ObstacleTrajectoryFeature obstacle_trajectory
repeated PredictionTrajectoryFeature trajectory