trajectory_cost.cc 5.3 KB
Newer Older
Y
Yifei Jiang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/******************************************************************************
 * 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.
 *****************************************************************************/

/**
 * @file trjactory_cost.h
 **/

D
Dong Li 已提交
21 22
#include "modules/planning/optimizer/dp_poly_path/trajectory_cost.h"

J
jiangyifei 已提交
23
#include <algorithm>
Y
Yifei Jiang 已提交
24 25
#include <cmath>

26
#include "modules/common/math/vec2d.h"
Y
Yifei Jiang 已提交
27
#include "modules/common/proto/path_point.pb.h"
Z
Zhang Liangliang 已提交
28
#include "modules/planning/common/planning_gflags.h"
Y
Yifei Jiang 已提交
29 30 31 32

namespace apollo {
namespace planning {

Z
Zhang Liangliang 已提交
33
TrajectoryCost::TrajectoryCost(const DpPolyPathConfig &config,
34
                               const ReferenceLine &reference_line,
D
Dong Li 已提交
35
                               const common::VehicleParam &vehicle_param,
Z
Zhang Liangliang 已提交
36 37
                               const SpeedData &heuristic_speed_data,
                               const DecisionData &decision_data)
D
Dong Li 已提交
38
    : _config(config),
39
      _reference_line(&reference_line),
D
Dong Li 已提交
40 41
      _vehicle_param(vehicle_param),
      _heuristic_speed_data(heuristic_speed_data) {
Y
Yifei Jiang 已提交
42 43 44
  const auto &static_obstacles = decision_data.StaticObstacles();
  const double total_time =
      std::min(_heuristic_speed_data.total_time(), FLAGS_prediction_total_time);
D
Dong Li 已提交
45 46
  _evaluate_times = static_cast<uint32_t>(
      std::floor(total_time / config.eval_time_interval()));
Y
Yifei Jiang 已提交
47 48

  // Mapping Static obstacle
49
  for (uint32_t i = 0; i < static_obstacles.size(); ++i) {
Y
Yifei Jiang 已提交
50 51 52
    std::vector<::apollo::common::math::Box2d> obstacle_by_time;
    ::apollo::common::TrajectoryPoint traj_point =
        static_obstacles[i]->prediction_trajectories()[0].evaluate(0.0);
53 54
    ::apollo::common::math::Vec2d center_point = {traj_point.path_point().x(),
                                                  traj_point.path_point().y()};
Z
Zhang Liangliang 已提交
55 56 57 58
    ::apollo::common::math::Box2d obstacle_box = {
        center_point, traj_point.path_point().theta(),
        static_obstacles[i]->BoundingBox().length(),
        static_obstacles[i]->BoundingBox().width()};
59
    for (uint32_t j = 0; i <= _evaluate_times; ++j) {
Y
Yifei Jiang 已提交
60 61 62 63 64 65 66 67
      obstacle_by_time.push_back(obstacle_box);
    }
    _obstacle_trajectory.push_back(obstacle_by_time);
    _obstacle_probability.push_back(1.0);
  }

  // Mapping dynamic obstacle
  const auto &dynamic_obstacles = decision_data.DynamicObstacles();
68
  for (uint32_t i = 0; i < dynamic_obstacles.size(); ++i) {
Y
Yifei Jiang 已提交
69
    const auto &trajectories = dynamic_obstacles[i]->prediction_trajectories();
70
    for (uint32_t j = 0; j < trajectories.size(); ++j) {
Y
Yifei Jiang 已提交
71 72
      const auto &trajectory = trajectories[j];
      std::vector<::apollo::common::math::Box2d> obstacle_by_time;
73
      for (uint32_t time = 0; time <= _evaluate_times; ++time) {
Y
Yifei Jiang 已提交
74 75
        TrajectoryPoint traj_point =
            trajectory.evaluate(time * config.eval_time_interval());
76 77
        ::apollo::common::math::Vec2d center_point = {
            traj_point.path_point().x(), traj_point.path_point().y()};
Z
Zhang Liangliang 已提交
78 79 80 81
        ::apollo::common::math::Box2d obstacle_box = {
            center_point, traj_point.path_point().theta(),
            static_obstacles[i]->BoundingBox().length(),
            static_obstacles[i]->BoundingBox().width()};
Y
Yifei Jiang 已提交
82 83 84 85 86 87
        obstacle_by_time.push_back(obstacle_box);
      }
      _obstacle_trajectory.push_back(obstacle_by_time);
      _obstacle_probability.push_back(trajectory.probability());
    }
  }
Z
Zhang Liangliang 已提交
88
  // CHECK(_obstacle_probability.size() == _obstacle_trajectory.size());
Y
Yifei Jiang 已提交
89 90 91
}

double TrajectoryCost::calculate(const QuinticPolynomialCurve1d &curve,
92 93
                                 const double start_s,
                                 const double end_s) const {
94 95
  // const double length = _vehicle_param.length();
  // const double width = _vehicle_param.width();
Y
Yifei Jiang 已提交
96
  double total_cost = 0.0;
97
  for (uint32_t i = 0; i < _evaluate_times; ++i) {
Y
Yifei Jiang 已提交
98 99
    double eval_time = i * _config.eval_time_interval();
    SpeedPoint speed_point;
Z
Zhang Liangliang 已提交
100 101
    if (!_heuristic_speed_data.get_speed_point_with_time(eval_time,
                                                         &speed_point) ||
102
        start_s <= speed_point.s()) {
Y
Yifei Jiang 已提交
103 104
      continue;
    }
105
    if (speed_point.s() >= end_s) {
Y
Yifei Jiang 已提交
106 107
      break;
    }
108
    double l = std::fabs(curve.evaluate(0, speed_point.s() - start_s));
Z
Zhang Liangliang 已提交
109
    total_cost += l;  // need refine l cost;
Y
Yifei Jiang 已提交
110

111 112 113 114 115 116
    // ::apollo::common::math::Vec2d car_point = {speed_point.s(), l};
    // ReferencePoint reference_point =
    //     reference_line.get_reference_point(car_point.x());
    // ::apollo::common::math::Box2d car_box = {
    //     car_point, reference_point.heading(), length, width};
    // for (uint32_t j = 0; j < _obstacle_trajectory.size(); ++j) {
117 118
    //   ::apollo::common::math::Box2d obstacle_box =
    //   _obstacle_trajectory[j][i];
119 120 121
    //   double distance = car_box.DistanceTo(obstacle_box);
    //   total_cost += distance * _obstacle_probability[j];  // obstacle cost
    // }
Y
Yifei Jiang 已提交
122 123 124 125 126
  }
  return total_cost;
}

}  // namespace planning
Z
Zhang Liangliang 已提交
127
}  // namespace apollo