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

终止支持通知:2025 年 9 月 10 日, AWS 将停止对的支持。 AWS RoboMaker2025 年 9 月 10 日之后,您将无法再访问 AWS RoboMaker 控制台或 AWS RoboMaker 资源。有关过渡 AWS Batch 到以帮助运行容器化仿真的更多信息,请访问此博客文章。

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

使用 ROS 2 Foxy 和 Gazebo 11 运行示例应用程序

以下教程向您展示了如何通过创建和运行 Hello World 机器人应用程序和模拟应用程序,通过容器映像使用 ROS 2 Foxy 和 Gazebo 11 进行开发。您可以通过运行本文档中描述的命令使示例应用程序正常运行。

在本教程中,我们创建并使用了三个容器映像。下文展示了我们在此示例应用程序中使用的目录结构。

├── HelloWorldSampleAppROS2FoxyGazebo11 // Base Image │ └── Dockerfile ├── HelloWorldSampleAppROS2FoxyGazebo11RobotApp // Image for Robot App │ ├── Dockerfile │ └── robot-entrypoint.sh ├── HelloWorldSampleAppROS2FoxyGazebo11SimApp // Image for Simulation App │ ├── Dockerfile │ └── simulation-entrypoint.sh

每个 Dockerfile 都有构建每个映像所需的指令;

  • 基础映像的 Dockerfile 包含设置 ROS 和 Gazebo 的命令。

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

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

机器人应用程序和模拟应用程序都有一个入口点脚本。这些脚本为其各自的应用程序提供环境。它们为您设置了运行命令以启动机器人和模拟真应用程序的路径。

创建基础映像

要创建基础映像,请将示例中的命令保存在 Dockerfile 中以创建环境。然后,运行并构建 Dockerfile。

  • 将以下命令保存在 Dockerfile 中。

    # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: MIT-0 FROM ros:foxy ENV DEBIAN_FRONTEND noninteractive RUN apt-get clean RUN apt-get update && apt-get install -y \ lsb \ unzip \ wget \ curl \ sudo \ python3-vcstool \ python3-rosinstall \ python3-colcon-common-extensions \ ros-foxy-rviz2 \ ros-foxy-rqt \ ros-foxy-rqt-common-plugins \ devilspie \ xfce4-terminal RUN wget https://packages.osrfoundation.org/gazebo.key -O - | sudo apt-key add -; \ sh -c 'echo "deb http://packages.osrfoundation.org/gazebo/ubuntu-stable `lsb_release -cs` main" > /etc/apt/sources.list.d/gazebo-stable.list' RUN apt-get update && apt-get install -y gazebo11 ENV QT_X11_NO_MITSHM=1 ARG USERNAME=robomaker RUN groupadd $USERNAME RUN useradd -ms /bin/bash -g $USERNAME $USERNAME RUN sh -c 'echo "$USERNAME ALL=(root) NOPASSWD:ALL" >> /etc/sudoers' USER $USERNAME RUN sh -c 'cd /home/$USERNAME' # Download and build our Robot and Simulation application RUN sh -c 'mkdir -p /home/robomaker/workspace' RUN sh -c 'cd /home/robomaker/workspace && wget https://github.com/aws-robotics/aws-robomaker-sample-application-helloworld/archive/3527834.zip && unzip 3527834.zip && mv aws-robomaker-sample-application-helloworld-3527834771373beff0ed3630c13479567db4149e aws-robomaker-sample-application-helloworld-ros2' RUN sh -c 'cd /home/robomaker/workspace/aws-robomaker-sample-application-helloworld-ros2' RUN sudo rosdep fix-permissions RUN rosdep update

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

cd ../HelloWorldSampleAppROS2FoxyGazebo11 docker build -t helloworldsampleappros2foxygazebo11:latest .

构建基础映像安装 ROS 2 Foxy 和 Gazebo 11。您需要安装两个库才能成功运行应用程序。

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

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

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: MIT-0 FROM helloworldsampleappros2foxygazebo11: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" ]

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

cd HelloWorldSampleAppROS2FoxyGazebo11RobotApp/HelloWorldSampleAppROS2FoxyGazebo11RobotApp docker build -t helloworldsampleappros2foxygazebo11robotapp:latest .

以下是您可以另存为 robot-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/robot_ws source /opt/ros/foxy/setup.bash source /usr/share/gazebo-11/setup.sh source ./install/setup.sh if [ ! -z $tmp_GAZEBO_MASTER_URI ]; then export GAZEBO_MASTER_URI=$tmp_GAZEBO_MASTER_URI unset tmp_GAZEBO_MASTER_URI fi printenv exec "${@:1}"

为模拟应用程序创建映像

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

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: MIT-0 FROM helloworldsampleappros2foxygazebo11: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" ]

以下命令创建映像。

cd HelloWorldSampleAppROS2FoxyGazebo11SimApp/HelloWorldSampleAppROS2FoxyGazebo11SimApp docker build -t helloworldsampleappros2foxygazebo11simapp:latest .

以下是您可以另存为 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 source ./install/setup.sh if [ ! -z $tmp_GAZEBO_MASTER_URI ]; then export GAZEBO_MASTER_URI=$tmp_GAZEBO_MASTER_URI unset tmp_GAZEBO_MASTER_URI fi printenv exec "${@:1}"

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

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

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

docker run -it -e DISPLAY -v /tmp/.X11-unix/:/tmp/.X11-unix/ --name robot_app \ -u robomaker -e ROBOMAKER_GAZEBO_MASTER_URI=http://localhost:5555 \ -e ROBOMAKER_ROS_MASTER_URI=http://localhost:11311 \ helloworldsampleappros2foxygazebo11robotapp:latest
docker run -it -e DISPLAY -v /tmp/.X11-unix/:/tmp/.X11-unix/ --name sim_app \ -u robomaker -e ROBOMAKER_GAZEBO_MASTER_URI=http://localhost:5555 \ -e ROBOMAKER_ROS_MASTER_URI=http://localhost:11311 \ helloworldsampleappros2foxygazebo11simapp:latest

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

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

  2. 通过运行 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 sim_app bash # Launch Gazebo from within the container $ /home/robomaker/simulation-entrypoint.sh ros2 launch gazebo_ros gzclient.launch.py

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

docker tag helloworldsampleappros2foxygazebo11robotapp:latest accountID.dkr.ecr.us-west-2.amazonaws.com/helloworldsampleappros2foxygazebo11robotapp:latest
docker tag helloworldsampleappros2foxygazebo11simapp:latest accountID.dkr.ecr.us-west-2.amazonaws.com/helloworldsampleappros2foxygazebo11simapp: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/helloworldsampleappros2foxygazebo11robotapp:latest docker push accountID.dkr.ecr.us-west-2.amazonaws.com/helloworldsampleappros2foxygazebo11simapp:latest

然后,您可以对映像运行模拟作业。有关模拟作业的更多信息,请参阅 使用 AWS RoboMaker 进行模拟