Apollo 11.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
44
46
47std::string PredictionComponent::Name() const {
48 return FLAGS_prediction_module_name;
49}
50
52 const std::string& features_proto_file_name) {
53 auto obstacles_container_ptr =
54 container_manager_->GetContainer<ObstaclesContainer>(
56 obstacles_container_ptr->Clear();
57 Features features;
58 apollo::cyber::common::GetProtoFromBinaryFile(features_proto_file_name,
59 &features);
60 for (const Feature& feature : features.feature()) {
61 obstacles_container_ptr->InsertFeatureProto(feature);
62 Obstacle* obstacle_ptr = obstacles_container_ptr->GetObstacle(feature.id());
63 if (!obstacle_ptr) {
64 continue;
65 }
66 evaluator_manager_->EvaluateObstacle(obstacle_ptr, obstacles_container_ptr);
67 }
68}
69
71 component_start_time_ = Clock::NowInSeconds();
72
73 container_manager_ = std::make_shared<ContainerManager>();
74 evaluator_manager_.reset(new EvaluatorManager());
75 predictor_manager_.reset(new PredictorManager());
76 scenario_manager_.reset(new ScenarioManager());
77
78 PredictionConf prediction_conf;
79 if (!ComponentBase::GetProtoConfig(&prediction_conf)) {
80 AERROR << "Unable to load prediction conf file: "
81 << ComponentBase::ConfigFilePath();
82 return false;
83 }
84 ADEBUG << "Prediction config file is loaded into: "
85 << prediction_conf.ShortDebugString();
86
87 if (!MessageProcess::Init(container_manager_.get(), evaluator_manager_.get(),
88 predictor_manager_.get(), prediction_conf)) {
89 return false;
90 }
91
92 planning_reader_ = node_->CreateReader<ADCTrajectory>(
93 prediction_conf.topic_conf().planning_trajectory_topic(), nullptr);
94
95 localization_reader_ =
97 prediction_conf.topic_conf().localization_topic(), nullptr);
98
99 storytelling_reader_ = node_->CreateReader<storytelling::Stories>(
100 prediction_conf.topic_conf().storytelling_topic(), nullptr);
101
102 prediction_writer_ = node_->CreateWriter<PredictionObstacles>(
103 prediction_conf.topic_conf().prediction_topic());
104
105 container_writer_ = node_->CreateWriter<SubmoduleOutput>(
106 prediction_conf.topic_conf().container_topic_name());
107
108 adc_container_writer_ = node_->CreateWriter<ADCTrajectoryContainer>(
109 prediction_conf.topic_conf().adccontainer_topic_name());
110
111 perception_obstacles_writer_ = node_->CreateWriter<PerceptionObstacles>(
112 prediction_conf.topic_conf().perception_obstacles_topic_name());
113
114 return true;
115}
116
118 const std::shared_ptr<PerceptionObstacles>& perception_obstacles) {
119 if (FLAGS_use_lego) {
120 return ContainerSubmoduleProcess(perception_obstacles);
121 }
122 return PredictionEndToEndProc(perception_obstacles);
123}
124
125bool PredictionComponent::ContainerSubmoduleProcess(
126 const std::shared_ptr<PerceptionObstacles>& perception_obstacles) {
127 constexpr static size_t kHistorySize = 10;
128 const auto frame_start_time = Clock::Now();
129 // Read localization info. and call OnLocalization to update
130 // the PoseContainer.
131 localization_reader_->Observe();
132 auto ptr_localization_msg = localization_reader_->GetLatestObserved();
133 if (ptr_localization_msg == nullptr) {
134 AERROR << "Prediction: cannot receive any localization message.";
135 return false;
136 }
137 MessageProcess::OnLocalization(container_manager_.get(),
138 *ptr_localization_msg);
139
140 // Read planning info. of last frame and call OnPlanning to update
141 // the ADCTrajectoryContainer
142 planning_reader_->Observe();
143 auto ptr_trajectory_msg = planning_reader_->GetLatestObserved();
144 if (ptr_trajectory_msg != nullptr) {
145 MessageProcess::OnPlanning(container_manager_.get(), *ptr_trajectory_msg);
146 }
147
148 // Read storytelling message and call OnStorytelling to update the
149 // StoryTellingContainer
150 storytelling_reader_->Observe();
151 auto ptr_storytelling_msg = storytelling_reader_->GetLatestObserved();
152 if (ptr_storytelling_msg != nullptr) {
153 MessageProcess::OnStoryTelling(container_manager_.get(),
154 *ptr_storytelling_msg);
155 }
156
157 MessageProcess::ContainerProcess(container_manager_, *perception_obstacles,
158 scenario_manager_.get());
159
160 auto obstacles_container_ptr =
161 container_manager_->GetContainer<ObstaclesContainer>(
163 CHECK_NOTNULL(obstacles_container_ptr);
164
165 auto adc_trajectory_container_ptr =
166 container_manager_->GetContainer<ADCTrajectoryContainer>(
168 CHECK_NOTNULL(adc_trajectory_container_ptr);
169
170 SubmoduleOutput submodule_output =
171 obstacles_container_ptr->GetSubmoduleOutput(kHistorySize,
172 frame_start_time);
173 submodule_output.set_curr_scenario(scenario_manager_->scenario());
174 container_writer_->Write(submodule_output);
175 adc_container_writer_->Write(*adc_trajectory_container_ptr);
176 perception_obstacles_writer_->Write(*perception_obstacles);
177 return true;
178}
179
180bool PredictionComponent::PredictionEndToEndProc(
181 const std::shared_ptr<PerceptionObstacles>& perception_obstacles) {
182 if (FLAGS_prediction_test_mode &&
183 (Clock::NowInSeconds() - component_start_time_ >
184 FLAGS_prediction_test_duration)) {
185 ADEBUG << "Prediction finished running in test mode";
186 }
187
188 // Update relative map if needed
189 if (FLAGS_use_navigation_mode && !PredictionMap::Ready()) {
190 AERROR << "Relative map is empty.";
191 return false;
192 }
193
194 frame_start_time_ = Clock::NowInSeconds();
195 auto end_time1 = std::chrono::system_clock::now();
196
197 // Read localization info. and call OnLocalization to update
198 // the PoseContainer.
199 localization_reader_->Observe();
200 auto ptr_localization_msg = localization_reader_->GetLatestObserved();
201 if (ptr_localization_msg == nullptr) {
202 AERROR << "Prediction: cannot receive any localization message.";
203 return false;
204 }
205 MessageProcess::OnLocalization(container_manager_.get(),
206 *ptr_localization_msg);
207 auto end_time2 = std::chrono::system_clock::now();
208 std::chrono::duration<double> diff = end_time2 - end_time1;
209 ADEBUG << "Time for updating PoseContainer: " << diff.count() * 1000
210 << " msec.";
211
212 // Read storytelling message and call OnStorytelling to update the
213 // StoryTellingContainer
214 storytelling_reader_->Observe();
215 auto ptr_storytelling_msg = storytelling_reader_->GetLatestObserved();
216 if (ptr_storytelling_msg != nullptr) {
217 MessageProcess::OnStoryTelling(container_manager_.get(),
218 *ptr_storytelling_msg);
219 }
220
221 // Read planning info. of last frame and call OnPlanning to update
222 // the ADCTrajectoryContainer
223 planning_reader_->Observe();
224 auto ptr_trajectory_msg = planning_reader_->GetLatestObserved();
225 if (ptr_trajectory_msg != nullptr) {
226 MessageProcess::OnPlanning(container_manager_.get(), *ptr_trajectory_msg);
227 }
228 auto end_time3 = std::chrono::system_clock::now();
229 diff = end_time3 - end_time2;
230 ADEBUG << "Time for updating ADCTrajectoryContainer: " << diff.count() * 1000
231 << " msec.";
232
233 // Get all perception_obstacles of this frame and call OnPerception to
234 // process them all.
235 auto perception_msg = *perception_obstacles;
236 PredictionObstacles prediction_obstacles;
238 perception_msg, container_manager_, evaluator_manager_.get(),
239 predictor_manager_.get(), scenario_manager_.get(), &prediction_obstacles);
240 auto end_time4 = std::chrono::system_clock::now();
241 diff = end_time4 - end_time3;
242 ADEBUG << "Time for updating PerceptionContainer: " << diff.count() * 1000
243 << " msec.";
244
245 // Postprocess prediction obstacles message
246 prediction_obstacles.set_start_timestamp(frame_start_time_);
247 prediction_obstacles.set_end_timestamp(Clock::NowInSeconds());
248 prediction_obstacles.mutable_header()->set_lidar_timestamp(
249 perception_msg.header().lidar_timestamp());
250 prediction_obstacles.mutable_header()->set_camera_timestamp(
251 perception_msg.header().camera_timestamp());
252 prediction_obstacles.mutable_header()->set_radar_timestamp(
253 perception_msg.header().radar_timestamp());
254
255 prediction_obstacles.set_perception_error_code(perception_msg.error_code());
256
257 if (FLAGS_prediction_test_mode) {
258 for (auto const& prediction_obstacle :
259 prediction_obstacles.prediction_obstacle()) {
260 for (auto const& trajectory : prediction_obstacle.trajectory()) {
261 for (auto const& trajectory_point : trajectory.trajectory_point()) {
262 if (!ValidationChecker::ValidTrajectoryPoint(trajectory_point)) {
263 AERROR << "Invalid trajectory point ["
264 << trajectory_point.ShortDebugString() << "]";
265 break;
266 }
267 }
268 }
269 }
270 }
271
272 auto end_time5 = std::chrono::system_clock::now();
273 diff = end_time5 - end_time1;
274 ADEBUG << "End to end time elapsed: " << diff.count() * 1000 << " msec.";
275
276 // Publish output
277 common::util::FillHeader(node_->Name(), &prediction_obstacles);
278 prediction_writer_->Write(prediction_obstacles);
279
280 if (FLAGS_prediction_eval_mode) {
281 for (auto const& prediction_obstacle :
282 prediction_obstacles.prediction_obstacle()) {
283 AINFO << "prediction_eval_log (info): "
284 << std::to_string(prediction_obstacle.timestamp()) << ", "
285 << prediction_obstacle.perception_obstacle().id() << ", "
286 << prediction_obstacle.priority().priority() << ", "
287 << prediction_obstacle.perception_obstacle().type() << ", "
288 << std::to_string(
289 prediction_obstacle.perception_obstacle().position().x())
290 << ", " << std::to_string(
291 prediction_obstacle.perception_obstacle().position().y());
292
293 if (prediction_obstacle.trajectory_size() != 0) {
294 for (auto const& trajectory : prediction_obstacle.trajectory()) {
295 for (auto const& trajectory_point : trajectory.trajectory_point()) {
296 AINFO << "prediction_eval_log (traj): "
297 << std::to_string(prediction_obstacle.timestamp()) << ", "
298 << prediction_obstacle.perception_obstacle().id() << ", "
299 << std::to_string(trajectory_point.path_point().x()) << ", "
300 << std::to_string(trajectory_point.path_point().y());
301 }
302 break;
303 }
304 }
305 }
306 }
307
308 return true;
309}
310
311} // namespace prediction
312} // 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
float diff(Image< float > *I, int x1, int y1, int x2, int y2)
Dissimilarity measure between pixels
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