EtherCAT igh函数尝试

本文介绍使用ecrt_master_get_slave函数从EtherCAT主站获取从站信息的方法。通过示例代码展示了如何遍历所有从站位置,读取并打印从站的位置、供应商ID和产品代码等关键信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

尝试igh函数的功能,加深对不同函数的认识,总结相关函数的用法。

1.ecrt_master_get_slave

/** Obtains slave information.
 *
 * Tries to find the slave with the given ring position. The obtained
 * information is stored in a structure. No memory is allocated on the heap in
 * this function.
 *
 * \attention The pointer to this structure must point to a valid variable.
 *
 * \return 0 in case of success, else < 0
 */
int ecrt_master_get_slave(
        ec_master_t *master, /**< EtherCAT master */
        uint16_t slave_position, /**< Slave position. */
        ec_slave_info_t *slave_info /**< Structure that will output the
                                      information */
        );
        
//example----------------------------------------------------------------------
typedef struct {
    unsigned short  position;
    unsigned int    vendor_id;
    unsigned int    product_code;
}EL_Slave_t;

int  GetSlavesInfo(EL_Slave_t* slaves, int num)
{
    ec_master_t* master = ecrt_request_master(0);
    ec_slave_info_t  tmps;
    int  i;
    for(i = 0;i< num;i++)
    {
        if(ecrt_master_get_slave(master, i, &tmps) == 0)
        {
            (slaves + i)->position = tmps.position;
            (slaves + i)->vendor_id = tmps.vendor_id;
            (slaves + i)->product_code = tmps.product_code;
            printf("slave position:%d,vendor_id:%d,product_code:%d,name:%s\n",
                   tmps.position, tmps.vendor_id, tmps.product_code, tmps.name);
        }
        else
            break;
    }
    ecrt_release_master(master);
    return i;
}

int main(int argc, char *argv[])
{
	...
    EL_Slave_t  slaves[16];
    int num = GetSlavesInfo(slaves, 16);
    printf("num = %d\n",num);
    ...
}

//测试结果、终端输出
slave position:0,vendor_id:9,product_code:37458,name:LAN9252-EVB-HBI
slave position:1,vendor_id:2,product_code:72100946,name:EK1100 EtherCAT-Koppler (2A E-Bus)
slave position:2,vendor_id:2,product_code:131608658,name:EL2008 8K. Dig. Ausgang 24V, 0.5A
slave position:3,vendor_id:2,product_code:131608658,name:EL2008 8K. Dig. Ausgang 24V, 0.5A
slave position:4,vendor_id:2,product_code:131608658,name:EL2008 8K. Dig. Ausgang 24V, 0.5A
num = 5
1.基于Igh-master1.5.2的多种Ethercat IO 模块及伺服电机的操作例子 2.基于Ethercat-Ighmaster二次开发库简化你的开发流程 所有源码及文档托管在码云平台,附件中有代码仓库地址 3.含有IghMaster1.5.2最新版本代码,编译脚本及安装到 最新的代码托管在: https://gitee.com/wllw7176/MyEthercat-IGH-1.5.2.git EtherCAT 1.5.2 编译及使用说明 1.交叉编译源码 源码中有内核模块编译,指令定模块目录 根据实际情况修改build_ethercat-1.5.2.sh中以下几个变量: output_dir='output' #编译输出目录 module_install_dir='module_install' #内核模块安装目录 kernel_source_dir='/mnt/fs_ext/imx6/linux-3.0.35' #内核源码目录 lib_modules_kernel_promt='3.0.35-2666-gbdde708' #内核版本号 host=arm-fsl-linux-gnueabi #交叉编译链前缀 注意要先编译内核,然后编译Ethrecat因为Ethercat依赖于内核通用网卡模块 以root用户运行./build_ethercat-1.5.2.sh #编译具体根据提示选择y/n就可以了 ---------------------------------------------------------------------------------------------------------------------------------------------- 手动安装Ethercat到ARM开发板: 2.内核模块安装 2.1 将output文件下通lib_modules_kernel_promt='3.0.35-2666-gbdde708'(build_ethercat-1.5.2.sh 中定义)放入开发板/lib/modules下 2.2 开发板中执行"depmod"命令 3. output里面的其它文件放入开发板对应位置 4. 设置参数 修改/etc/sysconfig/ethercat MASTER0_DEVICE="e4:f3:f5:c6:41:b6" #与ethercat绑定的Mac地址 DEVICE_MODULES="generic" #通用网卡就填generic,其余支持网卡换成模块名字就行 5.添加udev规则 echo KERNEL==\"EtherCAT[0-9]*\", MODE=\"0664\" > /etc/udev/rules.d/99-EtherCAT.rules 6. 启动服务 /etc/init.d/ethercat restart 出现以下信息表明移植成功 Shutting down EtherCAT master 1.5.2 done Starting EtherCAT master 1.5.2 ec_generic: Binding socket to interface 3 (eth0). done 7. 应用层测试 root@linaro-ubuntu-desktop:~# ethercat Please specify a command! Usage: ethercat [OPTIONS] [ARGUMENTS] Commands (can be abbreviated): alias Write alias addresses. config Show slave configurations. cstruct Generate slave PDO information in C language. data Output binary domain process data. debug Set the master's debug level. domains Show configured domains. download Write an SDO entry to a slave. eoe Display Ethernet over EtherCAT statictics. foe_read Read a file from a slave via FoE. foe_write Store a file on a slave via FoE. graph Output the bus topology as a graph. master Show master and Ethernet device information. pdos List Sync managers, PDO assignment and mapping. reg_read Output a slave's register contents. reg_write Write data to a slave's registers. rescan Rescan the bus. sdos List SDO dictionaries. sii_read Output a slave's SII contents. sii_write Write SII contents to a slave. slaves Display slaves on the bus. soe_read Read an SoE IDN from a slave. soe_write Write an SoE IDN to a slave. states Request application-layer states. upload Read an SDO entry from a slave. version Show version information. xml Generate slave information XML. Global options: --master -m Comma separated list of masters to select, ranges are allowed. Examples: '1,3', '5-7,9', '-3'. Default: '-' (all). --force -f Force a command. --quiet -q Output less information. --verbose -v Output more information. --help -h Show this help. Numerical values can be specified either with decimal (no prefix), octal (prefix '0') or hexadecimal (prefix '0x') base. Call 'ethercat --help' for command-specific help. 以上内容为基本的使用,进一步使用要结合ethercat说明文档和电机说明。 ---------------------------------------------------------------------------------------------------------------------------------------------- 自动安装Ethercat到ARM开发板: 将output目录复制到开发板然后运行output目录下install_to_arm.sh脚本 执行6,7步骤测试自动安装是否成功 ---------------------------------------------------------------------------------------------------------------------------------------------- 问题汇总: (1)Starting EtherCAT master 1.5.2 EtherCAT ERROR: MAC address may not be empty. FATAL: Error inserting ec_master (/lib/modules/3.0.35-2666-gbdde708-gbdbf2583/mnt/hgfs/win_linux_share/linux/download/ethercat-1.5.2/module_install/master/ec_master.ko): Invalid argument failed Mac地址为空,将对应的mac地址MASTER0_DEVICE="e4:f3:f5:c6:41:b6" (2)编译Ethercat 一定要保证所用编译器与内核与硬件平台一致,否则会出现内核模块不能使用或者其它未知问题 (3)内核源码要先编译通过,否则Ethercat模块将不能编译通过,因为Ethercat模块依赖于内核本身驱动模块。
<think>好的,我现在需要帮助用户了解如何使用EtherCATIGH库控制电机运行。首先,我需要整理用户提供的引用内容,看看里面有哪些有用的信息。 引用[1]提到了台达电机的控制代码,说明有现成的示例可用,特别是遵循DS402标准的电机。引用[2]和[3]则介绍了IGH库的结构、函数以及支持松下和安川伺服电机的示例代码。用户的需求是了解方法和示例代码,所以需要结合这些信息来构建回答。 首先,我应该概述EtherCATIGH的基本概念,然后分步骤说明实现流程。步骤可能包括环境搭建、配置网络、初始化主站、配置从站、启动主站、控制电机、关闭主站。每个步骤需要具体说明,并引用相关的引用内容。 接下来,示例代码部分需要参考引用中的项目地址,比如引用[1]的台达电机代码和引用[3]的松下、安川示例。可能需要给出代码结构,并解释关键部分,比如初始化、PDO映射、状态机切换等。 然后,注意事项部分需要强调实时性、从站配置、状态监测和错误处理,这些都是实际应用中容易遇到的问题,引用[2]提到需要深入了解IGH的结构体和函数,这应该在注意事项中提及。 最后,生成相关问题,帮助用户进一步学习,比如IGH库的实时性优化、DS402标准的内容、不同品牌电机的配置差异等,这些问题可以从引用内容中提取。 需要确保回答结构清晰,使用中文,并正确应用引用标识。同时,检查LaTeX格式是否符合要求,行内公式用$...$,独立公式用$$...$$,但根据用户的问题,可能数学公式较少,不过如果有的话要正确应用。 最后,验证所有引用是否正确对应,比如引用[1]对应台达代码,引用[2]对应IGH介绍,引用[3]对应松下和安川的示例。确保没有遗漏关键步骤或错误信息,保持回答的真实可靠。</think>### 使用 EtherCATIGH 控制电机运行的方法 #### 一、EtherCATIGH 库基础 EtherCAT 是一种实时工业以太网协议,支持高精度同步控制,适用于工业自动化场景。IGH 库(IGH EtherCAT Master)是开源的 EtherCAT 主站实现,提供对多品牌伺服电机的控制支持,包括台达、松下、安川等[^3]。 #### 二、实现步骤与关键代码 1. **环境搭建** - 安装 IGH EtherCAT Master 库,克隆仓库并编译: ```bash git clone https://gitcode.com/open-source-toolkit/0fe2a cd igh-ethercat ./configure && make ``` 2. **配置 EtherCAT 网络** - 加载 EtherCAT 内核模块: ```bash sudo modprobe ec_master ``` - 指定网卡为 EtherCAT 主站: ```bash sudo ethercatctl start -m eth0 ``` 3. **初始化主站与从站** ```c ec_master_t *master = ecrt_request_master(0); // 请求主站实例 ec_domain_t *domain = ecrt_master_create_domain(master); // 创建数据域 ec_slave_config_t *sc = ecrt_master_slave_config(master, 0, 0); // 配置从站(台达电机) ``` 4. **配置 PDO 映射(以台达电机为例)** ```c ecrt_slave_config_pdos(sc, EC_END, slave_0_syncs); // 同步管理器配置 ecrt_slave_config_dc(sc, 0x0300); // 分布式时钟配置 ``` 5. **启动主站与周期控制** ```c ecrt_master_activate(master); // 激活主站 while (running) { ecrt_master_receive(master); // 接收数据 ecrt_domain_process(domain); // 处理数据域 // 写入目标位置/速度(DS402 标准) EC_WRITE_U32(domain_pd + target_position, 0x7FFFFF); ecrt_domain_queue(domain); // 提交数据 ecrt_master_send(master); // 发送数据 usleep(5000); // 周期控制(如 5ms) } ``` 6. **关闭资源** ```c ecrt_release_master(master); // 释放主站资源 ``` #### 三、示例代码结构(台达电机) 参考仓库[^1]中的核心代码片段: ```c // 初始化 DS402 状态机 ecrt_slave_config_sdo8(sc, 0x6040, 0x00, 0x06); // 切换至 "Ready to switch on" ecrt_slave_config_sdo8(sc, 0x6040, 0x00, 0x07); // 切换至 "Switched on" ecrt_slave_config_sdo8(sc, 0x6040, 0x00, 0x0F); // 进入 "Operation enabled" // 周期性发送控制字 EC_WRITE_U16(domain_pd + control_word, 0x003F); // 启动运动 ``` #### 四、注意事项 1. **实时性要求**:建议使用 Xenomai 或 PREEMPT_RT 实时内核补丁[^2]。 2. **从站配置**:需提前通过 XML 文件(ESI)描述电机参数。 3. **状态监测**:需周期性读取 0x6041(状态字)和 0x6064(位置反馈)。 ### 相关技术文档 - IGH函数手册: - DS402 标准控制流程:[^1] - 松下/安川电机示例:[^3]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值