Example ROS 2 app

This how-to guide covers the steps, decisions, and implementation details that are unique when crafting a ROS 2-based snap. We’ll work through the aspects unique to ROS 2 apps by examining an existing recipe.

There are three supported bases for ROS 2 – core24, core20, and core18.

Example recipe for ROS 2 Talker/Listener

The following code comprises the recipe for the core18 version of ROS 2 Talker/Listener.

Code
name: ros2-talker-listener
version: '0.1'
summary: ROS 2 Talker/Listener Example
description: |
  This example launches a ROS 2 talker and listener.

base: core18
confinement: devmode

parts:
  ros-demos:
    plugin: colcon
    source: https://github.com/ros2/demos.git
    source-branch: dashing
    colcon-rosdistro: dashing
    colcon-source-space: demo_nodes_cpp
    stage-packages: [ros-dashing-ros2launch]

apps:
  ros2-talker-listener:
    command: opt/ros/dashing/bin/ros2 launch demo_nodes_cpp talker_listener.launch.py

Add an ROS 2 app

ROS 2 apps depend on special extensions that initialise the build- and run-time environments.

To add an ROS 2 app:

  1. Declare the general app keys, such as command.

  2. For extensions, list the corresponding variant based on the core:

    Core

    Extension

    core18

    None

    core20

    ros2-foxy

    core22

    ros2-humble

    core24

    ros2-jazzy

Add a part written for ROS 2

ROS 1 parts are built with the colcon plugin <https://snapcraft.io/docs/colcon-plugin>.

To add an ROS 2 part:

  1. Declare the general part keys, such as source, override-build, build-packages, and so on.

  2. If you’re crafting for core18, set the following special keys:

    • Set colcon-rosdistro to select the ROS distribution.

    • Set colcon-source-space to the path in the source tree where colcon packages are stored.

  3. For stage-packages, list the ROS launch command as a dependency, based on the core:

    Core

    Extension

    core18

    ros-dashing-ros2launch

    core20

    ros-foxy-ros2launch

    core22

    ros-humble-ros2launch

    core24

    ros-jazzy-ros2launch

Handle build issues

The following errors can occur while building for ROS 2.

core18 and core20

The warnings regarding missing libraries that you might see when building your snap are false positive. These libraries are build time dependencies only.

Share content between ROS 2 snaps

The core20, core22 and core24 bases also offer the option to build your ROS snap using the content-sharing interface. It shares the ROS 2 content packages across multiple snaps, saving space and ensuring package consistency throughout your snap build environment.

You can find more information in ROS architectures with snaps in the Canonical ROS documentation.

Turning on content sharing takes requires two small changes in the recipe. Here’s the difference in the recipe when content sharing is enabled:

source-subdir: demo_nodes_cpp
-  stage-packages: [ros-foxy-ros2launch]

apps:
  ros2-talker-listener:
    command: ros2 launch demo_nodes_cpp talker_listener.launch.py
-   extensions: [ros2-foxy]
+   extensions: [ros2-foxy-ros-base]

To turn on content sharing:

  1. Remove the stage-packages key from the part. The package is already available in the content-sharing snap.

  2. Change the ROS 2 extensions in extensions to the variant that correspond to the core:

    Core

    Content extension

    core20

    ros-foxy-ros-core

    core22

    ros2-humble-ros-base

    core24

    ros2-jazzy-ros-base

Because the snap makes use of the content provided by another snap, you must connect them through an interface before you can test the app.

To connect the snaps:

  1. Run:

    snap connect ros2-talker-listener:ros-foxy ros-foxy-ros-base
    
  2. Verify that the connection is established by running:

    snap connections ros2-talker-listener
    

    If the connection is successful, the output will show that through the content interface, the snap’s ROS launch command is manually plugged in to the ROS base snap.