Apollo自动驾驶入门指南:从环境搭建到第一个感知模块实战

1次阅读
没有评论

共计 2531 个字符,预计需要花费 7 分钟才能阅读完成。

image.webp

为什么选择 Apollo 平台

刚接触自动驾驶开发时,最头疼的就是环境配置。传统开发方式需要手动安装 ROS、驱动、各种依赖库,版本冲突问题频出。我曾花了两周时间在 Ubuntu 18.04 上配置 Autoware,光是 CUDA 和 OpenCV 的版本兼容问题就折腾了好几天。更麻烦的是多传感器同步——雷达、相机、IMU 的时间戳对齐需要自己写代码实现,调试时就像在黑暗中摸索。

Apollo 自动驾驶入门指南:从环境搭建到第一个感知模块实战

相比之下,Apollo 提供了一站式解决方案:

  • Docker 化环境:所有依赖项预装在容器里,无需担心系统污染
  • 标准化接口:传感器驱动、通信协议都有统一规范
  • 可视化工具链:Dreamview 能实时显示所有模块的运行状态

开发环境配置实战

1. 基础环境准备

  1. 安装 Ubuntu 20.04(必须这个版本,其他版本会踩坑)
  2. 禁用 nouveau 驱动(否则会与 NVIDIA 驱动冲突)
  3. 安装 Docker CE 和 nvidia-docker2

关键命令:

# 安装 nvidia-docker
curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add -
distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list
sudo apt-get update
sudo apt-get install -y nvidia-docker2

2. 获取 Apollo 镜像

官方提供了带 CUDA 支持的镜像:

docker pull apolloauto/apollo:dev-x86_64-2023-03-15

启动容器时要挂载显卡设备:

./docker/scripts/dev_start.sh --gpu

第一个感知模块开发

激光雷达聚类示例代码

modules/perception/lidar_clustering 目录创建 Python 文件:

# 基于欧式距离的点云聚类
from cyber.python.cyber_py3 import record
from modules.drivers.proto.pointcloud_pb2 import PointCloud

def cluster_points(points, dist_threshold=0.5):
    """
    :param points: numpy array of shape (N,3)
    :param dist_threshold: 聚类距离阈值(米)
    :return: list of clustered point indices
    """
    clusters = []
    visited = set()

    for i in range(len(points)):
        if i not in visited:
            cluster = []
            queue = [i]
            while queue:
                idx = queue.pop(0)
                if idx not in visited:
                    visited.add(idx)
                    cluster.append(idx)
                    # 查找邻域点
                    distances = np.linalg.norm(points - points[idx], axis=1)
                    neighbors = np.where(distances < dist_threshold)[0]
                    queue.extend(neighbors)
            clusters.append(cluster)
    return clusters

集成到 Cyber RT 框架

  1. cyberfile.xml 中添加依赖:

    <depend>modules/drivers/proto</depend>

  2. 创建 DAG 配置文件:

    module_config {
      module_library : "lib/lidar_clustering.so"
      components {
        class_name : "LidarClustering"
        config {
          name : "lidar_clustering"
          readers {channel: "/apollo/sensor/lidar/points"}
        }
      }
    }

常见问题解决

Protobuf 版本冲突

错误现象:

[libprotobuf FATAL google/protobuf/stubs/common.cc:] This program requires version 3.6.1 of the Protocol Buffer runtime library...

解决方法:

# 在 Docker 容器内执行
pip uninstall protobuf
pip install protobuf==3.6.1

传感器标定校验

Apollo 要求标定文件必须是 YAML 格式,特别注意:

  • 外参矩阵必须是 4 ×4 的齐次矩阵
  • 时间延迟参数单位是秒
  • 标定文件存放路径:/apollo/modules/calibration/data/

性能优化建议

当感知模块处理延迟超过 100ms 时,会导致 Planning 模块收到过时的环境信息。可以通过以下方式优化:

  1. 在 Cyber RT 中配置高优先级线程池:

    // 在 Component 初始化时设置
    auto component = std::make_shared<LidarClustering>();
    component->Initialize("/path/to/config", true);  // 第二个参数启用高性能模式

  2. 点云处理使用 OpenMP 并行:

    from cython.parallel import prange
    
    # 替换原 for 循环为并行版本
    for i in prange(len(points), nogil=True):
        ...

进一步学习

试着修改聚类算法的距离阈值(0.3-1.0 米范围),然后在 Dreamview 中观察:

  • 感知结果框的变化
  • Planning 模块生成的轨迹如何调整
  • Control 模块的转向角响应速度

推荐延伸阅读:
Apollo 官方文档
Cyber RT 通信框架白皮书
传感器标定规范

通过这个实战项目,你应该已经掌握了 Apollo 开发的核心流程。下次我们可以尝试集成相机目标检测,实现多传感器融合感知。

正文完
 0
评论(没有评论)