Configuring ROS2 over a Tailscale VPN on Ubuntu

Prerequisites

This tutorial assumes you have already installed ROS2 on a PC, Raspberry pi, or virtual machine running Ubuntu. For those tutorials, please see:

Instructions

Installing and using tailscale

First, go to the administrative interface at tailscale.com and ensure that the magicdns feature is enabled.

Then in a terminal window, download and run the install script straight from tailscale:

curl -fsSL https://tailscale.com/install.sh | sh

Before starting up, adjust some settings to enable other features

sudo tailscale set --operator=$USER -accept-dns=true --accept-routes=true 

Follow the instructions and link provided on the terimal interface to register your new computer with your tailscale account. you can do this in the device itself, or copy the link to the browser in your own pc if connecting remotely.

Finally, check that your install works:

tailscale status

You should see your computer listed

Set up fastrtps

sudo apt install -y ros-humble-rmw-fastrtps-cpp

Create a configuration file for fastrtps. replace [HOSTNAME1] and [HOSTNAME2] with the hostname or IP address of your computers as seen in the tailscale administrative interface. If you anticipate the ip address changing, for example if one of the machines is a docker container without ip address permanence enabled, it is suggested to use the hostname. This is only possible with “magicdns” enabled.

cat <<EOT | sudo tee /etc/fast_ts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<profiles xmlns="http://www.eprosima.com/XMLSchemas/fastRTPS_Profiles">
    <transport_descriptors>
        <transport_descriptor>
            <transport_id>TailscaleTransport</transport_id>
            <type>UDPv4</type>
        </transport_descriptor>
    </transport_descriptors>
    <participant profile_name="TailscaleSimple" is_default_profile="true">
        <rtps>
            <userTransports>
                <transport_id>TailscaleTransport</transport_id>
            </userTransports>
            <useBuiltinTransports>true</useBuiltinTransports>
            <builtin>
                <initialPeersList>
                    <locator>
                        <udpv4>
                            <address>[HOSTNAME1]</address>
                        </udpv4>
                    </locator>
                    <locator>
                        <udpv4>
                            <address>[HOSTNAME2]</address> 
                        </udpv4>
                    </locator>
                </initialPeersList>
            </builtin>
        </rtps>
    </participant>
</profiles>
EOT

Note: you can add more peers as needed to add more devices to your ROS2 network:

<locator>
    <udpv4>
        <address>[HOSTNAMEX]</address> 
    </udpv4>
</locator>

Next, modify .bashrc

sudo nano ~/.bashrc

it should look like the following example. The new lines should ideally be added before the source /opt/ros/humble/setup.bash line:

#....there will be other code above this

export RMW_IMPLEMENTATION=rmw_fastrtps_cpp
FASTRTPS_DEFAULT_PROFILES_FILE=/etc/fast.xml

#... the following lines should already be here.  Don't modify them

export ROS_DOMAIN_ID=0
export ROS_LOCALHOST_ONLY=0
source /opt/ros/humble/setup.bash

Next, save and close the file. In a terminal, type

source ~/.bashrc

even better, close and reopen your terminal window, or if you really want to test your changes, restart the pc.

sudo reboot now

Once restarted, In a new terminal window,

ros2 run demo_nodes_cpp listener

in a separate terminal window, type

ros2 run demo_nodes_cpp talker

Finally, open up a separate computer that has been set up to work over tailscale and is added to the fast.xml config file. It should work, no matter how it’s connected to the internet!

External Resources

https://adityakamath.hashnode.dev/ros-2-and-vpns

Oter Resources