44 CHECK_NOTNULL(obstacle);
50 std::vector<JunctionExit> junction_exits =
51 MostLikelyJunctions(latest_feature);
52 for (
const auto& junction_exit : junction_exits) {
53 std::vector<TrajectoryPoint> trajectory_points;
54 DrawJunctionTrajectoryPoints(
55 latest_feature, junction_exit, FLAGS_prediction_trajectory_time_length,
56 FLAGS_prediction_trajectory_time_resolution, &trajectory_points);
64void JunctionPredictor::DrawJunctionTrajectoryPoints(
66 const double total_time,
const double period,
67 std::vector<TrajectoryPoint>* trajectory_points) {
68 double speed = feature.
speed();
69 const std::array<double, 2> start_x = {feature.
position().
x(),
71 const std::array<double, 2> end_x = {
74 const std::array<double, 2> start_y = {feature.
position().
y(),
76 const std::array<double, 2> end_y = {
79 double exit_time = GetBestTime(start_x, end_x, start_y, end_y);
80 std::array<double, 4> x_coeffs =
81 ComputePolynomial<3>(start_x, end_x, exit_time);
82 std::array<double, 4> y_coeffs =
83 ComputePolynomial<3>(start_y, end_y, exit_time);
86 while (t <= exit_time) {
90 path_point.set_z(0.0);
93 TrajectoryPoint trajectory_point;
94 trajectory_point.mutable_path_point()->CopyFrom(path_point);
97 trajectory_point.set_relative_time(t);
98 trajectory_points->emplace_back(std::move(trajectory_point));
110 while (t <= total_time) {
113 AERROR <<
"Unable to get smooth point from lane [" << lane_id
114 <<
"] with s [" << lane_s <<
"] and l [" << 0.0 <<
"]";
117 TrajectoryPoint trajectory_point;
118 PathPoint path_point;
119 path_point.set_x(pos.x());
120 path_point.set_y(pos.y());
121 path_point.set_z(0.0);
122 path_point.set_theta(theta);
123 path_point.set_lane_id(lane_id);
124 trajectory_point.mutable_path_point()->CopyFrom(path_point);
125 trajectory_point.set_v(speed);
126 trajectory_point.set_a(0.0);
127 trajectory_point.set_relative_time(t);
128 trajectory_points->emplace_back(std::move(trajectory_point));
129 lane_s += speed * period;
142std::vector<JunctionExit> JunctionPredictor::MostLikelyJunctions(
143 const Feature& feature) {
144 if (!feature.has_junction_feature()) {
145 AERROR <<
"No junction_feature exist!";
148 if (feature.junction_feature().junction_exit_size() < 1 ||
149 feature.junction_feature().junction_mlp_probability_size() != 12) {
150 AERROR <<
"No junction_exit"
151 <<
"or no enough junction_mlp_probability to process!";
155 double max_prob = 0.0;
156 for (
int i = 0; i < 12; ++i) {
157 if (feature.junction_feature().junction_mlp_probability(i) > max_prob) {
158 max_prob = feature.junction_feature().junction_mlp_probability(i);
162 std::vector<JunctionExit> junction_exits;
163 for (
const JunctionExit& junction_exit :
164 feature.junction_feature().junction_exit()) {
165 double x = junction_exit.
exit_position().
x() - feature.position().x();
166 double y = junction_exit.
exit_position().
y() - feature.position().y();
167 double angle = std::atan2(y, x) - std::atan2(feature.raw_velocity().y(),
168 feature.raw_velocity().x());
169 double d_idx = (angle / (2.0 * M_PI)) * 12.0;
170 int idx =
static_cast<int>(floor(d_idx >= 0 ? d_idx : d_idx + 12));
171 if (idx == max_idx) {
172 junction_exits.push_back(junction_exit);
175 return junction_exits;
178double JunctionPredictor::GetBestTime(
const std::array<double, 2>& start_x,
179 const std::array<double, 2>& end_x,
180 const std::array<double, 2>& start_y,
181 const std::array<double, 2>& end_y) {
183 std::vector<double> candidate_times = GenerateCandidateTimes();
184 double best_time = 0.0;
185 double best_cost = std::numeric_limits<double>::infinity();
186 for (
size_t i = 0; i < candidate_times.size(); ++i) {
187 double time_to_exit = candidate_times[i];
188 std::array<double, 4> x_coeffs =
189 ComputePolynomial<3>(start_x, end_x, time_to_exit);
190 std::array<double, 4> y_coeffs =
191 ComputePolynomial<3>(start_y, end_y, time_to_exit);
192 double cost_of_trajectory = CostFunction(x_coeffs, y_coeffs, time_to_exit);
193 if (cost_of_trajectory <= best_cost) {
194 best_cost = cost_of_trajectory;
195 best_time = time_to_exit;
201double JunctionPredictor::CostFunction(
const std::array<double, 4>& x_coeffs,
202 const std::array<double, 4>& y_coeffs,
203 const double time_to_exit) {
206 while (t <= time_to_exit) {
213 std::max(cost, std::abs(x_1 * y_2 - y_1 * x_2) / std::hypot(x_1, y_1) +
215 t += FLAGS_prediction_trajectory_time_resolution;
220std::vector<double> JunctionPredictor::GenerateCandidateTimes() {
221 std::vector<double> candidate_times;
223 double time_gap = 0.5;
224 while (t <= FLAGS_prediction_trajectory_time_length) {
225 candidate_times.push_back(t);
228 return candidate_times;
JunctionPredictor()
Constructor
bool Predict(const ADCTrajectoryContainer *adc_trajectory_container, Obstacle *obstacle, ObstaclesContainer *obstacles_container) override
Make prediction
Feature * mutable_latest_feature()
Get a pointer to the latest feature.
size_t history_size() const
Get the number of historical features.
void SetPredictorType(const ObstacleConf::PredictorType &predictor_type)
const Feature & latest_feature() const
Get the latest feature.
static bool SmoothPointFromLane(const std::string &id, const double s, const double l, Eigen::Vector2d *point, double *heading)
Get the smooth point on a lane by a longitudinal coordinate.
static std::shared_ptr< const hdmap::LaneInfo > LaneById(const std::string &id)
Get a shared pointer to a lane by lane ID.
static bool GetProjection(const Eigen::Vector2d &position, const std::shared_ptr< const hdmap::LaneInfo > lane_info, double *s, double *l)
Get the frenet coordinates (s, l) on a lane by a position.
static Trajectory GenerateTrajectory(const std::vector< apollo::common::TrajectoryPoint > &points)
Generate trajectory from trajectory points
ObstacleConf::PredictorType predictor_type_
virtual void Clear()
Clear all trajectories
Define junction predictor
double EvaluateCubicPolynomial(const std::array< double, 4 > &coefs, const double t, const uint32_t order, const double end_t, const double end_v)
Evaluate cubic polynomial.
std::array< double, 2 *N - 2 > ComputePolynomial(const std::array< double, N - 1 > &start_state, const std::array< double, N - 1 > &end_state, const double param)
optional apollo::common::Point3D raw_velocity
optional apollo::common::Point3D position
optional double exit_heading
optional apollo::common::Point3D exit_position
optional string exit_lane_id