关于EtherCAT的主站DC补偿,可以看博主的这篇文章
主站DC补偿,EtherCAT的心电图终于正常啦(1)
测量主站DC抖动主要有两种方式,各有优缺点:
方案1:用示波器采集同一个ECAT从站的irq和sync0信号,观察两信号的周期&相位关系
方案2:用EtherCAT抓包仪采集EtherCAT的PDO报文。
方案1的优点就是,基于从站的irq和sync0信号是最准确的。缺点就是,使用不方便。因为采集irq和sync0信号需要把从站设备拆开,然后挂接示波器。另外,并不是所有从站设备都引出了irq和sync0引脚。如果手上没有从站设备的原理图的话,你甚至不清楚irq和sync0引脚在哪里。
方案2的优点是,测量最灵活,只需将抓包工具接入EtherCAT网络中即可。缺点是,无法反映irq信号和sync0信号的相位关系。
本文将重点介绍基于EtherCAT抓包仪进行DC抖动分析。
tb:拉松电子
测试环境
EtherCAT运动控制器(主站)带16个EtherCAT设备(从站),通讯周期250us。如下图所示,将EtherCAT抓包仪接入EtherCAT环路之中。如果想准确测量主站DC周期,尽可能将EtherCAT抓包仪接入运动控制器(主站)和第一个EtherCAT从站之间。

配置python环境
安装python
博主安装的版本如下
python-3.9.7-amd64.exe
安装python工具包
主要是pandas和matplotlib。
命令行中执行如下指令:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyinstaller
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple matplotlib
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pandas

将wireshark安装目录添加到windows环境变量中

因为后面要在命令行中调用wireshark的“tshark”工具,如果不添加到环境变量中的话,系统可能找不到“tshark”工具
wireshark抓包

wireshark过滤

在红框处可以输入报文过滤条件。。
比如“(ecat.cmd==lrw)&&(ecat.cnt==0)”
(ecat.cmd==lrw)&&(ecat.cnt==0)
这行代码的意思就是,把主站发出的pdo报文给过滤出来。
导出特定分组

给文件命名,我这里命名为tap,然后保存。


使用tshark工具提取timestamp
在命令行中,切换到tap.pcapng文件所在目录。然后执行如下命令:
tshark -r "tap.pcapng" -n -T fields -e esl.timestamp > "test.csv"
等待tshark将t"tap.pcapng"文件中的timestamp提取到"test.csv"中。如果文件大,可能会等久一些。
基于python脚本分析DC周期
python脚本
博主写了一个DC周期计算的脚本,开源给大家使用
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import os
print(os.getcwd()) # 查看当前工作目录
df = pd.read_csv("test.csv",encoding="utf-16",header=None)
df.columns = ['hex_data']
# 去除0x前缀
df['clean_hex'] = df['hex_data'].str.replace('0x', '', regex=False)
# 使用apply函数和int(x, 16)将十六进制字符串转换为整数
df['uint64_data'] = df['clean_hex'].apply(lambda x: int(x, 16) if pd.notna(x) else np.nan)
# 计算差值
df['diff'] = df['uint64_data'].diff()
# 显示数据框的前几行
print(df.head(5))
# 创建可视化
plt.figure(figsize=(12, 8))
# 直方图
plt.subplot(2, 1, 1)
plt.hist(df['diff'].dropna(), bins=50, alpha=0.7, color='skyblue', edgecolor='black')
plt.xlabel('diff')
plt.ylabel('frequency')
plt.title('histogram')
plt.grid(True, alpha=0.3)
# 差值图
plt.subplot(2, 1, 2)
#折线图
#plt.plot(df.index[1:], df['diff'][1:], 'r-', label='cycle') # 跳过第一个NaN值
#散点图
plt.scatter(df.index[1:], df['diff'][1:], color='red', alpha=0.6, label='cycle')
plt.xlabel('index')
plt.ylabel('timestamp(ns)')
plt.title('DC cycle')
plt.grid(True)
plt.legend()
plt.tight_layout()
plt.show()
# 差值统计信息
print("差值统计信息:")
print(f"平均差值: {df['diff'].mean()}")
print(f"最大差值: {df['diff'].max()}")
print(f"最小差值: {df['diff'].min()}")
print(f"标准差: {df['diff'].std()}")
DC周期分析


从图中可知,DC周期抖动范围是250000 - 40 ns到 250000 + 8 ns范围。我们的EtherCAT控制卡的DC周期设置的是250000ns。我这里用的控制卡应该是基于FPGA来跑EtherCAT主站的,所以主站周期抖动很小。
我给大家看一下另一个牌子的控制卡,跑250us周期的效果。


3848

被折叠的 条评论
为什么被折叠?



