ApolloPlanning模块源代码分析
严正声明:本⽂系作者davidhopper原创,未经允许,严禁转载!
⼀、模块主⼊⼝
该模块的主⼊⼝为:modules/:
APOLLO_MAIN(apollo::planning::Planning)
该宏展开后为:
int main(int argc,char**argv){
google::InitGoogleLogging(argv[0]);
google::ParCommandLineFlags(&argc,&argv,true);
signal(SIGINT, apollo::common::apollo_app_sigint_handler);
apollo::planning::Planning apollo_app_;
ros::init(argc, argv, apollo_app_.Name());
apollo_app_.Spin();
return0;
}
Main函数完成以下⼯作:始化Google⽇志⼯具,使⽤Google命令⾏解析⼯具解析相关参数,注册接收中⽌信号“SIGINT”的处理函数:apollo::common::apollo_app_sigint_handler(该函数的功能⼗分简单,就是收到中⽌信号“SIGINT”后,调⽤ros::shutdown()关闭ROS),创建apollo::planning::Planning对象:apollo_app_,初始化ROS环境,调⽤apollo_app_.Spin()函数开始消息处理循环。
int ApolloApp::Spin(){
ros::AsyncSpinner spinner(callback_thread_num_);
auto status =Init();
if(!status.ok()){
AERROR <<Name()<<" Init failed: "<< status;
ReportModuleStatus(apollo::hmi::ModuleStatus::UNINITIALIZED);
return-1;
}
ReportModuleStatus(apollo::hmi::ModuleStatus::INITIALIZED);
status =Start();
if(!status.ok()){
AERROR <<Name()<<" Start failed: "<< status;
ReportModuleStatus(apollo::hmi::ModuleStatus::STOPPED);
return-2;
}
ExportFlags();
ReportModuleStatus(apollo::hmi::ModuleStatus::STARTED);
spinner.start();
ros::waitForShutdown();
Stop();
ReportModuleStatus(apollo::hmi::ModuleStatus::STOPPED);
AINFO <<Name()<<" exited.";
return0;
}
西安春节旅游攻略
apollo::planning::Planning类的继承关系见下图:
基本时序如下图所⽰:
现在来看ApolloApp::Spin()函数内部,⾸先创建ros::AsyncSpinner对象spinner,监控⽤户操作。之后调⽤虚函数:Init()(实际调⽤apollo::planning::Planning::Init())执⾏初始化。
Status Planning::Init(){
hdmap_ = apollo::hdmap::HDMapUtil::BaMapPtr();
CHECK(hdmap_)<<"Failed to load map file:"<< apollo::hdmap::BaMapFile();
正角CHECK(apollo::common::util::GetProtoFromFile(FLAGS_planning_config_file,
&config_))
<<"failed to load planning config file "<< FLAGS_planning_config_file;
if(!AdapterManager::Initialized()){
梦见跳水AdapterManager::Init(FLAGS_planning_adapter_config_filename);
}
if(AdapterManager::GetLocalization()==nullptr){
std::string error_msg("Localization is not registered");
AERROR << error_msg;
return Status(ErrorCode::PLANNING_ERROR, error_msg);
}
if(AdapterManager::GetChassis()==nullptr){
std::string error_msg("Chassis is not registered");
AERROR << error_msg;
return Status(ErrorCode::PLANNING_ERROR, error_msg);
}
if(AdapterManager::GetRoutingRespon()==nullptr){
std::string error_msg("RoutingRespon is not registered");
AERROR << error_msg;
return Status(ErrorCode::PLANNING_ERROR, error_msg);
}
if(AdapterManager::GetRoutingRequest()==nullptr){
std::string error_msg("RoutingRequest is not registered");
AERROR << error_msg;
return Status(ErrorCode::PLANNING_ERROR, error_msg);
}
女主暗恋男主if(FLAGS_enable_prediction && AdapterManager::GetPrediction()==nullptr){ std::string error_msg("Enabled prediction, but no prediction not enabled");
AERROR << error_msg;
return Status(ErrorCode::PLANNING_ERROR, error_msg);
}
if(FLAGS_enable_traffic_light &&过氧化氢
AdapterManager::GetTrafficLightDetection()==nullptr){
std::string error_msg("Traffic Light Detection is not registered");
AERROR << error_msg;
return Status(ErrorCode::PLANNING_ERROR, error_msg);
电脑自动关机是什么原因}
ReferenceLineProvider::instance()->Init(
hdmap_, config_.qp_spline_reference_line_smoother_config());
RegisterPlanners();
planner_ = planner_factory_.CreateObject(config_.planner_type());
if(!planner_){
return Status(
ErrorCode::PLANNING_ERROR,
"planning is not initialized with config : "+ config_.DebugString());
}
return planner_->Init(config_);
}
在apollo::planning::Planning::Init()函数内部,⾸先获取⾼精地图指针,之后执⾏适配器管理者(AdapterManager)对象的初始化状态,接着检查AdapterManager⾥定位适配器(AdapterManager::GetLocalization())、底盘适配器(AdapterManager::GetChassis())、路由寻径响应适配器(AdapterManager::GetRoutingRespon())、路由寻径请求适配器(AdapterManager::GetRoutingRequest())的初始化状态,若启⽤预测(FLAGS_enable_prediction)则继续检查预测适配器(AdapterManager::GetPrediction())的初始化状态,若启⽤交通信号灯
(FLAGS_enable_traffic_light)则继续检查交通信号灯适配器(AdapterManager::GetTrafficLightDetection())的初始化状态。接下来,执⾏参考线提供者(所谓参考线就是⼀条候选路径)的初始化。此后调⽤Planning::RegisterPlanners()将RTKReplayPlanner、EMPlanner对象的创建函数注册到计划者⼯⼚类对象planner_factory_中。另外通过读取配置⽂件中给定的计划者类型,使
⽤planner_factory_.CreateObject(config_.planner_type())动态⽣成所需的计划者对象(查询配置⽂件得知,实际⽣成EMPlanner对象)。最后执⾏计划者对象的初始化:planner_->Init(config_)。apollo::planning::Planning::Init()函数内,适配器管理者的初始化过程值得深⼊探讨:
AdapterManager::Init(FLAGS_planning_adapter_config_filename);
⾸先研究初始化传⼊的参数:FLAGS_planning_adapter_config_filename实际上是利⽤Google开源库gflags宏:
DEFINE_string(planning_adapter_config_filename,
"modules/planning/f",
"The adapter configuration file");
定义出的⼀个字符串变量,该变量的缺省值为:“modules/planning/f”,关于该变量的描述信息为: “The adapter configuration file”。配置⽂件:modules/planning/f的内容如下:
config {
type: LOCALIZATION
mode: RECEIVE_ONLY
message_history_limit: 1
}
config {
type: CHASSIS
mode: RECEIVE_ONLY
message_history_limit: 1
}
描写蝴蝶的句子config {
type: ROUTING_RESPONSE
mode: RECEIVE_ONLY
message_history_limit: 1
}
config {
type: ROUTING_REQUEST
mode: PUBLISH_ONLY
message_history_limit: 1
}合规管理工作总结
config {
type: PREDICTION
mode: RECEIVE_ONLY
message_history_limit: 10
}
config {
type: PLANNING_TRAJECTORY
mode: PUBLISH_ONLY
message_history_limit: 1
}
config {
type: TRAFFIC_LIGHT_DETECTION
mode: RECEIVE_ONLY
message_history_limit: 1
}
is_ros: true
该⽂件表明,Planning模块配置以下⼏种类型的适配器:LOCALIZATION(仅接收)、CHASSIS(仅接收)、
ROUTING_RESPONSE(仅接收)、ROUTING_REQUEST(仅发布)、PREDICTION(仅接收)、PLANNING_TRAJECTORY(仅发布)、TRAFFIC_LIGHT_DETECTION(仅接收),且使⽤ROS环境。
现在进⼊AdapterManager::Init(const std::string &adapter_config_filename)内部⼀探究竟:
void AdapterManager::Init(const std::string &adapter_config_filename){
/
/ Par config file
AdapterManagerConfig configs;
CHECK(util::GetProtoFromFile(adapter_config_filename,&configs))
<<"Unable to par adapter config file "<< adapter_config_filename;
AINFO <<"Init AdapterManger config:"<< configs.DebugString();
Init(configs);
}
顺藤摸⽠,继续深⼊AdapterManager::Init(const AdapterManagerConfig &configs)内部,前⾯我们通过查看配置⽂件f得知,此次配置类别为:LOCALIZATION、CHASSIS、ROUTING_RESPONSE、ROUTING_REQUEST、PREDICTION、
PLANNING_TRAJECTORY、TRAFFIC_LIGHT_DETECTION。
void AdapterManager::Init(const AdapterManagerConfig &configs){
if(Initialized()){
return;
}
instance()->initialized_ =true;
if(configs.is_ros()){
instance()->node_handle_.ret(new ros::NodeHandle());
}
for(const auto&config : fig()){
pe()){
// 省略不相关的配置类别
...
ca AdapterConfig::CHASSIS:
EnableChassis(FLAGS_chassis_topic, config);
break;
ca AdapterConfig::LOCALIZATION:
EnableLocalization(FLAGS_localization_topic, config);
break;
// 省略不相关的配置类别
...
ca AdapterConfig::TRAFFIC_LIGHT_DETECTION:
EnableTrafficLightDetection(FLAGS_traffic_light_detection_topic,
config);
/
/ 省略不相关的配置类别
...
ca AdapterConfig::ROUTING_REQUEST:
EnableRoutingRequest(FLAGS_routing_request_topic, config);
break;
ca AdapterConfig::ROUTING_RESPONSE:
EnableRoutingRespon(FLAGS_routing_respon_topic, config);
break;
ca AdapterConfig::PLANNING_TRAJECTORY:
EnablePlanning(FLAGS_planning_trajectory_topic, config);
break;
ca AdapterConfig::PREDICTION:
EnablePrediction(FLAGS_prediction_topic, config);
break;
// 省略不相关的配置类别
...
default:
AERROR <<"Unknown adapter config type!";
break;
}