prediction.cc 5.5 KB
Newer Older
D
Dong Li 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/******************************************************************************
 * Copyright 2017 The Apollo Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *****************************************************************************/

#include "modules/prediction/prediction.h"

C
Calvin Miao 已提交
19
#include "modules/prediction/container/container_manager.h"
C
Calvin Miao 已提交
20
#include "modules/prediction/evaluator/evaluator_manager.h"
C
Calvin Miao 已提交
21
#include "modules/prediction/predictor/predictor_manager.h"
D
Dong Li 已提交
22
#include "modules/prediction/common/prediction_gflags.h"
C
Calvin Miao 已提交
23
#include "modules/prediction/proto/prediction_obstacle.pb.h"
K
kechxu 已提交
24 25
#include "modules/prediction/container/obstacles/obstacles_container.h"
#include "modules/prediction/container/pose/pose_container.h"
26 27
#include "modules/common/adapters/adapter_manager.h"
#include "modules/common/adapters/adapter_gflags.h"
C
Calvin Miao 已提交
28
#include "modules/common/util/file.h"
D
Dong Li 已提交
29 30 31 32 33

namespace apollo {
namespace prediction {

using ::apollo::perception::PerceptionObstacles;
C
Calvin Miao 已提交
34
using ::apollo::localization::LocalizationEstimate;
D
Dong Li 已提交
35
using ::apollo::common::adapter::AdapterManager;
36
using ::apollo::common::adapter::AdapterConfig;
C
Calvin Miao 已提交
37 38
using ::apollo::common::Status;
using ::apollo::common::ErrorCode;
D
Dong Li 已提交
39

40 41 42
std::string Prediction::Name() const {
  return FLAGS_prediction_module_name;
}
D
Dong Li 已提交
43

C
Calvin Miao 已提交
44 45
Status Prediction::Init() {
  // Load prediction conf
46
  prediction_conf_.Clear();
C
Calvin Miao 已提交
47
  if (!::apollo::common::util::GetProtoFromFile(FLAGS_prediction_conf_file,
48
                                                &prediction_conf_)) {
C
Calvin Miao 已提交
49
    return OnError("Unable to load prediction conf file: " +
C
Calvin Miao 已提交
50
                   FLAGS_prediction_conf_file);
C
Calvin Miao 已提交
51
  } else {
52
    ADEBUG << "Prediction config file is loaded into: "
53
           << prediction_conf_.ShortDebugString();
C
Calvin Miao 已提交
54 55
  }

56 57 58 59 60 61 62 63 64 65 66
  adapter_conf_.Clear();
  if (!::apollo::common::util::GetProtoFromFile(FLAGS_adapter_config_path,
                                                &adapter_conf_)) {
    return OnError("Unable to load adapter conf file: " +
                   FLAGS_adapter_config_path);
  } else {
    ADEBUG << "Adapter config file is loaded into: "
           << adapter_conf_.ShortDebugString();
  }

  // Initialization of all managers
D
Dong Li 已提交
67
  AdapterManager::instance()->Init();
68
  ContainerManager::instance()->Init(adapter_conf_);
69 70
  EvaluatorManager::instance()->Init(prediction_conf_);
  PredictorManager::instance()->Init(prediction_conf_);
C
Calvin Miao 已提交
71

72 73
  CHECK(AdapterManager::GetLocalization()) << "Localization is not ready.";
  CHECK(AdapterManager::GetPerceptionObstacles()) << "Perception is not ready.";
C
Calvin Miao 已提交
74

75
  // Set perception obstacle callback function
D
Dong Li 已提交
76 77
  AdapterManager::SetPerceptionObstaclesCallback(&Prediction::OnPerception,
                                                 this);
C
Calvin Miao 已提交
78
  return Status::OK();
D
Dong Li 已提交
79 80
}

C
Calvin Miao 已提交
81 82
Status Prediction::Start() {
  return Status::OK();
D
Dong Li 已提交
83 84 85 86 87
}

void Prediction::Stop() {}

void Prediction::OnPerception(const PerceptionObstacles &perception_obstacles) {
C
Calvin Miao 已提交
88
  auto localization_adapter = AdapterManager::GetLocalization();
89
  ObstaclesContainer* obstacles_container = dynamic_cast<ObstaclesContainer*>(
90 91
      ContainerManager::instance()->GetContainer(
      AdapterConfig::PERCEPTION_OBSTACLES));
92
  CHECK_NOTNULL(obstacles_container);
C
Calvin Miao 已提交
93
  if (localization_adapter->Empty()) {
94
    ADEBUG << "No localization message.";
C
Calvin Miao 已提交
95
  } else {
C
Calvin Miao 已提交
96
    const LocalizationEstimate& localization =
C
Calvin Miao 已提交
97 98 99 100
        localization_adapter->GetLatestObserved();
    ADEBUG << "Received localization message ["
           << localization.ShortDebugString()
           << "].";
101
    PoseContainer* pose_container = dynamic_cast<PoseContainer*>(
102 103
        ContainerManager::instance()->GetContainer(
        AdapterConfig::LOCALIZATION));
K
kechxu 已提交
104
    pose_container->Insert(localization);
105 106 107
    obstacles_container->InsertPerceptionObstacle(
        *(pose_container->ToPerceptionObstacle()),
        pose_container->GetTimestamp());
C
Calvin Miao 已提交
108
  }
109
  if (obstacles_container == nullptr) {
110 111
    AERROR << "Null obstacles container";
    return;
112
  }
K
kechxu 已提交
113
  obstacles_container->Insert(perception_obstacles);
C
Calvin Miao 已提交
114
  EvaluatorManager::instance()->Run(perception_obstacles);
C
Calvin Miao 已提交
115
  PredictorManager::instance()->Run(perception_obstacles);
116 117 118 119

  PredictionObstacles prediction_obstacles;
  prediction_obstacles.CopyFrom(
      PredictorManager::instance()->prediction_obstacles());
S
siyangy 已提交
120
  AdapterManager::FillPredictionHeader(Name(), &prediction_obstacles);
121
  AdapterManager::PublishPrediction(prediction_obstacles);
122 123 124 125 126 127 128 129 130 131 132 133
  ADEBUG << "Published a prediction message ["
         << prediction_obstacles.ShortDebugString() << "].";
  // for (const auto& pob : prediction_obstacles.prediction_obstacle()) {
  //   for (const auto& traj : pob.trajectory()) {
  //     for (const auto& traj_point : traj.trajectory_point()) {
  //       AINFO << "[" << std::fixed << std::setprecision(6)
  //             << traj_point.path_point().x() << ", "
  //             << std::fixed << std::setprecision(6)
  //             << traj_point.path_point().y() << "]";
  //     }
  //   }
  // }
D
Dong Li 已提交
134 135
}

C
Calvin Miao 已提交
136 137 138 139
Status Prediction::OnError(const std::string& error_msg) {
  return Status(ErrorCode::PREDICTION_ERROR, error_msg);
}

D
Dong Li 已提交
140 141
}  // namespace prediction
}  // namespace apollo