Apollo 11.0
自动驾驶开放平台
extrapolation_predictor.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2019 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
22#include "modules/common_msgs/prediction_msgs/lane_graph.pb.h"
23
24namespace apollo {
25namespace prediction {
26
29
33
35 const ADCTrajectoryContainer* adc_trajectory_container, Obstacle* obstacle,
36 ObstaclesContainer* obstacles_container) {
37 Clear();
38
39 CHECK_NOTNULL(obstacle);
40 CHECK_GT(obstacle->history_size(), 0U);
41
43
44 Feature* feature_ptr = obstacle->mutable_latest_feature();
45
46 if (!feature_ptr->lane().has_lane_graph()) {
47 AERROR << "Obstacle [" << obstacle->id() << "] has no lane graph.";
48 return false;
49 }
50 if (feature_ptr->predicted_trajectory().empty()) {
51 AERROR << "Obstacle [" << obstacle->id()
52 << "] has no short-term trajectories.";
53 return false;
54 }
55 auto* cluster_ptr = obstacles_container->GetClustersPtr();
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);
59 }
60 return true;
61}
62
63void ExtrapolationPredictor::PostProcess(Trajectory* trajectory_ptr,
64 ObstacleClusters* clusters_ptr) {
65 // TODO(kechxu) handle corner cases
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,
73 clusters_ptr);
74 } else {
75 ExtrapolateByFreeMove(kNumTailPoint, extraplation_speed, trajectory_ptr);
76 }
77}
78
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();
85
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();
95 auto lane_info_ptr = PredictionMap::GetMostLikelyCurrentLane(
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;
101 break;
102 }
103 }
104 return lane_search_result;
105}
106
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();
114 }
115 auto lane_info_ptr = PredictionMap::LaneById(start_lane_id);
116 int num_trajectory_point = trajectory_ptr->trajectory_point_size();
117 const TrajectoryPoint& last_point =
118 trajectory_ptr->trajectory_point(num_trajectory_point - 1);
119
120 Eigen::Vector2d position(last_point.path_point().x(),
121 last_point.path_point().y());
122 double lane_s = 0.0;
123 double lane_l = 0.0;
124 bool projected =
125 PredictionMap::GetProjection(position, lane_info_ptr, &lane_s, &lane_l);
126 if (!projected) {
127 AERROR << "Position (" << position.x() << ", " << position.y() << ") "
128 << "cannot be projected onto lane [" << start_lane_id << "]";
129 return;
130 }
131
132 double last_relative_time = last_point.relative_time();
133 double time_range =
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;
137
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();
145
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;
151 double theta = M_PI;
152 if (!PredictionMap::SmoothPointFromLane(lane_id, lane_s, lane_l, &point,
153 &theta)) {
154 AERROR << "Unable to get smooth point from lane [" << lane_id
155 << "] with s [" << lane_s << "] and l [" << lane_l << "]";
156 break;
157 }
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);
168
169 lane_s += extraplation_speed * time_resolution;
170 while (lane_s > PredictionMap::LaneById(lane_id)->total_length() &&
171 lane_segment_index + 1 < lane_sequence.lane_segment_size()) {
172 lane_segment_index += 1;
173 lane_s = lane_s - PredictionMap::LaneById(lane_id)->total_length();
174 lane_id = lane_sequence.lane_segment(lane_segment_index).lane_id();
175 }
176
177 lane_l *= FLAGS_go_approach_rate;
178 }
179}
180
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);
206
207 relative_time += time_resolution;
208 }
209}
210
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);
216 double v_sum = 0.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();
220 }
221 return v_sum / static_cast<double>(num_tail_point);
222}
223
224} // namespace prediction
225} // namespace apollo
bool Predict(const ADCTrajectoryContainer *adc_trajectory_container, Obstacle *obstacle, ObstaclesContainer *obstacles_container) override
Make prediction
Prediction obstacle.
Definition obstacle.h:52
Feature * mutable_latest_feature()
Get a pointer to the latest feature.
Definition obstacle.cc:88
int id() const
Get the obstacle's ID.
Definition obstacle.cc:60
size_t history_size() const
Get the number of historical features.
Definition obstacle.cc:93
void SetPredictorType(const ObstacleConf::PredictorType &predictor_type)
Definition obstacle.cc:1489
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_
Definition predictor.h:125
Define extrapolation predictor
#define AERROR
Definition log.h:44
class register implement
Definition arena_queue.h:37
repeated Trajectory predicted_trajectory
Defines the Vec2d class.