red_test.c

Go to the documentation of this file.
00001 /** \file
00002  * \brief Example code for Simple Open EtherCAT master
00003  *
00004  * Usage : red_test [ifname1] [ifname2] [cycletime]
00005  * ifname is NIC interface, f.e. eth0
00006  * cycletime in us, f.e. 500
00007  *
00008  * This is a redundancy test.
00009  *
00010  * (c)Arthur Ketels 2008 
00011  */
00012 
00013 #include <stdio.h>
00014 #include <stdlib.h>
00015 #include <sys/time.h>
00016 #include <unistd.h>
00017 #include <sched.h>
00018 #include <string.h>
00019 #include <sys/time.h>
00020 #include <time.h>
00021 #include <pthread.h>
00022 #include <math.h>
00023 
00024 #include "ethercattype.h"
00025 #include "nicdrv.h"
00026 #include "ethercatbase.h"
00027 #include "ethercatmain.h"
00028 #include "ethercatcoe.h"
00029 #include "ethercatconfig.h"
00030 #include "ethercatdc.h"
00031 
00032 #define NSEC_PER_SEC 1000000000
00033 
00034 struct sched_param schedp;
00035 char IOmap[4096];
00036 pthread_t thread1;
00037 struct timeval tv, t1, t2;
00038 int dorun = 0;
00039 int deltat, tmax = 0;
00040 int64 toff;
00041 int DCdiff;
00042 int os;
00043 uint8 ob;
00044 uint16 ob2;
00045 pthread_cond_t      cond  = PTHREAD_COND_INITIALIZER;
00046 pthread_mutex_t     mutex = PTHREAD_MUTEX_INITIALIZER;
00047 uint8 *digout = 0;
00048 
00049 void redtest(char *ifname, char *ifname2)
00050 {
00051   int cnt, i, j, oloop, iloop;
00052   
00053   printf("Starting Redundant test\n");
00054   
00055   /* initialise SOEM, bind socket to ifname */
00056   if (ec_init_redundant(ifname, ifname2))
00057   { 
00058     printf("ec_init on %s succeeded.\n",ifname);
00059     /* find and auto-config slaves */
00060     if ( ec_config(FALSE, &IOmap) > 0 )
00061     {
00062       printf("%d slaves found and configured.\n",ec_slavecount);
00063       /* wait for all slaves to reach SAFE_OP state */
00064       ec_statecheck(0, EC_STATE_SAFE_OP,  EC_TIMEOUTSTATE);
00065       
00066       /* configure DC options for every DC capable slave found in the list */
00067       ec_configdc();
00068             
00069       /* read indevidual slave state and store in ec_slave[] */
00070       ec_readstate();
00071       for(cnt = 1; cnt <= ec_slavecount ; cnt++)
00072       {
00073         printf("Slave:%d Name:%s Output size:%3dbits Input size:%3dbits State:%2d delay:%d.%d\n",
00074              cnt, ec_slave[cnt].name, ec_slave[cnt].Obits, ec_slave[cnt].Ibits,
00075              ec_slave[cnt].state, (int)ec_slave[cnt].pdelay, ec_slave[cnt].hasdc);
00076         printf("         Out:%8.8x,%4d In:%8.8x,%4d\n",
00077              (int)ec_slave[cnt].outputs, ec_slave[cnt].Obytes, (int)ec_slave[cnt].inputs, ec_slave[cnt].Ibytes);
00078         /* check for EL2004 or EL2008 */
00079         if( !digout && ((ec_slave[cnt].eep_id == 0x07d43052) || (ec_slave[cnt].eep_id == 0x07d83052)))
00080         {
00081           digout = ec_slave[cnt].outputs;
00082         } 
00083       }
00084       printf("Request operational state for all slaves\n");
00085       ec_slave[0].state = EC_STATE_OPERATIONAL;
00086       /* request OP state for all slaves */
00087       ec_writestate(0);
00088       /* wait for all slaves to reach OP state */
00089       ec_statecheck(0, EC_STATE_OPERATIONAL,  EC_TIMEOUTSTATE);
00090       oloop = ec_slave[0].Obytes;
00091       if ((oloop == 0) && (ec_slave[0].Obits > 0)) oloop = 1;
00092       if (oloop > 8) oloop = 8;
00093       iloop = ec_slave[0].Ibytes;
00094       if ((iloop == 0) && (ec_slave[0].Ibits > 0)) iloop = 1;
00095       if (iloop > 8) iloop = 8;
00096       if (ec_slave[0].state == EC_STATE_OPERATIONAL )
00097       {
00098         printf("Operational state reached for all slaves.\n");
00099         dorun = 1;
00100         /* acyclic loop 500 x 20ms = 10s */
00101         for(i = 1; i <= 500; i++)
00102         {
00103           printf("Processdata cycle %5d , DCtime %12lld, O:", dorun, ec_DCtime);
00104           for(j = 0 ; j < oloop; j++)
00105           {
00106             printf(" %2.2x", *(ec_slave[0].outputs + j));
00107           }
00108           printf(" I:");
00109           for(j = 0 ; j < iloop; j++)
00110           {
00111             printf(" %2.2x", *(ec_slave[0].inputs + j));
00112           } 
00113           printf("\n");
00114           usleep(20000);
00115         }
00116         dorun = 0;
00117       }
00118       else
00119       {
00120         printf("Not all slaves reached operational state.\n");
00121       }     
00122       printf("Request safe operational state for all slaves\n");
00123       ec_slave[0].state = EC_STATE_SAFE_OP;
00124       /* request SAFE_OP state for all slaves */
00125       ec_writestate(0);
00126     }
00127     else
00128     {
00129       printf("No slaves found!\n");
00130     }
00131     printf("End redundant test, close socket\n");
00132     /* stop SOEM, close socket */
00133     ec_close();
00134   }
00135   else
00136   {
00137     printf("No socket connection on %s\nExcecute as root\n",ifname);
00138   } 
00139 } 
00140 
00141 /* add ns to timespec */
00142 void add_timespec(struct timespec *ts, int64 addtime)
00143 {
00144   int64 sec, nsec;
00145   
00146   nsec = addtime % NSEC_PER_SEC;
00147   sec = (addtime - nsec) / NSEC_PER_SEC;
00148   ts->tv_sec += sec;
00149   ts->tv_nsec += nsec;
00150   if ( ts->tv_nsec > NSEC_PER_SEC ) 
00151   { 
00152     nsec = ts->tv_nsec % NSEC_PER_SEC;
00153     ts->tv_sec += (ts->tv_nsec - nsec) / NSEC_PER_SEC;
00154     ts->tv_nsec = nsec;
00155   } 
00156 } 
00157 
00158 /* PI calculation to get linux time synced to DC time */
00159 void ec_sync(int64 reftime, int64 cycletime , int64 *offsettime)
00160 {
00161   static int64 integral = 0;
00162   int64 delta;
00163   /* set linux sync point 50us later than DC sync, just as example */
00164   delta = (reftime - 50000) % cycletime;
00165   if(delta> (cycletime / 2)) { delta= delta - cycletime; }
00166   if(delta>0){ integral++; }
00167   if(delta<0){ integral--; }
00168   *offsettime = -(delta / 100) - (integral / 20);
00169 } 
00170 
00171 /* RT EtherCAT thread */
00172 void ecatthread( void *ptr )
00173 {
00174   struct timespec   ts;
00175   struct timeval    tp;
00176   int rc;
00177   int ht;
00178   int64 cycletime;
00179   
00180     rc = pthread_mutex_lock(&mutex);
00181   rc =  gettimeofday(&tp, NULL);
00182 
00183     /* Convert from timeval to timespec */
00184     ts.tv_sec  = tp.tv_sec;
00185   ht = (tp.tv_usec / 1000) + 1; /* round to nearest ms */
00186     ts.tv_nsec = ht * 1000000;
00187   cycletime = *(int*)ptr * 1000; /* cycletime in ns */
00188   toff = 0;
00189   dorun = 0;
00190   while(1)
00191   { 
00192     /* calculate next cycle start */
00193     add_timespec(&ts, cycletime + toff);
00194     /* wait to cycle start */
00195     rc = pthread_cond_timedwait(&cond, &mutex, &ts);
00196     if (dorun>0)
00197     {
00198       rc =  gettimeofday(&tp, NULL);
00199 
00200       ec_send_processdata();
00201       
00202       ec_receive_processdata(EC_TIMEOUTRET);
00203       
00204       dorun++;
00205       /* if we have some digital output, cycle */
00206       if( digout ) *digout = (uint8) ((dorun / 16) & 0xff); 
00207       
00208       if (ec_slave[0].hasdc)
00209       { 
00210         /* calulate toff to get linux time and DC synced */
00211         ec_sync(ec_DCtime, cycletime, &toff);
00212       } 
00213     } 
00214   }  
00215 }
00216 
00217 int main(int argc, char *argv[])
00218 {
00219   int iret1;
00220     int ctime;
00221   struct sched_param    param;
00222   int                   policy = SCHED_OTHER;
00223   
00224   printf("SOEM (Simple Open EtherCAT Master)\nRedundancy test\n");
00225   
00226   memset(&schedp, 0, sizeof(schedp));
00227   /* do not set priority above 49, otherwise sockets are starved */
00228   schedp.sched_priority = 30;
00229   sched_setscheduler(0, SCHED_FIFO, &schedp);
00230 
00231   do
00232   {
00233     usleep(1000);
00234   }
00235   while (dorun);
00236   
00237   if (argc > 3)
00238   {   
00239     dorun = 1;
00240     ctime = atoi(argv[3]);
00241     /* create RT thread */
00242     iret1 = pthread_create( &thread1, NULL, (void *) &ecatthread, (void*) &ctime);  
00243     memset(&param, 0, sizeof(param));
00244     /* give it higher priority */
00245           param.sched_priority = 40;
00246           iret1 = pthread_setschedparam(thread1, policy, &param);
00247 
00248     /* start acyclic part */
00249     redtest(argv[1],argv[2]);
00250   }
00251   else
00252   {
00253     printf("Usage: red_test ifname1 ifname2 cycletime\nifname = eth0 for example\ncycletime in us\n");
00254   } 
00255   
00256   schedp.sched_priority = 0;
00257   sched_setscheduler(0, SCHED_OTHER, &schedp);
00258 
00259   printf("End program\n");
00260   return (0);
00261 }
Generated by  doxygen 1.6.3