Apollo 11.0
自动驾驶开放平台
single_lane_predictor.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 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
13 *implied. See the License for the specific language governing
14 *permissions and limitations under the License.
15 *****************************************************************************/
16
18
19#include <memory>
20#include <string>
21#include <utility>
22
24
25namespace apollo {
26namespace prediction {
27
31
35
37 const ADCTrajectoryContainer* adc_trajectory_container, Obstacle* obstacle,
38 ObstaclesContainer* obstacles_container) {
39 Clear();
40
41 CHECK_NOTNULL(obstacle);
42 CHECK_GT(obstacle->history_size(), 0U);
43
45
46 const Feature& feature = obstacle->latest_feature();
47
48 if (!feature.has_lane() || !feature.lane().has_lane_graph()) {
49 AERROR << "Obstacle [" << obstacle->id() << "] has no lane graph.";
50 return false;
51 }
52
53 std::string lane_id = "";
54 if (feature.lane().has_lane_feature()) {
55 lane_id = feature.lane().lane_feature().lane_id();
56 }
57 int num_lane_sequence = feature.lane().lane_graph().lane_sequence_size();
58
59 for (int i = 0; i < num_lane_sequence; ++i) {
60 const LaneSequence& sequence = feature.lane().lane_graph().lane_sequence(i);
61 if (sequence.lane_segment().empty()) {
62 AERROR << "Empty lane segments.";
63 continue;
64 }
65
66 ADEBUG << "Obstacle [" << obstacle->id()
67 << "] will draw a lane sequence trajectory [" << ToString(sequence)
68 << "] with probability [" << sequence.probability() << "].";
69
70 std::vector<TrajectoryPoint> points;
72 *obstacle, sequence, FLAGS_prediction_trajectory_time_length,
73 FLAGS_prediction_trajectory_time_resolution, &points);
74
75 if (points.empty()) {
76 continue;
77 }
78
79 Trajectory trajectory = GenerateTrajectory(points);
80 trajectory.set_probability(sequence.probability());
81 obstacle->mutable_latest_feature()->add_predicted_trajectory()->CopyFrom(
82 trajectory);
83 }
84 return true;
85}
86
88 const Obstacle& obstacle, const LaneSequence& lane_sequence,
89 const double time_length, const double time_resolution,
90 std::vector<TrajectoryPoint>* points) {
91 const Feature& feature = obstacle.latest_feature();
92 if (!feature.has_position() || !feature.has_velocity() ||
93 !feature.position().has_x() || !feature.position().has_y()) {
94 AERROR << "Obstacle [" << obstacle.id()
95 << " is missing position or velocity";
96 return;
97 }
98
99 double probability = lane_sequence.probability();
100 double approach_rate = FLAGS_go_approach_rate;
101 if (probability < FLAGS_lane_sequence_threshold_cruise) {
102 approach_rate = 1.0;
103 }
104
105 Eigen::Vector2d position(feature.position().x(), feature.position().y());
106 double speed = feature.speed();
107
108 int lane_segment_index = 0;
109 std::string lane_id =
110 lane_sequence.lane_segment(lane_segment_index).lane_id();
111 std::shared_ptr<const LaneInfo> lane_info = PredictionMap::LaneById(lane_id);
112 double lane_s = 0.0;
113 double lane_l = 0.0;
114 if (!PredictionMap::GetProjection(position, lane_info, &lane_s, &lane_l)) {
115 AERROR << "Failed in getting lane s and lane l";
116 return;
117 }
118 size_t num_of_points = static_cast<size_t>(time_length / time_resolution);
119 for (size_t i = 0; i < num_of_points; ++i) {
120 double relative_time = static_cast<double>(i) * time_resolution;
121 Eigen::Vector2d point;
122 double theta = M_PI;
123 if (!PredictionMap::SmoothPointFromLane(lane_id, lane_s, lane_l, &point,
124 &theta)) {
125 AERROR << "Unable to get smooth point from lane [" << lane_id
126 << "] with s [" << lane_s << "] and l [" << lane_l << "]";
127 break;
128 }
129 TrajectoryPoint trajectory_point;
130 PathPoint path_point;
131 path_point.set_x(point.x());
132 path_point.set_y(point.y());
133 path_point.set_z(0.0);
134 path_point.set_theta(theta);
135 path_point.set_lane_id(lane_id);
136 trajectory_point.mutable_path_point()->CopyFrom(path_point);
137 trajectory_point.set_v(speed);
138 trajectory_point.set_a(0.0);
139 trajectory_point.set_relative_time(relative_time);
140 points->emplace_back(std::move(trajectory_point));
141
142 lane_s += speed * time_resolution;
143
144 while (lane_s > PredictionMap::LaneById(lane_id)->total_length() &&
145 lane_segment_index + 1 < lane_sequence.lane_segment_size()) {
146 lane_segment_index += 1;
147 lane_s = lane_s - PredictionMap::LaneById(lane_id)->total_length();
148 lane_id = lane_sequence.lane_segment(lane_segment_index).lane_id();
149 }
150
151 lane_l *= approach_rate;
152 }
153}
154
155} // namespace prediction
156} // namespace apollo
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
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
std::string ToString(const LaneSequence &sequence)
Convert a lane sequence to string
void GenerateTrajectoryPoints(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
bool Predict(const ADCTrajectoryContainer *adc_trajectory_container, Obstacle *obstacle, ObstaclesContainer *obstacles_container) override
Make prediction
#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