《多协议传输控制器》使用以太网直连方式接入 ROS 系统
开始前准备:
硬件:
传输控制板*1
Type-B电源线*1
网线*1
带网口的电脑或者USB网卡*1
9~28V电源
软件:
安装Ubuntu的系统*1
安装好ROS2 Humble以上版本ROS系统
1. micro-ROS Agent 和 消息接口下载与构建
在主目录下创建,ros2_transmission_ws 目录及子目录src(其他目录也可以)
mkdir -p ~/ros2_transmission_ws/src
下载源码
cd ~/ros2_transmission_ws/src
git clone http://github.fishros.org/https://github.com/fishros/micro-ROS-Agent -b humble
git clone http://github.fishros.org/https://github.com/fishros/robot_interfaces
git clone http://github.fishros.org/https://github.com/micro-ROS/micro_ros_msgs.git -b humble
构建工作空间
cd ~/ros2_transmission_ws/
colcon build
---
Starting >>> micro_ros_agent
Starting >>> robot_interfaces
Finished <<< robot_interfaces [6.76s]
[Processing: micro_ros_agent]
--- stderr: micro_ros_agent
正克隆到 'xrceagent'...
切换到一个新分支 'ros2'
HEAD 目前位于 c25243c Enable Domain Override on Reference and XML Participant (#351)
CMake Warning (dev) at /usr/share/cmake-3.22/Modules/FindPackageHandleStandardArgs.cmake:438 (message):
The package name passed to `find_package_handle_standard_args` (tinyxml2)
does not match the name of the calling package (TinyXML2). This can lead
to problems in calling code that expects `find_package` result variables
(e.g., `_FOUND`) to follow a certain pattern.
Call Stack (most recent call first):
cmake/modules/FindTinyXML2.cmake:40 (find_package_handle_standard_args)
/opt/ros/humble/share/fastrtps/cmake/fastrtps-config.cmake:51 (find_package)
CMakeLists.txt:153 (find_package)
This warning is for project developers. Use -Wno-dev to suppress it.
---
Finished <<< micro_ros_agent [56.9s]
Summary: 2 packages finished [58.9s]
1 package had stderr output: micro_ros_agent
看到:Summary: 2 packages finished [58.9s] 就表示构建成功了,中间的警告请忽略。
2.完成硬件连接
这种连接方式的特点是,通过网线将雷达和电脑(控制卡)直连,优点是稳定。
连接方式:
电脑<----Type-B|USB线----->控制板USB口
电脑<----网线----->控制板网口
电源---->控制板电源口
3.使用配置助手修改配置
打开配置助手,扫描配置,确认如下选项的值,若不是,请手动修改。
配置项
描述
默认值
transport_mode
传输模式(例如以太网静态IP配置协议)
eth_static
server_ip
服务器的IP地址
192.168.168.5
eth_ip
设备的以太网IP地址
192.168.168.250
server_port
服务器的端口号
8888
传输模式我们这里采用 eth_static ,静态IP模式,并设置控制板的IP地址为 192.168.168.250 ,你也可以自己根据情况设定,但需要保证,服务IP,以太网IP在同一个子网下。
设置完成后请手动或者点击配置助手的重启设备进行重启。
4. 设置电脑IP地址
为了调试方便,我们手动来修改电脑中对应接口的网卡IP地址为配置中的server_ip,在Linux中使用 ifconfig可以临时设置对应网卡的IP地址。(注意只是临时,调试阶段建议使用,正式使用请文末方法)
使用ifconfig查看所有网卡
ifconfig
---
enx4ce173422378: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet6 fe80::915b:c170:f24c:844c prefixlen 64 scopeid 0x20<link>
ether 4c:e1:73:42:23:78 txqueuelen 1000 (以太网)
RX packets 22 bytes 7392 (7.3 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 136 bytes 15185 (15.1 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (本地环回)
RX packets 879222 bytes 758350473 (758.3 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 879222 bytes 758350473 (758.3 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
可以看到我这里有一个USB网卡:enx4ce173422378 是以太网(你的可能不是这个名字,请仔细辨别)
接着我们用ifconfig来设置IP地址,
sudo ifconfig enx4ce173422378 192.168.168.5 netmask 255.255.255.0
设置完成后再次查看这个网卡的IP地址。
ifconfig enx4ce173422378
enx4ce173422378: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.168.5 netmask 255.255.255.0 broadcast 192.168.168.255
inet6 fe80::915b:c170:f24c:844c prefixlen 64 scopeid 0x20<link>
ether 4c:e1:73:42:23:78 txqueuelen 1000 (以太网)
RX packets 8 bytes 2688 (2.6 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 38 bytes 6858 (6.8 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
可以看到这里的地址,inet 192.168.168.5 netmask 255.255.255.0 broadcast 192.168.168.255, 已经被成功设置了。
5.运行agent,接收消息
进入到工作空间,运行agent
cd ~/ros2_transmission_ws/
source install/setup.bash
ros2 run micro_ros_agent micro_ros_agent udp4 --port 8888 -v4
---
[1718247114.695455] info | UDPv4AgentLinux.cpp | init | running... | port: 8888
[1718247114.695599] info | Root.cpp | set_verbose_level | logger setup | verbose_level: 4
[1718247213.629429] info | Root.cpp | create_client | create | client_key: 0x17679F09, session_id: 0x81
[1718247213.629612] info | SessionManager.hpp | establish_session | session established | client_key: 0x17679F09, address: 192.168.168.250:47138
[1718247213.661143] info | ProxyClient.cpp | create_participant | participant created | client_key: 0x17679F09, participant_id: 0x000(1)
[1718247213.663314] info | ProxyClient.cpp | create_topic | topic created | client_key: 0x17679F09, topic_id: 0x000(2), participant_id: 0x000(1)
[1718247213.664917] info | ProxyClient.cpp | create_publisher | publisher created | client_key: 0x17679F09, publisher_id: 0x000(3), participant_id: 0x000(1)
[1718247213.666583] info | ProxyClient.cpp | create_datawriter | datawriter created | client_key: 0x17679F09, datawriter_id: 0x000(5), publisher_id: 0x000(3)
[1718247213.668377] info | ProxyClient.cpp | create_topic | topic created | client_key: 0x17679F09, topic_id: 0x001(2), participant_id: 0x000(1)
[1718247213.669829] info | ProxyClient.cpp | create_subscriber | subscriber created | client_key: 0x17679F09, subscriber_id: 0x000(4), participant_id: 0x000(1)
[1718247213.671514] info | ProxyClient.cpp | create_datareader | datareader created | client_key: 0x17679F09, datareader_id: 0x000(6), subscriber_id: 0x000(4)
[1718247213.673291] info | ProxyClient.cpp | create_topic | topic created | client_key: 0x17679F09, topic_id: 0x002(2), participant_id: 0x000(1)
[1718247213.674897] info | ProxyClient.cpp | create_publisher | publisher created | client_key: 0x17679F09, publisher_id: 0x001(3), participant_id: 0x000(1)
[1718247213.676535] info | ProxyClient.cpp | create_datawriter | datawriter created | client_key: 0x17679F09, datawriter_id: 0x001(5), publisher_id: 0x001(3)
[1718247213.678195] info | ProxyClient.cpp | create_topic | topic created | client_key: 0x17679F09, topic_id: 0x003(2), participant_id: 0x000(1)
[1718247213.679745] info | ProxyClient.cpp | create_subscriber | subscriber created | client_key: 0x17679F09, subscriber_id: 0x001(4), participant_id: 0x000(1)
[1718247213.681438] info | ProxyClient.cpp | create_datareader | datareader created | client_key: 0x17679F09, datareader_id: 0x001(6), subscriber_id: 0x001(4)
[1718247213.683368] info | ProxyClient.cpp | create_topic | topic created | client_key: 0x17679F09, topic_id: 0x004(2), participant_id: 0x000(1)
[1718247213.685201] info | ProxyClient.cpp | create_publisher | publisher created | client_key: 0x17679F09, publisher_id: 0x002(3), participant_id: 0x000(1)
[1718247213.686799] info | ProxyClient.cpp | create_datawriter | datawriter created | client_key: 0x17679F09, datawriter_id: 0x002(5), publisher_id: 0x002(3)
[1718247213.688641] info | ProxyClient.cpp | create_topic | topic created | client_key: 0x17679F09, topic_id: 0x005(2), participant_id: 0x000(1)
[1718247213.690344] info | ProxyClient.cpp | create_subscriber | subscriber created | client_key: 0x17679F09, subscriber_id: 0x002(4), participant_id: 0x000(1)
[1718247213.692013] info | ProxyClient.cpp | create_datareader | datareader created | client_key: 0x17679F09, datareader_id: 0x002(6), subscriber_id: 0x002(4)
看到上面的日志就表示连接成功了,接着就可以尝试订阅IO状态话题,打开新的终端,输入如下命令:
cd ~/ros2_transmission_ws/
source install/setup.bash
ros2 topic echo /read_io
---
analog: []
digital:
- 1
- 1
- 1
- 1
---
analog: []
digital:
- 1
- 1
- 1
- 1
---
...
可以看到这里每帧输出了四个数据,是四个输入IO的状态,这个话题默认以10Hz进行发布,发布频率可以修改。
使用 ros2 topic list 可以查看控制器节点可以支持的话题。
话题名称
消息接口
订阅/发布
/write_485
robot_interfaces/msg/RawUInt8
订阅
/write_can
robot_interfaces/msg/CanFrame
订阅
/write_io
robot_interfaces/msg/WriteIO
订阅
/read_485
robot_interfaces/msg/RawUInt8
发布
/read_can
robot_interfaces/msg/CanFrame
发布
/read_io
robot_interfaces/msg/ReadIO
发布
关于每个话题如何使用,可以参考例程篇介绍。
6. 永久设置静态IP地址
方法1
在 Ubuntu/Debian 系统终端中输入如下命令:
编辑/etc/network/interfaces文件:
sudo nano /etc/network/interfaces
添加或修改以下内容:
auto enx4ce173422378
iface enx4ce173422378 inet static
address 192.168.168.5
netmask 255.255.255.0
gateway 192.168.168.1
保存后重启即可永久设置。
方法2
查看网络接口名称:
使用以下命令查看你的网络接口名称:
ip link
找到你要设置静态 IP 的接口名称(例如 ens33 或 enx4ce173422378)。
编辑 netplan 配置文件:
打开 netplan 配置文件,例如:
sudo nano /etc/netplan/00-installer-config.yaml
在 ethernets 部分添加以下内容(根据你的实际情况进行调整):
network:
version: 2
renderer: networkd
ethernets:
enx4ce173422378:
addresses: [192.168.168.5/24]
routes:
- to: 0.0.0.0/0
via: 192.168.168.1
nameservers:
addresses: [8.8.8.8, 8.8.4.4]
设置文件权限
sudo chmod 600 /etc/netplan/00-installer-config.yaml
保存并应用更改:
保存文件并应用更改:
sudo netplan apply
验证设置:
使用以下命令验证你的设置:
ip addr show enx4ce173422378
ip route show