Beispiel #1
0
/* RT EtherCAT thread */
OSAL_THREAD_FUNC_RT ecatthread(void *ptr)
{
   struct timespec   ts, tleft;
   int ht;
   int64 cycletime;

   clock_gettime(CLOCK_MONOTONIC, &ts);
   ht = (ts.tv_nsec / 1000000) + 1; /* round to nearest ms */
   ts.tv_nsec = ht * 1000000;
   cycletime = *(int*)ptr * 1000; /* cycletime in ns */
   toff = 0;
   dorun = 0;
   ec_send_processdata();
   while(1)
   {
      /* calculate next cycle start */
      add_timespec(&ts, cycletime + toff);
      /* wait to cycle start */
      clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts, &tleft);
      if (dorun>0)
      {
         wkc = ec_receive_processdata(EC_TIMEOUTRET);

         dorun++;
         /* if we have some digital output, cycle */
         if( digout ) *digout = (uint8) ((dorun / 16) & 0xff);

         if (ec_slave[0].hasdc)
         {
            /* calulate toff to get linux time and DC synced */
            ec_sync(ec_DCtime, cycletime, &toff);
         }
         ec_send_processdata();
      }
   }
}
Beispiel #2
0
/* RT EtherCAT thread */
void ecatthread( void *ptr )
{
	struct timespec   ts;
	struct timeval    tp;
	int rc;
	int ht;
	int i;
	int pcounter = 0;
	int64 cycletime;
	
    rc = pthread_mutex_lock(&mutex);
	rc =  gettimeofday(&tp, NULL);

    /* Convert from timeval to timespec */
    ts.tv_sec  = tp.tv_sec;
	ht = (tp.tv_usec / 1000) + 1; /* round to nearest ms */
    ts.tv_nsec = ht * 1000000;
	cycletime = *(int*)ptr * 1000; /* cycletime in ns */
	toff = 0;
	dorun = 0;
	while(1)
	{	
		/* calculate next cycle start */
		add_timespec(&ts, cycletime + toff);
		/* wait to cycle start */
		rc = pthread_cond_timedwait(&cond, &mutex, &ts);
		if (dorun>0)
		{
			rc =  gettimeofday(&tp, NULL);

			ec_send_processdata();

			ec_receive_processdata(EC_TIMEOUTRET);

			cyclecount++;

			
			if((in_EBOX->counter != pcounter) && (streampos < (MAXSTREAM - 1)))
			{
				// check if we have timing problems in master
				// if so, overwrite stream data so it shows up clearly in plots.
				if(in_EBOX->counter > (pcounter + 1))
					for(i = 0 ; i < 50 ; i++)
					{
						stream1[streampos]   = 20000;
						stream2[streampos++] = -20000;
					}
				else
					for(i = 0 ; i < 50 ; i++)
					{
						stream1[streampos]   = in_EBOX->stream[i * 2];
						stream2[streampos++] = in_EBOX->stream[(i * 2) + 1];
					}
				pcounter = in_EBOX->counter;
			}
						    
			/* calulate toff to get linux time and DC synced */
			ec_sync(ec_DCtime, cycletime, &toff);
		}	
	}	 
}