当前位置:   article > 正文

Apollo9.0 Lattice Planner算法源码学习_apollo lattice planner

apollo lattice planner

一、概述

主文件:lattice_planner.cc,相关路径如下:

  1. modules\planning\planners\lattice\lattice_planner.cc
  2. modules\planning\planners\lattice\lattice_planner.h

 主程序函数:

  1. Status LatticePlanner::Plan(const TrajectoryPoint& planning_start_point,
  2. Frame* frame,
  3. ADCTrajectory* ptr_computed_trajectory) {}

二、主程序学习(详细注释) 

  1. /******************************************************************************
  2. * Copyright 2017 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. /**
  17. * @file
  18. **/
  19. #include "modules/planning/planners/lattice/lattice_planner.h"
  20. #include <limits>
  21. #include <memory>
  22. #include <utility>
  23. #include <vector>
  24. #include "cyber/common/log.h"
  25. #include "cyber/common/macros.h"
  26. #include "cyber/time/clock.h"
  27. #include "modules/common/math/cartesian_frenet_conversion.h"
  28. #include "modules/common/math/path_matcher.h"
  29. #include "modules/planning/planners/lattice/behavior/collision_checker.h"
  30. #include "modules/planning/planners/lattice/behavior/path_time_graph.h"
  31. #include "modules/planning/planners/lattice/behavior/prediction_querier.h"
  32. #include "modules/planning/planners/lattice/trajectory_generation/backup_trajectory_generator.h"
  33. #include "modules/planning/planners/lattice/trajectory_generation/lattice_trajectory1d.h"
  34. #include "modules/planning/planners/lattice/trajectory_generation/trajectory1d_generator.h"
  35. #include "modules/planning/planners/lattice/trajectory_generation/trajectory_combiner.h"
  36. #include "modules/planning/planners/lattice/trajectory_generation/trajectory_evaluator.h"
  37. #include "modules/planning/planning_base/gflags/planning_gflags.h"
  38. #include "modules/planning/planning_base/math/constraint_checker/constraint_checker.h"
  39. namespace apollo {
  40. namespace planning {
  41. using apollo::common::ErrorCode;
  42. using apollo::common::PathPoint;
  43. using apollo::common::Status;
  44. using apollo::common::TrajectoryPoint;
  45. using apollo::common::math::CartesianFrenetConverter;
  46. using apollo::common::math::PathMatcher;
  47. using apollo::cyber::Clock;
  48. namespace {
  49. //---------该函数将输入的参考线进行离散化---------//
  50. //---------输入:为ReferencePoint类型的vector----//
  51. //---------输出:为PathPoint类型的vector---------//
  52. std::vector<PathPoint> ToDiscretizedReferenceLine(
  53. const std::vector<ReferencePoint>& ref_points) {
  54. double s = 0.0;
  55. std::vector<PathPoint> path_points;
  56. for (const auto& ref_point : ref_points) {
  57. PathPoint path_point; //-----Pathpoint相当于定义的一种结构体,包含了很多路径点信息
  58. path_point.set_x(ref_point.x());
  59. path_point.set_y(ref_point.y());
  60. path_point.set_theta(ref_point.heading());
  61. path_point.set_kappa(ref_point.kappa());
  62. path_point.set_dkappa(ref_point.dkappa());
  63. if (!path_points.empty()) {
  64. double dx = path_point.x() - path_points.back().x();
  65. double dy = path_point.y() - path_points.back().y();
  66. s += std::sqrt(dx * dx + dy * dy); //-----s的计算方式:以直代曲!
  67. }
  68. path_point.set_s(s);
  69. path_points.push_back(std::move(path_point));
  70. }
  71. return path_points;
  72. }
  73. //---------该函数将计算规划起点的Frenet坐标(状态)---------//
  74. //---------其中调用了Cartesian 转 Frenet坐标的函数---------//
  75. void ComputeInitFrenetState(const PathPoint& matched_point,
  76. const TrajectoryPoint& cartesian_state,
  77. std::array<double, 3>* ptr_s,
  78. std::array<double, 3>* ptr_d) {
  79. CartesianFrenetConverter::cartesian_to_frenet(
  80. matched_point.s(), matched_point.x(), matched_point.y(),
  81. matched_point.theta(), matched_point.kappa(), matched_point.dkappa(),
  82. cartesian_state.path_point().x(), cartesian_state.path_point().y(),
  83. cartesian_state.v(), cartesian_state.a(),
  84. cartesian_state.path_point().theta(),
  85. cartesian_state.path_point().kappa(), ptr_s, ptr_d);
  86. }
  87. } // namespace
  88. /*-----------------------------------------------
  89. 以下的planner函数就是规划主函数
  90. 输入为:planning_start_point 规划起点;frame 一次规划所需要的所有环境信息
  91. ptr_computed_trajectory 待规划的轨迹??
  92. -----------------------------------------------*/
  93. Status LatticePlanner::Plan(const TrajectoryPoint& planning_start_point,
  94. Frame* frame,
  95. ADCTrajectory* ptr_computed_trajectory) {
  96. size_t success_line_count = 0;
  97. size_t index = 0;
  98. /*-----------------------------------------------
  99. 由于一个规划帧开始之前有定位和导航routing模块都会得到若干
  100. 条参考线,因此规划开始之前要根据给定的cost计算每条参考
  101. 线的cost,然后选择其中cost最低的一条进行离散化。SetPriorityCost
  102. -----------------------------------------------*/
  103. for (auto& reference_line_info : *frame->mutable_reference_line_info()) {
  104. if (index != 0) {
  105. reference_line_info.SetPriorityCost(
  106. FLAGS_cost_non_priority_reference_line);
  107. } else {
  108. reference_line_info.SetPriorityCost(0.0);
  109. }
  110. /*-----------------------------------------------
  111. PlanOnReferenceLine()该函数为Lattice算法中最重要的函数,
  112. 即:在选定cost最优的参考线之后,在该参考线上进行规划!
  113. -----------------------------------------------*/
  114. auto status =
  115. PlanOnReferenceLine(planning_start_point, frame, &reference_line_info);
  116. if (status != Status::OK()) {
  117. if (reference_line_info.IsChangeLanePath()) {
  118. AERROR << "Planner failed to change lane to "
  119. << reference_line_info.Lanes().Id();
  120. } else {
  121. AERROR << "Planner failed to " << reference_line_info.Lanes().Id();
  122. }
  123. } else {
  124. success_line_count += 1;
  125. }
  126. ++index;
  127. }
  128. if (success_line_count > 0) {
  129. return Status::OK();
  130. }
  131. return Status(ErrorCode::PLANNING_ERROR,
  132. "Failed to plan on any reference line.");
  133. }
  134. /*-----------------------------------------------
  135. 对每一条参考线都会执行以下规划(?)
  136. 以下为PlanOnReferenceLine()的具体实现,分为7个步骤:
  137. 1、离散化参考线上的点,并计算s的值(目的:以直代曲,
  138. 为了在进行坐标转化以及计算障碍物距离自车的s坐标的时
  139. 候可以使用,如制作index2s表格,即根据参考线上点的索
  140. 引号映射到s值的表)
  141. 2、在参考线上计算“规划起点”的匹配点
  142. 3、根据匹配点,计算Frenet坐标系的S-L值
  143. 4、计算障碍物的S-T图(斜率表示速度)
  144. 5、生成纵横向采样路径
  145. 6、计算cost值,进行碰撞检测(依据S-T图)和动力学约束检测
  146. 7、优选出cost值最小的trajectory
  147. -----------------------------------------------*/
  148. Status LatticePlanner::PlanOnReferenceLine(
  149. const TrajectoryPoint& planning_init_point, Frame* frame,
  150. ReferenceLineInfo* reference_line_info) {
  151. static size_t num_planning_cycles = 0;
  152. static size_t num_planning_succeeded_cycles = 0;
  153. double start_time = Clock::NowInSeconds();
  154. double current_time = start_time;
  155. ADEBUG << "Number of planning cycles: " << num_planning_cycles << " "
  156. << num_planning_succeeded_cycles;
  157. ++num_planning_cycles;
  158. reference_line_info->set_is_on_reference_line();
  159. // 1. obtain a reference line and transform it to the PathPoint format.
  160. // 以下为参考线离散化的过程:
  161. auto ptr_reference_line =
  162. std::make_shared<std::vector<PathPoint>>(ToDiscretizedReferenceLine(
  163. reference_line_info->reference_line().reference_points()));
  164. // 2. compute the matched point of the init planning point on the reference
  165. // line.
  166. // 以下将计算规划起点的匹配点
  167. // 该函数的实现是在 path_matcher.cc 文件里面
  168. PathPoint matched_point = PathMatcher::MatchToPath(
  169. *ptr_reference_line, planning_init_point.path_point().x(),
  170. planning_init_point.path_point().y());
  171. // 3. according to the matched point, compute the init state in Frenet frame.
  172. std::array<double, 3> init_s;
  173. s
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
  

闽ICP备14008679号