Apollo 11.0
自动驾驶开放平台
trajectory_fallback_task.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2023 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
18
19#include <utility>
20#include <vector>
21
24
25namespace apollo {
26namespace planning {
27
29
30constexpr double kPathOptimizationFallbackCost = 2e4;
31constexpr double kSpeedOptimizationFallbackCost = 2e4;
32
34
36 Frame* frame, ReferenceLineInfo* reference_line_info) {
37 Task::Execute(frame, reference_line_info);
38 // Generate fallback path.
39 GenerateFallbackPath(frame, reference_line_info);
40
41 if (reference_line_info->speed_data().empty()) {
42 AERROR << "Speed fallback due to algorithm failure";
43 *reference_line_info->mutable_speed_data() = GenerateFallbackSpeed(
44 injector_->ego_info(), FLAGS_speed_fallback_distance);
45 AmendSpeedDataForControl(reference_line_info->mutable_speed_data());
46 }
47
48 if (reference_line_info->trajectory_type() != ADCTrajectory::PATH_FALLBACK) {
49 reference_line_info->AddCost(kSpeedOptimizationFallbackCost);
51 }
52
53 return Status::OK();
54}
55
56void TrajectoryFallbackTask::GenerateFallbackPath(
57 Frame* frame, ReferenceLineInfo* reference_line_info) {
58 // path and speed fall back
59 if (reference_line_info->path_data().Empty()) {
60 AERROR << "Path fallback due to algorithm failure";
61 GenerateFallbackPathProfile(reference_line_info,
62 reference_line_info->mutable_path_data());
63 reference_line_info->AddCost(kPathOptimizationFallbackCost);
65 // Clear speed data when path is re-generated.
66 reference_line_info->mutable_speed_data()->clear();
67 }
68
69 if (reference_line_info->trajectory_type() != ADCTrajectory::PATH_FALLBACK) {
70 if (!RetrieveLastFramePathProfile(
71 reference_line_info, frame,
72 reference_line_info->mutable_path_data())) {
73 const auto& candidate_path_data =
74 reference_line_info->GetCandidatePathData();
75 for (const auto& path_data : candidate_path_data) {
76 if (path_data.path_label().find("self") != std::string::npos) {
77 *reference_line_info->mutable_path_data() = path_data;
78 AERROR << "Use current frame self lane path as fallback ";
79 break;
80 }
81 }
82 }
83 }
84}
85
86void TrajectoryFallbackTask::GenerateFallbackPathProfile(
87 const ReferenceLineInfo* reference_line_info, PathData* path_data) {
88 const double unit_s = 1.0;
89 const auto& reference_line = reference_line_info->reference_line();
90
91 auto adc_point = injector_->ego_info()->start_point();
92 DCHECK(adc_point.has_path_point());
93 const auto adc_point_x = adc_point.path_point().x();
94 const auto adc_point_y = adc_point.path_point().y();
95
96 common::SLPoint adc_point_s_l;
97 if (!reference_line.XYToSL(adc_point.path_point(), &adc_point_s_l)) {
98 AERROR << "Fail to project ADC to reference line when calculating path "
99 "fallback. Straight forward path is generated";
100 const auto adc_point_heading = adc_point.path_point().theta();
101 const auto adc_point_kappa = adc_point.path_point().kappa();
102 const auto adc_point_dkappa = adc_point.path_point().dkappa();
103 std::vector<common::PathPoint> path_points;
104 double adc_traversed_x = adc_point_x;
105 double adc_traversed_y = adc_point_y;
106
107 const double max_s = 100.0;
108 for (double s = 0; s < max_s; s += unit_s) {
109 path_points.push_back(PointFactory::ToPathPoint(
110 adc_traversed_x, adc_traversed_y, 0.0, s, adc_point_heading,
111 adc_point_kappa, adc_point_dkappa));
112 adc_traversed_x += unit_s * std::cos(adc_point_heading);
113 adc_traversed_y += unit_s * std::sin(adc_point_heading);
114 }
115 path_data->SetDiscretizedPath(DiscretizedPath(std::move(path_points)));
116 return;
117 }
118
119 // Generate a fallback path along the reference line direction
120 const auto adc_s = adc_point_s_l.s();
121 const auto& adc_ref_point =
122 reference_line.GetReferencePoint(adc_point_x, adc_point_y);
123 const double dx = adc_point_x - adc_ref_point.x();
124 const double dy = adc_point_y - adc_ref_point.y();
125
126 std::vector<common::PathPoint> path_points;
127 const double max_s = reference_line.Length();
128 for (double s = adc_s; s < max_s; s += unit_s) {
129 const auto& ref_point = reference_line.GetReferencePoint(s);
130 path_points.push_back(PointFactory::ToPathPoint(
131 ref_point.x() + dx, ref_point.y() + dy, 0.0, s - adc_s,
132 ref_point.heading(), ref_point.kappa(), ref_point.dkappa()));
133 }
134 path_data->SetDiscretizedPath(DiscretizedPath(std::move(path_points)));
135}
136
137bool TrajectoryFallbackTask::RetrieveLastFramePathProfile(
138 const ReferenceLineInfo* reference_line_info, const Frame* frame,
139 PathData* path_data) {
140 const auto* ptr_last_frame = injector_->frame_history()->Latest();
141 if (ptr_last_frame == nullptr) {
142 AERROR
143 << "Last frame doesn't succeed, fail to retrieve last frame path data";
144 return false;
145 }
146
147 const auto& last_frame_discretized_path =
148 ptr_last_frame->current_frame_planned_path();
149
150 path_data->SetDiscretizedPath(last_frame_discretized_path);
151 const auto adc_frenet_frame_point_ =
152 reference_line_info->reference_line().GetFrenetPoint(
153 frame->PlanningStartPoint().path_point());
154
155 bool trim_success = path_data->LeftTrimWithRefS(adc_frenet_frame_point_);
156 if (!trim_success) {
157 AERROR << "Fail to trim path_data. adc_frenet_frame_point: "
158 << adc_frenet_frame_point_.ShortDebugString();
159 return false;
160 }
161 AERROR << "Use last frame good path to do speed fallback";
162 return true;
163}
164
165void TrajectoryFallbackTask::AmendSpeedDataForControl(
166 SpeedData* speed_data_ptr) {
167 // Iterate the speed data with reverse order to modify the deceleration before
168 // stopping. If the value of deceleration is not big enough, control module
169 // may not stop the vehicle in time.
170 SpeedData& speed_data = *speed_data_ptr;
171 const int speed_data_num = speed_data.size();
172 int modify_index = speed_data_num - 1;
173 for (; modify_index >= 0; --modify_index) {
174 auto& data = speed_data[modify_index];
175 if (data.v() < FLAGS_near_stop_speed && data.a() <= 0.0 &&
176 data.a() > FLAGS_near_stop_deceleration) {
177 data.set_a(FLAGS_near_stop_deceleration);
178 } else {
179 break;
180 }
181 }
182 if (modify_index < 0) {
183 modify_index = 0;
184 }
185 // Update the speed according to the new deceleration.
186 double pre_s = speed_data[modify_index].s();
187 double pre_v = speed_data[modify_index].v();
188 double s = 0.0;
189 double v = 0.0;
190 const double unit_t = FLAGS_fallback_time_unit;
191 for (int i = modify_index + 1; i < speed_data_num; ++i) {
192 if (pre_v > 0.0) {
193 v = std::fmax(0.0, pre_v + unit_t * FLAGS_near_stop_deceleration);
194 s = std::fmax(pre_s, pre_s + 0.5 * (pre_v + (pre_v + v)) * unit_t);
195 } else {
196 v = 0.0;
197 s = pre_s;
198 }
199 speed_data[i].set_v(v);
200 speed_data[i].set_s(s);
201 pre_s = s;
202 pre_v = v;
203 }
204}
205
206} // namespace planning
207} // namespace apollo
A general class to denote the return status of an API call.
Definition status.h:43
static Status OK()
generate a success status.
Definition status.h:60
static PathPoint ToPathPoint(const double x, const double y, const double z=0, const double s=0, const double theta=0, const double kappa=0, const double dkappa=0, const double ddkappa=0)
Frame holds all data for one planning cycle.
Definition frame.h:62
ReferenceLineInfo holds all data for one reference line.
const std::vector< PathData > & GetCandidatePathData() const
void set_trajectory_type(const ADCTrajectory::TrajectoryType trajectory_type)
ADCTrajectory::TrajectoryType trajectory_type() const
std::shared_ptr< DependencyInjector > injector_
Definition task.h:59
virtual common::Status Execute(Frame *frame, ReferenceLineInfo *reference_line_info)
Definition task.cc:60
common::Status Execute(Frame *frame, ReferenceLineInfo *reference_line_info) override
Planning module main class.
#define AERROR
Definition log.h:44
constexpr double kSpeedOptimizationFallbackCost
constexpr double kPathOptimizationFallbackCost
class register implement
Definition arena_queue.h:37