Apollo 10.0
自动驾驶开放平台
stage.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 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
22
23#include <unordered_map>
24#include <utility>
25
27#include "cyber/time/clock.h"
34
35namespace apollo {
36namespace planning {
37
39
41 : next_stage_(""), context_(nullptr), injector_(nullptr), name_("") {}
42
43bool Stage::Init(const StagePipeline& config,
44 const std::shared_ptr<DependencyInjector>& injector,
45 const std::string& config_dir, void* context) {
46 pipeline_config_ = config;
47 next_stage_ = config.name();
48 injector_ = injector;
49 name_ = config.name();
50 context_ = context;
51 injector_->planning_context()
52 ->mutable_planning_status()
53 ->mutable_scenario()
54 ->set_stage_type(name_);
55 std::string path_name = ConfigUtil::TransformToPathName(name_);
56 std::string task_config_dir = config_dir + "/" + path_name;
57 // Load task plugin.
58 for (int i = 0; i < pipeline_config_.task_size(); ++i) {
59 auto task = pipeline_config_.task(i);
60 auto task_type = task.type();
64 if (nullptr == task_ptr) {
65 AERROR << "Create task " << task.name() << " of " << name_ << " failed!";
66 return false;
67 }
68 if (task_ptr->Init(task_config_dir, task.name(), injector)) {
69 task_list_.push_back(task_ptr);
70 } else {
71 AERROR << task.name() << " init failed!";
72 return false;
73 }
74 }
75 // Load trajectory fallback task.
76 // If fallback task is not set, use "FastStopTrajectoryFallback" as default.
77 std::string fallback_task_type = "FastStopTrajectoryFallback";
78 std::string fallback_task_name = "FAST_STOP_TRAJECTORY_FALLBACK";
79 if (pipeline_config_.has_fallback_task()) {
80 fallback_task_type = pipeline_config_.fallback_task().type();
81 fallback_task_name = pipeline_config_.fallback_task().name();
82 }
86 ConfigUtil::GetFullPlanningClassName(fallback_task_type));
87 if (nullptr == fallback_task_) {
88 AERROR << "Create fallback task " << fallback_task_name << " of " << name_
89 << " failed!";
90 return false;
91 }
92 if (!fallback_task_->Init(task_config_dir, fallback_task_name, injector)) {
93 AERROR << fallback_task_name << " init failed!";
94 return false;
95 }
96 return true;
97}
98
99const std::string& Stage::Name() const { return name_; }
100
102 const common::TrajectoryPoint& planning_start_point, Frame* frame) {
103 StageResult stage_result;
104 if (frame->reference_line_info().empty()) {
105 AERROR << "referenceline is empty in stage" << name_;
106 return stage_result.SetStageStatus(StageStatusType::ERROR);
107 }
108 for (auto& reference_line_info : *frame->mutable_reference_line_info()) {
109 if (!reference_line_info.IsDrivable()) {
110 AERROR << "The generated path is not drivable skip";
111 reference_line_info.SetDrivable(false);
112 continue;
113 }
114
115 if (reference_line_info.IsChangeLanePath()) {
116 AERROR << "The generated refline is change lane path, skip";
117 reference_line_info.SetDrivable(false);
118 continue;
119 }
121 for (auto task : task_list_) {
122 const double start_timestamp = Clock::NowInSeconds();
123
124 ret = task->Execute(frame, &reference_line_info);
125
126 const double end_timestamp = Clock::NowInSeconds();
127 const double time_diff_ms = (end_timestamp - start_timestamp) * 1000;
128 ADEBUG << "after task[" << task->Name()
129 << "]: " << reference_line_info.PathSpeedDebugString();
130 ADEBUG << task->Name() << " time spend: " << time_diff_ms << " ms.";
131 AINFO << "Planning Perf: task name [" << task->Name() << "], "
132 << time_diff_ms << " ms.";
133 RecordDebugInfo(&reference_line_info, task->Name(), time_diff_ms);
134
135 if (!ret.ok()) {
136 stage_result.SetTaskStatus(ret);
137 AERROR << "Failed to run tasks[" << task->Name()
138 << "], Error message: " << ret.error_message();
139 break;
140 }
141 }
142 // Generate fallback trajectory in case of task error.
143 if (!ret.ok()) {
144 fallback_task_->Execute(frame, &reference_line_info);
145 }
146 DiscretizedTrajectory trajectory;
147 if (!reference_line_info.CombinePathAndSpeedProfile(
148 planning_start_point.relative_time(),
149 planning_start_point.path_point().s(), &trajectory)) {
150 AERROR << "Fail to aggregate planning trajectory."
151 << reference_line_info.IsChangeLanePath();
152 reference_line_info.SetDrivable(false);
153 continue;
154 }
155 reference_line_info.SetTrajectory(trajectory);
156 reference_line_info.SetDrivable(true);
157 return stage_result;
158 }
159 return stage_result;
160}
161
163 const common::TrajectoryPoint& planning_start_point, Frame* frame) {
164 // online learning mode
165 for (auto& reference_line_info : *frame->mutable_reference_line_info()) {
166 reference_line_info.SetDrivable(false);
167 }
168
169 StageResult stage_result;
170 // FIXME(all): current only pick up the first reference line to use
171 // learning model trajectory
172 auto& picked_reference_line_info =
173 frame->mutable_reference_line_info()->front();
174 for (auto task : task_list_) {
175 const double start_timestamp = Clock::NowInSeconds();
176
177 const auto ret = task->Execute(frame, &picked_reference_line_info);
178
179 const double end_timestamp = Clock::NowInSeconds();
180 const double time_diff_ms = (end_timestamp - start_timestamp) * 1000;
181 ADEBUG << "task[" << task->Name() << "] time spent: " << time_diff_ms
182 << " ms.";
183 RecordDebugInfo(&picked_reference_line_info, task->Name(), time_diff_ms);
184
185 if (!ret.ok()) {
186 stage_result.SetTaskStatus(ret);
187 AERROR << "Failed to run tasks[" << task->Name()
188 << "], Error message: " << ret.error_message();
189 break;
190 }
191 }
192
193 const std::vector<common::TrajectoryPoint>& adc_future_trajectory_points =
194 picked_reference_line_info.trajectory();
195 DiscretizedTrajectory trajectory;
196 if (picked_reference_line_info.AdjustTrajectoryWhichStartsFromCurrentPos(
197 planning_start_point, adc_future_trajectory_points, &trajectory)) {
198 picked_reference_line_info.SetTrajectory(trajectory);
199 picked_reference_line_info.SetDrivable(true);
200 picked_reference_line_info.SetCost(0);
201 }
202
203 return stage_result;
204}
205
207 auto ret = common::Status::OK();
208 StageResult stage_result;
209 for (auto task : task_list_) {
210 const double start_timestamp = Clock::NowInSeconds();
211
212 ret = task->Execute(frame);
213
214 if (!ret.ok()) {
215 stage_result.SetTaskStatus(ret);
216 AERROR << "Failed to run tasks[" << task->Name()
217 << "], Error message: " << ret.error_message();
218 const double end_timestamp = Clock::NowInSeconds();
219 const double time_diff_ms = (end_timestamp - start_timestamp) * 1000;
220 AINFO << "Planning Perf: task name [" << task->Name() << "], "
221 << time_diff_ms << " ms.";
222 return stage_result;
223 }
224
225 const double end_timestamp = Clock::NowInSeconds();
226 const double time_diff_ms = (end_timestamp - start_timestamp) * 1000;
227 AINFO << "Planning Perf: task name [" << task->Name() << "], "
228 << time_diff_ms << " ms.";
229 }
230
231 if (frame->open_space_info().fallback_flag() ||
232 frame->open_space_info().stop_flag()) {
233 auto& trajectory = frame->open_space_info().fallback_trajectory().first;
234 auto& gear = frame->open_space_info().fallback_trajectory().second;
235 PublishableTrajectory publishable_trajectory(Clock::NowInSeconds(),
236 trajectory);
237 auto publishable_traj_and_gear =
238 std::make_pair(std::move(publishable_trajectory), gear);
239
241 std::move(publishable_traj_and_gear);
242 } else {
243 auto& trajectory =
245 auto& gear =
247 PublishableTrajectory publishable_trajectory(Clock::NowInSeconds(),
248 trajectory);
249 auto publishable_traj_and_gear =
250 std::make_pair(std::move(publishable_trajectory), gear);
251
253 std::move(publishable_traj_and_gear);
254 }
255 return stage_result;
256}
257
262
264 const std::string& name,
265 const double time_diff_ms) {
266 if (!FLAGS_enable_record_debug) {
267 ADEBUG << "Skip record debug info";
268 return;
269 }
270 if (reference_line_info == nullptr) {
271 AERROR << "Reference line info is null.";
272 return;
273 }
274
275 auto ptr_latency_stats = reference_line_info->mutable_latency_stats();
276
277 auto ptr_stats = ptr_latency_stats->add_task_stats();
278 ptr_stats->set_name(name);
279 ptr_stats->set_time_ms(time_diff_ms);
280}
281
282} // namespace planning
283} // namespace apollo
A general class to denote the return status of an API call.
Definition status.h:43
bool ok() const
check whether the return status is OK.
Definition status.h:67
const std::string & error_message() const
returns the error message of the status, empty if the status is OK.
Definition status.h:91
static Status OK()
generate a success status.
Definition status.h:60
a singleton clock that can be used to get the current timestamp.
Definition clock.h:39
static double NowInSeconds()
gets the current time in second.
Definition clock.cc:56
static PluginManager * Instance()
get singleton instance of PluginManager
std::shared_ptr< Base > CreateInstance(const std::string &derived_class)
create plugin instance of derived class based on Base
static std::string TransformToPathName(const std::string &name)
Tranform the name to part of path.
static std::string GetFullPlanningClassName(const std::string &class_name)
Given the class name of planning module, combine the namespace "apollo::planning::" with it to create...
Frame holds all data for one planning cycle.
Definition frame.h:62
const OpenSpaceInfo & open_space_info() const
Definition frame.h:167
OpenSpaceInfo * mutable_open_space_info()
Definition frame.h:169
const std::list< ReferenceLineInfo > & reference_line_info() const
Definition frame.cc:123
std::list< ReferenceLineInfo > * mutable_reference_line_info()
Definition frame.cc:127
const TrajGearPair & fallback_trajectory() const
const TrajGearPair & chosen_partitioned_trajectory() const
std::pair< PublishableTrajectory, canbus::Chassis::GearPosition > * mutable_publishable_trajectory_data()
ReferenceLineInfo holds all data for one reference line.
const StageResult & SetStageStatus(const StageStatusType &stage_status)
Set the stage status.
const StageResult & SetTaskStatus(const common::Status &status)
Set the task execution result.
std::string next_stage_
Definition stage.h:89
virtual StageResult FinishScenario()
Definition stage.cc:258
StageResult ExecuteTaskOnOpenSpace(Frame *frame)
Definition stage.cc:206
virtual bool Init(const StagePipeline &config, const std::shared_ptr< DependencyInjector > &injector, const std::string &config_dir, void *context)
Definition stage.cc:43
std::shared_ptr< DependencyInjector > injector_
Definition stage.h:91
StagePipeline pipeline_config_
Definition stage.h:92
StageResult ExecuteTaskOnReferenceLineForOnlineLearning(const common::TrajectoryPoint &planning_start_point, Frame *frame)
Definition stage.cc:162
void RecordDebugInfo(ReferenceLineInfo *reference_line_info, const std::string &name, const double time_diff_ms)
Definition stage.cc:263
StageResult ExecuteTaskOnReferenceLine(const common::TrajectoryPoint &planning_start_point, Frame *frame)
Definition stage.cc:101
std::shared_ptr< Task > fallback_task_
Definition stage.h:88
const std::string & Name() const
Definition stage.cc:99
std::vector< std::shared_ptr< Task > > task_list_
Definition stage.h:87
Planning module main class.
#define ADEBUG
Definition log.h:41
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
class register implement
Definition arena_queue.h:37
repeated PluginDeclareInfo task
optional PluginDeclareInfo fallback_task