/****************Apollo源码分析****************************
Copyright 2018 The File Authors & zouyu. All Rights Reserved.
Contact with: 1746430162@qq.com 181663309504
源码主要是c++实现的,也有少量python,git下载几百兆,其实代码不太多,主要是地图和数据了大量空间,主要程序
在apollo/modules目录中,
在apollo/modules目录中,
我们把它分成以下几部分(具体说明见各目录下的modules):
感知:感知当前位置,速度,障碍物等等
Apollo/modules/perception
预测:对场景下一步的变化做出预测
Apollo/modules/prediction
规划:
(1) 全局路径规划:通过起点终点计算行驶路径
Apollo/modules/routing
(2) 规划当前轨道:通过感知,预测,路径规划等信息计算轨道
Apollo/modules/planning
(3) 规划转换成命令:将轨道转换成控制汽车的命令(加速,制动,转向等)
Apollo/modules/control
其它
(1) 输入输出
i. Apollo/modules/drivers 设备驱动
ii. Apollo/modules/localization 位置信息
iii. Apollo/modules/monitor 监控模块
iv. Apollo/modules/canbus 与汽车硬件交互
v. Apollo/modules/map 地图数据
vi. Apollo/modules/third_party_perception 三方感知器支持
(2) 交互
i. Apollo/modules/dreamview 可视化模块
ii. Apollo/modules/hmi 把汽车当前状态显示给用户
(3) 工具
i. Apollo/modules/calibration 标注工具
ii. Apollo/modules/common 支持其它模块的公共工具
iii. Apollo/modules/data 数据工具
iv. Apollo/modules/tools 一些Python工具
(4) 其它
i. Apollo/modules/elo 高精度定位系统,无源码,但有文档
ii. Apollo/modules/e2e 收集传感器数据给PX2,ROS
自动驾驶系统先通过起点终点规划出整体路径(routing);然后在行驶过程中感知(perception)当前环境
(识别车辆行人路况标志等),并预测下一步发展;然后把已知信息都传入规划模块(planning),规划出之后的轨道;
控制模块(control)将轨道数据转换成对车辆的控制信号,通过汽车交互模块(canbus)控制汽车.
(识别车辆行人路况标志等),并预测下一步发展;然后把已知信息都传入规划模块(planning),规划出之后的轨道;
控制模块(control)将轨道数据转换成对车辆的控制信号,通过汽车交互模块(canbus)控制汽车.
我觉得这里面算法技术含量最高的是感知perception和规划planning,具体请见本博客中各模块的分析代码。
/****************************************************************************************
adapter_manager.cc是适配器管理的重要文件,它利用adapter_manager.h中声明的函数来配置车辆的初始化参数,并且传输消息。这个文件是理解阿波罗工程的关键文件之一,请仔细体会。
*****************************************************************************
* Copyright 2017 The Apollo Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*****************************************************************************/
#include "modules/common/adapters/adapter_manager.h"
#include "modules/common/adapters/adapter_gflags.h"
#include "modules/common/util/util.h"
namespace apollo {
namespace common {
namespace adapter {
AdapterManager::AdapterManager() {}
void AdapterManager::Observe() {
for (const auto observe : instance()->observers_) {
observe();
}
}
bool AdapterManager::Initialized() { return instance()->initialized_; }
void AdapterManager::Reset() {
instance()->initialized_ = false;
instance()->observers_.clear();
}
void AdapterManager::Init(const std::string &adapter_config_filename) {
// Parse config file
AdapterManagerConfig configs;
CHECK(util::GetProtoFromFile(adapter_config_filename, &configs))
<< "Unable to parse adapter config file " << adapter_config_filename;
AINFO << "Init AdapterManger config:" << configs.DebugString();
Init(configs);
}
void AdapterManager::Init(const AdapterManagerConfig &configs) {
if (Initialized()) {
return;
}
instance()->initialized_ = true;
if (configs.is_ros()) {
instance()->node_handle_.reset(new ros::NodeHandle());
}
for (const auto &config : configs.config()) {
switch (config.type()) {
case AdapterConfig::POINT_CLOUD:
EnablePointCloud(FLAGS_pointcloud_topic, config);
break;
case AdapterConfig::GPS:
EnableGps(FLAGS_gps_topic, config);
break;
case AdapterConfig::IMU:
EnableImu(FLAGS_imu_topic, config);
break;
case AdapterConfig::RAW_IMU:
EnableRawImu(FLAGS_raw_imu_topic, config);
break;
case AdapterConfig::CHASSIS:
EnableChassis(FLAGS_chassis_topic, config);
break;
case AdapterConfig::LOCALIZATION:
EnableLocalization(FLAGS_localization_topic, config);
break;
case AdapterConfig::PERCEPTION_OBSTACLES:
EnablePerceptionObstacles(FLAGS_perception_obstacle_topic, config);
break;
case AdapterConfig::TRAFFIC_LIGHT_DETECTION:
EnableTrafficLightDetection(FLAGS_traffic_light_detection_topic,
config);
break;
case AdapterConfig::PAD:
EnablePad(FLAGS_pad_topic, config);
break;
case AdapterConfig::CONTROL_COMMAND:
EnableControlCommand(FLAGS_control_command_topic, config);
break;
case AdapterConfig::ROUTING_REQUEST:
EnableRoutingRequest(FLAGS_routing_request_topic, config);
break;
case AdapterConfig::ROUTING_RESPONSE:
EnableRoutingResponse(FLAGS_routing_response_topic, config);
break;
case AdapterConfig::PLANNING_TRAJECTORY:
EnablePlanning(FLAGS_planning_trajectory_topic, config);
break;
case AdapterConfig::PREDICTION:
EnablePrediction(FLAGS_prediction_topic, config);
break;
case AdapterConfig::MONITOR:
EnableMonitor(FLAGS_monitor_topic, config);
break;
case AdapterConfig::CHASSIS_DETAIL:
EnableChassisDetail(FLAGS_chassis_detail_topic, config);
break;
case AdapterConfig::RELATIVE_ODOMETRY:
EnableRelativeOdometry(FLAGS_relative_odometry_topic, config);
break;
case AdapterConfig::INS_STAT:
EnableInsStat(FLAGS_ins_stat_topic, config);
break;
case AdapterConfig::INS_STATUS:
EnableInsStatus(FLAGS_ins_status_topic, config);
break;
case AdapterConfig::GNSS_STATUS:
EnableGnssStatus(FLAGS_gnss_status_topic, config);
break;
case AdapterConfig::SYSTEM_STATUS:
EnableSystemStatus(FLAGS_system_status_topic, config);
break;
case AdapterConfig::STATIC_INFO:
EnableStaticInfo(FLAGS_static_info_topic, config);
break;
case AdapterConfig::MOBILEYE:
EnableMobileye(FLAGS_mobileye_topic, config);
break;
case AdapterConfig::DELPHIESR:
EnableDelphiESR(FLAGS_delphi_esr_topic, config);
break;
case AdapterConfig::CONTI_RADAR:
EnableContiRadar(FLAGS_conti_radar_topic, config);
break;
case AdapterConfig::ULTRASONIC_RADAR:
EnableUltrasonic(FLAGS_ultrasonic_radar_topic, config);
break;
case AdapterConfig::COMPRESSED_IMAGE:
EnableCompressedImage(FLAGS_compressed_image_topic, config);
break;
case AdapterConfig::IMAGE_SHORT:
EnableImageShort(FLAGS_image_short_topic, config);
break;
case AdapterConfig::IMAGE_LONG:
EnableImageLong(FLAGS_image_long_topic, config);
break;
case AdapterConfig::DRIVE_EVENT:
EnableDriveEvent(FLAGS_drive_event_topic, config);
break;
case AdapterConfig::GNSS_RTK_OBS:
EnableGnssRtkObs(FLAGS_gnss_rtk_obs_topic, config);
break;
case AdapterConfig::GNSS_RTK_EPH:
EnableGnssRtkEph(FLAGS_gnss_rtk_eph_topic, config);
break;
case AdapterConfig::GNSS_BEST_POSE:
EnableGnssBestPose(FLAGS_gnss_best_pose_topic, config);
break;
case AdapterConfig::LOCALIZATION_MSF_GNSS:
EnableLocalizationMsfGnss(FLAGS_localization_gnss_topic, config);
break;
case AdapterConfig::LOCALIZATION_MSF_LIDAR:
EnableLocalizationMsfLidar(FLAGS_localization_lidar_topic, config);
break;
case AdapterConfig::LOCALIZATION_MSF_SINS_PVA:
EnableLocalizationMsfSinsPva(FLAGS_localization_sins_pva_topic, config);
break;
case AdapterConfig::LOCALIZATION_MSF_STATUS:
EnableLocalizationMsfStatus(FLAGS_localization_msf_status, config);
break;
case AdapterConfig::RELATIVE_MAP:
EnableRelativeMap(FLAGS_relative_map_topic, config);
break;
case AdapterConfig::NAVIGATION:
EnableNavigation(FLAGS_navigation_topic, config);
break;
default:
AERROR << "Unknown adapter config type!";
break;
}
}
}
} // namespace adapter
} // namespace common
} // namespace apollo
下面是简要分析:
/******************************************************************************
* Copyright 2017 The Apollo Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*****************************************************************************/
#include
"modules/common/adapters/adapter_manager.h"
//包括上讲讲到的适配器管理的头文件,
//这个头文件非常清楚的声明了各个适配器的消息。
//这个头文件非常清楚的声明了各个适配器的消息。
#include
"modules/common/adapters/adapter_gflags.h"
//一个接口文件。
#include
"modules/common/util/util.h"
namespace
apollo
{
namespace
common
{
namespace
adapter
{
AdapterManager::AdapterManager
() {}
void
AdapterManager::Observe
() {
//在头文件adapter_manager.h中声明了AdapterManager这个类,
//然后,类里有Observe成员。
//然后,类里有Observe成员。
for
(
const
auto
observe :
instance
()->
observers_
) {
observe
();
}
}
bool
AdapterManager::Initialized
() {
return
instance
()->
initialized_
; }
//初始化
void
AdapterManager::Reset
() {
//重启。
instance
()->
initialized_
=
false
;
instance
()->
observers_
.
clear
();
}
void
AdapterManager::Init
(
const
std::string
&
adapter_config_filename) {
//初始化配置文件,在自动驾驶车辆自动
的时候,有一个配置文件要初始化,这代表了车辆以什么样的状态开始。
的时候,有一个配置文件要初始化,这代表了车辆以什么样的状态开始。
// Parse config file
AdapterManagerConfig configs;
CHECK
(
util::GetProtoFromFile
(adapter_config_filename,
&
configs))
<<
"Unable to parse adapter config file "
<<
adapter_config_filename;
AINFO
<<
"Init AdapterManger config:"
<<
configs.
DebugString
();
Init
(configs);
}
void
AdapterManager::Init
(
const
AdapterManagerConfig
&
configs) {
if
(
Initialized
()) {
return
;
}
instance
()->
initialized_
=
true
;
if
(configs.
is_ros
()) {
instance
()->
node_handle_
.
reset
(
new
ros::NodeHandle
());
}
for
(
const
auto
&
config : configs.
config
()) {
switch
(config.
type
()) {
case
AdapterConfig::POINT_CLOUD:
//这里就是配置初始化要传递过来的参数,
有点云、GPS、IMU、底盘参数等等。自己可以看一下CASE里判断的类别。
有点云、GPS、IMU、底盘参数等等。自己可以看一下CASE里判断的类别。
EnablePointCloud
(FLAGS_pointcloud_topic, config);
break
;
case
AdapterConfig::GPS:
EnableGps
(FLAGS_gps_topic, config);
break
;
case
AdapterConfig::IMU:
EnableImu
(FLAGS_imu_topic, config);
break
;
case
AdapterConfig::RAW_IMU:
EnableRawImu
(FLAGS_raw_imu_topic, config);
break
;
case
AdapterConfig::CHASSIS:
EnableChassis
(FLAGS_chassis_topic, config);
break
;
case
AdapterConfig::LOCALIZATION:
EnableLocalization
(FLAGS_localization_topic, config);
break
;
case
AdapterConfig::PERCEPTION_OBSTACLES:
EnablePerceptionObstacles
(FLAGS_perception_obstacle_topic, config);
break
;
case
AdapterConfig::TRAFFIC_LIGHT_DETECTION:
EnableTrafficLightDetection
(FLAGS_traffic_light_detection_topic,
config);
break
;
case
AdapterConfig::PAD:
EnablePad
(FLAGS_pad_topic, config);
break
;
case
AdapterConfig::CONTROL_COMMAND:
EnableControlCommand
(FLAGS_control_command_topic, config);
break
;
case
AdapterConfig::ROUTING_REQUEST:
EnableRoutingRequest
(FLAGS_routing_request_topic, config);
break
;
case
AdapterConfig::ROUTING_RESPONSE:
EnableRoutingResponse
(FLAGS_routing_response_topic, config);
break
;
case
AdapterConfig::PLANNING_TRAJECTORY:
EnablePlanning
(FLAGS_planning_trajectory_topic, config);
break
;
case
AdapterConfig::PREDICTION:
EnablePrediction
(FLAGS_prediction_topic, config);
break
;
case
AdapterConfig::MONITOR:
EnableMonitor
(FLAGS_monitor_topic, config);
break
;
case
AdapterConfig::CHASSIS_DETAIL:
EnableChassisDetail
(FLAGS_chassis_detail_topic, config);
break
;
case
AdapterConfig::RELATIVE_ODOMETRY:
EnableRelativeOdometry
(FLAGS_relative_odometry_topic, config);
break
;
case
AdapterConfig::INS_STAT:
EnableInsStat
(FLAGS_ins_stat_topic, config);
break
;
case
AdapterConfig::INS_STATUS:
EnableInsStatus
(FLAGS_ins_status_topic, config);
break
;
case
AdapterConfig::GNSS_STATUS:
EnableGnssStatus
(FLAGS_gnss_status_topic, config);
break
;
case
AdapterConfig::SYSTEM_STATUS:
EnableSystemStatus
(FLAGS_system_status_topic, config);
break
;
case
AdapterConfig::STATIC_INFO:
EnableStaticInfo
(FLAGS_static_info_topic, config);
break
;
case
AdapterConfig::MOBILEYE:
EnableMobileye
(FLAGS_mobileye_topic, config);
break
;
case
AdapterConfig::DELPHIESR:
EnableDelphiESR
(FLAGS_delphi_esr_topic, config);
break
;
case
AdapterConfig::CONTI_RADAR:
EnableContiRadar
(FLAGS_conti_radar_topic, config);
break
;
case
AdapterConfig::ULTRASONIC_RADAR:
EnableUltrasonic
(FLAGS_ultrasonic_radar_topic, config);
break
;
case
AdapterConfig::COMPRESSED_IMAGE:
EnableCompressedImage
(FLAGS_compressed_image_topic, config);
break
;
case
AdapterConfig::IMAGE_SHORT:
EnableImageShort
(FLAGS_image_short_topic, config);
break
;
case
AdapterConfig::IMAGE_LONG:
EnableImageLong
(FLAGS_image_long_topic, config);
break
;
case
AdapterConfig::DRIVE_EVENT:
EnableDriveEvent
(FLAGS_drive_event_topic, config);
break
;
case
AdapterConfig::GNSS_RTK_OBS:
EnableGnssRtkObs
(FLAGS_gnss_rtk_obs_topic, config);
break
;
case
AdapterConfig::GNSS_RTK_EPH:
EnableGnssRtkEph
(FLAGS_gnss_rtk_eph_topic, config);
break
;
case
AdapterConfig::GNSS_BEST_POSE:
EnableGnssBestPose
(FLAGS_gnss_best_pose_topic, config);
break
;
case
AdapterConfig::LOCALIZATION_MSF_GNSS:
EnableLocalizationMsfGnss
(FLAGS_localization_gnss_topic, config);
break
;
case
AdapterConfig::LOCALIZATION_MSF_LIDAR:
EnableLocalizationMsfLidar
(FLAGS_localization_lidar_topic, config);
break
;
case
AdapterConfig::LOCALIZATION_MSF_SINS_PVA:
EnableLocalizationMsfSinsPva
(FLAGS_localization_sins_pva_topic, config);
break
;
case
AdapterConfig::LOCALIZATION_MSF_STATUS:
EnableLocalizationMsfStatus
(FLAGS_localization_msf_status, config);
break
;
case
AdapterConfig::RELATIVE_MAP:
EnableRelativeMap
(FLAGS_relative_map_topic, config);
break
;
case
AdapterConfig::NAVIGATION:
EnableNavigation
(FLAGS_navigation_topic, config);
break
;
default
:
AERROR
<<
"Unknown adapter config type!"
;
break
;
}
}
}
}
// namespace adapter
}
// namespace common
}
// namespace apollo