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
The following code comprises the recipe for the core20 version of ROS 2 Talker/Listener core20.
Code
name: ros2-talker-listener
version: '0.1'
summary: ROS 2 Talker/Listener Example
description: |
  This example launches a ROS 2 talker and listener.
confinement: devmode
base: core20
parts:
  ros-demos:
    plugin: colcon
    source: https://github.com/ros2/demos.git
    source-branch: foxy
    source-subdir: demo_nodes_cpp
    stage-packages: [ros-foxy-ros2launch]
apps:
  ros2-talker-listener:
    command: opt/ros/foxy/bin/ros2 launch demo_nodes_cpp talker_listener.launch.py
    extensions: [ros2-foxy]
The following code comprises the recipe for the core20 version of ROS 2 Talker/Listener core22.
Code
name: ros2-talker-listener
version: '0.1'
summary: ROS 2 Talker/Listener Example
description: |
  This example launches a ROS 2 talker and listener.
confinement: devmode
base: core22
parts:
  ros-demos:
    plugin: colcon
    source: https://github.com/ros2/demos.git
    source-branch: humble
    source-subdir: demo_nodes_cpp
    stage-packages: [ros-humble-ros2launch]
apps:
  ros2-talker-listener:
    command: opt/ros/humble/bin/ros2 launch demo_nodes_cpp talker_listener.launch.py
    extensions: [ros2-humble]
The following code comprises the recipe for the core20 version of ROS 2 Talker/Listener core24.
Code
name: ros2-talker-listener
version: '0.1'
summary: ROS 2 Talker/Listener Example
description: |
  This example launches a ROS 2 talker and listener.
confinement: devmode
base: core24
parts:
  ros-demos:
    plugin: colcon
    source: https://github.com/ros2/demos.git
    source-branch: jazzy
    source-subdir: demo_nodes_cpp
    stage-packages: [ros-jazzy-ros2launch]
apps:
  ros2-talker-listener:
    command: ros2 launch demo_nodes_cpp talker_listener.launch.py
    extensions: [ros2-jazzy]
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:
- Declare the general app keys, such as - command.
- For - extensions, list the corresponding variant based on the core:- Core - Extension - core18 - None - core20 - core22 - core24 
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:
- Declare the general part keys, such as - source,- override-build,- build-packages, and so on.
- If you’re crafting for core18, set the following special keys: - Set - colcon-rosdistroto select the ROS distribution.
- Set - colcon-source-spaceto the path in the source tree where colcon packages are stored.
 
- 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.
