ROS2入门教程-跨域网络通讯
ROS2入门教程-异地跨域网络通讯
说明:
- 介绍如何异地跨域实现ROS2通讯
- 环境: ubuntu22.04 + humble + n2n (参考)
- 或利用wireguard搭建的基于跨域的内网
跨域的内网搭建步骤:
- 利用n2n的搭建基于跨域的内网,参考教程
- n2n是需要一台主机运行supernode,其他从机运行edge。所有edge通过主机建立交互。
- 假设有ABC三个设备
- 设备A,安装ubuntu下运行supernode的,IP为外网ip,比如:15.4.3.6
- 设备B,安装ubuntu22.04 + ros2的hubmle,运行edge,异地网络,设置局域网内IP为:10.0.0.3
- 设备c,安装ubuntu22.04 + ros2的hubmle,运行edge,本地网络,设置局域网内IP为:10.0.0.4
- 配置成功之后,设备B和设备C就构成一个异地局域网。
设备B,配置中间件步骤:
- 使用cyclonedds中间件
- 增加如下行到~/.bashrc
echo "export RMW_IMPLEMENTATION=rmw_cyclonedds_cpp " >> ~/.bashrc
echo "export CYCLONEDDS_URI=file:///home/ubuntu/cyclonedds.xml " >> ~/.bashrc
- 新增加cyclonedds.xml文件,在用户根目录,假设为/home/ubuntu/
- 文件内容为:
<?xml version="1.0" encoding="UTF-8" ?>
<CycloneDDS xmlns="https://cdds.io/config" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://cdds.io/config https://raw.githubusercontent.com/eclipse-cyclonedds/cyclonedds/master/etc/cyclonedds.xsd">
<Domain Id="any">
<General>
<Interfaces>
<NetworkInterface name="edge0"/>
</Interfaces>
<AllowMulticast>default</AllowMulticast>
</General>
<Discovery>
<Peers>
<Peer address="10.0.0.3"/>
<Peer address="10.0.0.4"/>
</Peers>
<ParticipantIndex>auto</ParticipantIndex>
</Discovery>
<Tracing>
<Verbosity>config</Verbosity>
<OutputFile>cdds.log.${CYCLONEDDS_PID}</OutputFile>
</Tracing>
</Domain>
</CycloneDDS>
设备C,配置中间件步骤:
- 步骤与设备B一样
- 使用cyclonedds中间件
- 增加如下行到~/.bashrc
echo "export RMW_IMPLEMENTATION=rmw_cyclonedds_cpp " >> ~/.bashrc
echo "export CYCLONEDDS_URI=file:///home/ubuntu/cyclonedds.xml " >> ~/.bashrc
- 新增加cyclonedds.xml文件,在用户根目录,假设为/home/ubuntu/
- 文件内容为:
<?xml version="1.0" encoding="UTF-8" ?>
<CycloneDDS xmlns="https://cdds.io/config" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://cdds.io/config https://raw.githubusercontent.com/eclipse-cyclonedds/cyclonedds/master/etc/cyclonedds.xsd">
<Domain Id="any">
<General>
<Interfaces>
<NetworkInterface name="edge0"/>
</Interfaces>
<AllowMulticast>default</AllowMulticast>
</General>
<Discovery>
<Peers>
<Peer address="10.0.0.3"/>
<Peer address="10.0.0.4"/>
</Peers>
<ParticipantIndex>auto</ParticipantIndex>
</Discovery>
<Tracing>
<Verbosity>config</Verbosity>
<OutputFile>cdds.log.${CYCLONEDDS_PID}</OutputFile>
</Tracing>
</Domain>
</CycloneDDS>
测试:
- 在设备B下启动
$ ros2 run demo_nodes_cpp talker
[INFO] [1672128500.852560738] [talker]: Publishing: 'Hello World: 1'
[INFO] [1672128501.852639418] [talker]: Publishing: 'Hello World: 2'
......
[INFO] [1672128502.852623038] [talker]: Publishing: 'Hello World: 3705'
[INFO] [1672128503.852701596] [talker]: Publishing: 'Hello World: 3706'
[INFO] [1672128504.852700431] [talker]: Publishing: 'Hello World: 3707'
[INFO] [1672128505.852761921] [talker]: Publishing: 'Hello World: 3708'
[INFO] [1672128506.852749808] [talker]: Publishing: 'Hello World: 3709'
[INFO] [1672128507.852819464] [talker]: Publishing: 'Hello World: 3710'
[INFO] [1672128508.852827849] [talker]: Publishing: 'Hello World: 3711'
[INFO] [1672128509.852867541] [talker]: Publishing: 'Hello World: 3712'
- 在设备C下输出话题列表
$ ros2 topic list
/chatter
/parameter_events
/rosout
- 在设备C下输出话题内容
$ ros2 topic echo /chatter
data: 'Hello World: 3823'
---
data: 'Hello World: 3824'
---
data: 'Hello World: 3825'
---
data: 'Hello World: 3826'
---
- 正常情况下,能看到话题列表和话题内容
获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号