| name | ros2-distributed-communication |
| description | ROS2 分布式通讯技能 - DDS/RMW 配置、多机器通讯、网络优化、跨域配置 |
| user-invocable | true |
| argument-hint | 分布式通讯 OR dds OR rmw OR 多机器 OR 跨域 OR network |
ROS2 Distributed Communication Skill
ROS2 DDS/RMW 分布式通讯完整指南
何时使用
当需要以下帮助时使用此技能:
- 多机器机器人通讯
- DDS 中间件配置
- 网络优化和 QoS
- 跨域通讯配置
- 通讯诊断和调试
快速参考
RMW 实现
| 实现 | 包 | 特性 |
|---|
| CycloneDDS | rclcpp | 高性能,默认 |
| FastRTPS | fastrtps | 低延迟 |
| Connext | rti-connext | 企业级(收费) |
架构
┌─────────────┐ ┌─────────────┐
│ Node A │ │ Node B │
│ (机器 A) │ │ (机器 B) │
└──────┬──────┘ └──────┬──────┘
│ │
└────────┬──────────┘
│
┌──────┴──────┐
│ RMW │
│ (DDS层) │
└──────┬──────┘
│
┌──────┴──────┐
│ Network │
│ (UDP/TCP) │
└─────────────┘
RMW 配置
切换中间件
export RMW_IMPLEMENTATION=rmw_fastrtps_cpp
export RMW_IMPLEMENTATION=rmw_cyclonedds_cpp
export RMW_IMPLEMENTATION=rmw_connext_cpp
ros2 doctor -r
运行时切换
rclcpp::init(argc, argv,
rclcpp::InitOptions::from_raw_arguments(
"--rmw", "rmw_fastrtps_cpp"));
多机器通讯
网络发现配置
<?xml version="1.0" ?>
<profiles>
<participant name="participant_name">
<rtps>
<discoveryProtocol>SIMPLE</discoveryProtocol>
<discoveryServers>
<RemoteServer prefix="uuid">
<metatrafficUnicastLocatorList>
<locator>
<udpv4>
<address>192.168.1.100</address>
<port>11811</port>
</udpv4>
</locator>
</metatrafficUnicastLocatorList>
</RemoteServer>
</discoveryServers>
</rtps>
</participant>
</profiles>
设置环境变量
export ROS_LOCALHOST_ONLY=0
export RMW_FASTRTPS_DEFAULT_PROFILES_FILE=/path/to/fastrtps.xml
export FASTRTPS_DEFAULT_PROFILE=file:///path/to/fastrtps.xml
export ROS_DOMAIN_ID=42
通讯测试
ros2 run demo_nodes_cpp listener
ros2 run demo_nodes_cpp talker
ros2 topic list
QoS 网络配置
可靠传输配置
rclcpp::QoS reliable_qos(10);
reliable_qos.reliability(RMW_QOS_POLICY_RELIABILITY_RELIABLE)
.durability(RMW_QOS_POLICY_DURABILITY_VOLATILE);
auto sub = node->create_subscription<MyMsg>(
"/topic", reliable_qos, callback);
跨域配置
<?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="0">
<discovery>
<ParticipantIndex>auto</ParticipantIndex>
<MaxAutoParticipantIndex>50</MaxAutoParticipantIndex>
</discovery>
<internal>
<NetworkInterfaceAddress>auto</NetworkInterfaceAddress>
</internal>
</domain>
<domain id="1">
<discovery>
<ParticipantIndex>auto</ParticipantIndex>
</discovery>
</domain>
</cyclonedds>
性能优化
网络绑定
<profiles>
<participant name="participant_name">
<rtps>
<transport>
<sendSocketBufferSize>
<size>1048576</size>
</sendSocketBufferSize>
<receiveSocketBufferSize>
<size>1048576</size>
</receiveSocketBufferSize>
</transport>
<metatrafficUnicastLocatorList>
<locator>
<udpv4>
<address>192.168.1.10</address>
<port>7400</port>
</udpv4>
</locator>
</metatrafficUnicastLocatorList>
</rtps>
</participant>
</profiles>
缓冲区配置
rclcpp::QoS high_throughput_qos(100);
high_throughput_qos.best_effort()
.durability_volatile();
auto pub = node->create_publisher<sensor_msgs::msg::PointCloud2>(
"/points", high_throughput_qos);
跨域通讯 (Domain)
域 ID 配置
export ROS_DOMAIN_ID=0
export ROS_DOMAIN_ID=1
export ROS_DOMAIN_ID=2
域过滤
<dataReader name="my_reader">
<topic>
<name>robot/chassis</name>
<dataType>MyType</dataType>
<domainId>0</domainId>
</topic>
</dataReader>
常见问题排查
节点不可见
ping other_machine
echo $ROS_DOMAIN_ID
ros2 doctor
ros2 node list
ros2 node info /talker
话题不通
ros2 topic list
ros2 topic type /chatter
ros2 topic pub /chatter std_msgs/msg/String "data: test"
ros2 topic echo /chatter
ros2 daemon start
ros2 doctor --report
命令行工具
ros2 pkg list | grep rmw
ros2 topic hz /chatter
ros2 topic bw /chatter
ros2 network_scanner
export RCUTILS_CONSOLE_OUTPUT_FORMAT="[{severity}] [{name}]: {message}"
export RCUTILS_LOGGING_BUFFER_OUTPUT_STRING=0
export RMW_IMPLEMENTATION=rmw_fastrtps_cpp
export FASTRTPS_DEFAULT_PROFILE=file://fastrtps_debug.xml
最佳实践
- 统一域 ID: 多机器使用相同 ROS_DOMAIN_ID
- QoS 匹配: 确保发布/订阅 QoS 兼容
- 网络优化: 使用有线网络,低延迟关键数据
- 监控: 定期检查话题带宽和延迟
- 隔离: 不同系统使用不同域 ID