We’ve just lately celebrated the discharge of ROS 2 Humble Hawksbill with a submit detailing the best way to get began creating for the brand new launch in containers. As well as, we shared an outline of the brand new options included with this new launch, significantly its enhanced safety features.
This week we’re tackling the logical subsequent step in software program growth: packaging. Certainly, as soon as we’re achieved creating our tremendous cool ROS 2 Humble software, we nonetheless should get it out into the arms of our customers.
On this submit, we’re going to see the best way to package deal a ROS 2 Humble software as a snap with an ‘hey world’-like instance.
A ROS 2 Humble snap you stated?
Snaps are app packages for desktop, cloud and IoT which might be simple to put in, safe, cross‐platform and dependency‐free.
Snaps are the perfect deployment answer for ROS-based functions in that they’re a self-contained, sandboxed, cross distribution packaging answer. To not point out that they natively assist ROS to ease your packaging journey. You will discover additional details about all of the options they provide on the devoted robotics web page!
Organising snapcraft
Firstly, let’s get the device that permits us to create snaps: snapcraft.
sudo snap set up --classic snapcraft
We may also set up LXD which snapcraft makes use of as a backend for clear, confined and reproducible packaging,
sudo snap set up lxd
sudo lxd init --minimal
Observe that we now have initialized LXD with a bunch of default parameters. Head to the LXD documentation in the event you want to specify a few of these parameters your self.
We’re all set as much as package deal our first instance, so let’s do exactly that.
A talker-listener instance
For this instance, we’ll take it simple as we gained’t even want to write down any code. As a substitute, we’re going to package deal one of many ROS 2 demo from the GitHub repository. I’ve picked a demo from the ‘demo_nodes_cpp‘ package deal, extra particularly the ‘talker_listener.launch.py‘ demo.
As its identify suggests, this demo launches a ‘talker’ which publishes a string message on a ROS 2 matter. along with a ‘listener’ which reads stated message. Each print the message despatched/acquired to the console to simply comply with alongside. And naturally each are launched directly from a single launch file.
Whereas this instance could seem somewhat too easy, it is a perfect first contact with snaps. We’ll see how simple it’s to package deal ROS 2 functions with snaps.
The snapcraft.yaml file
Snapcraft depends on the ‘snapcraft.yaml’ configuration file to drive the packaging course of. So, allow us to create one,
mkdir -p ~/ros2_ws/first_snap/
cd ~/ros2_ws/first_snap/
contact snapcraft.yaml
after which populate it as follows,
identify: ros2-talker-listener
model: '0.1'
abstract: ROS 2 Humble talker/listener instance
description: |
This instance launches a ROS 2 Humble talker and listener.base: core22
confinement: strictapps:
ros2-talker-listener:
command: decide/ros/humble/bin/ros2 launch demo_nodes_cpp talker_listener.launch.py
plugs: [network, network-bind]
extensions: [ros2-humble]elements:
ros-demos:
plugin: colcon
supply: https://github.com/ros2/demos.git
source-branch: humble
source-subdir: demo_nodes_cpp
stage-packages: [ros-humble-ros2launch]
Consider it or not, that is all we have to create our snap. However allow us to examine this file extra carefully.
Breaking all of it down
We will determine 3 distinct blocks within the aforementioned snapcraft.yaml
file.
On the high of the file, there’s some boiler-plate that’s widespread to most snaps. The snap identify
, model
and many others. Nothing uncommon right here. Then comes base: core22
which is a base snap that can present a runtime surroundings to our software based mostly on Ubuntu 22.04. The final entry on this block is confinement: strict
which states that our software is strictly confined. In different phrases, it can’t entry any useful resource on the host machine.
The second block, apps
, specifies the applying(s) that’s uncovered from the snap. Right here, a single software is listed whose command
may be very acquainted. Moreover, our software additionally lists some interfaces
within the plugs
part. Observe that Interfaces permit our confined software to entry particular sources of the host machine. On this case, our snap may have entry to network-related interfaces that permit for the ROS 2 matters to stream. Lastly, the extensions: [ros2-humble]
will mechanically replenish another fields that are widespread to ROS 2 Humble snaps. In case you’re inquisitive about what an extension does, observe you can ‘increase’ it. Reveal all of its secrets and techniques by issuing the next command,
SNAPCRAFT_ENABLE_EXPERIMENTAL_EXTENSIONS=1 snapcraft expand-extensions
Finally we’re looking on the third block: elements
. The elements
tag defines the totally different items that make up our ultimate software. They embrace a supply
entry for the supply code corresponding to native information, a tarball, or as on this instance, a GitHub repository at a selected department. What’s extra, a ‘half’ can embrace build-packages
, that are solely required at construct time in contrast to stage-packages
that are solely wanted at runtime. Most significantly, every half is dealt with by a plugin
. Right here we’re utilizing the colcon
plugin, which makes use of the acquainted construct device in ROS 2. Then once more this plugin has its personal choices, which you’ll evaluation with the command,
snapcraft assist colcon
You will discover additional details about the world metadata, the elements and their personal metadata, the colcon plugin, extensions, and extra within the documentation.
Now that the snapcraft.yaml
file is outlined, it’s time to construct the snap.
Constructing the ROS 2 Humble snap
To construct the snap, problem the command snapcraft
in a terminal,
$ SNAPCRAFT_ENABLE_EXPERIMENTAL_EXTENSIONS=1 snapcraft
Beginning Snapcraft
…
* metallic sound of software program being solid *
…
Created snap package deal
Snap, reveal your self,
$ ls
ros2-talker-listener_0.1_amd64.snap snapcraft.yaml
Certainly, there it’s!
How about we set up it now? To take action, sort,
$ sudo snap set up --dangerous ros2-talker-listener_0.1_amd64.snap
ros2-talker-listener 0.1 put in
The usage of the
--dangerous
flag since we’re putting in a snap from disk as a substitute of utilizing the retailer.
Okay, however does it chunk work?
$ ros2-talker-listener
[INFO] [launch]: All log information might be discovered beneath /dwelling/ubuntu/snap/ros2-talker-listener/x6/ros/log/2022-05-24-15-24-50-823207-localhost-24895
[INFO] [launch]: Default logging verbosity is about to INFO
[INFO] [talker-1]: course of began with pid [24944]
[INFO] [listener-2]: course of began with pid [24946]
[talker-1] 2022-05-24 15:24:51.341 [RTPS_TRANSPORT_SHM Error] Did not create phase ac1bbb6c86049e8a: Permission denied -> Perform compute_per_allocation_extra_size
[listener-2] 2022-05-24 15:24:51.346 [RTPS_TRANSPORT_SHM Error] Did not create phase e75bbd4ac39608ab: Permission denied -> Perform compute_per_allocation_extra_size
[talker-1] 2022-05-24 15:24:51.348 [RTPS_MSG_OUT Error] Permission denied -> Perform init
[listener-2] 2022-05-24 15:24:51.348 [RTPS_MSG_OUT Error] Permission denied -> Perform init
[talker-1] [INFO] [1653420292.379305200] [talker]: Publishing: 'Whats up World: 1'
[listener-2] [INFO] [1653420292.380423139] [listener]: I heard: [Hello World: 1]
[talker-1] [INFO] [1653420293.379072149] [talker]: Publishing: 'Whats up World: 2'
[listener-2] [INFO] [1653420293.379286698] [listener]: I heard: [Hello World: 2]
[talker-1] [INFO] [1653420294.379120962] [talker]: Publishing: 'Whats up World: 3'
[listener-2] [INFO] [1653420294.379682258] [listener]: I heard: [Hello World: 3]
...
Sure it does! How nice.
What’s even higher is that you possibly can set up and run this snap on one other pc even when it doesn’t have ROS 2 put in! Heck, you possibly can set up and run this snap on one other Linux distribution from 4 years in the past!
Observe that the error message from FastDDS, the underlying DDS library, doesn’t stop the applying from working. In different phrases we will safely ignore that for now. If you wish to know extra about how shared-memory performs together with snap, we lined the subject at size in a devoted weblog: “Tips on how to use ROS 2 shared reminiscence in snaps”.
What’s subsequent?
We’ve got seen on this submit the best way to create a snap for a ROS 2 Humble software. Whereas this demo is pretty trivial, packaging a extra advanced ROS stack isn’t way more sophisticated. To reveal that, take a look on the sequence “Tips on how to arrange TurtleBot3 in minutes with snaps” the place I element the best way to snap the whole Turtlebot3.
Moreover, we’ve seen how one can successfully and simply package deal your ROS 2 software. How about distributing it now? Take a look at how to take action with the shop right here.
Lastly, do be at liberty to ask any questions on the snapcraft discussion board, or on ROS solutions. I’d love to listen to any suggestions you might have.