• Apollo planning之hybrid A*


    欢迎大家关注我的B站:

    偷吃薯片的Zheng同学的个人空间-偷吃薯片的Zheng同学个人主页-哔哩哔哩视频 (bilibili.com)

    目录

    1 hybrid A*的介绍

    2 代码解读

    3 算法细节

    3.1 ValidityCheck

    3.2 GenerateDpMap 

    3.3 AnalyticExpansion 

    3.4 CalculateNodeCost 


    hybrid A*的介绍

    hybrid_a_star的目标是在开放空间中产生粗轨迹。Hybrid_a_star包含 node3d、grid_search、reeds_shepp_path和hybrid_a_star。hybrid_a_star是产生粗轨迹并调用grid_search和reeds_shepp_path的最重要组件。注意首字母大小写

    2 代码解读

    输入:当前点(计划起点),目标点(计划终点),ROI_xy_boundary(x和y的最大和最小边界值),障碍物顶点矢量(顶点位置信息)。该函数如下:

    1. bool HybridAStar::Plan(double sx, double sy, double sphi,
    2. double ex, double ey, double ephi,
    3. const std::vector<double>& XYbounds,
    4. const std::vectorvectormath::Vec2d>>& obstacles_vertices_vec,HybridAStartResult* result)

    输入的 HybridAStar::Plan() 由open_space_trajectory_provider调用,请参考如下代码

    1. OpenSpaceTrajectoryProvider::OpenSpaceTrajectoryProvider(
    2. const TaskConfig& config,
    3. const std::shared_ptr<DependencyInjector>& injector)
    4. : TrajectoryOptimizer(config, injector) {
    5. open_space_trajectory_optimizer_.reset(new OpenSpaceTrajectoryOptimizer(
    6. config.open_space_trajectory_provider_config()
    7. .open_space_trajectory_optimizer_config()));
    8. }
    9. OpenSpaceTrajectoryProvider::~OpenSpaceTrajectoryProvider() {
    10. if (FLAGS_enable_open_space_planner_thread) {
    11. Stop();
    12. }
    13. }
    14. void OpenSpaceTrajectoryProvider::Stop() {
    15. if (FLAGS_enable_open_space_planner_thread) {
    16. is_generation_thread_stop_.store(true);
    17. if (thread_init_flag_) {
    18. task_future_.get();
    19. }
    20. trajectory_updated_.store(false);
    21. trajectory_error_.store(false);
    22. trajectory_skipped_.store(false);
    23. optimizer_thread_counter = 0;
    24. }
    25. }
    26. void OpenSpaceTrajectoryProvider::Restart() {
    27. if (FLAGS_enable_open_space_planner_thread) {
    28. is_generation_thread_stop_.store(true);
    29. if (thread_init_flag_) {
    30. task_future_.get();
    31. }
    32. is_generation_thread_stop_.store(false);
    33. thread_init_flag_ = false;
    34. trajectory_updated_.store(false);
    35. trajectory_error_.store(false);
    36. trajectory_skipped_.store(false);
    37. optimizer_thread_counter = 0;
    38. }
    39. }
    40. Status OpenSpaceTrajectoryProvider::Process() {
    41. ADEBUG << "trajectory provider";
    42. auto trajectory_data =
    43. frame_->mutable_open_space_info()->mutable_stitched_trajectory_result();
    44. // generate stop trajectory at park_and_go check_stage
    45. if (injector_->planning_context()
    46. ->mutable_planning_status()
    47. ->mutable_park_and_go()
    48. ->in_check_stage()) {
    49. ADEBUG << "ParkAndGo Stage Check.";
    50. GenerateStopTrajectory(trajectory_data);
    51. return Status::OK();
    52. }
    53. // Start thread when getting in Process() for the first time
    54. if (FLAGS_enable_open_space_planner_thread && !thread_init_flag_) {
    55. task_future_ = cyber::Async(
    56. &OpenSpaceTrajectoryProvider::GenerateTrajectoryThread, this);
    57. thread_init_flag_ = true;
    58. }
    59. // Get stitching trajectory from last frame
    60. const common::VehicleState vehicle_state = frame_->vehicle_state();
    61. auto* previous_frame = injector_->frame_history()->Latest();
    62. // Use complete raw trajectory from last frame for stitching purpose
    63. std::vector<TrajectoryPoint> stitching_trajectory;
    64. if (!IsVehicleStopDueToFallBack(
    65. previous_frame->open_space_info().fallback_flag(), vehicle_state)) {
    66. const auto& previous_planning =
    67. previous_frame->open_space_info().stitched_trajectory_result();
    68. const auto& previous_planning_header =
    69. previous_frame->current_frame_planned_trajectory()
    70. .header()
    71. .timestamp_sec();
    72. const double planning_cycle_time = FLAGS_open_space_planning_period;
    73. PublishableTrajectory last_frame_complete_trajectory(
    74. previous_planning_header, previous_planning);
    75. std::string replan_reason;
    76. const double start_timestamp = Clock::NowInSeconds();
    77. stitching_trajectory = TrajectoryStitcher::ComputeStitchingTrajectory(
    78. vehicle_state, start_timestamp, planning_cycle_time,
    79. FLAGS_open_space_trajectory_stitching_preserved_length, false,
    80. &last_frame_complete_trajectory, &replan_reason);
    81. } else {
    82. ADEBUG << "Replan due to fallback stop";
    83. const double planning_cycle_time =
    84. 1.0 / static_cast<double>(FLAGS_planning_loop_rate);
    85. stitching_trajectory = TrajectoryStitcher::ComputeReinitStitchingTrajectory(
    86. planning_cycle_time, vehicle_state);
    87. auto* open_space_status = injector_->planning_context()
    88. ->mutable_planning_status()
    89. ->mutable_open_space();
    90. open_space_status->set_position_init(false);
    91. }
    92. // Get open_space_info from current frame
    93. const auto& open_space_info = frame_->open_space_info();
    94. if (FLAGS_enable_open_space_planner_thread) {
    95. ADEBUG << "Open space plan in multi-threads mode";
    96. if (is_generation_thread_stop_) {
    97. GenerateStopTrajectory(trajectory_data);
    98. return Status(ErrorCode::OK, "Parking finished");
    99. }
    100. {
    101. std::lock_guard<std::mutex> lock(open_space_mutex_);
    102. thread_data_.stitching_trajectory = stitching_trajectory;
    103. thread_data_.end_pose = open_space_info.open_space_end_pose();
    104. thread_data_.rotate_angle = open_space_info.origin_heading();
    105. thread_data_.translate_origin = open_space_info.origin_point();
    106. thread_data_.obstacles_edges_num = open_space_info.obstacles_edges_num();
    107. thread_data_.obstacles_A = open_space_info.obstacles_A();
    108. thread_data_.obstacles_b = open_space_info.obstacles_b();
    109. thread_data_.obstacles_vertices_vec =
    110. open_space_info.obstacles_vertices_vec();
    111. thread_data_.XYbounds = open_space_info.ROI_xy_boundary();
    112. data_ready_.store(true);
    113. }
    114. // Check vehicle state
    115. if (IsVehicleNearDestination(
    116. vehicle_state, open_space_info.open_space_end_pose(),
    117. open_space_info.origin_heading(), open_space_info.origin_point())) {
    118. GenerateStopTrajectory(trajectory_data);
    119. is_generation_thread_stop_.store(true);
    120. return Status(ErrorCode::OK, "Vehicle is near to destination");
    121. }
    122. // Check if trajectory updated
    123. if (trajectory_updated_) {
    124. std::lock_guard<std::mutex> lock(open_space_mutex_);
    125. LoadResult(trajectory_data);
    126. if (FLAGS_enable_record_debug) {
    127. // call merge debug ptr, open_space_trajectory_optimizer_
    128. auto* ptr_debug = frame_->mutable_open_space_info()->mutable_debug();
    129. open_space_trajectory_optimizer_->UpdateDebugInfo(
    130. ptr_debug->mutable_planning_data()->mutable_open_space());
    131. // sync debug instance
    132. frame_->mutable_open_space_info()->sync_debug_instance();
    133. }
    134. data_ready_.store(false);
    135. trajectory_updated_.store(false);
    136. return Status::OK();
    137. }
    138. if (trajectory_error_) {
    139. ++optimizer_thread_counter;
    140. std::lock_guard<std::mutex> lock(open_space_mutex_);
    141. trajectory_error_.store(false);
    142. // TODO(Jinyun) Use other fallback mechanism when last iteration smoothing
    143. // result has out of bound pathpoint which is not allowed for next
    144. // iteration hybrid astar algorithm which requires start position to be
    145. // strictly in bound
    146. if (optimizer_thread_counter > 1000) {
    147. return Status(ErrorCode::PLANNING_ERROR,
    148. "open_space_optimizer failed too many times");
    149. }
    150. }
    151. if (previous_frame->open_space_info().open_space_provider_success()) {
    152. ReuseLastFrameResult(previous_frame, trajectory_data);
    153. if (FLAGS_enable_record_debug) {
    154. // copy previous debug to current frame
    155. ReuseLastFrameDebug(previous_frame);
    156. }
    157. // reuse last frame debug when use last frame traj
    158. return Status(ErrorCode::OK,
    159. "Waiting for open_space_trajectory_optimizer in "
    160. "open_space_trajectory_provider");
    161. } else {
    162. GenerateStopTrajectory(trajectory_data);
    163. return Status(ErrorCode::OK, "Stop due to computation not finished");
    164. }
    165. } else {
    166. const auto& end_pose = open_space_info.open_space_end_pose();
    167. const auto& rotate_angle = open_space_info.origin_heading();
    168. const auto& translate_origin = open_space_info.origin_point();
    169. const auto& obstacles_edges_num = open_space_info.obstacles_edges_num();
    170. const auto& obstacles_A = open_space_info.obstacles_A();
    171. const auto& obstacles_b = open_space_info.obstacles_b();
    172. const auto& obstacles_vertices_vec =
    173. open_space_info.obstacles_vertices_vec();
    174. const auto& XYbounds = open_space_info.ROI_xy_boundary();
    175. // Check vehicle state
    176. if (IsVehicleNearDestination(vehicle_state, end_pose, rotate_angle,
    177. translate_origin)) {
    178. GenerateStopTrajectory(trajectory_data);
    179. return Status(ErrorCode::OK, "Vehicle is near to destination");
    180. }
    181. // Generate Trajectory;
    182. double time_latency;
    183. Status status = open_space_trajectory_optimizer_->Plan(
    184. stitching_trajectory, end_pose, XYbounds, rotate_angle,
    185. translate_origin, obstacles_edges_num, obstacles_A, obstacles_b,
    186. obstacles_vertices_vec, &time_latency);
    187. frame_->mutable_open_space_info()->set_time_latency(time_latency);
    188. // If status is OK, update vehicle trajectory;
    189. if (status == Status::OK()) {
    190. LoadResult(trajectory_data);
    191. return status;
    192. } else {
    193. return status;
    194. }
    195. }
    196. return Status(ErrorCode::PLANNING_ERROR);
    197. }
    198. void OpenSpaceTrajectoryProvider::GenerateTrajectoryThread() {
    199. while (!is_generation_thread_stop_) {
    200. if (!trajectory_updated_ && data_ready_) {
    201. OpenSpaceTrajectoryThreadData thread_data;
    202. {
    203. std::lock_guard<std::mutex> lock(open_space_mutex_);
    204. thread_data = thread_data_;
    205. }
    206. double time_latency;
    207. Status status = open_space_trajectory_optimizer_->Plan(
    208. thread_data.stitching_trajectory, thread_data.end_pose,
    209. thread_data.XYbounds, thread_data.rotate_angle,
    210. thread_data.translate_origin, thread_data.obstacles_edges_num,
    211. thread_data.obstacles_A, thread_data.obstacles_b,
    212. thread_data.obstacles_vertices_vec, &time_latency);
    213. frame_->mutable_open_space_info()->set_time_latency(time_latency);
    214. if (status == Status::OK()) {
    215. std::lock_guard<std::mutex> lock(open_space_mutex_);
    216. trajectory_updated_.store(true);
    217. } else {
    218. if (status.ok()) {
    219. std::lock_guard<std::mutex> lock(open_space_mutex_);
    220. trajectory_skipped_.store(true);
    221. } else {
    222. std::lock_guard<std::mutex> lock(open_space_mutex_);
    223. trajectory_error_.store(true);
    224. }
    225. }
    226. }
    227. }
    228. }
    229. bool OpenSpaceTrajectoryProvider::IsVehicleNearDestination(
    230. const common::VehicleState& vehicle_state,
    231. const std::vector<double>& end_pose, double rotate_angle,
    232. const Vec2d& translate_origin) {
    233. CHECK_EQ(end_pose.size(), 4U);
    234. Vec2d end_pose_to_world_frame = Vec2d(end_pose[0], end_pose[1]);
    235. end_pose_to_world_frame.SelfRotate(rotate_angle);
    236. end_pose_to_world_frame += translate_origin;
    237. double distance_to_vehicle2 =
    238. std::sqrt((vehicle_state.x() - end_pose_to_world_frame.x()) *
    239. (vehicle_state.x() - end_pose_to_world_frame.x()) +
    240. (vehicle_state.y() - end_pose_to_world_frame.y()) *
    241. (vehicle_state.y() - end_pose_to_world_frame.y()));
    242. double end_theta_to_world_frame = end_pose[2];
    243. end_theta_to_world_frame += rotate_angle;
    244. double distance_to_vehicle1 =
    245. std::sqrt((vehicle_state.x() - end_pose[0]) *
    246. (vehicle_state.x() - end_pose[0]) +
    247. (vehicle_state.y() - end_pose[1]) *
    248. (vehicle_state.y() - end_pose[1]));
    249. double distance_to_vehicle =
    250. std::sqrt((vehicle_state.x() - end_pose_to_world_frame.x()) *
    251. (vehicle_state.x() - end_pose_to_world_frame.x()) +
    252. (vehicle_state.y() - end_pose_to_world_frame.y()) *
    253. (vehicle_state.y() - end_pose_to_world_frame.y()));
    254. double theta_to_vehicle = std::abs(common::math::AngleDiff(
    255. vehicle_state.heading(), end_theta_to_world_frame));
    256. AERROR << "distance_to_vehicle1 is: " << distance_to_vehicle1;
    257. AERROR << "distance_to_vehicle2 is: " << distance_to_vehicle2;
    258. AERROR << "theta_to_vehicle" << theta_to_vehicle << "end_theta_to_world_frame"
    259. << end_theta_to_world_frame << "rotate_angle" << rotate_angle;
    260. AERROR << "is_near_destination_threshold"
    261. << config_.open_space_trajectory_provider_config()
    262. .open_space_trajectory_optimizer_config()
    263. .planner_open_space_config()
    264. .is_near_destination_threshold(); // which config file
    265. AERROR << "is_near_destination_theta_threshold"
    266. << config_.open_space_trajectory_provider_config()
    267. .open_space_trajectory_optimizer_config()
    268. .planner_open_space_config()
    269. .is_near_destination_theta_threshold();
    270. distance_to_vehicle = std::min(
    271. std::min(distance_to_vehicle, distance_to_vehicle1),
    272. distance_to_vehicle2);
    273. theta_to_vehicle = std::min(theta_to_vehicle,
    274. std::abs(vehicle_state.heading()));
    275. if (distance_to_vehicle < config_.open_space_trajectory_provider_config()
    276. .open_space_trajectory_optimizer_config()
    277. .planner_open_space_config()
    278. .is_near_destination_threshold() &&
    279. theta_to_vehicle < config_.open_space_trajectory_provider_config()
    280. .open_space_trajectory_optimizer_config()
    281. .planner_open_space_config()
    282. .is_near_destination_theta_threshold()) {
    283. AERROR << "vehicle reach end_pose";
    284. frame_->mutable_open_space_info()->set_destination_reached(true);
    285. return true;
    286. }
    287. return false;
    288. }
    289. bool OpenSpaceTrajectoryProvider::IsVehicleStopDueToFallBack(
    290. const bool is_on_fallback, const common::VehicleState& vehicle_state) {
    291. if (!is_on_fallback) {
    292. return false;
    293. }
    294. static constexpr double kEpsilon = 1.0e-1;
    295. const double adc_speed = vehicle_state.linear_velocity();
    296. const double adc_acceleration = vehicle_state.linear_acceleration();
    297. if (std::abs(adc_speed) < kEpsilon && std::abs(adc_acceleration) < kEpsilon) {
    298. ADEBUG << "ADC stops due to fallback trajectory";
    299. return true;
    300. }
    301. return false;
    302. }
    303. void OpenSpaceTrajectoryProvider::GenerateStopTrajectory(
    304. DiscretizedTrajectory* const trajectory_data) {
    305. double relative_time = 0.0;
    306. // TODO(Jinyun) Move to conf
    307. static constexpr int stop_trajectory_length = 10;
    308. static constexpr double relative_stop_time = 0.1;
    309. static constexpr double vEpsilon = 0.00001;
    310. double standstill_acceleration =
    311. frame_->vehicle_state().linear_velocity() >= -vEpsilon
    312. ? -FLAGS_open_space_standstill_acceleration
    313. : FLAGS_open_space_standstill_acceleration;
    314. trajectory_data->clear();
    315. for (size_t i = 0; i < stop_trajectory_length; i++) {
    316. TrajectoryPoint point;
    317. point.mutable_path_point()->set_x(frame_->vehicle_state().x());
    318. point.mutable_path_point()->set_y(frame_->vehicle_state().y());
    319. point.mutable_path_point()->set_theta(frame_->vehicle_state().heading());
    320. point.mutable_path_point()->set_s(0.0);
    321. point.mutable_path_point()->set_kappa(0.0);
    322. point.set_relative_time(relative_time);
    323. point.set_v(0.0);
    324. point.set_a(standstill_acceleration);
    325. trajectory_data->emplace_back(point);
    326. relative_time += relative_stop_time;
    327. }
    328. }
    329. void OpenSpaceTrajectoryProvider::LoadResult(
    330. DiscretizedTrajectory* const trajectory_data) {
    331. // Load unstitched two trajectories into frame for debug
    332. trajectory_data->clear();
    333. auto optimizer_trajectory_ptr =
    334. frame_->mutable_open_space_info()->mutable_optimizer_trajectory_data();
    335. auto stitching_trajectory_ptr =
    336. frame_->mutable_open_space_info()->mutable_stitching_trajectory_data();
    337. open_space_trajectory_optimizer_->GetOptimizedTrajectory(
    338. optimizer_trajectory_ptr);
    339. open_space_trajectory_optimizer_->GetStitchingTrajectory(
    340. stitching_trajectory_ptr);
    341. // Stitch two trajectories and load back to trajectory_data from frame
    342. size_t optimizer_trajectory_size = optimizer_trajectory_ptr->size();
    343. double stitching_point_relative_time =
    344. stitching_trajectory_ptr->back().relative_time();
    345. double stitching_point_relative_s =
    346. stitching_trajectory_ptr->back().path_point().s();
    347. for (size_t i = 0; i < optimizer_trajectory_size; ++i) {
    348. optimizer_trajectory_ptr->at(i).set_relative_time(
    349. optimizer_trajectory_ptr->at(i).relative_time() +
    350. stitching_point_relative_time);
    351. optimizer_trajectory_ptr->at(i).mutable_path_point()->set_s(
    352. optimizer_trajectory_ptr->at(i).path_point().s() +
    353. stitching_point_relative_s);
    354. }
    355. *(trajectory_data) = *(optimizer_trajectory_ptr);
    356. // Last point in stitching trajectory is already in optimized trajectory, so
    357. // it is deleted
    358. frame_->mutable_open_space_info()
    359. ->mutable_stitching_trajectory_data()
    360. ->pop_back();
    361. trajectory_data->PrependTrajectoryPoints(
    362. frame_->open_space_info().stitching_trajectory_data());
    363. frame_->mutable_open_space_info()->set_open_space_provider_success(true);
    364. }
    365. void OpenSpaceTrajectoryProvider::ReuseLastFrameResult(
    366. const Frame* last_frame, DiscretizedTrajectory* const trajectory_data) {
    367. *(trajectory_data) =
    368. last_frame->open_space_info().stitched_trajectory_result();
    369. frame_->mutable_open_space_info()->set_open_space_provider_success(true);
    370. }
    371. void OpenSpaceTrajectoryProvider::ReuseLastFrameDebug(const Frame* last_frame) {
    372. // reuse last frame's instance
    373. auto* ptr_debug = frame_->mutable_open_space_info()->mutable_debug_instance();
    374. ptr_debug->mutable_planning_data()->mutable_open_space()->MergeFrom(
    375. last_frame->open_space_info()
    376. .debug_instance()
    377. .planning_data()
    378. .open_space());
    379. }
    380. } // namespace planning
    381. } // namespace apollo

    构造obstacles_linesegment_vector。主要方法是按顺序从单个障碍点形成线段;然后,每个障碍线段都存储在将用于生成DP地图obstacles_linesegment_vector中。

    1. std::vector<std::vector<common::math::LineSegment2d>>
    2. obstacles_linesegments_vec;
    3. for (const auto& obstacle_vertices : obstacles_vertices_vec) {
    4. size_t vertices_num = obstacle_vertices.size();
    5. std::vector<common::math::LineSegment2d> obstacle_linesegments;
    6. for (size_t i = 0; i < vertices_num - 1; ++i) {
    7. common::math::LineSegment2d line_segment = common::math::LineSegment2d(
    8. obstacle_vertices[i], obstacle_vertices[i + 1]);
    9. obstacle_linesegments.emplace_back(line_segment);
    10. }
    11. obstacles_linesegments_vec.emplace_back(obstacle_linesegments);
    12. }
    13. obstacles_linesegments_vec_ = std::move(obstacles_linesegments_vec);

    构造与 Node3d 相同的计划点。计划的起点和终点以 Node3d 的形式构造,该节点将保存为打开集,并由有效性Check()函数进行检查。

    1. start_node_.reset(
    2. new Node3d({sx}, {sy}, {sphi}, XYbounds_, planner_open_space_config_));
    3. end_node_.reset(
    4. new Node3d({ex}, {ey}, {ephi}, XYbounds_, planner_open_space_config_));

    进入主 while 循环以获取一组节点。

    1. 当open_pq_为空时,将生成退出轨迹。open_pq_是 std::p riority_queue 类型,其中第一个元素表示打开集中节点的顺序,第二个元素表示按降序存储的节点的成本。

    2. 使用分析扩展()函数可以基于从当前点到目标端点reeds_shepp_path来确定是否存在无碰撞轨迹。如果存在,请退出 while 循环搜索。

      1. if (AnalyticExpansion(current_node)) {
      2. break;
      3. }
    3. 将当前点存储在闭合集中,并根据自行车运动学模型展开下一个节点。节点数是一个参数。通过Next_node_generator()函数生成下一个节点,并使用有效性Check()函数检测此节点。

    按节点生成粗轨迹。函数用于生成粗轨迹。

    bool HybridAStar::GetResult(HybridAStartResult* result)

     输出:可选输出是部分轨迹信息,包括 x、y、phi、v、a、转向、s。

    3 算法细节

    3.1 ValidityCheck

    bool HybridAStar::ValidityCheck(std::shared_ptr<Node3d> node)

    检测方法基于边界范围判断和边界重叠判断。

    参数:输入参数是与 node3d 相同的节点。

    简介:该功能用于检查碰撞。 

    处理细节:

    • 边界范围判断。如果节点的 x 和 y 超出边界的相应 x 和 y 的范围,则返回 false,则返回无效。
    • 边界重叠判断。如果车辆的边界框与任何线段重叠,则返回 false。通过线条和框是否相交来判断重叠。

    3.2 GenerateDpMap 

    1. bool GridSearch::GenerateDpMap(const double ex, const double ey,
    2. const std::vector<double>& XYbounds,
    3. const std::vectorvectormath::LineSegment2d>> &obstacles_linesegments_vec)

     该函数用于通过动态编程生成dp映射,如下

    1. #include "modules/planning/open_space/coarse_trajectory_generator/grid_search.h"
    2. namespace apollo {
    3. namespace planning {
    4. GridSearch::GridSearch(const PlannerOpenSpaceConfig& open_space_conf) {
    5. xy_grid_resolution_ =
    6. open_space_conf.warm_start_config().grid_a_star_xy_resolution();
    7. node_radius_ = open_space_conf.warm_start_config().node_radius();
    8. }
    9. double GridSearch::EuclidDistance(const double x1, const double y1,
    10. const double x2, const double y2) {
    11. return std::sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
    12. }
    13. bool GridSearch::CheckConstraints(std::shared_ptr<Node2d> node) {
    14. const double node_grid_x = node->GetGridX();
    15. const double node_grid_y = node->GetGridY();
    16. if (node_grid_x > max_grid_x_ || node_grid_x < 0 ||
    17. node_grid_y > max_grid_y_ || node_grid_y < 0) {
    18. return false;
    19. }
    20. if (obstacles_linesegments_vec_.empty()) {
    21. return true;
    22. }
    23. for (const auto& obstacle_linesegments : obstacles_linesegments_vec_) {
    24. for (const common::math::LineSegment2d& linesegment :
    25. obstacle_linesegments) {
    26. if (linesegment.DistanceTo({node->GetGridX(), node->GetGridY()}) <
    27. node_radius_) {
    28. return false;
    29. }
    30. }
    31. }
    32. return true;
    33. }
    34. std::vector<std::shared_ptr<Node2d>> GridSearch::GenerateNextNodes(
    35. std::shared_ptr<Node2d> current_node) {
    36. double current_node_x = current_node->GetGridX();
    37. double current_node_y = current_node->GetGridY();
    38. double current_node_path_cost = current_node->GetPathCost();
    39. double diagonal_distance = std::sqrt(2.0);
    40. std::vector<std::shared_ptr<Node2d>> next_nodes;
    41. std::shared_ptr<Node2d> up =
    42. std::make_shared<Node2d>(current_node_x, current_node_y + 1.0, XYbounds_);
    43. up->SetPathCost(current_node_path_cost + 1.0);
    44. std::shared_ptr<Node2d> up_right = std::make_shared<Node2d>(
    45. current_node_x + 1.0, current_node_y + 1.0, XYbounds_);
    46. up_right->SetPathCost(current_node_path_cost + diagonal_distance);
    47. std::shared_ptr<Node2d> right =
    48. std::make_shared<Node2d>(current_node_x + 1.0, current_node_y, XYbounds_);
    49. right->SetPathCost(current_node_path_cost + 1.0);
    50. std::shared_ptr<Node2d> down_right = std::make_shared<Node2d>(
    51. current_node_x + 1.0, current_node_y - 1.0, XYbounds_);
    52. down_right->SetPathCost(current_node_path_cost + diagonal_distance);
    53. std::shared_ptr<Node2d> down =
    54. std::make_shared<Node2d>(current_node_x, current_node_y - 1.0, XYbounds_);
    55. down->SetPathCost(current_node_path_cost + 1.0);
    56. std::shared_ptr<Node2d> down_left = std::make_shared<Node2d>(
    57. current_node_x - 1.0, current_node_y - 1.0, XYbounds_);
    58. down_left->SetPathCost(current_node_path_cost + diagonal_distance);
    59. std::shared_ptr<Node2d> left =
    60. std::make_shared<Node2d>(current_node_x - 1.0, current_node_y, XYbounds_);
    61. left->SetPathCost(current_node_path_cost + 1.0);
    62. std::shared_ptr<Node2d> up_left = std::make_shared<Node2d>(
    63. current_node_x - 1.0, current_node_y + 1.0, XYbounds_);
    64. up_left->SetPathCost(current_node_path_cost + diagonal_distance);
    65. next_nodes.emplace_back(up);
    66. next_nodes.emplace_back(up_right);
    67. next_nodes.emplace_back(right);
    68. next_nodes.emplace_back(down_right);
    69. next_nodes.emplace_back(down);
    70. next_nodes.emplace_back(down_left);
    71. next_nodes.emplace_back(left);
    72. next_nodes.emplace_back(up_left);
    73. return next_nodes;
    74. }
    75. bool GridSearch::GenerateAStarPath(
    76. const double sx, const double sy, const double ex, const double ey,
    77. const std::vector<double>& XYbounds,
    78. const std::vector<std::vector<common::math::LineSegment2d>>&
    79. obstacles_linesegments_vec,
    80. GridAStartResult* result) {
    81. std::priority_queue<std::pair<std::string, double>,
    82. std::vector<std::pair<std::string, double>>, cmp>
    83. open_pq;
    84. std::unordered_map<std::string, std::shared_ptr<Node2d>> open_set;
    85. std::unordered_map<std::string, std::shared_ptr<Node2d>> close_set;
    86. XYbounds_ = XYbounds;
    87. std::shared_ptr<Node2d> start_node =
    88. std::make_shared<Node2d>(sx, sy, xy_grid_resolution_, XYbounds_);
    89. std::shared_ptr<Node2d> end_node =
    90. std::make_shared<Node2d>(ex, ey, xy_grid_resolution_, XYbounds_);
    91. std::shared_ptr<Node2d> final_node_ = nullptr;
    92. obstacles_linesegments_vec_ = obstacles_linesegments_vec;
    93. open_set.emplace(start_node->GetIndex(), start_node);
    94. open_pq.emplace(start_node->GetIndex(), start_node->GetCost());
    95. // Grid a star begins
    96. size_t explored_node_num = 0;
    97. while (!open_pq.empty()) {
    98. std::string current_id = open_pq.top().first;
    99. open_pq.pop();
    100. std::shared_ptr<Node2d> current_node = open_set[current_id];
    101. // Check destination
    102. if (*(current_node) == *(end_node)) {
    103. final_node_ = current_node;
    104. break;
    105. }
    106. close_set.emplace(current_node->GetIndex(), current_node);
    107. std::vector<std::shared_ptr<Node2d>> next_nodes =
    108. std::move(GenerateNextNodes(current_node));
    109. for (auto& next_node : next_nodes) {
    110. if (!CheckConstraints(next_node)) {
    111. continue;
    112. }
    113. if (close_set.find(next_node->GetIndex()) != close_set.end()) {
    114. continue;
    115. }
    116. if (open_set.find(next_node->GetIndex()) == open_set.end()) {
    117. ++explored_node_num;
    118. next_node->SetHeuristic(
    119. EuclidDistance(next_node->GetGridX(), next_node->GetGridY(),
    120. end_node->GetGridX(), end_node->GetGridY()));
    121. next_node->SetPreNode(current_node);
    122. open_set.emplace(next_node->GetIndex(), next_node);
    123. open_pq.emplace(next_node->GetIndex(), next_node->GetCost());
    124. }
    125. }
    126. }
    127. if (final_node_ == nullptr) {
    128. AERROR << "Grid A searching return null ptr(open_set ran out)";
    129. return false;
    130. }
    131. LoadGridAStarResult(result);
    132. ADEBUG << "explored node num is " << explored_node_num;
    133. return true;
    134. }
    135. bool GridSearch::GenerateDpMap(
    136. const double ex, const double ey, const std::vector<double>& XYbounds,
    137. const std::vector<std::vector<common::math::LineSegment2d>>&
    138. obstacles_linesegments_vec) {
    139. std::priority_queue<std::pair<std::string, double>,
    140. std::vector<std::pair<std::string, double>>, cmp>
    141. open_pq;
    142. std::unordered_map<std::string, std::shared_ptr<Node2d>> open_set;
    143. dp_map_ = decltype(dp_map_)();
    144. XYbounds_ = XYbounds;
    145. // XYbounds with xmin, xmax, ymin, ymax
    146. max_grid_y_ = std::round((XYbounds_[3] - XYbounds_[2]) / xy_grid_resolution_);
    147. max_grid_x_ = std::round((XYbounds_[1] - XYbounds_[0]) / xy_grid_resolution_);
    148. std::shared_ptr<Node2d> end_node =
    149. std::make_shared<Node2d>(ex, ey, xy_grid_resolution_, XYbounds_);
    150. obstacles_linesegments_vec_ = obstacles_linesegments_vec;
    151. open_set.emplace(end_node->GetIndex(), end_node);
    152. open_pq.emplace(end_node->GetIndex(), end_node->GetCost());
    153. // Grid a star begins
    154. size_t explored_node_num = 0;
    155. while (!open_pq.empty()) {
    156. const std::string current_id = open_pq.top().first;
    157. open_pq.pop();
    158. std::shared_ptr<Node2d> current_node = open_set[current_id];
    159. dp_map_.emplace(current_node->GetIndex(), current_node);
    160. std::vector<std::shared_ptr<Node2d>> next_nodes =
    161. std::move(GenerateNextNodes(current_node));
    162. for (auto& next_node : next_nodes) {
    163. if (!CheckConstraints(next_node)) {
    164. continue;
    165. }
    166. if (dp_map_.find(next_node->GetIndex()) != dp_map_.end()) {
    167. continue;
    168. }
    169. if (open_set.find(next_node->GetIndex()) == open_set.end()) {
    170. ++explored_node_num;
    171. next_node->SetPreNode(current_node);
    172. open_set.emplace(next_node->GetIndex(), next_node);
    173. open_pq.emplace(next_node->GetIndex(), next_node->GetCost());
    174. } else {
    175. if (open_set[next_node->GetIndex()]->GetCost() > next_node->GetCost()) {
    176. open_set[next_node->GetIndex()]->SetCost(next_node->GetCost());
    177. open_set[next_node->GetIndex()]->SetPreNode(current_node);
    178. }
    179. }
    180. }
    181. }
    182. ADEBUG << "explored node num is " << explored_node_num;
    183. return true;
    184. }
    185. double GridSearch::CheckDpMap(const double sx, const double sy) {
    186. std::string index = Node2d::CalcIndex(sx, sy, xy_grid_resolution_, XYbounds_);
    187. if (dp_map_.find(index) != dp_map_.end()) {
    188. return dp_map_[index]->GetCost() * xy_grid_resolution_;
    189. } else {
    190. return std::numeric_limits<double>::infinity();
    191. }
    192. }
    193. void GridSearch::LoadGridAStarResult(GridAStartResult* result) {
    194. (*result).path_cost = final_node_->GetPathCost() * xy_grid_resolution_;
    195. std::shared_ptr<Node2d> current_node = final_node_;
    196. std::vector<double> grid_a_x;
    197. std::vector<double> grid_a_y;
    198. while (current_node->GetPreNode() != nullptr) {
    199. grid_a_x.push_back(current_node->GetGridX() * xy_grid_resolution_ +
    200. XYbounds_[0]);
    201. grid_a_y.push_back(current_node->GetGridY() * xy_grid_resolution_ +
    202. XYbounds_[2]);
    203. current_node = current_node->GetPreNode();
    204. }
    205. std::reverse(grid_a_x.begin(), grid_a_x.end());
    206. std::reverse(grid_a_y.begin(), grid_a_y.end());
    207. (*result).x = std::move(grid_a_x);
    208. (*result).y = std::move(grid_a_y);
    209. }
    210. } // namespace planning
    211. } // namespace apollo

    参数:ex和ey是目标点的位置,XYbounds_是x和y的边界,obstacles_linesegments_是由边界点组成的线段。

    简介:该函数用于生成dp图

    处理细节:

    • 根据网格分辨率对XYbounds_进行网格化,然后获取最大网格。
    • Dp映射存储节点的成本。

    3.3 AnalyticExpansion 

    bool HybridAStar::AnalyticExpansion(std::shared_ptr<Node3d> current_node)

    该函数用于检查分析曲线是否可以从当前配置连接到最终配置而不会发生冲突。如果是这样,搜索结束。

    参数:当前节点是规划的起点。

    简介:基于芦苇谢普法的函数,是一种由弧和线组成的几何算法。芦苇谢普用于搜索加速。

    处理细节:

    • 通过最短RSP()函数生成芦苇舍普斯路径。长度是最佳和最短的。
    • 检查路径是否由调用有效性检查()函数的RSPCheck()函数无冲突。
    • 将整个簧片 shepp 路径作为节点加载,并将节点添加到关闭集。

    3.4 CalculateNodeCost 

    1. void HybridAStar::CalculateNodeCost(std::shared_ptr<Node3d> current_node,
    2. std::shared_ptr<Node3d> next_node)

    该函数用于计算节点的成本。

    参数:当前节点(车辆位置)和下一个节点。

    简介:计算成本包括基于全息的考虑障碍物的轨迹成本和启发式成本。

    处理细节:

    • 轨迹成本包括当前节点的轨迹成本和从当前节点到下一个节点的轨迹成本。
    • 轨迹成本由采样距离、它们之间的换挡和转向变化率决定。
    • 启发式成本是从 dp 映射中获取的。

    本文参考GENERATE COARSE TRAJECTORY IN THE OPEN SPACE — Apollo Auto 0.0.1 文档 (daobook.github.io) 若有侵权,请联系删除

  • 相关阅读:
    ElasticSearch 报错 FORBIDDEN/12/index read-only / allow delete (api)
    10个打工人必备AI神器,升职加薪靠AI
    2022中国眼博会,山东眼健康展,视力矫正仪器展,护眼产品展
    【一起学Rust · 项目实战】命令行IO项目minigrep——重构优化模块和错误处理
    TCP和UDP C#代码实战
    Stream 流式编程 ( Java 8新特性 )
    德鲁伊数据库连接池的使用
    CentOS 7镜像下载 以及 DVD ISO 和 Minimal ISO 等各版本的区别介绍
    小李移动开发成长记 —— 大话小程序
    Vue----组件
  • 原文地址:https://blog.csdn.net/weixin_65089713/article/details/127449837