ROS network setup between two devices via ethernet cable - brennanyama/RobotOperatingSystem GitHub Wiki

ROS network setup between two devices via ethernet cable

In this walkthrough, we will setup a ROS network between two computers connected via ethernet cables. In subsequent walkthroughs we will incrementally add more complexity to the network. This walkthrough assumes that both computers are running Ubuntu.

1. Hardware setup

In this walkthough we have two Ubuntu computers running ROS. The specific ROS distribution does not matter since differing ROS distributions have no problem communicating with each other over a network. We will call the computer we want to use as the ROS Master computer A, and the second computer is called computer B. Connect computer A to computer B directly using an ethernet cable.

Connect computer A to computer B directly using an ethernet cable as shown in this image.

Note that older computers may require a crossover ethernet cable for direct cable connections; however, most modern network devices can recognize when two devices are directly connected and no longer need a crossover cable. If you want to connect more than two computers and/or devices directly via ethernet, you will need to use an ethernet switch; however, all of the same processes described in this walkthrough will be the same.

2. Network setup and SSH validation

2.1. Software installation

Install openssh on both computers:

sudo apt-get install openssh-server

Install net_tools on both computers:

sudo apt install net-tools

2.2. Find your MAC address

Find the MAC address of your computer by running:

ifconfig

Typically the first device listed will be associated with your hardware MAC address. This is usually called eth0 or enp1s0, but can be called something else. The MAC address will be of the form xx:xx:xx:xx:xx:xx (each 'x' could be a letter or a number).

2.3. Manually set the IPv4 address of each computer

To manually set your wired IPv4 address, navigate to your Ubuntu network settings, click on your wired connection, and open detailed settings of your wired connection. You should be presented with a window similar to this:

You should be presented with a window similar to this.

Click on the IPv4 tab (circled red in above image). Change the IPv4 method from "Automatic (DHCP)" to "Manual". Then add a new address; set the Address to 10.10.10.1, set the Netmask to 255.255.255.0, and leave the Gateway blank. You may see the Netmask automatically change from 255.255.255.0 to 24; this is fine. It should look similar to this:

It should look similar to this:.

Click "Apply" or "Save" as appropriate. Ensure that the wired network is enabled.

Repeat this process on computer B; however, this time set the Address to 10.10.10.2 (keep the Netmask and Gateway settings the same as the previous step: 255.255.255.0 and blank, respectively).

2.4. Validate communication between computers using ping and ssh

Computer A is now set to address 10.10.10.1 and computer B is now set to address 10.10.10.2. First, let's test that computer A can ping itself. On computer A enter:

ping 10.10.10.1

This should print successive successful ping messages. This tells us that computer A can correctly communicate with itself with our manually-set IPv4 address of 10.10.10.1. Now test that that computer A (IPv4: 10.10.10.1) can ping computer B (IPv4: 10.10.10.2) using:

ping 10.10.10.2

This should also print successive successful ping messages. This tells us that computer A can correctly communicate with computer B through the ethernet cable. Repeat this process on computer B to validate bi-directional communication.

Finally test that you can open an SSH connection between the two computers. On computer A, enter:

ssh 10.10.10.2

This will attempt to open a SSH connection from computer A (10.10.10.1) to computer B (10.10.10.2). The first time you open an SSH connection between two machines, SSH may ask you if you are sure you want to open the connection; type yes to continue. If the connection succeeds, terminal will prompt you to enter the password for computer B. Doing so should open a SSH connection between the two machines.

You should also repeat this process on computer B (this time using ssh 10.10.10.1).

3. ROS network setup

To summarize, up to this point we have:

  • directly connected computer A to computer B via ethernet cable
  • manually set the IPv4 address of computer A to 10.10.10.1 and computer B to 10.10.10.2
  • validated that computer A can ping computer B (and vice versa)
  • validated that computer A can open a SSH connection to computer B (and vice versa)

According to the ROS Network Setup walkthrough, to connect multiple computers to a single ROS instance, ROS requires: (1) there must be complete, bi-directional connectivity between all pairs of machines, on all ports, and (2) each machine must advertise itself by a name that all other machines can resolve. We now fulfill both of these criteria.

Let's assume that we want computer A to serve as the ROS master node. For computer B to "join" a ROS instance running on computer A, we need to correctly configure the ROS_MASTER_URI and ROS_HOSTNAME on both computers. According to the ROS Environment Variable walkthrough ROS_MASTER_URI is a required setting that tells nodes where they can locate the master and ROS_HOSTNAME is an environment variable that sets the declared network address of a ROS node. In short, ROS_MASTER_URI contains the network address of the ROS master computer and ROS_HOSTNAME contains the IPv4 address of the current computer.

With this in mind, we want to set ROS_MASTER_URI of both computer A and computer B to the network address of computer A, and we want to set ROS_HOSTNAME to each computers respective IPv4 address (10.10.10.1 for computer A and 10.10.10.2 for computer B).

On computer A, start a new terminal instance and type:

source /opt/ros/{$ROSDISTRO}/setup.bash
export ROS_MASTER_URI=http://10.10.10.1:11311
export ROS_HOSTNAME=10.10.10.1

Replacing {$ROSDISTRO} on the first line with the ROS distribution installed on computer A. These terminal lines must be set for each new terminal instance. Advanced users will recognize that these lines can be added to your ~/.bashrc file for convenience.

On computer B, start a new terminal instance and type:

source /opt/ros/{$ROSDISTRO}/setup.bash
export ROS_MASTER_URI=http://10.10.10.1:11311
export ROS_HOSTNAME=10.10.10.2

Again, replacing {$ROSDISTRO} on the first line with the ROS distribution installed on computer B. Note that ROS_MASTER_URI remains the same, as that contains the address of the ROS master computer; however, the ROS_HOSTNAME is different, as this should contain the IP address of the current computer. These lines can be added to your ~/.bashrc file for convenience.

On computer A, go back to your sourced terminal instance and launch a minimal instance of ROS using roscore. Check that everything is running properly using rostopic list. You should see the output:

/rosout
/rosout_agg

Create a new topic called \chatter and publish the string message "Hello world!" to it.

rostopic pub /chatter std_msgs/String "Hello world!"

This will publish and latch a String message "Hello World" to a new topic called /chatter. Publishing and latching is important because latching keeps the last topic in memory for any subscribers that may come later (simply publishing will pushing the message and remove it from memory). You can check that this new topic exists using rostopic list.

On computer B, go back to your sourced terminal instance view all topics using rostopic list. This should output:

/chatter
/rosout
/rosout_agg

Note the new topic /chatter that we just created on computer A. To echo the topic enter:

rostopic echo /chatter

This should output:

data: "Hello World!"
---

You have now successfully published a message on computer A, which you then echoed back on computer B! Feel free to continue playing with creating different ROS topics and publishing different messages to them. Anything that can be viewed on computer A should also be viewable on computer B.

⚠️ **GitHub.com Fallback** ⚠️