(!) Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.

Setup a ROS master synchronization

Description: This tutorial shows a simple synchronization of two ROS master.

Tutorial Level: BEGINNER

Example with one host

In this example we start two ROS master on the same host using different ROS_MASTER_URI. Then we start the synchronization to let rostopic pub and rostopic echo communicate together.

Setup the example environment

In the first console we start the ROS master and an example publisher, which publish Hello World to topic with name /test/topic:

export ROS_MASTER_URI=http://localhost:11311
roscore --port 11311 >/dev/null 2>&1 &
rostopic pub /test/topic std_msgs/String 'Hello World' -r 1 &

In the second console we start a second ROS master and a rostopic echo to receive the Hello World messages:

export ROS_MASTER_URI=http://localhost:11312
roscore --port 11312 >/dev/null 2>&1 &
rostopic echo /test/topic &

At this moment, however, no topic is being published on the master at localhost:11312 so rostopic echo command shouldn't print any topic.

Multi-master Synchronization

Now let's see how multiple ROS masters synchronize using the feature from multimaster_fkie.

On the 1st console in the previous section,

rosrun fkie_master_discovery master_discovery >/dev/null 2>&1 &
rosrun fkie_master_sync master_sync >/dev/null 2>&1 &

Do exactly the same on the 2nd console:

rosrun fkie_master_discovery master_discovery >/dev/null 2>&1 &
rosrun fkie_master_sync master_sync >/dev/null 2>&1 &

Now both ROS masters are synchronized, and the 2nd console should start printing the message ('Hello World') being published on the ROS master at localhost:11311 on the 1st console.

Note: for version lower than 1.x use master_discovery_fkie and master_sync_fkie for package names.

Example with two hosts

In this example we will start two ROS masters on two different hosts. It is highly recommended to first run through the example above. Much of this example was drawn from http://digital.csic.es/bitstream/10261/133333/1/ROS-systems.pdf.

To run multimaster_fkie on two hosts there is not much that has to change to the setup above apart from some network configurations. First, we will make sure we have the correct settings enabled. After that we will register the hostnames of the machines so that the two hosts can identify each other.

Enable the network settings

We will need to make sure two settings are working: IP forwarding and multicasting should be enabled. We can check if IP forwarding is enabled by executing the following command in a console on both hosts:

cat /proc/sys/net/ipv4/ip_forward

If this command returns 1 then IP forwarding is enabled. If it is not you can temporarily enable it using:

sudo sh -c "echo 1 >/proc/sys/net/ipv4/ip_forward"

To find out whether multicasting is enabled you can execute the following command:

cat /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts

If this command returns 0 then multicasting is enabled. If it is not you can temporarily enable it using:

sudo sh -c "echo 0 >/proc/sys/net/ipv4/icmp_echo_ignore_broadcasts"

Now you can test the network setup utilizing the code from the first example on two machines. The nodes should be able to discover each other but a warning is expected: "Master discovered with not known hostname". You cannot yet send messages from one host to the other.

Set the necessary hostname information

Linux keeps a file that contains information on the IP addresses that the computer can connect to and their respective hostnames. This file can be found under /etc/hosts. The warning that we got in the previous subsection told us that the computer found an IP address for which no host was set. We will do that now.

You can discover the hostname of a computer and the corresponding IP address by executing the following commands:

hostname -I

Now we need to specify the IP and corresponding hostname in each host for the other host. Simply add a line to the /etc/hosts file in each host containing the IP address and the hostname seperated by a tab (you will need administrator rights for this). Running the aforementioned commands should now allow you to send messages from one host to the other!

Hooray! You have now setup two ROS masters on two different hosts! We urge you to read http://digital.csic.es/bitstream/10261/133333/1/ROS-systems.pdf for a more extensive documentation than mentioned in this tutorial.

Wiki: multimaster_fkie/Tutorials/Setup a ROS master synchronization (last edited 2020-12-08 10:32:52 by AlexanderTiderko)