ubuntu14.04、xenomai3.1、ethercat igh构建主站(3)

这篇主要写了一个小例子的运行。我把完整的工程项目上传到了CSDN上https://download.csdn.net/download/cln512/12560600。
在这里插入图片描述
分享两个ethercat igh masterhttps://download.csdn.net/download/cln512/12560585
在这里插入图片描述
采用倍福的EL2008,运行效果如下图所示。

Makefile

之前有人问我怎么编译?我写了一个Makefile,在工程目录输入make就可以了。代码如下:

CC       = x86_64-linux-gnu-gcc
INCPATH  = -I./igh-output/include -I./xenomai-Output/include -I./xenomai-Output/include/posix 
#INCPATH  = -I./igh-output/include -I/usr/xenomai/include/cobalt -I/usr/xenomai/include -I/usr/xenomai/include/alchemy -I/usr/xenomai/include/trank
LIBS     = -L./igh-output/lib -lethercat_rtdm -L./xenomai-Output/lib -lrtdm -lpthread_rt -lnative  -lxenomai -lpthread -lrt -lm
#LIBS     = -L./igh-output/lib -lethercat_rtdm -L/usr/xenomai/lib -lcobalt -lmodechk -lpthread -lrt -lalchemy -lcopperplate
LFLAGS   = -Wl,@./xenomai-Output/lib/posix.wrappers 
#LFLAGS   = -Wl,@/usr/xenomai/lib/cobalt.wrappers -Wl,@/usr/xenomai/lib/modechk.wrappers
DEFINES  = -D_GNU_SOURCE -D_REENTRANT -D__XENO__ 
#DEFINES  = -D_GNU_SOURCE -D_REENTRANT -D__COBALT__ 

CFLAGS   = $(DEFINES)
TARGET        = xenomai
####### Output directory
OBJECTS_DIR   = ./
####### Files
SOURCES       = main.c  \
OBJECTS       = main.o  \	
####### Build rules
$(TARGET):  $(OBJECTS)  
	$(CC) $(LFLAGS) -o $(TARGET) $(OBJECTS)  $(LIBS)
####### Compile
main.o: main.c  
	$(CC) $(INCPATH) $(CFLAGS) -o main.o -c main.c
clean:
	rm  xenomai  *.o   

这里做一下说明,xenomai2.6.5和xenomai3.1版本的Makefile还是有细微的区别的。我这里采用了2.6.5版本的,把3.1版本的给注释掉了。如果想切换成3.1版本,则把2.6.5版本的注释掉就行了。

源代码

源码我针对官方提供的example进行了修改。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#include "ecrt.h"

RT_TASK my_task;

static int run = 1;

/****************************************************************************/

// 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 uint8_t *domain1_pd = NULL;

static ec_slave_config_t *sc_dig_out_01 = NULL;
static ec_slave_config_t *sc_dig_out_02 = NULL;
static ec_slave_config_t *sc_dig_out_03 = NULL;

/****************************************************************************/

// process data

#define BusCoupler01_Pos  0, 0
#define DigOutSlave01_Pos 0, 1
#define DigOutSlave02_Pos 0, 2
#define DigOutSlave03_Pos 0, 3

#define Beckhoff_EK1100 0x00000002, 0x044c2c52
#define Beckhoff_EL2008 0x00000002, 0x07d83052

// offsets for PDO entries
static unsigned int off_dig_out1 = 0;
static unsigned int off_dig_out2 = 0;
static unsigned int off_dig_out3 = 0;
// process data

const static ec_pdo_entry_reg_t domain1_regs[] = {
   {DigOutSlave01_Pos, Beckhoff_EL2008, 0x7000, 0x01, &off_dig_out1, NULL},
   {DigOutSlave02_Pos, Beckhoff_EL2008, 0x7000, 0x01, &off_dig_out2, NULL},
   {DigOutSlave03_Pos, Beckhoff_EL2008, 0x7000, 0x01, &off_dig_out3, NULL},
   {}
};

/****************************************************************************/

/* Master 0, Slave 1, "EL2008"
 * Vendor ID:       0x00000002
 * Product code:    0x07d83052
 * Revision number: 0x00100000
 */

ec_pdo_entry_info_t slave_1_pdo_entries[] = {
    {0x7000, 0x01, 1}, /* Output */
    {0x7010, 0x01, 1}, /* Output */
    {0x7020, 0x01, 1}, /* Output */
    {0x7030, 0x01, 1}, /* Output */
    {0x7040, 0x01, 1}, /* Output */
    {0x7050, 0x01, 1}, /* Output */
    {0x7060, 0x01, 1}, /* Output */
    {0x7070, 0x01, 1}, /* Output */
};

ec_pdo_info_t slave_1_pdos[] = {
    {0x1600, 1, slave_1_pdo_entries + 0}, /* Channel 1 */
    {0x1601, 1, slave_1_pdo_entries + 1}, /* Channel 2 */
    {0x1602, 1, slave_1_pdo_entries + 2}, /* Channel 3 */
    {0x1603, 1, slave_1_pdo_entries + 3}, /* Channel 4 */
    {0x1604, 1, slave_1_pdo_entries + 4}, /* Channel 5 */
    {0x1605, 1, slave_1_pdo_entries + 5}, /* Channel 6 */
    {0x1606, 1, slave_1_pdo_entries + 6}, /* Channel 7 */
    {0x1607, 1, slave_1_pdo_entries + 7}, /* Channel 8 */
};

ec_sync_info_t slave_1_syncs[] = {
    {0, EC_DIR_OUTPUT, 8, slave_1_pdos + 0, EC_WD_ENABLE},
    {0xff}
};
/*****************************************************************************
 * Realtime task
 ****************************************************************************/

void rt_check_domain_state(void)
{
    ec_domain_state_t ds = {};

	ecrt_domain_state(domain1, &ds);

    if (ds.working_counter != domain1_state.working_counter) {
        rt_printf("Domain1: WC %u.\n", ds.working_counter);
    }

    if (ds.wc_state != domain1_state.wc_state) {
        rt_printf("Domain1: State %u.\n", ds.wc_state);
    }

    domain1_state = ds;
}

/****************************************************************************/

void rt_check_master_state(void)
{
    ec_master_state_t ms;

	ecrt_master_state(master, &ms);

    if (ms.slaves_responding != master_state.slaves_responding) {
        rt_printf("%u slave(s).\n", ms.slaves_responding);
    }

    if (ms.al_states != master_state.al_states) {
        rt_printf("AL states: 0x%02X.\n", ms.al_states);
    }

    if (ms.link_up != master_state.link_up) {
        rt_printf("Link is %s.\n", ms.link_up ? "up" : "down");
    }

    master_state = ms;
}

/****************************************************************************/

void my_task_proc(void *arg)
{
	int cycle_counter = 0;
    unsigned int blink = 0;

	rt_task_set_periodic(NULL, TM_NOW, 1000000); // ns

	while (run) {
		rt_task_wait_period(NULL);

		cycle_counter++;

		// receive EtherCAT frames
		ecrt_master_receive(master);
		ecrt_domain_process(domain1);

		rt_check_domain_state();

		if (!(cycle_counter % 1000)) {
			rt_check_master_state();
		}

		if (!(cycle_counter % 200)) {
			blink = !blink;
		}

		EC_WRITE_U8(domain1_pd + off_dig_out1, blink ? 0x00 : 0xFF);
        EC_WRITE_U8(domain1_pd + off_dig_out2, blink ? 0xFF : 0x00);
        EC_WRITE_U8(domain1_pd + off_dig_out3, blink ? 0x00 : 0xFF);
		// send process data
		ecrt_domain_queue(domain1);
		ecrt_master_send(master);
	}
}

/****************************************************************************
 * Signal handler
 ***************************************************************************/

void signal_handler(int sig)
{
    run = 0;
}

/****************************************************************************
 * Main function
 ***************************************************************************/

int main(int argc, char *argv[])
{
    ec_slave_config_t *sc;
    int ret;

    /* Perform auto-init of rt_print buffers if the task doesn't do so */
    rt_print_auto_init(1);

    signal(SIGTERM, signal_handler);
    signal(SIGINT, signal_handler);

    mlockall(MCL_CURRENT | MCL_FUTURE);

    printf("Requesting master...\n");
    master = ecrt_request_master(0);
    if (!master) {
        return -1;
    }

    domain1 = ecrt_master_create_domain(master);
    if (!domain1) {
        return -1;
    }

    printf("Creating slave configurations...\n");

    // Create configuration for bus coupler
    sc = ecrt_master_slave_config(master, BusCoupler01_Pos, Beckhoff_EK1100);
    if (!sc) {
        return -1;
    }

    //1
    sc_dig_out_01 =
        ecrt_master_slave_config(master, DigOutSlave01_Pos, Beckhoff_EL2008);
    if (!sc_dig_out_01) {
        fprintf(stderr, "Failed to get slave configuration.\n");
        return -1;
    }

    if (ecrt_slave_config_pdos(sc_dig_out_01, EC_END, slave_1_syncs)) {
        fprintf(stderr, "Failed to configure PDOs.\n");
        return -1;
    }
    //2
    sc_dig_out_02 =
        ecrt_master_slave_config(master, DigOutSlave02_Pos, Beckhoff_EL2008);
    if (!sc_dig_out_02) {
        fprintf(stderr, "Failed to get slave configuration.\n");
        return -1;
    }

    if (ecrt_slave_config_pdos(sc_dig_out_02, EC_END, slave_1_syncs)) {
        fprintf(stderr, "Failed to configure PDOs.\n");
        return -1;
    }
    //3
    sc_dig_out_03 =
        ecrt_master_slave_config(master, DigOutSlave03_Pos, Beckhoff_EL2008);
    if (!sc_dig_out_03) {
        fprintf(stderr, "Failed to get slave configuration.\n");
        return -1;
    }

    if (ecrt_slave_config_pdos(sc_dig_out_03, EC_END, slave_1_syncs)) {
        fprintf(stderr, "Failed to configure PDOs.\n");
        return -1;
    }
    //

    if (ecrt_domain_reg_pdo_entry_list(domain1, domain1_regs)) {
        fprintf(stderr, "PDO entry registration failed!\n");
        return -1;
    }

    printf("Activating master...\n");
    if (ecrt_master_activate(master)) {
        return -1;
    }

    if (!(domain1_pd = ecrt_domain_data(domain1))) {
        fprintf(stderr, "Failed to get domain data pointer.\n");
        return -1;
    }

    ret = rt_task_create(&my_task, "my_task", 0, 80, T_FPU);
    if (ret < 0) {
        fprintf(stderr, "Failed to create task: %s\n", strerror(-ret));
        return -1;
    }

    printf("Starting my_task...\n");
    ret = rt_task_start(&my_task, &my_task_proc, NULL);
    if (ret < 0) {
        fprintf(stderr, "Failed to start task: %s\n", strerror(-ret));
        return -1;
    }

	while (run) {
		sched_yield();
	}

    printf("Deleting realtime task...\n");
    rt_task_delete(&my_task);

    printf("End of Program\n");
    ecrt_release_master(master);

    return 0;
}

/****************************************************************************/

你可能感兴趣的:(ethercat)