Apollo 11.0
自动驾驶开放平台
message_process.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2019 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
21#include "cyber/common/file.h"
34#include "modules/prediction/proto/offline_features.pb.h"
39
40namespace apollo {
41namespace prediction {
42
53
54bool MessageProcess::Init(ContainerManager* container_manager,
55 EvaluatorManager* evaluator_manager,
56 PredictorManager* predictor_manager,
57 const PredictionConf& prediction_conf) {
58 InitContainers(container_manager);
59 InitEvaluators(evaluator_manager, prediction_conf);
60 InitPredictors(predictor_manager, prediction_conf);
61
62 if (!FLAGS_use_navigation_mode && !PredictionMap::Ready()) {
63 AERROR << "Map cannot be loaded.";
64 return false;
65 }
66
67 return true;
68}
69
72 if (!cyber::common::GetProtoFromFile(FLAGS_prediction_adapter_config_filename,
73 &adapter_conf)) {
74 AERROR << "Unable to load adapter conf file: "
75 << FLAGS_prediction_adapter_config_filename;
76 return false;
77 }
78 ADEBUG << "Adapter config file is loaded into: "
79 << adapter_conf.ShortDebugString();
80
81 container_manager->Init(adapter_conf);
82 return true;
83}
84
86 const PredictionConf& prediction_conf) {
87 evaluator_manager->Init(prediction_conf);
88 return true;
89}
90
92 const PredictionConf& prediction_conf) {
93 predictor_manager->Init(prediction_conf);
94 return true;
95}
96
98 const std::shared_ptr<ContainerManager>& container_manager,
99 const perception::PerceptionObstacles& perception_obstacles,
100 ScenarioManager* scenario_manager) {
101 ADEBUG << "Received a perception message ["
102 << perception_obstacles.ShortDebugString() << "].";
103
104 // Get obstacles_container
105 auto ptr_obstacles_container =
106 container_manager->GetContainer<ObstaclesContainer>(
108 CHECK_NOTNULL(ptr_obstacles_container);
109 ptr_obstacles_container->CleanUp();
110
111 // Get pose_container
112 auto ptr_ego_pose_container = container_manager->GetContainer<PoseContainer>(
114 CHECK_NOTNULL(ptr_ego_pose_container);
115
116 // Get adc_trajectory_container
117 auto ptr_ego_trajectory_container =
118 container_manager->GetContainer<ADCTrajectoryContainer>(
120 CHECK_NOTNULL(ptr_ego_trajectory_container);
121
122 // Get storytelling_container
123 auto ptr_storytelling_container =
124 container_manager->GetContainer<StoryTellingContainer>(
126 CHECK_NOTNULL(ptr_storytelling_container);
127
128 // Insert ADC into the obstacle_container.
129 const PerceptionObstacle* ptr_ego_vehicle =
130 ptr_ego_pose_container->ToPerceptionObstacle();
131 if (ptr_ego_vehicle != nullptr) {
132 double perception_obs_timestamp = ptr_ego_vehicle->timestamp();
133 if (perception_obstacles.has_header() &&
134 perception_obstacles.header().has_timestamp_sec()) {
135 ADEBUG << "Correcting " << std::fixed << std::setprecision(6)
136 << ptr_ego_vehicle->timestamp() << " to " << std::fixed
137 << std::setprecision(6)
138 << perception_obstacles.header().timestamp_sec();
139 perception_obs_timestamp = perception_obstacles.header().timestamp_sec();
140 }
141 ptr_obstacles_container->InsertPerceptionObstacle(*ptr_ego_vehicle,
142 perception_obs_timestamp);
143 double x = ptr_ego_vehicle->position().x();
144 double y = ptr_ego_vehicle->position().y();
145 ADEBUG << "Get ADC position [" << std::fixed << std::setprecision(6) << x
146 << ", " << std::fixed << std::setprecision(6) << y << "].";
147 ptr_ego_trajectory_container->SetPosition({x, y});
148 }
149
150 // Insert perception_obstacles
151 ptr_obstacles_container->Insert(perception_obstacles);
152
153 ObstaclesPrioritizer obstacles_prioritizer(container_manager);
154
155 InteractionFilter interaction_filter(container_manager);
156
157 // Ignore some obstacles
158 obstacles_prioritizer.AssignIgnoreLevel();
159
160 // Scenario analysis
161 scenario_manager->Run(container_manager.get());
162
163 // Build junction feature for the obstacles in junction
164 const Scenario scenario = scenario_manager->scenario();
165 if (scenario.type() == Scenario::JUNCTION && scenario.has_junction_id()) {
166 ptr_obstacles_container->GetJunctionAnalyzer()->Init(
167 scenario.junction_id());
168 ptr_obstacles_container->BuildJunctionFeature();
169 }
170
171 // Build lane graph
172 ptr_obstacles_container->BuildLaneGraph();
173
174 // Assign CautionLevel for obstacles
175 obstacles_prioritizer.AssignCautionLevel();
176
177 // Add interactive tag
178 if (FLAGS_enable_interactive_tag) {
179 interaction_filter.AssignInteractiveTag();
180 }
181
182 // Analyze RightOfWay for the caution obstacles
183 RightOfWay::Analyze(container_manager.get());
184}
185
187 const perception::PerceptionObstacles& perception_obstacles,
188 const std::shared_ptr<ContainerManager>& container_manager,
189 EvaluatorManager* evaluator_manager, PredictorManager* predictor_manager,
190 ScenarioManager* scenario_manager,
191 PredictionObstacles* const prediction_obstacles) {
192 ContainerProcess(container_manager, perception_obstacles, scenario_manager);
193
194 auto ptr_obstacles_container =
195 container_manager->GetContainer<ObstaclesContainer>(
197 CHECK_NOTNULL(ptr_obstacles_container);
198
199 auto ptr_ego_trajectory_container =
200 container_manager->GetContainer<ADCTrajectoryContainer>(
202 CHECK_NOTNULL(ptr_ego_trajectory_container);
203
204 // Insert features to FeatureOutput for offline_mode
205 if (FLAGS_prediction_offline_mode == PredictionConstants::kDumpFeatureProto) {
206 for (const int id :
207 ptr_obstacles_container->curr_frame_movable_obstacle_ids()) {
208 Obstacle* obstacle_ptr = ptr_obstacles_container->GetObstacle(id);
209 if (obstacle_ptr == nullptr) {
210 AERROR << "Null obstacle found.";
211 continue;
212 }
213 if (!obstacle_ptr->latest_feature().IsInitialized()) {
214 AERROR << "Obstacle [" << id << "] has no latest feature.";
215 continue;
216 }
217 // TODO(all): the adc trajectory should be part of features for learning
218 // algorithms rather than part of the feature.proto
219 *obstacle_ptr->mutable_latest_feature()->mutable_adc_trajectory_point() =
220 ptr_ego_trajectory_container->adc_trajectory().trajectory_point();
221
222 // adc trajectory timestamp
223 obstacle_ptr->mutable_latest_feature()->set_adc_timestamp(
224 ptr_ego_trajectory_container->adc_trajectory()
225 .header().timestamp_sec());
226
227 // ego pose_container
228 auto ptr_ego_pose = container_manager->GetContainer<PoseContainer>(
230 CHECK_NOTNULL(ptr_ego_pose);
231
232 // adc localization
233 obstacle_ptr->mutable_latest_feature()->mutable_adc_localization()->
234 CopyFrom(*ptr_ego_pose->ToPerceptionObstacle());
235
237 ADEBUG << "Insert feature into feature output";
238 }
239 // Not doing evaluation on offline mode
240 return;
241 }
242
243 // Make evaluations
244 evaluator_manager->Run(ptr_ego_trajectory_container,
245 ptr_obstacles_container);
246 if (FLAGS_prediction_offline_mode ==
248 FLAGS_prediction_offline_mode == PredictionConstants::kDumpFrameEnv) {
249 return;
250 }
251 // Make predictions
252 predictor_manager->Run(perception_obstacles, ptr_ego_trajectory_container,
253 ptr_obstacles_container);
254
255 // Get predicted obstacles
256 *prediction_obstacles = predictor_manager->prediction_obstacles();
257}
258
260 ContainerManager* container_manager,
261 const localization::LocalizationEstimate& localization) {
262 auto ptr_ego_pose_container = container_manager->GetContainer<PoseContainer>(
264 ACHECK(ptr_ego_pose_container != nullptr);
265 ptr_ego_pose_container->Insert(localization);
266
267 ADEBUG << "Received a localization message ["
268 << localization.ShortDebugString() << "].";
269}
270
272 const planning::ADCTrajectory& adc_trajectory) {
273 auto ptr_ego_trajectory_container =
274 container_manager->GetContainer<ADCTrajectoryContainer>(
276 ACHECK(ptr_ego_trajectory_container != nullptr);
277 ptr_ego_trajectory_container->Insert(adc_trajectory);
278
279 ADEBUG << "Received a planning message [" << adc_trajectory.ShortDebugString()
280 << "].";
281
282 auto ptr_storytelling_container =
283 container_manager->GetContainer<StoryTellingContainer>(
285 CHECK_NOTNULL(ptr_storytelling_container);
286 ptr_ego_trajectory_container->SetJunction(
287 ptr_storytelling_container->ADCJunctionId(),
288 ptr_storytelling_container->ADCDistanceToJunction());
289}
290
292 const Stories& story) {
293 auto ptr_storytelling_container =
294 container_manager->GetContainer<StoryTellingContainer>(
296 CHECK_NOTNULL(ptr_storytelling_container);
297 ptr_storytelling_container->Insert(story);
298
299 ADEBUG << "Received a storytelling message [" << story.ShortDebugString()
300 << "].";
301}
302
304 const PredictionConf& prediction_conf,
305 const std::shared_ptr<ContainerManager>& container_manager,
306 EvaluatorManager* evaluator_manager, PredictorManager* predictor_manager,
307 ScenarioManager* scenario_manager, const std::string& record_filepath) {
308 RecordReader reader(record_filepath);
309 RecordMessage message;
310 RecordWriter writer;
311 if (FLAGS_prediction_offline_mode == PredictionConstants::kDumpRecord) {
312 writer.Open(record_filepath + ".new_prediction");
313 }
314 while (reader.ReadMessage(&message)) {
315 if (message.channel_name ==
316 prediction_conf.topic_conf().perception_obstacle_topic()) {
317 PerceptionObstacles perception_obstacles;
318 if (perception_obstacles.ParseFromString(message.content)) {
319 if (FLAGS_prediction_offline_mode == PredictionConstants::kDumpRecord) {
321 message.channel_name, perception_obstacles, message.time);
322 }
323 PredictionObstacles prediction_obstacles;
324 OnPerception(perception_obstacles, container_manager, evaluator_manager,
325 predictor_manager, scenario_manager,
326 &prediction_obstacles);
327 if (FLAGS_prediction_offline_mode == PredictionConstants::kDumpRecord) {
329 prediction_conf.topic_conf().perception_obstacle_topic(),
330 prediction_obstacles, message.time);
331 AINFO << "Generated a new prediction message.";
332 }
333 }
334 } else if (message.channel_name ==
335 prediction_conf.topic_conf().localization_topic()) {
336 LocalizationEstimate localization;
337 if (localization.ParseFromString(message.content)) {
338 if (FLAGS_prediction_offline_mode == PredictionConstants::kDumpRecord) {
340 localization, message.time);
341 }
342 OnLocalization(container_manager.get(), localization);
343 }
344 } else if (message.channel_name ==
345 prediction_conf.topic_conf().planning_trajectory_topic()) {
346 ADCTrajectory adc_trajectory;
347 if (adc_trajectory.ParseFromString(message.content)) {
348 OnPlanning(container_manager.get(), adc_trajectory);
349 }
350 }
351 }
352 if (FLAGS_prediction_offline_mode == PredictionConstants::kDumpRecord) {
353 writer.Close();
354 }
355}
356
357} // namespace prediction
358} // namespace apollo
bool ReadMessage(RecordMessage *message, uint64_t begin_time=0, uint64_t end_time=std::numeric_limits< uint64_t >::max())
Read one message from reader.
bool Open(const std::string &file)
Open a record to write.
bool WriteMessage(const std::string &channel_name, const MessageT &message, const uint64_t time_nanosec, const std::string &proto_desc="")
Write a message to record.
T * GetContainer(const common::adapter::AdapterConfig::MessageType &type)
Get mutable container
void Init(const common::adapter::AdapterManagerConfig &config)
Container manager initialization
void Run(const ADCTrajectoryContainer *adc_trajectory_container, ObstaclesContainer *obstacles_container)
Run evaluators
void Init(const PredictionConf &config)
Initializer
static void InsertFeatureProto(const Feature &feature)
Insert a feature
void AssignInteractiveTag()
Assign interactive tag for vehicle type obstacles which are close to ADC
static bool InitContainers(ContainerManager *container_manager)
static bool InitEvaluators(EvaluatorManager *evaluator_manager, const PredictionConf &prediction_conf)
static void ProcessOfflineData(const PredictionConf &prediction_conf, const std::shared_ptr< ContainerManager > &container_manager, EvaluatorManager *evaluator_manager, PredictorManager *predictor_manager, ScenarioManager *scenario_manager, const std::string &record_filepath)
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)
static bool InitPredictors(PredictorManager *predictor_manager, const PredictionConf &prediction_conf)
Prediction obstacle.
Definition obstacle.h:52
Feature * mutable_latest_feature()
Get a pointer to the latest feature.
Definition obstacle.cc:88
const Feature & latest_feature() const
Get the latest feature.
Definition obstacle.cc:78
static bool Ready()
Check if map is ready
void Run(const apollo::perception::PerceptionObstacles &perception_obstacles, const ADCTrajectoryContainer *adc_trajectory_container, ObstaclesContainer *obstacles_container)
Execute the predictor generation
void Init(const PredictionConf &config)
Initializer
const PredictionObstacles & prediction_obstacles()
Get prediction obstacles
static void Analyze(ContainerManager *container_manager)
Set right_of_way for all lane_sequence
const Scenario scenario() const
Get scenario analysis result
void Run(ContainerManager *container_manager)
Run scenario analysis
#define ACHECK(cond)
Definition log.h:80
#define ADEBUG
Definition log.h:41
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
bool GetProtoFromFile(const std::string &file_name, google::protobuf::Message *message)
Parses the content of the file specified by the file_name as a representation of protobufs,...
Definition file.cc:132
class register implement
Definition arena_queue.h:37
Use evaluator manager to manage all evaluators
Holds all global constants for the prediction module.
Use predictor manager to manage all predictors
story telling container
optional double timestamp_sec
Definition header.proto:9
Basic data struct of record message.
std::string content
The content of the message.
uint64_t time
The time (nanosecond) of the message.
std::string channel_name
The channel name of the message.
optional apollo::common::Point3D position