33constexpr double kInf = std::numeric_limits<double>::infinity();
38 const std::vector<const Obstacle*>& obstacles,
42 obstacles_(obstacles),
43 st_drivable_boundary_(st_drivable_boundary),
44 init_point_(init_point),
45 unit_t_(config.unit_t()),
48 for (
const auto& obstacle : obstacles) {
49 boundary_map_[obstacle->path_st_boundary().id()] = index++;
52 AddToKeepClearRange(obstacles);
54 const auto dimension_t =
55 static_cast<uint32_t
>(std::ceil(total_t /
static_cast<double>(unit_t_))) +
57 boundary_cost_.resize(obstacles_.size());
58 for (
auto& vec : boundary_cost_) {
59 vec.resize(dimension_t, std::make_pair(-1.0, -1.0));
61 accel_cost_.fill(-1.0);
62 jerk_cost_.fill(-1.0);
65void DpStCost::AddToKeepClearRange(
66 const std::vector<const Obstacle*>& obstacles) {
67 for (
const auto& obstacle : obstacles) {
68 if (obstacle->path_st_boundary().IsEmpty()) {
71 if (obstacle->path_st_boundary().boundary_type() !=
76 double start_s = obstacle->path_st_boundary().min_s();
77 double end_s = obstacle->path_st_boundary().max_s();
78 keep_clear_range_.emplace_back(start_s, end_s);
80 SortAndMergeRange(&keep_clear_range_);
83void DpStCost::SortAndMergeRange(
84 std::vector<std::pair<double, double>>* keep_clear_range) {
85 if (!keep_clear_range || keep_clear_range->empty()) {
88 std::sort(keep_clear_range->begin(), keep_clear_range->end());
91 while (j < keep_clear_range->size()) {
92 if (keep_clear_range->at(i).second < keep_clear_range->at(j).first) {
96 keep_clear_range->at(i).second = std::max(keep_clear_range->at(i).second,
97 keep_clear_range->at(j).second);
101 keep_clear_range->resize(i + 1);
104bool DpStCost::InKeepClearRange(
double s)
const {
105 for (
const auto& p : keep_clear_range_) {
106 if (p.first <= s && p.second >= s) {
114 const double s = st_graph_point.
point().
s();
115 const double t = st_graph_point.
point().
t();
119 if (FLAGS_use_st_drivable_boundary) {
121 static constexpr double boundary_resolution = 0.1;
122 int index =
static_cast<int>(t / boundary_resolution);
123 const double lower_bound =
125 const double upper_bound =
128 if (s > upper_bound || s < lower_bound) {
133 for (
const auto* obstacle : obstacles_) {
136 if (obstacle->IsVirtual()) {
143 if (obstacle->LongitudinalDecision().has_stop()) {
147 auto boundary = obstacle->path_st_boundary();
149 if (boundary.min_s() > FLAGS_speed_lon_decision_horizon) {
152 if (t < boundary.min_t() || t > boundary.max_t()) {
155 if (boundary.IsPointInBoundary(st_graph_point.
point())) {
158 double s_upper = 0.0;
159 double s_lower = 0.0;
161 int boundary_index = boundary_map_[boundary.id()];
162 if (boundary_cost_[boundary_index][st_graph_point.
index_t()].first < 0.0) {
163 boundary.GetBoundarySRange(t, &s_upper, &s_lower);
164 boundary_cost_[boundary_index][st_graph_point.
index_t()] =
165 std::make_pair(s_upper, s_lower);
167 s_upper = boundary_cost_[boundary_index][st_graph_point.
index_t()].first;
168 s_lower = boundary_cost_[boundary_index][st_graph_point.
index_t()].second;
172 if (s + follow_distance_s < s_lower) {
175 auto s_diff = follow_distance_s - s_lower + s;
179 }
else if (s > s_upper) {
180 const double overtake_distance_s =
182 if (s > s_upper + overtake_distance_s) {
185 auto s_diff = overtake_distance_s + s_upper - s;
191 return cost * unit_t_;
199 const STPoint& reference_point)
const {
201 (point.
s() - reference_point.
s()) * unit_t_;
205 const double speed_limit,
206 const double cruise_speed)
const {
208 const double speed = (second.
s() - first.
s()) / unit_t_;
213 const double max_adc_stop_speed = common::VehicleConfigHelper::Instance()
216 .max_abs_speed_when_stopped();
217 if (speed < max_adc_stop_speed && InKeepClearRange(second.
s())) {
223 double det_speed = (speed - speed_limit) / speed_limit;
226 (det_speed * det_speed) * unit_t_;
227 }
else if (det_speed < 0) {
229 -det_speed * unit_t_;
233 double diff_speed = speed - cruise_speed;
235 fabs(diff_speed) * unit_t_;
241double DpStCost::GetAccelCost(
const double accel) {
243 static constexpr double kEpsilon = 0.1;
244 static constexpr size_t kShift = 100;
245 const size_t accel_key =
static_cast<size_t>(accel / kEpsilon + 0.5 + kShift);
246 DCHECK_LT(accel_key, accel_cost_.size());
247 if (accel_key >= accel_cost_.size()) {
251 if (accel_cost_.at(accel_key) < 0.0) {
252 const double accel_sq = accel * accel;
259 cost = accel_penalty * accel_sq;
261 cost = decel_penalty * accel_sq;
263 cost += accel_sq * decel_penalty * decel_penalty /
264 (1 + std::exp(1.0 * (accel - max_dec))) +
265 accel_sq * accel_penalty * accel_penalty /
266 (1 + std::exp(-1.0 * (accel - max_acc)));
267 accel_cost_.at(accel_key) = cost;
269 cost = accel_cost_.at(accel_key);
271 return cost * unit_t_;
277 double accel = (first.
s() + third.
s() - 2 * second.
s()) / (unit_t_ * unit_t_);
278 return GetAccelCost(accel);
284 double current_speed = (curr_point.
s() - pre_point.
s()) / unit_t_;
285 double accel = (current_speed - pre_speed) / unit_t_;
286 return GetAccelCost(accel);
289double DpStCost::JerkCost(
const double jerk) {
291 static constexpr double kEpsilon = 0.1;
292 static constexpr size_t kShift = 200;
293 const size_t jerk_key =
static_cast<size_t>(jerk / kEpsilon + 0.5 + kShift);
294 if (jerk_key >= jerk_cost_.size()) {
298 if (jerk_cost_.at(jerk_key) < 0.0) {
299 double jerk_sq = jerk * jerk;
305 jerk_cost_.at(jerk_key) = cost;
307 cost = jerk_cost_.at(jerk_key);
318 double jerk = (fourth.
s() - 3 * third.
s() + 3 * second.
s() - first.
s()) /
319 (unit_t_ * unit_t_ * unit_t_);
320 return JerkCost(jerk);
324 const double pre_acc,
327 const double curr_speed = (curr_point.
s() - pre_point.
s()) / unit_t_;
328 const double curr_accel = (curr_speed - pre_speed) / unit_t_;
329 const double jerk = (curr_accel - pre_acc) / unit_t_;
330 return JerkCost(jerk);
337 const double pre_speed = (second.
s() - first.
s()) / unit_t_;
338 const double pre_acc = (pre_speed - first_speed) / unit_t_;
339 const double curr_speed = (third.
s() - second.
s()) / unit_t_;
340 const double curr_acc = (curr_speed - pre_speed) / unit_t_;
341 const double jerk = (curr_acc - pre_acc) / unit_t_;
342 return JerkCost(jerk);
double GetSpatialPotentialCost(const StGraphPoint &point)
double GetAccelCostByTwoPoints(const double pre_speed, const STPoint &first, const STPoint &second)
DpStCost(const DpStSpeedOptimizerConfig &config, const double total_t, const double total_s, const std::vector< const Obstacle * > &obstacles, const STDrivableBoundary &st_drivable_boundary, const common::TrajectoryPoint &init_point)
double GetJerkCostByThreePoints(const double first_speed, const STPoint &first_point, const STPoint &second_point, const STPoint &third_point)
double GetJerkCostByTwoPoints(const double pre_speed, const double pre_acc, const STPoint &pre_point, const STPoint &curr_point)
double GetReferenceCost(const STPoint &point, const STPoint &reference_point) const
double GetSpeedCost(const STPoint &first, const STPoint &second, const double speed_limit, const double cruise_speed) const
double GetJerkCostByFourPoints(const STPoint &first, const STPoint &second, const STPoint &third, const STPoint &fourth)
double GetAccelCostByThreePoints(const STPoint &first, const STPoint &second, const STPoint &third)
double GetObstacleCost(const StGraphPoint &point)
static double EstimateSafeOvertakingGap()
std::uint32_t index_t() const
const STPoint & point() const
Planning module main class.
Defines proper safety distance the ADC should keep from target obstacle considering different informa...
optional double default_speed_cost
optional double positive_jerk_coeff
optional double keep_clear_low_speed_penalty
optional double spatial_potential_penalty
optional double reference_speed_penalty
optional double max_deceleration
optional double exceed_speed_penalty
optional double max_acceleration
optional double accel_penalty
optional double safe_distance
optional double negative_jerk_coeff
optional double low_speed_penalty
optional double reference_weight
optional double decel_penalty
optional bool enable_dp_reference_speed
optional double obstacle_weight
optional double default_obstacle_cost
repeated STDrivableBoundaryInstance st_boundary