task_factory.cc 5.7 KB
Newer Older
D
Dong Li 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/******************************************************************************
 * Copyright 2018 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.
 *****************************************************************************/

/**
 * @file
 **/

21
#include "modules/planning/tasks/task_factory.h"
D
Dong Li 已提交
22 23 24 25

#include "modules/planning/proto/planning_config.pb.h"

#include "modules/common/status/status.h"
26 27 28 29 30 31 32 33 34 35 36 37 38 39
#include "modules/planning/tasks/deciders/decider_creep.h"
#include "modules/planning/tasks/deciders/decider_rule_based_stop.h"
#include "modules/planning/tasks/deciders/side_pass_path_decider.h"
#include "modules/planning/tasks/deciders/side_pass_safety.h"
#include "modules/planning/tasks/optimizers/dp_poly_path/dp_poly_path_optimizer.h"
#include "modules/planning/tasks/optimizers/dp_st_speed/dp_st_speed_optimizer.h"
#include "modules/planning/tasks/optimizers/path_decider/path_decider.h"
#include "modules/planning/tasks/optimizers/proceed_with_caution_speed/proceed_with_caution_speed_generator.h"
#include "modules/planning/tasks/optimizers/qp_piecewise_jerk_path/qp_piecewise_jerk_path_optimizer.h"
#include "modules/planning/tasks/optimizers/qp_spline_path/qp_spline_path_optimizer.h"
#include "modules/planning/tasks/optimizers/qp_spline_st_speed/qp_spline_st_speed_optimizer.h"
#include "modules/planning/tasks/optimizers/speed_decider/speed_decider.h"
#include "modules/planning/tasks/rss/decider_rss.h"
#include "modules/planning/tasks/task.h"
D
Dong Li 已提交
40 41 42 43

namespace apollo {
namespace planning {

44 45 46 47
apollo::common::util::Factory<
    TaskConfig::TaskType, Task, Task* (*)(const TaskConfig& config),
    std::unordered_map<TaskConfig::TaskType,
                       Task* (*)(const TaskConfig& config), std::hash<int>>>
D
Dong Li 已提交
48 49
    TaskFactory::task_factory_;

50
std::unordered_map<TaskConfig::TaskType, TaskConfig, std::hash<int>>
51 52
    TaskFactory::default_task_configs_;

53
void TaskFactory::Init(const PlanningConfig& config) {
D
Dong Li 已提交
54 55 56 57 58 59 60 61
  task_factory_.Register(TaskConfig::DP_ST_SPEED_OPTIMIZER,
                         [](const TaskConfig& config) -> Task* {
                           return new DpStSpeedOptimizer(config);
                         });
  task_factory_.Register(TaskConfig::QP_SPLINE_PATH_OPTIMIZER,
                         [](const TaskConfig& config) -> Task* {
                           return new QpSplinePathOptimizer(config);
                         });
62 63 64 65
  task_factory_.Register(TaskConfig::SIDE_PASS_PATH_DECIDER,
                         [](const TaskConfig& config) -> Task* {
                           return new SidePassPathDecider(config);
                         });
D
Dong Li 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
  task_factory_.Register(TaskConfig::DP_POLY_PATH_OPTIMIZER,
                         [](const TaskConfig& config) -> Task* {
                           return new DpPolyPathOptimizer(config);
                         });
  task_factory_.Register(TaskConfig::PATH_DECIDER,
                         [](const TaskConfig& config) -> Task* {
                           return new PathDecider(config);
                         });
  task_factory_.Register(TaskConfig::SPEED_DECIDER,
                         [](const TaskConfig& config) -> Task* {
                           return new SpeedDecider(config);
                         });
  task_factory_.Register(TaskConfig::QP_SPLINE_ST_SPEED_OPTIMIZER,
                         [](const TaskConfig& config) -> Task* {
                           return new QpSplineStSpeedOptimizer(config);
                         });
  task_factory_.Register(TaskConfig::QP_PIECEWISE_JERK_PATH_OPTIMIZER,
                         [](const TaskConfig& config) -> Task* {
                           return new QpPiecewiseJerkPathOptimizer(config);
                         });
86 87 88 89
  task_factory_.Register(TaskConfig::PROCEED_WITH_CAUTION_SPEED,
                         [](const TaskConfig& config) -> Task* {
                           return new ProceedWithCautionSpeedGenerator(config);
                         });
D
Dong Li 已提交
90 91 92 93
  task_factory_.Register(TaskConfig::DECIDER_CREEP,
                         [](const TaskConfig& config) -> Task* {
                           return new DeciderCreep(config);
                         });
94
  task_factory_.Register(TaskConfig::DECIDER_RULE_BASED_STOP,
95
                         [](const TaskConfig& config) -> Task* {
96
                           return new DeciderRuleBasedStop(config);
97
                         });
98 99 100 101
  task_factory_.Register(TaskConfig::SIDE_PASS_SAFETY,
                         [](const TaskConfig& config) -> Task* {
                           return new SidePassSafety(config);
                         });
102 103 104
  task_factory_.Register(
      TaskConfig::DECIDER_RSS,
      [](const TaskConfig& config) -> Task* { return new RssDecider(config); });
105 106 107 108
  for (const auto& default_task_config : config.default_task_config()) {
    default_task_configs_[default_task_config.task_type()] =
        default_task_config;
  }
D
Dong Li 已提交
109 110 111
}

std::unique_ptr<Task> TaskFactory::CreateTask(const TaskConfig& task_config) {
112 113 114 115 116 117 118
  TaskConfig merged_config;
  if (default_task_configs_.find(task_config.task_type()) !=
      default_task_configs_.end()) {
    merged_config = default_task_configs_[task_config.task_type()];
  }
  merged_config.MergeFrom(task_config);
  return task_factory_.CreateObject(task_config.task_type(), merged_config);
D
Dong Li 已提交
119 120 121 122
}

}  // namespace planning
}  // namespace apollo