Apollo 11.0
自动驾驶开放平台
open_space_path_planning.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
28using common::math::Vec2d;
29
31 const std::string &config_dir,
32 const std::string &name,
33 const std::shared_ptr<DependencyInjector> &injector) {
34 if (!Task::Init(config_dir, name, injector)) {
35 return false;
36 }
37 if (!Task::LoadConfig<OpenSpacePathPlanningConfig>(&config_)) {
38 return false;
39 }
40 path_finder_.reset(new HybridAStar(config_.warm_start_config()));
41 AINFO << config_.DebugString();
42 return true;
43}
44
50
51void OpenSpacePathPlanning::Stop() {
52 if (config_.enable_path_planning_thread()) {
53 if (thread_init_flag_) {
54 task_future_.get();
55 }
56 data_ready_.store(false);
57 path_update_.store(false);
58 }
59}
60
61Status OpenSpacePathPlanning::Process() {
62 AINFO << "data_ready_: " << (data_ready_ ? "true" : "false");
63 auto* previous_frame = injector_->frame_history()->Latest();
65 AINFO << "Not replan";
67 mutable_path_planning_trajectory_result()) =
68 previous_frame->open_space_info().path_planning_trajectory_result();
69 return Status::OK();
70 }
71
72 if (!previous_frame->open_space_info().
73 path_planning_trajectory_result().empty()) {
74 AINFO << "Previous frame has path planning result";
76 mutable_path_planning_trajectory_result()) =
77 previous_frame->open_space_info().path_planning_trajectory_result();
78 return Status::OK();
79 }
80 AINFO << "Path planning";
81
82 if (path_update_) {
83 AINFO << "Path planning updated";
84 LoadResult(frame_->mutable_open_space_info()->
85 mutable_path_planning_trajectory_result());
86 path_update_.store(false);
87 return Status::OK();
88 }
89 PathPlanning();
90 return Status::OK();
91}
92
93void OpenSpacePathPlanning::PathPlanning() {
94 if (config_.enable_path_planning_thread() && !thread_init_flag_) {
95 task_future_ = cyber::Async(
96 &OpenSpacePathPlanning::GeneratePathThread, this);
97 thread_init_flag_.store(true);
98 }
99
100 const auto& open_space_info = frame_->open_space_info();
101 double start_x = injector_->vehicle_state()->x();
102 double start_y = injector_->vehicle_state()->y();
103 double start_theta = injector_->vehicle_state()->heading();
105 open_space_info.origin_heading(),
106 open_space_info.origin_point(),
107 &start_x, &start_y, &start_theta);
108 if (config_.enable_path_planning_thread()) {
109 if (!data_ready_) {
110 std::lock_guard<std::mutex> lock(data_mutex_);
111 thread_data_.start_pose = {start_x, start_y, start_theta};
112 thread_data_.end_pose = open_space_info.open_space_end_pose();
113 thread_data_.rotate_angle = open_space_info.origin_heading();
114 thread_data_.translate_origin = open_space_info.origin_point();
115 thread_data_.obstacles_edges_num = open_space_info.obstacles_edges_num();
116 thread_data_.obstacles_A = open_space_info.obstacles_A();
117 thread_data_.obstacles_b = open_space_info.obstacles_b();
118 thread_data_.obstacles_vertices_vec =
119 open_space_info.obstacles_vertices_vec();
120 thread_data_.soft_boundary_vertices_vec =
121 open_space_info.soft_boundary_vertices_vec();
122 thread_data_.XYbounds = open_space_info.ROI_xy_boundary();
123 thread_data_.reeds_sheep_last_straight =
125 open_space_info.parking_type() == ParkingType::VERTICAL_PARKING) ||
127 open_space_info.parking_type() == ParkingType::PARALLEL_PARKING)) ?
128 true : false;
129 data_ready_.store(true);
130 }
131 } else {
132 const auto& end_pose = open_space_info.open_space_end_pose();
133 const auto& rotate_angle = open_space_info.origin_heading();
134 const auto& translate_origin = open_space_info.origin_point();
135 const auto& obstacles_edges_num = open_space_info.obstacles_edges_num();
136 const auto& obstacles_A = open_space_info.obstacles_A();
137 const auto& obstacles_b = open_space_info.obstacles_b();
138 const auto& obstacles_vertices_vec =
139 open_space_info.obstacles_vertices_vec();
140 const auto& soft_boundary_vertices_vec =
141 open_space_info.soft_boundary_vertices_vec();
142 const auto& XYbounds = open_space_info.ROI_xy_boundary();
143 const bool reeds_sheep_last_straight =
145 open_space_info.parking_type() == ParkingType::VERTICAL_PARKING) ||
147 open_space_info.parking_type() == ParkingType::PARALLEL_PARKING)) ?
148 true : false;
149 if (path_finder_->Plan(start_x, start_y, start_theta,
150 end_pose[0], end_pose[1],
151 end_pose[2], XYbounds,
152 obstacles_vertices_vec,
153 &result_,
154 soft_boundary_vertices_vec,
155 reeds_sheep_last_straight)) {
156 path_update_.store(true);
157 AINFO << "State warm start problem solved successfully!";
158 } else {
159 AERROR << "State warm start problem failed to solve";
160 }
161 }
162}
163
164void OpenSpacePathPlanning::GeneratePathThread() {
165 while (!data_ready_) {
166 usleep(10);
167 }
168 OpenSpacePathPlanningThreadData thread_data;
169 {
170 std::lock_guard<std::mutex> lock(data_mutex_);
171 thread_data = thread_data_;
172 }
173
174 AINFO << "start pose: " << thread_data.start_pose[0]
175 << " " << thread_data.start_pose[1]
176 << " " << thread_data.start_pose[2];
177 bool ret = path_finder_->Plan(
178 thread_data.start_pose[0], thread_data.start_pose[1],
179 thread_data.start_pose[2], thread_data.end_pose[0],
180 thread_data.end_pose[1], thread_data.end_pose[2],
181 thread_data.XYbounds, thread_data.obstacles_vertices_vec,
182 &result_,thread_data_.soft_boundary_vertices_vec,
183 thread_data.reeds_sheep_last_straight);
184
185 if (ret) {
186 std::lock_guard<std::mutex> lock(data_mutex_);
187 path_update_.store(true);
188 AINFO << "path find success!";
189 } else {
190 AERROR << "path find failed";
191 }
192 thread_init_flag_.store(false);
193 data_ready_.store(false);
194 path_finder_.reset(new HybridAStar(config_.warm_start_config()));
195}
196
197void OpenSpacePathPlanning::LoadResult(
198 DiscretizedTrajectory* const trajectory_data) {
199 trajectory_data->clear();
200 result_.x.back() = frame_->open_space_info().open_space_end_pose()[0];
201 result_.y.back() = frame_->open_space_info().open_space_end_pose()[1];
202 result_.phi.back() = frame_->open_space_info().open_space_end_pose()[2];
203 for (size_t i = 0; i < result_.x.size(); i++) {
207 &result_.x[i],
208 &result_.y[i],
209 &result_.phi[i]);
210 common::TrajectoryPoint point;
211 point.mutable_path_point()->set_x(result_.x[i]);
212 point.mutable_path_point()->set_y(result_.y[i]);
213 point.mutable_path_point()->set_theta(result_.phi[i]);
214 point.set_v(result_.v[i]);
215 point.set_steer(result_.steer[i]);
216 point.set_a(result_.a[i]);
217 point.set_relative_time(i * 0.5);
218 trajectory_data->emplace_back(point);
219 }
220}
221
222} // namespace planning
223} // 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
const std::vector< double > & open_space_end_pose() const
const common::math::Vec2d & origin_point() const
bool Init(const std::string &config_dir, const std::string &name, const std::shared_ptr< DependencyInjector > &injector) override
static void PathPointNormalizing(double rotate_angle, const common::math::Vec2d &translate_origin, double *x, double *y, double *phi)
static void PathPointDeNormalizing(double rotate_angle, const common::math::Vec2d &translate_origin, double *x, double *y, double *phi)
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
std::vector< std::vector< common::math::Vec2d > > soft_boundary_vertices_vec
std::vector< std::vector< common::math::Vec2d > > obstacles_vertices_vec