predictor_manager.cc 5.8 KB
Newer Older
C
Calvin Miao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/******************************************************************************
 * 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/predictor/predictor_manager.h"
18

19 20
#include <memory>

21 22 23
#include "modules/prediction/predictor/vehicle/lane_sequence_predictor.h"
#include "modules/prediction/predictor/vehicle/free_move_predictor.h"
#include "modules/prediction/predictor/pedestrian/regional_predictor.h"
24 25
#include "modules/prediction/container/container_manager.h"
#include "modules/prediction/container/obstacles/obstacles_container.h"
C
Calvin Miao 已提交
26 27 28 29

namespace apollo {
namespace prediction {

30 31
using ::apollo::perception::PerceptionObstacles;
using ::apollo::perception::PerceptionObstacle;
32
using ::apollo::common::adapter::AdapterConfig;
C
Calvin Miao 已提交
33

34
PredictorManager::PredictorManager() {
35 36 37 38 39 40 41
  RegisterPredictors();
}

void PredictorManager::RegisterPredictors() {
  RegisterPredictor(ObstacleConf::LANE_SEQUENCE_PREDICTOR);
  RegisterPredictor(ObstacleConf::FREE_MOVE_PREDICTOR);
  RegisterPredictor(ObstacleConf::REGIONAL_PREDICTOR);
42 43
}

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
void PredictorManager::Init(const PredictionConf& config) {
  for (const auto& obstacle_conf : config.obstacle_conf()) {
    if (!obstacle_conf.has_obstacle_type()) {
      ADEBUG << "Obstacle config ["
             << obstacle_conf.ShortDebugString()
             << "] has not defined obstacle type.";
      continue;
    }

    if (obstacle_conf.obstacle_type() == PerceptionObstacle::VEHICLE) {
      if (!obstacle_conf.has_obstacle_status() ||
          !obstacle_conf.has_predictor_type()) {
        ADEBUG << "Vehicle obstacle config ["
               << obstacle_conf.ShortDebugString()
               << "] has not defined obstacle status or predictor type.";
        continue;
      } else if (obstacle_conf.obstacle_status() == ObstacleConf::ON_LANE) {
        vehicle_on_lane_predictor_ = obstacle_conf.predictor_type();
      } else if (obstacle_conf.obstacle_status() == ObstacleConf::OFF_LANE) {
        vehicle_off_lane_predictor_ = obstacle_conf.predictor_type();
      }
    } else if (obstacle_conf.obstacle_type() ==
          PerceptionObstacle::PEDESTRIAN) {
      pedestrian_predictor_ = obstacle_conf.predictor_type();
    }
  }

  AINFO << "Defined vehicle on lane obstacle predictor ["
        << vehicle_on_lane_predictor_ << "].";
  AINFO << "Defined vehicle off lane obstacle predictor ["
        << vehicle_off_lane_predictor_ << "].";
  AINFO << "Defined pedestrian obstacle predictor ["
        << pedestrian_predictor_ << "].";
  AINFO << "Defined default obstacle predictor ["
        << default_predictor_ << "].";
}
80

81 82
Predictor* PredictorManager::GetPredictor(
    const ObstacleConf::PredictorType& type) {
83 84
  auto it = predictors_.find(type);
  return it != predictors_.end() ? it->second.get() : nullptr;
C
Calvin Miao 已提交
85 86
}

87
void PredictorManager::Run(const PerceptionObstacles& perception_obstacles) {
88
  prediction_obstacles_.Clear();
89
  ObstaclesContainer *container = dynamic_cast<ObstaclesContainer*>(
90 91
      ContainerManager::instance()->GetContainer(
      AdapterConfig::PERCEPTION_OBSTACLES));
92 93
  CHECK_NOTNULL(container);

94
  Predictor *predictor = nullptr;
95 96 97
  for (const auto& perception_obstacle :
      perception_obstacles.perception_obstacle()) {
    int id = perception_obstacle.id();
98 99
    Obstacle* obstacle = container->GetObstacle(id);
    CHECK_NOTNULL(obstacle);
100
    switch (perception_obstacle.type()) {
101
      case PerceptionObstacle::VEHICLE: {
102
        if (obstacle->IsOnLane()) {
103
          predictor = GetPredictor(vehicle_on_lane_predictor_);
104
        } else {
105
          predictor = GetPredictor(vehicle_off_lane_predictor_);
106
        }
107 108 109
        break;
      }
      case PerceptionObstacle::PEDESTRIAN: {
110
        predictor = GetPredictor(pedestrian_predictor_);
111 112 113
        break;
      }
      default: {
114
        predictor = GetPredictor(default_predictor_);
115 116 117
        break;
      }
    }
118
    PredictionObstacle prediction_obstacle;
119 120 121 122
    if (predictor != nullptr) {
      predictor->Predict(obstacle);
      prediction_obstacle.CopyFrom(predictor->prediction_obstacle());
    }
123 124 125 126
    prediction_obstacle.mutable_perception_obstacle()->
        CopyFrom(perception_obstacle);
    prediction_obstacles_.add_prediction_obstacle()->
        CopyFrom(prediction_obstacle);
127
  }
C
Calvin Miao 已提交
128 129
  prediction_obstacles_.set_perception_error_code(
      perception_obstacles.error_code());
130 131
}

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
std::unique_ptr<Predictor> PredictorManager::CreatePredictor(
    const ObstacleConf::PredictorType& type) {
  std::unique_ptr<Predictor> predictor_ptr(nullptr);
  switch (type) {
    case ObstacleConf::LANE_SEQUENCE_PREDICTOR: {
      predictor_ptr.reset(new LaneSequencePredictor());
      break;
    }
    case ObstacleConf::FREE_MOVE_PREDICTOR: {
      predictor_ptr.reset(new FreeMovePredictor());
      break;
    }
    case ObstacleConf::REGIONAL_PREDICTOR: {
      predictor_ptr.reset(new RegionalPredictor());
      break;
    }
    default: {
      break;
    }
  }
  return predictor_ptr;
}

void PredictorManager::RegisterPredictor(
    const ObstacleConf::PredictorType& type) {
  predictors_[type] = CreatePredictor(type);
C
Calvin Miao 已提交
158
  AINFO << "Predictor [" << type << "] is registered.";
159 160
}

161 162 163
const PredictionObstacles& PredictorManager::prediction_obstacles() {
  return prediction_obstacles_;
}
C
Calvin Miao 已提交
164

C
Calvin Miao 已提交
165 166 167
}  // namespace prediction
}  // namespace apollo