examples/user/main.c
branchstable-1.5
changeset 2549 933a1b36b05f
parent 2425 6a6dec6fc806
child 2555 18c226b66533
equal deleted inserted replaced
2526:2eff7c993a63 2549:933a1b36b05f
     1 /*****************************************************************************
     1 ../../../ccat/unittest/main.c
     2  *
       
     3  *  $Id$
       
     4  *
       
     5  *  Copyright (C) 2007-2009  Florian Pose, Ingenieurgemeinschaft IgH
       
     6  *
       
     7  *  This file is part of the IgH EtherCAT Master.
       
     8  *
       
     9  *  The IgH EtherCAT Master is free software; you can redistribute it and/or
       
    10  *  modify it under the terms of the GNU General Public License version 2, as
       
    11  *  published by the Free Software Foundation.
       
    12  *
       
    13  *  The IgH EtherCAT Master is distributed in the hope that it will be useful,
       
    14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
       
    16  *  Public License for more details.
       
    17  *
       
    18  *  You should have received a copy of the GNU General Public License along
       
    19  *  with the IgH EtherCAT Master; if not, write to the Free Software
       
    20  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
       
    21  *
       
    22  *  ---
       
    23  *
       
    24  *  The license mentioned above concerns the source code only. Using the
       
    25  *  EtherCAT technology and brand is only permitted in compliance with the
       
    26  *  industrial property and similar rights of Beckhoff Automation GmbH.
       
    27  *
       
    28  ****************************************************************************/
       
    29 
       
    30 #include <errno.h>
       
    31 #include <signal.h>
       
    32 #include <stdio.h>
       
    33 #include <string.h>
       
    34 #include <sys/resource.h>
       
    35 #include <sys/time.h>
       
    36 #include <sys/types.h>
       
    37 #include <unistd.h>
       
    38 
       
    39 /****************************************************************************/
       
    40 
       
    41 #include "ecrt.h"
       
    42 
       
    43 /****************************************************************************/
       
    44 
       
    45 // Application parameters
       
    46 #define FREQUENCY 100
       
    47 #define PRIORITY 1
       
    48 
       
    49 // Optional features
       
    50 #define CONFIGURE_PDOS  1
       
    51 #define SDO_ACCESS      0
       
    52 
       
    53 /****************************************************************************/
       
    54 
       
    55 // EtherCAT
       
    56 static ec_master_t *master = NULL;
       
    57 static ec_master_state_t master_state = {};
       
    58 
       
    59 static ec_domain_t *domain1 = NULL;
       
    60 static ec_domain_state_t domain1_state = {};
       
    61 
       
    62 static ec_slave_config_t *sc_ana_in = NULL;
       
    63 static ec_slave_config_state_t sc_ana_in_state = {};
       
    64 
       
    65 // Timer
       
    66 static unsigned int sig_alarms = 0;
       
    67 static unsigned int user_alarms = 0;
       
    68 
       
    69 /****************************************************************************/
       
    70 
       
    71 // process data
       
    72 static uint8_t *domain1_pd = NULL;
       
    73 
       
    74 #define BusCouplerPos  0, 0
       
    75 #define DigOutSlavePos 0, 2
       
    76 #define AnaInSlavePos  0, 3
       
    77 #define AnaOutSlavePos 0, 4
       
    78 
       
    79 #define Beckhoff_EK1100 0x00000002, 0x044c2c52
       
    80 #define Beckhoff_EL2004 0x00000002, 0x07d43052
       
    81 #define Beckhoff_EL2032 0x00000002, 0x07f03052
       
    82 #define Beckhoff_EL3152 0x00000002, 0x0c503052
       
    83 #define Beckhoff_EL3102 0x00000002, 0x0c1e3052
       
    84 #define Beckhoff_EL4102 0x00000002, 0x10063052
       
    85 
       
    86 // offsets for PDO entries
       
    87 static unsigned int off_ana_in_status;
       
    88 static unsigned int off_ana_in_value;
       
    89 static unsigned int off_ana_out;
       
    90 static unsigned int off_dig_out;
       
    91 
       
    92 const static ec_pdo_entry_reg_t domain1_regs[] = {
       
    93     {AnaInSlavePos,  Beckhoff_EL3102, 0x3101, 1, &off_ana_in_status},
       
    94     {AnaInSlavePos,  Beckhoff_EL3102, 0x3101, 2, &off_ana_in_value},
       
    95     {AnaOutSlavePos, Beckhoff_EL4102, 0x3001, 1, &off_ana_out},
       
    96     {DigOutSlavePos, Beckhoff_EL2032, 0x3001, 1, &off_dig_out},
       
    97     {}
       
    98 };
       
    99 
       
   100 static unsigned int counter = 0;
       
   101 static unsigned int blink = 0;
       
   102 
       
   103 /*****************************************************************************/
       
   104 
       
   105 #if CONFIGURE_PDOS
       
   106 
       
   107 // Analog in --------------------------
       
   108 
       
   109 static ec_pdo_entry_info_t el3102_pdo_entries[] = {
       
   110     {0x3101, 1,  8}, // channel 1 status
       
   111     {0x3101, 2, 16}, // channel 1 value
       
   112     {0x3102, 1,  8}, // channel 2 status
       
   113     {0x3102, 2, 16}, // channel 2 value
       
   114     {0x6401, 1, 16}, // channel 1 value (alt.)
       
   115     {0x6401, 2, 16}  // channel 2 value (alt.)
       
   116 };
       
   117 
       
   118 static ec_pdo_info_t el3102_pdos[] = {
       
   119     {0x1A00, 2, el3102_pdo_entries},
       
   120     {0x1A01, 2, el3102_pdo_entries + 2}
       
   121 };
       
   122 
       
   123 static ec_sync_info_t el3102_syncs[] = {
       
   124     {2, EC_DIR_OUTPUT},
       
   125     {3, EC_DIR_INPUT, 2, el3102_pdos},
       
   126     {0xff}
       
   127 };
       
   128 
       
   129 // Analog out -------------------------
       
   130 
       
   131 static ec_pdo_entry_info_t el4102_pdo_entries[] = {
       
   132     {0x3001, 1, 16}, // channel 1 value
       
   133     {0x3002, 1, 16}, // channel 2 value
       
   134 };
       
   135 
       
   136 static ec_pdo_info_t el4102_pdos[] = {
       
   137     {0x1600, 1, el4102_pdo_entries},
       
   138     {0x1601, 1, el4102_pdo_entries + 1}
       
   139 };
       
   140 
       
   141 static ec_sync_info_t el4102_syncs[] = {
       
   142     {2, EC_DIR_OUTPUT, 2, el4102_pdos},
       
   143     {3, EC_DIR_INPUT},
       
   144     {0xff}
       
   145 };
       
   146 
       
   147 // Digital out ------------------------
       
   148 
       
   149 static ec_pdo_entry_info_t el2004_channels[] = {
       
   150     {0x3001, 1, 1}, // Value 1
       
   151     {0x3001, 2, 1}, // Value 2
       
   152     {0x3001, 3, 1}, // Value 3
       
   153     {0x3001, 4, 1}  // Value 4
       
   154 };
       
   155 
       
   156 static ec_pdo_info_t el2004_pdos[] = {
       
   157     {0x1600, 1, &el2004_channels[0]},
       
   158     {0x1601, 1, &el2004_channels[1]},
       
   159     {0x1602, 1, &el2004_channels[2]},
       
   160     {0x1603, 1, &el2004_channels[3]}
       
   161 };
       
   162 
       
   163 static ec_sync_info_t el2004_syncs[] = {
       
   164     {0, EC_DIR_OUTPUT, 4, el2004_pdos},
       
   165     {1, EC_DIR_INPUT},
       
   166     {0xff}
       
   167 };
       
   168 #endif
       
   169 
       
   170 /*****************************************************************************/
       
   171 
       
   172 #if SDO_ACCESS
       
   173 static ec_sdo_request_t *sdo;
       
   174 #endif
       
   175 
       
   176 /*****************************************************************************/
       
   177 
       
   178 void check_domain1_state(void)
       
   179 {
       
   180     ec_domain_state_t ds;
       
   181 
       
   182     ecrt_domain_state(domain1, &ds);
       
   183 
       
   184     if (ds.working_counter != domain1_state.working_counter)
       
   185         printf("Domain1: WC %u.\n", ds.working_counter);
       
   186     if (ds.wc_state != domain1_state.wc_state)
       
   187         printf("Domain1: State %u.\n", ds.wc_state);
       
   188 
       
   189     domain1_state = ds;
       
   190 }
       
   191 
       
   192 /*****************************************************************************/
       
   193 
       
   194 void check_master_state(void)
       
   195 {
       
   196     ec_master_state_t ms;
       
   197 
       
   198     ecrt_master_state(master, &ms);
       
   199 
       
   200     if (ms.slaves_responding != master_state.slaves_responding)
       
   201         printf("%u slave(s).\n", ms.slaves_responding);
       
   202     if (ms.al_states != master_state.al_states)
       
   203         printf("AL states: 0x%02X.\n", ms.al_states);
       
   204     if (ms.link_up != master_state.link_up)
       
   205         printf("Link is %s.\n", ms.link_up ? "up" : "down");
       
   206 
       
   207     master_state = ms;
       
   208 }
       
   209 
       
   210 /*****************************************************************************/
       
   211 
       
   212 void check_slave_config_states(void)
       
   213 {
       
   214     ec_slave_config_state_t s;
       
   215 
       
   216     ecrt_slave_config_state(sc_ana_in, &s);
       
   217 
       
   218     if (s.al_state != sc_ana_in_state.al_state)
       
   219         printf("AnaIn: State 0x%02X.\n", s.al_state);
       
   220     if (s.online != sc_ana_in_state.online)
       
   221         printf("AnaIn: %s.\n", s.online ? "online" : "offline");
       
   222     if (s.operational != sc_ana_in_state.operational)
       
   223         printf("AnaIn: %soperational.\n",
       
   224                 s.operational ? "" : "Not ");
       
   225 
       
   226     sc_ana_in_state = s;
       
   227 }
       
   228 
       
   229 /*****************************************************************************/
       
   230 
       
   231 #if SDO_ACCESS
       
   232 void read_sdo(void)
       
   233 {
       
   234     switch (ecrt_sdo_request_state(sdo)) {
       
   235         case EC_REQUEST_UNUSED: // request was not used yet
       
   236             ecrt_sdo_request_read(sdo); // trigger first read
       
   237             break;
       
   238         case EC_REQUEST_BUSY:
       
   239             fprintf(stderr, "Still busy...\n");
       
   240             break;
       
   241         case EC_REQUEST_SUCCESS:
       
   242             fprintf(stderr, "SDO value: 0x%04X\n",
       
   243                     EC_READ_U16(ecrt_sdo_request_data(sdo)));
       
   244             ecrt_sdo_request_read(sdo); // trigger next read
       
   245             break;
       
   246         case EC_REQUEST_ERROR:
       
   247             fprintf(stderr, "Failed to read SDO!\n");
       
   248             ecrt_sdo_request_read(sdo); // retry reading
       
   249             break;
       
   250     }
       
   251 }
       
   252 #endif
       
   253 
       
   254 /****************************************************************************/
       
   255 
       
   256 void cyclic_task()
       
   257 {
       
   258     // receive process data
       
   259     ecrt_master_receive(master);
       
   260     ecrt_domain_process(domain1);
       
   261 
       
   262     // check process data state (optional)
       
   263     check_domain1_state();
       
   264 
       
   265     if (counter) {
       
   266         counter--;
       
   267     } else { // do this at 1 Hz
       
   268         counter = FREQUENCY;
       
   269 
       
   270         // calculate new process data
       
   271         blink = !blink;
       
   272 
       
   273         // check for master state (optional)
       
   274         check_master_state();
       
   275 
       
   276         // check for islave configuration state(s) (optional)
       
   277         check_slave_config_states();
       
   278 
       
   279 #if SDO_ACCESS
       
   280         // read process data SDO
       
   281         read_sdo();
       
   282 #endif
       
   283 
       
   284     }
       
   285 
       
   286 #if 0
       
   287     // read process data
       
   288     printf("AnaIn: state %u value %u\n",
       
   289             EC_READ_U8(domain1_pd + off_ana_in_status),
       
   290             EC_READ_U16(domain1_pd + off_ana_in_value));
       
   291 #endif
       
   292 
       
   293 #if 1
       
   294     // write process data
       
   295     EC_WRITE_U8(domain1_pd + off_dig_out, blink ? 0x06 : 0x09);
       
   296 #endif
       
   297 
       
   298     // send process data
       
   299     ecrt_domain_queue(domain1);
       
   300     ecrt_master_send(master);
       
   301 }
       
   302 
       
   303 /****************************************************************************/
       
   304 
       
   305 void signal_handler(int signum) {
       
   306     switch (signum) {
       
   307         case SIGALRM:
       
   308             sig_alarms++;
       
   309             break;
       
   310     }
       
   311 }
       
   312 
       
   313 /****************************************************************************/
       
   314 
       
   315 int main(int argc, char **argv)
       
   316 {
       
   317     ec_slave_config_t *sc;
       
   318     struct sigaction sa;
       
   319     struct itimerval tv;
       
   320 
       
   321     master = ecrt_request_master(0);
       
   322     if (!master)
       
   323         return -1;
       
   324 
       
   325     domain1 = ecrt_master_create_domain(master);
       
   326     if (!domain1)
       
   327         return -1;
       
   328 
       
   329     if (!(sc_ana_in = ecrt_master_slave_config(
       
   330                     master, AnaInSlavePos, Beckhoff_EL3102))) {
       
   331         fprintf(stderr, "Failed to get slave configuration.\n");
       
   332         return -1;
       
   333     }
       
   334 
       
   335 #if SDO_ACCESS
       
   336     fprintf(stderr, "Creating SDO requests...\n");
       
   337     if (!(sdo = ecrt_slave_config_create_sdo_request(sc_ana_in, 0x3102, 2, 2))) {
       
   338         fprintf(stderr, "Failed to create SDO request.\n");
       
   339         return -1;
       
   340     }
       
   341     ecrt_sdo_request_timeout(sdo, 500); // ms
       
   342 #endif
       
   343 
       
   344 #if CONFIGURE_PDOS
       
   345     printf("Configuring PDOs...\n");
       
   346     if (ecrt_slave_config_pdos(sc_ana_in, EC_END, el3102_syncs)) {
       
   347         fprintf(stderr, "Failed to configure PDOs.\n");
       
   348         return -1;
       
   349     }
       
   350 
       
   351     if (!(sc = ecrt_master_slave_config(
       
   352                     master, AnaOutSlavePos, Beckhoff_EL4102))) {
       
   353         fprintf(stderr, "Failed to get slave configuration.\n");
       
   354         return -1;
       
   355     }
       
   356 
       
   357     if (ecrt_slave_config_pdos(sc, EC_END, el4102_syncs)) {
       
   358         fprintf(stderr, "Failed to configure PDOs.\n");
       
   359         return -1;
       
   360     }
       
   361 
       
   362     if (!(sc = ecrt_master_slave_config(
       
   363                     master, DigOutSlavePos, Beckhoff_EL2032))) {
       
   364         fprintf(stderr, "Failed to get slave configuration.\n");
       
   365         return -1;
       
   366     }
       
   367 
       
   368     if (ecrt_slave_config_pdos(sc, EC_END, el2004_syncs)) {
       
   369         fprintf(stderr, "Failed to configure PDOs.\n");
       
   370         return -1;
       
   371     }
       
   372 #endif
       
   373 
       
   374     // Create configuration for bus coupler
       
   375     sc = ecrt_master_slave_config(master, BusCouplerPos, Beckhoff_EK1100);
       
   376     if (!sc)
       
   377         return -1;
       
   378 
       
   379     if (ecrt_domain_reg_pdo_entry_list(domain1, domain1_regs)) {
       
   380         fprintf(stderr, "PDO entry registration failed!\n");
       
   381         return -1;
       
   382     }
       
   383 
       
   384     printf("Activating master...\n");
       
   385     if (ecrt_master_activate(master))
       
   386         return -1;
       
   387 
       
   388     if (!(domain1_pd = ecrt_domain_data(domain1))) {
       
   389         return -1;
       
   390     }
       
   391 
       
   392 #if PRIORITY
       
   393     pid_t pid = getpid();
       
   394     if (setpriority(PRIO_PROCESS, pid, -19))
       
   395         fprintf(stderr, "Warning: Failed to set priority: %s\n",
       
   396                 strerror(errno));
       
   397 #endif
       
   398 
       
   399     sa.sa_handler = signal_handler;
       
   400     sigemptyset(&sa.sa_mask);
       
   401     sa.sa_flags = 0;
       
   402     if (sigaction(SIGALRM, &sa, 0)) {
       
   403         fprintf(stderr, "Failed to install signal handler!\n");
       
   404         return -1;
       
   405     }
       
   406 
       
   407     printf("Starting timer...\n");
       
   408     tv.it_interval.tv_sec = 0;
       
   409     tv.it_interval.tv_usec = 1000000 / FREQUENCY;
       
   410     tv.it_value.tv_sec = 0;
       
   411     tv.it_value.tv_usec = 1000;
       
   412     if (setitimer(ITIMER_REAL, &tv, NULL)) {
       
   413         fprintf(stderr, "Failed to start timer: %s\n", strerror(errno));
       
   414         return 1;
       
   415     }
       
   416 
       
   417     printf("Started.\n");
       
   418     while (1) {
       
   419         pause();
       
   420 
       
   421 #if 0
       
   422         struct timeval t;
       
   423         gettimeofday(&t, NULL);
       
   424         printf("%u.%06u\n", t.tv_sec, t.tv_usec);
       
   425 #endif
       
   426 
       
   427         while (sig_alarms != user_alarms) {
       
   428             cyclic_task();
       
   429             user_alarms++;
       
   430         }
       
   431     }
       
   432 
       
   433     return 0;
       
   434 }
       
   435 
       
   436 /****************************************************************************/