00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
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
00056 if (ec_init_redundant(ifname, ifname2))
00057 {
00058 printf("ec_init on %s succeeded.\n",ifname);
00059
00060 if ( ec_config(FALSE, &IOmap) > 0 )
00061 {
00062 printf("%d slaves found and configured.\n",ec_slavecount);
00063
00064 ec_statecheck(0, EC_STATE_SAFE_OP, EC_TIMEOUTSTATE);
00065
00066
00067 ec_configdc();
00068
00069
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
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
00087 ec_writestate(0);
00088
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
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
00125 ec_writestate(0);
00126 }
00127 else
00128 {
00129 printf("No slaves found!\n");
00130 }
00131 printf("End redundant test, close socket\n");
00132
00133 ec_close();
00134 }
00135 else
00136 {
00137 printf("No socket connection on %s\nExcecute as root\n",ifname);
00138 }
00139 }
00140
00141
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
00159 void ec_sync(int64 reftime, int64 cycletime , int64 *offsettime)
00160 {
00161 static int64 integral = 0;
00162 int64 delta;
00163
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
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
00184 ts.tv_sec = tp.tv_sec;
00185 ht = (tp.tv_usec / 1000) + 1;
00186 ts.tv_nsec = ht * 1000000;
00187 cycletime = *(int*)ptr * 1000;
00188 toff = 0;
00189 dorun = 0;
00190 while(1)
00191 {
00192
00193 add_timespec(&ts, cycletime + toff);
00194
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
00206 if( digout ) *digout = (uint8) ((dorun / 16) & 0xff);
00207
00208 if (ec_slave[0].hasdc)
00209 {
00210
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
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
00242 iret1 = pthread_create( &thread1, NULL, (void *) &ecatthread, (void*) &ctime);
00243 memset(¶m, 0, sizeof(param));
00244
00245 param.sched_priority = 40;
00246 iret1 = pthread_setschedparam(thread1, policy, ¶m);
00247
00248
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 }