A running ROS system can contain multiple nodes distributed across multiple computers. Depending on the system configuration, any node may need to communicate with any other node at any time. However, a necessary condition for implementing this distributed communication is that each machine must be on the same local area network. In this lesson, we will use Raspberry Pi and Virtual Machine as an example to illustrate how to configure and implement distributed communication.
Require:
The virtual machine and Raspberry Pi are connected to the same LAN;
Select one as the host. The host needs to start roscore. Here, select Raspberry Pi as the host and the virtual machine as the slave. The Raspberry Pi here needs to have ros-noetic installed;
Install the ssh and chrony packages on each device for synchronization. You can enter the following instructions to install.
sudo apt-get install chrony openssh-server -y
Both are connected to the same LAN, query the network IP address through the following command,
xxxxxxxxxx
ifconfig
Virtual machine side,
The address found is 192.168.2.131
Raspberry Pi side,
xxxxxxxxxx
ifconfig
The found address is 192.168.2.117
Taking the virtual machine as a slave machine as an example, you need to modify the ROS_MASTER_URI of the virtual machine to the Raspberry Pi IP. Here is the 192.168.2.117 you just queried, enter it in the terminal,
xxxxxxxxxx
sudo gedit ~/.bashrc
After entering the password, add it at the bottom of the file,
xxxxxxxxxx
export ROS_MASTER_URI=http://192.168.2.117:11311
Exit after saving and reopen the terminal to refresh the environment variables to take effect.
Then, modify the/etc/hosts file,
xxxxxxxxxx
sudo gedit /etc/hosts
As shown in the picture above, add the Raspberry Pi's IP and machine name, save and exit.
The modified files and steps are almost the same as above. The only difference is that the ROS_MASTER_URI of the Raspberry Pi needs to be replaced with the local IP, which is 192.168.2.131. Just add the ip and machine name of the virtual machine at the end of the /etc/hosts file. The two files are as follows:
~/.bashrc
xxxxxxxxxx
sudo vim ~/.bashrc
/etc/hosts
xxxxxxxxxx
sudo vim /etc/hosts
Similarly, exit after saving, reopen the terminal or source the terminal to refresh the environment variables.
Let's verify whether the configuration is successful. First, on the host (here is the Raspberry Pi), start roscore and enter in the host terminal,
xxxxxxxxxx
roscore
Then, run the little turtle node on the slave machine (virtual machine) and enter from the slave machine terminal,
xxxxxxxxxx
rosrun turtlesim turtlesim_node
A little turtle appears from the machine,
Then, we start the little turtle keyboard control node on the host (Raspberry Pi), and input at the host terminal,
xxxxxxxxxx
rosrun turtlesim turtle_teleop_key
Click the keyboard to control the terminal, and then use the up, down, left and right buttons to control the movement of the little turtle on the virtual machine.