[etherlab-users] Problem About EL2252
mfyhit
mafeiyang1995 at 163.com
Wed May 16 04:48:07 CEST 2018
Hi, guys
I have written a program about Beckhoff EL2252. But there are some problems during running.
The message is showed below.
/****************************************************************************/
Configuring PDOs...
Activating master...
Starting timer...
Started.
4 slave(s).
AL states: 0x02.
Link is up.
AnaIn: State 0x02.
AnaIn: online.
EtherCAT ERROR 0-1: Slave does not support CoE!
EtherCAT ERROR 0-1: Failed to read number of mapped PDO entries.
Domain1: WC 3.
Domain1: State 2.
AL states: 0x0A.
AnaIn: State 0x08.
AnaIn: operational.
/****************************************************************************/
The program:
#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 "ecrt.h"
#include "slaves.h" //generated with the ethercat cstruct command
#include "EL2252.h"
/****************************************************************************/
// Application parameters
#define FREQUENCY 100
#define PRIORITY 0
// Optional features
#define CONFIGURE_PDOS 1
#define SDO_ACCESS 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_domain_t *domain2 = NULL;
//static ec_domain_state_t domain2_state = {};
static ec_slave_config_t *sc_ana_in = NULL;
static ec_slave_config_state_t sc_ana_in_state = {};
// Timer
static unsigned int sig_alarms = 0;
static unsigned int user_alarms = 0;
/****************************************************************************/
// process data
static uint8_t *domain1_pd = NULL;
//static uint8_t *domain2_pd = NULL;
#define BusCouplerPos 0, 0
#define Slave1Pos 0,1
#define Slave2Pos 0,2
//#define TI_AM3359ICE 0xE000059D, 0x54490001
#define Beckhoff_EL3702 0x00000002, 0x0e763052
#define Beckhoff_EK1100 0x00000002, 0x044c2c52
// Define a struct for each slave to hold values read or written
static El2252 el2252; // frist slave
const static ec_pdo_entry_reg_t domain1_regs[] = {
// Slave 1: EL2252
{Slave1Pos, Beckhoff_EL2252, 0x1d09, 0x81, &el2252.offset_activate, &el2252.bit_pos_activate},
{Slave1Pos, Beckhoff_EL2252, 0x1d09, 0x90, &el2252.offset_start_time, &el2252.bit_pos_start_time},
{Slave1Pos, Beckhoff_EL2252, 0x7000, 0x01, &el2252.offset_out[0], &el2252.bit_pos_out[0]},
{Slave1Pos, Beckhoff_EL2252, 0x7000, 0x02, &el2252.offset_tristate[0], &el2252.bit_pos_tristate[0]},
{Slave1Pos, Beckhoff_EL2252, 0x7010, 0x01, &el2252.offset_out[1], &el2252.bit_pos_out[1]},
{Slave1Pos, Beckhoff_EL2252, 0x7010, 0x02, &el2252.offset_tristate[1], &el2252.bit_pos_tristate[1]},
// Slave 2:EL3702
{}
};
// offsets for PDO entries
//static unsigned int off_dig_out2;
//static unsigned int off_dig_in2;
static unsigned int counter = 0;
static unsigned int blink = 0;
/*****************************************************************************/
#if SDO_ACCESS
static ec_sdo_request_t *sdo;
#endif
/*****************************************************************************/
static 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_ana_in, &s);
if (s.al_state != sc_ana_in_state.al_state)
printf("AnaIn: State 0x%02X.\n", s.al_state);
if (s.online != sc_ana_in_state.online)
printf("AnaIn: %s.\n", s.online ? "online" : "offline");
if (s.operational != sc_ana_in_state.operational)
printf("AnaIn: %soperational.\n",
s.operational ? "" : "Not ");
sc_ana_in_state = s;
}
/*****************************************************************************/
#if SDO_ACCESS
static void read_sdo(void)
{
switch (ecrt_sdo_request_state(sdo)) {
case EC_REQUEST_UNUSED: // request was not used yet
ecrt_sdo_request_read(sdo); // trigger first read
break;
case EC_REQUEST_BUSY:
fprintf(stderr, "Still busy...\n");
break;
case EC_REQUEST_SUCCESS:
fprintf(stderr, "SDO value: 0x%04X\n",
EC_READ_U16(ecrt_sdo_request_data(sdo)));
ecrt_sdo_request_read(sdo); // trigger next read
break;
case EC_REQUEST_ERROR:
fprintf(stderr, "Failed to read SDO!\n");
ecrt_sdo_request_read(sdo); // retry reading
break;
}
}
#endif
/****************************************************************************/
// Do the write for the EL2252: alternately blink the LEDs
static void write_process_data_el2252() {
EC_WRITE_BIT(domain1_pd + el2252.offset_tristate[0], el2252.bit_pos_tristate[0], 0x00);
EC_WRITE_BIT(domain1_pd + el2252.offset_tristate[1], el2252.bit_pos_tristate[1], 0x00);
EC_WRITE_BIT(domain1_pd + el2252.offset_out[0], el2252.bit_pos_out[0], blink ? 0x01 : 0x00);
EC_WRITE_BIT(domain1_pd + el2252.offset_out[1], el2252.bit_pos_out[1], blink ? 0x00 : 0x01);
}
/****************************************************************************/
static void write_process_data() {
write_process_data_el2252();
}
/****************************************************************************/
// ONCE THE MASTER IS ACTIVATED, THE APP IS IN CHARGE OF EXCHANGING DATA THROUGH
// EXPLICIT CALLS TO THE ECRT LIBRARY (DONE IN THE IDLE STATE BY THE MASTER)
static void cyclic_task()
{
int i;
// receive process data
ecrt_master_receive(master); // RECEIVE A FRAME
ecrt_domain_process(domain1); // DETERMINE THE DATAGRAM STATES
// check process data state (optional)
check_domain1_state();
if (counter) {
counter--;
} else { // do this at 1 Hz
counter = FREQUENCY;
// calculate new process data
blink = !blink;
// check for master state (optional)
check_master_state();
// check for islave configuration state(s) (optional)
check_slave_config_states();
#if SDO_ACCESS
read_sdo();
#endif
}
#if 0
printf("Slave1 Out1: state %u value %u\n",
EC_READ_U8(domain1_pd + slave1_out1_status),
EC_READ_U16(domain1_pd + slave1_out1_value));
#endif
// write process data
write_process_data();
// send process data
ecrt_domain_queue(domain1); // MARK THE DOMAIN DATA AS READY FOR EXCHANGE
ecrt_master_send(master); // SEND ALL QUEUED DATAGRAMS
}
/****************************************************************************/
void signal_handler(int signum) {
switch (signum) {
case SIGALRM:
sig_alarms++;
break;
}
}
/****************************************************************************/
// Configures the PDO given the address of the slave's config pointer, syncs (from slaves.h),
// the slave's position and vendor info.
// Returns non-zero on error.
static int configure_pdo(
ec_slave_config_t** config, // output param
ec_sync_info_t* syncs,
uint16_t alias,
uint16_t position,
uint32_t vendor_id,
uint32_t product_code) {
if (!(*config = ecrt_master_slave_config(master, alias, position, vendor_id, product_code))) {
fprintf(stderr, "Failed to get slave configuration.\n");
return -1;
}
if (ecrt_slave_config_pdos(*config, EC_END, syncs)) {
fprintf(stderr, "Failed to configure PDOs.\n");
return -1;
}
return 0;
}
/****************************************************************************/
int main(int argc, char **argv)
{
ec_slave_config_t *sc;
struct sigaction sa;
struct itimerval tv;
// FIRST, REQUEST A MASTER INSTANCE
master = ecrt_request_master(0);
if (!master)
return -1;
// THEN, CREATE A DOMAIN
domain1 = ecrt_master_create_domain(master);
if (!domain1)
return -1;
if (!(sc_ana_in = ecrt_master_slave_config(
master, Slave1Pos, Beckhoff_EL2252))) {
fprintf(stderr, "Failed to get slave configuration.\n");
return -1;
}
printf("Configuring PDOs...\n");
if (configure_pdo(&el2252.config, slave_1_syncs, Slave1Pos, Beckhoff_EL2252)) return -1;
// Create configuration for bus coupler
sc = ecrt_master_slave_config(master, BusCouplerPos, Beckhoff_EK1100);
if (!sc) {
return -1;
}
if (ecrt_domain_reg_pdo_entry_list(domain1, domain1_regs)) {
fprintf(stderr, "PDO entry registration failed!\n");
return -1;
}
// ACTIVATE THE MASTER. DO NOT APPLY ANY CONFIGURATION AFTER THIS, IT WON'T WORK
printf("Activating master...\n");
if (ecrt_master_activate(master))
return -1;
// INITIALIZE THE PROCESS DOMAIN MEMORY (FOR USER-SPACE APPS)
if (!(domain1_pd = ecrt_domain_data(domain1))) {
return -1;
}
#if PRIORITY
pid_t pid = getpid();
if (setpriority(PRIO_PROCESS, pid, -19))
fprintf(stderr, "Warning: Failed to set priority: %s\n", strerror(errno));
#endif
sa.sa_handler = signal_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
if (sigaction(SIGALRM, &sa, 0)) {
fprintf(stderr, "Failed to install signal handler!\n");
return -1;
}
printf("Starting timer...\n");
tv.it_interval.tv_sec = 0;
tv.it_interval.tv_usec = 1000000 / FREQUENCY;
tv.it_value.tv_sec = 0;
tv.it_value.tv_usec = 1000;
if (setitimer(ITIMER_REAL, &tv, NULL)) {
fprintf(stderr, "Failed to start timer: %s\n", strerror(errno));
return 1;
}
printf("Started.\n");
while (1) {
pause();
#if 0
struct timeval t;
gettimeofday(&t, NULL);
printf("%u.%06u\n", t.tv_sec, t.tv_usec);
#endif
while (sig_alarms != user_alarms) {
cyclic_task();
user_alarms++;
}
}
return 0;
}
/****************************************************************************/
Can anyone gives me some suggestions?
Best Regards,
Yang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.etherlab.org/pipermail/etherlab-users/attachments/20180516/553fb8dc/attachment-0002.htm>
More information about the Etherlab-users
mailing list