Running a GPU sample application with ROS2 Foxy and Gazebo 11 - AWS RoboMaker

Running a GPU sample application with ROS2 Foxy and Gazebo 11

This tutorial explains how to use GPU drivers within container images to develop with ROS 2 Foxy and Gazebo 11 by creating and running the Hello World robot application and simulation application using three container images outlined in the following example.

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

Each Dockerfile contains the instructions needed to build each image.

  • The Dockerfile for the base image includes commands to set up ROS, Gazebo and GPU drivers.

  • The Dockerfile for the robot application includes the commands to set up the Hello World robot application.

  • The Dockerfile for the simulation application includes the commands to set up the Hello World simulation application.

Both the robot application and the simulation application have an entrypoint script. These scripts source the environments for their respective applications and set up the path for you to run commands to start your robot and simulation applications.

Creating a base GPU image

The following Dockerfile contains the commands to create a base image from NVIDIA OpenGL and install DCV.

  • Save the following commands in the Dockerfile in the SampleGPUBaseApp directory.

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

After you've created the Dockerfile, build it using the following commands on your terminal.

cd SampleGPUBaseApp docker build -t samplegpubaseapp:latest .

Building the base image installs ROS 2 Foxy, Gazebo 11, NVIDIA OpenGL, and NICE-DCV.

Creating an Image for the Robot Application

After you've created the base image, you can create the image for your robot application. Save the following script in the Dockerfile in the SampleGPURobotApp directory and build it. This script downloads the Hello World robot application and sets it up.

# 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" ]

The following are the contents of the script that you save as robot-entrypoint.sh. This script sources the environment for the robot application.

#!/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}"

The following command creates the image for the robot application from the Dockerfile.

cd SampleGPURobotApp docker build -t samplegpurobotapp:latest .

Creating an Image for the Simulation Application

Creating an Image for the Simulation Application

After you've created the base image and the image for the robot application, you can create the image for your simulation application. You save the following script in a Dockerfile in the SampleGPUSimulationApp directory and then build it. This script downloads the Hello World simulation application and sets it up.

# 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" ]

The following are the contents of the script that you save as simulation-entrypoint.sh. This script sources the environment for the simulation application.

#!/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}"

The following command creates the image.

cd SampleGPUSimulationApp docker build -t samplegpusimulationapp:latest .

Running the application and pushing it to Amazon ECR

After you've created your images, make sure they run properly in your local Linux environment. After you've checked that your image runs, you can push your Docker image to Amazon ECR and create a simulation job.

The following commands give you the ability to run the Hello World application in your local Linux environment.

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

When you run the robot application and simulation application containers, you can visualize the simulation using the Gazebo GUI tool. Use the following commands to:

  • Connect to your container running the simulation application.

  • Visualize your application by running the Gazebo Graphical User Interface (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

You can add tags to your images. The following commands give you the ability to tag your images.

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

After you've verified that the application is working properly, you can push it to Amazon ECR using the following commands.

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

You can now run a simulation job with GPU Compute using these images. For more information about simulation jobs, see Simulation with AWS RoboMaker.