Baidu Apollo代码解析之Planning的结构与调用流程(1)
大家好,我已經(jīng)把CSDN上的博客遷移到了知乎上,歡迎大家在知乎關(guān)注我的專欄慢慢悠悠小馬車(https://zhuanlan.zhihu.com/duangduangduang)。希望大家可以多多交流,互相學(xué)習(xí)。
本文主要圍繞 apollo/modules/planning/ 模塊分析Apollo5.0是如何組織軌跡規(guī)劃的實(shí)現(xiàn)的。
1. Planner
在?apollo/modules/planning/planner/planner.h 文件中,定義了2個(gè)類:Planner類和 PlannerWithReferenceLine類。其中,Planner類是所有規(guī)劃器的基類,重要函數(shù)有Init() 和 Plan()。PlannerWithReferenceLine類?也是繼承自 Planner類,重要函數(shù)有PlanOnReferenceLine()。
planning 模塊中有4種規(guī)劃器,分別為:RTKReplayPlanner,PublicRoadPlanner,NaviPlanner,LatticePlanner。每個(gè)規(guī)劃器針對(duì)不同的場(chǎng)景和問(wèn)題。在這4個(gè)規(guī)劃器中,都實(shí)現(xiàn)了繼承自Planner類的Plan()函數(shù)和繼承自PlannerWithReferenceLine類的PlanOnReferenceLine()函數(shù)。在執(zhí)行具體的規(guī)劃任務(wù)時(shí),都是在Plan()中調(diào)用PlanOnReferenceLine(),從而獲得規(guī)劃的軌跡結(jié)果。也就是說(shuō),最底層的規(guī)劃方法,是在各規(guī)劃器的PlanOnReferenceLine()中實(shí)現(xiàn)。
/*** @class NaviPlanner* @brief NaviPlanner is a planner based on real-time relative maps. It uses the* vehicle's FLU (Front-Left-Up) coordinate system to accomplish tasks such as* cruising, following, overtaking, nudging, changing lanes and stopping.* Note that NaviPlanner is only used in navigation mode (turn on navigation* mode by setting "FLAGS_use_navigation_mode" to "true") and do not use it in* standard mode.*/ class NaviPlanner : public PlannerWithReferenceLine { ... } /*** @class PublicRoadPlanner* @brief PublicRoadPlanner is an expectation maximization planner.*/ class PublicRoadPlanner : public PlannerWithReferenceLine { ... } /*** @class RTKReplayPlanner* @brief RTKReplayPlanner is a derived class of Planner.* It reads a recorded trajectory from a trajectory file and* outputs proper segment of the trajectory according to vehicle* position.*/ class RTKReplayPlanner : public PlannerWithReferenceLine { ... }2. PlannerDispatcher
在 apollo\modules\planning\planner\planner_dispatcher.h 文件中定義了PlannerDispatcher類,用來(lái)根據(jù)預(yù)先設(shè)定的配置文件,選擇合適的planner。該類中重要的函數(shù)是DispatchPlanner()。
NaviPlannerDispatcher類繼承自PlannerDispatcher類,實(shí)現(xiàn)了DispatchPlanner()。由下面的代碼可知,其從config文件(apollo\modules\planning\conf\planning_config_navi.pb.txt)中讀取planner_type,然后創(chuàng)建對(duì)應(yīng)類型的planner。而配置文件中設(shè)定planner_type為“NAVI”,對(duì)應(yīng)NaviPlanner。
std::unique_ptr<Planner> NaviPlannerDispatcher::DispatchPlanner() {PlanningConfig planning_config;if (!apollo::cyber::common::GetProtoFromFile(FLAGS_planning_config_file,&planning_config)) {return nullptr;}auto planner_type = PlannerType::NAVI;if (planning_config.has_navigation_planning_config()) {planner_type = planning_config.navigation_planning_config().planner_type(0);}return planner_factory_.CreateObject(planner_type); }與NaviPlannerDispatcher類相似,OnLanePlannerDispatcher類也是繼承自PlannerDispatcher類,實(shí)現(xiàn)了DispatchPlanner()。不同的是其config文件(apollo\modules\planning\conf\planning_config.pb.txt)設(shè)定了planner_type為“PUBLIC_ROAD”,對(duì)應(yīng)PublicRoadPlanner。
3.?PlanningBase
在 apollo\modules\planning\planning_base.h 文件中,定義了PlanningBase類。該類有2個(gè)重要的函數(shù):RunOnce()和Plan(),還有幾個(gè)重要的成員變量,這里暫時(shí)只關(guān)注2個(gè):
std::unique_ptr<Planner> planner_; std::unique_ptr<PlannerDispatcher> planner_dispatcher_;PlanningBase類用來(lái)描述規(guī)劃的執(zhí)行過(guò)程,這樣就可以把通用的規(guī)劃過(guò)程(planning)和具體的規(guī)劃算法(planner)解耦,具有非常強(qiáng)的魯棒性。
NaviPlanning類繼承自PlanningBase類,實(shí)現(xiàn)了RunOnce()和Plan() 2個(gè)函數(shù)。在NaviPlanning類的構(gòu)造函數(shù)中,planner_dispatcher_(從PlanningBase類繼承而來(lái))被初始化為了NaviPlannerDispatcher類型的unique_ptr。然后,在NaviPlanning::Init()中,調(diào)用DispatchPlanner() 指定要使用的規(guī)劃器。RunOnce()被計(jì)時(shí)器周期性的觸發(fā)和運(yùn)行,在其中調(diào)用了NaviPlanning::Plan(),而NaviPlanning::Plan()中又調(diào)用了NaviPlanner::Plan()。
//NaviPlanning類的構(gòu)造函數(shù)中設(shè)定PlannerDispatcher NaviPlanning() {planner_dispatcher_ = std::make_unique<NaviPlannerDispatcher>(); } //NaviPlanning::Init()中設(shè)定Planner planner_ = planner_dispatcher_->DispatchPlanner();與NaviPlanning類相似,OnLanePlanning類也是繼承自PlanningBase類,實(shí)現(xiàn)了RunOnce()和Plan() 2個(gè)函數(shù)。在OnLanePlanning類的構(gòu)造函數(shù)中,planner_dispatcher_被初始化為了OnLanePlannerDispatcher類型的unique_ptr。然后,在OnLanePlanning::Init()中,調(diào)用DispatchPlanner() 指定要使用的規(guī)劃器。RunOnce()被計(jì)時(shí)器周期性的觸發(fā)和運(yùn)行,在其中調(diào)用了OnLanePlanning::Plan(),而OnLanePlanning::Plan()中又調(diào)用了PublicRoadPlanner::Plan()。
4. PlanningComponent
在apollo\modules\planning\planning_component.h?文件中定義了PlanningComponent類,該類包含一個(gè)PlanningBase類型的unique_ptr成員變量。
class PlanningComponent final: public cyber::Component<prediction::PredictionObstacles, canbus::Chassis,localization::LocalizationEstimate> { public:bool Init() override;bool Proc(const std::shared_ptr<prediction::PredictionObstacles>&prediction_obstacles,const std::shared_ptr<canbus::Chassis>& chassis,const std::shared_ptr<localization::LocalizationEstimate>&localization_estimate) override; private:std::unique_ptr<PlanningBase> planning_base_;... }在PlanningComponent::Proc()中,調(diào)用了PlanningBase類的RunOnce函數(shù)。根據(jù)繼承與虛函數(shù)的多態(tài)特性,相對(duì)應(yīng)的planning流程和planner會(huì)執(zhí)行具體的規(guī)劃任務(wù)。
ADCTrajectory adc_trajectory_pb; planning_base_->RunOnce(local_view_, &adc_trajectory_pb);總結(jié)
以上是生活随笔為你收集整理的Baidu Apollo代码解析之Planning的结构与调用流程(1)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: MySQL字符集(表情包)
- 下一篇: linux中使用redshift进行防蓝