win32/red_test/red_test.c
Go to the documentation of this file.
1 
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <sys/time.h>
16 #include <unistd.h>
17 #include <sched.h>
18 #include <string.h>
19 #include <sys/time.h>
20 #include <time.h>
21 #include <pthread.h>
22 #include <math.h>
23 
24 #include "ethercattype.h"
25 #include "nicdrv.h"
26 #include "ethercatbase.h"
27 #include "ethercatmain.h"
28 #include "ethercatcoe.h"
29 #include "ethercatconfig.h"
30 #include "ethercatdc.h"
31 
32 #define NSEC_PER_SEC 1000000000
33 
34 struct sched_param schedp;
35 char IOmap[4096];
36 pthread_t thread1;
37 struct timeval tv, t1, t2;
38 int dorun = 0;
39 int deltat, tmax = 0;
41 int DCdiff;
42 int os;
45 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
46 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
49 
50 void redtest(char *ifname, char *ifname2)
51 {
52  int cnt, i, j, oloop, iloop;
53 
54  printf("Starting Redundant test\n");
55 
56  /* initialise SOEM, bind socket to ifname */
57  if (ec_init_redundant(ifname, ifname2))
58  {
59  printf("ec_init on %s succeeded.\n",ifname);
60  /* find and auto-config slaves */
61  if ( ec_config(FALSE, &IOmap) > 0 )
62  {
63  printf("%d slaves found and configured.\n",ec_slavecount);
64  /* wait for all slaves to reach SAFE_OP state */
66 
67  /* configure DC options for every DC capable slave found in the list */
68  ec_configdc();
69 
70  /* read indevidual slave state and store in ec_slave[] */
71  ec_readstate();
72  for(cnt = 1; cnt <= ec_slavecount ; cnt++)
73  {
74  printf("Slave:%d Name:%s Output size:%3dbits Input size:%3dbits State:%2d delay:%d.%d\n",
75  cnt, ec_slave[cnt].name, ec_slave[cnt].Obits, ec_slave[cnt].Ibits,
76  ec_slave[cnt].state, (int)ec_slave[cnt].pdelay, ec_slave[cnt].hasdc);
77  printf(" Out:%8.8x,%4d In:%8.8x,%4d\n",
78  (int)ec_slave[cnt].outputs, ec_slave[cnt].Obytes, (int)ec_slave[cnt].inputs, ec_slave[cnt].Ibytes);
79  /* check for EL2004 or EL2008 */
80  if( !digout && ((ec_slave[cnt].eep_id == 0x07d43052) || (ec_slave[cnt].eep_id == 0x07d83052)))
81  {
82  digout = ec_slave[cnt].outputs;
83  }
84  }
85  printf("Request operational state for all slaves\n");
87  /* request OP state for all slaves */
88  ec_writestate(0);
89  /* wait for all slaves to reach OP state */
91  oloop = ec_slave[0].Obytes;
92  if ((oloop == 0) && (ec_slave[0].Obits > 0)) oloop = 1;
93  if (oloop > 8) oloop = 8;
94  iloop = ec_slave[0].Ibytes;
95  if ((iloop == 0) && (ec_slave[0].Ibits > 0)) iloop = 1;
96  if (iloop > 8) iloop = 8;
97  if (ec_slave[0].state == EC_STATE_OPERATIONAL )
98  {
99  printf("Operational state reached for all slaves.\n");
100  dorun = 1;
101  /* acyclic loop 5000 x 20ms = 10s */
102  for(i = 1; i <= 5000; i++)
103  {
104  printf("Processdata cycle %5d , Wck %3d, DCtime %12lld, O:", dorun, wcounter , ec_DCtime);
105  for(j = 0 ; j < oloop; j++)
106  {
107  printf(" %2.2x", *(ec_slave[0].outputs + j));
108  }
109  printf(" I:");
110  for(j = 0 ; j < iloop; j++)
111  {
112  printf(" %2.2x", *(ec_slave[0].inputs + j));
113  }
114  printf("\n");
115  usleep(20000);
116  }
117  dorun = 0;
118  }
119  else
120  {
121  printf("Not all slaves reached operational state.\n");
122  }
123  printf("Request safe operational state for all slaves\n");
125  /* request SAFE_OP state for all slaves */
126  ec_writestate(0);
127  }
128  else
129  {
130  printf("No slaves found!\n");
131  }
132  printf("End redundant test, close socket\n");
133  /* stop SOEM, close socket */
134  ec_close();
135  }
136  else
137  {
138  printf("No socket connection on %s\nExcecute as root\n",ifname);
139  }
140 }
141 
142 /* add ns to timespec */
143 void add_timespec(struct timespec *ts, int64 addtime)
144 {
145  int64 sec, nsec;
146 
147  nsec = addtime % NSEC_PER_SEC;
148  sec = (addtime - nsec) / NSEC_PER_SEC;
149  ts->tv_sec += sec;
150  ts->tv_nsec += nsec;
151  if ( ts->tv_nsec > NSEC_PER_SEC )
152  {
153  nsec = ts->tv_nsec % NSEC_PER_SEC;
154  ts->tv_sec += (ts->tv_nsec - nsec) / NSEC_PER_SEC;
155  ts->tv_nsec = nsec;
156  }
157 }
158 
159 /* PI calculation to get linux time synced to DC time */
160 void ec_sync(int64 reftime, int64 cycletime , int64 *offsettime)
161 {
162  static int64 integral = 0;
163  int64 delta;
164  /* set linux sync point 50us later than DC sync, just as example */
165  delta = (reftime - 50000) % cycletime;
166  if(delta> (cycletime / 2)) { delta= delta - cycletime; }
167  if(delta>0){ integral++; }
168  if(delta<0){ integral--; }
169  *offsettime = -(delta / 100) - (integral / 20);
170 }
171 
172 /* RT EtherCAT thread */
173 void ecatthread( void *ptr )
174 {
175  struct timespec ts;
176  struct timeval tp;
177  int rc;
178  int ht;
179  int64 cycletime;
180 
181  rc = pthread_mutex_lock(&mutex);
182  rc = gettimeofday(&tp, NULL);
183 
184  /* Convert from timeval to timespec */
185  ts.tv_sec = tp.tv_sec;
186  ht = (tp.tv_usec / 1000) + 1; /* round to nearest ms */
187  ts.tv_nsec = ht * 1000000;
188  cycletime = *(int*)ptr * 1000; /* cycletime in ns */
189  toff = 0;
190  dorun = 0;
191  while(1)
192  {
193  /* calculate next cycle start */
194  add_timespec(&ts, cycletime + toff);
195  /* wait to cycle start */
196  rc = pthread_cond_timedwait(&cond, &mutex, &ts);
197  if (dorun>0)
198  {
199  rc = gettimeofday(&tp, NULL);
200 
202 
204 
205  dorun++;
206  /* if we have some digital output, cycle */
207  if( digout ) *digout = (uint8) ((dorun / 16) & 0xff);
208 
209  if (ec_slave[0].hasdc)
210  {
211  /* calulate toff to get linux time and DC synced */
212  ec_sync(ec_DCtime, cycletime, &toff);
213  }
214  }
215  }
216 }
217 
218 int main(int argc, char *argv[])
219 {
220  int iret1;
221  int ctime;
222  struct sched_param param;
223  int policy = SCHED_OTHER;
224 
225  printf("SOEM (Simple Open EtherCAT Master)\nRedundancy test\n");
226 
227  memset(&schedp, 0, sizeof(schedp));
228  /* do not set priority above 49, otherwise sockets are starved */
229  schedp.sched_priority = 30;
230  sched_setscheduler(0, SCHED_FIFO, &schedp);
231 
232  do
233  {
234  usleep(1000);
235  }
236  while (dorun);
237 
238  if (argc > 3)
239  {
240  dorun = 1;
241  ctime = atoi(argv[3]);
242  /* create RT thread */
243  iret1 = pthread_create( &thread1, NULL, (void *) &ecatthread, (void*) &ctime);
244  memset(&param, 0, sizeof(param));
245  /* give it higher priority */
246  param.sched_priority = 40;
247  iret1 = pthread_setschedparam(thread1, policy, &param);
248 
249  /* start acyclic part */
250  redtest(argv[1],argv[2]);
251  }
252  else
253  {
254  printf("Usage: red_test ifname1 ifname2 cycletime\nifname = eth0 for example\ncycletime in us\n");
255  }
256 
257  schedp.sched_priority = 0;
258  sched_setscheduler(0, SCHED_OTHER, &schedp);
259 
260  printf("End program\n");
261 
262  return (0);
263 }
pthread_mutex_t mutex
void ec_sync(int64 reftime, int64 cycletime, int64 *offsettime)
int wcounter
uint32 Obytes
Definition: ethercatmain.h:160
Headerfile for ethercatdc.c.
int ec_readstate(void)
int ec_send_processdata(void)
#define EC_TIMEOUTSTATE
Definition: ethercattype.h:102
PACKED_END ec_slavet ec_slave[EC_MAXSLAVE]
Definition: ethercatmain.c:104
uint8_t uint8
Definition: osal.h:33
int ec_init_redundant(char *ifname, char *if2name)
int64 toff
int ec_receive_processdata(int timeout)
uint16_t uint16
Definition: osal.h:34
uint8 * outputs
Definition: ethercatmain.h:162
int64 ec_DCtime
Definition: ethercatmain.c:132
General typedefs and defines for EtherCAT.
Headerfile for ethercatcoe.c.
int main(int argc, char *argv[])
uint16 ob2
pthread_t thread1
#define NSEC_PER_SEC
int64_t int64
Definition: osal.h:36
#define FALSE
Definition: osal.h:29
uint8 ob
uint8 * digout
#define EC_TIMEOUTRET
Definition: ethercattype.h:90
char IOmap[4096]
struct sched_param schedp
int ec_writestate(uint16 slave)
int64 integral
Headerfile for ethercatbase.c.
int ec_config(uint8 usetable, void *pIOmap)
struct timeval tv t1 t2
boolean ec_configdc(void)
Definition: ethercatdc.c:456
pthread_cond_t cond
Headerfile for ethercatmain.c.
void add_timespec(struct timespec *ts, int64 addtime)
void ecatthread(void *ptr)
uint32 Ibytes
Definition: ethercatmain.h:168
int gettimeofday(struct timeval *tp, void *tzp)
Definition: rtk/osal.c:65
uint16 state
Definition: ethercatmain.h:140
void ec_close(void)
uint16 ec_statecheck(uint16 slave, uint16 reqstate, int timeout)
int ec_slavecount
Definition: ethercatmain.c:106
Headerfile for ethercatconfig.c.
void redtest(char *ifname, char *ifname2)


soem
Author(s): Arthur Ketels and M.J.G. van den Molengraft
autogenerated on Sat Jun 8 2019 18:02:17