22#include "modules/common_msgs/prediction_msgs/lane_graph.pb.h"
39 CHECK_NOTNULL(obstacle);
46 if (!feature_ptr->
lane().has_lane_graph()) {
47 AERROR <<
"Obstacle [" << obstacle->
id() <<
"] has no lane graph.";
51 AERROR <<
"Obstacle [" << obstacle->
id()
52 <<
"] has no short-term trajectories.";
56 for (
int i = 0; i < feature_ptr->predicted_trajectory_size(); ++i) {
57 Trajectory* trajectory_ptr = feature_ptr->mutable_predicted_trajectory(i);
58 PostProcess(trajectory_ptr, cluster_ptr);
63void ExtrapolationPredictor::PostProcess(
Trajectory* trajectory_ptr,
66 static constexpr int kNumTailPoint = 5;
67 ExtrapolationPredictor::LaneSearchResult lane_search_result =
68 SearchExtrapolationLane(*trajectory_ptr, kNumTailPoint);
69 double extraplation_speed =
70 ComputeExtraplationSpeed(kNumTailPoint, *trajectory_ptr);
71 if (lane_search_result.found) {
72 ExtrapolateByLane(lane_search_result, extraplation_speed, trajectory_ptr,
75 ExtrapolateByFreeMove(kNumTailPoint, extraplation_speed, trajectory_ptr);
79ExtrapolationPredictor::LaneSearchResult
80ExtrapolationPredictor::SearchExtrapolationLane(
const Trajectory& trajectory,
81 const int num_tail_point) {
82 static constexpr double radius = 1.0;
83 static constexpr double angle_diff_threshold = M_PI / 3.0;
84 int num_trajectory_point = trajectory.trajectory_point_size();
86 LaneSearchResult lane_search_result;
87 for (
int i = num_trajectory_point - 1;
88 i >= num_trajectory_point - num_tail_point; --i) {
89 const TrajectoryPoint& trajectory_point = trajectory.trajectory_point(i);
90 const PathPoint& path_point = trajectory_point.path_point();
92 point_enu.set_x(path_point.x());
93 point_enu.set_y(path_point.y());
94 double heading = path_point.theta();
96 point_enu, radius, heading, angle_diff_threshold);
97 if (lane_info_ptr !=
nullptr) {
98 lane_search_result.found =
true;
99 lane_search_result.lane_id = lane_info_ptr->lane().id().id();
100 lane_search_result.point_index = i;
104 return lane_search_result;
107void ExtrapolationPredictor::ExtrapolateByLane(
108 const LaneSearchResult& lane_search_result,
const double extraplation_speed,
109 Trajectory* trajectory_ptr, ObstacleClusters* clusters_ptr) {
110 std::string start_lane_id = lane_search_result.lane_id;
111 int point_index = lane_search_result.point_index;
112 while (trajectory_ptr->trajectory_point_size() > point_index + 1) {
113 trajectory_ptr->mutable_trajectory_point()->RemoveLast();
116 int num_trajectory_point = trajectory_ptr->trajectory_point_size();
117 const TrajectoryPoint& last_point =
118 trajectory_ptr->trajectory_point(num_trajectory_point - 1);
120 Eigen::Vector2d position(last_point.path_point().x(),
121 last_point.path_point().y());
127 AERROR <<
"Position (" << position.x() <<
", " << position.y() <<
") "
128 <<
"cannot be projected onto lane [" << start_lane_id <<
"]";
132 double last_relative_time = last_point.relative_time();
134 FLAGS_prediction_trajectory_time_length - last_relative_time;
135 double time_resolution = FLAGS_prediction_trajectory_time_resolution;
136 double length = extraplation_speed * time_range;
138 LaneGraph lane_graph =
139 clusters_ptr->GetLaneGraph(lane_s, length,
false, lane_info_ptr);
140 CHECK_EQ(lane_graph.lane_sequence_size(), 1);
141 const LaneSequence& lane_sequence = lane_graph.lane_sequence(0);
142 int lane_segment_index = 0;
143 std::string lane_id =
144 lane_sequence.lane_segment(lane_segment_index).lane_id();
146 int num_point_remained =
static_cast<int>(time_range / time_resolution);
147 for (
int i = 1; i <= num_point_remained; ++i) {
148 double relative_time =
149 last_relative_time +
static_cast<double>(i) * time_resolution;
150 Eigen::Vector2d point;
154 AERROR <<
"Unable to get smooth point from lane [" << lane_id
155 <<
"] with s [" << lane_s <<
"] and l [" << lane_l <<
"]";
158 TrajectoryPoint* trajectory_point = trajectory_ptr->add_trajectory_point();
159 PathPoint* path_point = trajectory_point->mutable_path_point();
160 path_point->set_x(point.x());
161 path_point->set_y(point.y());
162 path_point->set_z(0.0);
163 path_point->set_theta(theta);
164 path_point->set_lane_id(lane_id);
165 trajectory_point->set_v(extraplation_speed);
166 trajectory_point->set_a(0.0);
167 trajectory_point->set_relative_time(relative_time);
169 lane_s += extraplation_speed * time_resolution;
171 lane_segment_index + 1 < lane_sequence.lane_segment_size()) {
172 lane_segment_index += 1;
174 lane_id = lane_sequence.lane_segment(lane_segment_index).lane_id();
177 lane_l *= FLAGS_go_approach_rate;
181void ExtrapolationPredictor::ExtrapolateByFreeMove(
182 const int num_tail_point,
const double extraplation_speed,
183 Trajectory* trajectory_ptr) {
184 int num_trajectory_point = trajectory_ptr->trajectory_point_size();
185 const TrajectoryPoint& last_point =
186 trajectory_ptr->trajectory_point(num_trajectory_point - 1);
187 double theta = last_point.path_point().theta();
188 double time_resolution = FLAGS_prediction_trajectory_time_resolution;
189 double time_length = FLAGS_prediction_trajectory_time_length;
190 double relative_time = last_point.relative_time() + time_resolution;
191 while (relative_time < time_length) {
192 int prev_size = trajectory_ptr->trajectory_point_size();
193 const TrajectoryPoint& prev_point =
194 trajectory_ptr->trajectory_point(prev_size - 1);
195 TrajectoryPoint* curr_point = trajectory_ptr->add_trajectory_point();
196 double dx = time_resolution * extraplation_speed * std::cos(theta);
197 double dy = time_resolution * extraplation_speed * std::sin(theta);
198 double curr_x = prev_point.path_point().x() + dx;
199 double curr_y = prev_point.path_point().y() + dy;
200 PathPoint* curr_path_point_ptr = curr_point->mutable_path_point();
201 curr_path_point_ptr->set_x(curr_x);
202 curr_path_point_ptr->set_y(curr_y);
203 curr_path_point_ptr->set_theta(theta);
204 curr_point->set_v(extraplation_speed);
205 curr_point->set_relative_time(relative_time);
207 relative_time += time_resolution;
211double ExtrapolationPredictor::ComputeExtraplationSpeed(
212 const int num_tail_point,
const Trajectory& trajectory) {
213 int num_trajectory_point = trajectory.trajectory_point_size();
214 CHECK_GT(num_trajectory_point, num_tail_point);
215 CHECK_GT(num_tail_point, 0);
217 int mid_index = num_trajectory_point - num_tail_point;
218 for (
int i = mid_index; i < num_trajectory_point; ++i) {
219 v_sum += trajectory.trajectory_point(i).v();
221 return v_sum /
static_cast<double>(num_tail_point);
Feature * mutable_latest_feature()
Get a pointer to the latest feature.
int id() const
Get the obstacle's ID.
size_t history_size() const
Get the number of historical features.
void SetPredictorType(const ObstacleConf::PredictorType &predictor_type)
ObstacleClusters * GetClustersPtr() const
Get the raw pointer of clusters_
static bool SmoothPointFromLane(const std::string &id, const double s, const double l, Eigen::Vector2d *point, double *heading)
Get the smooth point on a lane by a longitudinal coordinate.
static std::shared_ptr< const apollo::hdmap::LaneInfo > GetMostLikelyCurrentLane(const common::PointENU &position, const double radius, const double heading, const double angle_diff_threshold)
Get the lane that the position is on with minimal angle diff
static std::shared_ptr< const hdmap::LaneInfo > LaneById(const std::string &id)
Get a shared pointer to a lane by lane ID.
static bool GetProjection(const Eigen::Vector2d &position, const std::shared_ptr< const hdmap::LaneInfo > lane_info, double *s, double *l)
Get the frenet coordinates (s, l) on a lane by a position.
ObstacleConf::PredictorType predictor_type_
void Clear()
Clear private members
repeated Trajectory predicted_trajectory
@ EXTRAPOLATION_PREDICTOR