使用 ROS2 狡猾和涼亭 11 運行 GPU 示例應用程序 - AWS RoboMaker

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

使用 ROS2 狡猾和涼亭 11 運行 GPU 示例應用程序

本教學課程說明如何使用容器映像中的 GPU 驅動程式,透過使用下列範例概述的三個容器映像建立和執行 Hello World 機器人應用程式和模擬應用程式,以利用 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

每個 Docker 文件包含構建每個圖像所需的說明。

  • 基本圖像的碼頭文件包括用於設置 ROS,涼亭和 GPU 驅動程序的命令。

  • 自動機制應用程式的 Docker 檔案包含用於設定 Hello World 自動機制應用程式的指令。

  • 模擬應用程式的 Docker 檔案包括用於設置 Hello World 模擬應用程式的指令。

機器人應用程式和模擬應用程式都有入口點指令碼。這些指令碼為各自的應用程式提供環境的來源,並為您設定執行指令以啟動機器人和模擬應用程式的路徑。

建立基本 GPU 映像檔

下面的碼頭文件包含從 NVIDIA OpenGL 創建一個基本映像並安裝 DCV 的命令。

  • 將以下命令保存在碼頭文件中SampleGPUBaseApp目錄。

# 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'

創建 Docker 文件後,使用終端上的以下命令構建它。

cd SampleGPUBaseApp docker build -t samplegpubaseapp:latest .

構建基本映像安裝 ROS 2 狡猾的,涼亭 11,NVIDIA 的 OpenGL 和尼斯-DCV。

為自動機制應用程式建立影像

建立基本映像後,您可以為機器人應用程式建立映像。將以下腳本保存在碼頭文件SampleGPURobotApp目錄並構建它。此指令碼會下載 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}"

下列指令會從 Docker 檔案建立自動機制應用程式的影像。

cd SampleGPURobotApp docker build -t samplegpurobotapp:latest .

為模擬應用程式建立影像

為模擬應用程式建立影像

建立自動機械手應用程式的基本影像和影像後,您可以為模擬應用程式建立影像。您將以下腳本保存在碼頭文件中SampleGPUSimulationApp目錄,然後構建它。此指令碼會下載 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 .

執行應用程式並將其推送至亞馬遜 ECR

建立映像檔之後,請確定它們在您的本機 Linux 環境中正常執行。檢查映像是否執行之後,您可以將 Docker 映像推送到 Amazon ECR 並建立模擬任務。

以下命令使您能夠在本地 Linux 環境中運行 Hello World 應用程序。

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 工具將模擬視覺化。使用下列指令可以:

  • 連接至執行模擬應用程式的容器。

  • 通過運行涼亭圖形用戶界面(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 運算」執行模擬工作。如需模擬工作的更多資訊,請參閱使用模擬AWS RoboMaker