在自动驾驶和机器人仿真领域,ROS 2和CARLA的结合已经成为行业标准配置。作为一名长期从事自动驾驶系统开发的工程师,我经常需要搭建这个开发环境。本文将分享我在Ubuntu 22.04系统上配置ROS 2和CARLA的完整流程,包含从基础环境准备到最终联调的每个细节。
这个环境搭建的核心价值在于:它提供了一个高保真的传感器仿真平台,能够在不使用真实车辆的情况下测试感知算法、规划控制模块。CARLA提供的高质量3D环境和物理引擎,配合ROS 2强大的通信框架,构成了自动驾驶开发的黄金组合。
Ubuntu 22.04是目前ROS 2 Humble官方支持的最新LTS版本。在开始前,请确保:
bash复制sudo apt update && sudo apt upgrade -y
bash复制sudo apt install -y build-essential cmake git wget curl
注意:建议使用全新安装的Ubuntu系统,避免因已有软件包冲突导致问题。我曾遇到过因残留的ROS 1包导致编译失败的情况。
CARLA需要NVIDIA显卡支持,推荐使用RTX 3060及以上显卡:
bash复制lspci | grep -i nvidia
bash复制sudo apt install -y nvidia-driver-515
bash复制wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-ubuntu2204.pin
sudo mv cuda-ubuntu2204.pin /etc/apt/preferences.d/cuda-repository-pin-600
sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/3bf863cc.pub
sudo add-apt-repository "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/ /"
sudo apt install -y cuda-11-7
安装完成后,验证驱动和CUDA:
bash复制nvidia-smi
nvcc --version
ROS 2 Humble是当前最稳定的LTS版本,完全支持Ubuntu 22.04:
bash复制sudo apt install -y software-properties-common
sudo add-apt-repository universe
sudo apt update && sudo apt install -y curl
sudo curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key -o /usr/share/keyrings/ros-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] http://packages.ros.org/ros2/ubuntu $(. /etc/os-release && echo $UBUNTU_CODENAME) main" | sudo tee /etc/apt/sources.list.d/ros2.list > /dev/null
bash复制sudo apt update
sudo apt install -y ros-humble-desktop
bash复制echo "source /opt/ros/humble/setup.bash" >> ~/.bashrc
source ~/.bashrc
建议为CARLA相关开发创建独立的工作空间:
bash复制mkdir -p ~/carla_ws/src
cd ~/carla_ws
colcon build
提示:我习惯使用colcon build --symlink-install,这样可以避免每次修改后重新安装Python包。
当前稳定版本是0.9.13,建议从源码编译:
bash复制sudo apt install -y clang-8 lld-8 g++-7 cmake ninja-build libvulkan1 python3-pip python3-dev python3-opengl
bash复制git clone --depth 1 --branch 0.9.13 https://github.com/carla-simulator/carla.git
cd carla
bash复制make PythonAPI
make launch
注意:编译过程可能需要1-2小时,取决于硬件配置。我曾在一台32核服务器上看到编译时间缩短到25分钟。
CARLA提供了Python API,需要单独安装:
bash复制pip3 install carla==0.9.13
验证安装:
python复制python3 -c "import carla; print(carla.__version__)"
这是官方维护的ROS 2与CARLA通信桥接包:
bash复制cd ~/carla_ws/src
git clone https://github.com/carla-simulator/ros-bridge.git
cd ..
rosdep install --from-paths src --ignore-src -r -y
colcon build
确保CARLA和ROS 2的环境变量正确设置:
bash复制echo "export CARLA_ROOT=/path/to/carla" >> ~/.bashrc
echo "export PYTHONPATH=$PYTHONPATH:${CARLA_ROOT}/PythonAPI/carla/dist/carla-0.9.13-py3.8-linux-x86_64.egg" >> ~/.bashrc
source ~/.bashrc
bash复制cd ~/carla
./CarlaUE4.sh
bash复制source ~/carla_ws/install/setup.bash
ros2 launch carla_ros_bridge carla_ros_bridge.launch.py
问题现象:启动时崩溃或黑屏
解决方案:
bash复制./CarlaUE4.sh -vulkan -RenderOffScreen
问题现象:出现"Connection refused"错误
解决方案:
bash复制ros2 param set /carla_ros_bridge host 127.0.0.1
问题现象:ImportError: No module named 'carla'
解决方案:
bash复制cd ~/carla
make PythonAPI
经过多次实践,我发现以下配置可以显著提升仿真性能:
bash复制./CarlaUE4.sh -quality-level=Low -benchmark -fps=20
yaml复制/carla_ros_bridge:
ros__parameters:
synchronous_mode: true
fixed_delta_seconds: 0.05
bash复制sudo sysctl -w vm.swappiness=10
sudo cpupower frequency-set -g performance
在实际项目中,这个环境已经支持我们团队完成了多个自动驾驶模块的开发和测试。从传感器数据采集到控制算法验证,整套流程的仿真效率比实车测试提升了至少5倍。特别是在复杂场景测试中,可以快速创建雨雪天气、交通拥堵等特殊条件,大大加速了开发迭代周期。