最近做机器人控制器项目需要使用到 EtherCAT 现场总线通信,通过 EtherCAT master 控制伺服驱动器带动多个轴同步运动,从而达到工业机器人多轴同步运动控制的效果。目前使用比较广泛的EtherCAT master有: TwinCAT,IgH EtherCAT Master,SOEM。IgH EtherCAT Master 具有较好的开源性、丰富的功能,因此我打算用 IgH EtherCAT Master 来完成机器人控制器与伺服驱动之间的通信功能。
系 统:Ubuntu16.04.1
内 核:Linux-4.19.72-rt26
考虑到EtherCAT通信以及机器人控制系统所必须的实时性,采用带有RT_PREEMPT 实时补丁的内核,内核编译安装步骤见:https://blog.csdn.net/qq_46136275/article/details/103815595
安装包:https://sourceforge.net/projects/etherlabmaster/
~$ mkdir etherlab-build
~/etherlab-build$ unzip etherlabmaster-code-0c011dc6dbc4facb3ee75f100181ce89814ecefa.zip
~/etherlab-build$ cd etherlabmaster-code-0c011dc6dbc4facb3ee75f100181ce89814ecefa/
$ ./bootstrap # to create the configure script
配置出现问题:
errors:
$ sudo apt-get install autoconf automake libtool
$ ./bootstrap
$ ./configure --enable-cycles --enable-hrtimer --enable-8139too=no
$ make -j$(nproc) all modules
切换到root用户
make modules_install install (root用户使用)
depmod
ln -s /usr/local/etc/init.d/ethercat /etc/init.d/ethercat
mkdir /etc/sysconfig
cp /usr/local/etc/sysconfig/ethercat /etc/sysconfig/ethercat
vi /etc/sysconfig/ethercat
echo KERNEL==\"EtherCAT[0-9]*\", MODE=\"0664\" > /etc/udev/rules.d/99-EtherCAT.rules
~$ ifconfig
网卡地址:
enp0s31f6 Link encap:Ethernet HWaddr c4:00:ad:41:ce:72
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
Interrupt:16 Memory:df300000-df320000
将 MASTER0_DEVICE=" " 引号中的内容换成要作为EtherCAT总线的网卡地址,
在 DEVICE_MODULES=" " 引号里面填入 generic
启动 EtherCAT Master:
#as root,
/etc/init.d/ethercat start
#显示如下
Starting EtherCAT master 1.5.2 done
查看主站:
~$ ethercat master
Master0
Phase: Idle
Active: no
Slaves: 0
Ethernet devices:
Main: c4:00:ad:41:ce:72 (attached)
Link: DOWN
Tx frames: 0
Tx bytes: 0
Rx frames: 0
Rx bytes: 0
Tx errors: 0
Tx frame rate [1/s]: 0 0 0
Tx rate [KByte/s]: 0.0 0.0 0.0
Rx frame rate [1/s]: 0 0 0
Rx rate [KByte/s]: 0.0 0.0 0.0
Common:
Tx frames: 0
Tx bytes: 0
Rx frames: 0
Rx bytes: 0
Lost frames: 0
Tx frame rate [1/s]: 0 0 0
Tx rate [KByte/s]: 0.0 0.0 0.0
Rx frame rate [1/s]: 0 0 0
Rx rate [KByte/s]: 0.0 0.0 0.0
Loss rate [1/s]: 0 0 0
Frame loss [%]: 0.0 0.0 0.0
Distributed clocks:
Reference clock: None
DC reference time: 0
Application time: 0
2000-01-01 00:00:00.000000000
attached 说明主站已经找到, 连接清能德创驱动器 CoolDrive R6,
查看从站:
~$ ethercat slaves
0 0:0 PREOP + CoolDrive R Series
1 0:1 PREOP + CoolDrive R Series
2 0:2 PREOP + CoolDrive R Series
3 0:3 PREOP + CoolDrive R Series
4 0:4 PREOP + CoolDrive R Series
5 0:5 PREOP + CoolDrive R Series
可以看到,扫描出6个从站,
配置清能德创驱动器的第一个从站,采用dc同步模式,测试 EtherCAT 主站的实时性以及通信的稳定性
~$ cd ethercat-drive/firsttest/
~/ethercat-drive/firsttest$ make
# as root
./ethercat.run
./ethercat.run: error while loading shared libraries: libethercat.so.1: cannot open shared object file: No such file or directory
报错的原因应该是找不到动态链接库的路径,在 bash shell 中运行:
$ echo '
##### IgH EtherCAT master
export ETHERLAB_DIR=/usr/local
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ETHERLAB_DIR/lib
##### ' >> ~/.bashrc
# as root
echo '
##### IgH EtherCAT master
export ETHERLAB_DIR=/usr/local
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ETHERLAB_DIR/lib
##### ' >>/root/.bashrc
关掉终端,重新打开再来,
./ethercat.run
# /dev/cpu_dma_latency set to 0us
ecrt_request_master is called
Configuring PDOs...
configureing PDO is completed!
Activating master...
Using priority 99.Starting cyclic function.
6 slave(s).
AL states: 0x02.
Link is up.
period 0 ... 3996741415
exec 0 ... 1523261472
latency 0 ... 6840
period 491462 ... 507165
exec 6946 ... 77037
latency 2189 ... 14468
Domain: WC 2.
Domain: State 1.
Domain: WC 3.
Domain: State 2.
AL states: 0x0A.
period 492654 ... 506992
exec 6908 ... 34459
latency 2159 ... 12090
period 490708 ... 512266
exec 6864 ... 31996
latency 2166 ... 17585
period 488660 ... 506853
exec 6930 ... 26862
latency 2165 ... 13966
period 489279 ... 508888
exec 6940 ... 34887
latency 2172 ... 13613
.......
从程序运行的终端输出可以看出,程序正常运行。从站能进入OP状态,DC同步周期为500us,短时间内的延迟大概在 10us内,有较好的实时性和稳定性。