当我们使用ROS进行机器人间通信时,常常需要评估系统间通信带宽,因此,现在介绍一下。
获取话题名
rostopic list
当系统运行时,列出所以话题,找到机器人间通信的话题名称,比如/robot1/image_raw
查看通信带宽数据
rostopic bw /robot1/image_raw(话题名)
rostopic bw /topic_name
可以测量消息在特定话题上的带宽使用情况,包括传输速率和数据包大小输入要查看的话题名,可以显示带宽信息,它的形式如下:
subscribed to [/robot1/image_raw]
average: 1.325MB/s
mean: 1.36MB min: 1.36MB max: 1.36MB window: 11
average: 1.327MB/s
mean: 1.36MB min: 1.36MB max: 1.36MB window: 20
average: 1.337MB/s
mean: 1.36MB min: 1.36MB max: 1.36MB window: 30
average: 1.309MB/s
mean: 1.36MB min: 1.36MB max: 1.36MB window: 39
average: 1.319MB/s
mean: 1.36MB min: 1.36MB max: 1.36MB window: 49
将信息保存成txt文档
rostopic bw /robot1/image_raw > band.txt
将上述输出的带宽数据保存成txt文档,以便对他进行进一步处理,比如画出可视化曲线
绘制可视化曲线
使用python的matplotlib工具,当只对一个文档绘制一条曲线时,给出代码如下;
import matplotlib.pyplot as plt
time_points = []
bandwidth_values = []
with open('band.txt', 'r') as file:
lines = file.readlines()
for i, line in enumerate(lines):
if "average" in line:
avg_bandwidth_str = line.split()[1]
avg_bandwidth = float(avg_bandwidth_str[:-4]) / 1000
time_points.append(i)
bandwidth_values.append(avg_bandwidth)
plt.plot(time_points, bandwidth_values, marker='o', color='blue', label='Image')
plt.xlabel('Sample Index')
plt.ylabel('Bandwidth (MB/s)')
plt.title('Bandwidth Over Time')
plt.grid(True)
plt.legend() # 显示图例
plt.show()
当对多个文档绘制多条曲线,并用不同颜色且标注类别时,给出代码如下;
import matplotlib.pyplot as plt
# 文件名列表
file_names = ['band.txt', 'band_point.txt', 'band_point1.txt', 'band_point2.txt']
colors = ['blue', 'red', 'green', 'orange'] # 每个文件对应的颜色
labels = ['Image', 'Sequence_1', 'Sequence_2', 'Sequence_3'] # 每个文件的标签
# 创建图形和坐标轴
plt.figure()
for file_name, color, label in zip(file_names, colors, labels):
time_points = []
bandwidth_values = []
# 读取每个文件的数据
with open(file_name, 'r') as file:
lines = file.readlines()
# 解析文件内容
for i, line in enumerate(lines):
if "average" in line:
avg_bandwidth_str = line.split()[1] # 提取带宽数值
avg_bandwidth = float(avg_bandwidth_str[:-4]) / 1000 # 去掉 "MB/s KB/s" 并转换为浮点数
time_points.append(i)
bandwidth_values.append(avg_bandwidth)
# 绘制每个文件的带宽曲线
plt.plot(time_points, bandwidth_values, marker='o', color=color, label=label)
# 添加标签和标题
plt.xlabel('Sample Index')
plt.ylabel('Bandwidth (MB/s)')
plt.title('Bandwidth Over Time')
plt.grid(True)
plt.legend() # 显示图例
# 显示图形
plt.show()