使用 ROS2 Foxy 和 Gazebo 11 运行 GPU 示例应用程序 - AWS RoboMaker

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用 ROS2 Foxy 和 Gazebo 11 运行 GPU 示例应用程序

以下教程向您展示了如何使用以下示例中概述的三个容器映像通过创建和运行 Hello World 机器人应用程序和模拟应用程序,借助容器映像中的 GPU 驱动程序使用 ROS 2 Foxy 和 Gazebo 11 进行开发。

├── SampleGPUBaseApp // Base Image │ └── Dockerfile ├── SampleGPURobotApp // Image for Robot App │ ├── Dockerfile │ └── robot-entrypoint.sh ├── SampleGPUSimulationApp // Image for Simulation App │ ├── Dockerfile │ └── simulation-entrypoint.sh

每个 Dockerfile 都包含构建每个映像所需的指令。

  • 基础映像的 Dockerfile 包含设置 ROS、Gazebo 和 GPU 驱动程序的命令。

  • 机器人应用程序的 Dockerfile 包含设置 Hello World 机器人应用程序的命令。

  • 模拟应用程序的 Dockerfile 包含设置 Hello World 模拟应用程序的命令。

机器人应用程序和模拟应用程序都有一个入口点脚本。这些脚本为各自的应用程序获取环境,并为您设置运行命令以启动机器人和模拟应用程序的路径。

创建基础 GPU 映像

以下 Dockerfile 包含从 NVIDIA OpenGL 创建基础映像并安装 DCV 的命令。

  • 将以下命令保存在 SampleGPUBaseApp 目录下的 Dockerfile 中。

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: MIT-0 FROM nvidia/opengl:1.0-glvnd-runtime-ubuntu20.04 ENV DEBIAN_FRONTEND="noninteractive" ENV QT_X11_NO_MITSHM=1 RUN apt-get clean RUN apt-get update && apt-get install -y --no-install-recommends \ ca-certificates \ devilspie \ gnupg2 \ mesa-utils \ sudo \ unzip \ wget \ xfce4-terminal RUN wget https://d1uj6qtbmh3dt5.cloudfront.net/NICE-GPG-KEY && gpg --import NICE-GPG-KEY && \ wget https://d1uj6qtbmh3dt5.cloudfront.net/2021.2/Servers/nice-dcv-2021.2-11048-ubuntu1804-x86_64.tgz && \ tar xvzf nice-dcv-2021.2-11048-ubuntu1804-x86_64.tgz && \ cd nice-dcv-2021.2-11048-ubuntu1804-x86_64 && \ apt install -y ./nice-dcv-gl_2021.2.944-1_amd64.ubuntu1804.deb RUN apt update && apt -y install locales && \ locale-gen en_US en_US.UTF-8 && \ update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8 ENV LANG=en_US.UTF-8 RUN apt-get update && apt-get install -y --no-install-recommends curl lsb-release RUN curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key -o /usr/share/keyrings/ros-archive-keyring.gpg && \ curl -s https://raw.githubusercontent.com/ros/rosdistro/master/ros.asc | apt-key add - && \ echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] http://packages.ros.org/ros2/ubuntu $(lsb_release -cs) main" | tee /etc/apt/sources.list.d/ros2.list > /dev/null && \ apt update && \ apt install -y ros-foxy-desktop && \ /bin/bash -c "source /opt/ros/foxy/setup.bash" RUN apt -y install ros-foxy-gazebo-ros-pkgs RUN apt-key adv --fetch-keys 'http://packages.osrfoundation.org/gazebo.key' && \ apt update && \ apt install -y python3-rosdep git RUN if [ ! -f "/etc/ros/rosdep/sources.list.d/20-default.list" ]; then \ rosdep init; \ fi RUN rosdep update RUN apt-get install -y python3-apt python3-pip python3-vcstool python3-testresources RUN pip3 install -U pytest setuptools colcon-ros-bundle RUN useradd --create-home robomaker && \ sh -c 'echo "robomaker ALL=(root) NOPASSWD:ALL" >> /etc/sudoers' RUN sh -c 'mkdir -p /home/robomaker/workspace' && \ sh -c 'cd /home/robomaker/workspace && wget https://github.com/aws-robotics/aws-robomaker-sample-application-helloworld/archive/ros2.zip && unzip ros2.zip'

创建 Dockerfile 后,请在终端上使用以下命令进行构建。

cd SampleGPUBaseApp docker build -t samplegpubaseapp:latest .

构建基础映像安装 ROS 2 Foxy、Gazebo 11、NVIDIA OpenGL 和 NICE-DCV。

为机器人应用程序创建映像

创建基础映像后,您可以为机器人应用程序创建映像。将以下脚本保存在 SampleGPURobotApp 目录下的 Dockerfile 中并进行构建。此脚本下载 Hello World 机器人应用程序并对其进行设置。

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: MIT-0 FROM samplegpubaseapp:latest # Build the Robot application RUN cd /home/robomaker/workspace/aws-robomaker-sample-application-helloworld-ros2/robot_ws && \ /bin/bash -c "source /opt/ros/foxy/setup.bash && vcs import < .rosinstall && rosdep install --rosdistro foxy --from-paths src --ignore-src -r -y && colcon build" COPY robot-entrypoint.sh /home/robomaker/robot-entrypoint.sh RUN sh -c 'sudo chmod +x /home/robomaker/robot-entrypoint.sh' RUN sh -c 'sudo chown robomaker:robomaker /home/robomaker/robot-entrypoint.sh' CMD ros2 launch hello_world_robot rotate.launch.py ENTRYPOINT [ "/home/robomaker/robot-entrypoint.sh" ]

以下是您另存为 robot-entrypoint.sh 的脚本的内容。此脚本为机器人应用程序提供环境。

#!/bin/bash cd /home/robomaker/workspace/aws-robomaker-sample-application-helloworld-ros2/robot_ws source /opt/ros/foxy/setup.bash source /usr/share/gazebo-11/setup.sh source ./install/setup.sh printenv exec "${@:1}"

以下命令从 Dockerfile 中为机器人应用程序创建映像。

cd SampleGPURobotApp docker build -t samplegpurobotapp:latest .

为模拟应用程序创建映像

为模拟应用程序创建映像

创建基础映像和机器人应用程序映像后,您可以为模拟应用程序创建映像。您可以将以下脚本保存在 SampleGPUSimulationApp 目录下的 Dockerfile 中,然后进行构建。此脚本下载 Hello World 模拟应用程序并对其进行设置。

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: MIT-0 FROM samplegpubaseapp:latest # Build the Simulation application RUN cd /home/robomaker/workspace/aws-robomaker-sample-application-helloworld-ros2/simulation_ws && \ /bin/bash -c "source /opt/ros/foxy/setup.bash && vcs import < .rosinstall && rosdep install --rosdistro foxy --from-paths src --ignore-src -r -y && colcon build" COPY simulation-entrypoint.sh /home/robomaker/simulation-entrypoint.sh RUN sh -c 'sudo chmod +x /home/robomaker/simulation-entrypoint.sh' RUN sh -c 'sudo chown robomaker:robomaker /home/robomaker/simulation-entrypoint.sh' CMD ros2 launch hello_world_simulation empty_world.launch.py ENTRYPOINT [ "/home/robomaker/simulation-entrypoint.sh" ]

以下是您另存为 simulation-entrypoint.sh 的脚本的内容。此脚本为模拟应用程序提供环境。

#!/bin/bash if [ ! -z $GAZEBO_MASTER_URI ]; then tmp_GAZEBO_MASTER_URI=$GAZEBO_MASTER_URI fi cd /home/robomaker/workspace/aws-robomaker-sample-application-helloworld-ros2/simulation_ws source /opt/ros/foxy/setup.bash source /usr/share/gazebo-11/setup.sh if [ ! -z $tmp_GAZEBO_MASTER_URI ]; then export GAZEBO_MASTER_URI=$tmp_GAZEBO_MASTER_URI unset tmp_GAZEBO_MASTER_URI fi source ./install/setup.sh printenv exec "${@:1}"

以下命令创建映像。

cd SampleGPUSimulationApp docker build -t samplegpusimulationapp:latest .

运行应用程序并将其推送到 Amazon ECR

创建映像后,请确保它们在本地 Linux 环境中正常运行。检查映像是否运行后,您可以将您的 Docker 映像推送到 Amazon ECR 并创建模拟作业。

以下命令使您能够在本地 Linux 环境中运行该应用程序。

docker run -it -e DISPLAY -v /tmp/.X11-unix/:/tmp/.X11-unix/ --name gpu_robot_app \ -u robomaker -e ROBOMAKER_GAZEBO_MASTER_URI=http://localhost:5555 \ -e ROBOMAKER_ROS_MASTER_URI=http://localhost:11311 \ samplegpurobotapp:latest docker run -it -e DISPLAY -v /tmp/.X11-unix/:/tmp/.X11-unix/ --name gpu_sim_app \ -u robomaker -e ROBOMAKER_GAZEBO_MASTER_URI=http://localhost:5555 \ -e ROBOMAKER_ROS_MASTER_URI=http://localhost:11311 \ samplegpusimulationapp:latest

运行机器人应用程序和模拟应用程序容器时,可以使用 Gazebo GUI 工具对模拟进行可视化。使用以下命令:

  • 连接到运行模拟应用程序的容器。

  • 通过运行 Gazebo 图形用户界面 (GUI) 可视化您的应用程序。

# Enable access to X server to launch Gazebo from docker container $ xhost + # Check that the robot_app and sim_app containers are running. The command should list both containers $ docker container ls # Connect to the sim app container $ docker exec -it gpu_sim_app bash # Launch Gazebo from within the container $ /home/robomaker/simulation-entrypoint.sh ros2 launch gazebo_ros gzclient.launch.py

您可以为映像添加标签。以下命令使您能够为映像添加标签。

docker tag samplegpurobotapp:latest accountID.dkr.ecr.us-west-2.amazonaws.com/samplegpurobotapp:latest docker tag samplegpusimulationapp:latest accountID.dkr.ecr.us-west-2.amazonaws.com/samplegpusimulationapp:latest

验证应用程序运行正常后,您可以使用以下命令将其推送到 Amazon ECR。

aws ecr get-login-password --region us-west-2 | docker login --username AWS --password-stdin accountID.dkr.ecr.us-west-2.amazonaws.com docker push accountID.dkr.ecr.us-west-2.amazonaws.com/samplegpurobotapp:latest docker push accountID.dkr.ecr.us-west-2.amazonaws.com/samplegpusimulationapp:latest

现在,您可以使用这些映像在 GPU Compute 上运行模拟作业。有关模拟作业的更多信息,请参阅 使用 AWS RoboMaker 进行模拟