Apollo 11.0
自动驾驶开放平台
open_space_trajectory_optimizer_park.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
22
23namespace apollo {
24namespace planning {
25
28
30 const std::string &config_dir, const std::string &name,
31 const std::shared_ptr<DependencyInjector> &injector) {
32 if (!Task::Init(config_dir, name, injector)) {
33 return false;
34 }
35 if (!Task::LoadConfig<OpenSpaceTrajectoryOptimizerParkConfig>(&config_)) {
36 return false;
37 }
38 trajectory_optimizer_.reset(new Optimizer(config_));
39 return true;
40}
41
47
48void OpenSpaceTrajectoryOptimizerPark::Stop() {
50 if (thread_init_flag_) {
51 task_future_.get();
52 }
53 }
54}
55
56Status OpenSpaceTrajectoryOptimizerPark::Process() {
57 auto* previous_frame = injector_->frame_history()->Latest();
59 AINFO << "Not replan";
61 previous_frame->open_space_info().optimizer_trajectory_data();
63 previous_frame->open_space_info().open_space_provider_success());
64 if (FLAGS_enable_record_debug) {
65 // copy previous debug to current frame
66 ReuseLastFrameDebug(previous_frame);
67 }
68 return Status::OK();
69 }
70
71 if (replan_flag_) {
72 replan_flag_ = false;
74 mutable_path_planning_trajectory_result()->clear();
75 }
76
78 AINFO << "No initial trajectory solution";
79 return Status::OK();
80 }
81
82 Optimize();
83 if (trajectory_update_) {
84 // call merge debug ptr, open_space_trajectory_optimizer_
85 auto* ptr_debug = frame_->mutable_open_space_info()->mutable_debug();
86 trajectory_optimizer_->UpdateDebugInfo(
87 ptr_debug->mutable_planning_data()->mutable_open_space());
88 // sync debug instance
90
91 LoadResult(frame_->mutable_open_space_info()->
92 mutable_optimizer_trajectory_data());
93 thread_init_flag_ = false;
94 trajectory_update_ = false;
95 }
96
97 return Status::OK();
98}
99
100void OpenSpaceTrajectoryOptimizerPark::Optimize() {
101 if (config_.enable_trajectory_optimize_thread()) {
102 if (thread_init_flag_) {
103 AINFO << "trajectory is optimizing, please wait!";
104 return;
105 }
106
107 task_future_ = cyber::Async(
108 &OpenSpaceTrajectoryOptimizerPark::GenerateTrajectoryThread, this);
109 thread_init_flag_ = true;
110 } else {
111 GenerateTrajectoryThread();
112 LoadResult(frame_->mutable_open_space_info()->
113 mutable_optimizer_trajectory_data());
114 }
115}
116
117void OpenSpaceTrajectoryOptimizerPark::GenerateTrajectoryThread() {
118 Status ret = trajectory_optimizer_->Plan(frame_->open_space_info());
119
120 if (ret == Status::OK()) {
121 std::lock_guard<std::mutex> lock(data_mutex_);
122 trajectory_update_ = true;
123 AINFO << "tarjectory optimize successfully!";
124 } else {
125 thread_init_flag_ = false;
126 replan_flag_ = true;
127 AERROR << "tarjectory optimize failed";
128 }
129}
130
131void OpenSpaceTrajectoryOptimizerPark::LoadResult(
132 DiscretizedTrajectory* const trajectory_data) {
133 // Load unstitched two trajectories into frame for debug
134 trajectory_data->clear();
135
136 trajectory_optimizer_->GetOptimizedTrajectory(
137 *trajectory_data);
138
140}
141
142void OpenSpaceTrajectoryOptimizerPark::ReuseLastFrameDebug(
143 const Frame* last_frame) {
144 // reuse last frame's instance
146 ptr_debug->mutable_planning_data()->mutable_open_space()->MergeFrom(
147 last_frame->open_space_info()
148 .debug_instance()
149 .planning_data()
150 .open_space());
151 ptr_debug->mutable_planning_data()->mutable_open_space()->clear_obstacles();
152 // load obstacles
153 for (const auto& obstacle_vertices :
154 frame_->open_space_info().obstacles_vertices_vec()) {
155 auto* obstacle_ptr = ptr_debug->mutable_planning_data()->
156 mutable_open_space()->add_obstacles();
157 for (const auto& vertex : obstacle_vertices) {
158 obstacle_ptr->add_vertices_x_coords(vertex.x());
159 obstacle_ptr->add_vertices_y_coords(vertex.y());
160 }
161 }
162}
163
164} // namespace planning
165} // 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
const OpenSpaceInfo & open_space_info() const
Definition frame.h:167
OpenSpaceInfo * mutable_open_space_info()
Definition frame.h:169
apollo::planning_internal::Debug * mutable_debug_instance()
void set_open_space_provider_success(const bool flag)
DiscretizedTrajectory * mutable_optimizer_trajectory_data()
const DiscretizedTrajectory & path_planning_trajectory_result() const
apollo::planning_internal::Debug * mutable_debug()
bool Init(const std::string &config_dir, const std::string &name, const std::shared_ptr< DependencyInjector > &injector) override
std::shared_ptr< DependencyInjector > injector_
Definition task.h:59
virtual bool Init(const std::string &config_dir, const std::string &name, const std::shared_ptr< DependencyInjector > &injector)
Definition task.cc:40
Planning module main class.
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
class register implement
Definition arena_queue.h:37