使用 ROS Melodic 和 Gazebo 9 运行示例应用程序。 - AWS RoboMaker

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

使用 ROS Melodic 和 Gazebo 9 运行示例应用程序。

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

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

├── HelloWorldSampleAppROSMelodicGazebo9 // Base Image │ └── Dockerfile ├── HelloWorldSampleAppROSMelodicGazebo9RobotApp // Image for Robot App │ ├── Dockerfile │ └── robot-entrypoint.sh ├── HelloWorldSampleAppROSMelodicGazebo9SimApp // Image for Simulation App │ ├── Dockerfile │ └── simulation-entrypoint.sh

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

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

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

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

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

创建基础映像

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

  1. 将以下命令保存在 Dockerfile 中。

    # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: MIT-0 FROM ros:melodic ENV DEBIAN_FRONTEND noninteractive RUN apt-get clean RUN apt-get update && apt-get install -y \ lsb \ unzip \ wget \ curl \ sudo \ python-vcstool \ python-rosinstall \ python3-colcon-common-extensions \ ros-melodic-rviz \ ros-melodic-rqt \ ros-melodic-rqt-common-plugins \ devilspie \ xfce4-terminal \ ros-melodic-gazebo-ros-pkgs \ ros-melodic-gazebo-ros-control \ ros-melodic-turtlebot3 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/ros1.zip && unzip ros1.zip' RUN sh -c 'cd /home/robomaker/workspace/aws-robomaker-sample-application-helloworld-ros1' RUN sudo rosdep fix-permissions RUN rosdep update
  2. 创建 Dockerfile 后,请在终端上使用以下命令进行构建。

    cd ../HelloWorldSampleAppROSMelodicGazebo9 docker build -t helloworldsampleapprosmelodicgazebo9:latest .

构建基础镜像会安装 ROS Melodic 和 Gazebo 9。您需要安装两个库才能成功运行应用程序。

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

创建基础映像后,您可以为机器人应用程序创建映像。

  1. 请将以下脚本保存在 Dockerfile 中并进行构建。此脚本下载 Hello World 机器人应用程序并对其进行设置。

    # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: MIT-0 FROM helloworldsampleapprosmelodicgazebo9:latest # Build the Robot application RUN cd /home/robomaker/workspace/aws-robomaker-sample-application-helloworld-ros1/robot_ws && \ /bin/bash -c "source /opt/ros/melodic/setup.bash && vcs import < .rosinstall && rosdep install --rosdistro melodic --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 roslaunch hello_world_robot rotate.launch ENTRYPOINT [ "/home/robomaker/robot-entrypoint.sh" ]
  2. 使用以下命令从 Dockerfile 中为机器人应用程序创建映像。

    cd HelloWorldSampleAppROSMelodicGazebo9RobotApp/HelloWorldSampleAppROSMelodicGazebo9RobotApp docker build -t helloworldsampleapprosmelodicgazebo9robotapp:latest image/.
  3. 以下是您可以另存为 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-ros1/robot_ws source /opt/ros/melodic/setup.bash source /usr/share/gazebo-9/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}"

为模拟应用程序创建映像

创建基础映像和机器人应用程序映像后,您可以为模拟应用程序创建映像。

  1. 请将以下脚本保存在 Dockerfile 中并进行构建。此脚本下载 Hello World 机器人应用程序并对其进行设置。

    # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: MIT-0 FROM helloworldsampleapprosmelodicgazebo9:latest # Build the Simulation application RUN cd /home/robomaker/workspace/aws-robomaker-sample-application-helloworld-ros1/simulation_ws && \ /bin/bash -c "source /opt/ros/melodic/setup.bash && vcs import < .rosinstall && rosdep install --rosdistro melodic --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 roslaunch hello_world_simulation empty_world.launch ENTRYPOINT [ "/home/robomaker/simulation-entrypoint.sh" ]
  2. 请保存以下 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-ros1/simulation_ws source /opt/ros/melodic/setup.bash source /usr/share/gazebo-9/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}"

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

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

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

    docker run -it -e DISPLAY -v /tmp/.X11-unix/:/tmp/.X11-unix/ \ -u robomaker -e ROBOMAKER_GAZEBO_MASTER_URI=http://localhost:5555 \ -e ROBOMAKER_ROS_MASTER_URI=http://localhost:11311 \ helloworldsampleapprosmelodicgazebo9robotapp:latest
    docker run -it -e DISPLAY -v /tmp/.X11-unix/:/tmp/.X11-unix/ \ -u robomaker -e ROBOMAKER_GAZEBO_MASTER_URI=http://localhost:5555 \ -e ROBOMAKER_ROS_MASTER_URI=http://localhost:11311 \ helloworldsampleapprosmelodicgazebo9simapp:latest
  2. 运行机器人应用程序和模拟应用程序容器时,可以使用 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 $ rosrun gazebo_ros gzclient
  3. 为映像添加标签以使其井井有条。使用以下命令为映像添加标签。

    docker tag helloworldsampleapprosmelodicgazebo9robotapp:latest accountID.dkr.ecr.us-west-2.amazonaws.com/helloworldsampleapprosmelodicgazebo9robotapp:latest
    docker tag helloworldsampleapprosmelodicgazebo9simapp:latest accountID.dkr.ecr.us-west-2.amazonaws.com/helloworldsampleapprosmelodicgazebo9simapp:latest
  4. 验证应用程序运行正常后,您可以使用以下命令推送到 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/helloworldsampleapprosmelodicgazebo9robotapp:latest docker push accountID.dkr.ecr.us-west-2.amazonaws.com/helloworldsampleapprosmelodicgazebo9simapp:latest

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