最近打算买一台伺服电机,测试一下EtherCAT 接口。打算采用开源的lgh。网络上有许多的lgh 安装指南,但是照着安装仍然由一些问题,只能边做边记笔记。
硬件
- Dell T3360 台式机
- UP Squared 6000 边缘控制器
ubuntu 操作系统版本:
root@T3660:/home/yao2023/etherlab# uname -a
Linux T3660 6.2.0-31-generic #31~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Aug 16 13:45:26 UTC 2 x86_64 x86_64 x86_64 GNU/Linux
root@T3660:/home/yao2023/etherlab# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 22.04.3 LTS
Release: 22.04
Codename: jammy
下载源代码
git clone https://gitlab.com/etherlab.org/ethercat.git /home/yao2023/etherlab
配置和安装
配置
./bootstrap
sudo su
./configure --disable-8139too --prefix=/opt/etherlab --sysconfdir=/etc
安装
make
make all modules
sudo make modules_install install
sudo depmod
查看以太网的mac地址
使用 ifconfig
root@T3660:/home/yao2023/etherlab# ifconfig
docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255
inet6 fe80::42:cbff:fe8a:1e8f prefixlen 64 scopeid 0x20<link>
ether 02:42:cb:8a:1e:8f txqueuelen 0 (以太网)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 77 bytes 10819 (10.8 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
enp0s31f6: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
ether 74:86:e2:19:66:0f txqueuelen 1000 (以太网)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
device interrupt 19 memory 0x72600000-72620000
enp4s0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
ether a0:36:9f:30:32:a1 txqueuelen 1000 (以太网)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
device memory 0x72100000-721fffff
chmod +x /opt/etherlab/etc/init.d/ethercat
sudo cp -f script/init.d/ethercat /etc/init.d/
sudo mkdir -p /etc/sysconfig/
sudo chown yao.yao /etc/sysconfig/
cp -f /opt/etherlab/etc/sysconfig/ethercat /etc/sysconfig/
sudo mkdir -p /etc/udev/rules.d/
sudo chown yao.yao /etc/udev/rules.d/
touch /etc/udev/rules.d/99-ethercat.rules
echo MASTER0_DEVICE="$(cat /sys/class/net/enp2s0/address)" > /etc/sysconfig/ethercat
echo DEVICE_MODULES="generic" >> /etc/sysconfig/ethercat
chmod go+rwx /etc/udev/rules.d/99-ethercat.rules
echo "KERNEL=="\"EtherCAT[0-9]*\"", MODE="\"777\"", GROUP=""\"ethercat\"" > /etc/udev/rules.d/99-ethercat.rules
UP Squared 6000 系统上的安装
硬件 ATOM 6000
操作系统 ubuntu 22.04
内核: 15.5.0-1043-intel-iotg
UP Squared 6000 计算机上遇到了下面的问题,启动时:
root@yao-UPN-EHL01:/home/yao/etherlab# sudo /etc/init.d/ethercat start
Starting EtherCAT master 1.5.2 modprobe: ERROR: could not insert 'ec_master': Exec format error
failed
网络上建议的方法:
使用dmesg命令查看原因。
dmesg
[ 1158.061004] ec_master: disagrees about version of symbol module_layout
[ 1414.135696] ec_master: disagrees about version of symbol module_layout
结果是disagrees about version of symbol module_layout。看来是版本不兼容。ec_master是一个linux 内核库,它必须与当前运行的内核兼容。
1 使用uname -r 命令看当前内核的版本为 15.5.0-1046-intel-iotg
2 使用modinfo命令看/lib/modules/5.15.0-1043-intel-iotg/ethercat/master/ec_master.ko的信息
modinfo /lib/modules/5.15.0-1043-intel-iotg/ethercat/master/ec_master.ko
filename: /lib/modules/5.15.0-1043-intel-iotg/ethercat/master/ec_master.ko
version: 1.5.2 1.5.2-343-gb029271b
license: GPL
description: EtherCAT master driver module
author: Florian Pose <fp@igh-essen.com>
srcversion: 3EC58E246A5BE6A6C05916D
depends:
retpoline: Y
name: ec_master
vermagic: 5.15.0-1036-intel-iotg SMP mod_unload modversions
parm: main_devices:MAC addresses of main devices (array of charp)
parm: backup_devices:MAC addresses of backup devices (array of charp)
parm: debug_level:Debug level (uint)
parm: run_on_cpu:Bind kthreads to a specific cpu (uint)
结果发现它们的版本不同,一个是1043,一个是1036。可能是我一开始指定了15.5.0-1036-intel-iotg 安装后,以后make clean 去不掉mc-master.ko。于是:
删除 /lib/modules/5.15.0-1043-intel-iotg/ec_master.ko(这个版本是15.5.0-1036-intel-iotg)
然后,重新编译,成功了。
运行
root@T3660:/home/yao2023/etherlab# sudo /etc/init.d/ethercat start
Starting EtherCAT master 1.6.0-rc1 done
root@T3660:/home/yao2023/etherlab# sudo /etc/init.d/ethercat stop
Shutting down EtherCAT master 1.6.0-rc1 done
应用代码
/**
* compile : gcc test.c -o test -I/opt/etherlab/include -L/opt/etherlab/lib -lethercat
*/
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/mman.h>
/****************************************************************************/
#include "ecrt.h"
/****************************************************************************/
/*Application Parameters*/
#define TASK_FREQUENCY 10 /*Hz*/
#define TARGET_VELOCITY 110000 /*target velocity*/
#define PROFILE_VELOCITY 3 /*Operation mode for 0x6060:0*/
/*****************************************************************************/
/*EtherCAT*/
static ec_master_t *master = NULL;
static ec_master_state_t master_state = {};
static ec_domain_t *domain1 = NULL;
static ec_domain_state_t domain1_state = {};
static ec_slave_config_t *sc;
static ec_slave_config_state_t sc_state = {};
/****************************************************************************/
/*Process Data*/
static uint8_t *domain1_pd = NULL;
#define DM3E 0,0 /*EtherCAT address on the bus*/
#define VID_PID 0x00004321,0x00008100 /*Vendor ID, product code*/
/*Offsets for PDO entries*/
static struct{
unsigned int operation_mode;
unsigned int ctrl_word;
unsigned int target_velocity;
unsigned int status_word;
unsigned int current_velocity;
unsigned int current_position;
}offset;
const static ec_pdo_entry_reg_t domain1_regs[] = {
{DM3E, VID_PID, 0x6040, 0, &offset.ctrl_word},
{DM3E, VID_PID, 0x6060, 0, &offset.operation_mode },
{DM3E, VID_PID, 0x60FF, 0, &offset.target_velocity},
{DM3E, VID_PID, 0x6041, 0, &offset.status_word},
{DM3E, VID_PID, 0x606C, 0, &offset.current_velocity},
{DM3E, VID_PID, 0x6064, 0, &offset.current_position},
{}
};
/***************************************************************************/
/*Config PDOs*/
static ec_pdo_entry_info_t device_pdo_entries[] = {
/*RxPdo 0x1600*/
{0x6040, 0x00, 16},
{0x6060, 0x00, 8 },
{0x60FF, 0x00, 32},
/*TxPdo 0x1A00*/
{0x6041, 0x00, 16},
{0x606C, 0x00, 32},
{0x6064, 0x00, 32}
};
static ec_pdo_info_t device_pdos[] = {
//RxPdo
{0x1600, 3, device_pdo_entries + 0 },
//TxPdo
{0x1A00, 3, device_pdo_entries + 3 }
};
static ec_sync_info_t device_syncs[] = {
{ 0, EC_DIR_OUTPUT, 0, NULL, EC_WD_DISABLE },
{ 1, EC_DIR_INPUT, 0, NULL, EC_WD_DISABLE },
{ 2, EC_DIR_OUTPUT, 1, device_pdos + 0, EC_WD_DISABLE },
{ 3, EC_DIR_INPUT, 1, device_pdos + 1, EC_WD_DISABLE },
{ 0xFF}
};
/**************************************************************************/
/*************************************************************************/
void check_domain1_state(void)
{
ec_domain_state_t ds;
ecrt_domain_state(domain1, &ds);
if (ds.working_counter != domain1_state.working_counter)
{
printf("Domain1: WC %u.\n", ds.working_counter);
}
if (ds.wc_state != domain1_state.wc_state)
{
printf("Domain1: State %u.\n", ds.wc_state);
}
domain1_state = ds;
}
void check_master_state(void)
{
ec_master_state_t ms;
ecrt_master_state(master, &ms);
if (ms.slaves_responding != master_state.slaves_responding)
{
printf("%u slave(s).\n", ms.slaves_responding);
}
if (ms.al_states != master_state.al_states)
{
printf("AL states: 0x%02X.\n", ms.al_states);
}
if (ms.link_up != master_state.link_up)
{
printf("Link is %s.\n", ms.link_up ? "up" : "down");
}
master_state = ms;
}
/****************************************************************************/
void check_slave_config_states(void)
{
ec_slave_config_state_t s;
ecrt_slave_config_state(sc, &s);
if (s.al_state != sc_state.al_state)
{
printf("slave: State 0x%02X.\n", s.al_state);
}
if (s.online != sc_state.online)
{
printf("slave: %s.\n", s.online ? "online" : "offline");
}
if (s.operational != sc_state.operational)
{
printf("slave: %soperational.\n", s.operational ? "" : "Not ");
}
sc_state = s;
}
/*******************************************************************************/
void cyclic_task()
{
static uint16_t command=0x004F;//用来帮助判断状态字的值
uint16_t status;
/*Receive process data*/
ecrt_master_receive(master);
ecrt_domain_process(domain1);
/*Check process data state(optional)*/
check_domain1_state();
//Check for master state
check_master_state();
//Check for slave configuration state(s)
check_slave_config_states();
/*Read state*/
status = EC_READ_U16(domain1_pd + offset.status_word);//读取状态字
//DS402 CANOpen over EtherCAT status machine
if( (status & command) == 0x0040 )
{
EC_WRITE_U16(domain1_pd + offset.ctrl_word, 0x0006 );
EC_WRITE_S8(domain1_pd + offset.operation_mode, PROFILE_VELOCITY);
//设置控制模式
command = 0x006F;
}
else if( (status & command) == 0x0021)
{
EC_WRITE_U16(domain1_pd + offset.ctrl_word, 0x0007 );
command = 0x006F;
}
else if( (status & command) == 0x0023)
{
EC_WRITE_U16(domain1_pd + offset.ctrl_word, 0x000f );
command = 0x006F;
}
//operation enabled
else if( (status & command) == 0x0027)
{
EC_WRITE_S32(domain1_pd + offset.target_velocity, TARGET_VELOCITY);
EC_WRITE_U16(domain1_pd + offset.ctrl_word, 0x001f );
}
/*Send process data*/
ecrt_domain_queue(domain1);
ecrt_master_send(master);
}
/****************************************************************************/
int main(int argc, char **argv)
{
printf("Requesting master...\n");
master = ecrt_request_master(0);
if (!master)
{
exit(EXIT_FAILURE);
}
domain1 = ecrt_master_create_domain(master);
if (!domain1)
{
exit(EXIT_FAILURE);
}
if (!(sc = ecrt_master_slave_config(master, DM3E, VID_PID)))
{
fprintf(stderr, "Failed to get slave configuration for slave!\n");
exit(EXIT_FAILURE);
}
printf("Configuring PDOs...\n");
if (ecrt_slave_config_pdos(sc, EC_END, device_syncs))
{
fprintf(stderr, "Failed to configure slave PDOs!\n");
exit(EXIT_FAILURE);
}
else
{
printf("*Success to configuring slave PDOs*\n");
}
if (ecrt_domain_reg_pdo_entry_list(domain1, domain1_regs))
{
fprintf(stderr, "PDO entry registration failed!\n");
exit(EXIT_FAILURE);
}
printf("Activating master...\n");
if (ecrt_master_activate(master)) {
exit(EXIT_FAILURE);
}
else
{
printf("*Master activated*\n");
}
if (!(domain1_pd = ecrt_domain_data(domain1))) {
exit(EXIT_FAILURE);
}
printf("*It's working now*\n");
while (1)
{
usleep(100000/TASK_FREQUENCY);
cyclic_task();
}
return EXIT_SUCCESS;
}
编译
gcc testB.c -o test -lethercat
运行
- 启动ethercat
- 运行程序test
结束
下一步,连接松下的伺服驱动,希望顺利一点。