Apollo 10.0
自动驾驶开放平台
obstacles_container.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 <iomanip>
20#include <utility>
21
28
29namespace apollo {
30namespace prediction {
31
34
36 : ptr_obstacles_(FLAGS_max_num_obstacles),
37 clusters_(new ObstacleClusters()) {}
38
40 : ptr_obstacles_(FLAGS_max_num_obstacles),
41 clusters_(new ObstacleClusters()) {
42 for (const Obstacle& obstacle : submodule_output.curr_frame_obstacles()) {
43 // Deep copy of obstacle is needed for modification
44 std::unique_ptr<Obstacle> ptr_obstacle(new Obstacle(obstacle));
45 ptr_obstacle->SetJunctionAnalyzer(&junction_analyzer_);
46 ptr_obstacles_.Put(obstacle.id(), std::move(ptr_obstacle));
47 }
48
49 Obstacle ego_vehicle = submodule_output.GetEgoVehicle();
50 std::unique_ptr<Obstacle> ptr_ego_vehicle(
51 new Obstacle(std::move(ego_vehicle)));
52 ptr_ego_vehicle->SetJunctionAnalyzer(&junction_analyzer_);
53 ptr_obstacles_.Put(ego_vehicle.id(), std::move(ptr_ego_vehicle));
54
55 curr_frame_movable_obstacle_ids_ =
56 submodule_output.curr_frame_movable_obstacle_ids();
57 curr_frame_unmovable_obstacle_ids_ =
58 submodule_output.curr_frame_unmovable_obstacle_ids();
59 curr_frame_considered_obstacle_ids_ =
60 submodule_output.curr_frame_considered_obstacle_ids();
61 curr_scenario_ = submodule_output.curr_scenario();
62}
63
65 // Clean up the history and get the PerceptionObstacles
66 curr_frame_movable_obstacle_ids_.clear();
67 curr_frame_unmovable_obstacle_ids_.clear();
68 curr_frame_considered_obstacle_ids_.clear();
69}
70
71// This is called by Perception module at every frame to insert all
72// detected obstacles.
73void ObstaclesContainer::Insert(const ::google::protobuf::Message& message) {
74 PerceptionObstacles perception_obstacles;
75 perception_obstacles.CopyFrom(
76 dynamic_cast<const PerceptionObstacles&>(message));
77
78 // Get the new timestamp and update it in the class
79 // - If it's more than 10sec later than the most recent one, clear the
80 // obstacle history.
81 // - If it's not a valid time (earlier than history), continue.
82 // - Also consider the offline_mode case.
83 double timestamp = 0.0;
84 if (perception_obstacles.has_header() &&
85 perception_obstacles.header().has_timestamp_sec()) {
86 timestamp = perception_obstacles.header().timestamp_sec();
87 }
88 if (std::fabs(timestamp - timestamp_) > FLAGS_replay_timestamp_gap) {
89 ptr_obstacles_.Clear();
90 ADEBUG << "Replay mode is enabled.";
91 } else if (timestamp <= timestamp_) {
92 AERROR << "Invalid timestamp curr [" << timestamp << "] v.s. prev ["
93 << timestamp_ << "].";
94 return;
95 }
96
97 switch (FLAGS_prediction_offline_mode) {
98 case 1: {
99 if (std::fabs(timestamp - timestamp_) > FLAGS_replay_timestamp_gap ||
100 FeatureOutput::Size() > FLAGS_max_num_dump_feature) {
102 }
103 break;
104 }
105 case 2: {
106 if (std::fabs(timestamp - timestamp_) > FLAGS_replay_timestamp_gap ||
108 FLAGS_max_num_dump_dataforlearn) {
110 }
111 break;
112 }
113 case 3: {
114 if (std::fabs(timestamp - timestamp_) > FLAGS_replay_timestamp_gap ||
116 FLAGS_max_num_dump_feature) {
118 }
119 break;
120 }
121 case 4: {
122 if (std::fabs(timestamp - timestamp_) > FLAGS_replay_timestamp_gap ||
123 FeatureOutput::SizeOfFrameEnv() > FLAGS_max_num_dump_feature) {
125 }
126 break;
127 }
128 case 5: {
129 if (std::fabs(timestamp - timestamp_) > FLAGS_replay_timestamp_gap ||
130 FeatureOutput::SizeOfFrameEnv() > FLAGS_max_num_dump_feature) {
132 }
133 break;
134 }
135 default: {
136 // No data dump
138 break;
139 }
140 }
141
142 timestamp_ = timestamp;
143 ADEBUG << "Current timestamp is [" << std::fixed << std::setprecision(6)
144 << timestamp_ << "]";
145
146 // Set up the ObstacleClusters:
147 // Insert the Obstacles one by one
148 for (const PerceptionObstacle& perception_obstacle :
149 perception_obstacles.perception_obstacle()) {
150 ADEBUG << "Perception obstacle [" << perception_obstacle.id() << "] "
151 << "was detected";
152 InsertPerceptionObstacle(perception_obstacle, timestamp_);
153 ADEBUG << "Perception obstacle [" << perception_obstacle.id() << "] "
154 << "was inserted";
155 }
156
158 clusters_->SortObstacles();
159}
160
162 auto ptr_obstacle = ptr_obstacles_.GetSilently(id);
163 if (ptr_obstacle != nullptr) {
164 return ptr_obstacle->get();
165 }
166 return nullptr;
167}
168
169Obstacle* ObstaclesContainer::GetObstacleWithLRUUpdate(const int obstacle_id) {
170 auto ptr_obstacle = ptr_obstacles_.Get(obstacle_id);
171 if (ptr_obstacle != nullptr) {
172 return ptr_obstacle->get();
173 }
174 return nullptr;
175}
176
178 ptr_obstacles_.Clear();
179 timestamp_ = -1.0;
180}
181
183 return curr_frame_movable_obstacle_ids_;
184}
185
186const std::vector<int>&
188 return curr_frame_unmovable_obstacle_ids_;
189}
190
191const std::vector<int>&
193 return curr_frame_considered_obstacle_ids_;
194}
195
197 curr_frame_considered_obstacle_ids_.clear();
198 for (const int id : curr_frame_movable_obstacle_ids_) {
199 Obstacle* obstacle_ptr = GetObstacle(id);
200 if (obstacle_ptr == nullptr) {
201 AERROR << "Null obstacle found.";
202 continue;
203 }
204 if (obstacle_ptr->ToIgnore()) {
205 ADEBUG << "Ignore obstacle [" << obstacle_ptr->id() << "]";
206 continue;
207 }
208 curr_frame_considered_obstacle_ids_.push_back(id);
209 }
210}
211
213 std::vector<int> curr_frame_obs_ids = curr_frame_movable_obstacle_ids_;
214 curr_frame_obs_ids.insert(curr_frame_obs_ids.end(),
215 curr_frame_unmovable_obstacle_ids_.begin(),
216 curr_frame_unmovable_obstacle_ids_.end());
217 return curr_frame_obs_ids;
218}
219
221 const PerceptionObstacle& perception_obstacle, const double timestamp) {
222 // Sanity checks.
223 int id = perception_obstacle.id();
224 if (id < FLAGS_ego_vehicle_id) {
225 AERROR << "Invalid ID [" << id << "]";
226 return;
227 }
228 if (!IsMovable(perception_obstacle)) {
229 ADEBUG << "Perception obstacle [" << perception_obstacle.id()
230 << "] is unmovable.";
231 curr_frame_unmovable_obstacle_ids_.push_back(id);
232 return;
233 }
234
235 // Insert the obstacle and also update the LRUCache.
236 auto obstacle_ptr = GetObstacleWithLRUUpdate(id);
237 if (obstacle_ptr != nullptr) {
238 ADEBUG << "Current time = " << std::fixed << std::setprecision(6)
239 << timestamp;
240 obstacle_ptr->Insert(perception_obstacle, timestamp, id);
241 ADEBUG << "Refresh obstacle [" << id << "]";
242 } else {
243 auto ptr_obstacle =
244 Obstacle::Create(perception_obstacle, timestamp, id, clusters_.get());
245 if (ptr_obstacle == nullptr) {
246 AERROR << "Failed to insert obstacle into container";
247 return;
248 }
249 ptr_obstacle->SetJunctionAnalyzer(&junction_analyzer_);
250 ptr_obstacles_.Put(id, std::move(ptr_obstacle));
251 ADEBUG << "Insert obstacle [" << id << "]";
252 }
253
254 if (FLAGS_prediction_offline_mode ==
256 id != FLAGS_ego_vehicle_id) {
257 curr_frame_movable_obstacle_ids_.push_back(id);
258 }
259}
260
262 if (!feature.has_id()) {
263 AERROR << "Invalid feature, no ID found.";
264 return;
265 }
266 int id = feature.id();
267 auto obstacle_ptr = GetObstacleWithLRUUpdate(id);
268 if (obstacle_ptr != nullptr) {
269 obstacle_ptr->InsertFeature(feature);
270 } else {
271 auto ptr_obstacle = Obstacle::Create(feature, clusters_.get());
272 if (ptr_obstacle == nullptr) {
273 AERROR << "Failed to insert obstacle into container";
274 return;
275 }
276 ptr_obstacle->SetJunctionAnalyzer(&junction_analyzer_);
277 ptr_obstacles_.Put(id, std::move(ptr_obstacle));
278 }
279}
280
282 // Go through every obstacle in the current frame, after some
283 // sanity checks, build lane graph for non-junction cases.
284 for (const int id : curr_frame_considered_obstacle_ids_) {
285 Obstacle* obstacle_ptr = GetObstacle(id);
286 if (obstacle_ptr == nullptr) {
287 AERROR << "Null obstacle found.";
288 continue;
289 }
290 if (FLAGS_prediction_offline_mode !=
292 ADEBUG << "Building Lane Graph.";
293 obstacle_ptr->BuildLaneGraph();
294 obstacle_ptr->BuildLaneGraphFromLeftToRight();
295 } else {
296 ADEBUG << "Building ordered Lane Graph.";
297 ADEBUG << "Building lane graph for id = " << id;
298 obstacle_ptr->BuildLaneGraphFromLeftToRight();
299 }
300 obstacle_ptr->SetNearbyObstacles();
301 }
302
303 Obstacle* ego_vehicle_ptr = GetObstacle(FLAGS_ego_vehicle_id);
304 if (ego_vehicle_ptr == nullptr) {
305 AERROR << "Ego vehicle not inserted";
306 return;
307 }
308 ego_vehicle_ptr->BuildLaneGraph();
309 ego_vehicle_ptr->SetNearbyObstacles();
310}
311
313 // Go through every obstacle in the current frame, after some
314 // sanity checks, build junction features for those that are in junction.
315 for (const int id : curr_frame_considered_obstacle_ids_) {
316 Obstacle* obstacle_ptr = GetObstacle(id);
317 if (obstacle_ptr == nullptr) {
318 AERROR << "Null obstacle found.";
319 continue;
320 }
321 const std::string& junction_id = junction_analyzer_.GetJunctionId();
322 if (obstacle_ptr->IsInJunction(junction_id)) {
323 AINFO << "Build junction feature for obstacle [" << obstacle_ptr->id()
324 << "] in junction [" << junction_id << "]";
325 obstacle_ptr->BuildJunctionFeature();
326 }
327 }
328}
329
330bool ObstaclesContainer::IsMovable(
331 const PerceptionObstacle& perception_obstacle) {
332 if (!perception_obstacle.has_type() ||
333 perception_obstacle.type() == PerceptionObstacle::UNKNOWN_UNMOVABLE) {
334 return false;
335 }
336 return true;
337}
338
339double ObstaclesContainer::timestamp() const { return timestamp_; }
340
342 const size_t history_size, const apollo::cyber::Time& frame_start_time) {
343 SubmoduleOutput container_output;
344 for (int id : curr_frame_considered_obstacle_ids_) {
345 Obstacle* obstacle = GetObstacle(id);
346 if (obstacle == nullptr) {
347 AERROR << "Nullptr found for obstacle [" << id << "]";
348 continue;
349 }
350 obstacle->TrimHistory(history_size);
351 obstacle->ClearOldInformation();
352 container_output.InsertObstacle(std::move(*obstacle));
353 }
354
355 Obstacle* ego_obstacle = GetObstacle(FLAGS_ego_vehicle_id);
356 if (ego_obstacle != nullptr) {
357 container_output.InsertEgoVehicle(std::move(*ego_obstacle));
358 }
359
361 curr_frame_movable_obstacle_ids_);
363 curr_frame_unmovable_obstacle_ids_);
365 curr_frame_considered_obstacle_ids_);
366 container_output.set_frame_start_time(frame_start_time);
367
368 return container_output;
369}
370
372 return curr_scenario_;
373}
374
376 return clusters_.get();
377}
379 return &junction_analyzer_;
380}
381
382} // namespace prediction
383} // namespace apollo
Cyber has builtin time type Time.
Definition time.h:31
static void WriteDataForLearning()
Write DataForLearning features to a file
static int SizeOfPredictionResult()
Get the size of prediction results.
static int Size()
Get feature size
static int SizeOfFrameEnv()
Get the size of frame env.
static void WriteDataForTuning()
Write DataForTuning features to a file
static int SizeOfDataForLearning()
Get the size of data_for_learning features.
static void WriteFrameEnv()
Write frame env to a file
static void WriteFeatureProto()
Write features to a file
static void WritePredictionResult()
Write PredictionResult to a file
const std::string & GetJunctionId()
Get junction ID
Prediction obstacle.
Definition obstacle.h:52
bool IsInJunction(const std::string &junction_id) const
Check if the obstacle is a junction.
Definition obstacle.cc:223
void SetNearbyObstacles()
Set nearby obstacles.
Definition obstacle.cc:1279
void BuildLaneGraphFromLeftToRight()
Build obstacle's lane graph with lanes being ordered.
Definition obstacle.cc:981
void BuildLaneGraph()
Build obstacle's lane graph
Definition obstacle.cc:804
void TrimHistory(const size_t remain_size)
Definition obstacle.cc:217
int id() const
Get the obstacle's ID.
Definition obstacle.cc:60
bool ToIgnore()
Check if the obstacle can be ignored.
Definition obstacle.cc:123
void BuildJunctionFeature()
Build junction feature.
Definition obstacle.cc:242
static std::unique_ptr< Obstacle > Create(const perception::PerceptionObstacle &perception_obstacle, const double timestamp, const int prediction_id, ObstacleClusters *clusters_ptr)
Constructor
Definition obstacle.cc:1406
SubmoduleOutput GetSubmoduleOutput(const size_t history_size, const apollo::cyber::Time &frame_start_time)
ObstacleClusters * GetClustersPtr() const
Get the raw pointer of clusters_
const std::vector< int > & curr_frame_movable_obstacle_ids()
Get movable obstacle IDs in the current frame
void Insert(const ::google::protobuf::Message &message) override
Insert a data message into the container
const std::vector< int > & curr_frame_unmovable_obstacle_ids()
Get unmovable obstacle IDs in the current frame
const std::vector< int > & curr_frame_considered_obstacle_ids()
Get non-ignore obstacle IDs in the current frame
const Scenario & curr_scenario() const
Get current scenario
void BuildLaneGraph()
Build lane graph for obstacles
Obstacle * GetObstacle(const int id)
Get obstacle pointer
std::vector< int > curr_frame_obstacle_ids()
Get current frame obstacle IDs in the current frame
void BuildJunctionFeature()
Build junction feature for obstacles
void InsertFeatureProto(const Feature &feature)
Insert a feature proto message into the container
void InsertPerceptionObstacle(const perception::PerceptionObstacle &perception_obstacle, const double timestamp)
Insert an perception obstacle
const std::vector< Obstacle > & curr_frame_obstacles() const
void set_curr_frame_movable_obstacle_ids(const std::vector< int > &curr_frame_movable_obstacle_ids)
void InsertEgoVehicle(const Obstacle &&ego_vehicle)
void set_curr_frame_unmovable_obstacle_ids(const std::vector< int > &curr_frame_unmovable_obstacle_ids)
void InsertObstacle(const Obstacle &&obstacle)
const Scenario & curr_scenario() const
void set_curr_frame_considered_obstacle_ids(const std::vector< int > &curr_frame_considered_obstacle_ids)
std::vector< int > curr_frame_considered_obstacle_ids() const
void set_frame_start_time(const apollo::cyber::Time &frame_start_time)
std::vector< int > curr_frame_movable_obstacle_ids() const
const Obstacle & GetEgoVehicle() const
std::vector< int > curr_frame_unmovable_obstacle_ids() const
#define ADEBUG
Definition log.h:41
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
class register implement
Definition arena_queue.h:37
Obstacles container
Holds all global constants for the prediction module.
optional double timestamp_sec
Definition header.proto:9