Communication between Raspberry Pi and PC (ROS) - mktk1117/six_wheel_robot GitHub Wiki

Communicate with other PC

This explains how to start a ROS system using two machines. It explains the use of ROS_MASTER_URI to configure multiple machines to use a single master. (Running ROS across multiple machines)

Connect to Raspberry Pi by using ssh

The Raspberry Pi and your PC should be connected to the same network.

Check IP address

The first step is to know the IP address of Raspberry Pi and your PC.
Command ifconfig at your PC and Raspberry Pi. It would be like this

eth0      Link encap:Ethernet  HWaddr 54:ee:75:96:d1:ff  
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
          Interrupt:16 Memory:f1200000-f1220000 

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:11708 errors:0 dropped:0 overruns:0 frame:0
          TX packets:11708 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:1290895 (1.2 MB)  TX bytes:1290895 (1.2 MB)

wlan0     Link encap:Ethernet  HWaddr a4:34:d9:e8:55:03  
          inet addr:212.191.89.123  Bcast:212.191.89.127  Mask:255.255.255.128
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:275332 errors:0 dropped:0 overruns:0 frame:0
          TX packets:103783 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:336033503 (336.0 MB)  TX bytes:17411445 (17.4 MB)

Then you can know your IP address from inet addr: . Please remenber its number.

Connect to Raspberry Pi from your PC

Now, you know the IP address of Raspberry Pi, then, use ssh command to connect.

$ssh [email protected]

Use ROS between Raspberry Pi and your PC

###1. Set IP address to hosts First, you have to set hosts name. Edit the file /etc/hosts with some editor.

pi@raspberry$sudo vi /etc/hosts

Add these two lines and save it.

212.191.89.116 raspberry
212.191.89.123 mypc

Change 212.191.89.116 and 212.191.89.123 to what you have just checked.

Check if the hosts is right by using ping

$ping raspberry

###2. Run roscore at Raspberry Pi In this tutorial, we use Raspberry Pi as a master. Before starting roscore, export to ROS_HOSTNAME and ROS_MASTER_URI

pi@raspberry$export ROS_HOSTNAME=raspberry
pi@raspberry$export ROS_MASTER_URI=http://raspberry:11311
pi@raspberry$roscore

###3. See messages from Raspberry Pi from your PC First, start talker at Raspberry Pi

pi@raspberry$rosrun beginner_tutorials talker.py 

Then, export to ROS_HOSTNAME and ROS_MASTER_URI

$export ROS_HOSTNAME=mypc
$export ROS_MASTER_URI=http://raspberry:11311

Now, you can see rostopic of Raspberry Pi

$rostopic list

You can see like this

/chatter
/rosout
/rosout_agg

Then, you can see the messages by rostopic echo

$rostopic echo /chatter

It would be like this

data: hello world 1464543258.94
---
data: hello world 1464543259.05
---
data: hello world 1464543259.14
---
data: hello world 1464543259.24
---
data: hello world 1464543259.34
---
data: hello world 1464543259.44
---
data: hello world 1464543259.54

Add some commands to ~/.bashrc

It would be easy with adding export command to ~/.bashrc of Raspberry Pi and your PC.

At Raspberry Pi,

pi@raspberry$echo 'export ROS_HOSTNAME=raspberry' >> ~/.bashrc
pi@raspberry$echo 'export ROS_MASTER_URI=http://raspberry:11311' >> ~/.bashrc

At your PC,

$echo 'export ROS_HOSTNAME=mypc' >> ~/.bashrc
$echo 'export ROS_MASTER_URI=http://raspberry:11311' >> ~/.bashrc