apollo icon indicating copy to clipboard operation
apollo copied to clipboard

Boundary constraints in QpSplinePathGenerator inconsistent

Open berthu opened this issue 8 years ago • 1 comments

(Apollo 2.0) The boundary constraints implemented in QpSplinePathGenerator::AddConstraint seem inconsistent with constraints demanded by the QP path optimization problem using splines, as stated in qp_spline_path_optimizer.md. In particular:

  • End point constraints do not call method AddPointSecondDerivativeConstraintInRange to add a second derivative constraint
  • The method includes calls to first, second, and third derivative boundary constraints ("kappa" boundaries) not referenced in qp_spline_path_optimizer.md. (This is probably fine, but the documentation implies that the boundary constraints are primarily concerned with treating obstacle boundaries)
  • Joint smoothness constraints between splines only smooths the third derivative, and do not call methods AddSmoothConstraint, AddDerivativeSmoothConstraint, and AddSecondDerivativeSmoothConstraint

Is QpSplinePathGenerator implemented this way on purpose, or is the QP problem not fully implemented? Thank you.

bool QpSplinePathGenerator::AddConstraint(const QpFrenetFrame& qp_frenet_frame,
                                          const double boundary_extension) {
  Spline1dConstraint* spline_constraint =
      spline_generator_->mutable_spline_constraint();

  const int dim =
      (knots_.size() - 1) * (qp_spline_path_config_.spline_order() + 1);
  constexpr double param_range = 1e-4;
  for (int i = qp_spline_path_config_.spline_order(); i < dim;
       i += qp_spline_path_config_.spline_order() + 1) {
    Eigen::MatrixXd mat = Eigen::MatrixXd::Zero(1, dim);
    Eigen::MatrixXd bd = Eigen::MatrixXd::Zero(1, 1);
    mat(0, i) = -1;
    bd(0, 0) = -param_range;
    spline_constraint->AddInequalityConstraint(mat, bd);
    mat(0, i) = 1;
    bd(0, 0) = -param_range;
    spline_constraint->AddInequalityConstraint(mat, bd);
  }

  // add init status constraint, equality constraint
  const double kBoundaryEpsilon = 1e-4;
  spline_constraint->AddPointConstraintInRange(init_frenet_point_.s(),
                                               init_frenet_point_.l() - ref_l_,
                                               kBoundaryEpsilon);
  spline_constraint->AddPointDerivativeConstraintInRange(
      init_frenet_point_.s(), init_frenet_point_.dl(), kBoundaryEpsilon);

  if (init_trajectory_point_.v() > qp_spline_path_config_.uturn_speed_limit()) {
    spline_constraint->AddPointSecondDerivativeConstraintInRange(
        init_frenet_point_.s(), init_frenet_point_.ddl(), kBoundaryEpsilon);
  }

  ADEBUG << "init frenet point: " << init_frenet_point_.ShortDebugString();

  // add end point constraint, equality constraint
  double lat_shift = -ref_l_;
  if (is_change_lane_path_) {
    double lane_change_lateral_shift =
        GetLaneChangeLateralShift(init_trajectory_point_.v());
    lat_shift = std::copysign(
        std::fmin(std::fabs(ref_l_), lane_change_lateral_shift), -ref_l_);
  }

  ADEBUG << "lat_shift = " << lat_shift;
  const double kEndPointBoundaryEpsilon = 1e-2;
  constexpr double kReservedDistance = 20.0;
  const double target_s =
      std::fmin(qp_spline_path_config_.point_constraint_s_position(),
                kReservedDistance + init_frenet_point_.s() +
                    init_trajectory_point_.v() * FLAGS_look_forward_time_sec);
  spline_constraint->AddPointConstraintInRange(target_s, lat_shift,
                                               kEndPointBoundaryEpsilon);
  if (!is_change_lane_path_) {
    spline_constraint->AddPointDerivativeConstraintInRange(
        evaluated_s_.back(), 0.0, kEndPointBoundaryEpsilon);
  }

  // add first derivative bound to improve lane change smoothness
  std::vector<double> dl_lower_bound(evaluated_s_.size(), -FLAGS_dl_bound);
  std::vector<double> dl_upper_bound(evaluated_s_.size(), FLAGS_dl_bound);

  if (!spline_constraint->AddDerivativeBoundary(evaluated_s_, dl_lower_bound,
                                                dl_upper_bound)) {
    AERROR << "Fail to add second derivative boundary.";
    return false;
  }

  // kappa bound is based on the inequality:
  // kappa = d(phi)/ds <= d(phi)/dx = d2y/dx2
  std::vector<double> kappa_lower_bound(evaluated_s_.size(),
                                        -FLAGS_kappa_bound);
  std::vector<double> kappa_upper_bound(evaluated_s_.size(), FLAGS_kappa_bound);
  if (!spline_constraint->AddSecondDerivativeBoundary(
          evaluated_s_, kappa_lower_bound, kappa_upper_bound)) {
    AERROR << "Fail to add second derivative boundary.";
    return false;
  }

  // dkappa = d(kappa) / ds <= d3y/dx3
  std::vector<double> dkappa_lower_bound(evaluated_s_.size(),
                                         -FLAGS_dkappa_bound);
  std::vector<double> dkappa_upper_bound(evaluated_s_.size(),
                                         FLAGS_dkappa_bound);
  if (!spline_constraint->AddThirdDerivativeBoundary(
          evaluated_s_, dkappa_lower_bound, dkappa_upper_bound)) {
    AERROR << "Fail to add third derivative boundary.";
    return false;
  }

  // add map bound constraint
  double lateral_buf = boundary_extension;
  if (is_change_lane_path_) {
    lateral_buf = qp_spline_path_config_.cross_lane_lateral_extension();
  }
  std::vector<double> boundary_low;
  std::vector<double> boundary_high;

  for (uint32_t i = 0; i < evaluated_s_.size(); ++i) {
    auto road_boundary = qp_frenet_frame.GetMapBound().at(i);
    auto static_obs_boundary = qp_frenet_frame.GetStaticObstacleBound().at(i);
    auto dynamic_obs_boundary = qp_frenet_frame.GetDynamicObstacleBound().at(i);

    if (evaluated_s_.at(i) - evaluated_s_.front() <
        qp_spline_path_config_.cross_lane_longitudinal_extension()) {
      road_boundary.first =
          std::fmin(road_boundary.first, init_frenet_point_.l() - lateral_buf);
      road_boundary.second =
          std::fmax(road_boundary.second, init_frenet_point_.l() + lateral_buf);
    }
    boundary_low.emplace_back(common::util::MaxElement(
        std::vector<double>{road_boundary.first, static_obs_boundary.first,
                            dynamic_obs_boundary.first}));
    boundary_high.emplace_back(common::util::MinElement(
        std::vector<double>{road_boundary.second, static_obs_boundary.second,
                            dynamic_obs_boundary.second}));
    ADEBUG << "s[" << evaluated_s_[i] << "] boundary_low["
           << boundary_low.back() << "] boundary_high[" << boundary_high.back()
           << "] road_boundary_low[" << road_boundary.first
           << "] road_boundary_high[" << road_boundary.second
           << "] static_obs_boundary_low[" << static_obs_boundary.first
           << "] static_obs_boundary_high[" << static_obs_boundary.second
           << "] dynamic_obs_boundary_low[" << dynamic_obs_boundary.first
           << "] dynamic_obs_boundary_high[" << dynamic_obs_boundary.second
           << "].";
  }

  if (planning_debug_) {
    apollo::planning_internal::SLFrameDebug* sl_frame =
        planning_debug_->mutable_planning_data()->mutable_sl_frame()->Add();
    for (size_t i = 0; i < evaluated_s_.size(); ++i) {
      sl_frame->mutable_aggregated_boundary_s()->Add(evaluated_s_[i]);
      sl_frame->mutable_aggregated_boundary_low()->Add(boundary_low[i]);
      sl_frame->mutable_aggregated_boundary_high()->Add(boundary_high[i]);
    }
  }

  const double start_l = ref_l_;
  std::for_each(boundary_low.begin(), boundary_low.end(),
                [start_l](double& d) { d -= start_l; });
  std::for_each(boundary_high.begin(), boundary_high.end(),
                [start_l](double& d) { d -= start_l; });
  if (!spline_constraint->AddBoundary(evaluated_s_, boundary_low,
                                      boundary_high)) {
    AERROR << "Add boundary constraint failed";
    return false;
  }

  // add spline joint third derivative constraint
  if (knots_.size() >= 3 &&
      !spline_constraint->AddThirdDerivativeSmoothConstraint()) {
    AERROR << "Add spline joint derivative constraint failed!";
    return false;
  }
  return true;
}

berthu avatar Mar 05 '18 02:03 berthu

There were some updates after the releasing of Apollo 1.5, so you will see differences between the documents and real implementations.

In Apollo 2.0, the QpSplinePathGenerator is no longer used (see the config file).

lianglia-apollo avatar Mar 05 '18 05:03 lianglia-apollo