trajectory_cost.cc 5.0 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
 **/

J
jiangyifei 已提交
21
#include <algorithm>
Y
Yifei Jiang 已提交
22 23
#include <cmath>

J
jiangyifei 已提交
24
#include "modules/planning/optimizer/dp_poly_path/trajectory_cost.h"
25
#include "modules/common/math/vec2d.h"
Y
Yifei Jiang 已提交
26
#include "modules/common/proto/path_point.pb.h"
Z
Zhang Liangliang 已提交
27
#include "modules/planning/common/planning_gflags.h"
Y
Yifei Jiang 已提交
28 29 30 31

namespace apollo {
namespace planning {

Z
Zhang Liangliang 已提交
32 33 34 35
TrajectoryCost::TrajectoryCost(const DpPolyPathConfig &config,
                               const SpeedData &heuristic_speed_data,
                               const DecisionData &decision_data)
    : _config(config), _heuristic_speed_data(heuristic_speed_data) {
Y
Yifei Jiang 已提交
36 37 38
  const auto &static_obstacles = decision_data.StaticObstacles();
  const double total_time =
      std::min(_heuristic_speed_data.total_time(), FLAGS_prediction_total_time);
Z
Zhang Liangliang 已提交
39
  _evaluate_times =
40
      static_cast<uint32_t>(std::floor(total_time / config.eval_time_interval()));
Y
Yifei Jiang 已提交
41 42

  // Mapping Static obstacle
43
  for (uint32_t i = 0; i < static_obstacles.size(); ++i) {
Y
Yifei Jiang 已提交
44 45 46
    std::vector<::apollo::common::math::Box2d> obstacle_by_time;
    ::apollo::common::TrajectoryPoint traj_point =
        static_obstacles[i]->prediction_trajectories()[0].evaluate(0.0);
47 48
    ::apollo::common::math::Vec2d center_point = {traj_point.path_point().x(),
                                                  traj_point.path_point().y()};
Z
Zhang Liangliang 已提交
49 50 51 52
    ::apollo::common::math::Box2d obstacle_box = {
        center_point, traj_point.path_point().theta(),
        static_obstacles[i]->BoundingBox().length(),
        static_obstacles[i]->BoundingBox().width()};
53
    for (uint32_t j = 0; i <= _evaluate_times; ++j) {
Y
Yifei Jiang 已提交
54 55 56 57 58 59 60 61
      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();
62
  for (uint32_t i = 0; i < dynamic_obstacles.size(); ++i) {
Y
Yifei Jiang 已提交
63
    const auto &trajectories = dynamic_obstacles[i]->prediction_trajectories();
64
    for (uint32_t j = 0; j < trajectories.size(); ++j) {
Y
Yifei Jiang 已提交
65 66
      const auto &trajectory = trajectories[j];
      std::vector<::apollo::common::math::Box2d> obstacle_by_time;
67
      for (uint32_t time = 0; time <= _evaluate_times; ++time) {
Y
Yifei Jiang 已提交
68 69
        TrajectoryPoint traj_point =
            trajectory.evaluate(time * config.eval_time_interval());
70 71
        ::apollo::common::math::Vec2d center_point = {
            traj_point.path_point().x(), traj_point.path_point().y()};
Z
Zhang Liangliang 已提交
72 73 74 75
        ::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 已提交
76 77 78 79 80 81
        obstacle_by_time.push_back(obstacle_box);
      }
      _obstacle_trajectory.push_back(obstacle_by_time);
      _obstacle_probability.push_back(trajectory.probability());
    }
  }
Z
Zhang Liangliang 已提交
82
  // CHECK(_obstacle_probability.size() == _obstacle_trajectory.size());
Y
Yifei Jiang 已提交
83 84 85 86 87 88 89
}

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

104
    ::apollo::common::math::Vec2d car_point = {speed_point.s(), l};
Z
Zhang Liangliang 已提交
105 106 107 108
    ReferencePoint reference_point =
        reference_line.get_reference_point(car_point.x());
    ::apollo::common::math::Box2d car_box = {
        car_point, reference_point.heading(), length, width};
109
    for (uint32_t j = 0; j < _obstacle_trajectory.size(); ++j) {
Y
Yifei Jiang 已提交
110 111
      ::apollo::common::math::Box2d obstacle_box = _obstacle_trajectory[j][i];
      double distance = car_box.DistanceTo(obstacle_box);
Z
Zhang Liangliang 已提交
112
      total_cost += distance * _obstacle_probability[j];  // obstacle cost
Y
Yifei Jiang 已提交
113 114 115 116 117 118
    }
  }
  return total_cost;
}

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