dp_st_speed_optimizer.cc 4.1 KB
Newer Older
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.
 *****************************************************************************/

/**
18
 * @file dp_st_speed_optimizer.cc
19 20
 **/

D
Dong Li 已提交
21
#include "modules/planning/tasks/dp_st_speed/dp_st_speed_optimizer.h"
22

A
Aaron Xiao 已提交
23
#include <algorithm>
24
#include <vector>
25

26
#include "modules/common/adapters/adapter_manager.h"
27
#include "modules/common/configs/vehicle_config_helper.h"
L
lianglia-apollo 已提交
28
#include "modules/common/vehicle_state/vehicle_state.h"
29
#include "modules/planning/common/planning_gflags.h"
D
Dong Li 已提交
30 31
#include "modules/planning/tasks/dp_st_speed/dp_st_graph.h"
#include "modules/planning/tasks/st_graph/st_graph_data.h"
32 33 34 35

namespace apollo {
namespace planning {

36 37
using apollo::common::ErrorCode;
using apollo::common::Status;
D
Dong Li 已提交
38
using apollo::common::VehicleConfigHelper;
Y
Yajia Zhang 已提交
39 40
using apollo::common::adapter::AdapterManager;
using apollo::localization::LocalizationEstimate;
41 42

DpStSpeedOptimizer::DpStSpeedOptimizer(const std::string& name)
43 44
    : SpeedOptimizer(name) {}

45
bool DpStSpeedOptimizer::Init(const PlanningConfig& config) {
46 47
  dp_st_speed_config_ = config.em_planner_config().dp_st_speed_config();
  st_boundary_config_ = dp_st_speed_config_.st_boundary_config();
48
  is_init_ = true;
49 50 51
  return true;
}

52
Status DpStSpeedOptimizer::Process(const PathData& path_data,
D
Dong Li 已提交
53
                                   const common::TrajectoryPoint& init_point,
54
                                   const ReferenceLine& reference_line,
55
                                   PathDecision* const path_decision,
56
                                   SpeedData* const speed_data) {
57 58 59 60
  if (!is_init_) {
    AERROR << "Please call Init() before process DpStSpeedOptimizer.";
    return Status(ErrorCode::PLANNING_ERROR, "Not inited.");
  }
61

62 63 64 65 66 67
  if (path_data.discretized_path().NumOfPoints() == 0) {
    std::string msg("Empty path data");
    AERROR << msg;
    return Status(ErrorCode::PLANNING_ERROR, msg);
  }

68
  StBoundaryMapper boundary_mapper(st_boundary_config_, reference_line,
69
                                   path_data,
70 71
                                   dp_st_speed_config_.total_path_length(),
                                   dp_st_speed_config_.total_time());
D
Dong Li 已提交
72

73
  // step 1 get boundaries
74
  std::vector<StGraphBoundary> boundaries;
75 76
  if (boundary_mapper.GetGraphBoundary(*path_decision, &boundaries).code() ==
      ErrorCode::PLANNING_ERROR) {
77 78 79
    const std::string msg =
        "Mapping obstacle for dp st speed optimizer failed.";
    AERROR << msg;
80
    return Status(ErrorCode::PLANNING_ERROR, msg);
81
  }
82

83
  // step 2 perform graph search
84
  SpeedLimit speed_limit;
85
  if (!boundary_mapper.GetSpeedLimits(&speed_limit).ok()) {
86 87
    const std::string msg =
        "Getting speed limits for dp st speed optimizer failed!";
88 89
    AERROR << msg;
    return Status(ErrorCode::PLANNING_ERROR, msg);
90 91
  }

92
  const double path_length = path_data.discretized_path().Length();
93
  StGraphData st_graph_data(boundaries, init_point, speed_limit, path_length);
94 95
  const auto& veh_param =
      common::VehicleConfigHelper::GetConfig().vehicle_param();
96 97 98

  DpStGraph st_graph(dp_st_speed_config_, st_graph_data, veh_param, path_data);
  if (!st_graph.Search(path_decision, speed_data).ok()) {
99 100
    const std::string msg = "Failed to search graph with dynamic programming.";
    AERROR << msg;
101
    RecordSTGraphDebug(boundaries, speed_limit, *speed_data);
102
    return Status(ErrorCode::PLANNING_ERROR, msg);
103
  }
104 105 106

  RecordSTGraphDebug(boundaries, speed_limit, *speed_data);

107
  return Status::OK();
108 109 110 111
}

}  // namespace planning
}  // namespace apollo