ROS进阶学习手记 6 -- Configure ROS Network - 利用PC1控制PC2上的乌龟

ROS进阶学习手记 6 – Configure ROS Network - 利用PC1控制PC2上的乌龟

Configure the ROS Networks: Quick Reference: http://blog.csdn.net/sonictl/article/details/46986565#t4
Above is for the higher user’s reference.

Below is the tutorial for the beginners:

在完成了上一节,我们已经可以自己写node向某个topic发送某个msg data structure的Messages。

这一节我想完成 Remote 通信的任务,真正体现ROS的分布式计算的特点。

参考:

   http://wiki.ros.org/ROS/NetworkSetup
   http://wiki.ros.org/ROS/Tutorials/MultipleMachines

网络连接:

    物理连接:

   使用Bridge方式桥接,相当于现在有了4台电脑用网线连接起来。(怎么建立见文后附录)

  • 配置虚拟机1的ip为:
            IP addr:192.168.2.111    Mask:255.255.255.0  DefaultGateway: 192.168.2.1   Bcast:default
  • 配置宿主机1的ip为:
            IP addr:192.168.2.1       Mask:255.255.255.0  DefaultGateway: 192.168.2.1   Bcast:default
  • 配置虚拟机2的ip为:
            IP addr:192.168.2.222   Mask:255.255.255.0  DefaultGateway: 192.168.2.1   Bcast:default
  • 配置宿主机2的ip为:
            IP addr:192.168.2.2       Mask:255.255.255.0  DefaultGateway: 192.168.2.1   Bcast:default

好,我们假设建立了ubt - ubt之间的连接(怎么建立见文后附录,或http://blog.csdn.net/sonictl/article/details/47005273)

我们在ubt_for_ROS1上ping 192.168.2.222,  在ubt_for_ROS2上ping 192.168.2.111, 都能ping通。

用netcat测试端口是否连通:http://wiki.ros.org/ROS/NetworkSetup#Further_check:_netcat

    在ubt_for_ROS1上:

           $ netcat -l 1431

    在ubt_for_ROS2上:

           $ netcat 192.168.2.111 1431

此时在俩ubt_for_ROS上的Terminal里,敲入任何字符,回车,都能发送到对方,正如:you will be able to type back and forth between the two consoles, like an old-fashioned chat program.

反过来再测试一遍~  OK。

=== ROS 对 名称 的解析 ===

  当一个ROS节点广播一个topic时,它会提交 hostname:port 组合(URI),别的nodes想要收听这个topic时,会用到这个URI。所以一个node提供的hostname一定要被所有别的nodes用得上,才能与那个topic联系。ROS客户端的库使用了这个名字,这个名字是机器报告的它的hostname。这个hostname 就是在Terminal里运行命令hostname返回的那个值。

  所以我们不仅要改hosts文件,使机器访问的 机器名 与 ip对应上,我们还需要修改hostname。

  这里我们两台ubuntu for ros的hostname都是ubuntu,

  »把ubt_for_ROS1的hostname改为:

     ubtros1

  »把ubt_for_ROS2的hostname改为:

     ubtros2

在ubt_for_ROS1上

  $sudo gedit /etc/hostname

  把“ubuntu”改为“ubtros1”

在ubt_for_ROS2上

  $sudo gedit /etc/hostname

  把“ubuntu”改为“ubtros2” ,这样就区分开了俩ubt_for_ROS

修改hosts文件:

  $ sudo vi /etc/hosts
    #至于vi工具的使用方法,请自行搜索
    #添加如下的ip解析规则
    192.168.2.111   ubtros1
    192.168.2.222   ubtros2

After you are finished configuring your networking files, don’t forget to restart your network for the changes to take effect. 或者直接重启ubuntu更保险。

           exbot@ubuntu:~$ sudo /etc/init.d/networking restart 
           * Running /etc/init.d/networking restart is deprecated because it may not enable again some interfaces
           * Reconfiguring network interfaces...                                   [ OK ]         

背景知识:

修改Ubuntu主机名:
-$sudo vi /etc/hostname

修改hosts:
-$sudo vi /etc/hosts

#据说只改hostname不改hosts会出现问题。
#其实是hostname是主机名,hosts是DNS转发ip用的一个查询表而已。

hostname命令不带任何参数默认是用来显示主机名的,如果在该命令的后面加上一个字符串,那么这个字符串就是暂时设置主机名,重启后复原。

好了,我就改了hosts和hostname。检查后继续!ping ubtros1 和 ping ubtros2都是可以的。hostname命令运行结果也是变了的。

参考: my “/etc/hosts” file:

127.0.0.1	localhost
127.0.1.1	ubuntu
192.168.1.103   ubtros1
192.168.1.102   ubtros2

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

在俩虚拟机上都配置相同的环境变量:(有时候提示连接不上ros master, 检查环境变量)

      $ export ROS_MASTER_URI=http://ubtros1:11311

检查环境变量:

      $ echo $ROS_MASTER_URI

这里我要补充的是: ROS 在启动 roscore 时调用的 hostname 可能涉及到多个环境变量: $ROS_HOSTNAME, $ROS_MASTER_URI 这俩环境变量又和 hosts & hostname的值有关系。请不要设置错了。具体详细设置方法的可以参考: 《ROS by Example for Hydro Vol1》书中4.12 Network Between a Robot and a Desktop Computer
简而言之:$ROS_MASTER_URI 要统一。尽管HOSTNAME可以各设各的,但roscore启动时会引用本机的ROS_HOSTNAME,所以roscore应该在MASTER机器上启动。

测试:开始运行我们的nodes在俩虚拟机上!

  在虚拟机“ubt_for_ROS1”上:

           $ roscore

  此时会显示ROS_MASTER_URI=ubtros1:11311, 切记,是ROS_MASTER_URI= ubtros1:11311

在虚拟机“ubt_for_ROS2”上:

           $ rosrun turtlesim turtlesim_node

此时会在虚拟机“ubt_for_ROS2”上出现乌龟的窗口,说明已经连接上“ubt_for_ROS1”上的ros master!

在虚拟机“ubt_for_ROS1”上:

           $ rosrun robot_cleaner robot_cleaner_node

没动,在在虚拟机“ubt_for_ROS2”上,$ rostopic list : unable to communicate with ros master ,重新配置了一下ROS_MASTER_URI,解决。

在ubt_for_ROS1 屏幕截图:

    

在ubt_for_ROS2 屏幕截图:

    

大笑

附录:==== 俩虚拟机的网络通信的配置====

关于网线直连的配置:http://jingyan.baidu.com/article/3065b3b6c552edbecff8a4a1.html

双方Host_PC:Win7都能ping通对方Win7。

关于虚拟机的网络配置方式,参考:http://blog.csdn.net/mrjy1475726263/article/details/7772372

使用Bridge方式桥接,相当于现在有了4台电脑用网线连接起来。

  • 配置虚拟机1的ip为:
            IP addr:192.168.2.111    Mask:255.255.255.0  DefaultGateway: 192.168.2.1   Bcast:default
  • 配置宿主机1的ip为:
            IP addr:192.168.2.1       Mask:255.255.255.0  DefaultGateway: 192.168.2.1   Bcast:default
  • 配置虚拟机2的ip为:
            IP addr:192.168.2.222   Mask:255.255.255.0  DefaultGateway: 192.168.2.1   Bcast:default
  • 配置宿主机2的ip为:
            IP addr:192.168.2.2       Mask:255.255.255.0  DefaultGateway: 192.168.2.1   Bcast:default

相关截图:

     宿主机1的设置:
    

    

    虚拟机1相关设置:

    

现在费了这么大劲,解决了连接问题,当然还有别的方式,比如一台宿主机两台虚拟机,读者去自己调研。

只要两台虚拟机之间能ping通即可。

ROS Networking Between a Robot and Desktop Computer/Controller

                  by sonictl ref:book ‘ROS by Example_Vol1’
ROS requires the machines are in one domain network. i.e. Machine_A and Machine_B can ping each other.
 _____________                _____________
|  (Desktop)  |              |   (Robot)   |
|  Machine_A  |<— Ping —>|  Machine_B  |
|_____________|              |_____________|
 by: http://blog.csdn.net/sonictl

Let’s see the configuration steps necessary for a working ROS network:

Step1: Configure the IP address for M_A and M_B
  Manually setting the static IP address as below:
  »IP for M_A: 192.168.1.101
  »IP for M_B: 192.168.1.201

Step2: Configure the hostname of each machine
  The hostname is the ID/mark for the machine be identified by the domain network.
  On M_A and M_B, modify the file: /etc/hostname by this command:
  $ sudo gedit /etc/hostname
 
  » hostname for M_A: my_desktop
  » hostname for M_B: my_robot

Step3: Configure the hosts mapping table of each machine
  The hosts mapping table is mapping of IP to the machine’s hostname/ID/mark.
  the mapping table is to be identified by the domain network.
  Add all the hostname/ID/mark <–> IP mappings for the machines in the network, so that they know each other’s IP.
  On M_A and M_B, modify the file: /etc/hosts by this command:

  $ sudo gedit /etc/hosts
 
  » for M_A, add this in its hosts file:
     192.168.1.101 my_desktop
     192.168.1.201 my_robot
     
  » for M_B, add this in its hosts file:
     192.168.1.101 my_desktop
     192.168.1.201 my_robot

  Here you can test this: On M_A, ping M_B’s hostname. vice versa.
  http://blog.csdn.net/sonictl

Step4: Time Synchronization

  Time synchronization between machines is often critical in a ROS network since frame
  transformations and many message types are timestamped. An easy way to keep your
  computers synchronized is to install the Ubuntu chrony package on both your desktop
  and your robot. This package will keep your computer clocks synchronized with
  Internet servers and thus with each other.

  To install chrony, run the command:
  $ sudo apt-get install chrony

  After installation, the chrony daemon will automatically start and begin synchronizing
  your computer’s clock with a number of Internet servers.

Step5: Setting the ROS_MASTER_URI and ROS_HOS TNAME Variables
  These two environmental variables allocates the master and slaves of one ROS network.
  One ROS network can and have to have one Master.
  Modify the ~/.bashrc file to allocates the master and ROS_HOSTNAME variables.
  $ sudo gedit ~/.bashrc

  On M_A: (SLAVE), add these lines below to its ~/.bashrc file:
  » export ROS_HOSTNAME=my_desktop
  » export ROS_MASTER_URI=http://my_robot:11311
  Save and exit the ~/.bashrc file.

  On M_B: (Master), add these lines below to its ~/.bashrc file:
  » export ROS_HOSTNAME=my_robot
  » export ROS_MASTER_URI=http://my_robot:11311
  Save and exit the ~/.bashrc file.

  Close all the terminals and reopen, check the variables by this commands:
  $ echo $ROS_HOSTNAME
  $ echo $ROS_MASTER_URI

Step6: Test (blog.csdn.net/sonictl)
  You can use ssh to login to the other machine:
  $ ssh <user_name>@

  If you can log into it. you can run commands on the other machine through ssh tool.
 
  Example:http://blog.csdn.net/sonictl
  on M_A, open a terminal(Ctrl+Alt+T):
  $ ssh @my_robot
  $
  $ roscore

  http://blog.csdn.net/sonictl
  on M_A, open another terminal(Ctrl+Alt+T):
  $ rostopic list
  You can get:
  /rosout
  /rosout_agg
  That means you successfully configured the ROS network.

Good Luck! http://blog.csdn.net/sonictl

Published At
comments powered by Disqus