Пример #1
0
int main(int argc, char **argv){
/*
  key_t shmKey;
  int shmid;

  time_t curtime = -1;
  char xtime[40]="\0";
  long int size=4194304; //   = 4 MB // 131072;  //65536;
*/
  long int ii=0;
  long int adc=0;
  pid_t pid;
  int xx=0;

  struct sigaction act;

/*
  Set up LabJack U6
*/
  labjackSetup();

/*
  Shared memory creation and attachment
*/

  shmSetup();

/*  
   Get the process number for interupts when the two programs need to interact
*/
  pid = getpid();
  lnptr->pid = pid;
  printf("pid = %li \n",lnptr->pid);
/*  
   Set up the signal capture routine for when the reading
   program want something changed
*/

  memset (&act,'\0', sizeof(act));
  act.sa_sigaction = &handlerCommand;
  act.sa_flags = SA_SIGINFO;
  if (sigaction(SIGALRM,&act,NULL) < 0){
    perror("sigaction");
    return 1;
  }

/*  
   Read setup file on disk and load into shared memory
*/
  printf("Read conf file...\n");
  readConf();
  printf(" ... Conf file read \n");
/*
  Go out and read RTDs and get everything needed loaded into shared memory
*/

  updateRTD();
  /*
  for (ii=0; ii<20; ii++){
    if (lnptr->ge[ii].onoff == 1){
      lnptr->ge[ii].rtd = readRTD(lnptr->ge[ii].chanRTD);
      lnptr->ge[ii].oflo = readRTD(lnptr->ge[ii].chanOFLO);
      printf("Active det: %li ->  %lf   %lf\n", ii,lnptr->ge[ii].rtd,lnptr->ge[ii].oflo );
    }
  }
  lnptr->tank.rtd = readRTD(lnptr->tank.chanRTD);
  lnptr->tank.pressure = readRTD(lnptr->tank.chanPRES);

  printf("TANK: %lf\n", lnptr->tank.rtd);
  */
/*  
  Setup time of next fill based on current time and configure file
*/
  curtime = time(NULL);
  time0 = curtime;             // record starting time of program
  time1 = time0 + INTERVAL;      // set up next read of RTD...usually every minute

  for (ii=0; ii<20; ii++){
    if (lnptr->ge[ii].onoff == 1){
      lnptr->ge[ii].next = time0 + lnptr->ge[ii].interval;
    }
    else {
      lnptr->ge[ii].next = 0;
    }
  }

/*  
  Setup monitoring loop to look for changes and requests   
*/
  setTimer();
  /*
  tout_val.it_interval.tv_sec = 0;
  tout_val.it_interval.tv_usec = 0;

  tout_val.it_value.tv_sec = INTERVAL; // set timer for "INTERVAL (10) seconds 
  tout_val.it_value.tv_usec = 0;

  setitimer(ITIMER_REAL, &tout_val,0);   // start the timer
  */
/*  
  Setup monitoring loop to look for changes/minute and requests   
*/
  lnptr->command = 0;
  while(lnptr->command != -1) {
    curtime = time(NULL);               // check what time it is 
    if (curtime >= time1) {             // read RTDs every INTERVAL (usually ~60 s)
      time1 = time1+INTERVAL;
      updateRTD();
    }
/*
   Check if an RTD is too high
*/
    for (ii=0;ii<20;ii++){                 // run thru each detector possibility
      if (lnptr->ge[ii].onoff == 1) {      // run thru each detector that is ON


	if (lnptr->ge[ii].rtd > lnptr->ge[ii].limit){    // check rtd is within limit
	  printf("SHUT DOWN !\n");
	  strcpy(lnptr->ge[ii].status,"ALARM");
	}
	if (curtime > lnptr->ge[ii].next){               // check rtd is within limit
	  printf("start a fill !\n");
	} 
      }
    }

    //  AFTER THIS LOOP CHECK FOR COMMANDS COMING IN FROM CONTROL PROGRAM

      /*    
    switch (lnptr->command) {
    case 0:
      //      signal(SIGTERM,alarm_wakeup);   // set the Alarm signal capture 
      sleep (1);                      // pause for time
      curtime = time(NULL);
      printf ("curtime = %li %li \n", curtime, (time1-curtime));
      if (curtime >= time1) {
	time1 = time1+INTERVAL;
	updateRTD();
      }
      break;
    case 1:
      printf ("1 selected\n");
	break;
    case 2:
      printf ("2 selected\n");
	break;
    default:
      printf ("default selected\n");
      lnptr->command=-1;
	break;
    }
      */    
    sleep (59);
  }

/*
  Alarm processing
*/

/*  
   
*/

/*
   Print out some of shared memory to show it has been accessed

  ln.ge[0].rtd = 80.2;

  for (ii=0; ii< 20; ii++){
    ln.ge[ii].rtd = ln.ge[0].rtd + (double) ii;
    lnptr->ge[ii].rtd = ln.ge[ii].rtd;
    printf ("rtd[%li] = %.2lf = %.2lf\n", ii, ln.ge[ii].rtd,lnptr->ge[ii].rtd);

    sleep (1);
  }
*/
  shmdt(lnptr);                     // detach from shared memory segment
  printf("detached from SHM\n");

  shmctl(shmid, IPC_RMID, NULL);    // remove the shared memory segment hopefully forever
  printf("removed from SHM\n");

  closeUSBConnection(hU6);
  printf("USB closed\n");


  return 0;
}
Пример #2
0
//main
int main (int argc, const char * argv[]) {
    bool retval;
    HANDLE ljHandle;

	printf("Main\n");
    
    if( (ljHandle = openUSBConnection(-1)) == NULL) {    
        printf("Error\n");
        return -1;
    }
    
    retval = ljU6ConfigPorts(ljHandle);
    
    long long i;
    long long t1,t2;
    long state;
    long long savedEl[STATSN+1];
    long long tStart;
    int savedN=0;
    double meanEl;
    int iM;
    int r;
    double maxEl;
    

    //eDO(ljHandle, LJU6_REWARD_FIO, 0);
    tStart = timeUS();
    for (i=0; true; i++) {
        t1 = timeUS();
        
        // DI
        r = eDI(ljHandle, 0, &state);
        if (r < 0) {
            printf("eDI call failed. Exiting");
            return r;
        }
        
        // // DO high
        //usleep(1000);
        //r = eDO(ljHandle, 1, 1);
        //if (r < 0) {
        //    printf("eDO high call failed. Exiting");
        //    return r;
        //}
        
        //// DO low
        //usleep(2000);
        //r = eDO(ljHandle, 1, 0);
        //if (r < 0) {
        //    printf("eDO low call failed. Exiting");
        //    return r;
        //}

        t2 = timeUS();
        if (i % 100 == 0) {
            printf("Elapsed %lld us, iter %10lld\n", t2-t1, i);
            fflush(stdout);
        }

        savedEl[savedN] = t2-t1;
        savedN++;
        
        // stats, every STATSN trials
        if (i % STATSN == 0 && i>0) {  // not the first time
            meanEl = 0;
            maxEl = 0;
            for (iM=0; iM<STATSN; iM++) {
                meanEl = meanEl + savedEl[iM];
                if (savedEl[iM] > maxEl) {
                    maxEl = savedEl[iM];
                }
            }
            meanEl = meanEl/STATSN;
            savedN = 0;
            
            printf("** Mean %8.1f   max %8.1f  totalElapsedUs %lld\n", meanEl, maxEl, timeUS()-tStart);
        }
            
        //while (timeUS() - t1 < 1000) {
        //    usleep(1000);
        //}
    }
    
    closeUSBConnection(ljHandle);
    return 0;
}