Skip to main content

Robot Network

Chapter description

ROS allows you to run nodes on a single robot and on dozens of robots as well, as long as your devices are in the same network. You will learn here how to configure ROS to work on multiple computers. You will use this configuration to set up a system consisting of a robot and computer or two computers for Gazebo simulation version. In this tutorial you will need a computer and a robot with the same equipment as in the previous tutorials. In case you are working on Gazebo simulator, it is possible to set up a system to work on multiple computers. To do so, you need to have two computers with ROS.

You can run this tutorial on:

Repository containing the final effect after doing all the ROS Tutorials you can find here

Connecting ROS in LAN

To run ROS on multiple machines you need to connect them to the same LAN network at first. Only one device can run ROS Master. For example if you are connecting a robot and laptop, run roscore only on one of those two devices.

Let's assume we are connecting robot and laptop devices, with the following IP addresses:

  • robot: 192.168.1.1 (ROS Master with roscore running)
  • laptop: 192.168.1.2 (ROS Slave)
tip

You can check the address of your device with hostname -I command and verify connection using ping <SECOND_DEVICE_IP> .

Master configuration

On the robot device open the .bashrc file, then add following lines:

~/.bashrc
# Type your ROS Master IP
alias ROS_LAN='MASTER_IP=192.168.1.1
export ROS_MASTER_URI=http://$MASTER_IP:11311
export ROS_HOSTNAME=$MASTER_IP
export ROS_IP=$MASTER_IP
unset ROS_IPV6
echo ROS_LAN master config ready!'

Slave configuration

On the laptop device open the .bashrc file, then add following lines:

~/.bashrc
# Type your ROS Master IP and Slave IP
alias ROS_LAN='MASTER_IP=192.168.1.1
SLAVE_IP=192.168.1.2
export ROS_MASTER_URI=http://$MASTER_IP:11311
export ROS_HOSTNAME=$SLAVE_IP
export ROS_IP=$SLAVE_IP
unset ROS_IPV6
echo ROS_LAN slave config ready!'

You have just created an alias that will be used to configure your LAN. Note that you must execute the ROS_LAN command on each device and in each newly opened terminal.

Task 1

Open new terminal type ROS_LAN run roscore on the appropriate machine (for above configuration robot is set as ROS Master) and check on your other machine if the devices are sending information to each other. Use the rosnode command to see if any nodes are visible.

info
  1. If you make changes in the .bashrc file, always source it with . ~/.bashrc or open a new terminal to see changes.

  2. Remember that roscore must be running on the device with Master configuration.

  3. If there is an error when starting roscore on ROSbot, it is most likely caused by running ROS Master in the docker. To do this, you should stop running containers with the command:

docker kill $(docker ps -q)

Connecting ROS via Internet

As mentioned in the previous section you can run ROS nodes even on dozens of devices as long as they are in the same LAN network. This means that you can connect the ROS between the laptop and the robot when they are connected to the same Wi-Fi router. However, the above example will not work if the robot is connected to the Internet, e.g. via the LTE module.

You can solve the issue by using a VPN service. Standard VPNs, however, are designed for a different purpose than mobile robotics and introduce some disadvantages to your system, such as:

  • Maintenance of a VPN server
  • Dealing with a high latency (all traffic needs to go thought a VPN server)
  • Long reconfiguration time (a.k.a. switching a Wi-Fi hotspot by your robot)
  • Lack of "ROS awareness"

Fortunately there is a VPN service designed with ROS in mind - Husarnet VPN Client. Husarnet is an open source peer-to-peer VPN - that means you can connect your robots directly over the Internet, with no central VPN server for data exchange. All traffic is encrypted using ephemeral keys, and it works with ROS out of the box. Just install Husarnet Client on your laptop and robot, connect them to the same Husarnet network, by using an online dashboard and enjoy low-latency connection between your devices. :)

Husarnet is easy

Step 1 / 5

Create a free account at https://app.husarnet.com

Husarnet Register

At this point you should have your robot and laptop connected to the same Husarnet network with the following Husarnet Hostnames (that can be used instead of Husarnet IPv6 address)

  • robot: my-robot (running roscore - ROS Master)
  • laptop: my-laptop

The steps to configure ROS are as follows:

Husarnet configuration

Select master

At first place go to app.husarnet and in the dashboard make sure to select the ROS master checkbox in the setting of my-robot device. It will allow you to reach my-robot device by using both my-robot and master hostname (the second one is used in our configuration)

ROS master option in Husarnet online dashboard

Open the .bashrc file, and make the following configuration:

Master configuration

On the robot device open the .bashrc file, then add following lines:

~/.bashrc
alias ROS_HUSARNET='export ROS_MASTER_URI=http://master:11311
export ROS_IPV6=on
export ROS_HOSTNAME=master
unset ROS_IP
echo ROS_HUSARNET master config ready!'

Slave configuration

On the laptop device open the .bashrc file, then add following lines:

~/.bashrc
alias ROS_HUSARNET='export ROS_MASTER_URI=http://master:11311
export ROS_HOSTNAME=my-laptop
export ROS_IPv6=on
echo ROS_HUSARNET slave config ready!'
note

my-laptop is the name we used when adding devices to Husarnet. In case of failure, you can see the available names using the cat /etc/hosts command

You have just created an alias that will be used to configure your Husarnet connection. Note that you must execute the ROS_HUSARNET command on each device and in each newly opened terminal. As you can see, thanks to the use of Husarnet, the ROS configuration operating over the Internet is almost as simple as the LAN configuration. An additional advantage of Husarion is the lack of need to know the IP addresses of the devices, only the easy-to-remember device name.

Task 2

Again run roscore on the appropriate machine that was selected as a ROS master in Husarnet dashboard and check on your other machine if the devices are sending information to each other. Use the rosnode command to see if any nodes are visible.

tip

You can even quickly post a message from the terminal using the familiar rostopic command. For example, on one computer you can type:

rostopic pub -r 1 /msg std_msgs/String "data: 'Husarnet is awesome! :D'"

and use rostopic echo to listen for incoming topics.

More about Husarnet VPN

Husarnet is open source and free to use up to 5 devices. It works not only with ROS, but also with ROS 2, and here you can find a nice article showing how to integrate it with your ROS 2 system:

OK, at this point you should have your laptop and robot connected via LAN or Internet. Let's run some additional demo nodes to see how the connection works in practice.

Real-time video stream

In the first demo, the camera on one of the platforms will be launched. Thanks to the network connection, we can access any topic between devices. In this example, we will show you how to display the image from the camera on the second device.

Robot part

Select configuration and launch astra.launch.

husarion@husarion:~$
ROS_HUSARNET # or ROS_LAN
roslaunch astra_camera astra.launch

Laptop part

Select configuration and run rqt_image_view to see a real-time video from the robot (you need to select the topic name after executing the below command).

user@laptop:~$
ROS_HUSARNET # or ROS_LAN
rqt_image_view

Outsourcing robot computing power

In this section, we will program a robot and a computer to outsource the robot's computing power. The node that we are going to assign to the robot is responsible for image processing. Basically, the robot publishes the stream from the camera, and object recognition is done on the laptop (using the find_object_2d node). This approach allows to increase computing power, especially when it is necessary to use computer graphics.

In this section we will also use the image transport node. The image transport node provides the ability to compress and decompress the image. This approach will allow you to speed up data transfer between devices through data reduction. Below are step-by-step instructions for the previous exercise performing calculations on two connected devices.

  1. Configure the network.

  2. Create new image_recognition_rosbot.launch file on ROSbot device. This file will run the camera with image_transport which compresses the image.

    ~/ros_ws/src/tutorial_pkg/launch/image_recognition_rosbot.launch
    <launch>

    <arg name="use_gazebo" default="false" />
    <arg name="teach" default="false"/>

    <!-- Gazebo -->
    <group if="$(arg use_gazebo)">
    <param name="use_sim_time" value="true" />
    <include file="$(find rosbot_bringup)/launch/rosbot_tutorial.launch">
    <arg if="$(arg teach)" name="world" value="image_teaching"/>
    <arg unless="$(arg teach)" name="world" value="image_recognition"/>
    </include>
    </group>

    <!-- ROSbot -->
    <group unless="$(arg use_gazebo)">
    <include file="$(find astra_camera)/launch/astra.launch"/>
    </group>

    <!-- Compress images -->
    <node pkg="image_transport" type="republish" name="rgb_compress" args=" raw in:=/camera/color/image_raw compressed out:=/rgb_compressed"/>

    </launch>
  3. Create second launch image_recognition_laptop.launch file on your computer. This file will run:

    • image_transport on your host, which will be used to decompress the image,
    • find_2d_object node known from the previous chapter, which will perform calculations on our computerThanks to this, the most time-consuming calculations should be accelerated.
    ~/ros_ws/src/tutorial_pkg/launch/image_recognition_laptop.launch
    <launch>

    <arg name="use_gazebo" default="false" />
    <arg name="teach" default="false"/>

    <!-- Decompress images -->
    <node pkg="image_transport" type="republish" name="rgb_decompress" args=" compressed in:=/rgb_compressed raw out:=/rgb_decompressed" >
    <param name="compressed/mode" value="color"/>
    </node>

    <!-- Teleoperation - keyboard control -->
    <node name="teleop_twist_keyboard" pkg="teleop_twist_keyboard" type="teleop_twist_keyboard.py" output="screen"/>

    <!-- Object detection -->
    <node pkg="find_object_2d" type="find_object_2d" name="find_object_2d">
    <remap from="image" to="/rgb_decompressed"/>
    <param name="gui" value="$(arg teach)"/>
    <param unless="$(arg teach)" name="objects_path" value="$(find tutorial_pkg)/img_data/"/>
    </node>

    </launch>
  1. Start launch file on your ROSbot.

    husarion@husarion:~$
    docker compose up -d rosbot ros-master
    roslaunch tutorial_pkg image_recognition_rosbot.launch
  2. Start launch file on your ROSbot.

    husarion@husarion:~$
    roslaunch tutorial_pkg image_recognition_laptop.launch

Execute both launch files, open rqt_graph to see how the system is working now.

Task 3

Try to run img_follow code and check experimentally whether the smoothness of the robot's operation has been improved.

Hint
  1. Create workspace.
  2. Copy source file and edit CMakeList.txt. Remember to change #define OBJECT_TO_FOLLOW according to your object id.
  3. Build with catkin_make command.
  4. Run with rosrun tutorial_pkg follow_img teach:=false or add this line to image_recognition_laptop.launch.
<node pkg="tutorial_pkg" type="img_follow" name="img_follow" output="screen">
<arg name="teach" value="false"/>
</node>

Question 1 To connect multiple devices communicating with each other in the ROS application ROS Master must be:
a) running on every device
b) running only on so-called 'master' device
c) ROS master will automatically boot on the correct device provided you have a properly configured .bashrc file
d) it is not possible to connect multiple devices together in ROS

Details

Answer b)

Summary

That's all, you don't have to worry about anything more than properly configuration of .bashrc rest stays the same. Every time when you have 2 or more devices the pattern is the same. ROS takes care of all data exchange. That is really simple, isn't it? After completing this tutorial, you should be able to configure your devices to communicate with multiple devices. You should also know how to send and read information remotely, allowing you to control a robot. You've also seen the image_transport node for image compression.


by Łukasz Mitka, Rafał Górecki, Adam Krawczyk & Dominik Nowak, Husarion

Need help with this article or experiencing issues with software or hardware? 🤔

  • Feel free to share your thoughts and questions on our Community Forum. 💬
  • To contact service support, please use our dedicated Issue Form. 📝
  • Alternatively, you can also contact our support team directly at: support@husarion.com. 📧