Exemple #1
0
int main(int argc, char *argv[]) {
	char *line, word[12], cmdChar, *ip;
	int n;
	pthread_t driverTid;
#if _POSIX_VERSION >= 199506
	struct sched_param param;
#else  /* _POSIX_VERSION >= 199506 */
	pthread_attr_t attr;
#endif /* _POSIX_VERSION >= 199506 */

	if(processPresent("excl_Track")) {
	    printf("Another Track type process is running\n");
	    exit(1);
	}
	setpriority(PRIO_PROCESS, (0), (TRACKUSERPRIO));
	readline_initialize_everything();
	read_history(HIST_FILE);
	tsshm = OpenShm(TSSHMNAME, TSSHMSZ);
	tsshm->azCmd = OFF_CMD;
	tsshm->elCmd = OFF_CMD;
	tsshm->az = tsshm->encAz;
	tsshm->el = tsshm->encEl;
	tsshm->azVel = 0;
	tsshm->elVel = 0;
	tsshm->msecCmd = tsshm->msec;

	/* Start the driver thread */
#if _POSIX_VERSION >= 199506
	if(pthread_create(&driverTid, NULL, driver, (void *)0) < 0) {
	    perror("Failed to create driver");
	    exit(1);
	}
	param.sched_priority = TRACKPRIO;
	pthread_setschedparam(driverTid, SCHED_DEFAULT, &param);
#else  /* _POSIX_VERSION >= 199506 */
	pthread_attr_create(&attr);
	if(pthread_create(&driverTid, attr, driver, (void *)0) < 0) {
	    perror("Failed to create driver");
	    exit(1);
	}
	pthread_setprio(driverTid, TRACKPRIO);
#endif /* _POSIX_VERSION >= 199506 */

	/* This main loop communicates with the user */
	for(;;) {
	    line = readline("trk: ");
	    if(*line) {			/* Readline removes the '\n' */
		add_history(line);

		/* At this point the main part of the program should run.  */
	        cmdChar = *line;
		/* Skip the remainder of the first word */
	        for(ip = 1 + line; ! isspace(*ip); ip++) ;
		switch(cmdChar) {
		case 'p':
		    n = 1;
		    printf("Fault = %d az cmd %d, state %d    "
			    "el cmd %d, state %d\n", tsshm->fault,
			    tsshm->azCmd, tsshm->azState,
			    tsshm->elCmd, tsshm->elState);
		    printf("az %9.4f, azVel %9.4f  curAz %9.4f, encAz %9.4f\n",
			    tsshm->az/MSEC_PER_DEG, tsshm->azVel/MSEC_PER_DEG,
			    (double)tsshm->cmdAz/MSEC_PER_DEG,
			    (double)tsshm->encAz/MSEC_PER_DEG);
		    printf("el %9.4f, elVel %9.4f  curEl %9.4f, encEl %9.4f\n",
			    tsshm->el/MSEC_PER_DEG, tsshm->elVel/MSEC_PER_DEG,
			    (double)tsshm->cmdEl/MSEC_PER_DEG,
			    (double)tsshm->encEl/MSEC_PER_DEG);
		    printf("day %d, msec %d, msecCmd %d   "
			    "msecAccept %d\n", tsshm->day,
			    tsshm->msec, tsshm->msecCmd, tsshm->msecAccept);
		    printf("MSEC_PER_DEG = %9.4f\n", (double)n*MSEC_PER_DEG);
		    printf("m3Cmd = %d = %s  m3State = %d = %s\n", tsshm->m3Cmd,
			(tsshm->m3Cmd == CLOSE_M3_CMD)? "Closed":
			(tsshm->m3Cmd == OPEN_M3_CMD)? "open":"Unknown",
			tsshm->m3State,
			(tsshm->m3State == CLOSED_ST)? "Closed":
			(tsshm->m3State == OPEN_ST)? "open":"Unknown");
		    printf("lastMsec = %d, servoKnownDead = %d\n", lastMsec,
			servoKnownDead);
		    break;
		case 'a':
		    if(line[1] == 'a') {
			n = sscanf(ip, "%lf %lf", &newP, &newV);

			switch(n) {
			case 1:
			    newV = 0;
			case 2:		/* This is supposed to fall through */
			    if(line[2] == 's') {
				newP = tsshm->az/MSEC_PER_DEG + newP/3600;
				newV = tsshm->azVel/MSEC_PER_DEG + newV/3600;
			    } else {
				newP = tsshm->az/MSEC_PER_DEG + newP;
				newV = tsshm->azVel/MSEC_PER_DEG + newV;
			    }
			    newAz = 1;	/* Signal driver new az pos, vel. */
			    break;
			default:
			    printf("??\n");
			    break;
			}
			break;
		    } else if(line[1] == 'h') {
			if(sscanf(ip, "%s", word)) {
			    if(strcmp("on", word) == 0) {
				tsshm->sendEncToPalm = AZ_TO_PALM;
			    } else if(strcmp("off", word) == 0) {
				tsshm->sendEncToPalm = NOTHING_TO_PALM;
			    } else {
				printf("??\n");
			    }
			}
			break;
		    }
		    n = sscanf(ip, "%lf %lf", &newP, &newV);
		    switch(n) {
		    case 1:
			newV = 0;
		    case 2:		/* This is supposed to fall through */
			newAz = 1;	/* Signal driver new az pos, vel. */
			break;
		    case 0:
			if(sscanf(ip, "%s", word)) {
			    if(strcmp("on", word) == 0) {
				tsshm->msecCmd = tsshm->msec;
				usleep(10000);
				tsshm->azCmd = ON_CMD;
			    } else if(strcmp("off", word) == 0) {
				tsshm->azCmd = OFF_CMD;
			    } else {
				printf("??\n");
			    }
			}
			break;
		    default:
			printf("??\n");
			break;
		    }
		    break;
		case 'e':
		    if(line[1] == 'a') {
			n = sscanf(ip, "%lf %lf", &newP, &newV);

			switch(n) {
			case 1:
			    newV = 0;
			case 2:		/* This is supposed to fall through */
			    if(line[2] == 's') {
				newP = tsshm->el/MSEC_PER_DEG + newP/3600;
				newV = tsshm->elVel/MSEC_PER_DEG + newV/3600;
			    } else {
				newP = tsshm->el/MSEC_PER_DEG + newP;
				newV = tsshm->elVel/MSEC_PER_DEG + newV;
			    }
			    newEl = 1;	/* Signal driver new az pos, vel. */
			    break;
			default:
			    printf("??\n");
			    break;
			}
			break;
		    } else if(line[1] == 'h') {
			if(sscanf(ip, "%s", word)) {
			    if(strcmp("on", word) == 0) {
				tsshm->sendEncToPalm = EL_TO_PALM;
			    } else if(strcmp("off", word) == 0) {
				tsshm->sendEncToPalm = NOTHING_TO_PALM;
			    } else {
				printf("??\n");
			    }
			}
			break;
		    }
		    n = sscanf(ip, "%lf %lf", &newP, &newV);
		    switch(n) {
		    case 1:
			newV = 0;
		    case 2:		/* This is supposed to fall through */
			newEl = 1;	/* Signal driver */
			break;
		    case 0:
			if(sscanf(ip, "%s", word)) {
			    if(strcmp("on", word) == 0) {
				tsshm->msecCmd = tsshm->msec;
				usleep(10000);
				tsshm->elCmd = ON_CMD;
			    } else if(strcmp("off", word) == 0) {
				tsshm->msecCmd = tsshm->msec;
				tsshm->elCmd = OFF_CMD;
			    } else {
				printf("??\n");
			    }
			}
			break;
		    default:
			printf("??\n");
			break;
		    }
		    break;
		case 'm':
			if(sscanf(ip, "%s", word)) {
			    if(strcmp("open", word) == 0)
				tsshm->m3Cmd = OPEN_M3_CMD;
			    else if(strcmp("closed", word) == 0)
				tsshm->m3Cmd = CLOSE_M3_CMD;
			    else {
				printf("??\n");
			    }
			}
		    break;
		case 'q':
		    tsshm->azCmd = OFF_CMD;
		    tsshm->elCmd = OFF_CMD;
		    write_history(HIST_FILE);
		    return(0);
		default:
		    printf("??\n");
		    break;
		}

		/* end of the main loop */
	    }
	    free(line);
	}
	fprintf(stderr, "!!! Control should not get here !!!\n");
/*	shm_unlink(TSSHMNAME); */
	return(0);
}
int main(int argc, char *argv[]) {
    char *ip, *cp;
    char ttybuf[TTYLEN], cmd[CMDLEN];
    COMMAND *cmdp;
    int i;
    
    config.gpibPresent = 1;
    gpibDebug = 0;
    vector_voltmeter.address = HP8508;
    vector_voltmeter2.address = HP8508+1;
    for (i=1; i<argc; i++) {
      if (present(argv[i],"-a")) {
        if (++i < argc) {
          sscanf(argv[i],"%d",&vector_voltmeter.address);
	} else {
          vvUsage(argv[0]);
	}
      }
      if (present(argv[i],"-b")) {
        if (++i < argc) {
          sscanf(argv[i],"%d",&vector_voltmeter2.address);
	} else {
          vvUsage(argv[0]);
	}
      }
      if (present(argv[i],"-h")) {
        vvUsage(argv[0]);
      }
      if (present(argv[i],"-d")) {
        gpibDebug = 1;
      }
    }
    readline_initialize_everything();
    read_history(HIST_FILE);
    vector_voltmeter.present = 1;
    vector_voltmeter2.present = 1;
    /* find controller and setup the GPIB bus */
#if __Lynx__
    brd0 = call_ibfind(GPIBDEVICE);
  #if !__powerpc__
    call_ibsic(brd0);
    call_ibsre(brd0,1);
  #endif
#endif

    /* open up the device */
    WARN("opening GPIB device at address = %d....\n",
            vector_voltmeter.address);
    vector_voltmeter.dd = setupGPIBdevice(vector_voltmeter.address);
    if (vector_voltmeter.dd < 0) {
      WARN("Failed\n");
      exit(0);
    } else {
      WARN("Succeeded.  Descriptor = %d\n",vector_voltmeter.dd);
      clearVectorVoltmeter(vector_voltmeter.dd);
    }
    WARN("opening GPIB device at address = %d....\n",
            vector_voltmeter2.address);
    vector_voltmeter2.dd = setupGPIBdevice(vector_voltmeter2.address);
    if (vector_voltmeter2.dd < 0) {
      WARN("Failed\n");
      exit(0);
    } else {
      WARN("Succeeded.  Descriptor = %d\n",vector_voltmeter2.dd);
      clearVectorVoltmeter(vector_voltmeter2.dd);
    }
    whichVVMdd = vector_voltmeter.dd;
    whichVVM = 1;

    /* Now the main program is a loop, reading and executing commands */
    for (;;) {
            sprintf(ttybuf,"vectorVoltmeter%d@addr%d> ",whichVVM,
		    addressVVM(whichVVMdd));
            ip = readline(ttybuf);
            add_history(ip);
            if (ip[0] == '!') {
              system(++ip);
              continue;
            }
	    while((*ip == ' ' || *ip == '\t') && *ip != '\n')
		ip++;
	    for(cp = cmd; cp < &cmd[CMDLEN]; cp++) {
		if((*cp = *ip++) == ' ' || *cp == ',' ||
		*cp == '\t' || *cp == '\n' || *cp == 0)
		    break;
	    }
	    *cp = 0;
	    if (cp >= &cmd[CMDLEN]) {
		WARN( "cmd %s too long\n", cmd);
		continue;
	    }
	    if (cmd[0] == 0)
		continue;

            /* 'ip' now contains only the arguments to the command */
	    for (cmdp = cmds; cmdp < &cmds[NUMCMDS]; cmdp++) {
               /* look up the command */
	       if(*cmd == *cmdp->cmd && strcmp(cmd + 1, cmdp->cmd + 1) == 0) {
	          cmdp->sub(ip);
	          break;
               }
	    } /* end of 'for' loop */
	    if (cmdp >= &cmds[NUMCMDS]) {
              WARN("Unrecognized cmd = %s\n", cmd);
	    }
#if __Lynx__
            free(ip);
#endif
	  } /* end of infinite 'for' command loop */
    call_ibloc(vector_voltmeter.dd);
    call_ibloc(vector_voltmeter2.dd);
    write_history(HIST_FILE);
    return(0);
}