Apollo 10.0
自动驾驶开放平台
prediction_component.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 implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
18
19#include "cyber/common/file.h"
21#include "cyber/time/clock.h"
24
32#include "modules/prediction/proto/offline_features.pb.h"
33#include "modules/prediction/proto/prediction_conf.pb.h"
36
37namespace apollo {
38namespace prediction {
39
45
47
48std::string PredictionComponent::Name() const {
49 return FLAGS_prediction_module_name;
50}
51
53 const std::string& features_proto_file_name) {
54 auto obstacles_container_ptr =
55 container_manager_->GetContainer<ObstaclesContainer>(
57 obstacles_container_ptr->Clear();
58 Features features;
59 apollo::cyber::common::GetProtoFromBinaryFile(features_proto_file_name,
60 &features);
61 for (const Feature& feature : features.feature()) {
62 obstacles_container_ptr->InsertFeatureProto(feature);
63 Obstacle* obstacle_ptr = obstacles_container_ptr->GetObstacle(feature.id());
64 if (!obstacle_ptr) {
65 continue;
66 }
67 evaluator_manager_->EvaluateObstacle(obstacle_ptr, obstacles_container_ptr);
68 }
69}
70
72 component_start_time_ = Clock::NowInSeconds();
73
74 container_manager_ = std::make_shared<ContainerManager>();
75 evaluator_manager_.reset(new EvaluatorManager());
76 predictor_manager_.reset(new PredictorManager());
77 scenario_manager_.reset(new ScenarioManager());
78
79 PredictionConf prediction_conf;
80 if (!ComponentBase::GetProtoConfig(&prediction_conf)) {
81 AERROR << "Unable to load prediction conf file: "
82 << ComponentBase::ConfigFilePath();
83 return false;
84 }
85 ADEBUG << "Prediction config file is loaded into: "
86 << prediction_conf.ShortDebugString();
87
88 if (!MessageProcess::Init(container_manager_.get(), evaluator_manager_.get(),
89 predictor_manager_.get(), prediction_conf)) {
90 return false;
91 }
92
93 planning_reader_ = node_->CreateReader<ADCTrajectory>(
94 prediction_conf.topic_conf().planning_trajectory_topic(), nullptr);
95
96 localization_reader_ =
98 prediction_conf.topic_conf().localization_topic(), nullptr);
99
100 storytelling_reader_ = node_->CreateReader<storytelling::Stories>(
101 prediction_conf.topic_conf().storytelling_topic(), nullptr);
102
103 prediction_writer_ = node_->CreateWriter<PredictionObstacles>(
104 prediction_conf.topic_conf().prediction_topic());
105
106 container_writer_ = node_->CreateWriter<SubmoduleOutput>(
107 prediction_conf.topic_conf().container_topic_name());
108
109 adc_container_writer_ = node_->CreateWriter<ADCTrajectoryContainer>(
110 prediction_conf.topic_conf().adccontainer_topic_name());
111
112 perception_obstacles_writer_ = node_->CreateWriter<PerceptionObstacles>(
113 prediction_conf.topic_conf().perception_obstacles_topic_name());
114
115 return true;
116}
117
119 const std::shared_ptr<PerceptionObstacles>& perception_obstacles) {
120 if (FLAGS_use_lego) {
121 return ContainerSubmoduleProcess(perception_obstacles);
122 }
123 return PredictionEndToEndProc(perception_obstacles);
124}
125
126bool PredictionComponent::ContainerSubmoduleProcess(
127 const std::shared_ptr<PerceptionObstacles>& perception_obstacles) {
128 constexpr static size_t kHistorySize = 10;
129 const auto frame_start_time = Clock::Now();
130 // Read localization info. and call OnLocalization to update
131 // the PoseContainer.
132 localization_reader_->Observe();
133 auto ptr_localization_msg = localization_reader_->GetLatestObserved();
134 if (ptr_localization_msg == nullptr) {
135 AERROR << "Prediction: cannot receive any localization message.";
136 return false;
137 }
138 MessageProcess::OnLocalization(container_manager_.get(),
139 *ptr_localization_msg);
140
141 // Read planning info. of last frame and call OnPlanning to update
142 // the ADCTrajectoryContainer
143 planning_reader_->Observe();
144 auto ptr_trajectory_msg = planning_reader_->GetLatestObserved();
145 if (ptr_trajectory_msg != nullptr) {
146 MessageProcess::OnPlanning(container_manager_.get(), *ptr_trajectory_msg);
147 }
148
149 // Read storytelling message and call OnStorytelling to update the
150 // StoryTellingContainer
151 storytelling_reader_->Observe();
152 auto ptr_storytelling_msg = storytelling_reader_->GetLatestObserved();
153 if (ptr_storytelling_msg != nullptr) {
154 MessageProcess::OnStoryTelling(container_manager_.get(),
155 *ptr_storytelling_msg);
156 }
157
158 MessageProcess::ContainerProcess(container_manager_, *perception_obstacles,
159 scenario_manager_.get());
160
161 auto obstacles_container_ptr =
162 container_manager_->GetContainer<ObstaclesContainer>(
164 CHECK_NOTNULL(obstacles_container_ptr);
165
166 auto adc_trajectory_container_ptr =
167 container_manager_->GetContainer<ADCTrajectoryContainer>(
169 CHECK_NOTNULL(adc_trajectory_container_ptr);
170
171 SubmoduleOutput submodule_output =
172 obstacles_container_ptr->GetSubmoduleOutput(kHistorySize,
173 frame_start_time);
174 submodule_output.set_curr_scenario(scenario_manager_->scenario());
175 container_writer_->Write(submodule_output);
176 adc_container_writer_->Write(*adc_trajectory_container_ptr);
177 perception_obstacles_writer_->Write(*perception_obstacles);
178 return true;
179}
180
181bool PredictionComponent::PredictionEndToEndProc(
182 const std::shared_ptr<PerceptionObstacles>& perception_obstacles) {
183 if (FLAGS_prediction_test_mode &&
184 (Clock::NowInSeconds() - component_start_time_ >
185 FLAGS_prediction_test_duration)) {
186 ADEBUG << "Prediction finished running in test mode";
187 }
188
189 // Update relative map if needed
190 if (FLAGS_use_navigation_mode && !PredictionMap::Ready()) {
191 AERROR << "Relative map is empty.";
192 return false;
193 }
194
195 frame_start_time_ = Clock::NowInSeconds();
196 auto end_time1 = std::chrono::system_clock::now();
197
198 // Read localization info. and call OnLocalization to update
199 // the PoseContainer.
200 localization_reader_->Observe();
201 auto ptr_localization_msg = localization_reader_->GetLatestObserved();
202 if (ptr_localization_msg == nullptr) {
203 AERROR << "Prediction: cannot receive any localization message.";
204 return false;
205 }
206 MessageProcess::OnLocalization(container_manager_.get(),
207 *ptr_localization_msg);
208 auto end_time2 = std::chrono::system_clock::now();
209 std::chrono::duration<double> diff = end_time2 - end_time1;
210 ADEBUG << "Time for updating PoseContainer: " << diff.count() * 1000
211 << " msec.";
212
213 // Read storytelling message and call OnStorytelling to update the
214 // StoryTellingContainer
215 storytelling_reader_->Observe();
216 auto ptr_storytelling_msg = storytelling_reader_->GetLatestObserved();
217 if (ptr_storytelling_msg != nullptr) {
218 MessageProcess::OnStoryTelling(container_manager_.get(),
219 *ptr_storytelling_msg);
220 }
221
222 // Read planning info. of last frame and call OnPlanning to update
223 // the ADCTrajectoryContainer
224 planning_reader_->Observe();
225 auto ptr_trajectory_msg = planning_reader_->GetLatestObserved();
226 if (ptr_trajectory_msg != nullptr) {
227 MessageProcess::OnPlanning(container_manager_.get(), *ptr_trajectory_msg);
228 }
229 auto end_time3 = std::chrono::system_clock::now();
230 diff = end_time3 - end_time2;
231 ADEBUG << "Time for updating ADCTrajectoryContainer: " << diff.count() * 1000
232 << " msec.";
233
234 // Get all perception_obstacles of this frame and call OnPerception to
235 // process them all.
236 auto perception_msg = *perception_obstacles;
237 PredictionObstacles prediction_obstacles;
239 perception_msg, container_manager_, evaluator_manager_.get(),
240 predictor_manager_.get(), scenario_manager_.get(), &prediction_obstacles);
241 auto end_time4 = std::chrono::system_clock::now();
242 diff = end_time4 - end_time3;
243 ADEBUG << "Time for updating PerceptionContainer: " << diff.count() * 1000
244 << " msec.";
245
246 // Postprocess prediction obstacles message
247 prediction_obstacles.set_start_timestamp(frame_start_time_);
248 prediction_obstacles.set_end_timestamp(Clock::NowInSeconds());
249 prediction_obstacles.mutable_header()->set_lidar_timestamp(
250 perception_msg.header().lidar_timestamp());
251 prediction_obstacles.mutable_header()->set_camera_timestamp(
252 perception_msg.header().camera_timestamp());
253 prediction_obstacles.mutable_header()->set_radar_timestamp(
254 perception_msg.header().radar_timestamp());
255
256 for (const PerceptionWaste& perception_waste :
257 perception_msg.perception_waste()) {
258 prediction_obstacles.add_perception_waste()->CopyFrom(
259 perception_waste);
260 }
261
262 prediction_obstacles.set_perception_error_code(perception_msg.error_code());
263
264 if (FLAGS_prediction_test_mode) {
265 for (auto const& prediction_obstacle :
266 prediction_obstacles.prediction_obstacle()) {
267 for (auto const& trajectory : prediction_obstacle.trajectory()) {
268 for (auto const& trajectory_point : trajectory.trajectory_point()) {
269 if (!ValidationChecker::ValidTrajectoryPoint(trajectory_point)) {
270 AERROR << "Invalid trajectory point ["
271 << trajectory_point.ShortDebugString() << "]";
272 break;
273 }
274 }
275 }
276 }
277 }
278
279 auto end_time5 = std::chrono::system_clock::now();
280 diff = end_time5 - end_time1;
281 ADEBUG << "End to end time elapsed: " << diff.count() * 1000 << " msec.";
282
283 // Publish output
284 common::util::FillHeader(node_->Name(), &prediction_obstacles);
285 prediction_writer_->Write(prediction_obstacles);
286
287 if (FLAGS_prediction_eval_mode) {
288 for (auto const& prediction_obstacle :
289 prediction_obstacles.prediction_obstacle()) {
290 AINFO << "prediction_eval_log (info): "
291 << std::to_string(prediction_obstacle.timestamp()) << ", "
292 << prediction_obstacle.perception_obstacle().id() << ", "
293 << prediction_obstacle.priority().priority() << ", "
294 << prediction_obstacle.perception_obstacle().type() << ", "
295 << std::to_string(
296 prediction_obstacle.perception_obstacle().position().x())
297 << ", " << std::to_string(
298 prediction_obstacle.perception_obstacle().position().y());
299
300 if (prediction_obstacle.trajectory_size() != 0) {
301 for (auto const& trajectory : prediction_obstacle.trajectory()) {
302 for (auto const& trajectory_point : trajectory.trajectory_point()) {
303 AINFO << "prediction_eval_log (traj): "
304 << std::to_string(prediction_obstacle.timestamp()) << ", "
305 << prediction_obstacle.perception_obstacle().id() << ", "
306 << std::to_string(trajectory_point.path_point().x()) << ", "
307 << std::to_string(trajectory_point.path_point().y());
308 }
309 break;
310 }
311 }
312 }
313 }
314
315 return true;
316}
317
318} // namespace prediction
319} // namespace apollo
a singleton clock that can be used to get the current timestamp.
Definition clock.h:39
static double NowInSeconds()
gets the current time in second.
Definition clock.cc:56
static Time Now()
PRECISION >= 1000000 means the precision is at least 1us.
Definition clock.cc:40
std::shared_ptr< Node > node_
static void OnPerception(const perception::PerceptionObstacles &perception_obstacles, const std::shared_ptr< ContainerManager > &container_manager, EvaluatorManager *evaluator_manager, PredictorManager *predictor_manager, ScenarioManager *scenario_manager, PredictionObstacles *const prediction_obstacles)
static void OnStoryTelling(ContainerManager *container_manager, const storytelling::Stories &story)
static void OnLocalization(ContainerManager *container_manager, const localization::LocalizationEstimate &localization)
static void OnPlanning(ContainerManager *container_manager, const planning::ADCTrajectory &adc_trajectory)
static bool Init(ContainerManager *container_manager, EvaluatorManager *evaluator_manager, PredictorManager *predictor_manager, const PredictionConf &prediction_conf)
static void ContainerProcess(const std::shared_ptr< ContainerManager > &container_manager, const perception::PerceptionObstacles &perception_obstacles, ScenarioManager *scenario_manger)
Prediction obstacle.
Definition obstacle.h:52
bool Proc(const std::shared_ptr< perception::PerceptionObstacles > &) override
Data callback upon receiving a perception obstacle message.
void OfflineProcessFeatureProtoFile(const std::string &features_proto_file)
Load and process feature proto file.
bool Init() override
Initialize the node
std::string Name() const
Get name of the node
static bool Ready()
Check if map is ready
static bool ValidTrajectoryPoint(const common::TrajectoryPoint &trajectory_point)
Check if a trajectory point is valid
#define ADEBUG
Definition log.h:41
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
Some string util functions.
bool GetProtoFromBinaryFile(const std::string &file_name, google::protobuf::Message *message)
Parses the content of the file specified by the file_name as binary representation of protobufs,...
Definition file.cc:118
class register implement
Definition arena_queue.h:37
Use evaluator manager to manage all evaluators
Use predictor manager to manage all predictors
optional string perception_obstacles_topic_name