Apollo 11.0
自动驾驶开放平台
lane_sequence_predictor.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
18
19#include <memory>
20#include <string>
21#include <utility>
22
25#include "modules/prediction/proto/prediction_conf.pb.h"
26
27namespace apollo {
28namespace prediction {
29
30using common::PathPoint;
31using common::TrajectoryPoint;
32using hdmap::LaneInfo;
33
37
39 const ADCTrajectoryContainer* adc_trajectory_container, Obstacle* obstacle,
40 ObstaclesContainer* obstacles_container) {
41 Clear();
42
43 CHECK_NOTNULL(obstacle);
44 CHECK_GT(obstacle->history_size(), 0U);
45
47
48 const Feature& feature = obstacle->latest_feature();
49
50 if (!feature.has_lane() || !feature.lane().has_lane_graph()) {
51 AERROR << "Obstacle [" << obstacle->id() << " has no lane graph.";
52 return false;
53 }
54
55 std::string lane_id = "";
56 if (feature.lane().has_lane_feature()) {
57 lane_id = feature.lane().lane_feature().lane_id();
58 }
59 int num_lane_sequence = feature.lane().lane_graph().lane_sequence_size();
60 std::vector<bool> enable_lane_sequence(num_lane_sequence, true);
61 Obstacle* ego_vehicle_ptr =
62 obstacles_container->GetObstacle(FLAGS_ego_vehicle_id);
63 FilterLaneSequences(feature, lane_id, ego_vehicle_ptr,
64 adc_trajectory_container, &enable_lane_sequence);
65
66 for (int i = 0; i < num_lane_sequence; ++i) {
67 const LaneSequence& sequence = feature.lane().lane_graph().lane_sequence(i);
68 if (sequence.lane_segment().empty()) {
69 AERROR << "Empty lane segments.";
70 continue;
71 }
72
73 if (!enable_lane_sequence[i]) {
74 ADEBUG << "Lane sequence [" << ToString(sequence)
75 << "] with probability [" << sequence.probability()
76 << "] is disqualified.";
77 continue;
78 }
79
80 ADEBUG << "Obstacle [" << obstacle->id()
81 << "] will draw a lane sequence trajectory [" << ToString(sequence)
82 << "] with probability [" << sequence.probability() << "].";
83
84 std::vector<TrajectoryPoint> points;
85 bool is_about_to_stop = false;
86 double acceleration = 0.0;
87 if (sequence.has_stop_sign()) {
88 double stop_distance =
89 sequence.stop_sign().lane_sequence_s() - sequence.lane_s();
90 is_about_to_stop = SupposedToStop(feature, stop_distance, &acceleration);
91 }
92
93 if (is_about_to_stop) {
95 *obstacle, sequence, FLAGS_prediction_trajectory_time_length,
96 FLAGS_prediction_trajectory_time_resolution, acceleration, &points);
97 } else {
99 *obstacle, sequence, FLAGS_prediction_trajectory_time_length,
100 FLAGS_prediction_trajectory_time_resolution, &points);
101 }
102
103 if (points.empty()) {
104 continue;
105 }
106
107 if (FLAGS_enable_trajectory_validation_check &&
109 continue;
110 }
111
112 Trajectory trajectory = GenerateTrajectory(points);
113 trajectory.set_probability(sequence.probability());
114 obstacle->mutable_latest_feature()->add_predicted_trajectory()->CopyFrom(
115 trajectory);
116 }
117 return true;
118}
119
121 const Obstacle& obstacle, const LaneSequence& lane_sequence,
122 const double total_time, const double period,
123 std::vector<TrajectoryPoint>* points) {
124 const Feature& feature = obstacle.latest_feature();
125 if (!feature.has_position() || !feature.has_velocity() ||
126 !feature.position().has_x() || !feature.position().has_y()) {
127 AERROR << "Obstacle [" << obstacle.id()
128 << " is missing position or velocity";
129 return;
130 }
131
132 Eigen::Vector2d position(feature.position().x(), feature.position().y());
133 double speed = feature.speed();
134
135 int lane_segment_index = 0;
136 std::string lane_id =
137 lane_sequence.lane_segment(lane_segment_index).lane_id();
138 std::shared_ptr<const LaneInfo> lane_info = PredictionMap::LaneById(lane_id);
139 double lane_s = 0.0;
140 double lane_l = 0.0;
141 if (!PredictionMap::GetProjection(position, lane_info, &lane_s, &lane_l)) {
142 AERROR << "Failed in getting lane s and lane l";
143 return;
144 }
145 double approach_rate = FLAGS_go_approach_rate;
146 if (!lane_sequence.vehicle_on_lane()) {
147 approach_rate = FLAGS_cutin_approach_rate;
148 }
149 size_t total_num = static_cast<size_t>(total_time / period);
150 for (size_t i = 0; i < total_num; ++i) {
151 double relative_time = static_cast<double>(i) * period;
152 Eigen::Vector2d point;
153 double theta = M_PI;
154 if (!PredictionMap::SmoothPointFromLane(lane_id, lane_s, lane_l, &point,
155 &theta)) {
156 AERROR << "Unable to get smooth point from lane [" << lane_id
157 << "] with s [" << lane_s << "] and l [" << lane_l << "]";
158 break;
159 }
160 TrajectoryPoint trajectory_point;
161 PathPoint path_point;
162 path_point.set_x(point.x());
163 path_point.set_y(point.y());
164 path_point.set_z(0.0);
165 path_point.set_theta(theta);
166 path_point.set_lane_id(lane_id);
167 trajectory_point.mutable_path_point()->CopyFrom(path_point);
168 trajectory_point.set_v(speed);
169 trajectory_point.set_a(0.0);
170 trajectory_point.set_relative_time(relative_time);
171 points->emplace_back(std::move(trajectory_point));
172
173 lane_s += speed * period;
174
175 while (lane_s > PredictionMap::LaneById(lane_id)->total_length() &&
176 lane_segment_index + 1 < lane_sequence.lane_segment_size()) {
177 lane_segment_index += 1;
178 lane_s = lane_s - PredictionMap::LaneById(lane_id)->total_length();
179 lane_id = lane_sequence.lane_segment(lane_segment_index).lane_id();
180 }
181
182 lane_l *= approach_rate;
183 }
184}
185
186} // namespace prediction
187} // namespace apollo
bool Predict(const ADCTrajectoryContainer *adc_trajectory_container, Obstacle *obstacle, ObstaclesContainer *obstacles_container) override
Make prediction
void DrawLaneSequenceTrajectoryPoints(const Obstacle &obstacle, const LaneSequence &lane_sequence, const double total_time, const double period, std::vector< apollo::common::TrajectoryPoint > *points)
Draw lane sequence trajectory points
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
const Feature & latest_feature() const
Get the latest feature.
Definition obstacle.cc:78
Obstacle * GetObstacle(const int id)
Get obstacle pointer
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 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.
static Trajectory GenerateTrajectory(const std::vector< apollo::common::TrajectoryPoint > &points)
Generate trajectory from trajectory points
Definition predictor.cc:34
ObstacleConf::PredictorType predictor_type_
Definition predictor.h:125
bool SupposedToStop(const Feature &feature, const double stop_distance, double *acceleration)
Determine if an obstacle is supposed to stop within a distance
Definition predictor.cc:125
void FilterLaneSequences(const Feature &feature, const std::string &lane_id, const Obstacle *ego_vehicle_ptr, const ADCTrajectoryContainer *adc_trajectory_container, std::vector< bool > *enable_lane_sequence)
Filter lane sequences
void DrawConstantAccelerationTrajectory(const Obstacle &obstacle, const LaneSequence &lane_sequence, const double total_time, const double period, const double acceleration, std::vector< apollo::common::TrajectoryPoint > *points)
Draw constant acceleration trajectory points
std::string ToString(const LaneSequence &sequence)
Convert a lane sequence to string
static bool ValidCentripetalAcceleration(const std::vector< common::TrajectoryPoint > &discretized_trajectory)
Check the validity of trajectory's centripetal acceleration
Define lane sequence predictor
#define ADEBUG
Definition log.h:41
#define AERROR
Definition log.h:44
class register implement
Definition arena_queue.h:37
optional apollo::common::Point3D position
Definition feature.proto:88
repeated LaneSegment lane_segment